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"
38 #include <sys/ioctl.h>
41 #include <linux/usbdevice_fs.h>
42 #include <linux/version.h>
45 /* We redefine it to avoid version problems */
46 struct usb_ctrltransfer
{
56 struct usb_ctrlrequest
{
64 typedef int USBScanFunc(void *opaque
, int bus_num
, int addr
, int class_id
,
65 int vendor_id
, int product_id
,
66 const char *product_name
, int speed
);
71 #define DPRINTF printf
76 #define USBDBG_DEVOPENED "husb: opened %s/devices\n"
78 #define USBPROCBUS_PATH "/proc/bus/usb"
79 #define PRODUCT_NAME_SZ 32
80 #define MAX_ENDPOINTS 16
81 #define USBDEVBUS_PATH "/dev/bus/usb"
82 #define USBSYSBUS_PATH "/sys/bus/usb"
84 static char *usb_host_device_path
;
91 static int usb_fs_type
;
93 /* endpoint association data */
107 * Control transfer state.
108 * Note that 'buffer' _must_ follow 'req' field because
109 * we need contigious buffer when we submit control URB.
115 struct usb_ctrlrequest req
;
116 uint8_t buffer
[8192];
119 struct USBAutoFilter
{
126 typedef struct USBHostDevice
{
136 struct ctrl_struct ctrl
;
137 struct endp_data endp_table
[MAX_ENDPOINTS
];
139 /* Host side address */
142 struct USBAutoFilter match
;
144 QTAILQ_ENTRY(USBHostDevice
) next
;
147 static QTAILQ_HEAD(, USBHostDevice
) hostdevs
= QTAILQ_HEAD_INITIALIZER(hostdevs
);
149 static int usb_host_close(USBHostDevice
*dev
);
150 static int parse_filter(const char *spec
, struct USBAutoFilter
*f
);
151 static void usb_host_auto_check(void *unused
);
153 static int is_isoc(USBHostDevice
*s
, int ep
)
155 return s
->endp_table
[ep
- 1].type
== USBDEVFS_URB_TYPE_ISO
;
158 static int is_halted(USBHostDevice
*s
, int ep
)
160 return s
->endp_table
[ep
- 1].halted
;
163 static void clear_halt(USBHostDevice
*s
, int ep
)
165 s
->endp_table
[ep
- 1].halted
= 0;
168 static void set_halt(USBHostDevice
*s
, int ep
)
170 s
->endp_table
[ep
- 1].halted
= 1;
175 * We always allocate one isoc descriptor even for bulk transfers
176 * to simplify allocation and casts.
178 typedef struct AsyncURB
180 struct usbdevfs_urb urb
;
181 struct usbdevfs_iso_packet_desc isocpd
;
187 static AsyncURB
*async_alloc(void)
189 return (AsyncURB
*) qemu_mallocz(sizeof(AsyncURB
));
192 static void async_free(AsyncURB
*aurb
)
197 static void async_complete_ctrl(USBHostDevice
*s
, USBPacket
*p
)
199 switch(s
->ctrl
.state
) {
200 case CTRL_STATE_SETUP
:
201 if (p
->len
< s
->ctrl
.len
)
202 s
->ctrl
.len
= p
->len
;
203 s
->ctrl
.state
= CTRL_STATE_DATA
;
208 s
->ctrl
.state
= CTRL_STATE_IDLE
;
217 static void async_complete(void *opaque
)
219 USBHostDevice
*s
= opaque
;
225 int r
= ioctl(s
->fd
, USBDEVFS_REAPURBNDELAY
, &aurb
);
227 if (errno
== EAGAIN
) {
230 if (errno
== ENODEV
&& !s
->closing
) {
231 printf("husb: device %d.%d disconnected\n",
232 s
->bus_num
, s
->addr
);
234 usb_host_auto_check(NULL
);
238 DPRINTF("husb: async. reap urb failed errno %d\n", errno
);
244 DPRINTF("husb: async completed. aurb %p status %d alen %d\n",
245 aurb
, aurb
->urb
.status
, aurb
->urb
.actual_length
);
248 switch (aurb
->urb
.status
) {
250 p
->len
= aurb
->urb
.actual_length
;
251 if (aurb
->urb
.type
== USBDEVFS_URB_TYPE_CONTROL
) {
252 async_complete_ctrl(s
, p
);
257 set_halt(s
, p
->devep
);
258 p
->len
= USB_RET_STALL
;
262 p
->len
= USB_RET_NAK
;
266 usb_packet_complete(p
);
273 static void async_cancel(USBPacket
*unused
, void *opaque
)
275 AsyncURB
*aurb
= opaque
;
276 USBHostDevice
*s
= aurb
->hdev
;
278 DPRINTF("husb: async cancel. aurb %p\n", aurb
);
280 /* Mark it as dead (see async_complete above) */
283 int r
= ioctl(s
->fd
, USBDEVFS_DISCARDURB
, aurb
);
285 DPRINTF("husb: async. discard urb failed errno %d\n", errno
);
289 static int usb_host_claim_interfaces(USBHostDevice
*dev
, int configuration
)
291 int dev_descr_len
, config_descr_len
;
292 int interface
, nb_interfaces
;
295 if (configuration
== 0) /* address state - ignore */
298 DPRINTF("husb: claiming interfaces. config %d\n", configuration
);
301 dev_descr_len
= dev
->descr
[0];
302 if (dev_descr_len
> dev
->descr_len
) {
307 while (i
< dev
->descr_len
) {
308 DPRINTF("husb: i is %d, descr_len is %d, dl %d, dt %d\n",
310 dev
->descr
[i
], dev
->descr
[i
+1]);
312 if (dev
->descr
[i
+1] != USB_DT_CONFIG
) {
316 config_descr_len
= dev
->descr
[i
];
318 printf("husb: config #%d need %d\n", dev
->descr
[i
+ 5], configuration
);
320 if (configuration
< 0 || configuration
== dev
->descr
[i
+ 5]) {
321 configuration
= dev
->descr
[i
+ 5];
325 i
+= config_descr_len
;
328 if (i
>= dev
->descr_len
) {
330 "husb: update iface failed. no matching configuration\n");
333 nb_interfaces
= dev
->descr
[i
+ 4];
335 #ifdef USBDEVFS_DISCONNECT
336 /* earlier Linux 2.4 do not support that */
338 struct usbdevfs_ioctl ctrl
;
339 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
340 ctrl
.ioctl_code
= USBDEVFS_DISCONNECT
;
341 ctrl
.ifno
= interface
;
342 ret
= ioctl(dev
->fd
, USBDEVFS_IOCTL
, &ctrl
);
343 if (ret
< 0 && errno
!= ENODATA
) {
344 perror("USBDEVFS_DISCONNECT");
351 /* XXX: only grab if all interfaces are free */
352 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
353 ret
= ioctl(dev
->fd
, USBDEVFS_CLAIMINTERFACE
, &interface
);
355 if (errno
== EBUSY
) {
356 printf("husb: update iface. device already grabbed\n");
358 perror("husb: failed to claim interface");
365 printf("husb: %d interfaces claimed for configuration %d\n",
366 nb_interfaces
, configuration
);
368 dev
->ninterfaces
= nb_interfaces
;
369 dev
->configuration
= configuration
;
373 static int usb_host_release_interfaces(USBHostDevice
*s
)
377 DPRINTF("husb: releasing interfaces\n");
379 for (i
= 0; i
< s
->ninterfaces
; i
++) {
380 ret
= ioctl(s
->fd
, USBDEVFS_RELEASEINTERFACE
, &i
);
382 perror("husb: failed to release interface");
390 static void usb_host_handle_reset(USBDevice
*dev
)
392 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
394 DPRINTF("husb: reset device %u.%u\n", s
->bus_num
, s
->addr
);
396 ioctl(s
->fd
, USBDEVFS_RESET
);
398 usb_host_claim_interfaces(s
, s
->configuration
);
401 static void usb_host_handle_destroy(USBDevice
*dev
)
403 USBHostDevice
*s
= (USBHostDevice
*)dev
;
406 QTAILQ_REMOVE(&hostdevs
, s
, next
);
409 static int usb_linux_update_endp_table(USBHostDevice
*s
);
411 static int usb_host_handle_data(USBHostDevice
*s
, USBPacket
*p
)
413 struct usbdevfs_urb
*urb
;
417 aurb
= async_alloc();
423 if (p
->pid
== USB_TOKEN_IN
) {
424 urb
->endpoint
= p
->devep
| 0x80;
426 urb
->endpoint
= p
->devep
;
429 if (is_halted(s
, p
->devep
)) {
430 ret
= ioctl(s
->fd
, USBDEVFS_CLEAR_HALT
, &urb
->endpoint
);
432 DPRINTF("husb: failed to clear halt. ep 0x%x errno %d\n",
433 urb
->endpoint
, errno
);
436 clear_halt(s
, p
->devep
);
439 urb
->buffer
= p
->data
;
440 urb
->buffer_length
= p
->len
;
442 if (is_isoc(s
, p
->devep
)) {
443 /* Setup ISOC transfer */
444 urb
->type
= USBDEVFS_URB_TYPE_ISO
;
445 urb
->flags
= USBDEVFS_URB_ISO_ASAP
;
446 urb
->number_of_packets
= 1;
447 urb
->iso_frame_desc
[0].length
= p
->len
;
449 /* Setup bulk transfer */
450 urb
->type
= USBDEVFS_URB_TYPE_BULK
;
453 urb
->usercontext
= s
;
455 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
457 DPRINTF("husb: data submit. ep 0x%x len %u aurb %p\n",
458 urb
->endpoint
, p
->len
, aurb
);
461 DPRINTF("husb: submit failed. errno %d\n", errno
);
469 return USB_RET_STALL
;
473 usb_defer_packet(p
, async_cancel
, aurb
);
474 return USB_RET_ASYNC
;
477 static int ctrl_error(void)
479 if (errno
== ETIMEDOUT
) {
482 return USB_RET_STALL
;
486 static int usb_host_set_address(USBHostDevice
*s
, int addr
)
488 DPRINTF("husb: ctrl set addr %u\n", addr
);
493 static int usb_host_set_config(USBHostDevice
*s
, int config
)
495 usb_host_release_interfaces(s
);
497 int ret
= ioctl(s
->fd
, USBDEVFS_SETCONFIGURATION
, &config
);
499 DPRINTF("husb: ctrl set config %d ret %d errno %d\n", config
, ret
, errno
);
504 usb_host_claim_interfaces(s
, config
);
508 static int usb_host_set_interface(USBHostDevice
*s
, int iface
, int alt
)
510 struct usbdevfs_setinterface si
;
513 si
.interface
= iface
;
515 ret
= ioctl(s
->fd
, USBDEVFS_SETINTERFACE
, &si
);
517 DPRINTF("husb: ctrl set iface %d altset %d ret %d errno %d\n",
518 iface
, alt
, ret
, errno
);
523 usb_linux_update_endp_table(s
);
527 static int usb_host_handle_control(USBHostDevice
*s
, USBPacket
*p
)
529 struct usbdevfs_urb
*urb
;
531 int ret
, value
, index
;
535 * Process certain standard device requests.
536 * These are infrequent and are processed synchronously.
538 value
= le16_to_cpu(s
->ctrl
.req
.wValue
);
539 index
= le16_to_cpu(s
->ctrl
.req
.wIndex
);
541 DPRINTF("husb: ctrl type 0x%x req 0x%x val 0x%x index %u len %u\n",
542 s
->ctrl
.req
.bRequestType
, s
->ctrl
.req
.bRequest
, value
, index
,
545 if (s
->ctrl
.req
.bRequestType
== 0) {
546 switch (s
->ctrl
.req
.bRequest
) {
547 case USB_REQ_SET_ADDRESS
:
548 return usb_host_set_address(s
, value
);
550 case USB_REQ_SET_CONFIGURATION
:
551 return usb_host_set_config(s
, value
& 0xff);
555 if (s
->ctrl
.req
.bRequestType
== 1 &&
556 s
->ctrl
.req
.bRequest
== USB_REQ_SET_INTERFACE
) {
557 return usb_host_set_interface(s
, index
, value
);
560 /* The rest are asynchronous */
562 buffer_len
= 8 + s
->ctrl
.len
;
563 if (buffer_len
> sizeof(s
->ctrl
.buffer
)) {
564 fprintf(stderr
, "husb: ctrl buffer too small (%u > %zu)\n",
565 buffer_len
, sizeof(s
->ctrl
.buffer
));
566 return USB_RET_STALL
;
569 aurb
= async_alloc();
574 * Setup ctrl transfer.
576 * s->ctrl is layed out such that data buffer immediately follows
577 * 'req' struct which is exactly what usbdevfs expects.
581 urb
->type
= USBDEVFS_URB_TYPE_CONTROL
;
582 urb
->endpoint
= p
->devep
;
584 urb
->buffer
= &s
->ctrl
.req
;
585 urb
->buffer_length
= buffer_len
;
587 urb
->usercontext
= s
;
589 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
591 DPRINTF("husb: submit ctrl. len %u aurb %p\n", urb
->buffer_length
, aurb
);
594 DPRINTF("husb: submit failed. errno %d\n", errno
);
602 return USB_RET_STALL
;
606 usb_defer_packet(p
, async_cancel
, aurb
);
607 return USB_RET_ASYNC
;
610 static int do_token_setup(USBDevice
*dev
, USBPacket
*p
)
612 USBHostDevice
*s
= (USBHostDevice
*) dev
;
616 return USB_RET_STALL
;
619 memcpy(&s
->ctrl
.req
, p
->data
, 8);
620 s
->ctrl
.len
= le16_to_cpu(s
->ctrl
.req
.wLength
);
622 s
->ctrl
.state
= CTRL_STATE_SETUP
;
624 if (s
->ctrl
.req
.bRequestType
& USB_DIR_IN
) {
625 ret
= usb_host_handle_control(s
, p
);
630 if (ret
< s
->ctrl
.len
) {
633 s
->ctrl
.state
= CTRL_STATE_DATA
;
635 if (s
->ctrl
.len
== 0) {
636 s
->ctrl
.state
= CTRL_STATE_ACK
;
638 s
->ctrl
.state
= CTRL_STATE_DATA
;
645 static int do_token_in(USBDevice
*dev
, USBPacket
*p
)
647 USBHostDevice
*s
= (USBHostDevice
*) dev
;
651 return usb_host_handle_data(s
, p
);
654 switch(s
->ctrl
.state
) {
656 if (!(s
->ctrl
.req
.bRequestType
& USB_DIR_IN
)) {
657 ret
= usb_host_handle_control(s
, p
);
658 if (ret
== USB_RET_ASYNC
) {
659 return USB_RET_ASYNC
;
661 s
->ctrl
.state
= CTRL_STATE_IDLE
;
662 return ret
> 0 ? 0 : ret
;
667 case CTRL_STATE_DATA
:
668 if (s
->ctrl
.req
.bRequestType
& USB_DIR_IN
) {
669 int len
= s
->ctrl
.len
- s
->ctrl
.offset
;
673 memcpy(p
->data
, s
->ctrl
.buffer
+ s
->ctrl
.offset
, len
);
674 s
->ctrl
.offset
+= len
;
675 if (s
->ctrl
.offset
>= s
->ctrl
.len
) {
676 s
->ctrl
.state
= CTRL_STATE_ACK
;
681 s
->ctrl
.state
= CTRL_STATE_IDLE
;
682 return USB_RET_STALL
;
685 return USB_RET_STALL
;
689 static int do_token_out(USBDevice
*dev
, USBPacket
*p
)
691 USBHostDevice
*s
= (USBHostDevice
*) dev
;
694 return usb_host_handle_data(s
, p
);
697 switch(s
->ctrl
.state
) {
699 if (s
->ctrl
.req
.bRequestType
& USB_DIR_IN
) {
700 s
->ctrl
.state
= CTRL_STATE_IDLE
;
703 /* ignore additional output */
707 case CTRL_STATE_DATA
:
708 if (!(s
->ctrl
.req
.bRequestType
& USB_DIR_IN
)) {
709 int len
= s
->ctrl
.len
- s
->ctrl
.offset
;
713 memcpy(s
->ctrl
.buffer
+ s
->ctrl
.offset
, p
->data
, len
);
714 s
->ctrl
.offset
+= len
;
715 if (s
->ctrl
.offset
>= s
->ctrl
.len
) {
716 s
->ctrl
.state
= CTRL_STATE_ACK
;
721 s
->ctrl
.state
= CTRL_STATE_IDLE
;
722 return USB_RET_STALL
;
725 return USB_RET_STALL
;
731 * Called by the HC (host controller).
733 * Returns length of the transaction or one of the USB_RET_XXX codes.
735 static int usb_host_handle_packet(USBDevice
*s
, USBPacket
*p
)
739 s
->state
= USB_STATE_ATTACHED
;
743 s
->state
= USB_STATE_NOTATTACHED
;
747 s
->remote_wakeup
= 0;
749 s
->state
= USB_STATE_DEFAULT
;
750 s
->info
->handle_reset(s
);
754 /* Rest of the PIDs must match our address */
755 if (s
->state
< USB_STATE_DEFAULT
|| p
->devaddr
!= s
->addr
) {
756 return USB_RET_NODEV
;
760 case USB_TOKEN_SETUP
:
761 return do_token_setup(s
, p
);
764 return do_token_in(s
, p
);
767 return do_token_out(s
, p
);
770 return USB_RET_STALL
;
774 /* returns 1 on problem encountered or 0 for success */
775 static int usb_linux_update_endp_table(USBHostDevice
*s
)
777 uint8_t *descriptors
;
778 uint8_t devep
, type
, configuration
, alt_interface
;
779 struct usb_ctrltransfer ct
;
780 int interface
, ret
, length
, i
;
782 ct
.bRequestType
= USB_DIR_IN
;
783 ct
.bRequest
= USB_REQ_GET_CONFIGURATION
;
787 ct
.data
= &configuration
;
790 ret
= ioctl(s
->fd
, USBDEVFS_CONTROL
, &ct
);
792 perror("usb_linux_update_endp_table");
796 /* in address state */
797 if (configuration
== 0) {
801 /* get the desired configuration, interface, and endpoint descriptors
802 * from device description */
803 descriptors
= &s
->descr
[18];
804 length
= s
->descr_len
- 18;
807 if (descriptors
[i
+ 1] != USB_DT_CONFIG
||
808 descriptors
[i
+ 5] != configuration
) {
809 DPRINTF("invalid descriptor data - configuration\n");
815 if (descriptors
[i
+ 1] != USB_DT_INTERFACE
||
816 (descriptors
[i
+ 1] == USB_DT_INTERFACE
&&
817 descriptors
[i
+ 4] == 0)) {
822 interface
= descriptors
[i
+ 2];
824 ct
.bRequestType
= USB_DIR_IN
| USB_RECIP_INTERFACE
;
825 ct
.bRequest
= USB_REQ_GET_INTERFACE
;
827 ct
.wIndex
= interface
;
829 ct
.data
= &alt_interface
;
832 ret
= ioctl(s
->fd
, USBDEVFS_CONTROL
, &ct
);
834 alt_interface
= interface
;
837 /* the current interface descriptor is the active interface
838 * and has endpoints */
839 if (descriptors
[i
+ 3] != alt_interface
) {
844 /* advance to the endpoints */
845 while (i
< length
&& descriptors
[i
+1] != USB_DT_ENDPOINT
) {
853 if (descriptors
[i
+ 1] != USB_DT_ENDPOINT
) {
857 devep
= descriptors
[i
+ 2];
858 switch (descriptors
[i
+ 3] & 0x3) {
860 type
= USBDEVFS_URB_TYPE_CONTROL
;
863 type
= USBDEVFS_URB_TYPE_ISO
;
866 type
= USBDEVFS_URB_TYPE_BULK
;
869 type
= USBDEVFS_URB_TYPE_INTERRUPT
;
872 DPRINTF("usb_host: malformed endpoint type\n");
873 type
= USBDEVFS_URB_TYPE_BULK
;
875 s
->endp_table
[(devep
& 0xf) - 1].type
= type
;
876 s
->endp_table
[(devep
& 0xf) - 1].halted
= 0;
884 static int usb_host_open(USBHostDevice
*dev
, int bus_num
,
885 int addr
, const char *prod_name
)
888 struct usbdevfs_connectinfo ci
;
894 printf("husb: open device %d.%d\n", bus_num
, addr
);
896 if (!usb_host_device_path
) {
897 perror("husb: USB Host Device Path not set");
900 snprintf(buf
, sizeof(buf
), "%s/%03d/%03d", usb_host_device_path
,
902 fd
= open(buf
, O_RDWR
| O_NONBLOCK
);
907 DPRINTF("husb: opened %s\n", buf
);
909 dev
->bus_num
= bus_num
;
913 /* read the device description */
914 dev
->descr_len
= read(fd
, dev
->descr
, sizeof(dev
->descr
));
915 if (dev
->descr_len
<= 0) {
916 perror("husb: reading device data failed");
923 printf("=== begin dumping device descriptor data ===\n");
924 for (x
= 0; x
< dev
->descr_len
; x
++) {
925 printf("%02x ", dev
->descr
[x
]);
927 printf("\n=== end dumping device descriptor data ===\n");
933 * Initial configuration is -1 which makes us claim first
934 * available config. We used to start with 1, which does not
935 * always work. I've seen devices where first config starts
938 if (!usb_host_claim_interfaces(dev
, -1)) {
942 ret
= ioctl(fd
, USBDEVFS_CONNECTINFO
, &ci
);
944 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
948 printf("husb: grabbed usb device %d.%d\n", bus_num
, addr
);
950 ret
= usb_linux_update_endp_table(dev
);
956 dev
->dev
.speed
= USB_SPEED_LOW
;
958 dev
->dev
.speed
= USB_SPEED_HIGH
;
961 if (!prod_name
|| prod_name
[0] == '\0') {
962 snprintf(dev
->dev
.product_desc
, sizeof(dev
->dev
.product_desc
),
963 "host:%d.%d", bus_num
, addr
);
965 pstrcpy(dev
->dev
.product_desc
, sizeof(dev
->dev
.product_desc
),
969 /* USB devio uses 'write' flag to check for async completions */
970 qemu_set_fd_handler(dev
->fd
, NULL
, async_complete
, dev
);
972 usb_device_attach(&dev
->dev
);
983 static int usb_host_close(USBHostDevice
*dev
)
989 qemu_set_fd_handler(dev
->fd
, NULL
, NULL
, NULL
);
993 usb_device_detach(&dev
->dev
);
994 ioctl(dev
->fd
, USBDEVFS_RESET
);
1000 static int usb_host_initfn(USBDevice
*dev
)
1002 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
1004 dev
->auto_attach
= 0;
1006 QTAILQ_INSERT_TAIL(&hostdevs
, s
, next
);
1007 usb_host_auto_check(NULL
);
1011 static struct USBDeviceInfo usb_host_dev_info
= {
1012 .product_desc
= "USB Host Device",
1013 .qdev
.name
= "usb-host",
1014 .qdev
.size
= sizeof(USBHostDevice
),
1015 .init
= usb_host_initfn
,
1016 .handle_packet
= usb_host_handle_packet
,
1017 .handle_reset
= usb_host_handle_reset
,
1018 .handle_destroy
= usb_host_handle_destroy
,
1019 .usbdevice_name
= "host",
1020 .usbdevice_init
= usb_host_device_open
,
1021 .qdev
.props
= (Property
[]) {
1022 DEFINE_PROP_UINT32("hostbus", USBHostDevice
, match
.bus_num
, 0),
1023 DEFINE_PROP_UINT32("hostaddr", USBHostDevice
, match
.addr
, 0),
1024 DEFINE_PROP_HEX32("vendorid", USBHostDevice
, match
.vendor_id
, 0),
1025 DEFINE_PROP_HEX32("productid", USBHostDevice
, match
.product_id
, 0),
1026 DEFINE_PROP_END_OF_LIST(),
1030 static void usb_host_register_devices(void)
1032 usb_qdev_register(&usb_host_dev_info
);
1034 device_init(usb_host_register_devices
)
1036 USBDevice
*usb_host_device_open(const char *devname
)
1038 struct USBAutoFilter filter
;
1042 dev
= usb_create(NULL
/* FIXME */, "usb-host");
1044 if (strstr(devname
, "auto:")) {
1045 if (parse_filter(devname
, &filter
) < 0) {
1049 if ((p
= strchr(devname
, '.'))) {
1050 filter
.bus_num
= strtoul(devname
, NULL
, 0);
1051 filter
.addr
= strtoul(p
+ 1, NULL
, 0);
1052 filter
.vendor_id
= 0;
1053 filter
.product_id
= 0;
1054 } else if ((p
= strchr(devname
, ':'))) {
1057 filter
.vendor_id
= strtoul(devname
, NULL
, 16);
1058 filter
.product_id
= strtoul(p
+ 1, NULL
, 16);
1064 qdev_prop_set_uint32(&dev
->qdev
, "hostbus", filter
.bus_num
);
1065 qdev_prop_set_uint32(&dev
->qdev
, "hostaddr", filter
.addr
);
1066 qdev_prop_set_uint32(&dev
->qdev
, "vendorid", filter
.vendor_id
);
1067 qdev_prop_set_uint32(&dev
->qdev
, "productid", filter
.product_id
);
1068 qdev_init_nofail(&dev
->qdev
);
1072 qdev_free(&dev
->qdev
);
1076 int usb_host_device_close(const char *devname
)
1079 char product_name
[PRODUCT_NAME_SZ
];
1083 if (strstr(devname
, "auto:")) {
1084 return usb_host_auto_del(devname
);
1086 if (usb_host_find_device(&bus_num
, &addr
, product_name
,
1087 sizeof(product_name
), devname
) < 0) {
1090 s
= hostdev_find(bus_num
, addr
);
1092 usb_device_delete_addr(s
->bus_num
, s
->dev
.addr
);
1100 static int get_tag_value(char *buf
, int buf_size
,
1101 const char *str
, const char *tag
,
1102 const char *stopchars
)
1106 p
= strstr(str
, tag
);
1111 while (qemu_isspace(*p
)) {
1115 while (*p
!= '\0' && !strchr(stopchars
, *p
)) {
1116 if ((q
- buf
) < (buf_size
- 1)) {
1126 * Use /proc/bus/usb/devices or /dev/bus/usb/devices file to determine
1127 * host's USB devices. This is legacy support since many distributions
1128 * are moving to /sys/bus/usb
1130 static int usb_host_scan_dev(void *opaque
, USBScanFunc
*func
)
1135 int bus_num
, addr
, speed
, device_count
, class_id
, product_id
, vendor_id
;
1136 char product_name
[512];
1139 if (!usb_host_device_path
) {
1140 perror("husb: USB Host Device Path not set");
1143 snprintf(line
, sizeof(line
), "%s/devices", usb_host_device_path
);
1144 f
= fopen(line
, "r");
1146 perror("husb: cannot open devices file");
1151 bus_num
= addr
= speed
= class_id
= product_id
= vendor_id
= 0;
1153 if (fgets(line
, sizeof(line
), f
) == NULL
) {
1156 if (strlen(line
) > 0) {
1157 line
[strlen(line
) - 1] = '\0';
1159 if (line
[0] == 'T' && line
[1] == ':') {
1160 if (device_count
&& (vendor_id
|| product_id
)) {
1161 /* New device. Add the previously discovered device. */
1162 ret
= func(opaque
, bus_num
, addr
, class_id
, vendor_id
,
1163 product_id
, product_name
, speed
);
1168 if (get_tag_value(buf
, sizeof(buf
), line
, "Bus=", " ") < 0) {
1171 bus_num
= atoi(buf
);
1172 if (get_tag_value(buf
, sizeof(buf
), line
, "Dev#=", " ") < 0) {
1176 if (get_tag_value(buf
, sizeof(buf
), line
, "Spd=", " ") < 0) {
1179 if (!strcmp(buf
, "480")) {
1180 speed
= USB_SPEED_HIGH
;
1181 } else if (!strcmp(buf
, "1.5")) {
1182 speed
= USB_SPEED_LOW
;
1184 speed
= USB_SPEED_FULL
;
1186 product_name
[0] = '\0';
1191 } else if (line
[0] == 'P' && line
[1] == ':') {
1192 if (get_tag_value(buf
, sizeof(buf
), line
, "Vendor=", " ") < 0) {
1195 vendor_id
= strtoul(buf
, NULL
, 16);
1196 if (get_tag_value(buf
, sizeof(buf
), line
, "ProdID=", " ") < 0) {
1199 product_id
= strtoul(buf
, NULL
, 16);
1200 } else if (line
[0] == 'S' && line
[1] == ':') {
1201 if (get_tag_value(buf
, sizeof(buf
), line
, "Product=", "") < 0) {
1204 pstrcpy(product_name
, sizeof(product_name
), buf
);
1205 } else if (line
[0] == 'D' && line
[1] == ':') {
1206 if (get_tag_value(buf
, sizeof(buf
), line
, "Cls=", " (") < 0) {
1209 class_id
= strtoul(buf
, NULL
, 16);
1213 if (device_count
&& (vendor_id
|| product_id
)) {
1214 /* Add the last device. */
1215 ret
= func(opaque
, bus_num
, addr
, class_id
, vendor_id
,
1216 product_id
, product_name
, speed
);
1226 * Read sys file-system device file
1228 * @line address of buffer to put file contents in
1229 * @line_size size of line
1230 * @device_file path to device file (printf format string)
1231 * @device_name device being opened (inserted into device_file)
1233 * @return 0 failed, 1 succeeded ('line' contains data)
1235 static int usb_host_read_file(char *line
, size_t line_size
,
1236 const char *device_file
, const char *device_name
)
1240 char filename
[PATH_MAX
];
1242 snprintf(filename
, PATH_MAX
, USBSYSBUS_PATH
"/devices/%s/%s", device_name
,
1244 f
= fopen(filename
, "r");
1246 ret
= fgets(line
, line_size
, f
) != NULL
;
1254 * Use /sys/bus/usb/devices/ directory to determine host's USB
1257 * This code is based on Robert Schiele's original patches posted to
1258 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1260 static int usb_host_scan_sys(void *opaque
, USBScanFunc
*func
)
1264 int bus_num
, addr
, speed
, class_id
, product_id
, vendor_id
;
1266 char product_name
[512];
1269 dir
= opendir(USBSYSBUS_PATH
"/devices");
1271 perror("husb: cannot open devices directory");
1275 while ((de
= readdir(dir
))) {
1276 if (de
->d_name
[0] != '.' && !strchr(de
->d_name
, ':')) {
1277 char *tmpstr
= de
->d_name
;
1278 if (!strncmp(de
->d_name
, "usb", 3)) {
1281 bus_num
= atoi(tmpstr
);
1283 if (!usb_host_read_file(line
, sizeof(line
), "devnum", de
->d_name
)) {
1286 if (sscanf(line
, "%d", &addr
) != 1) {
1289 if (!usb_host_read_file(line
, sizeof(line
), "bDeviceClass",
1293 if (sscanf(line
, "%x", &class_id
) != 1) {
1297 if (!usb_host_read_file(line
, sizeof(line
), "idVendor",
1301 if (sscanf(line
, "%x", &vendor_id
) != 1) {
1304 if (!usb_host_read_file(line
, sizeof(line
), "idProduct",
1308 if (sscanf(line
, "%x", &product_id
) != 1) {
1311 if (!usb_host_read_file(line
, sizeof(line
), "product",
1315 if (strlen(line
) > 0) {
1316 line
[strlen(line
) - 1] = '\0';
1318 pstrcpy(product_name
, sizeof(product_name
), line
);
1321 if (!usb_host_read_file(line
, sizeof(line
), "speed", de
->d_name
)) {
1324 if (!strcmp(line
, "480\n")) {
1325 speed
= USB_SPEED_HIGH
;
1326 } else if (!strcmp(line
, "1.5\n")) {
1327 speed
= USB_SPEED_LOW
;
1329 speed
= USB_SPEED_FULL
;
1332 ret
= func(opaque
, bus_num
, addr
, class_id
, vendor_id
,
1333 product_id
, product_name
, speed
);
1347 * Determine how to access the host's USB devices and call the
1348 * specific support function.
1350 static int usb_host_scan(void *opaque
, USBScanFunc
*func
)
1352 Monitor
*mon
= cur_mon
;
1356 const char *fs_type
[] = {"unknown", "proc", "dev", "sys"};
1357 char devpath
[PATH_MAX
];
1359 /* only check the host once */
1361 dir
= opendir(USBSYSBUS_PATH
"/devices");
1363 /* devices found in /dev/bus/usb/ (yes - not a mistake!) */
1364 strcpy(devpath
, USBDEVBUS_PATH
);
1365 usb_fs_type
= USB_FS_SYS
;
1367 DPRINTF(USBDBG_DEVOPENED
, USBSYSBUS_PATH
);
1370 f
= fopen(USBPROCBUS_PATH
"/devices", "r");
1372 /* devices found in /proc/bus/usb/ */
1373 strcpy(devpath
, USBPROCBUS_PATH
);
1374 usb_fs_type
= USB_FS_PROC
;
1376 DPRINTF(USBDBG_DEVOPENED
, USBPROCBUS_PATH
);
1379 /* try additional methods if an access method hasn't been found yet */
1380 f
= fopen(USBDEVBUS_PATH
"/devices", "r");
1382 /* devices found in /dev/bus/usb/ */
1383 strcpy(devpath
, USBDEVBUS_PATH
);
1384 usb_fs_type
= USB_FS_DEV
;
1386 DPRINTF(USBDBG_DEVOPENED
, USBDEVBUS_PATH
);
1392 monitor_printf(mon
, "husb: unable to access USB devices\n");
1397 /* the module setting (used later for opening devices) */
1398 usb_host_device_path
= qemu_mallocz(strlen(devpath
)+1);
1399 strcpy(usb_host_device_path
, devpath
);
1401 monitor_printf(mon
, "husb: using %s file-system with %s\n",
1402 fs_type
[usb_fs_type
], usb_host_device_path
);
1406 switch (usb_fs_type
) {
1409 ret
= usb_host_scan_dev(opaque
, func
);
1412 ret
= usb_host_scan_sys(opaque
, func
);
1421 static QEMUTimer
*usb_auto_timer
;
1423 static int usb_host_auto_scan(void *opaque
, int bus_num
, int addr
,
1424 int class_id
, int vendor_id
, int product_id
,
1425 const char *product_name
, int speed
)
1427 struct USBAutoFilter
*f
;
1428 struct USBHostDevice
*s
;
1434 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1437 if (f
->bus_num
> 0 && f
->bus_num
!= bus_num
) {
1440 if (f
->addr
> 0 && f
->addr
!= addr
) {
1444 if (f
->vendor_id
> 0 && f
->vendor_id
!= vendor_id
) {
1448 if (f
->product_id
> 0 && f
->product_id
!= product_id
) {
1451 /* We got a match */
1453 /* Already attached ? */
1457 DPRINTF("husb: auto open: bus_num %d addr %d\n", bus_num
, addr
);
1459 usb_host_open(s
, bus_num
, addr
, product_name
);
1465 static void usb_host_auto_check(void *unused
)
1467 struct USBHostDevice
*s
;
1468 int unconnected
= 0;
1470 usb_host_scan(NULL
, usb_host_auto_scan
);
1472 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1478 if (unconnected
== 0) {
1479 /* nothing to watch */
1480 if (usb_auto_timer
) {
1481 qemu_del_timer(usb_auto_timer
);
1486 if (!usb_auto_timer
) {
1487 usb_auto_timer
= qemu_new_timer(rt_clock
, usb_host_auto_check
, NULL
);
1488 if (!usb_auto_timer
) {
1492 qemu_mod_timer(usb_auto_timer
, qemu_get_clock(rt_clock
) + 2000);
1496 * Autoconnect filter
1498 * auto:bus:dev[:vid:pid]
1499 * auto:bus.dev[:vid:pid]
1501 * bus - bus number (dec, * means any)
1502 * dev - device number (dec, * means any)
1503 * vid - vendor id (hex, * means any)
1504 * pid - product id (hex, * means any)
1506 * See 'lsusb' output.
1508 static int parse_filter(const char *spec
, struct USBAutoFilter
*f
)
1510 enum { BUS
, DEV
, VID
, PID
, DONE
};
1511 const char *p
= spec
;
1519 for (i
= BUS
; i
< DONE
; i
++) {
1520 p
= strpbrk(p
, ":.");
1530 case BUS
: f
->bus_num
= strtol(p
, NULL
, 10); break;
1531 case DEV
: f
->addr
= strtol(p
, NULL
, 10); break;
1532 case VID
: f
->vendor_id
= strtol(p
, NULL
, 16); break;
1533 case PID
: f
->product_id
= strtol(p
, NULL
, 16); break;
1538 fprintf(stderr
, "husb: invalid auto filter spec %s\n", spec
);
1545 /**********************/
1546 /* USB host device info */
1548 struct usb_class_info
{
1550 const char *class_name
;
1553 static const struct usb_class_info usb_class_info
[] = {
1554 { USB_CLASS_AUDIO
, "Audio"},
1555 { USB_CLASS_COMM
, "Communication"},
1556 { USB_CLASS_HID
, "HID"},
1557 { USB_CLASS_HUB
, "Hub" },
1558 { USB_CLASS_PHYSICAL
, "Physical" },
1559 { USB_CLASS_PRINTER
, "Printer" },
1560 { USB_CLASS_MASS_STORAGE
, "Storage" },
1561 { USB_CLASS_CDC_DATA
, "Data" },
1562 { USB_CLASS_APP_SPEC
, "Application Specific" },
1563 { USB_CLASS_VENDOR_SPEC
, "Vendor Specific" },
1564 { USB_CLASS_STILL_IMAGE
, "Still Image" },
1565 { USB_CLASS_CSCID
, "Smart Card" },
1566 { USB_CLASS_CONTENT_SEC
, "Content Security" },
1570 static const char *usb_class_str(uint8_t class)
1572 const struct usb_class_info
*p
;
1573 for(p
= usb_class_info
; p
->class != -1; p
++) {
1574 if (p
->class == class) {
1578 return p
->class_name
;
1581 static void usb_info_device(Monitor
*mon
, int bus_num
, int addr
, int class_id
,
1582 int vendor_id
, int product_id
,
1583 const char *product_name
,
1586 const char *class_str
, *speed_str
;
1592 case USB_SPEED_FULL
:
1595 case USB_SPEED_HIGH
:
1603 monitor_printf(mon
, " Device %d.%d, speed %s Mb/s\n",
1604 bus_num
, addr
, speed_str
);
1605 class_str
= usb_class_str(class_id
);
1607 monitor_printf(mon
, " %s:", class_str
);
1609 monitor_printf(mon
, " Class %02x:", class_id
);
1611 monitor_printf(mon
, " USB device %04x:%04x", vendor_id
, product_id
);
1612 if (product_name
[0] != '\0') {
1613 monitor_printf(mon
, ", %s", product_name
);
1615 monitor_printf(mon
, "\n");
1618 static int usb_host_info_device(void *opaque
, int bus_num
, int addr
,
1620 int vendor_id
, int product_id
,
1621 const char *product_name
,
1624 Monitor
*mon
= opaque
;
1626 usb_info_device(mon
, bus_num
, addr
, class_id
, vendor_id
, product_id
,
1627 product_name
, speed
);
1631 static void dec2str(int val
, char *str
, size_t size
)
1634 snprintf(str
, size
, "*");
1636 snprintf(str
, size
, "%d", val
);
1640 static void hex2str(int val
, char *str
, size_t size
)
1643 snprintf(str
, size
, "*");
1645 snprintf(str
, size
, "%04x", val
);
1649 void usb_host_info(Monitor
*mon
)
1651 struct USBAutoFilter
*f
;
1652 struct USBHostDevice
*s
;
1654 usb_host_scan(mon
, usb_host_info_device
);
1656 if (QTAILQ_EMPTY(&hostdevs
)) {
1660 monitor_printf(mon
, " Auto filters:\n");
1661 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1662 char bus
[10], addr
[10], vid
[10], pid
[10];
1664 dec2str(f
->bus_num
, bus
, sizeof(bus
));
1665 dec2str(f
->addr
, addr
, sizeof(addr
));
1666 hex2str(f
->vendor_id
, vid
, sizeof(vid
));
1667 hex2str(f
->product_id
, pid
, sizeof(pid
));
1668 monitor_printf(mon
, " Device %s.%s ID %s:%s\n",
1669 bus
, addr
, vid
, pid
);