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 class_id
,
66 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 */
144 struct USBAutoFilter match
;
146 QTAILQ_ENTRY(USBHostDevice
) next
;
149 static QTAILQ_HEAD(, USBHostDevice
) hostdevs
= QTAILQ_HEAD_INITIALIZER(hostdevs
);
151 static int usb_host_close(USBHostDevice
*dev
);
152 static int parse_filter(const char *spec
, struct USBAutoFilter
*f
);
153 static void usb_host_auto_check(void *unused
);
155 static int is_isoc(USBHostDevice
*s
, int ep
)
157 return s
->endp_table
[ep
- 1].type
== USBDEVFS_URB_TYPE_ISO
;
160 static int is_halted(USBHostDevice
*s
, int ep
)
162 return s
->endp_table
[ep
- 1].halted
;
165 static void clear_halt(USBHostDevice
*s
, int ep
)
167 s
->endp_table
[ep
- 1].halted
= 0;
170 static void set_halt(USBHostDevice
*s
, int ep
)
172 s
->endp_table
[ep
- 1].halted
= 1;
177 * We always allocate one isoc descriptor even for bulk transfers
178 * to simplify allocation and casts.
180 typedef struct AsyncURB
182 struct usbdevfs_urb urb
;
183 struct usbdevfs_iso_packet_desc isocpd
;
189 static AsyncURB
*async_alloc(void)
191 return (AsyncURB
*) qemu_mallocz(sizeof(AsyncURB
));
194 static void async_free(AsyncURB
*aurb
)
199 static void async_complete_ctrl(USBHostDevice
*s
, USBPacket
*p
)
201 switch(s
->ctrl
.state
) {
202 case CTRL_STATE_SETUP
:
203 if (p
->len
< s
->ctrl
.len
)
204 s
->ctrl
.len
= p
->len
;
205 s
->ctrl
.state
= CTRL_STATE_DATA
;
210 s
->ctrl
.state
= CTRL_STATE_IDLE
;
219 static void async_complete(void *opaque
)
221 USBHostDevice
*s
= opaque
;
227 int r
= ioctl(s
->fd
, USBDEVFS_REAPURBNDELAY
, &aurb
);
229 if (errno
== EAGAIN
) {
232 if (errno
== ENODEV
&& !s
->closing
) {
233 printf("husb: device %d.%d disconnected\n",
234 s
->bus_num
, s
->addr
);
236 usb_host_auto_check(NULL
);
240 DPRINTF("husb: async. reap urb failed errno %d\n", errno
);
246 DPRINTF("husb: async completed. aurb %p status %d alen %d\n",
247 aurb
, aurb
->urb
.status
, aurb
->urb
.actual_length
);
250 switch (aurb
->urb
.status
) {
252 p
->len
= aurb
->urb
.actual_length
;
253 if (aurb
->urb
.type
== USBDEVFS_URB_TYPE_CONTROL
) {
254 async_complete_ctrl(s
, p
);
259 set_halt(s
, p
->devep
);
260 p
->len
= USB_RET_STALL
;
264 p
->len
= USB_RET_NAK
;
268 usb_packet_complete(p
);
275 static void async_cancel(USBPacket
*unused
, void *opaque
)
277 AsyncURB
*aurb
= opaque
;
278 USBHostDevice
*s
= aurb
->hdev
;
280 DPRINTF("husb: async cancel. aurb %p\n", aurb
);
282 /* Mark it as dead (see async_complete above) */
285 int r
= ioctl(s
->fd
, USBDEVFS_DISCARDURB
, aurb
);
287 DPRINTF("husb: async. discard urb failed errno %d\n", errno
);
291 static int usb_host_claim_interfaces(USBHostDevice
*dev
, int configuration
)
293 int dev_descr_len
, config_descr_len
;
294 int interface
, nb_interfaces
;
297 if (configuration
== 0) /* address state - ignore */
300 DPRINTF("husb: claiming interfaces. config %d\n", configuration
);
303 dev_descr_len
= dev
->descr
[0];
304 if (dev_descr_len
> dev
->descr_len
) {
309 while (i
< dev
->descr_len
) {
310 DPRINTF("husb: i is %d, descr_len is %d, dl %d, dt %d\n",
312 dev
->descr
[i
], dev
->descr
[i
+1]);
314 if (dev
->descr
[i
+1] != USB_DT_CONFIG
) {
318 config_descr_len
= dev
->descr
[i
];
320 printf("husb: config #%d need %d\n", dev
->descr
[i
+ 5], configuration
);
322 if (configuration
< 0 || configuration
== dev
->descr
[i
+ 5]) {
323 configuration
= dev
->descr
[i
+ 5];
327 i
+= config_descr_len
;
330 if (i
>= dev
->descr_len
) {
332 "husb: update iface failed. no matching configuration\n");
335 nb_interfaces
= dev
->descr
[i
+ 4];
337 #ifdef USBDEVFS_DISCONNECT
338 /* earlier Linux 2.4 do not support that */
340 struct usbdevfs_ioctl ctrl
;
341 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
342 ctrl
.ioctl_code
= USBDEVFS_DISCONNECT
;
343 ctrl
.ifno
= interface
;
344 ret
= ioctl(dev
->fd
, USBDEVFS_IOCTL
, &ctrl
);
345 if (ret
< 0 && errno
!= ENODATA
) {
346 perror("USBDEVFS_DISCONNECT");
353 /* XXX: only grab if all interfaces are free */
354 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
355 ret
= ioctl(dev
->fd
, USBDEVFS_CLAIMINTERFACE
, &interface
);
357 if (errno
== EBUSY
) {
358 printf("husb: update iface. device already grabbed\n");
360 perror("husb: failed to claim interface");
367 printf("husb: %d interfaces claimed for configuration %d\n",
368 nb_interfaces
, configuration
);
370 dev
->ninterfaces
= nb_interfaces
;
371 dev
->configuration
= configuration
;
375 static int usb_host_release_interfaces(USBHostDevice
*s
)
379 DPRINTF("husb: releasing interfaces\n");
381 for (i
= 0; i
< s
->ninterfaces
; i
++) {
382 ret
= ioctl(s
->fd
, USBDEVFS_RELEASEINTERFACE
, &i
);
384 perror("husb: failed to release interface");
392 static void usb_host_handle_reset(USBDevice
*dev
)
394 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
396 DPRINTF("husb: reset device %u.%u\n", s
->bus_num
, s
->addr
);
398 ioctl(s
->fd
, USBDEVFS_RESET
);
400 usb_host_claim_interfaces(s
, s
->configuration
);
403 static void usb_host_handle_destroy(USBDevice
*dev
)
405 USBHostDevice
*s
= (USBHostDevice
*)dev
;
408 QTAILQ_REMOVE(&hostdevs
, s
, next
);
409 qemu_remove_exit_notifier(&s
->exit
);
412 static int usb_linux_update_endp_table(USBHostDevice
*s
);
414 static int usb_host_handle_data(USBHostDevice
*s
, USBPacket
*p
)
416 struct usbdevfs_urb
*urb
;
420 aurb
= async_alloc();
426 if (p
->pid
== USB_TOKEN_IN
) {
427 urb
->endpoint
= p
->devep
| 0x80;
429 urb
->endpoint
= p
->devep
;
432 if (is_halted(s
, p
->devep
)) {
433 ret
= ioctl(s
->fd
, USBDEVFS_CLEAR_HALT
, &urb
->endpoint
);
435 DPRINTF("husb: failed to clear halt. ep 0x%x errno %d\n",
436 urb
->endpoint
, errno
);
439 clear_halt(s
, p
->devep
);
442 urb
->buffer
= p
->data
;
443 urb
->buffer_length
= p
->len
;
445 if (is_isoc(s
, p
->devep
)) {
446 /* Setup ISOC transfer */
447 urb
->type
= USBDEVFS_URB_TYPE_ISO
;
448 urb
->flags
= USBDEVFS_URB_ISO_ASAP
;
449 urb
->number_of_packets
= 1;
450 urb
->iso_frame_desc
[0].length
= p
->len
;
452 /* Setup bulk transfer */
453 urb
->type
= USBDEVFS_URB_TYPE_BULK
;
456 urb
->usercontext
= s
;
458 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
460 DPRINTF("husb: data submit. ep 0x%x len %u aurb %p\n",
461 urb
->endpoint
, p
->len
, aurb
);
464 DPRINTF("husb: submit failed. errno %d\n", errno
);
472 return USB_RET_STALL
;
476 usb_defer_packet(p
, async_cancel
, aurb
);
477 return USB_RET_ASYNC
;
480 static int ctrl_error(void)
482 if (errno
== ETIMEDOUT
) {
485 return USB_RET_STALL
;
489 static int usb_host_set_address(USBHostDevice
*s
, int addr
)
491 DPRINTF("husb: ctrl set addr %u\n", addr
);
496 static int usb_host_set_config(USBHostDevice
*s
, int config
)
498 usb_host_release_interfaces(s
);
500 int ret
= ioctl(s
->fd
, USBDEVFS_SETCONFIGURATION
, &config
);
502 DPRINTF("husb: ctrl set config %d ret %d errno %d\n", config
, ret
, errno
);
507 usb_host_claim_interfaces(s
, config
);
511 static int usb_host_set_interface(USBHostDevice
*s
, int iface
, int alt
)
513 struct usbdevfs_setinterface si
;
516 si
.interface
= iface
;
518 ret
= ioctl(s
->fd
, USBDEVFS_SETINTERFACE
, &si
);
520 DPRINTF("husb: ctrl set iface %d altset %d ret %d errno %d\n",
521 iface
, alt
, ret
, errno
);
526 usb_linux_update_endp_table(s
);
530 static int usb_host_handle_control(USBHostDevice
*s
, USBPacket
*p
)
532 struct usbdevfs_urb
*urb
;
534 int ret
, value
, index
;
538 * Process certain standard device requests.
539 * These are infrequent and are processed synchronously.
541 value
= le16_to_cpu(s
->ctrl
.req
.wValue
);
542 index
= le16_to_cpu(s
->ctrl
.req
.wIndex
);
544 DPRINTF("husb: ctrl type 0x%x req 0x%x val 0x%x index %u len %u\n",
545 s
->ctrl
.req
.bRequestType
, s
->ctrl
.req
.bRequest
, value
, index
,
548 if (s
->ctrl
.req
.bRequestType
== 0) {
549 switch (s
->ctrl
.req
.bRequest
) {
550 case USB_REQ_SET_ADDRESS
:
551 return usb_host_set_address(s
, value
);
553 case USB_REQ_SET_CONFIGURATION
:
554 return usb_host_set_config(s
, value
& 0xff);
558 if (s
->ctrl
.req
.bRequestType
== 1 &&
559 s
->ctrl
.req
.bRequest
== USB_REQ_SET_INTERFACE
) {
560 return usb_host_set_interface(s
, index
, value
);
563 /* The rest are asynchronous */
565 buffer_len
= 8 + s
->ctrl
.len
;
566 if (buffer_len
> sizeof(s
->ctrl
.buffer
)) {
567 fprintf(stderr
, "husb: ctrl buffer too small (%u > %zu)\n",
568 buffer_len
, sizeof(s
->ctrl
.buffer
));
569 return USB_RET_STALL
;
572 aurb
= async_alloc();
577 * Setup ctrl transfer.
579 * s->ctrl is layed out such that data buffer immediately follows
580 * 'req' struct which is exactly what usbdevfs expects.
584 urb
->type
= USBDEVFS_URB_TYPE_CONTROL
;
585 urb
->endpoint
= p
->devep
;
587 urb
->buffer
= &s
->ctrl
.req
;
588 urb
->buffer_length
= buffer_len
;
590 urb
->usercontext
= s
;
592 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
594 DPRINTF("husb: submit ctrl. len %u aurb %p\n", urb
->buffer_length
, aurb
);
597 DPRINTF("husb: submit failed. errno %d\n", errno
);
605 return USB_RET_STALL
;
609 usb_defer_packet(p
, async_cancel
, aurb
);
610 return USB_RET_ASYNC
;
613 static int do_token_setup(USBDevice
*dev
, USBPacket
*p
)
615 USBHostDevice
*s
= (USBHostDevice
*) dev
;
619 return USB_RET_STALL
;
622 memcpy(&s
->ctrl
.req
, p
->data
, 8);
623 s
->ctrl
.len
= le16_to_cpu(s
->ctrl
.req
.wLength
);
625 s
->ctrl
.state
= CTRL_STATE_SETUP
;
627 if (s
->ctrl
.req
.bRequestType
& USB_DIR_IN
) {
628 ret
= usb_host_handle_control(s
, p
);
633 if (ret
< s
->ctrl
.len
) {
636 s
->ctrl
.state
= CTRL_STATE_DATA
;
638 if (s
->ctrl
.len
== 0) {
639 s
->ctrl
.state
= CTRL_STATE_ACK
;
641 s
->ctrl
.state
= CTRL_STATE_DATA
;
648 static int do_token_in(USBDevice
*dev
, USBPacket
*p
)
650 USBHostDevice
*s
= (USBHostDevice
*) dev
;
654 return usb_host_handle_data(s
, p
);
657 switch(s
->ctrl
.state
) {
659 if (!(s
->ctrl
.req
.bRequestType
& USB_DIR_IN
)) {
660 ret
= usb_host_handle_control(s
, p
);
661 if (ret
== USB_RET_ASYNC
) {
662 return USB_RET_ASYNC
;
664 s
->ctrl
.state
= CTRL_STATE_IDLE
;
665 return ret
> 0 ? 0 : ret
;
670 case CTRL_STATE_DATA
:
671 if (s
->ctrl
.req
.bRequestType
& USB_DIR_IN
) {
672 int len
= s
->ctrl
.len
- s
->ctrl
.offset
;
676 memcpy(p
->data
, s
->ctrl
.buffer
+ s
->ctrl
.offset
, len
);
677 s
->ctrl
.offset
+= len
;
678 if (s
->ctrl
.offset
>= s
->ctrl
.len
) {
679 s
->ctrl
.state
= CTRL_STATE_ACK
;
684 s
->ctrl
.state
= CTRL_STATE_IDLE
;
685 return USB_RET_STALL
;
688 return USB_RET_STALL
;
692 static int do_token_out(USBDevice
*dev
, USBPacket
*p
)
694 USBHostDevice
*s
= (USBHostDevice
*) dev
;
697 return usb_host_handle_data(s
, p
);
700 switch(s
->ctrl
.state
) {
702 if (s
->ctrl
.req
.bRequestType
& USB_DIR_IN
) {
703 s
->ctrl
.state
= CTRL_STATE_IDLE
;
706 /* ignore additional output */
710 case CTRL_STATE_DATA
:
711 if (!(s
->ctrl
.req
.bRequestType
& USB_DIR_IN
)) {
712 int len
= s
->ctrl
.len
- s
->ctrl
.offset
;
716 memcpy(s
->ctrl
.buffer
+ s
->ctrl
.offset
, p
->data
, len
);
717 s
->ctrl
.offset
+= len
;
718 if (s
->ctrl
.offset
>= s
->ctrl
.len
) {
719 s
->ctrl
.state
= CTRL_STATE_ACK
;
724 s
->ctrl
.state
= CTRL_STATE_IDLE
;
725 return USB_RET_STALL
;
728 return USB_RET_STALL
;
734 * Called by the HC (host controller).
736 * Returns length of the transaction or one of the USB_RET_XXX codes.
738 static int usb_host_handle_packet(USBDevice
*s
, USBPacket
*p
)
742 s
->state
= USB_STATE_ATTACHED
;
746 s
->state
= USB_STATE_NOTATTACHED
;
750 s
->remote_wakeup
= 0;
752 s
->state
= USB_STATE_DEFAULT
;
753 s
->info
->handle_reset(s
);
757 /* Rest of the PIDs must match our address */
758 if (s
->state
< USB_STATE_DEFAULT
|| p
->devaddr
!= s
->addr
) {
759 return USB_RET_NODEV
;
763 case USB_TOKEN_SETUP
:
764 return do_token_setup(s
, p
);
767 return do_token_in(s
, p
);
770 return do_token_out(s
, p
);
773 return USB_RET_STALL
;
777 /* returns 1 on problem encountered or 0 for success */
778 static int usb_linux_update_endp_table(USBHostDevice
*s
)
780 uint8_t *descriptors
;
781 uint8_t devep
, type
, configuration
, alt_interface
;
782 struct usb_ctrltransfer ct
;
783 int interface
, ret
, length
, i
;
785 ct
.bRequestType
= USB_DIR_IN
;
786 ct
.bRequest
= USB_REQ_GET_CONFIGURATION
;
790 ct
.data
= &configuration
;
793 ret
= ioctl(s
->fd
, USBDEVFS_CONTROL
, &ct
);
795 perror("usb_linux_update_endp_table");
799 /* in address state */
800 if (configuration
== 0) {
804 /* get the desired configuration, interface, and endpoint descriptors
805 * from device description */
806 descriptors
= &s
->descr
[18];
807 length
= s
->descr_len
- 18;
810 if (descriptors
[i
+ 1] != USB_DT_CONFIG
||
811 descriptors
[i
+ 5] != configuration
) {
812 DPRINTF("invalid descriptor data - configuration\n");
818 if (descriptors
[i
+ 1] != USB_DT_INTERFACE
||
819 (descriptors
[i
+ 1] == USB_DT_INTERFACE
&&
820 descriptors
[i
+ 4] == 0)) {
825 interface
= descriptors
[i
+ 2];
827 ct
.bRequestType
= USB_DIR_IN
| USB_RECIP_INTERFACE
;
828 ct
.bRequest
= USB_REQ_GET_INTERFACE
;
830 ct
.wIndex
= interface
;
832 ct
.data
= &alt_interface
;
835 ret
= ioctl(s
->fd
, USBDEVFS_CONTROL
, &ct
);
837 alt_interface
= interface
;
840 /* the current interface descriptor is the active interface
841 * and has endpoints */
842 if (descriptors
[i
+ 3] != alt_interface
) {
847 /* advance to the endpoints */
848 while (i
< length
&& descriptors
[i
+1] != USB_DT_ENDPOINT
) {
856 if (descriptors
[i
+ 1] != USB_DT_ENDPOINT
) {
860 devep
= descriptors
[i
+ 2];
861 switch (descriptors
[i
+ 3] & 0x3) {
863 type
= USBDEVFS_URB_TYPE_CONTROL
;
866 type
= USBDEVFS_URB_TYPE_ISO
;
869 type
= USBDEVFS_URB_TYPE_BULK
;
872 type
= USBDEVFS_URB_TYPE_INTERRUPT
;
875 DPRINTF("usb_host: malformed endpoint type\n");
876 type
= USBDEVFS_URB_TYPE_BULK
;
878 s
->endp_table
[(devep
& 0xf) - 1].type
= type
;
879 s
->endp_table
[(devep
& 0xf) - 1].halted
= 0;
887 static int usb_host_open(USBHostDevice
*dev
, int bus_num
,
888 int addr
, const char *prod_name
)
891 struct usbdevfs_connectinfo ci
;
897 printf("husb: open device %d.%d\n", bus_num
, addr
);
899 if (!usb_host_device_path
) {
900 perror("husb: USB Host Device Path not set");
903 snprintf(buf
, sizeof(buf
), "%s/%03d/%03d", usb_host_device_path
,
905 fd
= open(buf
, O_RDWR
| O_NONBLOCK
);
910 DPRINTF("husb: opened %s\n", buf
);
912 dev
->bus_num
= bus_num
;
916 /* read the device description */
917 dev
->descr_len
= read(fd
, dev
->descr
, sizeof(dev
->descr
));
918 if (dev
->descr_len
<= 0) {
919 perror("husb: reading device data failed");
926 printf("=== begin dumping device descriptor data ===\n");
927 for (x
= 0; x
< dev
->descr_len
; x
++) {
928 printf("%02x ", dev
->descr
[x
]);
930 printf("\n=== end dumping device descriptor data ===\n");
936 * Initial configuration is -1 which makes us claim first
937 * available config. We used to start with 1, which does not
938 * always work. I've seen devices where first config starts
941 if (!usb_host_claim_interfaces(dev
, -1)) {
945 ret
= ioctl(fd
, USBDEVFS_CONNECTINFO
, &ci
);
947 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
951 printf("husb: grabbed usb device %d.%d\n", bus_num
, addr
);
953 ret
= usb_linux_update_endp_table(dev
);
959 dev
->dev
.speed
= USB_SPEED_LOW
;
961 dev
->dev
.speed
= USB_SPEED_HIGH
;
964 if (!prod_name
|| prod_name
[0] == '\0') {
965 snprintf(dev
->dev
.product_desc
, sizeof(dev
->dev
.product_desc
),
966 "host:%d.%d", bus_num
, addr
);
968 pstrcpy(dev
->dev
.product_desc
, sizeof(dev
->dev
.product_desc
),
972 /* USB devio uses 'write' flag to check for async completions */
973 qemu_set_fd_handler(dev
->fd
, NULL
, async_complete
, dev
);
975 usb_device_attach(&dev
->dev
);
986 static int usb_host_close(USBHostDevice
*dev
)
992 qemu_set_fd_handler(dev
->fd
, NULL
, NULL
, NULL
);
996 usb_device_detach(&dev
->dev
);
997 ioctl(dev
->fd
, USBDEVFS_RESET
);
1003 static void usb_host_exit_notifier(struct Notifier
* n
)
1005 USBHostDevice
*s
= container_of(n
, USBHostDevice
, exit
);
1008 ioctl(s
->fd
, USBDEVFS_RESET
);
1012 static int usb_host_initfn(USBDevice
*dev
)
1014 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
1016 dev
->auto_attach
= 0;
1018 QTAILQ_INSERT_TAIL(&hostdevs
, s
, next
);
1019 s
->exit
.notify
= usb_host_exit_notifier
;
1020 qemu_add_exit_notifier(&s
->exit
);
1021 usb_host_auto_check(NULL
);
1025 static struct USBDeviceInfo usb_host_dev_info
= {
1026 .product_desc
= "USB Host Device",
1027 .qdev
.name
= "usb-host",
1028 .qdev
.size
= sizeof(USBHostDevice
),
1029 .init
= usb_host_initfn
,
1030 .handle_packet
= usb_host_handle_packet
,
1031 .handle_reset
= usb_host_handle_reset
,
1032 .handle_destroy
= usb_host_handle_destroy
,
1033 .usbdevice_name
= "host",
1034 .usbdevice_init
= usb_host_device_open
,
1035 .qdev
.props
= (Property
[]) {
1036 DEFINE_PROP_UINT32("hostbus", USBHostDevice
, match
.bus_num
, 0),
1037 DEFINE_PROP_UINT32("hostaddr", USBHostDevice
, match
.addr
, 0),
1038 DEFINE_PROP_HEX32("vendorid", USBHostDevice
, match
.vendor_id
, 0),
1039 DEFINE_PROP_HEX32("productid", USBHostDevice
, match
.product_id
, 0),
1040 DEFINE_PROP_END_OF_LIST(),
1044 static void usb_host_register_devices(void)
1046 usb_qdev_register(&usb_host_dev_info
);
1048 device_init(usb_host_register_devices
)
1050 USBDevice
*usb_host_device_open(const char *devname
)
1052 struct USBAutoFilter filter
;
1056 dev
= usb_create(NULL
/* FIXME */, "usb-host");
1058 if (strstr(devname
, "auto:")) {
1059 if (parse_filter(devname
, &filter
) < 0) {
1063 if ((p
= strchr(devname
, '.'))) {
1064 filter
.bus_num
= strtoul(devname
, NULL
, 0);
1065 filter
.addr
= strtoul(p
+ 1, NULL
, 0);
1066 filter
.vendor_id
= 0;
1067 filter
.product_id
= 0;
1068 } else if ((p
= strchr(devname
, ':'))) {
1071 filter
.vendor_id
= strtoul(devname
, NULL
, 16);
1072 filter
.product_id
= strtoul(p
+ 1, NULL
, 16);
1078 qdev_prop_set_uint32(&dev
->qdev
, "hostbus", filter
.bus_num
);
1079 qdev_prop_set_uint32(&dev
->qdev
, "hostaddr", filter
.addr
);
1080 qdev_prop_set_uint32(&dev
->qdev
, "vendorid", filter
.vendor_id
);
1081 qdev_prop_set_uint32(&dev
->qdev
, "productid", filter
.product_id
);
1082 qdev_init_nofail(&dev
->qdev
);
1086 qdev_free(&dev
->qdev
);
1090 int usb_host_device_close(const char *devname
)
1093 char product_name
[PRODUCT_NAME_SZ
];
1097 if (strstr(devname
, "auto:")) {
1098 return usb_host_auto_del(devname
);
1100 if (usb_host_find_device(&bus_num
, &addr
, product_name
,
1101 sizeof(product_name
), devname
) < 0) {
1104 s
= hostdev_find(bus_num
, addr
);
1106 usb_device_delete_addr(s
->bus_num
, s
->dev
.addr
);
1114 static int get_tag_value(char *buf
, int buf_size
,
1115 const char *str
, const char *tag
,
1116 const char *stopchars
)
1120 p
= strstr(str
, tag
);
1125 while (qemu_isspace(*p
)) {
1129 while (*p
!= '\0' && !strchr(stopchars
, *p
)) {
1130 if ((q
- buf
) < (buf_size
- 1)) {
1140 * Use /proc/bus/usb/devices or /dev/bus/usb/devices file to determine
1141 * host's USB devices. This is legacy support since many distributions
1142 * are moving to /sys/bus/usb
1144 static int usb_host_scan_dev(void *opaque
, USBScanFunc
*func
)
1149 int bus_num
, addr
, speed
, device_count
, class_id
, product_id
, vendor_id
;
1150 char product_name
[512];
1153 if (!usb_host_device_path
) {
1154 perror("husb: USB Host Device Path not set");
1157 snprintf(line
, sizeof(line
), "%s/devices", usb_host_device_path
);
1158 f
= fopen(line
, "r");
1160 perror("husb: cannot open devices file");
1165 bus_num
= addr
= speed
= class_id
= product_id
= vendor_id
= 0;
1167 if (fgets(line
, sizeof(line
), f
) == NULL
) {
1170 if (strlen(line
) > 0) {
1171 line
[strlen(line
) - 1] = '\0';
1173 if (line
[0] == 'T' && line
[1] == ':') {
1174 if (device_count
&& (vendor_id
|| product_id
)) {
1175 /* New device. Add the previously discovered device. */
1176 ret
= func(opaque
, bus_num
, addr
, class_id
, vendor_id
,
1177 product_id
, product_name
, speed
);
1182 if (get_tag_value(buf
, sizeof(buf
), line
, "Bus=", " ") < 0) {
1185 bus_num
= atoi(buf
);
1186 if (get_tag_value(buf
, sizeof(buf
), line
, "Dev#=", " ") < 0) {
1190 if (get_tag_value(buf
, sizeof(buf
), line
, "Spd=", " ") < 0) {
1193 if (!strcmp(buf
, "480")) {
1194 speed
= USB_SPEED_HIGH
;
1195 } else if (!strcmp(buf
, "1.5")) {
1196 speed
= USB_SPEED_LOW
;
1198 speed
= USB_SPEED_FULL
;
1200 product_name
[0] = '\0';
1205 } else if (line
[0] == 'P' && line
[1] == ':') {
1206 if (get_tag_value(buf
, sizeof(buf
), line
, "Vendor=", " ") < 0) {
1209 vendor_id
= strtoul(buf
, NULL
, 16);
1210 if (get_tag_value(buf
, sizeof(buf
), line
, "ProdID=", " ") < 0) {
1213 product_id
= strtoul(buf
, NULL
, 16);
1214 } else if (line
[0] == 'S' && line
[1] == ':') {
1215 if (get_tag_value(buf
, sizeof(buf
), line
, "Product=", "") < 0) {
1218 pstrcpy(product_name
, sizeof(product_name
), buf
);
1219 } else if (line
[0] == 'D' && line
[1] == ':') {
1220 if (get_tag_value(buf
, sizeof(buf
), line
, "Cls=", " (") < 0) {
1223 class_id
= strtoul(buf
, NULL
, 16);
1227 if (device_count
&& (vendor_id
|| product_id
)) {
1228 /* Add the last device. */
1229 ret
= func(opaque
, bus_num
, addr
, class_id
, vendor_id
,
1230 product_id
, product_name
, speed
);
1240 * Read sys file-system device file
1242 * @line address of buffer to put file contents in
1243 * @line_size size of line
1244 * @device_file path to device file (printf format string)
1245 * @device_name device being opened (inserted into device_file)
1247 * @return 0 failed, 1 succeeded ('line' contains data)
1249 static int usb_host_read_file(char *line
, size_t line_size
,
1250 const char *device_file
, const char *device_name
)
1254 char filename
[PATH_MAX
];
1256 snprintf(filename
, PATH_MAX
, USBSYSBUS_PATH
"/devices/%s/%s", device_name
,
1258 f
= fopen(filename
, "r");
1260 ret
= fgets(line
, line_size
, f
) != NULL
;
1268 * Use /sys/bus/usb/devices/ directory to determine host's USB
1271 * This code is based on Robert Schiele's original patches posted to
1272 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1274 static int usb_host_scan_sys(void *opaque
, USBScanFunc
*func
)
1278 int bus_num
, addr
, speed
, class_id
, product_id
, vendor_id
;
1280 char product_name
[512];
1283 dir
= opendir(USBSYSBUS_PATH
"/devices");
1285 perror("husb: cannot open devices directory");
1289 while ((de
= readdir(dir
))) {
1290 if (de
->d_name
[0] != '.' && !strchr(de
->d_name
, ':')) {
1291 char *tmpstr
= de
->d_name
;
1292 if (!strncmp(de
->d_name
, "usb", 3)) {
1295 bus_num
= atoi(tmpstr
);
1297 if (!usb_host_read_file(line
, sizeof(line
), "devnum", de
->d_name
)) {
1300 if (sscanf(line
, "%d", &addr
) != 1) {
1303 if (!usb_host_read_file(line
, sizeof(line
), "bDeviceClass",
1307 if (sscanf(line
, "%x", &class_id
) != 1) {
1311 if (!usb_host_read_file(line
, sizeof(line
), "idVendor",
1315 if (sscanf(line
, "%x", &vendor_id
) != 1) {
1318 if (!usb_host_read_file(line
, sizeof(line
), "idProduct",
1322 if (sscanf(line
, "%x", &product_id
) != 1) {
1325 if (!usb_host_read_file(line
, sizeof(line
), "product",
1329 if (strlen(line
) > 0) {
1330 line
[strlen(line
) - 1] = '\0';
1332 pstrcpy(product_name
, sizeof(product_name
), line
);
1335 if (!usb_host_read_file(line
, sizeof(line
), "speed", de
->d_name
)) {
1338 if (!strcmp(line
, "480\n")) {
1339 speed
= USB_SPEED_HIGH
;
1340 } else if (!strcmp(line
, "1.5\n")) {
1341 speed
= USB_SPEED_LOW
;
1343 speed
= USB_SPEED_FULL
;
1346 ret
= func(opaque
, bus_num
, addr
, class_id
, vendor_id
,
1347 product_id
, product_name
, speed
);
1361 * Determine how to access the host's USB devices and call the
1362 * specific support function.
1364 static int usb_host_scan(void *opaque
, USBScanFunc
*func
)
1366 Monitor
*mon
= cur_mon
;
1370 const char *fs_type
[] = {"unknown", "proc", "dev", "sys"};
1371 char devpath
[PATH_MAX
];
1373 /* only check the host once */
1375 dir
= opendir(USBSYSBUS_PATH
"/devices");
1377 /* devices found in /dev/bus/usb/ (yes - not a mistake!) */
1378 strcpy(devpath
, USBDEVBUS_PATH
);
1379 usb_fs_type
= USB_FS_SYS
;
1381 DPRINTF(USBDBG_DEVOPENED
, USBSYSBUS_PATH
);
1384 f
= fopen(USBPROCBUS_PATH
"/devices", "r");
1386 /* devices found in /proc/bus/usb/ */
1387 strcpy(devpath
, USBPROCBUS_PATH
);
1388 usb_fs_type
= USB_FS_PROC
;
1390 DPRINTF(USBDBG_DEVOPENED
, USBPROCBUS_PATH
);
1393 /* try additional methods if an access method hasn't been found yet */
1394 f
= fopen(USBDEVBUS_PATH
"/devices", "r");
1396 /* devices found in /dev/bus/usb/ */
1397 strcpy(devpath
, USBDEVBUS_PATH
);
1398 usb_fs_type
= USB_FS_DEV
;
1400 DPRINTF(USBDBG_DEVOPENED
, USBDEVBUS_PATH
);
1406 monitor_printf(mon
, "husb: unable to access USB devices\n");
1411 /* the module setting (used later for opening devices) */
1412 usb_host_device_path
= qemu_mallocz(strlen(devpath
)+1);
1413 strcpy(usb_host_device_path
, devpath
);
1415 monitor_printf(mon
, "husb: using %s file-system with %s\n",
1416 fs_type
[usb_fs_type
], usb_host_device_path
);
1420 switch (usb_fs_type
) {
1423 ret
= usb_host_scan_dev(opaque
, func
);
1426 ret
= usb_host_scan_sys(opaque
, func
);
1435 static QEMUTimer
*usb_auto_timer
;
1437 static int usb_host_auto_scan(void *opaque
, int bus_num
, int addr
,
1438 int class_id
, int vendor_id
, int product_id
,
1439 const char *product_name
, int speed
)
1441 struct USBAutoFilter
*f
;
1442 struct USBHostDevice
*s
;
1448 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1451 if (f
->bus_num
> 0 && f
->bus_num
!= bus_num
) {
1454 if (f
->addr
> 0 && f
->addr
!= addr
) {
1458 if (f
->vendor_id
> 0 && f
->vendor_id
!= vendor_id
) {
1462 if (f
->product_id
> 0 && f
->product_id
!= product_id
) {
1465 /* We got a match */
1467 /* Already attached ? */
1471 DPRINTF("husb: auto open: bus_num %d addr %d\n", bus_num
, addr
);
1473 usb_host_open(s
, bus_num
, addr
, product_name
);
1479 static void usb_host_auto_check(void *unused
)
1481 struct USBHostDevice
*s
;
1482 int unconnected
= 0;
1484 usb_host_scan(NULL
, usb_host_auto_scan
);
1486 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1492 if (unconnected
== 0) {
1493 /* nothing to watch */
1494 if (usb_auto_timer
) {
1495 qemu_del_timer(usb_auto_timer
);
1500 if (!usb_auto_timer
) {
1501 usb_auto_timer
= qemu_new_timer(rt_clock
, usb_host_auto_check
, NULL
);
1502 if (!usb_auto_timer
) {
1506 qemu_mod_timer(usb_auto_timer
, qemu_get_clock(rt_clock
) + 2000);
1510 * Autoconnect filter
1512 * auto:bus:dev[:vid:pid]
1513 * auto:bus.dev[:vid:pid]
1515 * bus - bus number (dec, * means any)
1516 * dev - device number (dec, * means any)
1517 * vid - vendor id (hex, * means any)
1518 * pid - product id (hex, * means any)
1520 * See 'lsusb' output.
1522 static int parse_filter(const char *spec
, struct USBAutoFilter
*f
)
1524 enum { BUS
, DEV
, VID
, PID
, DONE
};
1525 const char *p
= spec
;
1533 for (i
= BUS
; i
< DONE
; i
++) {
1534 p
= strpbrk(p
, ":.");
1544 case BUS
: f
->bus_num
= strtol(p
, NULL
, 10); break;
1545 case DEV
: f
->addr
= strtol(p
, NULL
, 10); break;
1546 case VID
: f
->vendor_id
= strtol(p
, NULL
, 16); break;
1547 case PID
: f
->product_id
= strtol(p
, NULL
, 16); break;
1552 fprintf(stderr
, "husb: invalid auto filter spec %s\n", spec
);
1559 /**********************/
1560 /* USB host device info */
1562 struct usb_class_info
{
1564 const char *class_name
;
1567 static const struct usb_class_info usb_class_info
[] = {
1568 { USB_CLASS_AUDIO
, "Audio"},
1569 { USB_CLASS_COMM
, "Communication"},
1570 { USB_CLASS_HID
, "HID"},
1571 { USB_CLASS_HUB
, "Hub" },
1572 { USB_CLASS_PHYSICAL
, "Physical" },
1573 { USB_CLASS_PRINTER
, "Printer" },
1574 { USB_CLASS_MASS_STORAGE
, "Storage" },
1575 { USB_CLASS_CDC_DATA
, "Data" },
1576 { USB_CLASS_APP_SPEC
, "Application Specific" },
1577 { USB_CLASS_VENDOR_SPEC
, "Vendor Specific" },
1578 { USB_CLASS_STILL_IMAGE
, "Still Image" },
1579 { USB_CLASS_CSCID
, "Smart Card" },
1580 { USB_CLASS_CONTENT_SEC
, "Content Security" },
1584 static const char *usb_class_str(uint8_t class)
1586 const struct usb_class_info
*p
;
1587 for(p
= usb_class_info
; p
->class != -1; p
++) {
1588 if (p
->class == class) {
1592 return p
->class_name
;
1595 static void usb_info_device(Monitor
*mon
, int bus_num
, int addr
, int class_id
,
1596 int vendor_id
, int product_id
,
1597 const char *product_name
,
1600 const char *class_str
, *speed_str
;
1606 case USB_SPEED_FULL
:
1609 case USB_SPEED_HIGH
:
1617 monitor_printf(mon
, " Device %d.%d, speed %s Mb/s\n",
1618 bus_num
, addr
, speed_str
);
1619 class_str
= usb_class_str(class_id
);
1621 monitor_printf(mon
, " %s:", class_str
);
1623 monitor_printf(mon
, " Class %02x:", class_id
);
1625 monitor_printf(mon
, " USB device %04x:%04x", vendor_id
, product_id
);
1626 if (product_name
[0] != '\0') {
1627 monitor_printf(mon
, ", %s", product_name
);
1629 monitor_printf(mon
, "\n");
1632 static int usb_host_info_device(void *opaque
, int bus_num
, int addr
,
1634 int vendor_id
, int product_id
,
1635 const char *product_name
,
1638 Monitor
*mon
= opaque
;
1640 usb_info_device(mon
, bus_num
, addr
, class_id
, vendor_id
, product_id
,
1641 product_name
, speed
);
1645 static void dec2str(int val
, char *str
, size_t size
)
1648 snprintf(str
, size
, "*");
1650 snprintf(str
, size
, "%d", val
);
1654 static void hex2str(int val
, char *str
, size_t size
)
1657 snprintf(str
, size
, "*");
1659 snprintf(str
, size
, "%04x", val
);
1663 void usb_host_info(Monitor
*mon
)
1665 struct USBAutoFilter
*f
;
1666 struct USBHostDevice
*s
;
1668 usb_host_scan(mon
, usb_host_info_device
);
1670 if (QTAILQ_EMPTY(&hostdevs
)) {
1674 monitor_printf(mon
, " Auto filters:\n");
1675 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1676 char bus
[10], addr
[10], vid
[10], pid
[10];
1678 dec2str(f
->bus_num
, bus
, sizeof(bus
));
1679 dec2str(f
->addr
, addr
, sizeof(addr
));
1680 hex2str(f
->vendor_id
, vid
, sizeof(vid
));
1681 hex2str(f
->product_id
, pid
, sizeof(pid
));
1682 monitor_printf(mon
, " Device %s.%s ID %s:%s\n",
1683 bus
, addr
, vid
, pid
);