json-lexer: Initialize 'x' and 'y'
[qemu/mdroth.git] / usb-linux.c
blob88273ff479ad57ecdfadf8781b4829edfe8d77dc
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 "monitor.h"
37 #include <dirent.h>
38 #include <sys/ioctl.h>
39 #include <signal.h>
41 #include <linux/usbdevice_fs.h>
42 #include <linux/version.h>
43 #include "hw/usb.h"
45 /* We redefine it to avoid version problems */
46 struct usb_ctrltransfer {
47 uint8_t bRequestType;
48 uint8_t bRequest;
49 uint16_t wValue;
50 uint16_t wIndex;
51 uint16_t wLength;
52 uint32_t timeout;
53 void *data;
56 struct usb_ctrlrequest {
57 uint8_t bRequestType;
58 uint8_t bRequest;
59 uint16_t wValue;
60 uint16_t wIndex;
61 uint16_t wLength;
64 typedef int USBScanFunc(void *opaque, int bus_num, int addr, int class_id,
65 int vendor_id, int product_id,
66 const char *product_name, int speed);
68 //#define DEBUG
70 #ifdef DEBUG
71 #define DPRINTF printf
72 #else
73 #define DPRINTF(...)
74 #endif
76 #define USBDBG_DEVOPENED "husb: opened %s/devices\n"
78 #define USBPROCBUS_PATH "/proc/bus/usb"
79 #define PRODUCT_NAME_SZ 32
80 #define MAX_ENDPOINTS 16
81 #define USBDEVBUS_PATH "/dev/bus/usb"
82 #define USBSYSBUS_PATH "/sys/bus/usb"
84 static char *usb_host_device_path;
86 #define USB_FS_NONE 0
87 #define USB_FS_PROC 1
88 #define USB_FS_DEV 2
89 #define USB_FS_SYS 3
91 static int usb_fs_type;
93 /* endpoint association data */
94 struct endp_data {
95 uint8_t type;
96 uint8_t halted;
99 enum {
100 CTRL_STATE_IDLE = 0,
101 CTRL_STATE_SETUP,
102 CTRL_STATE_DATA,
103 CTRL_STATE_ACK
107 * Control transfer state.
108 * Note that 'buffer' _must_ follow 'req' field because
109 * we need contigious buffer when we submit control URB.
111 struct ctrl_struct {
112 uint16_t len;
113 uint16_t offset;
114 uint8_t state;
115 struct usb_ctrlrequest req;
116 uint8_t buffer[8192];
119 struct USBAutoFilter {
120 uint32_t bus_num;
121 uint32_t addr;
122 uint32_t vendor_id;
123 uint32_t product_id;
126 typedef struct USBHostDevice {
127 USBDevice dev;
128 int fd;
130 uint8_t descr[1024];
131 int descr_len;
132 int configuration;
133 int ninterfaces;
134 int closing;
136 struct ctrl_struct ctrl;
137 struct endp_data endp_table[MAX_ENDPOINTS];
139 /* Host side address */
140 int bus_num;
141 int addr;
142 struct USBAutoFilter match;
144 QTAILQ_ENTRY(USBHostDevice) next;
145 } USBHostDevice;
147 static QTAILQ_HEAD(, USBHostDevice) hostdevs = QTAILQ_HEAD_INITIALIZER(hostdevs);
149 static int usb_host_close(USBHostDevice *dev);
150 static int parse_filter(const char *spec, struct USBAutoFilter *f);
151 static void usb_host_auto_check(void *unused);
153 static int is_isoc(USBHostDevice *s, int ep)
155 return s->endp_table[ep - 1].type == USBDEVFS_URB_TYPE_ISO;
158 static int is_halted(USBHostDevice *s, int ep)
160 return s->endp_table[ep - 1].halted;
163 static void clear_halt(USBHostDevice *s, int ep)
165 s->endp_table[ep - 1].halted = 0;
168 static void set_halt(USBHostDevice *s, int ep)
170 s->endp_table[ep - 1].halted = 1;
174 * Async URB state.
175 * We always allocate one isoc descriptor even for bulk transfers
176 * to simplify allocation and casts.
178 typedef struct AsyncURB
180 struct usbdevfs_urb urb;
181 struct usbdevfs_iso_packet_desc isocpd;
183 USBPacket *packet;
184 USBHostDevice *hdev;
185 } AsyncURB;
187 static AsyncURB *async_alloc(void)
189 return (AsyncURB *) qemu_mallocz(sizeof(AsyncURB));
192 static void async_free(AsyncURB *aurb)
194 qemu_free(aurb);
197 static void async_complete_ctrl(USBHostDevice *s, USBPacket *p)
199 switch(s->ctrl.state) {
200 case CTRL_STATE_SETUP:
201 if (p->len < s->ctrl.len)
202 s->ctrl.len = p->len;
203 s->ctrl.state = CTRL_STATE_DATA;
204 p->len = 8;
205 break;
207 case CTRL_STATE_ACK:
208 s->ctrl.state = CTRL_STATE_IDLE;
209 p->len = 0;
210 break;
212 default:
213 break;
217 static void async_complete(void *opaque)
219 USBHostDevice *s = opaque;
220 AsyncURB *aurb;
222 while (1) {
223 USBPacket *p;
225 int r = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &aurb);
226 if (r < 0) {
227 if (errno == EAGAIN) {
228 return;
230 if (errno == ENODEV && !s->closing) {
231 printf("husb: device %d.%d disconnected\n",
232 s->bus_num, s->addr);
233 usb_host_close(s);
234 usb_host_auto_check(NULL);
235 return;
238 DPRINTF("husb: async. reap urb failed errno %d\n", errno);
239 return;
242 p = aurb->packet;
244 DPRINTF("husb: async completed. aurb %p status %d alen %d\n",
245 aurb, aurb->urb.status, aurb->urb.actual_length);
247 if (p) {
248 switch (aurb->urb.status) {
249 case 0:
250 p->len = aurb->urb.actual_length;
251 if (aurb->urb.type == USBDEVFS_URB_TYPE_CONTROL) {
252 async_complete_ctrl(s, p);
254 break;
256 case -EPIPE:
257 set_halt(s, p->devep);
258 p->len = USB_RET_STALL;
259 break;
261 default:
262 p->len = USB_RET_NAK;
263 break;
266 usb_packet_complete(p);
269 async_free(aurb);
273 static void async_cancel(USBPacket *unused, void *opaque)
275 AsyncURB *aurb = opaque;
276 USBHostDevice *s = aurb->hdev;
278 DPRINTF("husb: async cancel. aurb %p\n", aurb);
280 /* Mark it as dead (see async_complete above) */
281 aurb->packet = NULL;
283 int r = ioctl(s->fd, USBDEVFS_DISCARDURB, aurb);
284 if (r < 0) {
285 DPRINTF("husb: async. discard urb failed errno %d\n", errno);
289 static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration)
291 int dev_descr_len, config_descr_len;
292 int interface, nb_interfaces;
293 int ret, i;
295 if (configuration == 0) /* address state - ignore */
296 return 1;
298 DPRINTF("husb: claiming interfaces. config %d\n", configuration);
300 i = 0;
301 dev_descr_len = dev->descr[0];
302 if (dev_descr_len > dev->descr_len) {
303 goto fail;
306 i += dev_descr_len;
307 while (i < dev->descr_len) {
308 DPRINTF("husb: i is %d, descr_len is %d, dl %d, dt %d\n",
309 i, dev->descr_len,
310 dev->descr[i], dev->descr[i+1]);
312 if (dev->descr[i+1] != USB_DT_CONFIG) {
313 i += dev->descr[i];
314 continue;
316 config_descr_len = dev->descr[i];
318 printf("husb: config #%d need %d\n", dev->descr[i + 5], configuration);
320 if (configuration < 0 || configuration == dev->descr[i + 5]) {
321 configuration = dev->descr[i + 5];
322 break;
325 i += config_descr_len;
328 if (i >= dev->descr_len) {
329 fprintf(stderr,
330 "husb: update iface failed. no matching configuration\n");
331 goto fail;
333 nb_interfaces = dev->descr[i + 4];
335 #ifdef USBDEVFS_DISCONNECT
336 /* earlier Linux 2.4 do not support that */
338 struct usbdevfs_ioctl ctrl;
339 for (interface = 0; interface < nb_interfaces; interface++) {
340 ctrl.ioctl_code = USBDEVFS_DISCONNECT;
341 ctrl.ifno = interface;
342 ret = ioctl(dev->fd, USBDEVFS_IOCTL, &ctrl);
343 if (ret < 0 && errno != ENODATA) {
344 perror("USBDEVFS_DISCONNECT");
345 goto fail;
349 #endif
351 /* XXX: only grab if all interfaces are free */
352 for (interface = 0; interface < nb_interfaces; interface++) {
353 ret = ioctl(dev->fd, USBDEVFS_CLAIMINTERFACE, &interface);
354 if (ret < 0) {
355 if (errno == EBUSY) {
356 printf("husb: update iface. device already grabbed\n");
357 } else {
358 perror("husb: failed to claim interface");
360 fail:
361 return 0;
365 printf("husb: %d interfaces claimed for configuration %d\n",
366 nb_interfaces, configuration);
368 dev->ninterfaces = nb_interfaces;
369 dev->configuration = configuration;
370 return 1;
373 static int usb_host_release_interfaces(USBHostDevice *s)
375 int ret, i;
377 DPRINTF("husb: releasing interfaces\n");
379 for (i = 0; i < s->ninterfaces; i++) {
380 ret = ioctl(s->fd, USBDEVFS_RELEASEINTERFACE, &i);
381 if (ret < 0) {
382 perror("husb: failed to release interface");
383 return 0;
387 return 1;
390 static void usb_host_handle_reset(USBDevice *dev)
392 USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
394 DPRINTF("husb: reset device %u.%u\n", s->bus_num, s->addr);
396 ioctl(s->fd, USBDEVFS_RESET);
398 usb_host_claim_interfaces(s, s->configuration);
401 static void usb_host_handle_destroy(USBDevice *dev)
403 USBHostDevice *s = (USBHostDevice *)dev;
405 usb_host_close(s);
406 QTAILQ_REMOVE(&hostdevs, s, next);
409 static int usb_linux_update_endp_table(USBHostDevice *s);
411 static int usb_host_handle_data(USBHostDevice *s, USBPacket *p)
413 struct usbdevfs_urb *urb;
414 AsyncURB *aurb;
415 int ret;
417 aurb = async_alloc();
418 aurb->hdev = s;
419 aurb->packet = p;
421 urb = &aurb->urb;
423 if (p->pid == USB_TOKEN_IN) {
424 urb->endpoint = p->devep | 0x80;
425 } else {
426 urb->endpoint = p->devep;
429 if (is_halted(s, p->devep)) {
430 ret = ioctl(s->fd, USBDEVFS_CLEAR_HALT, &urb->endpoint);
431 if (ret < 0) {
432 DPRINTF("husb: failed to clear halt. ep 0x%x errno %d\n",
433 urb->endpoint, errno);
434 return USB_RET_NAK;
436 clear_halt(s, p->devep);
439 urb->buffer = p->data;
440 urb->buffer_length = p->len;
442 if (is_isoc(s, p->devep)) {
443 /* Setup ISOC transfer */
444 urb->type = USBDEVFS_URB_TYPE_ISO;
445 urb->flags = USBDEVFS_URB_ISO_ASAP;
446 urb->number_of_packets = 1;
447 urb->iso_frame_desc[0].length = p->len;
448 } else {
449 /* Setup bulk transfer */
450 urb->type = USBDEVFS_URB_TYPE_BULK;
453 urb->usercontext = s;
455 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
457 DPRINTF("husb: data submit. ep 0x%x len %u aurb %p\n",
458 urb->endpoint, p->len, aurb);
460 if (ret < 0) {
461 DPRINTF("husb: submit failed. errno %d\n", errno);
462 async_free(aurb);
464 switch(errno) {
465 case ETIMEDOUT:
466 return USB_RET_NAK;
467 case EPIPE:
468 default:
469 return USB_RET_STALL;
473 usb_defer_packet(p, async_cancel, aurb);
474 return USB_RET_ASYNC;
477 static int ctrl_error(void)
479 if (errno == ETIMEDOUT) {
480 return USB_RET_NAK;
481 } else {
482 return USB_RET_STALL;
486 static int usb_host_set_address(USBHostDevice *s, int addr)
488 DPRINTF("husb: ctrl set addr %u\n", addr);
489 s->dev.addr = addr;
490 return 0;
493 static int usb_host_set_config(USBHostDevice *s, int config)
495 usb_host_release_interfaces(s);
497 int ret = ioctl(s->fd, USBDEVFS_SETCONFIGURATION, &config);
499 DPRINTF("husb: ctrl set config %d ret %d errno %d\n", config, ret, errno);
501 if (ret < 0) {
502 return ctrl_error();
504 usb_host_claim_interfaces(s, config);
505 return 0;
508 static int usb_host_set_interface(USBHostDevice *s, int iface, int alt)
510 struct usbdevfs_setinterface si;
511 int ret;
513 si.interface = iface;
514 si.altsetting = alt;
515 ret = ioctl(s->fd, USBDEVFS_SETINTERFACE, &si);
517 DPRINTF("husb: ctrl set iface %d altset %d ret %d errno %d\n",
518 iface, alt, ret, errno);
520 if (ret < 0) {
521 return ctrl_error();
523 usb_linux_update_endp_table(s);
524 return 0;
527 static int usb_host_handle_control(USBHostDevice *s, USBPacket *p)
529 struct usbdevfs_urb *urb;
530 AsyncURB *aurb;
531 int ret, value, index;
532 int buffer_len;
535 * Process certain standard device requests.
536 * These are infrequent and are processed synchronously.
538 value = le16_to_cpu(s->ctrl.req.wValue);
539 index = le16_to_cpu(s->ctrl.req.wIndex);
541 DPRINTF("husb: ctrl type 0x%x req 0x%x val 0x%x index %u len %u\n",
542 s->ctrl.req.bRequestType, s->ctrl.req.bRequest, value, index,
543 s->ctrl.len);
545 if (s->ctrl.req.bRequestType == 0) {
546 switch (s->ctrl.req.bRequest) {
547 case USB_REQ_SET_ADDRESS:
548 return usb_host_set_address(s, value);
550 case USB_REQ_SET_CONFIGURATION:
551 return usb_host_set_config(s, value & 0xff);
555 if (s->ctrl.req.bRequestType == 1 &&
556 s->ctrl.req.bRequest == USB_REQ_SET_INTERFACE) {
557 return usb_host_set_interface(s, index, value);
560 /* The rest are asynchronous */
562 buffer_len = 8 + s->ctrl.len;
563 if (buffer_len > sizeof(s->ctrl.buffer)) {
564 fprintf(stderr, "husb: ctrl buffer too small (%u > %zu)\n",
565 buffer_len, sizeof(s->ctrl.buffer));
566 return USB_RET_STALL;
569 aurb = async_alloc();
570 aurb->hdev = s;
571 aurb->packet = p;
574 * Setup ctrl transfer.
576 * s->ctrl is layed out such that data buffer immediately follows
577 * 'req' struct which is exactly what usbdevfs expects.
579 urb = &aurb->urb;
581 urb->type = USBDEVFS_URB_TYPE_CONTROL;
582 urb->endpoint = p->devep;
584 urb->buffer = &s->ctrl.req;
585 urb->buffer_length = buffer_len;
587 urb->usercontext = s;
589 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
591 DPRINTF("husb: submit ctrl. len %u aurb %p\n", urb->buffer_length, aurb);
593 if (ret < 0) {
594 DPRINTF("husb: submit failed. errno %d\n", errno);
595 async_free(aurb);
597 switch(errno) {
598 case ETIMEDOUT:
599 return USB_RET_NAK;
600 case EPIPE:
601 default:
602 return USB_RET_STALL;
606 usb_defer_packet(p, async_cancel, aurb);
607 return USB_RET_ASYNC;
610 static int do_token_setup(USBDevice *dev, USBPacket *p)
612 USBHostDevice *s = (USBHostDevice *) dev;
613 int ret = 0;
615 if (p->len != 8) {
616 return USB_RET_STALL;
619 memcpy(&s->ctrl.req, p->data, 8);
620 s->ctrl.len = le16_to_cpu(s->ctrl.req.wLength);
621 s->ctrl.offset = 0;
622 s->ctrl.state = CTRL_STATE_SETUP;
624 if (s->ctrl.req.bRequestType & USB_DIR_IN) {
625 ret = usb_host_handle_control(s, p);
626 if (ret < 0) {
627 return ret;
630 if (ret < s->ctrl.len) {
631 s->ctrl.len = ret;
633 s->ctrl.state = CTRL_STATE_DATA;
634 } else {
635 if (s->ctrl.len == 0) {
636 s->ctrl.state = CTRL_STATE_ACK;
637 } else {
638 s->ctrl.state = CTRL_STATE_DATA;
642 return ret;
645 static int do_token_in(USBDevice *dev, USBPacket *p)
647 USBHostDevice *s = (USBHostDevice *) dev;
648 int ret = 0;
650 if (p->devep != 0) {
651 return usb_host_handle_data(s, p);
654 switch(s->ctrl.state) {
655 case CTRL_STATE_ACK:
656 if (!(s->ctrl.req.bRequestType & USB_DIR_IN)) {
657 ret = usb_host_handle_control(s, p);
658 if (ret == USB_RET_ASYNC) {
659 return USB_RET_ASYNC;
661 s->ctrl.state = CTRL_STATE_IDLE;
662 return ret > 0 ? 0 : ret;
665 return 0;
667 case CTRL_STATE_DATA:
668 if (s->ctrl.req.bRequestType & USB_DIR_IN) {
669 int len = s->ctrl.len - s->ctrl.offset;
670 if (len > p->len) {
671 len = p->len;
673 memcpy(p->data, s->ctrl.buffer + s->ctrl.offset, len);
674 s->ctrl.offset += len;
675 if (s->ctrl.offset >= s->ctrl.len) {
676 s->ctrl.state = CTRL_STATE_ACK;
678 return len;
681 s->ctrl.state = CTRL_STATE_IDLE;
682 return USB_RET_STALL;
684 default:
685 return USB_RET_STALL;
689 static int do_token_out(USBDevice *dev, USBPacket *p)
691 USBHostDevice *s = (USBHostDevice *) dev;
693 if (p->devep != 0) {
694 return usb_host_handle_data(s, p);
697 switch(s->ctrl.state) {
698 case CTRL_STATE_ACK:
699 if (s->ctrl.req.bRequestType & USB_DIR_IN) {
700 s->ctrl.state = CTRL_STATE_IDLE;
701 /* transfer OK */
702 } else {
703 /* ignore additional output */
705 return 0;
707 case CTRL_STATE_DATA:
708 if (!(s->ctrl.req.bRequestType & USB_DIR_IN)) {
709 int len = s->ctrl.len - s->ctrl.offset;
710 if (len > p->len) {
711 len = p->len;
713 memcpy(s->ctrl.buffer + s->ctrl.offset, p->data, len);
714 s->ctrl.offset += len;
715 if (s->ctrl.offset >= s->ctrl.len) {
716 s->ctrl.state = CTRL_STATE_ACK;
718 return len;
721 s->ctrl.state = CTRL_STATE_IDLE;
722 return USB_RET_STALL;
724 default:
725 return USB_RET_STALL;
730 * Packet handler.
731 * Called by the HC (host controller).
733 * Returns length of the transaction or one of the USB_RET_XXX codes.
735 static int usb_host_handle_packet(USBDevice *s, USBPacket *p)
737 switch(p->pid) {
738 case USB_MSG_ATTACH:
739 s->state = USB_STATE_ATTACHED;
740 return 0;
742 case USB_MSG_DETACH:
743 s->state = USB_STATE_NOTATTACHED;
744 return 0;
746 case USB_MSG_RESET:
747 s->remote_wakeup = 0;
748 s->addr = 0;
749 s->state = USB_STATE_DEFAULT;
750 s->info->handle_reset(s);
751 return 0;
754 /* Rest of the PIDs must match our address */
755 if (s->state < USB_STATE_DEFAULT || p->devaddr != s->addr) {
756 return USB_RET_NODEV;
759 switch (p->pid) {
760 case USB_TOKEN_SETUP:
761 return do_token_setup(s, p);
763 case USB_TOKEN_IN:
764 return do_token_in(s, p);
766 case USB_TOKEN_OUT:
767 return do_token_out(s, p);
769 default:
770 return USB_RET_STALL;
774 /* returns 1 on problem encountered or 0 for success */
775 static int usb_linux_update_endp_table(USBHostDevice *s)
777 uint8_t *descriptors;
778 uint8_t devep, type, configuration, alt_interface;
779 struct usb_ctrltransfer ct;
780 int interface, ret, length, i;
782 ct.bRequestType = USB_DIR_IN;
783 ct.bRequest = USB_REQ_GET_CONFIGURATION;
784 ct.wValue = 0;
785 ct.wIndex = 0;
786 ct.wLength = 1;
787 ct.data = &configuration;
788 ct.timeout = 50;
790 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
791 if (ret < 0) {
792 perror("usb_linux_update_endp_table");
793 return 1;
796 /* in address state */
797 if (configuration == 0) {
798 return 1;
801 /* get the desired configuration, interface, and endpoint descriptors
802 * from device description */
803 descriptors = &s->descr[18];
804 length = s->descr_len - 18;
805 i = 0;
807 if (descriptors[i + 1] != USB_DT_CONFIG ||
808 descriptors[i + 5] != configuration) {
809 DPRINTF("invalid descriptor data - configuration\n");
810 return 1;
812 i += descriptors[i];
814 while (i < length) {
815 if (descriptors[i + 1] != USB_DT_INTERFACE ||
816 (descriptors[i + 1] == USB_DT_INTERFACE &&
817 descriptors[i + 4] == 0)) {
818 i += descriptors[i];
819 continue;
822 interface = descriptors[i + 2];
824 ct.bRequestType = USB_DIR_IN | USB_RECIP_INTERFACE;
825 ct.bRequest = USB_REQ_GET_INTERFACE;
826 ct.wValue = 0;
827 ct.wIndex = interface;
828 ct.wLength = 1;
829 ct.data = &alt_interface;
830 ct.timeout = 50;
832 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
833 if (ret < 0) {
834 alt_interface = interface;
837 /* the current interface descriptor is the active interface
838 * and has endpoints */
839 if (descriptors[i + 3] != alt_interface) {
840 i += descriptors[i];
841 continue;
844 /* advance to the endpoints */
845 while (i < length && descriptors[i +1] != USB_DT_ENDPOINT) {
846 i += descriptors[i];
849 if (i >= length)
850 break;
852 while (i < length) {
853 if (descriptors[i + 1] != USB_DT_ENDPOINT) {
854 break;
857 devep = descriptors[i + 2];
858 switch (descriptors[i + 3] & 0x3) {
859 case 0x00:
860 type = USBDEVFS_URB_TYPE_CONTROL;
861 break;
862 case 0x01:
863 type = USBDEVFS_URB_TYPE_ISO;
864 break;
865 case 0x02:
866 type = USBDEVFS_URB_TYPE_BULK;
867 break;
868 case 0x03:
869 type = USBDEVFS_URB_TYPE_INTERRUPT;
870 break;
871 default:
872 DPRINTF("usb_host: malformed endpoint type\n");
873 type = USBDEVFS_URB_TYPE_BULK;
875 s->endp_table[(devep & 0xf) - 1].type = type;
876 s->endp_table[(devep & 0xf) - 1].halted = 0;
878 i += descriptors[i];
881 return 0;
884 static int usb_host_open(USBHostDevice *dev, int bus_num,
885 int addr, const char *prod_name)
887 int fd = -1, ret;
888 struct usbdevfs_connectinfo ci;
889 char buf[1024];
891 if (dev->fd != -1) {
892 goto fail;
894 printf("husb: open device %d.%d\n", bus_num, addr);
896 if (!usb_host_device_path) {
897 perror("husb: USB Host Device Path not set");
898 goto fail;
900 snprintf(buf, sizeof(buf), "%s/%03d/%03d", usb_host_device_path,
901 bus_num, addr);
902 fd = open(buf, O_RDWR | O_NONBLOCK);
903 if (fd < 0) {
904 perror(buf);
905 goto fail;
907 DPRINTF("husb: opened %s\n", buf);
909 dev->bus_num = bus_num;
910 dev->addr = addr;
911 dev->fd = fd;
913 /* read the device description */
914 dev->descr_len = read(fd, dev->descr, sizeof(dev->descr));
915 if (dev->descr_len <= 0) {
916 perror("husb: reading device data failed");
917 goto fail;
920 #ifdef DEBUG
922 int x;
923 printf("=== begin dumping device descriptor data ===\n");
924 for (x = 0; x < dev->descr_len; x++) {
925 printf("%02x ", dev->descr[x]);
927 printf("\n=== end dumping device descriptor data ===\n");
929 #endif
933 * Initial configuration is -1 which makes us claim first
934 * available config. We used to start with 1, which does not
935 * always work. I've seen devices where first config starts
936 * with 2.
938 if (!usb_host_claim_interfaces(dev, -1)) {
939 goto fail;
942 ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci);
943 if (ret < 0) {
944 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
945 goto fail;
948 printf("husb: grabbed usb device %d.%d\n", bus_num, addr);
950 ret = usb_linux_update_endp_table(dev);
951 if (ret) {
952 goto fail;
955 if (ci.slow) {
956 dev->dev.speed = USB_SPEED_LOW;
957 } else {
958 dev->dev.speed = USB_SPEED_HIGH;
961 if (!prod_name || prod_name[0] == '\0') {
962 snprintf(dev->dev.product_desc, sizeof(dev->dev.product_desc),
963 "host:%d.%d", bus_num, addr);
964 } else {
965 pstrcpy(dev->dev.product_desc, sizeof(dev->dev.product_desc),
966 prod_name);
969 /* USB devio uses 'write' flag to check for async completions */
970 qemu_set_fd_handler(dev->fd, NULL, async_complete, dev);
972 usb_device_attach(&dev->dev);
973 return 0;
975 fail:
976 dev->fd = -1;
977 if (fd != -1) {
978 close(fd);
980 return -1;
983 static int usb_host_close(USBHostDevice *dev)
985 if (dev->fd == -1) {
986 return -1;
989 qemu_set_fd_handler(dev->fd, NULL, NULL, NULL);
990 dev->closing = 1;
991 async_complete(dev);
992 dev->closing = 0;
993 usb_device_detach(&dev->dev);
994 close(dev->fd);
995 dev->fd = -1;
996 return 0;
999 static int usb_host_initfn(USBDevice *dev)
1001 USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
1003 dev->auto_attach = 0;
1004 s->fd = -1;
1005 QTAILQ_INSERT_TAIL(&hostdevs, s, next);
1006 usb_host_auto_check(NULL);
1007 return 0;
1010 static struct USBDeviceInfo usb_host_dev_info = {
1011 .product_desc = "USB Host Device",
1012 .qdev.name = "usb-host",
1013 .qdev.size = sizeof(USBHostDevice),
1014 .init = usb_host_initfn,
1015 .handle_packet = usb_host_handle_packet,
1016 .handle_reset = usb_host_handle_reset,
1017 .handle_destroy = usb_host_handle_destroy,
1018 .usbdevice_name = "host",
1019 .usbdevice_init = usb_host_device_open,
1020 .qdev.props = (Property[]) {
1021 DEFINE_PROP_UINT32("hostbus", USBHostDevice, match.bus_num, 0),
1022 DEFINE_PROP_UINT32("hostaddr", USBHostDevice, match.addr, 0),
1023 DEFINE_PROP_HEX32("vendorid", USBHostDevice, match.vendor_id, 0),
1024 DEFINE_PROP_HEX32("productid", USBHostDevice, match.product_id, 0),
1025 DEFINE_PROP_END_OF_LIST(),
1029 static void usb_host_register_devices(void)
1031 usb_qdev_register(&usb_host_dev_info);
1033 device_init(usb_host_register_devices)
1035 USBDevice *usb_host_device_open(const char *devname)
1037 struct USBAutoFilter filter;
1038 USBDevice *dev;
1039 char *p;
1041 dev = usb_create(NULL /* FIXME */, "usb-host");
1043 if (strstr(devname, "auto:")) {
1044 if (parse_filter(devname, &filter) < 0) {
1045 goto fail;
1047 } else {
1048 if ((p = strchr(devname, '.'))) {
1049 filter.bus_num = strtoul(devname, NULL, 0);
1050 filter.addr = strtoul(p + 1, NULL, 0);
1051 filter.vendor_id = 0;
1052 filter.product_id = 0;
1053 } else if ((p = strchr(devname, ':'))) {
1054 filter.bus_num = 0;
1055 filter.addr = 0;
1056 filter.vendor_id = strtoul(devname, NULL, 16);
1057 filter.product_id = strtoul(p + 1, NULL, 16);
1058 } else {
1059 goto fail;
1063 qdev_prop_set_uint32(&dev->qdev, "hostbus", filter.bus_num);
1064 qdev_prop_set_uint32(&dev->qdev, "hostaddr", filter.addr);
1065 qdev_prop_set_uint32(&dev->qdev, "vendorid", filter.vendor_id);
1066 qdev_prop_set_uint32(&dev->qdev, "productid", filter.product_id);
1067 qdev_init_nofail(&dev->qdev);
1068 return dev;
1070 fail:
1071 qdev_free(&dev->qdev);
1072 return NULL;
1075 int usb_host_device_close(const char *devname)
1077 #if 0
1078 char product_name[PRODUCT_NAME_SZ];
1079 int bus_num, addr;
1080 USBHostDevice *s;
1082 if (strstr(devname, "auto:")) {
1083 return usb_host_auto_del(devname);
1085 if (usb_host_find_device(&bus_num, &addr, product_name,
1086 sizeof(product_name), devname) < 0) {
1087 return -1;
1089 s = hostdev_find(bus_num, addr);
1090 if (s) {
1091 usb_device_delete_addr(s->bus_num, s->dev.addr);
1092 return 0;
1094 #endif
1096 return -1;
1099 static int get_tag_value(char *buf, int buf_size,
1100 const char *str, const char *tag,
1101 const char *stopchars)
1103 const char *p;
1104 char *q;
1105 p = strstr(str, tag);
1106 if (!p) {
1107 return -1;
1109 p += strlen(tag);
1110 while (qemu_isspace(*p)) {
1111 p++;
1113 q = buf;
1114 while (*p != '\0' && !strchr(stopchars, *p)) {
1115 if ((q - buf) < (buf_size - 1)) {
1116 *q++ = *p;
1118 p++;
1120 *q = '\0';
1121 return q - buf;
1125 * Use /proc/bus/usb/devices or /dev/bus/usb/devices file to determine
1126 * host's USB devices. This is legacy support since many distributions
1127 * are moving to /sys/bus/usb
1129 static int usb_host_scan_dev(void *opaque, USBScanFunc *func)
1131 FILE *f = NULL;
1132 char line[1024];
1133 char buf[1024];
1134 int bus_num, addr, speed, device_count, class_id, product_id, vendor_id;
1135 char product_name[512];
1136 int ret = 0;
1138 if (!usb_host_device_path) {
1139 perror("husb: USB Host Device Path not set");
1140 goto the_end;
1142 snprintf(line, sizeof(line), "%s/devices", usb_host_device_path);
1143 f = fopen(line, "r");
1144 if (!f) {
1145 perror("husb: cannot open devices file");
1146 goto the_end;
1149 device_count = 0;
1150 bus_num = addr = speed = class_id = product_id = vendor_id = 0;
1151 for(;;) {
1152 if (fgets(line, sizeof(line), f) == NULL) {
1153 break;
1155 if (strlen(line) > 0) {
1156 line[strlen(line) - 1] = '\0';
1158 if (line[0] == 'T' && line[1] == ':') {
1159 if (device_count && (vendor_id || product_id)) {
1160 /* New device. Add the previously discovered device. */
1161 ret = func(opaque, bus_num, addr, class_id, vendor_id,
1162 product_id, product_name, speed);
1163 if (ret) {
1164 goto the_end;
1167 if (get_tag_value(buf, sizeof(buf), line, "Bus=", " ") < 0) {
1168 goto fail;
1170 bus_num = atoi(buf);
1171 if (get_tag_value(buf, sizeof(buf), line, "Dev#=", " ") < 0) {
1172 goto fail;
1174 addr = atoi(buf);
1175 if (get_tag_value(buf, sizeof(buf), line, "Spd=", " ") < 0) {
1176 goto fail;
1178 if (!strcmp(buf, "480")) {
1179 speed = USB_SPEED_HIGH;
1180 } else if (!strcmp(buf, "1.5")) {
1181 speed = USB_SPEED_LOW;
1182 } else {
1183 speed = USB_SPEED_FULL;
1185 product_name[0] = '\0';
1186 class_id = 0xff;
1187 device_count++;
1188 product_id = 0;
1189 vendor_id = 0;
1190 } else if (line[0] == 'P' && line[1] == ':') {
1191 if (get_tag_value(buf, sizeof(buf), line, "Vendor=", " ") < 0) {
1192 goto fail;
1194 vendor_id = strtoul(buf, NULL, 16);
1195 if (get_tag_value(buf, sizeof(buf), line, "ProdID=", " ") < 0) {
1196 goto fail;
1198 product_id = strtoul(buf, NULL, 16);
1199 } else if (line[0] == 'S' && line[1] == ':') {
1200 if (get_tag_value(buf, sizeof(buf), line, "Product=", "") < 0) {
1201 goto fail;
1203 pstrcpy(product_name, sizeof(product_name), buf);
1204 } else if (line[0] == 'D' && line[1] == ':') {
1205 if (get_tag_value(buf, sizeof(buf), line, "Cls=", " (") < 0) {
1206 goto fail;
1208 class_id = strtoul(buf, NULL, 16);
1210 fail: ;
1212 if (device_count && (vendor_id || product_id)) {
1213 /* Add the last device. */
1214 ret = func(opaque, bus_num, addr, class_id, vendor_id,
1215 product_id, product_name, speed);
1217 the_end:
1218 if (f) {
1219 fclose(f);
1221 return ret;
1225 * Read sys file-system device file
1227 * @line address of buffer to put file contents in
1228 * @line_size size of line
1229 * @device_file path to device file (printf format string)
1230 * @device_name device being opened (inserted into device_file)
1232 * @return 0 failed, 1 succeeded ('line' contains data)
1234 static int usb_host_read_file(char *line, size_t line_size,
1235 const char *device_file, const char *device_name)
1237 FILE *f;
1238 int ret = 0;
1239 char filename[PATH_MAX];
1241 snprintf(filename, PATH_MAX, USBSYSBUS_PATH "/devices/%s/%s", device_name,
1242 device_file);
1243 f = fopen(filename, "r");
1244 if (f) {
1245 ret = fgets(line, line_size, f) != NULL;
1246 fclose(f);
1249 return ret;
1253 * Use /sys/bus/usb/devices/ directory to determine host's USB
1254 * devices.
1256 * This code is based on Robert Schiele's original patches posted to
1257 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1259 static int usb_host_scan_sys(void *opaque, USBScanFunc *func)
1261 DIR *dir = NULL;
1262 char line[1024];
1263 int bus_num, addr, speed, class_id, product_id, vendor_id;
1264 int ret = 0;
1265 char product_name[512];
1266 struct dirent *de;
1268 dir = opendir(USBSYSBUS_PATH "/devices");
1269 if (!dir) {
1270 perror("husb: cannot open devices directory");
1271 goto the_end;
1274 while ((de = readdir(dir))) {
1275 if (de->d_name[0] != '.' && !strchr(de->d_name, ':')) {
1276 char *tmpstr = de->d_name;
1277 if (!strncmp(de->d_name, "usb", 3)) {
1278 tmpstr += 3;
1280 bus_num = atoi(tmpstr);
1282 if (!usb_host_read_file(line, sizeof(line), "devnum", de->d_name)) {
1283 goto the_end;
1285 if (sscanf(line, "%d", &addr) != 1) {
1286 goto the_end;
1288 if (!usb_host_read_file(line, sizeof(line), "bDeviceClass",
1289 de->d_name)) {
1290 goto the_end;
1292 if (sscanf(line, "%x", &class_id) != 1) {
1293 goto the_end;
1296 if (!usb_host_read_file(line, sizeof(line), "idVendor",
1297 de->d_name)) {
1298 goto the_end;
1300 if (sscanf(line, "%x", &vendor_id) != 1) {
1301 goto the_end;
1303 if (!usb_host_read_file(line, sizeof(line), "idProduct",
1304 de->d_name)) {
1305 goto the_end;
1307 if (sscanf(line, "%x", &product_id) != 1) {
1308 goto the_end;
1310 if (!usb_host_read_file(line, sizeof(line), "product",
1311 de->d_name)) {
1312 *product_name = 0;
1313 } else {
1314 if (strlen(line) > 0) {
1315 line[strlen(line) - 1] = '\0';
1317 pstrcpy(product_name, sizeof(product_name), line);
1320 if (!usb_host_read_file(line, sizeof(line), "speed", de->d_name)) {
1321 goto the_end;
1323 if (!strcmp(line, "480\n")) {
1324 speed = USB_SPEED_HIGH;
1325 } else if (!strcmp(line, "1.5\n")) {
1326 speed = USB_SPEED_LOW;
1327 } else {
1328 speed = USB_SPEED_FULL;
1331 ret = func(opaque, bus_num, addr, class_id, vendor_id,
1332 product_id, product_name, speed);
1333 if (ret) {
1334 goto the_end;
1338 the_end:
1339 if (dir) {
1340 closedir(dir);
1342 return ret;
1346 * Determine how to access the host's USB devices and call the
1347 * specific support function.
1349 static int usb_host_scan(void *opaque, USBScanFunc *func)
1351 Monitor *mon = cur_mon;
1352 FILE *f = NULL;
1353 DIR *dir = NULL;
1354 int ret = 0;
1355 const char *fs_type[] = {"unknown", "proc", "dev", "sys"};
1356 char devpath[PATH_MAX];
1358 /* only check the host once */
1359 if (!usb_fs_type) {
1360 dir = opendir(USBSYSBUS_PATH "/devices");
1361 if (dir) {
1362 /* devices found in /dev/bus/usb/ (yes - not a mistake!) */
1363 strcpy(devpath, USBDEVBUS_PATH);
1364 usb_fs_type = USB_FS_SYS;
1365 closedir(dir);
1366 DPRINTF(USBDBG_DEVOPENED, USBSYSBUS_PATH);
1367 goto found_devices;
1369 f = fopen(USBPROCBUS_PATH "/devices", "r");
1370 if (f) {
1371 /* devices found in /proc/bus/usb/ */
1372 strcpy(devpath, USBPROCBUS_PATH);
1373 usb_fs_type = USB_FS_PROC;
1374 fclose(f);
1375 DPRINTF(USBDBG_DEVOPENED, USBPROCBUS_PATH);
1376 goto found_devices;
1378 /* try additional methods if an access method hasn't been found yet */
1379 f = fopen(USBDEVBUS_PATH "/devices", "r");
1380 if (f) {
1381 /* devices found in /dev/bus/usb/ */
1382 strcpy(devpath, USBDEVBUS_PATH);
1383 usb_fs_type = USB_FS_DEV;
1384 fclose(f);
1385 DPRINTF(USBDBG_DEVOPENED, USBDEVBUS_PATH);
1386 goto found_devices;
1388 found_devices:
1389 if (!usb_fs_type) {
1390 if (mon) {
1391 monitor_printf(mon, "husb: unable to access USB devices\n");
1393 return -ENOENT;
1396 /* the module setting (used later for opening devices) */
1397 usb_host_device_path = qemu_mallocz(strlen(devpath)+1);
1398 strcpy(usb_host_device_path, devpath);
1399 if (mon) {
1400 monitor_printf(mon, "husb: using %s file-system with %s\n",
1401 fs_type[usb_fs_type], usb_host_device_path);
1405 switch (usb_fs_type) {
1406 case USB_FS_PROC:
1407 case USB_FS_DEV:
1408 ret = usb_host_scan_dev(opaque, func);
1409 break;
1410 case USB_FS_SYS:
1411 ret = usb_host_scan_sys(opaque, func);
1412 break;
1413 default:
1414 ret = -EINVAL;
1415 break;
1417 return ret;
1420 static QEMUTimer *usb_auto_timer;
1422 static int usb_host_auto_scan(void *opaque, int bus_num, int addr,
1423 int class_id, int vendor_id, int product_id,
1424 const char *product_name, int speed)
1426 struct USBAutoFilter *f;
1427 struct USBHostDevice *s;
1429 /* Ignore hubs */
1430 if (class_id == 9)
1431 return 0;
1433 QTAILQ_FOREACH(s, &hostdevs, next) {
1434 f = &s->match;
1436 if (f->bus_num > 0 && f->bus_num != bus_num) {
1437 continue;
1439 if (f->addr > 0 && f->addr != addr) {
1440 continue;
1443 if (f->vendor_id > 0 && f->vendor_id != vendor_id) {
1444 continue;
1447 if (f->product_id > 0 && f->product_id != product_id) {
1448 continue;
1450 /* We got a match */
1452 /* Already attached ? */
1453 if (s->fd != -1) {
1454 return 0;
1456 DPRINTF("husb: auto open: bus_num %d addr %d\n", bus_num, addr);
1458 usb_host_open(s, bus_num, addr, product_name);
1461 return 0;
1464 static void usb_host_auto_check(void *unused)
1466 struct USBHostDevice *s;
1467 int unconnected = 0;
1469 usb_host_scan(NULL, usb_host_auto_scan);
1471 QTAILQ_FOREACH(s, &hostdevs, next) {
1472 if (s->fd == -1) {
1473 unconnected++;
1477 if (unconnected == 0) {
1478 /* nothing to watch */
1479 if (usb_auto_timer) {
1480 qemu_del_timer(usb_auto_timer);
1482 return;
1485 if (!usb_auto_timer) {
1486 usb_auto_timer = qemu_new_timer(rt_clock, usb_host_auto_check, NULL);
1487 if (!usb_auto_timer) {
1488 return;
1491 qemu_mod_timer(usb_auto_timer, qemu_get_clock(rt_clock) + 2000);
1495 * Autoconnect filter
1496 * Format:
1497 * auto:bus:dev[:vid:pid]
1498 * auto:bus.dev[:vid:pid]
1500 * bus - bus number (dec, * means any)
1501 * dev - device number (dec, * means any)
1502 * vid - vendor id (hex, * means any)
1503 * pid - product id (hex, * means any)
1505 * See 'lsusb' output.
1507 static int parse_filter(const char *spec, struct USBAutoFilter *f)
1509 enum { BUS, DEV, VID, PID, DONE };
1510 const char *p = spec;
1511 int i;
1513 f->bus_num = 0;
1514 f->addr = 0;
1515 f->vendor_id = 0;
1516 f->product_id = 0;
1518 for (i = BUS; i < DONE; i++) {
1519 p = strpbrk(p, ":.");
1520 if (!p) {
1521 break;
1523 p++;
1525 if (*p == '*') {
1526 continue;
1528 switch(i) {
1529 case BUS: f->bus_num = strtol(p, NULL, 10); break;
1530 case DEV: f->addr = strtol(p, NULL, 10); break;
1531 case VID: f->vendor_id = strtol(p, NULL, 16); break;
1532 case PID: f->product_id = strtol(p, NULL, 16); break;
1536 if (i < DEV) {
1537 fprintf(stderr, "husb: invalid auto filter spec %s\n", spec);
1538 return -1;
1541 return 0;
1544 /**********************/
1545 /* USB host device info */
1547 struct usb_class_info {
1548 int class;
1549 const char *class_name;
1552 static const struct usb_class_info usb_class_info[] = {
1553 { USB_CLASS_AUDIO, "Audio"},
1554 { USB_CLASS_COMM, "Communication"},
1555 { USB_CLASS_HID, "HID"},
1556 { USB_CLASS_HUB, "Hub" },
1557 { USB_CLASS_PHYSICAL, "Physical" },
1558 { USB_CLASS_PRINTER, "Printer" },
1559 { USB_CLASS_MASS_STORAGE, "Storage" },
1560 { USB_CLASS_CDC_DATA, "Data" },
1561 { USB_CLASS_APP_SPEC, "Application Specific" },
1562 { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
1563 { USB_CLASS_STILL_IMAGE, "Still Image" },
1564 { USB_CLASS_CSCID, "Smart Card" },
1565 { USB_CLASS_CONTENT_SEC, "Content Security" },
1566 { -1, NULL }
1569 static const char *usb_class_str(uint8_t class)
1571 const struct usb_class_info *p;
1572 for(p = usb_class_info; p->class != -1; p++) {
1573 if (p->class == class) {
1574 break;
1577 return p->class_name;
1580 static void usb_info_device(Monitor *mon, int bus_num, int addr, int class_id,
1581 int vendor_id, int product_id,
1582 const char *product_name,
1583 int speed)
1585 const char *class_str, *speed_str;
1587 switch(speed) {
1588 case USB_SPEED_LOW:
1589 speed_str = "1.5";
1590 break;
1591 case USB_SPEED_FULL:
1592 speed_str = "12";
1593 break;
1594 case USB_SPEED_HIGH:
1595 speed_str = "480";
1596 break;
1597 default:
1598 speed_str = "?";
1599 break;
1602 monitor_printf(mon, " Device %d.%d, speed %s Mb/s\n",
1603 bus_num, addr, speed_str);
1604 class_str = usb_class_str(class_id);
1605 if (class_str) {
1606 monitor_printf(mon, " %s:", class_str);
1607 } else {
1608 monitor_printf(mon, " Class %02x:", class_id);
1610 monitor_printf(mon, " USB device %04x:%04x", vendor_id, product_id);
1611 if (product_name[0] != '\0') {
1612 monitor_printf(mon, ", %s", product_name);
1614 monitor_printf(mon, "\n");
1617 static int usb_host_info_device(void *opaque, int bus_num, int addr,
1618 int class_id,
1619 int vendor_id, int product_id,
1620 const char *product_name,
1621 int speed)
1623 Monitor *mon = opaque;
1625 usb_info_device(mon, bus_num, addr, class_id, vendor_id, product_id,
1626 product_name, speed);
1627 return 0;
1630 static void dec2str(int val, char *str, size_t size)
1632 if (val == 0) {
1633 snprintf(str, size, "*");
1634 } else {
1635 snprintf(str, size, "%d", val);
1639 static void hex2str(int val, char *str, size_t size)
1641 if (val == 0) {
1642 snprintf(str, size, "*");
1643 } else {
1644 snprintf(str, size, "%04x", val);
1648 void usb_host_info(Monitor *mon)
1650 struct USBAutoFilter *f;
1651 struct USBHostDevice *s;
1653 usb_host_scan(mon, usb_host_info_device);
1655 if (QTAILQ_EMPTY(&hostdevs)) {
1656 return;
1659 monitor_printf(mon, " Auto filters:\n");
1660 QTAILQ_FOREACH(s, &hostdevs, next) {
1661 char bus[10], addr[10], vid[10], pid[10];
1662 f = &s->match;
1663 dec2str(f->bus_num, bus, sizeof(bus));
1664 dec2str(f->addr, addr, sizeof(addr));
1665 hex2str(f->vendor_id, vid, sizeof(vid));
1666 hex2str(f->product_id, pid, sizeof(pid));
1667 monitor_printf(mon, " Device %s.%s ID %s:%s\n",
1668 bus, addr, vid, pid);