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
);
230 if (errno
== ENODEV
&& !s
->closing
) {
231 printf("husb: device %d.%d disconnected\n", s
->bus_num
, s
->addr
);
233 usb_host_auto_check(NULL
);
237 DPRINTF("husb: async. reap urb failed errno %d\n", errno
);
243 DPRINTF("husb: async completed. aurb %p status %d alen %d\n",
244 aurb
, aurb
->urb
.status
, aurb
->urb
.actual_length
);
247 switch (aurb
->urb
.status
) {
249 p
->len
= aurb
->urb
.actual_length
;
250 if (aurb
->urb
.type
== USBDEVFS_URB_TYPE_CONTROL
)
251 async_complete_ctrl(s
, p
);
255 set_halt(s
, p
->devep
);
256 p
->len
= USB_RET_STALL
;
260 p
->len
= USB_RET_NAK
;
264 usb_packet_complete(p
);
271 static void async_cancel(USBPacket
*unused
, void *opaque
)
273 AsyncURB
*aurb
= opaque
;
274 USBHostDevice
*s
= aurb
->hdev
;
276 DPRINTF("husb: async cancel. aurb %p\n", aurb
);
278 /* Mark it as dead (see async_complete above) */
281 int r
= ioctl(s
->fd
, USBDEVFS_DISCARDURB
, aurb
);
283 DPRINTF("husb: async. discard urb failed errno %d\n", errno
);
287 static int usb_host_claim_interfaces(USBHostDevice
*dev
, int configuration
)
289 int dev_descr_len
, config_descr_len
;
290 int interface
, nb_interfaces
, nb_configurations
;
293 if (configuration
== 0) /* address state - ignore */
296 DPRINTF("husb: claiming interfaces. config %d\n", configuration
);
299 dev_descr_len
= dev
->descr
[0];
300 if (dev_descr_len
> dev
->descr_len
)
302 nb_configurations
= dev
->descr
[17];
305 while (i
< dev
->descr_len
) {
306 DPRINTF("husb: i is %d, descr_len is %d, dl %d, dt %d\n", i
, dev
->descr_len
,
307 dev
->descr
[i
], dev
->descr
[i
+1]);
309 if (dev
->descr
[i
+1] != USB_DT_CONFIG
) {
313 config_descr_len
= dev
->descr
[i
];
315 printf("husb: config #%d need %d\n", dev
->descr
[i
+ 5], configuration
);
317 if (configuration
< 0 || configuration
== dev
->descr
[i
+ 5]) {
318 configuration
= dev
->descr
[i
+ 5];
322 i
+= config_descr_len
;
325 if (i
>= dev
->descr_len
) {
326 fprintf(stderr
, "husb: update iface failed. no matching configuration\n");
329 nb_interfaces
= dev
->descr
[i
+ 4];
331 #ifdef USBDEVFS_DISCONNECT
332 /* earlier Linux 2.4 do not support that */
334 struct usbdevfs_ioctl ctrl
;
335 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
336 ctrl
.ioctl_code
= USBDEVFS_DISCONNECT
;
337 ctrl
.ifno
= interface
;
338 ret
= ioctl(dev
->fd
, USBDEVFS_IOCTL
, &ctrl
);
339 if (ret
< 0 && errno
!= ENODATA
) {
340 perror("USBDEVFS_DISCONNECT");
347 /* XXX: only grab if all interfaces are free */
348 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
349 ret
= ioctl(dev
->fd
, USBDEVFS_CLAIMINTERFACE
, &interface
);
351 if (errno
== EBUSY
) {
352 printf("husb: update iface. device already grabbed\n");
354 perror("husb: failed to claim interface");
361 printf("husb: %d interfaces claimed for configuration %d\n",
362 nb_interfaces
, configuration
);
364 dev
->ninterfaces
= nb_interfaces
;
365 dev
->configuration
= configuration
;
369 static int usb_host_release_interfaces(USBHostDevice
*s
)
373 DPRINTF("husb: releasing interfaces\n");
375 for (i
= 0; i
< s
->ninterfaces
; i
++) {
376 ret
= ioctl(s
->fd
, USBDEVFS_RELEASEINTERFACE
, &i
);
378 perror("husb: failed to release interface");
386 static void usb_host_handle_reset(USBDevice
*dev
)
388 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
390 DPRINTF("husb: reset device %u.%u\n", s
->bus_num
, s
->addr
);
392 ioctl(s
->fd
, USBDEVFS_RESET
);
394 usb_host_claim_interfaces(s
, s
->configuration
);
397 static void usb_host_handle_destroy(USBDevice
*dev
)
399 USBHostDevice
*s
= (USBHostDevice
*)dev
;
402 QTAILQ_REMOVE(&hostdevs
, s
, next
);
405 static int usb_linux_update_endp_table(USBHostDevice
*s
);
407 static int usb_host_handle_data(USBHostDevice
*s
, USBPacket
*p
)
409 struct usbdevfs_urb
*urb
;
413 aurb
= async_alloc();
419 if (p
->pid
== USB_TOKEN_IN
)
420 urb
->endpoint
= p
->devep
| 0x80;
422 urb
->endpoint
= p
->devep
;
424 if (is_halted(s
, p
->devep
)) {
425 ret
= ioctl(s
->fd
, USBDEVFS_CLEAR_HALT
, &urb
->endpoint
);
427 DPRINTF("husb: failed to clear halt. ep 0x%x errno %d\n",
428 urb
->endpoint
, errno
);
431 clear_halt(s
, p
->devep
);
434 urb
->buffer
= p
->data
;
435 urb
->buffer_length
= p
->len
;
437 if (is_isoc(s
, p
->devep
)) {
438 /* Setup ISOC transfer */
439 urb
->type
= USBDEVFS_URB_TYPE_ISO
;
440 urb
->flags
= USBDEVFS_URB_ISO_ASAP
;
441 urb
->number_of_packets
= 1;
442 urb
->iso_frame_desc
[0].length
= p
->len
;
444 /* Setup bulk transfer */
445 urb
->type
= USBDEVFS_URB_TYPE_BULK
;
448 urb
->usercontext
= s
;
450 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
452 DPRINTF("husb: data submit. ep 0x%x len %u aurb %p\n", urb
->endpoint
, p
->len
, aurb
);
455 DPRINTF("husb: submit failed. errno %d\n", errno
);
463 return USB_RET_STALL
;
467 usb_defer_packet(p
, async_cancel
, aurb
);
468 return USB_RET_ASYNC
;
471 static int ctrl_error(void)
473 if (errno
== ETIMEDOUT
)
476 return USB_RET_STALL
;
479 static int usb_host_set_address(USBHostDevice
*s
, int addr
)
481 DPRINTF("husb: ctrl set addr %u\n", addr
);
486 static int usb_host_set_config(USBHostDevice
*s
, int config
)
488 usb_host_release_interfaces(s
);
490 int ret
= ioctl(s
->fd
, USBDEVFS_SETCONFIGURATION
, &config
);
492 DPRINTF("husb: ctrl set config %d ret %d errno %d\n", config
, ret
, errno
);
497 usb_host_claim_interfaces(s
, config
);
501 static int usb_host_set_interface(USBHostDevice
*s
, int iface
, int alt
)
503 struct usbdevfs_setinterface si
;
506 si
.interface
= iface
;
508 ret
= ioctl(s
->fd
, USBDEVFS_SETINTERFACE
, &si
);
510 DPRINTF("husb: ctrl set iface %d altset %d ret %d errno %d\n",
511 iface
, alt
, ret
, errno
);
516 usb_linux_update_endp_table(s
);
520 static int usb_host_handle_control(USBHostDevice
*s
, USBPacket
*p
)
522 struct usbdevfs_urb
*urb
;
524 int ret
, value
, index
;
528 * Process certain standard device requests.
529 * These are infrequent and are processed synchronously.
531 value
= le16_to_cpu(s
->ctrl
.req
.wValue
);
532 index
= le16_to_cpu(s
->ctrl
.req
.wIndex
);
534 DPRINTF("husb: ctrl type 0x%x req 0x%x val 0x%x index %u len %u\n",
535 s
->ctrl
.req
.bRequestType
, s
->ctrl
.req
.bRequest
, value
, index
,
538 if (s
->ctrl
.req
.bRequestType
== 0) {
539 switch (s
->ctrl
.req
.bRequest
) {
540 case USB_REQ_SET_ADDRESS
:
541 return usb_host_set_address(s
, value
);
543 case USB_REQ_SET_CONFIGURATION
:
544 return usb_host_set_config(s
, value
& 0xff);
548 if (s
->ctrl
.req
.bRequestType
== 1 &&
549 s
->ctrl
.req
.bRequest
== USB_REQ_SET_INTERFACE
)
550 return usb_host_set_interface(s
, index
, value
);
552 /* The rest are asynchronous */
554 buffer_len
= 8 + s
->ctrl
.len
;
555 if (buffer_len
> sizeof(s
->ctrl
.buffer
)) {
556 fprintf(stderr
, "husb: ctrl buffer too small (%u > %zu)\n",
557 buffer_len
, sizeof(s
->ctrl
.buffer
));
558 return USB_RET_STALL
;
561 aurb
= async_alloc();
566 * Setup ctrl transfer.
568 * s->ctrl is layed out such that data buffer immediately follows
569 * 'req' struct which is exactly what usbdevfs expects.
573 urb
->type
= USBDEVFS_URB_TYPE_CONTROL
;
574 urb
->endpoint
= p
->devep
;
576 urb
->buffer
= &s
->ctrl
.req
;
577 urb
->buffer_length
= buffer_len
;
579 urb
->usercontext
= s
;
581 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
583 DPRINTF("husb: submit ctrl. len %u aurb %p\n", urb
->buffer_length
, aurb
);
586 DPRINTF("husb: submit failed. errno %d\n", errno
);
594 return USB_RET_STALL
;
598 usb_defer_packet(p
, async_cancel
, aurb
);
599 return USB_RET_ASYNC
;
602 static int do_token_setup(USBDevice
*dev
, USBPacket
*p
)
604 USBHostDevice
*s
= (USBHostDevice
*) dev
;
608 return USB_RET_STALL
;
610 memcpy(&s
->ctrl
.req
, p
->data
, 8);
611 s
->ctrl
.len
= le16_to_cpu(s
->ctrl
.req
.wLength
);
613 s
->ctrl
.state
= CTRL_STATE_SETUP
;
615 if (s
->ctrl
.req
.bRequestType
& USB_DIR_IN
) {
616 ret
= usb_host_handle_control(s
, p
);
620 if (ret
< s
->ctrl
.len
)
622 s
->ctrl
.state
= CTRL_STATE_DATA
;
624 if (s
->ctrl
.len
== 0)
625 s
->ctrl
.state
= CTRL_STATE_ACK
;
627 s
->ctrl
.state
= CTRL_STATE_DATA
;
633 static int do_token_in(USBDevice
*dev
, USBPacket
*p
)
635 USBHostDevice
*s
= (USBHostDevice
*) dev
;
639 return usb_host_handle_data(s
, p
);
641 switch(s
->ctrl
.state
) {
643 if (!(s
->ctrl
.req
.bRequestType
& USB_DIR_IN
)) {
644 ret
= usb_host_handle_control(s
, p
);
645 if (ret
== USB_RET_ASYNC
)
646 return USB_RET_ASYNC
;
648 s
->ctrl
.state
= CTRL_STATE_IDLE
;
649 return ret
> 0 ? 0 : ret
;
654 case CTRL_STATE_DATA
:
655 if (s
->ctrl
.req
.bRequestType
& USB_DIR_IN
) {
656 int len
= s
->ctrl
.len
- s
->ctrl
.offset
;
659 memcpy(p
->data
, s
->ctrl
.buffer
+ s
->ctrl
.offset
, len
);
660 s
->ctrl
.offset
+= len
;
661 if (s
->ctrl
.offset
>= s
->ctrl
.len
)
662 s
->ctrl
.state
= CTRL_STATE_ACK
;
666 s
->ctrl
.state
= CTRL_STATE_IDLE
;
667 return USB_RET_STALL
;
670 return USB_RET_STALL
;
674 static int do_token_out(USBDevice
*dev
, USBPacket
*p
)
676 USBHostDevice
*s
= (USBHostDevice
*) dev
;
679 return usb_host_handle_data(s
, p
);
681 switch(s
->ctrl
.state
) {
683 if (s
->ctrl
.req
.bRequestType
& USB_DIR_IN
) {
684 s
->ctrl
.state
= CTRL_STATE_IDLE
;
687 /* ignore additional output */
691 case CTRL_STATE_DATA
:
692 if (!(s
->ctrl
.req
.bRequestType
& USB_DIR_IN
)) {
693 int len
= s
->ctrl
.len
- s
->ctrl
.offset
;
696 memcpy(s
->ctrl
.buffer
+ s
->ctrl
.offset
, p
->data
, len
);
697 s
->ctrl
.offset
+= len
;
698 if (s
->ctrl
.offset
>= s
->ctrl
.len
)
699 s
->ctrl
.state
= CTRL_STATE_ACK
;
703 s
->ctrl
.state
= CTRL_STATE_IDLE
;
704 return USB_RET_STALL
;
707 return USB_RET_STALL
;
713 * Called by the HC (host controller).
715 * Returns length of the transaction or one of the USB_RET_XXX codes.
717 static int usb_host_handle_packet(USBDevice
*s
, USBPacket
*p
)
721 s
->state
= USB_STATE_ATTACHED
;
725 s
->state
= USB_STATE_NOTATTACHED
;
729 s
->remote_wakeup
= 0;
731 s
->state
= USB_STATE_DEFAULT
;
732 s
->info
->handle_reset(s
);
736 /* Rest of the PIDs must match our address */
737 if (s
->state
< USB_STATE_DEFAULT
|| p
->devaddr
!= s
->addr
)
738 return USB_RET_NODEV
;
741 case USB_TOKEN_SETUP
:
742 return do_token_setup(s
, p
);
745 return do_token_in(s
, p
);
748 return do_token_out(s
, p
);
751 return USB_RET_STALL
;
755 /* returns 1 on problem encountered or 0 for success */
756 static int usb_linux_update_endp_table(USBHostDevice
*s
)
758 uint8_t *descriptors
;
759 uint8_t devep
, type
, configuration
, alt_interface
;
760 struct usb_ctrltransfer ct
;
761 int interface
, ret
, length
, i
;
763 ct
.bRequestType
= USB_DIR_IN
;
764 ct
.bRequest
= USB_REQ_GET_CONFIGURATION
;
768 ct
.data
= &configuration
;
771 ret
= ioctl(s
->fd
, USBDEVFS_CONTROL
, &ct
);
773 perror("usb_linux_update_endp_table");
777 /* in address state */
778 if (configuration
== 0)
781 /* get the desired configuration, interface, and endpoint descriptors
782 * from device description */
783 descriptors
= &s
->descr
[18];
784 length
= s
->descr_len
- 18;
787 if (descriptors
[i
+ 1] != USB_DT_CONFIG
||
788 descriptors
[i
+ 5] != configuration
) {
789 DPRINTF("invalid descriptor data - configuration\n");
795 if (descriptors
[i
+ 1] != USB_DT_INTERFACE
||
796 (descriptors
[i
+ 1] == USB_DT_INTERFACE
&&
797 descriptors
[i
+ 4] == 0)) {
802 interface
= descriptors
[i
+ 2];
804 ct
.bRequestType
= USB_DIR_IN
| USB_RECIP_INTERFACE
;
805 ct
.bRequest
= USB_REQ_GET_INTERFACE
;
807 ct
.wIndex
= interface
;
809 ct
.data
= &alt_interface
;
812 ret
= ioctl(s
->fd
, USBDEVFS_CONTROL
, &ct
);
814 alt_interface
= interface
;
817 /* the current interface descriptor is the active interface
818 * and has endpoints */
819 if (descriptors
[i
+ 3] != alt_interface
) {
824 /* advance to the endpoints */
825 while (i
< length
&& descriptors
[i
+1] != USB_DT_ENDPOINT
)
832 if (descriptors
[i
+ 1] != USB_DT_ENDPOINT
)
835 devep
= descriptors
[i
+ 2];
836 switch (descriptors
[i
+ 3] & 0x3) {
838 type
= USBDEVFS_URB_TYPE_CONTROL
;
841 type
= USBDEVFS_URB_TYPE_ISO
;
844 type
= USBDEVFS_URB_TYPE_BULK
;
847 type
= USBDEVFS_URB_TYPE_INTERRUPT
;
850 DPRINTF("usb_host: malformed endpoint type\n");
851 type
= USBDEVFS_URB_TYPE_BULK
;
853 s
->endp_table
[(devep
& 0xf) - 1].type
= type
;
854 s
->endp_table
[(devep
& 0xf) - 1].halted
= 0;
862 static int usb_host_open(USBHostDevice
*dev
, int bus_num
,
863 int addr
, const char *prod_name
)
866 struct usbdevfs_connectinfo ci
;
872 printf("husb: open device %d.%d\n", bus_num
, addr
);
874 if (!usb_host_device_path
) {
875 perror("husb: USB Host Device Path not set");
878 snprintf(buf
, sizeof(buf
), "%s/%03d/%03d", usb_host_device_path
,
880 fd
= open(buf
, O_RDWR
| O_NONBLOCK
);
885 DPRINTF("husb: opened %s\n", buf
);
887 dev
->bus_num
= bus_num
;
891 /* read the device description */
892 dev
->descr_len
= read(fd
, dev
->descr
, sizeof(dev
->descr
));
893 if (dev
->descr_len
<= 0) {
894 perror("husb: reading device data failed");
901 printf("=== begin dumping device descriptor data ===\n");
902 for (x
= 0; x
< dev
->descr_len
; x
++)
903 printf("%02x ", dev
->descr
[x
]);
904 printf("\n=== end dumping device descriptor data ===\n");
910 * Initial configuration is -1 which makes us claim first
911 * available config. We used to start with 1, which does not
912 * always work. I've seen devices where first config starts
915 if (!usb_host_claim_interfaces(dev
, -1))
918 ret
= ioctl(fd
, USBDEVFS_CONNECTINFO
, &ci
);
920 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
924 printf("husb: grabbed usb device %d.%d\n", bus_num
, addr
);
926 ret
= usb_linux_update_endp_table(dev
);
931 dev
->dev
.speed
= USB_SPEED_LOW
;
933 dev
->dev
.speed
= USB_SPEED_HIGH
;
935 if (!prod_name
|| prod_name
[0] == '\0')
936 snprintf(dev
->dev
.product_desc
, sizeof(dev
->dev
.product_desc
),
937 "host:%d.%d", bus_num
, addr
);
939 pstrcpy(dev
->dev
.product_desc
, sizeof(dev
->dev
.product_desc
),
942 /* USB devio uses 'write' flag to check for async completions */
943 qemu_set_fd_handler(dev
->fd
, NULL
, async_complete
, dev
);
945 usb_device_attach(&dev
->dev
);
955 static int usb_host_close(USBHostDevice
*dev
)
960 qemu_set_fd_handler(dev
->fd
, NULL
, NULL
, NULL
);
964 usb_device_detach(&dev
->dev
);
970 static int usb_host_initfn(USBDevice
*dev
)
972 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
974 dev
->auto_attach
= 0;
976 QTAILQ_INSERT_TAIL(&hostdevs
, s
, next
);
977 usb_host_auto_check(NULL
);
981 static struct USBDeviceInfo usb_host_dev_info
= {
982 .product_desc
= "USB Host Device",
983 .qdev
.name
= "usb-host",
984 .qdev
.size
= sizeof(USBHostDevice
),
985 .init
= usb_host_initfn
,
986 .handle_packet
= usb_host_handle_packet
,
987 .handle_reset
= usb_host_handle_reset
,
988 .handle_destroy
= usb_host_handle_destroy
,
989 .usbdevice_name
= "host",
990 .usbdevice_init
= usb_host_device_open
,
991 .qdev
.props
= (Property
[]) {
992 DEFINE_PROP_UINT32("hostbus", USBHostDevice
, match
.bus_num
, 0),
993 DEFINE_PROP_UINT32("hostaddr", USBHostDevice
, match
.addr
, 0),
994 DEFINE_PROP_HEX32("vendorid", USBHostDevice
, match
.vendor_id
, 0),
995 DEFINE_PROP_HEX32("productid", USBHostDevice
, match
.product_id
, 0),
996 DEFINE_PROP_END_OF_LIST(),
1000 static void usb_host_register_devices(void)
1002 usb_qdev_register(&usb_host_dev_info
);
1004 device_init(usb_host_register_devices
)
1006 USBDevice
*usb_host_device_open(const char *devname
)
1008 struct USBAutoFilter filter
;
1012 dev
= usb_create(NULL
/* FIXME */, "usb-host");
1014 if (strstr(devname
, "auto:")) {
1015 if (parse_filter(devname
, &filter
) < 0)
1018 if ((p
= strchr(devname
, '.'))) {
1019 filter
.bus_num
= strtoul(devname
, NULL
, 0);
1020 filter
.addr
= strtoul(p
+ 1, NULL
, 0);
1021 filter
.vendor_id
= 0;
1022 filter
.product_id
= 0;
1023 } else if ((p
= strchr(devname
, ':'))) {
1026 filter
.vendor_id
= strtoul(devname
, NULL
, 16);
1027 filter
.product_id
= strtoul(p
+ 1, NULL
, 16);
1033 qdev_prop_set_uint32(&dev
->qdev
, "hostbus", filter
.bus_num
);
1034 qdev_prop_set_uint32(&dev
->qdev
, "hostaddr", filter
.addr
);
1035 qdev_prop_set_uint32(&dev
->qdev
, "vendorid", filter
.vendor_id
);
1036 qdev_prop_set_uint32(&dev
->qdev
, "productid", filter
.product_id
);
1037 qdev_init_nofail(&dev
->qdev
);
1041 qdev_free(&dev
->qdev
);
1045 int usb_host_device_close(const char *devname
)
1048 char product_name
[PRODUCT_NAME_SZ
];
1052 if (strstr(devname
, "auto:"))
1053 return usb_host_auto_del(devname
);
1055 if (usb_host_find_device(&bus_num
, &addr
, product_name
, sizeof(product_name
),
1059 s
= hostdev_find(bus_num
, addr
);
1061 usb_device_delete_addr(s
->bus_num
, s
->dev
.addr
);
1069 static int get_tag_value(char *buf
, int buf_size
,
1070 const char *str
, const char *tag
,
1071 const char *stopchars
)
1075 p
= strstr(str
, tag
);
1079 while (qemu_isspace(*p
))
1082 while (*p
!= '\0' && !strchr(stopchars
, *p
)) {
1083 if ((q
- buf
) < (buf_size
- 1))
1092 * Use /proc/bus/usb/devices or /dev/bus/usb/devices file to determine
1093 * host's USB devices. This is legacy support since many distributions
1094 * are moving to /sys/bus/usb
1096 static int usb_host_scan_dev(void *opaque
, USBScanFunc
*func
)
1101 int bus_num
, addr
, speed
, device_count
, class_id
, product_id
, vendor_id
;
1102 char product_name
[512];
1105 if (!usb_host_device_path
) {
1106 perror("husb: USB Host Device Path not set");
1109 snprintf(line
, sizeof(line
), "%s/devices", usb_host_device_path
);
1110 f
= fopen(line
, "r");
1112 perror("husb: cannot open devices file");
1117 bus_num
= addr
= speed
= class_id
= product_id
= vendor_id
= 0;
1119 if (fgets(line
, sizeof(line
), f
) == NULL
)
1121 if (strlen(line
) > 0)
1122 line
[strlen(line
) - 1] = '\0';
1123 if (line
[0] == 'T' && line
[1] == ':') {
1124 if (device_count
&& (vendor_id
|| product_id
)) {
1125 /* New device. Add the previously discovered device. */
1126 ret
= func(opaque
, bus_num
, addr
, class_id
, vendor_id
,
1127 product_id
, product_name
, speed
);
1131 if (get_tag_value(buf
, sizeof(buf
), line
, "Bus=", " ") < 0)
1133 bus_num
= atoi(buf
);
1134 if (get_tag_value(buf
, sizeof(buf
), line
, "Dev#=", " ") < 0)
1137 if (get_tag_value(buf
, sizeof(buf
), line
, "Spd=", " ") < 0)
1139 if (!strcmp(buf
, "480"))
1140 speed
= USB_SPEED_HIGH
;
1141 else if (!strcmp(buf
, "1.5"))
1142 speed
= USB_SPEED_LOW
;
1144 speed
= USB_SPEED_FULL
;
1145 product_name
[0] = '\0';
1150 } else if (line
[0] == 'P' && line
[1] == ':') {
1151 if (get_tag_value(buf
, sizeof(buf
), line
, "Vendor=", " ") < 0)
1153 vendor_id
= strtoul(buf
, NULL
, 16);
1154 if (get_tag_value(buf
, sizeof(buf
), line
, "ProdID=", " ") < 0)
1156 product_id
= strtoul(buf
, NULL
, 16);
1157 } else if (line
[0] == 'S' && line
[1] == ':') {
1158 if (get_tag_value(buf
, sizeof(buf
), line
, "Product=", "") < 0)
1160 pstrcpy(product_name
, sizeof(product_name
), buf
);
1161 } else if (line
[0] == 'D' && line
[1] == ':') {
1162 if (get_tag_value(buf
, sizeof(buf
), line
, "Cls=", " (") < 0)
1164 class_id
= strtoul(buf
, NULL
, 16);
1168 if (device_count
&& (vendor_id
|| product_id
)) {
1169 /* Add the last device. */
1170 ret
= func(opaque
, bus_num
, addr
, class_id
, vendor_id
,
1171 product_id
, product_name
, speed
);
1180 * Read sys file-system device file
1182 * @line address of buffer to put file contents in
1183 * @line_size size of line
1184 * @device_file path to device file (printf format string)
1185 * @device_name device being opened (inserted into device_file)
1187 * @return 0 failed, 1 succeeded ('line' contains data)
1189 static int usb_host_read_file(char *line
, size_t line_size
, const char *device_file
, const char *device_name
)
1192 Monitor
*mon
= cur_mon
;
1196 char filename
[PATH_MAX
];
1198 snprintf(filename
, PATH_MAX
, USBSYSBUS_PATH
"/devices/%s/%s", device_name
,
1200 f
= fopen(filename
, "r");
1202 ret
= fgets(line
, line_size
, f
) != NULL
;
1207 monitor_printf(mon
, "husb: could not open %s\n", filename
);
1215 * Use /sys/bus/usb/devices/ directory to determine host's USB
1218 * This code is based on Robert Schiele's original patches posted to
1219 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1221 static int usb_host_scan_sys(void *opaque
, USBScanFunc
*func
)
1225 int bus_num
, addr
, speed
, class_id
, product_id
, vendor_id
;
1227 char product_name
[512];
1230 dir
= opendir(USBSYSBUS_PATH
"/devices");
1232 perror("husb: cannot open devices directory");
1236 while ((de
= readdir(dir
))) {
1237 if (de
->d_name
[0] != '.' && !strchr(de
->d_name
, ':')) {
1238 char *tmpstr
= de
->d_name
;
1239 if (!strncmp(de
->d_name
, "usb", 3))
1241 bus_num
= atoi(tmpstr
);
1243 if (!usb_host_read_file(line
, sizeof(line
), "devnum", de
->d_name
))
1245 if (sscanf(line
, "%d", &addr
) != 1)
1248 if (!usb_host_read_file(line
, sizeof(line
), "bDeviceClass",
1251 if (sscanf(line
, "%x", &class_id
) != 1)
1254 if (!usb_host_read_file(line
, sizeof(line
), "idVendor", de
->d_name
))
1256 if (sscanf(line
, "%x", &vendor_id
) != 1)
1259 if (!usb_host_read_file(line
, sizeof(line
), "idProduct",
1262 if (sscanf(line
, "%x", &product_id
) != 1)
1265 if (!usb_host_read_file(line
, sizeof(line
), "product",
1269 if (strlen(line
) > 0)
1270 line
[strlen(line
) - 1] = '\0';
1271 pstrcpy(product_name
, sizeof(product_name
), line
);
1274 if (!usb_host_read_file(line
, sizeof(line
), "speed", de
->d_name
))
1276 if (!strcmp(line
, "480\n"))
1277 speed
= USB_SPEED_HIGH
;
1278 else if (!strcmp(line
, "1.5\n"))
1279 speed
= USB_SPEED_LOW
;
1281 speed
= USB_SPEED_FULL
;
1283 ret
= func(opaque
, bus_num
, addr
, class_id
, vendor_id
,
1284 product_id
, product_name
, speed
);
1296 * Determine how to access the host's USB devices and call the
1297 * specific support function.
1299 static int usb_host_scan(void *opaque
, USBScanFunc
*func
)
1301 Monitor
*mon
= cur_mon
;
1305 const char *fs_type
[] = {"unknown", "proc", "dev", "sys"};
1306 char devpath
[PATH_MAX
];
1308 /* only check the host once */
1310 dir
= opendir(USBSYSBUS_PATH
"/devices");
1312 /* devices found in /dev/bus/usb/ (yes - not a mistake!) */
1313 strcpy(devpath
, USBDEVBUS_PATH
);
1314 usb_fs_type
= USB_FS_SYS
;
1316 DPRINTF(USBDBG_DEVOPENED
, USBSYSBUS_PATH
);
1319 f
= fopen(USBPROCBUS_PATH
"/devices", "r");
1321 /* devices found in /proc/bus/usb/ */
1322 strcpy(devpath
, USBPROCBUS_PATH
);
1323 usb_fs_type
= USB_FS_PROC
;
1325 DPRINTF(USBDBG_DEVOPENED
, USBPROCBUS_PATH
);
1328 /* try additional methods if an access method hasn't been found yet */
1329 f
= fopen(USBDEVBUS_PATH
"/devices", "r");
1331 /* devices found in /dev/bus/usb/ */
1332 strcpy(devpath
, USBDEVBUS_PATH
);
1333 usb_fs_type
= USB_FS_DEV
;
1335 DPRINTF(USBDBG_DEVOPENED
, USBDEVBUS_PATH
);
1341 monitor_printf(mon
, "husb: unable to access USB devices\n");
1345 /* the module setting (used later for opening devices) */
1346 usb_host_device_path
= qemu_mallocz(strlen(devpath
)+1);
1347 strcpy(usb_host_device_path
, devpath
);
1349 monitor_printf(mon
, "husb: using %s file-system with %s\n",
1350 fs_type
[usb_fs_type
], usb_host_device_path
);
1353 switch (usb_fs_type
) {
1356 ret
= usb_host_scan_dev(opaque
, func
);
1359 ret
= usb_host_scan_sys(opaque
, func
);
1368 static QEMUTimer
*usb_auto_timer
;
1370 static int usb_host_auto_scan(void *opaque
, int bus_num
, int addr
,
1371 int class_id
, int vendor_id
, int product_id
,
1372 const char *product_name
, int speed
)
1374 struct USBAutoFilter
*f
;
1375 struct USBHostDevice
*s
;
1381 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1384 if (f
->bus_num
> 0 && f
->bus_num
!= bus_num
)
1387 if (f
->addr
> 0 && f
->addr
!= addr
)
1390 if (f
->vendor_id
> 0 && f
->vendor_id
!= vendor_id
)
1393 if (f
->product_id
> 0 && f
->product_id
!= product_id
)
1396 /* We got a match */
1398 /* Already attached ? */
1402 DPRINTF("husb: auto open: bus_num %d addr %d\n", bus_num
, addr
);
1404 usb_host_open(s
, bus_num
, addr
, product_name
);
1410 static void usb_host_auto_check(void *unused
)
1412 struct USBHostDevice
*s
;
1413 int unconnected
= 0;
1415 usb_host_scan(NULL
, usb_host_auto_scan
);
1417 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1422 if (unconnected
== 0) {
1423 /* nothing to watch */
1425 qemu_del_timer(usb_auto_timer
);
1429 if (!usb_auto_timer
) {
1430 usb_auto_timer
= qemu_new_timer(rt_clock
, usb_host_auto_check
, NULL
);
1431 if (!usb_auto_timer
)
1434 qemu_mod_timer(usb_auto_timer
, qemu_get_clock(rt_clock
) + 2000);
1438 * Autoconnect filter
1440 * auto:bus:dev[:vid:pid]
1441 * auto:bus.dev[:vid:pid]
1443 * bus - bus number (dec, * means any)
1444 * dev - device number (dec, * means any)
1445 * vid - vendor id (hex, * means any)
1446 * pid - product id (hex, * means any)
1448 * See 'lsusb' output.
1450 static int parse_filter(const char *spec
, struct USBAutoFilter
*f
)
1452 enum { BUS
, DEV
, VID
, PID
, DONE
};
1453 const char *p
= spec
;
1461 for (i
= BUS
; i
< DONE
; i
++) {
1462 p
= strpbrk(p
, ":.");
1470 case BUS
: f
->bus_num
= strtol(p
, NULL
, 10); break;
1471 case DEV
: f
->addr
= strtol(p
, NULL
, 10); break;
1472 case VID
: f
->vendor_id
= strtol(p
, NULL
, 16); break;
1473 case PID
: f
->product_id
= strtol(p
, NULL
, 16); break;
1478 fprintf(stderr
, "husb: invalid auto filter spec %s\n", spec
);
1485 /**********************/
1486 /* USB host device info */
1488 struct usb_class_info
{
1490 const char *class_name
;
1493 static const struct usb_class_info usb_class_info
[] = {
1494 { USB_CLASS_AUDIO
, "Audio"},
1495 { USB_CLASS_COMM
, "Communication"},
1496 { USB_CLASS_HID
, "HID"},
1497 { USB_CLASS_HUB
, "Hub" },
1498 { USB_CLASS_PHYSICAL
, "Physical" },
1499 { USB_CLASS_PRINTER
, "Printer" },
1500 { USB_CLASS_MASS_STORAGE
, "Storage" },
1501 { USB_CLASS_CDC_DATA
, "Data" },
1502 { USB_CLASS_APP_SPEC
, "Application Specific" },
1503 { USB_CLASS_VENDOR_SPEC
, "Vendor Specific" },
1504 { USB_CLASS_STILL_IMAGE
, "Still Image" },
1505 { USB_CLASS_CSCID
, "Smart Card" },
1506 { USB_CLASS_CONTENT_SEC
, "Content Security" },
1510 static const char *usb_class_str(uint8_t class)
1512 const struct usb_class_info
*p
;
1513 for(p
= usb_class_info
; p
->class != -1; p
++) {
1514 if (p
->class == class)
1517 return p
->class_name
;
1520 static void usb_info_device(Monitor
*mon
, int bus_num
, int addr
, int class_id
,
1521 int vendor_id
, int product_id
,
1522 const char *product_name
,
1525 const char *class_str
, *speed_str
;
1531 case USB_SPEED_FULL
:
1534 case USB_SPEED_HIGH
:
1542 monitor_printf(mon
, " Device %d.%d, speed %s Mb/s\n",
1543 bus_num
, addr
, speed_str
);
1544 class_str
= usb_class_str(class_id
);
1546 monitor_printf(mon
, " %s:", class_str
);
1548 monitor_printf(mon
, " Class %02x:", class_id
);
1549 monitor_printf(mon
, " USB device %04x:%04x", vendor_id
, product_id
);
1550 if (product_name
[0] != '\0')
1551 monitor_printf(mon
, ", %s", product_name
);
1552 monitor_printf(mon
, "\n");
1555 static int usb_host_info_device(void *opaque
, int bus_num
, int addr
,
1557 int vendor_id
, int product_id
,
1558 const char *product_name
,
1561 Monitor
*mon
= opaque
;
1563 usb_info_device(mon
, bus_num
, addr
, class_id
, vendor_id
, product_id
,
1564 product_name
, speed
);
1568 static void dec2str(int val
, char *str
, size_t size
)
1571 snprintf(str
, size
, "*");
1573 snprintf(str
, size
, "%d", val
);
1576 static void hex2str(int val
, char *str
, size_t size
)
1579 snprintf(str
, size
, "*");
1581 snprintf(str
, size
, "%04x", val
);
1584 void usb_host_info(Monitor
*mon
)
1586 struct USBAutoFilter
*f
;
1587 struct USBHostDevice
*s
;
1589 usb_host_scan(mon
, usb_host_info_device
);
1591 if (QTAILQ_EMPTY(&hostdevs
))
1593 monitor_printf(mon
, " Auto filters:\n");
1594 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1595 char bus
[10], addr
[10], vid
[10], pid
[10];
1597 dec2str(f
->bus_num
, bus
, sizeof(bus
));
1598 dec2str(f
->addr
, addr
, sizeof(addr
));
1599 hex2str(f
->vendor_id
, vid
, sizeof(vid
));
1600 hex2str(f
->product_id
, pid
, sizeof(pid
));
1601 monitor_printf(mon
, " Device %s.%s ID %s:%s\n",
1602 bus
, addr
, vid
, pid
);