kvm: qemu: propagate errors on failed migration.
[kvm-userspace.git] / qemu / usb-linux.c
blob26643bd5898f3c5ad2c169733f0ae5c5bfde8ac4
1 /*
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
30 * THE SOFTWARE.
33 #include "qemu-common.h"
34 #include "qemu-timer.h"
35 #include "monitor.h"
37 #if defined(__linux__)
38 #define __user
39 #endif
41 #include <dirent.h>
42 #include <sys/ioctl.h>
43 #include <signal.h>
45 #include <linux/usbdevice_fs.h>
46 #include <linux/version.h>
47 #include "hw/usb.h"
49 /* We redefine it to avoid version problems */
50 struct usb_ctrltransfer {
51 uint8_t bRequestType;
52 uint8_t bRequest;
53 uint16_t wValue;
54 uint16_t wIndex;
55 uint16_t wLength;
56 uint32_t timeout;
57 void *data;
60 struct usb_ctrlrequest {
61 uint8_t bRequestType;
62 uint8_t bRequest;
63 uint16_t wValue;
64 uint16_t wIndex;
65 uint16_t wLength;
68 typedef int USBScanFunc(void *opaque, int bus_num, int addr, int class_id,
69 int vendor_id, int product_id,
70 const char *product_name, int speed);
71 static int usb_host_find_device(int *pbus_num, int *paddr,
72 char *product_name, int product_name_size,
73 const char *devname);
74 //#define DEBUG
76 #ifdef DEBUG
77 #define dprintf printf
78 #else
79 #define dprintf(...)
80 #endif
82 #define USBDBG_DEVOPENED "husb: opened %s/devices\n"
84 #define USBPROCBUS_PATH "/proc/bus/usb"
85 #define PRODUCT_NAME_SZ 32
86 #define MAX_ENDPOINTS 16
87 #define USBDEVBUS_PATH "/dev/bus/usb"
88 #define USBSYSBUS_PATH "/sys/bus/usb"
90 static char *usb_host_device_path;
92 #define USB_FS_NONE 0
93 #define USB_FS_PROC 1
94 #define USB_FS_DEV 2
95 #define USB_FS_SYS 3
97 static int usb_fs_type;
99 /* endpoint association data */
100 struct endp_data {
101 uint8_t type;
102 uint8_t halted;
105 enum {
106 CTRL_STATE_IDLE = 0,
107 CTRL_STATE_SETUP,
108 CTRL_STATE_DATA,
109 CTRL_STATE_ACK
113 * Control transfer state.
114 * Note that 'buffer' _must_ follow 'req' field because
115 * we need contigious buffer when we submit control URB.
117 struct ctrl_struct {
118 uint16_t len;
119 uint16_t offset;
120 uint8_t state;
121 struct usb_ctrlrequest req;
122 uint8_t buffer[1024];
125 typedef struct USBHostDevice {
126 USBDevice dev;
127 int fd;
129 uint8_t descr[1024];
130 int descr_len;
131 int configuration;
132 int ninterfaces;
133 int closing;
135 struct ctrl_struct ctrl;
136 struct endp_data endp_table[MAX_ENDPOINTS];
138 /* Host side address */
139 int bus_num;
140 int addr;
142 struct USBHostDevice *next;
143 } USBHostDevice;
145 static int is_isoc(USBHostDevice *s, int ep)
147 return s->endp_table[ep - 1].type == USBDEVFS_URB_TYPE_ISO;
150 static int is_halted(USBHostDevice *s, int ep)
152 return s->endp_table[ep - 1].halted;
155 static void clear_halt(USBHostDevice *s, int ep)
157 s->endp_table[ep - 1].halted = 0;
160 static void set_halt(USBHostDevice *s, int ep)
162 s->endp_table[ep - 1].halted = 1;
165 static USBHostDevice *hostdev_list;
167 static void hostdev_link(USBHostDevice *dev)
169 dev->next = hostdev_list;
170 hostdev_list = dev;
173 static void hostdev_unlink(USBHostDevice *dev)
175 USBHostDevice *pdev = hostdev_list;
176 USBHostDevice **prev = &hostdev_list;
178 while (pdev) {
179 if (pdev == dev) {
180 *prev = dev->next;
181 return;
184 prev = &pdev->next;
185 pdev = pdev->next;
189 static USBHostDevice *hostdev_find(int bus_num, int addr)
191 USBHostDevice *s = hostdev_list;
192 while (s) {
193 if (s->bus_num == bus_num && s->addr == addr)
194 return s;
195 s = s->next;
197 return NULL;
201 * Async URB state.
202 * We always allocate one isoc descriptor even for bulk transfers
203 * to simplify allocation and casts.
205 typedef struct AsyncURB
207 struct usbdevfs_urb urb;
208 struct usbdevfs_iso_packet_desc isocpd;
210 USBPacket *packet;
211 USBHostDevice *hdev;
212 } AsyncURB;
214 static AsyncURB *async_alloc(void)
216 return (AsyncURB *) qemu_mallocz(sizeof(AsyncURB));
219 static void async_free(AsyncURB *aurb)
221 qemu_free(aurb);
224 static void async_complete_ctrl(USBHostDevice *s, USBPacket *p)
226 switch(s->ctrl.state) {
227 case CTRL_STATE_SETUP:
228 if (p->len < s->ctrl.len)
229 s->ctrl.len = p->len;
230 s->ctrl.state = CTRL_STATE_DATA;
231 p->len = 8;
232 break;
234 case CTRL_STATE_ACK:
235 s->ctrl.state = CTRL_STATE_IDLE;
236 p->len = 0;
237 break;
239 default:
240 break;
244 static void async_complete(void *opaque)
246 USBHostDevice *s = opaque;
247 AsyncURB *aurb;
249 while (1) {
250 USBPacket *p;
252 int r = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &aurb);
253 if (r < 0) {
254 if (errno == EAGAIN)
255 return;
257 if (errno == ENODEV && !s->closing) {
258 printf("husb: device %d.%d disconnected\n", s->bus_num, s->addr);
259 usb_device_del_addr(0, s->dev.addr);
260 return;
263 dprintf("husb: async. reap urb failed errno %d\n", errno);
264 return;
267 p = aurb->packet;
269 dprintf("husb: async completed. aurb %p status %d alen %d\n",
270 aurb, aurb->urb.status, aurb->urb.actual_length);
272 if (p) {
273 switch (aurb->urb.status) {
274 case 0:
275 p->len = aurb->urb.actual_length;
276 if (aurb->urb.type == USBDEVFS_URB_TYPE_CONTROL)
277 async_complete_ctrl(s, p);
278 break;
280 case -EPIPE:
281 set_halt(s, p->devep);
282 /* fall through */
283 default:
284 p->len = USB_RET_NAK;
285 break;
288 usb_packet_complete(p);
291 async_free(aurb);
295 static void async_cancel(USBPacket *unused, void *opaque)
297 AsyncURB *aurb = opaque;
298 USBHostDevice *s = aurb->hdev;
300 dprintf("husb: async cancel. aurb %p\n", aurb);
302 /* Mark it as dead (see async_complete above) */
303 aurb->packet = NULL;
305 int r = ioctl(s->fd, USBDEVFS_DISCARDURB, aurb);
306 if (r < 0) {
307 dprintf("husb: async. discard urb failed errno %d\n", errno);
311 static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration)
313 int dev_descr_len, config_descr_len;
314 int interface, nb_interfaces, nb_configurations;
315 int ret, i;
317 if (configuration == 0) /* address state - ignore */
318 return 1;
320 dprintf("husb: claiming interfaces. config %d\n", configuration);
322 i = 0;
323 dev_descr_len = dev->descr[0];
324 if (dev_descr_len > dev->descr_len)
325 goto fail;
326 nb_configurations = dev->descr[17];
328 i += dev_descr_len;
329 while (i < dev->descr_len) {
330 dprintf("husb: i is %d, descr_len is %d, dl %d, dt %d\n", i, dev->descr_len,
331 dev->descr[i], dev->descr[i+1]);
333 if (dev->descr[i+1] != USB_DT_CONFIG) {
334 i += dev->descr[i];
335 continue;
337 config_descr_len = dev->descr[i];
339 printf("husb: config #%d need %d\n", dev->descr[i + 5], configuration);
341 if (configuration < 0 || configuration == dev->descr[i + 5]) {
342 configuration = dev->descr[i + 5];
343 break;
346 i += config_descr_len;
349 if (i >= dev->descr_len) {
350 fprintf(stderr, "husb: update iface failed. no matching configuration\n");
351 goto fail;
353 nb_interfaces = dev->descr[i + 4];
355 #ifdef USBDEVFS_DISCONNECT
356 /* earlier Linux 2.4 do not support that */
358 struct usbdevfs_ioctl ctrl;
359 for (interface = 0; interface < nb_interfaces; interface++) {
360 ctrl.ioctl_code = USBDEVFS_DISCONNECT;
361 ctrl.ifno = interface;
362 ret = ioctl(dev->fd, USBDEVFS_IOCTL, &ctrl);
363 if (ret < 0 && errno != ENODATA) {
364 perror("USBDEVFS_DISCONNECT");
365 goto fail;
369 #endif
371 /* XXX: only grab if all interfaces are free */
372 for (interface = 0; interface < nb_interfaces; interface++) {
373 ret = ioctl(dev->fd, USBDEVFS_CLAIMINTERFACE, &interface);
374 if (ret < 0) {
375 if (errno == EBUSY) {
376 printf("husb: update iface. device already grabbed\n");
377 } else {
378 perror("husb: failed to claim interface");
380 fail:
381 return 0;
385 printf("husb: %d interfaces claimed for configuration %d\n",
386 nb_interfaces, configuration);
388 dev->ninterfaces = nb_interfaces;
389 dev->configuration = configuration;
390 return 1;
393 static int usb_host_release_interfaces(USBHostDevice *s)
395 int ret, i;
397 dprintf("husb: releasing interfaces\n");
399 for (i = 0; i < s->ninterfaces; i++) {
400 ret = ioctl(s->fd, USBDEVFS_RELEASEINTERFACE, &i);
401 if (ret < 0) {
402 perror("husb: failed to release interface");
403 return 0;
407 return 1;
410 static void usb_host_handle_reset(USBDevice *dev)
412 USBHostDevice *s = (USBHostDevice *) dev;
414 dprintf("husb: reset device %u.%u\n", s->bus_num, s->addr);
416 ioctl(s->fd, USBDEVFS_RESET);
418 usb_host_claim_interfaces(s, s->configuration);
421 static void usb_host_handle_destroy(USBDevice *dev)
423 USBHostDevice *s = (USBHostDevice *)dev;
425 s->closing = 1;
427 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
429 hostdev_unlink(s);
431 async_complete(s);
433 if (s->fd >= 0)
434 close(s->fd);
436 qemu_free(s);
439 static int usb_linux_update_endp_table(USBHostDevice *s);
441 static int usb_host_handle_data(USBHostDevice *s, USBPacket *p)
443 struct usbdevfs_urb *urb;
444 AsyncURB *aurb;
445 int ret;
447 aurb = async_alloc();
448 aurb->hdev = s;
449 aurb->packet = p;
451 urb = &aurb->urb;
453 if (p->pid == USB_TOKEN_IN)
454 urb->endpoint = p->devep | 0x80;
455 else
456 urb->endpoint = p->devep;
458 if (is_halted(s, p->devep)) {
459 ret = ioctl(s->fd, USBDEVFS_CLEAR_HALT, &urb->endpoint);
460 if (ret < 0) {
461 dprintf("husb: failed to clear halt. ep 0x%x errno %d\n",
462 urb->endpoint, errno);
463 return USB_RET_NAK;
465 clear_halt(s, p->devep);
468 urb->buffer = p->data;
469 urb->buffer_length = p->len;
471 if (is_isoc(s, p->devep)) {
472 /* Setup ISOC transfer */
473 urb->type = USBDEVFS_URB_TYPE_ISO;
474 urb->flags = USBDEVFS_URB_ISO_ASAP;
475 urb->number_of_packets = 1;
476 urb->iso_frame_desc[0].length = p->len;
477 } else {
478 /* Setup bulk transfer */
479 urb->type = USBDEVFS_URB_TYPE_BULK;
482 urb->usercontext = s;
484 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
486 dprintf("husb: data submit. ep 0x%x len %u aurb %p\n", urb->endpoint, p->len, aurb);
488 if (ret < 0) {
489 dprintf("husb: submit failed. errno %d\n", errno);
490 async_free(aurb);
492 switch(errno) {
493 case ETIMEDOUT:
494 return USB_RET_NAK;
495 case EPIPE:
496 default:
497 return USB_RET_STALL;
501 usb_defer_packet(p, async_cancel, aurb);
502 return USB_RET_ASYNC;
505 static int ctrl_error(void)
507 if (errno == ETIMEDOUT)
508 return USB_RET_NAK;
509 else
510 return USB_RET_STALL;
513 static int usb_host_set_address(USBHostDevice *s, int addr)
515 dprintf("husb: ctrl set addr %u\n", addr);
516 s->dev.addr = addr;
517 return 0;
520 static int usb_host_set_config(USBHostDevice *s, int config)
522 usb_host_release_interfaces(s);
524 int ret = ioctl(s->fd, USBDEVFS_SETCONFIGURATION, &config);
526 dprintf("husb: ctrl set config %d ret %d errno %d\n", config, ret, errno);
528 if (ret < 0)
529 return ctrl_error();
531 usb_host_claim_interfaces(s, config);
532 return 0;
535 static int usb_host_set_interface(USBHostDevice *s, int iface, int alt)
537 struct usbdevfs_setinterface si;
538 int ret;
540 si.interface = iface;
541 si.altsetting = alt;
542 ret = ioctl(s->fd, USBDEVFS_SETINTERFACE, &si);
544 dprintf("husb: ctrl set iface %d altset %d ret %d errno %d\n",
545 iface, alt, ret, errno);
547 if (ret < 0)
548 return ctrl_error();
550 usb_linux_update_endp_table(s);
551 return 0;
554 static int usb_host_handle_control(USBHostDevice *s, USBPacket *p)
556 struct usbdevfs_urb *urb;
557 AsyncURB *aurb;
558 int ret, value, index;
561 * Process certain standard device requests.
562 * These are infrequent and are processed synchronously.
564 value = le16_to_cpu(s->ctrl.req.wValue);
565 index = le16_to_cpu(s->ctrl.req.wIndex);
567 dprintf("husb: ctrl type 0x%x req 0x%x val 0x%x index %u len %u\n",
568 s->ctrl.req.bRequestType, s->ctrl.req.bRequest, value, index,
569 s->ctrl.len);
571 if (s->ctrl.req.bRequestType == 0) {
572 switch (s->ctrl.req.bRequest) {
573 case USB_REQ_SET_ADDRESS:
574 return usb_host_set_address(s, value);
576 case USB_REQ_SET_CONFIGURATION:
577 return usb_host_set_config(s, value & 0xff);
581 if (s->ctrl.req.bRequestType == 1 &&
582 s->ctrl.req.bRequest == USB_REQ_SET_INTERFACE)
583 return usb_host_set_interface(s, index, value);
585 /* The rest are asynchronous */
587 aurb = async_alloc();
588 aurb->hdev = s;
589 aurb->packet = p;
592 * Setup ctrl transfer.
594 * s->ctrl is layed out such that data buffer immediately follows
595 * 'req' struct which is exactly what usbdevfs expects.
597 urb = &aurb->urb;
599 urb->type = USBDEVFS_URB_TYPE_CONTROL;
600 urb->endpoint = p->devep;
602 urb->buffer = &s->ctrl.req;
603 urb->buffer_length = 8 + s->ctrl.len;
605 urb->usercontext = s;
607 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
609 dprintf("husb: submit ctrl. len %u aurb %p\n", urb->buffer_length, aurb);
611 if (ret < 0) {
612 dprintf("husb: submit failed. errno %d\n", errno);
613 async_free(aurb);
615 switch(errno) {
616 case ETIMEDOUT:
617 return USB_RET_NAK;
618 case EPIPE:
619 default:
620 return USB_RET_STALL;
624 usb_defer_packet(p, async_cancel, aurb);
625 return USB_RET_ASYNC;
628 static int do_token_setup(USBDevice *dev, USBPacket *p)
630 USBHostDevice *s = (USBHostDevice *) dev;
631 int ret = 0;
633 if (p->len != 8)
634 return USB_RET_STALL;
636 memcpy(&s->ctrl.req, p->data, 8);
637 s->ctrl.len = le16_to_cpu(s->ctrl.req.wLength);
638 s->ctrl.offset = 0;
639 s->ctrl.state = CTRL_STATE_SETUP;
641 if (s->ctrl.req.bRequestType & USB_DIR_IN) {
642 ret = usb_host_handle_control(s, p);
643 if (ret < 0)
644 return ret;
646 if (ret < s->ctrl.len)
647 s->ctrl.len = ret;
648 s->ctrl.state = CTRL_STATE_DATA;
649 } else {
650 if (s->ctrl.len == 0)
651 s->ctrl.state = CTRL_STATE_ACK;
652 else
653 s->ctrl.state = CTRL_STATE_DATA;
656 return ret;
659 static int do_token_in(USBDevice *dev, USBPacket *p)
661 USBHostDevice *s = (USBHostDevice *) dev;
662 int ret = 0;
664 if (p->devep != 0)
665 return usb_host_handle_data(s, p);
667 switch(s->ctrl.state) {
668 case CTRL_STATE_ACK:
669 if (!(s->ctrl.req.bRequestType & USB_DIR_IN)) {
670 ret = usb_host_handle_control(s, p);
671 if (ret == USB_RET_ASYNC)
672 return USB_RET_ASYNC;
674 s->ctrl.state = CTRL_STATE_IDLE;
675 return ret > 0 ? 0 : ret;
678 return 0;
680 case CTRL_STATE_DATA:
681 if (s->ctrl.req.bRequestType & USB_DIR_IN) {
682 int len = s->ctrl.len - s->ctrl.offset;
683 if (len > p->len)
684 len = p->len;
685 memcpy(p->data, s->ctrl.buffer + s->ctrl.offset, len);
686 s->ctrl.offset += len;
687 if (s->ctrl.offset >= s->ctrl.len)
688 s->ctrl.state = CTRL_STATE_ACK;
689 return len;
692 s->ctrl.state = CTRL_STATE_IDLE;
693 return USB_RET_STALL;
695 default:
696 return USB_RET_STALL;
700 static int do_token_out(USBDevice *dev, USBPacket *p)
702 USBHostDevice *s = (USBHostDevice *) dev;
704 if (p->devep != 0)
705 return usb_host_handle_data(s, p);
707 switch(s->ctrl.state) {
708 case CTRL_STATE_ACK:
709 if (s->ctrl.req.bRequestType & USB_DIR_IN) {
710 s->ctrl.state = CTRL_STATE_IDLE;
711 /* transfer OK */
712 } else {
713 /* ignore additional output */
715 return 0;
717 case CTRL_STATE_DATA:
718 if (!(s->ctrl.req.bRequestType & USB_DIR_IN)) {
719 int len = s->ctrl.len - s->ctrl.offset;
720 if (len > p->len)
721 len = p->len;
722 memcpy(s->ctrl.buffer + s->ctrl.offset, p->data, len);
723 s->ctrl.offset += len;
724 if (s->ctrl.offset >= s->ctrl.len)
725 s->ctrl.state = CTRL_STATE_ACK;
726 return len;
729 s->ctrl.state = CTRL_STATE_IDLE;
730 return USB_RET_STALL;
732 default:
733 return USB_RET_STALL;
738 * Packet handler.
739 * Called by the HC (host controller).
741 * Returns length of the transaction or one of the USB_RET_XXX codes.
743 static int usb_host_handle_packet(USBDevice *s, USBPacket *p)
745 switch(p->pid) {
746 case USB_MSG_ATTACH:
747 s->state = USB_STATE_ATTACHED;
748 return 0;
750 case USB_MSG_DETACH:
751 s->state = USB_STATE_NOTATTACHED;
752 return 0;
754 case USB_MSG_RESET:
755 s->remote_wakeup = 0;
756 s->addr = 0;
757 s->state = USB_STATE_DEFAULT;
758 s->handle_reset(s);
759 return 0;
762 /* Rest of the PIDs must match our address */
763 if (s->state < USB_STATE_DEFAULT || p->devaddr != s->addr)
764 return USB_RET_NODEV;
766 switch (p->pid) {
767 case USB_TOKEN_SETUP:
768 return do_token_setup(s, p);
770 case USB_TOKEN_IN:
771 return do_token_in(s, p);
773 case USB_TOKEN_OUT:
774 return do_token_out(s, p);
776 default:
777 return USB_RET_STALL;
781 /* returns 1 on problem encountered or 0 for success */
782 static int usb_linux_update_endp_table(USBHostDevice *s)
784 uint8_t *descriptors;
785 uint8_t devep, type, configuration, alt_interface;
786 struct usb_ctrltransfer ct;
787 int interface, ret, length, i;
789 ct.bRequestType = USB_DIR_IN;
790 ct.bRequest = USB_REQ_GET_CONFIGURATION;
791 ct.wValue = 0;
792 ct.wIndex = 0;
793 ct.wLength = 1;
794 ct.data = &configuration;
795 ct.timeout = 50;
797 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
798 if (ret < 0) {
799 perror("usb_linux_update_endp_table");
800 return 1;
803 /* in address state */
804 if (configuration == 0)
805 return 1;
807 /* get the desired configuration, interface, and endpoint descriptors
808 * from device description */
809 descriptors = &s->descr[18];
810 length = s->descr_len - 18;
811 i = 0;
813 if (descriptors[i + 1] != USB_DT_CONFIG ||
814 descriptors[i + 5] != configuration) {
815 dprintf("invalid descriptor data - configuration\n");
816 return 1;
818 i += descriptors[i];
820 while (i < length) {
821 if (descriptors[i + 1] != USB_DT_INTERFACE ||
822 (descriptors[i + 1] == USB_DT_INTERFACE &&
823 descriptors[i + 4] == 0)) {
824 i += descriptors[i];
825 continue;
828 interface = descriptors[i + 2];
830 ct.bRequestType = USB_DIR_IN | USB_RECIP_INTERFACE;
831 ct.bRequest = USB_REQ_GET_INTERFACE;
832 ct.wValue = 0;
833 ct.wIndex = interface;
834 ct.wLength = 1;
835 ct.data = &alt_interface;
836 ct.timeout = 50;
838 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
839 if (ret < 0) {
840 perror("usb_linux_update_endp_table");
841 return 1;
844 /* the current interface descriptor is the active interface
845 * and has endpoints */
846 if (descriptors[i + 3] != alt_interface) {
847 i += descriptors[i];
848 continue;
851 /* advance to the endpoints */
852 while (i < length && descriptors[i +1] != USB_DT_ENDPOINT)
853 i += descriptors[i];
855 if (i >= length)
856 break;
858 while (i < length) {
859 if (descriptors[i + 1] != USB_DT_ENDPOINT)
860 break;
862 devep = descriptors[i + 2];
863 switch (descriptors[i + 3] & 0x3) {
864 case 0x00:
865 type = USBDEVFS_URB_TYPE_CONTROL;
866 break;
867 case 0x01:
868 type = USBDEVFS_URB_TYPE_ISO;
869 break;
870 case 0x02:
871 type = USBDEVFS_URB_TYPE_BULK;
872 break;
873 case 0x03:
874 type = USBDEVFS_URB_TYPE_INTERRUPT;
875 break;
876 default:
877 dprintf("usb_host: malformed endpoint type\n");
878 type = USBDEVFS_URB_TYPE_BULK;
880 s->endp_table[(devep & 0xf) - 1].type = type;
881 s->endp_table[(devep & 0xf) - 1].halted = 0;
883 i += descriptors[i];
886 return 0;
889 static USBDevice *usb_host_device_open_addr(int bus_num, int addr, const char *prod_name)
891 int fd = -1, ret;
892 USBHostDevice *dev = NULL;
893 struct usbdevfs_connectinfo ci;
894 char buf[1024];
896 dev = qemu_mallocz(sizeof(USBHostDevice));
898 dev->bus_num = bus_num;
899 dev->addr = addr;
901 printf("husb: open device %d.%d\n", bus_num, addr);
903 if (!usb_host_device_path) {
904 perror("husb: USB Host Device Path not set");
905 goto fail;
907 snprintf(buf, sizeof(buf), "%s/%03d/%03d", usb_host_device_path,
908 bus_num, addr);
909 fd = open(buf, O_RDWR | O_NONBLOCK);
910 if (fd < 0) {
911 perror(buf);
912 goto fail;
914 dprintf("husb: opened %s\n", buf);
916 /* read the device description */
917 dev->descr_len = read(fd, dev->descr, sizeof(dev->descr));
918 if (dev->descr_len <= 0) {
919 perror("husb: reading device data failed");
920 goto fail;
923 #ifdef DEBUG
925 int x;
926 printf("=== begin dumping device descriptor data ===\n");
927 for (x = 0; x < dev->descr_len; x++)
928 printf("%02x ", dev->descr[x]);
929 printf("\n=== end dumping device descriptor data ===\n");
931 #endif
933 dev->fd = fd;
936 * Initial configuration is -1 which makes us claim first
937 * available config. We used to start with 1, which does not
938 * always work. I've seen devices where first config starts
939 * with 2.
941 if (!usb_host_claim_interfaces(dev, -1))
942 goto fail;
944 ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci);
945 if (ret < 0) {
946 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
947 goto fail;
950 printf("husb: grabbed usb device %d.%d\n", bus_num, addr);
952 ret = usb_linux_update_endp_table(dev);
953 if (ret)
954 goto fail;
956 if (ci.slow)
957 dev->dev.speed = USB_SPEED_LOW;
958 else
959 dev->dev.speed = USB_SPEED_HIGH;
961 dev->dev.handle_packet = usb_host_handle_packet;
962 dev->dev.handle_reset = usb_host_handle_reset;
963 dev->dev.handle_destroy = usb_host_handle_destroy;
965 if (!prod_name || prod_name[0] == '\0')
966 snprintf(dev->dev.devname, sizeof(dev->dev.devname),
967 "host:%d.%d", bus_num, addr);
968 else
969 pstrcpy(dev->dev.devname, sizeof(dev->dev.devname),
970 prod_name);
972 /* USB devio uses 'write' flag to check for async completions */
973 qemu_set_fd_handler(dev->fd, NULL, async_complete, dev);
975 hostdev_link(dev);
977 return (USBDevice *) dev;
979 fail:
980 if (dev)
981 qemu_free(dev);
983 close(fd);
984 return NULL;
987 static int usb_host_auto_add(const char *spec);
988 static int usb_host_auto_del(const char *spec);
990 USBDevice *usb_host_device_open(const char *devname)
992 Monitor *mon = cur_mon;
993 int bus_num, addr;
994 char product_name[PRODUCT_NAME_SZ];
996 if (strstr(devname, "auto:")) {
997 usb_host_auto_add(devname);
998 return NULL;
1001 if (usb_host_find_device(&bus_num, &addr, product_name, sizeof(product_name),
1002 devname) < 0)
1003 return NULL;
1005 if (hostdev_find(bus_num, addr)) {
1006 monitor_printf(mon, "husb: host usb device %d.%d is already open\n",
1007 bus_num, addr);
1008 return NULL;
1011 return usb_host_device_open_addr(bus_num, addr, product_name);
1014 int usb_host_device_close(const char *devname)
1016 char product_name[PRODUCT_NAME_SZ];
1017 int bus_num, addr;
1018 USBHostDevice *s;
1020 if (strstr(devname, "auto:"))
1021 return usb_host_auto_del(devname);
1023 if (usb_host_find_device(&bus_num, &addr, product_name, sizeof(product_name),
1024 devname) < 0)
1025 return -1;
1027 s = hostdev_find(bus_num, addr);
1028 if (s) {
1029 usb_device_del_addr(0, s->dev.addr);
1030 return 0;
1033 return -1;
1036 static int get_tag_value(char *buf, int buf_size,
1037 const char *str, const char *tag,
1038 const char *stopchars)
1040 const char *p;
1041 char *q;
1042 p = strstr(str, tag);
1043 if (!p)
1044 return -1;
1045 p += strlen(tag);
1046 while (qemu_isspace(*p))
1047 p++;
1048 q = buf;
1049 while (*p != '\0' && !strchr(stopchars, *p)) {
1050 if ((q - buf) < (buf_size - 1))
1051 *q++ = *p;
1052 p++;
1054 *q = '\0';
1055 return q - buf;
1059 * Use /proc/bus/usb/devices or /dev/bus/usb/devices file to determine
1060 * host's USB devices. This is legacy support since many distributions
1061 * are moving to /sys/bus/usb
1063 static int usb_host_scan_dev(void *opaque, USBScanFunc *func)
1065 FILE *f = 0;
1066 char line[1024];
1067 char buf[1024];
1068 int bus_num, addr, speed, device_count, class_id, product_id, vendor_id;
1069 char product_name[512];
1070 int ret = 0;
1072 if (!usb_host_device_path) {
1073 perror("husb: USB Host Device Path not set");
1074 goto the_end;
1076 snprintf(line, sizeof(line), "%s/devices", usb_host_device_path);
1077 f = fopen(line, "r");
1078 if (!f) {
1079 perror("husb: cannot open devices file");
1080 goto the_end;
1083 device_count = 0;
1084 bus_num = addr = speed = class_id = product_id = vendor_id = 0;
1085 for(;;) {
1086 if (fgets(line, sizeof(line), f) == NULL)
1087 break;
1088 if (strlen(line) > 0)
1089 line[strlen(line) - 1] = '\0';
1090 if (line[0] == 'T' && line[1] == ':') {
1091 if (device_count && (vendor_id || product_id)) {
1092 /* New device. Add the previously discovered device. */
1093 ret = func(opaque, bus_num, addr, class_id, vendor_id,
1094 product_id, product_name, speed);
1095 if (ret)
1096 goto the_end;
1098 if (get_tag_value(buf, sizeof(buf), line, "Bus=", " ") < 0)
1099 goto fail;
1100 bus_num = atoi(buf);
1101 if (get_tag_value(buf, sizeof(buf), line, "Dev#=", " ") < 0)
1102 goto fail;
1103 addr = atoi(buf);
1104 if (get_tag_value(buf, sizeof(buf), line, "Spd=", " ") < 0)
1105 goto fail;
1106 if (!strcmp(buf, "480"))
1107 speed = USB_SPEED_HIGH;
1108 else if (!strcmp(buf, "1.5"))
1109 speed = USB_SPEED_LOW;
1110 else
1111 speed = USB_SPEED_FULL;
1112 product_name[0] = '\0';
1113 class_id = 0xff;
1114 device_count++;
1115 product_id = 0;
1116 vendor_id = 0;
1117 } else if (line[0] == 'P' && line[1] == ':') {
1118 if (get_tag_value(buf, sizeof(buf), line, "Vendor=", " ") < 0)
1119 goto fail;
1120 vendor_id = strtoul(buf, NULL, 16);
1121 if (get_tag_value(buf, sizeof(buf), line, "ProdID=", " ") < 0)
1122 goto fail;
1123 product_id = strtoul(buf, NULL, 16);
1124 } else if (line[0] == 'S' && line[1] == ':') {
1125 if (get_tag_value(buf, sizeof(buf), line, "Product=", "") < 0)
1126 goto fail;
1127 pstrcpy(product_name, sizeof(product_name), buf);
1128 } else if (line[0] == 'D' && line[1] == ':') {
1129 if (get_tag_value(buf, sizeof(buf), line, "Cls=", " (") < 0)
1130 goto fail;
1131 class_id = strtoul(buf, NULL, 16);
1133 fail: ;
1135 if (device_count && (vendor_id || product_id)) {
1136 /* Add the last device. */
1137 ret = func(opaque, bus_num, addr, class_id, vendor_id,
1138 product_id, product_name, speed);
1140 the_end:
1141 if (f)
1142 fclose(f);
1143 return ret;
1147 * Read sys file-system device file
1149 * @line address of buffer to put file contents in
1150 * @line_size size of line
1151 * @device_file path to device file (printf format string)
1152 * @device_name device being opened (inserted into device_file)
1154 * @return 0 failed, 1 succeeded ('line' contains data)
1156 static int usb_host_read_file(char *line, size_t line_size, const char *device_file, const char *device_name)
1158 Monitor *mon = cur_mon;
1159 FILE *f;
1160 int ret = 0;
1161 char filename[PATH_MAX];
1163 snprintf(filename, PATH_MAX, USBSYSBUS_PATH "/devices/%s/%s", device_name,
1164 device_file);
1165 f = fopen(filename, "r");
1166 if (f) {
1167 fgets(line, line_size, f);
1168 fclose(f);
1169 ret = 1;
1170 } else {
1171 monitor_printf(mon, "husb: could not open %s\n", filename);
1174 return ret;
1178 * Use /sys/bus/usb/devices/ directory to determine host's USB
1179 * devices.
1181 * This code is based on Robert Schiele's original patches posted to
1182 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1184 static int usb_host_scan_sys(void *opaque, USBScanFunc *func)
1186 DIR *dir = 0;
1187 char line[1024];
1188 int bus_num, addr, speed, class_id, product_id, vendor_id;
1189 int ret = 0;
1190 char product_name[512];
1191 struct dirent *de;
1193 dir = opendir(USBSYSBUS_PATH "/devices");
1194 if (!dir) {
1195 perror("husb: cannot open devices directory");
1196 goto the_end;
1199 while ((de = readdir(dir))) {
1200 if (de->d_name[0] != '.' && !strchr(de->d_name, ':')) {
1201 char *tmpstr = de->d_name;
1202 if (!strncmp(de->d_name, "usb", 3))
1203 tmpstr += 3;
1204 bus_num = atoi(tmpstr);
1206 if (!usb_host_read_file(line, sizeof(line), "devnum", de->d_name))
1207 goto the_end;
1208 if (sscanf(line, "%d", &addr) != 1)
1209 goto the_end;
1211 if (!usb_host_read_file(line, sizeof(line), "bDeviceClass",
1212 de->d_name))
1213 goto the_end;
1214 if (sscanf(line, "%x", &class_id) != 1)
1215 goto the_end;
1217 if (!usb_host_read_file(line, sizeof(line), "idVendor", de->d_name))
1218 goto the_end;
1219 if (sscanf(line, "%x", &vendor_id) != 1)
1220 goto the_end;
1222 if (!usb_host_read_file(line, sizeof(line), "idProduct",
1223 de->d_name))
1224 goto the_end;
1225 if (sscanf(line, "%x", &product_id) != 1)
1226 goto the_end;
1228 if (!usb_host_read_file(line, sizeof(line), "product",
1229 de->d_name)) {
1230 *product_name = 0;
1231 } else {
1232 if (strlen(line) > 0)
1233 line[strlen(line) - 1] = '\0';
1234 pstrcpy(product_name, sizeof(product_name), line);
1237 if (!usb_host_read_file(line, sizeof(line), "speed", de->d_name))
1238 goto the_end;
1239 if (!strcmp(line, "480\n"))
1240 speed = USB_SPEED_HIGH;
1241 else if (!strcmp(line, "1.5\n"))
1242 speed = USB_SPEED_LOW;
1243 else
1244 speed = USB_SPEED_FULL;
1246 ret = func(opaque, bus_num, addr, class_id, vendor_id,
1247 product_id, product_name, speed);
1248 if (ret)
1249 goto the_end;
1252 the_end:
1253 if (dir)
1254 closedir(dir);
1255 return ret;
1259 * Determine how to access the host's USB devices and call the
1260 * specific support function.
1262 static int usb_host_scan(void *opaque, USBScanFunc *func)
1264 Monitor *mon = cur_mon;
1265 FILE *f = 0;
1266 DIR *dir = 0;
1267 int ret = 0;
1268 const char *fs_type[] = {"unknown", "proc", "dev", "sys"};
1269 char devpath[PATH_MAX];
1271 /* only check the host once */
1272 if (!usb_fs_type) {
1273 f = fopen(USBPROCBUS_PATH "/devices", "r");
1274 if (f) {
1275 /* devices found in /proc/bus/usb/ */
1276 strcpy(devpath, USBPROCBUS_PATH);
1277 usb_fs_type = USB_FS_PROC;
1278 fclose(f);
1279 dprintf(USBDBG_DEVOPENED, USBPROCBUS_PATH);
1280 goto found_devices;
1282 /* try additional methods if an access method hasn't been found yet */
1283 f = fopen(USBDEVBUS_PATH "/devices", "r");
1284 if (f) {
1285 /* devices found in /dev/bus/usb/ */
1286 strcpy(devpath, USBDEVBUS_PATH);
1287 usb_fs_type = USB_FS_DEV;
1288 fclose(f);
1289 dprintf(USBDBG_DEVOPENED, USBDEVBUS_PATH);
1290 goto found_devices;
1292 dir = opendir(USBSYSBUS_PATH "/devices");
1293 if (dir) {
1294 /* devices found in /dev/bus/usb/ (yes - not a mistake!) */
1295 strcpy(devpath, USBDEVBUS_PATH);
1296 usb_fs_type = USB_FS_SYS;
1297 closedir(dir);
1298 dprintf(USBDBG_DEVOPENED, USBSYSBUS_PATH);
1299 goto found_devices;
1301 found_devices:
1302 if (!usb_fs_type) {
1303 monitor_printf(mon, "husb: unable to access USB devices\n");
1304 return -ENOENT;
1307 /* the module setting (used later for opening devices) */
1308 usb_host_device_path = qemu_mallocz(strlen(devpath)+1);
1309 strcpy(usb_host_device_path, devpath);
1310 monitor_printf(mon, "husb: using %s file-system with %s\n",
1311 fs_type[usb_fs_type], usb_host_device_path);
1314 switch (usb_fs_type) {
1315 case USB_FS_PROC:
1316 case USB_FS_DEV:
1317 ret = usb_host_scan_dev(opaque, func);
1318 break;
1319 case USB_FS_SYS:
1320 ret = usb_host_scan_sys(opaque, func);
1321 break;
1322 default:
1323 ret = -EINVAL;
1324 break;
1326 return ret;
1329 struct USBAutoFilter {
1330 struct USBAutoFilter *next;
1331 int bus_num;
1332 int addr;
1333 int vendor_id;
1334 int product_id;
1337 static QEMUTimer *usb_auto_timer;
1338 static struct USBAutoFilter *usb_auto_filter;
1340 static int usb_host_auto_scan(void *opaque, int bus_num, int addr,
1341 int class_id, int vendor_id, int product_id,
1342 const char *product_name, int speed)
1344 struct USBAutoFilter *f;
1345 struct USBDevice *dev;
1347 /* Ignore hubs */
1348 if (class_id == 9)
1349 return 0;
1351 for (f = usb_auto_filter; f; f = f->next) {
1352 if (f->bus_num >= 0 && f->bus_num != bus_num)
1353 continue;
1355 if (f->addr >= 0 && f->addr != addr)
1356 continue;
1358 if (f->vendor_id >= 0 && f->vendor_id != vendor_id)
1359 continue;
1361 if (f->product_id >= 0 && f->product_id != product_id)
1362 continue;
1364 /* We got a match */
1366 /* Allredy attached ? */
1367 if (hostdev_find(bus_num, addr))
1368 return 0;
1370 dprintf("husb: auto open: bus_num %d addr %d\n", bus_num, addr);
1372 dev = usb_host_device_open_addr(bus_num, addr, product_name);
1373 if (dev)
1374 usb_device_add_dev(dev);
1377 return 0;
1380 static void usb_host_auto_timer(void *unused)
1382 usb_host_scan(NULL, usb_host_auto_scan);
1383 qemu_mod_timer(usb_auto_timer, qemu_get_clock(rt_clock) + 2000);
1387 * Autoconnect filter
1388 * Format:
1389 * auto:bus:dev[:vid:pid]
1390 * auto:bus.dev[:vid:pid]
1392 * bus - bus number (dec, * means any)
1393 * dev - device number (dec, * means any)
1394 * vid - vendor id (hex, * means any)
1395 * pid - product id (hex, * means any)
1397 * See 'lsusb' output.
1399 static int parse_filter(const char *spec, struct USBAutoFilter *f)
1401 enum { BUS, DEV, VID, PID, DONE };
1402 const char *p = spec;
1403 int i;
1405 f->bus_num = -1;
1406 f->addr = -1;
1407 f->vendor_id = -1;
1408 f->product_id = -1;
1410 for (i = BUS; i < DONE; i++) {
1411 p = strpbrk(p, ":.");
1412 if (!p) break;
1413 p++;
1415 if (*p == '*')
1416 continue;
1418 switch(i) {
1419 case BUS: f->bus_num = strtol(p, NULL, 10); break;
1420 case DEV: f->addr = strtol(p, NULL, 10); break;
1421 case VID: f->vendor_id = strtol(p, NULL, 16); break;
1422 case PID: f->product_id = strtol(p, NULL, 16); break;
1426 if (i < DEV) {
1427 fprintf(stderr, "husb: invalid auto filter spec %s\n", spec);
1428 return -1;
1431 return 0;
1434 static int match_filter(const struct USBAutoFilter *f1,
1435 const struct USBAutoFilter *f2)
1437 return f1->bus_num == f2->bus_num &&
1438 f1->addr == f2->addr &&
1439 f1->vendor_id == f2->vendor_id &&
1440 f1->product_id == f2->product_id;
1443 static int usb_host_auto_add(const char *spec)
1445 struct USBAutoFilter filter, *f;
1447 if (parse_filter(spec, &filter) < 0)
1448 return -1;
1450 f = qemu_mallocz(sizeof(*f));
1452 *f = filter;
1454 if (!usb_auto_filter) {
1456 * First entry. Init and start the monitor.
1457 * Right now we're using timer to check for new devices.
1458 * If this turns out to be too expensive we can move that into a
1459 * separate thread.
1461 usb_auto_timer = qemu_new_timer(rt_clock, usb_host_auto_timer, NULL);
1462 if (!usb_auto_timer) {
1463 fprintf(stderr, "husb: failed to allocate auto scan timer\n");
1464 qemu_free(f);
1465 return -1;
1468 /* Check for new devices every two seconds */
1469 qemu_mod_timer(usb_auto_timer, qemu_get_clock(rt_clock) + 2000);
1472 dprintf("husb: added auto filter: bus_num %d addr %d vid %d pid %d\n",
1473 f->bus_num, f->addr, f->vendor_id, f->product_id);
1475 f->next = usb_auto_filter;
1476 usb_auto_filter = f;
1478 return 0;
1481 static int usb_host_auto_del(const char *spec)
1483 struct USBAutoFilter *pf = usb_auto_filter;
1484 struct USBAutoFilter **prev = &usb_auto_filter;
1485 struct USBAutoFilter filter;
1487 if (parse_filter(spec, &filter) < 0)
1488 return -1;
1490 while (pf) {
1491 if (match_filter(pf, &filter)) {
1492 dprintf("husb: removed auto filter: bus_num %d addr %d vid %d pid %d\n",
1493 pf->bus_num, pf->addr, pf->vendor_id, pf->product_id);
1495 *prev = pf->next;
1497 if (!usb_auto_filter) {
1498 /* No more filters. Stop scanning. */
1499 qemu_del_timer(usb_auto_timer);
1500 qemu_free_timer(usb_auto_timer);
1503 return 0;
1506 prev = &pf->next;
1507 pf = pf->next;
1510 return -1;
1513 typedef struct FindDeviceState {
1514 int vendor_id;
1515 int product_id;
1516 int bus_num;
1517 int addr;
1518 char product_name[PRODUCT_NAME_SZ];
1519 } FindDeviceState;
1521 static int usb_host_find_device_scan(void *opaque, int bus_num, int addr,
1522 int class_id,
1523 int vendor_id, int product_id,
1524 const char *product_name, int speed)
1526 FindDeviceState *s = opaque;
1527 if ((vendor_id == s->vendor_id &&
1528 product_id == s->product_id) ||
1529 (bus_num == s->bus_num &&
1530 addr == s->addr)) {
1531 pstrcpy(s->product_name, PRODUCT_NAME_SZ, product_name);
1532 s->bus_num = bus_num;
1533 s->addr = addr;
1534 return 1;
1535 } else {
1536 return 0;
1540 /* the syntax is :
1541 'bus.addr' (decimal numbers) or
1542 'vendor_id:product_id' (hexa numbers) */
1543 static int usb_host_find_device(int *pbus_num, int *paddr,
1544 char *product_name, int product_name_size,
1545 const char *devname)
1547 const char *p;
1548 int ret;
1549 FindDeviceState fs;
1551 p = strchr(devname, '.');
1552 if (p) {
1553 *pbus_num = strtoul(devname, NULL, 0);
1554 *paddr = strtoul(p + 1, NULL, 0);
1555 fs.bus_num = *pbus_num;
1556 fs.addr = *paddr;
1557 ret = usb_host_scan(&fs, usb_host_find_device_scan);
1558 if (ret)
1559 pstrcpy(product_name, product_name_size, fs.product_name);
1560 return 0;
1563 p = strchr(devname, ':');
1564 if (p) {
1565 fs.vendor_id = strtoul(devname, NULL, 16);
1566 fs.product_id = strtoul(p + 1, NULL, 16);
1567 ret = usb_host_scan(&fs, usb_host_find_device_scan);
1568 if (ret) {
1569 *pbus_num = fs.bus_num;
1570 *paddr = fs.addr;
1571 pstrcpy(product_name, product_name_size, fs.product_name);
1572 return 0;
1575 return -1;
1578 /**********************/
1579 /* USB host device info */
1581 struct usb_class_info {
1582 int class;
1583 const char *class_name;
1586 static const struct usb_class_info usb_class_info[] = {
1587 { USB_CLASS_AUDIO, "Audio"},
1588 { USB_CLASS_COMM, "Communication"},
1589 { USB_CLASS_HID, "HID"},
1590 { USB_CLASS_HUB, "Hub" },
1591 { USB_CLASS_PHYSICAL, "Physical" },
1592 { USB_CLASS_PRINTER, "Printer" },
1593 { USB_CLASS_MASS_STORAGE, "Storage" },
1594 { USB_CLASS_CDC_DATA, "Data" },
1595 { USB_CLASS_APP_SPEC, "Application Specific" },
1596 { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
1597 { USB_CLASS_STILL_IMAGE, "Still Image" },
1598 { USB_CLASS_CSCID, "Smart Card" },
1599 { USB_CLASS_CONTENT_SEC, "Content Security" },
1600 { -1, NULL }
1603 static const char *usb_class_str(uint8_t class)
1605 const struct usb_class_info *p;
1606 for(p = usb_class_info; p->class != -1; p++) {
1607 if (p->class == class)
1608 break;
1610 return p->class_name;
1613 static void usb_info_device(int bus_num, int addr, int class_id,
1614 int vendor_id, int product_id,
1615 const char *product_name,
1616 int speed)
1618 Monitor *mon = cur_mon;
1619 const char *class_str, *speed_str;
1621 switch(speed) {
1622 case USB_SPEED_LOW:
1623 speed_str = "1.5";
1624 break;
1625 case USB_SPEED_FULL:
1626 speed_str = "12";
1627 break;
1628 case USB_SPEED_HIGH:
1629 speed_str = "480";
1630 break;
1631 default:
1632 speed_str = "?";
1633 break;
1636 monitor_printf(mon, " Device %d.%d, speed %s Mb/s\n",
1637 bus_num, addr, speed_str);
1638 class_str = usb_class_str(class_id);
1639 if (class_str)
1640 monitor_printf(mon, " %s:", class_str);
1641 else
1642 monitor_printf(mon, " Class %02x:", class_id);
1643 monitor_printf(mon, " USB device %04x:%04x", vendor_id, product_id);
1644 if (product_name[0] != '\0')
1645 monitor_printf(mon, ", %s", product_name);
1646 monitor_printf(mon, "\n");
1649 static int usb_host_info_device(void *opaque, int bus_num, int addr,
1650 int class_id,
1651 int vendor_id, int product_id,
1652 const char *product_name,
1653 int speed)
1655 usb_info_device(bus_num, addr, class_id, vendor_id, product_id,
1656 product_name, speed);
1657 return 0;
1660 static void dec2str(int val, char *str, size_t size)
1662 if (val == -1)
1663 snprintf(str, size, "*");
1664 else
1665 snprintf(str, size, "%d", val);
1668 static void hex2str(int val, char *str, size_t size)
1670 if (val == -1)
1671 snprintf(str, size, "*");
1672 else
1673 snprintf(str, size, "%x", val);
1676 void usb_host_info(Monitor *mon)
1678 struct USBAutoFilter *f;
1680 usb_host_scan(NULL, usb_host_info_device);
1682 if (usb_auto_filter)
1683 monitor_printf(mon, " 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 monitor_printf(mon, " Device %s.%s ID %s:%s\n",
1691 bus, addr, vid, pid);