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"
39 #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
, 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 ISO_URB_COUNT 3
90 #define INVALID_EP_TYPE 255
92 typedef struct AsyncURB AsyncURB
;
104 struct USBAutoFilter
{
112 typedef struct USBHostDevice
{
123 struct endp_data endp_table
[MAX_ENDPOINTS
];
125 /* Host side address */
128 char port
[MAX_PORTLEN
];
129 struct USBAutoFilter match
;
131 QTAILQ_ENTRY(USBHostDevice
) next
;
134 static QTAILQ_HEAD(, USBHostDevice
) hostdevs
= QTAILQ_HEAD_INITIALIZER(hostdevs
);
136 static int usb_host_close(USBHostDevice
*dev
);
137 static int parse_filter(const char *spec
, struct USBAutoFilter
*f
);
138 static void usb_host_auto_check(void *unused
);
139 static int usb_host_read_file(char *line
, size_t line_size
,
140 const char *device_file
, const char *device_name
);
142 static int is_isoc(USBHostDevice
*s
, int ep
)
144 return s
->endp_table
[ep
- 1].type
== USBDEVFS_URB_TYPE_ISO
;
147 static int is_valid(USBHostDevice
*s
, int ep
)
149 return s
->endp_table
[ep
- 1].type
!= INVALID_EP_TYPE
;
152 static int is_halted(USBHostDevice
*s
, int ep
)
154 return s
->endp_table
[ep
- 1].halted
;
157 static void clear_halt(USBHostDevice
*s
, int ep
)
159 s
->endp_table
[ep
- 1].halted
= 0;
162 static void set_halt(USBHostDevice
*s
, int ep
)
164 s
->endp_table
[ep
- 1].halted
= 1;
167 static int is_iso_started(USBHostDevice
*s
, int ep
)
169 return s
->endp_table
[ep
- 1].iso_started
;
172 static void clear_iso_started(USBHostDevice
*s
, int ep
)
174 s
->endp_table
[ep
- 1].iso_started
= 0;
177 static void set_iso_started(USBHostDevice
*s
, int ep
)
179 s
->endp_table
[ep
- 1].iso_started
= 1;
182 static void set_iso_urb(USBHostDevice
*s
, int ep
, AsyncURB
*iso_urb
)
184 s
->endp_table
[ep
- 1].iso_urb
= iso_urb
;
187 static AsyncURB
*get_iso_urb(USBHostDevice
*s
, int ep
)
189 return s
->endp_table
[ep
- 1].iso_urb
;
192 static void set_iso_urb_idx(USBHostDevice
*s
, int ep
, int i
)
194 s
->endp_table
[ep
- 1].iso_urb_idx
= i
;
197 static int get_iso_urb_idx(USBHostDevice
*s
, int ep
)
199 return s
->endp_table
[ep
- 1].iso_urb_idx
;
202 static void set_iso_buffer_used(USBHostDevice
*s
, int ep
, int i
)
204 s
->endp_table
[ep
- 1].iso_buffer_used
= i
;
207 static int get_iso_buffer_used(USBHostDevice
*s
, int ep
)
209 return s
->endp_table
[ep
- 1].iso_buffer_used
;
212 static int get_max_packet_size(USBHostDevice
*s
, int ep
)
214 return s
->endp_table
[ep
- 1].max_packet_size
;
219 * We always allocate iso packet descriptors even for bulk transfers
220 * to simplify allocation and casts.
224 struct usbdevfs_urb urb
;
225 struct usbdevfs_iso_packet_desc isocpd
[ISO_FRAME_DESC_PER_URB
];
227 /* For regular async urbs */
231 /* For buffered iso handling */
232 int iso_frame_idx
; /* -1 means in flight */
235 static AsyncURB
*async_alloc(void)
237 return (AsyncURB
*) qemu_mallocz(sizeof(AsyncURB
));
240 static void async_free(AsyncURB
*aurb
)
245 static void async_complete(void *opaque
)
247 USBHostDevice
*s
= opaque
;
253 int r
= ioctl(s
->fd
, USBDEVFS_REAPURBNDELAY
, &aurb
);
255 if (errno
== EAGAIN
) {
258 if (errno
== ENODEV
&& !s
->closing
) {
259 printf("husb: device %d.%d disconnected\n",
260 s
->bus_num
, s
->addr
);
262 usb_host_auto_check(NULL
);
266 DPRINTF("husb: async. reap urb failed errno %d\n", errno
);
270 DPRINTF("husb: async completed. aurb %p status %d alen %d\n",
271 aurb
, aurb
->urb
.status
, aurb
->urb
.actual_length
);
273 /* If this is a buffered iso urb mark it as complete and don't do
274 anything else (it is handled further in usb_host_handle_iso_data) */
275 if (aurb
->iso_frame_idx
== -1) {
276 if (aurb
->urb
.status
== -EPIPE
) {
277 set_halt(s
, aurb
->urb
.endpoint
& 0xf);
279 aurb
->iso_frame_idx
= 0;
286 switch (aurb
->urb
.status
) {
288 p
->len
= aurb
->urb
.actual_length
;
292 set_halt(s
, p
->devep
);
293 p
->len
= USB_RET_STALL
;
297 p
->len
= USB_RET_NAK
;
301 if (aurb
->urb
.type
== USBDEVFS_URB_TYPE_CONTROL
) {
302 usb_generic_async_ctrl_complete(&s
->dev
, p
);
304 usb_packet_complete(&s
->dev
, p
);
312 static void async_cancel(USBPacket
*unused
, void *opaque
)
314 AsyncURB
*aurb
= opaque
;
315 USBHostDevice
*s
= aurb
->hdev
;
317 DPRINTF("husb: async cancel. aurb %p\n", aurb
);
319 /* Mark it as dead (see async_complete above) */
322 int r
= ioctl(s
->fd
, USBDEVFS_DISCARDURB
, aurb
);
324 DPRINTF("husb: async. discard urb failed errno %d\n", errno
);
328 static int usb_host_claim_interfaces(USBHostDevice
*dev
, int configuration
)
330 int dev_descr_len
, config_descr_len
;
331 int interface
, nb_interfaces
;
334 if (configuration
== 0) /* address state - ignore */
337 DPRINTF("husb: claiming interfaces. config %d\n", configuration
);
340 dev_descr_len
= dev
->descr
[0];
341 if (dev_descr_len
> dev
->descr_len
) {
346 while (i
< dev
->descr_len
) {
347 DPRINTF("husb: i is %d, descr_len is %d, dl %d, dt %d\n",
349 dev
->descr
[i
], dev
->descr
[i
+1]);
351 if (dev
->descr
[i
+1] != USB_DT_CONFIG
) {
355 config_descr_len
= dev
->descr
[i
];
357 printf("husb: config #%d need %d\n", dev
->descr
[i
+ 5], configuration
);
359 if (configuration
< 0 || configuration
== dev
->descr
[i
+ 5]) {
360 configuration
= dev
->descr
[i
+ 5];
364 i
+= config_descr_len
;
367 if (i
>= dev
->descr_len
) {
369 "husb: update iface failed. no matching configuration\n");
372 nb_interfaces
= dev
->descr
[i
+ 4];
374 #ifdef USBDEVFS_DISCONNECT
375 /* earlier Linux 2.4 do not support that */
377 struct usbdevfs_ioctl ctrl
;
378 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
379 ctrl
.ioctl_code
= USBDEVFS_DISCONNECT
;
380 ctrl
.ifno
= interface
;
382 ret
= ioctl(dev
->fd
, USBDEVFS_IOCTL
, &ctrl
);
383 if (ret
< 0 && errno
!= ENODATA
) {
384 perror("USBDEVFS_DISCONNECT");
391 /* XXX: only grab if all interfaces are free */
392 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
393 ret
= ioctl(dev
->fd
, USBDEVFS_CLAIMINTERFACE
, &interface
);
395 if (errno
== EBUSY
) {
396 printf("husb: update iface. device already grabbed\n");
398 perror("husb: failed to claim interface");
405 printf("husb: %d interfaces claimed for configuration %d\n",
406 nb_interfaces
, configuration
);
408 dev
->ninterfaces
= nb_interfaces
;
409 dev
->configuration
= configuration
;
413 static int usb_host_release_interfaces(USBHostDevice
*s
)
417 DPRINTF("husb: releasing interfaces\n");
419 for (i
= 0; i
< s
->ninterfaces
; i
++) {
420 ret
= ioctl(s
->fd
, USBDEVFS_RELEASEINTERFACE
, &i
);
422 perror("husb: failed to release interface");
430 static void usb_host_handle_reset(USBDevice
*dev
)
432 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
434 DPRINTF("husb: reset device %u.%u\n", s
->bus_num
, s
->addr
);
436 ioctl(s
->fd
, USBDEVFS_RESET
);
438 usb_host_claim_interfaces(s
, s
->configuration
);
441 static void usb_host_handle_destroy(USBDevice
*dev
)
443 USBHostDevice
*s
= (USBHostDevice
*)dev
;
446 QTAILQ_REMOVE(&hostdevs
, s
, next
);
447 qemu_remove_exit_notifier(&s
->exit
);
450 static int usb_linux_update_endp_table(USBHostDevice
*s
);
452 /* iso data is special, we need to keep enough urbs in flight to make sure
453 that the controller never runs out of them, otherwise the device will
454 likely suffer a buffer underrun / overrun. */
455 static AsyncURB
*usb_host_alloc_iso(USBHostDevice
*s
, uint8_t ep
, int in
)
458 int i
, j
, len
= get_max_packet_size(s
, ep
);
460 aurb
= qemu_mallocz(ISO_URB_COUNT
* sizeof(*aurb
));
461 for (i
= 0; i
< ISO_URB_COUNT
; i
++) {
462 aurb
[i
].urb
.endpoint
= ep
;
463 aurb
[i
].urb
.buffer_length
= ISO_FRAME_DESC_PER_URB
* len
;
464 aurb
[i
].urb
.buffer
= qemu_malloc(aurb
[i
].urb
.buffer_length
);
465 aurb
[i
].urb
.type
= USBDEVFS_URB_TYPE_ISO
;
466 aurb
[i
].urb
.flags
= USBDEVFS_URB_ISO_ASAP
;
467 aurb
[i
].urb
.number_of_packets
= ISO_FRAME_DESC_PER_URB
;
468 for (j
= 0 ; j
< ISO_FRAME_DESC_PER_URB
; j
++)
469 aurb
[i
].urb
.iso_frame_desc
[j
].length
= len
;
471 aurb
[i
].urb
.endpoint
|= 0x80;
472 /* Mark as fully consumed (idle) */
473 aurb
[i
].iso_frame_idx
= ISO_FRAME_DESC_PER_URB
;
476 set_iso_urb(s
, ep
, aurb
);
481 static void usb_host_stop_n_free_iso(USBHostDevice
*s
, uint8_t ep
)
484 int i
, ret
, killed
= 0, free
= 1;
486 aurb
= get_iso_urb(s
, ep
);
491 for (i
= 0; i
< ISO_URB_COUNT
; i
++) {
493 if (aurb
[i
].iso_frame_idx
== -1) {
494 ret
= ioctl(s
->fd
, USBDEVFS_DISCARDURB
, &aurb
[i
]);
496 printf("husb: discard isoc in urb failed errno %d\n", errno
);
504 /* Make sure any urbs we've killed are reaped before we free them */
509 for (i
= 0; i
< ISO_URB_COUNT
; i
++) {
510 qemu_free(aurb
[i
].urb
.buffer
);
516 printf("husb: leaking iso urbs because of discard failure\n");
517 set_iso_urb(s
, ep
, NULL
);
518 set_iso_urb_idx(s
, ep
, 0);
519 clear_iso_started(s
, ep
);
522 static int urb_status_to_usb_ret(int status
)
526 return USB_RET_STALL
;
532 static int usb_host_handle_iso_data(USBHostDevice
*s
, USBPacket
*p
, int in
)
535 int i
, j
, ret
, max_packet_size
, offset
, len
= 0;
537 max_packet_size
= get_max_packet_size(s
, p
->devep
);
538 if (max_packet_size
== 0)
541 aurb
= get_iso_urb(s
, p
->devep
);
543 aurb
= usb_host_alloc_iso(s
, p
->devep
, in
);
546 i
= get_iso_urb_idx(s
, p
->devep
);
547 j
= aurb
[i
].iso_frame_idx
;
548 if (j
>= 0 && j
< ISO_FRAME_DESC_PER_URB
) {
550 /* Check urb status */
551 if (aurb
[i
].urb
.status
) {
552 len
= urb_status_to_usb_ret(aurb
[i
].urb
.status
);
553 /* Move to the next urb */
554 aurb
[i
].iso_frame_idx
= ISO_FRAME_DESC_PER_URB
- 1;
555 /* Check frame status */
556 } else if (aurb
[i
].urb
.iso_frame_desc
[j
].status
) {
557 len
= urb_status_to_usb_ret(
558 aurb
[i
].urb
.iso_frame_desc
[j
].status
);
559 /* Check the frame fits */
560 } else if (aurb
[i
].urb
.iso_frame_desc
[j
].actual_length
> p
->len
) {
561 printf("husb: received iso data is larger then packet\n");
563 /* All good copy data over */
565 len
= aurb
[i
].urb
.iso_frame_desc
[j
].actual_length
;
568 j
* aurb
[i
].urb
.iso_frame_desc
[0].length
,
573 offset
= (j
== 0) ? 0 : get_iso_buffer_used(s
, p
->devep
);
575 /* Check the frame fits */
576 if (len
> max_packet_size
) {
577 printf("husb: send iso data is larger then max packet size\n");
581 /* All good copy data over */
582 memcpy(aurb
[i
].urb
.buffer
+ offset
, p
->data
, len
);
583 aurb
[i
].urb
.iso_frame_desc
[j
].length
= len
;
585 set_iso_buffer_used(s
, p
->devep
, offset
);
587 /* Start the stream once we have buffered enough data */
588 if (!is_iso_started(s
, p
->devep
) && i
== 1 && j
== 8) {
589 set_iso_started(s
, p
->devep
);
592 aurb
[i
].iso_frame_idx
++;
593 if (aurb
[i
].iso_frame_idx
== ISO_FRAME_DESC_PER_URB
) {
594 i
= (i
+ 1) % ISO_URB_COUNT
;
595 set_iso_urb_idx(s
, p
->devep
, i
);
599 set_iso_started(s
, p
->devep
);
601 DPRINTF("hubs: iso out error no free buffer, dropping packet\n");
605 if (is_iso_started(s
, p
->devep
)) {
606 /* (Re)-submit all fully consumed / filled urbs */
607 for (i
= 0; i
< ISO_URB_COUNT
; i
++) {
608 if (aurb
[i
].iso_frame_idx
== ISO_FRAME_DESC_PER_URB
) {
609 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, &aurb
[i
]);
611 printf("husb error submitting iso urb %d: %d\n", i
, errno
);
612 if (!in
|| len
== 0) {
624 aurb
[i
].iso_frame_idx
= -1;
632 static int usb_host_handle_data(USBDevice
*dev
, USBPacket
*p
)
634 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
635 struct usbdevfs_urb
*urb
;
640 if (!is_valid(s
, p
->devep
)) {
644 if (p
->pid
== USB_TOKEN_IN
) {
645 ep
= p
->devep
| 0x80;
650 if (is_halted(s
, p
->devep
)) {
651 ret
= ioctl(s
->fd
, USBDEVFS_CLEAR_HALT
, &ep
);
653 DPRINTF("husb: failed to clear halt. ep 0x%x errno %d\n",
657 clear_halt(s
, p
->devep
);
660 if (is_isoc(s
, p
->devep
)) {
661 return usb_host_handle_iso_data(s
, p
, p
->pid
== USB_TOKEN_IN
);
664 aurb
= async_alloc();
671 urb
->buffer
= p
->data
;
672 urb
->buffer_length
= p
->len
;
673 urb
->type
= USBDEVFS_URB_TYPE_BULK
;
674 urb
->usercontext
= s
;
676 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
678 DPRINTF("husb: data submit. ep 0x%x len %u aurb %p\n",
679 urb
->endpoint
, p
->len
, aurb
);
682 DPRINTF("husb: submit failed. errno %d\n", errno
);
690 return USB_RET_STALL
;
694 usb_defer_packet(p
, async_cancel
, aurb
);
695 return USB_RET_ASYNC
;
698 static int ctrl_error(void)
700 if (errno
== ETIMEDOUT
) {
703 return USB_RET_STALL
;
707 static int usb_host_set_address(USBHostDevice
*s
, int addr
)
709 DPRINTF("husb: ctrl set addr %u\n", addr
);
714 static int usb_host_set_config(USBHostDevice
*s
, int config
)
716 usb_host_release_interfaces(s
);
718 int ret
= ioctl(s
->fd
, USBDEVFS_SETCONFIGURATION
, &config
);
720 DPRINTF("husb: ctrl set config %d ret %d errno %d\n", config
, ret
, errno
);
725 usb_host_claim_interfaces(s
, config
);
729 static int usb_host_set_interface(USBHostDevice
*s
, int iface
, int alt
)
731 struct usbdevfs_setinterface si
;
734 for (i
= 1; i
<= MAX_ENDPOINTS
; i
++) {
736 usb_host_stop_n_free_iso(s
, i
);
740 si
.interface
= iface
;
742 ret
= ioctl(s
->fd
, USBDEVFS_SETINTERFACE
, &si
);
744 DPRINTF("husb: ctrl set iface %d altset %d ret %d errno %d\n",
745 iface
, alt
, ret
, errno
);
750 usb_linux_update_endp_table(s
);
754 static int usb_host_handle_control(USBDevice
*dev
, USBPacket
*p
,
755 int request
, int value
, int index
, int length
, uint8_t *data
)
757 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
758 struct usbdevfs_urb
*urb
;
763 * Process certain standard device requests.
764 * These are infrequent and are processed synchronously.
767 /* Note request is (bRequestType << 8) | bRequest */
768 DPRINTF("husb: ctrl type 0x%x req 0x%x val 0x%x index %u len %u\n",
769 request
>> 8, request
& 0xff, value
, index
, length
);
772 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
773 return usb_host_set_address(s
, value
);
775 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
776 return usb_host_set_config(s
, value
& 0xff);
778 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
779 return usb_host_set_interface(s
, index
, value
);
782 /* The rest are asynchronous */
784 if (length
> sizeof(dev
->data_buf
)) {
785 fprintf(stderr
, "husb: ctrl buffer too small (%d > %zu)\n",
786 length
, sizeof(dev
->data_buf
));
787 return USB_RET_STALL
;
790 aurb
= async_alloc();
795 * Setup ctrl transfer.
797 * s->ctrl is laid out such that data buffer immediately follows
798 * 'req' struct which is exactly what usbdevfs expects.
802 urb
->type
= USBDEVFS_URB_TYPE_CONTROL
;
803 urb
->endpoint
= p
->devep
;
805 urb
->buffer
= &dev
->setup_buf
;
806 urb
->buffer_length
= length
+ 8;
808 urb
->usercontext
= s
;
810 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
812 DPRINTF("husb: submit ctrl. len %u aurb %p\n", urb
->buffer_length
, aurb
);
815 DPRINTF("husb: submit failed. errno %d\n", errno
);
823 return USB_RET_STALL
;
827 usb_defer_packet(p
, async_cancel
, aurb
);
828 return USB_RET_ASYNC
;
831 static int usb_linux_get_configuration(USBHostDevice
*s
)
833 uint8_t configuration
;
834 struct usb_ctrltransfer ct
;
837 if (usb_fs_type
== USB_FS_SYS
) {
838 char device_name
[32], line
[1024];
841 sprintf(device_name
, "%d-%s", s
->bus_num
, s
->port
);
843 if (!usb_host_read_file(line
, sizeof(line
), "bConfigurationValue",
847 if (sscanf(line
, "%d", &configuration
) != 1) {
850 return configuration
;
854 ct
.bRequestType
= USB_DIR_IN
;
855 ct
.bRequest
= USB_REQ_GET_CONFIGURATION
;
859 ct
.data
= &configuration
;
862 ret
= ioctl(s
->fd
, USBDEVFS_CONTROL
, &ct
);
864 perror("usb_linux_get_configuration");
868 /* in address state */
869 if (configuration
== 0) {
873 return configuration
;
876 static uint8_t usb_linux_get_alt_setting(USBHostDevice
*s
,
877 uint8_t configuration
, uint8_t interface
)
880 struct usb_ctrltransfer ct
;
883 if (usb_fs_type
== USB_FS_SYS
) {
884 char device_name
[64], line
[1024];
887 sprintf(device_name
, "%d-%s:%d.%d", s
->bus_num
, s
->port
,
888 (int)configuration
, (int)interface
);
890 if (!usb_host_read_file(line
, sizeof(line
), "bAlternateSetting",
894 if (sscanf(line
, "%d", &alt_setting
) != 1) {
901 ct
.bRequestType
= USB_DIR_IN
| USB_RECIP_INTERFACE
;
902 ct
.bRequest
= USB_REQ_GET_INTERFACE
;
904 ct
.wIndex
= interface
;
906 ct
.data
= &alt_setting
;
908 ret
= ioctl(s
->fd
, USBDEVFS_CONTROL
, &ct
);
910 /* Assume alt 0 on error */
917 /* returns 1 on problem encountered or 0 for success */
918 static int usb_linux_update_endp_table(USBHostDevice
*s
)
920 uint8_t *descriptors
;
921 uint8_t devep
, type
, configuration
, alt_interface
;
922 int interface
, length
, i
;
924 for (i
= 0; i
< MAX_ENDPOINTS
; i
++)
925 s
->endp_table
[i
].type
= INVALID_EP_TYPE
;
927 i
= usb_linux_get_configuration(s
);
932 /* get the desired configuration, interface, and endpoint descriptors
933 * from device description */
934 descriptors
= &s
->descr
[18];
935 length
= s
->descr_len
- 18;
938 if (descriptors
[i
+ 1] != USB_DT_CONFIG
||
939 descriptors
[i
+ 5] != configuration
) {
940 DPRINTF("invalid descriptor data - configuration\n");
946 if (descriptors
[i
+ 1] != USB_DT_INTERFACE
||
947 (descriptors
[i
+ 1] == USB_DT_INTERFACE
&&
948 descriptors
[i
+ 4] == 0)) {
953 interface
= descriptors
[i
+ 2];
954 alt_interface
= usb_linux_get_alt_setting(s
, configuration
, interface
);
956 /* the current interface descriptor is the active interface
957 * and has endpoints */
958 if (descriptors
[i
+ 3] != alt_interface
) {
963 /* advance to the endpoints */
964 while (i
< length
&& descriptors
[i
+1] != USB_DT_ENDPOINT
) {
972 if (descriptors
[i
+ 1] != USB_DT_ENDPOINT
) {
976 devep
= descriptors
[i
+ 2];
977 switch (descriptors
[i
+ 3] & 0x3) {
979 type
= USBDEVFS_URB_TYPE_CONTROL
;
982 type
= USBDEVFS_URB_TYPE_ISO
;
983 s
->endp_table
[(devep
& 0xf) - 1].max_packet_size
=
984 descriptors
[i
+ 4] + (descriptors
[i
+ 5] << 8);
987 type
= USBDEVFS_URB_TYPE_BULK
;
990 type
= USBDEVFS_URB_TYPE_INTERRUPT
;
993 DPRINTF("usb_host: malformed endpoint type\n");
994 type
= USBDEVFS_URB_TYPE_BULK
;
996 s
->endp_table
[(devep
& 0xf) - 1].type
= type
;
997 s
->endp_table
[(devep
& 0xf) - 1].halted
= 0;
1005 static int usb_host_open(USBHostDevice
*dev
, int bus_num
,
1006 int addr
, char *port
, const char *prod_name
)
1009 struct usbdevfs_connectinfo ci
;
1012 if (dev
->fd
!= -1) {
1015 printf("husb: open device %d.%d\n", bus_num
, addr
);
1017 if (!usb_host_device_path
) {
1018 perror("husb: USB Host Device Path not set");
1021 snprintf(buf
, sizeof(buf
), "%s/%03d/%03d", usb_host_device_path
,
1023 fd
= open(buf
, O_RDWR
| O_NONBLOCK
);
1028 DPRINTF("husb: opened %s\n", buf
);
1030 dev
->bus_num
= bus_num
;
1032 strcpy(dev
->port
, port
);
1035 /* read the device description */
1036 dev
->descr_len
= read(fd
, dev
->descr
, sizeof(dev
->descr
));
1037 if (dev
->descr_len
<= 0) {
1038 perror("husb: reading device data failed");
1045 printf("=== begin dumping device descriptor data ===\n");
1046 for (x
= 0; x
< dev
->descr_len
; x
++) {
1047 printf("%02x ", dev
->descr
[x
]);
1049 printf("\n=== end dumping device descriptor data ===\n");
1055 * Initial configuration is -1 which makes us claim first
1056 * available config. We used to start with 1, which does not
1057 * always work. I've seen devices where first config starts
1060 if (!usb_host_claim_interfaces(dev
, -1)) {
1064 ret
= ioctl(fd
, USBDEVFS_CONNECTINFO
, &ci
);
1066 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
1070 printf("husb: grabbed usb device %d.%d\n", bus_num
, addr
);
1072 ret
= usb_linux_update_endp_table(dev
);
1078 dev
->dev
.speed
= USB_SPEED_LOW
;
1080 dev
->dev
.speed
= USB_SPEED_HIGH
;
1083 if (!prod_name
|| prod_name
[0] == '\0') {
1084 snprintf(dev
->dev
.product_desc
, sizeof(dev
->dev
.product_desc
),
1085 "host:%d.%d", bus_num
, addr
);
1087 pstrcpy(dev
->dev
.product_desc
, sizeof(dev
->dev
.product_desc
),
1091 /* USB devio uses 'write' flag to check for async completions */
1092 qemu_set_fd_handler(dev
->fd
, NULL
, async_complete
, dev
);
1094 usb_device_attach(&dev
->dev
);
1105 static int usb_host_close(USBHostDevice
*dev
)
1109 if (dev
->fd
== -1) {
1113 qemu_set_fd_handler(dev
->fd
, NULL
, NULL
, NULL
);
1115 for (i
= 1; i
<= MAX_ENDPOINTS
; i
++) {
1116 if (is_isoc(dev
, i
)) {
1117 usb_host_stop_n_free_iso(dev
, i
);
1120 async_complete(dev
);
1122 usb_device_detach(&dev
->dev
);
1123 ioctl(dev
->fd
, USBDEVFS_RESET
);
1129 static void usb_host_exit_notifier(struct Notifier
* n
)
1131 USBHostDevice
*s
= container_of(n
, USBHostDevice
, exit
);
1134 ioctl(s
->fd
, USBDEVFS_RESET
);
1138 static int usb_host_initfn(USBDevice
*dev
)
1140 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
1142 dev
->auto_attach
= 0;
1144 QTAILQ_INSERT_TAIL(&hostdevs
, s
, next
);
1145 s
->exit
.notify
= usb_host_exit_notifier
;
1146 qemu_add_exit_notifier(&s
->exit
);
1147 usb_host_auto_check(NULL
);
1151 static struct USBDeviceInfo usb_host_dev_info
= {
1152 .product_desc
= "USB Host Device",
1153 .qdev
.name
= "usb-host",
1154 .qdev
.size
= sizeof(USBHostDevice
),
1155 .init
= usb_host_initfn
,
1156 .handle_packet
= usb_generic_handle_packet
,
1157 .handle_data
= usb_host_handle_data
,
1158 .handle_control
= usb_host_handle_control
,
1159 .handle_reset
= usb_host_handle_reset
,
1160 .handle_destroy
= usb_host_handle_destroy
,
1161 .usbdevice_name
= "host",
1162 .usbdevice_init
= usb_host_device_open
,
1163 .qdev
.props
= (Property
[]) {
1164 DEFINE_PROP_UINT32("hostbus", USBHostDevice
, match
.bus_num
, 0),
1165 DEFINE_PROP_UINT32("hostaddr", USBHostDevice
, match
.addr
, 0),
1166 DEFINE_PROP_STRING("hostport", USBHostDevice
, match
.port
),
1167 DEFINE_PROP_HEX32("vendorid", USBHostDevice
, match
.vendor_id
, 0),
1168 DEFINE_PROP_HEX32("productid", USBHostDevice
, match
.product_id
, 0),
1169 DEFINE_PROP_END_OF_LIST(),
1173 static void usb_host_register_devices(void)
1175 usb_qdev_register(&usb_host_dev_info
);
1177 device_init(usb_host_register_devices
)
1179 USBDevice
*usb_host_device_open(const char *devname
)
1181 struct USBAutoFilter filter
;
1185 dev
= usb_create(NULL
/* FIXME */, "usb-host");
1187 if (strstr(devname
, "auto:")) {
1188 if (parse_filter(devname
, &filter
) < 0) {
1192 if ((p
= strchr(devname
, '.'))) {
1193 filter
.bus_num
= strtoul(devname
, NULL
, 0);
1194 filter
.addr
= strtoul(p
+ 1, NULL
, 0);
1195 filter
.vendor_id
= 0;
1196 filter
.product_id
= 0;
1197 } else if ((p
= strchr(devname
, ':'))) {
1200 filter
.vendor_id
= strtoul(devname
, NULL
, 16);
1201 filter
.product_id
= strtoul(p
+ 1, NULL
, 16);
1207 qdev_prop_set_uint32(&dev
->qdev
, "hostbus", filter
.bus_num
);
1208 qdev_prop_set_uint32(&dev
->qdev
, "hostaddr", filter
.addr
);
1209 qdev_prop_set_uint32(&dev
->qdev
, "vendorid", filter
.vendor_id
);
1210 qdev_prop_set_uint32(&dev
->qdev
, "productid", filter
.product_id
);
1211 qdev_init_nofail(&dev
->qdev
);
1215 qdev_free(&dev
->qdev
);
1219 int usb_host_device_close(const char *devname
)
1222 char product_name
[PRODUCT_NAME_SZ
];
1226 if (strstr(devname
, "auto:")) {
1227 return usb_host_auto_del(devname
);
1229 if (usb_host_find_device(&bus_num
, &addr
, product_name
,
1230 sizeof(product_name
), devname
) < 0) {
1233 s
= hostdev_find(bus_num
, addr
);
1235 usb_device_delete_addr(s
->bus_num
, s
->dev
.addr
);
1243 static int get_tag_value(char *buf
, int buf_size
,
1244 const char *str
, const char *tag
,
1245 const char *stopchars
)
1249 p
= strstr(str
, tag
);
1254 while (qemu_isspace(*p
)) {
1258 while (*p
!= '\0' && !strchr(stopchars
, *p
)) {
1259 if ((q
- buf
) < (buf_size
- 1)) {
1269 * Use /proc/bus/usb/devices or /dev/bus/usb/devices file to determine
1270 * host's USB devices. This is legacy support since many distributions
1271 * are moving to /sys/bus/usb
1273 static int usb_host_scan_dev(void *opaque
, USBScanFunc
*func
)
1278 int bus_num
, addr
, speed
, device_count
, class_id
, product_id
, vendor_id
;
1279 char product_name
[512];
1282 if (!usb_host_device_path
) {
1283 perror("husb: USB Host Device Path not set");
1286 snprintf(line
, sizeof(line
), "%s/devices", usb_host_device_path
);
1287 f
= fopen(line
, "r");
1289 perror("husb: cannot open devices file");
1294 bus_num
= addr
= speed
= class_id
= product_id
= vendor_id
= 0;
1296 if (fgets(line
, sizeof(line
), f
) == NULL
) {
1299 if (strlen(line
) > 0) {
1300 line
[strlen(line
) - 1] = '\0';
1302 if (line
[0] == 'T' && line
[1] == ':') {
1303 if (device_count
&& (vendor_id
|| product_id
)) {
1304 /* New device. Add the previously discovered device. */
1305 ret
= func(opaque
, bus_num
, addr
, 0, class_id
, vendor_id
,
1306 product_id
, product_name
, speed
);
1311 if (get_tag_value(buf
, sizeof(buf
), line
, "Bus=", " ") < 0) {
1314 bus_num
= atoi(buf
);
1315 if (get_tag_value(buf
, sizeof(buf
), line
, "Dev#=", " ") < 0) {
1319 if (get_tag_value(buf
, sizeof(buf
), line
, "Spd=", " ") < 0) {
1322 if (!strcmp(buf
, "480")) {
1323 speed
= USB_SPEED_HIGH
;
1324 } else if (!strcmp(buf
, "1.5")) {
1325 speed
= USB_SPEED_LOW
;
1327 speed
= USB_SPEED_FULL
;
1329 product_name
[0] = '\0';
1334 } else if (line
[0] == 'P' && line
[1] == ':') {
1335 if (get_tag_value(buf
, sizeof(buf
), line
, "Vendor=", " ") < 0) {
1338 vendor_id
= strtoul(buf
, NULL
, 16);
1339 if (get_tag_value(buf
, sizeof(buf
), line
, "ProdID=", " ") < 0) {
1342 product_id
= strtoul(buf
, NULL
, 16);
1343 } else if (line
[0] == 'S' && line
[1] == ':') {
1344 if (get_tag_value(buf
, sizeof(buf
), line
, "Product=", "") < 0) {
1347 pstrcpy(product_name
, sizeof(product_name
), buf
);
1348 } else if (line
[0] == 'D' && line
[1] == ':') {
1349 if (get_tag_value(buf
, sizeof(buf
), line
, "Cls=", " (") < 0) {
1352 class_id
= strtoul(buf
, NULL
, 16);
1356 if (device_count
&& (vendor_id
|| product_id
)) {
1357 /* Add the last device. */
1358 ret
= func(opaque
, bus_num
, addr
, 0, class_id
, vendor_id
,
1359 product_id
, product_name
, speed
);
1369 * Read sys file-system device file
1371 * @line address of buffer to put file contents in
1372 * @line_size size of line
1373 * @device_file path to device file (printf format string)
1374 * @device_name device being opened (inserted into device_file)
1376 * @return 0 failed, 1 succeeded ('line' contains data)
1378 static int usb_host_read_file(char *line
, size_t line_size
,
1379 const char *device_file
, const char *device_name
)
1383 char filename
[PATH_MAX
];
1385 snprintf(filename
, PATH_MAX
, USBSYSBUS_PATH
"/devices/%s/%s", device_name
,
1387 f
= fopen(filename
, "r");
1389 ret
= fgets(line
, line_size
, f
) != NULL
;
1397 * Use /sys/bus/usb/devices/ directory to determine host's USB
1400 * This code is based on Robert Schiele's original patches posted to
1401 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1403 static int usb_host_scan_sys(void *opaque
, USBScanFunc
*func
)
1407 int bus_num
, addr
, speed
, class_id
, product_id
, vendor_id
;
1409 char port
[MAX_PORTLEN
];
1410 char product_name
[512];
1413 dir
= opendir(USBSYSBUS_PATH
"/devices");
1415 perror("husb: cannot open devices directory");
1419 while ((de
= readdir(dir
))) {
1420 if (de
->d_name
[0] != '.' && !strchr(de
->d_name
, ':')) {
1421 if (sscanf(de
->d_name
, "%d-%7[0-9.]", &bus_num
, port
) < 2) {
1425 if (!usb_host_read_file(line
, sizeof(line
), "devnum", de
->d_name
)) {
1428 if (sscanf(line
, "%d", &addr
) != 1) {
1431 if (!usb_host_read_file(line
, sizeof(line
), "bDeviceClass",
1435 if (sscanf(line
, "%x", &class_id
) != 1) {
1439 if (!usb_host_read_file(line
, sizeof(line
), "idVendor",
1443 if (sscanf(line
, "%x", &vendor_id
) != 1) {
1446 if (!usb_host_read_file(line
, sizeof(line
), "idProduct",
1450 if (sscanf(line
, "%x", &product_id
) != 1) {
1453 if (!usb_host_read_file(line
, sizeof(line
), "product",
1457 if (strlen(line
) > 0) {
1458 line
[strlen(line
) - 1] = '\0';
1460 pstrcpy(product_name
, sizeof(product_name
), line
);
1463 if (!usb_host_read_file(line
, sizeof(line
), "speed", de
->d_name
)) {
1466 if (!strcmp(line
, "480\n")) {
1467 speed
= USB_SPEED_HIGH
;
1468 } else if (!strcmp(line
, "1.5\n")) {
1469 speed
= USB_SPEED_LOW
;
1471 speed
= USB_SPEED_FULL
;
1474 ret
= func(opaque
, bus_num
, addr
, port
, class_id
, vendor_id
,
1475 product_id
, product_name
, speed
);
1489 * Determine how to access the host's USB devices and call the
1490 * specific support function.
1492 static int usb_host_scan(void *opaque
, USBScanFunc
*func
)
1494 Monitor
*mon
= cur_mon
;
1498 const char *fs_type
[] = {"unknown", "proc", "dev", "sys"};
1499 char devpath
[PATH_MAX
];
1501 /* only check the host once */
1503 dir
= opendir(USBSYSBUS_PATH
"/devices");
1505 /* devices found in /dev/bus/usb/ (yes - not a mistake!) */
1506 strcpy(devpath
, USBDEVBUS_PATH
);
1507 usb_fs_type
= USB_FS_SYS
;
1509 DPRINTF(USBDBG_DEVOPENED
, USBSYSBUS_PATH
);
1512 f
= fopen(USBPROCBUS_PATH
"/devices", "r");
1514 /* devices found in /proc/bus/usb/ */
1515 strcpy(devpath
, USBPROCBUS_PATH
);
1516 usb_fs_type
= USB_FS_PROC
;
1518 DPRINTF(USBDBG_DEVOPENED
, USBPROCBUS_PATH
);
1521 /* try additional methods if an access method hasn't been found yet */
1522 f
= fopen(USBDEVBUS_PATH
"/devices", "r");
1524 /* devices found in /dev/bus/usb/ */
1525 strcpy(devpath
, USBDEVBUS_PATH
);
1526 usb_fs_type
= USB_FS_DEV
;
1528 DPRINTF(USBDBG_DEVOPENED
, USBDEVBUS_PATH
);
1534 monitor_printf(mon
, "husb: unable to access USB devices\n");
1539 /* the module setting (used later for opening devices) */
1540 usb_host_device_path
= qemu_mallocz(strlen(devpath
)+1);
1541 strcpy(usb_host_device_path
, devpath
);
1543 monitor_printf(mon
, "husb: using %s file-system with %s\n",
1544 fs_type
[usb_fs_type
], usb_host_device_path
);
1548 switch (usb_fs_type
) {
1551 ret
= usb_host_scan_dev(opaque
, func
);
1554 ret
= usb_host_scan_sys(opaque
, func
);
1563 static QEMUTimer
*usb_auto_timer
;
1565 static int usb_host_auto_scan(void *opaque
, int bus_num
, int addr
, char *port
,
1566 int class_id
, int vendor_id
, int product_id
,
1567 const char *product_name
, int speed
)
1569 struct USBAutoFilter
*f
;
1570 struct USBHostDevice
*s
;
1576 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1579 if (f
->bus_num
> 0 && f
->bus_num
!= bus_num
) {
1582 if (f
->addr
> 0 && f
->addr
!= addr
) {
1585 if (f
->port
!= NULL
&& (port
== NULL
|| strcmp(f
->port
, port
) != 0)) {
1589 if (f
->vendor_id
> 0 && f
->vendor_id
!= vendor_id
) {
1593 if (f
->product_id
> 0 && f
->product_id
!= product_id
) {
1596 /* We got a match */
1598 /* Already attached ? */
1602 DPRINTF("husb: auto open: bus_num %d addr %d\n", bus_num
, addr
);
1604 usb_host_open(s
, bus_num
, addr
, port
, product_name
);
1610 static void usb_host_auto_check(void *unused
)
1612 struct USBHostDevice
*s
;
1613 int unconnected
= 0;
1615 usb_host_scan(NULL
, usb_host_auto_scan
);
1617 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1623 if (unconnected
== 0) {
1624 /* nothing to watch */
1625 if (usb_auto_timer
) {
1626 qemu_del_timer(usb_auto_timer
);
1631 if (!usb_auto_timer
) {
1632 usb_auto_timer
= qemu_new_timer_ms(rt_clock
, usb_host_auto_check
, NULL
);
1633 if (!usb_auto_timer
) {
1637 qemu_mod_timer(usb_auto_timer
, qemu_get_clock_ms(rt_clock
) + 2000);
1641 * Autoconnect filter
1643 * auto:bus:dev[:vid:pid]
1644 * auto:bus.dev[:vid:pid]
1646 * bus - bus number (dec, * means any)
1647 * dev - device number (dec, * means any)
1648 * vid - vendor id (hex, * means any)
1649 * pid - product id (hex, * means any)
1651 * See 'lsusb' output.
1653 static int parse_filter(const char *spec
, struct USBAutoFilter
*f
)
1655 enum { BUS
, DEV
, VID
, PID
, DONE
};
1656 const char *p
= spec
;
1664 for (i
= BUS
; i
< DONE
; i
++) {
1665 p
= strpbrk(p
, ":.");
1675 case BUS
: f
->bus_num
= strtol(p
, NULL
, 10); break;
1676 case DEV
: f
->addr
= strtol(p
, NULL
, 10); break;
1677 case VID
: f
->vendor_id
= strtol(p
, NULL
, 16); break;
1678 case PID
: f
->product_id
= strtol(p
, NULL
, 16); break;
1683 fprintf(stderr
, "husb: invalid auto filter spec %s\n", spec
);
1690 /**********************/
1691 /* USB host device info */
1693 struct usb_class_info
{
1695 const char *class_name
;
1698 static const struct usb_class_info usb_class_info
[] = {
1699 { USB_CLASS_AUDIO
, "Audio"},
1700 { USB_CLASS_COMM
, "Communication"},
1701 { USB_CLASS_HID
, "HID"},
1702 { USB_CLASS_HUB
, "Hub" },
1703 { USB_CLASS_PHYSICAL
, "Physical" },
1704 { USB_CLASS_PRINTER
, "Printer" },
1705 { USB_CLASS_MASS_STORAGE
, "Storage" },
1706 { USB_CLASS_CDC_DATA
, "Data" },
1707 { USB_CLASS_APP_SPEC
, "Application Specific" },
1708 { USB_CLASS_VENDOR_SPEC
, "Vendor Specific" },
1709 { USB_CLASS_STILL_IMAGE
, "Still Image" },
1710 { USB_CLASS_CSCID
, "Smart Card" },
1711 { USB_CLASS_CONTENT_SEC
, "Content Security" },
1715 static const char *usb_class_str(uint8_t class)
1717 const struct usb_class_info
*p
;
1718 for(p
= usb_class_info
; p
->class != -1; p
++) {
1719 if (p
->class == class) {
1723 return p
->class_name
;
1726 static void usb_info_device(Monitor
*mon
, int bus_num
, int addr
, char *port
,
1727 int class_id
, int vendor_id
, int product_id
,
1728 const char *product_name
,
1731 const char *class_str
, *speed_str
;
1737 case USB_SPEED_FULL
:
1740 case USB_SPEED_HIGH
:
1748 monitor_printf(mon
, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
1749 bus_num
, addr
, port
, speed_str
);
1750 class_str
= usb_class_str(class_id
);
1752 monitor_printf(mon
, " %s:", class_str
);
1754 monitor_printf(mon
, " Class %02x:", class_id
);
1756 monitor_printf(mon
, " USB device %04x:%04x", vendor_id
, product_id
);
1757 if (product_name
[0] != '\0') {
1758 monitor_printf(mon
, ", %s", product_name
);
1760 monitor_printf(mon
, "\n");
1763 static int usb_host_info_device(void *opaque
, int bus_num
, int addr
,
1764 char *path
, int class_id
,
1765 int vendor_id
, int product_id
,
1766 const char *product_name
,
1769 Monitor
*mon
= opaque
;
1771 usb_info_device(mon
, bus_num
, addr
, path
, class_id
, vendor_id
, product_id
,
1772 product_name
, speed
);
1776 static void dec2str(int val
, char *str
, size_t size
)
1779 snprintf(str
, size
, "*");
1781 snprintf(str
, size
, "%d", val
);
1785 static void hex2str(int val
, char *str
, size_t size
)
1788 snprintf(str
, size
, "*");
1790 snprintf(str
, size
, "%04x", val
);
1794 void usb_host_info(Monitor
*mon
)
1796 struct USBAutoFilter
*f
;
1797 struct USBHostDevice
*s
;
1799 usb_host_scan(mon
, usb_host_info_device
);
1801 if (QTAILQ_EMPTY(&hostdevs
)) {
1805 monitor_printf(mon
, " Auto filters:\n");
1806 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1807 char bus
[10], addr
[10], vid
[10], pid
[10];
1809 dec2str(f
->bus_num
, bus
, sizeof(bus
));
1810 dec2str(f
->addr
, addr
, sizeof(addr
));
1811 hex2str(f
->vendor_id
, vid
, sizeof(vid
));
1812 hex2str(f
->product_id
, pid
, sizeof(pid
));
1813 monitor_printf(mon
, " Bus %s, Addr %s, Port %s, ID %s:%s\n",
1814 bus
, addr
, f
->port
? f
->port
: "*", vid
, pid
);