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 typedef struct USBHostDevice
{
106 uint32_t iso_urb_count
;
109 struct endp_data ep_in
[USB_MAX_ENDPOINTS
];
110 struct endp_data ep_out
[USB_MAX_ENDPOINTS
];
111 QLIST_HEAD(, AsyncURB
) aurbs
;
113 /* Host side address */
116 char port
[MAX_PORTLEN
];
117 struct USBAutoFilter match
;
120 QTAILQ_ENTRY(USBHostDevice
) next
;
123 static QTAILQ_HEAD(, USBHostDevice
) hostdevs
= QTAILQ_HEAD_INITIALIZER(hostdevs
);
125 static int usb_host_close(USBHostDevice
*dev
);
126 static int parse_filter(const char *spec
, struct USBAutoFilter
*f
);
127 static void usb_host_auto_check(void *unused
);
128 static int usb_host_read_file(char *line
, size_t line_size
,
129 const char *device_file
, const char *device_name
);
130 static int usb_linux_update_endp_table(USBHostDevice
*s
);
132 static int usb_host_usbfs_type(USBHostDevice
*s
, USBPacket
*p
)
134 static const int usbfs
[] = {
135 [USB_ENDPOINT_XFER_CONTROL
] = USBDEVFS_URB_TYPE_CONTROL
,
136 [USB_ENDPOINT_XFER_ISOC
] = USBDEVFS_URB_TYPE_ISO
,
137 [USB_ENDPOINT_XFER_BULK
] = USBDEVFS_URB_TYPE_BULK
,
138 [USB_ENDPOINT_XFER_INT
] = USBDEVFS_URB_TYPE_INTERRUPT
,
140 uint8_t type
= p
->ep
->type
;
141 assert(type
< ARRAY_SIZE(usbfs
));
145 static int usb_host_do_reset(USBHostDevice
*dev
)
151 gettimeofday(&s
, NULL
);
152 ret
= ioctl(dev
->fd
, USBDEVFS_RESET
);
153 gettimeofday(&e
, NULL
);
154 usecs
= (e
.tv_sec
- s
.tv_sec
) * 1000000;
155 usecs
+= e
.tv_usec
- s
.tv_usec
;
156 if (usecs
> 1000000) {
157 /* more than a second, something is fishy, broken usb device? */
158 fprintf(stderr
, "husb: device %d:%d reset took %d.%06d seconds\n",
159 dev
->bus_num
, dev
->addr
, usecs
/ 1000000, usecs
% 1000000);
164 static struct endp_data
*get_endp(USBHostDevice
*s
, int pid
, int ep
)
166 struct endp_data
*eps
= pid
== USB_TOKEN_IN
? s
->ep_in
: s
->ep_out
;
167 assert(pid
== USB_TOKEN_IN
|| pid
== USB_TOKEN_OUT
);
168 assert(ep
> 0 && ep
<= USB_MAX_ENDPOINTS
);
172 static int is_isoc(USBHostDevice
*s
, int pid
, int ep
)
174 return usb_ep_get_type(&s
->dev
, pid
, ep
) == USB_ENDPOINT_XFER_ISOC
;
177 static int is_valid(USBHostDevice
*s
, int pid
, int ep
)
179 return usb_ep_get_type(&s
->dev
, pid
, ep
) != USB_ENDPOINT_XFER_INVALID
;
182 static int is_halted(USBHostDevice
*s
, int pid
, int ep
)
184 return get_endp(s
, pid
, ep
)->halted
;
187 static void clear_halt(USBHostDevice
*s
, int pid
, int ep
)
189 trace_usb_host_ep_clear_halt(s
->bus_num
, s
->addr
, ep
);
190 get_endp(s
, pid
, ep
)->halted
= 0;
193 static void set_halt(USBHostDevice
*s
, int pid
, int ep
)
196 trace_usb_host_ep_set_halt(s
->bus_num
, s
->addr
, ep
);
197 get_endp(s
, pid
, ep
)->halted
= 1;
201 static int is_iso_started(USBHostDevice
*s
, int pid
, int ep
)
203 return get_endp(s
, pid
, ep
)->iso_started
;
206 static void clear_iso_started(USBHostDevice
*s
, int pid
, int ep
)
208 trace_usb_host_ep_stop_iso(s
->bus_num
, s
->addr
, ep
);
209 get_endp(s
, pid
, ep
)->iso_started
= 0;
212 static void set_iso_started(USBHostDevice
*s
, int pid
, int ep
)
214 struct endp_data
*e
= get_endp(s
, pid
, ep
);
216 trace_usb_host_ep_start_iso(s
->bus_num
, s
->addr
, ep
);
217 if (!e
->iso_started
) {
223 static int change_iso_inflight(USBHostDevice
*s
, int pid
, int ep
, int value
)
225 struct endp_data
*e
= get_endp(s
, pid
, ep
);
227 e
->inflight
+= value
;
231 static void set_iso_urb(USBHostDevice
*s
, int pid
, int ep
, AsyncURB
*iso_urb
)
233 get_endp(s
, pid
, ep
)->iso_urb
= iso_urb
;
236 static AsyncURB
*get_iso_urb(USBHostDevice
*s
, int pid
, int ep
)
238 return get_endp(s
, pid
, ep
)->iso_urb
;
241 static void set_iso_urb_idx(USBHostDevice
*s
, int pid
, int ep
, int i
)
243 get_endp(s
, pid
, ep
)->iso_urb_idx
= i
;
246 static int get_iso_urb_idx(USBHostDevice
*s
, int pid
, int ep
)
248 return get_endp(s
, pid
, ep
)->iso_urb_idx
;
251 static void set_iso_buffer_used(USBHostDevice
*s
, int pid
, int ep
, int i
)
253 get_endp(s
, pid
, ep
)->iso_buffer_used
= i
;
256 static int get_iso_buffer_used(USBHostDevice
*s
, int pid
, int ep
)
258 return get_endp(s
, pid
, ep
)->iso_buffer_used
;
263 * We always allocate iso packet descriptors even for bulk transfers
264 * to simplify allocation and casts.
268 struct usbdevfs_urb urb
;
269 struct usbdevfs_iso_packet_desc isocpd
[ISO_FRAME_DESC_PER_URB
];
271 QLIST_ENTRY(AsyncURB
) next
;
273 /* For regular async urbs */
275 int more
; /* large transfer, more urbs follow */
277 /* For buffered iso handling */
278 int iso_frame_idx
; /* -1 means in flight */
281 static AsyncURB
*async_alloc(USBHostDevice
*s
)
283 AsyncURB
*aurb
= g_malloc0(sizeof(AsyncURB
));
285 QLIST_INSERT_HEAD(&s
->aurbs
, aurb
, next
);
289 static void async_free(AsyncURB
*aurb
)
291 QLIST_REMOVE(aurb
, next
);
295 static void do_disconnect(USBHostDevice
*s
)
298 usb_host_auto_check(NULL
);
301 static void async_complete(void *opaque
)
303 USBHostDevice
*s
= opaque
;
310 int r
= ioctl(s
->fd
, USBDEVFS_REAPURBNDELAY
, &aurb
);
312 if (errno
== EAGAIN
) {
314 fprintf(stderr
, "husb: %d iso urbs finished at once\n", urbs
);
318 if (errno
== ENODEV
) {
320 trace_usb_host_disconnect(s
->bus_num
, s
->addr
);
326 perror("USBDEVFS_REAPURBNDELAY");
330 DPRINTF("husb: async completed. aurb %p status %d alen %d\n",
331 aurb
, aurb
->urb
.status
, aurb
->urb
.actual_length
);
333 /* If this is a buffered iso urb mark it as complete and don't do
334 anything else (it is handled further in usb_host_handle_iso_data) */
335 if (aurb
->iso_frame_idx
== -1) {
337 int pid
= (aurb
->urb
.endpoint
& USB_DIR_IN
) ?
338 USB_TOKEN_IN
: USB_TOKEN_OUT
;
339 int ep
= aurb
->urb
.endpoint
& 0xf;
340 if (aurb
->urb
.status
== -EPIPE
) {
341 set_halt(s
, pid
, ep
);
343 aurb
->iso_frame_idx
= 0;
345 inflight
= change_iso_inflight(s
, pid
, ep
, -1);
346 if (inflight
== 0 && is_iso_started(s
, pid
, ep
)) {
347 fprintf(stderr
, "husb: out of buffers for iso stream\n");
353 trace_usb_host_urb_complete(s
->bus_num
, s
->addr
, aurb
, aurb
->urb
.status
,
354 aurb
->urb
.actual_length
, aurb
->more
);
357 switch (aurb
->urb
.status
) {
359 p
->result
+= aurb
->urb
.actual_length
;
363 set_halt(s
, p
->pid
, p
->ep
->nr
);
364 p
->result
= USB_RET_STALL
;
368 p
->result
= USB_RET_BABBLE
;
372 p
->result
= USB_RET_IOERROR
;
376 if (aurb
->urb
.type
== USBDEVFS_URB_TYPE_CONTROL
) {
377 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
->result
);
378 usb_generic_async_ctrl_complete(&s
->dev
, p
);
379 } else if (!aurb
->more
) {
380 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
->result
);
381 usb_packet_complete(&s
->dev
, p
);
389 static void usb_host_async_cancel(USBDevice
*dev
, USBPacket
*p
)
391 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
394 QLIST_FOREACH(aurb
, &s
->aurbs
, next
) {
395 if (p
!= aurb
->packet
) {
399 DPRINTF("husb: async cancel: packet %p, aurb %p\n", p
, aurb
);
401 /* Mark it as dead (see async_complete above) */
404 int r
= ioctl(s
->fd
, USBDEVFS_DISCARDURB
, aurb
);
406 DPRINTF("husb: async. discard urb failed errno %d\n", errno
);
411 static int usb_host_open_device(int bus
, int addr
)
413 const char *usbfs
= NULL
;
418 rc
= stat("/dev/bus/usb", &st
);
419 if (rc
== 0 && S_ISDIR(st
.st_mode
)) {
420 /* udev-created device nodes available */
421 usbfs
= "/dev/bus/usb";
423 /* fallback: usbfs mounted below /proc */
424 usbfs
= "/proc/bus/usb";
427 snprintf(filename
, sizeof(filename
), "%s/%03d/%03d",
429 fd
= open(filename
, O_RDWR
| O_NONBLOCK
);
431 fprintf(stderr
, "husb: open %s: %s\n", filename
, strerror(errno
));
436 static int usb_host_claim_port(USBHostDevice
*s
)
438 #ifdef USBDEVFS_CLAIM_PORT
439 char *h
, hub_name
[64], line
[1024];
442 snprintf(hub_name
, sizeof(hub_name
), "%d-%s",
443 s
->match
.bus_num
, s
->match
.port
);
445 /* try strip off last ".$portnr" to get hub */
446 h
= strrchr(hub_name
, '.');
448 s
->hub_port
= atoi(h
+1);
451 /* no dot in there -> it is the root hub */
452 snprintf(hub_name
, sizeof(hub_name
), "usb%d",
454 s
->hub_port
= atoi(s
->match
.port
);
457 if (!usb_host_read_file(line
, sizeof(line
), "devnum",
461 if (sscanf(line
, "%d", &hub_addr
) != 1) {
465 s
->hub_fd
= usb_host_open_device(s
->match
.bus_num
, hub_addr
);
470 ret
= ioctl(s
->hub_fd
, USBDEVFS_CLAIM_PORT
, &s
->hub_port
);
477 trace_usb_host_claim_port(s
->match
.bus_num
, hub_addr
, s
->hub_port
);
484 static void usb_host_release_port(USBHostDevice
*s
)
486 if (s
->hub_fd
== -1) {
489 #ifdef USBDEVFS_RELEASE_PORT
490 ioctl(s
->hub_fd
, USBDEVFS_RELEASE_PORT
, &s
->hub_port
);
496 static int usb_host_disconnect_ifaces(USBHostDevice
*dev
, int nb_interfaces
)
498 /* earlier Linux 2.4 do not support that */
499 #ifdef USBDEVFS_DISCONNECT
500 struct usbdevfs_ioctl ctrl
;
503 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
504 ctrl
.ioctl_code
= USBDEVFS_DISCONNECT
;
505 ctrl
.ifno
= interface
;
507 ret
= ioctl(dev
->fd
, USBDEVFS_IOCTL
, &ctrl
);
508 if (ret
< 0 && errno
!= ENODATA
) {
509 perror("USBDEVFS_DISCONNECT");
517 static int usb_linux_get_num_interfaces(USBHostDevice
*s
)
519 char device_name
[64], line
[1024];
520 int num_interfaces
= 0;
522 sprintf(device_name
, "%d-%s", s
->bus_num
, s
->port
);
523 if (!usb_host_read_file(line
, sizeof(line
), "bNumInterfaces",
527 if (sscanf(line
, "%d", &num_interfaces
) != 1) {
530 return num_interfaces
;
533 static int usb_host_claim_interfaces(USBHostDevice
*dev
, int configuration
)
535 const char *op
= NULL
;
536 int dev_descr_len
, config_descr_len
;
537 int interface
, nb_interfaces
;
540 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
541 dev
->dev
.altsetting
[i
] = 0;
544 if (configuration
== 0) { /* address state - ignore */
545 dev
->dev
.ninterfaces
= 0;
546 dev
->dev
.configuration
= 0;
550 DPRINTF("husb: claiming interfaces. config %d\n", configuration
);
553 dev_descr_len
= dev
->descr
[0];
554 if (dev_descr_len
> dev
->descr_len
) {
555 fprintf(stderr
, "husb: update iface failed. descr too short\n");
560 while (i
< dev
->descr_len
) {
561 DPRINTF("husb: i is %d, descr_len is %d, dl %d, dt %d\n",
563 dev
->descr
[i
], dev
->descr
[i
+1]);
565 if (dev
->descr
[i
+1] != USB_DT_CONFIG
) {
569 config_descr_len
= dev
->descr
[i
];
571 DPRINTF("husb: config #%d need %d\n", dev
->descr
[i
+ 5], configuration
);
573 if (configuration
== dev
->descr
[i
+ 5]) {
574 configuration
= dev
->descr
[i
+ 5];
578 i
+= config_descr_len
;
581 if (i
>= dev
->descr_len
) {
583 "husb: update iface failed. no matching configuration\n");
586 nb_interfaces
= dev
->descr
[i
+ 4];
588 if (usb_host_disconnect_ifaces(dev
, nb_interfaces
) < 0) {
592 /* XXX: only grab if all interfaces are free */
593 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
594 op
= "USBDEVFS_CLAIMINTERFACE";
595 ret
= ioctl(dev
->fd
, USBDEVFS_CLAIMINTERFACE
, &interface
);
601 trace_usb_host_claim_interfaces(dev
->bus_num
, dev
->addr
,
602 nb_interfaces
, configuration
);
604 dev
->dev
.ninterfaces
= nb_interfaces
;
605 dev
->dev
.configuration
= configuration
;
609 if (errno
== ENODEV
) {
616 static int usb_host_release_interfaces(USBHostDevice
*s
)
620 trace_usb_host_release_interfaces(s
->bus_num
, s
->addr
);
622 for (i
= 0; i
< s
->dev
.ninterfaces
; i
++) {
623 ret
= ioctl(s
->fd
, USBDEVFS_RELEASEINTERFACE
, &i
);
625 perror("USBDEVFS_RELEASEINTERFACE");
632 static void usb_host_handle_reset(USBDevice
*dev
)
634 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
636 trace_usb_host_reset(s
->bus_num
, s
->addr
);
638 usb_host_do_reset(s
);;
640 usb_host_claim_interfaces(s
, 0);
641 usb_linux_update_endp_table(s
);
644 static void usb_host_handle_destroy(USBDevice
*dev
)
646 USBHostDevice
*s
= (USBHostDevice
*)dev
;
648 usb_host_release_port(s
);
650 QTAILQ_REMOVE(&hostdevs
, s
, next
);
651 qemu_remove_exit_notifier(&s
->exit
);
654 /* iso data is special, we need to keep enough urbs in flight to make sure
655 that the controller never runs out of them, otherwise the device will
656 likely suffer a buffer underrun / overrun. */
657 static AsyncURB
*usb_host_alloc_iso(USBHostDevice
*s
, int pid
, uint8_t ep
)
660 int i
, j
, len
= usb_ep_get_max_packet_size(&s
->dev
, pid
, ep
);
662 aurb
= g_malloc0(s
->iso_urb_count
* sizeof(*aurb
));
663 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
664 aurb
[i
].urb
.endpoint
= ep
;
665 aurb
[i
].urb
.buffer_length
= ISO_FRAME_DESC_PER_URB
* len
;
666 aurb
[i
].urb
.buffer
= g_malloc(aurb
[i
].urb
.buffer_length
);
667 aurb
[i
].urb
.type
= USBDEVFS_URB_TYPE_ISO
;
668 aurb
[i
].urb
.flags
= USBDEVFS_URB_ISO_ASAP
;
669 aurb
[i
].urb
.number_of_packets
= ISO_FRAME_DESC_PER_URB
;
670 for (j
= 0 ; j
< ISO_FRAME_DESC_PER_URB
; j
++)
671 aurb
[i
].urb
.iso_frame_desc
[j
].length
= len
;
672 if (pid
== USB_TOKEN_IN
) {
673 aurb
[i
].urb
.endpoint
|= 0x80;
674 /* Mark as fully consumed (idle) */
675 aurb
[i
].iso_frame_idx
= ISO_FRAME_DESC_PER_URB
;
678 set_iso_urb(s
, pid
, ep
, aurb
);
683 static void usb_host_stop_n_free_iso(USBHostDevice
*s
, int pid
, uint8_t ep
)
686 int i
, ret
, killed
= 0, free
= 1;
688 aurb
= get_iso_urb(s
, pid
, ep
);
693 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
695 if (aurb
[i
].iso_frame_idx
== -1) {
696 ret
= ioctl(s
->fd
, USBDEVFS_DISCARDURB
, &aurb
[i
]);
698 perror("USBDEVFS_DISCARDURB");
706 /* Make sure any urbs we've killed are reaped before we free them */
711 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
712 g_free(aurb
[i
].urb
.buffer
);
718 printf("husb: leaking iso urbs because of discard failure\n");
719 set_iso_urb(s
, pid
, ep
, NULL
);
720 set_iso_urb_idx(s
, pid
, ep
, 0);
721 clear_iso_started(s
, pid
, ep
);
724 static int urb_status_to_usb_ret(int status
)
728 return USB_RET_STALL
;
730 return USB_RET_BABBLE
;
732 return USB_RET_IOERROR
;
736 static int usb_host_handle_iso_data(USBHostDevice
*s
, USBPacket
*p
, int in
)
739 int i
, j
, ret
, max_packet_size
, offset
, len
= 0;
742 max_packet_size
= p
->ep
->max_packet_size
;
743 if (max_packet_size
== 0)
746 aurb
= get_iso_urb(s
, p
->pid
, p
->ep
->nr
);
748 aurb
= usb_host_alloc_iso(s
, p
->pid
, p
->ep
->nr
);
751 i
= get_iso_urb_idx(s
, p
->pid
, p
->ep
->nr
);
752 j
= aurb
[i
].iso_frame_idx
;
753 if (j
>= 0 && j
< ISO_FRAME_DESC_PER_URB
) {
755 /* Check urb status */
756 if (aurb
[i
].urb
.status
) {
757 len
= urb_status_to_usb_ret(aurb
[i
].urb
.status
);
758 /* Move to the next urb */
759 aurb
[i
].iso_frame_idx
= ISO_FRAME_DESC_PER_URB
- 1;
760 /* Check frame status */
761 } else if (aurb
[i
].urb
.iso_frame_desc
[j
].status
) {
762 len
= urb_status_to_usb_ret(
763 aurb
[i
].urb
.iso_frame_desc
[j
].status
);
764 /* Check the frame fits */
765 } else if (aurb
[i
].urb
.iso_frame_desc
[j
].actual_length
767 printf("husb: received iso data is larger then packet\n");
768 len
= USB_RET_BABBLE
;
769 /* All good copy data over */
771 len
= aurb
[i
].urb
.iso_frame_desc
[j
].actual_length
;
772 buf
= aurb
[i
].urb
.buffer
+
773 j
* aurb
[i
].urb
.iso_frame_desc
[0].length
;
774 usb_packet_copy(p
, buf
, len
);
778 offset
= (j
== 0) ? 0 : get_iso_buffer_used(s
, p
->pid
, p
->ep
->nr
);
780 /* Check the frame fits */
781 if (len
> max_packet_size
) {
782 printf("husb: send iso data is larger then max packet size\n");
786 /* All good copy data over */
787 usb_packet_copy(p
, aurb
[i
].urb
.buffer
+ offset
, len
);
788 aurb
[i
].urb
.iso_frame_desc
[j
].length
= len
;
790 set_iso_buffer_used(s
, p
->pid
, p
->ep
->nr
, offset
);
792 /* Start the stream once we have buffered enough data */
793 if (!is_iso_started(s
, p
->pid
, p
->ep
->nr
) && i
== 1 && j
== 8) {
794 set_iso_started(s
, p
->pid
, p
->ep
->nr
);
797 aurb
[i
].iso_frame_idx
++;
798 if (aurb
[i
].iso_frame_idx
== ISO_FRAME_DESC_PER_URB
) {
799 i
= (i
+ 1) % s
->iso_urb_count
;
800 set_iso_urb_idx(s
, p
->pid
, p
->ep
->nr
, i
);
804 set_iso_started(s
, p
->pid
, p
->ep
->nr
);
806 DPRINTF("hubs: iso out error no free buffer, dropping packet\n");
810 if (is_iso_started(s
, p
->pid
, p
->ep
->nr
)) {
811 /* (Re)-submit all fully consumed / filled urbs */
812 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
813 if (aurb
[i
].iso_frame_idx
== ISO_FRAME_DESC_PER_URB
) {
814 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, &aurb
[i
]);
816 perror("USBDEVFS_SUBMITURB");
817 if (!in
|| len
== 0) {
829 aurb
[i
].iso_frame_idx
= -1;
830 change_iso_inflight(s
, p
->pid
, p
->ep
->nr
, 1);
838 static int usb_host_handle_data(USBDevice
*dev
, USBPacket
*p
)
840 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
841 struct usbdevfs_urb
*urb
;
843 int ret
, rem
, prem
, v
;
847 trace_usb_host_req_data(s
->bus_num
, s
->addr
,
848 p
->pid
== USB_TOKEN_IN
,
849 p
->ep
->nr
, p
->iov
.size
);
851 if (!is_valid(s
, p
->pid
, p
->ep
->nr
)) {
852 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, USB_RET_NAK
);
856 if (p
->pid
== USB_TOKEN_IN
) {
857 ep
= p
->ep
->nr
| 0x80;
862 if (is_halted(s
, p
->pid
, p
->ep
->nr
)) {
863 unsigned int arg
= ep
;
864 ret
= ioctl(s
->fd
, USBDEVFS_CLEAR_HALT
, &arg
);
866 perror("USBDEVFS_CLEAR_HALT");
867 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, USB_RET_NAK
);
870 clear_halt(s
, p
->pid
, p
->ep
->nr
);
873 if (is_isoc(s
, p
->pid
, p
->ep
->nr
)) {
874 return usb_host_handle_iso_data(s
, p
, p
->pid
== USB_TOKEN_IN
);
878 prem
= p
->iov
.iov
[v
].iov_len
;
879 pbuf
= p
->iov
.iov
[v
].iov_base
;
884 assert(v
< p
->iov
.niov
);
885 prem
= p
->iov
.iov
[v
].iov_len
;
886 pbuf
= p
->iov
.iov
[v
].iov_base
;
889 aurb
= async_alloc(s
);
894 urb
->type
= usb_host_usbfs_type(s
, p
);
895 urb
->usercontext
= s
;
897 urb
->buffer_length
= prem
;
899 if (urb
->buffer_length
> MAX_USBFS_BUFFER_SIZE
) {
900 urb
->buffer_length
= MAX_USBFS_BUFFER_SIZE
;
902 pbuf
+= urb
->buffer_length
;
903 prem
-= urb
->buffer_length
;
904 rem
-= urb
->buffer_length
;
909 trace_usb_host_urb_submit(s
->bus_num
, s
->addr
, aurb
,
910 urb
->buffer_length
, aurb
->more
);
911 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
913 DPRINTF("husb: data submit: ep 0x%x, len %u, more %d, packet %p, aurb %p\n",
914 urb
->endpoint
, urb
->buffer_length
, aurb
->more
, p
, aurb
);
917 perror("USBDEVFS_SUBMITURB");
922 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, USB_RET_NAK
);
926 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, USB_RET_STALL
);
927 return USB_RET_STALL
;
932 return USB_RET_ASYNC
;
935 static int ctrl_error(void)
937 if (errno
== ETIMEDOUT
) {
940 return USB_RET_STALL
;
944 static int usb_host_set_address(USBHostDevice
*s
, int addr
)
946 trace_usb_host_set_address(s
->bus_num
, s
->addr
, addr
);
951 static int usb_host_set_config(USBHostDevice
*s
, int config
)
955 trace_usb_host_set_config(s
->bus_num
, s
->addr
, config
);
957 usb_host_release_interfaces(s
);
960 ret
= ioctl(s
->fd
, USBDEVFS_SETCONFIGURATION
, &config
);
962 DPRINTF("husb: ctrl set config %d ret %d errno %d\n", config
, ret
, errno
);
964 if (ret
< 0 && errno
== EBUSY
&& first
) {
965 /* happens if usb device is in use by host drivers */
966 int count
= usb_linux_get_num_interfaces(s
);
968 DPRINTF("husb: busy -> disconnecting %d interfaces\n", count
);
969 usb_host_disconnect_ifaces(s
, count
);
978 usb_host_claim_interfaces(s
, config
);
979 usb_linux_update_endp_table(s
);
983 static int usb_host_set_interface(USBHostDevice
*s
, int iface
, int alt
)
985 struct usbdevfs_setinterface si
;
988 trace_usb_host_set_interface(s
->bus_num
, s
->addr
, iface
, alt
);
990 for (i
= 1; i
<= USB_MAX_ENDPOINTS
; i
++) {
991 if (is_isoc(s
, USB_TOKEN_IN
, i
)) {
992 usb_host_stop_n_free_iso(s
, USB_TOKEN_IN
, i
);
994 if (is_isoc(s
, USB_TOKEN_OUT
, i
)) {
995 usb_host_stop_n_free_iso(s
, USB_TOKEN_OUT
, i
);
999 if (iface
>= USB_MAX_INTERFACES
) {
1000 return USB_RET_STALL
;
1003 si
.interface
= iface
;
1004 si
.altsetting
= alt
;
1005 ret
= ioctl(s
->fd
, USBDEVFS_SETINTERFACE
, &si
);
1007 DPRINTF("husb: ctrl set iface %d altset %d ret %d errno %d\n",
1008 iface
, alt
, ret
, errno
);
1011 return ctrl_error();
1014 s
->dev
.altsetting
[iface
] = alt
;
1015 usb_linux_update_endp_table(s
);
1019 static int usb_host_handle_control(USBDevice
*dev
, USBPacket
*p
,
1020 int request
, int value
, int index
, int length
, uint8_t *data
)
1022 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
1023 struct usbdevfs_urb
*urb
;
1028 * Process certain standard device requests.
1029 * These are infrequent and are processed synchronously.
1032 /* Note request is (bRequestType << 8) | bRequest */
1033 trace_usb_host_req_control(s
->bus_num
, s
->addr
, request
, value
, index
);
1036 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
1037 return usb_host_set_address(s
, value
);
1039 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
1040 return usb_host_set_config(s
, value
& 0xff);
1042 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
1043 return usb_host_set_interface(s
, index
, value
);
1046 /* The rest are asynchronous */
1048 if (length
> sizeof(dev
->data_buf
)) {
1049 fprintf(stderr
, "husb: ctrl buffer too small (%d > %zu)\n",
1050 length
, sizeof(dev
->data_buf
));
1051 return USB_RET_STALL
;
1054 aurb
= async_alloc(s
);
1058 * Setup ctrl transfer.
1060 * s->ctrl is laid out such that data buffer immediately follows
1061 * 'req' struct which is exactly what usbdevfs expects.
1065 urb
->type
= USBDEVFS_URB_TYPE_CONTROL
;
1066 urb
->endpoint
= p
->ep
->nr
;
1068 urb
->buffer
= &dev
->setup_buf
;
1069 urb
->buffer_length
= length
+ 8;
1071 urb
->usercontext
= s
;
1073 trace_usb_host_urb_submit(s
->bus_num
, s
->addr
, aurb
,
1074 urb
->buffer_length
, aurb
->more
);
1075 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
1077 DPRINTF("husb: submit ctrl. len %u aurb %p\n", urb
->buffer_length
, aurb
);
1080 DPRINTF("husb: submit failed. errno %d\n", errno
);
1088 return USB_RET_STALL
;
1092 return USB_RET_ASYNC
;
1095 static uint8_t usb_linux_get_alt_setting(USBHostDevice
*s
,
1096 uint8_t configuration
, uint8_t interface
)
1098 char device_name
[64], line
[1024];
1101 sprintf(device_name
, "%d-%s:%d.%d", s
->bus_num
, s
->port
,
1102 (int)configuration
, (int)interface
);
1104 if (!usb_host_read_file(line
, sizeof(line
), "bAlternateSetting",
1106 /* Assume alt 0 on error */
1109 if (sscanf(line
, "%d", &alt_setting
) != 1) {
1110 /* Assume alt 0 on error */
1116 /* returns 1 on problem encountered or 0 for success */
1117 static int usb_linux_update_endp_table(USBHostDevice
*s
)
1119 uint8_t *descriptors
;
1120 uint8_t devep
, type
, alt_interface
;
1122 int interface
, length
, i
, ep
, pid
;
1123 struct endp_data
*epd
;
1125 usb_ep_init(&s
->dev
);
1127 if (s
->dev
.configuration
== 0) {
1128 /* not configured yet -- leave all endpoints disabled */
1132 /* get the desired configuration, interface, and endpoint descriptors
1133 * from device description */
1134 descriptors
= &s
->descr
[18];
1135 length
= s
->descr_len
- 18;
1138 while (i
< length
) {
1139 if (descriptors
[i
+ 1] != USB_DT_CONFIG
) {
1140 fprintf(stderr
, "invalid descriptor data\n");
1142 } else if (descriptors
[i
+ 5] != s
->dev
.configuration
) {
1143 DPRINTF("not requested configuration %d\n", s
->dev
.configuration
);
1144 i
+= (descriptors
[i
+ 3] << 8) + descriptors
[i
+ 2];
1147 i
+= descriptors
[i
];
1149 if (descriptors
[i
+ 1] != USB_DT_INTERFACE
||
1150 (descriptors
[i
+ 1] == USB_DT_INTERFACE
&&
1151 descriptors
[i
+ 4] == 0)) {
1152 i
+= descriptors
[i
];
1156 interface
= descriptors
[i
+ 2];
1157 alt_interface
= usb_linux_get_alt_setting(s
, s
->dev
.configuration
,
1160 /* the current interface descriptor is the active interface
1161 * and has endpoints */
1162 if (descriptors
[i
+ 3] != alt_interface
) {
1163 i
+= descriptors
[i
];
1167 /* advance to the endpoints */
1168 while (i
< length
&& descriptors
[i
+1] != USB_DT_ENDPOINT
) {
1169 i
+= descriptors
[i
];
1175 while (i
< length
) {
1176 if (descriptors
[i
+ 1] != USB_DT_ENDPOINT
) {
1180 devep
= descriptors
[i
+ 2];
1181 pid
= (devep
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
1184 fprintf(stderr
, "usb-linux: invalid ep descriptor, ep == 0\n");
1188 type
= descriptors
[i
+ 3] & 0x3;
1189 raw
= descriptors
[i
+ 4] + (descriptors
[i
+ 5] << 8);
1190 usb_ep_set_max_packet_size(&s
->dev
, pid
, ep
, raw
);
1191 assert(usb_ep_get_type(&s
->dev
, pid
, ep
) ==
1192 USB_ENDPOINT_XFER_INVALID
);
1193 usb_ep_set_type(&s
->dev
, pid
, ep
, type
);
1194 usb_ep_set_ifnum(&s
->dev
, pid
, ep
, interface
);
1195 if (type
== USB_ENDPOINT_XFER_BULK
) {
1196 usb_ep_set_pipeline(&s
->dev
, pid
, ep
, true);
1199 epd
= get_endp(s
, pid
, ep
);
1202 i
+= descriptors
[i
];
1206 usb_ep_dump(&s
->dev
);
1212 * Check if we can safely redirect a usb2 device to a usb1 virtual controller,
1213 * this function assumes this is safe, if:
1214 * 1) There are no isoc endpoints
1215 * 2) There are no interrupt endpoints with a max_packet_size > 64
1216 * Note bulk endpoints with a max_packet_size > 64 in theory also are not
1217 * usb1 compatible, but in practice this seems to work fine.
1219 static int usb_linux_full_speed_compat(USBHostDevice
*dev
)
1224 * usb_linux_update_endp_table only registers info about ep in the current
1225 * interface altsettings, so we need to parse the descriptors again.
1227 for (i
= 0; (i
+ 5) < dev
->descr_len
; i
+= dev
->descr
[i
]) {
1228 if (dev
->descr
[i
+ 1] == USB_DT_ENDPOINT
) {
1229 switch (dev
->descr
[i
+ 3] & 0x3) {
1230 case 0x00: /* CONTROL */
1232 case 0x01: /* ISO */
1234 case 0x02: /* BULK */
1236 case 0x03: /* INTERRUPT */
1237 packet_size
= dev
->descr
[i
+ 4] + (dev
->descr
[i
+ 5] << 8);
1238 if (packet_size
> 64)
1247 static int usb_host_open(USBHostDevice
*dev
, int bus_num
,
1248 int addr
, const char *port
,
1249 const char *prod_name
, int speed
)
1253 trace_usb_host_open_started(bus_num
, addr
);
1255 if (dev
->fd
!= -1) {
1259 fd
= usb_host_open_device(bus_num
, addr
);
1263 DPRINTF("husb: opened %s\n", buf
);
1265 dev
->bus_num
= bus_num
;
1267 strcpy(dev
->port
, port
);
1270 /* read the device description */
1271 dev
->descr_len
= read(fd
, dev
->descr
, sizeof(dev
->descr
));
1272 if (dev
->descr_len
<= 0) {
1273 perror("husb: reading device data failed");
1280 printf("=== begin dumping device descriptor data ===\n");
1281 for (x
= 0; x
< dev
->descr_len
; x
++) {
1282 printf("%02x ", dev
->descr
[x
]);
1284 printf("\n=== end dumping device descriptor data ===\n");
1289 /* start unconfigured -- we'll wait for the guest to set a configuration */
1290 if (!usb_host_claim_interfaces(dev
, 0)) {
1294 ret
= usb_linux_update_endp_table(dev
);
1300 struct usbdevfs_connectinfo ci
;
1302 ret
= ioctl(fd
, USBDEVFS_CONNECTINFO
, &ci
);
1304 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
1309 speed
= USB_SPEED_LOW
;
1311 speed
= USB_SPEED_HIGH
;
1314 dev
->dev
.speed
= speed
;
1315 dev
->dev
.speedmask
= (1 << speed
);
1316 if (dev
->dev
.speed
== USB_SPEED_HIGH
&& usb_linux_full_speed_compat(dev
)) {
1317 dev
->dev
.speedmask
|= USB_SPEED_MASK_FULL
;
1320 trace_usb_host_open_success(bus_num
, addr
);
1322 if (!prod_name
|| prod_name
[0] == '\0') {
1323 snprintf(dev
->dev
.product_desc
, sizeof(dev
->dev
.product_desc
),
1324 "host:%d.%d", bus_num
, addr
);
1326 pstrcpy(dev
->dev
.product_desc
, sizeof(dev
->dev
.product_desc
),
1330 ret
= usb_device_attach(&dev
->dev
);
1335 /* USB devio uses 'write' flag to check for async completions */
1336 qemu_set_fd_handler(dev
->fd
, NULL
, async_complete
, dev
);
1341 trace_usb_host_open_failure(bus_num
, addr
);
1342 if (dev
->fd
!= -1) {
1349 static int usb_host_close(USBHostDevice
*dev
)
1353 if (dev
->fd
== -1) {
1357 trace_usb_host_close(dev
->bus_num
, dev
->addr
);
1359 qemu_set_fd_handler(dev
->fd
, NULL
, NULL
, NULL
);
1361 for (i
= 1; i
<= USB_MAX_ENDPOINTS
; i
++) {
1362 if (is_isoc(dev
, USB_TOKEN_IN
, i
)) {
1363 usb_host_stop_n_free_iso(dev
, USB_TOKEN_IN
, i
);
1365 if (is_isoc(dev
, USB_TOKEN_OUT
, i
)) {
1366 usb_host_stop_n_free_iso(dev
, USB_TOKEN_OUT
, i
);
1369 async_complete(dev
);
1371 if (dev
->dev
.attached
) {
1372 usb_device_detach(&dev
->dev
);
1374 usb_host_do_reset(dev
);
1380 static void usb_host_exit_notifier(struct Notifier
*n
, void *data
)
1382 USBHostDevice
*s
= container_of(n
, USBHostDevice
, exit
);
1384 usb_host_release_port(s
);
1386 usb_host_do_reset(s
);;
1390 static int usb_host_initfn(USBDevice
*dev
)
1392 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
1394 dev
->auto_attach
= 0;
1398 QTAILQ_INSERT_TAIL(&hostdevs
, s
, next
);
1399 s
->exit
.notify
= usb_host_exit_notifier
;
1400 qemu_add_exit_notifier(&s
->exit
);
1401 usb_host_auto_check(NULL
);
1403 if (s
->match
.bus_num
!= 0 && s
->match
.port
!= NULL
) {
1404 usb_host_claim_port(s
);
1409 static const VMStateDescription vmstate_usb_host
= {
1414 static Property usb_host_dev_properties
[] = {
1415 DEFINE_PROP_UINT32("hostbus", USBHostDevice
, match
.bus_num
, 0),
1416 DEFINE_PROP_UINT32("hostaddr", USBHostDevice
, match
.addr
, 0),
1417 DEFINE_PROP_STRING("hostport", USBHostDevice
, match
.port
),
1418 DEFINE_PROP_HEX32("vendorid", USBHostDevice
, match
.vendor_id
, 0),
1419 DEFINE_PROP_HEX32("productid", USBHostDevice
, match
.product_id
, 0),
1420 DEFINE_PROP_UINT32("isobufs", USBHostDevice
, iso_urb_count
, 4),
1421 DEFINE_PROP_END_OF_LIST(),
1424 static void usb_host_class_initfn(ObjectClass
*klass
, void *data
)
1426 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1427 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
1429 uc
->init
= usb_host_initfn
;
1430 uc
->product_desc
= "USB Host Device";
1431 uc
->cancel_packet
= usb_host_async_cancel
;
1432 uc
->handle_data
= usb_host_handle_data
;
1433 uc
->handle_control
= usb_host_handle_control
;
1434 uc
->handle_reset
= usb_host_handle_reset
;
1435 uc
->handle_destroy
= usb_host_handle_destroy
;
1436 dc
->vmsd
= &vmstate_usb_host
;
1437 dc
->props
= usb_host_dev_properties
;
1440 static TypeInfo usb_host_dev_info
= {
1442 .parent
= TYPE_USB_DEVICE
,
1443 .instance_size
= sizeof(USBHostDevice
),
1444 .class_init
= usb_host_class_initfn
,
1447 static void usb_host_register_types(void)
1449 type_register_static(&usb_host_dev_info
);
1450 usb_legacy_register("usb-host", "host", usb_host_device_open
);
1453 type_init(usb_host_register_types
)
1455 USBDevice
*usb_host_device_open(USBBus
*bus
, const char *devname
)
1457 struct USBAutoFilter filter
;
1461 dev
= usb_create(bus
, "usb-host");
1463 if (strstr(devname
, "auto:")) {
1464 if (parse_filter(devname
, &filter
) < 0) {
1468 if ((p
= strchr(devname
, '.'))) {
1469 filter
.bus_num
= strtoul(devname
, NULL
, 0);
1470 filter
.addr
= strtoul(p
+ 1, NULL
, 0);
1471 filter
.vendor_id
= 0;
1472 filter
.product_id
= 0;
1473 } else if ((p
= strchr(devname
, ':'))) {
1476 filter
.vendor_id
= strtoul(devname
, NULL
, 16);
1477 filter
.product_id
= strtoul(p
+ 1, NULL
, 16);
1483 qdev_prop_set_uint32(&dev
->qdev
, "hostbus", filter
.bus_num
);
1484 qdev_prop_set_uint32(&dev
->qdev
, "hostaddr", filter
.addr
);
1485 qdev_prop_set_uint32(&dev
->qdev
, "vendorid", filter
.vendor_id
);
1486 qdev_prop_set_uint32(&dev
->qdev
, "productid", filter
.product_id
);
1487 qdev_init_nofail(&dev
->qdev
);
1491 qdev_free(&dev
->qdev
);
1495 int usb_host_device_close(const char *devname
)
1498 char product_name
[PRODUCT_NAME_SZ
];
1502 if (strstr(devname
, "auto:")) {
1503 return usb_host_auto_del(devname
);
1505 if (usb_host_find_device(&bus_num
, &addr
, product_name
,
1506 sizeof(product_name
), devname
) < 0) {
1509 s
= hostdev_find(bus_num
, addr
);
1511 usb_device_delete_addr(s
->bus_num
, s
->dev
.addr
);
1520 * Read sys file-system device file
1522 * @line address of buffer to put file contents in
1523 * @line_size size of line
1524 * @device_file path to device file (printf format string)
1525 * @device_name device being opened (inserted into device_file)
1527 * @return 0 failed, 1 succeeded ('line' contains data)
1529 static int usb_host_read_file(char *line
, size_t line_size
,
1530 const char *device_file
, const char *device_name
)
1534 char filename
[PATH_MAX
];
1536 snprintf(filename
, PATH_MAX
, "/sys/bus/usb/devices/%s/%s", device_name
,
1538 f
= fopen(filename
, "r");
1540 ret
= fgets(line
, line_size
, f
) != NULL
;
1548 * Use /sys/bus/usb/devices/ directory to determine host's USB
1551 * This code is based on Robert Schiele's original patches posted to
1552 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1554 static int usb_host_scan(void *opaque
, USBScanFunc
*func
)
1558 int bus_num
, addr
, speed
, class_id
, product_id
, vendor_id
;
1560 char port
[MAX_PORTLEN
];
1561 char product_name
[512];
1564 dir
= opendir("/sys/bus/usb/devices");
1566 perror("husb: opendir /sys/bus/usb/devices");
1567 fprintf(stderr
, "husb: please make sure sysfs is mounted at /sys\n");
1571 while ((de
= readdir(dir
))) {
1572 if (de
->d_name
[0] != '.' && !strchr(de
->d_name
, ':')) {
1573 if (sscanf(de
->d_name
, "%d-%7[0-9.]", &bus_num
, port
) < 2) {
1577 if (!usb_host_read_file(line
, sizeof(line
), "devnum", de
->d_name
)) {
1580 if (sscanf(line
, "%d", &addr
) != 1) {
1583 if (!usb_host_read_file(line
, sizeof(line
), "bDeviceClass",
1587 if (sscanf(line
, "%x", &class_id
) != 1) {
1591 if (!usb_host_read_file(line
, sizeof(line
), "idVendor",
1595 if (sscanf(line
, "%x", &vendor_id
) != 1) {
1598 if (!usb_host_read_file(line
, sizeof(line
), "idProduct",
1602 if (sscanf(line
, "%x", &product_id
) != 1) {
1605 if (!usb_host_read_file(line
, sizeof(line
), "product",
1609 if (strlen(line
) > 0) {
1610 line
[strlen(line
) - 1] = '\0';
1612 pstrcpy(product_name
, sizeof(product_name
), line
);
1615 if (!usb_host_read_file(line
, sizeof(line
), "speed", de
->d_name
)) {
1618 if (!strcmp(line
, "5000\n")) {
1619 speed
= USB_SPEED_SUPER
;
1620 } else if (!strcmp(line
, "480\n")) {
1621 speed
= USB_SPEED_HIGH
;
1622 } else if (!strcmp(line
, "1.5\n")) {
1623 speed
= USB_SPEED_LOW
;
1625 speed
= USB_SPEED_FULL
;
1628 ret
= func(opaque
, bus_num
, addr
, port
, class_id
, vendor_id
,
1629 product_id
, product_name
, speed
);
1642 static QEMUTimer
*usb_auto_timer
;
1644 static int usb_host_auto_scan(void *opaque
, int bus_num
,
1645 int addr
, const char *port
,
1646 int class_id
, int vendor_id
, int product_id
,
1647 const char *product_name
, int speed
)
1649 struct USBAutoFilter
*f
;
1650 struct USBHostDevice
*s
;
1656 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1659 if (f
->bus_num
> 0 && f
->bus_num
!= bus_num
) {
1662 if (f
->addr
> 0 && f
->addr
!= addr
) {
1665 if (f
->port
!= NULL
&& (port
== NULL
|| strcmp(f
->port
, port
) != 0)) {
1669 if (f
->vendor_id
> 0 && f
->vendor_id
!= vendor_id
) {
1673 if (f
->product_id
> 0 && f
->product_id
!= product_id
) {
1676 /* We got a match */
1678 if (s
->errcount
>= 3) {
1682 /* Already attached ? */
1686 DPRINTF("husb: auto open: bus_num %d addr %d\n", bus_num
, addr
);
1688 if (usb_host_open(s
, bus_num
, addr
, port
, product_name
, speed
) < 0) {
1697 static void usb_host_auto_check(void *unused
)
1699 struct USBHostDevice
*s
;
1700 int unconnected
= 0;
1702 usb_host_scan(NULL
, usb_host_auto_scan
);
1704 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1714 if (unconnected
== 0) {
1715 /* nothing to watch */
1716 if (usb_auto_timer
) {
1717 qemu_del_timer(usb_auto_timer
);
1718 trace_usb_host_auto_scan_disabled();
1723 if (!usb_auto_timer
) {
1724 usb_auto_timer
= qemu_new_timer_ms(rt_clock
, usb_host_auto_check
, NULL
);
1725 if (!usb_auto_timer
) {
1728 trace_usb_host_auto_scan_enabled();
1730 qemu_mod_timer(usb_auto_timer
, qemu_get_clock_ms(rt_clock
) + 2000);
1734 * Autoconnect filter
1736 * auto:bus:dev[:vid:pid]
1737 * auto:bus.dev[:vid:pid]
1739 * bus - bus number (dec, * means any)
1740 * dev - device number (dec, * means any)
1741 * vid - vendor id (hex, * means any)
1742 * pid - product id (hex, * means any)
1744 * See 'lsusb' output.
1746 static int parse_filter(const char *spec
, struct USBAutoFilter
*f
)
1748 enum { BUS
, DEV
, VID
, PID
, DONE
};
1749 const char *p
= spec
;
1757 for (i
= BUS
; i
< DONE
; i
++) {
1758 p
= strpbrk(p
, ":.");
1768 case BUS
: f
->bus_num
= strtol(p
, NULL
, 10); break;
1769 case DEV
: f
->addr
= strtol(p
, NULL
, 10); break;
1770 case VID
: f
->vendor_id
= strtol(p
, NULL
, 16); break;
1771 case PID
: f
->product_id
= strtol(p
, NULL
, 16); break;
1776 fprintf(stderr
, "husb: invalid auto filter spec %s\n", spec
);
1783 /**********************/
1784 /* USB host device info */
1786 struct usb_class_info
{
1788 const char *class_name
;
1791 static const struct usb_class_info usb_class_info
[] = {
1792 { USB_CLASS_AUDIO
, "Audio"},
1793 { USB_CLASS_COMM
, "Communication"},
1794 { USB_CLASS_HID
, "HID"},
1795 { USB_CLASS_HUB
, "Hub" },
1796 { USB_CLASS_PHYSICAL
, "Physical" },
1797 { USB_CLASS_PRINTER
, "Printer" },
1798 { USB_CLASS_MASS_STORAGE
, "Storage" },
1799 { USB_CLASS_CDC_DATA
, "Data" },
1800 { USB_CLASS_APP_SPEC
, "Application Specific" },
1801 { USB_CLASS_VENDOR_SPEC
, "Vendor Specific" },
1802 { USB_CLASS_STILL_IMAGE
, "Still Image" },
1803 { USB_CLASS_CSCID
, "Smart Card" },
1804 { USB_CLASS_CONTENT_SEC
, "Content Security" },
1808 static const char *usb_class_str(uint8_t class)
1810 const struct usb_class_info
*p
;
1811 for(p
= usb_class_info
; p
->class != -1; p
++) {
1812 if (p
->class == class) {
1816 return p
->class_name
;
1819 static void usb_info_device(Monitor
*mon
, int bus_num
,
1820 int addr
, const char *port
,
1821 int class_id
, int vendor_id
, int product_id
,
1822 const char *product_name
,
1825 const char *class_str
, *speed_str
;
1831 case USB_SPEED_FULL
:
1834 case USB_SPEED_HIGH
:
1837 case USB_SPEED_SUPER
:
1845 monitor_printf(mon
, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
1846 bus_num
, addr
, port
, speed_str
);
1847 class_str
= usb_class_str(class_id
);
1849 monitor_printf(mon
, " %s:", class_str
);
1851 monitor_printf(mon
, " Class %02x:", class_id
);
1853 monitor_printf(mon
, " USB device %04x:%04x", vendor_id
, product_id
);
1854 if (product_name
[0] != '\0') {
1855 monitor_printf(mon
, ", %s", product_name
);
1857 monitor_printf(mon
, "\n");
1860 static int usb_host_info_device(void *opaque
, int bus_num
, int addr
,
1861 const char *path
, int class_id
,
1862 int vendor_id
, int product_id
,
1863 const char *product_name
,
1866 Monitor
*mon
= opaque
;
1868 usb_info_device(mon
, bus_num
, addr
, path
, class_id
, vendor_id
, product_id
,
1869 product_name
, speed
);
1873 static void dec2str(int val
, char *str
, size_t size
)
1876 snprintf(str
, size
, "*");
1878 snprintf(str
, size
, "%d", val
);
1882 static void hex2str(int val
, char *str
, size_t size
)
1885 snprintf(str
, size
, "*");
1887 snprintf(str
, size
, "%04x", val
);
1891 void usb_host_info(Monitor
*mon
)
1893 struct USBAutoFilter
*f
;
1894 struct USBHostDevice
*s
;
1896 usb_host_scan(mon
, usb_host_info_device
);
1898 if (QTAILQ_EMPTY(&hostdevs
)) {
1902 monitor_printf(mon
, " Auto filters:\n");
1903 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1904 char bus
[10], addr
[10], vid
[10], pid
[10];
1906 dec2str(f
->bus_num
, bus
, sizeof(bus
));
1907 dec2str(f
->addr
, addr
, sizeof(addr
));
1908 hex2str(f
->vendor_id
, vid
, sizeof(vid
));
1909 hex2str(f
->product_id
, pid
, sizeof(pid
));
1910 monitor_printf(mon
, " Bus %s, Addr %s, Port %s, ID %s:%s\n",
1911 bus
, addr
, f
->port
? f
->port
: "*", vid
, pid
);