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"
35 #include "monitor/monitor.h"
36 #include "sysemu/sysemu.h"
40 #include <sys/ioctl.h>
42 #include <linux/usbdevice_fs.h>
43 #include <linux/version.h>
45 #include "hw/usb/desc.h"
46 #include "hw/usb/host.h"
48 /* We redefine it to avoid version problems */
49 struct usb_ctrltransfer
{
59 typedef int USBScanFunc(void *opaque
, int bus_num
, int addr
, const char *port
,
60 int class_id
, int vendor_id
, int product_id
,
61 const char *product_name
, int speed
);
66 #define DPRINTF printf
71 #define PRODUCT_NAME_SZ 32
72 #define MAX_PORTLEN 16
74 /* endpoint association data */
75 #define ISO_FRAME_DESC_PER_URB 32
77 /* devio.c limits single requests to 16k */
78 #define MAX_USBFS_BUFFER_SIZE 16384
80 typedef struct AsyncURB AsyncURB
;
91 enum USBHostDeviceOptions
{
92 USB_HOST_OPT_PIPELINE
,
95 typedef struct USBHostDevice
{
104 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
;
121 QTAILQ_ENTRY(USBHostDevice
) next
;
124 static QTAILQ_HEAD(, USBHostDevice
) hostdevs
= QTAILQ_HEAD_INITIALIZER(hostdevs
);
126 static int usb_host_close(USBHostDevice
*dev
);
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 void 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_iso_stop(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_iso_start(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 /* indicates possible latency issues */
315 trace_usb_host_iso_many_urbs(s
->bus_num
, s
->addr
, urbs
);
319 if (errno
== ENODEV
) {
321 trace_usb_host_disconnect(s
->bus_num
, s
->addr
);
327 perror("USBDEVFS_REAPURBNDELAY");
331 DPRINTF("husb: async completed. aurb %p status %d alen %d\n",
332 aurb
, aurb
->urb
.status
, aurb
->urb
.actual_length
);
334 /* If this is a buffered iso urb mark it as complete and don't do
335 anything else (it is handled further in usb_host_handle_iso_data) */
336 if (aurb
->iso_frame_idx
== -1) {
338 int pid
= (aurb
->urb
.endpoint
& USB_DIR_IN
) ?
339 USB_TOKEN_IN
: USB_TOKEN_OUT
;
340 int ep
= aurb
->urb
.endpoint
& 0xf;
341 if (aurb
->urb
.status
== -EPIPE
) {
342 set_halt(s
, pid
, ep
);
344 aurb
->iso_frame_idx
= 0;
346 inflight
= change_iso_inflight(s
, pid
, ep
, -1);
347 if (inflight
== 0 && is_iso_started(s
, pid
, ep
)) {
348 /* can be latency issues, or simply end of stream */
349 trace_usb_host_iso_out_of_bufs(s
->bus_num
, s
->addr
, ep
);
355 trace_usb_host_urb_complete(s
->bus_num
, s
->addr
, aurb
, aurb
->urb
.status
,
356 aurb
->urb
.actual_length
, aurb
->more
);
359 switch (aurb
->urb
.status
) {
361 p
->actual_length
+= aurb
->urb
.actual_length
;
363 /* Clear previous ASYNC status */
364 p
->status
= USB_RET_SUCCESS
;
369 set_halt(s
, p
->pid
, p
->ep
->nr
);
370 p
->status
= USB_RET_STALL
;
374 p
->status
= USB_RET_BABBLE
;
378 p
->status
= USB_RET_IOERROR
;
382 if (aurb
->urb
.type
== USBDEVFS_URB_TYPE_CONTROL
) {
383 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
384 p
->status
, aurb
->urb
.actual_length
);
385 usb_generic_async_ctrl_complete(&s
->dev
, p
);
386 } else if (!aurb
->more
) {
387 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
388 p
->status
, aurb
->urb
.actual_length
);
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 void urb_status_to_usb_ret(int status
, USBPacket
*p
)
738 p
->status
= USB_RET_STALL
;
741 p
->status
= USB_RET_BABBLE
;
744 p
->status
= USB_RET_IOERROR
;
748 static void usb_host_handle_iso_data(USBHostDevice
*s
, USBPacket
*p
, int in
)
751 int i
, j
, max_packet_size
, offset
, len
;
754 max_packet_size
= p
->ep
->max_packet_size
;
755 if (max_packet_size
== 0) {
756 p
->status
= USB_RET_NAK
;
760 aurb
= get_iso_urb(s
, p
->pid
, p
->ep
->nr
);
762 aurb
= usb_host_alloc_iso(s
, p
->pid
, p
->ep
->nr
);
765 i
= get_iso_urb_idx(s
, p
->pid
, p
->ep
->nr
);
766 j
= aurb
[i
].iso_frame_idx
;
767 if (j
>= 0 && j
< ISO_FRAME_DESC_PER_URB
) {
769 /* Check urb status */
770 if (aurb
[i
].urb
.status
) {
771 urb_status_to_usb_ret(aurb
[i
].urb
.status
, p
);
772 /* Move to the next urb */
773 aurb
[i
].iso_frame_idx
= ISO_FRAME_DESC_PER_URB
- 1;
774 /* Check frame status */
775 } else if (aurb
[i
].urb
.iso_frame_desc
[j
].status
) {
776 urb_status_to_usb_ret(aurb
[i
].urb
.iso_frame_desc
[j
].status
, p
);
777 /* Check the frame fits */
778 } else if (aurb
[i
].urb
.iso_frame_desc
[j
].actual_length
780 printf("husb: received iso data is larger then packet\n");
781 p
->status
= USB_RET_BABBLE
;
782 /* All good copy data over */
784 len
= aurb
[i
].urb
.iso_frame_desc
[j
].actual_length
;
785 buf
= aurb
[i
].urb
.buffer
+
786 j
* aurb
[i
].urb
.iso_frame_desc
[0].length
;
787 usb_packet_copy(p
, buf
, len
);
791 offset
= (j
== 0) ? 0 : get_iso_buffer_used(s
, p
->pid
, p
->ep
->nr
);
793 /* Check the frame fits */
794 if (len
> max_packet_size
) {
795 printf("husb: send iso data is larger then max packet size\n");
796 p
->status
= USB_RET_NAK
;
800 /* All good copy data over */
801 usb_packet_copy(p
, aurb
[i
].urb
.buffer
+ offset
, len
);
802 aurb
[i
].urb
.iso_frame_desc
[j
].length
= len
;
804 set_iso_buffer_used(s
, p
->pid
, p
->ep
->nr
, offset
);
806 /* Start the stream once we have buffered enough data */
807 if (!is_iso_started(s
, p
->pid
, p
->ep
->nr
) && i
== 1 && j
== 8) {
808 set_iso_started(s
, p
->pid
, p
->ep
->nr
);
811 aurb
[i
].iso_frame_idx
++;
812 if (aurb
[i
].iso_frame_idx
== ISO_FRAME_DESC_PER_URB
) {
813 i
= (i
+ 1) % s
->iso_urb_count
;
814 set_iso_urb_idx(s
, p
->pid
, p
->ep
->nr
, i
);
818 set_iso_started(s
, p
->pid
, p
->ep
->nr
);
820 DPRINTF("hubs: iso out error no free buffer, dropping packet\n");
824 if (is_iso_started(s
, p
->pid
, p
->ep
->nr
)) {
825 /* (Re)-submit all fully consumed / filled urbs */
826 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
827 if (aurb
[i
].iso_frame_idx
== ISO_FRAME_DESC_PER_URB
) {
828 if (ioctl(s
->fd
, USBDEVFS_SUBMITURB
, &aurb
[i
]) < 0) {
829 perror("USBDEVFS_SUBMITURB");
830 if (!in
|| p
->status
== USB_RET_SUCCESS
) {
833 p
->status
= USB_RET_NAK
;
837 p
->status
= USB_RET_STALL
;
842 aurb
[i
].iso_frame_idx
= -1;
843 change_iso_inflight(s
, p
->pid
, p
->ep
->nr
, 1);
849 static void usb_host_handle_data(USBDevice
*dev
, USBPacket
*p
)
851 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
852 struct usbdevfs_urb
*urb
;
854 int ret
, rem
, prem
, v
;
858 trace_usb_host_req_data(s
->bus_num
, s
->addr
, p
,
859 p
->pid
== USB_TOKEN_IN
,
860 p
->ep
->nr
, p
->iov
.size
);
862 if (!is_valid(s
, p
->pid
, p
->ep
->nr
)) {
863 p
->status
= USB_RET_NAK
;
864 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
865 p
->status
, p
->actual_length
);
869 if (p
->pid
== USB_TOKEN_IN
) {
870 ep
= p
->ep
->nr
| 0x80;
875 if (is_halted(s
, p
->pid
, p
->ep
->nr
)) {
876 unsigned int arg
= ep
;
877 ret
= ioctl(s
->fd
, USBDEVFS_CLEAR_HALT
, &arg
);
879 perror("USBDEVFS_CLEAR_HALT");
880 p
->status
= USB_RET_NAK
;
881 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
882 p
->status
, p
->actual_length
);
885 clear_halt(s
, p
->pid
, p
->ep
->nr
);
888 if (is_isoc(s
, p
->pid
, p
->ep
->nr
)) {
889 usb_host_handle_iso_data(s
, p
, p
->pid
== USB_TOKEN_IN
);
898 if (prem
== 0 && rem
> 0) {
899 assert(v
< p
->iov
.niov
);
900 prem
= p
->iov
.iov
[v
].iov_len
;
901 pbuf
= p
->iov
.iov
[v
].iov_base
;
905 aurb
= async_alloc(s
);
910 urb
->type
= usb_host_usbfs_type(s
, p
);
911 urb
->usercontext
= s
;
913 urb
->buffer_length
= prem
;
915 if (urb
->buffer_length
> MAX_USBFS_BUFFER_SIZE
) {
916 urb
->buffer_length
= MAX_USBFS_BUFFER_SIZE
;
918 pbuf
+= urb
->buffer_length
;
919 prem
-= urb
->buffer_length
;
920 rem
-= urb
->buffer_length
;
925 trace_usb_host_urb_submit(s
->bus_num
, s
->addr
, aurb
,
926 urb
->buffer_length
, aurb
->more
);
927 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
929 DPRINTF("husb: data submit: ep 0x%x, len %u, more %d, packet %p, aurb %p\n",
930 urb
->endpoint
, urb
->buffer_length
, aurb
->more
, p
, aurb
);
933 perror("USBDEVFS_SUBMITURB");
938 p
->status
= USB_RET_NAK
;
939 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
940 p
->status
, p
->actual_length
);
944 p
->status
= USB_RET_STALL
;
945 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
,
946 p
->status
, p
->actual_length
);
952 p
->status
= USB_RET_ASYNC
;
955 static int ctrl_error(void)
957 if (errno
== ETIMEDOUT
) {
960 return USB_RET_STALL
;
964 static void usb_host_set_address(USBHostDevice
*s
, int addr
)
966 trace_usb_host_set_address(s
->bus_num
, s
->addr
, addr
);
970 static void usb_host_set_config(USBHostDevice
*s
, int config
, USBPacket
*p
)
974 trace_usb_host_set_config(s
->bus_num
, s
->addr
, config
);
976 usb_host_release_interfaces(s
);
979 ret
= ioctl(s
->fd
, USBDEVFS_SETCONFIGURATION
, &config
);
981 DPRINTF("husb: ctrl set config %d ret %d errno %d\n", config
, ret
, errno
);
983 if (ret
< 0 && errno
== EBUSY
&& first
) {
984 /* happens if usb device is in use by host drivers */
985 int count
= usb_linux_get_num_interfaces(s
);
987 DPRINTF("husb: busy -> disconnecting %d interfaces\n", count
);
988 usb_host_disconnect_ifaces(s
, count
);
995 p
->status
= ctrl_error();
998 usb_host_claim_interfaces(s
, config
);
999 usb_linux_update_endp_table(s
);
1002 static void usb_host_set_interface(USBHostDevice
*s
, int iface
, int alt
,
1005 struct usbdevfs_setinterface si
;
1008 trace_usb_host_set_interface(s
->bus_num
, s
->addr
, iface
, alt
);
1010 for (i
= 1; i
<= USB_MAX_ENDPOINTS
; i
++) {
1011 if (is_isoc(s
, USB_TOKEN_IN
, i
)) {
1012 usb_host_stop_n_free_iso(s
, USB_TOKEN_IN
, i
);
1014 if (is_isoc(s
, USB_TOKEN_OUT
, i
)) {
1015 usb_host_stop_n_free_iso(s
, USB_TOKEN_OUT
, i
);
1019 if (iface
>= USB_MAX_INTERFACES
) {
1020 p
->status
= USB_RET_STALL
;
1024 si
.interface
= iface
;
1025 si
.altsetting
= alt
;
1026 ret
= ioctl(s
->fd
, USBDEVFS_SETINTERFACE
, &si
);
1028 DPRINTF("husb: ctrl set iface %d altset %d ret %d errno %d\n",
1029 iface
, alt
, ret
, errno
);
1032 p
->status
= ctrl_error();
1036 s
->dev
.altsetting
[iface
] = alt
;
1037 usb_linux_update_endp_table(s
);
1040 static void usb_host_handle_control(USBDevice
*dev
, USBPacket
*p
,
1041 int request
, int value
, int index
, int length
, uint8_t *data
)
1043 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
1044 struct usbdevfs_urb
*urb
;
1049 * Process certain standard device requests.
1050 * These are infrequent and are processed synchronously.
1053 /* Note request is (bRequestType << 8) | bRequest */
1054 trace_usb_host_req_control(s
->bus_num
, s
->addr
, p
, request
, value
, index
);
1057 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
1058 usb_host_set_address(s
, value
);
1059 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1062 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
1063 usb_host_set_config(s
, value
& 0xff, p
);
1064 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1067 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
1068 usb_host_set_interface(s
, index
, value
, p
);
1069 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, p
->status
);
1072 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
1073 if (value
== 0) { /* clear halt */
1074 int pid
= (index
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
1075 ioctl(s
->fd
, USBDEVFS_CLEAR_HALT
, &index
);
1076 clear_halt(s
, pid
, index
& 0x0f);
1077 trace_usb_host_req_emulated(s
->bus_num
, s
->addr
, p
, 0);
1082 /* The rest are asynchronous */
1083 if (length
> sizeof(dev
->data_buf
)) {
1084 fprintf(stderr
, "husb: ctrl buffer too small (%d > %zu)\n",
1085 length
, sizeof(dev
->data_buf
));
1086 p
->status
= USB_RET_STALL
;
1090 aurb
= async_alloc(s
);
1094 * Setup ctrl transfer.
1096 * s->ctrl is laid out such that data buffer immediately follows
1097 * 'req' struct which is exactly what usbdevfs expects.
1101 urb
->type
= USBDEVFS_URB_TYPE_CONTROL
;
1102 urb
->endpoint
= p
->ep
->nr
;
1104 urb
->buffer
= &dev
->setup_buf
;
1105 urb
->buffer_length
= length
+ 8;
1107 urb
->usercontext
= s
;
1109 trace_usb_host_urb_submit(s
->bus_num
, s
->addr
, aurb
,
1110 urb
->buffer_length
, aurb
->more
);
1111 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
1113 DPRINTF("husb: submit ctrl. len %u aurb %p\n", urb
->buffer_length
, aurb
);
1116 DPRINTF("husb: submit failed. errno %d\n", errno
);
1121 p
->status
= USB_RET_NAK
;
1125 p
->status
= USB_RET_STALL
;
1131 p
->status
= USB_RET_ASYNC
;
1134 static void usb_linux_update_endp_table(USBHostDevice
*s
)
1136 static const char *tname
[] = {
1137 [USB_ENDPOINT_XFER_CONTROL
] = "control",
1138 [USB_ENDPOINT_XFER_ISOC
] = "isoc",
1139 [USB_ENDPOINT_XFER_BULK
] = "bulk",
1140 [USB_ENDPOINT_XFER_INT
] = "int",
1142 uint8_t devep
, type
;
1145 unsigned int i
, configuration
= -1, interface
= -1, altsetting
= -1;
1146 struct endp_data
*epd
;
1148 bool active
= false;
1150 usb_ep_reset(&s
->dev
);
1152 for (i
= 0;; i
+= d
->bLength
) {
1153 if (i
+2 >= s
->descr_len
) {
1156 d
= (void *)(s
->descr
+ i
);
1157 if (d
->bLength
< 2) {
1158 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1159 "descriptor too short");
1162 if (i
+ d
->bLength
> s
->descr_len
) {
1163 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1164 "descriptor too long");
1167 switch (d
->bDescriptorType
) {
1169 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1170 "invalid descriptor type");
1173 if (d
->bLength
< 0x12) {
1174 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1175 "device descriptor too short");
1178 v
= (d
->u
.device
.idVendor_hi
<< 8) | d
->u
.device
.idVendor_lo
;
1179 p
= (d
->u
.device
.idProduct_hi
<< 8) | d
->u
.device
.idProduct_lo
;
1180 trace_usb_host_parse_device(s
->bus_num
, s
->addr
, v
, p
);
1183 if (d
->bLength
< 0x09) {
1184 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1185 "config descriptor too short");
1188 configuration
= d
->u
.config
.bConfigurationValue
;
1189 active
= (configuration
== s
->dev
.configuration
);
1190 trace_usb_host_parse_config(s
->bus_num
, s
->addr
,
1191 configuration
, active
);
1193 case USB_DT_INTERFACE
:
1194 if (d
->bLength
< 0x09) {
1195 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1196 "interface descriptor too short");
1199 interface
= d
->u
.interface
.bInterfaceNumber
;
1200 altsetting
= d
->u
.interface
.bAlternateSetting
;
1201 active
= (configuration
== s
->dev
.configuration
) &&
1202 (altsetting
== s
->dev
.altsetting
[interface
]);
1203 trace_usb_host_parse_interface(s
->bus_num
, s
->addr
,
1204 interface
, altsetting
, active
);
1206 case USB_DT_ENDPOINT
:
1207 if (d
->bLength
< 0x07) {
1208 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1209 "endpoint descriptor too short");
1212 devep
= d
->u
.endpoint
.bEndpointAddress
;
1213 pid
= (devep
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
1216 trace_usb_host_parse_error(s
->bus_num
, s
->addr
,
1217 "invalid endpoint address");
1221 type
= d
->u
.endpoint
.bmAttributes
& 0x3;
1222 mps
= d
->u
.endpoint
.wMaxPacketSize_lo
|
1223 (d
->u
.endpoint
.wMaxPacketSize_hi
<< 8);
1224 trace_usb_host_parse_endpoint(s
->bus_num
, s
->addr
, ep
,
1225 (devep
& USB_DIR_IN
) ? "in" : "out",
1226 tname
[type
], active
);
1229 usb_ep_set_max_packet_size(&s
->dev
, pid
, ep
, mps
);
1230 assert(usb_ep_get_type(&s
->dev
, pid
, ep
) ==
1231 USB_ENDPOINT_XFER_INVALID
);
1232 usb_ep_set_type(&s
->dev
, pid
, ep
, type
);
1233 usb_ep_set_ifnum(&s
->dev
, pid
, ep
, interface
);
1234 if ((s
->options
& (1 << USB_HOST_OPT_PIPELINE
)) &&
1235 (type
== USB_ENDPOINT_XFER_BULK
) &&
1236 (pid
== USB_TOKEN_OUT
)) {
1237 usb_ep_set_pipeline(&s
->dev
, pid
, ep
, true);
1240 epd
= get_endp(s
, pid
, ep
);
1246 trace_usb_host_parse_unknown(s
->bus_num
, s
->addr
,
1247 d
->bLength
, d
->bDescriptorType
);
1254 * Check if we can safely redirect a usb2 device to a usb1 virtual controller,
1255 * this function assumes this is safe, if:
1256 * 1) There are no isoc endpoints
1257 * 2) There are no interrupt endpoints with a max_packet_size > 64
1258 * Note bulk endpoints with a max_packet_size > 64 in theory also are not
1259 * usb1 compatible, but in practice this seems to work fine.
1261 static int usb_linux_full_speed_compat(USBHostDevice
*dev
)
1266 * usb_linux_update_endp_table only registers info about ep in the current
1267 * interface altsettings, so we need to parse the descriptors again.
1269 for (i
= 0; (i
+ 5) < dev
->descr_len
; i
+= dev
->descr
[i
]) {
1270 if (dev
->descr
[i
+ 1] == USB_DT_ENDPOINT
) {
1271 switch (dev
->descr
[i
+ 3] & 0x3) {
1272 case 0x00: /* CONTROL */
1274 case 0x01: /* ISO */
1276 case 0x02: /* BULK */
1278 case 0x03: /* INTERRUPT */
1279 packet_size
= dev
->descr
[i
+ 4] + (dev
->descr
[i
+ 5] << 8);
1280 if (packet_size
> 64)
1289 static int usb_host_open(USBHostDevice
*dev
, int bus_num
,
1290 int addr
, const char *port
,
1291 const char *prod_name
, int speed
)
1295 trace_usb_host_open_started(bus_num
, addr
);
1297 if (dev
->fd
!= -1) {
1301 fd
= usb_host_open_device(bus_num
, addr
);
1305 DPRINTF("husb: opened %s\n", buf
);
1307 dev
->bus_num
= bus_num
;
1309 pstrcpy(dev
->port
, sizeof(dev
->port
), port
);
1312 /* read the device description */
1313 dev
->descr_len
= read(fd
, dev
->descr
, sizeof(dev
->descr
));
1314 if (dev
->descr_len
<= 0) {
1315 perror("husb: reading device data failed");
1322 printf("=== begin dumping device descriptor data ===\n");
1323 for (x
= 0; x
< dev
->descr_len
; x
++) {
1324 printf("%02x ", dev
->descr
[x
]);
1326 printf("\n=== end dumping device descriptor data ===\n");
1331 /* start unconfigured -- we'll wait for the guest to set a configuration */
1332 if (!usb_host_claim_interfaces(dev
, 0)) {
1336 usb_ep_init(&dev
->dev
);
1337 usb_linux_update_endp_table(dev
);
1340 struct usbdevfs_connectinfo ci
;
1342 ret
= ioctl(fd
, USBDEVFS_CONNECTINFO
, &ci
);
1344 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
1349 speed
= USB_SPEED_LOW
;
1351 speed
= USB_SPEED_HIGH
;
1354 dev
->dev
.speed
= speed
;
1355 dev
->dev
.speedmask
= (1 << speed
);
1356 if (dev
->dev
.speed
== USB_SPEED_HIGH
&& usb_linux_full_speed_compat(dev
)) {
1357 dev
->dev
.speedmask
|= USB_SPEED_MASK_FULL
;
1360 trace_usb_host_open_success(bus_num
, addr
);
1362 if (!prod_name
|| prod_name
[0] == '\0') {
1363 snprintf(dev
->dev
.product_desc
, sizeof(dev
->dev
.product_desc
),
1364 "host:%d.%d", bus_num
, addr
);
1366 pstrcpy(dev
->dev
.product_desc
, sizeof(dev
->dev
.product_desc
),
1370 ret
= usb_device_attach(&dev
->dev
);
1375 /* USB devio uses 'write' flag to check for async completions */
1376 qemu_set_fd_handler(dev
->fd
, NULL
, async_complete
, dev
);
1381 trace_usb_host_open_failure(bus_num
, addr
);
1382 if (dev
->fd
!= -1) {
1389 static int usb_host_close(USBHostDevice
*dev
)
1393 if (dev
->fd
== -1) {
1397 trace_usb_host_close(dev
->bus_num
, dev
->addr
);
1399 qemu_set_fd_handler(dev
->fd
, NULL
, NULL
, NULL
);
1401 for (i
= 1; i
<= USB_MAX_ENDPOINTS
; i
++) {
1402 if (is_isoc(dev
, USB_TOKEN_IN
, i
)) {
1403 usb_host_stop_n_free_iso(dev
, USB_TOKEN_IN
, i
);
1405 if (is_isoc(dev
, USB_TOKEN_OUT
, i
)) {
1406 usb_host_stop_n_free_iso(dev
, USB_TOKEN_OUT
, i
);
1409 async_complete(dev
);
1411 if (dev
->dev
.attached
) {
1412 usb_device_detach(&dev
->dev
);
1414 usb_host_do_reset(dev
);
1420 static void usb_host_exit_notifier(struct Notifier
*n
, void *data
)
1422 USBHostDevice
*s
= container_of(n
, USBHostDevice
, exit
);
1424 usb_host_release_port(s
);
1426 usb_host_do_reset(s
);;
1431 * This is *NOT* about restoring state. We have absolutely no idea
1432 * what state the host device is in at the moment and whenever it is
1433 * still present in the first place. Attemping to contine where we
1434 * left off is impossible.
1436 * What we are going to to to here is emulate a surprise removal of
1437 * the usb device passed through, then kick host scan so the device
1438 * will get re-attached (and re-initialized by the guest) in case it
1441 * As the device removal will change the state of other devices (usb
1442 * host controller, most likely interrupt controller too) we have to
1443 * wait with it until *all* vmstate is loaded. Thus post_load just
1444 * kicks a bottom half which then does the actual work.
1446 static void usb_host_post_load_bh(void *opaque
)
1448 USBHostDevice
*dev
= opaque
;
1450 if (dev
->fd
!= -1) {
1451 usb_host_close(dev
);
1453 if (dev
->dev
.attached
) {
1454 usb_device_detach(&dev
->dev
);
1456 usb_host_auto_check(NULL
);
1459 static int usb_host_post_load(void *opaque
, int version_id
)
1461 USBHostDevice
*dev
= opaque
;
1463 qemu_bh_schedule(dev
->bh
);
1467 static int usb_host_initfn(USBDevice
*dev
)
1469 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
1471 dev
->flags
|= (1 << USB_DEV_FLAG_IS_HOST
);
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 const 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
);
1541 type_init(usb_host_register_types
)
1544 * Read sys file-system device file
1546 * @line address of buffer to put file contents in
1547 * @line_size size of line
1548 * @device_file path to device file (printf format string)
1549 * @device_name device being opened (inserted into device_file)
1551 * @return 0 failed, 1 succeeded ('line' contains data)
1553 static int usb_host_read_file(char *line
, size_t line_size
,
1554 const char *device_file
, const char *device_name
)
1558 char filename
[PATH_MAX
];
1560 snprintf(filename
, PATH_MAX
, "/sys/bus/usb/devices/%s/%s", device_name
,
1562 f
= fopen(filename
, "r");
1564 ret
= fgets(line
, line_size
, f
) != NULL
;
1572 * Use /sys/bus/usb/devices/ directory to determine host's USB
1575 * This code is based on Robert Schiele's original patches posted to
1576 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1578 static int usb_host_scan(void *opaque
, USBScanFunc
*func
)
1582 int bus_num
, addr
, speed
, class_id
, product_id
, vendor_id
;
1584 char port
[MAX_PORTLEN
];
1585 char product_name
[512];
1588 dir
= opendir("/sys/bus/usb/devices");
1590 perror("husb: opendir /sys/bus/usb/devices");
1591 fprintf(stderr
, "husb: please make sure sysfs is mounted at /sys\n");
1595 while ((de
= readdir(dir
))) {
1596 if (de
->d_name
[0] != '.' && !strchr(de
->d_name
, ':')) {
1597 if (sscanf(de
->d_name
, "%d-%7[0-9.]", &bus_num
, port
) < 2) {
1601 if (!usb_host_read_file(line
, sizeof(line
), "devnum", de
->d_name
)) {
1604 if (sscanf(line
, "%d", &addr
) != 1) {
1607 if (!usb_host_read_file(line
, sizeof(line
), "bDeviceClass",
1611 if (sscanf(line
, "%x", &class_id
) != 1) {
1615 if (!usb_host_read_file(line
, sizeof(line
), "idVendor",
1619 if (sscanf(line
, "%x", &vendor_id
) != 1) {
1622 if (!usb_host_read_file(line
, sizeof(line
), "idProduct",
1626 if (sscanf(line
, "%x", &product_id
) != 1) {
1629 if (!usb_host_read_file(line
, sizeof(line
), "product",
1633 if (strlen(line
) > 0) {
1634 line
[strlen(line
) - 1] = '\0';
1636 pstrcpy(product_name
, sizeof(product_name
), line
);
1639 if (!usb_host_read_file(line
, sizeof(line
), "speed", de
->d_name
)) {
1642 if (!strcmp(line
, "5000\n")) {
1643 speed
= USB_SPEED_SUPER
;
1644 } else if (!strcmp(line
, "480\n")) {
1645 speed
= USB_SPEED_HIGH
;
1646 } else if (!strcmp(line
, "1.5\n")) {
1647 speed
= USB_SPEED_LOW
;
1649 speed
= USB_SPEED_FULL
;
1652 ret
= func(opaque
, bus_num
, addr
, port
, class_id
, vendor_id
,
1653 product_id
, product_name
, speed
);
1666 static QEMUTimer
*usb_auto_timer
;
1667 static VMChangeStateEntry
*usb_vmstate
;
1669 static int usb_host_auto_scan(void *opaque
, int bus_num
,
1670 int addr
, const char *port
,
1671 int class_id
, int vendor_id
, int product_id
,
1672 const char *product_name
, int speed
)
1674 struct USBAutoFilter
*f
;
1675 struct USBHostDevice
*s
;
1681 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1684 if (f
->bus_num
> 0 && f
->bus_num
!= bus_num
) {
1687 if (f
->addr
> 0 && f
->addr
!= addr
) {
1690 if (f
->port
!= NULL
&& strcmp(f
->port
, port
) != 0) {
1694 if (f
->vendor_id
> 0 && f
->vendor_id
!= vendor_id
) {
1698 if (f
->product_id
> 0 && f
->product_id
!= product_id
) {
1701 /* We got a match */
1703 if (s
->errcount
>= 3) {
1707 /* Already attached ? */
1711 DPRINTF("husb: auto open: bus_num %d addr %d\n", bus_num
, addr
);
1713 if (usb_host_open(s
, bus_num
, addr
, port
, product_name
, speed
) < 0) {
1722 static void usb_host_vm_state(void *unused
, int running
, RunState state
)
1725 usb_host_auto_check(unused
);
1729 static void usb_host_auto_check(void *unused
)
1731 struct USBHostDevice
*s
;
1732 int unconnected
= 0;
1734 if (runstate_is_running()) {
1735 usb_host_scan(NULL
, usb_host_auto_scan
);
1737 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1747 if (unconnected
== 0) {
1748 /* nothing to watch */
1749 if (usb_auto_timer
) {
1750 qemu_del_timer(usb_auto_timer
);
1751 trace_usb_host_auto_scan_disabled();
1758 usb_vmstate
= qemu_add_vm_change_state_handler(usb_host_vm_state
, NULL
);
1760 if (!usb_auto_timer
) {
1761 usb_auto_timer
= qemu_new_timer_ms(rt_clock
, usb_host_auto_check
, NULL
);
1762 if (!usb_auto_timer
) {
1765 trace_usb_host_auto_scan_enabled();
1767 qemu_mod_timer(usb_auto_timer
, qemu_get_clock_ms(rt_clock
) + 2000);
1770 /**********************/
1771 /* USB host device info */
1773 struct usb_class_info
{
1775 const char *class_name
;
1778 static const struct usb_class_info usb_class_info
[] = {
1779 { USB_CLASS_AUDIO
, "Audio"},
1780 { USB_CLASS_COMM
, "Communication"},
1781 { USB_CLASS_HID
, "HID"},
1782 { USB_CLASS_HUB
, "Hub" },
1783 { USB_CLASS_PHYSICAL
, "Physical" },
1784 { USB_CLASS_PRINTER
, "Printer" },
1785 { USB_CLASS_MASS_STORAGE
, "Storage" },
1786 { USB_CLASS_CDC_DATA
, "Data" },
1787 { USB_CLASS_APP_SPEC
, "Application Specific" },
1788 { USB_CLASS_VENDOR_SPEC
, "Vendor Specific" },
1789 { USB_CLASS_STILL_IMAGE
, "Still Image" },
1790 { USB_CLASS_CSCID
, "Smart Card" },
1791 { USB_CLASS_CONTENT_SEC
, "Content Security" },
1795 static const char *usb_class_str(uint8_t class)
1797 const struct usb_class_info
*p
;
1798 for(p
= usb_class_info
; p
->class != -1; p
++) {
1799 if (p
->class == class) {
1803 return p
->class_name
;
1806 static void usb_info_device(Monitor
*mon
, int bus_num
,
1807 int addr
, const char *port
,
1808 int class_id
, int vendor_id
, int product_id
,
1809 const char *product_name
,
1812 const char *class_str
, *speed_str
;
1818 case USB_SPEED_FULL
:
1821 case USB_SPEED_HIGH
:
1824 case USB_SPEED_SUPER
:
1832 monitor_printf(mon
, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
1833 bus_num
, addr
, port
, speed_str
);
1834 class_str
= usb_class_str(class_id
);
1836 monitor_printf(mon
, " %s:", class_str
);
1838 monitor_printf(mon
, " Class %02x:", class_id
);
1840 monitor_printf(mon
, " USB device %04x:%04x", vendor_id
, product_id
);
1841 if (product_name
[0] != '\0') {
1842 monitor_printf(mon
, ", %s", product_name
);
1844 monitor_printf(mon
, "\n");
1847 static int usb_host_info_device(void *opaque
, int bus_num
, int addr
,
1848 const char *path
, int class_id
,
1849 int vendor_id
, int product_id
,
1850 const char *product_name
,
1853 Monitor
*mon
= opaque
;
1855 usb_info_device(mon
, bus_num
, addr
, path
, class_id
, vendor_id
, product_id
,
1856 product_name
, speed
);
1860 static void dec2str(int val
, char *str
, size_t size
)
1863 snprintf(str
, size
, "*");
1865 snprintf(str
, size
, "%d", val
);
1869 static void hex2str(int val
, char *str
, size_t size
)
1872 snprintf(str
, size
, "*");
1874 snprintf(str
, size
, "%04x", val
);
1878 void usb_host_info(Monitor
*mon
, const QDict
*qdict
)
1880 struct USBAutoFilter
*f
;
1881 struct USBHostDevice
*s
;
1883 usb_host_scan(mon
, usb_host_info_device
);
1885 if (QTAILQ_EMPTY(&hostdevs
)) {
1889 monitor_printf(mon
, " Auto filters:\n");
1890 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1891 char bus
[10], addr
[10], vid
[10], pid
[10];
1893 dec2str(f
->bus_num
, bus
, sizeof(bus
));
1894 dec2str(f
->addr
, addr
, sizeof(addr
));
1895 hex2str(f
->vendor_id
, vid
, sizeof(vid
));
1896 hex2str(f
->product_id
, pid
, sizeof(pid
));
1897 monitor_printf(mon
, " Bus %s, Addr %s, Port %s, ID %s:%s\n",
1898 bus
, addr
, f
->port
? f
->port
: "*", vid
, pid
);