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
);
71 #define DPRINTF printf
76 #define USBDBG_DEVOPENED "husb: opened %s/devices\n"
78 #define USBPROCBUS_PATH "/proc/bus/usb"
79 #define PRODUCT_NAME_SZ 32
80 #define MAX_ENDPOINTS 16
81 #define USBDEVBUS_PATH "/dev/bus/usb"
82 #define USBSYSBUS_PATH "/sys/bus/usb"
84 static char *usb_host_device_path
;
91 static int usb_fs_type
;
93 /* endpoint association data */
107 * Control transfer state.
108 * Note that 'buffer' _must_ follow 'req' field because
109 * we need contigious buffer when we submit control URB.
115 struct usb_ctrlrequest req
;
116 uint8_t buffer
[8192];
119 struct USBAutoFilter
{
126 typedef struct USBHostDevice
{
136 struct ctrl_struct ctrl
;
137 struct endp_data endp_table
[MAX_ENDPOINTS
];
139 /* Host side address */
142 struct USBAutoFilter match
;
144 QTAILQ_ENTRY(USBHostDevice
) next
;
147 static QTAILQ_HEAD(, USBHostDevice
) hostdevs
= QTAILQ_HEAD_INITIALIZER(hostdevs
);
149 static int usb_host_close(USBHostDevice
*dev
);
150 static int parse_filter(const char *spec
, struct USBAutoFilter
*f
);
151 static void usb_host_auto_check(void *unused
);
153 static int is_isoc(USBHostDevice
*s
, int ep
)
155 return s
->endp_table
[ep
- 1].type
== USBDEVFS_URB_TYPE_ISO
;
158 static int is_halted(USBHostDevice
*s
, int ep
)
160 return s
->endp_table
[ep
- 1].halted
;
163 static void clear_halt(USBHostDevice
*s
, int ep
)
165 s
->endp_table
[ep
- 1].halted
= 0;
168 static void set_halt(USBHostDevice
*s
, int ep
)
170 s
->endp_table
[ep
- 1].halted
= 1;
175 * We always allocate one isoc descriptor even for bulk transfers
176 * to simplify allocation and casts.
178 typedef struct AsyncURB
180 struct usbdevfs_urb urb
;
181 struct usbdevfs_iso_packet_desc isocpd
;
187 static AsyncURB
*async_alloc(void)
189 return (AsyncURB
*) qemu_mallocz(sizeof(AsyncURB
));
192 static void async_free(AsyncURB
*aurb
)
197 static void async_complete_ctrl(USBHostDevice
*s
, USBPacket
*p
)
199 switch(s
->ctrl
.state
) {
200 case CTRL_STATE_SETUP
:
201 if (p
->len
< s
->ctrl
.len
)
202 s
->ctrl
.len
= p
->len
;
203 s
->ctrl
.state
= CTRL_STATE_DATA
;
208 s
->ctrl
.state
= CTRL_STATE_IDLE
;
217 static void async_complete(void *opaque
)
219 USBHostDevice
*s
= opaque
;
225 int r
= ioctl(s
->fd
, USBDEVFS_REAPURBNDELAY
, &aurb
);
230 if (errno
== ENODEV
&& !s
->closing
) {
231 printf("husb: device %d.%d disconnected\n", s
->bus_num
, s
->addr
);
233 usb_host_auto_check(NULL
);
237 DPRINTF("husb: async. reap urb failed errno %d\n", errno
);
243 DPRINTF("husb: async completed. aurb %p status %d alen %d\n",
244 aurb
, aurb
->urb
.status
, aurb
->urb
.actual_length
);
247 switch (aurb
->urb
.status
) {
249 p
->len
= aurb
->urb
.actual_length
;
250 if (aurb
->urb
.type
== USBDEVFS_URB_TYPE_CONTROL
)
251 async_complete_ctrl(s
, p
);
255 set_halt(s
, p
->devep
);
256 p
->len
= USB_RET_STALL
;
260 p
->len
= USB_RET_NAK
;
264 usb_packet_complete(p
);
271 static void async_cancel(USBPacket
*unused
, void *opaque
)
273 AsyncURB
*aurb
= opaque
;
274 USBHostDevice
*s
= aurb
->hdev
;
276 DPRINTF("husb: async cancel. aurb %p\n", aurb
);
278 /* Mark it as dead (see async_complete above) */
281 int r
= ioctl(s
->fd
, USBDEVFS_DISCARDURB
, aurb
);
283 DPRINTF("husb: async. discard urb failed errno %d\n", errno
);
287 static int usb_host_claim_interfaces(USBHostDevice
*dev
, int configuration
)
289 int dev_descr_len
, config_descr_len
;
290 int interface
, nb_interfaces
;
293 if (configuration
== 0) /* address state - ignore */
296 DPRINTF("husb: claiming interfaces. config %d\n", configuration
);
299 dev_descr_len
= dev
->descr
[0];
300 if (dev_descr_len
> dev
->descr_len
)
304 while (i
< dev
->descr_len
) {
305 DPRINTF("husb: i is %d, descr_len is %d, dl %d, dt %d\n", i
, dev
->descr_len
,
306 dev
->descr
[i
], dev
->descr
[i
+1]);
308 if (dev
->descr
[i
+1] != USB_DT_CONFIG
) {
312 config_descr_len
= dev
->descr
[i
];
314 printf("husb: config #%d need %d\n", dev
->descr
[i
+ 5], configuration
);
316 if (configuration
< 0 || configuration
== dev
->descr
[i
+ 5]) {
317 configuration
= dev
->descr
[i
+ 5];
321 i
+= config_descr_len
;
324 if (i
>= dev
->descr_len
) {
325 fprintf(stderr
, "husb: update iface failed. no matching configuration\n");
328 nb_interfaces
= dev
->descr
[i
+ 4];
330 #ifdef USBDEVFS_DISCONNECT
331 /* earlier Linux 2.4 do not support that */
333 struct usbdevfs_ioctl ctrl
;
334 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
335 ctrl
.ioctl_code
= USBDEVFS_DISCONNECT
;
336 ctrl
.ifno
= interface
;
337 ret
= ioctl(dev
->fd
, USBDEVFS_IOCTL
, &ctrl
);
338 if (ret
< 0 && errno
!= ENODATA
) {
339 perror("USBDEVFS_DISCONNECT");
346 /* XXX: only grab if all interfaces are free */
347 for (interface
= 0; interface
< nb_interfaces
; interface
++) {
348 ret
= ioctl(dev
->fd
, USBDEVFS_CLAIMINTERFACE
, &interface
);
350 if (errno
== EBUSY
) {
351 printf("husb: update iface. device already grabbed\n");
353 perror("husb: failed to claim interface");
360 printf("husb: %d interfaces claimed for configuration %d\n",
361 nb_interfaces
, configuration
);
363 dev
->ninterfaces
= nb_interfaces
;
364 dev
->configuration
= configuration
;
368 static int usb_host_release_interfaces(USBHostDevice
*s
)
372 DPRINTF("husb: releasing interfaces\n");
374 for (i
= 0; i
< s
->ninterfaces
; i
++) {
375 ret
= ioctl(s
->fd
, USBDEVFS_RELEASEINTERFACE
, &i
);
377 perror("husb: failed to release interface");
385 static void usb_host_handle_reset(USBDevice
*dev
)
387 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
389 DPRINTF("husb: reset device %u.%u\n", s
->bus_num
, s
->addr
);
391 ioctl(s
->fd
, USBDEVFS_RESET
);
393 usb_host_claim_interfaces(s
, s
->configuration
);
396 static void usb_host_handle_destroy(USBDevice
*dev
)
398 USBHostDevice
*s
= (USBHostDevice
*)dev
;
401 QTAILQ_REMOVE(&hostdevs
, s
, next
);
404 static int usb_linux_update_endp_table(USBHostDevice
*s
);
406 static int usb_host_handle_data(USBHostDevice
*s
, USBPacket
*p
)
408 struct usbdevfs_urb
*urb
;
412 aurb
= async_alloc();
418 if (p
->pid
== USB_TOKEN_IN
)
419 urb
->endpoint
= p
->devep
| 0x80;
421 urb
->endpoint
= p
->devep
;
423 if (is_halted(s
, p
->devep
)) {
424 ret
= ioctl(s
->fd
, USBDEVFS_CLEAR_HALT
, &urb
->endpoint
);
426 DPRINTF("husb: failed to clear halt. ep 0x%x errno %d\n",
427 urb
->endpoint
, errno
);
430 clear_halt(s
, p
->devep
);
433 urb
->buffer
= p
->data
;
434 urb
->buffer_length
= p
->len
;
436 if (is_isoc(s
, p
->devep
)) {
437 /* Setup ISOC transfer */
438 urb
->type
= USBDEVFS_URB_TYPE_ISO
;
439 urb
->flags
= USBDEVFS_URB_ISO_ASAP
;
440 urb
->number_of_packets
= 1;
441 urb
->iso_frame_desc
[0].length
= p
->len
;
443 /* Setup bulk transfer */
444 urb
->type
= USBDEVFS_URB_TYPE_BULK
;
447 urb
->usercontext
= s
;
449 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
451 DPRINTF("husb: data submit. ep 0x%x len %u aurb %p\n", urb
->endpoint
, p
->len
, aurb
);
454 DPRINTF("husb: submit failed. errno %d\n", errno
);
462 return USB_RET_STALL
;
466 usb_defer_packet(p
, async_cancel
, aurb
);
467 return USB_RET_ASYNC
;
470 static int ctrl_error(void)
472 if (errno
== ETIMEDOUT
)
475 return USB_RET_STALL
;
478 static int usb_host_set_address(USBHostDevice
*s
, int addr
)
480 DPRINTF("husb: ctrl set addr %u\n", addr
);
485 static int usb_host_set_config(USBHostDevice
*s
, int config
)
487 usb_host_release_interfaces(s
);
489 int ret
= ioctl(s
->fd
, USBDEVFS_SETCONFIGURATION
, &config
);
491 DPRINTF("husb: ctrl set config %d ret %d errno %d\n", config
, ret
, errno
);
496 usb_host_claim_interfaces(s
, config
);
500 static int usb_host_set_interface(USBHostDevice
*s
, int iface
, int alt
)
502 struct usbdevfs_setinterface si
;
505 si
.interface
= iface
;
507 ret
= ioctl(s
->fd
, USBDEVFS_SETINTERFACE
, &si
);
509 DPRINTF("husb: ctrl set iface %d altset %d ret %d errno %d\n",
510 iface
, alt
, ret
, errno
);
515 usb_linux_update_endp_table(s
);
519 static int usb_host_handle_control(USBHostDevice
*s
, USBPacket
*p
)
521 struct usbdevfs_urb
*urb
;
523 int ret
, value
, index
;
527 * Process certain standard device requests.
528 * These are infrequent and are processed synchronously.
530 value
= le16_to_cpu(s
->ctrl
.req
.wValue
);
531 index
= le16_to_cpu(s
->ctrl
.req
.wIndex
);
533 DPRINTF("husb: ctrl type 0x%x req 0x%x val 0x%x index %u len %u\n",
534 s
->ctrl
.req
.bRequestType
, s
->ctrl
.req
.bRequest
, value
, index
,
537 if (s
->ctrl
.req
.bRequestType
== 0) {
538 switch (s
->ctrl
.req
.bRequest
) {
539 case USB_REQ_SET_ADDRESS
:
540 return usb_host_set_address(s
, value
);
542 case USB_REQ_SET_CONFIGURATION
:
543 return usb_host_set_config(s
, value
& 0xff);
547 if (s
->ctrl
.req
.bRequestType
== 1 &&
548 s
->ctrl
.req
.bRequest
== USB_REQ_SET_INTERFACE
)
549 return usb_host_set_interface(s
, index
, value
);
551 /* The rest are asynchronous */
553 buffer_len
= 8 + s
->ctrl
.len
;
554 if (buffer_len
> sizeof(s
->ctrl
.buffer
)) {
555 fprintf(stderr
, "husb: ctrl buffer too small (%u > %zu)\n",
556 buffer_len
, sizeof(s
->ctrl
.buffer
));
557 return USB_RET_STALL
;
560 aurb
= async_alloc();
565 * Setup ctrl transfer.
567 * s->ctrl is layed out such that data buffer immediately follows
568 * 'req' struct which is exactly what usbdevfs expects.
572 urb
->type
= USBDEVFS_URB_TYPE_CONTROL
;
573 urb
->endpoint
= p
->devep
;
575 urb
->buffer
= &s
->ctrl
.req
;
576 urb
->buffer_length
= buffer_len
;
578 urb
->usercontext
= s
;
580 ret
= ioctl(s
->fd
, USBDEVFS_SUBMITURB
, urb
);
582 DPRINTF("husb: submit ctrl. len %u aurb %p\n", urb
->buffer_length
, aurb
);
585 DPRINTF("husb: submit failed. errno %d\n", errno
);
593 return USB_RET_STALL
;
597 usb_defer_packet(p
, async_cancel
, aurb
);
598 return USB_RET_ASYNC
;
601 static int do_token_setup(USBDevice
*dev
, USBPacket
*p
)
603 USBHostDevice
*s
= (USBHostDevice
*) dev
;
607 return USB_RET_STALL
;
609 memcpy(&s
->ctrl
.req
, p
->data
, 8);
610 s
->ctrl
.len
= le16_to_cpu(s
->ctrl
.req
.wLength
);
612 s
->ctrl
.state
= CTRL_STATE_SETUP
;
614 if (s
->ctrl
.req
.bRequestType
& USB_DIR_IN
) {
615 ret
= usb_host_handle_control(s
, p
);
619 if (ret
< s
->ctrl
.len
)
621 s
->ctrl
.state
= CTRL_STATE_DATA
;
623 if (s
->ctrl
.len
== 0)
624 s
->ctrl
.state
= CTRL_STATE_ACK
;
626 s
->ctrl
.state
= CTRL_STATE_DATA
;
632 static int do_token_in(USBDevice
*dev
, USBPacket
*p
)
634 USBHostDevice
*s
= (USBHostDevice
*) dev
;
638 return usb_host_handle_data(s
, p
);
640 switch(s
->ctrl
.state
) {
642 if (!(s
->ctrl
.req
.bRequestType
& USB_DIR_IN
)) {
643 ret
= usb_host_handle_control(s
, p
);
644 if (ret
== USB_RET_ASYNC
)
645 return USB_RET_ASYNC
;
647 s
->ctrl
.state
= CTRL_STATE_IDLE
;
648 return ret
> 0 ? 0 : ret
;
653 case CTRL_STATE_DATA
:
654 if (s
->ctrl
.req
.bRequestType
& USB_DIR_IN
) {
655 int len
= s
->ctrl
.len
- s
->ctrl
.offset
;
658 memcpy(p
->data
, s
->ctrl
.buffer
+ s
->ctrl
.offset
, len
);
659 s
->ctrl
.offset
+= len
;
660 if (s
->ctrl
.offset
>= s
->ctrl
.len
)
661 s
->ctrl
.state
= CTRL_STATE_ACK
;
665 s
->ctrl
.state
= CTRL_STATE_IDLE
;
666 return USB_RET_STALL
;
669 return USB_RET_STALL
;
673 static int do_token_out(USBDevice
*dev
, USBPacket
*p
)
675 USBHostDevice
*s
= (USBHostDevice
*) dev
;
678 return usb_host_handle_data(s
, p
);
680 switch(s
->ctrl
.state
) {
682 if (s
->ctrl
.req
.bRequestType
& USB_DIR_IN
) {
683 s
->ctrl
.state
= CTRL_STATE_IDLE
;
686 /* ignore additional output */
690 case CTRL_STATE_DATA
:
691 if (!(s
->ctrl
.req
.bRequestType
& USB_DIR_IN
)) {
692 int len
= s
->ctrl
.len
- s
->ctrl
.offset
;
695 memcpy(s
->ctrl
.buffer
+ s
->ctrl
.offset
, p
->data
, len
);
696 s
->ctrl
.offset
+= len
;
697 if (s
->ctrl
.offset
>= s
->ctrl
.len
)
698 s
->ctrl
.state
= CTRL_STATE_ACK
;
702 s
->ctrl
.state
= CTRL_STATE_IDLE
;
703 return USB_RET_STALL
;
706 return USB_RET_STALL
;
712 * Called by the HC (host controller).
714 * Returns length of the transaction or one of the USB_RET_XXX codes.
716 static int usb_host_handle_packet(USBDevice
*s
, USBPacket
*p
)
720 s
->state
= USB_STATE_ATTACHED
;
724 s
->state
= USB_STATE_NOTATTACHED
;
728 s
->remote_wakeup
= 0;
730 s
->state
= USB_STATE_DEFAULT
;
731 s
->info
->handle_reset(s
);
735 /* Rest of the PIDs must match our address */
736 if (s
->state
< USB_STATE_DEFAULT
|| p
->devaddr
!= s
->addr
)
737 return USB_RET_NODEV
;
740 case USB_TOKEN_SETUP
:
741 return do_token_setup(s
, p
);
744 return do_token_in(s
, p
);
747 return do_token_out(s
, p
);
750 return USB_RET_STALL
;
754 /* returns 1 on problem encountered or 0 for success */
755 static int usb_linux_update_endp_table(USBHostDevice
*s
)
757 uint8_t *descriptors
;
758 uint8_t devep
, type
, configuration
, alt_interface
;
759 struct usb_ctrltransfer ct
;
760 int interface
, ret
, length
, i
;
762 ct
.bRequestType
= USB_DIR_IN
;
763 ct
.bRequest
= USB_REQ_GET_CONFIGURATION
;
767 ct
.data
= &configuration
;
770 ret
= ioctl(s
->fd
, USBDEVFS_CONTROL
, &ct
);
772 perror("usb_linux_update_endp_table");
776 /* in address state */
777 if (configuration
== 0)
780 /* get the desired configuration, interface, and endpoint descriptors
781 * from device description */
782 descriptors
= &s
->descr
[18];
783 length
= s
->descr_len
- 18;
786 if (descriptors
[i
+ 1] != USB_DT_CONFIG
||
787 descriptors
[i
+ 5] != configuration
) {
788 DPRINTF("invalid descriptor data - configuration\n");
794 if (descriptors
[i
+ 1] != USB_DT_INTERFACE
||
795 (descriptors
[i
+ 1] == USB_DT_INTERFACE
&&
796 descriptors
[i
+ 4] == 0)) {
801 interface
= descriptors
[i
+ 2];
803 ct
.bRequestType
= USB_DIR_IN
| USB_RECIP_INTERFACE
;
804 ct
.bRequest
= USB_REQ_GET_INTERFACE
;
806 ct
.wIndex
= interface
;
808 ct
.data
= &alt_interface
;
811 ret
= ioctl(s
->fd
, USBDEVFS_CONTROL
, &ct
);
813 alt_interface
= interface
;
816 /* the current interface descriptor is the active interface
817 * and has endpoints */
818 if (descriptors
[i
+ 3] != alt_interface
) {
823 /* advance to the endpoints */
824 while (i
< length
&& descriptors
[i
+1] != USB_DT_ENDPOINT
)
831 if (descriptors
[i
+ 1] != USB_DT_ENDPOINT
)
834 devep
= descriptors
[i
+ 2];
835 switch (descriptors
[i
+ 3] & 0x3) {
837 type
= USBDEVFS_URB_TYPE_CONTROL
;
840 type
= USBDEVFS_URB_TYPE_ISO
;
843 type
= USBDEVFS_URB_TYPE_BULK
;
846 type
= USBDEVFS_URB_TYPE_INTERRUPT
;
849 DPRINTF("usb_host: malformed endpoint type\n");
850 type
= USBDEVFS_URB_TYPE_BULK
;
852 s
->endp_table
[(devep
& 0xf) - 1].type
= type
;
853 s
->endp_table
[(devep
& 0xf) - 1].halted
= 0;
861 static int usb_host_open(USBHostDevice
*dev
, int bus_num
,
862 int addr
, const char *prod_name
)
865 struct usbdevfs_connectinfo ci
;
871 printf("husb: open device %d.%d\n", bus_num
, addr
);
873 if (!usb_host_device_path
) {
874 perror("husb: USB Host Device Path not set");
877 snprintf(buf
, sizeof(buf
), "%s/%03d/%03d", usb_host_device_path
,
879 fd
= open(buf
, O_RDWR
| O_NONBLOCK
);
884 DPRINTF("husb: opened %s\n", buf
);
886 dev
->bus_num
= bus_num
;
890 /* read the device description */
891 dev
->descr_len
= read(fd
, dev
->descr
, sizeof(dev
->descr
));
892 if (dev
->descr_len
<= 0) {
893 perror("husb: reading device data failed");
900 printf("=== begin dumping device descriptor data ===\n");
901 for (x
= 0; x
< dev
->descr_len
; x
++)
902 printf("%02x ", dev
->descr
[x
]);
903 printf("\n=== end dumping device descriptor data ===\n");
909 * Initial configuration is -1 which makes us claim first
910 * available config. We used to start with 1, which does not
911 * always work. I've seen devices where first config starts
914 if (!usb_host_claim_interfaces(dev
, -1))
917 ret
= ioctl(fd
, USBDEVFS_CONNECTINFO
, &ci
);
919 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
923 printf("husb: grabbed usb device %d.%d\n", bus_num
, addr
);
925 ret
= usb_linux_update_endp_table(dev
);
930 dev
->dev
.speed
= USB_SPEED_LOW
;
932 dev
->dev
.speed
= USB_SPEED_HIGH
;
934 if (!prod_name
|| prod_name
[0] == '\0')
935 snprintf(dev
->dev
.product_desc
, sizeof(dev
->dev
.product_desc
),
936 "host:%d.%d", bus_num
, addr
);
938 pstrcpy(dev
->dev
.product_desc
, sizeof(dev
->dev
.product_desc
),
941 /* USB devio uses 'write' flag to check for async completions */
942 qemu_set_fd_handler(dev
->fd
, NULL
, async_complete
, dev
);
944 usb_device_attach(&dev
->dev
);
954 static int usb_host_close(USBHostDevice
*dev
)
959 qemu_set_fd_handler(dev
->fd
, NULL
, NULL
, NULL
);
963 usb_device_detach(&dev
->dev
);
969 static int usb_host_initfn(USBDevice
*dev
)
971 USBHostDevice
*s
= DO_UPCAST(USBHostDevice
, dev
, dev
);
973 dev
->auto_attach
= 0;
975 QTAILQ_INSERT_TAIL(&hostdevs
, s
, next
);
976 usb_host_auto_check(NULL
);
980 static struct USBDeviceInfo usb_host_dev_info
= {
981 .product_desc
= "USB Host Device",
982 .qdev
.name
= "usb-host",
983 .qdev
.size
= sizeof(USBHostDevice
),
984 .init
= usb_host_initfn
,
985 .handle_packet
= usb_host_handle_packet
,
986 .handle_reset
= usb_host_handle_reset
,
987 .handle_destroy
= usb_host_handle_destroy
,
988 .usbdevice_name
= "host",
989 .usbdevice_init
= usb_host_device_open
,
990 .qdev
.props
= (Property
[]) {
991 DEFINE_PROP_UINT32("hostbus", USBHostDevice
, match
.bus_num
, 0),
992 DEFINE_PROP_UINT32("hostaddr", USBHostDevice
, match
.addr
, 0),
993 DEFINE_PROP_HEX32("vendorid", USBHostDevice
, match
.vendor_id
, 0),
994 DEFINE_PROP_HEX32("productid", USBHostDevice
, match
.product_id
, 0),
995 DEFINE_PROP_END_OF_LIST(),
999 static void usb_host_register_devices(void)
1001 usb_qdev_register(&usb_host_dev_info
);
1003 device_init(usb_host_register_devices
)
1005 USBDevice
*usb_host_device_open(const char *devname
)
1007 struct USBAutoFilter filter
;
1011 dev
= usb_create(NULL
/* FIXME */, "usb-host");
1013 if (strstr(devname
, "auto:")) {
1014 if (parse_filter(devname
, &filter
) < 0)
1017 if ((p
= strchr(devname
, '.'))) {
1018 filter
.bus_num
= strtoul(devname
, NULL
, 0);
1019 filter
.addr
= strtoul(p
+ 1, NULL
, 0);
1020 filter
.vendor_id
= 0;
1021 filter
.product_id
= 0;
1022 } else if ((p
= strchr(devname
, ':'))) {
1025 filter
.vendor_id
= strtoul(devname
, NULL
, 16);
1026 filter
.product_id
= strtoul(p
+ 1, NULL
, 16);
1032 qdev_prop_set_uint32(&dev
->qdev
, "hostbus", filter
.bus_num
);
1033 qdev_prop_set_uint32(&dev
->qdev
, "hostaddr", filter
.addr
);
1034 qdev_prop_set_uint32(&dev
->qdev
, "vendorid", filter
.vendor_id
);
1035 qdev_prop_set_uint32(&dev
->qdev
, "productid", filter
.product_id
);
1036 qdev_init_nofail(&dev
->qdev
);
1040 qdev_free(&dev
->qdev
);
1044 int usb_host_device_close(const char *devname
)
1047 char product_name
[PRODUCT_NAME_SZ
];
1051 if (strstr(devname
, "auto:"))
1052 return usb_host_auto_del(devname
);
1054 if (usb_host_find_device(&bus_num
, &addr
, product_name
, sizeof(product_name
),
1058 s
= hostdev_find(bus_num
, addr
);
1060 usb_device_delete_addr(s
->bus_num
, s
->dev
.addr
);
1068 static int get_tag_value(char *buf
, int buf_size
,
1069 const char *str
, const char *tag
,
1070 const char *stopchars
)
1074 p
= strstr(str
, tag
);
1078 while (qemu_isspace(*p
))
1081 while (*p
!= '\0' && !strchr(stopchars
, *p
)) {
1082 if ((q
- buf
) < (buf_size
- 1))
1091 * Use /proc/bus/usb/devices or /dev/bus/usb/devices file to determine
1092 * host's USB devices. This is legacy support since many distributions
1093 * are moving to /sys/bus/usb
1095 static int usb_host_scan_dev(void *opaque
, USBScanFunc
*func
)
1100 int bus_num
, addr
, speed
, device_count
, class_id
, product_id
, vendor_id
;
1101 char product_name
[512];
1104 if (!usb_host_device_path
) {
1105 perror("husb: USB Host Device Path not set");
1108 snprintf(line
, sizeof(line
), "%s/devices", usb_host_device_path
);
1109 f
= fopen(line
, "r");
1111 perror("husb: cannot open devices file");
1116 bus_num
= addr
= speed
= class_id
= product_id
= vendor_id
= 0;
1118 if (fgets(line
, sizeof(line
), f
) == NULL
)
1120 if (strlen(line
) > 0)
1121 line
[strlen(line
) - 1] = '\0';
1122 if (line
[0] == 'T' && line
[1] == ':') {
1123 if (device_count
&& (vendor_id
|| product_id
)) {
1124 /* New device. Add the previously discovered device. */
1125 ret
= func(opaque
, bus_num
, addr
, class_id
, vendor_id
,
1126 product_id
, product_name
, speed
);
1130 if (get_tag_value(buf
, sizeof(buf
), line
, "Bus=", " ") < 0)
1132 bus_num
= atoi(buf
);
1133 if (get_tag_value(buf
, sizeof(buf
), line
, "Dev#=", " ") < 0)
1136 if (get_tag_value(buf
, sizeof(buf
), line
, "Spd=", " ") < 0)
1138 if (!strcmp(buf
, "480"))
1139 speed
= USB_SPEED_HIGH
;
1140 else if (!strcmp(buf
, "1.5"))
1141 speed
= USB_SPEED_LOW
;
1143 speed
= USB_SPEED_FULL
;
1144 product_name
[0] = '\0';
1149 } else if (line
[0] == 'P' && line
[1] == ':') {
1150 if (get_tag_value(buf
, sizeof(buf
), line
, "Vendor=", " ") < 0)
1152 vendor_id
= strtoul(buf
, NULL
, 16);
1153 if (get_tag_value(buf
, sizeof(buf
), line
, "ProdID=", " ") < 0)
1155 product_id
= strtoul(buf
, NULL
, 16);
1156 } else if (line
[0] == 'S' && line
[1] == ':') {
1157 if (get_tag_value(buf
, sizeof(buf
), line
, "Product=", "") < 0)
1159 pstrcpy(product_name
, sizeof(product_name
), buf
);
1160 } else if (line
[0] == 'D' && line
[1] == ':') {
1161 if (get_tag_value(buf
, sizeof(buf
), line
, "Cls=", " (") < 0)
1163 class_id
= strtoul(buf
, NULL
, 16);
1167 if (device_count
&& (vendor_id
|| product_id
)) {
1168 /* Add the last device. */
1169 ret
= func(opaque
, bus_num
, addr
, class_id
, vendor_id
,
1170 product_id
, product_name
, speed
);
1179 * Read sys file-system device file
1181 * @line address of buffer to put file contents in
1182 * @line_size size of line
1183 * @device_file path to device file (printf format string)
1184 * @device_name device being opened (inserted into device_file)
1186 * @return 0 failed, 1 succeeded ('line' contains data)
1188 static int usb_host_read_file(char *line
, size_t line_size
, const char *device_file
, const char *device_name
)
1192 char filename
[PATH_MAX
];
1194 snprintf(filename
, PATH_MAX
, USBSYSBUS_PATH
"/devices/%s/%s", device_name
,
1196 f
= fopen(filename
, "r");
1198 ret
= fgets(line
, line_size
, f
) != NULL
;
1206 * Use /sys/bus/usb/devices/ directory to determine host's USB
1209 * This code is based on Robert Schiele's original patches posted to
1210 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1212 static int usb_host_scan_sys(void *opaque
, USBScanFunc
*func
)
1216 int bus_num
, addr
, speed
, class_id
, product_id
, vendor_id
;
1218 char product_name
[512];
1221 dir
= opendir(USBSYSBUS_PATH
"/devices");
1223 perror("husb: cannot open devices directory");
1227 while ((de
= readdir(dir
))) {
1228 if (de
->d_name
[0] != '.' && !strchr(de
->d_name
, ':')) {
1229 char *tmpstr
= de
->d_name
;
1230 if (!strncmp(de
->d_name
, "usb", 3))
1232 bus_num
= atoi(tmpstr
);
1234 if (!usb_host_read_file(line
, sizeof(line
), "devnum", de
->d_name
))
1236 if (sscanf(line
, "%d", &addr
) != 1)
1239 if (!usb_host_read_file(line
, sizeof(line
), "bDeviceClass",
1242 if (sscanf(line
, "%x", &class_id
) != 1)
1245 if (!usb_host_read_file(line
, sizeof(line
), "idVendor", de
->d_name
))
1247 if (sscanf(line
, "%x", &vendor_id
) != 1)
1250 if (!usb_host_read_file(line
, sizeof(line
), "idProduct",
1253 if (sscanf(line
, "%x", &product_id
) != 1)
1256 if (!usb_host_read_file(line
, sizeof(line
), "product",
1260 if (strlen(line
) > 0)
1261 line
[strlen(line
) - 1] = '\0';
1262 pstrcpy(product_name
, sizeof(product_name
), line
);
1265 if (!usb_host_read_file(line
, sizeof(line
), "speed", de
->d_name
))
1267 if (!strcmp(line
, "480\n"))
1268 speed
= USB_SPEED_HIGH
;
1269 else if (!strcmp(line
, "1.5\n"))
1270 speed
= USB_SPEED_LOW
;
1272 speed
= USB_SPEED_FULL
;
1274 ret
= func(opaque
, bus_num
, addr
, class_id
, vendor_id
,
1275 product_id
, product_name
, speed
);
1287 * Determine how to access the host's USB devices and call the
1288 * specific support function.
1290 static int usb_host_scan(void *opaque
, USBScanFunc
*func
)
1292 Monitor
*mon
= cur_mon
;
1296 const char *fs_type
[] = {"unknown", "proc", "dev", "sys"};
1297 char devpath
[PATH_MAX
];
1299 /* only check the host once */
1301 dir
= opendir(USBSYSBUS_PATH
"/devices");
1303 /* devices found in /dev/bus/usb/ (yes - not a mistake!) */
1304 strcpy(devpath
, USBDEVBUS_PATH
);
1305 usb_fs_type
= USB_FS_SYS
;
1307 DPRINTF(USBDBG_DEVOPENED
, USBSYSBUS_PATH
);
1310 f
= fopen(USBPROCBUS_PATH
"/devices", "r");
1312 /* devices found in /proc/bus/usb/ */
1313 strcpy(devpath
, USBPROCBUS_PATH
);
1314 usb_fs_type
= USB_FS_PROC
;
1316 DPRINTF(USBDBG_DEVOPENED
, USBPROCBUS_PATH
);
1319 /* try additional methods if an access method hasn't been found yet */
1320 f
= fopen(USBDEVBUS_PATH
"/devices", "r");
1322 /* devices found in /dev/bus/usb/ */
1323 strcpy(devpath
, USBDEVBUS_PATH
);
1324 usb_fs_type
= USB_FS_DEV
;
1326 DPRINTF(USBDBG_DEVOPENED
, USBDEVBUS_PATH
);
1332 monitor_printf(mon
, "husb: unable to access USB devices\n");
1336 /* the module setting (used later for opening devices) */
1337 usb_host_device_path
= qemu_mallocz(strlen(devpath
)+1);
1338 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 static QEMUTimer
*usb_auto_timer
;
1361 static int usb_host_auto_scan(void *opaque
, int bus_num
, int addr
,
1362 int class_id
, int vendor_id
, int product_id
,
1363 const char *product_name
, int speed
)
1365 struct USBAutoFilter
*f
;
1366 struct USBHostDevice
*s
;
1372 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1375 if (f
->bus_num
> 0 && f
->bus_num
!= bus_num
)
1378 if (f
->addr
> 0 && f
->addr
!= addr
)
1381 if (f
->vendor_id
> 0 && f
->vendor_id
!= vendor_id
)
1384 if (f
->product_id
> 0 && f
->product_id
!= product_id
)
1387 /* We got a match */
1389 /* Already attached ? */
1393 DPRINTF("husb: auto open: bus_num %d addr %d\n", bus_num
, addr
);
1395 usb_host_open(s
, bus_num
, addr
, product_name
);
1401 static void usb_host_auto_check(void *unused
)
1403 struct USBHostDevice
*s
;
1404 int unconnected
= 0;
1406 usb_host_scan(NULL
, usb_host_auto_scan
);
1408 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1413 if (unconnected
== 0) {
1414 /* nothing to watch */
1416 qemu_del_timer(usb_auto_timer
);
1420 if (!usb_auto_timer
) {
1421 usb_auto_timer
= qemu_new_timer(rt_clock
, usb_host_auto_check
, NULL
);
1422 if (!usb_auto_timer
)
1425 qemu_mod_timer(usb_auto_timer
, qemu_get_clock(rt_clock
) + 2000);
1429 * Autoconnect filter
1431 * auto:bus:dev[:vid:pid]
1432 * auto:bus.dev[:vid:pid]
1434 * bus - bus number (dec, * means any)
1435 * dev - device number (dec, * means any)
1436 * vid - vendor id (hex, * means any)
1437 * pid - product id (hex, * means any)
1439 * See 'lsusb' output.
1441 static int parse_filter(const char *spec
, struct USBAutoFilter
*f
)
1443 enum { BUS
, DEV
, VID
, PID
, DONE
};
1444 const char *p
= spec
;
1452 for (i
= BUS
; i
< DONE
; i
++) {
1453 p
= strpbrk(p
, ":.");
1461 case BUS
: f
->bus_num
= strtol(p
, NULL
, 10); break;
1462 case DEV
: f
->addr
= strtol(p
, NULL
, 10); break;
1463 case VID
: f
->vendor_id
= strtol(p
, NULL
, 16); break;
1464 case PID
: f
->product_id
= strtol(p
, NULL
, 16); break;
1469 fprintf(stderr
, "husb: invalid auto filter spec %s\n", spec
);
1476 /**********************/
1477 /* USB host device info */
1479 struct usb_class_info
{
1481 const char *class_name
;
1484 static const struct usb_class_info usb_class_info
[] = {
1485 { USB_CLASS_AUDIO
, "Audio"},
1486 { USB_CLASS_COMM
, "Communication"},
1487 { USB_CLASS_HID
, "HID"},
1488 { USB_CLASS_HUB
, "Hub" },
1489 { USB_CLASS_PHYSICAL
, "Physical" },
1490 { USB_CLASS_PRINTER
, "Printer" },
1491 { USB_CLASS_MASS_STORAGE
, "Storage" },
1492 { USB_CLASS_CDC_DATA
, "Data" },
1493 { USB_CLASS_APP_SPEC
, "Application Specific" },
1494 { USB_CLASS_VENDOR_SPEC
, "Vendor Specific" },
1495 { USB_CLASS_STILL_IMAGE
, "Still Image" },
1496 { USB_CLASS_CSCID
, "Smart Card" },
1497 { USB_CLASS_CONTENT_SEC
, "Content Security" },
1501 static const char *usb_class_str(uint8_t class)
1503 const struct usb_class_info
*p
;
1504 for(p
= usb_class_info
; p
->class != -1; p
++) {
1505 if (p
->class == class)
1508 return p
->class_name
;
1511 static void usb_info_device(Monitor
*mon
, int bus_num
, int addr
, int class_id
,
1512 int vendor_id
, int product_id
,
1513 const char *product_name
,
1516 const char *class_str
, *speed_str
;
1522 case USB_SPEED_FULL
:
1525 case USB_SPEED_HIGH
:
1533 monitor_printf(mon
, " Device %d.%d, speed %s Mb/s\n",
1534 bus_num
, addr
, speed_str
);
1535 class_str
= usb_class_str(class_id
);
1537 monitor_printf(mon
, " %s:", class_str
);
1539 monitor_printf(mon
, " Class %02x:", class_id
);
1540 monitor_printf(mon
, " USB device %04x:%04x", vendor_id
, product_id
);
1541 if (product_name
[0] != '\0')
1542 monitor_printf(mon
, ", %s", product_name
);
1543 monitor_printf(mon
, "\n");
1546 static int usb_host_info_device(void *opaque
, int bus_num
, int addr
,
1548 int vendor_id
, int product_id
,
1549 const char *product_name
,
1552 Monitor
*mon
= opaque
;
1554 usb_info_device(mon
, bus_num
, addr
, class_id
, vendor_id
, product_id
,
1555 product_name
, speed
);
1559 static void dec2str(int val
, char *str
, size_t size
)
1562 snprintf(str
, size
, "*");
1564 snprintf(str
, size
, "%d", val
);
1567 static void hex2str(int val
, char *str
, size_t size
)
1570 snprintf(str
, size
, "*");
1572 snprintf(str
, size
, "%04x", val
);
1575 void usb_host_info(Monitor
*mon
)
1577 struct USBAutoFilter
*f
;
1578 struct USBHostDevice
*s
;
1580 usb_host_scan(mon
, usb_host_info_device
);
1582 if (QTAILQ_EMPTY(&hostdevs
))
1584 monitor_printf(mon
, " Auto filters:\n");
1585 QTAILQ_FOREACH(s
, &hostdevs
, next
) {
1586 char bus
[10], addr
[10], vid
[10], pid
[10];
1588 dec2str(f
->bus_num
, bus
, sizeof(bus
));
1589 dec2str(f
->addr
, addr
, sizeof(addr
));
1590 hex2str(f
->vendor_id
, vid
, sizeof(vid
));
1591 hex2str(f
->product_id
, pid
, sizeof(pid
));
1592 monitor_printf(mon
, " Device %s.%s ID %s:%s\n",
1593 bus
, addr
, vid
, pid
);