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 * Magor rewrite to support fully async operation
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 #include "qemu-common.h"
30 #include "qemu-timer.h"
33 #if defined(__linux__)
35 #include <sys/ioctl.h>
38 #include <linux/usb/ch9.h>
39 #include <linux/usbdevice_fs.h>
40 #include <linux/version.h>
43 typedef int USBScanFunc(void *opaque, int bus_num, int addr, int class_id,
44 int vendor_id, int product_id,
45 const char *product_name, int speed);
46 static int usb_host_find_device(int *pbus_num, int *paddr,
47 char *product_name, int product_name_size,
52 #define dprintf printf
57 #define USBDEVFS_PATH "/proc/bus/usb"
58 #define PRODUCT_NAME_SZ 32
59 #define MAX_ENDPOINTS 16
61 /* endpoint association data */
75 * Control transfer state.
76 * Note that 'buffer' _must_ follow 'req' field because
77 * we need contigious buffer when we submit control URB.
83 struct usb_ctrlrequest req;
87 typedef struct USBHostDevice {
97 struct ctrl_struct ctrl;
98 struct endp_data endp_table[MAX_ENDPOINTS];
100 /* Host side address */
104 struct USBHostDevice *next;
107 static int is_isoc(USBHostDevice *s, int ep)
109 return s->endp_table[ep - 1].type == USBDEVFS_URB_TYPE_ISO;
112 static int is_halted(USBHostDevice *s, int ep)
114 return s->endp_table[ep - 1].halted;
117 static void clear_halt(USBHostDevice *s, int ep)
119 s->endp_table[ep - 1].halted = 0;
122 static void set_halt(USBHostDevice *s, int ep)
124 s->endp_table[ep - 1].halted = 1;
127 static USBHostDevice *hostdev_list;
129 static void hostdev_link(USBHostDevice *dev)
131 dev->next = hostdev_list;
135 static void hostdev_unlink(USBHostDevice *dev)
137 USBHostDevice *pdev = hostdev_list;
138 USBHostDevice **prev = &hostdev_list;
151 static USBHostDevice *hostdev_find(int bus_num, int addr)
153 USBHostDevice *s = hostdev_list;
155 if (s->bus_num == bus_num && s->addr == addr)
164 * We always allocate one isoc descriptor even for bulk transfers
165 * to simplify allocation and casts.
167 typedef struct AsyncURB
169 struct usbdevfs_urb urb;
170 struct usbdevfs_iso_packet_desc isocpd;
176 static AsyncURB *async_alloc(void)
178 return (AsyncURB *) qemu_mallocz(sizeof(AsyncURB));
181 static void async_free(AsyncURB *aurb)
186 static void async_complete_ctrl(USBHostDevice *s, USBPacket *p)
188 switch(s->ctrl.state) {
189 case CTRL_STATE_SETUP:
190 if (p->len < s->ctrl.len)
191 s->ctrl.len = p->len;
192 s->ctrl.state = CTRL_STATE_DATA;
197 s->ctrl.state = CTRL_STATE_IDLE;
206 static void async_complete(void *opaque)
208 USBHostDevice *s = opaque;
214 int r = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &aurb);
219 if (errno == ENODEV && !s->closing) {
220 printf("husb: device %d.%d disconnected\n", s->bus_num, s->addr);
221 usb_device_del_addr(0, s->dev.addr);
225 dprintf("husb: async. reap urb failed errno %d\n", errno);
231 dprintf("husb: async completed. aurb %p status %d alen %d\n",
232 aurb, aurb->urb.status, aurb->urb.actual_length);
235 switch (aurb->urb.status) {
237 p->len = aurb->urb.actual_length;
238 if (aurb->urb.type == USBDEVFS_URB_TYPE_CONTROL)
239 async_complete_ctrl(s, p);
243 set_halt(s, p->devep);
246 p->len = USB_RET_NAK;
250 usb_packet_complete(p);
257 static void async_cancel(USBPacket *unused, void *opaque)
259 AsyncURB *aurb = opaque;
260 USBHostDevice *s = aurb->hdev;
262 dprintf("husb: async cancel. aurb %p\n", aurb);
264 /* Mark it as dead (see async_complete above) */
267 int r = ioctl(s->fd, USBDEVFS_DISCARDURB, aurb);
269 dprintf("husb: async. discard urb failed errno %d\n", errno);
273 static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration)
275 int dev_descr_len, config_descr_len;
276 int interface, nb_interfaces, nb_configurations;
279 if (configuration == 0) /* address state - ignore */
282 dprintf("husb: claiming interfaces. config %d\n", configuration);
285 dev_descr_len = dev->descr[0];
286 if (dev_descr_len > dev->descr_len)
288 nb_configurations = dev->descr[17];
291 while (i < dev->descr_len) {
292 dprintf("husb: i is %d, descr_len is %d, dl %d, dt %d\n", i, dev->descr_len,
293 dev->descr[i], dev->descr[i+1]);
295 if (dev->descr[i+1] != USB_DT_CONFIG) {
299 config_descr_len = dev->descr[i];
301 printf("husb: config #%d need %d\n", dev->descr[i + 5], configuration);
303 if (configuration < 0 || configuration == dev->descr[i + 5]) {
304 configuration = dev->descr[i + 5];
308 i += config_descr_len;
311 if (i >= dev->descr_len) {
312 fprintf(stderr, "husb: update iface failed. no matching configuration\n");
315 nb_interfaces = dev->descr[i + 4];
317 #ifdef USBDEVFS_DISCONNECT
318 /* earlier Linux 2.4 do not support that */
320 struct usbdevfs_ioctl ctrl;
321 for (interface = 0; interface < nb_interfaces; interface++) {
322 ctrl.ioctl_code = USBDEVFS_DISCONNECT;
323 ctrl.ifno = interface;
324 ret = ioctl(dev->fd, USBDEVFS_IOCTL, &ctrl);
325 if (ret < 0 && errno != ENODATA) {
326 perror("USBDEVFS_DISCONNECT");
333 /* XXX: only grab if all interfaces are free */
334 for (interface = 0; interface < nb_interfaces; interface++) {
335 ret = ioctl(dev->fd, USBDEVFS_CLAIMINTERFACE, &interface);
337 if (errno == EBUSY) {
338 printf("husb: update iface. device already grabbed\n");
340 perror("husb: failed to claim interface");
347 printf("husb: %d interfaces claimed for configuration %d\n",
348 nb_interfaces, configuration);
350 dev->ninterfaces = nb_interfaces;
351 dev->configuration = configuration;
355 static int usb_host_release_interfaces(USBHostDevice *s)
359 dprintf("husb: releasing interfaces\n");
361 for (i = 0; i < s->ninterfaces; i++) {
362 ret = ioctl(s->fd, USBDEVFS_RELEASEINTERFACE, &i);
364 perror("husb: failed to release interface");
372 static void usb_host_handle_reset(USBDevice *dev)
374 USBHostDevice *s = (USBHostDevice *) dev;
376 dprintf("husb: reset device %u.%u\n", s->bus_num, s->addr);
378 ioctl(s->fd, USBDEVFS_RESET);
380 usb_host_claim_interfaces(s, s->configuration);
383 static void usb_host_handle_destroy(USBDevice *dev)
385 USBHostDevice *s = (USBHostDevice *)dev;
389 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
401 static int usb_linux_update_endp_table(USBHostDevice *s);
403 static int usb_host_handle_data(USBHostDevice *s, USBPacket *p)
405 struct usbdevfs_urb *urb;
409 aurb = async_alloc();
411 dprintf("husb: async malloc failed\n");
419 if (p->pid == USB_TOKEN_IN)
420 urb->endpoint = p->devep | 0x80;
422 urb->endpoint = p->devep;
424 if (is_halted(s, p->devep)) {
425 ret = ioctl(s->fd, USBDEVFS_CLEAR_HALT, &urb->endpoint);
427 dprintf("husb: failed to clear halt. ep 0x%x errno %d\n",
428 urb->endpoint, errno);
431 clear_halt(s, p->devep);
434 urb->buffer = p->data;
435 urb->buffer_length = p->len;
437 if (is_isoc(s, p->devep)) {
438 /* Setup ISOC transfer */
439 urb->type = USBDEVFS_URB_TYPE_ISO;
440 urb->flags = USBDEVFS_URB_ISO_ASAP;
441 urb->number_of_packets = 1;
442 urb->iso_frame_desc[0].length = p->len;
444 /* Setup bulk transfer */
445 urb->type = USBDEVFS_URB_TYPE_BULK;
448 urb->usercontext = s;
450 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
452 dprintf("husb: data submit. ep 0x%x len %u aurb %p\n", urb->endpoint, p->len, aurb);
455 dprintf("husb: submit failed. errno %d\n", errno);
463 return USB_RET_STALL;
467 usb_defer_packet(p, async_cancel, aurb);
468 return USB_RET_ASYNC;
471 static int ctrl_error(void)
473 if (errno == ETIMEDOUT)
476 return USB_RET_STALL;
479 static int usb_host_set_address(USBHostDevice *s, int addr)
481 dprintf("husb: ctrl set addr %u\n", addr);
486 static int usb_host_set_config(USBHostDevice *s, int config)
488 usb_host_release_interfaces(s);
490 int ret = ioctl(s->fd, USBDEVFS_SETCONFIGURATION, &config);
492 dprintf("husb: ctrl set config %d ret %d errno %d\n", config, ret, errno);
497 usb_host_claim_interfaces(s, config);
501 static int usb_host_set_interface(USBHostDevice *s, int iface, int alt)
503 struct usbdevfs_setinterface si;
506 si.interface = iface;
508 ret = ioctl(s->fd, USBDEVFS_SETINTERFACE, &si);
510 dprintf("husb: ctrl set iface %d altset %d ret %d errno %d\n",
511 iface, alt, ret, errno);
516 usb_linux_update_endp_table(s);
520 static int usb_host_handle_control(USBHostDevice *s, USBPacket *p)
522 struct usbdevfs_urb *urb;
524 int ret, value, index;
527 * Process certain standard device requests.
528 * These are infrequent and are processed synchronously.
530 value = le16_to_cpu(s->ctrl.req.wValue);
531 index = le16_to_cpu(s->ctrl.req.wIndex);
533 dprintf("husb: ctrl type 0x%x req 0x%x val 0x%x index %u len %u\n",
534 s->ctrl.req.bRequestType, s->ctrl.req.bRequest, value, index,
537 if (s->ctrl.req.bRequestType == 0) {
538 switch (s->ctrl.req.bRequest) {
539 case USB_REQ_SET_ADDRESS:
540 return usb_host_set_address(s, value);
542 case USB_REQ_SET_CONFIGURATION:
543 return usb_host_set_config(s, value & 0xff);
547 if (s->ctrl.req.bRequestType == 1 &&
548 s->ctrl.req.bRequest == USB_REQ_SET_INTERFACE)
549 return usb_host_set_interface(s, index, value);
551 /* The rest are asynchronous */
553 aurb = async_alloc();
555 dprintf("husb: async malloc failed\n");
562 * Setup ctrl transfer.
564 * s->ctrl is layed out such that data buffer immediately follows
565 * 'req' struct which is exactly what usbdevfs expects.
569 urb->type = USBDEVFS_URB_TYPE_CONTROL;
570 urb->endpoint = p->devep;
572 urb->buffer = &s->ctrl.req;
573 urb->buffer_length = 8 + s->ctrl.len;
575 urb->usercontext = s;
577 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
579 dprintf("husb: submit ctrl. len %u aurb %p\n", urb->buffer_length, aurb);
582 dprintf("husb: submit failed. errno %d\n", errno);
590 return USB_RET_STALL;
594 usb_defer_packet(p, async_cancel, aurb);
595 return USB_RET_ASYNC;
598 static int do_token_setup(USBDevice *dev, USBPacket *p)
600 USBHostDevice *s = (USBHostDevice *) dev;
604 return USB_RET_STALL;
606 memcpy(&s->ctrl.req, p->data, 8);
607 s->ctrl.len = le16_to_cpu(s->ctrl.req.wLength);
609 s->ctrl.state = CTRL_STATE_SETUP;
611 if (s->ctrl.req.bRequestType & USB_DIR_IN) {
612 ret = usb_host_handle_control(s, p);
616 if (ret < s->ctrl.len)
618 s->ctrl.state = CTRL_STATE_DATA;
620 if (s->ctrl.len == 0)
621 s->ctrl.state = CTRL_STATE_ACK;
623 s->ctrl.state = CTRL_STATE_DATA;
629 static int do_token_in(USBDevice *dev, USBPacket *p)
631 USBHostDevice *s = (USBHostDevice *) dev;
635 return usb_host_handle_data(s, p);
637 switch(s->ctrl.state) {
639 if (!(s->ctrl.req.bRequestType & USB_DIR_IN)) {
640 ret = usb_host_handle_control(s, p);
641 if (ret == USB_RET_ASYNC)
642 return USB_RET_ASYNC;
644 s->ctrl.state = CTRL_STATE_IDLE;
645 return ret > 0 ? 0 : ret;
650 case CTRL_STATE_DATA:
651 if (s->ctrl.req.bRequestType & USB_DIR_IN) {
652 int len = s->ctrl.len - s->ctrl.offset;
655 memcpy(p->data, s->ctrl.buffer + s->ctrl.offset, len);
656 s->ctrl.offset += len;
657 if (s->ctrl.offset >= s->ctrl.len)
658 s->ctrl.state = CTRL_STATE_ACK;
662 s->ctrl.state = CTRL_STATE_IDLE;
663 return USB_RET_STALL;
666 return USB_RET_STALL;
670 static int do_token_out(USBDevice *dev, USBPacket *p)
672 USBHostDevice *s = (USBHostDevice *) dev;
675 return usb_host_handle_data(s, p);
677 switch(s->ctrl.state) {
679 if (s->ctrl.req.bRequestType & USB_DIR_IN) {
680 s->ctrl.state = CTRL_STATE_IDLE;
683 /* ignore additional output */
687 case CTRL_STATE_DATA:
688 if (!(s->ctrl.req.bRequestType & USB_DIR_IN)) {
689 int len = s->ctrl.len - s->ctrl.offset;
692 memcpy(s->ctrl.buffer + s->ctrl.offset, p->data, len);
693 s->ctrl.offset += len;
694 if (s->ctrl.offset >= s->ctrl.len)
695 s->ctrl.state = CTRL_STATE_ACK;
699 s->ctrl.state = CTRL_STATE_IDLE;
700 return USB_RET_STALL;
703 return USB_RET_STALL;
709 * Called by the HC (host controller).
711 * Returns length of the transaction or one of the USB_RET_XXX codes.
713 int usb_host_handle_packet(USBDevice *s, USBPacket *p)
717 s->state = USB_STATE_ATTACHED;
721 s->state = USB_STATE_NOTATTACHED;
725 s->remote_wakeup = 0;
727 s->state = USB_STATE_DEFAULT;
732 /* Rest of the PIDs must match our address */
733 if (s->state < USB_STATE_DEFAULT || p->devaddr != s->addr)
734 return USB_RET_NODEV;
737 case USB_TOKEN_SETUP:
738 return do_token_setup(s, p);
741 return do_token_in(s, p);
744 return do_token_out(s, p);
747 return USB_RET_STALL;
751 /* returns 1 on problem encountered or 0 for success */
752 static int usb_linux_update_endp_table(USBHostDevice *s)
754 uint8_t *descriptors;
755 uint8_t devep, type, configuration, alt_interface;
756 struct usbdevfs_ctrltransfer ct;
757 int interface, ret, length, i;
759 ct.bRequestType = USB_DIR_IN;
760 ct.bRequest = USB_REQ_GET_CONFIGURATION;
764 ct.data = &configuration;
767 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
769 perror("usb_linux_update_endp_table");
773 /* in address state */
774 if (configuration == 0)
777 /* get the desired configuration, interface, and endpoint descriptors
778 * from device description */
779 descriptors = &s->descr[18];
780 length = s->descr_len - 18;
783 if (descriptors[i + 1] != USB_DT_CONFIG ||
784 descriptors[i + 5] != configuration) {
785 dprintf("invalid descriptor data - configuration\n");
791 if (descriptors[i + 1] != USB_DT_INTERFACE ||
792 (descriptors[i + 1] == USB_DT_INTERFACE &&
793 descriptors[i + 4] == 0)) {
798 interface = descriptors[i + 2];
800 ct.bRequestType = USB_DIR_IN | USB_RECIP_INTERFACE;
801 ct.bRequest = USB_REQ_GET_INTERFACE;
803 ct.wIndex = interface;
805 ct.data = &alt_interface;
808 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
810 perror("usb_linux_update_endp_table");
814 /* the current interface descriptor is the active interface
815 * and has endpoints */
816 if (descriptors[i + 3] != alt_interface) {
821 /* advance to the endpoints */
822 while (i < length && descriptors[i +1] != USB_DT_ENDPOINT)
829 if (descriptors[i + 1] != USB_DT_ENDPOINT)
832 devep = descriptors[i + 2];
833 switch (descriptors[i + 3] & 0x3) {
835 type = USBDEVFS_URB_TYPE_CONTROL;
838 type = USBDEVFS_URB_TYPE_ISO;
841 type = USBDEVFS_URB_TYPE_BULK;
844 type = USBDEVFS_URB_TYPE_INTERRUPT;
847 dprintf("usb_host: malformed endpoint type\n");
848 type = USBDEVFS_URB_TYPE_BULK;
850 s->endp_table[(devep & 0xf) - 1].type = type;
851 s->endp_table[(devep & 0xf) - 1].halted = 0;
859 static USBDevice *usb_host_device_open_addr(int bus_num, int addr, const char *prod_name)
862 USBHostDevice *dev = NULL;
863 struct usbdevfs_connectinfo ci;
866 dev = qemu_mallocz(sizeof(USBHostDevice));
870 dev->bus_num = bus_num;
873 printf("husb: open device %d.%d\n", bus_num, addr);
875 snprintf(buf, sizeof(buf), USBDEVFS_PATH "/%03d/%03d",
877 fd = open(buf, O_RDWR | O_NONBLOCK);
883 /* read the device description */
884 dev->descr_len = read(fd, dev->descr, sizeof(dev->descr));
885 if (dev->descr_len <= 0) {
886 perror("husb: reading device data failed");
893 printf("=== begin dumping device descriptor data ===\n");
894 for (x = 0; x < dev->descr_len; x++)
895 printf("%02x ", dev->descr[x]);
896 printf("\n=== end dumping device descriptor data ===\n");
903 * Initial configuration is -1 which makes us claim first
904 * available config. We used to start with 1, which does not
905 * always work. I've seen devices where first config starts
908 if (!usb_host_claim_interfaces(dev, -1))
911 ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci);
913 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
917 printf("husb: grabbed usb device %d.%d\n", bus_num, addr);
919 ret = usb_linux_update_endp_table(dev);
924 dev->dev.speed = USB_SPEED_LOW;
926 dev->dev.speed = USB_SPEED_HIGH;
928 dev->dev.handle_packet = usb_host_handle_packet;
929 dev->dev.handle_reset = usb_host_handle_reset;
930 dev->dev.handle_destroy = usb_host_handle_destroy;
932 if (!prod_name || prod_name[0] == '\0')
933 snprintf(dev->dev.devname, sizeof(dev->dev.devname),
934 "host:%d.%d", bus_num, addr);
936 pstrcpy(dev->dev.devname, sizeof(dev->dev.devname),
939 /* USB devio uses 'write' flag to check for async completions */
940 qemu_set_fd_handler(dev->fd, NULL, async_complete, dev);
944 return (USBDevice *) dev;
954 USBDevice *usb_host_device_open(const char *devname)
957 char product_name[PRODUCT_NAME_SZ];
959 if (usb_host_find_device(&bus_num, &addr,
960 product_name, sizeof(product_name),
964 if (hostdev_find(bus_num, addr)) {
965 term_printf("husb: host usb device %d.%d is already open\n", bus_num, addr);
969 return usb_host_device_open_addr(bus_num, addr, product_name);
972 static int get_tag_value(char *buf, int buf_size,
973 const char *str, const char *tag,
974 const char *stopchars)
978 p = strstr(str, tag);
985 while (*p != '\0' && !strchr(stopchars, *p)) {
986 if ((q - buf) < (buf_size - 1))
994 static int usb_host_scan(void *opaque, USBScanFunc *func)
999 int bus_num, addr, speed, device_count, class_id, product_id, vendor_id;
1001 char product_name[512];
1003 f = fopen(USBDEVFS_PATH "/devices", "r");
1005 term_printf("husb: could not open %s\n", USBDEVFS_PATH "/devices");
1009 bus_num = addr = speed = class_id = product_id = vendor_id = 0;
1012 if (fgets(line, sizeof(line), f) == NULL)
1014 if (strlen(line) > 0)
1015 line[strlen(line) - 1] = '\0';
1016 if (line[0] == 'T' && line[1] == ':') {
1017 if (device_count && (vendor_id || product_id)) {
1018 /* New device. Add the previously discovered device. */
1019 ret = func(opaque, bus_num, addr, class_id, vendor_id,
1020 product_id, product_name, speed);
1024 if (get_tag_value(buf, sizeof(buf), line, "Bus=", " ") < 0)
1026 bus_num = atoi(buf);
1027 if (get_tag_value(buf, sizeof(buf), line, "Dev#=", " ") < 0)
1030 if (get_tag_value(buf, sizeof(buf), line, "Spd=", " ") < 0)
1032 if (!strcmp(buf, "480"))
1033 speed = USB_SPEED_HIGH;
1034 else if (!strcmp(buf, "1.5"))
1035 speed = USB_SPEED_LOW;
1037 speed = USB_SPEED_FULL;
1038 product_name[0] = '\0';
1043 } else if (line[0] == 'P' && line[1] == ':') {
1044 if (get_tag_value(buf, sizeof(buf), line, "Vendor=", " ") < 0)
1046 vendor_id = strtoul(buf, NULL, 16);
1047 if (get_tag_value(buf, sizeof(buf), line, "ProdID=", " ") < 0)
1049 product_id = strtoul(buf, NULL, 16);
1050 } else if (line[0] == 'S' && line[1] == ':') {
1051 if (get_tag_value(buf, sizeof(buf), line, "Product=", "") < 0)
1053 pstrcpy(product_name, sizeof(product_name), buf);
1054 } else if (line[0] == 'D' && line[1] == ':') {
1055 if (get_tag_value(buf, sizeof(buf), line, "Cls=", " (") < 0)
1057 class_id = strtoul(buf, NULL, 16);
1061 if (device_count && (vendor_id || product_id)) {
1062 /* Add the last device. */
1063 ret = func(opaque, bus_num, addr, class_id, vendor_id,
1064 product_id, product_name, speed);
1071 struct USBAutoFilter {
1072 struct USBAutoFilter *next;
1079 static QEMUTimer *usb_auto_timer;
1080 static struct USBAutoFilter *usb_auto_filter;
1082 static int usb_host_auto_scan(void *opaque, int bus_num, int addr,
1083 int class_id, int vendor_id, int product_id,
1084 const char *product_name, int speed)
1086 struct USBAutoFilter *f;
1087 struct USBDevice *dev;
1093 for (f = usb_auto_filter; f; f = f->next) {
1094 if (f->bus_num >= 0 && f->bus_num != bus_num)
1097 if (f->addr >= 0 && f->addr != addr)
1100 if (f->vendor_id >= 0 && f->vendor_id != vendor_id)
1103 if (f->product_id >= 0 && f->product_id != product_id)
1106 /* We got a match */
1108 /* Allredy attached ? */
1109 if (hostdev_find(bus_num, addr))
1112 dprintf("husb: auto open: bus_num %d addr %d\n", bus_num, addr);
1114 dev = usb_host_device_open_addr(bus_num, addr, product_name);
1116 usb_device_add_dev(dev);
1122 static void usb_host_auto_timer(void *unused)
1124 usb_host_scan(NULL, usb_host_auto_scan);
1125 qemu_mod_timer(usb_auto_timer, qemu_get_clock(rt_clock) + 2000);
1129 * Add autoconnect filter
1130 * -1 means 'any' (device, vendor, etc)
1132 static void usb_host_auto_add(int bus_num, int addr, int vendor_id, int product_id)
1134 struct USBAutoFilter *f = qemu_mallocz(sizeof(*f));
1136 fprintf(stderr, "husb: failed to allocate auto filter\n");
1140 f->bus_num = bus_num;
1142 f->vendor_id = vendor_id;
1143 f->product_id = product_id;
1145 if (!usb_auto_filter) {
1147 * First entry. Init and start the monitor.
1148 * Right now we're using timer to check for new devices.
1149 * If this turns out to be too expensive we can move that into a
1152 usb_auto_timer = qemu_new_timer(rt_clock, usb_host_auto_timer, NULL);
1153 if (!usb_auto_timer) {
1154 fprintf(stderr, "husb: failed to allocate auto scan timer\n");
1159 /* Check for new devices every two seconds */
1160 qemu_mod_timer(usb_auto_timer, qemu_get_clock(rt_clock) + 2000);
1163 dprintf("husb: auto filter: bus_num %d addr %d vid %d pid %d\n",
1164 bus_num, addr, vendor_id, product_id);
1166 f->next = usb_auto_filter;
1167 usb_auto_filter = f;
1170 typedef struct FindDeviceState {
1175 char product_name[PRODUCT_NAME_SZ];
1178 static int usb_host_find_device_scan(void *opaque, int bus_num, int addr,
1180 int vendor_id, int product_id,
1181 const char *product_name, int speed)
1183 FindDeviceState *s = opaque;
1184 if ((vendor_id == s->vendor_id &&
1185 product_id == s->product_id) ||
1186 (bus_num == s->bus_num &&
1188 pstrcpy(s->product_name, PRODUCT_NAME_SZ, product_name);
1189 s->bus_num = bus_num;
1198 'bus.addr' (decimal numbers) or
1199 'vendor_id:product_id' (hexa numbers) */
1200 static int usb_host_find_device(int *pbus_num, int *paddr,
1201 char *product_name, int product_name_size,
1202 const char *devname)
1208 p = strchr(devname, '.');
1210 *pbus_num = strtoul(devname, NULL, 0);
1212 if (*(p + 1) == '*') {
1213 usb_host_auto_add(*pbus_num, -1, -1, -1);
1217 *paddr = strtoul(p + 1, NULL, 0);
1218 fs.bus_num = *pbus_num;
1220 ret = usb_host_scan(&fs, usb_host_find_device_scan);
1222 pstrcpy(product_name, product_name_size, fs.product_name);
1225 p = strchr(devname, ':');
1227 fs.vendor_id = strtoul(devname, NULL, 16);
1229 if (*(p + 1) == '*') {
1230 usb_host_auto_add(-1, -1, fs.vendor_id, -1);
1234 fs.product_id = strtoul(p + 1, NULL, 16);
1235 ret = usb_host_scan(&fs, usb_host_find_device_scan);
1237 *pbus_num = fs.bus_num;
1239 pstrcpy(product_name, product_name_size, fs.product_name);
1246 /**********************/
1247 /* USB host device info */
1249 struct usb_class_info {
1251 const char *class_name;
1254 static const struct usb_class_info usb_class_info[] = {
1255 { USB_CLASS_AUDIO, "Audio"},
1256 { USB_CLASS_COMM, "Communication"},
1257 { USB_CLASS_HID, "HID"},
1258 { USB_CLASS_HUB, "Hub" },
1259 { USB_CLASS_PHYSICAL, "Physical" },
1260 { USB_CLASS_PRINTER, "Printer" },
1261 { USB_CLASS_MASS_STORAGE, "Storage" },
1262 { USB_CLASS_CDC_DATA, "Data" },
1263 { USB_CLASS_APP_SPEC, "Application Specific" },
1264 { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
1265 { USB_CLASS_STILL_IMAGE, "Still Image" },
1266 { USB_CLASS_CSCID, "Smart Card" },
1267 { USB_CLASS_CONTENT_SEC, "Content Security" },
1271 static const char *usb_class_str(uint8_t class)
1273 const struct usb_class_info *p;
1274 for(p = usb_class_info; p->class != -1; p++) {
1275 if (p->class == class)
1278 return p->class_name;
1281 static void usb_info_device(int bus_num, int addr, int class_id,
1282 int vendor_id, int product_id,
1283 const char *product_name,
1286 const char *class_str, *speed_str;
1292 case USB_SPEED_FULL:
1295 case USB_SPEED_HIGH:
1303 term_printf(" Device %d.%d, speed %s Mb/s\n",
1304 bus_num, addr, speed_str);
1305 class_str = usb_class_str(class_id);
1307 term_printf(" %s:", class_str);
1309 term_printf(" Class %02x:", class_id);
1310 term_printf(" USB device %04x:%04x", vendor_id, product_id);
1311 if (product_name[0] != '\0')
1312 term_printf(", %s", product_name);
1316 static int usb_host_info_device(void *opaque, int bus_num, int addr,
1318 int vendor_id, int product_id,
1319 const char *product_name,
1322 usb_info_device(bus_num, addr, class_id, vendor_id, product_id,
1323 product_name, speed);
1327 void usb_host_info(void)
1329 usb_host_scan(NULL, usb_host_info_device);
1336 void usb_host_info(void)
1338 term_printf("USB host devices not supported\n");
1341 /* XXX: modify configure to compile the right host driver */
1342 USBDevice *usb_host_device_open(const char *devname)