Add fls()
[qemu-kvm/fedora.git] / usb-linux.c
blob78cd3171a586dfcc3879673689bd020fe5bd8852
1 /*
2 * Linux host USB redirector
4 * Copyright (c) 2005 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include "qemu-common.h"
25 #include "hw/usb.h"
26 #include "console.h"
28 #if defined(__linux__)
29 #define __user
31 #include <dirent.h>
32 #include <sys/ioctl.h>
33 #include <linux/usbdevice_fs.h>
34 #include <linux/version.h>
35 #include <signal.h>
37 /* We redefine it to avoid version problems */
38 struct usb_ctrltransfer {
39 uint8_t bRequestType;
40 uint8_t bRequest;
41 uint16_t wValue;
42 uint16_t wIndex;
43 uint16_t wLength;
44 uint32_t timeout;
45 void *data;
48 typedef int USBScanFunc(void *opaque, int bus_num, int addr, int class_id,
49 int vendor_id, int product_id,
50 const char *product_name, int speed);
51 static int usb_host_find_device(int *pbus_num, int *paddr,
52 char *product_name, int product_name_size,
53 const char *devname);
55 //#define DEBUG
56 //#define DEBUG_ISOCH
57 //#define USE_ASYNCIO
59 #define USBDEVFS_PATH "/proc/bus/usb"
60 #define PRODUCT_NAME_SZ 32
61 #define SIG_ISOCOMPLETE (SIGRTMIN+7)
62 #define MAX_ENDPOINTS 16
64 struct sigaction sigact;
66 /* endpoint association data */
67 struct endp_data {
68 uint8_t type;
71 /* FIXME: move USBPacket to PendingURB */
72 typedef struct USBHostDevice {
73 USBDevice dev;
74 int fd;
75 int pipe_fds[2];
76 USBPacket *packet;
77 struct endp_data endp_table[MAX_ENDPOINTS];
78 int configuration;
79 uint8_t descr[1024];
80 int descr_len;
81 int urbs_ready;
82 } USBHostDevice;
84 typedef struct PendingURB {
85 struct usbdevfs_urb *urb;
86 int status;
87 struct PendingURB *next;
88 } PendingURB;
90 static PendingURB *pending_urbs = NULL;
92 static int add_pending_urb(struct usbdevfs_urb *urb)
94 PendingURB *purb = qemu_mallocz(sizeof(PendingURB));
95 if (purb) {
96 purb->urb = urb;
97 purb->status = 0;
98 purb->next = pending_urbs;
99 pending_urbs = purb;
100 return 1;
102 return 0;
105 static int del_pending_urb(struct usbdevfs_urb *urb)
107 PendingURB *purb = pending_urbs;
108 PendingURB *prev = NULL;
110 while (purb && purb->urb != urb) {
111 prev = purb;
112 purb = purb->next;
115 if (purb && purb->urb == urb) {
116 if (prev) {
117 prev->next = purb->next;
118 } else {
119 pending_urbs = purb->next;
121 qemu_free(purb);
122 return 1;
124 return 0;
127 #ifdef USE_ASYNCIO
128 static PendingURB *get_pending_urb(struct usbdevfs_urb *urb)
130 PendingURB *purb = pending_urbs;
132 while (purb && purb->urb != urb) {
133 purb = purb->next;
136 if (purb && purb->urb == urb) {
137 return purb;
139 return NULL;
141 #endif
143 static int usb_host_update_interfaces(USBHostDevice *dev, int configuration)
145 int dev_descr_len, config_descr_len;
146 int interface, nb_interfaces, nb_configurations;
147 int ret, i;
149 if (configuration == 0) /* address state - ignore */
150 return 1;
152 i = 0;
153 dev_descr_len = dev->descr[0];
154 if (dev_descr_len > dev->descr_len)
155 goto fail;
156 nb_configurations = dev->descr[17];
158 i += dev_descr_len;
159 while (i < dev->descr_len) {
160 #ifdef DEBUG
161 printf("i is %d, descr_len is %d, dl %d, dt %d\n", i, dev->descr_len,
162 dev->descr[i], dev->descr[i+1]);
163 #endif
164 if (dev->descr[i+1] != USB_DT_CONFIG) {
165 i += dev->descr[i];
166 continue;
168 config_descr_len = dev->descr[i];
170 if (configuration == dev->descr[i + 5])
171 break;
173 i += config_descr_len;
176 if (i >= dev->descr_len) {
177 printf("usb_host: error - device has no matching configuration\n");
178 goto fail;
180 nb_interfaces = dev->descr[i + 4];
182 #ifdef USBDEVFS_DISCONNECT
183 /* earlier Linux 2.4 do not support that */
185 struct usbdevfs_ioctl ctrl;
186 for (interface = 0; interface < nb_interfaces; interface++) {
187 ctrl.ioctl_code = USBDEVFS_DISCONNECT;
188 ctrl.ifno = interface;
189 ret = ioctl(dev->fd, USBDEVFS_IOCTL, &ctrl);
190 if (ret < 0 && errno != ENODATA) {
191 perror("USBDEVFS_DISCONNECT");
192 goto fail;
196 #endif
198 /* XXX: only grab if all interfaces are free */
199 for (interface = 0; interface < nb_interfaces; interface++) {
200 ret = ioctl(dev->fd, USBDEVFS_CLAIMINTERFACE, &interface);
201 if (ret < 0) {
202 if (errno == EBUSY) {
203 fprintf(stderr,
204 "usb_host: warning - device already grabbed\n");
205 } else {
206 perror("USBDEVFS_CLAIMINTERFACE");
208 fail:
209 return 0;
213 #ifdef DEBUG
214 printf("usb_host: %d interfaces claimed for configuration %d\n",
215 nb_interfaces, configuration);
216 #endif
218 return 1;
221 static void usb_host_handle_reset(USBDevice *dev)
223 #if 0
224 USBHostDevice *s = (USBHostDevice *)dev;
225 /* USBDEVFS_RESET, but not the first time as it has already be
226 done by the host OS */
227 ioctl(s->fd, USBDEVFS_RESET);
228 #endif
231 static void usb_host_handle_destroy(USBDevice *dev)
233 USBHostDevice *s = (USBHostDevice *)dev;
235 if (s->fd >= 0)
236 close(s->fd);
237 qemu_free(s);
240 static int usb_linux_update_endp_table(USBHostDevice *s);
242 static int usb_host_handle_control(USBDevice *dev,
243 int request,
244 int value,
245 int index,
246 int length,
247 uint8_t *data)
249 USBHostDevice *s = (USBHostDevice *)dev;
250 struct usb_ctrltransfer ct;
251 struct usbdevfs_setinterface si;
252 int intf_update_required = 0;
253 int ret;
255 if (request == (DeviceOutRequest | USB_REQ_SET_ADDRESS)) {
256 /* specific SET_ADDRESS support */
257 dev->addr = value;
258 return 0;
259 } else if (request == ((USB_RECIP_INTERFACE << 8) |
260 USB_REQ_SET_INTERFACE)) {
261 /* set alternate setting for the interface */
262 si.interface = index;
263 si.altsetting = value;
264 ret = ioctl(s->fd, USBDEVFS_SETINTERFACE, &si);
265 usb_linux_update_endp_table(s);
266 } else if (request == (DeviceOutRequest | USB_REQ_SET_CONFIGURATION)) {
267 #ifdef DEBUG
268 printf("usb_host_handle_control: SET_CONFIGURATION request - "
269 "config %d\n", value & 0xff);
270 #endif
271 if (s->configuration != (value & 0xff)) {
272 s->configuration = (value & 0xff);
273 intf_update_required = 1;
275 goto do_request;
276 } else {
277 do_request:
278 ct.bRequestType = request >> 8;
279 ct.bRequest = request;
280 ct.wValue = value;
281 ct.wIndex = index;
282 ct.wLength = length;
283 ct.timeout = 50;
284 ct.data = data;
285 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
288 if (ret < 0) {
289 switch(errno) {
290 case ETIMEDOUT:
291 return USB_RET_NAK;
292 default:
293 return USB_RET_STALL;
295 } else {
296 if (intf_update_required) {
297 #ifdef DEBUG
298 printf("usb_host_handle_control: updating interfaces\n");
299 #endif
300 usb_host_update_interfaces(s, value & 0xff);
302 return ret;
306 static int usb_host_handle_isoch(USBDevice *dev, USBPacket *p);
308 static int usb_host_handle_data(USBDevice *dev, USBPacket *p)
310 USBHostDevice *s = (USBHostDevice *)dev;
311 struct usbdevfs_bulktransfer bt;
312 int ret;
313 uint8_t devep = p->devep;
315 if (s->endp_table[p->devep - 1].type == USBDEVFS_URB_TYPE_ISO) {
316 return usb_host_handle_isoch(dev, p);
319 /* XXX: optimize and handle all data types by looking at the
320 config descriptor */
321 if (p->pid == USB_TOKEN_IN)
322 devep |= 0x80;
323 bt.ep = devep;
324 bt.len = p->len;
325 bt.timeout = 50;
326 bt.data = p->data;
327 ret = ioctl(s->fd, USBDEVFS_BULK, &bt);
328 if (ret < 0) {
329 switch(errno) {
330 case ETIMEDOUT:
331 return USB_RET_NAK;
332 case EPIPE:
333 default:
334 #ifdef DEBUG
335 printf("handle_data: errno=%d\n", errno);
336 #endif
337 return USB_RET_STALL;
339 } else {
340 return ret;
344 #ifdef USE_ASYNCIO
345 static void urb_completion_pipe_read(void *opaque)
347 USBHostDevice *s = opaque;
348 USBPacket *p = s->packet;
349 PendingURB *pending_urb = NULL;
350 struct usbdevfs_urb *purb = NULL;
351 int len, ret;
353 len = read(s->pipe_fds[0], &pending_urb, sizeof(pending_urb));
354 if (len != sizeof(pending_urb)) {
355 printf("urb_completion: error reading pending_urb, len=%d\n", len);
356 return;
359 /* FIXME: handle pending_urb->status */
360 del_pending_urb(pending_urb->urb);
362 if (!p) {
363 s->urbs_ready++;
364 return;
367 ret = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &purb);
368 if (ret < 0) {
369 printf("urb_completion: REAPURBNDELAY ioctl=%d errno=%d\n",
370 ret, errno);
371 return;
374 #ifdef DEBUG_ISOCH
375 if (purb == pending_urb->urb) {
376 printf("urb_completion: urb mismatch reaped=%p pending=%p\n",
377 purb, urb);
379 #endif
381 p->len = purb->actual_length;
382 usb_packet_complete(p);
383 qemu_free(purb);
384 s->packet = NULL;
387 static void isoch_done(int signum, siginfo_t *info, void *context)
389 struct usbdevfs_urb *urb = (struct usbdevfs_urb *)info->si_addr;
390 USBHostDevice *s = (USBHostDevice *)urb->usercontext;
391 PendingURB *purb;
393 if (info->si_code != SI_ASYNCIO ||
394 info->si_signo != SIG_ISOCOMPLETE) {
395 return;
398 purb = get_pending_urb(urb);
399 if (purb) {
400 purb->status = info->si_errno;
401 write(s->pipe_fds[1], &purb, sizeof(purb));
404 #endif
406 static int usb_host_handle_isoch(USBDevice *dev, USBPacket *p)
408 USBHostDevice *s = (USBHostDevice *)dev;
409 struct usbdevfs_urb *urb, *purb = NULL;
410 int ret;
411 uint8_t devep = p->devep;
413 if (p->pid == USB_TOKEN_IN)
414 devep |= 0x80;
416 urb = qemu_mallocz(sizeof(struct usbdevfs_urb) +
417 sizeof(struct usbdevfs_iso_packet_desc));
418 if (!urb) {
419 printf("usb_host_handle_isoch: malloc failed\n");
420 return 0;
423 urb->type = USBDEVFS_URB_TYPE_ISO;
424 urb->endpoint = devep;
425 urb->status = 0;
426 urb->flags = USBDEVFS_URB_ISO_ASAP;
427 urb->buffer = p->data;
428 urb->buffer_length = p->len;
429 urb->actual_length = 0;
430 urb->start_frame = 0;
431 urb->error_count = 0;
432 #ifdef USE_ASYNCIO
433 urb->signr = SIG_ISOCOMPLETE;
434 #else
435 urb->signr = 0;
436 #endif
437 urb->usercontext = s;
438 urb->number_of_packets = 1;
439 urb->iso_frame_desc[0].length = p->len;
440 urb->iso_frame_desc[0].actual_length = 0;
441 urb->iso_frame_desc[0].status = 0;
442 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
443 if (ret == 0) {
444 if (!add_pending_urb(urb)) {
445 printf("usb_host_handle_isoch: add_pending_urb failed %p\n", urb);
447 } else {
448 printf("usb_host_handle_isoch: SUBMITURB ioctl=%d errno=%d\n",
449 ret, errno);
450 qemu_free(urb);
451 switch(errno) {
452 case ETIMEDOUT:
453 return USB_RET_NAK;
454 case EPIPE:
455 default:
456 return USB_RET_STALL;
459 #ifdef USE_ASYNCIO
460 /* FIXME: handle urbs_ready together with sync io
461 * workaround for injecting the signaled urbs into current frame */
462 if (s->urbs_ready > 0) {
463 ret = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &purb);
464 if (ret == 0) {
465 ret = purb->actual_length;
466 qemu_free(purb);
467 s->urbs_ready--;
469 return ret;
471 s->packet = p;
472 return USB_RET_ASYNC;
473 #else
474 ret = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &purb);
475 if (ret == 0) {
476 if (del_pending_urb(purb)) {
477 ret = purb->actual_length;
478 qemu_free(purb);
479 } else {
480 printf("usb_host_handle_isoch: del_pending_urb failed %p\n", purb);
482 } else {
483 #ifdef DEBUG_ISOCH
484 printf("usb_host_handle_isoch: REAPURBNDELAY ioctl=%d errno=%d\n",
485 ret, errno);
486 #endif
488 return ret;
489 #endif
492 /* returns 1 on problem encountered or 0 for success */
493 static int usb_linux_update_endp_table(USBHostDevice *s)
495 uint8_t *descriptors;
496 uint8_t devep, type, configuration, alt_interface;
497 struct usb_ctrltransfer ct;
498 int interface, ret, length, i;
500 ct.bRequestType = USB_DIR_IN;
501 ct.bRequest = USB_REQ_GET_CONFIGURATION;
502 ct.wValue = 0;
503 ct.wIndex = 0;
504 ct.wLength = 1;
505 ct.data = &configuration;
506 ct.timeout = 50;
508 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
509 if (ret < 0) {
510 perror("usb_linux_update_endp_table");
511 return 1;
514 /* in address state */
515 if (configuration == 0)
516 return 1;
518 /* get the desired configuration, interface, and endpoint descriptors
519 * from device description */
520 descriptors = &s->descr[18];
521 length = s->descr_len - 18;
522 i = 0;
524 if (descriptors[i + 1] != USB_DT_CONFIG ||
525 descriptors[i + 5] != configuration) {
526 printf("invalid descriptor data - configuration\n");
527 return 1;
529 i += descriptors[i];
531 while (i < length) {
532 if (descriptors[i + 1] != USB_DT_INTERFACE ||
533 (descriptors[i + 1] == USB_DT_INTERFACE &&
534 descriptors[i + 4] == 0)) {
535 i += descriptors[i];
536 continue;
539 interface = descriptors[i + 2];
541 ct.bRequestType = USB_DIR_IN | USB_RECIP_INTERFACE;
542 ct.bRequest = USB_REQ_GET_INTERFACE;
543 ct.wValue = 0;
544 ct.wIndex = interface;
545 ct.wLength = 1;
546 ct.data = &alt_interface;
547 ct.timeout = 50;
549 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
550 if (ret < 0) {
551 perror("usb_linux_update_endp_table");
552 return 1;
555 /* the current interface descriptor is the active interface
556 * and has endpoints */
557 if (descriptors[i + 3] != alt_interface) {
558 i += descriptors[i];
559 continue;
562 /* advance to the endpoints */
563 while (i < length && descriptors[i +1] != USB_DT_ENDPOINT)
564 i += descriptors[i];
566 if (i >= length)
567 break;
569 while (i < length) {
570 if (descriptors[i + 1] != USB_DT_ENDPOINT)
571 break;
573 devep = descriptors[i + 2];
574 switch (descriptors[i + 3] & 0x3) {
575 case 0x00:
576 type = USBDEVFS_URB_TYPE_CONTROL;
577 break;
578 case 0x01:
579 type = USBDEVFS_URB_TYPE_ISO;
580 break;
581 case 0x02:
582 type = USBDEVFS_URB_TYPE_BULK;
583 break;
584 case 0x03:
585 type = USBDEVFS_URB_TYPE_INTERRUPT;
586 break;
587 default:
588 printf("usb_host: malformed endpoint type\n");
589 type = USBDEVFS_URB_TYPE_BULK;
591 s->endp_table[(devep & 0xf) - 1].type = type;
593 i += descriptors[i];
596 return 0;
599 /* XXX: exclude high speed devices or implement EHCI */
600 USBDevice *usb_host_device_open(const char *devname)
602 int fd = -1, ret;
603 USBHostDevice *dev = NULL;
604 struct usbdevfs_connectinfo ci;
605 char buf[1024];
606 int bus_num, addr;
607 char product_name[PRODUCT_NAME_SZ];
609 dev = qemu_mallocz(sizeof(USBHostDevice));
610 if (!dev)
611 goto fail;
613 #ifdef DEBUG_ISOCH
614 printf("usb_host_device_open %s\n", devname);
615 #endif
616 if (usb_host_find_device(&bus_num, &addr,
617 product_name, sizeof(product_name),
618 devname) < 0)
619 return NULL;
621 snprintf(buf, sizeof(buf), USBDEVFS_PATH "/%03d/%03d",
622 bus_num, addr);
623 fd = open(buf, O_RDWR | O_NONBLOCK);
624 if (fd < 0) {
625 perror(buf);
626 return NULL;
629 /* read the device description */
630 dev->descr_len = read(fd, dev->descr, sizeof(dev->descr));
631 if (dev->descr_len <= 0) {
632 perror("usb_host_device_open: reading device data failed");
633 goto fail;
636 #ifdef DEBUG
638 int x;
639 printf("=== begin dumping device descriptor data ===\n");
640 for (x = 0; x < dev->descr_len; x++)
641 printf("%02x ", dev->descr[x]);
642 printf("\n=== end dumping device descriptor data ===\n");
644 #endif
646 dev->fd = fd;
647 dev->configuration = 1;
649 /* XXX - do something about initial configuration */
650 if (!usb_host_update_interfaces(dev, 1))
651 goto fail;
653 ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci);
654 if (ret < 0) {
655 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
656 goto fail;
659 #ifdef DEBUG
660 printf("host USB device %d.%d grabbed\n", bus_num, addr);
661 #endif
663 ret = usb_linux_update_endp_table(dev);
664 if (ret)
665 goto fail;
667 if (ci.slow)
668 dev->dev.speed = USB_SPEED_LOW;
669 else
670 dev->dev.speed = USB_SPEED_HIGH;
671 dev->dev.handle_packet = usb_generic_handle_packet;
673 dev->dev.handle_reset = usb_host_handle_reset;
674 dev->dev.handle_control = usb_host_handle_control;
675 dev->dev.handle_data = usb_host_handle_data;
676 dev->dev.handle_destroy = usb_host_handle_destroy;
678 if (product_name[0] == '\0')
679 snprintf(dev->dev.devname, sizeof(dev->dev.devname),
680 "host:%s", devname);
681 else
682 pstrcpy(dev->dev.devname, sizeof(dev->dev.devname),
683 product_name);
685 #ifdef USE_ASYNCIO
686 /* set up the signal handlers */
687 sigemptyset(&sigact.sa_mask);
688 sigact.sa_sigaction = isoch_done;
689 sigact.sa_flags = SA_SIGINFO;
690 sigact.sa_restorer = 0;
691 ret = sigaction(SIG_ISOCOMPLETE, &sigact, NULL);
692 if (ret < 0) {
693 perror("usb_host_device_open: sigaction failed");
694 goto fail;
697 if (pipe(dev->pipe_fds) < 0) {
698 perror("usb_host_device_open: pipe creation failed");
699 goto fail;
701 fcntl(dev->pipe_fds[0], F_SETFL, O_NONBLOCK | O_ASYNC);
702 fcntl(dev->pipe_fds[1], F_SETFL, O_NONBLOCK);
703 qemu_set_fd_handler(dev->pipe_fds[0], urb_completion_pipe_read, NULL, dev);
704 #endif
705 dev->urbs_ready = 0;
706 return (USBDevice *)dev;
707 fail:
708 if (dev)
709 qemu_free(dev);
710 close(fd);
711 return NULL;
714 static int get_tag_value(char *buf, int buf_size,
715 const char *str, const char *tag,
716 const char *stopchars)
718 const char *p;
719 char *q;
720 p = strstr(str, tag);
721 if (!p)
722 return -1;
723 p += strlen(tag);
724 while (isspace(*p))
725 p++;
726 q = buf;
727 while (*p != '\0' && !strchr(stopchars, *p)) {
728 if ((q - buf) < (buf_size - 1))
729 *q++ = *p;
730 p++;
732 *q = '\0';
733 return q - buf;
736 static int usb_host_scan(void *opaque, USBScanFunc *func)
738 FILE *f;
739 char line[1024];
740 char buf[1024];
741 int bus_num, addr, speed, device_count, class_id, product_id, vendor_id;
742 int ret;
743 char product_name[512];
745 f = fopen(USBDEVFS_PATH "/devices", "r");
746 if (!f) {
747 term_printf("Could not open %s\n", USBDEVFS_PATH "/devices");
748 return 0;
750 device_count = 0;
751 bus_num = addr = speed = class_id = product_id = vendor_id = 0;
752 ret = 0;
753 for(;;) {
754 if (fgets(line, sizeof(line), f) == NULL)
755 break;
756 if (strlen(line) > 0)
757 line[strlen(line) - 1] = '\0';
758 if (line[0] == 'T' && line[1] == ':') {
759 if (device_count && (vendor_id || product_id)) {
760 /* New device. Add the previously discovered device. */
761 ret = func(opaque, bus_num, addr, class_id, vendor_id,
762 product_id, product_name, speed);
763 if (ret)
764 goto the_end;
766 if (get_tag_value(buf, sizeof(buf), line, "Bus=", " ") < 0)
767 goto fail;
768 bus_num = atoi(buf);
769 if (get_tag_value(buf, sizeof(buf), line, "Dev#=", " ") < 0)
770 goto fail;
771 addr = atoi(buf);
772 if (get_tag_value(buf, sizeof(buf), line, "Spd=", " ") < 0)
773 goto fail;
774 if (!strcmp(buf, "480"))
775 speed = USB_SPEED_HIGH;
776 else if (!strcmp(buf, "1.5"))
777 speed = USB_SPEED_LOW;
778 else
779 speed = USB_SPEED_FULL;
780 product_name[0] = '\0';
781 class_id = 0xff;
782 device_count++;
783 product_id = 0;
784 vendor_id = 0;
785 } else if (line[0] == 'P' && line[1] == ':') {
786 if (get_tag_value(buf, sizeof(buf), line, "Vendor=", " ") < 0)
787 goto fail;
788 vendor_id = strtoul(buf, NULL, 16);
789 if (get_tag_value(buf, sizeof(buf), line, "ProdID=", " ") < 0)
790 goto fail;
791 product_id = strtoul(buf, NULL, 16);
792 } else if (line[0] == 'S' && line[1] == ':') {
793 if (get_tag_value(buf, sizeof(buf), line, "Product=", "") < 0)
794 goto fail;
795 pstrcpy(product_name, sizeof(product_name), buf);
796 } else if (line[0] == 'D' && line[1] == ':') {
797 if (get_tag_value(buf, sizeof(buf), line, "Cls=", " (") < 0)
798 goto fail;
799 class_id = strtoul(buf, NULL, 16);
801 fail: ;
803 if (device_count && (vendor_id || product_id)) {
804 /* Add the last device. */
805 ret = func(opaque, bus_num, addr, class_id, vendor_id,
806 product_id, product_name, speed);
808 the_end:
809 fclose(f);
810 return ret;
813 typedef struct FindDeviceState {
814 int vendor_id;
815 int product_id;
816 int bus_num;
817 int addr;
818 char product_name[PRODUCT_NAME_SZ];
819 } FindDeviceState;
821 static int usb_host_find_device_scan(void *opaque, int bus_num, int addr,
822 int class_id,
823 int vendor_id, int product_id,
824 const char *product_name, int speed)
826 FindDeviceState *s = opaque;
827 if ((vendor_id == s->vendor_id &&
828 product_id == s->product_id) ||
829 (bus_num == s->bus_num &&
830 addr == s->addr)) {
831 pstrcpy(s->product_name, PRODUCT_NAME_SZ, product_name);
832 s->bus_num = bus_num;
833 s->addr = addr;
834 return 1;
835 } else {
836 return 0;
840 /* the syntax is :
841 'bus.addr' (decimal numbers) or
842 'vendor_id:product_id' (hexa numbers) */
843 static int usb_host_find_device(int *pbus_num, int *paddr,
844 char *product_name, int product_name_size,
845 const char *devname)
847 const char *p;
848 int ret;
849 FindDeviceState fs;
851 p = strchr(devname, '.');
852 if (p) {
853 *pbus_num = strtoul(devname, NULL, 0);
854 *paddr = strtoul(p + 1, NULL, 0);
855 fs.bus_num = *pbus_num;
856 fs.addr = *paddr;
857 ret = usb_host_scan(&fs, usb_host_find_device_scan);
858 if (ret)
859 pstrcpy(product_name, product_name_size, fs.product_name);
860 return 0;
862 p = strchr(devname, ':');
863 if (p) {
864 fs.vendor_id = strtoul(devname, NULL, 16);
865 fs.product_id = strtoul(p + 1, NULL, 16);
866 ret = usb_host_scan(&fs, usb_host_find_device_scan);
867 if (ret) {
868 *pbus_num = fs.bus_num;
869 *paddr = fs.addr;
870 pstrcpy(product_name, product_name_size, fs.product_name);
871 return 0;
874 return -1;
877 /**********************/
878 /* USB host device info */
880 struct usb_class_info {
881 int class;
882 const char *class_name;
885 static const struct usb_class_info usb_class_info[] = {
886 { USB_CLASS_AUDIO, "Audio"},
887 { USB_CLASS_COMM, "Communication"},
888 { USB_CLASS_HID, "HID"},
889 { USB_CLASS_HUB, "Hub" },
890 { USB_CLASS_PHYSICAL, "Physical" },
891 { USB_CLASS_PRINTER, "Printer" },
892 { USB_CLASS_MASS_STORAGE, "Storage" },
893 { USB_CLASS_CDC_DATA, "Data" },
894 { USB_CLASS_APP_SPEC, "Application Specific" },
895 { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
896 { USB_CLASS_STILL_IMAGE, "Still Image" },
897 { USB_CLASS_CSCID, "Smart Card" },
898 { USB_CLASS_CONTENT_SEC, "Content Security" },
899 { -1, NULL }
902 static const char *usb_class_str(uint8_t class)
904 const struct usb_class_info *p;
905 for(p = usb_class_info; p->class != -1; p++) {
906 if (p->class == class)
907 break;
909 return p->class_name;
912 static void usb_info_device(int bus_num, int addr, int class_id,
913 int vendor_id, int product_id,
914 const char *product_name,
915 int speed)
917 const char *class_str, *speed_str;
919 switch(speed) {
920 case USB_SPEED_LOW:
921 speed_str = "1.5";
922 break;
923 case USB_SPEED_FULL:
924 speed_str = "12";
925 break;
926 case USB_SPEED_HIGH:
927 speed_str = "480";
928 break;
929 default:
930 speed_str = "?";
931 break;
934 term_printf(" Device %d.%d, speed %s Mb/s\n",
935 bus_num, addr, speed_str);
936 class_str = usb_class_str(class_id);
937 if (class_str)
938 term_printf(" %s:", class_str);
939 else
940 term_printf(" Class %02x:", class_id);
941 term_printf(" USB device %04x:%04x", vendor_id, product_id);
942 if (product_name[0] != '\0')
943 term_printf(", %s", product_name);
944 term_printf("\n");
947 static int usb_host_info_device(void *opaque, int bus_num, int addr,
948 int class_id,
949 int vendor_id, int product_id,
950 const char *product_name,
951 int speed)
953 usb_info_device(bus_num, addr, class_id, vendor_id, product_id,
954 product_name, speed);
955 return 0;
958 void usb_host_info(void)
960 usb_host_scan(NULL, usb_host_info_device);
963 #else
965 void usb_host_info(void)
967 term_printf("USB host devices not supported\n");
970 /* XXX: modify configure to compile the right host driver */
971 USBDevice *usb_host_device_open(const char *devname)
973 return NULL;
976 #endif