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>
46 /* We redefine it to avoid version problems */
47 struct usb_ctrltransfer
{
57 typedef int USBScanFunc(void *opaque
, int bus_num
, int addr
, const char *port
,
58 int class_id
, int vendor_id
, int product_id
,
59 const char *product_name
, int speed
);
64 #define DPRINTF printf
69 #define PRODUCT_NAME_SZ 32
70 #define MAX_PORTLEN 16
72 /* endpoint association data */
73 #define ISO_FRAME_DESC_PER_URB 32
75 /* devio.c limits single requests to 16k */
76 #define MAX_USBFS_BUFFER_SIZE 16384
78 typedef struct AsyncURB AsyncURB
;
89 struct USBAutoFilter
{
97 enum USBHostDeviceOptions
{
98 USB_HOST_OPT_PIPELINE
,
101 typedef struct USBHostDevice
{
110 uint32_t iso_urb_count
;
114 struct endp_data ep_in
[USB_MAX_ENDPOINTS
];
115 struct endp_data ep_out
[USB_MAX_ENDPOINTS
];
116 QLIST_HEAD(, AsyncURB
) aurbs
;
118 /* Host side address */
121 char port
[MAX_PORTLEN
];
122 struct USBAutoFilter match
;
126 QTAILQ_ENTRY(USBHostDevice
) next
;
129 static QTAILQ_HEAD(, USBHostDevice
) hostdevs
= QTAILQ_HEAD_INITIALIZER(hostdevs
);
131 static int usb_host_close(USBHostDevice
*dev
);
132 static int parse_filter(const char *spec
, struct USBAutoFilter
*f
);
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 int 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_ep_stop_iso(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_ep_start_iso(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 fprintf(stderr
, "husb: %d iso urbs finished at once\n", urbs
);
324 if (errno
== ENODEV
) {
326 trace_usb_host_disconnect(s
->bus_num
, s
->addr
);
332 perror("USBDEVFS_REAPURBNDELAY");
336 DPRINTF("husb: async completed. aurb %p status %d alen %d\n",
337 aurb
, aurb
->urb
.status
, aurb
->urb
.actual_length
);
339 /* If this is a buffered iso urb mark it as complete and don't do
340 anything else (it is handled further in usb_host_handle_iso_data) */
341 if (aurb
->iso_frame_idx
== -1) {
343 int pid
= (aurb
->urb
.endpoint
& USB_DIR_IN
) ?
344 USB_TOKEN_IN
: USB_TOKEN_OUT
;
345 int ep
= aurb
->urb
.endpoint
& 0xf;
346 if (aurb
->urb
.status
== -EPIPE
) {
347 set_halt(s
, pid
, ep
);
349 aurb
->iso_frame_idx
= 0;
351 inflight
= change_iso_inflight(s
, pid
, ep
, -1);
352 if (inflight
== 0 && is_iso_started(s
, pid
, ep
)) {
353 fprintf(stderr
, "husb: out of buffers for iso stream\n");
359 trace_usb_host_urb_complete(s
->bus_num
, s
->addr
, aurb
, aurb
->urb
.status
,
360 aurb
->urb
.actual_length
, aurb
->more
);
363 switch (aurb
->urb
.status
) {
365 p
->result
+= aurb
->urb
.actual_length
;
369 set_halt(s
, p
->pid
, p
->ep
->nr
);
370 p
->result
= USB_RET_STALL
;
374 p
->result
= USB_RET_BABBLE
;
378 p
->result
= USB_RET_IOERROR
;
382 if (aurb
->urb
.type
== USBDEVFS_URB_TYPE_CONTROL
) {
383 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
, p
->result
);
384 usb_generic_async_ctrl_complete(&s
->dev
, p
);
385 } else if (!aurb
->more
) {
386 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
, p
->result
);
387 usb_packet_complete(&s
->dev
, p
);
395 static void usb_host_async_cancel(USBDevice
*dev
, USBPacket
*p
)
397 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
400 trace_usb_host_req_canceled(s
->bus_num
, s
->addr
, p
);
402 QLIST_FOREACH(aurb
, &s
->aurbs
, next
) {
403 if (p
!= aurb
->packet
) {
407 trace_usb_host_urb_canceled(s
->bus_num
, s
->addr
, aurb
);
409 /* Mark it as dead (see async_complete above) */
412 int r
= ioctl(s
->fd
, USBDEVFS_DISCARDURB
, aurb
);
414 DPRINTF("husb: async. discard urb failed errno %d\n", errno
);
419 static int usb_host_open_device(int bus
, int addr
)
421 const char *usbfs
= NULL
;
426 rc
= stat("/dev/bus/usb", &st
);
427 if (rc
== 0 && S_ISDIR(st
.st_mode
)) {
428 /* udev-created device nodes available */
429 usbfs
= "/dev/bus/usb";
431 /* fallback: usbfs mounted below /proc */
432 usbfs
= "/proc/bus/usb";
435 snprintf(filename
, sizeof(filename
), "%s/%03d/%03d",
437 fd
= open(filename
, O_RDWR
| O_NONBLOCK
);
439 fprintf(stderr
, "husb: open %s: %s\n", filename
, strerror(errno
));
444 static int usb_host_claim_port(USBHostDevice
*s
)
446 #ifdef USBDEVFS_CLAIM_PORT
447 char *h
, hub_name
[64], line
[1024];
450 snprintf(hub_name
, sizeof(hub_name
), "%d-%s",
451 s
->match
.bus_num
, s
->match
.port
);
453 /* try strip off last ".$portnr" to get hub */
454 h
= strrchr(hub_name
, '.');
456 s
->hub_port
= atoi(h
+1);
459 /* no dot in there -> it is the root hub */
460 snprintf(hub_name
, sizeof(hub_name
), "usb%d",
462 s
->hub_port
= atoi(s
->match
.port
);
465 if (!usb_host_read_file(line
, sizeof(line
), "devnum",
469 if (sscanf(line
, "%d", &hub_addr
) != 1) {
473 s
->hub_fd
= usb_host_open_device(s
->match
.bus_num
, hub_addr
);
478 ret
= ioctl(s
->hub_fd
, USBDEVFS_CLAIM_PORT
, &s
->hub_port
);
485 trace_usb_host_claim_port(s
->match
.bus_num
, hub_addr
, s
->hub_port
);
492 static void usb_host_release_port(USBHostDevice
*s
)
494 if (s
->hub_fd
== -1) {
497 #ifdef USBDEVFS_RELEASE_PORT
498 ioctl(s
->hub_fd
, USBDEVFS_RELEASE_PORT
, &s
->hub_port
);
504 static int usb_host_disconnect_ifaces(USBHostDevice
*dev
, int nb_interfaces
)
506 /* earlier Linux 2.4 do not support that */
507 #ifdef USBDEVFS_DISCONNECT
508 struct usbdevfs_ioctl ctrl
;
511 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
512 ctrl
.ioctl_code
= USBDEVFS_DISCONNECT
;
513 ctrl
.ifno
= interface
;
515 ret
= ioctl(dev
->fd
, USBDEVFS_IOCTL
, &ctrl
);
516 if (ret
< 0 && errno
!= ENODATA
) {
517 perror("USBDEVFS_DISCONNECT");
525 static int usb_linux_get_num_interfaces(USBHostDevice
*s
)
527 char device_name
[64], line
[1024];
528 int num_interfaces
= 0;
530 sprintf(device_name
, "%d-%s", s
->bus_num
, s
->port
);
531 if (!usb_host_read_file(line
, sizeof(line
), "bNumInterfaces",
535 if (sscanf(line
, "%d", &num_interfaces
) != 1) {
538 return num_interfaces
;
541 static int usb_host_claim_interfaces(USBHostDevice
*dev
, int configuration
)
543 const char *op
= NULL
;
544 int dev_descr_len
, config_descr_len
;
545 int interface
, nb_interfaces
;
548 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
549 dev
->dev
.altsetting
[i
] = 0;
552 if (configuration
== 0) { /* address state - ignore */
553 dev
->dev
.ninterfaces
= 0;
554 dev
->dev
.configuration
= 0;
558 DPRINTF("husb: claiming interfaces. config %d\n", configuration
);
561 dev_descr_len
= dev
->descr
[0];
562 if (dev_descr_len
> dev
->descr_len
) {
563 fprintf(stderr
, "husb: update iface failed. descr too short\n");
568 while (i
< dev
->descr_len
) {
569 DPRINTF("husb: i is %d, descr_len is %d, dl %d, dt %d\n",
571 dev
->descr
[i
], dev
->descr
[i
+1]);
573 if (dev
->descr
[i
+1] != USB_DT_CONFIG
) {
577 config_descr_len
= dev
->descr
[i
];
579 DPRINTF("husb: config #%d need %d\n", dev
->descr
[i
+ 5], configuration
);
581 if (configuration
== dev
->descr
[i
+ 5]) {
582 configuration
= dev
->descr
[i
+ 5];
586 i
+= config_descr_len
;
589 if (i
>= dev
->descr_len
) {
591 "husb: update iface failed. no matching configuration\n");
594 nb_interfaces
= dev
->descr
[i
+ 4];
596 if (usb_host_disconnect_ifaces(dev
, nb_interfaces
) < 0) {
600 /* XXX: only grab if all interfaces are free */
601 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
602 op
= "USBDEVFS_CLAIMINTERFACE";
603 ret
= ioctl(dev
->fd
, USBDEVFS_CLAIMINTERFACE
, &interface
);
609 trace_usb_host_claim_interfaces(dev
->bus_num
, dev
->addr
,
610 nb_interfaces
, configuration
);
612 dev
->dev
.ninterfaces
= nb_interfaces
;
613 dev
->dev
.configuration
= configuration
;
617 if (errno
== ENODEV
) {
624 static int usb_host_release_interfaces(USBHostDevice
*s
)
628 trace_usb_host_release_interfaces(s
->bus_num
, s
->addr
);
630 for (i
= 0; i
< s
->dev
.ninterfaces
; i
++) {
631 ret
= ioctl(s
->fd
, USBDEVFS_RELEASEINTERFACE
, &i
);
633 perror("USBDEVFS_RELEASEINTERFACE");
640 static void usb_host_handle_reset(USBDevice
*dev
)
642 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
644 trace_usb_host_reset(s
->bus_num
, s
->addr
);
646 usb_host_do_reset(s
);;
648 usb_host_claim_interfaces(s
, 0);
649 usb_linux_update_endp_table(s
);
652 static void usb_host_handle_destroy(USBDevice
*dev
)
654 USBHostDevice
*s
= (USBHostDevice
*)dev
;
656 usb_host_release_port(s
);
658 QTAILQ_REMOVE(&hostdevs
, s
, next
);
659 qemu_remove_exit_notifier(&s
->exit
);
662 /* iso data is special, we need to keep enough urbs in flight to make sure
663 that the controller never runs out of them, otherwise the device will
664 likely suffer a buffer underrun / overrun. */
665 static AsyncURB
*usb_host_alloc_iso(USBHostDevice
*s
, int pid
, uint8_t ep
)
668 int i
, j
, len
= usb_ep_get_max_packet_size(&s
->dev
, pid
, ep
);
670 aurb
= g_malloc0(s
->iso_urb_count
* sizeof(*aurb
));
671 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
672 aurb
[i
].urb
.endpoint
= ep
;
673 aurb
[i
].urb
.buffer_length
= ISO_FRAME_DESC_PER_URB
* len
;
674 aurb
[i
].urb
.buffer
= g_malloc(aurb
[i
].urb
.buffer_length
);
675 aurb
[i
].urb
.type
= USBDEVFS_URB_TYPE_ISO
;
676 aurb
[i
].urb
.flags
= USBDEVFS_URB_ISO_ASAP
;
677 aurb
[i
].urb
.number_of_packets
= ISO_FRAME_DESC_PER_URB
;
678 for (j
= 0 ; j
< ISO_FRAME_DESC_PER_URB
; j
++)
679 aurb
[i
].urb
.iso_frame_desc
[j
].length
= len
;
680 if (pid
== USB_TOKEN_IN
) {
681 aurb
[i
].urb
.endpoint
|= 0x80;
682 /* Mark as fully consumed (idle) */
683 aurb
[i
].iso_frame_idx
= ISO_FRAME_DESC_PER_URB
;
686 set_iso_urb(s
, pid
, ep
, aurb
);
691 static void usb_host_stop_n_free_iso(USBHostDevice
*s
, int pid
, uint8_t ep
)
694 int i
, ret
, killed
= 0, free
= 1;
696 aurb
= get_iso_urb(s
, pid
, ep
);
701 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
703 if (aurb
[i
].iso_frame_idx
== -1) {
704 ret
= ioctl(s
->fd
, USBDEVFS_DISCARDURB
, &aurb
[i
]);
706 perror("USBDEVFS_DISCARDURB");
714 /* Make sure any urbs we've killed are reaped before we free them */
719 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
720 g_free(aurb
[i
].urb
.buffer
);
726 printf("husb: leaking iso urbs because of discard failure\n");
727 set_iso_urb(s
, pid
, ep
, NULL
);
728 set_iso_urb_idx(s
, pid
, ep
, 0);
729 clear_iso_started(s
, pid
, ep
);
732 static int urb_status_to_usb_ret(int status
)
736 return USB_RET_STALL
;
738 return USB_RET_BABBLE
;
740 return USB_RET_IOERROR
;
744 static int usb_host_handle_iso_data(USBHostDevice
*s
, USBPacket
*p
, int in
)
747 int i
, j
, ret
, max_packet_size
, offset
, len
= 0;
750 max_packet_size
= p
->ep
->max_packet_size
;
751 if (max_packet_size
== 0)
754 aurb
= get_iso_urb(s
, p
->pid
, p
->ep
->nr
);
756 aurb
= usb_host_alloc_iso(s
, p
->pid
, p
->ep
->nr
);
759 i
= get_iso_urb_idx(s
, p
->pid
, p
->ep
->nr
);
760 j
= aurb
[i
].iso_frame_idx
;
761 if (j
>= 0 && j
< ISO_FRAME_DESC_PER_URB
) {
763 /* Check urb status */
764 if (aurb
[i
].urb
.status
) {
765 len
= urb_status_to_usb_ret(aurb
[i
].urb
.status
);
766 /* Move to the next urb */
767 aurb
[i
].iso_frame_idx
= ISO_FRAME_DESC_PER_URB
- 1;
768 /* Check frame status */
769 } else if (aurb
[i
].urb
.iso_frame_desc
[j
].status
) {
770 len
= urb_status_to_usb_ret(
771 aurb
[i
].urb
.iso_frame_desc
[j
].status
);
772 /* Check the frame fits */
773 } else if (aurb
[i
].urb
.iso_frame_desc
[j
].actual_length
775 printf("husb: received iso data is larger then packet\n");
776 len
= USB_RET_BABBLE
;
777 /* All good copy data over */
779 len
= aurb
[i
].urb
.iso_frame_desc
[j
].actual_length
;
780 buf
= aurb
[i
].urb
.buffer
+
781 j
* aurb
[i
].urb
.iso_frame_desc
[0].length
;
782 usb_packet_copy(p
, buf
, len
);
786 offset
= (j
== 0) ? 0 : get_iso_buffer_used(s
, p
->pid
, p
->ep
->nr
);
788 /* Check the frame fits */
789 if (len
> max_packet_size
) {
790 printf("husb: send iso data is larger then max packet size\n");
794 /* All good copy data over */
795 usb_packet_copy(p
, aurb
[i
].urb
.buffer
+ offset
, len
);
796 aurb
[i
].urb
.iso_frame_desc
[j
].length
= len
;
798 set_iso_buffer_used(s
, p
->pid
, p
->ep
->nr
, offset
);
800 /* Start the stream once we have buffered enough data */
801 if (!is_iso_started(s
, p
->pid
, p
->ep
->nr
) && i
== 1 && j
== 8) {
802 set_iso_started(s
, p
->pid
, p
->ep
->nr
);
805 aurb
[i
].iso_frame_idx
++;
806 if (aurb
[i
].iso_frame_idx
== ISO_FRAME_DESC_PER_URB
) {
807 i
= (i
+ 1) % s
->iso_urb_count
;
808 set_iso_urb_idx(s
, p
->pid
, p
->ep
->nr
, i
);
812 set_iso_started(s
, p
->pid
, p
->ep
->nr
);
814 DPRINTF("hubs: iso out error no free buffer, dropping packet\n");
818 if (is_iso_started(s
, p
->pid
, p
->ep
->nr
)) {
819 /* (Re)-submit all fully consumed / filled urbs */
820 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
821 if (aurb
[i
].iso_frame_idx
== ISO_FRAME_DESC_PER_URB
) {
822 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, &aurb
[i
]);
824 perror("USBDEVFS_SUBMITURB");
825 if (!in
|| len
== 0) {
837 aurb
[i
].iso_frame_idx
= -1;
838 change_iso_inflight(s
, p
->pid
, p
->ep
->nr
, 1);
846 static int usb_host_handle_data(USBDevice
*dev
, USBPacket
*p
)
848 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
849 struct usbdevfs_urb
*urb
;
851 int ret
, rem
, prem
, v
;
855 trace_usb_host_req_data(s
->bus_num
, s
->addr
, p
,
856 p
->pid
== USB_TOKEN_IN
,
857 p
->ep
->nr
, p
->iov
.size
);
859 if (!is_valid(s
, p
->pid
, p
->ep
->nr
)) {
860 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
, USB_RET_NAK
);
864 if (p
->pid
== USB_TOKEN_IN
) {
865 ep
= p
->ep
->nr
| 0x80;
870 if (is_halted(s
, p
->pid
, p
->ep
->nr
)) {
871 unsigned int arg
= ep
;
872 ret
= ioctl(s
->fd
, USBDEVFS_CLEAR_HALT
, &arg
);
874 perror("USBDEVFS_CLEAR_HALT");
875 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
, USB_RET_NAK
);
878 clear_halt(s
, p
->pid
, p
->ep
->nr
);
881 if (is_isoc(s
, p
->pid
, p
->ep
->nr
)) {
882 return usb_host_handle_iso_data(s
, p
, p
->pid
== USB_TOKEN_IN
);
886 prem
= p
->iov
.iov
[v
].iov_len
;
887 pbuf
= p
->iov
.iov
[v
].iov_base
;
892 assert(v
< p
->iov
.niov
);
893 prem
= p
->iov
.iov
[v
].iov_len
;
894 pbuf
= p
->iov
.iov
[v
].iov_base
;
897 aurb
= async_alloc(s
);
902 urb
->type
= usb_host_usbfs_type(s
, p
);
903 urb
->usercontext
= s
;
905 urb
->buffer_length
= prem
;
907 if (urb
->buffer_length
> MAX_USBFS_BUFFER_SIZE
) {
908 urb
->buffer_length
= MAX_USBFS_BUFFER_SIZE
;
910 pbuf
+= urb
->buffer_length
;
911 prem
-= urb
->buffer_length
;
912 rem
-= urb
->buffer_length
;
917 trace_usb_host_urb_submit(s
->bus_num
, s
->addr
, aurb
,
918 urb
->buffer_length
, aurb
->more
);
919 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
921 DPRINTF("husb: data submit: ep 0x%x, len %u, more %d, packet %p, aurb %p\n",
922 urb
->endpoint
, urb
->buffer_length
, aurb
->more
, p
, aurb
);
925 perror("USBDEVFS_SUBMITURB");
930 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
935 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
937 return USB_RET_STALL
;
942 return USB_RET_ASYNC
;
945 static int ctrl_error(void)
947 if (errno
== ETIMEDOUT
) {
950 return USB_RET_STALL
;
954 static int usb_host_set_address(USBHostDevice
*s
, int addr
)
956 trace_usb_host_set_address(s
->bus_num
, s
->addr
, addr
);
961 static int usb_host_set_config(USBHostDevice
*s
, int config
)
965 trace_usb_host_set_config(s
->bus_num
, s
->addr
, config
);
967 usb_host_release_interfaces(s
);
970 ret
= ioctl(s
->fd
, USBDEVFS_SETCONFIGURATION
, &config
);
972 DPRINTF("husb: ctrl set config %d ret %d errno %d\n", config
, ret
, errno
);
974 if (ret
< 0 && errno
== EBUSY
&& first
) {
975 /* happens if usb device is in use by host drivers */
976 int count
= usb_linux_get_num_interfaces(s
);
978 DPRINTF("husb: busy -> disconnecting %d interfaces\n", count
);
979 usb_host_disconnect_ifaces(s
, count
);
988 usb_host_claim_interfaces(s
, config
);
989 usb_linux_update_endp_table(s
);
993 static int usb_host_set_interface(USBHostDevice
*s
, int iface
, int alt
)
995 struct usbdevfs_setinterface si
;
998 trace_usb_host_set_interface(s
->bus_num
, s
->addr
, iface
, alt
);
1000 for (i
= 1; i
<= USB_MAX_ENDPOINTS
; i
++) {
1001 if (is_isoc(s
, USB_TOKEN_IN
, i
)) {
1002 usb_host_stop_n_free_iso(s
, USB_TOKEN_IN
, i
);
1004 if (is_isoc(s
, USB_TOKEN_OUT
, i
)) {
1005 usb_host_stop_n_free_iso(s
, USB_TOKEN_OUT
, i
);
1009 if (iface
>= USB_MAX_INTERFACES
) {
1010 return USB_RET_STALL
;
1013 si
.interface
= iface
;
1014 si
.altsetting
= alt
;
1015 ret
= ioctl(s
->fd
, USBDEVFS_SETINTERFACE
, &si
);
1017 DPRINTF("husb: ctrl set iface %d altset %d ret %d errno %d\n",
1018 iface
, alt
, ret
, errno
);
1021 return ctrl_error();
1024 s
->dev
.altsetting
[iface
] = alt
;
1025 usb_linux_update_endp_table(s
);
1029 static int usb_host_handle_control(USBDevice
*dev
, USBPacket
*p
,
1030 int request
, int value
, int index
, int length
, uint8_t *data
)
1032 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
1033 struct usbdevfs_urb
*urb
;
1038 * Process certain standard device requests.
1039 * These are infrequent and are processed synchronously.
1042 /* Note request is (bRequestType << 8) | bRequest */
1043 trace_usb_host_req_control(s
->bus_num
, s
->addr
, p
, request
, value
, index
);
1046 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
1047 ret
= usb_host_set_address(s
, value
);
1048 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, ret
);
1051 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
1052 ret
= usb_host_set_config(s
, value
& 0xff);
1053 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, ret
);
1056 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
1057 ret
= usb_host_set_interface(s
, index
, value
);
1058 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, ret
);
1062 /* The rest are asynchronous */
1064 if (length
> sizeof(dev
->data_buf
)) {
1065 fprintf(stderr
, "husb: ctrl buffer too small (%d > %zu)\n",
1066 length
, sizeof(dev
->data_buf
));
1067 return USB_RET_STALL
;
1070 aurb
= async_alloc(s
);
1074 * Setup ctrl transfer.
1076 * s->ctrl is laid out such that data buffer immediately follows
1077 * 'req' struct which is exactly what usbdevfs expects.
1081 urb
->type
= USBDEVFS_URB_TYPE_CONTROL
;
1082 urb
->endpoint
= p
->ep
->nr
;
1084 urb
->buffer
= &dev
->setup_buf
;
1085 urb
->buffer_length
= length
+ 8;
1087 urb
->usercontext
= s
;
1089 trace_usb_host_urb_submit(s
->bus_num
, s
->addr
, aurb
,
1090 urb
->buffer_length
, aurb
->more
);
1091 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
1093 DPRINTF("husb: submit ctrl. len %u aurb %p\n", urb
->buffer_length
, aurb
);
1096 DPRINTF("husb: submit failed. errno %d\n", errno
);
1104 return USB_RET_STALL
;
1108 return USB_RET_ASYNC
;
1111 static uint8_t usb_linux_get_alt_setting(USBHostDevice
*s
,
1112 uint8_t configuration
, uint8_t interface
)
1114 char device_name
[64], line
[1024];
1117 sprintf(device_name
, "%d-%s:%d.%d", s
->bus_num
, s
->port
,
1118 (int)configuration
, (int)interface
);
1120 if (!usb_host_read_file(line
, sizeof(line
), "bAlternateSetting",
1122 /* Assume alt 0 on error */
1125 if (sscanf(line
, "%d", &alt_setting
) != 1) {
1126 /* Assume alt 0 on error */
1132 /* returns 1 on problem encountered or 0 for success */
1133 static int usb_linux_update_endp_table(USBHostDevice
*s
)
1135 uint8_t *descriptors
;
1136 uint8_t devep
, type
, alt_interface
;
1138 int interface
, length
, i
, ep
, pid
;
1139 struct endp_data
*epd
;
1141 usb_ep_init(&s
->dev
);
1143 if (s
->dev
.configuration
== 0) {
1144 /* not configured yet -- leave all endpoints disabled */
1148 /* get the desired configuration, interface, and endpoint descriptors
1149 * from device description */
1150 descriptors
= &s
->descr
[18];
1151 length
= s
->descr_len
- 18;
1154 while (i
< length
) {
1155 if (descriptors
[i
+ 1] != USB_DT_CONFIG
) {
1156 fprintf(stderr
, "invalid descriptor data\n");
1158 } else if (descriptors
[i
+ 5] != s
->dev
.configuration
) {
1159 DPRINTF("not requested configuration %d\n", s
->dev
.configuration
);
1160 i
+= (descriptors
[i
+ 3] << 8) + descriptors
[i
+ 2];
1163 i
+= descriptors
[i
];
1165 if (descriptors
[i
+ 1] != USB_DT_INTERFACE
||
1166 (descriptors
[i
+ 1] == USB_DT_INTERFACE
&&
1167 descriptors
[i
+ 4] == 0)) {
1168 i
+= descriptors
[i
];
1172 interface
= descriptors
[i
+ 2];
1173 alt_interface
= usb_linux_get_alt_setting(s
, s
->dev
.configuration
,
1176 /* the current interface descriptor is the active interface
1177 * and has endpoints */
1178 if (descriptors
[i
+ 3] != alt_interface
) {
1179 i
+= descriptors
[i
];
1183 /* advance to the endpoints */
1184 while (i
< length
&& descriptors
[i
+1] != USB_DT_ENDPOINT
) {
1185 i
+= descriptors
[i
];
1191 while (i
< length
) {
1192 if (descriptors
[i
+ 1] != USB_DT_ENDPOINT
) {
1196 devep
= descriptors
[i
+ 2];
1197 pid
= (devep
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
1200 fprintf(stderr
, "usb-linux: invalid ep descriptor, ep == 0\n");
1204 type
= descriptors
[i
+ 3] & 0x3;
1205 raw
= descriptors
[i
+ 4] + (descriptors
[i
+ 5] << 8);
1206 usb_ep_set_max_packet_size(&s
->dev
, pid
, ep
, raw
);
1207 assert(usb_ep_get_type(&s
->dev
, pid
, ep
) ==
1208 USB_ENDPOINT_XFER_INVALID
);
1209 usb_ep_set_type(&s
->dev
, pid
, ep
, type
);
1210 usb_ep_set_ifnum(&s
->dev
, pid
, ep
, interface
);
1211 if ((s
->options
& (1 << USB_HOST_OPT_PIPELINE
)) &&
1212 (type
== USB_ENDPOINT_XFER_BULK
)) {
1213 usb_ep_set_pipeline(&s
->dev
, pid
, ep
, true);
1216 epd
= get_endp(s
, pid
, ep
);
1219 i
+= descriptors
[i
];
1223 usb_ep_dump(&s
->dev
);
1229 * Check if we can safely redirect a usb2 device to a usb1 virtual controller,
1230 * this function assumes this is safe, if:
1231 * 1) There are no isoc endpoints
1232 * 2) There are no interrupt endpoints with a max_packet_size > 64
1233 * Note bulk endpoints with a max_packet_size > 64 in theory also are not
1234 * usb1 compatible, but in practice this seems to work fine.
1236 static int usb_linux_full_speed_compat(USBHostDevice
*dev
)
1241 * usb_linux_update_endp_table only registers info about ep in the current
1242 * interface altsettings, so we need to parse the descriptors again.
1244 for (i
= 0; (i
+ 5) < dev
->descr_len
; i
+= dev
->descr
[i
]) {
1245 if (dev
->descr
[i
+ 1] == USB_DT_ENDPOINT
) {
1246 switch (dev
->descr
[i
+ 3] & 0x3) {
1247 case 0x00: /* CONTROL */
1249 case 0x01: /* ISO */
1251 case 0x02: /* BULK */
1253 case 0x03: /* INTERRUPT */
1254 packet_size
= dev
->descr
[i
+ 4] + (dev
->descr
[i
+ 5] << 8);
1255 if (packet_size
> 64)
1264 static int usb_host_open(USBHostDevice
*dev
, int bus_num
,
1265 int addr
, const char *port
,
1266 const char *prod_name
, int speed
)
1270 trace_usb_host_open_started(bus_num
, addr
);
1272 if (dev
->fd
!= -1) {
1276 fd
= usb_host_open_device(bus_num
, addr
);
1280 DPRINTF("husb: opened %s\n", buf
);
1282 dev
->bus_num
= bus_num
;
1284 strcpy(dev
->port
, port
);
1287 /* read the device description */
1288 dev
->descr_len
= read(fd
, dev
->descr
, sizeof(dev
->descr
));
1289 if (dev
->descr_len
<= 0) {
1290 perror("husb: reading device data failed");
1297 printf("=== begin dumping device descriptor data ===\n");
1298 for (x
= 0; x
< dev
->descr_len
; x
++) {
1299 printf("%02x ", dev
->descr
[x
]);
1301 printf("\n=== end dumping device descriptor data ===\n");
1306 /* start unconfigured -- we'll wait for the guest to set a configuration */
1307 if (!usb_host_claim_interfaces(dev
, 0)) {
1311 ret
= usb_linux_update_endp_table(dev
);
1317 struct usbdevfs_connectinfo ci
;
1319 ret
= ioctl(fd
, USBDEVFS_CONNECTINFO
, &ci
);
1321 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
1326 speed
= USB_SPEED_LOW
;
1328 speed
= USB_SPEED_HIGH
;
1331 dev
->dev
.speed
= speed
;
1332 dev
->dev
.speedmask
= (1 << speed
);
1333 if (dev
->dev
.speed
== USB_SPEED_HIGH
&& usb_linux_full_speed_compat(dev
)) {
1334 dev
->dev
.speedmask
|= USB_SPEED_MASK_FULL
;
1337 trace_usb_host_open_success(bus_num
, addr
);
1339 if (!prod_name
|| prod_name
[0] == '\0') {
1340 snprintf(dev
->dev
.product_desc
, sizeof(dev
->dev
.product_desc
),
1341 "host:%d.%d", bus_num
, addr
);
1343 pstrcpy(dev
->dev
.product_desc
, sizeof(dev
->dev
.product_desc
),
1347 ret
= usb_device_attach(&dev
->dev
);
1352 /* USB devio uses 'write' flag to check for async completions */
1353 qemu_set_fd_handler(dev
->fd
, NULL
, async_complete
, dev
);
1358 trace_usb_host_open_failure(bus_num
, addr
);
1359 if (dev
->fd
!= -1) {
1366 static int usb_host_close(USBHostDevice
*dev
)
1370 if (dev
->fd
== -1) {
1374 trace_usb_host_close(dev
->bus_num
, dev
->addr
);
1376 qemu_set_fd_handler(dev
->fd
, NULL
, NULL
, NULL
);
1378 for (i
= 1; i
<= USB_MAX_ENDPOINTS
; i
++) {
1379 if (is_isoc(dev
, USB_TOKEN_IN
, i
)) {
1380 usb_host_stop_n_free_iso(dev
, USB_TOKEN_IN
, i
);
1382 if (is_isoc(dev
, USB_TOKEN_OUT
, i
)) {
1383 usb_host_stop_n_free_iso(dev
, USB_TOKEN_OUT
, i
);
1386 async_complete(dev
);
1388 if (dev
->dev
.attached
) {
1389 usb_device_detach(&dev
->dev
);
1391 usb_host_do_reset(dev
);
1397 static void usb_host_exit_notifier(struct Notifier
*n
, void *data
)
1399 USBHostDevice
*s
= container_of(n
, USBHostDevice
, exit
);
1401 usb_host_release_port(s
);
1403 usb_host_do_reset(s
);;
1407 static int usb_host_initfn(USBDevice
*dev
)
1409 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
1411 dev
->auto_attach
= 0;
1415 QTAILQ_INSERT_TAIL(&hostdevs
, s
, next
);
1416 s
->exit
.notify
= usb_host_exit_notifier
;
1417 qemu_add_exit_notifier(&s
->exit
);
1418 usb_host_auto_check(NULL
);
1420 if (s
->match
.bus_num
!= 0 && s
->match
.port
!= NULL
) {
1421 usb_host_claim_port(s
);
1423 add_boot_device_path(s
->bootindex
, &dev
->qdev
, NULL
);
1427 static const VMStateDescription vmstate_usb_host
= {
1432 static Property usb_host_dev_properties
[] = {
1433 DEFINE_PROP_UINT32("hostbus", USBHostDevice
, match
.bus_num
, 0),
1434 DEFINE_PROP_UINT32("hostaddr", USBHostDevice
, match
.addr
, 0),
1435 DEFINE_PROP_STRING("hostport", USBHostDevice
, match
.port
),
1436 DEFINE_PROP_HEX32("vendorid", USBHostDevice
, match
.vendor_id
, 0),
1437 DEFINE_PROP_HEX32("productid", USBHostDevice
, match
.product_id
, 0),
1438 DEFINE_PROP_UINT32("isobufs", USBHostDevice
, iso_urb_count
, 4),
1439 DEFINE_PROP_INT32("bootindex", USBHostDevice
, bootindex
, -1),
1440 DEFINE_PROP_BIT("pipeline", USBHostDevice
, options
,
1441 USB_HOST_OPT_PIPELINE
, true),
1442 DEFINE_PROP_END_OF_LIST(),
1445 static void usb_host_class_initfn(ObjectClass
*klass
, void *data
)
1447 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1448 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
1450 uc
->init
= usb_host_initfn
;
1451 uc
->product_desc
= "USB Host Device";
1452 uc
->cancel_packet
= usb_host_async_cancel
;
1453 uc
->handle_data
= usb_host_handle_data
;
1454 uc
->handle_control
= usb_host_handle_control
;
1455 uc
->handle_reset
= usb_host_handle_reset
;
1456 uc
->handle_destroy
= usb_host_handle_destroy
;
1457 dc
->vmsd
= &vmstate_usb_host
;
1458 dc
->props
= usb_host_dev_properties
;
1461 static TypeInfo usb_host_dev_info
= {
1463 .parent
= TYPE_USB_DEVICE
,
1464 .instance_size
= sizeof(USBHostDevice
),
1465 .class_init
= usb_host_class_initfn
,
1468 static void usb_host_register_types(void)
1470 type_register_static(&usb_host_dev_info
);
1471 usb_legacy_register("usb-host", "host", usb_host_device_open
);
1474 type_init(usb_host_register_types
)
1476 USBDevice
*usb_host_device_open(USBBus
*bus
, const char *devname
)
1478 struct USBAutoFilter filter
;
1482 dev
= usb_create(bus
, "usb-host");
1484 if (strstr(devname
, "auto:")) {
1485 if (parse_filter(devname
, &filter
) < 0) {
1489 if ((p
= strchr(devname
, '.'))) {
1490 filter
.bus_num
= strtoul(devname
, NULL
, 0);
1491 filter
.addr
= strtoul(p
+ 1, NULL
, 0);
1492 filter
.vendor_id
= 0;
1493 filter
.product_id
= 0;
1494 } else if ((p
= strchr(devname
, ':'))) {
1497 filter
.vendor_id
= strtoul(devname
, NULL
, 16);
1498 filter
.product_id
= strtoul(p
+ 1, NULL
, 16);
1504 qdev_prop_set_uint32(&dev
->qdev
, "hostbus", filter
.bus_num
);
1505 qdev_prop_set_uint32(&dev
->qdev
, "hostaddr", filter
.addr
);
1506 qdev_prop_set_uint32(&dev
->qdev
, "vendorid", filter
.vendor_id
);
1507 qdev_prop_set_uint32(&dev
->qdev
, "productid", filter
.product_id
);
1508 qdev_init_nofail(&dev
->qdev
);
1512 qdev_free(&dev
->qdev
);
1516 int usb_host_device_close(const char *devname
)
1519 char product_name
[PRODUCT_NAME_SZ
];
1523 if (strstr(devname
, "auto:")) {
1524 return usb_host_auto_del(devname
);
1526 if (usb_host_find_device(&bus_num
, &addr
, product_name
,
1527 sizeof(product_name
), devname
) < 0) {
1530 s
= hostdev_find(bus_num
, addr
);
1532 usb_device_delete_addr(s
->bus_num
, s
->dev
.addr
);
1541 * Read sys file-system device file
1543 * @line address of buffer to put file contents in
1544 * @line_size size of line
1545 * @device_file path to device file (printf format string)
1546 * @device_name device being opened (inserted into device_file)
1548 * @return 0 failed, 1 succeeded ('line' contains data)
1550 static int usb_host_read_file(char *line
, size_t line_size
,
1551 const char *device_file
, const char *device_name
)
1555 char filename
[PATH_MAX
];
1557 snprintf(filename
, PATH_MAX
, "/sys/bus/usb/devices/%s/%s", device_name
,
1559 f
= fopen(filename
, "r");
1561 ret
= fgets(line
, line_size
, f
) != NULL
;
1569 * Use /sys/bus/usb/devices/ directory to determine host's USB
1572 * This code is based on Robert Schiele's original patches posted to
1573 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1575 static int usb_host_scan(void *opaque
, USBScanFunc
*func
)
1579 int bus_num
, addr
, speed
, class_id
, product_id
, vendor_id
;
1581 char port
[MAX_PORTLEN
];
1582 char product_name
[512];
1585 dir
= opendir("/sys/bus/usb/devices");
1587 perror("husb: opendir /sys/bus/usb/devices");
1588 fprintf(stderr
, "husb: please make sure sysfs is mounted at /sys\n");
1592 while ((de
= readdir(dir
))) {
1593 if (de
->d_name
[0] != '.' && !strchr(de
->d_name
, ':')) {
1594 if (sscanf(de
->d_name
, "%d-%7[0-9.]", &bus_num
, port
) < 2) {
1598 if (!usb_host_read_file(line
, sizeof(line
), "devnum", de
->d_name
)) {
1601 if (sscanf(line
, "%d", &addr
) != 1) {
1604 if (!usb_host_read_file(line
, sizeof(line
), "bDeviceClass",
1608 if (sscanf(line
, "%x", &class_id
) != 1) {
1612 if (!usb_host_read_file(line
, sizeof(line
), "idVendor",
1616 if (sscanf(line
, "%x", &vendor_id
) != 1) {
1619 if (!usb_host_read_file(line
, sizeof(line
), "idProduct",
1623 if (sscanf(line
, "%x", &product_id
) != 1) {
1626 if (!usb_host_read_file(line
, sizeof(line
), "product",
1630 if (strlen(line
) > 0) {
1631 line
[strlen(line
) - 1] = '\0';
1633 pstrcpy(product_name
, sizeof(product_name
), line
);
1636 if (!usb_host_read_file(line
, sizeof(line
), "speed", de
->d_name
)) {
1639 if (!strcmp(line
, "5000\n")) {
1640 speed
= USB_SPEED_SUPER
;
1641 } else if (!strcmp(line
, "480\n")) {
1642 speed
= USB_SPEED_HIGH
;
1643 } else if (!strcmp(line
, "1.5\n")) {
1644 speed
= USB_SPEED_LOW
;
1646 speed
= USB_SPEED_FULL
;
1649 ret
= func(opaque
, bus_num
, addr
, port
, class_id
, vendor_id
,
1650 product_id
, product_name
, speed
);
1663 static QEMUTimer
*usb_auto_timer
;
1665 static int usb_host_auto_scan(void *opaque
, int bus_num
,
1666 int addr
, const char *port
,
1667 int class_id
, int vendor_id
, int product_id
,
1668 const char *product_name
, int speed
)
1670 struct USBAutoFilter
*f
;
1671 struct USBHostDevice
*s
;
1677 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1680 if (f
->bus_num
> 0 && f
->bus_num
!= bus_num
) {
1683 if (f
->addr
> 0 && f
->addr
!= addr
) {
1686 if (f
->port
!= NULL
&& (port
== NULL
|| strcmp(f
->port
, port
) != 0)) {
1690 if (f
->vendor_id
> 0 && f
->vendor_id
!= vendor_id
) {
1694 if (f
->product_id
> 0 && f
->product_id
!= product_id
) {
1697 /* We got a match */
1699 if (s
->errcount
>= 3) {
1703 /* Already attached ? */
1707 DPRINTF("husb: auto open: bus_num %d addr %d\n", bus_num
, addr
);
1709 if (usb_host_open(s
, bus_num
, addr
, port
, product_name
, speed
) < 0) {
1718 static void usb_host_auto_check(void *unused
)
1720 struct USBHostDevice
*s
;
1721 int unconnected
= 0;
1723 usb_host_scan(NULL
, usb_host_auto_scan
);
1725 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1735 if (unconnected
== 0) {
1736 /* nothing to watch */
1737 if (usb_auto_timer
) {
1738 qemu_del_timer(usb_auto_timer
);
1739 trace_usb_host_auto_scan_disabled();
1744 if (!usb_auto_timer
) {
1745 usb_auto_timer
= qemu_new_timer_ms(rt_clock
, usb_host_auto_check
, NULL
);
1746 if (!usb_auto_timer
) {
1749 trace_usb_host_auto_scan_enabled();
1751 qemu_mod_timer(usb_auto_timer
, qemu_get_clock_ms(rt_clock
) + 2000);
1755 * Autoconnect filter
1757 * auto:bus:dev[:vid:pid]
1758 * auto:bus.dev[:vid:pid]
1760 * bus - bus number (dec, * means any)
1761 * dev - device number (dec, * means any)
1762 * vid - vendor id (hex, * means any)
1763 * pid - product id (hex, * means any)
1765 * See 'lsusb' output.
1767 static int parse_filter(const char *spec
, struct USBAutoFilter
*f
)
1769 enum { BUS
, DEV
, VID
, PID
, DONE
};
1770 const char *p
= spec
;
1778 for (i
= BUS
; i
< DONE
; i
++) {
1779 p
= strpbrk(p
, ":.");
1789 case BUS
: f
->bus_num
= strtol(p
, NULL
, 10); break;
1790 case DEV
: f
->addr
= strtol(p
, NULL
, 10); break;
1791 case VID
: f
->vendor_id
= strtol(p
, NULL
, 16); break;
1792 case PID
: f
->product_id
= strtol(p
, NULL
, 16); break;
1797 fprintf(stderr
, "husb: invalid auto filter spec %s\n", spec
);
1804 /**********************/
1805 /* USB host device info */
1807 struct usb_class_info
{
1809 const char *class_name
;
1812 static const struct usb_class_info usb_class_info
[] = {
1813 { USB_CLASS_AUDIO
, "Audio"},
1814 { USB_CLASS_COMM
, "Communication"},
1815 { USB_CLASS_HID
, "HID"},
1816 { USB_CLASS_HUB
, "Hub" },
1817 { USB_CLASS_PHYSICAL
, "Physical" },
1818 { USB_CLASS_PRINTER
, "Printer" },
1819 { USB_CLASS_MASS_STORAGE
, "Storage" },
1820 { USB_CLASS_CDC_DATA
, "Data" },
1821 { USB_CLASS_APP_SPEC
, "Application Specific" },
1822 { USB_CLASS_VENDOR_SPEC
, "Vendor Specific" },
1823 { USB_CLASS_STILL_IMAGE
, "Still Image" },
1824 { USB_CLASS_CSCID
, "Smart Card" },
1825 { USB_CLASS_CONTENT_SEC
, "Content Security" },
1829 static const char *usb_class_str(uint8_t class)
1831 const struct usb_class_info
*p
;
1832 for(p
= usb_class_info
; p
->class != -1; p
++) {
1833 if (p
->class == class) {
1837 return p
->class_name
;
1840 static void usb_info_device(Monitor
*mon
, int bus_num
,
1841 int addr
, const char *port
,
1842 int class_id
, int vendor_id
, int product_id
,
1843 const char *product_name
,
1846 const char *class_str
, *speed_str
;
1852 case USB_SPEED_FULL
:
1855 case USB_SPEED_HIGH
:
1858 case USB_SPEED_SUPER
:
1866 monitor_printf(mon
, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
1867 bus_num
, addr
, port
, speed_str
);
1868 class_str
= usb_class_str(class_id
);
1870 monitor_printf(mon
, " %s:", class_str
);
1872 monitor_printf(mon
, " Class %02x:", class_id
);
1874 monitor_printf(mon
, " USB device %04x:%04x", vendor_id
, product_id
);
1875 if (product_name
[0] != '\0') {
1876 monitor_printf(mon
, ", %s", product_name
);
1878 monitor_printf(mon
, "\n");
1881 static int usb_host_info_device(void *opaque
, int bus_num
, int addr
,
1882 const char *path
, int class_id
,
1883 int vendor_id
, int product_id
,
1884 const char *product_name
,
1887 Monitor
*mon
= opaque
;
1889 usb_info_device(mon
, bus_num
, addr
, path
, class_id
, vendor_id
, product_id
,
1890 product_name
, speed
);
1894 static void dec2str(int val
, char *str
, size_t size
)
1897 snprintf(str
, size
, "*");
1899 snprintf(str
, size
, "%d", val
);
1903 static void hex2str(int val
, char *str
, size_t size
)
1906 snprintf(str
, size
, "*");
1908 snprintf(str
, size
, "%04x", val
);
1912 void usb_host_info(Monitor
*mon
)
1914 struct USBAutoFilter
*f
;
1915 struct USBHostDevice
*s
;
1917 usb_host_scan(mon
, usb_host_info_device
);
1919 if (QTAILQ_EMPTY(&hostdevs
)) {
1923 monitor_printf(mon
, " Auto filters:\n");
1924 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1925 char bus
[10], addr
[10], vid
[10], pid
[10];
1927 dec2str(f
->bus_num
, bus
, sizeof(bus
));
1928 dec2str(f
->addr
, addr
, sizeof(addr
));
1929 hex2str(f
->vendor_id
, vid
, sizeof(vid
));
1930 hex2str(f
->product_id
, pid
, sizeof(pid
));
1931 monitor_printf(mon
, " Bus %s, Addr %s, Port %s, ID %s:%s\n",
1932 bus
, addr
, f
->port
? f
->port
: "*", vid
, pid
);