2 * Linux host USB redirector
4 * Copyright (c) 2005 Fabrice Bellard
6 * Copyright (c) 2008 Max Krasnyansky
7 * Support for host device auto connect & disconnect
8 * Major rewrite to support fully async operation
10 * Copyright 2008 TJ <linux@tjworld.net>
11 * Added flexible support for /dev/bus/usb /sys/bus/usb/devices in addition
12 * to the legacy /proc/bus/usb USB device discovery and handling
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this software and associated documentation files (the "Software"), to deal
16 * in the Software without restriction, including without limitation the rights
17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 * copies of the Software, and to permit persons to whom the Software is
19 * furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33 #include "qemu-common.h"
34 #include "qemu-timer.h"
40 #include <sys/ioctl.h>
42 #include <linux/usbdevice_fs.h>
43 #include <linux/version.h>
46 /* We redefine it to avoid version problems */
47 struct usb_ctrltransfer
{
57 typedef int USBScanFunc(void *opaque
, int bus_num
, int addr
, const char *port
,
58 int class_id
, int vendor_id
, int product_id
,
59 const char *product_name
, int speed
);
64 #define DPRINTF printf
69 #define USBDBG_DEVOPENED "husb: opened %s/devices\n"
71 #define USBPROCBUS_PATH "/proc/bus/usb"
72 #define PRODUCT_NAME_SZ 32
73 #define MAX_ENDPOINTS 15
74 #define MAX_PORTLEN 16
75 #define USBDEVBUS_PATH "/dev/bus/usb"
76 #define USBSYSBUS_PATH "/sys/bus/usb"
78 static char *usb_host_device_path
;
85 static int usb_fs_type
;
87 /* endpoint association data */
88 #define ISO_FRAME_DESC_PER_URB 32
89 #define INVALID_EP_TYPE 255
91 /* devio.c limits single requests to 16k */
92 #define MAX_USBFS_BUFFER_SIZE 16384
94 typedef struct AsyncURB AsyncURB
;
107 struct USBAutoFilter
{
115 typedef struct USBHostDevice
{
126 uint32_t iso_urb_count
;
129 struct endp_data ep_in
[MAX_ENDPOINTS
];
130 struct endp_data ep_out
[MAX_ENDPOINTS
];
131 QLIST_HEAD(, AsyncURB
) aurbs
;
133 /* Host side address */
136 char port
[MAX_PORTLEN
];
137 struct USBAutoFilter match
;
140 QTAILQ_ENTRY(USBHostDevice
) next
;
143 static QTAILQ_HEAD(, USBHostDevice
) hostdevs
= QTAILQ_HEAD_INITIALIZER(hostdevs
);
145 static int usb_host_close(USBHostDevice
*dev
);
146 static int parse_filter(const char *spec
, struct USBAutoFilter
*f
);
147 static void usb_host_auto_check(void *unused
);
148 static int usb_host_read_file(char *line
, size_t line_size
,
149 const char *device_file
, const char *device_name
);
150 static int usb_linux_update_endp_table(USBHostDevice
*s
);
152 static int usb_host_do_reset(USBHostDevice
*dev
)
158 gettimeofday(&s
, NULL
);
159 ret
= ioctl(dev
->fd
, USBDEVFS_RESET
);
160 gettimeofday(&e
, NULL
);
161 usecs
= (e
.tv_sec
- s
.tv_sec
) * 1000000;
162 usecs
+= e
.tv_usec
- s
.tv_usec
;
163 if (usecs
> 1000000) {
164 /* more than a second, something is fishy, broken usb device? */
165 fprintf(stderr
, "husb: device %d:%d reset took %d.%06d seconds\n",
166 dev
->bus_num
, dev
->addr
, usecs
/ 1000000, usecs
% 1000000);
171 static struct endp_data
*get_endp(USBHostDevice
*s
, int pid
, int ep
)
173 struct endp_data
*eps
= pid
== USB_TOKEN_IN
? s
->ep_in
: s
->ep_out
;
174 assert(pid
== USB_TOKEN_IN
|| pid
== USB_TOKEN_OUT
);
175 assert(ep
> 0 && ep
<= MAX_ENDPOINTS
);
179 static int is_isoc(USBHostDevice
*s
, int pid
, int ep
)
181 return get_endp(s
, pid
, ep
)->type
== USBDEVFS_URB_TYPE_ISO
;
184 static int is_valid(USBHostDevice
*s
, int pid
, int ep
)
186 return get_endp(s
, pid
, ep
)->type
!= INVALID_EP_TYPE
;
189 static int is_halted(USBHostDevice
*s
, int pid
, int ep
)
191 return get_endp(s
, pid
, ep
)->halted
;
194 static void clear_halt(USBHostDevice
*s
, int pid
, int ep
)
196 trace_usb_host_ep_clear_halt(s
->bus_num
, s
->addr
, ep
);
197 get_endp(s
, pid
, ep
)->halted
= 0;
200 static void set_halt(USBHostDevice
*s
, int pid
, int ep
)
203 trace_usb_host_ep_set_halt(s
->bus_num
, s
->addr
, ep
);
204 get_endp(s
, pid
, ep
)->halted
= 1;
208 static int is_iso_started(USBHostDevice
*s
, int pid
, int ep
)
210 return get_endp(s
, pid
, ep
)->iso_started
;
213 static void clear_iso_started(USBHostDevice
*s
, int pid
, int ep
)
215 trace_usb_host_ep_stop_iso(s
->bus_num
, s
->addr
, ep
);
216 get_endp(s
, pid
, ep
)->iso_started
= 0;
219 static void set_iso_started(USBHostDevice
*s
, int pid
, int ep
)
221 struct endp_data
*e
= get_endp(s
, pid
, ep
);
223 trace_usb_host_ep_start_iso(s
->bus_num
, s
->addr
, ep
);
224 if (!e
->iso_started
) {
230 static int change_iso_inflight(USBHostDevice
*s
, int pid
, int ep
, int value
)
232 struct endp_data
*e
= get_endp(s
, pid
, ep
);
234 e
->inflight
+= value
;
238 static void set_iso_urb(USBHostDevice
*s
, int pid
, int ep
, AsyncURB
*iso_urb
)
240 get_endp(s
, pid
, ep
)->iso_urb
= iso_urb
;
243 static AsyncURB
*get_iso_urb(USBHostDevice
*s
, int pid
, int ep
)
245 return get_endp(s
, pid
, ep
)->iso_urb
;
248 static void set_iso_urb_idx(USBHostDevice
*s
, int pid
, int ep
, int i
)
250 get_endp(s
, pid
, ep
)->iso_urb_idx
= i
;
253 static int get_iso_urb_idx(USBHostDevice
*s
, int pid
, int ep
)
255 return get_endp(s
, pid
, ep
)->iso_urb_idx
;
258 static void set_iso_buffer_used(USBHostDevice
*s
, int pid
, int ep
, int i
)
260 get_endp(s
, pid
, ep
)->iso_buffer_used
= i
;
263 static int get_iso_buffer_used(USBHostDevice
*s
, int pid
, int ep
)
265 return get_endp(s
, pid
, ep
)->iso_buffer_used
;
268 static void set_max_packet_size(USBHostDevice
*s
, int pid
, int ep
,
271 int raw
= descriptor
[4] + (descriptor
[5] << 8);
272 int size
, microframes
;
275 switch ((raw
>> 11) & 3) {
276 case 1: microframes
= 2; break;
277 case 2: microframes
= 3; break;
278 default: microframes
= 1; break;
280 get_endp(s
, pid
, ep
)->max_packet_size
= size
* microframes
;
283 static int get_max_packet_size(USBHostDevice
*s
, int pid
, int ep
)
285 return get_endp(s
, pid
, ep
)->max_packet_size
;
290 * We always allocate iso packet descriptors even for bulk transfers
291 * to simplify allocation and casts.
295 struct usbdevfs_urb urb
;
296 struct usbdevfs_iso_packet_desc isocpd
[ISO_FRAME_DESC_PER_URB
];
298 QLIST_ENTRY(AsyncURB
) next
;
300 /* For regular async urbs */
302 int more
; /* large transfer, more urbs follow */
304 /* For buffered iso handling */
305 int iso_frame_idx
; /* -1 means in flight */
308 static AsyncURB
*async_alloc(USBHostDevice
*s
)
310 AsyncURB
*aurb
= g_malloc0(sizeof(AsyncURB
));
312 QLIST_INSERT_HEAD(&s
->aurbs
, aurb
, next
);
316 static void async_free(AsyncURB
*aurb
)
318 QLIST_REMOVE(aurb
, next
);
322 static void do_disconnect(USBHostDevice
*s
)
325 usb_host_auto_check(NULL
);
328 static void async_complete(void *opaque
)
330 USBHostDevice
*s
= opaque
;
337 int r
= ioctl(s
->fd
, USBDEVFS_REAPURBNDELAY
, &aurb
);
339 if (errno
== EAGAIN
) {
341 fprintf(stderr
, "husb: %d iso urbs finished at once\n", urbs
);
345 if (errno
== ENODEV
) {
347 trace_usb_host_disconnect(s
->bus_num
, s
->addr
);
353 perror("USBDEVFS_REAPURBNDELAY");
357 DPRINTF("husb: async completed. aurb %p status %d alen %d\n",
358 aurb
, aurb
->urb
.status
, aurb
->urb
.actual_length
);
360 /* If this is a buffered iso urb mark it as complete and don't do
361 anything else (it is handled further in usb_host_handle_iso_data) */
362 if (aurb
->iso_frame_idx
== -1) {
364 int pid
= (aurb
->urb
.endpoint
& USB_DIR_IN
) ?
365 USB_TOKEN_IN
: USB_TOKEN_OUT
;
366 int ep
= aurb
->urb
.endpoint
& 0xf;
367 if (aurb
->urb
.status
== -EPIPE
) {
368 set_halt(s
, pid
, ep
);
370 aurb
->iso_frame_idx
= 0;
372 inflight
= change_iso_inflight(s
, pid
, ep
, -1);
373 if (inflight
== 0 && is_iso_started(s
, pid
, ep
)) {
374 fprintf(stderr
, "husb: out of buffers for iso stream\n");
380 trace_usb_host_urb_complete(s
->bus_num
, s
->addr
, aurb
, aurb
->urb
.status
,
381 aurb
->urb
.actual_length
, aurb
->more
);
384 switch (aurb
->urb
.status
) {
386 p
->result
+= aurb
->urb
.actual_length
;
390 set_halt(s
, p
->pid
, p
->devep
);
391 p
->result
= USB_RET_STALL
;
395 p
->result
= USB_RET_NAK
;
399 if (aurb
->urb
.type
== USBDEVFS_URB_TYPE_CONTROL
) {
400 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
->result
);
401 usb_generic_async_ctrl_complete(&s
->dev
, p
);
402 } else if (!aurb
->more
) {
403 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, p
->result
);
404 usb_packet_complete(&s
->dev
, p
);
412 static void usb_host_async_cancel(USBDevice
*dev
, USBPacket
*p
)
414 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
417 QLIST_FOREACH(aurb
, &s
->aurbs
, next
) {
418 if (p
!= aurb
->packet
) {
422 DPRINTF("husb: async cancel: packet %p, aurb %p\n", p
, aurb
);
424 /* Mark it as dead (see async_complete above) */
427 int r
= ioctl(s
->fd
, USBDEVFS_DISCARDURB
, aurb
);
429 DPRINTF("husb: async. discard urb failed errno %d\n", errno
);
434 static int usb_host_claim_port(USBHostDevice
*s
)
436 #ifdef USBDEVFS_CLAIM_PORT
437 char *h
, hub_name
[64], line
[1024];
440 snprintf(hub_name
, sizeof(hub_name
), "%d-%s",
441 s
->match
.bus_num
, s
->match
.port
);
443 /* try strip off last ".$portnr" to get hub */
444 h
= strrchr(hub_name
, '.');
446 s
->hub_port
= atoi(h
+1);
449 /* no dot in there -> it is the root hub */
450 snprintf(hub_name
, sizeof(hub_name
), "usb%d",
452 s
->hub_port
= atoi(s
->match
.port
);
455 if (!usb_host_read_file(line
, sizeof(line
), "devnum",
459 if (sscanf(line
, "%d", &hub_addr
) != 1) {
463 if (!usb_host_device_path
) {
466 snprintf(line
, sizeof(line
), "%s/%03d/%03d",
467 usb_host_device_path
, s
->match
.bus_num
, hub_addr
);
468 s
->hub_fd
= open(line
, O_RDWR
| O_NONBLOCK
);
473 ret
= ioctl(s
->hub_fd
, USBDEVFS_CLAIM_PORT
, &s
->hub_port
);
480 trace_usb_host_claim_port(s
->match
.bus_num
, hub_addr
, s
->hub_port
);
487 static void usb_host_release_port(USBHostDevice
*s
)
489 if (s
->hub_fd
== -1) {
492 #ifdef USBDEVFS_RELEASE_PORT
493 ioctl(s
->hub_fd
, USBDEVFS_RELEASE_PORT
, &s
->hub_port
);
499 static int usb_host_disconnect_ifaces(USBHostDevice
*dev
, int nb_interfaces
)
501 /* earlier Linux 2.4 do not support that */
502 #ifdef USBDEVFS_DISCONNECT
503 struct usbdevfs_ioctl ctrl
;
506 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
507 ctrl
.ioctl_code
= USBDEVFS_DISCONNECT
;
508 ctrl
.ifno
= interface
;
510 ret
= ioctl(dev
->fd
, USBDEVFS_IOCTL
, &ctrl
);
511 if (ret
< 0 && errno
!= ENODATA
) {
512 perror("USBDEVFS_DISCONNECT");
520 static int usb_linux_get_num_interfaces(USBHostDevice
*s
)
522 char device_name
[64], line
[1024];
523 int num_interfaces
= 0;
525 if (usb_fs_type
!= USB_FS_SYS
) {
529 sprintf(device_name
, "%d-%s", s
->bus_num
, s
->port
);
530 if (!usb_host_read_file(line
, sizeof(line
), "bNumInterfaces",
534 if (sscanf(line
, "%d", &num_interfaces
) != 1) {
537 return num_interfaces
;
540 static int usb_host_claim_interfaces(USBHostDevice
*dev
, int configuration
)
542 const char *op
= NULL
;
543 int dev_descr_len
, config_descr_len
;
544 int interface
, nb_interfaces
;
547 if (configuration
== 0) { /* address state - ignore */
548 dev
->ninterfaces
= 0;
549 dev
->configuration
= 0;
553 DPRINTF("husb: claiming interfaces. config %d\n", configuration
);
556 dev_descr_len
= dev
->descr
[0];
557 if (dev_descr_len
> dev
->descr_len
) {
558 fprintf(stderr
, "husb: update iface failed. descr too short\n");
563 while (i
< dev
->descr_len
) {
564 DPRINTF("husb: i is %d, descr_len is %d, dl %d, dt %d\n",
566 dev
->descr
[i
], dev
->descr
[i
+1]);
568 if (dev
->descr
[i
+1] != USB_DT_CONFIG
) {
572 config_descr_len
= dev
->descr
[i
];
574 DPRINTF("husb: config #%d need %d\n", dev
->descr
[i
+ 5], configuration
);
576 if (configuration
== dev
->descr
[i
+ 5]) {
577 configuration
= dev
->descr
[i
+ 5];
581 i
+= config_descr_len
;
584 if (i
>= dev
->descr_len
) {
586 "husb: update iface failed. no matching configuration\n");
589 nb_interfaces
= dev
->descr
[i
+ 4];
591 if (usb_host_disconnect_ifaces(dev
, nb_interfaces
) < 0) {
595 /* XXX: only grab if all interfaces are free */
596 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
597 op
= "USBDEVFS_CLAIMINTERFACE";
598 ret
= ioctl(dev
->fd
, USBDEVFS_CLAIMINTERFACE
, &interface
);
604 trace_usb_host_claim_interfaces(dev
->bus_num
, dev
->addr
,
605 nb_interfaces
, configuration
);
607 dev
->ninterfaces
= nb_interfaces
;
608 dev
->configuration
= configuration
;
612 if (errno
== ENODEV
) {
619 static int usb_host_release_interfaces(USBHostDevice
*s
)
623 trace_usb_host_release_interfaces(s
->bus_num
, s
->addr
);
625 for (i
= 0; i
< s
->ninterfaces
; i
++) {
626 ret
= ioctl(s
->fd
, USBDEVFS_RELEASEINTERFACE
, &i
);
628 perror("USBDEVFS_RELEASEINTERFACE");
635 static void usb_host_handle_reset(USBDevice
*dev
)
637 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
639 trace_usb_host_reset(s
->bus_num
, s
->addr
);
641 usb_host_do_reset(s
);;
643 usb_host_claim_interfaces(s
, 0);
644 usb_linux_update_endp_table(s
);
647 static void usb_host_handle_destroy(USBDevice
*dev
)
649 USBHostDevice
*s
= (USBHostDevice
*)dev
;
651 usb_host_release_port(s
);
653 QTAILQ_REMOVE(&hostdevs
, s
, next
);
654 qemu_remove_exit_notifier(&s
->exit
);
657 /* iso data is special, we need to keep enough urbs in flight to make sure
658 that the controller never runs out of them, otherwise the device will
659 likely suffer a buffer underrun / overrun. */
660 static AsyncURB
*usb_host_alloc_iso(USBHostDevice
*s
, int pid
, uint8_t ep
)
663 int i
, j
, len
= get_max_packet_size(s
, pid
, ep
);
665 aurb
= g_malloc0(s
->iso_urb_count
* sizeof(*aurb
));
666 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
667 aurb
[i
].urb
.endpoint
= ep
;
668 aurb
[i
].urb
.buffer_length
= ISO_FRAME_DESC_PER_URB
* len
;
669 aurb
[i
].urb
.buffer
= g_malloc(aurb
[i
].urb
.buffer_length
);
670 aurb
[i
].urb
.type
= USBDEVFS_URB_TYPE_ISO
;
671 aurb
[i
].urb
.flags
= USBDEVFS_URB_ISO_ASAP
;
672 aurb
[i
].urb
.number_of_packets
= ISO_FRAME_DESC_PER_URB
;
673 for (j
= 0 ; j
< ISO_FRAME_DESC_PER_URB
; j
++)
674 aurb
[i
].urb
.iso_frame_desc
[j
].length
= len
;
675 if (pid
== USB_TOKEN_IN
) {
676 aurb
[i
].urb
.endpoint
|= 0x80;
677 /* Mark as fully consumed (idle) */
678 aurb
[i
].iso_frame_idx
= ISO_FRAME_DESC_PER_URB
;
681 set_iso_urb(s
, pid
, ep
, aurb
);
686 static void usb_host_stop_n_free_iso(USBHostDevice
*s
, int pid
, uint8_t ep
)
689 int i
, ret
, killed
= 0, free
= 1;
691 aurb
= get_iso_urb(s
, pid
, ep
);
696 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
698 if (aurb
[i
].iso_frame_idx
== -1) {
699 ret
= ioctl(s
->fd
, USBDEVFS_DISCARDURB
, &aurb
[i
]);
701 perror("USBDEVFS_DISCARDURB");
709 /* Make sure any urbs we've killed are reaped before we free them */
714 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
715 g_free(aurb
[i
].urb
.buffer
);
721 printf("husb: leaking iso urbs because of discard failure\n");
722 set_iso_urb(s
, pid
, ep
, NULL
);
723 set_iso_urb_idx(s
, pid
, ep
, 0);
724 clear_iso_started(s
, pid
, ep
);
727 static int urb_status_to_usb_ret(int status
)
731 return USB_RET_STALL
;
737 static int usb_host_handle_iso_data(USBHostDevice
*s
, USBPacket
*p
, int in
)
740 int i
, j
, ret
, max_packet_size
, offset
, len
= 0;
743 max_packet_size
= get_max_packet_size(s
, p
->pid
, p
->devep
);
744 if (max_packet_size
== 0)
747 aurb
= get_iso_urb(s
, p
->pid
, p
->devep
);
749 aurb
= usb_host_alloc_iso(s
, p
->pid
, p
->devep
);
752 i
= get_iso_urb_idx(s
, p
->pid
, p
->devep
);
753 j
= aurb
[i
].iso_frame_idx
;
754 if (j
>= 0 && j
< ISO_FRAME_DESC_PER_URB
) {
756 /* Check urb status */
757 if (aurb
[i
].urb
.status
) {
758 len
= urb_status_to_usb_ret(aurb
[i
].urb
.status
);
759 /* Move to the next urb */
760 aurb
[i
].iso_frame_idx
= ISO_FRAME_DESC_PER_URB
- 1;
761 /* Check frame status */
762 } else if (aurb
[i
].urb
.iso_frame_desc
[j
].status
) {
763 len
= urb_status_to_usb_ret(
764 aurb
[i
].urb
.iso_frame_desc
[j
].status
);
765 /* Check the frame fits */
766 } else if (aurb
[i
].urb
.iso_frame_desc
[j
].actual_length
768 printf("husb: received iso data is larger then packet\n");
770 /* All good copy data over */
772 len
= aurb
[i
].urb
.iso_frame_desc
[j
].actual_length
;
773 buf
= aurb
[i
].urb
.buffer
+
774 j
* aurb
[i
].urb
.iso_frame_desc
[0].length
;
775 usb_packet_copy(p
, buf
, len
);
779 offset
= (j
== 0) ? 0 : get_iso_buffer_used(s
, p
->pid
, p
->devep
);
781 /* Check the frame fits */
782 if (len
> max_packet_size
) {
783 printf("husb: send iso data is larger then max packet size\n");
787 /* All good copy data over */
788 usb_packet_copy(p
, aurb
[i
].urb
.buffer
+ offset
, len
);
789 aurb
[i
].urb
.iso_frame_desc
[j
].length
= len
;
791 set_iso_buffer_used(s
, p
->pid
, p
->devep
, offset
);
793 /* Start the stream once we have buffered enough data */
794 if (!is_iso_started(s
, p
->pid
, p
->devep
) && i
== 1 && j
== 8) {
795 set_iso_started(s
, p
->pid
, p
->devep
);
798 aurb
[i
].iso_frame_idx
++;
799 if (aurb
[i
].iso_frame_idx
== ISO_FRAME_DESC_PER_URB
) {
800 i
= (i
+ 1) % s
->iso_urb_count
;
801 set_iso_urb_idx(s
, p
->pid
, p
->devep
, i
);
805 set_iso_started(s
, p
->pid
, p
->devep
);
807 DPRINTF("hubs: iso out error no free buffer, dropping packet\n");
811 if (is_iso_started(s
, p
->pid
, p
->devep
)) {
812 /* (Re)-submit all fully consumed / filled urbs */
813 for (i
= 0; i
< s
->iso_urb_count
; i
++) {
814 if (aurb
[i
].iso_frame_idx
== ISO_FRAME_DESC_PER_URB
) {
815 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, &aurb
[i
]);
817 perror("USBDEVFS_SUBMITURB");
818 if (!in
|| len
== 0) {
830 aurb
[i
].iso_frame_idx
= -1;
831 change_iso_inflight(s
, p
->pid
, p
->devep
, 1);
839 static int usb_host_handle_data(USBDevice
*dev
, USBPacket
*p
)
841 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
842 struct usbdevfs_urb
*urb
;
844 int ret
, rem
, prem
, v
;
848 trace_usb_host_req_data(s
->bus_num
, s
->addr
,
849 p
->pid
== USB_TOKEN_IN
,
850 p
->devep
, p
->iov
.size
);
852 if (!is_valid(s
, p
->pid
, p
->devep
)) {
853 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, USB_RET_NAK
);
857 if (p
->pid
== USB_TOKEN_IN
) {
858 ep
= p
->devep
| 0x80;
863 if (is_halted(s
, p
->pid
, p
->devep
)) {
864 unsigned int arg
= ep
;
865 ret
= ioctl(s
->fd
, USBDEVFS_CLEAR_HALT
, &arg
);
867 perror("USBDEVFS_CLEAR_HALT");
868 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, USB_RET_NAK
);
871 clear_halt(s
, p
->pid
, p
->devep
);
874 if (is_isoc(s
, p
->pid
, p
->devep
)) {
875 return usb_host_handle_iso_data(s
, p
, p
->pid
== USB_TOKEN_IN
);
879 prem
= p
->iov
.iov
[v
].iov_len
;
880 pbuf
= p
->iov
.iov
[v
].iov_base
;
885 assert(v
< p
->iov
.niov
);
886 prem
= p
->iov
.iov
[v
].iov_len
;
887 pbuf
= p
->iov
.iov
[v
].iov_base
;
890 aurb
= async_alloc(s
);
895 urb
->type
= USBDEVFS_URB_TYPE_BULK
;
896 urb
->usercontext
= s
;
898 urb
->buffer_length
= prem
;
900 if (urb
->buffer_length
> MAX_USBFS_BUFFER_SIZE
) {
901 urb
->buffer_length
= MAX_USBFS_BUFFER_SIZE
;
903 pbuf
+= urb
->buffer_length
;
904 prem
-= urb
->buffer_length
;
905 rem
-= urb
->buffer_length
;
910 trace_usb_host_urb_submit(s
->bus_num
, s
->addr
, aurb
,
911 urb
->buffer_length
, aurb
->more
);
912 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
914 DPRINTF("husb: data submit: ep 0x%x, len %u, more %d, packet %p, aurb %p\n",
915 urb
->endpoint
, urb
->buffer_length
, aurb
->more
, p
, aurb
);
918 perror("USBDEVFS_SUBMITURB");
923 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, USB_RET_NAK
);
927 trace_usb_host_req_complete(s
->bus_num
, s
->addr
, USB_RET_STALL
);
928 return USB_RET_STALL
;
933 return USB_RET_ASYNC
;
936 static int ctrl_error(void)
938 if (errno
== ETIMEDOUT
) {
941 return USB_RET_STALL
;
945 static int usb_host_set_address(USBHostDevice
*s
, int addr
)
947 trace_usb_host_set_address(s
->bus_num
, s
->addr
, addr
);
952 static int usb_host_set_config(USBHostDevice
*s
, int config
)
956 trace_usb_host_set_config(s
->bus_num
, s
->addr
, config
);
958 usb_host_release_interfaces(s
);
961 ret
= ioctl(s
->fd
, USBDEVFS_SETCONFIGURATION
, &config
);
963 DPRINTF("husb: ctrl set config %d ret %d errno %d\n", config
, ret
, errno
);
965 if (ret
< 0 && errno
== EBUSY
&& first
) {
966 /* happens if usb device is in use by host drivers */
967 int count
= usb_linux_get_num_interfaces(s
);
969 DPRINTF("husb: busy -> disconnecting %d interfaces\n", count
);
970 usb_host_disconnect_ifaces(s
, count
);
979 usb_host_claim_interfaces(s
, config
);
980 usb_linux_update_endp_table(s
);
984 static int usb_host_set_interface(USBHostDevice
*s
, int iface
, int alt
)
986 struct usbdevfs_setinterface si
;
989 trace_usb_host_set_interface(s
->bus_num
, s
->addr
, iface
, alt
);
991 for (i
= 1; i
<= MAX_ENDPOINTS
; i
++) {
992 if (is_isoc(s
, USB_TOKEN_IN
, i
)) {
993 usb_host_stop_n_free_iso(s
, USB_TOKEN_IN
, i
);
995 if (is_isoc(s
, USB_TOKEN_OUT
, i
)) {
996 usb_host_stop_n_free_iso(s
, USB_TOKEN_OUT
, i
);
1000 si
.interface
= iface
;
1001 si
.altsetting
= alt
;
1002 ret
= ioctl(s
->fd
, USBDEVFS_SETINTERFACE
, &si
);
1004 DPRINTF("husb: ctrl set iface %d altset %d ret %d errno %d\n",
1005 iface
, alt
, ret
, errno
);
1008 return ctrl_error();
1010 usb_linux_update_endp_table(s
);
1014 static int usb_host_handle_control(USBDevice
*dev
, USBPacket
*p
,
1015 int request
, int value
, int index
, int length
, uint8_t *data
)
1017 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
1018 struct usbdevfs_urb
*urb
;
1023 * Process certain standard device requests.
1024 * These are infrequent and are processed synchronously.
1027 /* Note request is (bRequestType << 8) | bRequest */
1028 trace_usb_host_req_control(s
->bus_num
, s
->addr
, request
, value
, index
);
1031 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
1032 return usb_host_set_address(s
, value
);
1034 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
1035 return usb_host_set_config(s
, value
& 0xff);
1037 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
1038 return usb_host_set_interface(s
, index
, value
);
1041 /* The rest are asynchronous */
1043 if (length
> sizeof(dev
->data_buf
)) {
1044 fprintf(stderr
, "husb: ctrl buffer too small (%d > %zu)\n",
1045 length
, sizeof(dev
->data_buf
));
1046 return USB_RET_STALL
;
1049 aurb
= async_alloc(s
);
1053 * Setup ctrl transfer.
1055 * s->ctrl is laid out such that data buffer immediately follows
1056 * 'req' struct which is exactly what usbdevfs expects.
1060 urb
->type
= USBDEVFS_URB_TYPE_CONTROL
;
1061 urb
->endpoint
= p
->devep
;
1063 urb
->buffer
= &dev
->setup_buf
;
1064 urb
->buffer_length
= length
+ 8;
1066 urb
->usercontext
= s
;
1068 trace_usb_host_urb_submit(s
->bus_num
, s
->addr
, aurb
,
1069 urb
->buffer_length
, aurb
->more
);
1070 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
1072 DPRINTF("husb: submit ctrl. len %u aurb %p\n", urb
->buffer_length
, aurb
);
1075 DPRINTF("husb: submit failed. errno %d\n", errno
);
1083 return USB_RET_STALL
;
1087 return USB_RET_ASYNC
;
1090 static uint8_t usb_linux_get_alt_setting(USBHostDevice
*s
,
1091 uint8_t configuration
, uint8_t interface
)
1093 uint8_t alt_setting
;
1094 struct usb_ctrltransfer ct
;
1097 if (usb_fs_type
== USB_FS_SYS
) {
1098 char device_name
[64], line
[1024];
1101 sprintf(device_name
, "%d-%s:%d.%d", s
->bus_num
, s
->port
,
1102 (int)configuration
, (int)interface
);
1104 if (!usb_host_read_file(line
, sizeof(line
), "bAlternateSetting",
1108 if (sscanf(line
, "%d", &alt_setting
) != 1) {
1115 ct
.bRequestType
= USB_DIR_IN
| USB_RECIP_INTERFACE
;
1116 ct
.bRequest
= USB_REQ_GET_INTERFACE
;
1118 ct
.wIndex
= interface
;
1120 ct
.data
= &alt_setting
;
1122 ret
= ioctl(s
->fd
, USBDEVFS_CONTROL
, &ct
);
1124 /* Assume alt 0 on error */
1131 /* returns 1 on problem encountered or 0 for success */
1132 static int usb_linux_update_endp_table(USBHostDevice
*s
)
1134 uint8_t *descriptors
;
1135 uint8_t devep
, type
, alt_interface
;
1136 int interface
, length
, i
, ep
, pid
;
1137 struct endp_data
*epd
;
1139 for (i
= 0; i
< MAX_ENDPOINTS
; i
++) {
1140 s
->ep_in
[i
].type
= INVALID_EP_TYPE
;
1141 s
->ep_out
[i
].type
= INVALID_EP_TYPE
;
1144 if (s
->configuration
== 0) {
1145 /* not configured yet -- leave all endpoints disabled */
1149 /* get the desired configuration, interface, and endpoint descriptors
1150 * from device description */
1151 descriptors
= &s
->descr
[18];
1152 length
= s
->descr_len
- 18;
1155 while (i
< length
) {
1156 if (descriptors
[i
+ 1] != USB_DT_CONFIG
) {
1157 fprintf(stderr
, "invalid descriptor data\n");
1159 } else if (descriptors
[i
+ 5] != s
->configuration
) {
1160 DPRINTF("not requested configuration %d\n", s
->configuration
);
1161 i
+= (descriptors
[i
+ 3] << 8) + descriptors
[i
+ 2];
1165 i
+= descriptors
[i
];
1167 if (descriptors
[i
+ 1] != USB_DT_INTERFACE
||
1168 (descriptors
[i
+ 1] == USB_DT_INTERFACE
&&
1169 descriptors
[i
+ 4] == 0)) {
1170 i
+= descriptors
[i
];
1174 interface
= descriptors
[i
+ 2];
1175 alt_interface
= usb_linux_get_alt_setting(s
, s
->configuration
,
1178 /* the current interface descriptor is the active interface
1179 * and has endpoints */
1180 if (descriptors
[i
+ 3] != alt_interface
) {
1181 i
+= descriptors
[i
];
1185 /* advance to the endpoints */
1186 while (i
< length
&& descriptors
[i
+1] != USB_DT_ENDPOINT
) {
1187 i
+= descriptors
[i
];
1193 while (i
< length
) {
1194 if (descriptors
[i
+ 1] != USB_DT_ENDPOINT
) {
1198 devep
= descriptors
[i
+ 2];
1199 pid
= (devep
& USB_DIR_IN
) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
1202 fprintf(stderr
, "usb-linux: invalid ep descriptor, ep == 0\n");
1206 switch (descriptors
[i
+ 3] & 0x3) {
1208 type
= USBDEVFS_URB_TYPE_CONTROL
;
1211 type
= USBDEVFS_URB_TYPE_ISO
;
1212 set_max_packet_size(s
, pid
, ep
, descriptors
+ i
);
1215 type
= USBDEVFS_URB_TYPE_BULK
;
1218 type
= USBDEVFS_URB_TYPE_INTERRUPT
;
1221 DPRINTF("usb_host: malformed endpoint type\n");
1222 type
= USBDEVFS_URB_TYPE_BULK
;
1224 epd
= get_endp(s
, pid
, ep
);
1225 assert(epd
->type
== INVALID_EP_TYPE
);
1229 i
+= descriptors
[i
];
1236 * Check if we can safely redirect a usb2 device to a usb1 virtual controller,
1237 * this function assumes this is safe, if:
1238 * 1) There are no isoc endpoints
1239 * 2) There are no interrupt endpoints with a max_packet_size > 64
1240 * Note bulk endpoints with a max_packet_size > 64 in theory also are not
1241 * usb1 compatible, but in practice this seems to work fine.
1243 static int usb_linux_full_speed_compat(USBHostDevice
*dev
)
1248 * usb_linux_update_endp_table only registers info about ep in the current
1249 * interface altsettings, so we need to parse the descriptors again.
1251 for (i
= 0; (i
+ 5) < dev
->descr_len
; i
+= dev
->descr
[i
]) {
1252 if (dev
->descr
[i
+ 1] == USB_DT_ENDPOINT
) {
1253 switch (dev
->descr
[i
+ 3] & 0x3) {
1254 case 0x00: /* CONTROL */
1256 case 0x01: /* ISO */
1258 case 0x02: /* BULK */
1260 case 0x03: /* INTERRUPT */
1261 packet_size
= dev
->descr
[i
+ 4] + (dev
->descr
[i
+ 5] << 8);
1262 if (packet_size
> 64)
1271 static int usb_host_open(USBHostDevice
*dev
, int bus_num
,
1272 int addr
, const char *port
,
1273 const char *prod_name
, int speed
)
1278 trace_usb_host_open_started(bus_num
, addr
);
1280 if (dev
->fd
!= -1) {
1284 if (!usb_host_device_path
) {
1285 perror("husb: USB Host Device Path not set");
1288 snprintf(buf
, sizeof(buf
), "%s/%03d/%03d", usb_host_device_path
,
1290 fd
= open(buf
, O_RDWR
| O_NONBLOCK
);
1295 DPRINTF("husb: opened %s\n", buf
);
1297 dev
->bus_num
= bus_num
;
1299 strcpy(dev
->port
, port
);
1302 /* read the device description */
1303 dev
->descr_len
= read(fd
, dev
->descr
, sizeof(dev
->descr
));
1304 if (dev
->descr_len
<= 0) {
1305 perror("husb: reading device data failed");
1312 printf("=== begin dumping device descriptor data ===\n");
1313 for (x
= 0; x
< dev
->descr_len
; x
++) {
1314 printf("%02x ", dev
->descr
[x
]);
1316 printf("\n=== end dumping device descriptor data ===\n");
1321 /* start unconfigured -- we'll wait for the guest to set a configuration */
1322 if (!usb_host_claim_interfaces(dev
, 0)) {
1326 ret
= usb_linux_update_endp_table(dev
);
1332 struct usbdevfs_connectinfo ci
;
1334 ret
= ioctl(fd
, USBDEVFS_CONNECTINFO
, &ci
);
1336 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
1341 speed
= USB_SPEED_LOW
;
1343 speed
= USB_SPEED_HIGH
;
1346 dev
->dev
.speed
= speed
;
1347 dev
->dev
.speedmask
= (1 << speed
);
1348 if (dev
->dev
.speed
== USB_SPEED_HIGH
&& usb_linux_full_speed_compat(dev
)) {
1349 dev
->dev
.speedmask
|= USB_SPEED_MASK_FULL
;
1352 trace_usb_host_open_success(bus_num
, addr
);
1354 if (!prod_name
|| prod_name
[0] == '\0') {
1355 snprintf(dev
->dev
.product_desc
, sizeof(dev
->dev
.product_desc
),
1356 "host:%d.%d", bus_num
, addr
);
1358 pstrcpy(dev
->dev
.product_desc
, sizeof(dev
->dev
.product_desc
),
1362 ret
= usb_device_attach(&dev
->dev
);
1367 /* USB devio uses 'write' flag to check for async completions */
1368 qemu_set_fd_handler(dev
->fd
, NULL
, async_complete
, dev
);
1373 trace_usb_host_open_failure(bus_num
, addr
);
1374 if (dev
->fd
!= -1) {
1381 static int usb_host_close(USBHostDevice
*dev
)
1385 if (dev
->fd
== -1) {
1389 trace_usb_host_close(dev
->bus_num
, dev
->addr
);
1391 qemu_set_fd_handler(dev
->fd
, NULL
, NULL
, NULL
);
1393 for (i
= 1; i
<= MAX_ENDPOINTS
; i
++) {
1394 if (is_isoc(dev
, USB_TOKEN_IN
, i
)) {
1395 usb_host_stop_n_free_iso(dev
, USB_TOKEN_IN
, i
);
1397 if (is_isoc(dev
, USB_TOKEN_OUT
, i
)) {
1398 usb_host_stop_n_free_iso(dev
, USB_TOKEN_OUT
, i
);
1401 async_complete(dev
);
1403 if (dev
->dev
.attached
) {
1404 usb_device_detach(&dev
->dev
);
1406 usb_host_do_reset(dev
);
1412 static void usb_host_exit_notifier(struct Notifier
*n
, void *data
)
1414 USBHostDevice
*s
= container_of(n
, USBHostDevice
, exit
);
1416 usb_host_release_port(s
);
1418 usb_host_do_reset(s
);;
1422 static int usb_host_initfn(USBDevice
*dev
)
1424 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
1426 dev
->auto_attach
= 0;
1430 QTAILQ_INSERT_TAIL(&hostdevs
, s
, next
);
1431 s
->exit
.notify
= usb_host_exit_notifier
;
1432 qemu_add_exit_notifier(&s
->exit
);
1433 usb_host_auto_check(NULL
);
1435 if (s
->match
.bus_num
!= 0 && s
->match
.port
!= NULL
) {
1436 usb_host_claim_port(s
);
1441 static const VMStateDescription vmstate_usb_host
= {
1446 static struct USBDeviceInfo usb_host_dev_info
= {
1447 .product_desc
= "USB Host Device",
1448 .qdev
.name
= "usb-host",
1449 .qdev
.size
= sizeof(USBHostDevice
),
1450 .qdev
.vmsd
= &vmstate_usb_host
,
1451 .init
= usb_host_initfn
,
1452 .handle_packet
= usb_generic_handle_packet
,
1453 .cancel_packet
= usb_host_async_cancel
,
1454 .handle_data
= usb_host_handle_data
,
1455 .handle_control
= usb_host_handle_control
,
1456 .handle_reset
= usb_host_handle_reset
,
1457 .handle_destroy
= usb_host_handle_destroy
,
1458 .usbdevice_name
= "host",
1459 .usbdevice_init
= usb_host_device_open
,
1460 .qdev
.props
= (Property
[]) {
1461 DEFINE_PROP_UINT32("hostbus", USBHostDevice
, match
.bus_num
, 0),
1462 DEFINE_PROP_UINT32("hostaddr", USBHostDevice
, match
.addr
, 0),
1463 DEFINE_PROP_STRING("hostport", USBHostDevice
, match
.port
),
1464 DEFINE_PROP_HEX32("vendorid", USBHostDevice
, match
.vendor_id
, 0),
1465 DEFINE_PROP_HEX32("productid", USBHostDevice
, match
.product_id
, 0),
1466 DEFINE_PROP_UINT32("isobufs", USBHostDevice
, iso_urb_count
, 4),
1467 DEFINE_PROP_END_OF_LIST(),
1471 static void usb_host_register_devices(void)
1473 usb_qdev_register(&usb_host_dev_info
);
1475 device_init(usb_host_register_devices
)
1477 USBDevice
*usb_host_device_open(const char *devname
)
1479 struct USBAutoFilter filter
;
1483 dev
= usb_create(NULL
/* FIXME */, "usb-host");
1485 if (strstr(devname
, "auto:")) {
1486 if (parse_filter(devname
, &filter
) < 0) {
1490 if ((p
= strchr(devname
, '.'))) {
1491 filter
.bus_num
= strtoul(devname
, NULL
, 0);
1492 filter
.addr
= strtoul(p
+ 1, NULL
, 0);
1493 filter
.vendor_id
= 0;
1494 filter
.product_id
= 0;
1495 } else if ((p
= strchr(devname
, ':'))) {
1498 filter
.vendor_id
= strtoul(devname
, NULL
, 16);
1499 filter
.product_id
= strtoul(p
+ 1, NULL
, 16);
1505 qdev_prop_set_uint32(&dev
->qdev
, "hostbus", filter
.bus_num
);
1506 qdev_prop_set_uint32(&dev
->qdev
, "hostaddr", filter
.addr
);
1507 qdev_prop_set_uint32(&dev
->qdev
, "vendorid", filter
.vendor_id
);
1508 qdev_prop_set_uint32(&dev
->qdev
, "productid", filter
.product_id
);
1509 qdev_init_nofail(&dev
->qdev
);
1513 qdev_free(&dev
->qdev
);
1517 int usb_host_device_close(const char *devname
)
1520 char product_name
[PRODUCT_NAME_SZ
];
1524 if (strstr(devname
, "auto:")) {
1525 return usb_host_auto_del(devname
);
1527 if (usb_host_find_device(&bus_num
, &addr
, product_name
,
1528 sizeof(product_name
), devname
) < 0) {
1531 s
= hostdev_find(bus_num
, addr
);
1533 usb_device_delete_addr(s
->bus_num
, s
->dev
.addr
);
1541 static int get_tag_value(char *buf
, int buf_size
,
1542 const char *str
, const char *tag
,
1543 const char *stopchars
)
1547 p
= strstr(str
, tag
);
1552 while (qemu_isspace(*p
)) {
1556 while (*p
!= '\0' && !strchr(stopchars
, *p
)) {
1557 if ((q
- buf
) < (buf_size
- 1)) {
1567 * Use /proc/bus/usb/devices or /dev/bus/usb/devices file to determine
1568 * host's USB devices. This is legacy support since many distributions
1569 * are moving to /sys/bus/usb
1571 static int usb_host_scan_dev(void *opaque
, USBScanFunc
*func
)
1576 int bus_num
, addr
, speed
, device_count
;
1577 int class_id
, product_id
, vendor_id
, port
;
1578 char product_name
[512];
1581 if (!usb_host_device_path
) {
1582 perror("husb: USB Host Device Path not set");
1585 snprintf(line
, sizeof(line
), "%s/devices", usb_host_device_path
);
1586 f
= fopen(line
, "r");
1588 perror("husb: cannot open devices file");
1593 bus_num
= addr
= class_id
= product_id
= vendor_id
= port
= 0;
1594 speed
= -1; /* Can't get the speed from /[proc|dev]/bus/usb/devices */
1596 if (fgets(line
, sizeof(line
), f
) == NULL
) {
1599 if (strlen(line
) > 0) {
1600 line
[strlen(line
) - 1] = '\0';
1602 if (line
[0] == 'T' && line
[1] == ':') {
1603 if (device_count
&& (vendor_id
|| product_id
)) {
1604 /* New device. Add the previously discovered device. */
1606 snprintf(buf
, sizeof(buf
), "%d", port
);
1608 snprintf(buf
, sizeof(buf
), "?");
1610 ret
= func(opaque
, bus_num
, addr
, buf
, class_id
, vendor_id
,
1611 product_id
, product_name
, speed
);
1616 if (get_tag_value(buf
, sizeof(buf
), line
, "Bus=", " ") < 0) {
1619 bus_num
= atoi(buf
);
1620 if (get_tag_value(buf
, sizeof(buf
), line
, "Port=", " ") < 0) {
1624 if (get_tag_value(buf
, sizeof(buf
), line
, "Dev#=", " ") < 0) {
1628 if (get_tag_value(buf
, sizeof(buf
), line
, "Spd=", " ") < 0) {
1631 if (!strcmp(buf
, "5000")) {
1632 speed
= USB_SPEED_SUPER
;
1633 } else if (!strcmp(buf
, "480")) {
1634 speed
= USB_SPEED_HIGH
;
1635 } else if (!strcmp(buf
, "1.5")) {
1636 speed
= USB_SPEED_LOW
;
1638 speed
= USB_SPEED_FULL
;
1640 product_name
[0] = '\0';
1645 } else if (line
[0] == 'P' && line
[1] == ':') {
1646 if (get_tag_value(buf
, sizeof(buf
), line
, "Vendor=", " ") < 0) {
1649 vendor_id
= strtoul(buf
, NULL
, 16);
1650 if (get_tag_value(buf
, sizeof(buf
), line
, "ProdID=", " ") < 0) {
1653 product_id
= strtoul(buf
, NULL
, 16);
1654 } else if (line
[0] == 'S' && line
[1] == ':') {
1655 if (get_tag_value(buf
, sizeof(buf
), line
, "Product=", "") < 0) {
1658 pstrcpy(product_name
, sizeof(product_name
), buf
);
1659 } else if (line
[0] == 'D' && line
[1] == ':') {
1660 if (get_tag_value(buf
, sizeof(buf
), line
, "Cls=", " (") < 0) {
1663 class_id
= strtoul(buf
, NULL
, 16);
1667 if (device_count
&& (vendor_id
|| product_id
)) {
1668 /* Add the last device. */
1670 snprintf(buf
, sizeof(buf
), "%d", port
);
1672 snprintf(buf
, sizeof(buf
), "?");
1674 ret
= func(opaque
, bus_num
, addr
, buf
, class_id
, vendor_id
,
1675 product_id
, product_name
, speed
);
1685 * Read sys file-system device file
1687 * @line address of buffer to put file contents in
1688 * @line_size size of line
1689 * @device_file path to device file (printf format string)
1690 * @device_name device being opened (inserted into device_file)
1692 * @return 0 failed, 1 succeeded ('line' contains data)
1694 static int usb_host_read_file(char *line
, size_t line_size
,
1695 const char *device_file
, const char *device_name
)
1699 char filename
[PATH_MAX
];
1701 snprintf(filename
, PATH_MAX
, USBSYSBUS_PATH
"/devices/%s/%s", device_name
,
1703 f
= fopen(filename
, "r");
1705 ret
= fgets(line
, line_size
, f
) != NULL
;
1713 * Use /sys/bus/usb/devices/ directory to determine host's USB
1716 * This code is based on Robert Schiele's original patches posted to
1717 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1719 static int usb_host_scan_sys(void *opaque
, USBScanFunc
*func
)
1723 int bus_num
, addr
, speed
, class_id
, product_id
, vendor_id
;
1725 char port
[MAX_PORTLEN
];
1726 char product_name
[512];
1729 dir
= opendir(USBSYSBUS_PATH
"/devices");
1731 perror("husb: cannot open devices directory");
1735 while ((de
= readdir(dir
))) {
1736 if (de
->d_name
[0] != '.' && !strchr(de
->d_name
, ':')) {
1737 if (sscanf(de
->d_name
, "%d-%7[0-9.]", &bus_num
, port
) < 2) {
1741 if (!usb_host_read_file(line
, sizeof(line
), "devnum", de
->d_name
)) {
1744 if (sscanf(line
, "%d", &addr
) != 1) {
1747 if (!usb_host_read_file(line
, sizeof(line
), "bDeviceClass",
1751 if (sscanf(line
, "%x", &class_id
) != 1) {
1755 if (!usb_host_read_file(line
, sizeof(line
), "idVendor",
1759 if (sscanf(line
, "%x", &vendor_id
) != 1) {
1762 if (!usb_host_read_file(line
, sizeof(line
), "idProduct",
1766 if (sscanf(line
, "%x", &product_id
) != 1) {
1769 if (!usb_host_read_file(line
, sizeof(line
), "product",
1773 if (strlen(line
) > 0) {
1774 line
[strlen(line
) - 1] = '\0';
1776 pstrcpy(product_name
, sizeof(product_name
), line
);
1779 if (!usb_host_read_file(line
, sizeof(line
), "speed", de
->d_name
)) {
1782 if (!strcmp(line
, "5000\n")) {
1783 speed
= USB_SPEED_SUPER
;
1784 } else if (!strcmp(line
, "480\n")) {
1785 speed
= USB_SPEED_HIGH
;
1786 } else if (!strcmp(line
, "1.5\n")) {
1787 speed
= USB_SPEED_LOW
;
1789 speed
= USB_SPEED_FULL
;
1792 ret
= func(opaque
, bus_num
, addr
, port
, class_id
, vendor_id
,
1793 product_id
, product_name
, speed
);
1807 * Determine how to access the host's USB devices and call the
1808 * specific support function.
1810 static int usb_host_scan(void *opaque
, USBScanFunc
*func
)
1812 Monitor
*mon
= cur_mon
;
1816 const char *fs_type
[] = {"unknown", "proc", "dev", "sys"};
1817 char devpath
[PATH_MAX
];
1819 /* only check the host once */
1821 dir
= opendir(USBSYSBUS_PATH
"/devices");
1823 /* devices found in /dev/bus/usb/ (yes - not a mistake!) */
1824 strcpy(devpath
, USBDEVBUS_PATH
);
1825 usb_fs_type
= USB_FS_SYS
;
1827 DPRINTF(USBDBG_DEVOPENED
, USBSYSBUS_PATH
);
1830 f
= fopen(USBPROCBUS_PATH
"/devices", "r");
1832 /* devices found in /proc/bus/usb/ */
1833 strcpy(devpath
, USBPROCBUS_PATH
);
1834 usb_fs_type
= USB_FS_PROC
;
1836 DPRINTF(USBDBG_DEVOPENED
, USBPROCBUS_PATH
);
1839 /* try additional methods if an access method hasn't been found yet */
1840 f
= fopen(USBDEVBUS_PATH
"/devices", "r");
1842 /* devices found in /dev/bus/usb/ */
1843 strcpy(devpath
, USBDEVBUS_PATH
);
1844 usb_fs_type
= USB_FS_DEV
;
1846 DPRINTF(USBDBG_DEVOPENED
, USBDEVBUS_PATH
);
1852 monitor_printf(mon
, "husb: unable to access USB devices\n");
1857 /* the module setting (used later for opening devices) */
1858 usb_host_device_path
= g_malloc0(strlen(devpath
)+1);
1859 strcpy(usb_host_device_path
, devpath
);
1861 monitor_printf(mon
, "husb: using %s file-system with %s\n",
1862 fs_type
[usb_fs_type
], usb_host_device_path
);
1866 switch (usb_fs_type
) {
1869 ret
= usb_host_scan_dev(opaque
, func
);
1872 ret
= usb_host_scan_sys(opaque
, func
);
1881 static QEMUTimer
*usb_auto_timer
;
1883 static int usb_host_auto_scan(void *opaque
, int bus_num
,
1884 int addr
, const char *port
,
1885 int class_id
, int vendor_id
, int product_id
,
1886 const char *product_name
, int speed
)
1888 struct USBAutoFilter
*f
;
1889 struct USBHostDevice
*s
;
1895 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1898 if (f
->bus_num
> 0 && f
->bus_num
!= bus_num
) {
1901 if (f
->addr
> 0 && f
->addr
!= addr
) {
1904 if (f
->port
!= NULL
&& (port
== NULL
|| strcmp(f
->port
, port
) != 0)) {
1908 if (f
->vendor_id
> 0 && f
->vendor_id
!= vendor_id
) {
1912 if (f
->product_id
> 0 && f
->product_id
!= product_id
) {
1915 /* We got a match */
1917 if (s
->errcount
>= 3) {
1921 /* Already attached ? */
1925 DPRINTF("husb: auto open: bus_num %d addr %d\n", bus_num
, addr
);
1927 if (usb_host_open(s
, bus_num
, addr
, port
, product_name
, speed
) < 0) {
1936 static void usb_host_auto_check(void *unused
)
1938 struct USBHostDevice
*s
;
1939 int unconnected
= 0;
1941 usb_host_scan(NULL
, usb_host_auto_scan
);
1943 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1953 if (unconnected
== 0) {
1954 /* nothing to watch */
1955 if (usb_auto_timer
) {
1956 qemu_del_timer(usb_auto_timer
);
1957 trace_usb_host_auto_scan_disabled();
1962 if (!usb_auto_timer
) {
1963 usb_auto_timer
= qemu_new_timer_ms(rt_clock
, usb_host_auto_check
, NULL
);
1964 if (!usb_auto_timer
) {
1967 trace_usb_host_auto_scan_enabled();
1969 qemu_mod_timer(usb_auto_timer
, qemu_get_clock_ms(rt_clock
) + 2000);
1973 * Autoconnect filter
1975 * auto:bus:dev[:vid:pid]
1976 * auto:bus.dev[:vid:pid]
1978 * bus - bus number (dec, * means any)
1979 * dev - device number (dec, * means any)
1980 * vid - vendor id (hex, * means any)
1981 * pid - product id (hex, * means any)
1983 * See 'lsusb' output.
1985 static int parse_filter(const char *spec
, struct USBAutoFilter
*f
)
1987 enum { BUS
, DEV
, VID
, PID
, DONE
};
1988 const char *p
= spec
;
1996 for (i
= BUS
; i
< DONE
; i
++) {
1997 p
= strpbrk(p
, ":.");
2007 case BUS
: f
->bus_num
= strtol(p
, NULL
, 10); break;
2008 case DEV
: f
->addr
= strtol(p
, NULL
, 10); break;
2009 case VID
: f
->vendor_id
= strtol(p
, NULL
, 16); break;
2010 case PID
: f
->product_id
= strtol(p
, NULL
, 16); break;
2015 fprintf(stderr
, "husb: invalid auto filter spec %s\n", spec
);
2022 /**********************/
2023 /* USB host device info */
2025 struct usb_class_info
{
2027 const char *class_name
;
2030 static const struct usb_class_info usb_class_info
[] = {
2031 { USB_CLASS_AUDIO
, "Audio"},
2032 { USB_CLASS_COMM
, "Communication"},
2033 { USB_CLASS_HID
, "HID"},
2034 { USB_CLASS_HUB
, "Hub" },
2035 { USB_CLASS_PHYSICAL
, "Physical" },
2036 { USB_CLASS_PRINTER
, "Printer" },
2037 { USB_CLASS_MASS_STORAGE
, "Storage" },
2038 { USB_CLASS_CDC_DATA
, "Data" },
2039 { USB_CLASS_APP_SPEC
, "Application Specific" },
2040 { USB_CLASS_VENDOR_SPEC
, "Vendor Specific" },
2041 { USB_CLASS_STILL_IMAGE
, "Still Image" },
2042 { USB_CLASS_CSCID
, "Smart Card" },
2043 { USB_CLASS_CONTENT_SEC
, "Content Security" },
2047 static const char *usb_class_str(uint8_t class)
2049 const struct usb_class_info
*p
;
2050 for(p
= usb_class_info
; p
->class != -1; p
++) {
2051 if (p
->class == class) {
2055 return p
->class_name
;
2058 static void usb_info_device(Monitor
*mon
, int bus_num
,
2059 int addr
, const char *port
,
2060 int class_id
, int vendor_id
, int product_id
,
2061 const char *product_name
,
2064 const char *class_str
, *speed_str
;
2070 case USB_SPEED_FULL
:
2073 case USB_SPEED_HIGH
:
2076 case USB_SPEED_SUPER
:
2084 monitor_printf(mon
, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
2085 bus_num
, addr
, port
, speed_str
);
2086 class_str
= usb_class_str(class_id
);
2088 monitor_printf(mon
, " %s:", class_str
);
2090 monitor_printf(mon
, " Class %02x:", class_id
);
2092 monitor_printf(mon
, " USB device %04x:%04x", vendor_id
, product_id
);
2093 if (product_name
[0] != '\0') {
2094 monitor_printf(mon
, ", %s", product_name
);
2096 monitor_printf(mon
, "\n");
2099 static int usb_host_info_device(void *opaque
, int bus_num
, int addr
,
2100 const char *path
, int class_id
,
2101 int vendor_id
, int product_id
,
2102 const char *product_name
,
2105 Monitor
*mon
= opaque
;
2107 usb_info_device(mon
, bus_num
, addr
, path
, class_id
, vendor_id
, product_id
,
2108 product_name
, speed
);
2112 static void dec2str(int val
, char *str
, size_t size
)
2115 snprintf(str
, size
, "*");
2117 snprintf(str
, size
, "%d", val
);
2121 static void hex2str(int val
, char *str
, size_t size
)
2124 snprintf(str
, size
, "*");
2126 snprintf(str
, size
, "%04x", val
);
2130 void usb_host_info(Monitor
*mon
)
2132 struct USBAutoFilter
*f
;
2133 struct USBHostDevice
*s
;
2135 usb_host_scan(mon
, usb_host_info_device
);
2137 if (QTAILQ_EMPTY(&hostdevs
)) {
2141 monitor_printf(mon
, " Auto filters:\n");
2142 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
2143 char bus
[10], addr
[10], vid
[10], pid
[10];
2145 dec2str(f
->bus_num
, bus
, sizeof(bus
));
2146 dec2str(f
->addr
, addr
, sizeof(addr
));
2147 hex2str(f
->vendor_id
, vid
, sizeof(vid
));
2148 hex2str(f
->product_id
, pid
, sizeof(pid
));
2149 monitor_printf(mon
, " Bus %s, Addr %s, Port %s, ID %s:%s\n",
2150 bus
, addr
, f
->port
? f
->port
: "*", vid
, pid
);