2 * Linux host USB redirector
4 * Copyright (c) 2005 Fabrice Bellard
6 * Copyright (c) 2008 Max Krasnyansky
7 * Support for host device auto connect & disconnect
8 * Major rewrite to support fully async operation
10 * Copyright 2008 TJ <linux@tjworld.net>
11 * Added flexible support for /dev/bus/usb /sys/bus/usb/devices in addition
12 * to the legacy /proc/bus/usb USB device discovery and handling
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this software and associated documentation files (the "Software"), to deal
16 * in the Software without restriction, including without limitation the rights
17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 * copies of the Software, and to permit persons to whom the Software is
19 * furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33 #include "qemu-common.h"
34 #include "qemu/timer.h"
35 #include "monitor/monitor.h"
36 #include "sysemu/sysemu.h"
40 #include <sys/ioctl.h>
42 #include <linux/usbdevice_fs.h>
43 #include <linux/version.h>
45 #include "hw/usb/desc.h"
46 #include "hw/usb/host.h"
48 #ifdef CONFIG_USB_LIBUSB
49 # define DEVNAME "usb-host-linux"
51 # define DEVNAME "usb-host"
54 /* We redefine it to avoid version problems */
55 struct usb_ctrltransfer
{
65 typedef int USBScanFunc(void *opaque
, int bus_num
, int addr
, const char *port
,
66 int class_id
, int vendor_id
, int product_id
,
67 const char *product_name
, int speed
);
72 #define DPRINTF printf
77 #define PRODUCT_NAME_SZ 32
78 #define MAX_PORTLEN 16
80 /* endpoint association data */
81 #define ISO_FRAME_DESC_PER_URB 32
83 /* devio.c limits single requests to 16k */
84 #define MAX_USBFS_BUFFER_SIZE 16384
86 typedef struct AsyncURB AsyncURB
;
97 enum USBHostDeviceOptions
{
98 USB_HOST_OPT_PIPELINE
,
101 typedef struct USBHostDevice
{
110 uint32_t iso_urb_count
;
115 struct endp_data ep_in
[USB_MAX_ENDPOINTS
];
116 struct endp_data ep_out
[USB_MAX_ENDPOINTS
];
117 QLIST_HEAD(, AsyncURB
) aurbs
;
119 /* Host side address */
122 char port
[MAX_PORTLEN
];
123 struct USBAutoFilter match
;
127 QTAILQ_ENTRY(USBHostDevice
) next
;
130 static QTAILQ_HEAD(, USBHostDevice
) hostdevs
= QTAILQ_HEAD_INITIALIZER(hostdevs
);
132 static int usb_host_close(USBHostDevice
*dev
);
133 static void usb_host_auto_check(void *unused
);
134 static int usb_host_read_file(char *line
, size_t line_size
,
135 const char *device_file
, const char *device_name
);
136 static void usb_linux_update_endp_table(USBHostDevice
*s
);
138 static int usb_host_usbfs_type(USBHostDevice
*s
, USBPacket
*p
)
140 static const int usbfs
[] = {
141 [USB_ENDPOINT_XFER_CONTROL
] = USBDEVFS_URB_TYPE_CONTROL
,
142 [USB_ENDPOINT_XFER_ISOC
] = USBDEVFS_URB_TYPE_ISO
,
143 [USB_ENDPOINT_XFER_BULK
] = USBDEVFS_URB_TYPE_BULK
,
144 [USB_ENDPOINT_XFER_INT
] = USBDEVFS_URB_TYPE_INTERRUPT
,
146 uint8_t type
= p
->ep
->type
;
147 assert(type
< ARRAY_SIZE(usbfs
));
151 static int usb_host_do_reset(USBHostDevice
*dev
)
157 gettimeofday(&s
, NULL
);
158 ret
= ioctl(dev
->fd
, USBDEVFS_RESET
);
159 gettimeofday(&e
, NULL
);
160 usecs
= (e
.tv_sec
- s
.tv_sec
) * 1000000;
161 usecs
+= e
.tv_usec
- s
.tv_usec
;
162 if (usecs
> 1000000) {
163 /* more than a second, something is fishy, broken usb device? */
164 fprintf(stderr
, "husb: device %d:%d reset took %d.%06d seconds\n",
165 dev
->bus_num
, dev
->addr
, usecs
/ 1000000, usecs
% 1000000);
170 static struct endp_data
*get_endp(USBHostDevice
*s
, int pid
, int ep
)
172 struct endp_data
*eps
= pid
== USB_TOKEN_IN
? s
->ep_in
: s
->ep_out
;
173 assert(pid
== USB_TOKEN_IN
|| pid
== USB_TOKEN_OUT
);
174 assert(ep
> 0 && ep
<= USB_MAX_ENDPOINTS
);
178 static int is_isoc(USBHostDevice
*s
, int pid
, int ep
)
180 return usb_ep_get_type(&s
->dev
, pid
, ep
) == USB_ENDPOINT_XFER_ISOC
;
183 static int is_valid(USBHostDevice
*s
, int pid
, int ep
)
185 return usb_ep_get_type(&s
->dev
, pid
, ep
) != USB_ENDPOINT_XFER_INVALID
;
188 static int is_halted(USBHostDevice
*s
, int pid
, int ep
)
190 return get_endp(s
, pid
, ep
)->halted
;
193 static void clear_halt(USBHostDevice
*s
, int pid
, int ep
)
195 trace_usb_host_ep_clear_halt(s
->bus_num
, s
->addr
, ep
);
196 get_endp(s
, pid
, ep
)->halted
= 0;
199 static void set_halt(USBHostDevice
*s
, int pid
, int ep
)
202 trace_usb_host_ep_set_halt(s
->bus_num
, s
->addr
, ep
);
203 get_endp(s
, pid
, ep
)->halted
= 1;
207 static int is_iso_started(USBHostDevice
*s
, int pid
, int ep
)
209 return get_endp(s
, pid
, ep
)->iso_started
;
212 static void clear_iso_started(USBHostDevice
*s
, int pid
, int ep
)
214 trace_usb_host_iso_stop(s
->bus_num
, s
->addr
, ep
);
215 get_endp(s
, pid
, ep
)->iso_started
= 0;
218 static void set_iso_started(USBHostDevice
*s
, int pid
, int ep
)
220 struct endp_data
*e
= get_endp(s
, pid
, ep
);
222 trace_usb_host_iso_start(s
->bus_num
, s
->addr
, ep
);
223 if (!e
->iso_started
) {
229 static int change_iso_inflight(USBHostDevice
*s
, int pid
, int ep
, int value
)
231 struct endp_data
*e
= get_endp(s
, pid
, ep
);
233 e
->inflight
+= value
;
237 static void set_iso_urb(USBHostDevice
*s
, int pid
, int ep
, AsyncURB
*iso_urb
)
239 get_endp(s
, pid
, ep
)->iso_urb
= iso_urb
;
242 static AsyncURB
*get_iso_urb(USBHostDevice
*s
, int pid
, int ep
)
244 return get_endp(s
, pid
, ep
)->iso_urb
;
247 static void set_iso_urb_idx(USBHostDevice
*s
, int pid
, int ep
, int i
)
249 get_endp(s
, pid
, ep
)->iso_urb_idx
= i
;
252 static int get_iso_urb_idx(USBHostDevice
*s
, int pid
, int ep
)
254 return get_endp(s
, pid
, ep
)->iso_urb_idx
;
257 static void set_iso_buffer_used(USBHostDevice
*s
, int pid
, int ep
, int i
)
259 get_endp(s
, pid
, ep
)->iso_buffer_used
= i
;
262 static int get_iso_buffer_used(USBHostDevice
*s
, int pid
, int ep
)
264 return get_endp(s
, pid
, ep
)->iso_buffer_used
;
269 * We always allocate iso packet descriptors even for bulk transfers
270 * to simplify allocation and casts.
274 struct usbdevfs_urb urb
;
275 struct usbdevfs_iso_packet_desc isocpd
[ISO_FRAME_DESC_PER_URB
];
277 QLIST_ENTRY(AsyncURB
) next
;
279 /* For regular async urbs */
281 int more
; /* large transfer, more urbs follow */
283 /* For buffered iso handling */
284 int iso_frame_idx
; /* -1 means in flight */
287 static AsyncURB
*async_alloc(USBHostDevice
*s
)
289 AsyncURB
*aurb
= g_malloc0(sizeof(AsyncURB
));
291 QLIST_INSERT_HEAD(&s
->aurbs
, aurb
, next
);
295 static void async_free(AsyncURB
*aurb
)
297 QLIST_REMOVE(aurb
, next
);
301 static void do_disconnect(USBHostDevice
*s
)
304 usb_host_auto_check(NULL
);
307 static void async_complete(void *opaque
)
309 USBHostDevice
*s
= opaque
;
316 int r
= ioctl(s
->fd
, USBDEVFS_REAPURBNDELAY
, &aurb
);
318 if (errno
== EAGAIN
) {
320 /* indicates possible latency issues */
321 trace_usb_host_iso_many_urbs(s
->bus_num
, s
->addr
, urbs
);
325 if (errno
== ENODEV
) {
327 trace_usb_host_disconnect(s
->bus_num
, s
->addr
);
333 perror("USBDEVFS_REAPURBNDELAY");
337 DPRINTF("husb: async completed. aurb %p status %d alen %d\n",
338 aurb
, aurb
->urb
.status
, aurb
->urb
.actual_length
);
340 /* If this is a buffered iso urb mark it as complete and don't do
341 anything else (it is handled further in usb_host_handle_iso_data) */
342 if (aurb
->iso_frame_idx
== -1) {
344 int pid
= (aurb
->urb
.endpoint
& USB_DIR_IN
) ?
345 USB_TOKEN_IN
: USB_TOKEN_OUT
;
346 int ep
= aurb
->urb
.endpoint
& 0xf;
347 if (aurb
->urb
.status
== -EPIPE
) {
348 set_halt(s
, pid
, ep
);
350 aurb
->iso_frame_idx
= 0;
352 inflight
= change_iso_inflight(s
, pid
, ep
, -1);
353 if (inflight
== 0 && is_iso_started(s
, pid
, ep
)) {
354 /* can be latency issues, or simply end of stream */
355 trace_usb_host_iso_out_of_bufs(s
->bus_num
, s
->addr
, ep
);
361 trace_usb_host_urb_complete(s
->bus_num
, s
->addr
, aurb
, aurb
->urb
.status
,
362 aurb
->urb
.actual_length
, aurb
->more
);
365 switch (aurb
->urb
.status
) {
367 p
->actual_length
+= aurb
->urb
.actual_length
;
369 /* Clear previous ASYNC status */
370 p
->status
= USB_RET_SUCCESS
;
375 set_halt(s
, p
->pid
, p
->ep
->nr
);
376 p
->status
= USB_RET_STALL
;
380 p
->status
= USB_RET_BABBLE
;
384 p
->status
= USB_RET_IOERROR
;
388 if (aurb
->urb
.type
== USBDEVFS_URB_TYPE_CONTROL
) {
389 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
390 p
->status
, aurb
->urb
.actual_length
);
391 usb_generic_async_ctrl_complete(&s
->dev
, p
);
392 } else if (!aurb
->more
) {
393 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
394 p
->status
, aurb
->urb
.actual_length
);
395 usb_packet_complete(&s
->dev
, p
);
403 static void usb_host_async_cancel(USBDevice
*dev
, USBPacket
*p
)
405 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
408 trace_usb_host_req_canceled(s
->bus_num
, s
->addr
, p
);
410 QLIST_FOREACH(aurb
, &s
->aurbs
, next
) {
411 if (p
!= aurb
->packet
) {
415 trace_usb_host_urb_canceled(s
->bus_num
, s
->addr
, aurb
);
417 /* Mark it as dead (see async_complete above) */
420 int r
= ioctl(s
->fd
, USBDEVFS_DISCARDURB
, aurb
);
422 DPRINTF("husb: async. discard urb failed errno %d\n", errno
);
427 static int usb_host_open_device(int bus
, int addr
)
429 const char *usbfs
= NULL
;
434 rc
= stat("/dev/bus/usb", &st
);
435 if (rc
== 0 && S_ISDIR(st
.st_mode
)) {
436 /* udev-created device nodes available */
437 usbfs
= "/dev/bus/usb";
439 /* fallback: usbfs mounted below /proc */
440 usbfs
= "/proc/bus/usb";
443 snprintf(filename
, sizeof(filename
), "%s/%03d/%03d",
445 fd
= open(filename
, O_RDWR
| O_NONBLOCK
);
447 fprintf(stderr
, "husb: open %s: %s\n", filename
, strerror(errno
));
452 static int usb_host_claim_port(USBHostDevice
*s
)
454 #ifdef USBDEVFS_CLAIM_PORT
455 char *h
, hub_name
[64], line
[1024];
458 snprintf(hub_name
, sizeof(hub_name
), "%d-%s",
459 s
->match
.bus_num
, s
->match
.port
);
461 /* try strip off last ".$portnr" to get hub */
462 h
= strrchr(hub_name
, '.');
464 s
->hub_port
= atoi(h
+1);
467 /* no dot in there -> it is the root hub */
468 snprintf(hub_name
, sizeof(hub_name
), "usb%d",
470 s
->hub_port
= atoi(s
->match
.port
);
473 if (!usb_host_read_file(line
, sizeof(line
), "devnum",
477 if (sscanf(line
, "%d", &hub_addr
) != 1) {
481 s
->hub_fd
= usb_host_open_device(s
->match
.bus_num
, hub_addr
);
486 ret
= ioctl(s
->hub_fd
, USBDEVFS_CLAIM_PORT
, &s
->hub_port
);
493 trace_usb_host_claim_port(s
->match
.bus_num
, hub_addr
, s
->hub_port
);
500 static void usb_host_release_port(USBHostDevice
*s
)
502 if (s
->hub_fd
== -1) {
505 #ifdef USBDEVFS_RELEASE_PORT
506 ioctl(s
->hub_fd
, USBDEVFS_RELEASE_PORT
, &s
->hub_port
);
512 static int usb_host_disconnect_ifaces(USBHostDevice
*dev
, int nb_interfaces
)
514 /* earlier Linux 2.4 do not support that */
515 #ifdef USBDEVFS_DISCONNECT
516 struct usbdevfs_ioctl ctrl
;
519 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
520 ctrl
.ioctl_code
= USBDEVFS_DISCONNECT
;
521 ctrl
.ifno
= interface
;
523 ret
= ioctl(dev
->fd
, USBDEVFS_IOCTL
, &ctrl
);
524 if (ret
< 0 && errno
!= ENODATA
) {
525 perror("USBDEVFS_DISCONNECT");
533 static int usb_linux_get_num_interfaces(USBHostDevice
*s
)
535 char device_name
[64], line
[1024];
536 int num_interfaces
= 0;
538 sprintf(device_name
, "%d-%s", s
->bus_num
, s
->port
);
539 if (!usb_host_read_file(line
, sizeof(line
), "bNumInterfaces",
543 if (sscanf(line
, "%d", &num_interfaces
) != 1) {
546 return num_interfaces
;
549 static int usb_host_claim_interfaces(USBHostDevice
*dev
, int configuration
)
551 const char *op
= NULL
;
552 int dev_descr_len
, config_descr_len
;
553 int interface
, nb_interfaces
;
556 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
557 dev
->dev
.altsetting
[i
] = 0;
560 if (configuration
== 0) { /* address state - ignore */
561 dev
->dev
.ninterfaces
= 0;
562 dev
->dev
.configuration
= 0;
566 DPRINTF("husb: claiming interfaces. config %d\n", configuration
);
569 dev_descr_len
= dev
->descr
[0];
570 if (dev_descr_len
> dev
->descr_len
) {
571 fprintf(stderr
, "husb: update iface failed. descr too short\n");
576 while (i
< dev
->descr_len
) {
577 DPRINTF("husb: i is %d, descr_len is %d, dl %d, dt %d\n",
579 dev
->descr
[i
], dev
->descr
[i
+1]);
581 if (dev
->descr
[i
+1] != USB_DT_CONFIG
) {
585 config_descr_len
= dev
->descr
[i
];
587 DPRINTF("husb: config #%d need %d\n", dev
->descr
[i
+ 5], configuration
);
589 if (configuration
== dev
->descr
[i
+ 5]) {
590 configuration
= dev
->descr
[i
+ 5];
594 i
+= config_descr_len
;
597 if (i
>= dev
->descr_len
) {
599 "husb: update iface failed. no matching configuration\n");
602 nb_interfaces
= dev
->descr
[i
+ 4];
604 if (usb_host_disconnect_ifaces(dev
, nb_interfaces
) < 0) {
608 /* XXX: only grab if all interfaces are free */
609 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
610 op
= "USBDEVFS_CLAIMINTERFACE";
611 ret
= ioctl(dev
->fd
, USBDEVFS_CLAIMINTERFACE
, &interface
);
617 trace_usb_host_claim_interfaces(dev
->bus_num
, dev
->addr
,
618 nb_interfaces
, configuration
);
620 dev
->dev
.ninterfaces
= nb_interfaces
;
621 dev
->dev
.configuration
= configuration
;
625 if (errno
== ENODEV
) {
632 static int usb_host_release_interfaces(USBHostDevice
*s
)
636 trace_usb_host_release_interfaces(s
->bus_num
, s
->addr
);
638 for (i
= 0; i
< s
->dev
.ninterfaces
; i
++) {
639 ret
= ioctl(s
->fd
, USBDEVFS_RELEASEINTERFACE
, &i
);
641 perror("USBDEVFS_RELEASEINTERFACE");
648 static void usb_host_handle_reset(USBDevice
*dev
)
650 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
652 trace_usb_host_reset(s
->bus_num
, s
->addr
);
654 usb_host_do_reset(s
);;
656 usb_host_claim_interfaces(s
, 0);
657 usb_linux_update_endp_table(s
);
660 static void usb_host_handle_destroy(USBDevice
*dev
)
662 USBHostDevice
*s
= (USBHostDevice
*)dev
;
664 usb_host_release_port(s
);
666 QTAILQ_REMOVE(&hostdevs
, s
, next
);
667 qemu_remove_exit_notifier(&s
->exit
);
670 /* iso data is special, we need to keep enough urbs in flight to make sure
671 that the controller never runs out of them, otherwise the device will
672 likely suffer a buffer underrun / overrun. */
673 static AsyncURB
*usb_host_alloc_iso(USBHostDevice
*s
, int pid
, uint8_t ep
)
676 int i
, j
, len
= usb_ep_get_max_packet_size(&s
->dev
, pid
, ep
);
678 aurb
= g_malloc0(s
->iso_urb_count
* sizeof(*aurb
));
679 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
680 aurb
[i
].urb
.endpoint
= ep
;
681 aurb
[i
].urb
.buffer_length
= ISO_FRAME_DESC_PER_URB
* len
;
682 aurb
[i
].urb
.buffer
= g_malloc(aurb
[i
].urb
.buffer_length
);
683 aurb
[i
].urb
.type
= USBDEVFS_URB_TYPE_ISO
;
684 aurb
[i
].urb
.flags
= USBDEVFS_URB_ISO_ASAP
;
685 aurb
[i
].urb
.number_of_packets
= ISO_FRAME_DESC_PER_URB
;
686 for (j
= 0 ; j
< ISO_FRAME_DESC_PER_URB
; j
++)
687 aurb
[i
].urb
.iso_frame_desc
[j
].length
= len
;
688 if (pid
== USB_TOKEN_IN
) {
689 aurb
[i
].urb
.endpoint
|= 0x80;
690 /* Mark as fully consumed (idle) */
691 aurb
[i
].iso_frame_idx
= ISO_FRAME_DESC_PER_URB
;
694 set_iso_urb(s
, pid
, ep
, aurb
);
699 static void usb_host_stop_n_free_iso(USBHostDevice
*s
, int pid
, uint8_t ep
)
702 int i
, ret
, killed
= 0, free
= 1;
704 aurb
= get_iso_urb(s
, pid
, ep
);
709 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
711 if (aurb
[i
].iso_frame_idx
== -1) {
712 ret
= ioctl(s
->fd
, USBDEVFS_DISCARDURB
, &aurb
[i
]);
714 perror("USBDEVFS_DISCARDURB");
722 /* Make sure any urbs we've killed are reaped before we free them */
727 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
728 g_free(aurb
[i
].urb
.buffer
);
734 printf("husb: leaking iso urbs because of discard failure\n");
735 set_iso_urb(s
, pid
, ep
, NULL
);
736 set_iso_urb_idx(s
, pid
, ep
, 0);
737 clear_iso_started(s
, pid
, ep
);
740 static void urb_status_to_usb_ret(int status
, USBPacket
*p
)
744 p
->status
= USB_RET_STALL
;
747 p
->status
= USB_RET_BABBLE
;
750 p
->status
= USB_RET_IOERROR
;
754 static void usb_host_handle_iso_data(USBHostDevice
*s
, USBPacket
*p
, int in
)
757 int i
, j
, max_packet_size
, offset
, len
;
760 max_packet_size
= p
->ep
->max_packet_size
;
761 if (max_packet_size
== 0) {
762 p
->status
= USB_RET_NAK
;
766 aurb
= get_iso_urb(s
, p
->pid
, p
->ep
->nr
);
768 aurb
= usb_host_alloc_iso(s
, p
->pid
, p
->ep
->nr
);
771 i
= get_iso_urb_idx(s
, p
->pid
, p
->ep
->nr
);
772 j
= aurb
[i
].iso_frame_idx
;
773 if (j
>= 0 && j
< ISO_FRAME_DESC_PER_URB
) {
775 /* Check urb status */
776 if (aurb
[i
].urb
.status
) {
777 urb_status_to_usb_ret(aurb
[i
].urb
.status
, p
);
778 /* Move to the next urb */
779 aurb
[i
].iso_frame_idx
= ISO_FRAME_DESC_PER_URB
- 1;
780 /* Check frame status */
781 } else if (aurb
[i
].urb
.iso_frame_desc
[j
].status
) {
782 urb_status_to_usb_ret(aurb
[i
].urb
.iso_frame_desc
[j
].status
, p
);
783 /* Check the frame fits */
784 } else if (aurb
[i
].urb
.iso_frame_desc
[j
].actual_length
786 printf("husb: received iso data is larger then packet\n");
787 p
->status
= USB_RET_BABBLE
;
788 /* All good copy data over */
790 len
= aurb
[i
].urb
.iso_frame_desc
[j
].actual_length
;
791 buf
= aurb
[i
].urb
.buffer
+
792 j
* aurb
[i
].urb
.iso_frame_desc
[0].length
;
793 usb_packet_copy(p
, buf
, len
);
797 offset
= (j
== 0) ? 0 : get_iso_buffer_used(s
, p
->pid
, p
->ep
->nr
);
799 /* Check the frame fits */
800 if (len
> max_packet_size
) {
801 printf("husb: send iso data is larger then max packet size\n");
802 p
->status
= USB_RET_NAK
;
806 /* All good copy data over */
807 usb_packet_copy(p
, aurb
[i
].urb
.buffer
+ offset
, len
);
808 aurb
[i
].urb
.iso_frame_desc
[j
].length
= len
;
810 set_iso_buffer_used(s
, p
->pid
, p
->ep
->nr
, offset
);
812 /* Start the stream once we have buffered enough data */
813 if (!is_iso_started(s
, p
->pid
, p
->ep
->nr
) && i
== 1 && j
== 8) {
814 set_iso_started(s
, p
->pid
, p
->ep
->nr
);
817 aurb
[i
].iso_frame_idx
++;
818 if (aurb
[i
].iso_frame_idx
== ISO_FRAME_DESC_PER_URB
) {
819 i
= (i
+ 1) % s
->iso_urb_count
;
820 set_iso_urb_idx(s
, p
->pid
, p
->ep
->nr
, i
);
824 set_iso_started(s
, p
->pid
, p
->ep
->nr
);
826 DPRINTF("hubs: iso out error no free buffer, dropping packet\n");
830 if (is_iso_started(s
, p
->pid
, p
->ep
->nr
)) {
831 /* (Re)-submit all fully consumed / filled urbs */
832 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
833 if (aurb
[i
].iso_frame_idx
== ISO_FRAME_DESC_PER_URB
) {
834 if (ioctl(s
->fd
, USBDEVFS_SUBMITURB
, &aurb
[i
]) < 0) {
835 perror("USBDEVFS_SUBMITURB");
836 if (!in
|| p
->status
== USB_RET_SUCCESS
) {
839 p
->status
= USB_RET_NAK
;
843 p
->status
= USB_RET_STALL
;
848 aurb
[i
].iso_frame_idx
= -1;
849 change_iso_inflight(s
, p
->pid
, p
->ep
->nr
, 1);
855 static void usb_host_handle_data(USBDevice
*dev
, USBPacket
*p
)
857 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
858 struct usbdevfs_urb
*urb
;
860 int ret
, rem
, prem
, v
;
864 trace_usb_host_req_data(s
->bus_num
, s
->addr
, p
,
865 p
->pid
== USB_TOKEN_IN
,
866 p
->ep
->nr
, p
->iov
.size
);
868 if (!is_valid(s
, p
->pid
, p
->ep
->nr
)) {
869 p
->status
= USB_RET_NAK
;
870 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
871 p
->status
, p
->actual_length
);
875 if (p
->pid
== USB_TOKEN_IN
) {
876 ep
= p
->ep
->nr
| 0x80;
881 if (is_halted(s
, p
->pid
, p
->ep
->nr
)) {
882 unsigned int arg
= ep
;
883 ret
= ioctl(s
->fd
, USBDEVFS_CLEAR_HALT
, &arg
);
885 perror("USBDEVFS_CLEAR_HALT");
886 p
->status
= USB_RET_NAK
;
887 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
888 p
->status
, p
->actual_length
);
891 clear_halt(s
, p
->pid
, p
->ep
->nr
);
894 if (is_isoc(s
, p
->pid
, p
->ep
->nr
)) {
895 usb_host_handle_iso_data(s
, p
, p
->pid
== USB_TOKEN_IN
);
904 if (prem
== 0 && rem
> 0) {
905 assert(v
< p
->iov
.niov
);
906 prem
= p
->iov
.iov
[v
].iov_len
;
907 pbuf
= p
->iov
.iov
[v
].iov_base
;
911 aurb
= async_alloc(s
);
916 urb
->type
= usb_host_usbfs_type(s
, p
);
917 urb
->usercontext
= s
;
919 urb
->buffer_length
= prem
;
921 if (urb
->buffer_length
> MAX_USBFS_BUFFER_SIZE
) {
922 urb
->buffer_length
= MAX_USBFS_BUFFER_SIZE
;
924 pbuf
+= urb
->buffer_length
;
925 prem
-= urb
->buffer_length
;
926 rem
-= urb
->buffer_length
;
931 trace_usb_host_urb_submit(s
->bus_num
, s
->addr
, aurb
,
932 urb
->buffer_length
, aurb
->more
);
933 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
935 DPRINTF("husb: data submit: ep 0x%x, len %u, more %d, packet %p, aurb %p\n",
936 urb
->endpoint
, urb
->buffer_length
, aurb
->more
, p
, aurb
);
939 perror("USBDEVFS_SUBMITURB");
944 p
->status
= USB_RET_NAK
;
945 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
946 p
->status
, p
->actual_length
);
950 p
->status
= USB_RET_STALL
;
951 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
952 p
->status
, p
->actual_length
);
958 p
->status
= USB_RET_ASYNC
;
961 static int ctrl_error(void)
963 if (errno
== ETIMEDOUT
) {
966 return USB_RET_STALL
;
970 static void usb_host_set_address(USBHostDevice
*s
, int addr
)
972 trace_usb_host_set_address(s
->bus_num
, s
->addr
, addr
);
976 static void usb_host_set_config(USBHostDevice
*s
, int config
, USBPacket
*p
)
980 trace_usb_host_set_config(s
->bus_num
, s
->addr
, config
);
982 usb_host_release_interfaces(s
);
985 ret
= ioctl(s
->fd
, USBDEVFS_SETCONFIGURATION
, &config
);
987 DPRINTF("husb: ctrl set config %d ret %d errno %d\n", config
, ret
, errno
);
989 if (ret
< 0 && errno
== EBUSY
&& first
) {
990 /* happens if usb device is in use by host drivers */
991 int count
= usb_linux_get_num_interfaces(s
);
993 DPRINTF("husb: busy -> disconnecting %d interfaces\n", count
);
994 usb_host_disconnect_ifaces(s
, count
);
1001 p
->status
= ctrl_error();
1004 usb_host_claim_interfaces(s
, config
);
1005 usb_linux_update_endp_table(s
);
1008 static void usb_host_set_interface(USBHostDevice
*s
, int iface
, int alt
,
1011 struct usbdevfs_setinterface si
;
1014 trace_usb_host_set_interface(s
->bus_num
, s
->addr
, iface
, alt
);
1016 for (i
= 1; i
<= USB_MAX_ENDPOINTS
; i
++) {
1017 if (is_isoc(s
, USB_TOKEN_IN
, i
)) {
1018 usb_host_stop_n_free_iso(s
, USB_TOKEN_IN
, i
);
1020 if (is_isoc(s
, USB_TOKEN_OUT
, i
)) {
1021 usb_host_stop_n_free_iso(s
, USB_TOKEN_OUT
, i
);
1025 if (iface
>= USB_MAX_INTERFACES
) {
1026 p
->status
= USB_RET_STALL
;
1030 si
.interface
= iface
;
1031 si
.altsetting
= alt
;
1032 ret
= ioctl(s
->fd
, USBDEVFS_SETINTERFACE
, &si
);
1034 DPRINTF("husb: ctrl set iface %d altset %d ret %d errno %d\n",
1035 iface
, alt
, ret
, errno
);
1038 p
->status
= ctrl_error();
1042 s
->dev
.altsetting
[iface
] = alt
;
1043 usb_linux_update_endp_table(s
);
1046 static void usb_host_handle_control(USBDevice
*dev
, USBPacket
*p
,
1047 int request
, int value
, int index
, int length
, uint8_t *data
)
1049 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
1050 struct usbdevfs_urb
*urb
;
1055 * Process certain standard device requests.
1056 * These are infrequent and are processed synchronously.
1059 /* Note request is (bRequestType << 8) | bRequest */
1060 trace_usb_host_req_control(s
->bus_num
, s
->addr
, p
, request
, value
, index
);
1063 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
1064 usb_host_set_address(s
, value
);
1065 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1068 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
1069 usb_host_set_config(s
, value
& 0xff, p
);
1070 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1073 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
1074 usb_host_set_interface(s
, index
, value
, p
);
1075 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1078 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
1079 if (value
== 0) { /* clear halt */
1080 int pid
= (index
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
1081 ioctl(s
->fd
, USBDEVFS_CLEAR_HALT
, &index
);
1082 clear_halt(s
, pid
, index
& 0x0f);
1083 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, 0);
1088 /* The rest are asynchronous */
1089 if (length
> sizeof(dev
->data_buf
)) {
1090 fprintf(stderr
, "husb: ctrl buffer too small (%d > %zu)\n",
1091 length
, sizeof(dev
->data_buf
));
1092 p
->status
= USB_RET_STALL
;
1096 aurb
= async_alloc(s
);
1100 * Setup ctrl transfer.
1102 * s->ctrl is laid out such that data buffer immediately follows
1103 * 'req' struct which is exactly what usbdevfs expects.
1107 urb
->type
= USBDEVFS_URB_TYPE_CONTROL
;
1108 urb
->endpoint
= p
->ep
->nr
;
1110 urb
->buffer
= &dev
->setup_buf
;
1111 urb
->buffer_length
= length
+ 8;
1113 urb
->usercontext
= s
;
1115 trace_usb_host_urb_submit(s
->bus_num
, s
->addr
, aurb
,
1116 urb
->buffer_length
, aurb
->more
);
1117 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
1119 DPRINTF("husb: submit ctrl. len %u aurb %p\n", urb
->buffer_length
, aurb
);
1122 DPRINTF("husb: submit failed. errno %d\n", errno
);
1127 p
->status
= USB_RET_NAK
;
1131 p
->status
= USB_RET_STALL
;
1137 p
->status
= USB_RET_ASYNC
;
1140 static void usb_linux_update_endp_table(USBHostDevice
*s
)
1142 static const char *tname
[] = {
1143 [USB_ENDPOINT_XFER_CONTROL
] = "control",
1144 [USB_ENDPOINT_XFER_ISOC
] = "isoc",
1145 [USB_ENDPOINT_XFER_BULK
] = "bulk",
1146 [USB_ENDPOINT_XFER_INT
] = "int",
1148 uint8_t devep
, type
;
1151 unsigned int i
, configuration
= -1, interface
= -1, altsetting
= -1;
1152 struct endp_data
*epd
;
1154 bool active
= false;
1156 usb_ep_reset(&s
->dev
);
1158 for (i
= 0;; i
+= d
->bLength
) {
1159 if (i
+2 >= s
->descr_len
) {
1162 d
= (void *)(s
->descr
+ i
);
1163 if (d
->bLength
< 2) {
1164 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1165 "descriptor too short");
1168 if (i
+ d
->bLength
> s
->descr_len
) {
1169 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1170 "descriptor too long");
1173 switch (d
->bDescriptorType
) {
1175 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1176 "invalid descriptor type");
1179 if (d
->bLength
< 0x12) {
1180 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1181 "device descriptor too short");
1184 v
= (d
->u
.device
.idVendor_hi
<< 8) | d
->u
.device
.idVendor_lo
;
1185 p
= (d
->u
.device
.idProduct_hi
<< 8) | d
->u
.device
.idProduct_lo
;
1186 trace_usb_host_parse_device(s
->bus_num
, s
->addr
, v
, p
);
1189 if (d
->bLength
< 0x09) {
1190 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1191 "config descriptor too short");
1194 configuration
= d
->u
.config
.bConfigurationValue
;
1195 active
= (configuration
== s
->dev
.configuration
);
1196 trace_usb_host_parse_config(s
->bus_num
, s
->addr
,
1197 configuration
, active
);
1199 case USB_DT_INTERFACE
:
1200 if (d
->bLength
< 0x09) {
1201 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1202 "interface descriptor too short");
1205 interface
= d
->u
.interface
.bInterfaceNumber
;
1206 altsetting
= d
->u
.interface
.bAlternateSetting
;
1207 active
= (configuration
== s
->dev
.configuration
) &&
1208 (altsetting
== s
->dev
.altsetting
[interface
]);
1209 trace_usb_host_parse_interface(s
->bus_num
, s
->addr
,
1210 interface
, altsetting
, active
);
1212 case USB_DT_ENDPOINT
:
1213 if (d
->bLength
< 0x07) {
1214 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1215 "endpoint descriptor too short");
1218 devep
= d
->u
.endpoint
.bEndpointAddress
;
1219 pid
= (devep
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
1222 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1223 "invalid endpoint address");
1227 type
= d
->u
.endpoint
.bmAttributes
& 0x3;
1228 mps
= d
->u
.endpoint
.wMaxPacketSize_lo
|
1229 (d
->u
.endpoint
.wMaxPacketSize_hi
<< 8);
1230 trace_usb_host_parse_endpoint(s
->bus_num
, s
->addr
, ep
,
1231 (devep
& USB_DIR_IN
) ? "in" : "out",
1232 tname
[type
], active
);
1235 usb_ep_set_max_packet_size(&s
->dev
, pid
, ep
, mps
);
1236 assert(usb_ep_get_type(&s
->dev
, pid
, ep
) ==
1237 USB_ENDPOINT_XFER_INVALID
);
1238 usb_ep_set_type(&s
->dev
, pid
, ep
, type
);
1239 usb_ep_set_ifnum(&s
->dev
, pid
, ep
, interface
);
1240 if ((s
->options
& (1 << USB_HOST_OPT_PIPELINE
)) &&
1241 (type
== USB_ENDPOINT_XFER_BULK
) &&
1242 (pid
== USB_TOKEN_OUT
)) {
1243 usb_ep_set_pipeline(&s
->dev
, pid
, ep
, true);
1246 epd
= get_endp(s
, pid
, ep
);
1252 trace_usb_host_parse_unknown(s
->bus_num
, s
->addr
,
1253 d
->bLength
, d
->bDescriptorType
);
1260 * Check if we can safely redirect a usb2 device to a usb1 virtual controller,
1261 * this function assumes this is safe, if:
1262 * 1) There are no isoc endpoints
1263 * 2) There are no interrupt endpoints with a max_packet_size > 64
1264 * Note bulk endpoints with a max_packet_size > 64 in theory also are not
1265 * usb1 compatible, but in practice this seems to work fine.
1267 static int usb_linux_full_speed_compat(USBHostDevice
*dev
)
1272 * usb_linux_update_endp_table only registers info about ep in the current
1273 * interface altsettings, so we need to parse the descriptors again.
1275 for (i
= 0; (i
+ 5) < dev
->descr_len
; i
+= dev
->descr
[i
]) {
1276 if (dev
->descr
[i
+ 1] == USB_DT_ENDPOINT
) {
1277 switch (dev
->descr
[i
+ 3] & 0x3) {
1278 case 0x00: /* CONTROL */
1280 case 0x01: /* ISO */
1282 case 0x02: /* BULK */
1284 case 0x03: /* INTERRUPT */
1285 packet_size
= dev
->descr
[i
+ 4] + (dev
->descr
[i
+ 5] << 8);
1286 if (packet_size
> 64)
1295 static int usb_host_open(USBHostDevice
*dev
, int bus_num
,
1296 int addr
, const char *port
,
1297 const char *prod_name
, int speed
)
1301 trace_usb_host_open_started(bus_num
, addr
);
1303 if (dev
->fd
!= -1) {
1307 fd
= usb_host_open_device(bus_num
, addr
);
1311 DPRINTF("husb: opened %s\n", buf
);
1313 dev
->bus_num
= bus_num
;
1315 pstrcpy(dev
->port
, sizeof(dev
->port
), port
);
1318 /* read the device description */
1319 dev
->descr_len
= read(fd
, dev
->descr
, sizeof(dev
->descr
));
1320 if (dev
->descr_len
<= 0) {
1321 perror("husb: reading device data failed");
1328 printf("=== begin dumping device descriptor data ===\n");
1329 for (x
= 0; x
< dev
->descr_len
; x
++) {
1330 printf("%02x ", dev
->descr
[x
]);
1332 printf("\n=== end dumping device descriptor data ===\n");
1337 /* start unconfigured -- we'll wait for the guest to set a configuration */
1338 if (!usb_host_claim_interfaces(dev
, 0)) {
1342 usb_ep_init(&dev
->dev
);
1343 usb_linux_update_endp_table(dev
);
1346 struct usbdevfs_connectinfo ci
;
1348 ret
= ioctl(fd
, USBDEVFS_CONNECTINFO
, &ci
);
1350 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
1355 speed
= USB_SPEED_LOW
;
1357 speed
= USB_SPEED_HIGH
;
1360 dev
->dev
.speed
= speed
;
1361 dev
->dev
.speedmask
= (1 << speed
);
1362 if (dev
->dev
.speed
== USB_SPEED_HIGH
&& usb_linux_full_speed_compat(dev
)) {
1363 dev
->dev
.speedmask
|= USB_SPEED_MASK_FULL
;
1366 trace_usb_host_open_success(bus_num
, addr
);
1368 if (!prod_name
|| prod_name
[0] == '\0') {
1369 snprintf(dev
->dev
.product_desc
, sizeof(dev
->dev
.product_desc
),
1370 "host:%d.%d", bus_num
, addr
);
1372 pstrcpy(dev
->dev
.product_desc
, sizeof(dev
->dev
.product_desc
),
1376 ret
= usb_device_attach(&dev
->dev
);
1381 /* USB devio uses 'write' flag to check for async completions */
1382 qemu_set_fd_handler(dev
->fd
, NULL
, async_complete
, dev
);
1387 trace_usb_host_open_failure(bus_num
, addr
);
1388 if (dev
->fd
!= -1) {
1395 static int usb_host_close(USBHostDevice
*dev
)
1399 if (dev
->fd
== -1) {
1403 trace_usb_host_close(dev
->bus_num
, dev
->addr
);
1405 qemu_set_fd_handler(dev
->fd
, NULL
, NULL
, NULL
);
1407 for (i
= 1; i
<= USB_MAX_ENDPOINTS
; i
++) {
1408 if (is_isoc(dev
, USB_TOKEN_IN
, i
)) {
1409 usb_host_stop_n_free_iso(dev
, USB_TOKEN_IN
, i
);
1411 if (is_isoc(dev
, USB_TOKEN_OUT
, i
)) {
1412 usb_host_stop_n_free_iso(dev
, USB_TOKEN_OUT
, i
);
1415 async_complete(dev
);
1417 if (dev
->dev
.attached
) {
1418 usb_device_detach(&dev
->dev
);
1420 usb_host_do_reset(dev
);
1426 static void usb_host_exit_notifier(struct Notifier
*n
, void *data
)
1428 USBHostDevice
*s
= container_of(n
, USBHostDevice
, exit
);
1430 usb_host_release_port(s
);
1432 usb_host_do_reset(s
);;
1437 * This is *NOT* about restoring state. We have absolutely no idea
1438 * what state the host device is in at the moment and whenever it is
1439 * still present in the first place. Attemping to contine where we
1440 * left off is impossible.
1442 * What we are going to to to here is emulate a surprise removal of
1443 * the usb device passed through, then kick host scan so the device
1444 * will get re-attached (and re-initialized by the guest) in case it
1447 * As the device removal will change the state of other devices (usb
1448 * host controller, most likely interrupt controller too) we have to
1449 * wait with it until *all* vmstate is loaded. Thus post_load just
1450 * kicks a bottom half which then does the actual work.
1452 static void usb_host_post_load_bh(void *opaque
)
1454 USBHostDevice
*dev
= opaque
;
1456 if (dev
->fd
!= -1) {
1457 usb_host_close(dev
);
1459 if (dev
->dev
.attached
) {
1460 usb_device_detach(&dev
->dev
);
1462 usb_host_auto_check(NULL
);
1465 static int usb_host_post_load(void *opaque
, int version_id
)
1467 USBHostDevice
*dev
= opaque
;
1469 qemu_bh_schedule(dev
->bh
);
1473 static int usb_host_initfn(USBDevice
*dev
)
1475 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
1477 dev
->flags
|= (1 << USB_DEV_FLAG_IS_HOST
);
1478 dev
->auto_attach
= 0;
1482 QTAILQ_INSERT_TAIL(&hostdevs
, s
, next
);
1483 s
->exit
.notify
= usb_host_exit_notifier
;
1484 qemu_add_exit_notifier(&s
->exit
);
1485 s
->bh
= qemu_bh_new(usb_host_post_load_bh
, s
);
1486 usb_host_auto_check(NULL
);
1488 if (s
->match
.bus_num
!= 0 && s
->match
.port
!= NULL
) {
1489 usb_host_claim_port(s
);
1491 add_boot_device_path(s
->bootindex
, &dev
->qdev
, NULL
);
1495 static const VMStateDescription vmstate_usb_host
= {
1498 .minimum_version_id
= 1,
1499 .post_load
= usb_host_post_load
,
1500 .fields
= (VMStateField
[]) {
1501 VMSTATE_USB_DEVICE(dev
, USBHostDevice
),
1502 VMSTATE_END_OF_LIST()
1506 static Property usb_host_dev_properties
[] = {
1507 DEFINE_PROP_UINT32("hostbus", USBHostDevice
, match
.bus_num
, 0),
1508 DEFINE_PROP_UINT32("hostaddr", USBHostDevice
, match
.addr
, 0),
1509 DEFINE_PROP_STRING("hostport", USBHostDevice
, match
.port
),
1510 DEFINE_PROP_HEX32("vendorid", USBHostDevice
, match
.vendor_id
, 0),
1511 DEFINE_PROP_HEX32("productid", USBHostDevice
, match
.product_id
, 0),
1512 DEFINE_PROP_UINT32("isobufs", USBHostDevice
, iso_urb_count
, 4),
1513 DEFINE_PROP_INT32("bootindex", USBHostDevice
, bootindex
, -1),
1514 DEFINE_PROP_BIT("pipeline", USBHostDevice
, options
,
1515 USB_HOST_OPT_PIPELINE
, true),
1516 DEFINE_PROP_END_OF_LIST(),
1519 static void usb_host_class_initfn(ObjectClass
*klass
, void *data
)
1521 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1522 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
1524 uc
->init
= usb_host_initfn
;
1525 uc
->product_desc
= "USB Host Device";
1526 uc
->cancel_packet
= usb_host_async_cancel
;
1527 uc
->handle_data
= usb_host_handle_data
;
1528 uc
->handle_control
= usb_host_handle_control
;
1529 uc
->handle_reset
= usb_host_handle_reset
;
1530 uc
->handle_destroy
= usb_host_handle_destroy
;
1531 dc
->vmsd
= &vmstate_usb_host
;
1532 dc
->props
= usb_host_dev_properties
;
1535 static const TypeInfo usb_host_dev_info
= {
1537 .parent
= TYPE_USB_DEVICE
,
1538 .instance_size
= sizeof(USBHostDevice
),
1539 .class_init
= usb_host_class_initfn
,
1542 static void usb_host_register_types(void)
1544 type_register_static(&usb_host_dev_info
);
1547 type_init(usb_host_register_types
)
1550 * Read sys file-system device file
1552 * @line address of buffer to put file contents in
1553 * @line_size size of line
1554 * @device_file path to device file (printf format string)
1555 * @device_name device being opened (inserted into device_file)
1557 * @return 0 failed, 1 succeeded ('line' contains data)
1559 static int usb_host_read_file(char *line
, size_t line_size
,
1560 const char *device_file
, const char *device_name
)
1564 char filename
[PATH_MAX
];
1566 snprintf(filename
, PATH_MAX
, "/sys/bus/usb/devices/%s/%s", device_name
,
1568 f
= fopen(filename
, "r");
1570 ret
= fgets(line
, line_size
, f
) != NULL
;
1578 * Use /sys/bus/usb/devices/ directory to determine host's USB
1581 * This code is based on Robert Schiele's original patches posted to
1582 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1584 static int usb_host_scan(void *opaque
, USBScanFunc
*func
)
1588 int bus_num
, addr
, speed
, class_id
, product_id
, vendor_id
;
1590 char port
[MAX_PORTLEN
];
1591 char product_name
[512];
1594 dir
= opendir("/sys/bus/usb/devices");
1596 perror("husb: opendir /sys/bus/usb/devices");
1597 fprintf(stderr
, "husb: please make sure sysfs is mounted at /sys\n");
1601 while ((de
= readdir(dir
))) {
1602 if (de
->d_name
[0] != '.' && !strchr(de
->d_name
, ':')) {
1603 if (sscanf(de
->d_name
, "%d-%7[0-9.]", &bus_num
, port
) < 2) {
1607 if (!usb_host_read_file(line
, sizeof(line
), "devnum", de
->d_name
)) {
1610 if (sscanf(line
, "%d", &addr
) != 1) {
1613 if (!usb_host_read_file(line
, sizeof(line
), "bDeviceClass",
1617 if (sscanf(line
, "%x", &class_id
) != 1) {
1621 if (!usb_host_read_file(line
, sizeof(line
), "idVendor",
1625 if (sscanf(line
, "%x", &vendor_id
) != 1) {
1628 if (!usb_host_read_file(line
, sizeof(line
), "idProduct",
1632 if (sscanf(line
, "%x", &product_id
) != 1) {
1635 if (!usb_host_read_file(line
, sizeof(line
), "product",
1639 if (strlen(line
) > 0) {
1640 line
[strlen(line
) - 1] = '\0';
1642 pstrcpy(product_name
, sizeof(product_name
), line
);
1645 if (!usb_host_read_file(line
, sizeof(line
), "speed", de
->d_name
)) {
1648 if (!strcmp(line
, "5000\n")) {
1649 speed
= USB_SPEED_SUPER
;
1650 } else if (!strcmp(line
, "480\n")) {
1651 speed
= USB_SPEED_HIGH
;
1652 } else if (!strcmp(line
, "1.5\n")) {
1653 speed
= USB_SPEED_LOW
;
1655 speed
= USB_SPEED_FULL
;
1658 ret
= func(opaque
, bus_num
, addr
, port
, class_id
, vendor_id
,
1659 product_id
, product_name
, speed
);
1672 static QEMUTimer
*usb_auto_timer
;
1673 static VMChangeStateEntry
*usb_vmstate
;
1675 static int usb_host_auto_scan(void *opaque
, int bus_num
,
1676 int addr
, const char *port
,
1677 int class_id
, int vendor_id
, int product_id
,
1678 const char *product_name
, int speed
)
1680 struct USBAutoFilter
*f
;
1681 struct USBHostDevice
*s
;
1687 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1690 if (f
->bus_num
> 0 && f
->bus_num
!= bus_num
) {
1693 if (f
->addr
> 0 && f
->addr
!= addr
) {
1696 if (f
->port
!= NULL
&& strcmp(f
->port
, port
) != 0) {
1700 if (f
->vendor_id
> 0 && f
->vendor_id
!= vendor_id
) {
1704 if (f
->product_id
> 0 && f
->product_id
!= product_id
) {
1707 /* We got a match */
1709 if (s
->errcount
>= 3) {
1713 /* Already attached ? */
1717 DPRINTF("husb: auto open: bus_num %d addr %d\n", bus_num
, addr
);
1719 if (usb_host_open(s
, bus_num
, addr
, port
, product_name
, speed
) < 0) {
1728 static void usb_host_vm_state(void *unused
, int running
, RunState state
)
1731 usb_host_auto_check(unused
);
1735 static void usb_host_auto_check(void *unused
)
1737 struct USBHostDevice
*s
;
1738 int unconnected
= 0;
1740 if (runstate_is_running()) {
1741 usb_host_scan(NULL
, usb_host_auto_scan
);
1743 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1753 if (unconnected
== 0) {
1754 /* nothing to watch */
1755 if (usb_auto_timer
) {
1756 qemu_del_timer(usb_auto_timer
);
1757 trace_usb_host_auto_scan_disabled();
1764 usb_vmstate
= qemu_add_vm_change_state_handler(usb_host_vm_state
, NULL
);
1766 if (!usb_auto_timer
) {
1767 usb_auto_timer
= qemu_new_timer_ms(rt_clock
, usb_host_auto_check
, NULL
);
1768 if (!usb_auto_timer
) {
1771 trace_usb_host_auto_scan_enabled();
1773 qemu_mod_timer(usb_auto_timer
, qemu_get_clock_ms(rt_clock
) + 2000);
1776 #ifndef CONFIG_USB_LIBUSB
1778 /**********************/
1779 /* USB host device info */
1781 struct usb_class_info
{
1783 const char *class_name
;
1786 static const struct usb_class_info usb_class_info
[] = {
1787 { USB_CLASS_AUDIO
, "Audio"},
1788 { USB_CLASS_COMM
, "Communication"},
1789 { USB_CLASS_HID
, "HID"},
1790 { USB_CLASS_HUB
, "Hub" },
1791 { USB_CLASS_PHYSICAL
, "Physical" },
1792 { USB_CLASS_PRINTER
, "Printer" },
1793 { USB_CLASS_MASS_STORAGE
, "Storage" },
1794 { USB_CLASS_CDC_DATA
, "Data" },
1795 { USB_CLASS_APP_SPEC
, "Application Specific" },
1796 { USB_CLASS_VENDOR_SPEC
, "Vendor Specific" },
1797 { USB_CLASS_STILL_IMAGE
, "Still Image" },
1798 { USB_CLASS_CSCID
, "Smart Card" },
1799 { USB_CLASS_CONTENT_SEC
, "Content Security" },
1803 static const char *usb_class_str(uint8_t class)
1805 const struct usb_class_info
*p
;
1806 for(p
= usb_class_info
; p
->class != -1; p
++) {
1807 if (p
->class == class) {
1811 return p
->class_name
;
1814 static void usb_info_device(Monitor
*mon
, int bus_num
,
1815 int addr
, const char *port
,
1816 int class_id
, int vendor_id
, int product_id
,
1817 const char *product_name
,
1820 const char *class_str
, *speed_str
;
1826 case USB_SPEED_FULL
:
1829 case USB_SPEED_HIGH
:
1832 case USB_SPEED_SUPER
:
1840 monitor_printf(mon
, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
1841 bus_num
, addr
, port
, speed_str
);
1842 class_str
= usb_class_str(class_id
);
1844 monitor_printf(mon
, " %s:", class_str
);
1846 monitor_printf(mon
, " Class %02x:", class_id
);
1848 monitor_printf(mon
, " USB device %04x:%04x", vendor_id
, product_id
);
1849 if (product_name
[0] != '\0') {
1850 monitor_printf(mon
, ", %s", product_name
);
1852 monitor_printf(mon
, "\n");
1855 static int usb_host_info_device(void *opaque
, int bus_num
, int addr
,
1856 const char *path
, int class_id
,
1857 int vendor_id
, int product_id
,
1858 const char *product_name
,
1861 Monitor
*mon
= opaque
;
1863 usb_info_device(mon
, bus_num
, addr
, path
, class_id
, vendor_id
, product_id
,
1864 product_name
, speed
);
1868 static void dec2str(int val
, char *str
, size_t size
)
1871 snprintf(str
, size
, "*");
1873 snprintf(str
, size
, "%d", val
);
1877 static void hex2str(int val
, char *str
, size_t size
)
1880 snprintf(str
, size
, "*");
1882 snprintf(str
, size
, "%04x", val
);
1886 void usb_host_info(Monitor
*mon
, const QDict
*qdict
)
1888 struct USBAutoFilter
*f
;
1889 struct USBHostDevice
*s
;
1891 usb_host_scan(mon
, usb_host_info_device
);
1893 if (QTAILQ_EMPTY(&hostdevs
)) {
1897 monitor_printf(mon
, " Auto filters:\n");
1898 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1899 char bus
[10], addr
[10], vid
[10], pid
[10];
1901 dec2str(f
->bus_num
, bus
, sizeof(bus
));
1902 dec2str(f
->addr
, addr
, sizeof(addr
));
1903 hex2str(f
->vendor_id
, vid
, sizeof(vid
));
1904 hex2str(f
->product_id
, pid
, sizeof(pid
));
1905 monitor_printf(mon
, " Bus %s, Addr %s, Port %s, ID %s:%s\n",
1906 bus
, addr
, f
->port
? f
->port
: "*", vid
, pid
);