qdev: drop iobase properties from isa bus
[qemu.git] / usb-linux.c
blob01d145d1737b912ec66161c10de57b8f0ff245ec
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 #include <dirent.h>
38 #include <sys/ioctl.h>
39 #include <signal.h>
41 #include <linux/usbdevice_fs.h>
42 #include <linux/version.h>
43 #include "hw/usb.h"
45 /* We redefine it to avoid version problems */
46 struct usb_ctrltransfer {
47 uint8_t bRequestType;
48 uint8_t bRequest;
49 uint16_t wValue;
50 uint16_t wIndex;
51 uint16_t wLength;
52 uint32_t timeout;
53 void *data;
56 struct usb_ctrlrequest {
57 uint8_t bRequestType;
58 uint8_t bRequest;
59 uint16_t wValue;
60 uint16_t wIndex;
61 uint16_t wLength;
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,
69 const char *devname);
70 //#define DEBUG
72 #ifdef DEBUG
73 #define dprintf printf
74 #else
75 #define dprintf(...)
76 #endif
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;
88 #define USB_FS_NONE 0
89 #define USB_FS_PROC 1
90 #define USB_FS_DEV 2
91 #define USB_FS_SYS 3
93 static int usb_fs_type;
95 /* endpoint association data */
96 struct endp_data {
97 uint8_t type;
98 uint8_t halted;
101 enum {
102 CTRL_STATE_IDLE = 0,
103 CTRL_STATE_SETUP,
104 CTRL_STATE_DATA,
105 CTRL_STATE_ACK
109 * Control transfer state.
110 * Note that 'buffer' _must_ follow 'req' field because
111 * we need contigious buffer when we submit control URB.
113 struct ctrl_struct {
114 uint16_t len;
115 uint16_t offset;
116 uint8_t state;
117 struct usb_ctrlrequest req;
118 uint8_t buffer[1024];
121 typedef struct USBHostDevice {
122 USBDevice dev;
123 int fd;
125 uint8_t descr[1024];
126 int descr_len;
127 int configuration;
128 int ninterfaces;
129 int closing;
131 struct ctrl_struct ctrl;
132 struct endp_data endp_table[MAX_ENDPOINTS];
134 /* Host side address */
135 int bus_num;
136 int addr;
138 struct USBHostDevice *next;
139 } USBHostDevice;
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;
166 hostdev_list = dev;
169 static void hostdev_unlink(USBHostDevice *dev)
171 USBHostDevice *pdev = hostdev_list;
172 USBHostDevice **prev = &hostdev_list;
174 while (pdev) {
175 if (pdev == dev) {
176 *prev = dev->next;
177 return;
180 prev = &pdev->next;
181 pdev = pdev->next;
185 static USBHostDevice *hostdev_find(int bus_num, int addr)
187 USBHostDevice *s = hostdev_list;
188 while (s) {
189 if (s->bus_num == bus_num && s->addr == addr)
190 return s;
191 s = s->next;
193 return NULL;
197 * Async URB state.
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;
206 USBPacket *packet;
207 USBHostDevice *hdev;
208 } AsyncURB;
210 static AsyncURB *async_alloc(void)
212 return (AsyncURB *) qemu_mallocz(sizeof(AsyncURB));
215 static void async_free(AsyncURB *aurb)
217 qemu_free(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;
227 p->len = 8;
228 break;
230 case CTRL_STATE_ACK:
231 s->ctrl.state = CTRL_STATE_IDLE;
232 p->len = 0;
233 break;
235 default:
236 break;
240 static void async_complete(void *opaque)
242 USBHostDevice *s = opaque;
243 AsyncURB *aurb;
245 while (1) {
246 USBPacket *p;
248 int r = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &aurb);
249 if (r < 0) {
250 if (errno == EAGAIN)
251 return;
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);
256 return;
259 dprintf("husb: async. reap urb failed errno %d\n", errno);
260 return;
263 p = aurb->packet;
265 dprintf("husb: async completed. aurb %p status %d alen %d\n",
266 aurb, aurb->urb.status, aurb->urb.actual_length);
268 if (p) {
269 switch (aurb->urb.status) {
270 case 0:
271 p->len = aurb->urb.actual_length;
272 if (aurb->urb.type == USBDEVFS_URB_TYPE_CONTROL)
273 async_complete_ctrl(s, p);
274 break;
276 case -EPIPE:
277 set_halt(s, p->devep);
278 /* fall through */
279 default:
280 p->len = USB_RET_NAK;
281 break;
284 usb_packet_complete(p);
287 async_free(aurb);
291 static void async_cancel(USBPacket *unused, void *opaque)
293 AsyncURB *aurb = opaque;
294 USBHostDevice *s = aurb->hdev;
296 dprintf("husb: async cancel. aurb %p\n", aurb);
298 /* Mark it as dead (see async_complete above) */
299 aurb->packet = NULL;
301 int r = ioctl(s->fd, USBDEVFS_DISCARDURB, aurb);
302 if (r < 0) {
303 dprintf("husb: async. discard urb failed errno %d\n", errno);
307 static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration)
309 int dev_descr_len, config_descr_len;
310 int interface, nb_interfaces, nb_configurations;
311 int ret, i;
313 if (configuration == 0) /* address state - ignore */
314 return 1;
316 dprintf("husb: claiming interfaces. config %d\n", configuration);
318 i = 0;
319 dev_descr_len = dev->descr[0];
320 if (dev_descr_len > dev->descr_len)
321 goto fail;
322 nb_configurations = dev->descr[17];
324 i += dev_descr_len;
325 while (i < dev->descr_len) {
326 dprintf("husb: i is %d, descr_len is %d, dl %d, dt %d\n", i, dev->descr_len,
327 dev->descr[i], dev->descr[i+1]);
329 if (dev->descr[i+1] != USB_DT_CONFIG) {
330 i += dev->descr[i];
331 continue;
333 config_descr_len = dev->descr[i];
335 printf("husb: config #%d need %d\n", dev->descr[i + 5], configuration);
337 if (configuration < 0 || configuration == dev->descr[i + 5]) {
338 configuration = dev->descr[i + 5];
339 break;
342 i += config_descr_len;
345 if (i >= dev->descr_len) {
346 fprintf(stderr, "husb: update iface failed. no matching configuration\n");
347 goto fail;
349 nb_interfaces = dev->descr[i + 4];
351 #ifdef USBDEVFS_DISCONNECT
352 /* earlier Linux 2.4 do not support that */
354 struct usbdevfs_ioctl ctrl;
355 for (interface = 0; interface < nb_interfaces; interface++) {
356 ctrl.ioctl_code = USBDEVFS_DISCONNECT;
357 ctrl.ifno = interface;
358 ret = ioctl(dev->fd, USBDEVFS_IOCTL, &ctrl);
359 if (ret < 0 && errno != ENODATA) {
360 perror("USBDEVFS_DISCONNECT");
361 goto fail;
365 #endif
367 /* XXX: only grab if all interfaces are free */
368 for (interface = 0; interface < nb_interfaces; interface++) {
369 ret = ioctl(dev->fd, USBDEVFS_CLAIMINTERFACE, &interface);
370 if (ret < 0) {
371 if (errno == EBUSY) {
372 printf("husb: update iface. device already grabbed\n");
373 } else {
374 perror("husb: failed to claim interface");
376 fail:
377 return 0;
381 printf("husb: %d interfaces claimed for configuration %d\n",
382 nb_interfaces, configuration);
384 dev->ninterfaces = nb_interfaces;
385 dev->configuration = configuration;
386 return 1;
389 static int usb_host_release_interfaces(USBHostDevice *s)
391 int ret, i;
393 dprintf("husb: releasing interfaces\n");
395 for (i = 0; i < s->ninterfaces; i++) {
396 ret = ioctl(s->fd, USBDEVFS_RELEASEINTERFACE, &i);
397 if (ret < 0) {
398 perror("husb: failed to release interface");
399 return 0;
403 return 1;
406 static void usb_host_handle_reset(USBDevice *dev)
408 USBHostDevice *s = (USBHostDevice *) dev;
410 dprintf("husb: reset device %u.%u\n", s->bus_num, s->addr);
412 ioctl(s->fd, USBDEVFS_RESET);
414 usb_host_claim_interfaces(s, s->configuration);
417 static void usb_host_handle_destroy(USBDevice *dev)
419 USBHostDevice *s = (USBHostDevice *)dev;
421 s->closing = 1;
423 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
425 hostdev_unlink(s);
427 async_complete(s);
429 if (s->fd >= 0)
430 close(s->fd);
432 qemu_free(s);
435 static int usb_linux_update_endp_table(USBHostDevice *s);
437 static int usb_host_handle_data(USBHostDevice *s, USBPacket *p)
439 struct usbdevfs_urb *urb;
440 AsyncURB *aurb;
441 int ret;
443 aurb = async_alloc();
444 aurb->hdev = s;
445 aurb->packet = p;
447 urb = &aurb->urb;
449 if (p->pid == USB_TOKEN_IN)
450 urb->endpoint = p->devep | 0x80;
451 else
452 urb->endpoint = p->devep;
454 if (is_halted(s, p->devep)) {
455 ret = ioctl(s->fd, USBDEVFS_CLEAR_HALT, &urb->endpoint);
456 if (ret < 0) {
457 dprintf("husb: failed to clear halt. ep 0x%x errno %d\n",
458 urb->endpoint, errno);
459 return USB_RET_NAK;
461 clear_halt(s, p->devep);
464 urb->buffer = p->data;
465 urb->buffer_length = p->len;
467 if (is_isoc(s, p->devep)) {
468 /* Setup ISOC transfer */
469 urb->type = USBDEVFS_URB_TYPE_ISO;
470 urb->flags = USBDEVFS_URB_ISO_ASAP;
471 urb->number_of_packets = 1;
472 urb->iso_frame_desc[0].length = p->len;
473 } else {
474 /* Setup bulk transfer */
475 urb->type = USBDEVFS_URB_TYPE_BULK;
478 urb->usercontext = s;
480 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
482 dprintf("husb: data submit. ep 0x%x len %u aurb %p\n", urb->endpoint, p->len, aurb);
484 if (ret < 0) {
485 dprintf("husb: submit failed. errno %d\n", errno);
486 async_free(aurb);
488 switch(errno) {
489 case ETIMEDOUT:
490 return USB_RET_NAK;
491 case EPIPE:
492 default:
493 return USB_RET_STALL;
497 usb_defer_packet(p, async_cancel, aurb);
498 return USB_RET_ASYNC;
501 static int ctrl_error(void)
503 if (errno == ETIMEDOUT)
504 return USB_RET_NAK;
505 else
506 return USB_RET_STALL;
509 static int usb_host_set_address(USBHostDevice *s, int addr)
511 dprintf("husb: ctrl set addr %u\n", addr);
512 s->dev.addr = addr;
513 return 0;
516 static int usb_host_set_config(USBHostDevice *s, int config)
518 usb_host_release_interfaces(s);
520 int ret = ioctl(s->fd, USBDEVFS_SETCONFIGURATION, &config);
522 dprintf("husb: ctrl set config %d ret %d errno %d\n", config, ret, errno);
524 if (ret < 0)
525 return ctrl_error();
527 usb_host_claim_interfaces(s, config);
528 return 0;
531 static int usb_host_set_interface(USBHostDevice *s, int iface, int alt)
533 struct usbdevfs_setinterface si;
534 int ret;
536 si.interface = iface;
537 si.altsetting = alt;
538 ret = ioctl(s->fd, USBDEVFS_SETINTERFACE, &si);
540 dprintf("husb: ctrl set iface %d altset %d ret %d errno %d\n",
541 iface, alt, ret, errno);
543 if (ret < 0)
544 return ctrl_error();
546 usb_linux_update_endp_table(s);
547 return 0;
550 static int usb_host_handle_control(USBHostDevice *s, USBPacket *p)
552 struct usbdevfs_urb *urb;
553 AsyncURB *aurb;
554 int ret, value, index;
557 * Process certain standard device requests.
558 * These are infrequent and are processed synchronously.
560 value = le16_to_cpu(s->ctrl.req.wValue);
561 index = le16_to_cpu(s->ctrl.req.wIndex);
563 dprintf("husb: ctrl type 0x%x req 0x%x val 0x%x index %u len %u\n",
564 s->ctrl.req.bRequestType, s->ctrl.req.bRequest, value, index,
565 s->ctrl.len);
567 if (s->ctrl.req.bRequestType == 0) {
568 switch (s->ctrl.req.bRequest) {
569 case USB_REQ_SET_ADDRESS:
570 return usb_host_set_address(s, value);
572 case USB_REQ_SET_CONFIGURATION:
573 return usb_host_set_config(s, value & 0xff);
577 if (s->ctrl.req.bRequestType == 1 &&
578 s->ctrl.req.bRequest == USB_REQ_SET_INTERFACE)
579 return usb_host_set_interface(s, index, value);
581 /* The rest are asynchronous */
583 aurb = async_alloc();
584 aurb->hdev = s;
585 aurb->packet = p;
588 * Setup ctrl transfer.
590 * s->ctrl is layed out such that data buffer immediately follows
591 * 'req' struct which is exactly what usbdevfs expects.
593 urb = &aurb->urb;
595 urb->type = USBDEVFS_URB_TYPE_CONTROL;
596 urb->endpoint = p->devep;
598 urb->buffer = &s->ctrl.req;
599 urb->buffer_length = 8 + s->ctrl.len;
601 urb->usercontext = s;
603 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
605 dprintf("husb: submit ctrl. len %u aurb %p\n", urb->buffer_length, aurb);
607 if (ret < 0) {
608 dprintf("husb: submit failed. errno %d\n", errno);
609 async_free(aurb);
611 switch(errno) {
612 case ETIMEDOUT:
613 return USB_RET_NAK;
614 case EPIPE:
615 default:
616 return USB_RET_STALL;
620 usb_defer_packet(p, async_cancel, aurb);
621 return USB_RET_ASYNC;
624 static int do_token_setup(USBDevice *dev, USBPacket *p)
626 USBHostDevice *s = (USBHostDevice *) dev;
627 int ret = 0;
629 if (p->len != 8)
630 return USB_RET_STALL;
632 memcpy(&s->ctrl.req, p->data, 8);
633 s->ctrl.len = le16_to_cpu(s->ctrl.req.wLength);
634 s->ctrl.offset = 0;
635 s->ctrl.state = CTRL_STATE_SETUP;
637 if (s->ctrl.req.bRequestType & USB_DIR_IN) {
638 ret = usb_host_handle_control(s, p);
639 if (ret < 0)
640 return ret;
642 if (ret < s->ctrl.len)
643 s->ctrl.len = ret;
644 s->ctrl.state = CTRL_STATE_DATA;
645 } else {
646 if (s->ctrl.len == 0)
647 s->ctrl.state = CTRL_STATE_ACK;
648 else
649 s->ctrl.state = CTRL_STATE_DATA;
652 return ret;
655 static int do_token_in(USBDevice *dev, USBPacket *p)
657 USBHostDevice *s = (USBHostDevice *) dev;
658 int ret = 0;
660 if (p->devep != 0)
661 return usb_host_handle_data(s, p);
663 switch(s->ctrl.state) {
664 case CTRL_STATE_ACK:
665 if (!(s->ctrl.req.bRequestType & USB_DIR_IN)) {
666 ret = usb_host_handle_control(s, p);
667 if (ret == USB_RET_ASYNC)
668 return USB_RET_ASYNC;
670 s->ctrl.state = CTRL_STATE_IDLE;
671 return ret > 0 ? 0 : ret;
674 return 0;
676 case CTRL_STATE_DATA:
677 if (s->ctrl.req.bRequestType & USB_DIR_IN) {
678 int len = s->ctrl.len - s->ctrl.offset;
679 if (len > p->len)
680 len = p->len;
681 memcpy(p->data, s->ctrl.buffer + s->ctrl.offset, len);
682 s->ctrl.offset += len;
683 if (s->ctrl.offset >= s->ctrl.len)
684 s->ctrl.state = CTRL_STATE_ACK;
685 return len;
688 s->ctrl.state = CTRL_STATE_IDLE;
689 return USB_RET_STALL;
691 default:
692 return USB_RET_STALL;
696 static int do_token_out(USBDevice *dev, USBPacket *p)
698 USBHostDevice *s = (USBHostDevice *) dev;
700 if (p->devep != 0)
701 return usb_host_handle_data(s, p);
703 switch(s->ctrl.state) {
704 case CTRL_STATE_ACK:
705 if (s->ctrl.req.bRequestType & USB_DIR_IN) {
706 s->ctrl.state = CTRL_STATE_IDLE;
707 /* transfer OK */
708 } else {
709 /* ignore additional output */
711 return 0;
713 case CTRL_STATE_DATA:
714 if (!(s->ctrl.req.bRequestType & USB_DIR_IN)) {
715 int len = s->ctrl.len - s->ctrl.offset;
716 if (len > p->len)
717 len = p->len;
718 memcpy(s->ctrl.buffer + s->ctrl.offset, p->data, len);
719 s->ctrl.offset += len;
720 if (s->ctrl.offset >= s->ctrl.len)
721 s->ctrl.state = CTRL_STATE_ACK;
722 return len;
725 s->ctrl.state = CTRL_STATE_IDLE;
726 return USB_RET_STALL;
728 default:
729 return USB_RET_STALL;
734 * Packet handler.
735 * Called by the HC (host controller).
737 * Returns length of the transaction or one of the USB_RET_XXX codes.
739 static int usb_host_handle_packet(USBDevice *s, USBPacket *p)
741 switch(p->pid) {
742 case USB_MSG_ATTACH:
743 s->state = USB_STATE_ATTACHED;
744 return 0;
746 case USB_MSG_DETACH:
747 s->state = USB_STATE_NOTATTACHED;
748 return 0;
750 case USB_MSG_RESET:
751 s->remote_wakeup = 0;
752 s->addr = 0;
753 s->state = USB_STATE_DEFAULT;
754 s->info->handle_reset(s);
755 return 0;
758 /* Rest of the PIDs must match our address */
759 if (s->state < USB_STATE_DEFAULT || p->devaddr != s->addr)
760 return USB_RET_NODEV;
762 switch (p->pid) {
763 case USB_TOKEN_SETUP:
764 return do_token_setup(s, p);
766 case USB_TOKEN_IN:
767 return do_token_in(s, p);
769 case USB_TOKEN_OUT:
770 return do_token_out(s, p);
772 default:
773 return USB_RET_STALL;
777 /* returns 1 on problem encountered or 0 for success */
778 static int usb_linux_update_endp_table(USBHostDevice *s)
780 uint8_t *descriptors;
781 uint8_t devep, type, configuration, alt_interface;
782 struct usb_ctrltransfer ct;
783 int interface, ret, length, i;
785 ct.bRequestType = USB_DIR_IN;
786 ct.bRequest = USB_REQ_GET_CONFIGURATION;
787 ct.wValue = 0;
788 ct.wIndex = 0;
789 ct.wLength = 1;
790 ct.data = &configuration;
791 ct.timeout = 50;
793 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
794 if (ret < 0) {
795 perror("usb_linux_update_endp_table");
796 return 1;
799 /* in address state */
800 if (configuration == 0)
801 return 1;
803 /* get the desired configuration, interface, and endpoint descriptors
804 * from device description */
805 descriptors = &s->descr[18];
806 length = s->descr_len - 18;
807 i = 0;
809 if (descriptors[i + 1] != USB_DT_CONFIG ||
810 descriptors[i + 5] != configuration) {
811 dprintf("invalid descriptor data - configuration\n");
812 return 1;
814 i += descriptors[i];
816 while (i < length) {
817 if (descriptors[i + 1] != USB_DT_INTERFACE ||
818 (descriptors[i + 1] == USB_DT_INTERFACE &&
819 descriptors[i + 4] == 0)) {
820 i += descriptors[i];
821 continue;
824 interface = descriptors[i + 2];
826 ct.bRequestType = USB_DIR_IN | USB_RECIP_INTERFACE;
827 ct.bRequest = USB_REQ_GET_INTERFACE;
828 ct.wValue = 0;
829 ct.wIndex = interface;
830 ct.wLength = 1;
831 ct.data = &alt_interface;
832 ct.timeout = 50;
834 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
835 if (ret < 0) {
836 alt_interface = interface;
839 /* the current interface descriptor is the active interface
840 * and has endpoints */
841 if (descriptors[i + 3] != alt_interface) {
842 i += descriptors[i];
843 continue;
846 /* advance to the endpoints */
847 while (i < length && descriptors[i +1] != USB_DT_ENDPOINT)
848 i += descriptors[i];
850 if (i >= length)
851 break;
853 while (i < length) {
854 if (descriptors[i + 1] != USB_DT_ENDPOINT)
855 break;
857 devep = descriptors[i + 2];
858 switch (descriptors[i + 3] & 0x3) {
859 case 0x00:
860 type = USBDEVFS_URB_TYPE_CONTROL;
861 break;
862 case 0x01:
863 type = USBDEVFS_URB_TYPE_ISO;
864 break;
865 case 0x02:
866 type = USBDEVFS_URB_TYPE_BULK;
867 break;
868 case 0x03:
869 type = USBDEVFS_URB_TYPE_INTERRUPT;
870 break;
871 default:
872 dprintf("usb_host: malformed endpoint type\n");
873 type = USBDEVFS_URB_TYPE_BULK;
875 s->endp_table[(devep & 0xf) - 1].type = type;
876 s->endp_table[(devep & 0xf) - 1].halted = 0;
878 i += descriptors[i];
881 return 0;
884 static int usb_host_initfn(USBDevice *dev)
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 USBDevice *d = NULL;
893 USBHostDevice *dev;
894 struct usbdevfs_connectinfo ci;
895 char buf[1024];
897 printf("husb: open device %d.%d\n", bus_num, addr);
899 if (!usb_host_device_path) {
900 perror("husb: USB Host Device Path not set");
901 goto fail;
903 snprintf(buf, sizeof(buf), "%s/%03d/%03d", usb_host_device_path,
904 bus_num, addr);
905 fd = open(buf, O_RDWR | O_NONBLOCK);
906 if (fd < 0) {
907 perror(buf);
908 goto fail;
910 dprintf("husb: opened %s\n", buf);
912 d = usb_create(NULL /* FIXME */, "USB Host Device");
913 dev = DO_UPCAST(USBHostDevice, dev, d);
915 dev->bus_num = bus_num;
916 dev->addr = addr;
918 /* read the device description */
919 dev->descr_len = read(fd, dev->descr, sizeof(dev->descr));
920 if (dev->descr_len <= 0) {
921 perror("husb: reading device data failed");
922 goto fail;
925 #ifdef DEBUG
927 int x;
928 printf("=== begin dumping device descriptor data ===\n");
929 for (x = 0; x < dev->descr_len; x++)
930 printf("%02x ", dev->descr[x]);
931 printf("\n=== end dumping device descriptor data ===\n");
933 #endif
937 * Initial configuration is -1 which makes us claim first
938 * available config. We used to start with 1, which does not
939 * always work. I've seen devices where first config starts
940 * with 2.
942 if (!usb_host_claim_interfaces(dev, -1))
943 goto fail;
945 ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci);
946 if (ret < 0) {
947 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
948 goto fail;
951 printf("husb: grabbed usb device %d.%d\n", bus_num, addr);
953 ret = usb_linux_update_endp_table(dev);
954 if (ret)
955 goto fail;
957 if (ci.slow)
958 dev->dev.speed = USB_SPEED_LOW;
959 else
960 dev->dev.speed = USB_SPEED_HIGH;
962 if (!prod_name || prod_name[0] == '\0')
963 snprintf(dev->dev.devname, sizeof(dev->dev.devname),
964 "host:%d.%d", bus_num, addr);
965 else
966 pstrcpy(dev->dev.devname, sizeof(dev->dev.devname),
967 prod_name);
969 /* USB devio uses 'write' flag to check for async completions */
970 qemu_set_fd_handler(dev->fd, NULL, async_complete, dev);
972 hostdev_link(dev);
974 return (USBDevice *) dev;
976 fail:
977 if (d)
978 qdev_free(&d->qdev);
979 if (fd != -1)
980 close(fd);
981 return NULL;
984 static struct USBDeviceInfo usb_host_dev_info = {
985 .qdev.name = "USB Host Device",
986 .qdev.size = sizeof(USBHostDevice),
987 .init = usb_host_initfn,
988 .handle_packet = usb_host_handle_packet,
989 .handle_reset = usb_host_handle_reset,
990 #if 0
991 .handle_control = usb_host_handle_control,
992 .handle_data = usb_host_handle_data,
993 #endif
994 .handle_destroy = usb_host_handle_destroy,
997 static void usb_host_register_devices(void)
999 usb_qdev_register(&usb_host_dev_info);
1001 device_init(usb_host_register_devices)
1003 static int usb_host_auto_add(const char *spec);
1004 static int usb_host_auto_del(const char *spec);
1006 USBDevice *usb_host_device_open(const char *devname)
1008 Monitor *mon = cur_mon;
1009 int bus_num, addr;
1010 char product_name[PRODUCT_NAME_SZ];
1012 if (strstr(devname, "auto:")) {
1013 usb_host_auto_add(devname);
1014 return NULL;
1017 if (usb_host_find_device(&bus_num, &addr, product_name, sizeof(product_name),
1018 devname) < 0)
1019 return NULL;
1021 if (hostdev_find(bus_num, addr)) {
1022 monitor_printf(mon, "husb: host usb device %d.%d is already open\n",
1023 bus_num, addr);
1024 return NULL;
1027 return usb_host_device_open_addr(bus_num, addr, product_name);
1030 int usb_host_device_close(const char *devname)
1032 char product_name[PRODUCT_NAME_SZ];
1033 int bus_num, addr;
1034 USBHostDevice *s;
1036 if (strstr(devname, "auto:"))
1037 return usb_host_auto_del(devname);
1039 if (usb_host_find_device(&bus_num, &addr, product_name, sizeof(product_name),
1040 devname) < 0)
1041 return -1;
1043 s = hostdev_find(bus_num, addr);
1044 if (s) {
1045 usb_device_delete_addr(s->bus_num, s->dev.addr);
1046 return 0;
1049 return -1;
1052 static int get_tag_value(char *buf, int buf_size,
1053 const char *str, const char *tag,
1054 const char *stopchars)
1056 const char *p;
1057 char *q;
1058 p = strstr(str, tag);
1059 if (!p)
1060 return -1;
1061 p += strlen(tag);
1062 while (qemu_isspace(*p))
1063 p++;
1064 q = buf;
1065 while (*p != '\0' && !strchr(stopchars, *p)) {
1066 if ((q - buf) < (buf_size - 1))
1067 *q++ = *p;
1068 p++;
1070 *q = '\0';
1071 return q - buf;
1075 * Use /proc/bus/usb/devices or /dev/bus/usb/devices file to determine
1076 * host's USB devices. This is legacy support since many distributions
1077 * are moving to /sys/bus/usb
1079 static int usb_host_scan_dev(void *opaque, USBScanFunc *func)
1081 FILE *f = NULL;
1082 char line[1024];
1083 char buf[1024];
1084 int bus_num, addr, speed, device_count, class_id, product_id, vendor_id;
1085 char product_name[512];
1086 int ret = 0;
1088 if (!usb_host_device_path) {
1089 perror("husb: USB Host Device Path not set");
1090 goto the_end;
1092 snprintf(line, sizeof(line), "%s/devices", usb_host_device_path);
1093 f = fopen(line, "r");
1094 if (!f) {
1095 perror("husb: cannot open devices file");
1096 goto the_end;
1099 device_count = 0;
1100 bus_num = addr = speed = class_id = product_id = vendor_id = 0;
1101 for(;;) {
1102 if (fgets(line, sizeof(line), f) == NULL)
1103 break;
1104 if (strlen(line) > 0)
1105 line[strlen(line) - 1] = '\0';
1106 if (line[0] == 'T' && line[1] == ':') {
1107 if (device_count && (vendor_id || product_id)) {
1108 /* New device. Add the previously discovered device. */
1109 ret = func(opaque, bus_num, addr, class_id, vendor_id,
1110 product_id, product_name, speed);
1111 if (ret)
1112 goto the_end;
1114 if (get_tag_value(buf, sizeof(buf), line, "Bus=", " ") < 0)
1115 goto fail;
1116 bus_num = atoi(buf);
1117 if (get_tag_value(buf, sizeof(buf), line, "Dev#=", " ") < 0)
1118 goto fail;
1119 addr = atoi(buf);
1120 if (get_tag_value(buf, sizeof(buf), line, "Spd=", " ") < 0)
1121 goto fail;
1122 if (!strcmp(buf, "480"))
1123 speed = USB_SPEED_HIGH;
1124 else if (!strcmp(buf, "1.5"))
1125 speed = USB_SPEED_LOW;
1126 else
1127 speed = USB_SPEED_FULL;
1128 product_name[0] = '\0';
1129 class_id = 0xff;
1130 device_count++;
1131 product_id = 0;
1132 vendor_id = 0;
1133 } else if (line[0] == 'P' && line[1] == ':') {
1134 if (get_tag_value(buf, sizeof(buf), line, "Vendor=", " ") < 0)
1135 goto fail;
1136 vendor_id = strtoul(buf, NULL, 16);
1137 if (get_tag_value(buf, sizeof(buf), line, "ProdID=", " ") < 0)
1138 goto fail;
1139 product_id = strtoul(buf, NULL, 16);
1140 } else if (line[0] == 'S' && line[1] == ':') {
1141 if (get_tag_value(buf, sizeof(buf), line, "Product=", "") < 0)
1142 goto fail;
1143 pstrcpy(product_name, sizeof(product_name), buf);
1144 } else if (line[0] == 'D' && line[1] == ':') {
1145 if (get_tag_value(buf, sizeof(buf), line, "Cls=", " (") < 0)
1146 goto fail;
1147 class_id = strtoul(buf, NULL, 16);
1149 fail: ;
1151 if (device_count && (vendor_id || product_id)) {
1152 /* Add the last device. */
1153 ret = func(opaque, bus_num, addr, class_id, vendor_id,
1154 product_id, product_name, speed);
1156 the_end:
1157 if (f)
1158 fclose(f);
1159 return ret;
1163 * Read sys file-system device file
1165 * @line address of buffer to put file contents in
1166 * @line_size size of line
1167 * @device_file path to device file (printf format string)
1168 * @device_name device being opened (inserted into device_file)
1170 * @return 0 failed, 1 succeeded ('line' contains data)
1172 static int usb_host_read_file(char *line, size_t line_size, const char *device_file, const char *device_name)
1174 Monitor *mon = cur_mon;
1175 FILE *f;
1176 int ret = 0;
1177 char filename[PATH_MAX];
1179 snprintf(filename, PATH_MAX, USBSYSBUS_PATH "/devices/%s/%s", device_name,
1180 device_file);
1181 f = fopen(filename, "r");
1182 if (f) {
1183 fgets(line, line_size, f);
1184 fclose(f);
1185 ret = 1;
1186 } else {
1187 monitor_printf(mon, "husb: could not open %s\n", filename);
1190 return ret;
1194 * Use /sys/bus/usb/devices/ directory to determine host's USB
1195 * devices.
1197 * This code is based on Robert Schiele's original patches posted to
1198 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1200 static int usb_host_scan_sys(void *opaque, USBScanFunc *func)
1202 DIR *dir = NULL;
1203 char line[1024];
1204 int bus_num, addr, speed, class_id, product_id, vendor_id;
1205 int ret = 0;
1206 char product_name[512];
1207 struct dirent *de;
1209 dir = opendir(USBSYSBUS_PATH "/devices");
1210 if (!dir) {
1211 perror("husb: cannot open devices directory");
1212 goto the_end;
1215 while ((de = readdir(dir))) {
1216 if (de->d_name[0] != '.' && !strchr(de->d_name, ':')) {
1217 char *tmpstr = de->d_name;
1218 if (!strncmp(de->d_name, "usb", 3))
1219 tmpstr += 3;
1220 bus_num = atoi(tmpstr);
1222 if (!usb_host_read_file(line, sizeof(line), "devnum", de->d_name))
1223 goto the_end;
1224 if (sscanf(line, "%d", &addr) != 1)
1225 goto the_end;
1227 if (!usb_host_read_file(line, sizeof(line), "bDeviceClass",
1228 de->d_name))
1229 goto the_end;
1230 if (sscanf(line, "%x", &class_id) != 1)
1231 goto the_end;
1233 if (!usb_host_read_file(line, sizeof(line), "idVendor", de->d_name))
1234 goto the_end;
1235 if (sscanf(line, "%x", &vendor_id) != 1)
1236 goto the_end;
1238 if (!usb_host_read_file(line, sizeof(line), "idProduct",
1239 de->d_name))
1240 goto the_end;
1241 if (sscanf(line, "%x", &product_id) != 1)
1242 goto the_end;
1244 if (!usb_host_read_file(line, sizeof(line), "product",
1245 de->d_name)) {
1246 *product_name = 0;
1247 } else {
1248 if (strlen(line) > 0)
1249 line[strlen(line) - 1] = '\0';
1250 pstrcpy(product_name, sizeof(product_name), line);
1253 if (!usb_host_read_file(line, sizeof(line), "speed", de->d_name))
1254 goto the_end;
1255 if (!strcmp(line, "480\n"))
1256 speed = USB_SPEED_HIGH;
1257 else if (!strcmp(line, "1.5\n"))
1258 speed = USB_SPEED_LOW;
1259 else
1260 speed = USB_SPEED_FULL;
1262 ret = func(opaque, bus_num, addr, class_id, vendor_id,
1263 product_id, product_name, speed);
1264 if (ret)
1265 goto the_end;
1268 the_end:
1269 if (dir)
1270 closedir(dir);
1271 return ret;
1275 * Determine how to access the host's USB devices and call the
1276 * specific support function.
1278 static int usb_host_scan(void *opaque, USBScanFunc *func)
1280 Monitor *mon = cur_mon;
1281 FILE *f = NULL;
1282 DIR *dir = NULL;
1283 int ret = 0;
1284 const char *fs_type[] = {"unknown", "proc", "dev", "sys"};
1285 char devpath[PATH_MAX];
1287 /* only check the host once */
1288 if (!usb_fs_type) {
1289 dir = opendir(USBSYSBUS_PATH "/devices");
1290 if (dir) {
1291 /* devices found in /dev/bus/usb/ (yes - not a mistake!) */
1292 strcpy(devpath, USBDEVBUS_PATH);
1293 usb_fs_type = USB_FS_SYS;
1294 closedir(dir);
1295 dprintf(USBDBG_DEVOPENED, USBSYSBUS_PATH);
1296 goto found_devices;
1298 f = fopen(USBPROCBUS_PATH "/devices", "r");
1299 if (f) {
1300 /* devices found in /proc/bus/usb/ */
1301 strcpy(devpath, USBPROCBUS_PATH);
1302 usb_fs_type = USB_FS_PROC;
1303 fclose(f);
1304 dprintf(USBDBG_DEVOPENED, USBPROCBUS_PATH);
1305 goto found_devices;
1307 /* try additional methods if an access method hasn't been found yet */
1308 f = fopen(USBDEVBUS_PATH "/devices", "r");
1309 if (f) {
1310 /* devices found in /dev/bus/usb/ */
1311 strcpy(devpath, USBDEVBUS_PATH);
1312 usb_fs_type = USB_FS_DEV;
1313 fclose(f);
1314 dprintf(USBDBG_DEVOPENED, USBDEVBUS_PATH);
1315 goto found_devices;
1317 found_devices:
1318 if (!usb_fs_type) {
1319 monitor_printf(mon, "husb: unable to access USB devices\n");
1320 return -ENOENT;
1323 /* the module setting (used later for opening devices) */
1324 usb_host_device_path = qemu_mallocz(strlen(devpath)+1);
1325 strcpy(usb_host_device_path, devpath);
1326 monitor_printf(mon, "husb: using %s file-system with %s\n",
1327 fs_type[usb_fs_type], usb_host_device_path);
1330 switch (usb_fs_type) {
1331 case USB_FS_PROC:
1332 case USB_FS_DEV:
1333 ret = usb_host_scan_dev(opaque, func);
1334 break;
1335 case USB_FS_SYS:
1336 ret = usb_host_scan_sys(opaque, func);
1337 break;
1338 default:
1339 ret = -EINVAL;
1340 break;
1342 return ret;
1345 struct USBAutoFilter {
1346 struct USBAutoFilter *next;
1347 int bus_num;
1348 int addr;
1349 int vendor_id;
1350 int product_id;
1353 static QEMUTimer *usb_auto_timer;
1354 static struct USBAutoFilter *usb_auto_filter;
1356 static int usb_host_auto_scan(void *opaque, int bus_num, int addr,
1357 int class_id, int vendor_id, int product_id,
1358 const char *product_name, int speed)
1360 struct USBAutoFilter *f;
1361 struct USBDevice *dev;
1363 /* Ignore hubs */
1364 if (class_id == 9)
1365 return 0;
1367 for (f = usb_auto_filter; f; f = f->next) {
1368 if (f->bus_num >= 0 && f->bus_num != bus_num)
1369 continue;
1371 if (f->addr >= 0 && f->addr != addr)
1372 continue;
1374 if (f->vendor_id >= 0 && f->vendor_id != vendor_id)
1375 continue;
1377 if (f->product_id >= 0 && f->product_id != product_id)
1378 continue;
1380 /* We got a match */
1382 /* Allredy attached ? */
1383 if (hostdev_find(bus_num, addr))
1384 return 0;
1386 dprintf("husb: auto open: bus_num %d addr %d\n", bus_num, addr);
1388 dev = usb_host_device_open_addr(bus_num, addr, product_name);
1389 if (dev)
1390 qdev_init(&dev->qdev);
1393 return 0;
1396 static void usb_host_auto_timer(void *unused)
1398 usb_host_scan(NULL, usb_host_auto_scan);
1399 qemu_mod_timer(usb_auto_timer, qemu_get_clock(rt_clock) + 2000);
1403 * Autoconnect filter
1404 * Format:
1405 * auto:bus:dev[:vid:pid]
1406 * auto:bus.dev[:vid:pid]
1408 * bus - bus number (dec, * means any)
1409 * dev - device number (dec, * means any)
1410 * vid - vendor id (hex, * means any)
1411 * pid - product id (hex, * means any)
1413 * See 'lsusb' output.
1415 static int parse_filter(const char *spec, struct USBAutoFilter *f)
1417 enum { BUS, DEV, VID, PID, DONE };
1418 const char *p = spec;
1419 int i;
1421 f->bus_num = -1;
1422 f->addr = -1;
1423 f->vendor_id = -1;
1424 f->product_id = -1;
1426 for (i = BUS; i < DONE; i++) {
1427 p = strpbrk(p, ":.");
1428 if (!p) break;
1429 p++;
1431 if (*p == '*')
1432 continue;
1434 switch(i) {
1435 case BUS: f->bus_num = strtol(p, NULL, 10); break;
1436 case DEV: f->addr = strtol(p, NULL, 10); break;
1437 case VID: f->vendor_id = strtol(p, NULL, 16); break;
1438 case PID: f->product_id = strtol(p, NULL, 16); break;
1442 if (i < DEV) {
1443 fprintf(stderr, "husb: invalid auto filter spec %s\n", spec);
1444 return -1;
1447 return 0;
1450 static int match_filter(const struct USBAutoFilter *f1,
1451 const struct USBAutoFilter *f2)
1453 return f1->bus_num == f2->bus_num &&
1454 f1->addr == f2->addr &&
1455 f1->vendor_id == f2->vendor_id &&
1456 f1->product_id == f2->product_id;
1459 static int usb_host_auto_add(const char *spec)
1461 struct USBAutoFilter filter, *f;
1463 if (parse_filter(spec, &filter) < 0)
1464 return -1;
1466 f = qemu_mallocz(sizeof(*f));
1468 *f = filter;
1470 if (!usb_auto_filter) {
1472 * First entry. Init and start the monitor.
1473 * Right now we're using timer to check for new devices.
1474 * If this turns out to be too expensive we can move that into a
1475 * separate thread.
1477 usb_auto_timer = qemu_new_timer(rt_clock, usb_host_auto_timer, NULL);
1478 if (!usb_auto_timer) {
1479 fprintf(stderr, "husb: failed to allocate auto scan timer\n");
1480 qemu_free(f);
1481 return -1;
1484 /* Check for new devices every two seconds */
1485 qemu_mod_timer(usb_auto_timer, qemu_get_clock(rt_clock) + 2000);
1488 dprintf("husb: added auto filter: bus_num %d addr %d vid %d pid %d\n",
1489 f->bus_num, f->addr, f->vendor_id, f->product_id);
1491 f->next = usb_auto_filter;
1492 usb_auto_filter = f;
1494 return 0;
1497 static int usb_host_auto_del(const char *spec)
1499 struct USBAutoFilter *pf = usb_auto_filter;
1500 struct USBAutoFilter **prev = &usb_auto_filter;
1501 struct USBAutoFilter filter;
1503 if (parse_filter(spec, &filter) < 0)
1504 return -1;
1506 while (pf) {
1507 if (match_filter(pf, &filter)) {
1508 dprintf("husb: removed auto filter: bus_num %d addr %d vid %d pid %d\n",
1509 pf->bus_num, pf->addr, pf->vendor_id, pf->product_id);
1511 *prev = pf->next;
1513 if (!usb_auto_filter) {
1514 /* No more filters. Stop scanning. */
1515 qemu_del_timer(usb_auto_timer);
1516 qemu_free_timer(usb_auto_timer);
1519 return 0;
1522 prev = &pf->next;
1523 pf = pf->next;
1526 return -1;
1529 typedef struct FindDeviceState {
1530 int vendor_id;
1531 int product_id;
1532 int bus_num;
1533 int addr;
1534 char product_name[PRODUCT_NAME_SZ];
1535 } FindDeviceState;
1537 static int usb_host_find_device_scan(void *opaque, int bus_num, int addr,
1538 int class_id,
1539 int vendor_id, int product_id,
1540 const char *product_name, int speed)
1542 FindDeviceState *s = opaque;
1543 if ((vendor_id == s->vendor_id &&
1544 product_id == s->product_id) ||
1545 (bus_num == s->bus_num &&
1546 addr == s->addr)) {
1547 pstrcpy(s->product_name, PRODUCT_NAME_SZ, product_name);
1548 s->bus_num = bus_num;
1549 s->addr = addr;
1550 return 1;
1551 } else {
1552 return 0;
1556 /* the syntax is :
1557 'bus.addr' (decimal numbers) or
1558 'vendor_id:product_id' (hexa numbers) */
1559 static int usb_host_find_device(int *pbus_num, int *paddr,
1560 char *product_name, int product_name_size,
1561 const char *devname)
1563 const char *p;
1564 int ret;
1565 FindDeviceState fs;
1567 p = strchr(devname, '.');
1568 if (p) {
1569 *pbus_num = strtoul(devname, NULL, 0);
1570 *paddr = strtoul(p + 1, NULL, 0);
1571 fs.bus_num = *pbus_num;
1572 fs.addr = *paddr;
1573 ret = usb_host_scan(&fs, usb_host_find_device_scan);
1574 if (ret)
1575 pstrcpy(product_name, product_name_size, fs.product_name);
1576 return 0;
1579 p = strchr(devname, ':');
1580 if (p) {
1581 fs.vendor_id = strtoul(devname, NULL, 16);
1582 fs.product_id = strtoul(p + 1, NULL, 16);
1583 ret = usb_host_scan(&fs, usb_host_find_device_scan);
1584 if (ret) {
1585 *pbus_num = fs.bus_num;
1586 *paddr = fs.addr;
1587 pstrcpy(product_name, product_name_size, fs.product_name);
1588 return 0;
1591 return -1;
1594 /**********************/
1595 /* USB host device info */
1597 struct usb_class_info {
1598 int class;
1599 const char *class_name;
1602 static const struct usb_class_info usb_class_info[] = {
1603 { USB_CLASS_AUDIO, "Audio"},
1604 { USB_CLASS_COMM, "Communication"},
1605 { USB_CLASS_HID, "HID"},
1606 { USB_CLASS_HUB, "Hub" },
1607 { USB_CLASS_PHYSICAL, "Physical" },
1608 { USB_CLASS_PRINTER, "Printer" },
1609 { USB_CLASS_MASS_STORAGE, "Storage" },
1610 { USB_CLASS_CDC_DATA, "Data" },
1611 { USB_CLASS_APP_SPEC, "Application Specific" },
1612 { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
1613 { USB_CLASS_STILL_IMAGE, "Still Image" },
1614 { USB_CLASS_CSCID, "Smart Card" },
1615 { USB_CLASS_CONTENT_SEC, "Content Security" },
1616 { -1, NULL }
1619 static const char *usb_class_str(uint8_t class)
1621 const struct usb_class_info *p;
1622 for(p = usb_class_info; p->class != -1; p++) {
1623 if (p->class == class)
1624 break;
1626 return p->class_name;
1629 static void usb_info_device(Monitor *mon, int bus_num, int addr, int class_id,
1630 int vendor_id, int product_id,
1631 const char *product_name,
1632 int speed)
1634 const char *class_str, *speed_str;
1636 switch(speed) {
1637 case USB_SPEED_LOW:
1638 speed_str = "1.5";
1639 break;
1640 case USB_SPEED_FULL:
1641 speed_str = "12";
1642 break;
1643 case USB_SPEED_HIGH:
1644 speed_str = "480";
1645 break;
1646 default:
1647 speed_str = "?";
1648 break;
1651 monitor_printf(mon, " Device %d.%d, speed %s Mb/s\n",
1652 bus_num, addr, speed_str);
1653 class_str = usb_class_str(class_id);
1654 if (class_str)
1655 monitor_printf(mon, " %s:", class_str);
1656 else
1657 monitor_printf(mon, " Class %02x:", class_id);
1658 monitor_printf(mon, " USB device %04x:%04x", vendor_id, product_id);
1659 if (product_name[0] != '\0')
1660 monitor_printf(mon, ", %s", product_name);
1661 monitor_printf(mon, "\n");
1664 static int usb_host_info_device(void *opaque, int bus_num, int addr,
1665 int class_id,
1666 int vendor_id, int product_id,
1667 const char *product_name,
1668 int speed)
1670 Monitor *mon = opaque;
1672 usb_info_device(mon, bus_num, addr, class_id, vendor_id, product_id,
1673 product_name, speed);
1674 return 0;
1677 static void dec2str(int val, char *str, size_t size)
1679 if (val == -1)
1680 snprintf(str, size, "*");
1681 else
1682 snprintf(str, size, "%d", val);
1685 static void hex2str(int val, char *str, size_t size)
1687 if (val == -1)
1688 snprintf(str, size, "*");
1689 else
1690 snprintf(str, size, "%x", val);
1693 void usb_host_info(Monitor *mon)
1695 struct USBAutoFilter *f;
1697 usb_host_scan(mon, usb_host_info_device);
1699 if (usb_auto_filter)
1700 monitor_printf(mon, " Auto filters:\n");
1701 for (f = usb_auto_filter; f; f = f->next) {
1702 char bus[10], addr[10], vid[10], pid[10];
1703 dec2str(f->bus_num, bus, sizeof(bus));
1704 dec2str(f->addr, addr, sizeof(addr));
1705 hex2str(f->vendor_id, vid, sizeof(vid));
1706 hex2str(f->product_id, pid, sizeof(pid));
1707 monitor_printf(mon, " Device %s.%s ID %s:%s\n",
1708 bus, addr, vid, pid);