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
);
156 static int is_isoc(USBHostDevice
*s
, int ep
)
158 return s
->endp_table
[ep
- 1].type
== USBDEVFS_URB_TYPE_ISO
;
161 static int is_halted(USBHostDevice
*s
, int ep
)
163 return s
->endp_table
[ep
- 1].halted
;
166 static void clear_halt(USBHostDevice
*s
, int ep
)
168 s
->endp_table
[ep
- 1].halted
= 0;
171 static void set_halt(USBHostDevice
*s
, int ep
)
173 s
->endp_table
[ep
- 1].halted
= 1;
178 * We always allocate one isoc descriptor even for bulk transfers
179 * to simplify allocation and casts.
181 typedef struct AsyncURB
183 struct usbdevfs_urb urb
;
184 struct usbdevfs_iso_packet_desc isocpd
;
190 static AsyncURB
*async_alloc(void)
192 return (AsyncURB
*) qemu_mallocz(sizeof(AsyncURB
));
195 static void async_free(AsyncURB
*aurb
)
200 static void async_complete_ctrl(USBHostDevice
*s
, USBPacket
*p
)
202 switch(s
->ctrl
.state
) {
203 case CTRL_STATE_SETUP
:
204 if (p
->len
< s
->ctrl
.len
)
205 s
->ctrl
.len
= p
->len
;
206 s
->ctrl
.state
= CTRL_STATE_DATA
;
211 s
->ctrl
.state
= CTRL_STATE_IDLE
;
220 static void async_complete(void *opaque
)
222 USBHostDevice
*s
= opaque
;
228 int r
= ioctl(s
->fd
, USBDEVFS_REAPURBNDELAY
, &aurb
);
230 if (errno
== EAGAIN
) {
233 if (errno
== ENODEV
&& !s
->closing
) {
234 printf("husb: device %d.%d disconnected\n",
235 s
->bus_num
, s
->addr
);
237 usb_host_auto_check(NULL
);
241 DPRINTF("husb: async. reap urb failed errno %d\n", errno
);
247 DPRINTF("husb: async completed. aurb %p status %d alen %d\n",
248 aurb
, aurb
->urb
.status
, aurb
->urb
.actual_length
);
251 switch (aurb
->urb
.status
) {
253 p
->len
= aurb
->urb
.actual_length
;
254 if (aurb
->urb
.type
== USBDEVFS_URB_TYPE_CONTROL
) {
255 async_complete_ctrl(s
, p
);
260 set_halt(s
, p
->devep
);
261 p
->len
= USB_RET_STALL
;
265 p
->len
= USB_RET_NAK
;
269 usb_packet_complete(p
);
276 static void async_cancel(USBPacket
*unused
, void *opaque
)
278 AsyncURB
*aurb
= opaque
;
279 USBHostDevice
*s
= aurb
->hdev
;
281 DPRINTF("husb: async cancel. aurb %p\n", aurb
);
283 /* Mark it as dead (see async_complete above) */
286 int r
= ioctl(s
->fd
, USBDEVFS_DISCARDURB
, aurb
);
288 DPRINTF("husb: async. discard urb failed errno %d\n", errno
);
292 static int usb_host_claim_interfaces(USBHostDevice
*dev
, int configuration
)
294 int dev_descr_len
, config_descr_len
;
295 int interface
, nb_interfaces
;
298 if (configuration
== 0) /* address state - ignore */
301 DPRINTF("husb: claiming interfaces. config %d\n", configuration
);
304 dev_descr_len
= dev
->descr
[0];
305 if (dev_descr_len
> dev
->descr_len
) {
310 while (i
< dev
->descr_len
) {
311 DPRINTF("husb: i is %d, descr_len is %d, dl %d, dt %d\n",
313 dev
->descr
[i
], dev
->descr
[i
+1]);
315 if (dev
->descr
[i
+1] != USB_DT_CONFIG
) {
319 config_descr_len
= dev
->descr
[i
];
321 printf("husb: config #%d need %d\n", dev
->descr
[i
+ 5], configuration
);
323 if (configuration
< 0 || configuration
== dev
->descr
[i
+ 5]) {
324 configuration
= dev
->descr
[i
+ 5];
328 i
+= config_descr_len
;
331 if (i
>= dev
->descr_len
) {
333 "husb: update iface failed. no matching configuration\n");
336 nb_interfaces
= dev
->descr
[i
+ 4];
338 #ifdef USBDEVFS_DISCONNECT
339 /* earlier Linux 2.4 do not support that */
341 struct usbdevfs_ioctl ctrl
;
342 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
343 ctrl
.ioctl_code
= USBDEVFS_DISCONNECT
;
344 ctrl
.ifno
= interface
;
345 ret
= ioctl(dev
->fd
, USBDEVFS_IOCTL
, &ctrl
);
346 if (ret
< 0 && errno
!= ENODATA
) {
347 perror("USBDEVFS_DISCONNECT");
354 /* XXX: only grab if all interfaces are free */
355 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
356 ret
= ioctl(dev
->fd
, USBDEVFS_CLAIMINTERFACE
, &interface
);
358 if (errno
== EBUSY
) {
359 printf("husb: update iface. device already grabbed\n");
361 perror("husb: failed to claim interface");
368 printf("husb: %d interfaces claimed for configuration %d\n",
369 nb_interfaces
, configuration
);
371 dev
->ninterfaces
= nb_interfaces
;
372 dev
->configuration
= configuration
;
376 static int usb_host_release_interfaces(USBHostDevice
*s
)
380 DPRINTF("husb: releasing interfaces\n");
382 for (i
= 0; i
< s
->ninterfaces
; i
++) {
383 ret
= ioctl(s
->fd
, USBDEVFS_RELEASEINTERFACE
, &i
);
385 perror("husb: failed to release interface");
393 static void usb_host_handle_reset(USBDevice
*dev
)
395 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
397 DPRINTF("husb: reset device %u.%u\n", s
->bus_num
, s
->addr
);
399 ioctl(s
->fd
, USBDEVFS_RESET
);
401 usb_host_claim_interfaces(s
, s
->configuration
);
404 static void usb_host_handle_destroy(USBDevice
*dev
)
406 USBHostDevice
*s
= (USBHostDevice
*)dev
;
409 QTAILQ_REMOVE(&hostdevs
, s
, next
);
410 qemu_remove_exit_notifier(&s
->exit
);
413 static int usb_linux_update_endp_table(USBHostDevice
*s
);
415 static int usb_host_handle_data(USBHostDevice
*s
, USBPacket
*p
)
417 struct usbdevfs_urb
*urb
;
421 aurb
= async_alloc();
427 if (p
->pid
== USB_TOKEN_IN
) {
428 urb
->endpoint
= p
->devep
| 0x80;
430 urb
->endpoint
= p
->devep
;
433 if (is_halted(s
, p
->devep
)) {
434 ret
= ioctl(s
->fd
, USBDEVFS_CLEAR_HALT
, &urb
->endpoint
);
436 DPRINTF("husb: failed to clear halt. ep 0x%x errno %d\n",
437 urb
->endpoint
, errno
);
440 clear_halt(s
, p
->devep
);
443 urb
->buffer
= p
->data
;
444 urb
->buffer_length
= p
->len
;
446 if (is_isoc(s
, p
->devep
)) {
447 /* Setup ISOC transfer */
448 urb
->type
= USBDEVFS_URB_TYPE_ISO
;
449 urb
->flags
= USBDEVFS_URB_ISO_ASAP
;
450 urb
->number_of_packets
= 1;
451 urb
->iso_frame_desc
[0].length
= p
->len
;
453 /* Setup bulk transfer */
454 urb
->type
= USBDEVFS_URB_TYPE_BULK
;
457 urb
->usercontext
= s
;
459 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
461 DPRINTF("husb: data submit. ep 0x%x len %u aurb %p\n",
462 urb
->endpoint
, p
->len
, aurb
);
465 DPRINTF("husb: submit failed. errno %d\n", errno
);
473 return USB_RET_STALL
;
477 usb_defer_packet(p
, async_cancel
, aurb
);
478 return USB_RET_ASYNC
;
481 static int ctrl_error(void)
483 if (errno
== ETIMEDOUT
) {
486 return USB_RET_STALL
;
490 static int usb_host_set_address(USBHostDevice
*s
, int addr
)
492 DPRINTF("husb: ctrl set addr %u\n", addr
);
497 static int usb_host_set_config(USBHostDevice
*s
, int config
)
499 usb_host_release_interfaces(s
);
501 int ret
= ioctl(s
->fd
, USBDEVFS_SETCONFIGURATION
, &config
);
503 DPRINTF("husb: ctrl set config %d ret %d errno %d\n", config
, ret
, errno
);
508 usb_host_claim_interfaces(s
, config
);
512 static int usb_host_set_interface(USBHostDevice
*s
, int iface
, int alt
)
514 struct usbdevfs_setinterface si
;
517 si
.interface
= iface
;
519 ret
= ioctl(s
->fd
, USBDEVFS_SETINTERFACE
, &si
);
521 DPRINTF("husb: ctrl set iface %d altset %d ret %d errno %d\n",
522 iface
, alt
, ret
, errno
);
527 usb_linux_update_endp_table(s
);
531 static int usb_host_handle_control(USBHostDevice
*s
, USBPacket
*p
)
533 struct usbdevfs_urb
*urb
;
535 int ret
, value
, index
;
539 * Process certain standard device requests.
540 * These are infrequent and are processed synchronously.
542 value
= le16_to_cpu(s
->ctrl
.req
.wValue
);
543 index
= le16_to_cpu(s
->ctrl
.req
.wIndex
);
545 DPRINTF("husb: ctrl type 0x%x req 0x%x val 0x%x index %u len %u\n",
546 s
->ctrl
.req
.bRequestType
, s
->ctrl
.req
.bRequest
, value
, index
,
549 if (s
->ctrl
.req
.bRequestType
== 0) {
550 switch (s
->ctrl
.req
.bRequest
) {
551 case USB_REQ_SET_ADDRESS
:
552 return usb_host_set_address(s
, value
);
554 case USB_REQ_SET_CONFIGURATION
:
555 return usb_host_set_config(s
, value
& 0xff);
559 if (s
->ctrl
.req
.bRequestType
== 1 &&
560 s
->ctrl
.req
.bRequest
== USB_REQ_SET_INTERFACE
) {
561 return usb_host_set_interface(s
, index
, value
);
564 /* The rest are asynchronous */
566 buffer_len
= 8 + s
->ctrl
.len
;
567 if (buffer_len
> sizeof(s
->ctrl
.buffer
)) {
568 fprintf(stderr
, "husb: ctrl buffer too small (%u > %zu)\n",
569 buffer_len
, sizeof(s
->ctrl
.buffer
));
570 return USB_RET_STALL
;
573 aurb
= async_alloc();
578 * Setup ctrl transfer.
580 * s->ctrl is layed out such that data buffer immediately follows
581 * 'req' struct which is exactly what usbdevfs expects.
585 urb
->type
= USBDEVFS_URB_TYPE_CONTROL
;
586 urb
->endpoint
= p
->devep
;
588 urb
->buffer
= &s
->ctrl
.req
;
589 urb
->buffer_length
= buffer_len
;
591 urb
->usercontext
= s
;
593 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
595 DPRINTF("husb: submit ctrl. len %u aurb %p\n", urb
->buffer_length
, aurb
);
598 DPRINTF("husb: submit failed. errno %d\n", errno
);
606 return USB_RET_STALL
;
610 usb_defer_packet(p
, async_cancel
, aurb
);
611 return USB_RET_ASYNC
;
614 static int do_token_setup(USBDevice
*dev
, USBPacket
*p
)
616 USBHostDevice
*s
= (USBHostDevice
*) dev
;
620 return USB_RET_STALL
;
623 memcpy(&s
->ctrl
.req
, p
->data
, 8);
624 s
->ctrl
.len
= le16_to_cpu(s
->ctrl
.req
.wLength
);
626 s
->ctrl
.state
= CTRL_STATE_SETUP
;
628 if (s
->ctrl
.req
.bRequestType
& USB_DIR_IN
) {
629 ret
= usb_host_handle_control(s
, p
);
634 if (ret
< s
->ctrl
.len
) {
637 s
->ctrl
.state
= CTRL_STATE_DATA
;
639 if (s
->ctrl
.len
== 0) {
640 s
->ctrl
.state
= CTRL_STATE_ACK
;
642 s
->ctrl
.state
= CTRL_STATE_DATA
;
649 static int do_token_in(USBDevice
*dev
, USBPacket
*p
)
651 USBHostDevice
*s
= (USBHostDevice
*) dev
;
655 return usb_host_handle_data(s
, p
);
658 switch(s
->ctrl
.state
) {
660 if (!(s
->ctrl
.req
.bRequestType
& USB_DIR_IN
)) {
661 ret
= usb_host_handle_control(s
, p
);
662 if (ret
== USB_RET_ASYNC
) {
663 return USB_RET_ASYNC
;
665 s
->ctrl
.state
= CTRL_STATE_IDLE
;
666 return ret
> 0 ? 0 : ret
;
671 case CTRL_STATE_DATA
:
672 if (s
->ctrl
.req
.bRequestType
& USB_DIR_IN
) {
673 int len
= s
->ctrl
.len
- s
->ctrl
.offset
;
677 memcpy(p
->data
, s
->ctrl
.buffer
+ s
->ctrl
.offset
, len
);
678 s
->ctrl
.offset
+= len
;
679 if (s
->ctrl
.offset
>= s
->ctrl
.len
) {
680 s
->ctrl
.state
= CTRL_STATE_ACK
;
685 s
->ctrl
.state
= CTRL_STATE_IDLE
;
686 return USB_RET_STALL
;
689 return USB_RET_STALL
;
693 static int do_token_out(USBDevice
*dev
, USBPacket
*p
)
695 USBHostDevice
*s
= (USBHostDevice
*) dev
;
698 return usb_host_handle_data(s
, p
);
701 switch(s
->ctrl
.state
) {
703 if (s
->ctrl
.req
.bRequestType
& USB_DIR_IN
) {
704 s
->ctrl
.state
= CTRL_STATE_IDLE
;
707 /* ignore additional output */
711 case CTRL_STATE_DATA
:
712 if (!(s
->ctrl
.req
.bRequestType
& USB_DIR_IN
)) {
713 int len
= s
->ctrl
.len
- s
->ctrl
.offset
;
717 memcpy(s
->ctrl
.buffer
+ s
->ctrl
.offset
, p
->data
, len
);
718 s
->ctrl
.offset
+= len
;
719 if (s
->ctrl
.offset
>= s
->ctrl
.len
) {
720 s
->ctrl
.state
= CTRL_STATE_ACK
;
725 s
->ctrl
.state
= CTRL_STATE_IDLE
;
726 return USB_RET_STALL
;
729 return USB_RET_STALL
;
735 * Called by the HC (host controller).
737 * Returns length of the transaction or one of the USB_RET_XXX codes.
739 static int usb_host_handle_packet(USBDevice
*s
, USBPacket
*p
)
743 s
->state
= USB_STATE_ATTACHED
;
747 s
->state
= USB_STATE_NOTATTACHED
;
751 s
->remote_wakeup
= 0;
753 s
->state
= USB_STATE_DEFAULT
;
754 s
->info
->handle_reset(s
);
758 /* Rest of the PIDs must match our address */
759 if (s
->state
< USB_STATE_DEFAULT
|| p
->devaddr
!= s
->addr
) {
760 return USB_RET_NODEV
;
764 case USB_TOKEN_SETUP
:
765 return do_token_setup(s
, p
);
768 return do_token_in(s
, p
);
771 return do_token_out(s
, p
);
774 return USB_RET_STALL
;
778 /* returns 1 on problem encountered or 0 for success */
779 static int usb_linux_update_endp_table(USBHostDevice
*s
)
781 uint8_t *descriptors
;
782 uint8_t devep
, type
, configuration
, alt_interface
;
783 struct usb_ctrltransfer ct
;
784 int interface
, ret
, length
, i
;
786 ct
.bRequestType
= USB_DIR_IN
;
787 ct
.bRequest
= USB_REQ_GET_CONFIGURATION
;
791 ct
.data
= &configuration
;
794 ret
= ioctl(s
->fd
, USBDEVFS_CONTROL
, &ct
);
796 perror("usb_linux_update_endp_table");
800 /* in address state */
801 if (configuration
== 0) {
805 /* get the desired configuration, interface, and endpoint descriptors
806 * from device description */
807 descriptors
= &s
->descr
[18];
808 length
= s
->descr_len
- 18;
811 if (descriptors
[i
+ 1] != USB_DT_CONFIG
||
812 descriptors
[i
+ 5] != configuration
) {
813 DPRINTF("invalid descriptor data - configuration\n");
819 if (descriptors
[i
+ 1] != USB_DT_INTERFACE
||
820 (descriptors
[i
+ 1] == USB_DT_INTERFACE
&&
821 descriptors
[i
+ 4] == 0)) {
826 interface
= descriptors
[i
+ 2];
828 ct
.bRequestType
= USB_DIR_IN
| USB_RECIP_INTERFACE
;
829 ct
.bRequest
= USB_REQ_GET_INTERFACE
;
831 ct
.wIndex
= interface
;
833 ct
.data
= &alt_interface
;
836 ret
= ioctl(s
->fd
, USBDEVFS_CONTROL
, &ct
);
838 alt_interface
= interface
;
841 /* the current interface descriptor is the active interface
842 * and has endpoints */
843 if (descriptors
[i
+ 3] != alt_interface
) {
848 /* advance to the endpoints */
849 while (i
< length
&& descriptors
[i
+1] != USB_DT_ENDPOINT
) {
857 if (descriptors
[i
+ 1] != USB_DT_ENDPOINT
) {
861 devep
= descriptors
[i
+ 2];
862 switch (descriptors
[i
+ 3] & 0x3) {
864 type
= USBDEVFS_URB_TYPE_CONTROL
;
867 type
= USBDEVFS_URB_TYPE_ISO
;
870 type
= USBDEVFS_URB_TYPE_BULK
;
873 type
= USBDEVFS_URB_TYPE_INTERRUPT
;
876 DPRINTF("usb_host: malformed endpoint type\n");
877 type
= USBDEVFS_URB_TYPE_BULK
;
879 s
->endp_table
[(devep
& 0xf) - 1].type
= type
;
880 s
->endp_table
[(devep
& 0xf) - 1].halted
= 0;
888 static int usb_host_open(USBHostDevice
*dev
, int bus_num
,
889 int addr
, int devpath
, const char *prod_name
)
892 struct usbdevfs_connectinfo ci
;
898 printf("husb: open device %d.%d\n", bus_num
, addr
);
900 if (!usb_host_device_path
) {
901 perror("husb: USB Host Device Path not set");
904 snprintf(buf
, sizeof(buf
), "%s/%03d/%03d", usb_host_device_path
,
906 fd
= open(buf
, O_RDWR
| O_NONBLOCK
);
911 DPRINTF("husb: opened %s\n", buf
);
913 dev
->bus_num
= bus_num
;
915 dev
->devpath
= devpath
;
918 /* read the device description */
919 dev
->descr_len
= read(fd
, dev
->descr
, sizeof(dev
->descr
));
920 if (dev
->descr_len
<= 0) {
921 perror("husb: reading device data failed");
928 printf("=== begin dumping device descriptor data ===\n");
929 for (x
= 0; x
< dev
->descr_len
; x
++) {
930 printf("%02x ", dev
->descr
[x
]);
932 printf("\n=== end dumping device descriptor data ===\n");
938 * Initial configuration is -1 which makes us claim first
939 * available config. We used to start with 1, which does not
940 * always work. I've seen devices where first config starts
943 if (!usb_host_claim_interfaces(dev
, -1)) {
947 ret
= ioctl(fd
, USBDEVFS_CONNECTINFO
, &ci
);
949 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
953 printf("husb: grabbed usb device %d.%d\n", bus_num
, addr
);
955 ret
= usb_linux_update_endp_table(dev
);
961 dev
->dev
.speed
= USB_SPEED_LOW
;
963 dev
->dev
.speed
= USB_SPEED_HIGH
;
966 if (!prod_name
|| prod_name
[0] == '\0') {
967 snprintf(dev
->dev
.product_desc
, sizeof(dev
->dev
.product_desc
),
968 "host:%d.%d", bus_num
, addr
);
970 pstrcpy(dev
->dev
.product_desc
, sizeof(dev
->dev
.product_desc
),
974 /* USB devio uses 'write' flag to check for async completions */
975 qemu_set_fd_handler(dev
->fd
, NULL
, async_complete
, dev
);
977 usb_device_attach(&dev
->dev
);
988 static int usb_host_close(USBHostDevice
*dev
)
994 qemu_set_fd_handler(dev
->fd
, NULL
, NULL
, NULL
);
998 usb_device_detach(&dev
->dev
);
999 ioctl(dev
->fd
, USBDEVFS_RESET
);
1005 static void usb_host_exit_notifier(struct Notifier
* n
)
1007 USBHostDevice
*s
= container_of(n
, USBHostDevice
, exit
);
1010 ioctl(s
->fd
, USBDEVFS_RESET
);
1014 static int usb_host_initfn(USBDevice
*dev
)
1016 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
1018 dev
->auto_attach
= 0;
1020 QTAILQ_INSERT_TAIL(&hostdevs
, s
, next
);
1021 s
->exit
.notify
= usb_host_exit_notifier
;
1022 qemu_add_exit_notifier(&s
->exit
);
1023 usb_host_auto_check(NULL
);
1027 static struct USBDeviceInfo usb_host_dev_info
= {
1028 .product_desc
= "USB Host Device",
1029 .qdev
.name
= "usb-host",
1030 .qdev
.size
= sizeof(USBHostDevice
),
1031 .init
= usb_host_initfn
,
1032 .handle_packet
= usb_host_handle_packet
,
1033 .handle_reset
= usb_host_handle_reset
,
1034 .handle_destroy
= usb_host_handle_destroy
,
1035 .usbdevice_name
= "host",
1036 .usbdevice_init
= usb_host_device_open
,
1037 .qdev
.props
= (Property
[]) {
1038 DEFINE_PROP_UINT32("hostbus", USBHostDevice
, match
.bus_num
, 0),
1039 DEFINE_PROP_UINT32("hostaddr", USBHostDevice
, match
.addr
, 0),
1040 DEFINE_PROP_HEX32("vendorid", USBHostDevice
, match
.vendor_id
, 0),
1041 DEFINE_PROP_HEX32("productid", USBHostDevice
, match
.product_id
, 0),
1042 DEFINE_PROP_END_OF_LIST(),
1046 static void usb_host_register_devices(void)
1048 usb_qdev_register(&usb_host_dev_info
);
1050 device_init(usb_host_register_devices
)
1052 USBDevice
*usb_host_device_open(const char *devname
)
1054 struct USBAutoFilter filter
;
1058 dev
= usb_create(NULL
/* FIXME */, "usb-host");
1060 if (strstr(devname
, "auto:")) {
1061 if (parse_filter(devname
, &filter
) < 0) {
1065 if ((p
= strchr(devname
, '.'))) {
1066 filter
.bus_num
= strtoul(devname
, NULL
, 0);
1067 filter
.addr
= strtoul(p
+ 1, NULL
, 0);
1068 filter
.vendor_id
= 0;
1069 filter
.product_id
= 0;
1070 } else if ((p
= strchr(devname
, ':'))) {
1073 filter
.vendor_id
= strtoul(devname
, NULL
, 16);
1074 filter
.product_id
= strtoul(p
+ 1, NULL
, 16);
1080 qdev_prop_set_uint32(&dev
->qdev
, "hostbus", filter
.bus_num
);
1081 qdev_prop_set_uint32(&dev
->qdev
, "hostaddr", filter
.addr
);
1082 qdev_prop_set_uint32(&dev
->qdev
, "vendorid", filter
.vendor_id
);
1083 qdev_prop_set_uint32(&dev
->qdev
, "productid", filter
.product_id
);
1084 qdev_init_nofail(&dev
->qdev
);
1088 qdev_free(&dev
->qdev
);
1092 int usb_host_device_close(const char *devname
)
1095 char product_name
[PRODUCT_NAME_SZ
];
1099 if (strstr(devname
, "auto:")) {
1100 return usb_host_auto_del(devname
);
1102 if (usb_host_find_device(&bus_num
, &addr
, product_name
,
1103 sizeof(product_name
), devname
) < 0) {
1106 s
= hostdev_find(bus_num
, addr
);
1108 usb_device_delete_addr(s
->bus_num
, s
->dev
.addr
);
1116 static int get_tag_value(char *buf
, int buf_size
,
1117 const char *str
, const char *tag
,
1118 const char *stopchars
)
1122 p
= strstr(str
, tag
);
1127 while (qemu_isspace(*p
)) {
1131 while (*p
!= '\0' && !strchr(stopchars
, *p
)) {
1132 if ((q
- buf
) < (buf_size
- 1)) {
1142 * Use /proc/bus/usb/devices or /dev/bus/usb/devices file to determine
1143 * host's USB devices. This is legacy support since many distributions
1144 * are moving to /sys/bus/usb
1146 static int usb_host_scan_dev(void *opaque
, USBScanFunc
*func
)
1151 int bus_num
, addr
, speed
, device_count
, class_id
, product_id
, vendor_id
;
1152 char product_name
[512];
1155 if (!usb_host_device_path
) {
1156 perror("husb: USB Host Device Path not set");
1159 snprintf(line
, sizeof(line
), "%s/devices", usb_host_device_path
);
1160 f
= fopen(line
, "r");
1162 perror("husb: cannot open devices file");
1167 bus_num
= addr
= speed
= class_id
= product_id
= vendor_id
= 0;
1169 if (fgets(line
, sizeof(line
), f
) == NULL
) {
1172 if (strlen(line
) > 0) {
1173 line
[strlen(line
) - 1] = '\0';
1175 if (line
[0] == 'T' && line
[1] == ':') {
1176 if (device_count
&& (vendor_id
|| product_id
)) {
1177 /* New device. Add the previously discovered device. */
1178 ret
= func(opaque
, bus_num
, addr
, 0, class_id
, vendor_id
,
1179 product_id
, product_name
, speed
);
1184 if (get_tag_value(buf
, sizeof(buf
), line
, "Bus=", " ") < 0) {
1187 bus_num
= atoi(buf
);
1188 if (get_tag_value(buf
, sizeof(buf
), line
, "Dev#=", " ") < 0) {
1192 if (get_tag_value(buf
, sizeof(buf
), line
, "Spd=", " ") < 0) {
1195 if (!strcmp(buf
, "480")) {
1196 speed
= USB_SPEED_HIGH
;
1197 } else if (!strcmp(buf
, "1.5")) {
1198 speed
= USB_SPEED_LOW
;
1200 speed
= USB_SPEED_FULL
;
1202 product_name
[0] = '\0';
1207 } else if (line
[0] == 'P' && line
[1] == ':') {
1208 if (get_tag_value(buf
, sizeof(buf
), line
, "Vendor=", " ") < 0) {
1211 vendor_id
= strtoul(buf
, NULL
, 16);
1212 if (get_tag_value(buf
, sizeof(buf
), line
, "ProdID=", " ") < 0) {
1215 product_id
= strtoul(buf
, NULL
, 16);
1216 } else if (line
[0] == 'S' && line
[1] == ':') {
1217 if (get_tag_value(buf
, sizeof(buf
), line
, "Product=", "") < 0) {
1220 pstrcpy(product_name
, sizeof(product_name
), buf
);
1221 } else if (line
[0] == 'D' && line
[1] == ':') {
1222 if (get_tag_value(buf
, sizeof(buf
), line
, "Cls=", " (") < 0) {
1225 class_id
= strtoul(buf
, NULL
, 16);
1229 if (device_count
&& (vendor_id
|| product_id
)) {
1230 /* Add the last device. */
1231 ret
= func(opaque
, bus_num
, addr
, 0, class_id
, vendor_id
,
1232 product_id
, product_name
, speed
);
1242 * Read sys file-system device file
1244 * @line address of buffer to put file contents in
1245 * @line_size size of line
1246 * @device_file path to device file (printf format string)
1247 * @device_name device being opened (inserted into device_file)
1249 * @return 0 failed, 1 succeeded ('line' contains data)
1251 static int usb_host_read_file(char *line
, size_t line_size
,
1252 const char *device_file
, const char *device_name
)
1256 char filename
[PATH_MAX
];
1258 snprintf(filename
, PATH_MAX
, USBSYSBUS_PATH
"/devices/%s/%s", device_name
,
1260 f
= fopen(filename
, "r");
1262 ret
= fgets(line
, line_size
, f
) != NULL
;
1270 * Use /sys/bus/usb/devices/ directory to determine host's USB
1273 * This code is based on Robert Schiele's original patches posted to
1274 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1276 static int usb_host_scan_sys(void *opaque
, USBScanFunc
*func
)
1280 int bus_num
, addr
, devpath
, speed
, class_id
, product_id
, vendor_id
;
1282 char product_name
[512];
1285 dir
= opendir(USBSYSBUS_PATH
"/devices");
1287 perror("husb: cannot open devices directory");
1291 while ((de
= readdir(dir
))) {
1292 if (de
->d_name
[0] != '.' && !strchr(de
->d_name
, ':')) {
1293 char *tmpstr
= de
->d_name
;
1294 if (!strncmp(de
->d_name
, "usb", 3)) {
1297 if (sscanf(tmpstr
, "%d-%d", &bus_num
, &devpath
) < 1) {
1301 if (!usb_host_read_file(line
, sizeof(line
), "devnum", de
->d_name
)) {
1304 if (sscanf(line
, "%d", &addr
) != 1) {
1307 if (!usb_host_read_file(line
, sizeof(line
), "bDeviceClass",
1311 if (sscanf(line
, "%x", &class_id
) != 1) {
1315 if (!usb_host_read_file(line
, sizeof(line
), "idVendor",
1319 if (sscanf(line
, "%x", &vendor_id
) != 1) {
1322 if (!usb_host_read_file(line
, sizeof(line
), "idProduct",
1326 if (sscanf(line
, "%x", &product_id
) != 1) {
1329 if (!usb_host_read_file(line
, sizeof(line
), "product",
1333 if (strlen(line
) > 0) {
1334 line
[strlen(line
) - 1] = '\0';
1336 pstrcpy(product_name
, sizeof(product_name
), line
);
1339 if (!usb_host_read_file(line
, sizeof(line
), "speed", de
->d_name
)) {
1342 if (!strcmp(line
, "480\n")) {
1343 speed
= USB_SPEED_HIGH
;
1344 } else if (!strcmp(line
, "1.5\n")) {
1345 speed
= USB_SPEED_LOW
;
1347 speed
= USB_SPEED_FULL
;
1350 ret
= func(opaque
, bus_num
, addr
, devpath
, class_id
, vendor_id
,
1351 product_id
, product_name
, speed
);
1365 * Determine how to access the host's USB devices and call the
1366 * specific support function.
1368 static int usb_host_scan(void *opaque
, USBScanFunc
*func
)
1370 Monitor
*mon
= cur_mon
;
1374 const char *fs_type
[] = {"unknown", "proc", "dev", "sys"};
1375 char devpath
[PATH_MAX
];
1377 /* only check the host once */
1379 dir
= opendir(USBSYSBUS_PATH
"/devices");
1381 /* devices found in /dev/bus/usb/ (yes - not a mistake!) */
1382 strcpy(devpath
, USBDEVBUS_PATH
);
1383 usb_fs_type
= USB_FS_SYS
;
1385 DPRINTF(USBDBG_DEVOPENED
, USBSYSBUS_PATH
);
1388 f
= fopen(USBPROCBUS_PATH
"/devices", "r");
1390 /* devices found in /proc/bus/usb/ */
1391 strcpy(devpath
, USBPROCBUS_PATH
);
1392 usb_fs_type
= USB_FS_PROC
;
1394 DPRINTF(USBDBG_DEVOPENED
, USBPROCBUS_PATH
);
1397 /* try additional methods if an access method hasn't been found yet */
1398 f
= fopen(USBDEVBUS_PATH
"/devices", "r");
1400 /* devices found in /dev/bus/usb/ */
1401 strcpy(devpath
, USBDEVBUS_PATH
);
1402 usb_fs_type
= USB_FS_DEV
;
1404 DPRINTF(USBDBG_DEVOPENED
, USBDEVBUS_PATH
);
1410 monitor_printf(mon
, "husb: unable to access USB devices\n");
1415 /* the module setting (used later for opening devices) */
1416 usb_host_device_path
= qemu_mallocz(strlen(devpath
)+1);
1417 strcpy(usb_host_device_path
, devpath
);
1419 monitor_printf(mon
, "husb: using %s file-system with %s\n",
1420 fs_type
[usb_fs_type
], usb_host_device_path
);
1424 switch (usb_fs_type
) {
1427 ret
= usb_host_scan_dev(opaque
, func
);
1430 ret
= usb_host_scan_sys(opaque
, func
);
1439 static QEMUTimer
*usb_auto_timer
;
1441 static int usb_host_auto_scan(void *opaque
, int bus_num
, int addr
, int devpath
,
1442 int class_id
, int vendor_id
, int product_id
,
1443 const char *product_name
, int speed
)
1445 struct USBAutoFilter
*f
;
1446 struct USBHostDevice
*s
;
1452 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1455 if (f
->bus_num
> 0 && f
->bus_num
!= bus_num
) {
1458 if (f
->addr
> 0 && f
->addr
!= addr
) {
1462 if (f
->vendor_id
> 0 && f
->vendor_id
!= vendor_id
) {
1466 if (f
->product_id
> 0 && f
->product_id
!= product_id
) {
1469 /* We got a match */
1471 /* Already attached ? */
1475 DPRINTF("husb: auto open: bus_num %d addr %d\n", bus_num
, addr
);
1477 usb_host_open(s
, bus_num
, addr
, devpath
, product_name
);
1483 static void usb_host_auto_check(void *unused
)
1485 struct USBHostDevice
*s
;
1486 int unconnected
= 0;
1488 usb_host_scan(NULL
, usb_host_auto_scan
);
1490 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1496 if (unconnected
== 0) {
1497 /* nothing to watch */
1498 if (usb_auto_timer
) {
1499 qemu_del_timer(usb_auto_timer
);
1504 if (!usb_auto_timer
) {
1505 usb_auto_timer
= qemu_new_timer(rt_clock
, usb_host_auto_check
, NULL
);
1506 if (!usb_auto_timer
) {
1510 qemu_mod_timer(usb_auto_timer
, qemu_get_clock(rt_clock
) + 2000);
1514 * Autoconnect filter
1516 * auto:bus:dev[:vid:pid]
1517 * auto:bus.dev[:vid:pid]
1519 * bus - bus number (dec, * means any)
1520 * dev - device number (dec, * means any)
1521 * vid - vendor id (hex, * means any)
1522 * pid - product id (hex, * means any)
1524 * See 'lsusb' output.
1526 static int parse_filter(const char *spec
, struct USBAutoFilter
*f
)
1528 enum { BUS
, DEV
, VID
, PID
, DONE
};
1529 const char *p
= spec
;
1537 for (i
= BUS
; i
< DONE
; i
++) {
1538 p
= strpbrk(p
, ":.");
1548 case BUS
: f
->bus_num
= strtol(p
, NULL
, 10); break;
1549 case DEV
: f
->addr
= strtol(p
, NULL
, 10); break;
1550 case VID
: f
->vendor_id
= strtol(p
, NULL
, 16); break;
1551 case PID
: f
->product_id
= strtol(p
, NULL
, 16); break;
1556 fprintf(stderr
, "husb: invalid auto filter spec %s\n", spec
);
1563 /**********************/
1564 /* USB host device info */
1566 struct usb_class_info
{
1568 const char *class_name
;
1571 static const struct usb_class_info usb_class_info
[] = {
1572 { USB_CLASS_AUDIO
, "Audio"},
1573 { USB_CLASS_COMM
, "Communication"},
1574 { USB_CLASS_HID
, "HID"},
1575 { USB_CLASS_HUB
, "Hub" },
1576 { USB_CLASS_PHYSICAL
, "Physical" },
1577 { USB_CLASS_PRINTER
, "Printer" },
1578 { USB_CLASS_MASS_STORAGE
, "Storage" },
1579 { USB_CLASS_CDC_DATA
, "Data" },
1580 { USB_CLASS_APP_SPEC
, "Application Specific" },
1581 { USB_CLASS_VENDOR_SPEC
, "Vendor Specific" },
1582 { USB_CLASS_STILL_IMAGE
, "Still Image" },
1583 { USB_CLASS_CSCID
, "Smart Card" },
1584 { USB_CLASS_CONTENT_SEC
, "Content Security" },
1588 static const char *usb_class_str(uint8_t class)
1590 const struct usb_class_info
*p
;
1591 for(p
= usb_class_info
; p
->class != -1; p
++) {
1592 if (p
->class == class) {
1596 return p
->class_name
;
1599 static void usb_info_device(Monitor
*mon
, int bus_num
, int addr
, int class_id
,
1600 int vendor_id
, int product_id
,
1601 const char *product_name
,
1604 const char *class_str
, *speed_str
;
1610 case USB_SPEED_FULL
:
1613 case USB_SPEED_HIGH
:
1621 monitor_printf(mon
, " Device %d.%d, speed %s Mb/s\n",
1622 bus_num
, addr
, speed_str
);
1623 class_str
= usb_class_str(class_id
);
1625 monitor_printf(mon
, " %s:", class_str
);
1627 monitor_printf(mon
, " Class %02x:", class_id
);
1629 monitor_printf(mon
, " USB device %04x:%04x", vendor_id
, product_id
);
1630 if (product_name
[0] != '\0') {
1631 monitor_printf(mon
, ", %s", product_name
);
1633 monitor_printf(mon
, "\n");
1636 static int usb_host_info_device(void *opaque
, int bus_num
, int addr
,
1637 int devpath
, int class_id
,
1638 int vendor_id
, int product_id
,
1639 const char *product_name
,
1642 Monitor
*mon
= opaque
;
1644 usb_info_device(mon
, bus_num
, addr
, class_id
, vendor_id
, product_id
,
1645 product_name
, speed
);
1649 static void dec2str(int val
, char *str
, size_t size
)
1652 snprintf(str
, size
, "*");
1654 snprintf(str
, size
, "%d", val
);
1658 static void hex2str(int val
, char *str
, size_t size
)
1661 snprintf(str
, size
, "*");
1663 snprintf(str
, size
, "%04x", val
);
1667 void usb_host_info(Monitor
*mon
)
1669 struct USBAutoFilter
*f
;
1670 struct USBHostDevice
*s
;
1672 usb_host_scan(mon
, usb_host_info_device
);
1674 if (QTAILQ_EMPTY(&hostdevs
)) {
1678 monitor_printf(mon
, " Auto filters:\n");
1679 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1680 char bus
[10], addr
[10], vid
[10], pid
[10];
1682 dec2str(f
->bus_num
, bus
, sizeof(bus
));
1683 dec2str(f
->addr
, addr
, sizeof(addr
));
1684 hex2str(f
->vendor_id
, vid
, sizeof(vid
));
1685 hex2str(f
->product_id
, pid
, sizeof(pid
));
1686 monitor_printf(mon
, " Device %s.%s ID %s:%s\n",
1687 bus
, addr
, vid
, pid
);