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
;
116 struct endp_data ep_in
[USB_MAX_ENDPOINTS
];
117 struct endp_data ep_out
[USB_MAX_ENDPOINTS
];
118 QLIST_HEAD(, AsyncURB
) aurbs
;
120 /* Host side address */
123 char port
[MAX_PORTLEN
];
124 struct USBAutoFilter match
;
128 QTAILQ_ENTRY(USBHostDevice
) next
;
131 static QTAILQ_HEAD(, USBHostDevice
) hostdevs
= QTAILQ_HEAD_INITIALIZER(hostdevs
);
133 static int usb_host_close(USBHostDevice
*dev
);
134 static int parse_filter(const char *spec
, struct USBAutoFilter
*f
);
135 static void usb_host_auto_check(void *unused
);
136 static int usb_host_read_file(char *line
, size_t line_size
,
137 const char *device_file
, const char *device_name
);
138 static int usb_linux_update_endp_table(USBHostDevice
*s
);
140 static int usb_host_usbfs_type(USBHostDevice
*s
, USBPacket
*p
)
142 static const int usbfs
[] = {
143 [USB_ENDPOINT_XFER_CONTROL
] = USBDEVFS_URB_TYPE_CONTROL
,
144 [USB_ENDPOINT_XFER_ISOC
] = USBDEVFS_URB_TYPE_ISO
,
145 [USB_ENDPOINT_XFER_BULK
] = USBDEVFS_URB_TYPE_BULK
,
146 [USB_ENDPOINT_XFER_INT
] = USBDEVFS_URB_TYPE_INTERRUPT
,
148 uint8_t type
= p
->ep
->type
;
149 assert(type
< ARRAY_SIZE(usbfs
));
153 static int usb_host_do_reset(USBHostDevice
*dev
)
159 gettimeofday(&s
, NULL
);
160 ret
= ioctl(dev
->fd
, USBDEVFS_RESET
);
161 gettimeofday(&e
, NULL
);
162 usecs
= (e
.tv_sec
- s
.tv_sec
) * 1000000;
163 usecs
+= e
.tv_usec
- s
.tv_usec
;
164 if (usecs
> 1000000) {
165 /* more than a second, something is fishy, broken usb device? */
166 fprintf(stderr
, "husb: device %d:%d reset took %d.%06d seconds\n",
167 dev
->bus_num
, dev
->addr
, usecs
/ 1000000, usecs
% 1000000);
172 static struct endp_data
*get_endp(USBHostDevice
*s
, int pid
, int ep
)
174 struct endp_data
*eps
= pid
== USB_TOKEN_IN
? s
->ep_in
: s
->ep_out
;
175 assert(pid
== USB_TOKEN_IN
|| pid
== USB_TOKEN_OUT
);
176 assert(ep
> 0 && ep
<= USB_MAX_ENDPOINTS
);
180 static int is_isoc(USBHostDevice
*s
, int pid
, int ep
)
182 return usb_ep_get_type(&s
->dev
, pid
, ep
) == USB_ENDPOINT_XFER_ISOC
;
185 static int is_valid(USBHostDevice
*s
, int pid
, int ep
)
187 return usb_ep_get_type(&s
->dev
, pid
, ep
) != USB_ENDPOINT_XFER_INVALID
;
190 static int is_halted(USBHostDevice
*s
, int pid
, int ep
)
192 return get_endp(s
, pid
, ep
)->halted
;
195 static void clear_halt(USBHostDevice
*s
, int pid
, int ep
)
197 trace_usb_host_ep_clear_halt(s
->bus_num
, s
->addr
, ep
);
198 get_endp(s
, pid
, ep
)->halted
= 0;
201 static void set_halt(USBHostDevice
*s
, int pid
, int ep
)
204 trace_usb_host_ep_set_halt(s
->bus_num
, s
->addr
, ep
);
205 get_endp(s
, pid
, ep
)->halted
= 1;
209 static int is_iso_started(USBHostDevice
*s
, int pid
, int ep
)
211 return get_endp(s
, pid
, ep
)->iso_started
;
214 static void clear_iso_started(USBHostDevice
*s
, int pid
, int ep
)
216 trace_usb_host_ep_stop_iso(s
->bus_num
, s
->addr
, ep
);
217 get_endp(s
, pid
, ep
)->iso_started
= 0;
220 static void set_iso_started(USBHostDevice
*s
, int pid
, int ep
)
222 struct endp_data
*e
= get_endp(s
, pid
, ep
);
224 trace_usb_host_ep_start_iso(s
->bus_num
, s
->addr
, ep
);
225 if (!e
->iso_started
) {
231 static int change_iso_inflight(USBHostDevice
*s
, int pid
, int ep
, int value
)
233 struct endp_data
*e
= get_endp(s
, pid
, ep
);
235 e
->inflight
+= value
;
239 static void set_iso_urb(USBHostDevice
*s
, int pid
, int ep
, AsyncURB
*iso_urb
)
241 get_endp(s
, pid
, ep
)->iso_urb
= iso_urb
;
244 static AsyncURB
*get_iso_urb(USBHostDevice
*s
, int pid
, int ep
)
246 return get_endp(s
, pid
, ep
)->iso_urb
;
249 static void set_iso_urb_idx(USBHostDevice
*s
, int pid
, int ep
, int i
)
251 get_endp(s
, pid
, ep
)->iso_urb_idx
= i
;
254 static int get_iso_urb_idx(USBHostDevice
*s
, int pid
, int ep
)
256 return get_endp(s
, pid
, ep
)->iso_urb_idx
;
259 static void set_iso_buffer_used(USBHostDevice
*s
, int pid
, int ep
, int i
)
261 get_endp(s
, pid
, ep
)->iso_buffer_used
= i
;
264 static int get_iso_buffer_used(USBHostDevice
*s
, int pid
, int ep
)
266 return get_endp(s
, pid
, ep
)->iso_buffer_used
;
271 * We always allocate iso packet descriptors even for bulk transfers
272 * to simplify allocation and casts.
276 struct usbdevfs_urb urb
;
277 struct usbdevfs_iso_packet_desc isocpd
[ISO_FRAME_DESC_PER_URB
];
279 QLIST_ENTRY(AsyncURB
) next
;
281 /* For regular async urbs */
283 int more
; /* large transfer, more urbs follow */
285 /* For buffered iso handling */
286 int iso_frame_idx
; /* -1 means in flight */
289 static AsyncURB
*async_alloc(USBHostDevice
*s
)
291 AsyncURB
*aurb
= g_malloc0(sizeof(AsyncURB
));
293 QLIST_INSERT_HEAD(&s
->aurbs
, aurb
, next
);
297 static void async_free(AsyncURB
*aurb
)
299 QLIST_REMOVE(aurb
, next
);
303 static void do_disconnect(USBHostDevice
*s
)
306 usb_host_auto_check(NULL
);
309 static void async_complete(void *opaque
)
311 USBHostDevice
*s
= opaque
;
318 int r
= ioctl(s
->fd
, USBDEVFS_REAPURBNDELAY
, &aurb
);
320 if (errno
== EAGAIN
) {
322 fprintf(stderr
, "husb: %d iso urbs finished at once\n", urbs
);
326 if (errno
== ENODEV
) {
328 trace_usb_host_disconnect(s
->bus_num
, s
->addr
);
334 perror("USBDEVFS_REAPURBNDELAY");
338 DPRINTF("husb: async completed. aurb %p status %d alen %d\n",
339 aurb
, aurb
->urb
.status
, aurb
->urb
.actual_length
);
341 /* If this is a buffered iso urb mark it as complete and don't do
342 anything else (it is handled further in usb_host_handle_iso_data) */
343 if (aurb
->iso_frame_idx
== -1) {
345 int pid
= (aurb
->urb
.endpoint
& USB_DIR_IN
) ?
346 USB_TOKEN_IN
: USB_TOKEN_OUT
;
347 int ep
= aurb
->urb
.endpoint
& 0xf;
348 if (aurb
->urb
.status
== -EPIPE
) {
349 set_halt(s
, pid
, ep
);
351 aurb
->iso_frame_idx
= 0;
353 inflight
= change_iso_inflight(s
, pid
, ep
, -1);
354 if (inflight
== 0 && is_iso_started(s
, pid
, ep
)) {
355 fprintf(stderr
, "husb: out of buffers for iso stream\n");
361 trace_usb_host_urb_complete(s
->bus_num
, s
->addr
, aurb
, aurb
->urb
.status
,
362 aurb
->urb
.actual_length
, aurb
->more
);
365 switch (aurb
->urb
.status
) {
367 p
->result
+= aurb
->urb
.actual_length
;
371 set_halt(s
, p
->pid
, p
->ep
->nr
);
372 p
->result
= USB_RET_STALL
;
376 p
->result
= USB_RET_BABBLE
;
380 p
->result
= USB_RET_IOERROR
;
384 if (aurb
->urb
.type
== USBDEVFS_URB_TYPE_CONTROL
) {
385 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
, p
->result
);
386 usb_generic_async_ctrl_complete(&s
->dev
, p
);
387 } else if (!aurb
->more
) {
388 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
, p
->result
);
389 usb_packet_complete(&s
->dev
, p
);
397 static void usb_host_async_cancel(USBDevice
*dev
, USBPacket
*p
)
399 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
402 trace_usb_host_req_canceled(s
->bus_num
, s
->addr
, p
);
404 QLIST_FOREACH(aurb
, &s
->aurbs
, next
) {
405 if (p
!= aurb
->packet
) {
409 trace_usb_host_urb_canceled(s
->bus_num
, s
->addr
, aurb
);
411 /* Mark it as dead (see async_complete above) */
414 int r
= ioctl(s
->fd
, USBDEVFS_DISCARDURB
, aurb
);
416 DPRINTF("husb: async. discard urb failed errno %d\n", errno
);
421 static int usb_host_open_device(int bus
, int addr
)
423 const char *usbfs
= NULL
;
428 rc
= stat("/dev/bus/usb", &st
);
429 if (rc
== 0 && S_ISDIR(st
.st_mode
)) {
430 /* udev-created device nodes available */
431 usbfs
= "/dev/bus/usb";
433 /* fallback: usbfs mounted below /proc */
434 usbfs
= "/proc/bus/usb";
437 snprintf(filename
, sizeof(filename
), "%s/%03d/%03d",
439 fd
= open(filename
, O_RDWR
| O_NONBLOCK
);
441 fprintf(stderr
, "husb: open %s: %s\n", filename
, strerror(errno
));
446 static int usb_host_claim_port(USBHostDevice
*s
)
448 #ifdef USBDEVFS_CLAIM_PORT
449 char *h
, hub_name
[64], line
[1024];
452 snprintf(hub_name
, sizeof(hub_name
), "%d-%s",
453 s
->match
.bus_num
, s
->match
.port
);
455 /* try strip off last ".$portnr" to get hub */
456 h
= strrchr(hub_name
, '.');
458 s
->hub_port
= atoi(h
+1);
461 /* no dot in there -> it is the root hub */
462 snprintf(hub_name
, sizeof(hub_name
), "usb%d",
464 s
->hub_port
= atoi(s
->match
.port
);
467 if (!usb_host_read_file(line
, sizeof(line
), "devnum",
471 if (sscanf(line
, "%d", &hub_addr
) != 1) {
475 s
->hub_fd
= usb_host_open_device(s
->match
.bus_num
, hub_addr
);
480 ret
= ioctl(s
->hub_fd
, USBDEVFS_CLAIM_PORT
, &s
->hub_port
);
487 trace_usb_host_claim_port(s
->match
.bus_num
, hub_addr
, s
->hub_port
);
494 static void usb_host_release_port(USBHostDevice
*s
)
496 if (s
->hub_fd
== -1) {
499 #ifdef USBDEVFS_RELEASE_PORT
500 ioctl(s
->hub_fd
, USBDEVFS_RELEASE_PORT
, &s
->hub_port
);
506 static int usb_host_disconnect_ifaces(USBHostDevice
*dev
, int nb_interfaces
)
508 /* earlier Linux 2.4 do not support that */
509 #ifdef USBDEVFS_DISCONNECT
510 struct usbdevfs_ioctl ctrl
;
513 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
514 ctrl
.ioctl_code
= USBDEVFS_DISCONNECT
;
515 ctrl
.ifno
= interface
;
517 ret
= ioctl(dev
->fd
, USBDEVFS_IOCTL
, &ctrl
);
518 if (ret
< 0 && errno
!= ENODATA
) {
519 perror("USBDEVFS_DISCONNECT");
527 static int usb_linux_get_num_interfaces(USBHostDevice
*s
)
529 char device_name
[64], line
[1024];
530 int num_interfaces
= 0;
532 sprintf(device_name
, "%d-%s", s
->bus_num
, s
->port
);
533 if (!usb_host_read_file(line
, sizeof(line
), "bNumInterfaces",
537 if (sscanf(line
, "%d", &num_interfaces
) != 1) {
540 return num_interfaces
;
543 static int usb_host_claim_interfaces(USBHostDevice
*dev
, int configuration
)
545 const char *op
= NULL
;
546 int dev_descr_len
, config_descr_len
;
547 int interface
, nb_interfaces
;
550 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
551 dev
->dev
.altsetting
[i
] = 0;
554 if (configuration
== 0) { /* address state - ignore */
555 dev
->dev
.ninterfaces
= 0;
556 dev
->dev
.configuration
= 0;
560 DPRINTF("husb: claiming interfaces. config %d\n", configuration
);
563 dev_descr_len
= dev
->descr
[0];
564 if (dev_descr_len
> dev
->descr_len
) {
565 fprintf(stderr
, "husb: update iface failed. descr too short\n");
570 while (i
< dev
->descr_len
) {
571 DPRINTF("husb: i is %d, descr_len is %d, dl %d, dt %d\n",
573 dev
->descr
[i
], dev
->descr
[i
+1]);
575 if (dev
->descr
[i
+1] != USB_DT_CONFIG
) {
579 config_descr_len
= dev
->descr
[i
];
581 DPRINTF("husb: config #%d need %d\n", dev
->descr
[i
+ 5], configuration
);
583 if (configuration
== dev
->descr
[i
+ 5]) {
584 configuration
= dev
->descr
[i
+ 5];
588 i
+= config_descr_len
;
591 if (i
>= dev
->descr_len
) {
593 "husb: update iface failed. no matching configuration\n");
596 nb_interfaces
= dev
->descr
[i
+ 4];
598 if (usb_host_disconnect_ifaces(dev
, nb_interfaces
) < 0) {
602 /* XXX: only grab if all interfaces are free */
603 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
604 op
= "USBDEVFS_CLAIMINTERFACE";
605 ret
= ioctl(dev
->fd
, USBDEVFS_CLAIMINTERFACE
, &interface
);
611 trace_usb_host_claim_interfaces(dev
->bus_num
, dev
->addr
,
612 nb_interfaces
, configuration
);
614 dev
->dev
.ninterfaces
= nb_interfaces
;
615 dev
->dev
.configuration
= configuration
;
619 if (errno
== ENODEV
) {
626 static int usb_host_release_interfaces(USBHostDevice
*s
)
630 trace_usb_host_release_interfaces(s
->bus_num
, s
->addr
);
632 for (i
= 0; i
< s
->dev
.ninterfaces
; i
++) {
633 ret
= ioctl(s
->fd
, USBDEVFS_RELEASEINTERFACE
, &i
);
635 perror("USBDEVFS_RELEASEINTERFACE");
642 static void usb_host_handle_reset(USBDevice
*dev
)
644 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
646 trace_usb_host_reset(s
->bus_num
, s
->addr
);
648 usb_host_do_reset(s
);;
650 usb_host_claim_interfaces(s
, 0);
651 usb_linux_update_endp_table(s
);
654 static void usb_host_handle_destroy(USBDevice
*dev
)
656 USBHostDevice
*s
= (USBHostDevice
*)dev
;
658 usb_host_release_port(s
);
660 QTAILQ_REMOVE(&hostdevs
, s
, next
);
661 qemu_remove_exit_notifier(&s
->exit
);
664 /* iso data is special, we need to keep enough urbs in flight to make sure
665 that the controller never runs out of them, otherwise the device will
666 likely suffer a buffer underrun / overrun. */
667 static AsyncURB
*usb_host_alloc_iso(USBHostDevice
*s
, int pid
, uint8_t ep
)
670 int i
, j
, len
= usb_ep_get_max_packet_size(&s
->dev
, pid
, ep
);
672 aurb
= g_malloc0(s
->iso_urb_count
* sizeof(*aurb
));
673 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
674 aurb
[i
].urb
.endpoint
= ep
;
675 aurb
[i
].urb
.buffer_length
= ISO_FRAME_DESC_PER_URB
* len
;
676 aurb
[i
].urb
.buffer
= g_malloc(aurb
[i
].urb
.buffer_length
);
677 aurb
[i
].urb
.type
= USBDEVFS_URB_TYPE_ISO
;
678 aurb
[i
].urb
.flags
= USBDEVFS_URB_ISO_ASAP
;
679 aurb
[i
].urb
.number_of_packets
= ISO_FRAME_DESC_PER_URB
;
680 for (j
= 0 ; j
< ISO_FRAME_DESC_PER_URB
; j
++)
681 aurb
[i
].urb
.iso_frame_desc
[j
].length
= len
;
682 if (pid
== USB_TOKEN_IN
) {
683 aurb
[i
].urb
.endpoint
|= 0x80;
684 /* Mark as fully consumed (idle) */
685 aurb
[i
].iso_frame_idx
= ISO_FRAME_DESC_PER_URB
;
688 set_iso_urb(s
, pid
, ep
, aurb
);
693 static void usb_host_stop_n_free_iso(USBHostDevice
*s
, int pid
, uint8_t ep
)
696 int i
, ret
, killed
= 0, free
= 1;
698 aurb
= get_iso_urb(s
, pid
, ep
);
703 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
705 if (aurb
[i
].iso_frame_idx
== -1) {
706 ret
= ioctl(s
->fd
, USBDEVFS_DISCARDURB
, &aurb
[i
]);
708 perror("USBDEVFS_DISCARDURB");
716 /* Make sure any urbs we've killed are reaped before we free them */
721 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
722 g_free(aurb
[i
].urb
.buffer
);
728 printf("husb: leaking iso urbs because of discard failure\n");
729 set_iso_urb(s
, pid
, ep
, NULL
);
730 set_iso_urb_idx(s
, pid
, ep
, 0);
731 clear_iso_started(s
, pid
, ep
);
734 static int urb_status_to_usb_ret(int status
)
738 return USB_RET_STALL
;
740 return USB_RET_BABBLE
;
742 return USB_RET_IOERROR
;
746 static int usb_host_handle_iso_data(USBHostDevice
*s
, USBPacket
*p
, int in
)
749 int i
, j
, ret
, max_packet_size
, offset
, len
= 0;
752 max_packet_size
= p
->ep
->max_packet_size
;
753 if (max_packet_size
== 0)
756 aurb
= get_iso_urb(s
, p
->pid
, p
->ep
->nr
);
758 aurb
= usb_host_alloc_iso(s
, p
->pid
, p
->ep
->nr
);
761 i
= get_iso_urb_idx(s
, p
->pid
, p
->ep
->nr
);
762 j
= aurb
[i
].iso_frame_idx
;
763 if (j
>= 0 && j
< ISO_FRAME_DESC_PER_URB
) {
765 /* Check urb status */
766 if (aurb
[i
].urb
.status
) {
767 len
= urb_status_to_usb_ret(aurb
[i
].urb
.status
);
768 /* Move to the next urb */
769 aurb
[i
].iso_frame_idx
= ISO_FRAME_DESC_PER_URB
- 1;
770 /* Check frame status */
771 } else if (aurb
[i
].urb
.iso_frame_desc
[j
].status
) {
772 len
= urb_status_to_usb_ret(
773 aurb
[i
].urb
.iso_frame_desc
[j
].status
);
774 /* Check the frame fits */
775 } else if (aurb
[i
].urb
.iso_frame_desc
[j
].actual_length
777 printf("husb: received iso data is larger then packet\n");
778 len
= USB_RET_BABBLE
;
779 /* All good copy data over */
781 len
= aurb
[i
].urb
.iso_frame_desc
[j
].actual_length
;
782 buf
= aurb
[i
].urb
.buffer
+
783 j
* aurb
[i
].urb
.iso_frame_desc
[0].length
;
784 usb_packet_copy(p
, buf
, len
);
788 offset
= (j
== 0) ? 0 : get_iso_buffer_used(s
, p
->pid
, p
->ep
->nr
);
790 /* Check the frame fits */
791 if (len
> max_packet_size
) {
792 printf("husb: send iso data is larger then max packet size\n");
796 /* All good copy data over */
797 usb_packet_copy(p
, aurb
[i
].urb
.buffer
+ offset
, len
);
798 aurb
[i
].urb
.iso_frame_desc
[j
].length
= len
;
800 set_iso_buffer_used(s
, p
->pid
, p
->ep
->nr
, offset
);
802 /* Start the stream once we have buffered enough data */
803 if (!is_iso_started(s
, p
->pid
, p
->ep
->nr
) && i
== 1 && j
== 8) {
804 set_iso_started(s
, p
->pid
, p
->ep
->nr
);
807 aurb
[i
].iso_frame_idx
++;
808 if (aurb
[i
].iso_frame_idx
== ISO_FRAME_DESC_PER_URB
) {
809 i
= (i
+ 1) % s
->iso_urb_count
;
810 set_iso_urb_idx(s
, p
->pid
, p
->ep
->nr
, i
);
814 set_iso_started(s
, p
->pid
, p
->ep
->nr
);
816 DPRINTF("hubs: iso out error no free buffer, dropping packet\n");
820 if (is_iso_started(s
, p
->pid
, p
->ep
->nr
)) {
821 /* (Re)-submit all fully consumed / filled urbs */
822 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
823 if (aurb
[i
].iso_frame_idx
== ISO_FRAME_DESC_PER_URB
) {
824 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, &aurb
[i
]);
826 perror("USBDEVFS_SUBMITURB");
827 if (!in
|| len
== 0) {
839 aurb
[i
].iso_frame_idx
= -1;
840 change_iso_inflight(s
, p
->pid
, p
->ep
->nr
, 1);
848 static int usb_host_handle_data(USBDevice
*dev
, USBPacket
*p
)
850 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
851 struct usbdevfs_urb
*urb
;
853 int ret
, rem
, prem
, v
;
857 trace_usb_host_req_data(s
->bus_num
, s
->addr
, p
,
858 p
->pid
== USB_TOKEN_IN
,
859 p
->ep
->nr
, p
->iov
.size
);
861 if (!is_valid(s
, p
->pid
, p
->ep
->nr
)) {
862 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
, USB_RET_NAK
);
866 if (p
->pid
== USB_TOKEN_IN
) {
867 ep
= p
->ep
->nr
| 0x80;
872 if (is_halted(s
, p
->pid
, p
->ep
->nr
)) {
873 unsigned int arg
= ep
;
874 ret
= ioctl(s
->fd
, USBDEVFS_CLEAR_HALT
, &arg
);
876 perror("USBDEVFS_CLEAR_HALT");
877 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
, USB_RET_NAK
);
880 clear_halt(s
, p
->pid
, p
->ep
->nr
);
883 if (is_isoc(s
, p
->pid
, p
->ep
->nr
)) {
884 return usb_host_handle_iso_data(s
, p
, p
->pid
== USB_TOKEN_IN
);
892 if (prem
== 0 && rem
> 0) {
893 assert(v
< p
->iov
.niov
);
894 prem
= p
->iov
.iov
[v
].iov_len
;
895 pbuf
= p
->iov
.iov
[v
].iov_base
;
899 aurb
= async_alloc(s
);
904 urb
->type
= usb_host_usbfs_type(s
, p
);
905 urb
->usercontext
= s
;
907 urb
->buffer_length
= prem
;
909 if (urb
->buffer_length
> MAX_USBFS_BUFFER_SIZE
) {
910 urb
->buffer_length
= MAX_USBFS_BUFFER_SIZE
;
912 pbuf
+= urb
->buffer_length
;
913 prem
-= urb
->buffer_length
;
914 rem
-= urb
->buffer_length
;
919 trace_usb_host_urb_submit(s
->bus_num
, s
->addr
, aurb
,
920 urb
->buffer_length
, aurb
->more
);
921 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
923 DPRINTF("husb: data submit: ep 0x%x, len %u, more %d, packet %p, aurb %p\n",
924 urb
->endpoint
, urb
->buffer_length
, aurb
->more
, p
, aurb
);
927 perror("USBDEVFS_SUBMITURB");
932 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
937 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
939 return USB_RET_STALL
;
944 return USB_RET_ASYNC
;
947 static int ctrl_error(void)
949 if (errno
== ETIMEDOUT
) {
952 return USB_RET_STALL
;
956 static int usb_host_set_address(USBHostDevice
*s
, int addr
)
958 trace_usb_host_set_address(s
->bus_num
, s
->addr
, addr
);
963 static int usb_host_set_config(USBHostDevice
*s
, int config
)
967 trace_usb_host_set_config(s
->bus_num
, s
->addr
, config
);
969 usb_host_release_interfaces(s
);
972 ret
= ioctl(s
->fd
, USBDEVFS_SETCONFIGURATION
, &config
);
974 DPRINTF("husb: ctrl set config %d ret %d errno %d\n", config
, ret
, errno
);
976 if (ret
< 0 && errno
== EBUSY
&& first
) {
977 /* happens if usb device is in use by host drivers */
978 int count
= usb_linux_get_num_interfaces(s
);
980 DPRINTF("husb: busy -> disconnecting %d interfaces\n", count
);
981 usb_host_disconnect_ifaces(s
, count
);
990 usb_host_claim_interfaces(s
, config
);
991 usb_linux_update_endp_table(s
);
995 static int usb_host_set_interface(USBHostDevice
*s
, int iface
, int alt
)
997 struct usbdevfs_setinterface si
;
1000 trace_usb_host_set_interface(s
->bus_num
, s
->addr
, iface
, alt
);
1002 for (i
= 1; i
<= USB_MAX_ENDPOINTS
; i
++) {
1003 if (is_isoc(s
, USB_TOKEN_IN
, i
)) {
1004 usb_host_stop_n_free_iso(s
, USB_TOKEN_IN
, i
);
1006 if (is_isoc(s
, USB_TOKEN_OUT
, i
)) {
1007 usb_host_stop_n_free_iso(s
, USB_TOKEN_OUT
, i
);
1011 if (iface
>= USB_MAX_INTERFACES
) {
1012 return USB_RET_STALL
;
1015 si
.interface
= iface
;
1016 si
.altsetting
= alt
;
1017 ret
= ioctl(s
->fd
, USBDEVFS_SETINTERFACE
, &si
);
1019 DPRINTF("husb: ctrl set iface %d altset %d ret %d errno %d\n",
1020 iface
, alt
, ret
, errno
);
1023 return ctrl_error();
1026 s
->dev
.altsetting
[iface
] = alt
;
1027 usb_linux_update_endp_table(s
);
1031 static int usb_host_handle_control(USBDevice
*dev
, USBPacket
*p
,
1032 int request
, int value
, int index
, int length
, uint8_t *data
)
1034 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
1035 struct usbdevfs_urb
*urb
;
1040 * Process certain standard device requests.
1041 * These are infrequent and are processed synchronously.
1044 /* Note request is (bRequestType << 8) | bRequest */
1045 trace_usb_host_req_control(s
->bus_num
, s
->addr
, p
, request
, value
, index
);
1048 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
1049 ret
= usb_host_set_address(s
, value
);
1050 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, ret
);
1053 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
1054 ret
= usb_host_set_config(s
, value
& 0xff);
1055 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, ret
);
1058 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
1059 ret
= usb_host_set_interface(s
, index
, value
);
1060 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, ret
);
1063 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
1064 if (value
== 0) { /* clear halt */
1065 int pid
= (index
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
1066 ioctl(s
->fd
, USBDEVFS_CLEAR_HALT
, &index
);
1067 clear_halt(s
, pid
, index
& 0x0f);
1068 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, 0);
1073 /* The rest are asynchronous */
1075 if (length
> sizeof(dev
->data_buf
)) {
1076 fprintf(stderr
, "husb: ctrl buffer too small (%d > %zu)\n",
1077 length
, sizeof(dev
->data_buf
));
1078 return USB_RET_STALL
;
1081 aurb
= async_alloc(s
);
1085 * Setup ctrl transfer.
1087 * s->ctrl is laid out such that data buffer immediately follows
1088 * 'req' struct which is exactly what usbdevfs expects.
1092 urb
->type
= USBDEVFS_URB_TYPE_CONTROL
;
1093 urb
->endpoint
= p
->ep
->nr
;
1095 urb
->buffer
= &dev
->setup_buf
;
1096 urb
->buffer_length
= length
+ 8;
1098 urb
->usercontext
= s
;
1100 trace_usb_host_urb_submit(s
->bus_num
, s
->addr
, aurb
,
1101 urb
->buffer_length
, aurb
->more
);
1102 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
1104 DPRINTF("husb: submit ctrl. len %u aurb %p\n", urb
->buffer_length
, aurb
);
1107 DPRINTF("husb: submit failed. errno %d\n", errno
);
1115 return USB_RET_STALL
;
1119 return USB_RET_ASYNC
;
1122 /* returns 1 on problem encountered or 0 for success */
1123 static int usb_linux_update_endp_table(USBHostDevice
*s
)
1125 static const char *tname
[] = {
1126 [USB_ENDPOINT_XFER_CONTROL
] = "control",
1127 [USB_ENDPOINT_XFER_ISOC
] = "isoc",
1128 [USB_ENDPOINT_XFER_BULK
] = "bulk",
1129 [USB_ENDPOINT_XFER_INT
] = "int",
1131 uint8_t devep
, type
;
1134 unsigned int i
, configuration
= -1, interface
= -1, altsetting
= -1;
1135 struct endp_data
*epd
;
1137 bool active
= false;
1139 usb_ep_init(&s
->dev
);
1141 for (i
= 0;; i
+= d
->bLength
) {
1142 if (i
+2 >= s
->descr_len
) {
1145 d
= (void *)(s
->descr
+ i
);
1146 if (d
->bLength
< 2) {
1147 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1148 "descriptor too short");
1151 if (i
+ d
->bLength
> s
->descr_len
) {
1152 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1153 "descriptor too long");
1156 switch (d
->bDescriptorType
) {
1158 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1159 "invalid descriptor type");
1162 if (d
->bLength
< 0x12) {
1163 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1164 "device descriptor too short");
1167 v
= (d
->u
.device
.idVendor_hi
<< 8) | d
->u
.device
.idVendor_lo
;
1168 p
= (d
->u
.device
.idProduct_hi
<< 8) | d
->u
.device
.idProduct_lo
;
1169 trace_usb_host_parse_device(s
->bus_num
, s
->addr
, v
, p
);
1172 if (d
->bLength
< 0x09) {
1173 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1174 "config descriptor too short");
1177 configuration
= d
->u
.config
.bConfigurationValue
;
1178 active
= (configuration
== s
->dev
.configuration
);
1179 trace_usb_host_parse_config(s
->bus_num
, s
->addr
,
1180 configuration
, active
);
1182 case USB_DT_INTERFACE
:
1183 if (d
->bLength
< 0x09) {
1184 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1185 "interface descriptor too short");
1188 interface
= d
->u
.interface
.bInterfaceNumber
;
1189 altsetting
= d
->u
.interface
.bAlternateSetting
;
1190 active
= (configuration
== s
->dev
.configuration
) &&
1191 (altsetting
== s
->dev
.altsetting
[interface
]);
1192 trace_usb_host_parse_interface(s
->bus_num
, s
->addr
,
1193 interface
, altsetting
, active
);
1195 case USB_DT_ENDPOINT
:
1196 if (d
->bLength
< 0x07) {
1197 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1198 "endpoint descriptor too short");
1201 devep
= d
->u
.endpoint
.bEndpointAddress
;
1202 pid
= (devep
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
1205 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1206 "invalid endpoint address");
1210 type
= d
->u
.endpoint
.bmAttributes
& 0x3;
1211 mps
= d
->u
.endpoint
.wMaxPacketSize_lo
|
1212 (d
->u
.endpoint
.wMaxPacketSize_hi
<< 8);
1213 trace_usb_host_parse_endpoint(s
->bus_num
, s
->addr
, ep
,
1214 (devep
& USB_DIR_IN
) ? "in" : "out",
1215 tname
[type
], active
);
1218 usb_ep_set_max_packet_size(&s
->dev
, pid
, ep
, mps
);
1219 assert(usb_ep_get_type(&s
->dev
, pid
, ep
) ==
1220 USB_ENDPOINT_XFER_INVALID
);
1221 usb_ep_set_type(&s
->dev
, pid
, ep
, type
);
1222 usb_ep_set_ifnum(&s
->dev
, pid
, ep
, interface
);
1223 if ((s
->options
& (1 << USB_HOST_OPT_PIPELINE
)) &&
1224 (type
== USB_ENDPOINT_XFER_BULK
)) {
1225 usb_ep_set_pipeline(&s
->dev
, pid
, ep
, true);
1228 epd
= get_endp(s
, pid
, ep
);
1234 trace_usb_host_parse_unknown(s
->bus_num
, s
->addr
,
1235 d
->bLength
, d
->bDescriptorType
);
1242 usb_ep_init(&s
->dev
);
1247 * Check if we can safely redirect a usb2 device to a usb1 virtual controller,
1248 * this function assumes this is safe, if:
1249 * 1) There are no isoc endpoints
1250 * 2) There are no interrupt endpoints with a max_packet_size > 64
1251 * Note bulk endpoints with a max_packet_size > 64 in theory also are not
1252 * usb1 compatible, but in practice this seems to work fine.
1254 static int usb_linux_full_speed_compat(USBHostDevice
*dev
)
1259 * usb_linux_update_endp_table only registers info about ep in the current
1260 * interface altsettings, so we need to parse the descriptors again.
1262 for (i
= 0; (i
+ 5) < dev
->descr_len
; i
+= dev
->descr
[i
]) {
1263 if (dev
->descr
[i
+ 1] == USB_DT_ENDPOINT
) {
1264 switch (dev
->descr
[i
+ 3] & 0x3) {
1265 case 0x00: /* CONTROL */
1267 case 0x01: /* ISO */
1269 case 0x02: /* BULK */
1271 case 0x03: /* INTERRUPT */
1272 packet_size
= dev
->descr
[i
+ 4] + (dev
->descr
[i
+ 5] << 8);
1273 if (packet_size
> 64)
1282 static int usb_host_open(USBHostDevice
*dev
, int bus_num
,
1283 int addr
, const char *port
,
1284 const char *prod_name
, int speed
)
1288 trace_usb_host_open_started(bus_num
, addr
);
1290 if (dev
->fd
!= -1) {
1294 fd
= usb_host_open_device(bus_num
, addr
);
1298 DPRINTF("husb: opened %s\n", buf
);
1300 dev
->bus_num
= bus_num
;
1302 strcpy(dev
->port
, port
);
1305 /* read the device description */
1306 dev
->descr_len
= read(fd
, dev
->descr
, sizeof(dev
->descr
));
1307 if (dev
->descr_len
<= 0) {
1308 perror("husb: reading device data failed");
1315 printf("=== begin dumping device descriptor data ===\n");
1316 for (x
= 0; x
< dev
->descr_len
; x
++) {
1317 printf("%02x ", dev
->descr
[x
]);
1319 printf("\n=== end dumping device descriptor data ===\n");
1324 /* start unconfigured -- we'll wait for the guest to set a configuration */
1325 if (!usb_host_claim_interfaces(dev
, 0)) {
1329 ret
= usb_linux_update_endp_table(dev
);
1335 struct usbdevfs_connectinfo ci
;
1337 ret
= ioctl(fd
, USBDEVFS_CONNECTINFO
, &ci
);
1339 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
1344 speed
= USB_SPEED_LOW
;
1346 speed
= USB_SPEED_HIGH
;
1349 dev
->dev
.speed
= speed
;
1350 dev
->dev
.speedmask
= (1 << speed
);
1351 if (dev
->dev
.speed
== USB_SPEED_HIGH
&& usb_linux_full_speed_compat(dev
)) {
1352 dev
->dev
.speedmask
|= USB_SPEED_MASK_FULL
;
1355 trace_usb_host_open_success(bus_num
, addr
);
1357 if (!prod_name
|| prod_name
[0] == '\0') {
1358 snprintf(dev
->dev
.product_desc
, sizeof(dev
->dev
.product_desc
),
1359 "host:%d.%d", bus_num
, addr
);
1361 pstrcpy(dev
->dev
.product_desc
, sizeof(dev
->dev
.product_desc
),
1365 ret
= usb_device_attach(&dev
->dev
);
1370 /* USB devio uses 'write' flag to check for async completions */
1371 qemu_set_fd_handler(dev
->fd
, NULL
, async_complete
, dev
);
1376 trace_usb_host_open_failure(bus_num
, addr
);
1377 if (dev
->fd
!= -1) {
1384 static int usb_host_close(USBHostDevice
*dev
)
1388 if (dev
->fd
== -1) {
1392 trace_usb_host_close(dev
->bus_num
, dev
->addr
);
1394 qemu_set_fd_handler(dev
->fd
, NULL
, NULL
, NULL
);
1396 for (i
= 1; i
<= USB_MAX_ENDPOINTS
; i
++) {
1397 if (is_isoc(dev
, USB_TOKEN_IN
, i
)) {
1398 usb_host_stop_n_free_iso(dev
, USB_TOKEN_IN
, i
);
1400 if (is_isoc(dev
, USB_TOKEN_OUT
, i
)) {
1401 usb_host_stop_n_free_iso(dev
, USB_TOKEN_OUT
, i
);
1404 async_complete(dev
);
1406 if (dev
->dev
.attached
) {
1407 usb_device_detach(&dev
->dev
);
1409 usb_host_do_reset(dev
);
1415 static void usb_host_exit_notifier(struct Notifier
*n
, void *data
)
1417 USBHostDevice
*s
= container_of(n
, USBHostDevice
, exit
);
1419 usb_host_release_port(s
);
1421 usb_host_do_reset(s
);;
1426 * This is *NOT* about restoring state. We have absolutely no idea
1427 * what state the host device is in at the moment and whenever it is
1428 * still present in the first place. Attemping to contine where we
1429 * left off is impossible.
1431 * What we are going to to to here is emulate a surprise removal of
1432 * the usb device passed through, then kick host scan so the device
1433 * will get re-attached (and re-initialized by the guest) in case it
1436 * As the device removal will change the state of other devices (usb
1437 * host controller, most likely interrupt controller too) we have to
1438 * wait with it until *all* vmstate is loaded. Thus post_load just
1439 * kicks a bottom half which then does the actual work.
1441 static void usb_host_post_load_bh(void *opaque
)
1443 USBHostDevice
*dev
= opaque
;
1445 if (dev
->fd
!= -1) {
1446 usb_host_close(dev
);
1448 if (dev
->dev
.attached
) {
1449 usb_device_detach(&dev
->dev
);
1451 usb_host_auto_check(NULL
);
1454 static int usb_host_post_load(void *opaque
, int version_id
)
1456 USBHostDevice
*dev
= opaque
;
1458 qemu_bh_schedule(dev
->bh
);
1462 static int usb_host_initfn(USBDevice
*dev
)
1464 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
1466 dev
->auto_attach
= 0;
1470 QTAILQ_INSERT_TAIL(&hostdevs
, s
, next
);
1471 s
->exit
.notify
= usb_host_exit_notifier
;
1472 qemu_add_exit_notifier(&s
->exit
);
1473 s
->bh
= qemu_bh_new(usb_host_post_load_bh
, s
);
1474 usb_host_auto_check(NULL
);
1476 if (s
->match
.bus_num
!= 0 && s
->match
.port
!= NULL
) {
1477 usb_host_claim_port(s
);
1479 add_boot_device_path(s
->bootindex
, &dev
->qdev
, NULL
);
1483 static const VMStateDescription vmstate_usb_host
= {
1486 .minimum_version_id
= 1,
1487 .post_load
= usb_host_post_load
,
1488 .fields
= (VMStateField
[]) {
1489 VMSTATE_USB_DEVICE(dev
, USBHostDevice
),
1490 VMSTATE_END_OF_LIST()
1494 static Property usb_host_dev_properties
[] = {
1495 DEFINE_PROP_UINT32("hostbus", USBHostDevice
, match
.bus_num
, 0),
1496 DEFINE_PROP_UINT32("hostaddr", USBHostDevice
, match
.addr
, 0),
1497 DEFINE_PROP_STRING("hostport", USBHostDevice
, match
.port
),
1498 DEFINE_PROP_HEX32("vendorid", USBHostDevice
, match
.vendor_id
, 0),
1499 DEFINE_PROP_HEX32("productid", USBHostDevice
, match
.product_id
, 0),
1500 DEFINE_PROP_UINT32("isobufs", USBHostDevice
, iso_urb_count
, 4),
1501 DEFINE_PROP_INT32("bootindex", USBHostDevice
, bootindex
, -1),
1502 DEFINE_PROP_BIT("pipeline", USBHostDevice
, options
,
1503 USB_HOST_OPT_PIPELINE
, true),
1504 DEFINE_PROP_END_OF_LIST(),
1507 static void usb_host_class_initfn(ObjectClass
*klass
, void *data
)
1509 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1510 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
1512 uc
->init
= usb_host_initfn
;
1513 uc
->product_desc
= "USB Host Device";
1514 uc
->cancel_packet
= usb_host_async_cancel
;
1515 uc
->handle_data
= usb_host_handle_data
;
1516 uc
->handle_control
= usb_host_handle_control
;
1517 uc
->handle_reset
= usb_host_handle_reset
;
1518 uc
->handle_destroy
= usb_host_handle_destroy
;
1519 dc
->vmsd
= &vmstate_usb_host
;
1520 dc
->props
= usb_host_dev_properties
;
1523 static TypeInfo usb_host_dev_info
= {
1525 .parent
= TYPE_USB_DEVICE
,
1526 .instance_size
= sizeof(USBHostDevice
),
1527 .class_init
= usb_host_class_initfn
,
1530 static void usb_host_register_types(void)
1532 type_register_static(&usb_host_dev_info
);
1533 usb_legacy_register("usb-host", "host", usb_host_device_open
);
1536 type_init(usb_host_register_types
)
1538 USBDevice
*usb_host_device_open(USBBus
*bus
, const char *devname
)
1540 struct USBAutoFilter filter
;
1544 dev
= usb_create(bus
, "usb-host");
1546 if (strstr(devname
, "auto:")) {
1547 if (parse_filter(devname
, &filter
) < 0) {
1551 if ((p
= strchr(devname
, '.'))) {
1552 filter
.bus_num
= strtoul(devname
, NULL
, 0);
1553 filter
.addr
= strtoul(p
+ 1, NULL
, 0);
1554 filter
.vendor_id
= 0;
1555 filter
.product_id
= 0;
1556 } else if ((p
= strchr(devname
, ':'))) {
1559 filter
.vendor_id
= strtoul(devname
, NULL
, 16);
1560 filter
.product_id
= strtoul(p
+ 1, NULL
, 16);
1566 qdev_prop_set_uint32(&dev
->qdev
, "hostbus", filter
.bus_num
);
1567 qdev_prop_set_uint32(&dev
->qdev
, "hostaddr", filter
.addr
);
1568 qdev_prop_set_uint32(&dev
->qdev
, "vendorid", filter
.vendor_id
);
1569 qdev_prop_set_uint32(&dev
->qdev
, "productid", filter
.product_id
);
1570 qdev_init_nofail(&dev
->qdev
);
1574 qdev_free(&dev
->qdev
);
1578 int usb_host_device_close(const char *devname
)
1581 char product_name
[PRODUCT_NAME_SZ
];
1585 if (strstr(devname
, "auto:")) {
1586 return usb_host_auto_del(devname
);
1588 if (usb_host_find_device(&bus_num
, &addr
, product_name
,
1589 sizeof(product_name
), devname
) < 0) {
1592 s
= hostdev_find(bus_num
, addr
);
1594 usb_device_delete_addr(s
->bus_num
, s
->dev
.addr
);
1603 * Read sys file-system device file
1605 * @line address of buffer to put file contents in
1606 * @line_size size of line
1607 * @device_file path to device file (printf format string)
1608 * @device_name device being opened (inserted into device_file)
1610 * @return 0 failed, 1 succeeded ('line' contains data)
1612 static int usb_host_read_file(char *line
, size_t line_size
,
1613 const char *device_file
, const char *device_name
)
1617 char filename
[PATH_MAX
];
1619 snprintf(filename
, PATH_MAX
, "/sys/bus/usb/devices/%s/%s", device_name
,
1621 f
= fopen(filename
, "r");
1623 ret
= fgets(line
, line_size
, f
) != NULL
;
1631 * Use /sys/bus/usb/devices/ directory to determine host's USB
1634 * This code is based on Robert Schiele's original patches posted to
1635 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1637 static int usb_host_scan(void *opaque
, USBScanFunc
*func
)
1641 int bus_num
, addr
, speed
, class_id
, product_id
, vendor_id
;
1643 char port
[MAX_PORTLEN
];
1644 char product_name
[512];
1647 dir
= opendir("/sys/bus/usb/devices");
1649 perror("husb: opendir /sys/bus/usb/devices");
1650 fprintf(stderr
, "husb: please make sure sysfs is mounted at /sys\n");
1654 while ((de
= readdir(dir
))) {
1655 if (de
->d_name
[0] != '.' && !strchr(de
->d_name
, ':')) {
1656 if (sscanf(de
->d_name
, "%d-%7[0-9.]", &bus_num
, port
) < 2) {
1660 if (!usb_host_read_file(line
, sizeof(line
), "devnum", de
->d_name
)) {
1663 if (sscanf(line
, "%d", &addr
) != 1) {
1666 if (!usb_host_read_file(line
, sizeof(line
), "bDeviceClass",
1670 if (sscanf(line
, "%x", &class_id
) != 1) {
1674 if (!usb_host_read_file(line
, sizeof(line
), "idVendor",
1678 if (sscanf(line
, "%x", &vendor_id
) != 1) {
1681 if (!usb_host_read_file(line
, sizeof(line
), "idProduct",
1685 if (sscanf(line
, "%x", &product_id
) != 1) {
1688 if (!usb_host_read_file(line
, sizeof(line
), "product",
1692 if (strlen(line
) > 0) {
1693 line
[strlen(line
) - 1] = '\0';
1695 pstrcpy(product_name
, sizeof(product_name
), line
);
1698 if (!usb_host_read_file(line
, sizeof(line
), "speed", de
->d_name
)) {
1701 if (!strcmp(line
, "5000\n")) {
1702 speed
= USB_SPEED_SUPER
;
1703 } else if (!strcmp(line
, "480\n")) {
1704 speed
= USB_SPEED_HIGH
;
1705 } else if (!strcmp(line
, "1.5\n")) {
1706 speed
= USB_SPEED_LOW
;
1708 speed
= USB_SPEED_FULL
;
1711 ret
= func(opaque
, bus_num
, addr
, port
, class_id
, vendor_id
,
1712 product_id
, product_name
, speed
);
1725 static QEMUTimer
*usb_auto_timer
;
1727 static int usb_host_auto_scan(void *opaque
, int bus_num
,
1728 int addr
, const char *port
,
1729 int class_id
, int vendor_id
, int product_id
,
1730 const char *product_name
, int speed
)
1732 struct USBAutoFilter
*f
;
1733 struct USBHostDevice
*s
;
1739 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1742 if (f
->bus_num
> 0 && f
->bus_num
!= bus_num
) {
1745 if (f
->addr
> 0 && f
->addr
!= addr
) {
1748 if (f
->port
!= NULL
&& (port
== NULL
|| strcmp(f
->port
, port
) != 0)) {
1752 if (f
->vendor_id
> 0 && f
->vendor_id
!= vendor_id
) {
1756 if (f
->product_id
> 0 && f
->product_id
!= product_id
) {
1759 /* We got a match */
1761 if (s
->errcount
>= 3) {
1765 /* Already attached ? */
1769 DPRINTF("husb: auto open: bus_num %d addr %d\n", bus_num
, addr
);
1771 if (usb_host_open(s
, bus_num
, addr
, port
, product_name
, speed
) < 0) {
1780 static void usb_host_auto_check(void *unused
)
1782 struct USBHostDevice
*s
;
1783 int unconnected
= 0;
1785 if (runstate_is_running()) {
1786 usb_host_scan(NULL
, usb_host_auto_scan
);
1788 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1798 if (unconnected
== 0) {
1799 /* nothing to watch */
1800 if (usb_auto_timer
) {
1801 qemu_del_timer(usb_auto_timer
);
1802 trace_usb_host_auto_scan_disabled();
1808 if (!usb_auto_timer
) {
1809 usb_auto_timer
= qemu_new_timer_ms(rt_clock
, usb_host_auto_check
, NULL
);
1810 if (!usb_auto_timer
) {
1813 trace_usb_host_auto_scan_enabled();
1815 qemu_mod_timer(usb_auto_timer
, qemu_get_clock_ms(rt_clock
) + 2000);
1819 * Autoconnect filter
1821 * auto:bus:dev[:vid:pid]
1822 * auto:bus.dev[:vid:pid]
1824 * bus - bus number (dec, * means any)
1825 * dev - device number (dec, * means any)
1826 * vid - vendor id (hex, * means any)
1827 * pid - product id (hex, * means any)
1829 * See 'lsusb' output.
1831 static int parse_filter(const char *spec
, struct USBAutoFilter
*f
)
1833 enum { BUS
, DEV
, VID
, PID
, DONE
};
1834 const char *p
= spec
;
1842 for (i
= BUS
; i
< DONE
; i
++) {
1843 p
= strpbrk(p
, ":.");
1853 case BUS
: f
->bus_num
= strtol(p
, NULL
, 10); break;
1854 case DEV
: f
->addr
= strtol(p
, NULL
, 10); break;
1855 case VID
: f
->vendor_id
= strtol(p
, NULL
, 16); break;
1856 case PID
: f
->product_id
= strtol(p
, NULL
, 16); break;
1861 fprintf(stderr
, "husb: invalid auto filter spec %s\n", spec
);
1868 /**********************/
1869 /* USB host device info */
1871 struct usb_class_info
{
1873 const char *class_name
;
1876 static const struct usb_class_info usb_class_info
[] = {
1877 { USB_CLASS_AUDIO
, "Audio"},
1878 { USB_CLASS_COMM
, "Communication"},
1879 { USB_CLASS_HID
, "HID"},
1880 { USB_CLASS_HUB
, "Hub" },
1881 { USB_CLASS_PHYSICAL
, "Physical" },
1882 { USB_CLASS_PRINTER
, "Printer" },
1883 { USB_CLASS_MASS_STORAGE
, "Storage" },
1884 { USB_CLASS_CDC_DATA
, "Data" },
1885 { USB_CLASS_APP_SPEC
, "Application Specific" },
1886 { USB_CLASS_VENDOR_SPEC
, "Vendor Specific" },
1887 { USB_CLASS_STILL_IMAGE
, "Still Image" },
1888 { USB_CLASS_CSCID
, "Smart Card" },
1889 { USB_CLASS_CONTENT_SEC
, "Content Security" },
1893 static const char *usb_class_str(uint8_t class)
1895 const struct usb_class_info
*p
;
1896 for(p
= usb_class_info
; p
->class != -1; p
++) {
1897 if (p
->class == class) {
1901 return p
->class_name
;
1904 static void usb_info_device(Monitor
*mon
, int bus_num
,
1905 int addr
, const char *port
,
1906 int class_id
, int vendor_id
, int product_id
,
1907 const char *product_name
,
1910 const char *class_str
, *speed_str
;
1916 case USB_SPEED_FULL
:
1919 case USB_SPEED_HIGH
:
1922 case USB_SPEED_SUPER
:
1930 monitor_printf(mon
, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
1931 bus_num
, addr
, port
, speed_str
);
1932 class_str
= usb_class_str(class_id
);
1934 monitor_printf(mon
, " %s:", class_str
);
1936 monitor_printf(mon
, " Class %02x:", class_id
);
1938 monitor_printf(mon
, " USB device %04x:%04x", vendor_id
, product_id
);
1939 if (product_name
[0] != '\0') {
1940 monitor_printf(mon
, ", %s", product_name
);
1942 monitor_printf(mon
, "\n");
1945 static int usb_host_info_device(void *opaque
, int bus_num
, int addr
,
1946 const char *path
, int class_id
,
1947 int vendor_id
, int product_id
,
1948 const char *product_name
,
1951 Monitor
*mon
= opaque
;
1953 usb_info_device(mon
, bus_num
, addr
, path
, class_id
, vendor_id
, product_id
,
1954 product_name
, speed
);
1958 static void dec2str(int val
, char *str
, size_t size
)
1961 snprintf(str
, size
, "*");
1963 snprintf(str
, size
, "%d", val
);
1967 static void hex2str(int val
, char *str
, size_t size
)
1970 snprintf(str
, size
, "*");
1972 snprintf(str
, size
, "%04x", val
);
1976 void usb_host_info(Monitor
*mon
)
1978 struct USBAutoFilter
*f
;
1979 struct USBHostDevice
*s
;
1981 usb_host_scan(mon
, usb_host_info_device
);
1983 if (QTAILQ_EMPTY(&hostdevs
)) {
1987 monitor_printf(mon
, " Auto filters:\n");
1988 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1989 char bus
[10], addr
[10], vid
[10], pid
[10];
1991 dec2str(f
->bus_num
, bus
, sizeof(bus
));
1992 dec2str(f
->addr
, addr
, sizeof(addr
));
1993 hex2str(f
->vendor_id
, vid
, sizeof(vid
));
1994 hex2str(f
->product_id
, pid
, sizeof(pid
));
1995 monitor_printf(mon
, " Bus %s, Addr %s, Port %s, ID %s:%s\n",
1996 bus
, addr
, f
->port
? f
->port
: "*", vid
, pid
);