kvm: testsuite: Fix halt callback
[qemu-kvm/fedora.git] / usb-linux.c
blob40e79603b98ef184373833abc28c98a829d6965d
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 "console.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 if (!aurb) {
449 dprintf("husb: async malloc failed\n");
450 return USB_RET_NAK;
452 aurb->hdev = s;
453 aurb->packet = p;
455 urb = &aurb->urb;
457 if (p->pid == USB_TOKEN_IN)
458 urb->endpoint = p->devep | 0x80;
459 else
460 urb->endpoint = p->devep;
462 if (is_halted(s, p->devep)) {
463 ret = ioctl(s->fd, USBDEVFS_CLEAR_HALT, &urb->endpoint);
464 if (ret < 0) {
465 dprintf("husb: failed to clear halt. ep 0x%x errno %d\n",
466 urb->endpoint, errno);
467 return USB_RET_NAK;
469 clear_halt(s, p->devep);
472 urb->buffer = p->data;
473 urb->buffer_length = p->len;
475 if (is_isoc(s, p->devep)) {
476 /* Setup ISOC transfer */
477 urb->type = USBDEVFS_URB_TYPE_ISO;
478 urb->flags = USBDEVFS_URB_ISO_ASAP;
479 urb->number_of_packets = 1;
480 urb->iso_frame_desc[0].length = p->len;
481 } else {
482 /* Setup bulk transfer */
483 urb->type = USBDEVFS_URB_TYPE_BULK;
486 urb->usercontext = s;
488 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
490 dprintf("husb: data submit. ep 0x%x len %u aurb %p\n", urb->endpoint, p->len, aurb);
492 if (ret < 0) {
493 dprintf("husb: submit failed. errno %d\n", errno);
494 async_free(aurb);
496 switch(errno) {
497 case ETIMEDOUT:
498 return USB_RET_NAK;
499 case EPIPE:
500 default:
501 return USB_RET_STALL;
505 usb_defer_packet(p, async_cancel, aurb);
506 return USB_RET_ASYNC;
509 static int ctrl_error(void)
511 if (errno == ETIMEDOUT)
512 return USB_RET_NAK;
513 else
514 return USB_RET_STALL;
517 static int usb_host_set_address(USBHostDevice *s, int addr)
519 dprintf("husb: ctrl set addr %u\n", addr);
520 s->dev.addr = addr;
521 return 0;
524 static int usb_host_set_config(USBHostDevice *s, int config)
526 usb_host_release_interfaces(s);
528 int ret = ioctl(s->fd, USBDEVFS_SETCONFIGURATION, &config);
530 dprintf("husb: ctrl set config %d ret %d errno %d\n", config, ret, errno);
532 if (ret < 0)
533 return ctrl_error();
535 usb_host_claim_interfaces(s, config);
536 return 0;
539 static int usb_host_set_interface(USBHostDevice *s, int iface, int alt)
541 struct usbdevfs_setinterface si;
542 int ret;
544 si.interface = iface;
545 si.altsetting = alt;
546 ret = ioctl(s->fd, USBDEVFS_SETINTERFACE, &si);
548 dprintf("husb: ctrl set iface %d altset %d ret %d errno %d\n",
549 iface, alt, ret, errno);
551 if (ret < 0)
552 return ctrl_error();
554 usb_linux_update_endp_table(s);
555 return 0;
558 static int usb_host_handle_control(USBHostDevice *s, USBPacket *p)
560 struct usbdevfs_urb *urb;
561 AsyncURB *aurb;
562 int ret, value, index;
565 * Process certain standard device requests.
566 * These are infrequent and are processed synchronously.
568 value = le16_to_cpu(s->ctrl.req.wValue);
569 index = le16_to_cpu(s->ctrl.req.wIndex);
571 dprintf("husb: ctrl type 0x%x req 0x%x val 0x%x index %u len %u\n",
572 s->ctrl.req.bRequestType, s->ctrl.req.bRequest, value, index,
573 s->ctrl.len);
575 if (s->ctrl.req.bRequestType == 0) {
576 switch (s->ctrl.req.bRequest) {
577 case USB_REQ_SET_ADDRESS:
578 return usb_host_set_address(s, value);
580 case USB_REQ_SET_CONFIGURATION:
581 return usb_host_set_config(s, value & 0xff);
585 if (s->ctrl.req.bRequestType == 1 &&
586 s->ctrl.req.bRequest == USB_REQ_SET_INTERFACE)
587 return usb_host_set_interface(s, index, value);
589 /* The rest are asynchronous */
591 aurb = async_alloc();
592 if (!aurb) {
593 dprintf("husb: async malloc failed\n");
594 return USB_RET_NAK;
596 aurb->hdev = s;
597 aurb->packet = p;
600 * Setup ctrl transfer.
602 * s->ctrl is layed out such that data buffer immediately follows
603 * 'req' struct which is exactly what usbdevfs expects.
605 urb = &aurb->urb;
607 urb->type = USBDEVFS_URB_TYPE_CONTROL;
608 urb->endpoint = p->devep;
610 urb->buffer = &s->ctrl.req;
611 urb->buffer_length = 8 + s->ctrl.len;
613 urb->usercontext = s;
615 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
617 dprintf("husb: submit ctrl. len %u aurb %p\n", urb->buffer_length, aurb);
619 if (ret < 0) {
620 dprintf("husb: submit failed. errno %d\n", errno);
621 async_free(aurb);
623 switch(errno) {
624 case ETIMEDOUT:
625 return USB_RET_NAK;
626 case EPIPE:
627 default:
628 return USB_RET_STALL;
632 usb_defer_packet(p, async_cancel, aurb);
633 return USB_RET_ASYNC;
636 static int do_token_setup(USBDevice *dev, USBPacket *p)
638 USBHostDevice *s = (USBHostDevice *) dev;
639 int ret = 0;
641 if (p->len != 8)
642 return USB_RET_STALL;
644 memcpy(&s->ctrl.req, p->data, 8);
645 s->ctrl.len = le16_to_cpu(s->ctrl.req.wLength);
646 s->ctrl.offset = 0;
647 s->ctrl.state = CTRL_STATE_SETUP;
649 if (s->ctrl.req.bRequestType & USB_DIR_IN) {
650 ret = usb_host_handle_control(s, p);
651 if (ret < 0)
652 return ret;
654 if (ret < s->ctrl.len)
655 s->ctrl.len = ret;
656 s->ctrl.state = CTRL_STATE_DATA;
657 } else {
658 if (s->ctrl.len == 0)
659 s->ctrl.state = CTRL_STATE_ACK;
660 else
661 s->ctrl.state = CTRL_STATE_DATA;
664 return ret;
667 static int do_token_in(USBDevice *dev, USBPacket *p)
669 USBHostDevice *s = (USBHostDevice *) dev;
670 int ret = 0;
672 if (p->devep != 0)
673 return usb_host_handle_data(s, p);
675 switch(s->ctrl.state) {
676 case CTRL_STATE_ACK:
677 if (!(s->ctrl.req.bRequestType & USB_DIR_IN)) {
678 ret = usb_host_handle_control(s, p);
679 if (ret == USB_RET_ASYNC)
680 return USB_RET_ASYNC;
682 s->ctrl.state = CTRL_STATE_IDLE;
683 return ret > 0 ? 0 : ret;
686 return 0;
688 case CTRL_STATE_DATA:
689 if (s->ctrl.req.bRequestType & USB_DIR_IN) {
690 int len = s->ctrl.len - s->ctrl.offset;
691 if (len > p->len)
692 len = p->len;
693 memcpy(p->data, s->ctrl.buffer + s->ctrl.offset, len);
694 s->ctrl.offset += len;
695 if (s->ctrl.offset >= s->ctrl.len)
696 s->ctrl.state = CTRL_STATE_ACK;
697 return len;
700 s->ctrl.state = CTRL_STATE_IDLE;
701 return USB_RET_STALL;
703 default:
704 return USB_RET_STALL;
708 static int do_token_out(USBDevice *dev, USBPacket *p)
710 USBHostDevice *s = (USBHostDevice *) dev;
712 if (p->devep != 0)
713 return usb_host_handle_data(s, p);
715 switch(s->ctrl.state) {
716 case CTRL_STATE_ACK:
717 if (s->ctrl.req.bRequestType & USB_DIR_IN) {
718 s->ctrl.state = CTRL_STATE_IDLE;
719 /* transfer OK */
720 } else {
721 /* ignore additional output */
723 return 0;
725 case CTRL_STATE_DATA:
726 if (!(s->ctrl.req.bRequestType & USB_DIR_IN)) {
727 int len = s->ctrl.len - s->ctrl.offset;
728 if (len > p->len)
729 len = p->len;
730 memcpy(s->ctrl.buffer + s->ctrl.offset, p->data, len);
731 s->ctrl.offset += len;
732 if (s->ctrl.offset >= s->ctrl.len)
733 s->ctrl.state = CTRL_STATE_ACK;
734 return len;
737 s->ctrl.state = CTRL_STATE_IDLE;
738 return USB_RET_STALL;
740 default:
741 return USB_RET_STALL;
746 * Packet handler.
747 * Called by the HC (host controller).
749 * Returns length of the transaction or one of the USB_RET_XXX codes.
751 static int usb_host_handle_packet(USBDevice *s, USBPacket *p)
753 switch(p->pid) {
754 case USB_MSG_ATTACH:
755 s->state = USB_STATE_ATTACHED;
756 return 0;
758 case USB_MSG_DETACH:
759 s->state = USB_STATE_NOTATTACHED;
760 return 0;
762 case USB_MSG_RESET:
763 s->remote_wakeup = 0;
764 s->addr = 0;
765 s->state = USB_STATE_DEFAULT;
766 s->handle_reset(s);
767 return 0;
770 /* Rest of the PIDs must match our address */
771 if (s->state < USB_STATE_DEFAULT || p->devaddr != s->addr)
772 return USB_RET_NODEV;
774 switch (p->pid) {
775 case USB_TOKEN_SETUP:
776 return do_token_setup(s, p);
778 case USB_TOKEN_IN:
779 return do_token_in(s, p);
781 case USB_TOKEN_OUT:
782 return do_token_out(s, p);
784 default:
785 return USB_RET_STALL;
789 /* returns 1 on problem encountered or 0 for success */
790 static int usb_linux_update_endp_table(USBHostDevice *s)
792 uint8_t *descriptors;
793 uint8_t devep, type, configuration, alt_interface;
794 struct usb_ctrltransfer ct;
795 int interface, ret, length, i;
797 ct.bRequestType = USB_DIR_IN;
798 ct.bRequest = USB_REQ_GET_CONFIGURATION;
799 ct.wValue = 0;
800 ct.wIndex = 0;
801 ct.wLength = 1;
802 ct.data = &configuration;
803 ct.timeout = 50;
805 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
806 if (ret < 0) {
807 perror("usb_linux_update_endp_table");
808 return 1;
811 /* in address state */
812 if (configuration == 0)
813 return 1;
815 /* get the desired configuration, interface, and endpoint descriptors
816 * from device description */
817 descriptors = &s->descr[18];
818 length = s->descr_len - 18;
819 i = 0;
821 if (descriptors[i + 1] != USB_DT_CONFIG ||
822 descriptors[i + 5] != configuration) {
823 dprintf("invalid descriptor data - configuration\n");
824 return 1;
826 i += descriptors[i];
828 while (i < length) {
829 if (descriptors[i + 1] != USB_DT_INTERFACE ||
830 (descriptors[i + 1] == USB_DT_INTERFACE &&
831 descriptors[i + 4] == 0)) {
832 i += descriptors[i];
833 continue;
836 interface = descriptors[i + 2];
838 ct.bRequestType = USB_DIR_IN | USB_RECIP_INTERFACE;
839 ct.bRequest = USB_REQ_GET_INTERFACE;
840 ct.wValue = 0;
841 ct.wIndex = interface;
842 ct.wLength = 1;
843 ct.data = &alt_interface;
844 ct.timeout = 50;
846 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
847 if (ret < 0) {
848 perror("usb_linux_update_endp_table");
849 return 1;
852 /* the current interface descriptor is the active interface
853 * and has endpoints */
854 if (descriptors[i + 3] != alt_interface) {
855 i += descriptors[i];
856 continue;
859 /* advance to the endpoints */
860 while (i < length && descriptors[i +1] != USB_DT_ENDPOINT)
861 i += descriptors[i];
863 if (i >= length)
864 break;
866 while (i < length) {
867 if (descriptors[i + 1] != USB_DT_ENDPOINT)
868 break;
870 devep = descriptors[i + 2];
871 switch (descriptors[i + 3] & 0x3) {
872 case 0x00:
873 type = USBDEVFS_URB_TYPE_CONTROL;
874 break;
875 case 0x01:
876 type = USBDEVFS_URB_TYPE_ISO;
877 break;
878 case 0x02:
879 type = USBDEVFS_URB_TYPE_BULK;
880 break;
881 case 0x03:
882 type = USBDEVFS_URB_TYPE_INTERRUPT;
883 break;
884 default:
885 dprintf("usb_host: malformed endpoint type\n");
886 type = USBDEVFS_URB_TYPE_BULK;
888 s->endp_table[(devep & 0xf) - 1].type = type;
889 s->endp_table[(devep & 0xf) - 1].halted = 0;
891 i += descriptors[i];
894 return 0;
897 static USBDevice *usb_host_device_open_addr(int bus_num, int addr, const char *prod_name)
899 int fd = -1, ret;
900 USBHostDevice *dev = NULL;
901 struct usbdevfs_connectinfo ci;
902 char buf[1024];
904 dev = qemu_mallocz(sizeof(USBHostDevice));
905 if (!dev)
906 goto fail;
908 dev->bus_num = bus_num;
909 dev->addr = addr;
911 printf("husb: open device %d.%d\n", bus_num, addr);
913 if (!usb_host_device_path) {
914 perror("husb: USB Host Device Path not set");
915 goto fail;
917 snprintf(buf, sizeof(buf), "%s/%03d/%03d", usb_host_device_path,
918 bus_num, addr);
919 fd = open(buf, O_RDWR | O_NONBLOCK);
920 if (fd < 0) {
921 perror(buf);
922 goto fail;
924 dprintf("husb: opened %s\n", buf);
926 /* read the device description */
927 dev->descr_len = read(fd, dev->descr, sizeof(dev->descr));
928 if (dev->descr_len <= 0) {
929 perror("husb: reading device data failed");
930 goto fail;
933 #ifdef DEBUG
935 int x;
936 printf("=== begin dumping device descriptor data ===\n");
937 for (x = 0; x < dev->descr_len; x++)
938 printf("%02x ", dev->descr[x]);
939 printf("\n=== end dumping device descriptor data ===\n");
941 #endif
943 dev->fd = fd;
946 * Initial configuration is -1 which makes us claim first
947 * available config. We used to start with 1, which does not
948 * always work. I've seen devices where first config starts
949 * with 2.
951 if (!usb_host_claim_interfaces(dev, -1))
952 goto fail;
954 ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci);
955 if (ret < 0) {
956 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
957 goto fail;
960 printf("husb: grabbed usb device %d.%d\n", bus_num, addr);
962 ret = usb_linux_update_endp_table(dev);
963 if (ret)
964 goto fail;
966 if (ci.slow)
967 dev->dev.speed = USB_SPEED_LOW;
968 else
969 dev->dev.speed = USB_SPEED_HIGH;
971 dev->dev.handle_packet = usb_host_handle_packet;
972 dev->dev.handle_reset = usb_host_handle_reset;
973 dev->dev.handle_destroy = usb_host_handle_destroy;
975 if (!prod_name || prod_name[0] == '\0')
976 snprintf(dev->dev.devname, sizeof(dev->dev.devname),
977 "host:%d.%d", bus_num, addr);
978 else
979 pstrcpy(dev->dev.devname, sizeof(dev->dev.devname),
980 prod_name);
982 /* USB devio uses 'write' flag to check for async completions */
983 qemu_set_fd_handler(dev->fd, NULL, async_complete, dev);
985 hostdev_link(dev);
987 return (USBDevice *) dev;
989 fail:
990 if (dev)
991 qemu_free(dev);
993 close(fd);
994 return NULL;
997 static int usb_host_auto_add(const char *spec);
998 static int usb_host_auto_del(const char *spec);
1000 USBDevice *usb_host_device_open(const char *devname)
1002 int bus_num, addr;
1003 char product_name[PRODUCT_NAME_SZ];
1005 if (strstr(devname, "auto:")) {
1006 usb_host_auto_add(devname);
1007 return NULL;
1010 if (usb_host_find_device(&bus_num, &addr, product_name, sizeof(product_name),
1011 devname) < 0)
1012 return NULL;
1014 if (hostdev_find(bus_num, addr)) {
1015 term_printf("husb: host usb device %d.%d is already open\n", bus_num, addr);
1016 return NULL;
1019 return usb_host_device_open_addr(bus_num, addr, product_name);
1022 int usb_host_device_close(const char *devname)
1024 char product_name[PRODUCT_NAME_SZ];
1025 int bus_num, addr;
1026 USBHostDevice *s;
1028 if (strstr(devname, "auto:"))
1029 return usb_host_auto_del(devname);
1031 if (usb_host_find_device(&bus_num, &addr, product_name, sizeof(product_name),
1032 devname) < 0)
1033 return -1;
1035 s = hostdev_find(bus_num, addr);
1036 if (s) {
1037 usb_device_del_addr(0, s->dev.addr);
1038 return 0;
1041 return -1;
1044 static int get_tag_value(char *buf, int buf_size,
1045 const char *str, const char *tag,
1046 const char *stopchars)
1048 const char *p;
1049 char *q;
1050 p = strstr(str, tag);
1051 if (!p)
1052 return -1;
1053 p += strlen(tag);
1054 while (qemu_isspace(*p))
1055 p++;
1056 q = buf;
1057 while (*p != '\0' && !strchr(stopchars, *p)) {
1058 if ((q - buf) < (buf_size - 1))
1059 *q++ = *p;
1060 p++;
1062 *q = '\0';
1063 return q - buf;
1067 * Use /proc/bus/usb/devices or /dev/bus/usb/devices file to determine
1068 * host's USB devices. This is legacy support since many distributions
1069 * are moving to /sys/bus/usb
1071 static int usb_host_scan_dev(void *opaque, USBScanFunc *func)
1073 FILE *f = 0;
1074 char line[1024];
1075 char buf[1024];
1076 int bus_num, addr, speed, device_count, class_id, product_id, vendor_id;
1077 char product_name[512];
1078 int ret = 0;
1080 if (!usb_host_device_path) {
1081 perror("husb: USB Host Device Path not set");
1082 goto the_end;
1084 snprintf(line, sizeof(line), "%s/devices", usb_host_device_path);
1085 f = fopen(line, "r");
1086 if (!f) {
1087 perror("husb: cannot open devices file");
1088 goto the_end;
1091 device_count = 0;
1092 bus_num = addr = speed = class_id = product_id = vendor_id = 0;
1093 for(;;) {
1094 if (fgets(line, sizeof(line), f) == NULL)
1095 break;
1096 if (strlen(line) > 0)
1097 line[strlen(line) - 1] = '\0';
1098 if (line[0] == 'T' && line[1] == ':') {
1099 if (device_count && (vendor_id || product_id)) {
1100 /* New device. Add the previously discovered device. */
1101 ret = func(opaque, bus_num, addr, class_id, vendor_id,
1102 product_id, product_name, speed);
1103 if (ret)
1104 goto the_end;
1106 if (get_tag_value(buf, sizeof(buf), line, "Bus=", " ") < 0)
1107 goto fail;
1108 bus_num = atoi(buf);
1109 if (get_tag_value(buf, sizeof(buf), line, "Dev#=", " ") < 0)
1110 goto fail;
1111 addr = atoi(buf);
1112 if (get_tag_value(buf, sizeof(buf), line, "Spd=", " ") < 0)
1113 goto fail;
1114 if (!strcmp(buf, "480"))
1115 speed = USB_SPEED_HIGH;
1116 else if (!strcmp(buf, "1.5"))
1117 speed = USB_SPEED_LOW;
1118 else
1119 speed = USB_SPEED_FULL;
1120 product_name[0] = '\0';
1121 class_id = 0xff;
1122 device_count++;
1123 product_id = 0;
1124 vendor_id = 0;
1125 } else if (line[0] == 'P' && line[1] == ':') {
1126 if (get_tag_value(buf, sizeof(buf), line, "Vendor=", " ") < 0)
1127 goto fail;
1128 vendor_id = strtoul(buf, NULL, 16);
1129 if (get_tag_value(buf, sizeof(buf), line, "ProdID=", " ") < 0)
1130 goto fail;
1131 product_id = strtoul(buf, NULL, 16);
1132 } else if (line[0] == 'S' && line[1] == ':') {
1133 if (get_tag_value(buf, sizeof(buf), line, "Product=", "") < 0)
1134 goto fail;
1135 pstrcpy(product_name, sizeof(product_name), buf);
1136 } else if (line[0] == 'D' && line[1] == ':') {
1137 if (get_tag_value(buf, sizeof(buf), line, "Cls=", " (") < 0)
1138 goto fail;
1139 class_id = strtoul(buf, NULL, 16);
1141 fail: ;
1143 if (device_count && (vendor_id || product_id)) {
1144 /* Add the last device. */
1145 ret = func(opaque, bus_num, addr, class_id, vendor_id,
1146 product_id, product_name, speed);
1148 the_end:
1149 if (f)
1150 fclose(f);
1151 return ret;
1155 * Read sys file-system device file
1157 * @line address of buffer to put file contents in
1158 * @line_size size of line
1159 * @device_file path to device file (printf format string)
1160 * @device_name device being opened (inserted into device_file)
1162 * @return 0 failed, 1 succeeded ('line' contains data)
1164 static int usb_host_read_file(char *line, size_t line_size, const char *device_file, const char *device_name)
1166 FILE *f;
1167 int ret = 0;
1168 char filename[PATH_MAX];
1170 snprintf(filename, PATH_MAX, device_file, device_name);
1171 f = fopen(filename, "r");
1172 if (f) {
1173 fgets(line, line_size, f);
1174 fclose(f);
1175 ret = 1;
1176 } else {
1177 term_printf("husb: could not open %s\n", filename);
1180 return ret;
1184 * Use /sys/bus/usb/devices/ directory to determine host's USB
1185 * devices.
1187 * This code is based on Robert Schiele's original patches posted to
1188 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1190 static int usb_host_scan_sys(void *opaque, USBScanFunc *func)
1192 DIR *dir = 0;
1193 char line[1024];
1194 int bus_num, addr, speed, class_id, product_id, vendor_id;
1195 int ret = 0;
1196 char product_name[512];
1197 struct dirent *de;
1199 dir = opendir(USBSYSBUS_PATH "/devices");
1200 if (!dir) {
1201 perror("husb: cannot open devices directory");
1202 goto the_end;
1205 while ((de = readdir(dir))) {
1206 if (de->d_name[0] != '.' && !strchr(de->d_name, ':')) {
1207 char *tmpstr = de->d_name;
1208 if (!strncmp(de->d_name, "usb", 3))
1209 tmpstr += 3;
1210 bus_num = atoi(tmpstr);
1212 if (!usb_host_read_file(line, sizeof(line), USBSYSBUS_PATH "/devices/%s/devnum", de->d_name))
1213 goto the_end;
1214 if (sscanf(line, "%d", &addr) != 1)
1215 goto the_end;
1217 if (!usb_host_read_file(line, sizeof(line), USBSYSBUS_PATH "/devices/%s/bDeviceClass", de->d_name))
1218 goto the_end;
1219 if (sscanf(line, "%x", &class_id) != 1)
1220 goto the_end;
1222 if (!usb_host_read_file(line, sizeof(line), USBSYSBUS_PATH "/devices/%s/idVendor", de->d_name))
1223 goto the_end;
1224 if (sscanf(line, "%x", &vendor_id) != 1)
1225 goto the_end;
1227 if (!usb_host_read_file(line, sizeof(line), USBSYSBUS_PATH "/devices/%s/idProduct", de->d_name))
1228 goto the_end;
1229 if (sscanf(line, "%x", &product_id) != 1)
1230 goto the_end;
1232 if (!usb_host_read_file(line, sizeof(line), USBSYSBUS_PATH "/devices/%s/product", de->d_name)) {
1233 *product_name = 0;
1234 } else {
1235 if (strlen(line) > 0)
1236 line[strlen(line) - 1] = '\0';
1237 pstrcpy(product_name, sizeof(product_name), line);
1240 if (!usb_host_read_file(line, sizeof(line), USBSYSBUS_PATH "/devices/%s/speed", de->d_name))
1241 goto the_end;
1242 if (!strcmp(line, "480\n"))
1243 speed = USB_SPEED_HIGH;
1244 else if (!strcmp(line, "1.5\n"))
1245 speed = USB_SPEED_LOW;
1246 else
1247 speed = USB_SPEED_FULL;
1249 ret = func(opaque, bus_num, addr, class_id, vendor_id,
1250 product_id, product_name, speed);
1251 if (ret)
1252 goto the_end;
1255 the_end:
1256 if (dir)
1257 closedir(dir);
1258 return ret;
1262 * Determine how to access the host's USB devices and call the
1263 * specific support function.
1265 static int usb_host_scan(void *opaque, USBScanFunc *func)
1267 FILE *f = 0;
1268 DIR *dir = 0;
1269 int ret = 0;
1270 const char *fs_type[] = {"unknown", "proc", "dev", "sys"};
1271 char devpath[PATH_MAX];
1273 /* only check the host once */
1274 if (!usb_fs_type) {
1275 f = fopen(USBPROCBUS_PATH "/devices", "r");
1276 if (f) {
1277 /* devices found in /proc/bus/usb/ */
1278 strcpy(devpath, USBPROCBUS_PATH);
1279 usb_fs_type = USB_FS_PROC;
1280 fclose(f);
1281 dprintf(USBDBG_DEVOPENED, USBPROCBUS_PATH);
1282 goto found_devices;
1284 /* try additional methods if an access method hasn't been found yet */
1285 f = fopen(USBDEVBUS_PATH "/devices", "r");
1286 if (f) {
1287 /* devices found in /dev/bus/usb/ */
1288 strcpy(devpath, USBDEVBUS_PATH);
1289 usb_fs_type = USB_FS_DEV;
1290 fclose(f);
1291 dprintf(USBDBG_DEVOPENED, USBDEVBUS_PATH);
1292 goto found_devices;
1294 dir = opendir(USBSYSBUS_PATH "/devices");
1295 if (dir) {
1296 /* devices found in /dev/bus/usb/ (yes - not a mistake!) */
1297 strcpy(devpath, USBDEVBUS_PATH);
1298 usb_fs_type = USB_FS_SYS;
1299 closedir(dir);
1300 dprintf(USBDBG_DEVOPENED, USBSYSBUS_PATH);
1301 goto found_devices;
1303 found_devices:
1304 if (!usb_fs_type) {
1305 term_printf("husb: unable to access USB devices\n");
1306 return -ENOENT;
1309 /* the module setting (used later for opening devices) */
1310 usb_host_device_path = qemu_mallocz(strlen(devpath)+1);
1311 if (usb_host_device_path) {
1312 strcpy(usb_host_device_path, devpath);
1313 term_printf("husb: using %s file-system with %s\n", fs_type[usb_fs_type], usb_host_device_path);
1314 } else {
1315 /* out of memory? */
1316 perror("husb: unable to allocate memory for device path");
1317 return -ENOMEM;
1321 switch (usb_fs_type) {
1322 case USB_FS_PROC:
1323 case USB_FS_DEV:
1324 ret = usb_host_scan_dev(opaque, func);
1325 break;
1326 case USB_FS_SYS:
1327 ret = usb_host_scan_sys(opaque, func);
1328 break;
1329 default:
1330 ret = -EINVAL;
1331 break;
1333 return ret;
1336 struct USBAutoFilter {
1337 struct USBAutoFilter *next;
1338 int bus_num;
1339 int addr;
1340 int vendor_id;
1341 int product_id;
1344 static QEMUTimer *usb_auto_timer;
1345 static struct USBAutoFilter *usb_auto_filter;
1347 static int usb_host_auto_scan(void *opaque, int bus_num, int addr,
1348 int class_id, int vendor_id, int product_id,
1349 const char *product_name, int speed)
1351 struct USBAutoFilter *f;
1352 struct USBDevice *dev;
1354 /* Ignore hubs */
1355 if (class_id == 9)
1356 return 0;
1358 for (f = usb_auto_filter; f; f = f->next) {
1359 if (f->bus_num >= 0 && f->bus_num != bus_num)
1360 continue;
1362 if (f->addr >= 0 && f->addr != addr)
1363 continue;
1365 if (f->vendor_id >= 0 && f->vendor_id != vendor_id)
1366 continue;
1368 if (f->product_id >= 0 && f->product_id != product_id)
1369 continue;
1371 /* We got a match */
1373 /* Allredy attached ? */
1374 if (hostdev_find(bus_num, addr))
1375 return 0;
1377 dprintf("husb: auto open: bus_num %d addr %d\n", bus_num, addr);
1379 dev = usb_host_device_open_addr(bus_num, addr, product_name);
1380 if (dev)
1381 usb_device_add_dev(dev);
1384 return 0;
1387 static void usb_host_auto_timer(void *unused)
1389 usb_host_scan(NULL, usb_host_auto_scan);
1390 qemu_mod_timer(usb_auto_timer, qemu_get_clock(rt_clock) + 2000);
1394 * Autoconnect filter
1395 * Format:
1396 * auto:bus:dev[:vid:pid]
1397 * auto:bus.dev[:vid:pid]
1399 * bus - bus number (dec, * means any)
1400 * dev - device number (dec, * means any)
1401 * vid - vendor id (hex, * means any)
1402 * pid - product id (hex, * means any)
1404 * See 'lsusb' output.
1406 static int parse_filter(const char *spec, struct USBAutoFilter *f)
1408 enum { BUS, DEV, VID, PID, DONE };
1409 const char *p = spec;
1410 int i;
1412 f->bus_num = -1;
1413 f->addr = -1;
1414 f->vendor_id = -1;
1415 f->product_id = -1;
1417 for (i = BUS; i < DONE; i++) {
1418 p = strpbrk(p, ":.");
1419 if (!p) break;
1420 p++;
1422 if (*p == '*')
1423 continue;
1425 switch(i) {
1426 case BUS: f->bus_num = strtol(p, NULL, 10); break;
1427 case DEV: f->addr = strtol(p, NULL, 10); break;
1428 case VID: f->vendor_id = strtol(p, NULL, 16); break;
1429 case PID: f->product_id = strtol(p, NULL, 16); break;
1433 if (i < DEV) {
1434 fprintf(stderr, "husb: invalid auto filter spec %s\n", spec);
1435 return -1;
1438 return 0;
1441 static int match_filter(const struct USBAutoFilter *f1,
1442 const struct USBAutoFilter *f2)
1444 return f1->bus_num == f2->bus_num &&
1445 f1->addr == f2->addr &&
1446 f1->vendor_id == f2->vendor_id &&
1447 f1->product_id == f2->product_id;
1450 static int usb_host_auto_add(const char *spec)
1452 struct USBAutoFilter filter, *f;
1454 if (parse_filter(spec, &filter) < 0)
1455 return -1;
1457 f = qemu_mallocz(sizeof(*f));
1458 if (!f) {
1459 fprintf(stderr, "husb: failed to allocate auto filter\n");
1460 return -1;
1463 *f = filter;
1465 if (!usb_auto_filter) {
1467 * First entry. Init and start the monitor.
1468 * Right now we're using timer to check for new devices.
1469 * If this turns out to be too expensive we can move that into a
1470 * separate thread.
1472 usb_auto_timer = qemu_new_timer(rt_clock, usb_host_auto_timer, NULL);
1473 if (!usb_auto_timer) {
1474 fprintf(stderr, "husb: failed to allocate auto scan timer\n");
1475 qemu_free(f);
1476 return -1;
1479 /* Check for new devices every two seconds */
1480 qemu_mod_timer(usb_auto_timer, qemu_get_clock(rt_clock) + 2000);
1483 dprintf("husb: added auto filter: bus_num %d addr %d vid %d pid %d\n",
1484 f->bus_num, f->addr, f->vendor_id, f->product_id);
1486 f->next = usb_auto_filter;
1487 usb_auto_filter = f;
1489 return 0;
1492 static int usb_host_auto_del(const char *spec)
1494 struct USBAutoFilter *pf = usb_auto_filter;
1495 struct USBAutoFilter **prev = &usb_auto_filter;
1496 struct USBAutoFilter filter;
1498 if (parse_filter(spec, &filter) < 0)
1499 return -1;
1501 while (pf) {
1502 if (match_filter(pf, &filter)) {
1503 dprintf("husb: removed auto filter: bus_num %d addr %d vid %d pid %d\n",
1504 pf->bus_num, pf->addr, pf->vendor_id, pf->product_id);
1506 *prev = pf->next;
1508 if (!usb_auto_filter) {
1509 /* No more filters. Stop scanning. */
1510 qemu_del_timer(usb_auto_timer);
1511 qemu_free_timer(usb_auto_timer);
1514 return 0;
1517 prev = &pf->next;
1518 pf = pf->next;
1521 return -1;
1524 typedef struct FindDeviceState {
1525 int vendor_id;
1526 int product_id;
1527 int bus_num;
1528 int addr;
1529 char product_name[PRODUCT_NAME_SZ];
1530 } FindDeviceState;
1532 static int usb_host_find_device_scan(void *opaque, int bus_num, int addr,
1533 int class_id,
1534 int vendor_id, int product_id,
1535 const char *product_name, int speed)
1537 FindDeviceState *s = opaque;
1538 if ((vendor_id == s->vendor_id &&
1539 product_id == s->product_id) ||
1540 (bus_num == s->bus_num &&
1541 addr == s->addr)) {
1542 pstrcpy(s->product_name, PRODUCT_NAME_SZ, product_name);
1543 s->bus_num = bus_num;
1544 s->addr = addr;
1545 return 1;
1546 } else {
1547 return 0;
1551 /* the syntax is :
1552 'bus.addr' (decimal numbers) or
1553 'vendor_id:product_id' (hexa numbers) */
1554 static int usb_host_find_device(int *pbus_num, int *paddr,
1555 char *product_name, int product_name_size,
1556 const char *devname)
1558 const char *p;
1559 int ret;
1560 FindDeviceState fs;
1562 p = strchr(devname, '.');
1563 if (p) {
1564 *pbus_num = strtoul(devname, NULL, 0);
1565 *paddr = strtoul(p + 1, NULL, 0);
1566 fs.bus_num = *pbus_num;
1567 fs.addr = *paddr;
1568 ret = usb_host_scan(&fs, usb_host_find_device_scan);
1569 if (ret)
1570 pstrcpy(product_name, product_name_size, fs.product_name);
1571 return 0;
1574 p = strchr(devname, ':');
1575 if (p) {
1576 fs.vendor_id = strtoul(devname, NULL, 16);
1577 fs.product_id = strtoul(p + 1, NULL, 16);
1578 ret = usb_host_scan(&fs, usb_host_find_device_scan);
1579 if (ret) {
1580 *pbus_num = fs.bus_num;
1581 *paddr = fs.addr;
1582 pstrcpy(product_name, product_name_size, fs.product_name);
1583 return 0;
1586 return -1;
1589 /**********************/
1590 /* USB host device info */
1592 struct usb_class_info {
1593 int class;
1594 const char *class_name;
1597 static const struct usb_class_info usb_class_info[] = {
1598 { USB_CLASS_AUDIO, "Audio"},
1599 { USB_CLASS_COMM, "Communication"},
1600 { USB_CLASS_HID, "HID"},
1601 { USB_CLASS_HUB, "Hub" },
1602 { USB_CLASS_PHYSICAL, "Physical" },
1603 { USB_CLASS_PRINTER, "Printer" },
1604 { USB_CLASS_MASS_STORAGE, "Storage" },
1605 { USB_CLASS_CDC_DATA, "Data" },
1606 { USB_CLASS_APP_SPEC, "Application Specific" },
1607 { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
1608 { USB_CLASS_STILL_IMAGE, "Still Image" },
1609 { USB_CLASS_CSCID, "Smart Card" },
1610 { USB_CLASS_CONTENT_SEC, "Content Security" },
1611 { -1, NULL }
1614 static const char *usb_class_str(uint8_t class)
1616 const struct usb_class_info *p;
1617 for(p = usb_class_info; p->class != -1; p++) {
1618 if (p->class == class)
1619 break;
1621 return p->class_name;
1624 static void usb_info_device(int bus_num, int addr, int class_id,
1625 int vendor_id, int product_id,
1626 const char *product_name,
1627 int speed)
1629 const char *class_str, *speed_str;
1631 switch(speed) {
1632 case USB_SPEED_LOW:
1633 speed_str = "1.5";
1634 break;
1635 case USB_SPEED_FULL:
1636 speed_str = "12";
1637 break;
1638 case USB_SPEED_HIGH:
1639 speed_str = "480";
1640 break;
1641 default:
1642 speed_str = "?";
1643 break;
1646 term_printf(" Device %d.%d, speed %s Mb/s\n",
1647 bus_num, addr, speed_str);
1648 class_str = usb_class_str(class_id);
1649 if (class_str)
1650 term_printf(" %s:", class_str);
1651 else
1652 term_printf(" Class %02x:", class_id);
1653 term_printf(" USB device %04x:%04x", vendor_id, product_id);
1654 if (product_name[0] != '\0')
1655 term_printf(", %s", product_name);
1656 term_printf("\n");
1659 static int usb_host_info_device(void *opaque, int bus_num, int addr,
1660 int class_id,
1661 int vendor_id, int product_id,
1662 const char *product_name,
1663 int speed)
1665 usb_info_device(bus_num, addr, class_id, vendor_id, product_id,
1666 product_name, speed);
1667 return 0;
1670 static void dec2str(int val, char *str, size_t size)
1672 if (val == -1)
1673 snprintf(str, size, "*");
1674 else
1675 snprintf(str, size, "%d", val);
1678 static void hex2str(int val, char *str, size_t size)
1680 if (val == -1)
1681 snprintf(str, size, "*");
1682 else
1683 snprintf(str, size, "%x", val);
1686 void usb_host_info(void)
1688 struct USBAutoFilter *f;
1690 usb_host_scan(NULL, usb_host_info_device);
1692 if (usb_auto_filter)
1693 term_printf(" Auto filters:\n");
1694 for (f = usb_auto_filter; f; f = f->next) {
1695 char bus[10], addr[10], vid[10], pid[10];
1696 dec2str(f->bus_num, bus, sizeof(bus));
1697 dec2str(f->addr, addr, sizeof(addr));
1698 hex2str(f->vendor_id, vid, sizeof(vid));
1699 hex2str(f->product_id, pid, sizeof(pid));
1700 term_printf(" Device %s.%s ID %s:%s\n", bus, addr, vid, pid);