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 USBPROCBUS_PATH "/proc/bus/usb"
80 #define PRODUCT_NAME_SZ 32
81 #define MAX_ENDPOINTS 16
82 #define USBDEVBUS_PATH "/dev/bus/usb"
83 #define USBSYSBUS_PATH "/sys/bus/usb"
85 static char *usb_host_device_path
;
92 static int usb_fs_type
;
94 /* endpoint association data */
108 * Control transfer state.
109 * Note that 'buffer' _must_ follow 'req' field because
110 * we need contigious buffer when we submit control URB.
116 struct usb_ctrlrequest req
;
117 uint8_t buffer
[1024];
120 typedef struct USBHostDevice
{
130 struct ctrl_struct ctrl
;
131 struct endp_data endp_table
[MAX_ENDPOINTS
];
133 /* Host side address */
137 struct USBHostDevice
*next
;
140 static int is_isoc(USBHostDevice
*s
, int ep
)
142 return s
->endp_table
[ep
- 1].type
== USBDEVFS_URB_TYPE_ISO
;
145 static int is_halted(USBHostDevice
*s
, int ep
)
147 return s
->endp_table
[ep
- 1].halted
;
150 static void clear_halt(USBHostDevice
*s
, int ep
)
152 s
->endp_table
[ep
- 1].halted
= 0;
155 static void set_halt(USBHostDevice
*s
, int ep
)
157 s
->endp_table
[ep
- 1].halted
= 1;
160 static USBHostDevice
*hostdev_list
;
162 static void hostdev_link(USBHostDevice
*dev
)
164 dev
->next
= hostdev_list
;
168 static void hostdev_unlink(USBHostDevice
*dev
)
170 USBHostDevice
*pdev
= hostdev_list
;
171 USBHostDevice
**prev
= &hostdev_list
;
184 static USBHostDevice
*hostdev_find(int bus_num
, int addr
)
186 USBHostDevice
*s
= hostdev_list
;
188 if (s
->bus_num
== bus_num
&& s
->addr
== addr
)
197 * We always allocate one isoc descriptor even for bulk transfers
198 * to simplify allocation and casts.
200 typedef struct AsyncURB
202 struct usbdevfs_urb urb
;
203 struct usbdevfs_iso_packet_desc isocpd
;
209 static AsyncURB
*async_alloc(void)
211 return (AsyncURB
*) qemu_mallocz(sizeof(AsyncURB
));
214 static void async_free(AsyncURB
*aurb
)
219 static void async_complete_ctrl(USBHostDevice
*s
, USBPacket
*p
)
221 switch(s
->ctrl
.state
) {
222 case CTRL_STATE_SETUP
:
223 if (p
->len
< s
->ctrl
.len
)
224 s
->ctrl
.len
= p
->len
;
225 s
->ctrl
.state
= CTRL_STATE_DATA
;
230 s
->ctrl
.state
= CTRL_STATE_IDLE
;
239 static void async_complete(void *opaque
)
241 USBHostDevice
*s
= opaque
;
247 int r
= ioctl(s
->fd
, USBDEVFS_REAPURBNDELAY
, &aurb
);
252 if (errno
== ENODEV
&& !s
->closing
) {
253 printf("husb: device %d.%d disconnected\n", s
->bus_num
, s
->addr
);
254 usb_device_del_addr(0, s
->dev
.addr
);
258 dprintf("husb: async. reap urb failed errno %d\n", errno
);
264 dprintf("husb: async completed. aurb %p status %d alen %d\n",
265 aurb
, aurb
->urb
.status
, aurb
->urb
.actual_length
);
268 switch (aurb
->urb
.status
) {
270 p
->len
= aurb
->urb
.actual_length
;
271 if (aurb
->urb
.type
== USBDEVFS_URB_TYPE_CONTROL
)
272 async_complete_ctrl(s
, p
);
276 set_halt(s
, p
->devep
);
279 p
->len
= USB_RET_NAK
;
283 usb_packet_complete(p
);
290 static void async_cancel(USBPacket
*unused
, void *opaque
)
292 AsyncURB
*aurb
= opaque
;
293 USBHostDevice
*s
= aurb
->hdev
;
295 dprintf("husb: async cancel. aurb %p\n", aurb
);
297 /* Mark it as dead (see async_complete above) */
300 int r
= ioctl(s
->fd
, USBDEVFS_DISCARDURB
, aurb
);
302 dprintf("husb: async. discard urb failed errno %d\n", errno
);
306 static int usb_host_claim_interfaces(USBHostDevice
*dev
, int configuration
)
308 int dev_descr_len
, config_descr_len
;
309 int interface
, nb_interfaces
, nb_configurations
;
312 if (configuration
== 0) /* address state - ignore */
315 dprintf("husb: claiming interfaces. config %d\n", configuration
);
318 dev_descr_len
= dev
->descr
[0];
319 if (dev_descr_len
> dev
->descr_len
)
321 nb_configurations
= dev
->descr
[17];
324 while (i
< dev
->descr_len
) {
325 dprintf("husb: i is %d, descr_len is %d, dl %d, dt %d\n", i
, dev
->descr_len
,
326 dev
->descr
[i
], dev
->descr
[i
+1]);
328 if (dev
->descr
[i
+1] != USB_DT_CONFIG
) {
332 config_descr_len
= dev
->descr
[i
];
334 printf("husb: config #%d need %d\n", dev
->descr
[i
+ 5], configuration
);
336 if (configuration
< 0 || configuration
== dev
->descr
[i
+ 5]) {
337 configuration
= dev
->descr
[i
+ 5];
341 i
+= config_descr_len
;
344 if (i
>= dev
->descr_len
) {
345 fprintf(stderr
, "husb: update iface failed. no matching configuration\n");
348 nb_interfaces
= dev
->descr
[i
+ 4];
350 #ifdef USBDEVFS_DISCONNECT
351 /* earlier Linux 2.4 do not support that */
353 struct usbdevfs_ioctl ctrl
;
354 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
355 ctrl
.ioctl_code
= USBDEVFS_DISCONNECT
;
356 ctrl
.ifno
= interface
;
357 ret
= ioctl(dev
->fd
, USBDEVFS_IOCTL
, &ctrl
);
358 if (ret
< 0 && errno
!= ENODATA
) {
359 perror("USBDEVFS_DISCONNECT");
366 /* XXX: only grab if all interfaces are free */
367 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
368 ret
= ioctl(dev
->fd
, USBDEVFS_CLAIMINTERFACE
, &interface
);
370 if (errno
== EBUSY
) {
371 printf("husb: update iface. device already grabbed\n");
373 perror("husb: failed to claim interface");
380 printf("husb: %d interfaces claimed for configuration %d\n",
381 nb_interfaces
, configuration
);
383 dev
->ninterfaces
= nb_interfaces
;
384 dev
->configuration
= configuration
;
388 static int usb_host_release_interfaces(USBHostDevice
*s
)
392 dprintf("husb: releasing interfaces\n");
394 for (i
= 0; i
< s
->ninterfaces
; i
++) {
395 ret
= ioctl(s
->fd
, USBDEVFS_RELEASEINTERFACE
, &i
);
397 perror("husb: failed to release interface");
405 static void usb_host_handle_reset(USBDevice
*dev
)
407 USBHostDevice
*s
= (USBHostDevice
*) dev
;
409 dprintf("husb: reset device %u.%u\n", s
->bus_num
, s
->addr
);
411 ioctl(s
->fd
, USBDEVFS_RESET
);
413 usb_host_claim_interfaces(s
, s
->configuration
);
416 static void usb_host_handle_destroy(USBDevice
*dev
)
418 USBHostDevice
*s
= (USBHostDevice
*)dev
;
422 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
434 static int usb_linux_update_endp_table(USBHostDevice
*s
);
436 static int usb_host_handle_data(USBHostDevice
*s
, USBPacket
*p
)
438 struct usbdevfs_urb
*urb
;
442 aurb
= async_alloc();
444 dprintf("husb: async malloc failed\n");
452 if (p
->pid
== USB_TOKEN_IN
)
453 urb
->endpoint
= p
->devep
| 0x80;
455 urb
->endpoint
= p
->devep
;
457 if (is_halted(s
, p
->devep
)) {
458 ret
= ioctl(s
->fd
, USBDEVFS_CLEAR_HALT
, &urb
->endpoint
);
460 dprintf("husb: failed to clear halt. ep 0x%x errno %d\n",
461 urb
->endpoint
, errno
);
464 clear_halt(s
, p
->devep
);
467 urb
->buffer
= p
->data
;
468 urb
->buffer_length
= p
->len
;
470 if (is_isoc(s
, p
->devep
)) {
471 /* Setup ISOC transfer */
472 urb
->type
= USBDEVFS_URB_TYPE_ISO
;
473 urb
->flags
= USBDEVFS_URB_ISO_ASAP
;
474 urb
->number_of_packets
= 1;
475 urb
->iso_frame_desc
[0].length
= p
->len
;
477 /* Setup bulk transfer */
478 urb
->type
= USBDEVFS_URB_TYPE_BULK
;
481 urb
->usercontext
= s
;
483 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
485 dprintf("husb: data submit. ep 0x%x len %u aurb %p\n", urb
->endpoint
, p
->len
, aurb
);
488 dprintf("husb: submit failed. errno %d\n", errno
);
496 return USB_RET_STALL
;
500 usb_defer_packet(p
, async_cancel
, aurb
);
501 return USB_RET_ASYNC
;
504 static int ctrl_error(void)
506 if (errno
== ETIMEDOUT
)
509 return USB_RET_STALL
;
512 static int usb_host_set_address(USBHostDevice
*s
, int addr
)
514 dprintf("husb: ctrl set addr %u\n", addr
);
519 static int usb_host_set_config(USBHostDevice
*s
, int config
)
521 usb_host_release_interfaces(s
);
523 int ret
= ioctl(s
->fd
, USBDEVFS_SETCONFIGURATION
, &config
);
525 dprintf("husb: ctrl set config %d ret %d errno %d\n", config
, ret
, errno
);
530 usb_host_claim_interfaces(s
, config
);
534 static int usb_host_set_interface(USBHostDevice
*s
, int iface
, int alt
)
536 struct usbdevfs_setinterface si
;
539 si
.interface
= iface
;
541 ret
= ioctl(s
->fd
, USBDEVFS_SETINTERFACE
, &si
);
543 dprintf("husb: ctrl set iface %d altset %d ret %d errno %d\n",
544 iface
, alt
, ret
, errno
);
549 usb_linux_update_endp_table(s
);
553 static int usb_host_handle_control(USBHostDevice
*s
, USBPacket
*p
)
555 struct usbdevfs_urb
*urb
;
557 int ret
, value
, index
;
560 * Process certain standard device requests.
561 * These are infrequent and are processed synchronously.
563 value
= le16_to_cpu(s
->ctrl
.req
.wValue
);
564 index
= le16_to_cpu(s
->ctrl
.req
.wIndex
);
566 dprintf("husb: ctrl type 0x%x req 0x%x val 0x%x index %u len %u\n",
567 s
->ctrl
.req
.bRequestType
, s
->ctrl
.req
.bRequest
, value
, index
,
570 if (s
->ctrl
.req
.bRequestType
== 0) {
571 switch (s
->ctrl
.req
.bRequest
) {
572 case USB_REQ_SET_ADDRESS
:
573 return usb_host_set_address(s
, value
);
575 case USB_REQ_SET_CONFIGURATION
:
576 return usb_host_set_config(s
, value
& 0xff);
580 if (s
->ctrl
.req
.bRequestType
== 1 &&
581 s
->ctrl
.req
.bRequest
== USB_REQ_SET_INTERFACE
)
582 return usb_host_set_interface(s
, index
, value
);
584 /* The rest are asynchronous */
586 aurb
= async_alloc();
588 dprintf("husb: async malloc failed\n");
595 * Setup ctrl transfer.
597 * s->ctrl is layed out such that data buffer immediately follows
598 * 'req' struct which is exactly what usbdevfs expects.
602 urb
->type
= USBDEVFS_URB_TYPE_CONTROL
;
603 urb
->endpoint
= p
->devep
;
605 urb
->buffer
= &s
->ctrl
.req
;
606 urb
->buffer_length
= 8 + s
->ctrl
.len
;
608 urb
->usercontext
= s
;
610 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
612 dprintf("husb: submit ctrl. len %u aurb %p\n", urb
->buffer_length
, aurb
);
615 dprintf("husb: submit failed. errno %d\n", errno
);
623 return USB_RET_STALL
;
627 usb_defer_packet(p
, async_cancel
, aurb
);
628 return USB_RET_ASYNC
;
631 static int do_token_setup(USBDevice
*dev
, USBPacket
*p
)
633 USBHostDevice
*s
= (USBHostDevice
*) dev
;
637 return USB_RET_STALL
;
639 memcpy(&s
->ctrl
.req
, p
->data
, 8);
640 s
->ctrl
.len
= le16_to_cpu(s
->ctrl
.req
.wLength
);
642 s
->ctrl
.state
= CTRL_STATE_SETUP
;
644 if (s
->ctrl
.req
.bRequestType
& USB_DIR_IN
) {
645 ret
= usb_host_handle_control(s
, p
);
649 if (ret
< s
->ctrl
.len
)
651 s
->ctrl
.state
= CTRL_STATE_DATA
;
653 if (s
->ctrl
.len
== 0)
654 s
->ctrl
.state
= CTRL_STATE_ACK
;
656 s
->ctrl
.state
= CTRL_STATE_DATA
;
662 static int do_token_in(USBDevice
*dev
, USBPacket
*p
)
664 USBHostDevice
*s
= (USBHostDevice
*) dev
;
668 return usb_host_handle_data(s
, p
);
670 switch(s
->ctrl
.state
) {
672 if (!(s
->ctrl
.req
.bRequestType
& USB_DIR_IN
)) {
673 ret
= usb_host_handle_control(s
, p
);
674 if (ret
== USB_RET_ASYNC
)
675 return USB_RET_ASYNC
;
677 s
->ctrl
.state
= CTRL_STATE_IDLE
;
678 return ret
> 0 ? 0 : ret
;
683 case CTRL_STATE_DATA
:
684 if (s
->ctrl
.req
.bRequestType
& USB_DIR_IN
) {
685 int len
= s
->ctrl
.len
- s
->ctrl
.offset
;
688 memcpy(p
->data
, s
->ctrl
.buffer
+ s
->ctrl
.offset
, len
);
689 s
->ctrl
.offset
+= len
;
690 if (s
->ctrl
.offset
>= s
->ctrl
.len
)
691 s
->ctrl
.state
= CTRL_STATE_ACK
;
695 s
->ctrl
.state
= CTRL_STATE_IDLE
;
696 return USB_RET_STALL
;
699 return USB_RET_STALL
;
703 static int do_token_out(USBDevice
*dev
, USBPacket
*p
)
705 USBHostDevice
*s
= (USBHostDevice
*) dev
;
708 return usb_host_handle_data(s
, p
);
710 switch(s
->ctrl
.state
) {
712 if (s
->ctrl
.req
.bRequestType
& USB_DIR_IN
) {
713 s
->ctrl
.state
= CTRL_STATE_IDLE
;
716 /* ignore additional output */
720 case CTRL_STATE_DATA
:
721 if (!(s
->ctrl
.req
.bRequestType
& USB_DIR_IN
)) {
722 int len
= s
->ctrl
.len
- s
->ctrl
.offset
;
725 memcpy(s
->ctrl
.buffer
+ s
->ctrl
.offset
, p
->data
, len
);
726 s
->ctrl
.offset
+= len
;
727 if (s
->ctrl
.offset
>= s
->ctrl
.len
)
728 s
->ctrl
.state
= CTRL_STATE_ACK
;
732 s
->ctrl
.state
= CTRL_STATE_IDLE
;
733 return USB_RET_STALL
;
736 return USB_RET_STALL
;
742 * Called by the HC (host controller).
744 * Returns length of the transaction or one of the USB_RET_XXX codes.
746 static int usb_host_handle_packet(USBDevice
*s
, USBPacket
*p
)
750 s
->state
= USB_STATE_ATTACHED
;
754 s
->state
= USB_STATE_NOTATTACHED
;
758 s
->remote_wakeup
= 0;
760 s
->state
= USB_STATE_DEFAULT
;
765 /* Rest of the PIDs must match our address */
766 if (s
->state
< USB_STATE_DEFAULT
|| p
->devaddr
!= s
->addr
)
767 return USB_RET_NODEV
;
770 case USB_TOKEN_SETUP
:
771 return do_token_setup(s
, p
);
774 return do_token_in(s
, p
);
777 return do_token_out(s
, p
);
780 return USB_RET_STALL
;
784 /* returns 1 on problem encountered or 0 for success */
785 static int usb_linux_update_endp_table(USBHostDevice
*s
)
787 uint8_t *descriptors
;
788 uint8_t devep
, type
, configuration
, alt_interface
;
789 struct usbdevfs_ctrltransfer ct
;
790 int interface
, ret
, length
, i
;
792 ct
.bRequestType
= USB_DIR_IN
;
793 ct
.bRequest
= USB_REQ_GET_CONFIGURATION
;
797 ct
.data
= &configuration
;
800 ret
= ioctl(s
->fd
, USBDEVFS_CONTROL
, &ct
);
802 perror("usb_linux_update_endp_table");
806 /* in address state */
807 if (configuration
== 0)
810 /* get the desired configuration, interface, and endpoint descriptors
811 * from device description */
812 descriptors
= &s
->descr
[18];
813 length
= s
->descr_len
- 18;
816 if (descriptors
[i
+ 1] != USB_DT_CONFIG
||
817 descriptors
[i
+ 5] != configuration
) {
818 dprintf("invalid descriptor data - configuration\n");
824 if (descriptors
[i
+ 1] != USB_DT_INTERFACE
||
825 (descriptors
[i
+ 1] == USB_DT_INTERFACE
&&
826 descriptors
[i
+ 4] == 0)) {
831 interface
= descriptors
[i
+ 2];
833 ct
.bRequestType
= USB_DIR_IN
| USB_RECIP_INTERFACE
;
834 ct
.bRequest
= USB_REQ_GET_INTERFACE
;
836 ct
.wIndex
= interface
;
838 ct
.data
= &alt_interface
;
841 ret
= ioctl(s
->fd
, USBDEVFS_CONTROL
, &ct
);
843 perror("usb_linux_update_endp_table");
847 /* the current interface descriptor is the active interface
848 * and has endpoints */
849 if (descriptors
[i
+ 3] != alt_interface
) {
854 /* advance to the endpoints */
855 while (i
< length
&& descriptors
[i
+1] != USB_DT_ENDPOINT
)
862 if (descriptors
[i
+ 1] != USB_DT_ENDPOINT
)
865 devep
= descriptors
[i
+ 2];
866 switch (descriptors
[i
+ 3] & 0x3) {
868 type
= USBDEVFS_URB_TYPE_CONTROL
;
871 type
= USBDEVFS_URB_TYPE_ISO
;
874 type
= USBDEVFS_URB_TYPE_BULK
;
877 type
= USBDEVFS_URB_TYPE_INTERRUPT
;
880 dprintf("usb_host: malformed endpoint type\n");
881 type
= USBDEVFS_URB_TYPE_BULK
;
883 s
->endp_table
[(devep
& 0xf) - 1].type
= type
;
884 s
->endp_table
[(devep
& 0xf) - 1].halted
= 0;
892 static USBDevice
*usb_host_device_open_addr(int bus_num
, int addr
, const char *prod_name
)
895 USBHostDevice
*dev
= NULL
;
896 struct usbdevfs_connectinfo ci
;
899 dev
= qemu_mallocz(sizeof(USBHostDevice
));
903 dev
->bus_num
= bus_num
;
906 printf("husb: open device %d.%d\n", bus_num
, addr
);
908 if (!usb_host_device_path
) {
909 perror("husb: USB Host Device Path not set");
912 snprintf(buf
, sizeof(buf
), "%s/%03d/%03d", usb_host_device_path
,
914 fd
= open(buf
, O_RDWR
| O_NONBLOCK
);
919 dprintf("husb: opened %s\n", buf
);
921 /* read the device description */
922 dev
->descr_len
= read(fd
, dev
->descr
, sizeof(dev
->descr
));
923 if (dev
->descr_len
<= 0) {
924 perror("husb: reading device data failed");
931 printf("=== begin dumping device descriptor data ===\n");
932 for (x
= 0; x
< dev
->descr_len
; x
++)
933 printf("%02x ", dev
->descr
[x
]);
934 printf("\n=== end dumping device descriptor data ===\n");
941 * Initial configuration is -1 which makes us claim first
942 * available config. We used to start with 1, which does not
943 * always work. I've seen devices where first config starts
946 if (!usb_host_claim_interfaces(dev
, -1))
949 ret
= ioctl(fd
, USBDEVFS_CONNECTINFO
, &ci
);
951 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
955 printf("husb: grabbed usb device %d.%d\n", bus_num
, addr
);
957 ret
= usb_linux_update_endp_table(dev
);
962 dev
->dev
.speed
= USB_SPEED_LOW
;
964 dev
->dev
.speed
= USB_SPEED_HIGH
;
966 dev
->dev
.handle_packet
= usb_host_handle_packet
;
967 dev
->dev
.handle_reset
= usb_host_handle_reset
;
968 dev
->dev
.handle_destroy
= usb_host_handle_destroy
;
970 if (!prod_name
|| prod_name
[0] == '\0')
971 snprintf(dev
->dev
.devname
, sizeof(dev
->dev
.devname
),
972 "host:%d.%d", bus_num
, addr
);
974 pstrcpy(dev
->dev
.devname
, sizeof(dev
->dev
.devname
),
977 /* USB devio uses 'write' flag to check for async completions */
978 qemu_set_fd_handler(dev
->fd
, NULL
, async_complete
, dev
);
982 return (USBDevice
*) dev
;
992 static int usb_host_auto_add(const char *spec
);
993 static int usb_host_auto_del(const char *spec
);
995 USBDevice
*usb_host_device_open(const char *devname
)
998 char product_name
[PRODUCT_NAME_SZ
];
1000 if (strstr(devname
, "auto:")) {
1001 usb_host_auto_add(devname
);
1005 if (usb_host_find_device(&bus_num
, &addr
, product_name
, sizeof(product_name
),
1009 if (hostdev_find(bus_num
, addr
)) {
1010 term_printf("husb: host usb device %d.%d is already open\n", bus_num
, addr
);
1014 return usb_host_device_open_addr(bus_num
, addr
, product_name
);
1017 int usb_host_device_close(const char *devname
)
1019 char product_name
[PRODUCT_NAME_SZ
];
1023 if (strstr(devname
, "auto:"))
1024 return usb_host_auto_del(devname
);
1026 if (usb_host_find_device(&bus_num
, &addr
, product_name
, sizeof(product_name
),
1030 s
= hostdev_find(bus_num
, addr
);
1032 usb_device_del_addr(0, s
->dev
.addr
);
1039 static int get_tag_value(char *buf
, int buf_size
,
1040 const char *str
, const char *tag
,
1041 const char *stopchars
)
1045 p
= strstr(str
, tag
);
1052 while (*p
!= '\0' && !strchr(stopchars
, *p
)) {
1053 if ((q
- buf
) < (buf_size
- 1))
1062 * Use /proc/bus/usb/devices or /dev/bus/usb/devices file to determine
1063 * host's USB devices. This is legacy support since many distributions
1064 * are moving to /sys/bus/usb
1066 static int usb_host_scan_dev(void *opaque
, USBScanFunc
*func
)
1071 int bus_num
, addr
, speed
, device_count
, class_id
, product_id
, vendor_id
;
1072 char product_name
[512];
1075 if (!usb_host_device_path
) {
1076 perror("husb: USB Host Device Path not set");
1079 snprintf(line
, sizeof(line
), "%s/devices", usb_host_device_path
);
1080 f
= fopen(line
, "r");
1082 perror("husb: cannot open devices file");
1087 bus_num
= addr
= speed
= class_id
= product_id
= vendor_id
= 0;
1089 if (fgets(line
, sizeof(line
), f
) == NULL
)
1091 if (strlen(line
) > 0)
1092 line
[strlen(line
) - 1] = '\0';
1093 if (line
[0] == 'T' && line
[1] == ':') {
1094 if (device_count
&& (vendor_id
|| product_id
)) {
1095 /* New device. Add the previously discovered device. */
1096 ret
= func(opaque
, bus_num
, addr
, class_id
, vendor_id
,
1097 product_id
, product_name
, speed
);
1101 if (get_tag_value(buf
, sizeof(buf
), line
, "Bus=", " ") < 0)
1103 bus_num
= atoi(buf
);
1104 if (get_tag_value(buf
, sizeof(buf
), line
, "Dev#=", " ") < 0)
1107 if (get_tag_value(buf
, sizeof(buf
), line
, "Spd=", " ") < 0)
1109 if (!strcmp(buf
, "480"))
1110 speed
= USB_SPEED_HIGH
;
1111 else if (!strcmp(buf
, "1.5"))
1112 speed
= USB_SPEED_LOW
;
1114 speed
= USB_SPEED_FULL
;
1115 product_name
[0] = '\0';
1120 } else if (line
[0] == 'P' && line
[1] == ':') {
1121 if (get_tag_value(buf
, sizeof(buf
), line
, "Vendor=", " ") < 0)
1123 vendor_id
= strtoul(buf
, NULL
, 16);
1124 if (get_tag_value(buf
, sizeof(buf
), line
, "ProdID=", " ") < 0)
1126 product_id
= strtoul(buf
, NULL
, 16);
1127 } else if (line
[0] == 'S' && line
[1] == ':') {
1128 if (get_tag_value(buf
, sizeof(buf
), line
, "Product=", "") < 0)
1130 pstrcpy(product_name
, sizeof(product_name
), buf
);
1131 } else if (line
[0] == 'D' && line
[1] == ':') {
1132 if (get_tag_value(buf
, sizeof(buf
), line
, "Cls=", " (") < 0)
1134 class_id
= strtoul(buf
, NULL
, 16);
1138 if (device_count
&& (vendor_id
|| product_id
)) {
1139 /* Add the last device. */
1140 ret
= func(opaque
, bus_num
, addr
, class_id
, vendor_id
,
1141 product_id
, product_name
, speed
);
1150 * Read sys file-system device file
1152 * @line address of buffer to put file contents in
1153 * @line_size size of line
1154 * @device_file path to device file (printf format string)
1155 * @device_name device being opened (inserted into device_file)
1157 * @return 0 failed, 1 succeeded ('line' contains data)
1159 static int usb_host_read_file(char *line
, size_t line_size
, const char *device_file
, const char *device_name
)
1163 char filename
[PATH_MAX
];
1165 snprintf(filename
, PATH_MAX
, device_file
, device_name
);
1166 f
= fopen(filename
, "r");
1168 fgets(line
, line_size
, f
);
1172 term_printf("husb: could not open %s\n", filename
);
1179 * Use /sys/bus/usb/devices/ directory to determine host's USB
1182 * This code is based on Robert Schiele's original patches posted to
1183 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1185 static int usb_host_scan_sys(void *opaque
, USBScanFunc
*func
)
1189 int bus_num
, addr
, speed
, class_id
, product_id
, vendor_id
;
1191 char product_name
[512];
1194 dir
= opendir(USBSYSBUS_PATH
"/devices");
1196 perror("husb: cannot open devices directory");
1200 while ((de
= readdir(dir
))) {
1201 if (de
->d_name
[0] != '.' && !strchr(de
->d_name
, ':')) {
1202 char *tmpstr
= de
->d_name
;
1203 if (!strncmp(de
->d_name
, "usb", 3))
1205 bus_num
= atoi(tmpstr
);
1207 if (!usb_host_read_file(line
, sizeof(line
), USBSYSBUS_PATH
"/devices/%s/devnum", de
->d_name
))
1209 if (sscanf(line
, "%d", &addr
) != 1)
1212 if (!usb_host_read_file(line
, sizeof(line
), USBSYSBUS_PATH
"/devices/%s/bDeviceClass", de
->d_name
))
1214 if (sscanf(line
, "%x", &class_id
) != 1)
1217 if (!usb_host_read_file(line
, sizeof(line
), USBSYSBUS_PATH
"/devices/%s/idVendor", de
->d_name
))
1219 if (sscanf(line
, "%x", &vendor_id
) != 1)
1222 if (!usb_host_read_file(line
, sizeof(line
), USBSYSBUS_PATH
"/devices/%s/idProduct", de
->d_name
))
1224 if (sscanf(line
, "%x", &product_id
) != 1)
1227 if (!usb_host_read_file(line
, sizeof(line
), USBSYSBUS_PATH
"/devices/%s/product", de
->d_name
)) {
1230 if (strlen(line
) > 0)
1231 line
[strlen(line
) - 1] = '\0';
1232 pstrcpy(product_name
, sizeof(product_name
), line
);
1235 if (!usb_host_read_file(line
, sizeof(line
), USBSYSBUS_PATH
"/devices/%s/speed", de
->d_name
))
1237 if (!strcmp(line
, "480\n"))
1238 speed
= USB_SPEED_HIGH
;
1239 else if (!strcmp(line
, "1.5\n"))
1240 speed
= USB_SPEED_LOW
;
1242 speed
= USB_SPEED_FULL
;
1244 ret
= func(opaque
, bus_num
, addr
, class_id
, vendor_id
,
1245 product_id
, product_name
, speed
);
1257 * Determine how to access the host's USB devices and call the
1258 * specific support function.
1260 static int usb_host_scan(void *opaque
, USBScanFunc
*func
)
1265 const char *devices
= "/devices";
1266 const char *opened
= "husb: opened %s%s\n";
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(opened
, USBPROCBUS_PATH
, devices
);
1280 /* try additional methods if an access method hasn't been found yet */
1281 f
= fopen(USBDEVBUS_PATH
"/devices", "r");
1282 if (!usb_fs_type
&& f
) {
1283 /* devices found in /dev/bus/usb/ */
1284 strcpy(devpath
, USBDEVBUS_PATH
);
1285 usb_fs_type
= USB_FS_DEV
;
1287 dprintf(opened
, USBDEVBUS_PATH
, devices
);
1289 dir
= opendir(USBSYSBUS_PATH
"/devices");
1290 if (!usb_fs_type
&& dir
) {
1291 /* devices found in /dev/bus/usb/ (yes - not a mistake!) */
1292 strcpy(devpath
, USBDEVBUS_PATH
);
1293 usb_fs_type
= USB_FS_SYS
;
1295 dprintf(opened
, USBSYSBUS_PATH
, devices
);
1297 term_printf("husb: unable to access USB devices\n");
1301 /* the module setting (used later for opening devices) */
1302 usb_host_device_path
= qemu_mallocz(strlen(devpath
)+1);
1303 if (usb_host_device_path
) {
1304 strcpy(usb_host_device_path
, devpath
);
1305 term_printf("husb: using %s file-system with %s\n", fs_type
[usb_fs_type
], usb_host_device_path
);
1307 /* out of memory? */
1308 perror("husb: unable to allocate memory for device path");
1313 switch (usb_fs_type
) {
1316 ret
= usb_host_scan_dev(opaque
, func
);
1319 ret
= usb_host_scan_sys(opaque
, func
);
1326 struct USBAutoFilter
{
1327 struct USBAutoFilter
*next
;
1334 static QEMUTimer
*usb_auto_timer
;
1335 static struct USBAutoFilter
*usb_auto_filter
;
1337 static int usb_host_auto_scan(void *opaque
, int bus_num
, int addr
,
1338 int class_id
, int vendor_id
, int product_id
,
1339 const char *product_name
, int speed
)
1341 struct USBAutoFilter
*f
;
1342 struct USBDevice
*dev
;
1348 for (f
= usb_auto_filter
; f
; f
= f
->next
) {
1349 if (f
->bus_num
>= 0 && f
->bus_num
!= bus_num
)
1352 if (f
->addr
>= 0 && f
->addr
!= addr
)
1355 if (f
->vendor_id
>= 0 && f
->vendor_id
!= vendor_id
)
1358 if (f
->product_id
>= 0 && f
->product_id
!= product_id
)
1361 /* We got a match */
1363 /* Allredy attached ? */
1364 if (hostdev_find(bus_num
, addr
))
1367 dprintf("husb: auto open: bus_num %d addr %d\n", bus_num
, addr
);
1369 dev
= usb_host_device_open_addr(bus_num
, addr
, product_name
);
1371 usb_device_add_dev(dev
);
1377 static void usb_host_auto_timer(void *unused
)
1379 usb_host_scan(NULL
, usb_host_auto_scan
);
1380 qemu_mod_timer(usb_auto_timer
, qemu_get_clock(rt_clock
) + 2000);
1384 * Autoconnect filter
1386 * auto:bus:dev[:vid:pid]
1387 * auto:bus.dev[:vid:pid]
1389 * bus - bus number (dec, * means any)
1390 * dev - device number (dec, * means any)
1391 * vid - vendor id (hex, * means any)
1392 * pid - product id (hex, * means any)
1394 * See 'lsusb' output.
1396 static int parse_filter(const char *spec
, struct USBAutoFilter
*f
)
1398 enum { BUS
, DEV
, VID
, PID
, DONE
};
1399 const char *p
= spec
;
1407 for (i
= BUS
; i
< DONE
; i
++) {
1408 p
= strpbrk(p
, ":.");
1416 case BUS
: f
->bus_num
= strtol(p
, NULL
, 10); break;
1417 case DEV
: f
->addr
= strtol(p
, NULL
, 10); break;
1418 case VID
: f
->vendor_id
= strtol(p
, NULL
, 16); break;
1419 case PID
: f
->product_id
= strtol(p
, NULL
, 16); break;
1424 fprintf(stderr
, "husb: invalid auto filter spec %s\n", spec
);
1431 static int match_filter(const struct USBAutoFilter
*f1
,
1432 const struct USBAutoFilter
*f2
)
1434 return f1
->bus_num
== f2
->bus_num
&&
1435 f1
->addr
== f2
->addr
&&
1436 f1
->vendor_id
== f2
->vendor_id
&&
1437 f1
->product_id
== f2
->product_id
;
1440 static int usb_host_auto_add(const char *spec
)
1442 struct USBAutoFilter filter
, *f
;
1444 if (parse_filter(spec
, &filter
) < 0)
1447 f
= qemu_mallocz(sizeof(*f
));
1449 fprintf(stderr
, "husb: failed to allocate auto filter\n");
1455 if (!usb_auto_filter
) {
1457 * First entry. Init and start the monitor.
1458 * Right now we're using timer to check for new devices.
1459 * If this turns out to be too expensive we can move that into a
1462 usb_auto_timer
= qemu_new_timer(rt_clock
, usb_host_auto_timer
, NULL
);
1463 if (!usb_auto_timer
) {
1464 fprintf(stderr
, "husb: failed to allocate auto scan timer\n");
1469 /* Check for new devices every two seconds */
1470 qemu_mod_timer(usb_auto_timer
, qemu_get_clock(rt_clock
) + 2000);
1473 dprintf("husb: added auto filter: bus_num %d addr %d vid %d pid %d\n",
1474 f
->bus_num
, f
->addr
, f
->vendor_id
, f
->product_id
);
1476 f
->next
= usb_auto_filter
;
1477 usb_auto_filter
= f
;
1482 static int usb_host_auto_del(const char *spec
)
1484 struct USBAutoFilter
*pf
= usb_auto_filter
;
1485 struct USBAutoFilter
**prev
= &usb_auto_filter
;
1486 struct USBAutoFilter filter
;
1488 if (parse_filter(spec
, &filter
) < 0)
1492 if (match_filter(pf
, &filter
)) {
1493 dprintf("husb: removed auto filter: bus_num %d addr %d vid %d pid %d\n",
1494 pf
->bus_num
, pf
->addr
, pf
->vendor_id
, pf
->product_id
);
1498 if (!usb_auto_filter
) {
1499 /* No more filters. Stop scanning. */
1500 qemu_del_timer(usb_auto_timer
);
1501 qemu_free_timer(usb_auto_timer
);
1514 typedef struct FindDeviceState
{
1519 char product_name
[PRODUCT_NAME_SZ
];
1522 static int usb_host_find_device_scan(void *opaque
, int bus_num
, int addr
,
1524 int vendor_id
, int product_id
,
1525 const char *product_name
, int speed
)
1527 FindDeviceState
*s
= opaque
;
1528 if ((vendor_id
== s
->vendor_id
&&
1529 product_id
== s
->product_id
) ||
1530 (bus_num
== s
->bus_num
&&
1532 pstrcpy(s
->product_name
, PRODUCT_NAME_SZ
, product_name
);
1533 s
->bus_num
= bus_num
;
1542 'bus.addr' (decimal numbers) or
1543 'vendor_id:product_id' (hexa numbers) */
1544 static int usb_host_find_device(int *pbus_num
, int *paddr
,
1545 char *product_name
, int product_name_size
,
1546 const char *devname
)
1552 p
= strchr(devname
, '.');
1554 *pbus_num
= strtoul(devname
, NULL
, 0);
1555 *paddr
= strtoul(p
+ 1, NULL
, 0);
1556 fs
.bus_num
= *pbus_num
;
1558 ret
= usb_host_scan(&fs
, usb_host_find_device_scan
);
1560 pstrcpy(product_name
, product_name_size
, fs
.product_name
);
1564 p
= strchr(devname
, ':');
1566 fs
.vendor_id
= strtoul(devname
, NULL
, 16);
1567 fs
.product_id
= strtoul(p
+ 1, NULL
, 16);
1568 ret
= usb_host_scan(&fs
, usb_host_find_device_scan
);
1570 *pbus_num
= fs
.bus_num
;
1572 pstrcpy(product_name
, product_name_size
, fs
.product_name
);
1579 /**********************/
1580 /* USB host device info */
1582 struct usb_class_info
{
1584 const char *class_name
;
1587 static const struct usb_class_info usb_class_info
[] = {
1588 { USB_CLASS_AUDIO
, "Audio"},
1589 { USB_CLASS_COMM
, "Communication"},
1590 { USB_CLASS_HID
, "HID"},
1591 { USB_CLASS_HUB
, "Hub" },
1592 { USB_CLASS_PHYSICAL
, "Physical" },
1593 { USB_CLASS_PRINTER
, "Printer" },
1594 { USB_CLASS_MASS_STORAGE
, "Storage" },
1595 { USB_CLASS_CDC_DATA
, "Data" },
1596 { USB_CLASS_APP_SPEC
, "Application Specific" },
1597 { USB_CLASS_VENDOR_SPEC
, "Vendor Specific" },
1598 { USB_CLASS_STILL_IMAGE
, "Still Image" },
1599 { USB_CLASS_CSCID
, "Smart Card" },
1600 { USB_CLASS_CONTENT_SEC
, "Content Security" },
1604 static const char *usb_class_str(uint8_t class)
1606 const struct usb_class_info
*p
;
1607 for(p
= usb_class_info
; p
->class != -1; p
++) {
1608 if (p
->class == class)
1611 return p
->class_name
;
1614 static void usb_info_device(int bus_num
, int addr
, int class_id
,
1615 int vendor_id
, int product_id
,
1616 const char *product_name
,
1619 const char *class_str
, *speed_str
;
1625 case USB_SPEED_FULL
:
1628 case USB_SPEED_HIGH
:
1636 term_printf(" Device %d.%d, speed %s Mb/s\n",
1637 bus_num
, addr
, speed_str
);
1638 class_str
= usb_class_str(class_id
);
1640 term_printf(" %s:", class_str
);
1642 term_printf(" Class %02x:", class_id
);
1643 term_printf(" USB device %04x:%04x", vendor_id
, product_id
);
1644 if (product_name
[0] != '\0')
1645 term_printf(", %s", product_name
);
1649 static int usb_host_info_device(void *opaque
, int bus_num
, int addr
,
1651 int vendor_id
, int product_id
,
1652 const char *product_name
,
1655 usb_info_device(bus_num
, addr
, class_id
, vendor_id
, product_id
,
1656 product_name
, speed
);
1660 static void dec2str(int val
, char *str
, size_t size
)
1663 snprintf(str
, size
, "*");
1665 snprintf(str
, size
, "%d", val
);
1668 static void hex2str(int val
, char *str
, size_t size
)
1671 snprintf(str
, size
, "*");
1673 snprintf(str
, size
, "%x", val
);
1676 void usb_host_info(void)
1678 struct USBAutoFilter
*f
;
1680 usb_host_scan(NULL
, usb_host_info_device
);
1682 if (usb_auto_filter
)
1683 term_printf(" Auto filters:\n");
1684 for (f
= usb_auto_filter
; f
; f
= f
->next
) {
1685 char bus
[10], addr
[10], vid
[10], pid
[10];
1686 dec2str(f
->bus_num
, bus
, sizeof(bus
));
1687 dec2str(f
->addr
, addr
, sizeof(addr
));
1688 hex2str(f
->vendor_id
, vid
, sizeof(vid
));
1689 hex2str(f
->product_id
, pid
, sizeof(pid
));
1690 term_printf(" Device %s.%s ID %s:%s\n", bus
, addr
, vid
, pid
);
1698 void usb_host_info(void)
1700 term_printf("USB host devices not supported\n");
1703 /* XXX: modify configure to compile the right host driver */
1704 USBDevice
*usb_host_device_open(const char *devname
)
1709 int usb_host_device_close(const char *devname
)