kvm: testsuite: rename apic.h to fake-apic.h
[qemu-kvm/fedora.git] / usb-linux.c
blob60297e37363d0abe9352af791c58140ec009b052
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
40 #include <dirent.h>
41 #include <sys/ioctl.h>
42 #include <signal.h>
44 #include <linux/usbdevice_fs.h>
45 #include <linux/version.h>
46 #include "hw/usb.h"
48 /* We redefine it to avoid version problems */
49 struct usb_ctrltransfer {
50 uint8_t bRequestType;
51 uint8_t bRequest;
52 uint16_t wValue;
53 uint16_t wIndex;
54 uint16_t wLength;
55 uint32_t timeout;
56 void *data;
59 struct usb_ctrlrequest {
60 uint8_t bRequestType;
61 uint8_t bRequest;
62 uint16_t wValue;
63 uint16_t wIndex;
64 uint16_t wLength;
67 typedef int USBScanFunc(void *opaque, int bus_num, int addr, int class_id,
68 int vendor_id, int product_id,
69 const char *product_name, int speed);
70 static int usb_host_find_device(int *pbus_num, int *paddr,
71 char *product_name, int product_name_size,
72 const char *devname);
73 //#define DEBUG
75 #ifdef DEBUG
76 #define dprintf printf
77 #else
78 #define dprintf(...)
79 #endif
81 #define USBDBG_DEVOPENED "husb: opened %s/devices\n"
83 #define USBPROCBUS_PATH "/proc/bus/usb"
84 #define PRODUCT_NAME_SZ 32
85 #define MAX_ENDPOINTS 16
86 #define USBDEVBUS_PATH "/dev/bus/usb"
87 #define USBSYSBUS_PATH "/sys/bus/usb"
89 static char *usb_host_device_path;
91 #define USB_FS_NONE 0
92 #define USB_FS_PROC 1
93 #define USB_FS_DEV 2
94 #define USB_FS_SYS 3
96 static int usb_fs_type;
98 /* endpoint association data */
99 struct endp_data {
100 uint8_t type;
101 uint8_t halted;
104 enum {
105 CTRL_STATE_IDLE = 0,
106 CTRL_STATE_SETUP,
107 CTRL_STATE_DATA,
108 CTRL_STATE_ACK
112 * Control transfer state.
113 * Note that 'buffer' _must_ follow 'req' field because
114 * we need contigious buffer when we submit control URB.
116 struct ctrl_struct {
117 uint16_t len;
118 uint16_t offset;
119 uint8_t state;
120 struct usb_ctrlrequest req;
121 uint8_t buffer[1024];
124 typedef struct USBHostDevice {
125 USBDevice dev;
126 int fd;
128 uint8_t descr[1024];
129 int descr_len;
130 int configuration;
131 int ninterfaces;
132 int closing;
134 struct ctrl_struct ctrl;
135 struct endp_data endp_table[MAX_ENDPOINTS];
137 /* Host side address */
138 int bus_num;
139 int addr;
141 struct USBHostDevice *next;
142 } USBHostDevice;
144 static int is_isoc(USBHostDevice *s, int ep)
146 return s->endp_table[ep - 1].type == USBDEVFS_URB_TYPE_ISO;
149 static int is_halted(USBHostDevice *s, int ep)
151 return s->endp_table[ep - 1].halted;
154 static void clear_halt(USBHostDevice *s, int ep)
156 s->endp_table[ep - 1].halted = 0;
159 static void set_halt(USBHostDevice *s, int ep)
161 s->endp_table[ep - 1].halted = 1;
164 static USBHostDevice *hostdev_list;
166 static void hostdev_link(USBHostDevice *dev)
168 dev->next = hostdev_list;
169 hostdev_list = dev;
172 static void hostdev_unlink(USBHostDevice *dev)
174 USBHostDevice *pdev = hostdev_list;
175 USBHostDevice **prev = &hostdev_list;
177 while (pdev) {
178 if (pdev == dev) {
179 *prev = dev->next;
180 return;
183 prev = &pdev->next;
184 pdev = pdev->next;
188 static USBHostDevice *hostdev_find(int bus_num, int addr)
190 USBHostDevice *s = hostdev_list;
191 while (s) {
192 if (s->bus_num == bus_num && s->addr == addr)
193 return s;
194 s = s->next;
196 return NULL;
200 * Async URB state.
201 * We always allocate one isoc descriptor even for bulk transfers
202 * to simplify allocation and casts.
204 typedef struct AsyncURB
206 struct usbdevfs_urb urb;
207 struct usbdevfs_iso_packet_desc isocpd;
209 USBPacket *packet;
210 USBHostDevice *hdev;
211 } AsyncURB;
213 static AsyncURB *async_alloc(void)
215 return (AsyncURB *) qemu_mallocz(sizeof(AsyncURB));
218 static void async_free(AsyncURB *aurb)
220 qemu_free(aurb);
223 static void async_complete_ctrl(USBHostDevice *s, USBPacket *p)
225 switch(s->ctrl.state) {
226 case CTRL_STATE_SETUP:
227 if (p->len < s->ctrl.len)
228 s->ctrl.len = p->len;
229 s->ctrl.state = CTRL_STATE_DATA;
230 p->len = 8;
231 break;
233 case CTRL_STATE_ACK:
234 s->ctrl.state = CTRL_STATE_IDLE;
235 p->len = 0;
236 break;
238 default:
239 break;
243 static void async_complete(void *opaque)
245 USBHostDevice *s = opaque;
246 AsyncURB *aurb;
248 while (1) {
249 USBPacket *p;
251 int r = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &aurb);
252 if (r < 0) {
253 if (errno == EAGAIN)
254 return;
256 if (errno == ENODEV && !s->closing) {
257 printf("husb: device %d.%d disconnected\n", s->bus_num, s->addr);
258 usb_device_del_addr(0, s->dev.addr);
259 return;
262 dprintf("husb: async. reap urb failed errno %d\n", errno);
263 return;
266 p = aurb->packet;
268 dprintf("husb: async completed. aurb %p status %d alen %d\n",
269 aurb, aurb->urb.status, aurb->urb.actual_length);
271 if (p) {
272 switch (aurb->urb.status) {
273 case 0:
274 p->len = aurb->urb.actual_length;
275 if (aurb->urb.type == USBDEVFS_URB_TYPE_CONTROL)
276 async_complete_ctrl(s, p);
277 break;
279 case -EPIPE:
280 set_halt(s, p->devep);
281 /* fall through */
282 default:
283 p->len = USB_RET_NAK;
284 break;
287 usb_packet_complete(p);
290 async_free(aurb);
294 static void async_cancel(USBPacket *unused, void *opaque)
296 AsyncURB *aurb = opaque;
297 USBHostDevice *s = aurb->hdev;
299 dprintf("husb: async cancel. aurb %p\n", aurb);
301 /* Mark it as dead (see async_complete above) */
302 aurb->packet = NULL;
304 int r = ioctl(s->fd, USBDEVFS_DISCARDURB, aurb);
305 if (r < 0) {
306 dprintf("husb: async. discard urb failed errno %d\n", errno);
310 static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration)
312 int dev_descr_len, config_descr_len;
313 int interface, nb_interfaces, nb_configurations;
314 int ret, i;
316 if (configuration == 0) /* address state - ignore */
317 return 1;
319 dprintf("husb: claiming interfaces. config %d\n", configuration);
321 i = 0;
322 dev_descr_len = dev->descr[0];
323 if (dev_descr_len > dev->descr_len)
324 goto fail;
325 nb_configurations = dev->descr[17];
327 i += dev_descr_len;
328 while (i < dev->descr_len) {
329 dprintf("husb: i is %d, descr_len is %d, dl %d, dt %d\n", i, dev->descr_len,
330 dev->descr[i], dev->descr[i+1]);
332 if (dev->descr[i+1] != USB_DT_CONFIG) {
333 i += dev->descr[i];
334 continue;
336 config_descr_len = dev->descr[i];
338 printf("husb: config #%d need %d\n", dev->descr[i + 5], configuration);
340 if (configuration < 0 || configuration == dev->descr[i + 5]) {
341 configuration = dev->descr[i + 5];
342 break;
345 i += config_descr_len;
348 if (i >= dev->descr_len) {
349 fprintf(stderr, "husb: update iface failed. no matching configuration\n");
350 goto fail;
352 nb_interfaces = dev->descr[i + 4];
354 #ifdef USBDEVFS_DISCONNECT
355 /* earlier Linux 2.4 do not support that */
357 struct usbdevfs_ioctl ctrl;
358 for (interface = 0; interface < nb_interfaces; interface++) {
359 ctrl.ioctl_code = USBDEVFS_DISCONNECT;
360 ctrl.ifno = interface;
361 ret = ioctl(dev->fd, USBDEVFS_IOCTL, &ctrl);
362 if (ret < 0 && errno != ENODATA) {
363 perror("USBDEVFS_DISCONNECT");
364 goto fail;
368 #endif
370 /* XXX: only grab if all interfaces are free */
371 for (interface = 0; interface < nb_interfaces; interface++) {
372 ret = ioctl(dev->fd, USBDEVFS_CLAIMINTERFACE, &interface);
373 if (ret < 0) {
374 if (errno == EBUSY) {
375 printf("husb: update iface. device already grabbed\n");
376 } else {
377 perror("husb: failed to claim interface");
379 fail:
380 return 0;
384 printf("husb: %d interfaces claimed for configuration %d\n",
385 nb_interfaces, configuration);
387 dev->ninterfaces = nb_interfaces;
388 dev->configuration = configuration;
389 return 1;
392 static int usb_host_release_interfaces(USBHostDevice *s)
394 int ret, i;
396 dprintf("husb: releasing interfaces\n");
398 for (i = 0; i < s->ninterfaces; i++) {
399 ret = ioctl(s->fd, USBDEVFS_RELEASEINTERFACE, &i);
400 if (ret < 0) {
401 perror("husb: failed to release interface");
402 return 0;
406 return 1;
409 static void usb_host_handle_reset(USBDevice *dev)
411 USBHostDevice *s = (USBHostDevice *) dev;
413 dprintf("husb: reset device %u.%u\n", s->bus_num, s->addr);
415 ioctl(s->fd, USBDEVFS_RESET);
417 usb_host_claim_interfaces(s, s->configuration);
420 static void usb_host_handle_destroy(USBDevice *dev)
422 USBHostDevice *s = (USBHostDevice *)dev;
424 s->closing = 1;
426 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
428 hostdev_unlink(s);
430 async_complete(s);
432 if (s->fd >= 0)
433 close(s->fd);
435 qemu_free(s);
438 static int usb_linux_update_endp_table(USBHostDevice *s);
440 static int usb_host_handle_data(USBHostDevice *s, USBPacket *p)
442 struct usbdevfs_urb *urb;
443 AsyncURB *aurb;
444 int ret;
446 aurb = async_alloc();
447 if (!aurb) {
448 dprintf("husb: async malloc failed\n");
449 return USB_RET_NAK;
451 aurb->hdev = s;
452 aurb->packet = p;
454 urb = &aurb->urb;
456 if (p->pid == USB_TOKEN_IN)
457 urb->endpoint = p->devep | 0x80;
458 else
459 urb->endpoint = p->devep;
461 if (is_halted(s, p->devep)) {
462 ret = ioctl(s->fd, USBDEVFS_CLEAR_HALT, &urb->endpoint);
463 if (ret < 0) {
464 dprintf("husb: failed to clear halt. ep 0x%x errno %d\n",
465 urb->endpoint, errno);
466 return USB_RET_NAK;
468 clear_halt(s, p->devep);
471 urb->buffer = p->data;
472 urb->buffer_length = p->len;
474 if (is_isoc(s, p->devep)) {
475 /* Setup ISOC transfer */
476 urb->type = USBDEVFS_URB_TYPE_ISO;
477 urb->flags = USBDEVFS_URB_ISO_ASAP;
478 urb->number_of_packets = 1;
479 urb->iso_frame_desc[0].length = p->len;
480 } else {
481 /* Setup bulk transfer */
482 urb->type = USBDEVFS_URB_TYPE_BULK;
485 urb->usercontext = s;
487 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
489 dprintf("husb: data submit. ep 0x%x len %u aurb %p\n", urb->endpoint, p->len, aurb);
491 if (ret < 0) {
492 dprintf("husb: submit failed. errno %d\n", errno);
493 async_free(aurb);
495 switch(errno) {
496 case ETIMEDOUT:
497 return USB_RET_NAK;
498 case EPIPE:
499 default:
500 return USB_RET_STALL;
504 usb_defer_packet(p, async_cancel, aurb);
505 return USB_RET_ASYNC;
508 static int ctrl_error(void)
510 if (errno == ETIMEDOUT)
511 return USB_RET_NAK;
512 else
513 return USB_RET_STALL;
516 static int usb_host_set_address(USBHostDevice *s, int addr)
518 dprintf("husb: ctrl set addr %u\n", addr);
519 s->dev.addr = addr;
520 return 0;
523 static int usb_host_set_config(USBHostDevice *s, int config)
525 usb_host_release_interfaces(s);
527 int ret = ioctl(s->fd, USBDEVFS_SETCONFIGURATION, &config);
529 dprintf("husb: ctrl set config %d ret %d errno %d\n", config, ret, errno);
531 if (ret < 0)
532 return ctrl_error();
534 usb_host_claim_interfaces(s, config);
535 return 0;
538 static int usb_host_set_interface(USBHostDevice *s, int iface, int alt)
540 struct usbdevfs_setinterface si;
541 int ret;
543 si.interface = iface;
544 si.altsetting = alt;
545 ret = ioctl(s->fd, USBDEVFS_SETINTERFACE, &si);
547 dprintf("husb: ctrl set iface %d altset %d ret %d errno %d\n",
548 iface, alt, ret, errno);
550 if (ret < 0)
551 return ctrl_error();
553 usb_linux_update_endp_table(s);
554 return 0;
557 static int usb_host_handle_control(USBHostDevice *s, USBPacket *p)
559 struct usbdevfs_urb *urb;
560 AsyncURB *aurb;
561 int ret, value, index;
564 * Process certain standard device requests.
565 * These are infrequent and are processed synchronously.
567 value = le16_to_cpu(s->ctrl.req.wValue);
568 index = le16_to_cpu(s->ctrl.req.wIndex);
570 dprintf("husb: ctrl type 0x%x req 0x%x val 0x%x index %u len %u\n",
571 s->ctrl.req.bRequestType, s->ctrl.req.bRequest, value, index,
572 s->ctrl.len);
574 if (s->ctrl.req.bRequestType == 0) {
575 switch (s->ctrl.req.bRequest) {
576 case USB_REQ_SET_ADDRESS:
577 return usb_host_set_address(s, value);
579 case USB_REQ_SET_CONFIGURATION:
580 return usb_host_set_config(s, value & 0xff);
584 if (s->ctrl.req.bRequestType == 1 &&
585 s->ctrl.req.bRequest == USB_REQ_SET_INTERFACE)
586 return usb_host_set_interface(s, index, value);
588 /* The rest are asynchronous */
590 aurb = async_alloc();
591 if (!aurb) {
592 dprintf("husb: async malloc failed\n");
593 return USB_RET_NAK;
595 aurb->hdev = s;
596 aurb->packet = p;
599 * Setup ctrl transfer.
601 * s->ctrl is layed out such that data buffer immediately follows
602 * 'req' struct which is exactly what usbdevfs expects.
604 urb = &aurb->urb;
606 urb->type = USBDEVFS_URB_TYPE_CONTROL;
607 urb->endpoint = p->devep;
609 urb->buffer = &s->ctrl.req;
610 urb->buffer_length = 8 + s->ctrl.len;
612 urb->usercontext = s;
614 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
616 dprintf("husb: submit ctrl. len %u aurb %p\n", urb->buffer_length, aurb);
618 if (ret < 0) {
619 dprintf("husb: submit failed. errno %d\n", errno);
620 async_free(aurb);
622 switch(errno) {
623 case ETIMEDOUT:
624 return USB_RET_NAK;
625 case EPIPE:
626 default:
627 return USB_RET_STALL;
631 usb_defer_packet(p, async_cancel, aurb);
632 return USB_RET_ASYNC;
635 static int do_token_setup(USBDevice *dev, USBPacket *p)
637 USBHostDevice *s = (USBHostDevice *) dev;
638 int ret = 0;
640 if (p->len != 8)
641 return USB_RET_STALL;
643 memcpy(&s->ctrl.req, p->data, 8);
644 s->ctrl.len = le16_to_cpu(s->ctrl.req.wLength);
645 s->ctrl.offset = 0;
646 s->ctrl.state = CTRL_STATE_SETUP;
648 if (s->ctrl.req.bRequestType & USB_DIR_IN) {
649 ret = usb_host_handle_control(s, p);
650 if (ret < 0)
651 return ret;
653 if (ret < s->ctrl.len)
654 s->ctrl.len = ret;
655 s->ctrl.state = CTRL_STATE_DATA;
656 } else {
657 if (s->ctrl.len == 0)
658 s->ctrl.state = CTRL_STATE_ACK;
659 else
660 s->ctrl.state = CTRL_STATE_DATA;
663 return ret;
666 static int do_token_in(USBDevice *dev, USBPacket *p)
668 USBHostDevice *s = (USBHostDevice *) dev;
669 int ret = 0;
671 if (p->devep != 0)
672 return usb_host_handle_data(s, p);
674 switch(s->ctrl.state) {
675 case CTRL_STATE_ACK:
676 if (!(s->ctrl.req.bRequestType & USB_DIR_IN)) {
677 ret = usb_host_handle_control(s, p);
678 if (ret == USB_RET_ASYNC)
679 return USB_RET_ASYNC;
681 s->ctrl.state = CTRL_STATE_IDLE;
682 return ret > 0 ? 0 : ret;
685 return 0;
687 case CTRL_STATE_DATA:
688 if (s->ctrl.req.bRequestType & USB_DIR_IN) {
689 int len = s->ctrl.len - s->ctrl.offset;
690 if (len > p->len)
691 len = p->len;
692 memcpy(p->data, s->ctrl.buffer + s->ctrl.offset, len);
693 s->ctrl.offset += len;
694 if (s->ctrl.offset >= s->ctrl.len)
695 s->ctrl.state = CTRL_STATE_ACK;
696 return len;
699 s->ctrl.state = CTRL_STATE_IDLE;
700 return USB_RET_STALL;
702 default:
703 return USB_RET_STALL;
707 static int do_token_out(USBDevice *dev, USBPacket *p)
709 USBHostDevice *s = (USBHostDevice *) dev;
711 if (p->devep != 0)
712 return usb_host_handle_data(s, p);
714 switch(s->ctrl.state) {
715 case CTRL_STATE_ACK:
716 if (s->ctrl.req.bRequestType & USB_DIR_IN) {
717 s->ctrl.state = CTRL_STATE_IDLE;
718 /* transfer OK */
719 } else {
720 /* ignore additional output */
722 return 0;
724 case CTRL_STATE_DATA:
725 if (!(s->ctrl.req.bRequestType & USB_DIR_IN)) {
726 int len = s->ctrl.len - s->ctrl.offset;
727 if (len > p->len)
728 len = p->len;
729 memcpy(s->ctrl.buffer + s->ctrl.offset, p->data, len);
730 s->ctrl.offset += len;
731 if (s->ctrl.offset >= s->ctrl.len)
732 s->ctrl.state = CTRL_STATE_ACK;
733 return len;
736 s->ctrl.state = CTRL_STATE_IDLE;
737 return USB_RET_STALL;
739 default:
740 return USB_RET_STALL;
745 * Packet handler.
746 * Called by the HC (host controller).
748 * Returns length of the transaction or one of the USB_RET_XXX codes.
750 static int usb_host_handle_packet(USBDevice *s, USBPacket *p)
752 switch(p->pid) {
753 case USB_MSG_ATTACH:
754 s->state = USB_STATE_ATTACHED;
755 return 0;
757 case USB_MSG_DETACH:
758 s->state = USB_STATE_NOTATTACHED;
759 return 0;
761 case USB_MSG_RESET:
762 s->remote_wakeup = 0;
763 s->addr = 0;
764 s->state = USB_STATE_DEFAULT;
765 s->handle_reset(s);
766 return 0;
769 /* Rest of the PIDs must match our address */
770 if (s->state < USB_STATE_DEFAULT || p->devaddr != s->addr)
771 return USB_RET_NODEV;
773 switch (p->pid) {
774 case USB_TOKEN_SETUP:
775 return do_token_setup(s, p);
777 case USB_TOKEN_IN:
778 return do_token_in(s, p);
780 case USB_TOKEN_OUT:
781 return do_token_out(s, p);
783 default:
784 return USB_RET_STALL;
788 /* returns 1 on problem encountered or 0 for success */
789 static int usb_linux_update_endp_table(USBHostDevice *s)
791 uint8_t *descriptors;
792 uint8_t devep, type, configuration, alt_interface;
793 struct usb_ctrltransfer ct;
794 int interface, ret, length, i;
796 ct.bRequestType = USB_DIR_IN;
797 ct.bRequest = USB_REQ_GET_CONFIGURATION;
798 ct.wValue = 0;
799 ct.wIndex = 0;
800 ct.wLength = 1;
801 ct.data = &configuration;
802 ct.timeout = 50;
804 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
805 if (ret < 0) {
806 perror("usb_linux_update_endp_table");
807 return 1;
810 /* in address state */
811 if (configuration == 0)
812 return 1;
814 /* get the desired configuration, interface, and endpoint descriptors
815 * from device description */
816 descriptors = &s->descr[18];
817 length = s->descr_len - 18;
818 i = 0;
820 if (descriptors[i + 1] != USB_DT_CONFIG ||
821 descriptors[i + 5] != configuration) {
822 dprintf("invalid descriptor data - configuration\n");
823 return 1;
825 i += descriptors[i];
827 while (i < length) {
828 if (descriptors[i + 1] != USB_DT_INTERFACE ||
829 (descriptors[i + 1] == USB_DT_INTERFACE &&
830 descriptors[i + 4] == 0)) {
831 i += descriptors[i];
832 continue;
835 interface = descriptors[i + 2];
837 ct.bRequestType = USB_DIR_IN | USB_RECIP_INTERFACE;
838 ct.bRequest = USB_REQ_GET_INTERFACE;
839 ct.wValue = 0;
840 ct.wIndex = interface;
841 ct.wLength = 1;
842 ct.data = &alt_interface;
843 ct.timeout = 50;
845 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
846 if (ret < 0) {
847 perror("usb_linux_update_endp_table");
848 return 1;
851 /* the current interface descriptor is the active interface
852 * and has endpoints */
853 if (descriptors[i + 3] != alt_interface) {
854 i += descriptors[i];
855 continue;
858 /* advance to the endpoints */
859 while (i < length && descriptors[i +1] != USB_DT_ENDPOINT)
860 i += descriptors[i];
862 if (i >= length)
863 break;
865 while (i < length) {
866 if (descriptors[i + 1] != USB_DT_ENDPOINT)
867 break;
869 devep = descriptors[i + 2];
870 switch (descriptors[i + 3] & 0x3) {
871 case 0x00:
872 type = USBDEVFS_URB_TYPE_CONTROL;
873 break;
874 case 0x01:
875 type = USBDEVFS_URB_TYPE_ISO;
876 break;
877 case 0x02:
878 type = USBDEVFS_URB_TYPE_BULK;
879 break;
880 case 0x03:
881 type = USBDEVFS_URB_TYPE_INTERRUPT;
882 break;
883 default:
884 dprintf("usb_host: malformed endpoint type\n");
885 type = USBDEVFS_URB_TYPE_BULK;
887 s->endp_table[(devep & 0xf) - 1].type = type;
888 s->endp_table[(devep & 0xf) - 1].halted = 0;
890 i += descriptors[i];
893 return 0;
896 static USBDevice *usb_host_device_open_addr(int bus_num, int addr, const char *prod_name)
898 int fd = -1, ret;
899 USBHostDevice *dev = NULL;
900 struct usbdevfs_connectinfo ci;
901 char buf[1024];
903 dev = qemu_mallocz(sizeof(USBHostDevice));
904 if (!dev)
905 goto fail;
907 dev->bus_num = bus_num;
908 dev->addr = addr;
910 printf("husb: open device %d.%d\n", bus_num, addr);
912 if (!usb_host_device_path) {
913 perror("husb: USB Host Device Path not set");
914 goto fail;
916 snprintf(buf, sizeof(buf), "%s/%03d/%03d", usb_host_device_path,
917 bus_num, addr);
918 fd = open(buf, O_RDWR | O_NONBLOCK);
919 if (fd < 0) {
920 perror(buf);
921 goto fail;
923 dprintf("husb: opened %s\n", buf);
925 /* read the device description */
926 dev->descr_len = read(fd, dev->descr, sizeof(dev->descr));
927 if (dev->descr_len <= 0) {
928 perror("husb: reading device data failed");
929 goto fail;
932 #ifdef DEBUG
934 int x;
935 printf("=== begin dumping device descriptor data ===\n");
936 for (x = 0; x < dev->descr_len; x++)
937 printf("%02x ", dev->descr[x]);
938 printf("\n=== end dumping device descriptor data ===\n");
940 #endif
942 dev->fd = fd;
945 * Initial configuration is -1 which makes us claim first
946 * available config. We used to start with 1, which does not
947 * always work. I've seen devices where first config starts
948 * with 2.
950 if (!usb_host_claim_interfaces(dev, -1))
951 goto fail;
953 ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci);
954 if (ret < 0) {
955 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
956 goto fail;
959 printf("husb: grabbed usb device %d.%d\n", bus_num, addr);
961 ret = usb_linux_update_endp_table(dev);
962 if (ret)
963 goto fail;
965 if (ci.slow)
966 dev->dev.speed = USB_SPEED_LOW;
967 else
968 dev->dev.speed = USB_SPEED_HIGH;
970 dev->dev.handle_packet = usb_host_handle_packet;
971 dev->dev.handle_reset = usb_host_handle_reset;
972 dev->dev.handle_destroy = usb_host_handle_destroy;
974 if (!prod_name || prod_name[0] == '\0')
975 snprintf(dev->dev.devname, sizeof(dev->dev.devname),
976 "host:%d.%d", bus_num, addr);
977 else
978 pstrcpy(dev->dev.devname, sizeof(dev->dev.devname),
979 prod_name);
981 /* USB devio uses 'write' flag to check for async completions */
982 qemu_set_fd_handler(dev->fd, NULL, async_complete, dev);
984 hostdev_link(dev);
986 return (USBDevice *) dev;
988 fail:
989 if (dev)
990 qemu_free(dev);
992 close(fd);
993 return NULL;
996 static int usb_host_auto_add(const char *spec);
997 static int usb_host_auto_del(const char *spec);
999 USBDevice *usb_host_device_open(const char *devname)
1001 int bus_num, addr;
1002 char product_name[PRODUCT_NAME_SZ];
1004 if (strstr(devname, "auto:")) {
1005 usb_host_auto_add(devname);
1006 return NULL;
1009 if (usb_host_find_device(&bus_num, &addr, product_name, sizeof(product_name),
1010 devname) < 0)
1011 return NULL;
1013 if (hostdev_find(bus_num, addr)) {
1014 term_printf("husb: host usb device %d.%d is already open\n", bus_num, addr);
1015 return NULL;
1018 return usb_host_device_open_addr(bus_num, addr, product_name);
1021 int usb_host_device_close(const char *devname)
1023 char product_name[PRODUCT_NAME_SZ];
1024 int bus_num, addr;
1025 USBHostDevice *s;
1027 if (strstr(devname, "auto:"))
1028 return usb_host_auto_del(devname);
1030 if (usb_host_find_device(&bus_num, &addr, product_name, sizeof(product_name),
1031 devname) < 0)
1032 return -1;
1034 s = hostdev_find(bus_num, addr);
1035 if (s) {
1036 usb_device_del_addr(0, s->dev.addr);
1037 return 0;
1040 return -1;
1043 static int get_tag_value(char *buf, int buf_size,
1044 const char *str, const char *tag,
1045 const char *stopchars)
1047 const char *p;
1048 char *q;
1049 p = strstr(str, tag);
1050 if (!p)
1051 return -1;
1052 p += strlen(tag);
1053 while (isspace(*p))
1054 p++;
1055 q = buf;
1056 while (*p != '\0' && !strchr(stopchars, *p)) {
1057 if ((q - buf) < (buf_size - 1))
1058 *q++ = *p;
1059 p++;
1061 *q = '\0';
1062 return q - buf;
1066 * Use /proc/bus/usb/devices or /dev/bus/usb/devices file to determine
1067 * host's USB devices. This is legacy support since many distributions
1068 * are moving to /sys/bus/usb
1070 static int usb_host_scan_dev(void *opaque, USBScanFunc *func)
1072 FILE *f = 0;
1073 char line[1024];
1074 char buf[1024];
1075 int bus_num, addr, speed, device_count, class_id, product_id, vendor_id;
1076 char product_name[512];
1077 int ret = 0;
1079 if (!usb_host_device_path) {
1080 perror("husb: USB Host Device Path not set");
1081 goto the_end;
1083 snprintf(line, sizeof(line), "%s/devices", usb_host_device_path);
1084 f = fopen(line, "r");
1085 if (!f) {
1086 perror("husb: cannot open devices file");
1087 goto the_end;
1090 device_count = 0;
1091 bus_num = addr = speed = class_id = product_id = vendor_id = 0;
1092 for(;;) {
1093 if (fgets(line, sizeof(line), f) == NULL)
1094 break;
1095 if (strlen(line) > 0)
1096 line[strlen(line) - 1] = '\0';
1097 if (line[0] == 'T' && line[1] == ':') {
1098 if (device_count && (vendor_id || product_id)) {
1099 /* New device. Add the previously discovered device. */
1100 ret = func(opaque, bus_num, addr, class_id, vendor_id,
1101 product_id, product_name, speed);
1102 if (ret)
1103 goto the_end;
1105 if (get_tag_value(buf, sizeof(buf), line, "Bus=", " ") < 0)
1106 goto fail;
1107 bus_num = atoi(buf);
1108 if (get_tag_value(buf, sizeof(buf), line, "Dev#=", " ") < 0)
1109 goto fail;
1110 addr = atoi(buf);
1111 if (get_tag_value(buf, sizeof(buf), line, "Spd=", " ") < 0)
1112 goto fail;
1113 if (!strcmp(buf, "480"))
1114 speed = USB_SPEED_HIGH;
1115 else if (!strcmp(buf, "1.5"))
1116 speed = USB_SPEED_LOW;
1117 else
1118 speed = USB_SPEED_FULL;
1119 product_name[0] = '\0';
1120 class_id = 0xff;
1121 device_count++;
1122 product_id = 0;
1123 vendor_id = 0;
1124 } else if (line[0] == 'P' && line[1] == ':') {
1125 if (get_tag_value(buf, sizeof(buf), line, "Vendor=", " ") < 0)
1126 goto fail;
1127 vendor_id = strtoul(buf, NULL, 16);
1128 if (get_tag_value(buf, sizeof(buf), line, "ProdID=", " ") < 0)
1129 goto fail;
1130 product_id = strtoul(buf, NULL, 16);
1131 } else if (line[0] == 'S' && line[1] == ':') {
1132 if (get_tag_value(buf, sizeof(buf), line, "Product=", "") < 0)
1133 goto fail;
1134 pstrcpy(product_name, sizeof(product_name), buf);
1135 } else if (line[0] == 'D' && line[1] == ':') {
1136 if (get_tag_value(buf, sizeof(buf), line, "Cls=", " (") < 0)
1137 goto fail;
1138 class_id = strtoul(buf, NULL, 16);
1140 fail: ;
1142 if (device_count && (vendor_id || product_id)) {
1143 /* Add the last device. */
1144 ret = func(opaque, bus_num, addr, class_id, vendor_id,
1145 product_id, product_name, speed);
1147 the_end:
1148 if (f)
1149 fclose(f);
1150 return ret;
1154 * Read sys file-system device file
1156 * @line address of buffer to put file contents in
1157 * @line_size size of line
1158 * @device_file path to device file (printf format string)
1159 * @device_name device being opened (inserted into device_file)
1161 * @return 0 failed, 1 succeeded ('line' contains data)
1163 static int usb_host_read_file(char *line, size_t line_size, const char *device_file, const char *device_name)
1165 FILE *f;
1166 int ret = 0;
1167 char filename[PATH_MAX];
1169 snprintf(filename, PATH_MAX, device_file, device_name);
1170 f = fopen(filename, "r");
1171 if (f) {
1172 fgets(line, line_size, f);
1173 fclose(f);
1174 ret = 1;
1175 } else {
1176 term_printf("husb: could not open %s\n", filename);
1179 return ret;
1183 * Use /sys/bus/usb/devices/ directory to determine host's USB
1184 * devices.
1186 * This code is based on Robert Schiele's original patches posted to
1187 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1189 static int usb_host_scan_sys(void *opaque, USBScanFunc *func)
1191 DIR *dir = 0;
1192 char line[1024];
1193 int bus_num, addr, speed, class_id, product_id, vendor_id;
1194 int ret = 0;
1195 char product_name[512];
1196 struct dirent *de;
1198 dir = opendir(USBSYSBUS_PATH "/devices");
1199 if (!dir) {
1200 perror("husb: cannot open devices directory");
1201 goto the_end;
1204 while ((de = readdir(dir))) {
1205 if (de->d_name[0] != '.' && !strchr(de->d_name, ':')) {
1206 char *tmpstr = de->d_name;
1207 if (!strncmp(de->d_name, "usb", 3))
1208 tmpstr += 3;
1209 bus_num = atoi(tmpstr);
1211 if (!usb_host_read_file(line, sizeof(line), USBSYSBUS_PATH "/devices/%s/devnum", de->d_name))
1212 goto the_end;
1213 if (sscanf(line, "%d", &addr) != 1)
1214 goto the_end;
1216 if (!usb_host_read_file(line, sizeof(line), USBSYSBUS_PATH "/devices/%s/bDeviceClass", de->d_name))
1217 goto the_end;
1218 if (sscanf(line, "%x", &class_id) != 1)
1219 goto the_end;
1221 if (!usb_host_read_file(line, sizeof(line), USBSYSBUS_PATH "/devices/%s/idVendor", de->d_name))
1222 goto the_end;
1223 if (sscanf(line, "%x", &vendor_id) != 1)
1224 goto the_end;
1226 if (!usb_host_read_file(line, sizeof(line), USBSYSBUS_PATH "/devices/%s/idProduct", de->d_name))
1227 goto the_end;
1228 if (sscanf(line, "%x", &product_id) != 1)
1229 goto the_end;
1231 if (!usb_host_read_file(line, sizeof(line), USBSYSBUS_PATH "/devices/%s/product", de->d_name)) {
1232 *product_name = 0;
1233 } else {
1234 if (strlen(line) > 0)
1235 line[strlen(line) - 1] = '\0';
1236 pstrcpy(product_name, sizeof(product_name), line);
1239 if (!usb_host_read_file(line, sizeof(line), USBSYSBUS_PATH "/devices/%s/speed", de->d_name))
1240 goto the_end;
1241 if (!strcmp(line, "480\n"))
1242 speed = USB_SPEED_HIGH;
1243 else if (!strcmp(line, "1.5\n"))
1244 speed = USB_SPEED_LOW;
1245 else
1246 speed = USB_SPEED_FULL;
1248 ret = func(opaque, bus_num, addr, class_id, vendor_id,
1249 product_id, product_name, speed);
1250 if (ret)
1251 goto the_end;
1254 the_end:
1255 if (dir)
1256 closedir(dir);
1257 return ret;
1261 * Determine how to access the host's USB devices and call the
1262 * specific support function.
1264 static int usb_host_scan(void *opaque, USBScanFunc *func)
1266 FILE *f = 0;
1267 DIR *dir = 0;
1268 int ret = 0;
1269 const char *fs_type[] = {"unknown", "proc", "dev", "sys"};
1270 char devpath[PATH_MAX];
1272 /* only check the host once */
1273 if (!usb_fs_type) {
1274 f = fopen(USBPROCBUS_PATH "/devices", "r");
1275 if (f) {
1276 /* devices found in /proc/bus/usb/ */
1277 strcpy(devpath, USBPROCBUS_PATH);
1278 usb_fs_type = USB_FS_PROC;
1279 fclose(f);
1280 dprintf(USBDBG_DEVOPENED, USBPROCBUS_PATH);
1281 goto found_devices;
1283 /* try additional methods if an access method hasn't been found yet */
1284 f = fopen(USBDEVBUS_PATH "/devices", "r");
1285 if (f) {
1286 /* devices found in /dev/bus/usb/ */
1287 strcpy(devpath, USBDEVBUS_PATH);
1288 usb_fs_type = USB_FS_DEV;
1289 fclose(f);
1290 dprintf(USBDBG_DEVOPENED, USBDEVBUS_PATH);
1291 goto found_devices;
1293 dir = opendir(USBSYSBUS_PATH "/devices");
1294 if (dir) {
1295 /* devices found in /dev/bus/usb/ (yes - not a mistake!) */
1296 strcpy(devpath, USBDEVBUS_PATH);
1297 usb_fs_type = USB_FS_SYS;
1298 closedir(dir);
1299 dprintf(USBDBG_DEVOPENED, USBSYSBUS_PATH);
1300 goto found_devices;
1302 found_devices:
1303 if (!usb_fs_type) {
1304 term_printf("husb: unable to access USB devices\n");
1305 return -ENOENT;
1308 /* the module setting (used later for opening devices) */
1309 usb_host_device_path = qemu_mallocz(strlen(devpath)+1);
1310 if (usb_host_device_path) {
1311 strcpy(usb_host_device_path, devpath);
1312 term_printf("husb: using %s file-system with %s\n", fs_type[usb_fs_type], usb_host_device_path);
1313 } else {
1314 /* out of memory? */
1315 perror("husb: unable to allocate memory for device path");
1316 return -ENOMEM;
1320 switch (usb_fs_type) {
1321 case USB_FS_PROC:
1322 case USB_FS_DEV:
1323 ret = usb_host_scan_dev(opaque, func);
1324 break;
1325 case USB_FS_SYS:
1326 ret = usb_host_scan_sys(opaque, func);
1327 break;
1328 default:
1329 ret = -EINVAL;
1330 break;
1332 return ret;
1335 struct USBAutoFilter {
1336 struct USBAutoFilter *next;
1337 int bus_num;
1338 int addr;
1339 int vendor_id;
1340 int product_id;
1343 static QEMUTimer *usb_auto_timer;
1344 static struct USBAutoFilter *usb_auto_filter;
1346 static int usb_host_auto_scan(void *opaque, int bus_num, int addr,
1347 int class_id, int vendor_id, int product_id,
1348 const char *product_name, int speed)
1350 struct USBAutoFilter *f;
1351 struct USBDevice *dev;
1353 /* Ignore hubs */
1354 if (class_id == 9)
1355 return 0;
1357 for (f = usb_auto_filter; f; f = f->next) {
1358 if (f->bus_num >= 0 && f->bus_num != bus_num)
1359 continue;
1361 if (f->addr >= 0 && f->addr != addr)
1362 continue;
1364 if (f->vendor_id >= 0 && f->vendor_id != vendor_id)
1365 continue;
1367 if (f->product_id >= 0 && f->product_id != product_id)
1368 continue;
1370 /* We got a match */
1372 /* Allredy attached ? */
1373 if (hostdev_find(bus_num, addr))
1374 return 0;
1376 dprintf("husb: auto open: bus_num %d addr %d\n", bus_num, addr);
1378 dev = usb_host_device_open_addr(bus_num, addr, product_name);
1379 if (dev)
1380 usb_device_add_dev(dev);
1383 return 0;
1386 static void usb_host_auto_timer(void *unused)
1388 usb_host_scan(NULL, usb_host_auto_scan);
1389 qemu_mod_timer(usb_auto_timer, qemu_get_clock(rt_clock) + 2000);
1393 * Autoconnect filter
1394 * Format:
1395 * auto:bus:dev[:vid:pid]
1396 * auto:bus.dev[:vid:pid]
1398 * bus - bus number (dec, * means any)
1399 * dev - device number (dec, * means any)
1400 * vid - vendor id (hex, * means any)
1401 * pid - product id (hex, * means any)
1403 * See 'lsusb' output.
1405 static int parse_filter(const char *spec, struct USBAutoFilter *f)
1407 enum { BUS, DEV, VID, PID, DONE };
1408 const char *p = spec;
1409 int i;
1411 f->bus_num = -1;
1412 f->addr = -1;
1413 f->vendor_id = -1;
1414 f->product_id = -1;
1416 for (i = BUS; i < DONE; i++) {
1417 p = strpbrk(p, ":.");
1418 if (!p) break;
1419 p++;
1421 if (*p == '*')
1422 continue;
1424 switch(i) {
1425 case BUS: f->bus_num = strtol(p, NULL, 10); break;
1426 case DEV: f->addr = strtol(p, NULL, 10); break;
1427 case VID: f->vendor_id = strtol(p, NULL, 16); break;
1428 case PID: f->product_id = strtol(p, NULL, 16); break;
1432 if (i < DEV) {
1433 fprintf(stderr, "husb: invalid auto filter spec %s\n", spec);
1434 return -1;
1437 return 0;
1440 static int match_filter(const struct USBAutoFilter *f1,
1441 const struct USBAutoFilter *f2)
1443 return f1->bus_num == f2->bus_num &&
1444 f1->addr == f2->addr &&
1445 f1->vendor_id == f2->vendor_id &&
1446 f1->product_id == f2->product_id;
1449 static int usb_host_auto_add(const char *spec)
1451 struct USBAutoFilter filter, *f;
1453 if (parse_filter(spec, &filter) < 0)
1454 return -1;
1456 f = qemu_mallocz(sizeof(*f));
1457 if (!f) {
1458 fprintf(stderr, "husb: failed to allocate auto filter\n");
1459 return -1;
1462 *f = filter;
1464 if (!usb_auto_filter) {
1466 * First entry. Init and start the monitor.
1467 * Right now we're using timer to check for new devices.
1468 * If this turns out to be too expensive we can move that into a
1469 * separate thread.
1471 usb_auto_timer = qemu_new_timer(rt_clock, usb_host_auto_timer, NULL);
1472 if (!usb_auto_timer) {
1473 fprintf(stderr, "husb: failed to allocate auto scan timer\n");
1474 qemu_free(f);
1475 return -1;
1478 /* Check for new devices every two seconds */
1479 qemu_mod_timer(usb_auto_timer, qemu_get_clock(rt_clock) + 2000);
1482 dprintf("husb: added auto filter: bus_num %d addr %d vid %d pid %d\n",
1483 f->bus_num, f->addr, f->vendor_id, f->product_id);
1485 f->next = usb_auto_filter;
1486 usb_auto_filter = f;
1488 return 0;
1491 static int usb_host_auto_del(const char *spec)
1493 struct USBAutoFilter *pf = usb_auto_filter;
1494 struct USBAutoFilter **prev = &usb_auto_filter;
1495 struct USBAutoFilter filter;
1497 if (parse_filter(spec, &filter) < 0)
1498 return -1;
1500 while (pf) {
1501 if (match_filter(pf, &filter)) {
1502 dprintf("husb: removed auto filter: bus_num %d addr %d vid %d pid %d\n",
1503 pf->bus_num, pf->addr, pf->vendor_id, pf->product_id);
1505 *prev = pf->next;
1507 if (!usb_auto_filter) {
1508 /* No more filters. Stop scanning. */
1509 qemu_del_timer(usb_auto_timer);
1510 qemu_free_timer(usb_auto_timer);
1513 return 0;
1516 prev = &pf->next;
1517 pf = pf->next;
1520 return -1;
1523 typedef struct FindDeviceState {
1524 int vendor_id;
1525 int product_id;
1526 int bus_num;
1527 int addr;
1528 char product_name[PRODUCT_NAME_SZ];
1529 } FindDeviceState;
1531 static int usb_host_find_device_scan(void *opaque, int bus_num, int addr,
1532 int class_id,
1533 int vendor_id, int product_id,
1534 const char *product_name, int speed)
1536 FindDeviceState *s = opaque;
1537 if ((vendor_id == s->vendor_id &&
1538 product_id == s->product_id) ||
1539 (bus_num == s->bus_num &&
1540 addr == s->addr)) {
1541 pstrcpy(s->product_name, PRODUCT_NAME_SZ, product_name);
1542 s->bus_num = bus_num;
1543 s->addr = addr;
1544 return 1;
1545 } else {
1546 return 0;
1550 /* the syntax is :
1551 'bus.addr' (decimal numbers) or
1552 'vendor_id:product_id' (hexa numbers) */
1553 static int usb_host_find_device(int *pbus_num, int *paddr,
1554 char *product_name, int product_name_size,
1555 const char *devname)
1557 const char *p;
1558 int ret;
1559 FindDeviceState fs;
1561 p = strchr(devname, '.');
1562 if (p) {
1563 *pbus_num = strtoul(devname, NULL, 0);
1564 *paddr = strtoul(p + 1, NULL, 0);
1565 fs.bus_num = *pbus_num;
1566 fs.addr = *paddr;
1567 ret = usb_host_scan(&fs, usb_host_find_device_scan);
1568 if (ret)
1569 pstrcpy(product_name, product_name_size, fs.product_name);
1570 return 0;
1573 p = strchr(devname, ':');
1574 if (p) {
1575 fs.vendor_id = strtoul(devname, NULL, 16);
1576 fs.product_id = strtoul(p + 1, NULL, 16);
1577 ret = usb_host_scan(&fs, usb_host_find_device_scan);
1578 if (ret) {
1579 *pbus_num = fs.bus_num;
1580 *paddr = fs.addr;
1581 pstrcpy(product_name, product_name_size, fs.product_name);
1582 return 0;
1585 return -1;
1588 /**********************/
1589 /* USB host device info */
1591 struct usb_class_info {
1592 int class;
1593 const char *class_name;
1596 static const struct usb_class_info usb_class_info[] = {
1597 { USB_CLASS_AUDIO, "Audio"},
1598 { USB_CLASS_COMM, "Communication"},
1599 { USB_CLASS_HID, "HID"},
1600 { USB_CLASS_HUB, "Hub" },
1601 { USB_CLASS_PHYSICAL, "Physical" },
1602 { USB_CLASS_PRINTER, "Printer" },
1603 { USB_CLASS_MASS_STORAGE, "Storage" },
1604 { USB_CLASS_CDC_DATA, "Data" },
1605 { USB_CLASS_APP_SPEC, "Application Specific" },
1606 { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
1607 { USB_CLASS_STILL_IMAGE, "Still Image" },
1608 { USB_CLASS_CSCID, "Smart Card" },
1609 { USB_CLASS_CONTENT_SEC, "Content Security" },
1610 { -1, NULL }
1613 static const char *usb_class_str(uint8_t class)
1615 const struct usb_class_info *p;
1616 for(p = usb_class_info; p->class != -1; p++) {
1617 if (p->class == class)
1618 break;
1620 return p->class_name;
1623 static void usb_info_device(int bus_num, int addr, int class_id,
1624 int vendor_id, int product_id,
1625 const char *product_name,
1626 int speed)
1628 const char *class_str, *speed_str;
1630 switch(speed) {
1631 case USB_SPEED_LOW:
1632 speed_str = "1.5";
1633 break;
1634 case USB_SPEED_FULL:
1635 speed_str = "12";
1636 break;
1637 case USB_SPEED_HIGH:
1638 speed_str = "480";
1639 break;
1640 default:
1641 speed_str = "?";
1642 break;
1645 term_printf(" Device %d.%d, speed %s Mb/s\n",
1646 bus_num, addr, speed_str);
1647 class_str = usb_class_str(class_id);
1648 if (class_str)
1649 term_printf(" %s:", class_str);
1650 else
1651 term_printf(" Class %02x:", class_id);
1652 term_printf(" USB device %04x:%04x", vendor_id, product_id);
1653 if (product_name[0] != '\0')
1654 term_printf(", %s", product_name);
1655 term_printf("\n");
1658 static int usb_host_info_device(void *opaque, int bus_num, int addr,
1659 int class_id,
1660 int vendor_id, int product_id,
1661 const char *product_name,
1662 int speed)
1664 usb_info_device(bus_num, addr, class_id, vendor_id, product_id,
1665 product_name, speed);
1666 return 0;
1669 static void dec2str(int val, char *str, size_t size)
1671 if (val == -1)
1672 snprintf(str, size, "*");
1673 else
1674 snprintf(str, size, "%d", val);
1677 static void hex2str(int val, char *str, size_t size)
1679 if (val == -1)
1680 snprintf(str, size, "*");
1681 else
1682 snprintf(str, size, "%x", val);
1685 void usb_host_info(void)
1687 struct USBAutoFilter *f;
1689 usb_host_scan(NULL, usb_host_info_device);
1691 if (usb_auto_filter)
1692 term_printf(" Auto filters:\n");
1693 for (f = usb_auto_filter; f; f = f->next) {
1694 char bus[10], addr[10], vid[10], pid[10];
1695 dec2str(f->bus_num, bus, sizeof(bus));
1696 dec2str(f->addr, addr, sizeof(addr));
1697 hex2str(f->vendor_id, vid, sizeof(vid));
1698 hex2str(f->product_id, pid, sizeof(pid));
1699 term_printf(" Device %s.%s ID %s:%s\n", bus, addr, vid, pid);
1703 #else
1705 #include "hw/usb.h"
1707 void usb_host_info(void)
1709 term_printf("USB host devices not supported\n");
1712 /* XXX: modify configure to compile the right host driver */
1713 USBDevice *usb_host_device_open(const char *devname)
1715 return NULL;
1718 int usb_host_device_close(const char *devname)
1720 return 0;
1723 #endif