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__)
41 #include <sys/ioctl.h>
44 #include <linux/usbdevice_fs.h>
45 #include <linux/version.h>
48 /* We redefine it to avoid version problems */
49 struct usb_ctrltransfer
{
59 struct usb_ctrlrequest
{
67 typedef int USBScanFunc(void *opaque
, int bus_num
, int addr
, int class_id
,
68 int vendor_id
, int product_id
,
69 const char *product_name
, int speed
);
70 static int usb_host_find_device(int *pbus_num
, int *paddr
,
71 char *product_name
, int product_name_size
,
76 #define dprintf printf
81 #define USBDBG_DEVOPENED "husb: opened %s/devices\n"
83 #define USBPROCBUS_PATH "/proc/bus/usb"
84 #define PRODUCT_NAME_SZ 32
85 #define MAX_ENDPOINTS 16
86 #define USBDEVBUS_PATH "/dev/bus/usb"
87 #define USBSYSBUS_PATH "/sys/bus/usb"
89 static char *usb_host_device_path
;
96 static int usb_fs_type
;
98 /* endpoint association data */
112 * Control transfer state.
113 * Note that 'buffer' _must_ follow 'req' field because
114 * we need contigious buffer when we submit control URB.
120 struct usb_ctrlrequest req
;
121 uint8_t buffer
[1024];
124 typedef struct USBHostDevice
{
134 struct ctrl_struct ctrl
;
135 struct endp_data endp_table
[MAX_ENDPOINTS
];
137 /* Host side address */
141 struct USBHostDevice
*next
;
144 static int is_isoc(USBHostDevice
*s
, int ep
)
146 return s
->endp_table
[ep
- 1].type
== USBDEVFS_URB_TYPE_ISO
;
149 static int is_halted(USBHostDevice
*s
, int ep
)
151 return s
->endp_table
[ep
- 1].halted
;
154 static void clear_halt(USBHostDevice
*s
, int ep
)
156 s
->endp_table
[ep
- 1].halted
= 0;
159 static void set_halt(USBHostDevice
*s
, int ep
)
161 s
->endp_table
[ep
- 1].halted
= 1;
164 static USBHostDevice
*hostdev_list
;
166 static void hostdev_link(USBHostDevice
*dev
)
168 dev
->next
= hostdev_list
;
172 static void hostdev_unlink(USBHostDevice
*dev
)
174 USBHostDevice
*pdev
= hostdev_list
;
175 USBHostDevice
**prev
= &hostdev_list
;
188 static USBHostDevice
*hostdev_find(int bus_num
, int addr
)
190 USBHostDevice
*s
= hostdev_list
;
192 if (s
->bus_num
== bus_num
&& s
->addr
== addr
)
201 * We always allocate one isoc descriptor even for bulk transfers
202 * to simplify allocation and casts.
204 typedef struct AsyncURB
206 struct usbdevfs_urb urb
;
207 struct usbdevfs_iso_packet_desc isocpd
;
213 static AsyncURB
*async_alloc(void)
215 return (AsyncURB
*) qemu_mallocz(sizeof(AsyncURB
));
218 static void async_free(AsyncURB
*aurb
)
223 static void async_complete_ctrl(USBHostDevice
*s
, USBPacket
*p
)
225 switch(s
->ctrl
.state
) {
226 case CTRL_STATE_SETUP
:
227 if (p
->len
< s
->ctrl
.len
)
228 s
->ctrl
.len
= p
->len
;
229 s
->ctrl
.state
= CTRL_STATE_DATA
;
234 s
->ctrl
.state
= CTRL_STATE_IDLE
;
243 static void async_complete(void *opaque
)
245 USBHostDevice
*s
= opaque
;
251 int r
= ioctl(s
->fd
, USBDEVFS_REAPURBNDELAY
, &aurb
);
256 if (errno
== ENODEV
&& !s
->closing
) {
257 printf("husb: device %d.%d disconnected\n", s
->bus_num
, s
->addr
);
258 usb_device_del_addr(0, s
->dev
.addr
);
262 dprintf("husb: async. reap urb failed errno %d\n", errno
);
268 dprintf("husb: async completed. aurb %p status %d alen %d\n",
269 aurb
, aurb
->urb
.status
, aurb
->urb
.actual_length
);
272 switch (aurb
->urb
.status
) {
274 p
->len
= aurb
->urb
.actual_length
;
275 if (aurb
->urb
.type
== USBDEVFS_URB_TYPE_CONTROL
)
276 async_complete_ctrl(s
, p
);
280 set_halt(s
, p
->devep
);
283 p
->len
= USB_RET_NAK
;
287 usb_packet_complete(p
);
294 static void async_cancel(USBPacket
*unused
, void *opaque
)
296 AsyncURB
*aurb
= opaque
;
297 USBHostDevice
*s
= aurb
->hdev
;
299 dprintf("husb: async cancel. aurb %p\n", aurb
);
301 /* Mark it as dead (see async_complete above) */
304 int r
= ioctl(s
->fd
, USBDEVFS_DISCARDURB
, aurb
);
306 dprintf("husb: async. discard urb failed errno %d\n", errno
);
310 static int usb_host_claim_interfaces(USBHostDevice
*dev
, int configuration
)
312 int dev_descr_len
, config_descr_len
;
313 int interface
, nb_interfaces
, nb_configurations
;
316 if (configuration
== 0) /* address state - ignore */
319 dprintf("husb: claiming interfaces. config %d\n", configuration
);
322 dev_descr_len
= dev
->descr
[0];
323 if (dev_descr_len
> dev
->descr_len
)
325 nb_configurations
= dev
->descr
[17];
328 while (i
< dev
->descr_len
) {
329 dprintf("husb: i is %d, descr_len is %d, dl %d, dt %d\n", i
, dev
->descr_len
,
330 dev
->descr
[i
], dev
->descr
[i
+1]);
332 if (dev
->descr
[i
+1] != USB_DT_CONFIG
) {
336 config_descr_len
= dev
->descr
[i
];
338 printf("husb: config #%d need %d\n", dev
->descr
[i
+ 5], configuration
);
340 if (configuration
< 0 || configuration
== dev
->descr
[i
+ 5]) {
341 configuration
= dev
->descr
[i
+ 5];
345 i
+= config_descr_len
;
348 if (i
>= dev
->descr_len
) {
349 fprintf(stderr
, "husb: update iface failed. no matching configuration\n");
352 nb_interfaces
= dev
->descr
[i
+ 4];
354 #ifdef USBDEVFS_DISCONNECT
355 /* earlier Linux 2.4 do not support that */
357 struct usbdevfs_ioctl ctrl
;
358 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
359 ctrl
.ioctl_code
= USBDEVFS_DISCONNECT
;
360 ctrl
.ifno
= interface
;
361 ret
= ioctl(dev
->fd
, USBDEVFS_IOCTL
, &ctrl
);
362 if (ret
< 0 && errno
!= ENODATA
) {
363 perror("USBDEVFS_DISCONNECT");
370 /* XXX: only grab if all interfaces are free */
371 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
372 ret
= ioctl(dev
->fd
, USBDEVFS_CLAIMINTERFACE
, &interface
);
374 if (errno
== EBUSY
) {
375 printf("husb: update iface. device already grabbed\n");
377 perror("husb: failed to claim interface");
384 printf("husb: %d interfaces claimed for configuration %d\n",
385 nb_interfaces
, configuration
);
387 dev
->ninterfaces
= nb_interfaces
;
388 dev
->configuration
= configuration
;
392 static int usb_host_release_interfaces(USBHostDevice
*s
)
396 dprintf("husb: releasing interfaces\n");
398 for (i
= 0; i
< s
->ninterfaces
; i
++) {
399 ret
= ioctl(s
->fd
, USBDEVFS_RELEASEINTERFACE
, &i
);
401 perror("husb: failed to release interface");
409 static void usb_host_handle_reset(USBDevice
*dev
)
411 USBHostDevice
*s
= (USBHostDevice
*) dev
;
413 dprintf("husb: reset device %u.%u\n", s
->bus_num
, s
->addr
);
415 ioctl(s
->fd
, USBDEVFS_RESET
);
417 usb_host_claim_interfaces(s
, s
->configuration
);
420 static void usb_host_handle_destroy(USBDevice
*dev
)
422 USBHostDevice
*s
= (USBHostDevice
*)dev
;
426 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
438 static int usb_linux_update_endp_table(USBHostDevice
*s
);
440 static int usb_host_handle_data(USBHostDevice
*s
, USBPacket
*p
)
442 struct usbdevfs_urb
*urb
;
446 aurb
= async_alloc();
448 dprintf("husb: async malloc failed\n");
456 if (p
->pid
== USB_TOKEN_IN
)
457 urb
->endpoint
= p
->devep
| 0x80;
459 urb
->endpoint
= p
->devep
;
461 if (is_halted(s
, p
->devep
)) {
462 ret
= ioctl(s
->fd
, USBDEVFS_CLEAR_HALT
, &urb
->endpoint
);
464 dprintf("husb: failed to clear halt. ep 0x%x errno %d\n",
465 urb
->endpoint
, errno
);
468 clear_halt(s
, p
->devep
);
471 urb
->buffer
= p
->data
;
472 urb
->buffer_length
= p
->len
;
474 if (is_isoc(s
, p
->devep
)) {
475 /* Setup ISOC transfer */
476 urb
->type
= USBDEVFS_URB_TYPE_ISO
;
477 urb
->flags
= USBDEVFS_URB_ISO_ASAP
;
478 urb
->number_of_packets
= 1;
479 urb
->iso_frame_desc
[0].length
= p
->len
;
481 /* Setup bulk transfer */
482 urb
->type
= USBDEVFS_URB_TYPE_BULK
;
485 urb
->usercontext
= s
;
487 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
489 dprintf("husb: data submit. ep 0x%x len %u aurb %p\n", urb
->endpoint
, p
->len
, aurb
);
492 dprintf("husb: submit failed. errno %d\n", errno
);
500 return USB_RET_STALL
;
504 usb_defer_packet(p
, async_cancel
, aurb
);
505 return USB_RET_ASYNC
;
508 static int ctrl_error(void)
510 if (errno
== ETIMEDOUT
)
513 return USB_RET_STALL
;
516 static int usb_host_set_address(USBHostDevice
*s
, int addr
)
518 dprintf("husb: ctrl set addr %u\n", addr
);
523 static int usb_host_set_config(USBHostDevice
*s
, int config
)
525 usb_host_release_interfaces(s
);
527 int ret
= ioctl(s
->fd
, USBDEVFS_SETCONFIGURATION
, &config
);
529 dprintf("husb: ctrl set config %d ret %d errno %d\n", config
, ret
, errno
);
534 usb_host_claim_interfaces(s
, config
);
538 static int usb_host_set_interface(USBHostDevice
*s
, int iface
, int alt
)
540 struct usbdevfs_setinterface si
;
543 si
.interface
= iface
;
545 ret
= ioctl(s
->fd
, USBDEVFS_SETINTERFACE
, &si
);
547 dprintf("husb: ctrl set iface %d altset %d ret %d errno %d\n",
548 iface
, alt
, ret
, errno
);
553 usb_linux_update_endp_table(s
);
557 static int usb_host_handle_control(USBHostDevice
*s
, USBPacket
*p
)
559 struct usbdevfs_urb
*urb
;
561 int ret
, value
, index
;
564 * Process certain standard device requests.
565 * These are infrequent and are processed synchronously.
567 value
= le16_to_cpu(s
->ctrl
.req
.wValue
);
568 index
= le16_to_cpu(s
->ctrl
.req
.wIndex
);
570 dprintf("husb: ctrl type 0x%x req 0x%x val 0x%x index %u len %u\n",
571 s
->ctrl
.req
.bRequestType
, s
->ctrl
.req
.bRequest
, value
, index
,
574 if (s
->ctrl
.req
.bRequestType
== 0) {
575 switch (s
->ctrl
.req
.bRequest
) {
576 case USB_REQ_SET_ADDRESS
:
577 return usb_host_set_address(s
, value
);
579 case USB_REQ_SET_CONFIGURATION
:
580 return usb_host_set_config(s
, value
& 0xff);
584 if (s
->ctrl
.req
.bRequestType
== 1 &&
585 s
->ctrl
.req
.bRequest
== USB_REQ_SET_INTERFACE
)
586 return usb_host_set_interface(s
, index
, value
);
588 /* The rest are asynchronous */
590 aurb
= async_alloc();
592 dprintf("husb: async malloc failed\n");
599 * Setup ctrl transfer.
601 * s->ctrl is layed out such that data buffer immediately follows
602 * 'req' struct which is exactly what usbdevfs expects.
606 urb
->type
= USBDEVFS_URB_TYPE_CONTROL
;
607 urb
->endpoint
= p
->devep
;
609 urb
->buffer
= &s
->ctrl
.req
;
610 urb
->buffer_length
= 8 + s
->ctrl
.len
;
612 urb
->usercontext
= s
;
614 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
616 dprintf("husb: submit ctrl. len %u aurb %p\n", urb
->buffer_length
, aurb
);
619 dprintf("husb: submit failed. errno %d\n", errno
);
627 return USB_RET_STALL
;
631 usb_defer_packet(p
, async_cancel
, aurb
);
632 return USB_RET_ASYNC
;
635 static int do_token_setup(USBDevice
*dev
, USBPacket
*p
)
637 USBHostDevice
*s
= (USBHostDevice
*) dev
;
641 return USB_RET_STALL
;
643 memcpy(&s
->ctrl
.req
, p
->data
, 8);
644 s
->ctrl
.len
= le16_to_cpu(s
->ctrl
.req
.wLength
);
646 s
->ctrl
.state
= CTRL_STATE_SETUP
;
648 if (s
->ctrl
.req
.bRequestType
& USB_DIR_IN
) {
649 ret
= usb_host_handle_control(s
, p
);
653 if (ret
< s
->ctrl
.len
)
655 s
->ctrl
.state
= CTRL_STATE_DATA
;
657 if (s
->ctrl
.len
== 0)
658 s
->ctrl
.state
= CTRL_STATE_ACK
;
660 s
->ctrl
.state
= CTRL_STATE_DATA
;
666 static int do_token_in(USBDevice
*dev
, USBPacket
*p
)
668 USBHostDevice
*s
= (USBHostDevice
*) dev
;
672 return usb_host_handle_data(s
, p
);
674 switch(s
->ctrl
.state
) {
676 if (!(s
->ctrl
.req
.bRequestType
& USB_DIR_IN
)) {
677 ret
= usb_host_handle_control(s
, p
);
678 if (ret
== USB_RET_ASYNC
)
679 return USB_RET_ASYNC
;
681 s
->ctrl
.state
= CTRL_STATE_IDLE
;
682 return ret
> 0 ? 0 : ret
;
687 case CTRL_STATE_DATA
:
688 if (s
->ctrl
.req
.bRequestType
& USB_DIR_IN
) {
689 int len
= s
->ctrl
.len
- s
->ctrl
.offset
;
692 memcpy(p
->data
, s
->ctrl
.buffer
+ s
->ctrl
.offset
, len
);
693 s
->ctrl
.offset
+= len
;
694 if (s
->ctrl
.offset
>= s
->ctrl
.len
)
695 s
->ctrl
.state
= CTRL_STATE_ACK
;
699 s
->ctrl
.state
= CTRL_STATE_IDLE
;
700 return USB_RET_STALL
;
703 return USB_RET_STALL
;
707 static int do_token_out(USBDevice
*dev
, USBPacket
*p
)
709 USBHostDevice
*s
= (USBHostDevice
*) dev
;
712 return usb_host_handle_data(s
, p
);
714 switch(s
->ctrl
.state
) {
716 if (s
->ctrl
.req
.bRequestType
& USB_DIR_IN
) {
717 s
->ctrl
.state
= CTRL_STATE_IDLE
;
720 /* ignore additional output */
724 case CTRL_STATE_DATA
:
725 if (!(s
->ctrl
.req
.bRequestType
& USB_DIR_IN
)) {
726 int len
= s
->ctrl
.len
- s
->ctrl
.offset
;
729 memcpy(s
->ctrl
.buffer
+ s
->ctrl
.offset
, p
->data
, len
);
730 s
->ctrl
.offset
+= len
;
731 if (s
->ctrl
.offset
>= s
->ctrl
.len
)
732 s
->ctrl
.state
= CTRL_STATE_ACK
;
736 s
->ctrl
.state
= CTRL_STATE_IDLE
;
737 return USB_RET_STALL
;
740 return USB_RET_STALL
;
746 * Called by the HC (host controller).
748 * Returns length of the transaction or one of the USB_RET_XXX codes.
750 static int usb_host_handle_packet(USBDevice
*s
, USBPacket
*p
)
754 s
->state
= USB_STATE_ATTACHED
;
758 s
->state
= USB_STATE_NOTATTACHED
;
762 s
->remote_wakeup
= 0;
764 s
->state
= USB_STATE_DEFAULT
;
769 /* Rest of the PIDs must match our address */
770 if (s
->state
< USB_STATE_DEFAULT
|| p
->devaddr
!= s
->addr
)
771 return USB_RET_NODEV
;
774 case USB_TOKEN_SETUP
:
775 return do_token_setup(s
, p
);
778 return do_token_in(s
, p
);
781 return do_token_out(s
, p
);
784 return USB_RET_STALL
;
788 /* returns 1 on problem encountered or 0 for success */
789 static int usb_linux_update_endp_table(USBHostDevice
*s
)
791 uint8_t *descriptors
;
792 uint8_t devep
, type
, configuration
, alt_interface
;
793 struct usb_ctrltransfer ct
;
794 int interface
, ret
, length
, i
;
796 ct
.bRequestType
= USB_DIR_IN
;
797 ct
.bRequest
= USB_REQ_GET_CONFIGURATION
;
801 ct
.data
= &configuration
;
804 ret
= ioctl(s
->fd
, USBDEVFS_CONTROL
, &ct
);
806 perror("usb_linux_update_endp_table");
810 /* in address state */
811 if (configuration
== 0)
814 /* get the desired configuration, interface, and endpoint descriptors
815 * from device description */
816 descriptors
= &s
->descr
[18];
817 length
= s
->descr_len
- 18;
820 if (descriptors
[i
+ 1] != USB_DT_CONFIG
||
821 descriptors
[i
+ 5] != configuration
) {
822 dprintf("invalid descriptor data - configuration\n");
828 if (descriptors
[i
+ 1] != USB_DT_INTERFACE
||
829 (descriptors
[i
+ 1] == USB_DT_INTERFACE
&&
830 descriptors
[i
+ 4] == 0)) {
835 interface
= descriptors
[i
+ 2];
837 ct
.bRequestType
= USB_DIR_IN
| USB_RECIP_INTERFACE
;
838 ct
.bRequest
= USB_REQ_GET_INTERFACE
;
840 ct
.wIndex
= interface
;
842 ct
.data
= &alt_interface
;
845 ret
= ioctl(s
->fd
, USBDEVFS_CONTROL
, &ct
);
847 perror("usb_linux_update_endp_table");
851 /* the current interface descriptor is the active interface
852 * and has endpoints */
853 if (descriptors
[i
+ 3] != alt_interface
) {
858 /* advance to the endpoints */
859 while (i
< length
&& descriptors
[i
+1] != USB_DT_ENDPOINT
)
866 if (descriptors
[i
+ 1] != USB_DT_ENDPOINT
)
869 devep
= descriptors
[i
+ 2];
870 switch (descriptors
[i
+ 3] & 0x3) {
872 type
= USBDEVFS_URB_TYPE_CONTROL
;
875 type
= USBDEVFS_URB_TYPE_ISO
;
878 type
= USBDEVFS_URB_TYPE_BULK
;
881 type
= USBDEVFS_URB_TYPE_INTERRUPT
;
884 dprintf("usb_host: malformed endpoint type\n");
885 type
= USBDEVFS_URB_TYPE_BULK
;
887 s
->endp_table
[(devep
& 0xf) - 1].type
= type
;
888 s
->endp_table
[(devep
& 0xf) - 1].halted
= 0;
896 static USBDevice
*usb_host_device_open_addr(int bus_num
, int addr
, const char *prod_name
)
899 USBHostDevice
*dev
= NULL
;
900 struct usbdevfs_connectinfo ci
;
903 dev
= qemu_mallocz(sizeof(USBHostDevice
));
907 dev
->bus_num
= bus_num
;
910 printf("husb: open device %d.%d\n", bus_num
, addr
);
912 if (!usb_host_device_path
) {
913 perror("husb: USB Host Device Path not set");
916 snprintf(buf
, sizeof(buf
), "%s/%03d/%03d", usb_host_device_path
,
918 fd
= open(buf
, O_RDWR
| O_NONBLOCK
);
923 dprintf("husb: opened %s\n", buf
);
925 /* read the device description */
926 dev
->descr_len
= read(fd
, dev
->descr
, sizeof(dev
->descr
));
927 if (dev
->descr_len
<= 0) {
928 perror("husb: reading device data failed");
935 printf("=== begin dumping device descriptor data ===\n");
936 for (x
= 0; x
< dev
->descr_len
; x
++)
937 printf("%02x ", dev
->descr
[x
]);
938 printf("\n=== end dumping device descriptor data ===\n");
945 * Initial configuration is -1 which makes us claim first
946 * available config. We used to start with 1, which does not
947 * always work. I've seen devices where first config starts
950 if (!usb_host_claim_interfaces(dev
, -1))
953 ret
= ioctl(fd
, USBDEVFS_CONNECTINFO
, &ci
);
955 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
959 printf("husb: grabbed usb device %d.%d\n", bus_num
, addr
);
961 ret
= usb_linux_update_endp_table(dev
);
966 dev
->dev
.speed
= USB_SPEED_LOW
;
968 dev
->dev
.speed
= USB_SPEED_HIGH
;
970 dev
->dev
.handle_packet
= usb_host_handle_packet
;
971 dev
->dev
.handle_reset
= usb_host_handle_reset
;
972 dev
->dev
.handle_destroy
= usb_host_handle_destroy
;
974 if (!prod_name
|| prod_name
[0] == '\0')
975 snprintf(dev
->dev
.devname
, sizeof(dev
->dev
.devname
),
976 "host:%d.%d", bus_num
, addr
);
978 pstrcpy(dev
->dev
.devname
, sizeof(dev
->dev
.devname
),
981 /* USB devio uses 'write' flag to check for async completions */
982 qemu_set_fd_handler(dev
->fd
, NULL
, async_complete
, dev
);
986 return (USBDevice
*) dev
;
996 static int usb_host_auto_add(const char *spec
);
997 static int usb_host_auto_del(const char *spec
);
999 USBDevice
*usb_host_device_open(const char *devname
)
1002 char product_name
[PRODUCT_NAME_SZ
];
1004 if (strstr(devname
, "auto:")) {
1005 usb_host_auto_add(devname
);
1009 if (usb_host_find_device(&bus_num
, &addr
, product_name
, sizeof(product_name
),
1013 if (hostdev_find(bus_num
, addr
)) {
1014 term_printf("husb: host usb device %d.%d is already open\n", bus_num
, addr
);
1018 return usb_host_device_open_addr(bus_num
, addr
, product_name
);
1021 int usb_host_device_close(const char *devname
)
1023 char product_name
[PRODUCT_NAME_SZ
];
1027 if (strstr(devname
, "auto:"))
1028 return usb_host_auto_del(devname
);
1030 if (usb_host_find_device(&bus_num
, &addr
, product_name
, sizeof(product_name
),
1034 s
= hostdev_find(bus_num
, addr
);
1036 usb_device_del_addr(0, s
->dev
.addr
);
1043 static int get_tag_value(char *buf
, int buf_size
,
1044 const char *str
, const char *tag
,
1045 const char *stopchars
)
1049 p
= strstr(str
, tag
);
1056 while (*p
!= '\0' && !strchr(stopchars
, *p
)) {
1057 if ((q
- buf
) < (buf_size
- 1))
1066 * Use /proc/bus/usb/devices or /dev/bus/usb/devices file to determine
1067 * host's USB devices. This is legacy support since many distributions
1068 * are moving to /sys/bus/usb
1070 static int usb_host_scan_dev(void *opaque
, USBScanFunc
*func
)
1075 int bus_num
, addr
, speed
, device_count
, class_id
, product_id
, vendor_id
;
1076 char product_name
[512];
1079 if (!usb_host_device_path
) {
1080 perror("husb: USB Host Device Path not set");
1083 snprintf(line
, sizeof(line
), "%s/devices", usb_host_device_path
);
1084 f
= fopen(line
, "r");
1086 perror("husb: cannot open devices file");
1091 bus_num
= addr
= speed
= class_id
= product_id
= vendor_id
= 0;
1093 if (fgets(line
, sizeof(line
), f
) == NULL
)
1095 if (strlen(line
) > 0)
1096 line
[strlen(line
) - 1] = '\0';
1097 if (line
[0] == 'T' && line
[1] == ':') {
1098 if (device_count
&& (vendor_id
|| product_id
)) {
1099 /* New device. Add the previously discovered device. */
1100 ret
= func(opaque
, bus_num
, addr
, class_id
, vendor_id
,
1101 product_id
, product_name
, speed
);
1105 if (get_tag_value(buf
, sizeof(buf
), line
, "Bus=", " ") < 0)
1107 bus_num
= atoi(buf
);
1108 if (get_tag_value(buf
, sizeof(buf
), line
, "Dev#=", " ") < 0)
1111 if (get_tag_value(buf
, sizeof(buf
), line
, "Spd=", " ") < 0)
1113 if (!strcmp(buf
, "480"))
1114 speed
= USB_SPEED_HIGH
;
1115 else if (!strcmp(buf
, "1.5"))
1116 speed
= USB_SPEED_LOW
;
1118 speed
= USB_SPEED_FULL
;
1119 product_name
[0] = '\0';
1124 } else if (line
[0] == 'P' && line
[1] == ':') {
1125 if (get_tag_value(buf
, sizeof(buf
), line
, "Vendor=", " ") < 0)
1127 vendor_id
= strtoul(buf
, NULL
, 16);
1128 if (get_tag_value(buf
, sizeof(buf
), line
, "ProdID=", " ") < 0)
1130 product_id
= strtoul(buf
, NULL
, 16);
1131 } else if (line
[0] == 'S' && line
[1] == ':') {
1132 if (get_tag_value(buf
, sizeof(buf
), line
, "Product=", "") < 0)
1134 pstrcpy(product_name
, sizeof(product_name
), buf
);
1135 } else if (line
[0] == 'D' && line
[1] == ':') {
1136 if (get_tag_value(buf
, sizeof(buf
), line
, "Cls=", " (") < 0)
1138 class_id
= strtoul(buf
, NULL
, 16);
1142 if (device_count
&& (vendor_id
|| product_id
)) {
1143 /* Add the last device. */
1144 ret
= func(opaque
, bus_num
, addr
, class_id
, vendor_id
,
1145 product_id
, product_name
, speed
);
1154 * Read sys file-system device file
1156 * @line address of buffer to put file contents in
1157 * @line_size size of line
1158 * @device_file path to device file (printf format string)
1159 * @device_name device being opened (inserted into device_file)
1161 * @return 0 failed, 1 succeeded ('line' contains data)
1163 static int usb_host_read_file(char *line
, size_t line_size
, const char *device_file
, const char *device_name
)
1167 char filename
[PATH_MAX
];
1169 snprintf(filename
, PATH_MAX
, device_file
, device_name
);
1170 f
= fopen(filename
, "r");
1172 fgets(line
, line_size
, f
);
1176 term_printf("husb: could not open %s\n", filename
);
1183 * Use /sys/bus/usb/devices/ directory to determine host's USB
1186 * This code is based on Robert Schiele's original patches posted to
1187 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1189 static int usb_host_scan_sys(void *opaque
, USBScanFunc
*func
)
1193 int bus_num
, addr
, speed
, class_id
, product_id
, vendor_id
;
1195 char product_name
[512];
1198 dir
= opendir(USBSYSBUS_PATH
"/devices");
1200 perror("husb: cannot open devices directory");
1204 while ((de
= readdir(dir
))) {
1205 if (de
->d_name
[0] != '.' && !strchr(de
->d_name
, ':')) {
1206 char *tmpstr
= de
->d_name
;
1207 if (!strncmp(de
->d_name
, "usb", 3))
1209 bus_num
= atoi(tmpstr
);
1211 if (!usb_host_read_file(line
, sizeof(line
), USBSYSBUS_PATH
"/devices/%s/devnum", de
->d_name
))
1213 if (sscanf(line
, "%d", &addr
) != 1)
1216 if (!usb_host_read_file(line
, sizeof(line
), USBSYSBUS_PATH
"/devices/%s/bDeviceClass", de
->d_name
))
1218 if (sscanf(line
, "%x", &class_id
) != 1)
1221 if (!usb_host_read_file(line
, sizeof(line
), USBSYSBUS_PATH
"/devices/%s/idVendor", de
->d_name
))
1223 if (sscanf(line
, "%x", &vendor_id
) != 1)
1226 if (!usb_host_read_file(line
, sizeof(line
), USBSYSBUS_PATH
"/devices/%s/idProduct", de
->d_name
))
1228 if (sscanf(line
, "%x", &product_id
) != 1)
1231 if (!usb_host_read_file(line
, sizeof(line
), USBSYSBUS_PATH
"/devices/%s/product", de
->d_name
)) {
1234 if (strlen(line
) > 0)
1235 line
[strlen(line
) - 1] = '\0';
1236 pstrcpy(product_name
, sizeof(product_name
), line
);
1239 if (!usb_host_read_file(line
, sizeof(line
), USBSYSBUS_PATH
"/devices/%s/speed", de
->d_name
))
1241 if (!strcmp(line
, "480\n"))
1242 speed
= USB_SPEED_HIGH
;
1243 else if (!strcmp(line
, "1.5\n"))
1244 speed
= USB_SPEED_LOW
;
1246 speed
= USB_SPEED_FULL
;
1248 ret
= func(opaque
, bus_num
, addr
, class_id
, vendor_id
,
1249 product_id
, product_name
, speed
);
1261 * Determine how to access the host's USB devices and call the
1262 * specific support function.
1264 static int usb_host_scan(void *opaque
, USBScanFunc
*func
)
1269 const char *fs_type
[] = {"unknown", "proc", "dev", "sys"};
1270 char devpath
[PATH_MAX
];
1272 /* only check the host once */
1274 f
= fopen(USBPROCBUS_PATH
"/devices", "r");
1276 /* devices found in /proc/bus/usb/ */
1277 strcpy(devpath
, USBPROCBUS_PATH
);
1278 usb_fs_type
= USB_FS_PROC
;
1280 dprintf(USBDBG_DEVOPENED
, USBPROCBUS_PATH
);
1283 /* try additional methods if an access method hasn't been found yet */
1284 f
= fopen(USBDEVBUS_PATH
"/devices", "r");
1286 /* devices found in /dev/bus/usb/ */
1287 strcpy(devpath
, USBDEVBUS_PATH
);
1288 usb_fs_type
= USB_FS_DEV
;
1290 dprintf(USBDBG_DEVOPENED
, USBDEVBUS_PATH
);
1293 dir
= opendir(USBSYSBUS_PATH
"/devices");
1295 /* devices found in /dev/bus/usb/ (yes - not a mistake!) */
1296 strcpy(devpath
, USBDEVBUS_PATH
);
1297 usb_fs_type
= USB_FS_SYS
;
1299 dprintf(USBDBG_DEVOPENED
, USBSYSBUS_PATH
);
1304 term_printf("husb: unable to access USB devices\n");
1308 /* the module setting (used later for opening devices) */
1309 usb_host_device_path
= qemu_mallocz(strlen(devpath
)+1);
1310 if (usb_host_device_path
) {
1311 strcpy(usb_host_device_path
, devpath
);
1312 term_printf("husb: using %s file-system with %s\n", fs_type
[usb_fs_type
], usb_host_device_path
);
1314 /* out of memory? */
1315 perror("husb: unable to allocate memory for device path");
1320 switch (usb_fs_type
) {
1323 ret
= usb_host_scan_dev(opaque
, func
);
1326 ret
= usb_host_scan_sys(opaque
, func
);
1335 struct USBAutoFilter
{
1336 struct USBAutoFilter
*next
;
1343 static QEMUTimer
*usb_auto_timer
;
1344 static struct USBAutoFilter
*usb_auto_filter
;
1346 static int usb_host_auto_scan(void *opaque
, int bus_num
, int addr
,
1347 int class_id
, int vendor_id
, int product_id
,
1348 const char *product_name
, int speed
)
1350 struct USBAutoFilter
*f
;
1351 struct USBDevice
*dev
;
1357 for (f
= usb_auto_filter
; f
; f
= f
->next
) {
1358 if (f
->bus_num
>= 0 && f
->bus_num
!= bus_num
)
1361 if (f
->addr
>= 0 && f
->addr
!= addr
)
1364 if (f
->vendor_id
>= 0 && f
->vendor_id
!= vendor_id
)
1367 if (f
->product_id
>= 0 && f
->product_id
!= product_id
)
1370 /* We got a match */
1372 /* Allredy attached ? */
1373 if (hostdev_find(bus_num
, addr
))
1376 dprintf("husb: auto open: bus_num %d addr %d\n", bus_num
, addr
);
1378 dev
= usb_host_device_open_addr(bus_num
, addr
, product_name
);
1380 usb_device_add_dev(dev
);
1386 static void usb_host_auto_timer(void *unused
)
1388 usb_host_scan(NULL
, usb_host_auto_scan
);
1389 qemu_mod_timer(usb_auto_timer
, qemu_get_clock(rt_clock
) + 2000);
1393 * Autoconnect filter
1395 * auto:bus:dev[:vid:pid]
1396 * auto:bus.dev[:vid:pid]
1398 * bus - bus number (dec, * means any)
1399 * dev - device number (dec, * means any)
1400 * vid - vendor id (hex, * means any)
1401 * pid - product id (hex, * means any)
1403 * See 'lsusb' output.
1405 static int parse_filter(const char *spec
, struct USBAutoFilter
*f
)
1407 enum { BUS
, DEV
, VID
, PID
, DONE
};
1408 const char *p
= spec
;
1416 for (i
= BUS
; i
< DONE
; i
++) {
1417 p
= strpbrk(p
, ":.");
1425 case BUS
: f
->bus_num
= strtol(p
, NULL
, 10); break;
1426 case DEV
: f
->addr
= strtol(p
, NULL
, 10); break;
1427 case VID
: f
->vendor_id
= strtol(p
, NULL
, 16); break;
1428 case PID
: f
->product_id
= strtol(p
, NULL
, 16); break;
1433 fprintf(stderr
, "husb: invalid auto filter spec %s\n", spec
);
1440 static int match_filter(const struct USBAutoFilter
*f1
,
1441 const struct USBAutoFilter
*f2
)
1443 return f1
->bus_num
== f2
->bus_num
&&
1444 f1
->addr
== f2
->addr
&&
1445 f1
->vendor_id
== f2
->vendor_id
&&
1446 f1
->product_id
== f2
->product_id
;
1449 static int usb_host_auto_add(const char *spec
)
1451 struct USBAutoFilter filter
, *f
;
1453 if (parse_filter(spec
, &filter
) < 0)
1456 f
= qemu_mallocz(sizeof(*f
));
1458 fprintf(stderr
, "husb: failed to allocate auto filter\n");
1464 if (!usb_auto_filter
) {
1466 * First entry. Init and start the monitor.
1467 * Right now we're using timer to check for new devices.
1468 * If this turns out to be too expensive we can move that into a
1471 usb_auto_timer
= qemu_new_timer(rt_clock
, usb_host_auto_timer
, NULL
);
1472 if (!usb_auto_timer
) {
1473 fprintf(stderr
, "husb: failed to allocate auto scan timer\n");
1478 /* Check for new devices every two seconds */
1479 qemu_mod_timer(usb_auto_timer
, qemu_get_clock(rt_clock
) + 2000);
1482 dprintf("husb: added auto filter: bus_num %d addr %d vid %d pid %d\n",
1483 f
->bus_num
, f
->addr
, f
->vendor_id
, f
->product_id
);
1485 f
->next
= usb_auto_filter
;
1486 usb_auto_filter
= f
;
1491 static int usb_host_auto_del(const char *spec
)
1493 struct USBAutoFilter
*pf
= usb_auto_filter
;
1494 struct USBAutoFilter
**prev
= &usb_auto_filter
;
1495 struct USBAutoFilter filter
;
1497 if (parse_filter(spec
, &filter
) < 0)
1501 if (match_filter(pf
, &filter
)) {
1502 dprintf("husb: removed auto filter: bus_num %d addr %d vid %d pid %d\n",
1503 pf
->bus_num
, pf
->addr
, pf
->vendor_id
, pf
->product_id
);
1507 if (!usb_auto_filter
) {
1508 /* No more filters. Stop scanning. */
1509 qemu_del_timer(usb_auto_timer
);
1510 qemu_free_timer(usb_auto_timer
);
1523 typedef struct FindDeviceState
{
1528 char product_name
[PRODUCT_NAME_SZ
];
1531 static int usb_host_find_device_scan(void *opaque
, int bus_num
, int addr
,
1533 int vendor_id
, int product_id
,
1534 const char *product_name
, int speed
)
1536 FindDeviceState
*s
= opaque
;
1537 if ((vendor_id
== s
->vendor_id
&&
1538 product_id
== s
->product_id
) ||
1539 (bus_num
== s
->bus_num
&&
1541 pstrcpy(s
->product_name
, PRODUCT_NAME_SZ
, product_name
);
1542 s
->bus_num
= bus_num
;
1551 'bus.addr' (decimal numbers) or
1552 'vendor_id:product_id' (hexa numbers) */
1553 static int usb_host_find_device(int *pbus_num
, int *paddr
,
1554 char *product_name
, int product_name_size
,
1555 const char *devname
)
1561 p
= strchr(devname
, '.');
1563 *pbus_num
= strtoul(devname
, NULL
, 0);
1564 *paddr
= strtoul(p
+ 1, NULL
, 0);
1565 fs
.bus_num
= *pbus_num
;
1567 ret
= usb_host_scan(&fs
, usb_host_find_device_scan
);
1569 pstrcpy(product_name
, product_name_size
, fs
.product_name
);
1573 p
= strchr(devname
, ':');
1575 fs
.vendor_id
= strtoul(devname
, NULL
, 16);
1576 fs
.product_id
= strtoul(p
+ 1, NULL
, 16);
1577 ret
= usb_host_scan(&fs
, usb_host_find_device_scan
);
1579 *pbus_num
= fs
.bus_num
;
1581 pstrcpy(product_name
, product_name_size
, fs
.product_name
);
1588 /**********************/
1589 /* USB host device info */
1591 struct usb_class_info
{
1593 const char *class_name
;
1596 static const struct usb_class_info usb_class_info
[] = {
1597 { USB_CLASS_AUDIO
, "Audio"},
1598 { USB_CLASS_COMM
, "Communication"},
1599 { USB_CLASS_HID
, "HID"},
1600 { USB_CLASS_HUB
, "Hub" },
1601 { USB_CLASS_PHYSICAL
, "Physical" },
1602 { USB_CLASS_PRINTER
, "Printer" },
1603 { USB_CLASS_MASS_STORAGE
, "Storage" },
1604 { USB_CLASS_CDC_DATA
, "Data" },
1605 { USB_CLASS_APP_SPEC
, "Application Specific" },
1606 { USB_CLASS_VENDOR_SPEC
, "Vendor Specific" },
1607 { USB_CLASS_STILL_IMAGE
, "Still Image" },
1608 { USB_CLASS_CSCID
, "Smart Card" },
1609 { USB_CLASS_CONTENT_SEC
, "Content Security" },
1613 static const char *usb_class_str(uint8_t class)
1615 const struct usb_class_info
*p
;
1616 for(p
= usb_class_info
; p
->class != -1; p
++) {
1617 if (p
->class == class)
1620 return p
->class_name
;
1623 static void usb_info_device(int bus_num
, int addr
, int class_id
,
1624 int vendor_id
, int product_id
,
1625 const char *product_name
,
1628 const char *class_str
, *speed_str
;
1634 case USB_SPEED_FULL
:
1637 case USB_SPEED_HIGH
:
1645 term_printf(" Device %d.%d, speed %s Mb/s\n",
1646 bus_num
, addr
, speed_str
);
1647 class_str
= usb_class_str(class_id
);
1649 term_printf(" %s:", class_str
);
1651 term_printf(" Class %02x:", class_id
);
1652 term_printf(" USB device %04x:%04x", vendor_id
, product_id
);
1653 if (product_name
[0] != '\0')
1654 term_printf(", %s", product_name
);
1658 static int usb_host_info_device(void *opaque
, int bus_num
, int addr
,
1660 int vendor_id
, int product_id
,
1661 const char *product_name
,
1664 usb_info_device(bus_num
, addr
, class_id
, vendor_id
, product_id
,
1665 product_name
, speed
);
1669 static void dec2str(int val
, char *str
, size_t size
)
1672 snprintf(str
, size
, "*");
1674 snprintf(str
, size
, "%d", val
);
1677 static void hex2str(int val
, char *str
, size_t size
)
1680 snprintf(str
, size
, "*");
1682 snprintf(str
, size
, "%x", val
);
1685 void usb_host_info(void)
1687 struct USBAutoFilter
*f
;
1689 usb_host_scan(NULL
, usb_host_info_device
);
1691 if (usb_auto_filter
)
1692 term_printf(" Auto filters:\n");
1693 for (f
= usb_auto_filter
; f
; f
= f
->next
) {
1694 char bus
[10], addr
[10], vid
[10], pid
[10];
1695 dec2str(f
->bus_num
, bus
, sizeof(bus
));
1696 dec2str(f
->addr
, addr
, sizeof(addr
));
1697 hex2str(f
->vendor_id
, vid
, sizeof(vid
));
1698 hex2str(f
->product_id
, pid
, sizeof(pid
));
1699 term_printf(" Device %s.%s ID %s:%s\n", bus
, addr
, vid
, pid
);
1707 void usb_host_info(void)
1709 term_printf("USB host devices not supported\n");
1712 /* XXX: modify configure to compile the right host driver */
1713 USBDevice
*usb_host_device_open(const char *devname
)
1718 int usb_host_device_close(const char *devname
)