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__)
39 #include <sys/ioctl.h>
42 #include <linux/usbdevice_fs.h>
43 #include <linux/version.h>
46 /* We redefine it to avoid version problems */
47 struct usb_ctrltransfer
{
57 struct usb_ctrlrequest
{
65 typedef int USBScanFunc(void *opaque
, int bus_num
, int addr
, int class_id
,
66 int vendor_id
, int product_id
,
67 const char *product_name
, int speed
);
68 static int usb_host_find_device(int *pbus_num
, int *paddr
,
69 char *product_name
, int product_name_size
,
74 #define dprintf printf
79 #define USBDBG_DEVOPENED "husb: opened %s/devices\n"
81 #define USBPROCBUS_PATH "/proc/bus/usb"
82 #define PRODUCT_NAME_SZ 32
83 #define MAX_ENDPOINTS 16
84 #define USBDEVBUS_PATH "/dev/bus/usb"
85 #define USBSYSBUS_PATH "/sys/bus/usb"
87 static char *usb_host_device_path
;
94 static int usb_fs_type
;
96 /* endpoint association data */
110 * Control transfer state.
111 * Note that 'buffer' _must_ follow 'req' field because
112 * we need contigious buffer when we submit control URB.
118 struct usb_ctrlrequest req
;
119 uint8_t buffer
[1024];
122 typedef struct USBHostDevice
{
132 struct ctrl_struct ctrl
;
133 struct endp_data endp_table
[MAX_ENDPOINTS
];
135 /* Host side address */
139 struct USBHostDevice
*next
;
142 static int is_isoc(USBHostDevice
*s
, int ep
)
144 return s
->endp_table
[ep
- 1].type
== USBDEVFS_URB_TYPE_ISO
;
147 static int is_halted(USBHostDevice
*s
, int ep
)
149 return s
->endp_table
[ep
- 1].halted
;
152 static void clear_halt(USBHostDevice
*s
, int ep
)
154 s
->endp_table
[ep
- 1].halted
= 0;
157 static void set_halt(USBHostDevice
*s
, int ep
)
159 s
->endp_table
[ep
- 1].halted
= 1;
162 static USBHostDevice
*hostdev_list
;
164 static void hostdev_link(USBHostDevice
*dev
)
166 dev
->next
= hostdev_list
;
170 static void hostdev_unlink(USBHostDevice
*dev
)
172 USBHostDevice
*pdev
= hostdev_list
;
173 USBHostDevice
**prev
= &hostdev_list
;
186 static USBHostDevice
*hostdev_find(int bus_num
, int addr
)
188 USBHostDevice
*s
= hostdev_list
;
190 if (s
->bus_num
== bus_num
&& s
->addr
== addr
)
199 * We always allocate one isoc descriptor even for bulk transfers
200 * to simplify allocation and casts.
202 typedef struct AsyncURB
204 struct usbdevfs_urb urb
;
205 struct usbdevfs_iso_packet_desc isocpd
;
211 static AsyncURB
*async_alloc(void)
213 return (AsyncURB
*) qemu_mallocz(sizeof(AsyncURB
));
216 static void async_free(AsyncURB
*aurb
)
221 static void async_complete_ctrl(USBHostDevice
*s
, USBPacket
*p
)
223 switch(s
->ctrl
.state
) {
224 case CTRL_STATE_SETUP
:
225 if (p
->len
< s
->ctrl
.len
)
226 s
->ctrl
.len
= p
->len
;
227 s
->ctrl
.state
= CTRL_STATE_DATA
;
232 s
->ctrl
.state
= CTRL_STATE_IDLE
;
241 static void async_complete(void *opaque
)
243 USBHostDevice
*s
= opaque
;
249 int r
= ioctl(s
->fd
, USBDEVFS_REAPURBNDELAY
, &aurb
);
254 if (errno
== ENODEV
&& !s
->closing
) {
255 printf("husb: device %d.%d disconnected\n", s
->bus_num
, s
->addr
);
256 usb_device_del_addr(0, s
->dev
.addr
);
260 dprintf("husb: async. reap urb failed errno %d\n", errno
);
266 dprintf("husb: async completed. aurb %p status %d alen %d\n",
267 aurb
, aurb
->urb
.status
, aurb
->urb
.actual_length
);
270 switch (aurb
->urb
.status
) {
272 p
->len
= aurb
->urb
.actual_length
;
273 if (aurb
->urb
.type
== USBDEVFS_URB_TYPE_CONTROL
)
274 async_complete_ctrl(s
, p
);
278 set_halt(s
, p
->devep
);
281 p
->len
= USB_RET_NAK
;
285 usb_packet_complete(p
);
292 static void async_cancel(USBPacket
*unused
, void *opaque
)
294 AsyncURB
*aurb
= opaque
;
295 USBHostDevice
*s
= aurb
->hdev
;
297 dprintf("husb: async cancel. aurb %p\n", aurb
);
299 /* Mark it as dead (see async_complete above) */
302 int r
= ioctl(s
->fd
, USBDEVFS_DISCARDURB
, aurb
);
304 dprintf("husb: async. discard urb failed errno %d\n", errno
);
308 static int usb_host_claim_interfaces(USBHostDevice
*dev
, int configuration
)
310 int dev_descr_len
, config_descr_len
;
311 int interface
, nb_interfaces
, nb_configurations
;
314 if (configuration
== 0) /* address state - ignore */
317 dprintf("husb: claiming interfaces. config %d\n", configuration
);
320 dev_descr_len
= dev
->descr
[0];
321 if (dev_descr_len
> dev
->descr_len
)
323 nb_configurations
= dev
->descr
[17];
326 while (i
< dev
->descr_len
) {
327 dprintf("husb: i is %d, descr_len is %d, dl %d, dt %d\n", i
, dev
->descr_len
,
328 dev
->descr
[i
], dev
->descr
[i
+1]);
330 if (dev
->descr
[i
+1] != USB_DT_CONFIG
) {
334 config_descr_len
= dev
->descr
[i
];
336 printf("husb: config #%d need %d\n", dev
->descr
[i
+ 5], configuration
);
338 if (configuration
< 0 || configuration
== dev
->descr
[i
+ 5]) {
339 configuration
= dev
->descr
[i
+ 5];
343 i
+= config_descr_len
;
346 if (i
>= dev
->descr_len
) {
347 fprintf(stderr
, "husb: update iface failed. no matching configuration\n");
350 nb_interfaces
= dev
->descr
[i
+ 4];
352 #ifdef USBDEVFS_DISCONNECT
353 /* earlier Linux 2.4 do not support that */
355 struct usbdevfs_ioctl ctrl
;
356 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
357 ctrl
.ioctl_code
= USBDEVFS_DISCONNECT
;
358 ctrl
.ifno
= interface
;
359 ret
= ioctl(dev
->fd
, USBDEVFS_IOCTL
, &ctrl
);
360 if (ret
< 0 && errno
!= ENODATA
) {
361 perror("USBDEVFS_DISCONNECT");
368 /* XXX: only grab if all interfaces are free */
369 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
370 ret
= ioctl(dev
->fd
, USBDEVFS_CLAIMINTERFACE
, &interface
);
372 if (errno
== EBUSY
) {
373 printf("husb: update iface. device already grabbed\n");
375 perror("husb: failed to claim interface");
382 printf("husb: %d interfaces claimed for configuration %d\n",
383 nb_interfaces
, configuration
);
385 dev
->ninterfaces
= nb_interfaces
;
386 dev
->configuration
= configuration
;
390 static int usb_host_release_interfaces(USBHostDevice
*s
)
394 dprintf("husb: releasing interfaces\n");
396 for (i
= 0; i
< s
->ninterfaces
; i
++) {
397 ret
= ioctl(s
->fd
, USBDEVFS_RELEASEINTERFACE
, &i
);
399 perror("husb: failed to release interface");
407 static void usb_host_handle_reset(USBDevice
*dev
)
409 USBHostDevice
*s
= (USBHostDevice
*) dev
;
411 dprintf("husb: reset device %u.%u\n", s
->bus_num
, s
->addr
);
413 ioctl(s
->fd
, USBDEVFS_RESET
);
415 usb_host_claim_interfaces(s
, s
->configuration
);
418 static void usb_host_handle_destroy(USBDevice
*dev
)
420 USBHostDevice
*s
= (USBHostDevice
*)dev
;
424 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
436 static int usb_linux_update_endp_table(USBHostDevice
*s
);
438 static int usb_host_handle_data(USBHostDevice
*s
, USBPacket
*p
)
440 struct usbdevfs_urb
*urb
;
444 aurb
= async_alloc();
446 dprintf("husb: async malloc failed\n");
454 if (p
->pid
== USB_TOKEN_IN
)
455 urb
->endpoint
= p
->devep
| 0x80;
457 urb
->endpoint
= p
->devep
;
459 if (is_halted(s
, p
->devep
)) {
460 ret
= ioctl(s
->fd
, USBDEVFS_CLEAR_HALT
, &urb
->endpoint
);
462 dprintf("husb: failed to clear halt. ep 0x%x errno %d\n",
463 urb
->endpoint
, errno
);
466 clear_halt(s
, p
->devep
);
469 urb
->buffer
= p
->data
;
470 urb
->buffer_length
= p
->len
;
472 if (is_isoc(s
, p
->devep
)) {
473 /* Setup ISOC transfer */
474 urb
->type
= USBDEVFS_URB_TYPE_ISO
;
475 urb
->flags
= USBDEVFS_URB_ISO_ASAP
;
476 urb
->number_of_packets
= 1;
477 urb
->iso_frame_desc
[0].length
= p
->len
;
479 /* Setup bulk transfer */
480 urb
->type
= USBDEVFS_URB_TYPE_BULK
;
483 urb
->usercontext
= s
;
485 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
487 dprintf("husb: data submit. ep 0x%x len %u aurb %p\n", urb
->endpoint
, p
->len
, aurb
);
490 dprintf("husb: submit failed. errno %d\n", errno
);
498 return USB_RET_STALL
;
502 usb_defer_packet(p
, async_cancel
, aurb
);
503 return USB_RET_ASYNC
;
506 static int ctrl_error(void)
508 if (errno
== ETIMEDOUT
)
511 return USB_RET_STALL
;
514 static int usb_host_set_address(USBHostDevice
*s
, int addr
)
516 dprintf("husb: ctrl set addr %u\n", addr
);
521 static int usb_host_set_config(USBHostDevice
*s
, int config
)
523 usb_host_release_interfaces(s
);
525 int ret
= ioctl(s
->fd
, USBDEVFS_SETCONFIGURATION
, &config
);
527 dprintf("husb: ctrl set config %d ret %d errno %d\n", config
, ret
, errno
);
532 usb_host_claim_interfaces(s
, config
);
536 static int usb_host_set_interface(USBHostDevice
*s
, int iface
, int alt
)
538 struct usbdevfs_setinterface si
;
541 si
.interface
= iface
;
543 ret
= ioctl(s
->fd
, USBDEVFS_SETINTERFACE
, &si
);
545 dprintf("husb: ctrl set iface %d altset %d ret %d errno %d\n",
546 iface
, alt
, ret
, errno
);
551 usb_linux_update_endp_table(s
);
555 static int usb_host_handle_control(USBHostDevice
*s
, USBPacket
*p
)
557 struct usbdevfs_urb
*urb
;
559 int ret
, value
, index
;
562 * Process certain standard device requests.
563 * These are infrequent and are processed synchronously.
565 value
= le16_to_cpu(s
->ctrl
.req
.wValue
);
566 index
= le16_to_cpu(s
->ctrl
.req
.wIndex
);
568 dprintf("husb: ctrl type 0x%x req 0x%x val 0x%x index %u len %u\n",
569 s
->ctrl
.req
.bRequestType
, s
->ctrl
.req
.bRequest
, value
, index
,
572 if (s
->ctrl
.req
.bRequestType
== 0) {
573 switch (s
->ctrl
.req
.bRequest
) {
574 case USB_REQ_SET_ADDRESS
:
575 return usb_host_set_address(s
, value
);
577 case USB_REQ_SET_CONFIGURATION
:
578 return usb_host_set_config(s
, value
& 0xff);
582 if (s
->ctrl
.req
.bRequestType
== 1 &&
583 s
->ctrl
.req
.bRequest
== USB_REQ_SET_INTERFACE
)
584 return usb_host_set_interface(s
, index
, value
);
586 /* The rest are asynchronous */
588 aurb
= async_alloc();
590 dprintf("husb: async malloc failed\n");
597 * Setup ctrl transfer.
599 * s->ctrl is layed out such that data buffer immediately follows
600 * 'req' struct which is exactly what usbdevfs expects.
604 urb
->type
= USBDEVFS_URB_TYPE_CONTROL
;
605 urb
->endpoint
= p
->devep
;
607 urb
->buffer
= &s
->ctrl
.req
;
608 urb
->buffer_length
= 8 + s
->ctrl
.len
;
610 urb
->usercontext
= s
;
612 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
614 dprintf("husb: submit ctrl. len %u aurb %p\n", urb
->buffer_length
, aurb
);
617 dprintf("husb: submit failed. errno %d\n", errno
);
625 return USB_RET_STALL
;
629 usb_defer_packet(p
, async_cancel
, aurb
);
630 return USB_RET_ASYNC
;
633 static int do_token_setup(USBDevice
*dev
, USBPacket
*p
)
635 USBHostDevice
*s
= (USBHostDevice
*) dev
;
639 return USB_RET_STALL
;
641 memcpy(&s
->ctrl
.req
, p
->data
, 8);
642 s
->ctrl
.len
= le16_to_cpu(s
->ctrl
.req
.wLength
);
644 s
->ctrl
.state
= CTRL_STATE_SETUP
;
646 if (s
->ctrl
.req
.bRequestType
& USB_DIR_IN
) {
647 ret
= usb_host_handle_control(s
, p
);
651 if (ret
< s
->ctrl
.len
)
653 s
->ctrl
.state
= CTRL_STATE_DATA
;
655 if (s
->ctrl
.len
== 0)
656 s
->ctrl
.state
= CTRL_STATE_ACK
;
658 s
->ctrl
.state
= CTRL_STATE_DATA
;
664 static int do_token_in(USBDevice
*dev
, USBPacket
*p
)
666 USBHostDevice
*s
= (USBHostDevice
*) dev
;
670 return usb_host_handle_data(s
, p
);
672 switch(s
->ctrl
.state
) {
674 if (!(s
->ctrl
.req
.bRequestType
& USB_DIR_IN
)) {
675 ret
= usb_host_handle_control(s
, p
);
676 if (ret
== USB_RET_ASYNC
)
677 return USB_RET_ASYNC
;
679 s
->ctrl
.state
= CTRL_STATE_IDLE
;
680 return ret
> 0 ? 0 : ret
;
685 case CTRL_STATE_DATA
:
686 if (s
->ctrl
.req
.bRequestType
& USB_DIR_IN
) {
687 int len
= s
->ctrl
.len
- s
->ctrl
.offset
;
690 memcpy(p
->data
, s
->ctrl
.buffer
+ s
->ctrl
.offset
, len
);
691 s
->ctrl
.offset
+= len
;
692 if (s
->ctrl
.offset
>= s
->ctrl
.len
)
693 s
->ctrl
.state
= CTRL_STATE_ACK
;
697 s
->ctrl
.state
= CTRL_STATE_IDLE
;
698 return USB_RET_STALL
;
701 return USB_RET_STALL
;
705 static int do_token_out(USBDevice
*dev
, USBPacket
*p
)
707 USBHostDevice
*s
= (USBHostDevice
*) dev
;
710 return usb_host_handle_data(s
, p
);
712 switch(s
->ctrl
.state
) {
714 if (s
->ctrl
.req
.bRequestType
& USB_DIR_IN
) {
715 s
->ctrl
.state
= CTRL_STATE_IDLE
;
718 /* ignore additional output */
722 case CTRL_STATE_DATA
:
723 if (!(s
->ctrl
.req
.bRequestType
& USB_DIR_IN
)) {
724 int len
= s
->ctrl
.len
- s
->ctrl
.offset
;
727 memcpy(s
->ctrl
.buffer
+ s
->ctrl
.offset
, p
->data
, len
);
728 s
->ctrl
.offset
+= len
;
729 if (s
->ctrl
.offset
>= s
->ctrl
.len
)
730 s
->ctrl
.state
= CTRL_STATE_ACK
;
734 s
->ctrl
.state
= CTRL_STATE_IDLE
;
735 return USB_RET_STALL
;
738 return USB_RET_STALL
;
744 * Called by the HC (host controller).
746 * Returns length of the transaction or one of the USB_RET_XXX codes.
748 static int usb_host_handle_packet(USBDevice
*s
, USBPacket
*p
)
752 s
->state
= USB_STATE_ATTACHED
;
756 s
->state
= USB_STATE_NOTATTACHED
;
760 s
->remote_wakeup
= 0;
762 s
->state
= USB_STATE_DEFAULT
;
767 /* Rest of the PIDs must match our address */
768 if (s
->state
< USB_STATE_DEFAULT
|| p
->devaddr
!= s
->addr
)
769 return USB_RET_NODEV
;
772 case USB_TOKEN_SETUP
:
773 return do_token_setup(s
, p
);
776 return do_token_in(s
, p
);
779 return do_token_out(s
, p
);
782 return USB_RET_STALL
;
786 /* returns 1 on problem encountered or 0 for success */
787 static int usb_linux_update_endp_table(USBHostDevice
*s
)
789 uint8_t *descriptors
;
790 uint8_t devep
, type
, configuration
, alt_interface
;
791 struct usb_ctrltransfer ct
;
792 int interface
, ret
, length
, i
;
794 ct
.bRequestType
= USB_DIR_IN
;
795 ct
.bRequest
= USB_REQ_GET_CONFIGURATION
;
799 ct
.data
= &configuration
;
802 ret
= ioctl(s
->fd
, USBDEVFS_CONTROL
, &ct
);
804 perror("usb_linux_update_endp_table");
808 /* in address state */
809 if (configuration
== 0)
812 /* get the desired configuration, interface, and endpoint descriptors
813 * from device description */
814 descriptors
= &s
->descr
[18];
815 length
= s
->descr_len
- 18;
818 if (descriptors
[i
+ 1] != USB_DT_CONFIG
||
819 descriptors
[i
+ 5] != configuration
) {
820 dprintf("invalid descriptor data - configuration\n");
826 if (descriptors
[i
+ 1] != USB_DT_INTERFACE
||
827 (descriptors
[i
+ 1] == USB_DT_INTERFACE
&&
828 descriptors
[i
+ 4] == 0)) {
833 interface
= descriptors
[i
+ 2];
835 ct
.bRequestType
= USB_DIR_IN
| USB_RECIP_INTERFACE
;
836 ct
.bRequest
= USB_REQ_GET_INTERFACE
;
838 ct
.wIndex
= interface
;
840 ct
.data
= &alt_interface
;
843 ret
= ioctl(s
->fd
, USBDEVFS_CONTROL
, &ct
);
845 perror("usb_linux_update_endp_table");
849 /* the current interface descriptor is the active interface
850 * and has endpoints */
851 if (descriptors
[i
+ 3] != alt_interface
) {
856 /* advance to the endpoints */
857 while (i
< length
&& descriptors
[i
+1] != USB_DT_ENDPOINT
)
864 if (descriptors
[i
+ 1] != USB_DT_ENDPOINT
)
867 devep
= descriptors
[i
+ 2];
868 switch (descriptors
[i
+ 3] & 0x3) {
870 type
= USBDEVFS_URB_TYPE_CONTROL
;
873 type
= USBDEVFS_URB_TYPE_ISO
;
876 type
= USBDEVFS_URB_TYPE_BULK
;
879 type
= USBDEVFS_URB_TYPE_INTERRUPT
;
882 dprintf("usb_host: malformed endpoint type\n");
883 type
= USBDEVFS_URB_TYPE_BULK
;
885 s
->endp_table
[(devep
& 0xf) - 1].type
= type
;
886 s
->endp_table
[(devep
& 0xf) - 1].halted
= 0;
894 static USBDevice
*usb_host_device_open_addr(int bus_num
, int addr
, const char *prod_name
)
897 USBHostDevice
*dev
= NULL
;
898 struct usbdevfs_connectinfo ci
;
901 dev
= qemu_mallocz(sizeof(USBHostDevice
));
905 dev
->bus_num
= bus_num
;
908 printf("husb: open device %d.%d\n", bus_num
, addr
);
910 if (!usb_host_device_path
) {
911 perror("husb: USB Host Device Path not set");
914 snprintf(buf
, sizeof(buf
), "%s/%03d/%03d", usb_host_device_path
,
916 fd
= open(buf
, O_RDWR
| O_NONBLOCK
);
921 dprintf("husb: opened %s\n", buf
);
923 /* read the device description */
924 dev
->descr_len
= read(fd
, dev
->descr
, sizeof(dev
->descr
));
925 if (dev
->descr_len
<= 0) {
926 perror("husb: reading device data failed");
933 printf("=== begin dumping device descriptor data ===\n");
934 for (x
= 0; x
< dev
->descr_len
; x
++)
935 printf("%02x ", dev
->descr
[x
]);
936 printf("\n=== end dumping device descriptor data ===\n");
943 * Initial configuration is -1 which makes us claim first
944 * available config. We used to start with 1, which does not
945 * always work. I've seen devices where first config starts
948 if (!usb_host_claim_interfaces(dev
, -1))
951 ret
= ioctl(fd
, USBDEVFS_CONNECTINFO
, &ci
);
953 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
957 printf("husb: grabbed usb device %d.%d\n", bus_num
, addr
);
959 ret
= usb_linux_update_endp_table(dev
);
964 dev
->dev
.speed
= USB_SPEED_LOW
;
966 dev
->dev
.speed
= USB_SPEED_HIGH
;
968 dev
->dev
.handle_packet
= usb_host_handle_packet
;
969 dev
->dev
.handle_reset
= usb_host_handle_reset
;
970 dev
->dev
.handle_destroy
= usb_host_handle_destroy
;
972 if (!prod_name
|| prod_name
[0] == '\0')
973 snprintf(dev
->dev
.devname
, sizeof(dev
->dev
.devname
),
974 "host:%d.%d", bus_num
, addr
);
976 pstrcpy(dev
->dev
.devname
, sizeof(dev
->dev
.devname
),
979 /* USB devio uses 'write' flag to check for async completions */
980 qemu_set_fd_handler(dev
->fd
, NULL
, async_complete
, dev
);
984 return (USBDevice
*) dev
;
994 static int usb_host_auto_add(const char *spec
);
995 static int usb_host_auto_del(const char *spec
);
997 USBDevice
*usb_host_device_open(const char *devname
)
1000 char product_name
[PRODUCT_NAME_SZ
];
1002 if (strstr(devname
, "auto:")) {
1003 usb_host_auto_add(devname
);
1007 if (usb_host_find_device(&bus_num
, &addr
, product_name
, sizeof(product_name
),
1011 if (hostdev_find(bus_num
, addr
)) {
1012 term_printf("husb: host usb device %d.%d is already open\n", bus_num
, addr
);
1016 return usb_host_device_open_addr(bus_num
, addr
, product_name
);
1019 int usb_host_device_close(const char *devname
)
1021 char product_name
[PRODUCT_NAME_SZ
];
1025 if (strstr(devname
, "auto:"))
1026 return usb_host_auto_del(devname
);
1028 if (usb_host_find_device(&bus_num
, &addr
, product_name
, sizeof(product_name
),
1032 s
= hostdev_find(bus_num
, addr
);
1034 usb_device_del_addr(0, s
->dev
.addr
);
1041 static int get_tag_value(char *buf
, int buf_size
,
1042 const char *str
, const char *tag
,
1043 const char *stopchars
)
1047 p
= strstr(str
, tag
);
1054 while (*p
!= '\0' && !strchr(stopchars
, *p
)) {
1055 if ((q
- buf
) < (buf_size
- 1))
1064 * Use /proc/bus/usb/devices or /dev/bus/usb/devices file to determine
1065 * host's USB devices. This is legacy support since many distributions
1066 * are moving to /sys/bus/usb
1068 static int usb_host_scan_dev(void *opaque
, USBScanFunc
*func
)
1073 int bus_num
, addr
, speed
, device_count
, class_id
, product_id
, vendor_id
;
1074 char product_name
[512];
1077 if (!usb_host_device_path
) {
1078 perror("husb: USB Host Device Path not set");
1081 snprintf(line
, sizeof(line
), "%s/devices", usb_host_device_path
);
1082 f
= fopen(line
, "r");
1084 perror("husb: cannot open devices file");
1089 bus_num
= addr
= speed
= class_id
= product_id
= vendor_id
= 0;
1091 if (fgets(line
, sizeof(line
), f
) == NULL
)
1093 if (strlen(line
) > 0)
1094 line
[strlen(line
) - 1] = '\0';
1095 if (line
[0] == 'T' && line
[1] == ':') {
1096 if (device_count
&& (vendor_id
|| product_id
)) {
1097 /* New device. Add the previously discovered device. */
1098 ret
= func(opaque
, bus_num
, addr
, class_id
, vendor_id
,
1099 product_id
, product_name
, speed
);
1103 if (get_tag_value(buf
, sizeof(buf
), line
, "Bus=", " ") < 0)
1105 bus_num
= atoi(buf
);
1106 if (get_tag_value(buf
, sizeof(buf
), line
, "Dev#=", " ") < 0)
1109 if (get_tag_value(buf
, sizeof(buf
), line
, "Spd=", " ") < 0)
1111 if (!strcmp(buf
, "480"))
1112 speed
= USB_SPEED_HIGH
;
1113 else if (!strcmp(buf
, "1.5"))
1114 speed
= USB_SPEED_LOW
;
1116 speed
= USB_SPEED_FULL
;
1117 product_name
[0] = '\0';
1122 } else if (line
[0] == 'P' && line
[1] == ':') {
1123 if (get_tag_value(buf
, sizeof(buf
), line
, "Vendor=", " ") < 0)
1125 vendor_id
= strtoul(buf
, NULL
, 16);
1126 if (get_tag_value(buf
, sizeof(buf
), line
, "ProdID=", " ") < 0)
1128 product_id
= strtoul(buf
, NULL
, 16);
1129 } else if (line
[0] == 'S' && line
[1] == ':') {
1130 if (get_tag_value(buf
, sizeof(buf
), line
, "Product=", "") < 0)
1132 pstrcpy(product_name
, sizeof(product_name
), buf
);
1133 } else if (line
[0] == 'D' && line
[1] == ':') {
1134 if (get_tag_value(buf
, sizeof(buf
), line
, "Cls=", " (") < 0)
1136 class_id
= strtoul(buf
, NULL
, 16);
1140 if (device_count
&& (vendor_id
|| product_id
)) {
1141 /* Add the last device. */
1142 ret
= func(opaque
, bus_num
, addr
, class_id
, vendor_id
,
1143 product_id
, product_name
, speed
);
1152 * Read sys file-system device file
1154 * @line address of buffer to put file contents in
1155 * @line_size size of line
1156 * @device_file path to device file (printf format string)
1157 * @device_name device being opened (inserted into device_file)
1159 * @return 0 failed, 1 succeeded ('line' contains data)
1161 static int usb_host_read_file(char *line
, size_t line_size
, const char *device_file
, const char *device_name
)
1165 char filename
[PATH_MAX
];
1167 snprintf(filename
, PATH_MAX
, device_file
, device_name
);
1168 f
= fopen(filename
, "r");
1170 fgets(line
, line_size
, f
);
1174 term_printf("husb: could not open %s\n", filename
);
1181 * Use /sys/bus/usb/devices/ directory to determine host's USB
1184 * This code is based on Robert Schiele's original patches posted to
1185 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1187 static int usb_host_scan_sys(void *opaque
, USBScanFunc
*func
)
1191 int bus_num
, addr
, speed
, class_id
, product_id
, vendor_id
;
1193 char product_name
[512];
1196 dir
= opendir(USBSYSBUS_PATH
"/devices");
1198 perror("husb: cannot open devices directory");
1202 while ((de
= readdir(dir
))) {
1203 if (de
->d_name
[0] != '.' && !strchr(de
->d_name
, ':')) {
1204 char *tmpstr
= de
->d_name
;
1205 if (!strncmp(de
->d_name
, "usb", 3))
1207 bus_num
= atoi(tmpstr
);
1209 if (!usb_host_read_file(line
, sizeof(line
), USBSYSBUS_PATH
"/devices/%s/devnum", de
->d_name
))
1211 if (sscanf(line
, "%d", &addr
) != 1)
1214 if (!usb_host_read_file(line
, sizeof(line
), USBSYSBUS_PATH
"/devices/%s/bDeviceClass", de
->d_name
))
1216 if (sscanf(line
, "%x", &class_id
) != 1)
1219 if (!usb_host_read_file(line
, sizeof(line
), USBSYSBUS_PATH
"/devices/%s/idVendor", de
->d_name
))
1221 if (sscanf(line
, "%x", &vendor_id
) != 1)
1224 if (!usb_host_read_file(line
, sizeof(line
), USBSYSBUS_PATH
"/devices/%s/idProduct", de
->d_name
))
1226 if (sscanf(line
, "%x", &product_id
) != 1)
1229 if (!usb_host_read_file(line
, sizeof(line
), USBSYSBUS_PATH
"/devices/%s/product", de
->d_name
)) {
1232 if (strlen(line
) > 0)
1233 line
[strlen(line
) - 1] = '\0';
1234 pstrcpy(product_name
, sizeof(product_name
), line
);
1237 if (!usb_host_read_file(line
, sizeof(line
), USBSYSBUS_PATH
"/devices/%s/speed", de
->d_name
))
1239 if (!strcmp(line
, "480\n"))
1240 speed
= USB_SPEED_HIGH
;
1241 else if (!strcmp(line
, "1.5\n"))
1242 speed
= USB_SPEED_LOW
;
1244 speed
= USB_SPEED_FULL
;
1246 ret
= func(opaque
, bus_num
, addr
, class_id
, vendor_id
,
1247 product_id
, product_name
, speed
);
1259 * Determine how to access the host's USB devices and call the
1260 * specific support function.
1262 static int usb_host_scan(void *opaque
, USBScanFunc
*func
)
1267 const char *fs_type
[] = {"unknown", "proc", "dev", "sys"};
1268 char devpath
[PATH_MAX
];
1270 /* only check the host once */
1272 f
= fopen(USBPROCBUS_PATH
"/devices", "r");
1274 /* devices found in /proc/bus/usb/ */
1275 strcpy(devpath
, USBPROCBUS_PATH
);
1276 usb_fs_type
= USB_FS_PROC
;
1278 dprintf(USBDBG_DEVOPENED
, USBPROCBUS_PATH
);
1281 /* try additional methods if an access method hasn't been found yet */
1282 f
= fopen(USBDEVBUS_PATH
"/devices", "r");
1284 /* devices found in /dev/bus/usb/ */
1285 strcpy(devpath
, USBDEVBUS_PATH
);
1286 usb_fs_type
= USB_FS_DEV
;
1288 dprintf(USBDBG_DEVOPENED
, USBDEVBUS_PATH
);
1291 dir
= opendir(USBSYSBUS_PATH
"/devices");
1293 /* devices found in /dev/bus/usb/ (yes - not a mistake!) */
1294 strcpy(devpath
, USBDEVBUS_PATH
);
1295 usb_fs_type
= USB_FS_SYS
;
1297 dprintf(USBDBG_DEVOPENED
, USBSYSBUS_PATH
);
1302 term_printf("husb: unable to access USB devices\n");
1306 /* the module setting (used later for opening devices) */
1307 usb_host_device_path
= qemu_mallocz(strlen(devpath
)+1);
1308 if (usb_host_device_path
) {
1309 strcpy(usb_host_device_path
, devpath
);
1310 term_printf("husb: using %s file-system with %s\n", fs_type
[usb_fs_type
], usb_host_device_path
);
1312 /* out of memory? */
1313 perror("husb: unable to allocate memory for device path");
1318 switch (usb_fs_type
) {
1321 ret
= usb_host_scan_dev(opaque
, func
);
1324 ret
= usb_host_scan_sys(opaque
, func
);
1333 struct USBAutoFilter
{
1334 struct USBAutoFilter
*next
;
1341 static QEMUTimer
*usb_auto_timer
;
1342 static struct USBAutoFilter
*usb_auto_filter
;
1344 static int usb_host_auto_scan(void *opaque
, int bus_num
, int addr
,
1345 int class_id
, int vendor_id
, int product_id
,
1346 const char *product_name
, int speed
)
1348 struct USBAutoFilter
*f
;
1349 struct USBDevice
*dev
;
1355 for (f
= usb_auto_filter
; f
; f
= f
->next
) {
1356 if (f
->bus_num
>= 0 && f
->bus_num
!= bus_num
)
1359 if (f
->addr
>= 0 && f
->addr
!= addr
)
1362 if (f
->vendor_id
>= 0 && f
->vendor_id
!= vendor_id
)
1365 if (f
->product_id
>= 0 && f
->product_id
!= product_id
)
1368 /* We got a match */
1370 /* Allredy attached ? */
1371 if (hostdev_find(bus_num
, addr
))
1374 dprintf("husb: auto open: bus_num %d addr %d\n", bus_num
, addr
);
1376 dev
= usb_host_device_open_addr(bus_num
, addr
, product_name
);
1378 usb_device_add_dev(dev
);
1384 static void usb_host_auto_timer(void *unused
)
1386 usb_host_scan(NULL
, usb_host_auto_scan
);
1387 qemu_mod_timer(usb_auto_timer
, qemu_get_clock(rt_clock
) + 2000);
1391 * Autoconnect filter
1393 * auto:bus:dev[:vid:pid]
1394 * auto:bus.dev[:vid:pid]
1396 * bus - bus number (dec, * means any)
1397 * dev - device number (dec, * means any)
1398 * vid - vendor id (hex, * means any)
1399 * pid - product id (hex, * means any)
1401 * See 'lsusb' output.
1403 static int parse_filter(const char *spec
, struct USBAutoFilter
*f
)
1405 enum { BUS
, DEV
, VID
, PID
, DONE
};
1406 const char *p
= spec
;
1414 for (i
= BUS
; i
< DONE
; i
++) {
1415 p
= strpbrk(p
, ":.");
1423 case BUS
: f
->bus_num
= strtol(p
, NULL
, 10); break;
1424 case DEV
: f
->addr
= strtol(p
, NULL
, 10); break;
1425 case VID
: f
->vendor_id
= strtol(p
, NULL
, 16); break;
1426 case PID
: f
->product_id
= strtol(p
, NULL
, 16); break;
1431 fprintf(stderr
, "husb: invalid auto filter spec %s\n", spec
);
1438 static int match_filter(const struct USBAutoFilter
*f1
,
1439 const struct USBAutoFilter
*f2
)
1441 return f1
->bus_num
== f2
->bus_num
&&
1442 f1
->addr
== f2
->addr
&&
1443 f1
->vendor_id
== f2
->vendor_id
&&
1444 f1
->product_id
== f2
->product_id
;
1447 static int usb_host_auto_add(const char *spec
)
1449 struct USBAutoFilter filter
, *f
;
1451 if (parse_filter(spec
, &filter
) < 0)
1454 f
= qemu_mallocz(sizeof(*f
));
1456 fprintf(stderr
, "husb: failed to allocate auto filter\n");
1462 if (!usb_auto_filter
) {
1464 * First entry. Init and start the monitor.
1465 * Right now we're using timer to check for new devices.
1466 * If this turns out to be too expensive we can move that into a
1469 usb_auto_timer
= qemu_new_timer(rt_clock
, usb_host_auto_timer
, NULL
);
1470 if (!usb_auto_timer
) {
1471 fprintf(stderr
, "husb: failed to allocate auto scan timer\n");
1476 /* Check for new devices every two seconds */
1477 qemu_mod_timer(usb_auto_timer
, qemu_get_clock(rt_clock
) + 2000);
1480 dprintf("husb: added auto filter: bus_num %d addr %d vid %d pid %d\n",
1481 f
->bus_num
, f
->addr
, f
->vendor_id
, f
->product_id
);
1483 f
->next
= usb_auto_filter
;
1484 usb_auto_filter
= f
;
1489 static int usb_host_auto_del(const char *spec
)
1491 struct USBAutoFilter
*pf
= usb_auto_filter
;
1492 struct USBAutoFilter
**prev
= &usb_auto_filter
;
1493 struct USBAutoFilter filter
;
1495 if (parse_filter(spec
, &filter
) < 0)
1499 if (match_filter(pf
, &filter
)) {
1500 dprintf("husb: removed auto filter: bus_num %d addr %d vid %d pid %d\n",
1501 pf
->bus_num
, pf
->addr
, pf
->vendor_id
, pf
->product_id
);
1505 if (!usb_auto_filter
) {
1506 /* No more filters. Stop scanning. */
1507 qemu_del_timer(usb_auto_timer
);
1508 qemu_free_timer(usb_auto_timer
);
1521 typedef struct FindDeviceState
{
1526 char product_name
[PRODUCT_NAME_SZ
];
1529 static int usb_host_find_device_scan(void *opaque
, int bus_num
, int addr
,
1531 int vendor_id
, int product_id
,
1532 const char *product_name
, int speed
)
1534 FindDeviceState
*s
= opaque
;
1535 if ((vendor_id
== s
->vendor_id
&&
1536 product_id
== s
->product_id
) ||
1537 (bus_num
== s
->bus_num
&&
1539 pstrcpy(s
->product_name
, PRODUCT_NAME_SZ
, product_name
);
1540 s
->bus_num
= bus_num
;
1549 'bus.addr' (decimal numbers) or
1550 'vendor_id:product_id' (hexa numbers) */
1551 static int usb_host_find_device(int *pbus_num
, int *paddr
,
1552 char *product_name
, int product_name_size
,
1553 const char *devname
)
1559 p
= strchr(devname
, '.');
1561 *pbus_num
= strtoul(devname
, NULL
, 0);
1562 *paddr
= strtoul(p
+ 1, NULL
, 0);
1563 fs
.bus_num
= *pbus_num
;
1565 ret
= usb_host_scan(&fs
, usb_host_find_device_scan
);
1567 pstrcpy(product_name
, product_name_size
, fs
.product_name
);
1571 p
= strchr(devname
, ':');
1573 fs
.vendor_id
= strtoul(devname
, NULL
, 16);
1574 fs
.product_id
= strtoul(p
+ 1, NULL
, 16);
1575 ret
= usb_host_scan(&fs
, usb_host_find_device_scan
);
1577 *pbus_num
= fs
.bus_num
;
1579 pstrcpy(product_name
, product_name_size
, fs
.product_name
);
1586 /**********************/
1587 /* USB host device info */
1589 struct usb_class_info
{
1591 const char *class_name
;
1594 static const struct usb_class_info usb_class_info
[] = {
1595 { USB_CLASS_AUDIO
, "Audio"},
1596 { USB_CLASS_COMM
, "Communication"},
1597 { USB_CLASS_HID
, "HID"},
1598 { USB_CLASS_HUB
, "Hub" },
1599 { USB_CLASS_PHYSICAL
, "Physical" },
1600 { USB_CLASS_PRINTER
, "Printer" },
1601 { USB_CLASS_MASS_STORAGE
, "Storage" },
1602 { USB_CLASS_CDC_DATA
, "Data" },
1603 { USB_CLASS_APP_SPEC
, "Application Specific" },
1604 { USB_CLASS_VENDOR_SPEC
, "Vendor Specific" },
1605 { USB_CLASS_STILL_IMAGE
, "Still Image" },
1606 { USB_CLASS_CSCID
, "Smart Card" },
1607 { USB_CLASS_CONTENT_SEC
, "Content Security" },
1611 static const char *usb_class_str(uint8_t class)
1613 const struct usb_class_info
*p
;
1614 for(p
= usb_class_info
; p
->class != -1; p
++) {
1615 if (p
->class == class)
1618 return p
->class_name
;
1621 static void usb_info_device(int bus_num
, int addr
, int class_id
,
1622 int vendor_id
, int product_id
,
1623 const char *product_name
,
1626 const char *class_str
, *speed_str
;
1632 case USB_SPEED_FULL
:
1635 case USB_SPEED_HIGH
:
1643 term_printf(" Device %d.%d, speed %s Mb/s\n",
1644 bus_num
, addr
, speed_str
);
1645 class_str
= usb_class_str(class_id
);
1647 term_printf(" %s:", class_str
);
1649 term_printf(" Class %02x:", class_id
);
1650 term_printf(" USB device %04x:%04x", vendor_id
, product_id
);
1651 if (product_name
[0] != '\0')
1652 term_printf(", %s", product_name
);
1656 static int usb_host_info_device(void *opaque
, int bus_num
, int addr
,
1658 int vendor_id
, int product_id
,
1659 const char *product_name
,
1662 usb_info_device(bus_num
, addr
, class_id
, vendor_id
, product_id
,
1663 product_name
, speed
);
1667 static void dec2str(int val
, char *str
, size_t size
)
1670 snprintf(str
, size
, "*");
1672 snprintf(str
, size
, "%d", val
);
1675 static void hex2str(int val
, char *str
, size_t size
)
1678 snprintf(str
, size
, "*");
1680 snprintf(str
, size
, "%x", val
);
1683 void usb_host_info(void)
1685 struct USBAutoFilter
*f
;
1687 usb_host_scan(NULL
, usb_host_info_device
);
1689 if (usb_auto_filter
)
1690 term_printf(" Auto filters:\n");
1691 for (f
= usb_auto_filter
; f
; f
= f
->next
) {
1692 char bus
[10], addr
[10], vid
[10], pid
[10];
1693 dec2str(f
->bus_num
, bus
, sizeof(bus
));
1694 dec2str(f
->addr
, addr
, sizeof(addr
));
1695 hex2str(f
->vendor_id
, vid
, sizeof(vid
));
1696 hex2str(f
->product_id
, pid
, sizeof(pid
));
1697 term_printf(" Device %s.%s ID %s:%s\n", bus
, addr
, vid
, pid
);
1705 void usb_host_info(void)
1707 term_printf("USB host devices not supported\n");
1710 /* XXX: modify configure to compile the right host driver */
1711 USBDevice
*usb_host_device_open(const char *devname
)
1716 int usb_host_device_close(const char *devname
)