2 * Linux host USB redirector
4 * Copyright (c) 2005 Fabrice Bellard
6 * Copyright (c) 2008 Max Krasnyansky
7 * Support for host device auto connect & disconnect
8 * Major rewrite to support fully async operation
10 * Copyright 2008 TJ <linux@tjworld.net>
11 * Added flexible support for /dev/bus/usb /sys/bus/usb/devices in addition
12 * to the legacy /proc/bus/usb USB device discovery and handling
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this software and associated documentation files (the "Software"), to deal
16 * in the Software without restriction, including without limitation the rights
17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 * copies of the Software, and to permit persons to whom the Software is
19 * furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33 #include "qemu-common.h"
34 #include "qemu-timer.h"
38 #include <sys/ioctl.h>
41 #include <linux/usbdevice_fs.h>
42 #include <linux/version.h>
45 /* We redefine it to avoid version problems */
46 struct usb_ctrltransfer
{
56 struct usb_ctrlrequest
{
64 typedef int USBScanFunc(void *opaque
, int bus_num
, int addr
, int class_id
,
65 int vendor_id
, int product_id
,
66 const char *product_name
, int speed
);
67 static int usb_host_find_device(int *pbus_num
, int *paddr
,
68 char *product_name
, int product_name_size
,
73 #define dprintf printf
78 #define USBDBG_DEVOPENED "husb: opened %s/devices\n"
80 #define USBPROCBUS_PATH "/proc/bus/usb"
81 #define PRODUCT_NAME_SZ 32
82 #define MAX_ENDPOINTS 16
83 #define USBDEVBUS_PATH "/dev/bus/usb"
84 #define USBSYSBUS_PATH "/sys/bus/usb"
86 static char *usb_host_device_path
;
93 static int usb_fs_type
;
95 /* endpoint association data */
109 * Control transfer state.
110 * Note that 'buffer' _must_ follow 'req' field because
111 * we need contigious buffer when we submit control URB.
117 struct usb_ctrlrequest req
;
118 uint8_t buffer
[2048];
121 typedef struct USBHostDevice
{
131 struct ctrl_struct ctrl
;
132 struct endp_data endp_table
[MAX_ENDPOINTS
];
134 /* Host side address */
138 struct USBHostDevice
*next
;
141 static int is_isoc(USBHostDevice
*s
, int ep
)
143 return s
->endp_table
[ep
- 1].type
== USBDEVFS_URB_TYPE_ISO
;
146 static int is_halted(USBHostDevice
*s
, int ep
)
148 return s
->endp_table
[ep
- 1].halted
;
151 static void clear_halt(USBHostDevice
*s
, int ep
)
153 s
->endp_table
[ep
- 1].halted
= 0;
156 static void set_halt(USBHostDevice
*s
, int ep
)
158 s
->endp_table
[ep
- 1].halted
= 1;
161 static USBHostDevice
*hostdev_list
;
163 static void hostdev_link(USBHostDevice
*dev
)
165 dev
->next
= hostdev_list
;
169 static void hostdev_unlink(USBHostDevice
*dev
)
171 USBHostDevice
*pdev
= hostdev_list
;
172 USBHostDevice
**prev
= &hostdev_list
;
185 static USBHostDevice
*hostdev_find(int bus_num
, int addr
)
187 USBHostDevice
*s
= hostdev_list
;
189 if (s
->bus_num
== bus_num
&& s
->addr
== addr
)
198 * We always allocate one isoc descriptor even for bulk transfers
199 * to simplify allocation and casts.
201 typedef struct AsyncURB
203 struct usbdevfs_urb urb
;
204 struct usbdevfs_iso_packet_desc isocpd
;
210 static AsyncURB
*async_alloc(void)
212 return (AsyncURB
*) qemu_mallocz(sizeof(AsyncURB
));
215 static void async_free(AsyncURB
*aurb
)
220 static void async_complete_ctrl(USBHostDevice
*s
, USBPacket
*p
)
222 switch(s
->ctrl
.state
) {
223 case CTRL_STATE_SETUP
:
224 if (p
->len
< s
->ctrl
.len
)
225 s
->ctrl
.len
= p
->len
;
226 s
->ctrl
.state
= CTRL_STATE_DATA
;
231 s
->ctrl
.state
= CTRL_STATE_IDLE
;
240 static void async_complete(void *opaque
)
242 USBHostDevice
*s
= opaque
;
248 int r
= ioctl(s
->fd
, USBDEVFS_REAPURBNDELAY
, &aurb
);
253 if (errno
== ENODEV
&& !s
->closing
) {
254 printf("husb: device %d.%d disconnected\n", s
->bus_num
, s
->addr
);
255 usb_device_delete_addr(s
->bus_num
, s
->dev
.addr
);
259 dprintf("husb: async. reap urb failed errno %d\n", errno
);
265 dprintf("husb: async completed. aurb %p status %d alen %d\n",
266 aurb
, aurb
->urb
.status
, aurb
->urb
.actual_length
);
269 switch (aurb
->urb
.status
) {
271 p
->len
= aurb
->urb
.actual_length
;
272 if (aurb
->urb
.type
== USBDEVFS_URB_TYPE_CONTROL
)
273 async_complete_ctrl(s
, p
);
277 set_halt(s
, p
->devep
);
278 p
->len
= USB_RET_STALL
;
282 p
->len
= USB_RET_NAK
;
286 usb_packet_complete(p
);
293 static void async_cancel(USBPacket
*unused
, void *opaque
)
295 AsyncURB
*aurb
= opaque
;
296 USBHostDevice
*s
= aurb
->hdev
;
298 dprintf("husb: async cancel. aurb %p\n", aurb
);
300 /* Mark it as dead (see async_complete above) */
303 int r
= ioctl(s
->fd
, USBDEVFS_DISCARDURB
, aurb
);
305 dprintf("husb: async. discard urb failed errno %d\n", errno
);
309 static int usb_host_claim_interfaces(USBHostDevice
*dev
, int configuration
)
311 int dev_descr_len
, config_descr_len
;
312 int interface
, nb_interfaces
, nb_configurations
;
315 if (configuration
== 0) /* address state - ignore */
318 dprintf("husb: claiming interfaces. config %d\n", configuration
);
321 dev_descr_len
= dev
->descr
[0];
322 if (dev_descr_len
> dev
->descr_len
)
324 nb_configurations
= dev
->descr
[17];
327 while (i
< dev
->descr_len
) {
328 dprintf("husb: i is %d, descr_len is %d, dl %d, dt %d\n", i
, dev
->descr_len
,
329 dev
->descr
[i
], dev
->descr
[i
+1]);
331 if (dev
->descr
[i
+1] != USB_DT_CONFIG
) {
335 config_descr_len
= dev
->descr
[i
];
337 printf("husb: config #%d need %d\n", dev
->descr
[i
+ 5], configuration
);
339 if (configuration
< 0 || configuration
== dev
->descr
[i
+ 5]) {
340 configuration
= dev
->descr
[i
+ 5];
344 i
+= config_descr_len
;
347 if (i
>= dev
->descr_len
) {
348 fprintf(stderr
, "husb: update iface failed. no matching configuration\n");
351 nb_interfaces
= dev
->descr
[i
+ 4];
353 #ifdef USBDEVFS_DISCONNECT
354 /* earlier Linux 2.4 do not support that */
356 struct usbdevfs_ioctl ctrl
;
357 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
358 ctrl
.ioctl_code
= USBDEVFS_DISCONNECT
;
359 ctrl
.ifno
= interface
;
360 ret
= ioctl(dev
->fd
, USBDEVFS_IOCTL
, &ctrl
);
361 if (ret
< 0 && errno
!= ENODATA
) {
362 perror("USBDEVFS_DISCONNECT");
369 /* XXX: only grab if all interfaces are free */
370 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
371 ret
= ioctl(dev
->fd
, USBDEVFS_CLAIMINTERFACE
, &interface
);
373 if (errno
== EBUSY
) {
374 printf("husb: update iface. device already grabbed\n");
376 perror("husb: failed to claim interface");
383 printf("husb: %d interfaces claimed for configuration %d\n",
384 nb_interfaces
, configuration
);
386 dev
->ninterfaces
= nb_interfaces
;
387 dev
->configuration
= configuration
;
391 static int usb_host_release_interfaces(USBHostDevice
*s
)
395 dprintf("husb: releasing interfaces\n");
397 for (i
= 0; i
< s
->ninterfaces
; i
++) {
398 ret
= ioctl(s
->fd
, USBDEVFS_RELEASEINTERFACE
, &i
);
400 perror("husb: failed to release interface");
408 static void usb_host_handle_reset(USBDevice
*dev
)
410 USBHostDevice
*s
= (USBHostDevice
*) dev
;
412 dprintf("husb: reset device %u.%u\n", s
->bus_num
, s
->addr
);
414 ioctl(s
->fd
, USBDEVFS_RESET
);
416 usb_host_claim_interfaces(s
, s
->configuration
);
419 static void usb_host_handle_destroy(USBDevice
*dev
)
421 USBHostDevice
*s
= (USBHostDevice
*)dev
;
425 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
437 static int usb_linux_update_endp_table(USBHostDevice
*s
);
439 static int usb_host_handle_data(USBHostDevice
*s
, USBPacket
*p
)
441 struct usbdevfs_urb
*urb
;
445 aurb
= async_alloc();
451 if (p
->pid
== USB_TOKEN_IN
)
452 urb
->endpoint
= p
->devep
| 0x80;
454 urb
->endpoint
= p
->devep
;
456 if (is_halted(s
, p
->devep
)) {
457 ret
= ioctl(s
->fd
, USBDEVFS_CLEAR_HALT
, &urb
->endpoint
);
459 dprintf("husb: failed to clear halt. ep 0x%x errno %d\n",
460 urb
->endpoint
, errno
);
463 clear_halt(s
, p
->devep
);
466 urb
->buffer
= p
->data
;
467 urb
->buffer_length
= p
->len
;
469 if (is_isoc(s
, p
->devep
)) {
470 /* Setup ISOC transfer */
471 urb
->type
= USBDEVFS_URB_TYPE_ISO
;
472 urb
->flags
= USBDEVFS_URB_ISO_ASAP
;
473 urb
->number_of_packets
= 1;
474 urb
->iso_frame_desc
[0].length
= p
->len
;
476 /* Setup bulk transfer */
477 urb
->type
= USBDEVFS_URB_TYPE_BULK
;
480 urb
->usercontext
= s
;
482 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
484 dprintf("husb: data submit. ep 0x%x len %u aurb %p\n", urb
->endpoint
, p
->len
, aurb
);
487 dprintf("husb: submit failed. errno %d\n", errno
);
495 return USB_RET_STALL
;
499 usb_defer_packet(p
, async_cancel
, aurb
);
500 return USB_RET_ASYNC
;
503 static int ctrl_error(void)
505 if (errno
== ETIMEDOUT
)
508 return USB_RET_STALL
;
511 static int usb_host_set_address(USBHostDevice
*s
, int addr
)
513 dprintf("husb: ctrl set addr %u\n", addr
);
518 static int usb_host_set_config(USBHostDevice
*s
, int config
)
520 usb_host_release_interfaces(s
);
522 int ret
= ioctl(s
->fd
, USBDEVFS_SETCONFIGURATION
, &config
);
524 dprintf("husb: ctrl set config %d ret %d errno %d\n", config
, ret
, errno
);
529 usb_host_claim_interfaces(s
, config
);
533 static int usb_host_set_interface(USBHostDevice
*s
, int iface
, int alt
)
535 struct usbdevfs_setinterface si
;
538 si
.interface
= iface
;
540 ret
= ioctl(s
->fd
, USBDEVFS_SETINTERFACE
, &si
);
542 dprintf("husb: ctrl set iface %d altset %d ret %d errno %d\n",
543 iface
, alt
, ret
, errno
);
548 usb_linux_update_endp_table(s
);
552 static int usb_host_handle_control(USBHostDevice
*s
, USBPacket
*p
)
554 struct usbdevfs_urb
*urb
;
556 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 buffer_len
= 8 + s
->ctrl
.len
;
587 if (buffer_len
> sizeof(s
->ctrl
.buffer
)) {
588 fprintf(stderr
, "husb: ctrl buffer too small (%u > %zu)\n",
589 buffer_len
, sizeof(s
->ctrl
.buffer
));
590 return USB_RET_STALL
;
593 aurb
= async_alloc();
598 * Setup ctrl transfer.
600 * s->ctrl is layed out such that data buffer immediately follows
601 * 'req' struct which is exactly what usbdevfs expects.
605 urb
->type
= USBDEVFS_URB_TYPE_CONTROL
;
606 urb
->endpoint
= p
->devep
;
608 urb
->buffer
= &s
->ctrl
.req
;
609 urb
->buffer_length
= buffer_len
;
611 urb
->usercontext
= s
;
613 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
615 dprintf("husb: submit ctrl. len %u aurb %p\n", urb
->buffer_length
, aurb
);
618 dprintf("husb: submit failed. errno %d\n", errno
);
626 return USB_RET_STALL
;
630 usb_defer_packet(p
, async_cancel
, aurb
);
631 return USB_RET_ASYNC
;
634 static int do_token_setup(USBDevice
*dev
, USBPacket
*p
)
636 USBHostDevice
*s
= (USBHostDevice
*) dev
;
640 return USB_RET_STALL
;
642 memcpy(&s
->ctrl
.req
, p
->data
, 8);
643 s
->ctrl
.len
= le16_to_cpu(s
->ctrl
.req
.wLength
);
645 s
->ctrl
.state
= CTRL_STATE_SETUP
;
647 if (s
->ctrl
.req
.bRequestType
& USB_DIR_IN
) {
648 ret
= usb_host_handle_control(s
, p
);
652 if (ret
< s
->ctrl
.len
)
654 s
->ctrl
.state
= CTRL_STATE_DATA
;
656 if (s
->ctrl
.len
== 0)
657 s
->ctrl
.state
= CTRL_STATE_ACK
;
659 s
->ctrl
.state
= CTRL_STATE_DATA
;
665 static int do_token_in(USBDevice
*dev
, USBPacket
*p
)
667 USBHostDevice
*s
= (USBHostDevice
*) dev
;
671 return usb_host_handle_data(s
, p
);
673 switch(s
->ctrl
.state
) {
675 if (!(s
->ctrl
.req
.bRequestType
& USB_DIR_IN
)) {
676 ret
= usb_host_handle_control(s
, p
);
677 if (ret
== USB_RET_ASYNC
)
678 return USB_RET_ASYNC
;
680 s
->ctrl
.state
= CTRL_STATE_IDLE
;
681 return ret
> 0 ? 0 : ret
;
686 case CTRL_STATE_DATA
:
687 if (s
->ctrl
.req
.bRequestType
& USB_DIR_IN
) {
688 int len
= s
->ctrl
.len
- s
->ctrl
.offset
;
691 memcpy(p
->data
, s
->ctrl
.buffer
+ s
->ctrl
.offset
, len
);
692 s
->ctrl
.offset
+= len
;
693 if (s
->ctrl
.offset
>= s
->ctrl
.len
)
694 s
->ctrl
.state
= CTRL_STATE_ACK
;
698 s
->ctrl
.state
= CTRL_STATE_IDLE
;
699 return USB_RET_STALL
;
702 return USB_RET_STALL
;
706 static int do_token_out(USBDevice
*dev
, USBPacket
*p
)
708 USBHostDevice
*s
= (USBHostDevice
*) dev
;
711 return usb_host_handle_data(s
, p
);
713 switch(s
->ctrl
.state
) {
715 if (s
->ctrl
.req
.bRequestType
& USB_DIR_IN
) {
716 s
->ctrl
.state
= CTRL_STATE_IDLE
;
719 /* ignore additional output */
723 case CTRL_STATE_DATA
:
724 if (!(s
->ctrl
.req
.bRequestType
& USB_DIR_IN
)) {
725 int len
= s
->ctrl
.len
- s
->ctrl
.offset
;
728 memcpy(s
->ctrl
.buffer
+ s
->ctrl
.offset
, p
->data
, len
);
729 s
->ctrl
.offset
+= len
;
730 if (s
->ctrl
.offset
>= s
->ctrl
.len
)
731 s
->ctrl
.state
= CTRL_STATE_ACK
;
735 s
->ctrl
.state
= CTRL_STATE_IDLE
;
736 return USB_RET_STALL
;
739 return USB_RET_STALL
;
745 * Called by the HC (host controller).
747 * Returns length of the transaction or one of the USB_RET_XXX codes.
749 static int usb_host_handle_packet(USBDevice
*s
, USBPacket
*p
)
753 s
->state
= USB_STATE_ATTACHED
;
757 s
->state
= USB_STATE_NOTATTACHED
;
761 s
->remote_wakeup
= 0;
763 s
->state
= USB_STATE_DEFAULT
;
764 s
->info
->handle_reset(s
);
768 /* Rest of the PIDs must match our address */
769 if (s
->state
< USB_STATE_DEFAULT
|| p
->devaddr
!= s
->addr
)
770 return USB_RET_NODEV
;
773 case USB_TOKEN_SETUP
:
774 return do_token_setup(s
, p
);
777 return do_token_in(s
, p
);
780 return do_token_out(s
, p
);
783 return USB_RET_STALL
;
787 /* returns 1 on problem encountered or 0 for success */
788 static int usb_linux_update_endp_table(USBHostDevice
*s
)
790 uint8_t *descriptors
;
791 uint8_t devep
, type
, configuration
, alt_interface
;
792 struct usb_ctrltransfer ct
;
793 int interface
, ret
, length
, i
;
795 ct
.bRequestType
= USB_DIR_IN
;
796 ct
.bRequest
= USB_REQ_GET_CONFIGURATION
;
800 ct
.data
= &configuration
;
803 ret
= ioctl(s
->fd
, USBDEVFS_CONTROL
, &ct
);
805 perror("usb_linux_update_endp_table");
809 /* in address state */
810 if (configuration
== 0)
813 /* get the desired configuration, interface, and endpoint descriptors
814 * from device description */
815 descriptors
= &s
->descr
[18];
816 length
= s
->descr_len
- 18;
819 if (descriptors
[i
+ 1] != USB_DT_CONFIG
||
820 descriptors
[i
+ 5] != configuration
) {
821 dprintf("invalid descriptor data - configuration\n");
827 if (descriptors
[i
+ 1] != USB_DT_INTERFACE
||
828 (descriptors
[i
+ 1] == USB_DT_INTERFACE
&&
829 descriptors
[i
+ 4] == 0)) {
834 interface
= descriptors
[i
+ 2];
836 ct
.bRequestType
= USB_DIR_IN
| USB_RECIP_INTERFACE
;
837 ct
.bRequest
= USB_REQ_GET_INTERFACE
;
839 ct
.wIndex
= interface
;
841 ct
.data
= &alt_interface
;
844 ret
= ioctl(s
->fd
, USBDEVFS_CONTROL
, &ct
);
846 alt_interface
= interface
;
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 int usb_host_initfn(USBDevice
*dev
)
899 static USBDevice
*usb_host_device_open_addr(int bus_num
, int addr
, const char *prod_name
)
904 struct usbdevfs_connectinfo ci
;
907 printf("husb: open device %d.%d\n", bus_num
, addr
);
909 if (!usb_host_device_path
) {
910 perror("husb: USB Host Device Path not set");
913 snprintf(buf
, sizeof(buf
), "%s/%03d/%03d", usb_host_device_path
,
915 fd
= open(buf
, O_RDWR
| O_NONBLOCK
);
920 dprintf("husb: opened %s\n", buf
);
922 d
= usb_create(NULL
/* FIXME */, "USB Host Device");
923 dev
= DO_UPCAST(USBHostDevice
, dev
, d
);
925 dev
->bus_num
= bus_num
;
929 /* read the device description */
930 dev
->descr_len
= read(fd
, dev
->descr
, sizeof(dev
->descr
));
931 if (dev
->descr_len
<= 0) {
932 perror("husb: reading device data failed");
939 printf("=== begin dumping device descriptor data ===\n");
940 for (x
= 0; x
< dev
->descr_len
; x
++)
941 printf("%02x ", dev
->descr
[x
]);
942 printf("\n=== end dumping device descriptor data ===\n");
948 * Initial configuration is -1 which makes us claim first
949 * available config. We used to start with 1, which does not
950 * always work. I've seen devices where first config starts
953 if (!usb_host_claim_interfaces(dev
, -1))
956 ret
= ioctl(fd
, USBDEVFS_CONNECTINFO
, &ci
);
958 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
962 printf("husb: grabbed usb device %d.%d\n", bus_num
, addr
);
964 ret
= usb_linux_update_endp_table(dev
);
969 dev
->dev
.speed
= USB_SPEED_LOW
;
971 dev
->dev
.speed
= USB_SPEED_HIGH
;
973 if (!prod_name
|| prod_name
[0] == '\0')
974 snprintf(dev
->dev
.devname
, sizeof(dev
->dev
.devname
),
975 "host:%d.%d", bus_num
, addr
);
977 pstrcpy(dev
->dev
.devname
, sizeof(dev
->dev
.devname
),
980 /* USB devio uses 'write' flag to check for async completions */
981 qemu_set_fd_handler(dev
->fd
, NULL
, async_complete
, dev
);
985 if (qdev_init(&d
->qdev
) < 0)
987 return (USBDevice
*) dev
;
998 static struct USBDeviceInfo usb_host_dev_info
= {
999 .qdev
.name
= "USB Host Device",
1000 .qdev
.size
= sizeof(USBHostDevice
),
1001 .init
= usb_host_initfn
,
1002 .handle_packet
= usb_host_handle_packet
,
1003 .handle_reset
= usb_host_handle_reset
,
1005 .handle_control
= usb_host_handle_control
,
1006 .handle_data
= usb_host_handle_data
,
1008 .handle_destroy
= usb_host_handle_destroy
,
1011 static void usb_host_register_devices(void)
1013 usb_qdev_register(&usb_host_dev_info
);
1015 device_init(usb_host_register_devices
)
1017 static int usb_host_auto_add(const char *spec
);
1018 static int usb_host_auto_del(const char *spec
);
1020 USBDevice
*usb_host_device_open(const char *devname
)
1022 Monitor
*mon
= cur_mon
;
1024 char product_name
[PRODUCT_NAME_SZ
];
1026 if (strstr(devname
, "auto:")) {
1027 usb_host_auto_add(devname
);
1031 if (usb_host_find_device(&bus_num
, &addr
, product_name
, sizeof(product_name
),
1035 if (hostdev_find(bus_num
, addr
)) {
1036 monitor_printf(mon
, "husb: host usb device %d.%d is already open\n",
1041 return usb_host_device_open_addr(bus_num
, addr
, product_name
);
1044 int usb_host_device_close(const char *devname
)
1046 char product_name
[PRODUCT_NAME_SZ
];
1050 if (strstr(devname
, "auto:"))
1051 return usb_host_auto_del(devname
);
1053 if (usb_host_find_device(&bus_num
, &addr
, product_name
, sizeof(product_name
),
1057 s
= hostdev_find(bus_num
, addr
);
1059 usb_device_delete_addr(s
->bus_num
, s
->dev
.addr
);
1066 static int get_tag_value(char *buf
, int buf_size
,
1067 const char *str
, const char *tag
,
1068 const char *stopchars
)
1072 p
= strstr(str
, tag
);
1076 while (qemu_isspace(*p
))
1079 while (*p
!= '\0' && !strchr(stopchars
, *p
)) {
1080 if ((q
- buf
) < (buf_size
- 1))
1089 * Use /proc/bus/usb/devices or /dev/bus/usb/devices file to determine
1090 * host's USB devices. This is legacy support since many distributions
1091 * are moving to /sys/bus/usb
1093 static int usb_host_scan_dev(void *opaque
, USBScanFunc
*func
)
1098 int bus_num
, addr
, speed
, device_count
, class_id
, product_id
, vendor_id
;
1099 char product_name
[512];
1102 if (!usb_host_device_path
) {
1103 perror("husb: USB Host Device Path not set");
1106 snprintf(line
, sizeof(line
), "%s/devices", usb_host_device_path
);
1107 f
= fopen(line
, "r");
1109 perror("husb: cannot open devices file");
1114 bus_num
= addr
= speed
= class_id
= product_id
= vendor_id
= 0;
1116 if (fgets(line
, sizeof(line
), f
) == NULL
)
1118 if (strlen(line
) > 0)
1119 line
[strlen(line
) - 1] = '\0';
1120 if (line
[0] == 'T' && line
[1] == ':') {
1121 if (device_count
&& (vendor_id
|| product_id
)) {
1122 /* New device. Add the previously discovered device. */
1123 ret
= func(opaque
, bus_num
, addr
, class_id
, vendor_id
,
1124 product_id
, product_name
, speed
);
1128 if (get_tag_value(buf
, sizeof(buf
), line
, "Bus=", " ") < 0)
1130 bus_num
= atoi(buf
);
1131 if (get_tag_value(buf
, sizeof(buf
), line
, "Dev#=", " ") < 0)
1134 if (get_tag_value(buf
, sizeof(buf
), line
, "Spd=", " ") < 0)
1136 if (!strcmp(buf
, "480"))
1137 speed
= USB_SPEED_HIGH
;
1138 else if (!strcmp(buf
, "1.5"))
1139 speed
= USB_SPEED_LOW
;
1141 speed
= USB_SPEED_FULL
;
1142 product_name
[0] = '\0';
1147 } else if (line
[0] == 'P' && line
[1] == ':') {
1148 if (get_tag_value(buf
, sizeof(buf
), line
, "Vendor=", " ") < 0)
1150 vendor_id
= strtoul(buf
, NULL
, 16);
1151 if (get_tag_value(buf
, sizeof(buf
), line
, "ProdID=", " ") < 0)
1153 product_id
= strtoul(buf
, NULL
, 16);
1154 } else if (line
[0] == 'S' && line
[1] == ':') {
1155 if (get_tag_value(buf
, sizeof(buf
), line
, "Product=", "") < 0)
1157 pstrcpy(product_name
, sizeof(product_name
), buf
);
1158 } else if (line
[0] == 'D' && line
[1] == ':') {
1159 if (get_tag_value(buf
, sizeof(buf
), line
, "Cls=", " (") < 0)
1161 class_id
= strtoul(buf
, NULL
, 16);
1165 if (device_count
&& (vendor_id
|| product_id
)) {
1166 /* Add the last device. */
1167 ret
= func(opaque
, bus_num
, addr
, class_id
, vendor_id
,
1168 product_id
, product_name
, speed
);
1177 * Read sys file-system device file
1179 * @line address of buffer to put file contents in
1180 * @line_size size of line
1181 * @device_file path to device file (printf format string)
1182 * @device_name device being opened (inserted into device_file)
1184 * @return 0 failed, 1 succeeded ('line' contains data)
1186 static int usb_host_read_file(char *line
, size_t line_size
, const char *device_file
, const char *device_name
)
1188 Monitor
*mon
= cur_mon
;
1191 char filename
[PATH_MAX
];
1193 snprintf(filename
, PATH_MAX
, USBSYSBUS_PATH
"/devices/%s/%s", device_name
,
1195 f
= fopen(filename
, "r");
1197 fgets(line
, line_size
, f
);
1201 monitor_printf(mon
, "husb: could not open %s\n", filename
);
1208 * Use /sys/bus/usb/devices/ directory to determine host's USB
1211 * This code is based on Robert Schiele's original patches posted to
1212 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1214 static int usb_host_scan_sys(void *opaque
, USBScanFunc
*func
)
1218 int bus_num
, addr
, speed
, class_id
, product_id
, vendor_id
;
1220 char product_name
[512];
1223 dir
= opendir(USBSYSBUS_PATH
"/devices");
1225 perror("husb: cannot open devices directory");
1229 while ((de
= readdir(dir
))) {
1230 if (de
->d_name
[0] != '.' && !strchr(de
->d_name
, ':')) {
1231 char *tmpstr
= de
->d_name
;
1232 if (!strncmp(de
->d_name
, "usb", 3))
1234 bus_num
= atoi(tmpstr
);
1236 if (!usb_host_read_file(line
, sizeof(line
), "devnum", de
->d_name
))
1238 if (sscanf(line
, "%d", &addr
) != 1)
1241 if (!usb_host_read_file(line
, sizeof(line
), "bDeviceClass",
1244 if (sscanf(line
, "%x", &class_id
) != 1)
1247 if (!usb_host_read_file(line
, sizeof(line
), "idVendor", de
->d_name
))
1249 if (sscanf(line
, "%x", &vendor_id
) != 1)
1252 if (!usb_host_read_file(line
, sizeof(line
), "idProduct",
1255 if (sscanf(line
, "%x", &product_id
) != 1)
1258 if (!usb_host_read_file(line
, sizeof(line
), "product",
1262 if (strlen(line
) > 0)
1263 line
[strlen(line
) - 1] = '\0';
1264 pstrcpy(product_name
, sizeof(product_name
), line
);
1267 if (!usb_host_read_file(line
, sizeof(line
), "speed", de
->d_name
))
1269 if (!strcmp(line
, "480\n"))
1270 speed
= USB_SPEED_HIGH
;
1271 else if (!strcmp(line
, "1.5\n"))
1272 speed
= USB_SPEED_LOW
;
1274 speed
= USB_SPEED_FULL
;
1276 ret
= func(opaque
, bus_num
, addr
, class_id
, vendor_id
,
1277 product_id
, product_name
, speed
);
1289 * Determine how to access the host's USB devices and call the
1290 * specific support function.
1292 static int usb_host_scan(void *opaque
, USBScanFunc
*func
)
1294 Monitor
*mon
= cur_mon
;
1298 const char *fs_type
[] = {"unknown", "proc", "dev", "sys"};
1299 char devpath
[PATH_MAX
];
1301 /* only check the host once */
1303 dir
= opendir(USBSYSBUS_PATH
"/devices");
1305 /* devices found in /dev/bus/usb/ (yes - not a mistake!) */
1306 strcpy(devpath
, USBDEVBUS_PATH
);
1307 usb_fs_type
= USB_FS_SYS
;
1309 dprintf(USBDBG_DEVOPENED
, USBSYSBUS_PATH
);
1312 f
= fopen(USBPROCBUS_PATH
"/devices", "r");
1314 /* devices found in /proc/bus/usb/ */
1315 strcpy(devpath
, USBPROCBUS_PATH
);
1316 usb_fs_type
= USB_FS_PROC
;
1318 dprintf(USBDBG_DEVOPENED
, USBPROCBUS_PATH
);
1321 /* try additional methods if an access method hasn't been found yet */
1322 f
= fopen(USBDEVBUS_PATH
"/devices", "r");
1324 /* devices found in /dev/bus/usb/ */
1325 strcpy(devpath
, USBDEVBUS_PATH
);
1326 usb_fs_type
= USB_FS_DEV
;
1328 dprintf(USBDBG_DEVOPENED
, USBDEVBUS_PATH
);
1333 monitor_printf(mon
, "husb: unable to access USB devices\n");
1337 /* the module setting (used later for opening devices) */
1338 usb_host_device_path
= qemu_mallocz(strlen(devpath
)+1);
1339 strcpy(usb_host_device_path
, devpath
);
1340 monitor_printf(mon
, "husb: using %s file-system with %s\n",
1341 fs_type
[usb_fs_type
], usb_host_device_path
);
1344 switch (usb_fs_type
) {
1347 ret
= usb_host_scan_dev(opaque
, func
);
1350 ret
= usb_host_scan_sys(opaque
, func
);
1359 struct USBAutoFilter
{
1360 struct USBAutoFilter
*next
;
1367 static QEMUTimer
*usb_auto_timer
;
1368 static struct USBAutoFilter
*usb_auto_filter
;
1370 static int usb_host_auto_scan(void *opaque
, int bus_num
, int addr
,
1371 int class_id
, int vendor_id
, int product_id
,
1372 const char *product_name
, int speed
)
1374 struct USBAutoFilter
*f
;
1375 struct USBDevice
*dev
;
1381 for (f
= usb_auto_filter
; f
; f
= f
->next
) {
1382 if (f
->bus_num
>= 0 && f
->bus_num
!= bus_num
)
1385 if (f
->addr
>= 0 && f
->addr
!= addr
)
1388 if (f
->vendor_id
>= 0 && f
->vendor_id
!= vendor_id
)
1391 if (f
->product_id
>= 0 && f
->product_id
!= product_id
)
1394 /* We got a match */
1396 /* Already attached ? */
1397 if (hostdev_find(bus_num
, addr
))
1400 dprintf("husb: auto open: bus_num %d addr %d\n", bus_num
, addr
);
1402 dev
= usb_host_device_open_addr(bus_num
, addr
, product_name
);
1408 static void usb_host_auto_timer(void *unused
)
1410 usb_host_scan(NULL
, usb_host_auto_scan
);
1411 qemu_mod_timer(usb_auto_timer
, qemu_get_clock(rt_clock
) + 2000);
1415 * Autoconnect filter
1417 * auto:bus:dev[:vid:pid]
1418 * auto:bus.dev[:vid:pid]
1420 * bus - bus number (dec, * means any)
1421 * dev - device number (dec, * means any)
1422 * vid - vendor id (hex, * means any)
1423 * pid - product id (hex, * means any)
1425 * See 'lsusb' output.
1427 static int parse_filter(const char *spec
, struct USBAutoFilter
*f
)
1429 enum { BUS
, DEV
, VID
, PID
, DONE
};
1430 const char *p
= spec
;
1438 for (i
= BUS
; i
< DONE
; i
++) {
1439 p
= strpbrk(p
, ":.");
1447 case BUS
: f
->bus_num
= strtol(p
, NULL
, 10); break;
1448 case DEV
: f
->addr
= strtol(p
, NULL
, 10); break;
1449 case VID
: f
->vendor_id
= strtol(p
, NULL
, 16); break;
1450 case PID
: f
->product_id
= strtol(p
, NULL
, 16); break;
1455 fprintf(stderr
, "husb: invalid auto filter spec %s\n", spec
);
1462 static int match_filter(const struct USBAutoFilter
*f1
,
1463 const struct USBAutoFilter
*f2
)
1465 return f1
->bus_num
== f2
->bus_num
&&
1466 f1
->addr
== f2
->addr
&&
1467 f1
->vendor_id
== f2
->vendor_id
&&
1468 f1
->product_id
== f2
->product_id
;
1471 static int usb_host_auto_add(const char *spec
)
1473 struct USBAutoFilter filter
, *f
;
1475 if (parse_filter(spec
, &filter
) < 0)
1478 f
= qemu_mallocz(sizeof(*f
));
1482 if (!usb_auto_filter
) {
1484 * First entry. Init and start the monitor.
1485 * Right now we're using timer to check for new devices.
1486 * If this turns out to be too expensive we can move that into a
1489 usb_auto_timer
= qemu_new_timer(rt_clock
, usb_host_auto_timer
, NULL
);
1490 if (!usb_auto_timer
) {
1491 fprintf(stderr
, "husb: failed to allocate auto scan timer\n");
1496 /* Check for new devices every two seconds */
1497 qemu_mod_timer(usb_auto_timer
, qemu_get_clock(rt_clock
) + 2000);
1500 dprintf("husb: added auto filter: bus_num %d addr %d vid %d pid %d\n",
1501 f
->bus_num
, f
->addr
, f
->vendor_id
, f
->product_id
);
1503 f
->next
= usb_auto_filter
;
1504 usb_auto_filter
= f
;
1509 static int usb_host_auto_del(const char *spec
)
1511 struct USBAutoFilter
*pf
= usb_auto_filter
;
1512 struct USBAutoFilter
**prev
= &usb_auto_filter
;
1513 struct USBAutoFilter filter
;
1515 if (parse_filter(spec
, &filter
) < 0)
1519 if (match_filter(pf
, &filter
)) {
1520 dprintf("husb: removed auto filter: bus_num %d addr %d vid %d pid %d\n",
1521 pf
->bus_num
, pf
->addr
, pf
->vendor_id
, pf
->product_id
);
1525 if (!usb_auto_filter
) {
1526 /* No more filters. Stop scanning. */
1527 qemu_del_timer(usb_auto_timer
);
1528 qemu_free_timer(usb_auto_timer
);
1541 typedef struct FindDeviceState
{
1546 char product_name
[PRODUCT_NAME_SZ
];
1549 static int usb_host_find_device_scan(void *opaque
, int bus_num
, int addr
,
1551 int vendor_id
, int product_id
,
1552 const char *product_name
, int speed
)
1554 FindDeviceState
*s
= opaque
;
1555 if ((vendor_id
== s
->vendor_id
&&
1556 product_id
== s
->product_id
) ||
1557 (bus_num
== s
->bus_num
&&
1559 pstrcpy(s
->product_name
, PRODUCT_NAME_SZ
, product_name
);
1560 s
->bus_num
= bus_num
;
1569 'bus.addr' (decimal numbers) or
1570 'vendor_id:product_id' (hexa numbers) */
1571 static int usb_host_find_device(int *pbus_num
, int *paddr
,
1572 char *product_name
, int product_name_size
,
1573 const char *devname
)
1579 p
= strchr(devname
, '.');
1581 *pbus_num
= strtoul(devname
, NULL
, 0);
1582 *paddr
= strtoul(p
+ 1, NULL
, 0);
1583 fs
.bus_num
= *pbus_num
;
1585 ret
= usb_host_scan(&fs
, usb_host_find_device_scan
);
1587 pstrcpy(product_name
, product_name_size
, fs
.product_name
);
1591 p
= strchr(devname
, ':');
1593 fs
.vendor_id
= strtoul(devname
, NULL
, 16);
1594 fs
.product_id
= strtoul(p
+ 1, NULL
, 16);
1595 ret
= usb_host_scan(&fs
, usb_host_find_device_scan
);
1597 *pbus_num
= fs
.bus_num
;
1599 pstrcpy(product_name
, product_name_size
, fs
.product_name
);
1606 /**********************/
1607 /* USB host device info */
1609 struct usb_class_info
{
1611 const char *class_name
;
1614 static const struct usb_class_info usb_class_info
[] = {
1615 { USB_CLASS_AUDIO
, "Audio"},
1616 { USB_CLASS_COMM
, "Communication"},
1617 { USB_CLASS_HID
, "HID"},
1618 { USB_CLASS_HUB
, "Hub" },
1619 { USB_CLASS_PHYSICAL
, "Physical" },
1620 { USB_CLASS_PRINTER
, "Printer" },
1621 { USB_CLASS_MASS_STORAGE
, "Storage" },
1622 { USB_CLASS_CDC_DATA
, "Data" },
1623 { USB_CLASS_APP_SPEC
, "Application Specific" },
1624 { USB_CLASS_VENDOR_SPEC
, "Vendor Specific" },
1625 { USB_CLASS_STILL_IMAGE
, "Still Image" },
1626 { USB_CLASS_CSCID
, "Smart Card" },
1627 { USB_CLASS_CONTENT_SEC
, "Content Security" },
1631 static const char *usb_class_str(uint8_t class)
1633 const struct usb_class_info
*p
;
1634 for(p
= usb_class_info
; p
->class != -1; p
++) {
1635 if (p
->class == class)
1638 return p
->class_name
;
1641 static void usb_info_device(Monitor
*mon
, int bus_num
, int addr
, int class_id
,
1642 int vendor_id
, int product_id
,
1643 const char *product_name
,
1646 const char *class_str
, *speed_str
;
1652 case USB_SPEED_FULL
:
1655 case USB_SPEED_HIGH
:
1663 monitor_printf(mon
, " Device %d.%d, speed %s Mb/s\n",
1664 bus_num
, addr
, speed_str
);
1665 class_str
= usb_class_str(class_id
);
1667 monitor_printf(mon
, " %s:", class_str
);
1669 monitor_printf(mon
, " Class %02x:", class_id
);
1670 monitor_printf(mon
, " USB device %04x:%04x", vendor_id
, product_id
);
1671 if (product_name
[0] != '\0')
1672 monitor_printf(mon
, ", %s", product_name
);
1673 monitor_printf(mon
, "\n");
1676 static int usb_host_info_device(void *opaque
, int bus_num
, int addr
,
1678 int vendor_id
, int product_id
,
1679 const char *product_name
,
1682 Monitor
*mon
= opaque
;
1684 usb_info_device(mon
, bus_num
, addr
, class_id
, vendor_id
, product_id
,
1685 product_name
, speed
);
1689 static void dec2str(int val
, char *str
, size_t size
)
1692 snprintf(str
, size
, "*");
1694 snprintf(str
, size
, "%d", val
);
1697 static void hex2str(int val
, char *str
, size_t size
)
1700 snprintf(str
, size
, "*");
1702 snprintf(str
, size
, "%x", val
);
1705 void usb_host_info(Monitor
*mon
)
1707 struct USBAutoFilter
*f
;
1709 usb_host_scan(mon
, usb_host_info_device
);
1711 if (usb_auto_filter
)
1712 monitor_printf(mon
, " Auto filters:\n");
1713 for (f
= usb_auto_filter
; f
; f
= f
->next
) {
1714 char bus
[10], addr
[10], vid
[10], pid
[10];
1715 dec2str(f
->bus_num
, bus
, sizeof(bus
));
1716 dec2str(f
->addr
, addr
, sizeof(addr
));
1717 hex2str(f
->vendor_id
, vid
, sizeof(vid
));
1718 hex2str(f
->product_id
, pid
, sizeof(pid
));
1719 monitor_printf(mon
, " Device %s.%s ID %s:%s\n",
1720 bus
, addr
, vid
, pid
);