kvm: libkvm: fix indentation
[qemu-kvm/fedora.git] / usb-linux.c
blob8ee789f9f127933c547a0f7c9931cda3e723bae0
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 * 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
26 * THE SOFTWARE.
28 #include "qemu-common.h"
29 #include "qemu-timer.h"
30 #include "hw/usb.h"
31 #include "console.h"
33 #if defined(__linux__)
34 #define __user
36 #include <dirent.h>
37 #include <sys/ioctl.h>
38 #include <linux/usbdevice_fs.h>
39 #include <linux/version.h>
40 #include <signal.h>
42 /* We redefine it to avoid version problems */
43 struct usb_ctrltransfer {
44 uint8_t bRequestType;
45 uint8_t bRequest;
46 uint16_t wValue;
47 uint16_t wIndex;
48 uint16_t wLength;
49 uint32_t timeout;
50 void *data;
53 typedef int USBScanFunc(void *opaque, int bus_num, int addr, int class_id,
54 int vendor_id, int product_id,
55 const char *product_name, int speed);
56 static int usb_host_find_device(int *pbus_num, int *paddr,
57 char *product_name, int product_name_size,
58 const char *devname);
60 //#define DEBUG
62 #ifdef DEBUG
63 #define dprintf printf
64 #else
65 #define dprintf(...)
66 #endif
68 #define USBDEVFS_PATH "/proc/bus/usb"
69 #define PRODUCT_NAME_SZ 32
70 #define MAX_ENDPOINTS 16
72 struct sigaction sigact;
74 /* endpoint association data */
75 struct endp_data {
76 uint8_t type;
77 uint8_t halted;
80 typedef struct USBHostDevice {
81 USBDevice dev;
82 int fd;
84 uint8_t descr[1024];
85 int descr_len;
86 int configuration;
87 int closing;
89 struct endp_data endp_table[MAX_ENDPOINTS];
91 /* Host side address */
92 int bus_num;
93 int addr;
95 struct USBHostDevice *next;
96 } USBHostDevice;
98 static int is_isoc(USBHostDevice *s, int ep)
100 return s->endp_table[ep - 1].type == USBDEVFS_URB_TYPE_ISO;
103 static int is_halted(USBHostDevice *s, int ep)
105 return s->endp_table[ep - 1].halted;
108 static void clear_halt(USBHostDevice *s, int ep)
110 s->endp_table[ep - 1].halted = 0;
113 static void set_halt(USBHostDevice *s, int ep)
115 s->endp_table[ep - 1].halted = 1;
118 static USBHostDevice *hostdev_list;
120 static void hostdev_link(USBHostDevice *dev)
122 dev->next = hostdev_list;
123 hostdev_list = dev;
126 static void hostdev_unlink(USBHostDevice *dev)
128 USBHostDevice *pdev = hostdev_list;
129 USBHostDevice **prev = &hostdev_list;
131 while (pdev) {
132 if (pdev == dev) {
133 *prev = dev->next;
134 return;
137 prev = &pdev->next;
138 pdev = pdev->next;
142 static USBHostDevice *hostdev_find(int bus_num, int addr)
144 USBHostDevice *s = hostdev_list;
145 while (s) {
146 if (s->bus_num == bus_num && s->addr == addr)
147 return s;
148 s = s->next;
150 return NULL;
154 * Async URB state.
155 * We always allocate one isoc descriptor even for bulk transfers
156 * to simplify allocation and casts.
158 typedef struct AsyncURB
160 struct usbdevfs_urb urb;
161 struct usbdevfs_iso_packet_desc isocpd;
163 USBPacket *packet;
164 USBHostDevice *hdev;
165 } AsyncURB;
167 static AsyncURB *async_alloc(void)
169 return (AsyncURB *) qemu_mallocz(sizeof(AsyncURB));
172 static void async_free(AsyncURB *aurb)
174 qemu_free(aurb);
177 static void async_complete(void *opaque)
179 USBHostDevice *s = opaque;
180 AsyncURB *aurb;
182 while (1) {
183 USBPacket *p;
185 int r = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &aurb);
186 if (r < 0) {
187 if (errno == EAGAIN)
188 return;
190 if (errno == ENODEV && !s->closing) {
191 printf("husb: device %d.%d disconnected\n", s->bus_num, s->addr);
192 usb_device_del_addr(0, s->dev.addr);
193 return;
196 dprintf("husb: async. reap urb failed errno %d\n", errno);
197 return;
200 p = aurb->packet;
202 dprintf("husb: async completed. aurb %p status %d alen %d\n",
203 aurb, aurb->urb.status, aurb->urb.actual_length);
205 if (p) {
206 switch (aurb->urb.status) {
207 case 0:
208 p->len = aurb->urb.actual_length;
209 break;
211 case -EPIPE:
212 set_halt(s, p->devep);
213 /* fall through */
214 default:
215 p->len = USB_RET_NAK;
216 break;
219 usb_packet_complete(p);
222 async_free(aurb);
226 static void async_cancel(USBPacket *unused, void *opaque)
228 AsyncURB *aurb = opaque;
229 USBHostDevice *s = aurb->hdev;
231 dprintf("husb: async cancel. aurb %p\n", aurb);
233 /* Mark it as dead (see async_complete above) */
234 aurb->packet = NULL;
236 int r = ioctl(s->fd, USBDEVFS_DISCARDURB, aurb);
237 if (r < 0) {
238 dprintf("husb: async. discard urb failed errno %d\n", errno);
242 static int usb_host_update_interfaces(USBHostDevice *dev, int configuration)
244 int dev_descr_len, config_descr_len;
245 int interface, nb_interfaces, nb_configurations;
246 int ret, i;
248 if (configuration == 0) /* address state - ignore */
249 return 1;
251 i = 0;
252 dev_descr_len = dev->descr[0];
253 if (dev_descr_len > dev->descr_len)
254 goto fail;
255 nb_configurations = dev->descr[17];
257 i += dev_descr_len;
258 while (i < dev->descr_len) {
259 dprintf("husb: i is %d, descr_len is %d, dl %d, dt %d\n", i, dev->descr_len,
260 dev->descr[i], dev->descr[i+1]);
262 if (dev->descr[i+1] != USB_DT_CONFIG) {
263 i += dev->descr[i];
264 continue;
266 config_descr_len = dev->descr[i];
268 printf("husb: config #%d need %d\n", dev->descr[i + 5], configuration);
270 if (configuration < 0 || configuration == dev->descr[i + 5])
271 break;
273 i += config_descr_len;
276 if (i >= dev->descr_len) {
277 fprintf(stderr, "husb: update iface failed. no matching configuration\n");
278 goto fail;
280 nb_interfaces = dev->descr[i + 4];
282 #ifdef USBDEVFS_DISCONNECT
283 /* earlier Linux 2.4 do not support that */
285 struct usbdevfs_ioctl ctrl;
286 for (interface = 0; interface < nb_interfaces; interface++) {
287 ctrl.ioctl_code = USBDEVFS_DISCONNECT;
288 ctrl.ifno = interface;
289 ret = ioctl(dev->fd, USBDEVFS_IOCTL, &ctrl);
290 if (ret < 0 && errno != ENODATA) {
291 perror("USBDEVFS_DISCONNECT");
292 goto fail;
296 #endif
298 /* XXX: only grab if all interfaces are free */
299 for (interface = 0; interface < nb_interfaces; interface++) {
300 ret = ioctl(dev->fd, USBDEVFS_CLAIMINTERFACE, &interface);
301 if (ret < 0) {
302 if (errno == EBUSY) {
303 printf("husb: update iface. device already grabbed\n");
304 } else {
305 perror("husb: failed to claim interface");
307 fail:
308 return 0;
312 printf("husb: %d interfaces claimed for configuration %d\n",
313 nb_interfaces, configuration);
315 return 1;
318 static void usb_host_handle_reset(USBDevice *dev)
320 USBHostDevice *s = (USBHostDevice *)dev;
322 dprintf("husb: reset device %u.%u\n", s->bus_num, s->addr);
324 ioctl(s->fd, USBDEVFS_RESET);
325 usb_host_update_interfaces(s, s->configuration);
328 static void usb_host_handle_destroy(USBDevice *dev)
330 USBHostDevice *s = (USBHostDevice *)dev;
332 s->closing = 1;
334 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
336 hostdev_unlink(s);
338 async_complete(s);
340 if (s->fd >= 0)
341 close(s->fd);
343 qemu_free(s);
346 static int usb_linux_update_endp_table(USBHostDevice *s);
348 static int usb_host_handle_control(USBDevice *dev,
349 int request,
350 int value,
351 int index,
352 int length,
353 uint8_t *data)
355 USBHostDevice *s = (USBHostDevice *)dev;
356 struct usb_ctrltransfer ct;
357 struct usbdevfs_setinterface si;
358 int intf_update_required = 0;
359 int ret;
361 if (request == (DeviceOutRequest | USB_REQ_SET_ADDRESS)) {
362 /* specific SET_ADDRESS support */
363 dev->addr = value;
364 return 0;
365 } else if (request == ((USB_RECIP_INTERFACE << 8) |
366 USB_REQ_SET_INTERFACE)) {
367 /* set alternate setting for the interface */
368 si.interface = index;
369 si.altsetting = value;
370 ret = ioctl(s->fd, USBDEVFS_SETINTERFACE, &si);
371 usb_linux_update_endp_table(s);
372 } else if (request == (DeviceOutRequest | USB_REQ_SET_CONFIGURATION)) {
373 dprintf("husb: ctrl set config %d\n", value & 0xff);
374 if (s->configuration != (value & 0xff)) {
375 s->configuration = (value & 0xff);
376 intf_update_required = 1;
378 goto do_request;
379 } else {
380 do_request:
381 ct.bRequestType = request >> 8;
382 ct.bRequest = request;
383 ct.wValue = value;
384 ct.wIndex = index;
385 ct.wLength = length;
386 ct.timeout = 50;
387 ct.data = data;
388 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
390 dprintf("husb: ctrl req 0x%x val 0x%x index %u len %u ret %d\n",
391 ct.bRequest, ct.wValue, ct.wIndex, ct.wLength, ret);
394 if (ret < 0) {
395 switch(errno) {
396 case ETIMEDOUT:
397 return USB_RET_NAK;
398 default:
399 return USB_RET_STALL;
401 } else {
402 if (intf_update_required) {
403 dprintf("husb: updating interfaces\n");
404 usb_host_update_interfaces(s, value & 0xff);
406 return ret;
410 static int usb_host_handle_data(USBDevice *dev, USBPacket *p)
412 USBHostDevice *s = (USBHostDevice *) dev;
413 AsyncURB *aurb;
414 struct usbdevfs_urb *urb;
415 int ret;
417 aurb = async_alloc();
418 if (!aurb) {
419 dprintf("husb: async malloc failed\n");
420 return USB_RET_NAK;
422 aurb->hdev = s;
423 aurb->packet = p;
425 urb = &aurb->urb;
427 if (p->pid == USB_TOKEN_IN)
428 urb->endpoint = p->devep | 0x80;
429 else
430 urb->endpoint = p->devep;
432 if (is_halted(s, p->devep)) {
433 ret = ioctl(s->fd, USBDEVFS_CLEAR_HALT, &urb->endpoint);
434 if (ret < 0) {
435 dprintf("husb: failed to clear halt. ep 0x%x errno %d\n",
436 urb->endpoint, errno);
437 return USB_RET_NAK;
439 clear_halt(s, p->devep);
442 urb->buffer = p->data;
443 urb->buffer_length = p->len;
445 if (is_isoc(s, p->devep)) {
446 /* Setup ISOC transfer */
447 urb->type = USBDEVFS_URB_TYPE_ISO;
448 urb->flags = USBDEVFS_URB_ISO_ASAP;
449 urb->number_of_packets = 1;
450 urb->iso_frame_desc[0].length = p->len;
451 } else {
452 /* Setup bulk transfer */
453 urb->type = USBDEVFS_URB_TYPE_BULK;
456 urb->usercontext = s;
458 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
460 dprintf("husb: data submit. ep 0x%x len %u aurb %p\n", urb->endpoint, p->len, aurb);
462 if (ret < 0) {
463 dprintf("husb: submit failed. errno %d\n", errno);
464 async_free(aurb);
466 switch(errno) {
467 case ETIMEDOUT:
468 return USB_RET_NAK;
469 case EPIPE:
470 default:
471 return USB_RET_STALL;
475 usb_defer_packet(p, async_cancel, aurb);
476 return USB_RET_ASYNC;
479 /* returns 1 on problem encountered or 0 for success */
480 static int usb_linux_update_endp_table(USBHostDevice *s)
482 uint8_t *descriptors;
483 uint8_t devep, type, configuration, alt_interface;
484 struct usb_ctrltransfer ct;
485 int interface, ret, length, i;
487 ct.bRequestType = USB_DIR_IN;
488 ct.bRequest = USB_REQ_GET_CONFIGURATION;
489 ct.wValue = 0;
490 ct.wIndex = 0;
491 ct.wLength = 1;
492 ct.data = &configuration;
493 ct.timeout = 50;
495 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
496 if (ret < 0) {
497 perror("usb_linux_update_endp_table");
498 return 1;
501 /* in address state */
502 if (configuration == 0)
503 return 1;
505 /* get the desired configuration, interface, and endpoint descriptors
506 * from device description */
507 descriptors = &s->descr[18];
508 length = s->descr_len - 18;
509 i = 0;
511 if (descriptors[i + 1] != USB_DT_CONFIG ||
512 descriptors[i + 5] != configuration) {
513 dprintf("invalid descriptor data - configuration\n");
514 return 1;
516 i += descriptors[i];
518 while (i < length) {
519 if (descriptors[i + 1] != USB_DT_INTERFACE ||
520 (descriptors[i + 1] == USB_DT_INTERFACE &&
521 descriptors[i + 4] == 0)) {
522 i += descriptors[i];
523 continue;
526 interface = descriptors[i + 2];
528 ct.bRequestType = USB_DIR_IN | USB_RECIP_INTERFACE;
529 ct.bRequest = USB_REQ_GET_INTERFACE;
530 ct.wValue = 0;
531 ct.wIndex = interface;
532 ct.wLength = 1;
533 ct.data = &alt_interface;
534 ct.timeout = 50;
536 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
537 if (ret < 0) {
538 perror("usb_linux_update_endp_table");
539 return 1;
542 /* the current interface descriptor is the active interface
543 * and has endpoints */
544 if (descriptors[i + 3] != alt_interface) {
545 i += descriptors[i];
546 continue;
549 /* advance to the endpoints */
550 while (i < length && descriptors[i +1] != USB_DT_ENDPOINT)
551 i += descriptors[i];
553 if (i >= length)
554 break;
556 while (i < length) {
557 if (descriptors[i + 1] != USB_DT_ENDPOINT)
558 break;
560 devep = descriptors[i + 2];
561 switch (descriptors[i + 3] & 0x3) {
562 case 0x00:
563 type = USBDEVFS_URB_TYPE_CONTROL;
564 break;
565 case 0x01:
566 type = USBDEVFS_URB_TYPE_ISO;
567 break;
568 case 0x02:
569 type = USBDEVFS_URB_TYPE_BULK;
570 break;
571 case 0x03:
572 type = USBDEVFS_URB_TYPE_INTERRUPT;
573 break;
574 default:
575 dprintf("usb_host: malformed endpoint type\n");
576 type = USBDEVFS_URB_TYPE_BULK;
578 s->endp_table[(devep & 0xf) - 1].type = type;
579 s->endp_table[(devep & 0xf) - 1].halted = 0;
581 i += descriptors[i];
584 return 0;
587 static USBDevice *usb_host_device_open_addr(int bus_num, int addr, const char *prod_name)
589 int fd = -1, ret;
590 USBHostDevice *dev = NULL;
591 struct usbdevfs_connectinfo ci;
592 char buf[1024];
594 dev = qemu_mallocz(sizeof(USBHostDevice));
595 if (!dev)
596 goto fail;
598 dev->bus_num = bus_num;
599 dev->addr = addr;
601 printf("husb: open device %d.%d\n", bus_num, addr);
603 snprintf(buf, sizeof(buf), USBDEVFS_PATH "/%03d/%03d",
604 bus_num, addr);
605 fd = open(buf, O_RDWR | O_NONBLOCK);
606 if (fd < 0) {
607 perror(buf);
608 goto fail;
611 /* read the device description */
612 dev->descr_len = read(fd, dev->descr, sizeof(dev->descr));
613 if (dev->descr_len <= 0) {
614 perror("husb: reading device data failed");
615 goto fail;
618 #ifdef DEBUG
620 int x;
621 printf("=== begin dumping device descriptor data ===\n");
622 for (x = 0; x < dev->descr_len; x++)
623 printf("%02x ", dev->descr[x]);
624 printf("\n=== end dumping device descriptor data ===\n");
626 #endif
628 dev->fd = fd;
629 dev->configuration = 1;
631 /* XXX - do something about initial configuration */
632 if (!usb_host_update_interfaces(dev, -1))
633 goto fail;
635 ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci);
636 if (ret < 0) {
637 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
638 goto fail;
641 printf("husb: grabbed usb device %d.%d\n", bus_num, addr);
643 ret = usb_linux_update_endp_table(dev);
644 if (ret)
645 goto fail;
647 if (ci.slow)
648 dev->dev.speed = USB_SPEED_LOW;
649 else
650 dev->dev.speed = USB_SPEED_HIGH;
651 dev->dev.handle_packet = usb_generic_handle_packet;
653 dev->dev.handle_reset = usb_host_handle_reset;
654 dev->dev.handle_control = usb_host_handle_control;
655 dev->dev.handle_data = usb_host_handle_data;
656 dev->dev.handle_destroy = usb_host_handle_destroy;
658 if (!prod_name || prod_name[0] == '\0')
659 snprintf(dev->dev.devname, sizeof(dev->dev.devname),
660 "host:%d.%d", bus_num, addr);
661 else
662 pstrcpy(dev->dev.devname, sizeof(dev->dev.devname),
663 prod_name);
665 /* USB devio uses 'write' flag to check for async completions */
666 qemu_set_fd_handler(dev->fd, NULL, async_complete, dev);
668 hostdev_link(dev);
670 return (USBDevice *) dev;
672 fail:
673 if (dev)
674 qemu_free(dev);
676 close(fd);
677 return NULL;
680 USBDevice *usb_host_device_open(const char *devname)
682 int bus_num, addr;
683 char product_name[PRODUCT_NAME_SZ];
685 if (usb_host_find_device(&bus_num, &addr,
686 product_name, sizeof(product_name),
687 devname) < 0)
688 return NULL;
690 if (hostdev_find(bus_num, addr)) {
691 term_printf("husb: host usb device %d.%d is already open\n", bus_num, addr);
692 return NULL;
695 return usb_host_device_open_addr(bus_num, addr, product_name);
698 static int get_tag_value(char *buf, int buf_size,
699 const char *str, const char *tag,
700 const char *stopchars)
702 const char *p;
703 char *q;
704 p = strstr(str, tag);
705 if (!p)
706 return -1;
707 p += strlen(tag);
708 while (isspace(*p))
709 p++;
710 q = buf;
711 while (*p != '\0' && !strchr(stopchars, *p)) {
712 if ((q - buf) < (buf_size - 1))
713 *q++ = *p;
714 p++;
716 *q = '\0';
717 return q - buf;
720 static int usb_host_scan(void *opaque, USBScanFunc *func)
722 FILE *f;
723 char line[1024];
724 char buf[1024];
725 int bus_num, addr, speed, device_count, class_id, product_id, vendor_id;
726 int ret;
727 char product_name[512];
729 f = fopen(USBDEVFS_PATH "/devices", "r");
730 if (!f) {
731 term_printf("husb: could not open %s\n", USBDEVFS_PATH "/devices");
732 return 0;
734 device_count = 0;
735 bus_num = addr = speed = class_id = product_id = vendor_id = 0;
736 ret = 0;
737 for(;;) {
738 if (fgets(line, sizeof(line), f) == NULL)
739 break;
740 if (strlen(line) > 0)
741 line[strlen(line) - 1] = '\0';
742 if (line[0] == 'T' && line[1] == ':') {
743 if (device_count && (vendor_id || product_id)) {
744 /* New device. Add the previously discovered device. */
745 ret = func(opaque, bus_num, addr, class_id, vendor_id,
746 product_id, product_name, speed);
747 if (ret)
748 goto the_end;
750 if (get_tag_value(buf, sizeof(buf), line, "Bus=", " ") < 0)
751 goto fail;
752 bus_num = atoi(buf);
753 if (get_tag_value(buf, sizeof(buf), line, "Dev#=", " ") < 0)
754 goto fail;
755 addr = atoi(buf);
756 if (get_tag_value(buf, sizeof(buf), line, "Spd=", " ") < 0)
757 goto fail;
758 if (!strcmp(buf, "480"))
759 speed = USB_SPEED_HIGH;
760 else if (!strcmp(buf, "1.5"))
761 speed = USB_SPEED_LOW;
762 else
763 speed = USB_SPEED_FULL;
764 product_name[0] = '\0';
765 class_id = 0xff;
766 device_count++;
767 product_id = 0;
768 vendor_id = 0;
769 } else if (line[0] == 'P' && line[1] == ':') {
770 if (get_tag_value(buf, sizeof(buf), line, "Vendor=", " ") < 0)
771 goto fail;
772 vendor_id = strtoul(buf, NULL, 16);
773 if (get_tag_value(buf, sizeof(buf), line, "ProdID=", " ") < 0)
774 goto fail;
775 product_id = strtoul(buf, NULL, 16);
776 } else if (line[0] == 'S' && line[1] == ':') {
777 if (get_tag_value(buf, sizeof(buf), line, "Product=", "") < 0)
778 goto fail;
779 pstrcpy(product_name, sizeof(product_name), buf);
780 } else if (line[0] == 'D' && line[1] == ':') {
781 if (get_tag_value(buf, sizeof(buf), line, "Cls=", " (") < 0)
782 goto fail;
783 class_id = strtoul(buf, NULL, 16);
785 fail: ;
787 if (device_count && (vendor_id || product_id)) {
788 /* Add the last device. */
789 ret = func(opaque, bus_num, addr, class_id, vendor_id,
790 product_id, product_name, speed);
792 the_end:
793 fclose(f);
794 return ret;
797 struct USBAutoFilter {
798 struct USBAutoFilter *next;
799 int bus_num;
800 int addr;
801 int vendor_id;
802 int product_id;
805 static QEMUTimer *usb_auto_timer;
806 static struct USBAutoFilter *usb_auto_filter;
808 static int usb_host_auto_scan(void *opaque, int bus_num, int addr,
809 int class_id, int vendor_id, int product_id,
810 const char *product_name, int speed)
812 struct USBAutoFilter *f;
813 struct USBDevice *dev;
815 /* Ignore hubs */
816 if (class_id == 9)
817 return 0;
819 for (f = usb_auto_filter; f; f = f->next) {
820 if (f->bus_num >= 0 && f->bus_num != bus_num)
821 continue;
823 if (f->addr >= 0 && f->addr != addr)
824 continue;
826 if (f->vendor_id >= 0 && f->vendor_id != vendor_id)
827 continue;
829 if (f->product_id >= 0 && f->product_id != product_id)
830 continue;
832 /* We got a match */
834 /* Allredy attached ? */
835 if (hostdev_find(bus_num, addr))
836 return 0;
838 dprintf("husb: auto open: bus_num %d addr %d\n", bus_num, addr);
840 dev = usb_host_device_open_addr(bus_num, addr, product_name);
841 if (dev)
842 usb_device_add_dev(dev);
845 return 0;
848 static void usb_host_auto_timer(void *unused)
850 usb_host_scan(NULL, usb_host_auto_scan);
851 qemu_mod_timer(usb_auto_timer, qemu_get_clock(rt_clock) + 2000);
855 * Add autoconnect filter
856 * -1 means 'any' (device, vendor, etc)
858 static void usb_host_auto_add(int bus_num, int addr, int vendor_id, int product_id)
860 struct USBAutoFilter *f = qemu_mallocz(sizeof(*f));
861 if (!f) {
862 fprintf(stderr, "husb: failed to allocate auto filter\n");
863 return;
866 f->bus_num = bus_num;
867 f->addr = addr;
868 f->vendor_id = vendor_id;
869 f->product_id = product_id;
871 if (!usb_auto_filter) {
873 * First entry. Init and start the monitor.
874 * Right now we're using timer to check for new devices.
875 * If this turns out to be too expensive we can move that into a
876 * separate thread.
878 usb_auto_timer = qemu_new_timer(rt_clock, usb_host_auto_timer, NULL);
879 if (!usb_auto_timer) {
880 fprintf(stderr, "husb: failed to allocate auto scan timer\n");
881 qemu_free(f);
882 return;
885 /* Check for new devices every two seconds */
886 qemu_mod_timer(usb_auto_timer, qemu_get_clock(rt_clock) + 2000);
889 dprintf("husb: auto filter: bus_num %d addr %d vid %d pid %d\n",
890 bus_num, addr, vendor_id, product_id);
892 f->next = usb_auto_filter;
893 usb_auto_filter = f;
896 typedef struct FindDeviceState {
897 int vendor_id;
898 int product_id;
899 int bus_num;
900 int addr;
901 char product_name[PRODUCT_NAME_SZ];
902 } FindDeviceState;
904 static int usb_host_find_device_scan(void *opaque, int bus_num, int addr,
905 int class_id,
906 int vendor_id, int product_id,
907 const char *product_name, int speed)
909 FindDeviceState *s = opaque;
910 if ((vendor_id == s->vendor_id &&
911 product_id == s->product_id) ||
912 (bus_num == s->bus_num &&
913 addr == s->addr)) {
914 pstrcpy(s->product_name, PRODUCT_NAME_SZ, product_name);
915 s->bus_num = bus_num;
916 s->addr = addr;
917 return 1;
918 } else {
919 return 0;
923 /* the syntax is :
924 'bus.addr' (decimal numbers) or
925 'vendor_id:product_id' (hexa numbers) */
926 static int usb_host_find_device(int *pbus_num, int *paddr,
927 char *product_name, int product_name_size,
928 const char *devname)
930 const char *p;
931 int ret;
932 FindDeviceState fs;
934 p = strchr(devname, '.');
935 if (p) {
936 *pbus_num = strtoul(devname, NULL, 0);
938 if (*(p + 1) == '*') {
939 usb_host_auto_add(*pbus_num, -1, -1, -1);
940 return -1;
943 *paddr = strtoul(p + 1, NULL, 0);
944 fs.bus_num = *pbus_num;
945 fs.addr = *paddr;
946 ret = usb_host_scan(&fs, usb_host_find_device_scan);
947 if (ret)
948 pstrcpy(product_name, product_name_size, fs.product_name);
949 return 0;
951 p = strchr(devname, ':');
952 if (p) {
953 fs.vendor_id = strtoul(devname, NULL, 16);
955 if (*(p + 1) == '*') {
956 usb_host_auto_add(-1, -1, fs.vendor_id, -1);
957 return -1;
960 fs.product_id = strtoul(p + 1, NULL, 16);
961 ret = usb_host_scan(&fs, usb_host_find_device_scan);
962 if (ret) {
963 *pbus_num = fs.bus_num;
964 *paddr = fs.addr;
965 pstrcpy(product_name, product_name_size, fs.product_name);
966 return 0;
969 return -1;
972 /**********************/
973 /* USB host device info */
975 struct usb_class_info {
976 int class;
977 const char *class_name;
980 static const struct usb_class_info usb_class_info[] = {
981 { USB_CLASS_AUDIO, "Audio"},
982 { USB_CLASS_COMM, "Communication"},
983 { USB_CLASS_HID, "HID"},
984 { USB_CLASS_HUB, "Hub" },
985 { USB_CLASS_PHYSICAL, "Physical" },
986 { USB_CLASS_PRINTER, "Printer" },
987 { USB_CLASS_MASS_STORAGE, "Storage" },
988 { USB_CLASS_CDC_DATA, "Data" },
989 { USB_CLASS_APP_SPEC, "Application Specific" },
990 { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
991 { USB_CLASS_STILL_IMAGE, "Still Image" },
992 { USB_CLASS_CSCID, "Smart Card" },
993 { USB_CLASS_CONTENT_SEC, "Content Security" },
994 { -1, NULL }
997 static const char *usb_class_str(uint8_t class)
999 const struct usb_class_info *p;
1000 for(p = usb_class_info; p->class != -1; p++) {
1001 if (p->class == class)
1002 break;
1004 return p->class_name;
1007 static void usb_info_device(int bus_num, int addr, int class_id,
1008 int vendor_id, int product_id,
1009 const char *product_name,
1010 int speed)
1012 const char *class_str, *speed_str;
1014 switch(speed) {
1015 case USB_SPEED_LOW:
1016 speed_str = "1.5";
1017 break;
1018 case USB_SPEED_FULL:
1019 speed_str = "12";
1020 break;
1021 case USB_SPEED_HIGH:
1022 speed_str = "480";
1023 break;
1024 default:
1025 speed_str = "?";
1026 break;
1029 term_printf(" Device %d.%d, speed %s Mb/s\n",
1030 bus_num, addr, speed_str);
1031 class_str = usb_class_str(class_id);
1032 if (class_str)
1033 term_printf(" %s:", class_str);
1034 else
1035 term_printf(" Class %02x:", class_id);
1036 term_printf(" USB device %04x:%04x", vendor_id, product_id);
1037 if (product_name[0] != '\0')
1038 term_printf(", %s", product_name);
1039 term_printf("\n");
1042 static int usb_host_info_device(void *opaque, int bus_num, int addr,
1043 int class_id,
1044 int vendor_id, int product_id,
1045 const char *product_name,
1046 int speed)
1048 usb_info_device(bus_num, addr, class_id, vendor_id, product_id,
1049 product_name, speed);
1050 return 0;
1053 void usb_host_info(void)
1055 usb_host_scan(NULL, usb_host_info_device);
1058 #else
1060 void usb_host_info(void)
1062 term_printf("USB host devices not supported\n");
1065 /* XXX: modify configure to compile the right host driver */
1066 USBDevice *usb_host_device_open(const char *devname)
1068 return NULL;
1071 #endif