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 void 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_iso_stop(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_iso_start(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 /* indicates possible latency issues */
323 trace_usb_host_iso_many_urbs(s
->bus_num
, s
->addr
, urbs
);
327 if (errno
== ENODEV
) {
329 trace_usb_host_disconnect(s
->bus_num
, s
->addr
);
335 perror("USBDEVFS_REAPURBNDELAY");
339 DPRINTF("husb: async completed. aurb %p status %d alen %d\n",
340 aurb
, aurb
->urb
.status
, aurb
->urb
.actual_length
);
342 /* If this is a buffered iso urb mark it as complete and don't do
343 anything else (it is handled further in usb_host_handle_iso_data) */
344 if (aurb
->iso_frame_idx
== -1) {
346 int pid
= (aurb
->urb
.endpoint
& USB_DIR_IN
) ?
347 USB_TOKEN_IN
: USB_TOKEN_OUT
;
348 int ep
= aurb
->urb
.endpoint
& 0xf;
349 if (aurb
->urb
.status
== -EPIPE
) {
350 set_halt(s
, pid
, ep
);
352 aurb
->iso_frame_idx
= 0;
354 inflight
= change_iso_inflight(s
, pid
, ep
, -1);
355 if (inflight
== 0 && is_iso_started(s
, pid
, ep
)) {
356 /* can be latency issues, or simply end of stream */
357 trace_usb_host_iso_out_of_bufs(s
->bus_num
, s
->addr
, ep
);
363 trace_usb_host_urb_complete(s
->bus_num
, s
->addr
, aurb
, aurb
->urb
.status
,
364 aurb
->urb
.actual_length
, aurb
->more
);
367 switch (aurb
->urb
.status
) {
369 p
->actual_length
= aurb
->urb
.actual_length
;
370 p
->status
= USB_RET_SUCCESS
; /* Clear previous ASYNC status */
374 set_halt(s
, p
->pid
, p
->ep
->nr
);
375 p
->status
= USB_RET_STALL
;
379 p
->status
= USB_RET_BABBLE
;
383 p
->status
= USB_RET_IOERROR
;
387 if (aurb
->urb
.type
== USBDEVFS_URB_TYPE_CONTROL
) {
388 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
, p
->status
);
389 usb_generic_async_ctrl_complete(&s
->dev
, p
);
390 } else if (!aurb
->more
) {
391 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
, p
->status
);
392 usb_packet_complete(&s
->dev
, p
);
400 static void usb_host_async_cancel(USBDevice
*dev
, USBPacket
*p
)
402 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
405 trace_usb_host_req_canceled(s
->bus_num
, s
->addr
, p
);
407 QLIST_FOREACH(aurb
, &s
->aurbs
, next
) {
408 if (p
!= aurb
->packet
) {
412 trace_usb_host_urb_canceled(s
->bus_num
, s
->addr
, aurb
);
414 /* Mark it as dead (see async_complete above) */
417 int r
= ioctl(s
->fd
, USBDEVFS_DISCARDURB
, aurb
);
419 DPRINTF("husb: async. discard urb failed errno %d\n", errno
);
424 static int usb_host_open_device(int bus
, int addr
)
426 const char *usbfs
= NULL
;
431 rc
= stat("/dev/bus/usb", &st
);
432 if (rc
== 0 && S_ISDIR(st
.st_mode
)) {
433 /* udev-created device nodes available */
434 usbfs
= "/dev/bus/usb";
436 /* fallback: usbfs mounted below /proc */
437 usbfs
= "/proc/bus/usb";
440 snprintf(filename
, sizeof(filename
), "%s/%03d/%03d",
442 fd
= open(filename
, O_RDWR
| O_NONBLOCK
);
444 fprintf(stderr
, "husb: open %s: %s\n", filename
, strerror(errno
));
449 static int usb_host_claim_port(USBHostDevice
*s
)
451 #ifdef USBDEVFS_CLAIM_PORT
452 char *h
, hub_name
[64], line
[1024];
455 snprintf(hub_name
, sizeof(hub_name
), "%d-%s",
456 s
->match
.bus_num
, s
->match
.port
);
458 /* try strip off last ".$portnr" to get hub */
459 h
= strrchr(hub_name
, '.');
461 s
->hub_port
= atoi(h
+1);
464 /* no dot in there -> it is the root hub */
465 snprintf(hub_name
, sizeof(hub_name
), "usb%d",
467 s
->hub_port
= atoi(s
->match
.port
);
470 if (!usb_host_read_file(line
, sizeof(line
), "devnum",
474 if (sscanf(line
, "%d", &hub_addr
) != 1) {
478 s
->hub_fd
= usb_host_open_device(s
->match
.bus_num
, hub_addr
);
483 ret
= ioctl(s
->hub_fd
, USBDEVFS_CLAIM_PORT
, &s
->hub_port
);
490 trace_usb_host_claim_port(s
->match
.bus_num
, hub_addr
, s
->hub_port
);
497 static void usb_host_release_port(USBHostDevice
*s
)
499 if (s
->hub_fd
== -1) {
502 #ifdef USBDEVFS_RELEASE_PORT
503 ioctl(s
->hub_fd
, USBDEVFS_RELEASE_PORT
, &s
->hub_port
);
509 static int usb_host_disconnect_ifaces(USBHostDevice
*dev
, int nb_interfaces
)
511 /* earlier Linux 2.4 do not support that */
512 #ifdef USBDEVFS_DISCONNECT
513 struct usbdevfs_ioctl ctrl
;
516 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
517 ctrl
.ioctl_code
= USBDEVFS_DISCONNECT
;
518 ctrl
.ifno
= interface
;
520 ret
= ioctl(dev
->fd
, USBDEVFS_IOCTL
, &ctrl
);
521 if (ret
< 0 && errno
!= ENODATA
) {
522 perror("USBDEVFS_DISCONNECT");
530 static int usb_linux_get_num_interfaces(USBHostDevice
*s
)
532 char device_name
[64], line
[1024];
533 int num_interfaces
= 0;
535 sprintf(device_name
, "%d-%s", s
->bus_num
, s
->port
);
536 if (!usb_host_read_file(line
, sizeof(line
), "bNumInterfaces",
540 if (sscanf(line
, "%d", &num_interfaces
) != 1) {
543 return num_interfaces
;
546 static int usb_host_claim_interfaces(USBHostDevice
*dev
, int configuration
)
548 const char *op
= NULL
;
549 int dev_descr_len
, config_descr_len
;
550 int interface
, nb_interfaces
;
553 for (i
= 0; i
< USB_MAX_INTERFACES
; i
++) {
554 dev
->dev
.altsetting
[i
] = 0;
557 if (configuration
== 0) { /* address state - ignore */
558 dev
->dev
.ninterfaces
= 0;
559 dev
->dev
.configuration
= 0;
563 DPRINTF("husb: claiming interfaces. config %d\n", configuration
);
566 dev_descr_len
= dev
->descr
[0];
567 if (dev_descr_len
> dev
->descr_len
) {
568 fprintf(stderr
, "husb: update iface failed. descr too short\n");
573 while (i
< dev
->descr_len
) {
574 DPRINTF("husb: i is %d, descr_len is %d, dl %d, dt %d\n",
576 dev
->descr
[i
], dev
->descr
[i
+1]);
578 if (dev
->descr
[i
+1] != USB_DT_CONFIG
) {
582 config_descr_len
= dev
->descr
[i
];
584 DPRINTF("husb: config #%d need %d\n", dev
->descr
[i
+ 5], configuration
);
586 if (configuration
== dev
->descr
[i
+ 5]) {
587 configuration
= dev
->descr
[i
+ 5];
591 i
+= config_descr_len
;
594 if (i
>= dev
->descr_len
) {
596 "husb: update iface failed. no matching configuration\n");
599 nb_interfaces
= dev
->descr
[i
+ 4];
601 if (usb_host_disconnect_ifaces(dev
, nb_interfaces
) < 0) {
605 /* XXX: only grab if all interfaces are free */
606 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
607 op
= "USBDEVFS_CLAIMINTERFACE";
608 ret
= ioctl(dev
->fd
, USBDEVFS_CLAIMINTERFACE
, &interface
);
614 trace_usb_host_claim_interfaces(dev
->bus_num
, dev
->addr
,
615 nb_interfaces
, configuration
);
617 dev
->dev
.ninterfaces
= nb_interfaces
;
618 dev
->dev
.configuration
= configuration
;
622 if (errno
== ENODEV
) {
629 static int usb_host_release_interfaces(USBHostDevice
*s
)
633 trace_usb_host_release_interfaces(s
->bus_num
, s
->addr
);
635 for (i
= 0; i
< s
->dev
.ninterfaces
; i
++) {
636 ret
= ioctl(s
->fd
, USBDEVFS_RELEASEINTERFACE
, &i
);
638 perror("USBDEVFS_RELEASEINTERFACE");
645 static void usb_host_handle_reset(USBDevice
*dev
)
647 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
649 trace_usb_host_reset(s
->bus_num
, s
->addr
);
651 usb_host_do_reset(s
);;
653 usb_host_claim_interfaces(s
, 0);
654 usb_linux_update_endp_table(s
);
657 static void usb_host_handle_destroy(USBDevice
*dev
)
659 USBHostDevice
*s
= (USBHostDevice
*)dev
;
661 usb_host_release_port(s
);
663 QTAILQ_REMOVE(&hostdevs
, s
, next
);
664 qemu_remove_exit_notifier(&s
->exit
);
667 /* iso data is special, we need to keep enough urbs in flight to make sure
668 that the controller never runs out of them, otherwise the device will
669 likely suffer a buffer underrun / overrun. */
670 static AsyncURB
*usb_host_alloc_iso(USBHostDevice
*s
, int pid
, uint8_t ep
)
673 int i
, j
, len
= usb_ep_get_max_packet_size(&s
->dev
, pid
, ep
);
675 aurb
= g_malloc0(s
->iso_urb_count
* sizeof(*aurb
));
676 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
677 aurb
[i
].urb
.endpoint
= ep
;
678 aurb
[i
].urb
.buffer_length
= ISO_FRAME_DESC_PER_URB
* len
;
679 aurb
[i
].urb
.buffer
= g_malloc(aurb
[i
].urb
.buffer_length
);
680 aurb
[i
].urb
.type
= USBDEVFS_URB_TYPE_ISO
;
681 aurb
[i
].urb
.flags
= USBDEVFS_URB_ISO_ASAP
;
682 aurb
[i
].urb
.number_of_packets
= ISO_FRAME_DESC_PER_URB
;
683 for (j
= 0 ; j
< ISO_FRAME_DESC_PER_URB
; j
++)
684 aurb
[i
].urb
.iso_frame_desc
[j
].length
= len
;
685 if (pid
== USB_TOKEN_IN
) {
686 aurb
[i
].urb
.endpoint
|= 0x80;
687 /* Mark as fully consumed (idle) */
688 aurb
[i
].iso_frame_idx
= ISO_FRAME_DESC_PER_URB
;
691 set_iso_urb(s
, pid
, ep
, aurb
);
696 static void usb_host_stop_n_free_iso(USBHostDevice
*s
, int pid
, uint8_t ep
)
699 int i
, ret
, killed
= 0, free
= 1;
701 aurb
= get_iso_urb(s
, pid
, ep
);
706 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
708 if (aurb
[i
].iso_frame_idx
== -1) {
709 ret
= ioctl(s
->fd
, USBDEVFS_DISCARDURB
, &aurb
[i
]);
711 perror("USBDEVFS_DISCARDURB");
719 /* Make sure any urbs we've killed are reaped before we free them */
724 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
725 g_free(aurb
[i
].urb
.buffer
);
731 printf("husb: leaking iso urbs because of discard failure\n");
732 set_iso_urb(s
, pid
, ep
, NULL
);
733 set_iso_urb_idx(s
, pid
, ep
, 0);
734 clear_iso_started(s
, pid
, ep
);
737 static void urb_status_to_usb_ret(int status
, USBPacket
*p
)
741 p
->status
= USB_RET_STALL
;
744 p
->status
= USB_RET_BABBLE
;
747 p
->status
= USB_RET_IOERROR
;
751 static void usb_host_handle_iso_data(USBHostDevice
*s
, USBPacket
*p
, int in
)
754 int i
, j
, max_packet_size
, offset
, len
;
757 max_packet_size
= p
->ep
->max_packet_size
;
758 if (max_packet_size
== 0) {
759 p
->status
= USB_RET_NAK
;
763 aurb
= get_iso_urb(s
, p
->pid
, p
->ep
->nr
);
765 aurb
= usb_host_alloc_iso(s
, p
->pid
, p
->ep
->nr
);
768 i
= get_iso_urb_idx(s
, p
->pid
, p
->ep
->nr
);
769 j
= aurb
[i
].iso_frame_idx
;
770 if (j
>= 0 && j
< ISO_FRAME_DESC_PER_URB
) {
772 /* Check urb status */
773 if (aurb
[i
].urb
.status
) {
774 urb_status_to_usb_ret(aurb
[i
].urb
.status
, p
);
775 /* Move to the next urb */
776 aurb
[i
].iso_frame_idx
= ISO_FRAME_DESC_PER_URB
- 1;
777 /* Check frame status */
778 } else if (aurb
[i
].urb
.iso_frame_desc
[j
].status
) {
779 urb_status_to_usb_ret(aurb
[i
].urb
.iso_frame_desc
[j
].status
, p
);
780 /* Check the frame fits */
781 } else if (aurb
[i
].urb
.iso_frame_desc
[j
].actual_length
783 printf("husb: received iso data is larger then packet\n");
784 p
->status
= USB_RET_BABBLE
;
785 /* All good copy data over */
787 len
= aurb
[i
].urb
.iso_frame_desc
[j
].actual_length
;
788 buf
= aurb
[i
].urb
.buffer
+
789 j
* aurb
[i
].urb
.iso_frame_desc
[0].length
;
790 usb_packet_copy(p
, buf
, len
);
794 offset
= (j
== 0) ? 0 : get_iso_buffer_used(s
, p
->pid
, p
->ep
->nr
);
796 /* Check the frame fits */
797 if (len
> max_packet_size
) {
798 printf("husb: send iso data is larger then max packet size\n");
799 p
->status
= USB_RET_NAK
;
803 /* All good copy data over */
804 usb_packet_copy(p
, aurb
[i
].urb
.buffer
+ offset
, len
);
805 aurb
[i
].urb
.iso_frame_desc
[j
].length
= len
;
807 set_iso_buffer_used(s
, p
->pid
, p
->ep
->nr
, offset
);
809 /* Start the stream once we have buffered enough data */
810 if (!is_iso_started(s
, p
->pid
, p
->ep
->nr
) && i
== 1 && j
== 8) {
811 set_iso_started(s
, p
->pid
, p
->ep
->nr
);
814 aurb
[i
].iso_frame_idx
++;
815 if (aurb
[i
].iso_frame_idx
== ISO_FRAME_DESC_PER_URB
) {
816 i
= (i
+ 1) % s
->iso_urb_count
;
817 set_iso_urb_idx(s
, p
->pid
, p
->ep
->nr
, i
);
821 set_iso_started(s
, p
->pid
, p
->ep
->nr
);
823 DPRINTF("hubs: iso out error no free buffer, dropping packet\n");
827 if (is_iso_started(s
, p
->pid
, p
->ep
->nr
)) {
828 /* (Re)-submit all fully consumed / filled urbs */
829 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
830 if (aurb
[i
].iso_frame_idx
== ISO_FRAME_DESC_PER_URB
) {
831 if (ioctl(s
->fd
, USBDEVFS_SUBMITURB
, &aurb
[i
]) < 0) {
832 perror("USBDEVFS_SUBMITURB");
833 if (!in
|| p
->status
== USB_RET_SUCCESS
) {
836 p
->status
= USB_RET_NAK
;
840 p
->status
= USB_RET_STALL
;
845 aurb
[i
].iso_frame_idx
= -1;
846 change_iso_inflight(s
, p
->pid
, p
->ep
->nr
, 1);
852 static void usb_host_handle_data(USBDevice
*dev
, USBPacket
*p
)
854 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
855 struct usbdevfs_urb
*urb
;
857 int ret
, rem
, prem
, v
;
861 trace_usb_host_req_data(s
->bus_num
, s
->addr
, p
,
862 p
->pid
== USB_TOKEN_IN
,
863 p
->ep
->nr
, p
->iov
.size
);
865 if (!is_valid(s
, p
->pid
, p
->ep
->nr
)) {
866 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
, USB_RET_NAK
);
867 p
->status
= USB_RET_NAK
;
871 if (p
->pid
== USB_TOKEN_IN
) {
872 ep
= p
->ep
->nr
| 0x80;
877 if (is_halted(s
, p
->pid
, p
->ep
->nr
)) {
878 unsigned int arg
= ep
;
879 ret
= ioctl(s
->fd
, USBDEVFS_CLEAR_HALT
, &arg
);
881 perror("USBDEVFS_CLEAR_HALT");
882 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
, USB_RET_NAK
);
883 p
->status
= USB_RET_NAK
;
886 clear_halt(s
, p
->pid
, p
->ep
->nr
);
889 if (is_isoc(s
, p
->pid
, p
->ep
->nr
)) {
890 usb_host_handle_iso_data(s
, p
, p
->pid
== USB_TOKEN_IN
);
899 if (prem
== 0 && rem
> 0) {
900 assert(v
< p
->iov
.niov
);
901 prem
= p
->iov
.iov
[v
].iov_len
;
902 pbuf
= p
->iov
.iov
[v
].iov_base
;
906 aurb
= async_alloc(s
);
911 urb
->type
= usb_host_usbfs_type(s
, p
);
912 urb
->usercontext
= s
;
914 urb
->buffer_length
= prem
;
916 if (urb
->buffer_length
> MAX_USBFS_BUFFER_SIZE
) {
917 urb
->buffer_length
= MAX_USBFS_BUFFER_SIZE
;
919 pbuf
+= urb
->buffer_length
;
920 prem
-= urb
->buffer_length
;
921 rem
-= urb
->buffer_length
;
926 trace_usb_host_urb_submit(s
->bus_num
, s
->addr
, aurb
,
927 urb
->buffer_length
, aurb
->more
);
928 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
930 DPRINTF("husb: data submit: ep 0x%x, len %u, more %d, packet %p, aurb %p\n",
931 urb
->endpoint
, urb
->buffer_length
, aurb
->more
, p
, aurb
);
934 perror("USBDEVFS_SUBMITURB");
939 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
941 p
->status
= USB_RET_NAK
;
945 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
947 p
->status
= USB_RET_STALL
;
953 p
->status
= USB_RET_ASYNC
;
956 static int ctrl_error(void)
958 if (errno
== ETIMEDOUT
) {
961 return USB_RET_STALL
;
965 static void usb_host_set_address(USBHostDevice
*s
, int addr
)
967 trace_usb_host_set_address(s
->bus_num
, s
->addr
, addr
);
971 static void usb_host_set_config(USBHostDevice
*s
, int config
, USBPacket
*p
)
975 trace_usb_host_set_config(s
->bus_num
, s
->addr
, config
);
977 usb_host_release_interfaces(s
);
980 ret
= ioctl(s
->fd
, USBDEVFS_SETCONFIGURATION
, &config
);
982 DPRINTF("husb: ctrl set config %d ret %d errno %d\n", config
, ret
, errno
);
984 if (ret
< 0 && errno
== EBUSY
&& first
) {
985 /* happens if usb device is in use by host drivers */
986 int count
= usb_linux_get_num_interfaces(s
);
988 DPRINTF("husb: busy -> disconnecting %d interfaces\n", count
);
989 usb_host_disconnect_ifaces(s
, count
);
996 p
->status
= ctrl_error();
999 usb_host_claim_interfaces(s
, config
);
1000 usb_linux_update_endp_table(s
);
1003 static void usb_host_set_interface(USBHostDevice
*s
, int iface
, int alt
,
1006 struct usbdevfs_setinterface si
;
1009 trace_usb_host_set_interface(s
->bus_num
, s
->addr
, iface
, alt
);
1011 for (i
= 1; i
<= USB_MAX_ENDPOINTS
; i
++) {
1012 if (is_isoc(s
, USB_TOKEN_IN
, i
)) {
1013 usb_host_stop_n_free_iso(s
, USB_TOKEN_IN
, i
);
1015 if (is_isoc(s
, USB_TOKEN_OUT
, i
)) {
1016 usb_host_stop_n_free_iso(s
, USB_TOKEN_OUT
, i
);
1020 if (iface
>= USB_MAX_INTERFACES
) {
1021 p
->status
= USB_RET_STALL
;
1025 si
.interface
= iface
;
1026 si
.altsetting
= alt
;
1027 ret
= ioctl(s
->fd
, USBDEVFS_SETINTERFACE
, &si
);
1029 DPRINTF("husb: ctrl set iface %d altset %d ret %d errno %d\n",
1030 iface
, alt
, ret
, errno
);
1033 p
->status
= ctrl_error();
1037 s
->dev
.altsetting
[iface
] = alt
;
1038 usb_linux_update_endp_table(s
);
1041 static void usb_host_handle_control(USBDevice
*dev
, USBPacket
*p
,
1042 int request
, int value
, int index
, int length
, uint8_t *data
)
1044 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
1045 struct usbdevfs_urb
*urb
;
1050 * Process certain standard device requests.
1051 * These are infrequent and are processed synchronously.
1054 /* Note request is (bRequestType << 8) | bRequest */
1055 trace_usb_host_req_control(s
->bus_num
, s
->addr
, p
, request
, value
, index
);
1058 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
1059 usb_host_set_address(s
, value
);
1060 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1063 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
1064 usb_host_set_config(s
, value
& 0xff, p
);
1065 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1068 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
1069 usb_host_set_interface(s
, index
, value
, p
);
1070 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1073 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
1074 if (value
== 0) { /* clear halt */
1075 int pid
= (index
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
1076 ioctl(s
->fd
, USBDEVFS_CLEAR_HALT
, &index
);
1077 clear_halt(s
, pid
, index
& 0x0f);
1078 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, 0);
1083 /* The rest are asynchronous */
1084 if (length
> sizeof(dev
->data_buf
)) {
1085 fprintf(stderr
, "husb: ctrl buffer too small (%d > %zu)\n",
1086 length
, sizeof(dev
->data_buf
));
1087 p
->status
= USB_RET_STALL
;
1091 aurb
= async_alloc(s
);
1095 * Setup ctrl transfer.
1097 * s->ctrl is laid out such that data buffer immediately follows
1098 * 'req' struct which is exactly what usbdevfs expects.
1102 urb
->type
= USBDEVFS_URB_TYPE_CONTROL
;
1103 urb
->endpoint
= p
->ep
->nr
;
1105 urb
->buffer
= &dev
->setup_buf
;
1106 urb
->buffer_length
= length
+ 8;
1108 urb
->usercontext
= s
;
1110 trace_usb_host_urb_submit(s
->bus_num
, s
->addr
, aurb
,
1111 urb
->buffer_length
, aurb
->more
);
1112 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
1114 DPRINTF("husb: submit ctrl. len %u aurb %p\n", urb
->buffer_length
, aurb
);
1117 DPRINTF("husb: submit failed. errno %d\n", errno
);
1122 p
->status
= USB_RET_NAK
;
1126 p
->status
= USB_RET_STALL
;
1132 p
->status
= USB_RET_ASYNC
;
1135 static void usb_linux_update_endp_table(USBHostDevice
*s
)
1137 static const char *tname
[] = {
1138 [USB_ENDPOINT_XFER_CONTROL
] = "control",
1139 [USB_ENDPOINT_XFER_ISOC
] = "isoc",
1140 [USB_ENDPOINT_XFER_BULK
] = "bulk",
1141 [USB_ENDPOINT_XFER_INT
] = "int",
1143 uint8_t devep
, type
;
1146 unsigned int i
, configuration
= -1, interface
= -1, altsetting
= -1;
1147 struct endp_data
*epd
;
1149 bool active
= false;
1151 usb_ep_reset(&s
->dev
);
1153 for (i
= 0;; i
+= d
->bLength
) {
1154 if (i
+2 >= s
->descr_len
) {
1157 d
= (void *)(s
->descr
+ i
);
1158 if (d
->bLength
< 2) {
1159 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1160 "descriptor too short");
1163 if (i
+ d
->bLength
> s
->descr_len
) {
1164 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1165 "descriptor too long");
1168 switch (d
->bDescriptorType
) {
1170 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1171 "invalid descriptor type");
1174 if (d
->bLength
< 0x12) {
1175 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1176 "device descriptor too short");
1179 v
= (d
->u
.device
.idVendor_hi
<< 8) | d
->u
.device
.idVendor_lo
;
1180 p
= (d
->u
.device
.idProduct_hi
<< 8) | d
->u
.device
.idProduct_lo
;
1181 trace_usb_host_parse_device(s
->bus_num
, s
->addr
, v
, p
);
1184 if (d
->bLength
< 0x09) {
1185 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1186 "config descriptor too short");
1189 configuration
= d
->u
.config
.bConfigurationValue
;
1190 active
= (configuration
== s
->dev
.configuration
);
1191 trace_usb_host_parse_config(s
->bus_num
, s
->addr
,
1192 configuration
, active
);
1194 case USB_DT_INTERFACE
:
1195 if (d
->bLength
< 0x09) {
1196 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1197 "interface descriptor too short");
1200 interface
= d
->u
.interface
.bInterfaceNumber
;
1201 altsetting
= d
->u
.interface
.bAlternateSetting
;
1202 active
= (configuration
== s
->dev
.configuration
) &&
1203 (altsetting
== s
->dev
.altsetting
[interface
]);
1204 trace_usb_host_parse_interface(s
->bus_num
, s
->addr
,
1205 interface
, altsetting
, active
);
1207 case USB_DT_ENDPOINT
:
1208 if (d
->bLength
< 0x07) {
1209 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1210 "endpoint descriptor too short");
1213 devep
= d
->u
.endpoint
.bEndpointAddress
;
1214 pid
= (devep
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
1217 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1218 "invalid endpoint address");
1222 type
= d
->u
.endpoint
.bmAttributes
& 0x3;
1223 mps
= d
->u
.endpoint
.wMaxPacketSize_lo
|
1224 (d
->u
.endpoint
.wMaxPacketSize_hi
<< 8);
1225 trace_usb_host_parse_endpoint(s
->bus_num
, s
->addr
, ep
,
1226 (devep
& USB_DIR_IN
) ? "in" : "out",
1227 tname
[type
], active
);
1230 usb_ep_set_max_packet_size(&s
->dev
, pid
, ep
, mps
);
1231 assert(usb_ep_get_type(&s
->dev
, pid
, ep
) ==
1232 USB_ENDPOINT_XFER_INVALID
);
1233 usb_ep_set_type(&s
->dev
, pid
, ep
, type
);
1234 usb_ep_set_ifnum(&s
->dev
, pid
, ep
, interface
);
1235 if ((s
->options
& (1 << USB_HOST_OPT_PIPELINE
)) &&
1236 (type
== USB_ENDPOINT_XFER_BULK
) &&
1237 (pid
== USB_TOKEN_OUT
)) {
1238 usb_ep_set_pipeline(&s
->dev
, pid
, ep
, true);
1241 epd
= get_endp(s
, pid
, ep
);
1247 trace_usb_host_parse_unknown(s
->bus_num
, s
->addr
,
1248 d
->bLength
, d
->bDescriptorType
);
1255 * Check if we can safely redirect a usb2 device to a usb1 virtual controller,
1256 * this function assumes this is safe, if:
1257 * 1) There are no isoc endpoints
1258 * 2) There are no interrupt endpoints with a max_packet_size > 64
1259 * Note bulk endpoints with a max_packet_size > 64 in theory also are not
1260 * usb1 compatible, but in practice this seems to work fine.
1262 static int usb_linux_full_speed_compat(USBHostDevice
*dev
)
1267 * usb_linux_update_endp_table only registers info about ep in the current
1268 * interface altsettings, so we need to parse the descriptors again.
1270 for (i
= 0; (i
+ 5) < dev
->descr_len
; i
+= dev
->descr
[i
]) {
1271 if (dev
->descr
[i
+ 1] == USB_DT_ENDPOINT
) {
1272 switch (dev
->descr
[i
+ 3] & 0x3) {
1273 case 0x00: /* CONTROL */
1275 case 0x01: /* ISO */
1277 case 0x02: /* BULK */
1279 case 0x03: /* INTERRUPT */
1280 packet_size
= dev
->descr
[i
+ 4] + (dev
->descr
[i
+ 5] << 8);
1281 if (packet_size
> 64)
1290 static int usb_host_open(USBHostDevice
*dev
, int bus_num
,
1291 int addr
, const char *port
,
1292 const char *prod_name
, int speed
)
1296 trace_usb_host_open_started(bus_num
, addr
);
1298 if (dev
->fd
!= -1) {
1302 fd
= usb_host_open_device(bus_num
, addr
);
1306 DPRINTF("husb: opened %s\n", buf
);
1308 dev
->bus_num
= bus_num
;
1310 strcpy(dev
->port
, port
);
1313 /* read the device description */
1314 dev
->descr_len
= read(fd
, dev
->descr
, sizeof(dev
->descr
));
1315 if (dev
->descr_len
<= 0) {
1316 perror("husb: reading device data failed");
1323 printf("=== begin dumping device descriptor data ===\n");
1324 for (x
= 0; x
< dev
->descr_len
; x
++) {
1325 printf("%02x ", dev
->descr
[x
]);
1327 printf("\n=== end dumping device descriptor data ===\n");
1332 /* start unconfigured -- we'll wait for the guest to set a configuration */
1333 if (!usb_host_claim_interfaces(dev
, 0)) {
1337 usb_ep_init(&dev
->dev
);
1338 usb_linux_update_endp_table(dev
);
1341 struct usbdevfs_connectinfo ci
;
1343 ret
= ioctl(fd
, USBDEVFS_CONNECTINFO
, &ci
);
1345 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
1350 speed
= USB_SPEED_LOW
;
1352 speed
= USB_SPEED_HIGH
;
1355 dev
->dev
.speed
= speed
;
1356 dev
->dev
.speedmask
= (1 << speed
);
1357 if (dev
->dev
.speed
== USB_SPEED_HIGH
&& usb_linux_full_speed_compat(dev
)) {
1358 dev
->dev
.speedmask
|= USB_SPEED_MASK_FULL
;
1361 trace_usb_host_open_success(bus_num
, addr
);
1363 if (!prod_name
|| prod_name
[0] == '\0') {
1364 snprintf(dev
->dev
.product_desc
, sizeof(dev
->dev
.product_desc
),
1365 "host:%d.%d", bus_num
, addr
);
1367 pstrcpy(dev
->dev
.product_desc
, sizeof(dev
->dev
.product_desc
),
1371 ret
= usb_device_attach(&dev
->dev
);
1376 /* USB devio uses 'write' flag to check for async completions */
1377 qemu_set_fd_handler(dev
->fd
, NULL
, async_complete
, dev
);
1382 trace_usb_host_open_failure(bus_num
, addr
);
1383 if (dev
->fd
!= -1) {
1390 static int usb_host_close(USBHostDevice
*dev
)
1394 if (dev
->fd
== -1) {
1398 trace_usb_host_close(dev
->bus_num
, dev
->addr
);
1400 qemu_set_fd_handler(dev
->fd
, NULL
, NULL
, NULL
);
1402 for (i
= 1; i
<= USB_MAX_ENDPOINTS
; i
++) {
1403 if (is_isoc(dev
, USB_TOKEN_IN
, i
)) {
1404 usb_host_stop_n_free_iso(dev
, USB_TOKEN_IN
, i
);
1406 if (is_isoc(dev
, USB_TOKEN_OUT
, i
)) {
1407 usb_host_stop_n_free_iso(dev
, USB_TOKEN_OUT
, i
);
1410 async_complete(dev
);
1412 if (dev
->dev
.attached
) {
1413 usb_device_detach(&dev
->dev
);
1415 usb_host_do_reset(dev
);
1421 static void usb_host_exit_notifier(struct Notifier
*n
, void *data
)
1423 USBHostDevice
*s
= container_of(n
, USBHostDevice
, exit
);
1425 usb_host_release_port(s
);
1427 usb_host_do_reset(s
);;
1432 * This is *NOT* about restoring state. We have absolutely no idea
1433 * what state the host device is in at the moment and whenever it is
1434 * still present in the first place. Attemping to contine where we
1435 * left off is impossible.
1437 * What we are going to to to here is emulate a surprise removal of
1438 * the usb device passed through, then kick host scan so the device
1439 * will get re-attached (and re-initialized by the guest) in case it
1442 * As the device removal will change the state of other devices (usb
1443 * host controller, most likely interrupt controller too) we have to
1444 * wait with it until *all* vmstate is loaded. Thus post_load just
1445 * kicks a bottom half which then does the actual work.
1447 static void usb_host_post_load_bh(void *opaque
)
1449 USBHostDevice
*dev
= opaque
;
1451 if (dev
->fd
!= -1) {
1452 usb_host_close(dev
);
1454 if (dev
->dev
.attached
) {
1455 usb_device_detach(&dev
->dev
);
1457 usb_host_auto_check(NULL
);
1460 static int usb_host_post_load(void *opaque
, int version_id
)
1462 USBHostDevice
*dev
= opaque
;
1464 qemu_bh_schedule(dev
->bh
);
1468 static int usb_host_initfn(USBDevice
*dev
)
1470 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
1472 dev
->auto_attach
= 0;
1476 QTAILQ_INSERT_TAIL(&hostdevs
, s
, next
);
1477 s
->exit
.notify
= usb_host_exit_notifier
;
1478 qemu_add_exit_notifier(&s
->exit
);
1479 s
->bh
= qemu_bh_new(usb_host_post_load_bh
, s
);
1480 usb_host_auto_check(NULL
);
1482 if (s
->match
.bus_num
!= 0 && s
->match
.port
!= NULL
) {
1483 usb_host_claim_port(s
);
1485 add_boot_device_path(s
->bootindex
, &dev
->qdev
, NULL
);
1489 static const VMStateDescription vmstate_usb_host
= {
1492 .minimum_version_id
= 1,
1493 .post_load
= usb_host_post_load
,
1494 .fields
= (VMStateField
[]) {
1495 VMSTATE_USB_DEVICE(dev
, USBHostDevice
),
1496 VMSTATE_END_OF_LIST()
1500 static Property usb_host_dev_properties
[] = {
1501 DEFINE_PROP_UINT32("hostbus", USBHostDevice
, match
.bus_num
, 0),
1502 DEFINE_PROP_UINT32("hostaddr", USBHostDevice
, match
.addr
, 0),
1503 DEFINE_PROP_STRING("hostport", USBHostDevice
, match
.port
),
1504 DEFINE_PROP_HEX32("vendorid", USBHostDevice
, match
.vendor_id
, 0),
1505 DEFINE_PROP_HEX32("productid", USBHostDevice
, match
.product_id
, 0),
1506 DEFINE_PROP_UINT32("isobufs", USBHostDevice
, iso_urb_count
, 4),
1507 DEFINE_PROP_INT32("bootindex", USBHostDevice
, bootindex
, -1),
1508 DEFINE_PROP_BIT("pipeline", USBHostDevice
, options
,
1509 USB_HOST_OPT_PIPELINE
, true),
1510 DEFINE_PROP_END_OF_LIST(),
1513 static void usb_host_class_initfn(ObjectClass
*klass
, void *data
)
1515 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1516 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
1518 uc
->init
= usb_host_initfn
;
1519 uc
->product_desc
= "USB Host Device";
1520 uc
->cancel_packet
= usb_host_async_cancel
;
1521 uc
->handle_data
= usb_host_handle_data
;
1522 uc
->handle_control
= usb_host_handle_control
;
1523 uc
->handle_reset
= usb_host_handle_reset
;
1524 uc
->handle_destroy
= usb_host_handle_destroy
;
1525 dc
->vmsd
= &vmstate_usb_host
;
1526 dc
->props
= usb_host_dev_properties
;
1529 static TypeInfo usb_host_dev_info
= {
1531 .parent
= TYPE_USB_DEVICE
,
1532 .instance_size
= sizeof(USBHostDevice
),
1533 .class_init
= usb_host_class_initfn
,
1536 static void usb_host_register_types(void)
1538 type_register_static(&usb_host_dev_info
);
1539 usb_legacy_register("usb-host", "host", usb_host_device_open
);
1542 type_init(usb_host_register_types
)
1544 USBDevice
*usb_host_device_open(USBBus
*bus
, const char *devname
)
1546 struct USBAutoFilter filter
;
1550 dev
= usb_create(bus
, "usb-host");
1552 if (strstr(devname
, "auto:")) {
1553 if (parse_filter(devname
, &filter
) < 0) {
1557 if ((p
= strchr(devname
, '.'))) {
1558 filter
.bus_num
= strtoul(devname
, NULL
, 0);
1559 filter
.addr
= strtoul(p
+ 1, NULL
, 0);
1560 filter
.vendor_id
= 0;
1561 filter
.product_id
= 0;
1562 } else if ((p
= strchr(devname
, ':'))) {
1565 filter
.vendor_id
= strtoul(devname
, NULL
, 16);
1566 filter
.product_id
= strtoul(p
+ 1, NULL
, 16);
1572 qdev_prop_set_uint32(&dev
->qdev
, "hostbus", filter
.bus_num
);
1573 qdev_prop_set_uint32(&dev
->qdev
, "hostaddr", filter
.addr
);
1574 qdev_prop_set_uint32(&dev
->qdev
, "vendorid", filter
.vendor_id
);
1575 qdev_prop_set_uint32(&dev
->qdev
, "productid", filter
.product_id
);
1576 qdev_init_nofail(&dev
->qdev
);
1580 qdev_free(&dev
->qdev
);
1584 int usb_host_device_close(const char *devname
)
1587 char product_name
[PRODUCT_NAME_SZ
];
1591 if (strstr(devname
, "auto:")) {
1592 return usb_host_auto_del(devname
);
1594 if (usb_host_find_device(&bus_num
, &addr
, product_name
,
1595 sizeof(product_name
), devname
) < 0) {
1598 s
= hostdev_find(bus_num
, addr
);
1600 usb_device_delete_addr(s
->bus_num
, s
->dev
.addr
);
1609 * Read sys file-system device file
1611 * @line address of buffer to put file contents in
1612 * @line_size size of line
1613 * @device_file path to device file (printf format string)
1614 * @device_name device being opened (inserted into device_file)
1616 * @return 0 failed, 1 succeeded ('line' contains data)
1618 static int usb_host_read_file(char *line
, size_t line_size
,
1619 const char *device_file
, const char *device_name
)
1623 char filename
[PATH_MAX
];
1625 snprintf(filename
, PATH_MAX
, "/sys/bus/usb/devices/%s/%s", device_name
,
1627 f
= fopen(filename
, "r");
1629 ret
= fgets(line
, line_size
, f
) != NULL
;
1637 * Use /sys/bus/usb/devices/ directory to determine host's USB
1640 * This code is based on Robert Schiele's original patches posted to
1641 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1643 static int usb_host_scan(void *opaque
, USBScanFunc
*func
)
1647 int bus_num
, addr
, speed
, class_id
, product_id
, vendor_id
;
1649 char port
[MAX_PORTLEN
];
1650 char product_name
[512];
1653 dir
= opendir("/sys/bus/usb/devices");
1655 perror("husb: opendir /sys/bus/usb/devices");
1656 fprintf(stderr
, "husb: please make sure sysfs is mounted at /sys\n");
1660 while ((de
= readdir(dir
))) {
1661 if (de
->d_name
[0] != '.' && !strchr(de
->d_name
, ':')) {
1662 if (sscanf(de
->d_name
, "%d-%7[0-9.]", &bus_num
, port
) < 2) {
1666 if (!usb_host_read_file(line
, sizeof(line
), "devnum", de
->d_name
)) {
1669 if (sscanf(line
, "%d", &addr
) != 1) {
1672 if (!usb_host_read_file(line
, sizeof(line
), "bDeviceClass",
1676 if (sscanf(line
, "%x", &class_id
) != 1) {
1680 if (!usb_host_read_file(line
, sizeof(line
), "idVendor",
1684 if (sscanf(line
, "%x", &vendor_id
) != 1) {
1687 if (!usb_host_read_file(line
, sizeof(line
), "idProduct",
1691 if (sscanf(line
, "%x", &product_id
) != 1) {
1694 if (!usb_host_read_file(line
, sizeof(line
), "product",
1698 if (strlen(line
) > 0) {
1699 line
[strlen(line
) - 1] = '\0';
1701 pstrcpy(product_name
, sizeof(product_name
), line
);
1704 if (!usb_host_read_file(line
, sizeof(line
), "speed", de
->d_name
)) {
1707 if (!strcmp(line
, "5000\n")) {
1708 speed
= USB_SPEED_SUPER
;
1709 } else if (!strcmp(line
, "480\n")) {
1710 speed
= USB_SPEED_HIGH
;
1711 } else if (!strcmp(line
, "1.5\n")) {
1712 speed
= USB_SPEED_LOW
;
1714 speed
= USB_SPEED_FULL
;
1717 ret
= func(opaque
, bus_num
, addr
, port
, class_id
, vendor_id
,
1718 product_id
, product_name
, speed
);
1731 static QEMUTimer
*usb_auto_timer
;
1732 static VMChangeStateEntry
*usb_vmstate
;
1734 static int usb_host_auto_scan(void *opaque
, int bus_num
,
1735 int addr
, const char *port
,
1736 int class_id
, int vendor_id
, int product_id
,
1737 const char *product_name
, int speed
)
1739 struct USBAutoFilter
*f
;
1740 struct USBHostDevice
*s
;
1746 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1749 if (f
->bus_num
> 0 && f
->bus_num
!= bus_num
) {
1752 if (f
->addr
> 0 && f
->addr
!= addr
) {
1755 if (f
->port
!= NULL
&& (port
== NULL
|| strcmp(f
->port
, port
) != 0)) {
1759 if (f
->vendor_id
> 0 && f
->vendor_id
!= vendor_id
) {
1763 if (f
->product_id
> 0 && f
->product_id
!= product_id
) {
1766 /* We got a match */
1768 if (s
->errcount
>= 3) {
1772 /* Already attached ? */
1776 DPRINTF("husb: auto open: bus_num %d addr %d\n", bus_num
, addr
);
1778 if (usb_host_open(s
, bus_num
, addr
, port
, product_name
, speed
) < 0) {
1787 static void usb_host_vm_state(void *unused
, int running
, RunState state
)
1790 usb_host_auto_check(unused
);
1794 static void usb_host_auto_check(void *unused
)
1796 struct USBHostDevice
*s
;
1797 int unconnected
= 0;
1799 if (runstate_is_running()) {
1800 usb_host_scan(NULL
, usb_host_auto_scan
);
1802 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1812 if (unconnected
== 0) {
1813 /* nothing to watch */
1814 if (usb_auto_timer
) {
1815 qemu_del_timer(usb_auto_timer
);
1816 trace_usb_host_auto_scan_disabled();
1823 usb_vmstate
= qemu_add_vm_change_state_handler(usb_host_vm_state
, NULL
);
1825 if (!usb_auto_timer
) {
1826 usb_auto_timer
= qemu_new_timer_ms(rt_clock
, usb_host_auto_check
, NULL
);
1827 if (!usb_auto_timer
) {
1830 trace_usb_host_auto_scan_enabled();
1832 qemu_mod_timer(usb_auto_timer
, qemu_get_clock_ms(rt_clock
) + 2000);
1836 * Autoconnect filter
1838 * auto:bus:dev[:vid:pid]
1839 * auto:bus.dev[:vid:pid]
1841 * bus - bus number (dec, * means any)
1842 * dev - device number (dec, * means any)
1843 * vid - vendor id (hex, * means any)
1844 * pid - product id (hex, * means any)
1846 * See 'lsusb' output.
1848 static int parse_filter(const char *spec
, struct USBAutoFilter
*f
)
1850 enum { BUS
, DEV
, VID
, PID
, DONE
};
1851 const char *p
= spec
;
1859 for (i
= BUS
; i
< DONE
; i
++) {
1860 p
= strpbrk(p
, ":.");
1870 case BUS
: f
->bus_num
= strtol(p
, NULL
, 10); break;
1871 case DEV
: f
->addr
= strtol(p
, NULL
, 10); break;
1872 case VID
: f
->vendor_id
= strtol(p
, NULL
, 16); break;
1873 case PID
: f
->product_id
= strtol(p
, NULL
, 16); break;
1878 fprintf(stderr
, "husb: invalid auto filter spec %s\n", spec
);
1885 /**********************/
1886 /* USB host device info */
1888 struct usb_class_info
{
1890 const char *class_name
;
1893 static const struct usb_class_info usb_class_info
[] = {
1894 { USB_CLASS_AUDIO
, "Audio"},
1895 { USB_CLASS_COMM
, "Communication"},
1896 { USB_CLASS_HID
, "HID"},
1897 { USB_CLASS_HUB
, "Hub" },
1898 { USB_CLASS_PHYSICAL
, "Physical" },
1899 { USB_CLASS_PRINTER
, "Printer" },
1900 { USB_CLASS_MASS_STORAGE
, "Storage" },
1901 { USB_CLASS_CDC_DATA
, "Data" },
1902 { USB_CLASS_APP_SPEC
, "Application Specific" },
1903 { USB_CLASS_VENDOR_SPEC
, "Vendor Specific" },
1904 { USB_CLASS_STILL_IMAGE
, "Still Image" },
1905 { USB_CLASS_CSCID
, "Smart Card" },
1906 { USB_CLASS_CONTENT_SEC
, "Content Security" },
1910 static const char *usb_class_str(uint8_t class)
1912 const struct usb_class_info
*p
;
1913 for(p
= usb_class_info
; p
->class != -1; p
++) {
1914 if (p
->class == class) {
1918 return p
->class_name
;
1921 static void usb_info_device(Monitor
*mon
, int bus_num
,
1922 int addr
, const char *port
,
1923 int class_id
, int vendor_id
, int product_id
,
1924 const char *product_name
,
1927 const char *class_str
, *speed_str
;
1933 case USB_SPEED_FULL
:
1936 case USB_SPEED_HIGH
:
1939 case USB_SPEED_SUPER
:
1947 monitor_printf(mon
, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
1948 bus_num
, addr
, port
, speed_str
);
1949 class_str
= usb_class_str(class_id
);
1951 monitor_printf(mon
, " %s:", class_str
);
1953 monitor_printf(mon
, " Class %02x:", class_id
);
1955 monitor_printf(mon
, " USB device %04x:%04x", vendor_id
, product_id
);
1956 if (product_name
[0] != '\0') {
1957 monitor_printf(mon
, ", %s", product_name
);
1959 monitor_printf(mon
, "\n");
1962 static int usb_host_info_device(void *opaque
, int bus_num
, int addr
,
1963 const char *path
, int class_id
,
1964 int vendor_id
, int product_id
,
1965 const char *product_name
,
1968 Monitor
*mon
= opaque
;
1970 usb_info_device(mon
, bus_num
, addr
, path
, class_id
, vendor_id
, product_id
,
1971 product_name
, speed
);
1975 static void dec2str(int val
, char *str
, size_t size
)
1978 snprintf(str
, size
, "*");
1980 snprintf(str
, size
, "%d", val
);
1984 static void hex2str(int val
, char *str
, size_t size
)
1987 snprintf(str
, size
, "*");
1989 snprintf(str
, size
, "%04x", val
);
1993 void usb_host_info(Monitor
*mon
)
1995 struct USBAutoFilter
*f
;
1996 struct USBHostDevice
*s
;
1998 usb_host_scan(mon
, usb_host_info_device
);
2000 if (QTAILQ_EMPTY(&hostdevs
)) {
2004 monitor_printf(mon
, " Auto filters:\n");
2005 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
2006 char bus
[10], addr
[10], vid
[10], pid
[10];
2008 dec2str(f
->bus_num
, bus
, sizeof(bus
));
2009 dec2str(f
->addr
, addr
, sizeof(addr
));
2010 hex2str(f
->vendor_id
, vid
, sizeof(vid
));
2011 hex2str(f
->product_id
, pid
, sizeof(pid
));
2012 monitor_printf(mon
, " Bus %s, Addr %s, Port %s, ID %s:%s\n",
2013 bus
, addr
, f
->port
? f
->port
: "*", vid
, pid
);