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"
40 #include <sys/ioctl.h>
42 #include <linux/usbdevice_fs.h>
43 #include <linux/version.h>
45 #include "hw/usb/desc.h"
47 /* We redefine it to avoid version problems */
48 struct usb_ctrltransfer
{
58 typedef int USBScanFunc(void *opaque
, int bus_num
, int addr
, const char *port
,
59 int class_id
, int vendor_id
, int product_id
,
60 const char *product_name
, int speed
);
65 #define DPRINTF printf
70 #define PRODUCT_NAME_SZ 32
71 #define MAX_PORTLEN 16
73 /* endpoint association data */
74 #define ISO_FRAME_DESC_PER_URB 32
76 /* devio.c limits single requests to 16k */
77 #define MAX_USBFS_BUFFER_SIZE 16384
79 typedef struct AsyncURB AsyncURB
;
90 struct USBAutoFilter
{
98 enum USBHostDeviceOptions
{
99 USB_HOST_OPT_PIPELINE
,
102 typedef struct USBHostDevice
{
111 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 int parse_filter(const char *spec
, struct USBAutoFilter
*f
);
134 static void usb_host_auto_check(void *unused
);
135 static int usb_host_read_file(char *line
, size_t line_size
,
136 const char *device_file
, const char *device_name
);
137 static int usb_linux_update_endp_table(USBHostDevice
*s
);
139 static int usb_host_usbfs_type(USBHostDevice
*s
, USBPacket
*p
)
141 static const int usbfs
[] = {
142 [USB_ENDPOINT_XFER_CONTROL
] = USBDEVFS_URB_TYPE_CONTROL
,
143 [USB_ENDPOINT_XFER_ISOC
] = USBDEVFS_URB_TYPE_ISO
,
144 [USB_ENDPOINT_XFER_BULK
] = USBDEVFS_URB_TYPE_BULK
,
145 [USB_ENDPOINT_XFER_INT
] = USBDEVFS_URB_TYPE_INTERRUPT
,
147 uint8_t type
= p
->ep
->type
;
148 assert(type
< ARRAY_SIZE(usbfs
));
152 static int usb_host_do_reset(USBHostDevice
*dev
)
158 gettimeofday(&s
, NULL
);
159 ret
= ioctl(dev
->fd
, USBDEVFS_RESET
);
160 gettimeofday(&e
, NULL
);
161 usecs
= (e
.tv_sec
- s
.tv_sec
) * 1000000;
162 usecs
+= e
.tv_usec
- s
.tv_usec
;
163 if (usecs
> 1000000) {
164 /* more than a second, something is fishy, broken usb device? */
165 fprintf(stderr
, "husb: device %d:%d reset took %d.%06d seconds\n",
166 dev
->bus_num
, dev
->addr
, usecs
/ 1000000, usecs
% 1000000);
171 static struct endp_data
*get_endp(USBHostDevice
*s
, int pid
, int ep
)
173 struct endp_data
*eps
= pid
== USB_TOKEN_IN
? s
->ep_in
: s
->ep_out
;
174 assert(pid
== USB_TOKEN_IN
|| pid
== USB_TOKEN_OUT
);
175 assert(ep
> 0 && ep
<= USB_MAX_ENDPOINTS
);
179 static int is_isoc(USBHostDevice
*s
, int pid
, int ep
)
181 return usb_ep_get_type(&s
->dev
, pid
, ep
) == USB_ENDPOINT_XFER_ISOC
;
184 static int is_valid(USBHostDevice
*s
, int pid
, int ep
)
186 return usb_ep_get_type(&s
->dev
, pid
, ep
) != USB_ENDPOINT_XFER_INVALID
;
189 static int is_halted(USBHostDevice
*s
, int pid
, int ep
)
191 return get_endp(s
, pid
, ep
)->halted
;
194 static void clear_halt(USBHostDevice
*s
, int pid
, int ep
)
196 trace_usb_host_ep_clear_halt(s
->bus_num
, s
->addr
, ep
);
197 get_endp(s
, pid
, ep
)->halted
= 0;
200 static void set_halt(USBHostDevice
*s
, int pid
, int ep
)
203 trace_usb_host_ep_set_halt(s
->bus_num
, s
->addr
, ep
);
204 get_endp(s
, pid
, ep
)->halted
= 1;
208 static int is_iso_started(USBHostDevice
*s
, int pid
, int ep
)
210 return get_endp(s
, pid
, ep
)->iso_started
;
213 static void clear_iso_started(USBHostDevice
*s
, int pid
, int ep
)
215 trace_usb_host_ep_stop_iso(s
->bus_num
, s
->addr
, ep
);
216 get_endp(s
, pid
, ep
)->iso_started
= 0;
219 static void set_iso_started(USBHostDevice
*s
, int pid
, int ep
)
221 struct endp_data
*e
= get_endp(s
, pid
, ep
);
223 trace_usb_host_ep_start_iso(s
->bus_num
, s
->addr
, ep
);
224 if (!e
->iso_started
) {
230 static int change_iso_inflight(USBHostDevice
*s
, int pid
, int ep
, int value
)
232 struct endp_data
*e
= get_endp(s
, pid
, ep
);
234 e
->inflight
+= value
;
238 static void set_iso_urb(USBHostDevice
*s
, int pid
, int ep
, AsyncURB
*iso_urb
)
240 get_endp(s
, pid
, ep
)->iso_urb
= iso_urb
;
243 static AsyncURB
*get_iso_urb(USBHostDevice
*s
, int pid
, int ep
)
245 return get_endp(s
, pid
, ep
)->iso_urb
;
248 static void set_iso_urb_idx(USBHostDevice
*s
, int pid
, int ep
, int i
)
250 get_endp(s
, pid
, ep
)->iso_urb_idx
= i
;
253 static int get_iso_urb_idx(USBHostDevice
*s
, int pid
, int ep
)
255 return get_endp(s
, pid
, ep
)->iso_urb_idx
;
258 static void set_iso_buffer_used(USBHostDevice
*s
, int pid
, int ep
, int i
)
260 get_endp(s
, pid
, ep
)->iso_buffer_used
= i
;
263 static int get_iso_buffer_used(USBHostDevice
*s
, int pid
, int ep
)
265 return get_endp(s
, pid
, ep
)->iso_buffer_used
;
270 * We always allocate iso packet descriptors even for bulk transfers
271 * to simplify allocation and casts.
275 struct usbdevfs_urb urb
;
276 struct usbdevfs_iso_packet_desc isocpd
[ISO_FRAME_DESC_PER_URB
];
278 QLIST_ENTRY(AsyncURB
) next
;
280 /* For regular async urbs */
282 int more
; /* large transfer, more urbs follow */
284 /* For buffered iso handling */
285 int iso_frame_idx
; /* -1 means in flight */
288 static AsyncURB
*async_alloc(USBHostDevice
*s
)
290 AsyncURB
*aurb
= g_malloc0(sizeof(AsyncURB
));
292 QLIST_INSERT_HEAD(&s
->aurbs
, aurb
, next
);
296 static void async_free(AsyncURB
*aurb
)
298 QLIST_REMOVE(aurb
, next
);
302 static void do_disconnect(USBHostDevice
*s
)
305 usb_host_auto_check(NULL
);
308 static void async_complete(void *opaque
)
310 USBHostDevice
*s
= opaque
;
317 int r
= ioctl(s
->fd
, USBDEVFS_REAPURBNDELAY
, &aurb
);
319 if (errno
== EAGAIN
) {
321 fprintf(stderr
, "husb: %d iso urbs finished at once\n", 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 fprintf(stderr
, "husb: out of buffers for iso stream\n");
360 trace_usb_host_urb_complete(s
->bus_num
, s
->addr
, aurb
, aurb
->urb
.status
,
361 aurb
->urb
.actual_length
, aurb
->more
);
364 switch (aurb
->urb
.status
) {
366 p
->result
+= aurb
->urb
.actual_length
;
370 set_halt(s
, p
->pid
, p
->ep
->nr
);
371 p
->result
= USB_RET_STALL
;
375 p
->result
= USB_RET_BABBLE
;
379 p
->result
= USB_RET_IOERROR
;
383 if (aurb
->urb
.type
== USBDEVFS_URB_TYPE_CONTROL
) {
384 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
, p
->result
);
385 usb_generic_async_ctrl_complete(&s
->dev
, p
);
386 } else if (!aurb
->more
) {
387 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
, p
->result
);
388 usb_packet_complete(&s
->dev
, p
);
396 static void usb_host_async_cancel(USBDevice
*dev
, USBPacket
*p
)
398 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
401 trace_usb_host_req_canceled(s
->bus_num
, s
->addr
, p
);
403 QLIST_FOREACH(aurb
, &s
->aurbs
, next
) {
404 if (p
!= aurb
->packet
) {
408 trace_usb_host_urb_canceled(s
->bus_num
, s
->addr
, aurb
);
410 /* Mark it as dead (see async_complete above) */
413 int r
= ioctl(s
->fd
, USBDEVFS_DISCARDURB
, aurb
);
415 DPRINTF("husb: async. discard urb failed errno %d\n", errno
);
420 static int usb_host_open_device(int bus
, int addr
)
422 const char *usbfs
= NULL
;
427 rc
= stat("/dev/bus/usb", &st
);
428 if (rc
== 0 && S_ISDIR(st
.st_mode
)) {
429 /* udev-created device nodes available */
430 usbfs
= "/dev/bus/usb";
432 /* fallback: usbfs mounted below /proc */
433 usbfs
= "/proc/bus/usb";
436 snprintf(filename
, sizeof(filename
), "%s/%03d/%03d",
438 fd
= open(filename
, O_RDWR
| O_NONBLOCK
);
440 fprintf(stderr
, "husb: open %s: %s\n", filename
, strerror(errno
));
445 static int usb_host_claim_port(USBHostDevice
*s
)
447 #ifdef USBDEVFS_CLAIM_PORT
448 char *h
, hub_name
[64], line
[1024];
451 snprintf(hub_name
, sizeof(hub_name
), "%d-%s",
452 s
->match
.bus_num
, s
->match
.port
);
454 /* try strip off last ".$portnr" to get hub */
455 h
= strrchr(hub_name
, '.');
457 s
->hub_port
= atoi(h
+1);
460 /* no dot in there -> it is the root hub */
461 snprintf(hub_name
, sizeof(hub_name
), "usb%d",
463 s
->hub_port
= atoi(s
->match
.port
);
466 if (!usb_host_read_file(line
, sizeof(line
), "devnum",
470 if (sscanf(line
, "%d", &hub_addr
) != 1) {
474 s
->hub_fd
= usb_host_open_device(s
->match
.bus_num
, hub_addr
);
479 ret
= ioctl(s
->hub_fd
, USBDEVFS_CLAIM_PORT
, &s
->hub_port
);
486 trace_usb_host_claim_port(s
->match
.bus_num
, hub_addr
, s
->hub_port
);
493 static void usb_host_release_port(USBHostDevice
*s
)
495 if (s
->hub_fd
== -1) {
498 #ifdef USBDEVFS_RELEASE_PORT
499 ioctl(s
->hub_fd
, USBDEVFS_RELEASE_PORT
, &s
->hub_port
);
505 static int usb_host_disconnect_ifaces(USBHostDevice
*dev
, int nb_interfaces
)
507 /* earlier Linux 2.4 do not support that */
508 #ifdef USBDEVFS_DISCONNECT
509 struct usbdevfs_ioctl ctrl
;
512 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
513 ctrl
.ioctl_code
= USBDEVFS_DISCONNECT
;
514 ctrl
.ifno
= interface
;
516 ret
= ioctl(dev
->fd
, USBDEVFS_IOCTL
, &ctrl
);
517 if (ret
< 0 && errno
!= ENODATA
) {
518 perror("USBDEVFS_DISCONNECT");
526 static int usb_linux_get_num_interfaces(USBHostDevice
*s
)
528 char device_name
[64], line
[1024];
529 int num_interfaces
= 0;
531 sprintf(device_name
, "%d-%s", s
->bus_num
, s
->port
);
532 if (!usb_host_read_file(line
, sizeof(line
), "bNumInterfaces",
536 if (sscanf(line
, "%d", &num_interfaces
) != 1) {
539 return num_interfaces
;
542 static int usb_host_claim_interfaces(USBHostDevice
*dev
, int configuration
)
544 const char *op
= NULL
;
545 int dev_descr_len
, config_descr_len
;
546 int interface
, nb_interfaces
;
549 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
550 dev
->dev
.altsetting
[i
] = 0;
553 if (configuration
== 0) { /* address state - ignore */
554 dev
->dev
.ninterfaces
= 0;
555 dev
->dev
.configuration
= 0;
559 DPRINTF("husb: claiming interfaces. config %d\n", configuration
);
562 dev_descr_len
= dev
->descr
[0];
563 if (dev_descr_len
> dev
->descr_len
) {
564 fprintf(stderr
, "husb: update iface failed. descr too short\n");
569 while (i
< dev
->descr_len
) {
570 DPRINTF("husb: i is %d, descr_len is %d, dl %d, dt %d\n",
572 dev
->descr
[i
], dev
->descr
[i
+1]);
574 if (dev
->descr
[i
+1] != USB_DT_CONFIG
) {
578 config_descr_len
= dev
->descr
[i
];
580 DPRINTF("husb: config #%d need %d\n", dev
->descr
[i
+ 5], configuration
);
582 if (configuration
== dev
->descr
[i
+ 5]) {
583 configuration
= dev
->descr
[i
+ 5];
587 i
+= config_descr_len
;
590 if (i
>= dev
->descr_len
) {
592 "husb: update iface failed. no matching configuration\n");
595 nb_interfaces
= dev
->descr
[i
+ 4];
597 if (usb_host_disconnect_ifaces(dev
, nb_interfaces
) < 0) {
601 /* XXX: only grab if all interfaces are free */
602 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
603 op
= "USBDEVFS_CLAIMINTERFACE";
604 ret
= ioctl(dev
->fd
, USBDEVFS_CLAIMINTERFACE
, &interface
);
610 trace_usb_host_claim_interfaces(dev
->bus_num
, dev
->addr
,
611 nb_interfaces
, configuration
);
613 dev
->dev
.ninterfaces
= nb_interfaces
;
614 dev
->dev
.configuration
= configuration
;
618 if (errno
== ENODEV
) {
625 static int usb_host_release_interfaces(USBHostDevice
*s
)
629 trace_usb_host_release_interfaces(s
->bus_num
, s
->addr
);
631 for (i
= 0; i
< s
->dev
.ninterfaces
; i
++) {
632 ret
= ioctl(s
->fd
, USBDEVFS_RELEASEINTERFACE
, &i
);
634 perror("USBDEVFS_RELEASEINTERFACE");
641 static void usb_host_handle_reset(USBDevice
*dev
)
643 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
645 trace_usb_host_reset(s
->bus_num
, s
->addr
);
647 usb_host_do_reset(s
);;
649 usb_host_claim_interfaces(s
, 0);
650 usb_linux_update_endp_table(s
);
653 static void usb_host_handle_destroy(USBDevice
*dev
)
655 USBHostDevice
*s
= (USBHostDevice
*)dev
;
657 usb_host_release_port(s
);
659 QTAILQ_REMOVE(&hostdevs
, s
, next
);
660 qemu_remove_exit_notifier(&s
->exit
);
663 /* iso data is special, we need to keep enough urbs in flight to make sure
664 that the controller never runs out of them, otherwise the device will
665 likely suffer a buffer underrun / overrun. */
666 static AsyncURB
*usb_host_alloc_iso(USBHostDevice
*s
, int pid
, uint8_t ep
)
669 int i
, j
, len
= usb_ep_get_max_packet_size(&s
->dev
, pid
, ep
);
671 aurb
= g_malloc0(s
->iso_urb_count
* sizeof(*aurb
));
672 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
673 aurb
[i
].urb
.endpoint
= ep
;
674 aurb
[i
].urb
.buffer_length
= ISO_FRAME_DESC_PER_URB
* len
;
675 aurb
[i
].urb
.buffer
= g_malloc(aurb
[i
].urb
.buffer_length
);
676 aurb
[i
].urb
.type
= USBDEVFS_URB_TYPE_ISO
;
677 aurb
[i
].urb
.flags
= USBDEVFS_URB_ISO_ASAP
;
678 aurb
[i
].urb
.number_of_packets
= ISO_FRAME_DESC_PER_URB
;
679 for (j
= 0 ; j
< ISO_FRAME_DESC_PER_URB
; j
++)
680 aurb
[i
].urb
.iso_frame_desc
[j
].length
= len
;
681 if (pid
== USB_TOKEN_IN
) {
682 aurb
[i
].urb
.endpoint
|= 0x80;
683 /* Mark as fully consumed (idle) */
684 aurb
[i
].iso_frame_idx
= ISO_FRAME_DESC_PER_URB
;
687 set_iso_urb(s
, pid
, ep
, aurb
);
692 static void usb_host_stop_n_free_iso(USBHostDevice
*s
, int pid
, uint8_t ep
)
695 int i
, ret
, killed
= 0, free
= 1;
697 aurb
= get_iso_urb(s
, pid
, ep
);
702 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
704 if (aurb
[i
].iso_frame_idx
== -1) {
705 ret
= ioctl(s
->fd
, USBDEVFS_DISCARDURB
, &aurb
[i
]);
707 perror("USBDEVFS_DISCARDURB");
715 /* Make sure any urbs we've killed are reaped before we free them */
720 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
721 g_free(aurb
[i
].urb
.buffer
);
727 printf("husb: leaking iso urbs because of discard failure\n");
728 set_iso_urb(s
, pid
, ep
, NULL
);
729 set_iso_urb_idx(s
, pid
, ep
, 0);
730 clear_iso_started(s
, pid
, ep
);
733 static int urb_status_to_usb_ret(int status
)
737 return USB_RET_STALL
;
739 return USB_RET_BABBLE
;
741 return USB_RET_IOERROR
;
745 static int usb_host_handle_iso_data(USBHostDevice
*s
, USBPacket
*p
, int in
)
748 int i
, j
, ret
, max_packet_size
, offset
, len
= 0;
751 max_packet_size
= p
->ep
->max_packet_size
;
752 if (max_packet_size
== 0)
755 aurb
= get_iso_urb(s
, p
->pid
, p
->ep
->nr
);
757 aurb
= usb_host_alloc_iso(s
, p
->pid
, p
->ep
->nr
);
760 i
= get_iso_urb_idx(s
, p
->pid
, p
->ep
->nr
);
761 j
= aurb
[i
].iso_frame_idx
;
762 if (j
>= 0 && j
< ISO_FRAME_DESC_PER_URB
) {
764 /* Check urb status */
765 if (aurb
[i
].urb
.status
) {
766 len
= urb_status_to_usb_ret(aurb
[i
].urb
.status
);
767 /* Move to the next urb */
768 aurb
[i
].iso_frame_idx
= ISO_FRAME_DESC_PER_URB
- 1;
769 /* Check frame status */
770 } else if (aurb
[i
].urb
.iso_frame_desc
[j
].status
) {
771 len
= urb_status_to_usb_ret(
772 aurb
[i
].urb
.iso_frame_desc
[j
].status
);
773 /* Check the frame fits */
774 } else if (aurb
[i
].urb
.iso_frame_desc
[j
].actual_length
776 printf("husb: received iso data is larger then packet\n");
777 len
= USB_RET_BABBLE
;
778 /* All good copy data over */
780 len
= aurb
[i
].urb
.iso_frame_desc
[j
].actual_length
;
781 buf
= aurb
[i
].urb
.buffer
+
782 j
* aurb
[i
].urb
.iso_frame_desc
[0].length
;
783 usb_packet_copy(p
, buf
, len
);
787 offset
= (j
== 0) ? 0 : get_iso_buffer_used(s
, p
->pid
, p
->ep
->nr
);
789 /* Check the frame fits */
790 if (len
> max_packet_size
) {
791 printf("husb: send iso data is larger then max packet size\n");
795 /* All good copy data over */
796 usb_packet_copy(p
, aurb
[i
].urb
.buffer
+ offset
, len
);
797 aurb
[i
].urb
.iso_frame_desc
[j
].length
= len
;
799 set_iso_buffer_used(s
, p
->pid
, p
->ep
->nr
, offset
);
801 /* Start the stream once we have buffered enough data */
802 if (!is_iso_started(s
, p
->pid
, p
->ep
->nr
) && i
== 1 && j
== 8) {
803 set_iso_started(s
, p
->pid
, p
->ep
->nr
);
806 aurb
[i
].iso_frame_idx
++;
807 if (aurb
[i
].iso_frame_idx
== ISO_FRAME_DESC_PER_URB
) {
808 i
= (i
+ 1) % s
->iso_urb_count
;
809 set_iso_urb_idx(s
, p
->pid
, p
->ep
->nr
, i
);
813 set_iso_started(s
, p
->pid
, p
->ep
->nr
);
815 DPRINTF("hubs: iso out error no free buffer, dropping packet\n");
819 if (is_iso_started(s
, p
->pid
, p
->ep
->nr
)) {
820 /* (Re)-submit all fully consumed / filled urbs */
821 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
822 if (aurb
[i
].iso_frame_idx
== ISO_FRAME_DESC_PER_URB
) {
823 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, &aurb
[i
]);
825 perror("USBDEVFS_SUBMITURB");
826 if (!in
|| len
== 0) {
838 aurb
[i
].iso_frame_idx
= -1;
839 change_iso_inflight(s
, p
->pid
, p
->ep
->nr
, 1);
847 static int usb_host_handle_data(USBDevice
*dev
, USBPacket
*p
)
849 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
850 struct usbdevfs_urb
*urb
;
852 int ret
, rem
, prem
, v
;
856 trace_usb_host_req_data(s
->bus_num
, s
->addr
, p
,
857 p
->pid
== USB_TOKEN_IN
,
858 p
->ep
->nr
, p
->iov
.size
);
860 if (!is_valid(s
, p
->pid
, p
->ep
->nr
)) {
861 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
, USB_RET_NAK
);
865 if (p
->pid
== USB_TOKEN_IN
) {
866 ep
= p
->ep
->nr
| 0x80;
871 if (is_halted(s
, p
->pid
, p
->ep
->nr
)) {
872 unsigned int arg
= ep
;
873 ret
= ioctl(s
->fd
, USBDEVFS_CLEAR_HALT
, &arg
);
875 perror("USBDEVFS_CLEAR_HALT");
876 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
, USB_RET_NAK
);
879 clear_halt(s
, p
->pid
, p
->ep
->nr
);
882 if (is_isoc(s
, p
->pid
, p
->ep
->nr
)) {
883 return usb_host_handle_iso_data(s
, p
, p
->pid
== USB_TOKEN_IN
);
887 prem
= p
->iov
.iov
[v
].iov_len
;
888 pbuf
= p
->iov
.iov
[v
].iov_base
;
893 assert(v
< p
->iov
.niov
);
894 prem
= p
->iov
.iov
[v
].iov_len
;
895 pbuf
= p
->iov
.iov
[v
].iov_base
;
898 aurb
= async_alloc(s
);
903 urb
->type
= usb_host_usbfs_type(s
, p
);
904 urb
->usercontext
= s
;
906 urb
->buffer_length
= prem
;
908 if (urb
->buffer_length
> MAX_USBFS_BUFFER_SIZE
) {
909 urb
->buffer_length
= MAX_USBFS_BUFFER_SIZE
;
911 pbuf
+= urb
->buffer_length
;
912 prem
-= urb
->buffer_length
;
913 rem
-= urb
->buffer_length
;
918 trace_usb_host_urb_submit(s
->bus_num
, s
->addr
, aurb
,
919 urb
->buffer_length
, aurb
->more
);
920 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
922 DPRINTF("husb: data submit: ep 0x%x, len %u, more %d, packet %p, aurb %p\n",
923 urb
->endpoint
, urb
->buffer_length
, aurb
->more
, p
, aurb
);
926 perror("USBDEVFS_SUBMITURB");
931 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
936 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
938 return USB_RET_STALL
;
943 return USB_RET_ASYNC
;
946 static int ctrl_error(void)
948 if (errno
== ETIMEDOUT
) {
951 return USB_RET_STALL
;
955 static int usb_host_set_address(USBHostDevice
*s
, int addr
)
957 trace_usb_host_set_address(s
->bus_num
, s
->addr
, addr
);
962 static int usb_host_set_config(USBHostDevice
*s
, int config
)
966 trace_usb_host_set_config(s
->bus_num
, s
->addr
, config
);
968 usb_host_release_interfaces(s
);
971 ret
= ioctl(s
->fd
, USBDEVFS_SETCONFIGURATION
, &config
);
973 DPRINTF("husb: ctrl set config %d ret %d errno %d\n", config
, ret
, errno
);
975 if (ret
< 0 && errno
== EBUSY
&& first
) {
976 /* happens if usb device is in use by host drivers */
977 int count
= usb_linux_get_num_interfaces(s
);
979 DPRINTF("husb: busy -> disconnecting %d interfaces\n", count
);
980 usb_host_disconnect_ifaces(s
, count
);
989 usb_host_claim_interfaces(s
, config
);
990 usb_linux_update_endp_table(s
);
994 static int usb_host_set_interface(USBHostDevice
*s
, int iface
, int alt
)
996 struct usbdevfs_setinterface si
;
999 trace_usb_host_set_interface(s
->bus_num
, s
->addr
, iface
, alt
);
1001 for (i
= 1; i
<= USB_MAX_ENDPOINTS
; i
++) {
1002 if (is_isoc(s
, USB_TOKEN_IN
, i
)) {
1003 usb_host_stop_n_free_iso(s
, USB_TOKEN_IN
, i
);
1005 if (is_isoc(s
, USB_TOKEN_OUT
, i
)) {
1006 usb_host_stop_n_free_iso(s
, USB_TOKEN_OUT
, i
);
1010 if (iface
>= USB_MAX_INTERFACES
) {
1011 return USB_RET_STALL
;
1014 si
.interface
= iface
;
1015 si
.altsetting
= alt
;
1016 ret
= ioctl(s
->fd
, USBDEVFS_SETINTERFACE
, &si
);
1018 DPRINTF("husb: ctrl set iface %d altset %d ret %d errno %d\n",
1019 iface
, alt
, ret
, errno
);
1022 return ctrl_error();
1025 s
->dev
.altsetting
[iface
] = alt
;
1026 usb_linux_update_endp_table(s
);
1030 static int usb_host_handle_control(USBDevice
*dev
, USBPacket
*p
,
1031 int request
, int value
, int index
, int length
, uint8_t *data
)
1033 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
1034 struct usbdevfs_urb
*urb
;
1039 * Process certain standard device requests.
1040 * These are infrequent and are processed synchronously.
1043 /* Note request is (bRequestType << 8) | bRequest */
1044 trace_usb_host_req_control(s
->bus_num
, s
->addr
, p
, request
, value
, index
);
1047 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
1048 ret
= usb_host_set_address(s
, value
);
1049 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, ret
);
1052 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
1053 ret
= usb_host_set_config(s
, value
& 0xff);
1054 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, ret
);
1057 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
1058 ret
= usb_host_set_interface(s
, index
, value
);
1059 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, ret
);
1063 /* The rest are asynchronous */
1065 if (length
> sizeof(dev
->data_buf
)) {
1066 fprintf(stderr
, "husb: ctrl buffer too small (%d > %zu)\n",
1067 length
, sizeof(dev
->data_buf
));
1068 return USB_RET_STALL
;
1071 aurb
= async_alloc(s
);
1075 * Setup ctrl transfer.
1077 * s->ctrl is laid out such that data buffer immediately follows
1078 * 'req' struct which is exactly what usbdevfs expects.
1082 urb
->type
= USBDEVFS_URB_TYPE_CONTROL
;
1083 urb
->endpoint
= p
->ep
->nr
;
1085 urb
->buffer
= &dev
->setup_buf
;
1086 urb
->buffer_length
= length
+ 8;
1088 urb
->usercontext
= s
;
1090 trace_usb_host_urb_submit(s
->bus_num
, s
->addr
, aurb
,
1091 urb
->buffer_length
, aurb
->more
);
1092 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
1094 DPRINTF("husb: submit ctrl. len %u aurb %p\n", urb
->buffer_length
, aurb
);
1097 DPRINTF("husb: submit failed. errno %d\n", errno
);
1105 return USB_RET_STALL
;
1109 return USB_RET_ASYNC
;
1112 /* returns 1 on problem encountered or 0 for success */
1113 static int usb_linux_update_endp_table(USBHostDevice
*s
)
1115 static const char *tname
[] = {
1116 [USB_ENDPOINT_XFER_CONTROL
] = "control",
1117 [USB_ENDPOINT_XFER_ISOC
] = "isoc",
1118 [USB_ENDPOINT_XFER_BULK
] = "bulk",
1119 [USB_ENDPOINT_XFER_INT
] = "int",
1121 uint8_t devep
, type
;
1124 unsigned int i
, configuration
= -1, interface
= -1, altsetting
= -1;
1125 struct endp_data
*epd
;
1127 bool active
= false;
1129 usb_ep_init(&s
->dev
);
1131 for (i
= 0;; i
+= d
->bLength
) {
1132 if (i
+2 >= s
->descr_len
) {
1135 d
= (void *)(s
->descr
+ i
);
1136 if (d
->bLength
< 2) {
1137 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1138 "descriptor too short");
1141 if (i
+ d
->bLength
> s
->descr_len
) {
1142 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1143 "descriptor too long");
1146 switch (d
->bDescriptorType
) {
1148 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1149 "invalid descriptor type");
1152 if (d
->bLength
< 0x12) {
1153 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1154 "device descriptor too short");
1157 v
= (d
->u
.device
.idVendor_hi
<< 8) | d
->u
.device
.idVendor_lo
;
1158 p
= (d
->u
.device
.idProduct_hi
<< 8) | d
->u
.device
.idProduct_lo
;
1159 trace_usb_host_parse_device(s
->bus_num
, s
->addr
, v
, p
);
1162 if (d
->bLength
< 0x09) {
1163 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1164 "config descriptor too short");
1167 configuration
= d
->u
.config
.bConfigurationValue
;
1168 active
= (configuration
== s
->dev
.configuration
);
1169 trace_usb_host_parse_config(s
->bus_num
, s
->addr
,
1170 configuration
, active
);
1172 case USB_DT_INTERFACE
:
1173 if (d
->bLength
< 0x09) {
1174 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1175 "interface descriptor too short");
1178 interface
= d
->u
.interface
.bInterfaceNumber
;
1179 altsetting
= d
->u
.interface
.bAlternateSetting
;
1180 active
= (configuration
== s
->dev
.configuration
) &&
1181 (altsetting
== s
->dev
.altsetting
[interface
]);
1182 trace_usb_host_parse_interface(s
->bus_num
, s
->addr
,
1183 interface
, altsetting
, active
);
1185 case USB_DT_ENDPOINT
:
1186 if (d
->bLength
< 0x07) {
1187 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1188 "endpoint descriptor too short");
1191 devep
= d
->u
.endpoint
.bEndpointAddress
;
1192 pid
= (devep
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
1195 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1196 "invalid endpoint address");
1200 type
= d
->u
.endpoint
.bmAttributes
& 0x3;
1201 mps
= d
->u
.endpoint
.wMaxPacketSize_lo
|
1202 (d
->u
.endpoint
.wMaxPacketSize_hi
<< 8);
1203 trace_usb_host_parse_endpoint(s
->bus_num
, s
->addr
, ep
,
1204 (devep
& USB_DIR_IN
) ? "in" : "out",
1205 tname
[type
], active
);
1208 usb_ep_set_max_packet_size(&s
->dev
, pid
, ep
, mps
);
1209 assert(usb_ep_get_type(&s
->dev
, pid
, ep
) ==
1210 USB_ENDPOINT_XFER_INVALID
);
1211 usb_ep_set_type(&s
->dev
, pid
, ep
, type
);
1212 usb_ep_set_ifnum(&s
->dev
, pid
, ep
, interface
);
1213 if ((s
->options
& (1 << USB_HOST_OPT_PIPELINE
)) &&
1214 (type
== USB_ENDPOINT_XFER_BULK
)) {
1215 usb_ep_set_pipeline(&s
->dev
, pid
, ep
, true);
1218 epd
= get_endp(s
, pid
, ep
);
1224 trace_usb_host_parse_unknown(s
->bus_num
, s
->addr
,
1225 d
->bLength
, d
->bDescriptorType
);
1232 usb_ep_init(&s
->dev
);
1237 * Check if we can safely redirect a usb2 device to a usb1 virtual controller,
1238 * this function assumes this is safe, if:
1239 * 1) There are no isoc endpoints
1240 * 2) There are no interrupt endpoints with a max_packet_size > 64
1241 * Note bulk endpoints with a max_packet_size > 64 in theory also are not
1242 * usb1 compatible, but in practice this seems to work fine.
1244 static int usb_linux_full_speed_compat(USBHostDevice
*dev
)
1249 * usb_linux_update_endp_table only registers info about ep in the current
1250 * interface altsettings, so we need to parse the descriptors again.
1252 for (i
= 0; (i
+ 5) < dev
->descr_len
; i
+= dev
->descr
[i
]) {
1253 if (dev
->descr
[i
+ 1] == USB_DT_ENDPOINT
) {
1254 switch (dev
->descr
[i
+ 3] & 0x3) {
1255 case 0x00: /* CONTROL */
1257 case 0x01: /* ISO */
1259 case 0x02: /* BULK */
1261 case 0x03: /* INTERRUPT */
1262 packet_size
= dev
->descr
[i
+ 4] + (dev
->descr
[i
+ 5] << 8);
1263 if (packet_size
> 64)
1272 static int usb_host_open(USBHostDevice
*dev
, int bus_num
,
1273 int addr
, const char *port
,
1274 const char *prod_name
, int speed
)
1278 trace_usb_host_open_started(bus_num
, addr
);
1280 if (dev
->fd
!= -1) {
1284 fd
= usb_host_open_device(bus_num
, addr
);
1288 DPRINTF("husb: opened %s\n", buf
);
1290 dev
->bus_num
= bus_num
;
1292 strcpy(dev
->port
, port
);
1295 /* read the device description */
1296 dev
->descr_len
= read(fd
, dev
->descr
, sizeof(dev
->descr
));
1297 if (dev
->descr_len
<= 0) {
1298 perror("husb: reading device data failed");
1305 printf("=== begin dumping device descriptor data ===\n");
1306 for (x
= 0; x
< dev
->descr_len
; x
++) {
1307 printf("%02x ", dev
->descr
[x
]);
1309 printf("\n=== end dumping device descriptor data ===\n");
1314 /* start unconfigured -- we'll wait for the guest to set a configuration */
1315 if (!usb_host_claim_interfaces(dev
, 0)) {
1319 ret
= usb_linux_update_endp_table(dev
);
1325 struct usbdevfs_connectinfo ci
;
1327 ret
= ioctl(fd
, USBDEVFS_CONNECTINFO
, &ci
);
1329 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
1334 speed
= USB_SPEED_LOW
;
1336 speed
= USB_SPEED_HIGH
;
1339 dev
->dev
.speed
= speed
;
1340 dev
->dev
.speedmask
= (1 << speed
);
1341 if (dev
->dev
.speed
== USB_SPEED_HIGH
&& usb_linux_full_speed_compat(dev
)) {
1342 dev
->dev
.speedmask
|= USB_SPEED_MASK_FULL
;
1345 trace_usb_host_open_success(bus_num
, addr
);
1347 if (!prod_name
|| prod_name
[0] == '\0') {
1348 snprintf(dev
->dev
.product_desc
, sizeof(dev
->dev
.product_desc
),
1349 "host:%d.%d", bus_num
, addr
);
1351 pstrcpy(dev
->dev
.product_desc
, sizeof(dev
->dev
.product_desc
),
1355 ret
= usb_device_attach(&dev
->dev
);
1360 /* USB devio uses 'write' flag to check for async completions */
1361 qemu_set_fd_handler(dev
->fd
, NULL
, async_complete
, dev
);
1366 trace_usb_host_open_failure(bus_num
, addr
);
1367 if (dev
->fd
!= -1) {
1374 static int usb_host_close(USBHostDevice
*dev
)
1378 if (dev
->fd
== -1) {
1382 trace_usb_host_close(dev
->bus_num
, dev
->addr
);
1384 qemu_set_fd_handler(dev
->fd
, NULL
, NULL
, NULL
);
1386 for (i
= 1; i
<= USB_MAX_ENDPOINTS
; i
++) {
1387 if (is_isoc(dev
, USB_TOKEN_IN
, i
)) {
1388 usb_host_stop_n_free_iso(dev
, USB_TOKEN_IN
, i
);
1390 if (is_isoc(dev
, USB_TOKEN_OUT
, i
)) {
1391 usb_host_stop_n_free_iso(dev
, USB_TOKEN_OUT
, i
);
1394 async_complete(dev
);
1396 if (dev
->dev
.attached
) {
1397 usb_device_detach(&dev
->dev
);
1399 usb_host_do_reset(dev
);
1405 static void usb_host_exit_notifier(struct Notifier
*n
, void *data
)
1407 USBHostDevice
*s
= container_of(n
, USBHostDevice
, exit
);
1409 usb_host_release_port(s
);
1411 usb_host_do_reset(s
);;
1415 static int usb_host_initfn(USBDevice
*dev
)
1417 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
1419 dev
->auto_attach
= 0;
1423 QTAILQ_INSERT_TAIL(&hostdevs
, s
, next
);
1424 s
->exit
.notify
= usb_host_exit_notifier
;
1425 qemu_add_exit_notifier(&s
->exit
);
1426 usb_host_auto_check(NULL
);
1428 if (s
->match
.bus_num
!= 0 && s
->match
.port
!= NULL
) {
1429 usb_host_claim_port(s
);
1431 add_boot_device_path(s
->bootindex
, &dev
->qdev
, NULL
);
1435 static const VMStateDescription vmstate_usb_host
= {
1440 static Property usb_host_dev_properties
[] = {
1441 DEFINE_PROP_UINT32("hostbus", USBHostDevice
, match
.bus_num
, 0),
1442 DEFINE_PROP_UINT32("hostaddr", USBHostDevice
, match
.addr
, 0),
1443 DEFINE_PROP_STRING("hostport", USBHostDevice
, match
.port
),
1444 DEFINE_PROP_HEX32("vendorid", USBHostDevice
, match
.vendor_id
, 0),
1445 DEFINE_PROP_HEX32("productid", USBHostDevice
, match
.product_id
, 0),
1446 DEFINE_PROP_UINT32("isobufs", USBHostDevice
, iso_urb_count
, 4),
1447 DEFINE_PROP_INT32("bootindex", USBHostDevice
, bootindex
, -1),
1448 DEFINE_PROP_BIT("pipeline", USBHostDevice
, options
,
1449 USB_HOST_OPT_PIPELINE
, true),
1450 DEFINE_PROP_END_OF_LIST(),
1453 static void usb_host_class_initfn(ObjectClass
*klass
, void *data
)
1455 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1456 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
1458 uc
->init
= usb_host_initfn
;
1459 uc
->product_desc
= "USB Host Device";
1460 uc
->cancel_packet
= usb_host_async_cancel
;
1461 uc
->handle_data
= usb_host_handle_data
;
1462 uc
->handle_control
= usb_host_handle_control
;
1463 uc
->handle_reset
= usb_host_handle_reset
;
1464 uc
->handle_destroy
= usb_host_handle_destroy
;
1465 dc
->vmsd
= &vmstate_usb_host
;
1466 dc
->props
= usb_host_dev_properties
;
1469 static TypeInfo usb_host_dev_info
= {
1471 .parent
= TYPE_USB_DEVICE
,
1472 .instance_size
= sizeof(USBHostDevice
),
1473 .class_init
= usb_host_class_initfn
,
1476 static void usb_host_register_types(void)
1478 type_register_static(&usb_host_dev_info
);
1479 usb_legacy_register("usb-host", "host", usb_host_device_open
);
1482 type_init(usb_host_register_types
)
1484 USBDevice
*usb_host_device_open(USBBus
*bus
, const char *devname
)
1486 struct USBAutoFilter filter
;
1490 dev
= usb_create(bus
, "usb-host");
1492 if (strstr(devname
, "auto:")) {
1493 if (parse_filter(devname
, &filter
) < 0) {
1497 if ((p
= strchr(devname
, '.'))) {
1498 filter
.bus_num
= strtoul(devname
, NULL
, 0);
1499 filter
.addr
= strtoul(p
+ 1, NULL
, 0);
1500 filter
.vendor_id
= 0;
1501 filter
.product_id
= 0;
1502 } else if ((p
= strchr(devname
, ':'))) {
1505 filter
.vendor_id
= strtoul(devname
, NULL
, 16);
1506 filter
.product_id
= strtoul(p
+ 1, NULL
, 16);
1512 qdev_prop_set_uint32(&dev
->qdev
, "hostbus", filter
.bus_num
);
1513 qdev_prop_set_uint32(&dev
->qdev
, "hostaddr", filter
.addr
);
1514 qdev_prop_set_uint32(&dev
->qdev
, "vendorid", filter
.vendor_id
);
1515 qdev_prop_set_uint32(&dev
->qdev
, "productid", filter
.product_id
);
1516 qdev_init_nofail(&dev
->qdev
);
1520 qdev_free(&dev
->qdev
);
1524 int usb_host_device_close(const char *devname
)
1527 char product_name
[PRODUCT_NAME_SZ
];
1531 if (strstr(devname
, "auto:")) {
1532 return usb_host_auto_del(devname
);
1534 if (usb_host_find_device(&bus_num
, &addr
, product_name
,
1535 sizeof(product_name
), devname
) < 0) {
1538 s
= hostdev_find(bus_num
, addr
);
1540 usb_device_delete_addr(s
->bus_num
, s
->dev
.addr
);
1549 * Read sys file-system device file
1551 * @line address of buffer to put file contents in
1552 * @line_size size of line
1553 * @device_file path to device file (printf format string)
1554 * @device_name device being opened (inserted into device_file)
1556 * @return 0 failed, 1 succeeded ('line' contains data)
1558 static int usb_host_read_file(char *line
, size_t line_size
,
1559 const char *device_file
, const char *device_name
)
1563 char filename
[PATH_MAX
];
1565 snprintf(filename
, PATH_MAX
, "/sys/bus/usb/devices/%s/%s", device_name
,
1567 f
= fopen(filename
, "r");
1569 ret
= fgets(line
, line_size
, f
) != NULL
;
1577 * Use /sys/bus/usb/devices/ directory to determine host's USB
1580 * This code is based on Robert Schiele's original patches posted to
1581 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1583 static int usb_host_scan(void *opaque
, USBScanFunc
*func
)
1587 int bus_num
, addr
, speed
, class_id
, product_id
, vendor_id
;
1589 char port
[MAX_PORTLEN
];
1590 char product_name
[512];
1593 dir
= opendir("/sys/bus/usb/devices");
1595 perror("husb: opendir /sys/bus/usb/devices");
1596 fprintf(stderr
, "husb: please make sure sysfs is mounted at /sys\n");
1600 while ((de
= readdir(dir
))) {
1601 if (de
->d_name
[0] != '.' && !strchr(de
->d_name
, ':')) {
1602 if (sscanf(de
->d_name
, "%d-%7[0-9.]", &bus_num
, port
) < 2) {
1606 if (!usb_host_read_file(line
, sizeof(line
), "devnum", de
->d_name
)) {
1609 if (sscanf(line
, "%d", &addr
) != 1) {
1612 if (!usb_host_read_file(line
, sizeof(line
), "bDeviceClass",
1616 if (sscanf(line
, "%x", &class_id
) != 1) {
1620 if (!usb_host_read_file(line
, sizeof(line
), "idVendor",
1624 if (sscanf(line
, "%x", &vendor_id
) != 1) {
1627 if (!usb_host_read_file(line
, sizeof(line
), "idProduct",
1631 if (sscanf(line
, "%x", &product_id
) != 1) {
1634 if (!usb_host_read_file(line
, sizeof(line
), "product",
1638 if (strlen(line
) > 0) {
1639 line
[strlen(line
) - 1] = '\0';
1641 pstrcpy(product_name
, sizeof(product_name
), line
);
1644 if (!usb_host_read_file(line
, sizeof(line
), "speed", de
->d_name
)) {
1647 if (!strcmp(line
, "5000\n")) {
1648 speed
= USB_SPEED_SUPER
;
1649 } else if (!strcmp(line
, "480\n")) {
1650 speed
= USB_SPEED_HIGH
;
1651 } else if (!strcmp(line
, "1.5\n")) {
1652 speed
= USB_SPEED_LOW
;
1654 speed
= USB_SPEED_FULL
;
1657 ret
= func(opaque
, bus_num
, addr
, port
, class_id
, vendor_id
,
1658 product_id
, product_name
, speed
);
1671 static QEMUTimer
*usb_auto_timer
;
1673 static int usb_host_auto_scan(void *opaque
, int bus_num
,
1674 int addr
, const char *port
,
1675 int class_id
, int vendor_id
, int product_id
,
1676 const char *product_name
, int speed
)
1678 struct USBAutoFilter
*f
;
1679 struct USBHostDevice
*s
;
1685 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1688 if (f
->bus_num
> 0 && f
->bus_num
!= bus_num
) {
1691 if (f
->addr
> 0 && f
->addr
!= addr
) {
1694 if (f
->port
!= NULL
&& (port
== NULL
|| strcmp(f
->port
, port
) != 0)) {
1698 if (f
->vendor_id
> 0 && f
->vendor_id
!= vendor_id
) {
1702 if (f
->product_id
> 0 && f
->product_id
!= product_id
) {
1705 /* We got a match */
1707 if (s
->errcount
>= 3) {
1711 /* Already attached ? */
1715 DPRINTF("husb: auto open: bus_num %d addr %d\n", bus_num
, addr
);
1717 if (usb_host_open(s
, bus_num
, addr
, port
, product_name
, speed
) < 0) {
1726 static void usb_host_auto_check(void *unused
)
1728 struct USBHostDevice
*s
;
1729 int unconnected
= 0;
1731 usb_host_scan(NULL
, usb_host_auto_scan
);
1733 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1743 if (unconnected
== 0) {
1744 /* nothing to watch */
1745 if (usb_auto_timer
) {
1746 qemu_del_timer(usb_auto_timer
);
1747 trace_usb_host_auto_scan_disabled();
1752 if (!usb_auto_timer
) {
1753 usb_auto_timer
= qemu_new_timer_ms(rt_clock
, usb_host_auto_check
, NULL
);
1754 if (!usb_auto_timer
) {
1757 trace_usb_host_auto_scan_enabled();
1759 qemu_mod_timer(usb_auto_timer
, qemu_get_clock_ms(rt_clock
) + 2000);
1763 * Autoconnect filter
1765 * auto:bus:dev[:vid:pid]
1766 * auto:bus.dev[:vid:pid]
1768 * bus - bus number (dec, * means any)
1769 * dev - device number (dec, * means any)
1770 * vid - vendor id (hex, * means any)
1771 * pid - product id (hex, * means any)
1773 * See 'lsusb' output.
1775 static int parse_filter(const char *spec
, struct USBAutoFilter
*f
)
1777 enum { BUS
, DEV
, VID
, PID
, DONE
};
1778 const char *p
= spec
;
1786 for (i
= BUS
; i
< DONE
; i
++) {
1787 p
= strpbrk(p
, ":.");
1797 case BUS
: f
->bus_num
= strtol(p
, NULL
, 10); break;
1798 case DEV
: f
->addr
= strtol(p
, NULL
, 10); break;
1799 case VID
: f
->vendor_id
= strtol(p
, NULL
, 16); break;
1800 case PID
: f
->product_id
= strtol(p
, NULL
, 16); break;
1805 fprintf(stderr
, "husb: invalid auto filter spec %s\n", spec
);
1812 /**********************/
1813 /* USB host device info */
1815 struct usb_class_info
{
1817 const char *class_name
;
1820 static const struct usb_class_info usb_class_info
[] = {
1821 { USB_CLASS_AUDIO
, "Audio"},
1822 { USB_CLASS_COMM
, "Communication"},
1823 { USB_CLASS_HID
, "HID"},
1824 { USB_CLASS_HUB
, "Hub" },
1825 { USB_CLASS_PHYSICAL
, "Physical" },
1826 { USB_CLASS_PRINTER
, "Printer" },
1827 { USB_CLASS_MASS_STORAGE
, "Storage" },
1828 { USB_CLASS_CDC_DATA
, "Data" },
1829 { USB_CLASS_APP_SPEC
, "Application Specific" },
1830 { USB_CLASS_VENDOR_SPEC
, "Vendor Specific" },
1831 { USB_CLASS_STILL_IMAGE
, "Still Image" },
1832 { USB_CLASS_CSCID
, "Smart Card" },
1833 { USB_CLASS_CONTENT_SEC
, "Content Security" },
1837 static const char *usb_class_str(uint8_t class)
1839 const struct usb_class_info
*p
;
1840 for(p
= usb_class_info
; p
->class != -1; p
++) {
1841 if (p
->class == class) {
1845 return p
->class_name
;
1848 static void usb_info_device(Monitor
*mon
, int bus_num
,
1849 int addr
, const char *port
,
1850 int class_id
, int vendor_id
, int product_id
,
1851 const char *product_name
,
1854 const char *class_str
, *speed_str
;
1860 case USB_SPEED_FULL
:
1863 case USB_SPEED_HIGH
:
1866 case USB_SPEED_SUPER
:
1874 monitor_printf(mon
, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
1875 bus_num
, addr
, port
, speed_str
);
1876 class_str
= usb_class_str(class_id
);
1878 monitor_printf(mon
, " %s:", class_str
);
1880 monitor_printf(mon
, " Class %02x:", class_id
);
1882 monitor_printf(mon
, " USB device %04x:%04x", vendor_id
, product_id
);
1883 if (product_name
[0] != '\0') {
1884 monitor_printf(mon
, ", %s", product_name
);
1886 monitor_printf(mon
, "\n");
1889 static int usb_host_info_device(void *opaque
, int bus_num
, int addr
,
1890 const char *path
, int class_id
,
1891 int vendor_id
, int product_id
,
1892 const char *product_name
,
1895 Monitor
*mon
= opaque
;
1897 usb_info_device(mon
, bus_num
, addr
, path
, class_id
, vendor_id
, product_id
,
1898 product_name
, speed
);
1902 static void dec2str(int val
, char *str
, size_t size
)
1905 snprintf(str
, size
, "*");
1907 snprintf(str
, size
, "%d", val
);
1911 static void hex2str(int val
, char *str
, size_t size
)
1914 snprintf(str
, size
, "*");
1916 snprintf(str
, size
, "%04x", val
);
1920 void usb_host_info(Monitor
*mon
)
1922 struct USBAutoFilter
*f
;
1923 struct USBHostDevice
*s
;
1925 usb_host_scan(mon
, usb_host_info_device
);
1927 if (QTAILQ_EMPTY(&hostdevs
)) {
1931 monitor_printf(mon
, " Auto filters:\n");
1932 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1933 char bus
[10], addr
[10], vid
[10], pid
[10];
1935 dec2str(f
->bus_num
, bus
, sizeof(bus
));
1936 dec2str(f
->addr
, addr
, sizeof(addr
));
1937 hex2str(f
->vendor_id
, vid
, sizeof(vid
));
1938 hex2str(f
->product_id
, pid
, sizeof(pid
));
1939 monitor_printf(mon
, " Bus %s, Addr %s, Port %s, ID %s:%s\n",
1940 bus
, addr
, f
->port
? f
->port
: "*", vid
, pid
);