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 struct usb_ctrlrequest
{
65 typedef int USBScanFunc(void *opaque
, int bus_num
, int addr
, int devpath
,
66 int class_id
, int vendor_id
, int product_id
,
67 const char *product_name
, int speed
);
72 #define DPRINTF printf
77 #define USBDBG_DEVOPENED "husb: opened %s/devices\n"
79 #define USBPROCBUS_PATH "/proc/bus/usb"
80 #define PRODUCT_NAME_SZ 32
81 #define MAX_ENDPOINTS 16
82 #define USBDEVBUS_PATH "/dev/bus/usb"
83 #define USBSYSBUS_PATH "/sys/bus/usb"
85 static char *usb_host_device_path
;
92 static int usb_fs_type
;
94 /* endpoint association data */
108 * Control transfer state.
109 * Note that 'buffer' _must_ follow 'req' field because
110 * we need contigious buffer when we submit control URB.
116 struct usb_ctrlrequest req
;
117 uint8_t buffer
[8192];
120 struct USBAutoFilter
{
127 typedef struct USBHostDevice
{
138 struct ctrl_struct ctrl
;
139 struct endp_data endp_table
[MAX_ENDPOINTS
];
141 /* Host side address */
145 struct USBAutoFilter match
;
147 QTAILQ_ENTRY(USBHostDevice
) next
;
150 static QTAILQ_HEAD(, USBHostDevice
) hostdevs
= QTAILQ_HEAD_INITIALIZER(hostdevs
);
152 static int usb_host_close(USBHostDevice
*dev
);
153 static int parse_filter(const char *spec
, struct USBAutoFilter
*f
);
154 static void usb_host_auto_check(void *unused
);
155 static int usb_host_read_file(char *line
, size_t line_size
,
156 const char *device_file
, const char *device_name
);
158 static int is_isoc(USBHostDevice
*s
, int ep
)
160 return s
->endp_table
[ep
- 1].type
== USBDEVFS_URB_TYPE_ISO
;
163 static int is_halted(USBHostDevice
*s
, int ep
)
165 return s
->endp_table
[ep
- 1].halted
;
168 static void clear_halt(USBHostDevice
*s
, int ep
)
170 s
->endp_table
[ep
- 1].halted
= 0;
173 static void set_halt(USBHostDevice
*s
, int ep
)
175 s
->endp_table
[ep
- 1].halted
= 1;
180 * We always allocate one isoc descriptor even for bulk transfers
181 * to simplify allocation and casts.
183 typedef struct AsyncURB
185 struct usbdevfs_urb urb
;
186 struct usbdevfs_iso_packet_desc isocpd
;
192 static AsyncURB
*async_alloc(void)
194 return (AsyncURB
*) qemu_mallocz(sizeof(AsyncURB
));
197 static void async_free(AsyncURB
*aurb
)
202 static void async_complete_ctrl(USBHostDevice
*s
, USBPacket
*p
)
204 switch(s
->ctrl
.state
) {
205 case CTRL_STATE_SETUP
:
206 if (p
->len
< s
->ctrl
.len
)
207 s
->ctrl
.len
= p
->len
;
208 s
->ctrl
.state
= CTRL_STATE_DATA
;
213 s
->ctrl
.state
= CTRL_STATE_IDLE
;
222 static void async_complete(void *opaque
)
224 USBHostDevice
*s
= opaque
;
230 int r
= ioctl(s
->fd
, USBDEVFS_REAPURBNDELAY
, &aurb
);
232 if (errno
== EAGAIN
) {
235 if (errno
== ENODEV
&& !s
->closing
) {
236 printf("husb: device %d.%d disconnected\n",
237 s
->bus_num
, s
->addr
);
239 usb_host_auto_check(NULL
);
243 DPRINTF("husb: async. reap urb failed errno %d\n", errno
);
249 DPRINTF("husb: async completed. aurb %p status %d alen %d\n",
250 aurb
, aurb
->urb
.status
, aurb
->urb
.actual_length
);
253 switch (aurb
->urb
.status
) {
255 p
->len
= aurb
->urb
.actual_length
;
256 if (aurb
->urb
.type
== USBDEVFS_URB_TYPE_CONTROL
) {
257 async_complete_ctrl(s
, p
);
262 set_halt(s
, p
->devep
);
263 p
->len
= USB_RET_STALL
;
267 p
->len
= USB_RET_NAK
;
271 usb_packet_complete(p
);
278 static void async_cancel(USBPacket
*unused
, void *opaque
)
280 AsyncURB
*aurb
= opaque
;
281 USBHostDevice
*s
= aurb
->hdev
;
283 DPRINTF("husb: async cancel. aurb %p\n", aurb
);
285 /* Mark it as dead (see async_complete above) */
288 int r
= ioctl(s
->fd
, USBDEVFS_DISCARDURB
, aurb
);
290 DPRINTF("husb: async. discard urb failed errno %d\n", errno
);
294 static int usb_host_claim_interfaces(USBHostDevice
*dev
, int configuration
)
296 int dev_descr_len
, config_descr_len
;
297 int interface
, nb_interfaces
;
300 if (configuration
== 0) /* address state - ignore */
303 DPRINTF("husb: claiming interfaces. config %d\n", configuration
);
306 dev_descr_len
= dev
->descr
[0];
307 if (dev_descr_len
> dev
->descr_len
) {
312 while (i
< dev
->descr_len
) {
313 DPRINTF("husb: i is %d, descr_len is %d, dl %d, dt %d\n",
315 dev
->descr
[i
], dev
->descr
[i
+1]);
317 if (dev
->descr
[i
+1] != USB_DT_CONFIG
) {
321 config_descr_len
= dev
->descr
[i
];
323 printf("husb: config #%d need %d\n", dev
->descr
[i
+ 5], configuration
);
325 if (configuration
< 0 || configuration
== dev
->descr
[i
+ 5]) {
326 configuration
= dev
->descr
[i
+ 5];
330 i
+= config_descr_len
;
333 if (i
>= dev
->descr_len
) {
335 "husb: update iface failed. no matching configuration\n");
338 nb_interfaces
= dev
->descr
[i
+ 4];
340 #ifdef USBDEVFS_DISCONNECT
341 /* earlier Linux 2.4 do not support that */
343 struct usbdevfs_ioctl ctrl
;
344 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
345 ctrl
.ioctl_code
= USBDEVFS_DISCONNECT
;
346 ctrl
.ifno
= interface
;
347 ret
= ioctl(dev
->fd
, USBDEVFS_IOCTL
, &ctrl
);
348 if (ret
< 0 && errno
!= ENODATA
) {
349 perror("USBDEVFS_DISCONNECT");
356 /* XXX: only grab if all interfaces are free */
357 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
358 ret
= ioctl(dev
->fd
, USBDEVFS_CLAIMINTERFACE
, &interface
);
360 if (errno
== EBUSY
) {
361 printf("husb: update iface. device already grabbed\n");
363 perror("husb: failed to claim interface");
370 printf("husb: %d interfaces claimed for configuration %d\n",
371 nb_interfaces
, configuration
);
373 dev
->ninterfaces
= nb_interfaces
;
374 dev
->configuration
= configuration
;
378 static int usb_host_release_interfaces(USBHostDevice
*s
)
382 DPRINTF("husb: releasing interfaces\n");
384 for (i
= 0; i
< s
->ninterfaces
; i
++) {
385 ret
= ioctl(s
->fd
, USBDEVFS_RELEASEINTERFACE
, &i
);
387 perror("husb: failed to release interface");
395 static void usb_host_handle_reset(USBDevice
*dev
)
397 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
399 DPRINTF("husb: reset device %u.%u\n", s
->bus_num
, s
->addr
);
401 ioctl(s
->fd
, USBDEVFS_RESET
);
403 usb_host_claim_interfaces(s
, s
->configuration
);
406 static void usb_host_handle_destroy(USBDevice
*dev
)
408 USBHostDevice
*s
= (USBHostDevice
*)dev
;
411 QTAILQ_REMOVE(&hostdevs
, s
, next
);
412 qemu_remove_exit_notifier(&s
->exit
);
415 static int usb_linux_update_endp_table(USBHostDevice
*s
);
417 static int usb_host_handle_data(USBHostDevice
*s
, USBPacket
*p
)
419 struct usbdevfs_urb
*urb
;
423 aurb
= async_alloc();
429 if (p
->pid
== USB_TOKEN_IN
) {
430 urb
->endpoint
= p
->devep
| 0x80;
432 urb
->endpoint
= p
->devep
;
435 if (is_halted(s
, p
->devep
)) {
436 ret
= ioctl(s
->fd
, USBDEVFS_CLEAR_HALT
, &urb
->endpoint
);
438 DPRINTF("husb: failed to clear halt. ep 0x%x errno %d\n",
439 urb
->endpoint
, errno
);
442 clear_halt(s
, p
->devep
);
445 urb
->buffer
= p
->data
;
446 urb
->buffer_length
= p
->len
;
448 if (is_isoc(s
, p
->devep
)) {
449 /* Setup ISOC transfer */
450 urb
->type
= USBDEVFS_URB_TYPE_ISO
;
451 urb
->flags
= USBDEVFS_URB_ISO_ASAP
;
452 urb
->number_of_packets
= 1;
453 urb
->iso_frame_desc
[0].length
= p
->len
;
455 /* Setup bulk transfer */
456 urb
->type
= USBDEVFS_URB_TYPE_BULK
;
459 urb
->usercontext
= s
;
461 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
463 DPRINTF("husb: data submit. ep 0x%x len %u aurb %p\n",
464 urb
->endpoint
, p
->len
, aurb
);
467 DPRINTF("husb: submit failed. errno %d\n", errno
);
475 return USB_RET_STALL
;
479 usb_defer_packet(p
, async_cancel
, aurb
);
480 return USB_RET_ASYNC
;
483 static int ctrl_error(void)
485 if (errno
== ETIMEDOUT
) {
488 return USB_RET_STALL
;
492 static int usb_host_set_address(USBHostDevice
*s
, int addr
)
494 DPRINTF("husb: ctrl set addr %u\n", addr
);
499 static int usb_host_set_config(USBHostDevice
*s
, int config
)
501 usb_host_release_interfaces(s
);
503 int ret
= ioctl(s
->fd
, USBDEVFS_SETCONFIGURATION
, &config
);
505 DPRINTF("husb: ctrl set config %d ret %d errno %d\n", config
, ret
, errno
);
510 usb_host_claim_interfaces(s
, config
);
514 static int usb_host_set_interface(USBHostDevice
*s
, int iface
, int alt
)
516 struct usbdevfs_setinterface si
;
519 si
.interface
= iface
;
521 ret
= ioctl(s
->fd
, USBDEVFS_SETINTERFACE
, &si
);
523 DPRINTF("husb: ctrl set iface %d altset %d ret %d errno %d\n",
524 iface
, alt
, ret
, errno
);
529 usb_linux_update_endp_table(s
);
533 static int usb_host_handle_control(USBHostDevice
*s
, USBPacket
*p
)
535 struct usbdevfs_urb
*urb
;
537 int ret
, value
, index
;
541 * Process certain standard device requests.
542 * These are infrequent and are processed synchronously.
544 value
= le16_to_cpu(s
->ctrl
.req
.wValue
);
545 index
= le16_to_cpu(s
->ctrl
.req
.wIndex
);
547 DPRINTF("husb: ctrl type 0x%x req 0x%x val 0x%x index %u len %u\n",
548 s
->ctrl
.req
.bRequestType
, s
->ctrl
.req
.bRequest
, value
, index
,
551 if (s
->ctrl
.req
.bRequestType
== 0) {
552 switch (s
->ctrl
.req
.bRequest
) {
553 case USB_REQ_SET_ADDRESS
:
554 return usb_host_set_address(s
, value
);
556 case USB_REQ_SET_CONFIGURATION
:
557 return usb_host_set_config(s
, value
& 0xff);
561 if (s
->ctrl
.req
.bRequestType
== 1 &&
562 s
->ctrl
.req
.bRequest
== USB_REQ_SET_INTERFACE
) {
563 return usb_host_set_interface(s
, index
, value
);
566 /* The rest are asynchronous */
568 buffer_len
= 8 + s
->ctrl
.len
;
569 if (buffer_len
> sizeof(s
->ctrl
.buffer
)) {
570 fprintf(stderr
, "husb: ctrl buffer too small (%u > %zu)\n",
571 buffer_len
, sizeof(s
->ctrl
.buffer
));
572 return USB_RET_STALL
;
575 aurb
= async_alloc();
580 * Setup ctrl transfer.
582 * s->ctrl is layed out such that data buffer immediately follows
583 * 'req' struct which is exactly what usbdevfs expects.
587 urb
->type
= USBDEVFS_URB_TYPE_CONTROL
;
588 urb
->endpoint
= p
->devep
;
590 urb
->buffer
= &s
->ctrl
.req
;
591 urb
->buffer_length
= buffer_len
;
593 urb
->usercontext
= s
;
595 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
597 DPRINTF("husb: submit ctrl. len %u aurb %p\n", urb
->buffer_length
, aurb
);
600 DPRINTF("husb: submit failed. errno %d\n", errno
);
608 return USB_RET_STALL
;
612 usb_defer_packet(p
, async_cancel
, aurb
);
613 return USB_RET_ASYNC
;
616 static int do_token_setup(USBDevice
*dev
, USBPacket
*p
)
618 USBHostDevice
*s
= (USBHostDevice
*) dev
;
622 return USB_RET_STALL
;
625 memcpy(&s
->ctrl
.req
, p
->data
, 8);
626 s
->ctrl
.len
= le16_to_cpu(s
->ctrl
.req
.wLength
);
628 s
->ctrl
.state
= CTRL_STATE_SETUP
;
630 if (s
->ctrl
.req
.bRequestType
& USB_DIR_IN
) {
631 ret
= usb_host_handle_control(s
, p
);
636 if (ret
< s
->ctrl
.len
) {
639 s
->ctrl
.state
= CTRL_STATE_DATA
;
641 if (s
->ctrl
.len
== 0) {
642 s
->ctrl
.state
= CTRL_STATE_ACK
;
644 s
->ctrl
.state
= CTRL_STATE_DATA
;
651 static int do_token_in(USBDevice
*dev
, USBPacket
*p
)
653 USBHostDevice
*s
= (USBHostDevice
*) dev
;
657 return usb_host_handle_data(s
, p
);
660 switch(s
->ctrl
.state
) {
662 if (!(s
->ctrl
.req
.bRequestType
& USB_DIR_IN
)) {
663 ret
= usb_host_handle_control(s
, p
);
664 if (ret
== USB_RET_ASYNC
) {
665 return USB_RET_ASYNC
;
667 s
->ctrl
.state
= CTRL_STATE_IDLE
;
668 return ret
> 0 ? 0 : ret
;
673 case CTRL_STATE_DATA
:
674 if (s
->ctrl
.req
.bRequestType
& USB_DIR_IN
) {
675 int len
= s
->ctrl
.len
- s
->ctrl
.offset
;
679 memcpy(p
->data
, s
->ctrl
.buffer
+ s
->ctrl
.offset
, len
);
680 s
->ctrl
.offset
+= len
;
681 if (s
->ctrl
.offset
>= s
->ctrl
.len
) {
682 s
->ctrl
.state
= CTRL_STATE_ACK
;
687 s
->ctrl
.state
= CTRL_STATE_IDLE
;
688 return USB_RET_STALL
;
691 return USB_RET_STALL
;
695 static int do_token_out(USBDevice
*dev
, USBPacket
*p
)
697 USBHostDevice
*s
= (USBHostDevice
*) dev
;
700 return usb_host_handle_data(s
, p
);
703 switch(s
->ctrl
.state
) {
705 if (s
->ctrl
.req
.bRequestType
& USB_DIR_IN
) {
706 s
->ctrl
.state
= CTRL_STATE_IDLE
;
709 /* ignore additional output */
713 case CTRL_STATE_DATA
:
714 if (!(s
->ctrl
.req
.bRequestType
& USB_DIR_IN
)) {
715 int len
= s
->ctrl
.len
- s
->ctrl
.offset
;
719 memcpy(s
->ctrl
.buffer
+ s
->ctrl
.offset
, p
->data
, len
);
720 s
->ctrl
.offset
+= len
;
721 if (s
->ctrl
.offset
>= s
->ctrl
.len
) {
722 s
->ctrl
.state
= CTRL_STATE_ACK
;
727 s
->ctrl
.state
= CTRL_STATE_IDLE
;
728 return USB_RET_STALL
;
731 return USB_RET_STALL
;
737 * Called by the HC (host controller).
739 * Returns length of the transaction or one of the USB_RET_XXX codes.
741 static int usb_host_handle_packet(USBDevice
*s
, USBPacket
*p
)
745 s
->state
= USB_STATE_ATTACHED
;
749 s
->state
= USB_STATE_NOTATTACHED
;
753 s
->remote_wakeup
= 0;
755 s
->state
= USB_STATE_DEFAULT
;
756 s
->info
->handle_reset(s
);
760 /* Rest of the PIDs must match our address */
761 if (s
->state
< USB_STATE_DEFAULT
|| p
->devaddr
!= s
->addr
) {
762 return USB_RET_NODEV
;
766 case USB_TOKEN_SETUP
:
767 return do_token_setup(s
, p
);
770 return do_token_in(s
, p
);
773 return do_token_out(s
, p
);
776 return USB_RET_STALL
;
780 static int usb_linux_get_configuration(USBHostDevice
*s
)
782 uint8_t configuration
;
783 struct usb_ctrltransfer ct
;
786 if (usb_fs_type
== USB_FS_SYS
) {
787 char device_name
[32], line
[1024];
790 sprintf(device_name
, "%d-%d", s
->bus_num
, s
->devpath
);
792 if (!usb_host_read_file(line
, sizeof(line
), "bConfigurationValue",
796 if (sscanf(line
, "%d", &configuration
) != 1) {
799 return configuration
;
803 ct
.bRequestType
= USB_DIR_IN
;
804 ct
.bRequest
= USB_REQ_GET_CONFIGURATION
;
808 ct
.data
= &configuration
;
811 ret
= ioctl(s
->fd
, USBDEVFS_CONTROL
, &ct
);
813 perror("usb_linux_get_configuration");
817 /* in address state */
818 if (configuration
== 0) {
822 return configuration
;
825 /* returns 1 on problem encountered or 0 for success */
826 static int usb_linux_update_endp_table(USBHostDevice
*s
)
828 uint8_t *descriptors
;
829 uint8_t devep
, type
, configuration
, alt_interface
;
830 struct usb_ctrltransfer ct
;
831 int interface
, ret
, length
, i
;
833 i
= usb_linux_get_configuration(s
);
838 /* get the desired configuration, interface, and endpoint descriptors
839 * from device description */
840 descriptors
= &s
->descr
[18];
841 length
= s
->descr_len
- 18;
844 if (descriptors
[i
+ 1] != USB_DT_CONFIG
||
845 descriptors
[i
+ 5] != configuration
) {
846 DPRINTF("invalid descriptor data - configuration\n");
852 if (descriptors
[i
+ 1] != USB_DT_INTERFACE
||
853 (descriptors
[i
+ 1] == USB_DT_INTERFACE
&&
854 descriptors
[i
+ 4] == 0)) {
859 interface
= descriptors
[i
+ 2];
861 ct
.bRequestType
= USB_DIR_IN
| USB_RECIP_INTERFACE
;
862 ct
.bRequest
= USB_REQ_GET_INTERFACE
;
864 ct
.wIndex
= interface
;
866 ct
.data
= &alt_interface
;
869 ret
= ioctl(s
->fd
, USBDEVFS_CONTROL
, &ct
);
871 alt_interface
= interface
;
874 /* the current interface descriptor is the active interface
875 * and has endpoints */
876 if (descriptors
[i
+ 3] != alt_interface
) {
881 /* advance to the endpoints */
882 while (i
< length
&& descriptors
[i
+1] != USB_DT_ENDPOINT
) {
890 if (descriptors
[i
+ 1] != USB_DT_ENDPOINT
) {
894 devep
= descriptors
[i
+ 2];
895 switch (descriptors
[i
+ 3] & 0x3) {
897 type
= USBDEVFS_URB_TYPE_CONTROL
;
900 type
= USBDEVFS_URB_TYPE_ISO
;
903 type
= USBDEVFS_URB_TYPE_BULK
;
906 type
= USBDEVFS_URB_TYPE_INTERRUPT
;
909 DPRINTF("usb_host: malformed endpoint type\n");
910 type
= USBDEVFS_URB_TYPE_BULK
;
912 s
->endp_table
[(devep
& 0xf) - 1].type
= type
;
913 s
->endp_table
[(devep
& 0xf) - 1].halted
= 0;
921 static int usb_host_open(USBHostDevice
*dev
, int bus_num
,
922 int addr
, int devpath
, const char *prod_name
)
925 struct usbdevfs_connectinfo ci
;
931 printf("husb: open device %d.%d\n", bus_num
, addr
);
933 if (!usb_host_device_path
) {
934 perror("husb: USB Host Device Path not set");
937 snprintf(buf
, sizeof(buf
), "%s/%03d/%03d", usb_host_device_path
,
939 fd
= open(buf
, O_RDWR
| O_NONBLOCK
);
944 DPRINTF("husb: opened %s\n", buf
);
946 dev
->bus_num
= bus_num
;
948 dev
->devpath
= devpath
;
951 /* read the device description */
952 dev
->descr_len
= read(fd
, dev
->descr
, sizeof(dev
->descr
));
953 if (dev
->descr_len
<= 0) {
954 perror("husb: reading device data failed");
961 printf("=== begin dumping device descriptor data ===\n");
962 for (x
= 0; x
< dev
->descr_len
; x
++) {
963 printf("%02x ", dev
->descr
[x
]);
965 printf("\n=== end dumping device descriptor data ===\n");
971 * Initial configuration is -1 which makes us claim first
972 * available config. We used to start with 1, which does not
973 * always work. I've seen devices where first config starts
976 if (!usb_host_claim_interfaces(dev
, -1)) {
980 ret
= ioctl(fd
, USBDEVFS_CONNECTINFO
, &ci
);
982 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
986 printf("husb: grabbed usb device %d.%d\n", bus_num
, addr
);
988 ret
= usb_linux_update_endp_table(dev
);
994 dev
->dev
.speed
= USB_SPEED_LOW
;
996 dev
->dev
.speed
= USB_SPEED_HIGH
;
999 if (!prod_name
|| prod_name
[0] == '\0') {
1000 snprintf(dev
->dev
.product_desc
, sizeof(dev
->dev
.product_desc
),
1001 "host:%d.%d", bus_num
, addr
);
1003 pstrcpy(dev
->dev
.product_desc
, sizeof(dev
->dev
.product_desc
),
1007 /* USB devio uses 'write' flag to check for async completions */
1008 qemu_set_fd_handler(dev
->fd
, NULL
, async_complete
, dev
);
1010 usb_device_attach(&dev
->dev
);
1021 static int usb_host_close(USBHostDevice
*dev
)
1023 if (dev
->fd
== -1) {
1027 qemu_set_fd_handler(dev
->fd
, NULL
, NULL
, NULL
);
1029 async_complete(dev
);
1031 usb_device_detach(&dev
->dev
);
1032 ioctl(dev
->fd
, USBDEVFS_RESET
);
1038 static void usb_host_exit_notifier(struct Notifier
* n
)
1040 USBHostDevice
*s
= container_of(n
, USBHostDevice
, exit
);
1043 ioctl(s
->fd
, USBDEVFS_RESET
);
1047 static int usb_host_initfn(USBDevice
*dev
)
1049 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
1051 dev
->auto_attach
= 0;
1053 QTAILQ_INSERT_TAIL(&hostdevs
, s
, next
);
1054 s
->exit
.notify
= usb_host_exit_notifier
;
1055 qemu_add_exit_notifier(&s
->exit
);
1056 usb_host_auto_check(NULL
);
1060 static struct USBDeviceInfo usb_host_dev_info
= {
1061 .product_desc
= "USB Host Device",
1062 .qdev
.name
= "usb-host",
1063 .qdev
.size
= sizeof(USBHostDevice
),
1064 .init
= usb_host_initfn
,
1065 .handle_packet
= usb_host_handle_packet
,
1066 .handle_reset
= usb_host_handle_reset
,
1067 .handle_destroy
= usb_host_handle_destroy
,
1068 .usbdevice_name
= "host",
1069 .usbdevice_init
= usb_host_device_open
,
1070 .qdev
.props
= (Property
[]) {
1071 DEFINE_PROP_UINT32("hostbus", USBHostDevice
, match
.bus_num
, 0),
1072 DEFINE_PROP_UINT32("hostaddr", USBHostDevice
, match
.addr
, 0),
1073 DEFINE_PROP_HEX32("vendorid", USBHostDevice
, match
.vendor_id
, 0),
1074 DEFINE_PROP_HEX32("productid", USBHostDevice
, match
.product_id
, 0),
1075 DEFINE_PROP_END_OF_LIST(),
1079 static void usb_host_register_devices(void)
1081 usb_qdev_register(&usb_host_dev_info
);
1083 device_init(usb_host_register_devices
)
1085 USBDevice
*usb_host_device_open(const char *devname
)
1087 struct USBAutoFilter filter
;
1091 dev
= usb_create(NULL
/* FIXME */, "usb-host");
1093 if (strstr(devname
, "auto:")) {
1094 if (parse_filter(devname
, &filter
) < 0) {
1098 if ((p
= strchr(devname
, '.'))) {
1099 filter
.bus_num
= strtoul(devname
, NULL
, 0);
1100 filter
.addr
= strtoul(p
+ 1, NULL
, 0);
1101 filter
.vendor_id
= 0;
1102 filter
.product_id
= 0;
1103 } else if ((p
= strchr(devname
, ':'))) {
1106 filter
.vendor_id
= strtoul(devname
, NULL
, 16);
1107 filter
.product_id
= strtoul(p
+ 1, NULL
, 16);
1113 qdev_prop_set_uint32(&dev
->qdev
, "hostbus", filter
.bus_num
);
1114 qdev_prop_set_uint32(&dev
->qdev
, "hostaddr", filter
.addr
);
1115 qdev_prop_set_uint32(&dev
->qdev
, "vendorid", filter
.vendor_id
);
1116 qdev_prop_set_uint32(&dev
->qdev
, "productid", filter
.product_id
);
1117 qdev_init_nofail(&dev
->qdev
);
1121 qdev_free(&dev
->qdev
);
1125 int usb_host_device_close(const char *devname
)
1128 char product_name
[PRODUCT_NAME_SZ
];
1132 if (strstr(devname
, "auto:")) {
1133 return usb_host_auto_del(devname
);
1135 if (usb_host_find_device(&bus_num
, &addr
, product_name
,
1136 sizeof(product_name
), devname
) < 0) {
1139 s
= hostdev_find(bus_num
, addr
);
1141 usb_device_delete_addr(s
->bus_num
, s
->dev
.addr
);
1149 static int get_tag_value(char *buf
, int buf_size
,
1150 const char *str
, const char *tag
,
1151 const char *stopchars
)
1155 p
= strstr(str
, tag
);
1160 while (qemu_isspace(*p
)) {
1164 while (*p
!= '\0' && !strchr(stopchars
, *p
)) {
1165 if ((q
- buf
) < (buf_size
- 1)) {
1175 * Use /proc/bus/usb/devices or /dev/bus/usb/devices file to determine
1176 * host's USB devices. This is legacy support since many distributions
1177 * are moving to /sys/bus/usb
1179 static int usb_host_scan_dev(void *opaque
, USBScanFunc
*func
)
1184 int bus_num
, addr
, speed
, device_count
, class_id
, product_id
, vendor_id
;
1185 char product_name
[512];
1188 if (!usb_host_device_path
) {
1189 perror("husb: USB Host Device Path not set");
1192 snprintf(line
, sizeof(line
), "%s/devices", usb_host_device_path
);
1193 f
= fopen(line
, "r");
1195 perror("husb: cannot open devices file");
1200 bus_num
= addr
= speed
= class_id
= product_id
= vendor_id
= 0;
1202 if (fgets(line
, sizeof(line
), f
) == NULL
) {
1205 if (strlen(line
) > 0) {
1206 line
[strlen(line
) - 1] = '\0';
1208 if (line
[0] == 'T' && line
[1] == ':') {
1209 if (device_count
&& (vendor_id
|| product_id
)) {
1210 /* New device. Add the previously discovered device. */
1211 ret
= func(opaque
, bus_num
, addr
, 0, class_id
, vendor_id
,
1212 product_id
, product_name
, speed
);
1217 if (get_tag_value(buf
, sizeof(buf
), line
, "Bus=", " ") < 0) {
1220 bus_num
= atoi(buf
);
1221 if (get_tag_value(buf
, sizeof(buf
), line
, "Dev#=", " ") < 0) {
1225 if (get_tag_value(buf
, sizeof(buf
), line
, "Spd=", " ") < 0) {
1228 if (!strcmp(buf
, "480")) {
1229 speed
= USB_SPEED_HIGH
;
1230 } else if (!strcmp(buf
, "1.5")) {
1231 speed
= USB_SPEED_LOW
;
1233 speed
= USB_SPEED_FULL
;
1235 product_name
[0] = '\0';
1240 } else if (line
[0] == 'P' && line
[1] == ':') {
1241 if (get_tag_value(buf
, sizeof(buf
), line
, "Vendor=", " ") < 0) {
1244 vendor_id
= strtoul(buf
, NULL
, 16);
1245 if (get_tag_value(buf
, sizeof(buf
), line
, "ProdID=", " ") < 0) {
1248 product_id
= strtoul(buf
, NULL
, 16);
1249 } else if (line
[0] == 'S' && line
[1] == ':') {
1250 if (get_tag_value(buf
, sizeof(buf
), line
, "Product=", "") < 0) {
1253 pstrcpy(product_name
, sizeof(product_name
), buf
);
1254 } else if (line
[0] == 'D' && line
[1] == ':') {
1255 if (get_tag_value(buf
, sizeof(buf
), line
, "Cls=", " (") < 0) {
1258 class_id
= strtoul(buf
, NULL
, 16);
1262 if (device_count
&& (vendor_id
|| product_id
)) {
1263 /* Add the last device. */
1264 ret
= func(opaque
, bus_num
, addr
, 0, class_id
, vendor_id
,
1265 product_id
, product_name
, speed
);
1275 * Read sys file-system device file
1277 * @line address of buffer to put file contents in
1278 * @line_size size of line
1279 * @device_file path to device file (printf format string)
1280 * @device_name device being opened (inserted into device_file)
1282 * @return 0 failed, 1 succeeded ('line' contains data)
1284 static int usb_host_read_file(char *line
, size_t line_size
,
1285 const char *device_file
, const char *device_name
)
1289 char filename
[PATH_MAX
];
1291 snprintf(filename
, PATH_MAX
, USBSYSBUS_PATH
"/devices/%s/%s", device_name
,
1293 f
= fopen(filename
, "r");
1295 ret
= fgets(line
, line_size
, f
) != NULL
;
1303 * Use /sys/bus/usb/devices/ directory to determine host's USB
1306 * This code is based on Robert Schiele's original patches posted to
1307 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1309 static int usb_host_scan_sys(void *opaque
, USBScanFunc
*func
)
1313 int bus_num
, addr
, devpath
, speed
, class_id
, product_id
, vendor_id
;
1315 char product_name
[512];
1318 dir
= opendir(USBSYSBUS_PATH
"/devices");
1320 perror("husb: cannot open devices directory");
1324 while ((de
= readdir(dir
))) {
1325 if (de
->d_name
[0] != '.' && !strchr(de
->d_name
, ':')) {
1326 char *tmpstr
= de
->d_name
;
1327 if (!strncmp(de
->d_name
, "usb", 3)) {
1330 if (sscanf(tmpstr
, "%d-%d", &bus_num
, &devpath
) < 1) {
1334 if (!usb_host_read_file(line
, sizeof(line
), "devnum", de
->d_name
)) {
1337 if (sscanf(line
, "%d", &addr
) != 1) {
1340 if (!usb_host_read_file(line
, sizeof(line
), "bDeviceClass",
1344 if (sscanf(line
, "%x", &class_id
) != 1) {
1348 if (!usb_host_read_file(line
, sizeof(line
), "idVendor",
1352 if (sscanf(line
, "%x", &vendor_id
) != 1) {
1355 if (!usb_host_read_file(line
, sizeof(line
), "idProduct",
1359 if (sscanf(line
, "%x", &product_id
) != 1) {
1362 if (!usb_host_read_file(line
, sizeof(line
), "product",
1366 if (strlen(line
) > 0) {
1367 line
[strlen(line
) - 1] = '\0';
1369 pstrcpy(product_name
, sizeof(product_name
), line
);
1372 if (!usb_host_read_file(line
, sizeof(line
), "speed", de
->d_name
)) {
1375 if (!strcmp(line
, "480\n")) {
1376 speed
= USB_SPEED_HIGH
;
1377 } else if (!strcmp(line
, "1.5\n")) {
1378 speed
= USB_SPEED_LOW
;
1380 speed
= USB_SPEED_FULL
;
1383 ret
= func(opaque
, bus_num
, addr
, devpath
, class_id
, vendor_id
,
1384 product_id
, product_name
, speed
);
1398 * Determine how to access the host's USB devices and call the
1399 * specific support function.
1401 static int usb_host_scan(void *opaque
, USBScanFunc
*func
)
1403 Monitor
*mon
= cur_mon
;
1407 const char *fs_type
[] = {"unknown", "proc", "dev", "sys"};
1408 char devpath
[PATH_MAX
];
1410 /* only check the host once */
1412 dir
= opendir(USBSYSBUS_PATH
"/devices");
1414 /* devices found in /dev/bus/usb/ (yes - not a mistake!) */
1415 strcpy(devpath
, USBDEVBUS_PATH
);
1416 usb_fs_type
= USB_FS_SYS
;
1418 DPRINTF(USBDBG_DEVOPENED
, USBSYSBUS_PATH
);
1421 f
= fopen(USBPROCBUS_PATH
"/devices", "r");
1423 /* devices found in /proc/bus/usb/ */
1424 strcpy(devpath
, USBPROCBUS_PATH
);
1425 usb_fs_type
= USB_FS_PROC
;
1427 DPRINTF(USBDBG_DEVOPENED
, USBPROCBUS_PATH
);
1430 /* try additional methods if an access method hasn't been found yet */
1431 f
= fopen(USBDEVBUS_PATH
"/devices", "r");
1433 /* devices found in /dev/bus/usb/ */
1434 strcpy(devpath
, USBDEVBUS_PATH
);
1435 usb_fs_type
= USB_FS_DEV
;
1437 DPRINTF(USBDBG_DEVOPENED
, USBDEVBUS_PATH
);
1443 monitor_printf(mon
, "husb: unable to access USB devices\n");
1448 /* the module setting (used later for opening devices) */
1449 usb_host_device_path
= qemu_mallocz(strlen(devpath
)+1);
1450 strcpy(usb_host_device_path
, devpath
);
1452 monitor_printf(mon
, "husb: using %s file-system with %s\n",
1453 fs_type
[usb_fs_type
], usb_host_device_path
);
1457 switch (usb_fs_type
) {
1460 ret
= usb_host_scan_dev(opaque
, func
);
1463 ret
= usb_host_scan_sys(opaque
, func
);
1472 static QEMUTimer
*usb_auto_timer
;
1474 static int usb_host_auto_scan(void *opaque
, int bus_num
, int addr
, int devpath
,
1475 int class_id
, int vendor_id
, int product_id
,
1476 const char *product_name
, int speed
)
1478 struct USBAutoFilter
*f
;
1479 struct USBHostDevice
*s
;
1485 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1488 if (f
->bus_num
> 0 && f
->bus_num
!= bus_num
) {
1491 if (f
->addr
> 0 && f
->addr
!= addr
) {
1495 if (f
->vendor_id
> 0 && f
->vendor_id
!= vendor_id
) {
1499 if (f
->product_id
> 0 && f
->product_id
!= product_id
) {
1502 /* We got a match */
1504 /* Already attached ? */
1508 DPRINTF("husb: auto open: bus_num %d addr %d\n", bus_num
, addr
);
1510 usb_host_open(s
, bus_num
, addr
, devpath
, product_name
);
1516 static void usb_host_auto_check(void *unused
)
1518 struct USBHostDevice
*s
;
1519 int unconnected
= 0;
1521 usb_host_scan(NULL
, usb_host_auto_scan
);
1523 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1529 if (unconnected
== 0) {
1530 /* nothing to watch */
1531 if (usb_auto_timer
) {
1532 qemu_del_timer(usb_auto_timer
);
1537 if (!usb_auto_timer
) {
1538 usb_auto_timer
= qemu_new_timer_ms(rt_clock
, usb_host_auto_check
, NULL
);
1539 if (!usb_auto_timer
) {
1543 qemu_mod_timer(usb_auto_timer
, qemu_get_clock_ms(rt_clock
) + 2000);
1547 * Autoconnect filter
1549 * auto:bus:dev[:vid:pid]
1550 * auto:bus.dev[:vid:pid]
1552 * bus - bus number (dec, * means any)
1553 * dev - device number (dec, * means any)
1554 * vid - vendor id (hex, * means any)
1555 * pid - product id (hex, * means any)
1557 * See 'lsusb' output.
1559 static int parse_filter(const char *spec
, struct USBAutoFilter
*f
)
1561 enum { BUS
, DEV
, VID
, PID
, DONE
};
1562 const char *p
= spec
;
1570 for (i
= BUS
; i
< DONE
; i
++) {
1571 p
= strpbrk(p
, ":.");
1581 case BUS
: f
->bus_num
= strtol(p
, NULL
, 10); break;
1582 case DEV
: f
->addr
= strtol(p
, NULL
, 10); break;
1583 case VID
: f
->vendor_id
= strtol(p
, NULL
, 16); break;
1584 case PID
: f
->product_id
= strtol(p
, NULL
, 16); break;
1589 fprintf(stderr
, "husb: invalid auto filter spec %s\n", spec
);
1596 /**********************/
1597 /* USB host device info */
1599 struct usb_class_info
{
1601 const char *class_name
;
1604 static const struct usb_class_info usb_class_info
[] = {
1605 { USB_CLASS_AUDIO
, "Audio"},
1606 { USB_CLASS_COMM
, "Communication"},
1607 { USB_CLASS_HID
, "HID"},
1608 { USB_CLASS_HUB
, "Hub" },
1609 { USB_CLASS_PHYSICAL
, "Physical" },
1610 { USB_CLASS_PRINTER
, "Printer" },
1611 { USB_CLASS_MASS_STORAGE
, "Storage" },
1612 { USB_CLASS_CDC_DATA
, "Data" },
1613 { USB_CLASS_APP_SPEC
, "Application Specific" },
1614 { USB_CLASS_VENDOR_SPEC
, "Vendor Specific" },
1615 { USB_CLASS_STILL_IMAGE
, "Still Image" },
1616 { USB_CLASS_CSCID
, "Smart Card" },
1617 { USB_CLASS_CONTENT_SEC
, "Content Security" },
1621 static const char *usb_class_str(uint8_t class)
1623 const struct usb_class_info
*p
;
1624 for(p
= usb_class_info
; p
->class != -1; p
++) {
1625 if (p
->class == class) {
1629 return p
->class_name
;
1632 static void usb_info_device(Monitor
*mon
, int bus_num
, int addr
, int class_id
,
1633 int vendor_id
, int product_id
,
1634 const char *product_name
,
1637 const char *class_str
, *speed_str
;
1643 case USB_SPEED_FULL
:
1646 case USB_SPEED_HIGH
:
1654 monitor_printf(mon
, " Device %d.%d, speed %s Mb/s\n",
1655 bus_num
, addr
, speed_str
);
1656 class_str
= usb_class_str(class_id
);
1658 monitor_printf(mon
, " %s:", class_str
);
1660 monitor_printf(mon
, " Class %02x:", class_id
);
1662 monitor_printf(mon
, " USB device %04x:%04x", vendor_id
, product_id
);
1663 if (product_name
[0] != '\0') {
1664 monitor_printf(mon
, ", %s", product_name
);
1666 monitor_printf(mon
, "\n");
1669 static int usb_host_info_device(void *opaque
, int bus_num
, int addr
,
1670 int devpath
, int class_id
,
1671 int vendor_id
, int product_id
,
1672 const char *product_name
,
1675 Monitor
*mon
= opaque
;
1677 usb_info_device(mon
, bus_num
, addr
, class_id
, vendor_id
, product_id
,
1678 product_name
, speed
);
1682 static void dec2str(int val
, char *str
, size_t size
)
1685 snprintf(str
, size
, "*");
1687 snprintf(str
, size
, "%d", val
);
1691 static void hex2str(int val
, char *str
, size_t size
)
1694 snprintf(str
, size
, "*");
1696 snprintf(str
, size
, "%04x", val
);
1700 void usb_host_info(Monitor
*mon
)
1702 struct USBAutoFilter
*f
;
1703 struct USBHostDevice
*s
;
1705 usb_host_scan(mon
, usb_host_info_device
);
1707 if (QTAILQ_EMPTY(&hostdevs
)) {
1711 monitor_printf(mon
, " Auto filters:\n");
1712 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1713 char bus
[10], addr
[10], vid
[10], pid
[10];
1715 dec2str(f
->bus_num
, bus
, sizeof(bus
));
1716 dec2str(f
->addr
, addr
, sizeof(addr
));
1717 hex2str(f
->vendor_id
, vid
, sizeof(vid
));
1718 hex2str(f
->product_id
, pid
, sizeof(pid
));
1719 monitor_printf(mon
, " Device %s.%s ID %s:%s\n",
1720 bus
, addr
, vid
, pid
);