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"
37 #if defined(__linux__)
42 #include <sys/ioctl.h>
45 #include <linux/usbdevice_fs.h>
46 #include <linux/version.h>
49 /* We redefine it to avoid version problems */
50 struct usb_ctrltransfer
{
60 struct usb_ctrlrequest
{
68 typedef int USBScanFunc(void *opaque
, int bus_num
, int addr
, int class_id
,
69 int vendor_id
, int product_id
,
70 const char *product_name
, int speed
);
71 static int usb_host_find_device(int *pbus_num
, int *paddr
,
72 char *product_name
, int product_name_size
,
77 #define dprintf printf
82 #define USBDBG_DEVOPENED "husb: opened %s/devices\n"
84 #define USBPROCBUS_PATH "/proc/bus/usb"
85 #define PRODUCT_NAME_SZ 32
86 #define MAX_ENDPOINTS 16
87 #define USBDEVBUS_PATH "/dev/bus/usb"
88 #define USBSYSBUS_PATH "/sys/bus/usb"
90 static char *usb_host_device_path
;
97 static int usb_fs_type
;
99 /* endpoint association data */
113 * Control transfer state.
114 * Note that 'buffer' _must_ follow 'req' field because
115 * we need contigious buffer when we submit control URB.
121 struct usb_ctrlrequest req
;
122 uint8_t buffer
[1024];
125 typedef struct USBHostDevice
{
135 struct ctrl_struct ctrl
;
136 struct endp_data endp_table
[MAX_ENDPOINTS
];
138 /* Host side address */
142 struct USBHostDevice
*next
;
145 static int is_isoc(USBHostDevice
*s
, int ep
)
147 return s
->endp_table
[ep
- 1].type
== USBDEVFS_URB_TYPE_ISO
;
150 static int is_halted(USBHostDevice
*s
, int ep
)
152 return s
->endp_table
[ep
- 1].halted
;
155 static void clear_halt(USBHostDevice
*s
, int ep
)
157 s
->endp_table
[ep
- 1].halted
= 0;
160 static void set_halt(USBHostDevice
*s
, int ep
)
162 s
->endp_table
[ep
- 1].halted
= 1;
165 static USBHostDevice
*hostdev_list
;
167 static void hostdev_link(USBHostDevice
*dev
)
169 dev
->next
= hostdev_list
;
173 static void hostdev_unlink(USBHostDevice
*dev
)
175 USBHostDevice
*pdev
= hostdev_list
;
176 USBHostDevice
**prev
= &hostdev_list
;
189 static USBHostDevice
*hostdev_find(int bus_num
, int addr
)
191 USBHostDevice
*s
= hostdev_list
;
193 if (s
->bus_num
== bus_num
&& s
->addr
== addr
)
202 * We always allocate one isoc descriptor even for bulk transfers
203 * to simplify allocation and casts.
205 typedef struct AsyncURB
207 struct usbdevfs_urb urb
;
208 struct usbdevfs_iso_packet_desc isocpd
;
214 static AsyncURB
*async_alloc(void)
216 return (AsyncURB
*) qemu_mallocz(sizeof(AsyncURB
));
219 static void async_free(AsyncURB
*aurb
)
224 static void async_complete_ctrl(USBHostDevice
*s
, USBPacket
*p
)
226 switch(s
->ctrl
.state
) {
227 case CTRL_STATE_SETUP
:
228 if (p
->len
< s
->ctrl
.len
)
229 s
->ctrl
.len
= p
->len
;
230 s
->ctrl
.state
= CTRL_STATE_DATA
;
235 s
->ctrl
.state
= CTRL_STATE_IDLE
;
244 static void async_complete(void *opaque
)
246 USBHostDevice
*s
= opaque
;
252 int r
= ioctl(s
->fd
, USBDEVFS_REAPURBNDELAY
, &aurb
);
257 if (errno
== ENODEV
&& !s
->closing
) {
258 printf("husb: device %d.%d disconnected\n", s
->bus_num
, s
->addr
);
259 usb_device_del_addr(0, s
->dev
.addr
);
263 dprintf("husb: async. reap urb failed errno %d\n", errno
);
269 dprintf("husb: async completed. aurb %p status %d alen %d\n",
270 aurb
, aurb
->urb
.status
, aurb
->urb
.actual_length
);
273 switch (aurb
->urb
.status
) {
275 p
->len
= aurb
->urb
.actual_length
;
276 if (aurb
->urb
.type
== USBDEVFS_URB_TYPE_CONTROL
)
277 async_complete_ctrl(s
, p
);
281 set_halt(s
, p
->devep
);
284 p
->len
= USB_RET_NAK
;
288 usb_packet_complete(p
);
295 static void async_cancel(USBPacket
*unused
, void *opaque
)
297 AsyncURB
*aurb
= opaque
;
298 USBHostDevice
*s
= aurb
->hdev
;
300 dprintf("husb: async cancel. aurb %p\n", aurb
);
302 /* Mark it as dead (see async_complete above) */
305 int r
= ioctl(s
->fd
, USBDEVFS_DISCARDURB
, aurb
);
307 dprintf("husb: async. discard urb failed errno %d\n", errno
);
311 static int usb_host_claim_interfaces(USBHostDevice
*dev
, int configuration
)
313 int dev_descr_len
, config_descr_len
;
314 int interface
, nb_interfaces
, nb_configurations
;
317 if (configuration
== 0) /* address state - ignore */
320 dprintf("husb: claiming interfaces. config %d\n", configuration
);
323 dev_descr_len
= dev
->descr
[0];
324 if (dev_descr_len
> dev
->descr_len
)
326 nb_configurations
= dev
->descr
[17];
329 while (i
< dev
->descr_len
) {
330 dprintf("husb: i is %d, descr_len is %d, dl %d, dt %d\n", i
, dev
->descr_len
,
331 dev
->descr
[i
], dev
->descr
[i
+1]);
333 if (dev
->descr
[i
+1] != USB_DT_CONFIG
) {
337 config_descr_len
= dev
->descr
[i
];
339 printf("husb: config #%d need %d\n", dev
->descr
[i
+ 5], configuration
);
341 if (configuration
< 0 || configuration
== dev
->descr
[i
+ 5]) {
342 configuration
= dev
->descr
[i
+ 5];
346 i
+= config_descr_len
;
349 if (i
>= dev
->descr_len
) {
350 fprintf(stderr
, "husb: update iface failed. no matching configuration\n");
353 nb_interfaces
= dev
->descr
[i
+ 4];
355 #ifdef USBDEVFS_DISCONNECT
356 /* earlier Linux 2.4 do not support that */
358 struct usbdevfs_ioctl ctrl
;
359 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
360 ctrl
.ioctl_code
= USBDEVFS_DISCONNECT
;
361 ctrl
.ifno
= interface
;
362 ret
= ioctl(dev
->fd
, USBDEVFS_IOCTL
, &ctrl
);
363 if (ret
< 0 && errno
!= ENODATA
) {
364 perror("USBDEVFS_DISCONNECT");
371 /* XXX: only grab if all interfaces are free */
372 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
373 ret
= ioctl(dev
->fd
, USBDEVFS_CLAIMINTERFACE
, &interface
);
375 if (errno
== EBUSY
) {
376 printf("husb: update iface. device already grabbed\n");
378 perror("husb: failed to claim interface");
385 printf("husb: %d interfaces claimed for configuration %d\n",
386 nb_interfaces
, configuration
);
388 dev
->ninterfaces
= nb_interfaces
;
389 dev
->configuration
= configuration
;
393 static int usb_host_release_interfaces(USBHostDevice
*s
)
397 dprintf("husb: releasing interfaces\n");
399 for (i
= 0; i
< s
->ninterfaces
; i
++) {
400 ret
= ioctl(s
->fd
, USBDEVFS_RELEASEINTERFACE
, &i
);
402 perror("husb: failed to release interface");
410 static void usb_host_handle_reset(USBDevice
*dev
)
412 USBHostDevice
*s
= (USBHostDevice
*) dev
;
414 dprintf("husb: reset device %u.%u\n", s
->bus_num
, s
->addr
);
416 ioctl(s
->fd
, USBDEVFS_RESET
);
418 usb_host_claim_interfaces(s
, s
->configuration
);
421 static void usb_host_handle_destroy(USBDevice
*dev
)
423 USBHostDevice
*s
= (USBHostDevice
*)dev
;
427 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
439 static int usb_linux_update_endp_table(USBHostDevice
*s
);
441 static int usb_host_handle_data(USBHostDevice
*s
, USBPacket
*p
)
443 struct usbdevfs_urb
*urb
;
447 aurb
= async_alloc();
453 if (p
->pid
== USB_TOKEN_IN
)
454 urb
->endpoint
= p
->devep
| 0x80;
456 urb
->endpoint
= p
->devep
;
458 if (is_halted(s
, p
->devep
)) {
459 ret
= ioctl(s
->fd
, USBDEVFS_CLEAR_HALT
, &urb
->endpoint
);
461 dprintf("husb: failed to clear halt. ep 0x%x errno %d\n",
462 urb
->endpoint
, errno
);
465 clear_halt(s
, p
->devep
);
468 urb
->buffer
= p
->data
;
469 urb
->buffer_length
= p
->len
;
471 if (is_isoc(s
, p
->devep
)) {
472 /* Setup ISOC transfer */
473 urb
->type
= USBDEVFS_URB_TYPE_ISO
;
474 urb
->flags
= USBDEVFS_URB_ISO_ASAP
;
475 urb
->number_of_packets
= 1;
476 urb
->iso_frame_desc
[0].length
= p
->len
;
478 /* Setup bulk transfer */
479 urb
->type
= USBDEVFS_URB_TYPE_BULK
;
482 urb
->usercontext
= s
;
484 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
486 dprintf("husb: data submit. ep 0x%x len %u aurb %p\n", urb
->endpoint
, p
->len
, aurb
);
489 dprintf("husb: submit failed. errno %d\n", errno
);
497 return USB_RET_STALL
;
501 usb_defer_packet(p
, async_cancel
, aurb
);
502 return USB_RET_ASYNC
;
505 static int ctrl_error(void)
507 if (errno
== ETIMEDOUT
)
510 return USB_RET_STALL
;
513 static int usb_host_set_address(USBHostDevice
*s
, int addr
)
515 dprintf("husb: ctrl set addr %u\n", addr
);
520 static int usb_host_set_config(USBHostDevice
*s
, int config
)
522 usb_host_release_interfaces(s
);
524 int ret
= ioctl(s
->fd
, USBDEVFS_SETCONFIGURATION
, &config
);
526 dprintf("husb: ctrl set config %d ret %d errno %d\n", config
, ret
, errno
);
531 usb_host_claim_interfaces(s
, config
);
535 static int usb_host_set_interface(USBHostDevice
*s
, int iface
, int alt
)
537 struct usbdevfs_setinterface si
;
540 si
.interface
= iface
;
542 ret
= ioctl(s
->fd
, USBDEVFS_SETINTERFACE
, &si
);
544 dprintf("husb: ctrl set iface %d altset %d ret %d errno %d\n",
545 iface
, alt
, ret
, errno
);
550 usb_linux_update_endp_table(s
);
554 static int usb_host_handle_control(USBHostDevice
*s
, USBPacket
*p
)
556 struct usbdevfs_urb
*urb
;
558 int ret
, value
, index
;
561 * Process certain standard device requests.
562 * These are infrequent and are processed synchronously.
564 value
= le16_to_cpu(s
->ctrl
.req
.wValue
);
565 index
= le16_to_cpu(s
->ctrl
.req
.wIndex
);
567 dprintf("husb: ctrl type 0x%x req 0x%x val 0x%x index %u len %u\n",
568 s
->ctrl
.req
.bRequestType
, s
->ctrl
.req
.bRequest
, value
, index
,
571 if (s
->ctrl
.req
.bRequestType
== 0) {
572 switch (s
->ctrl
.req
.bRequest
) {
573 case USB_REQ_SET_ADDRESS
:
574 return usb_host_set_address(s
, value
);
576 case USB_REQ_SET_CONFIGURATION
:
577 return usb_host_set_config(s
, value
& 0xff);
581 if (s
->ctrl
.req
.bRequestType
== 1 &&
582 s
->ctrl
.req
.bRequest
== USB_REQ_SET_INTERFACE
)
583 return usb_host_set_interface(s
, index
, value
);
585 /* The rest are asynchronous */
587 aurb
= async_alloc();
592 * Setup ctrl transfer.
594 * s->ctrl is layed out such that data buffer immediately follows
595 * 'req' struct which is exactly what usbdevfs expects.
599 urb
->type
= USBDEVFS_URB_TYPE_CONTROL
;
600 urb
->endpoint
= p
->devep
;
602 urb
->buffer
= &s
->ctrl
.req
;
603 urb
->buffer_length
= 8 + s
->ctrl
.len
;
605 urb
->usercontext
= s
;
607 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
609 dprintf("husb: submit ctrl. len %u aurb %p\n", urb
->buffer_length
, aurb
);
612 dprintf("husb: submit failed. errno %d\n", errno
);
620 return USB_RET_STALL
;
624 usb_defer_packet(p
, async_cancel
, aurb
);
625 return USB_RET_ASYNC
;
628 static int do_token_setup(USBDevice
*dev
, USBPacket
*p
)
630 USBHostDevice
*s
= (USBHostDevice
*) dev
;
634 return USB_RET_STALL
;
636 memcpy(&s
->ctrl
.req
, p
->data
, 8);
637 s
->ctrl
.len
= le16_to_cpu(s
->ctrl
.req
.wLength
);
639 s
->ctrl
.state
= CTRL_STATE_SETUP
;
641 if (s
->ctrl
.req
.bRequestType
& USB_DIR_IN
) {
642 ret
= usb_host_handle_control(s
, p
);
646 if (ret
< s
->ctrl
.len
)
648 s
->ctrl
.state
= CTRL_STATE_DATA
;
650 if (s
->ctrl
.len
== 0)
651 s
->ctrl
.state
= CTRL_STATE_ACK
;
653 s
->ctrl
.state
= CTRL_STATE_DATA
;
659 static int do_token_in(USBDevice
*dev
, USBPacket
*p
)
661 USBHostDevice
*s
= (USBHostDevice
*) dev
;
665 return usb_host_handle_data(s
, p
);
667 switch(s
->ctrl
.state
) {
669 if (!(s
->ctrl
.req
.bRequestType
& USB_DIR_IN
)) {
670 ret
= usb_host_handle_control(s
, p
);
671 if (ret
== USB_RET_ASYNC
)
672 return USB_RET_ASYNC
;
674 s
->ctrl
.state
= CTRL_STATE_IDLE
;
675 return ret
> 0 ? 0 : ret
;
680 case CTRL_STATE_DATA
:
681 if (s
->ctrl
.req
.bRequestType
& USB_DIR_IN
) {
682 int len
= s
->ctrl
.len
- s
->ctrl
.offset
;
685 memcpy(p
->data
, s
->ctrl
.buffer
+ s
->ctrl
.offset
, len
);
686 s
->ctrl
.offset
+= len
;
687 if (s
->ctrl
.offset
>= s
->ctrl
.len
)
688 s
->ctrl
.state
= CTRL_STATE_ACK
;
692 s
->ctrl
.state
= CTRL_STATE_IDLE
;
693 return USB_RET_STALL
;
696 return USB_RET_STALL
;
700 static int do_token_out(USBDevice
*dev
, USBPacket
*p
)
702 USBHostDevice
*s
= (USBHostDevice
*) dev
;
705 return usb_host_handle_data(s
, p
);
707 switch(s
->ctrl
.state
) {
709 if (s
->ctrl
.req
.bRequestType
& USB_DIR_IN
) {
710 s
->ctrl
.state
= CTRL_STATE_IDLE
;
713 /* ignore additional output */
717 case CTRL_STATE_DATA
:
718 if (!(s
->ctrl
.req
.bRequestType
& USB_DIR_IN
)) {
719 int len
= s
->ctrl
.len
- s
->ctrl
.offset
;
722 memcpy(s
->ctrl
.buffer
+ s
->ctrl
.offset
, p
->data
, len
);
723 s
->ctrl
.offset
+= len
;
724 if (s
->ctrl
.offset
>= s
->ctrl
.len
)
725 s
->ctrl
.state
= CTRL_STATE_ACK
;
729 s
->ctrl
.state
= CTRL_STATE_IDLE
;
730 return USB_RET_STALL
;
733 return USB_RET_STALL
;
739 * Called by the HC (host controller).
741 * Returns length of the transaction or one of the USB_RET_XXX codes.
743 static int usb_host_handle_packet(USBDevice
*s
, USBPacket
*p
)
747 s
->state
= USB_STATE_ATTACHED
;
751 s
->state
= USB_STATE_NOTATTACHED
;
755 s
->remote_wakeup
= 0;
757 s
->state
= USB_STATE_DEFAULT
;
762 /* Rest of the PIDs must match our address */
763 if (s
->state
< USB_STATE_DEFAULT
|| p
->devaddr
!= s
->addr
)
764 return USB_RET_NODEV
;
767 case USB_TOKEN_SETUP
:
768 return do_token_setup(s
, p
);
771 return do_token_in(s
, p
);
774 return do_token_out(s
, p
);
777 return USB_RET_STALL
;
781 /* returns 1 on problem encountered or 0 for success */
782 static int usb_linux_update_endp_table(USBHostDevice
*s
)
784 uint8_t *descriptors
;
785 uint8_t devep
, type
, configuration
, alt_interface
;
786 struct usb_ctrltransfer ct
;
787 int interface
, ret
, length
, i
;
789 ct
.bRequestType
= USB_DIR_IN
;
790 ct
.bRequest
= USB_REQ_GET_CONFIGURATION
;
794 ct
.data
= &configuration
;
797 ret
= ioctl(s
->fd
, USBDEVFS_CONTROL
, &ct
);
799 perror("usb_linux_update_endp_table");
803 /* in address state */
804 if (configuration
== 0)
807 /* get the desired configuration, interface, and endpoint descriptors
808 * from device description */
809 descriptors
= &s
->descr
[18];
810 length
= s
->descr_len
- 18;
813 if (descriptors
[i
+ 1] != USB_DT_CONFIG
||
814 descriptors
[i
+ 5] != configuration
) {
815 dprintf("invalid descriptor data - configuration\n");
821 if (descriptors
[i
+ 1] != USB_DT_INTERFACE
||
822 (descriptors
[i
+ 1] == USB_DT_INTERFACE
&&
823 descriptors
[i
+ 4] == 0)) {
828 interface
= descriptors
[i
+ 2];
830 ct
.bRequestType
= USB_DIR_IN
| USB_RECIP_INTERFACE
;
831 ct
.bRequest
= USB_REQ_GET_INTERFACE
;
833 ct
.wIndex
= interface
;
835 ct
.data
= &alt_interface
;
838 ret
= ioctl(s
->fd
, USBDEVFS_CONTROL
, &ct
);
840 perror("usb_linux_update_endp_table");
844 /* the current interface descriptor is the active interface
845 * and has endpoints */
846 if (descriptors
[i
+ 3] != alt_interface
) {
851 /* advance to the endpoints */
852 while (i
< length
&& descriptors
[i
+1] != USB_DT_ENDPOINT
)
859 if (descriptors
[i
+ 1] != USB_DT_ENDPOINT
)
862 devep
= descriptors
[i
+ 2];
863 switch (descriptors
[i
+ 3] & 0x3) {
865 type
= USBDEVFS_URB_TYPE_CONTROL
;
868 type
= USBDEVFS_URB_TYPE_ISO
;
871 type
= USBDEVFS_URB_TYPE_BULK
;
874 type
= USBDEVFS_URB_TYPE_INTERRUPT
;
877 dprintf("usb_host: malformed endpoint type\n");
878 type
= USBDEVFS_URB_TYPE_BULK
;
880 s
->endp_table
[(devep
& 0xf) - 1].type
= type
;
881 s
->endp_table
[(devep
& 0xf) - 1].halted
= 0;
889 static USBDevice
*usb_host_device_open_addr(int bus_num
, int addr
, const char *prod_name
)
892 USBHostDevice
*dev
= NULL
;
893 struct usbdevfs_connectinfo ci
;
896 dev
= qemu_mallocz(sizeof(USBHostDevice
));
898 dev
->bus_num
= bus_num
;
901 printf("husb: open device %d.%d\n", bus_num
, addr
);
903 if (!usb_host_device_path
) {
904 perror("husb: USB Host Device Path not set");
907 snprintf(buf
, sizeof(buf
), "%s/%03d/%03d", usb_host_device_path
,
909 fd
= open(buf
, O_RDWR
| O_NONBLOCK
);
914 dprintf("husb: opened %s\n", buf
);
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
]);
929 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))
944 ret
= ioctl(fd
, USBDEVFS_CONNECTINFO
, &ci
);
946 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
950 printf("husb: grabbed usb device %d.%d\n", bus_num
, addr
);
952 ret
= usb_linux_update_endp_table(dev
);
957 dev
->dev
.speed
= USB_SPEED_LOW
;
959 dev
->dev
.speed
= USB_SPEED_HIGH
;
961 dev
->dev
.handle_packet
= usb_host_handle_packet
;
962 dev
->dev
.handle_reset
= usb_host_handle_reset
;
963 dev
->dev
.handle_destroy
= usb_host_handle_destroy
;
965 if (!prod_name
|| prod_name
[0] == '\0')
966 snprintf(dev
->dev
.devname
, sizeof(dev
->dev
.devname
),
967 "host:%d.%d", bus_num
, addr
);
969 pstrcpy(dev
->dev
.devname
, sizeof(dev
->dev
.devname
),
972 /* USB devio uses 'write' flag to check for async completions */
973 qemu_set_fd_handler(dev
->fd
, NULL
, async_complete
, dev
);
977 return (USBDevice
*) dev
;
987 static int usb_host_auto_add(const char *spec
);
988 static int usb_host_auto_del(const char *spec
);
990 USBDevice
*usb_host_device_open(const char *devname
)
993 char product_name
[PRODUCT_NAME_SZ
];
995 if (strstr(devname
, "auto:")) {
996 usb_host_auto_add(devname
);
1000 if (usb_host_find_device(&bus_num
, &addr
, product_name
, sizeof(product_name
),
1004 if (hostdev_find(bus_num
, addr
)) {
1005 term_printf("husb: host usb device %d.%d is already open\n", bus_num
, addr
);
1009 return usb_host_device_open_addr(bus_num
, addr
, product_name
);
1012 int usb_host_device_close(const char *devname
)
1014 char product_name
[PRODUCT_NAME_SZ
];
1018 if (strstr(devname
, "auto:"))
1019 return usb_host_auto_del(devname
);
1021 if (usb_host_find_device(&bus_num
, &addr
, product_name
, sizeof(product_name
),
1025 s
= hostdev_find(bus_num
, addr
);
1027 usb_device_del_addr(0, s
->dev
.addr
);
1034 static int get_tag_value(char *buf
, int buf_size
,
1035 const char *str
, const char *tag
,
1036 const char *stopchars
)
1040 p
= strstr(str
, tag
);
1044 while (qemu_isspace(*p
))
1047 while (*p
!= '\0' && !strchr(stopchars
, *p
)) {
1048 if ((q
- buf
) < (buf_size
- 1))
1057 * Use /proc/bus/usb/devices or /dev/bus/usb/devices file to determine
1058 * host's USB devices. This is legacy support since many distributions
1059 * are moving to /sys/bus/usb
1061 static int usb_host_scan_dev(void *opaque
, USBScanFunc
*func
)
1066 int bus_num
, addr
, speed
, device_count
, class_id
, product_id
, vendor_id
;
1067 char product_name
[512];
1070 if (!usb_host_device_path
) {
1071 perror("husb: USB Host Device Path not set");
1074 snprintf(line
, sizeof(line
), "%s/devices", usb_host_device_path
);
1075 f
= fopen(line
, "r");
1077 perror("husb: cannot open devices file");
1082 bus_num
= addr
= speed
= class_id
= product_id
= vendor_id
= 0;
1084 if (fgets(line
, sizeof(line
), f
) == NULL
)
1086 if (strlen(line
) > 0)
1087 line
[strlen(line
) - 1] = '\0';
1088 if (line
[0] == 'T' && line
[1] == ':') {
1089 if (device_count
&& (vendor_id
|| product_id
)) {
1090 /* New device. Add the previously discovered device. */
1091 ret
= func(opaque
, bus_num
, addr
, class_id
, vendor_id
,
1092 product_id
, product_name
, speed
);
1096 if (get_tag_value(buf
, sizeof(buf
), line
, "Bus=", " ") < 0)
1098 bus_num
= atoi(buf
);
1099 if (get_tag_value(buf
, sizeof(buf
), line
, "Dev#=", " ") < 0)
1102 if (get_tag_value(buf
, sizeof(buf
), line
, "Spd=", " ") < 0)
1104 if (!strcmp(buf
, "480"))
1105 speed
= USB_SPEED_HIGH
;
1106 else if (!strcmp(buf
, "1.5"))
1107 speed
= USB_SPEED_LOW
;
1109 speed
= USB_SPEED_FULL
;
1110 product_name
[0] = '\0';
1115 } else if (line
[0] == 'P' && line
[1] == ':') {
1116 if (get_tag_value(buf
, sizeof(buf
), line
, "Vendor=", " ") < 0)
1118 vendor_id
= strtoul(buf
, NULL
, 16);
1119 if (get_tag_value(buf
, sizeof(buf
), line
, "ProdID=", " ") < 0)
1121 product_id
= strtoul(buf
, NULL
, 16);
1122 } else if (line
[0] == 'S' && line
[1] == ':') {
1123 if (get_tag_value(buf
, sizeof(buf
), line
, "Product=", "") < 0)
1125 pstrcpy(product_name
, sizeof(product_name
), buf
);
1126 } else if (line
[0] == 'D' && line
[1] == ':') {
1127 if (get_tag_value(buf
, sizeof(buf
), line
, "Cls=", " (") < 0)
1129 class_id
= strtoul(buf
, NULL
, 16);
1133 if (device_count
&& (vendor_id
|| product_id
)) {
1134 /* Add the last device. */
1135 ret
= func(opaque
, bus_num
, addr
, class_id
, vendor_id
,
1136 product_id
, product_name
, speed
);
1145 * Read sys file-system device file
1147 * @line address of buffer to put file contents in
1148 * @line_size size of line
1149 * @device_file path to device file (printf format string)
1150 * @device_name device being opened (inserted into device_file)
1152 * @return 0 failed, 1 succeeded ('line' contains data)
1154 static int usb_host_read_file(char *line
, size_t line_size
, const char *device_file
, const char *device_name
)
1158 char filename
[PATH_MAX
];
1160 snprintf(filename
, PATH_MAX
, USBSYSBUS_PATH
"/devices/%s/%s", device_name
,
1162 f
= fopen(filename
, "r");
1164 fgets(line
, line_size
, f
);
1168 term_printf("husb: could not open %s\n", filename
);
1175 * Use /sys/bus/usb/devices/ directory to determine host's USB
1178 * This code is based on Robert Schiele's original patches posted to
1179 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1181 static int usb_host_scan_sys(void *opaque
, USBScanFunc
*func
)
1185 int bus_num
, addr
, speed
, class_id
, product_id
, vendor_id
;
1187 char product_name
[512];
1190 dir
= opendir(USBSYSBUS_PATH
"/devices");
1192 perror("husb: cannot open devices directory");
1196 while ((de
= readdir(dir
))) {
1197 if (de
->d_name
[0] != '.' && !strchr(de
->d_name
, ':')) {
1198 char *tmpstr
= de
->d_name
;
1199 if (!strncmp(de
->d_name
, "usb", 3))
1201 bus_num
= atoi(tmpstr
);
1203 if (!usb_host_read_file(line
, sizeof(line
), "devnum", de
->d_name
))
1205 if (sscanf(line
, "%d", &addr
) != 1)
1208 if (!usb_host_read_file(line
, sizeof(line
), "bDeviceClass",
1211 if (sscanf(line
, "%x", &class_id
) != 1)
1214 if (!usb_host_read_file(line
, sizeof(line
), "idVendor", de
->d_name
))
1216 if (sscanf(line
, "%x", &vendor_id
) != 1)
1219 if (!usb_host_read_file(line
, sizeof(line
), "idProduct",
1222 if (sscanf(line
, "%x", &product_id
) != 1)
1225 if (!usb_host_read_file(line
, sizeof(line
), "product",
1229 if (strlen(line
) > 0)
1230 line
[strlen(line
) - 1] = '\0';
1231 pstrcpy(product_name
, sizeof(product_name
), line
);
1234 if (!usb_host_read_file(line
, sizeof(line
), "speed", de
->d_name
))
1236 if (!strcmp(line
, "480\n"))
1237 speed
= USB_SPEED_HIGH
;
1238 else if (!strcmp(line
, "1.5\n"))
1239 speed
= USB_SPEED_LOW
;
1241 speed
= USB_SPEED_FULL
;
1243 ret
= func(opaque
, bus_num
, addr
, class_id
, vendor_id
,
1244 product_id
, product_name
, speed
);
1256 * Determine how to access the host's USB devices and call the
1257 * specific support function.
1259 static int usb_host_scan(void *opaque
, USBScanFunc
*func
)
1264 const char *fs_type
[] = {"unknown", "proc", "dev", "sys"};
1265 char devpath
[PATH_MAX
];
1267 /* only check the host once */
1269 f
= fopen(USBPROCBUS_PATH
"/devices", "r");
1271 /* devices found in /proc/bus/usb/ */
1272 strcpy(devpath
, USBPROCBUS_PATH
);
1273 usb_fs_type
= USB_FS_PROC
;
1275 dprintf(USBDBG_DEVOPENED
, USBPROCBUS_PATH
);
1278 /* try additional methods if an access method hasn't been found yet */
1279 f
= fopen(USBDEVBUS_PATH
"/devices", "r");
1281 /* devices found in /dev/bus/usb/ */
1282 strcpy(devpath
, USBDEVBUS_PATH
);
1283 usb_fs_type
= USB_FS_DEV
;
1285 dprintf(USBDBG_DEVOPENED
, USBDEVBUS_PATH
);
1288 dir
= opendir(USBSYSBUS_PATH
"/devices");
1290 /* devices found in /dev/bus/usb/ (yes - not a mistake!) */
1291 strcpy(devpath
, USBDEVBUS_PATH
);
1292 usb_fs_type
= USB_FS_SYS
;
1294 dprintf(USBDBG_DEVOPENED
, USBSYSBUS_PATH
);
1299 term_printf("husb: unable to access USB devices\n");
1303 /* the module setting (used later for opening devices) */
1304 usb_host_device_path
= qemu_mallocz(strlen(devpath
)+1);
1305 strcpy(usb_host_device_path
, devpath
);
1306 term_printf("husb: using %s file-system with %s\n", fs_type
[usb_fs_type
], usb_host_device_path
);
1309 switch (usb_fs_type
) {
1312 ret
= usb_host_scan_dev(opaque
, func
);
1315 ret
= usb_host_scan_sys(opaque
, func
);
1324 struct USBAutoFilter
{
1325 struct USBAutoFilter
*next
;
1332 static QEMUTimer
*usb_auto_timer
;
1333 static struct USBAutoFilter
*usb_auto_filter
;
1335 static int usb_host_auto_scan(void *opaque
, int bus_num
, int addr
,
1336 int class_id
, int vendor_id
, int product_id
,
1337 const char *product_name
, int speed
)
1339 struct USBAutoFilter
*f
;
1340 struct USBDevice
*dev
;
1346 for (f
= usb_auto_filter
; f
; f
= f
->next
) {
1347 if (f
->bus_num
>= 0 && f
->bus_num
!= bus_num
)
1350 if (f
->addr
>= 0 && f
->addr
!= addr
)
1353 if (f
->vendor_id
>= 0 && f
->vendor_id
!= vendor_id
)
1356 if (f
->product_id
>= 0 && f
->product_id
!= product_id
)
1359 /* We got a match */
1361 /* Allredy attached ? */
1362 if (hostdev_find(bus_num
, addr
))
1365 dprintf("husb: auto open: bus_num %d addr %d\n", bus_num
, addr
);
1367 dev
= usb_host_device_open_addr(bus_num
, addr
, product_name
);
1369 usb_device_add_dev(dev
);
1375 static void usb_host_auto_timer(void *unused
)
1377 usb_host_scan(NULL
, usb_host_auto_scan
);
1378 qemu_mod_timer(usb_auto_timer
, qemu_get_clock(rt_clock
) + 2000);
1382 * Autoconnect filter
1384 * auto:bus:dev[:vid:pid]
1385 * auto:bus.dev[:vid:pid]
1387 * bus - bus number (dec, * means any)
1388 * dev - device number (dec, * means any)
1389 * vid - vendor id (hex, * means any)
1390 * pid - product id (hex, * means any)
1392 * See 'lsusb' output.
1394 static int parse_filter(const char *spec
, struct USBAutoFilter
*f
)
1396 enum { BUS
, DEV
, VID
, PID
, DONE
};
1397 const char *p
= spec
;
1405 for (i
= BUS
; i
< DONE
; i
++) {
1406 p
= strpbrk(p
, ":.");
1414 case BUS
: f
->bus_num
= strtol(p
, NULL
, 10); break;
1415 case DEV
: f
->addr
= strtol(p
, NULL
, 10); break;
1416 case VID
: f
->vendor_id
= strtol(p
, NULL
, 16); break;
1417 case PID
: f
->product_id
= strtol(p
, NULL
, 16); break;
1422 fprintf(stderr
, "husb: invalid auto filter spec %s\n", spec
);
1429 static int match_filter(const struct USBAutoFilter
*f1
,
1430 const struct USBAutoFilter
*f2
)
1432 return f1
->bus_num
== f2
->bus_num
&&
1433 f1
->addr
== f2
->addr
&&
1434 f1
->vendor_id
== f2
->vendor_id
&&
1435 f1
->product_id
== f2
->product_id
;
1438 static int usb_host_auto_add(const char *spec
)
1440 struct USBAutoFilter filter
, *f
;
1442 if (parse_filter(spec
, &filter
) < 0)
1445 f
= qemu_mallocz(sizeof(*f
));
1449 if (!usb_auto_filter
) {
1451 * First entry. Init and start the monitor.
1452 * Right now we're using timer to check for new devices.
1453 * If this turns out to be too expensive we can move that into a
1456 usb_auto_timer
= qemu_new_timer(rt_clock
, usb_host_auto_timer
, NULL
);
1457 if (!usb_auto_timer
) {
1458 fprintf(stderr
, "husb: failed to allocate auto scan timer\n");
1463 /* Check for new devices every two seconds */
1464 qemu_mod_timer(usb_auto_timer
, qemu_get_clock(rt_clock
) + 2000);
1467 dprintf("husb: added auto filter: bus_num %d addr %d vid %d pid %d\n",
1468 f
->bus_num
, f
->addr
, f
->vendor_id
, f
->product_id
);
1470 f
->next
= usb_auto_filter
;
1471 usb_auto_filter
= f
;
1476 static int usb_host_auto_del(const char *spec
)
1478 struct USBAutoFilter
*pf
= usb_auto_filter
;
1479 struct USBAutoFilter
**prev
= &usb_auto_filter
;
1480 struct USBAutoFilter filter
;
1482 if (parse_filter(spec
, &filter
) < 0)
1486 if (match_filter(pf
, &filter
)) {
1487 dprintf("husb: removed auto filter: bus_num %d addr %d vid %d pid %d\n",
1488 pf
->bus_num
, pf
->addr
, pf
->vendor_id
, pf
->product_id
);
1492 if (!usb_auto_filter
) {
1493 /* No more filters. Stop scanning. */
1494 qemu_del_timer(usb_auto_timer
);
1495 qemu_free_timer(usb_auto_timer
);
1508 typedef struct FindDeviceState
{
1513 char product_name
[PRODUCT_NAME_SZ
];
1516 static int usb_host_find_device_scan(void *opaque
, int bus_num
, int addr
,
1518 int vendor_id
, int product_id
,
1519 const char *product_name
, int speed
)
1521 FindDeviceState
*s
= opaque
;
1522 if ((vendor_id
== s
->vendor_id
&&
1523 product_id
== s
->product_id
) ||
1524 (bus_num
== s
->bus_num
&&
1526 pstrcpy(s
->product_name
, PRODUCT_NAME_SZ
, product_name
);
1527 s
->bus_num
= bus_num
;
1536 'bus.addr' (decimal numbers) or
1537 'vendor_id:product_id' (hexa numbers) */
1538 static int usb_host_find_device(int *pbus_num
, int *paddr
,
1539 char *product_name
, int product_name_size
,
1540 const char *devname
)
1546 p
= strchr(devname
, '.');
1548 *pbus_num
= strtoul(devname
, NULL
, 0);
1549 *paddr
= strtoul(p
+ 1, NULL
, 0);
1550 fs
.bus_num
= *pbus_num
;
1552 ret
= usb_host_scan(&fs
, usb_host_find_device_scan
);
1554 pstrcpy(product_name
, product_name_size
, fs
.product_name
);
1558 p
= strchr(devname
, ':');
1560 fs
.vendor_id
= strtoul(devname
, NULL
, 16);
1561 fs
.product_id
= strtoul(p
+ 1, NULL
, 16);
1562 ret
= usb_host_scan(&fs
, usb_host_find_device_scan
);
1564 *pbus_num
= fs
.bus_num
;
1566 pstrcpy(product_name
, product_name_size
, fs
.product_name
);
1573 /**********************/
1574 /* USB host device info */
1576 struct usb_class_info
{
1578 const char *class_name
;
1581 static const struct usb_class_info usb_class_info
[] = {
1582 { USB_CLASS_AUDIO
, "Audio"},
1583 { USB_CLASS_COMM
, "Communication"},
1584 { USB_CLASS_HID
, "HID"},
1585 { USB_CLASS_HUB
, "Hub" },
1586 { USB_CLASS_PHYSICAL
, "Physical" },
1587 { USB_CLASS_PRINTER
, "Printer" },
1588 { USB_CLASS_MASS_STORAGE
, "Storage" },
1589 { USB_CLASS_CDC_DATA
, "Data" },
1590 { USB_CLASS_APP_SPEC
, "Application Specific" },
1591 { USB_CLASS_VENDOR_SPEC
, "Vendor Specific" },
1592 { USB_CLASS_STILL_IMAGE
, "Still Image" },
1593 { USB_CLASS_CSCID
, "Smart Card" },
1594 { USB_CLASS_CONTENT_SEC
, "Content Security" },
1598 static const char *usb_class_str(uint8_t class)
1600 const struct usb_class_info
*p
;
1601 for(p
= usb_class_info
; p
->class != -1; p
++) {
1602 if (p
->class == class)
1605 return p
->class_name
;
1608 static void usb_info_device(int bus_num
, int addr
, int class_id
,
1609 int vendor_id
, int product_id
,
1610 const char *product_name
,
1613 const char *class_str
, *speed_str
;
1619 case USB_SPEED_FULL
:
1622 case USB_SPEED_HIGH
:
1630 term_printf(" Device %d.%d, speed %s Mb/s\n",
1631 bus_num
, addr
, speed_str
);
1632 class_str
= usb_class_str(class_id
);
1634 term_printf(" %s:", class_str
);
1636 term_printf(" Class %02x:", class_id
);
1637 term_printf(" USB device %04x:%04x", vendor_id
, product_id
);
1638 if (product_name
[0] != '\0')
1639 term_printf(", %s", product_name
);
1643 static int usb_host_info_device(void *opaque
, int bus_num
, int addr
,
1645 int vendor_id
, int product_id
,
1646 const char *product_name
,
1649 usb_info_device(bus_num
, addr
, class_id
, vendor_id
, product_id
,
1650 product_name
, speed
);
1654 static void dec2str(int val
, char *str
, size_t size
)
1657 snprintf(str
, size
, "*");
1659 snprintf(str
, size
, "%d", val
);
1662 static void hex2str(int val
, char *str
, size_t size
)
1665 snprintf(str
, size
, "*");
1667 snprintf(str
, size
, "%x", val
);
1670 void usb_host_info(void)
1672 struct USBAutoFilter
*f
;
1674 usb_host_scan(NULL
, usb_host_info_device
);
1676 if (usb_auto_filter
)
1677 term_printf(" Auto filters:\n");
1678 for (f
= usb_auto_filter
; f
; f
= f
->next
) {
1679 char bus
[10], addr
[10], vid
[10], pid
[10];
1680 dec2str(f
->bus_num
, bus
, sizeof(bus
));
1681 dec2str(f
->addr
, addr
, sizeof(addr
));
1682 hex2str(f
->vendor_id
, vid
, sizeof(vid
));
1683 hex2str(f
->product_id
, pid
, sizeof(pid
));
1684 term_printf(" Device %s.%s ID %s:%s\n", bus
, addr
, vid
, pid
);