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
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
33 #include "qemu-common.h"
34 #include "qemu-timer.h"
40 #include <sys/ioctl.h>
42 #include <linux/usbdevice_fs.h>
43 #include <linux/version.h>
46 /* We redefine it to avoid version problems */
47 struct usb_ctrltransfer {
57 typedef int USBScanFunc(void *opaque, int bus_num, int addr, char *port,
58 int class_id, int vendor_id, int product_id,
59 const char *product_name, int speed);
64 #define DPRINTF printf
69 #define USBDBG_DEVOPENED "husb: opened %s/devices\n"
71 #define USBPROCBUS_PATH "/proc/bus/usb"
72 #define PRODUCT_NAME_SZ 32
73 #define MAX_ENDPOINTS 15
74 #define MAX_PORTLEN 16
75 #define USBDEVBUS_PATH "/dev/bus/usb"
76 #define USBSYSBUS_PATH "/sys/bus/usb"
78 static char *usb_host_device_path;
85 static int usb_fs_type;
87 /* endpoint association data */
88 #define ISO_FRAME_DESC_PER_URB 32
89 #define INVALID_EP_TYPE 255
91 /* devio.c limits single requests to 16k */
92 #define MAX_USBFS_BUFFER_SIZE 16384
94 typedef struct AsyncURB AsyncURB;
107 struct USBAutoFilter {
115 typedef struct USBHostDevice {
124 uint32_t iso_urb_count;
127 struct endp_data endp_table[MAX_ENDPOINTS];
128 QLIST_HEAD(, AsyncURB) aurbs;
130 /* Host side address */
133 char port[MAX_PORTLEN];
134 struct USBAutoFilter match;
136 QTAILQ_ENTRY(USBHostDevice) next;
139 static QTAILQ_HEAD(, USBHostDevice) hostdevs = QTAILQ_HEAD_INITIALIZER(hostdevs);
141 static int usb_host_close(USBHostDevice *dev);
142 static int parse_filter(const char *spec, struct USBAutoFilter *f);
143 static void usb_host_auto_check(void *unused);
144 static int usb_host_read_file(char *line, size_t line_size,
145 const char *device_file, const char *device_name);
146 static int usb_linux_update_endp_table(USBHostDevice *s);
148 static struct endp_data *get_endp(USBHostDevice *s, int ep)
150 return s->endp_table + ep - 1;
153 static int is_isoc(USBHostDevice *s, int ep)
155 return get_endp(s, ep)->type == USBDEVFS_URB_TYPE_ISO;
158 static int is_valid(USBHostDevice *s, int ep)
160 return get_endp(s, ep)->type != INVALID_EP_TYPE;
163 static int is_halted(USBHostDevice *s, int ep)
165 return get_endp(s, ep)->halted;
168 static void clear_halt(USBHostDevice *s, int ep)
170 trace_usb_host_ep_clear_halt(s->bus_num, s->addr, ep);
171 get_endp(s, ep)->halted = 0;
174 static void set_halt(USBHostDevice *s, int ep)
176 trace_usb_host_ep_set_halt(s->bus_num, s->addr, ep);
177 get_endp(s, ep)->halted = 1;
180 static int is_iso_started(USBHostDevice *s, int ep)
182 return get_endp(s, ep)->iso_started;
185 static void clear_iso_started(USBHostDevice *s, int ep)
187 trace_usb_host_ep_stop_iso(s->bus_num, s->addr, ep);
188 get_endp(s, ep)->iso_started = 0;
191 static void set_iso_started(USBHostDevice *s, int ep)
193 struct endp_data *e = get_endp(s, ep);
195 trace_usb_host_ep_start_iso(s->bus_num, s->addr, ep);
196 if (!e->iso_started) {
202 static int change_iso_inflight(USBHostDevice *s, int ep, int value)
204 struct endp_data *e = get_endp(s, ep);
206 e->inflight += value;
210 static void set_iso_urb(USBHostDevice *s, int ep, AsyncURB *iso_urb)
212 get_endp(s, ep)->iso_urb = iso_urb;
215 static AsyncURB *get_iso_urb(USBHostDevice *s, int ep)
217 return get_endp(s, ep)->iso_urb;
220 static void set_iso_urb_idx(USBHostDevice *s, int ep, int i)
222 get_endp(s, ep)->iso_urb_idx = i;
225 static int get_iso_urb_idx(USBHostDevice *s, int ep)
227 return get_endp(s, ep)->iso_urb_idx;
230 static void set_iso_buffer_used(USBHostDevice *s, int ep, int i)
232 get_endp(s, ep)->iso_buffer_used = i;
235 static int get_iso_buffer_used(USBHostDevice *s, int ep)
237 return get_endp(s, ep)->iso_buffer_used;
240 static void set_max_packet_size(USBHostDevice *s, int ep, uint8_t *descriptor)
242 int raw = descriptor[4] + (descriptor[5] << 8);
243 int size, microframes;
246 switch ((raw >> 11) & 3) {
247 case 1: microframes = 2; break;
248 case 2: microframes = 3; break;
249 default: microframes = 1; break;
251 get_endp(s, ep)->max_packet_size = size * microframes;
254 static int get_max_packet_size(USBHostDevice *s, int ep)
256 return get_endp(s, ep)->max_packet_size;
261 * We always allocate iso packet descriptors even for bulk transfers
262 * to simplify allocation and casts.
266 struct usbdevfs_urb urb;
267 struct usbdevfs_iso_packet_desc isocpd[ISO_FRAME_DESC_PER_URB];
269 QLIST_ENTRY(AsyncURB) next;
271 /* For regular async urbs */
273 int more; /* large transfer, more urbs follow */
275 /* For buffered iso handling */
276 int iso_frame_idx; /* -1 means in flight */
279 static AsyncURB *async_alloc(USBHostDevice *s)
281 AsyncURB *aurb = g_malloc0(sizeof(AsyncURB));
283 QLIST_INSERT_HEAD(&s->aurbs, aurb, next);
287 static void async_free(AsyncURB *aurb)
289 QLIST_REMOVE(aurb, next);
293 static void do_disconnect(USBHostDevice *s)
296 usb_host_auto_check(NULL);
299 static void async_complete(void *opaque)
301 USBHostDevice *s = opaque;
308 int r = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &aurb);
310 if (errno == EAGAIN) {
312 fprintf(stderr, "husb: %d iso urbs finished at once\n", urbs);
316 if (errno == ENODEV) {
318 trace_usb_host_disconnect(s->bus_num, s->addr);
324 perror("USBDEVFS_REAPURBNDELAY");
328 DPRINTF("husb: async completed. aurb %p status %d alen %d\n",
329 aurb, aurb->urb.status, aurb->urb.actual_length);
331 /* If this is a buffered iso urb mark it as complete and don't do
332 anything else (it is handled further in usb_host_handle_iso_data) */
333 if (aurb->iso_frame_idx == -1) {
335 if (aurb->urb.status == -EPIPE) {
336 set_halt(s, aurb->urb.endpoint & 0xf);
338 aurb->iso_frame_idx = 0;
340 inflight = change_iso_inflight(s, aurb->urb.endpoint & 0xf, -1);
341 if (inflight == 0 && is_iso_started(s, aurb->urb.endpoint & 0xf)) {
342 fprintf(stderr, "husb: out of buffers for iso stream\n");
348 trace_usb_host_urb_complete(s->bus_num, s->addr, aurb, aurb->urb.status,
349 aurb->urb.actual_length, aurb->more);
352 switch (aurb->urb.status) {
354 p->result += aurb->urb.actual_length;
358 set_halt(s, p->devep);
359 p->result = USB_RET_STALL;
363 p->result = USB_RET_NAK;
367 if (aurb->urb.type == USBDEVFS_URB_TYPE_CONTROL) {
368 trace_usb_host_req_complete(s->bus_num, s->addr, p->result);
369 usb_generic_async_ctrl_complete(&s->dev, p);
370 } else if (!aurb->more) {
371 trace_usb_host_req_complete(s->bus_num, s->addr, p->result);
372 usb_packet_complete(&s->dev, p);
380 static void usb_host_async_cancel(USBDevice *dev, USBPacket *p)
382 USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
385 QLIST_FOREACH(aurb, &s->aurbs, next) {
386 if (p != aurb->packet) {
390 DPRINTF("husb: async cancel: packet %p, aurb %p\n", p, aurb);
392 /* Mark it as dead (see async_complete above) */
395 int r = ioctl(s->fd, USBDEVFS_DISCARDURB, aurb);
397 DPRINTF("husb: async. discard urb failed errno %d\n", errno);
402 static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration)
404 const char *op = NULL;
405 int dev_descr_len, config_descr_len;
406 int interface, nb_interfaces;
409 if (configuration == 0) /* address state - ignore */
412 DPRINTF("husb: claiming interfaces. config %d\n", configuration);
415 dev_descr_len = dev->descr[0];
416 if (dev_descr_len > dev->descr_len) {
417 fprintf(stderr, "husb: update iface failed. descr too short\n");
422 while (i < dev->descr_len) {
423 DPRINTF("husb: i is %d, descr_len is %d, dl %d, dt %d\n",
425 dev->descr[i], dev->descr[i+1]);
427 if (dev->descr[i+1] != USB_DT_CONFIG) {
431 config_descr_len = dev->descr[i];
433 DPRINTF("husb: config #%d need %d\n", dev->descr[i + 5], configuration);
435 if (configuration < 0 || configuration == dev->descr[i + 5]) {
436 configuration = dev->descr[i + 5];
440 i += config_descr_len;
443 if (i >= dev->descr_len) {
445 "husb: update iface failed. no matching configuration\n");
448 nb_interfaces = dev->descr[i + 4];
450 #ifdef USBDEVFS_DISCONNECT
451 /* earlier Linux 2.4 do not support that */
453 struct usbdevfs_ioctl ctrl;
454 for (interface = 0; interface < nb_interfaces; interface++) {
455 ctrl.ioctl_code = USBDEVFS_DISCONNECT;
456 ctrl.ifno = interface;
458 op = "USBDEVFS_DISCONNECT";
459 ret = ioctl(dev->fd, USBDEVFS_IOCTL, &ctrl);
460 if (ret < 0 && errno != ENODATA) {
467 /* XXX: only grab if all interfaces are free */
468 for (interface = 0; interface < nb_interfaces; interface++) {
469 op = "USBDEVFS_CLAIMINTERFACE";
470 ret = ioctl(dev->fd, USBDEVFS_CLAIMINTERFACE, &interface);
476 trace_usb_host_claim_interfaces(dev->bus_num, dev->addr,
477 nb_interfaces, configuration);
479 dev->ninterfaces = nb_interfaces;
480 dev->configuration = configuration;
484 if (errno == ENODEV) {
491 static int usb_host_release_interfaces(USBHostDevice *s)
495 trace_usb_host_release_interfaces(s->bus_num, s->addr);
497 for (i = 0; i < s->ninterfaces; i++) {
498 ret = ioctl(s->fd, USBDEVFS_RELEASEINTERFACE, &i);
500 perror("USBDEVFS_RELEASEINTERFACE");
507 static void usb_host_handle_reset(USBDevice *dev)
509 USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
511 trace_usb_host_reset(s->bus_num, s->addr);
513 ioctl(s->fd, USBDEVFS_RESET);
515 usb_host_claim_interfaces(s, s->configuration);
516 usb_linux_update_endp_table(s);
519 static void usb_host_handle_destroy(USBDevice *dev)
521 USBHostDevice *s = (USBHostDevice *)dev;
524 QTAILQ_REMOVE(&hostdevs, s, next);
525 qemu_remove_exit_notifier(&s->exit);
528 /* iso data is special, we need to keep enough urbs in flight to make sure
529 that the controller never runs out of them, otherwise the device will
530 likely suffer a buffer underrun / overrun. */
531 static AsyncURB *usb_host_alloc_iso(USBHostDevice *s, uint8_t ep, int in)
534 int i, j, len = get_max_packet_size(s, ep);
536 aurb = g_malloc0(s->iso_urb_count * sizeof(*aurb));
537 for (i = 0; i < s->iso_urb_count; i++) {
538 aurb[i].urb.endpoint = ep;
539 aurb[i].urb.buffer_length = ISO_FRAME_DESC_PER_URB * len;
540 aurb[i].urb.buffer = g_malloc(aurb[i].urb.buffer_length);
541 aurb[i].urb.type = USBDEVFS_URB_TYPE_ISO;
542 aurb[i].urb.flags = USBDEVFS_URB_ISO_ASAP;
543 aurb[i].urb.number_of_packets = ISO_FRAME_DESC_PER_URB;
544 for (j = 0 ; j < ISO_FRAME_DESC_PER_URB; j++)
545 aurb[i].urb.iso_frame_desc[j].length = len;
547 aurb[i].urb.endpoint |= 0x80;
548 /* Mark as fully consumed (idle) */
549 aurb[i].iso_frame_idx = ISO_FRAME_DESC_PER_URB;
552 set_iso_urb(s, ep, aurb);
557 static void usb_host_stop_n_free_iso(USBHostDevice *s, uint8_t ep)
560 int i, ret, killed = 0, free = 1;
562 aurb = get_iso_urb(s, ep);
567 for (i = 0; i < s->iso_urb_count; i++) {
569 if (aurb[i].iso_frame_idx == -1) {
570 ret = ioctl(s->fd, USBDEVFS_DISCARDURB, &aurb[i]);
572 perror("USBDEVFS_DISCARDURB");
580 /* Make sure any urbs we've killed are reaped before we free them */
585 for (i = 0; i < s->iso_urb_count; i++) {
586 g_free(aurb[i].urb.buffer);
592 printf("husb: leaking iso urbs because of discard failure\n");
593 set_iso_urb(s, ep, NULL);
594 set_iso_urb_idx(s, ep, 0);
595 clear_iso_started(s, ep);
598 static int urb_status_to_usb_ret(int status)
602 return USB_RET_STALL;
608 static int usb_host_handle_iso_data(USBHostDevice *s, USBPacket *p, int in)
611 int i, j, ret, max_packet_size, offset, len = 0;
614 max_packet_size = get_max_packet_size(s, p->devep);
615 if (max_packet_size == 0)
618 aurb = get_iso_urb(s, p->devep);
620 aurb = usb_host_alloc_iso(s, p->devep, in);
623 i = get_iso_urb_idx(s, p->devep);
624 j = aurb[i].iso_frame_idx;
625 if (j >= 0 && j < ISO_FRAME_DESC_PER_URB) {
627 /* Check urb status */
628 if (aurb[i].urb.status) {
629 len = urb_status_to_usb_ret(aurb[i].urb.status);
630 /* Move to the next urb */
631 aurb[i].iso_frame_idx = ISO_FRAME_DESC_PER_URB - 1;
632 /* Check frame status */
633 } else if (aurb[i].urb.iso_frame_desc[j].status) {
634 len = urb_status_to_usb_ret(
635 aurb[i].urb.iso_frame_desc[j].status);
636 /* Check the frame fits */
637 } else if (aurb[i].urb.iso_frame_desc[j].actual_length
639 printf("husb: received iso data is larger then packet\n");
641 /* All good copy data over */
643 len = aurb[i].urb.iso_frame_desc[j].actual_length;
644 buf = aurb[i].urb.buffer +
645 j * aurb[i].urb.iso_frame_desc[0].length;
646 usb_packet_copy(p, buf, len);
650 offset = (j == 0) ? 0 : get_iso_buffer_used(s, p->devep);
652 /* Check the frame fits */
653 if (len > max_packet_size) {
654 printf("husb: send iso data is larger then max packet size\n");
658 /* All good copy data over */
659 usb_packet_copy(p, aurb[i].urb.buffer + offset, len);
660 aurb[i].urb.iso_frame_desc[j].length = len;
662 set_iso_buffer_used(s, p->devep, offset);
664 /* Start the stream once we have buffered enough data */
665 if (!is_iso_started(s, p->devep) && i == 1 && j == 8) {
666 set_iso_started(s, p->devep);
669 aurb[i].iso_frame_idx++;
670 if (aurb[i].iso_frame_idx == ISO_FRAME_DESC_PER_URB) {
671 i = (i + 1) % s->iso_urb_count;
672 set_iso_urb_idx(s, p->devep, i);
676 set_iso_started(s, p->devep);
678 DPRINTF("hubs: iso out error no free buffer, dropping packet\n");
682 if (is_iso_started(s, p->devep)) {
683 /* (Re)-submit all fully consumed / filled urbs */
684 for (i = 0; i < s->iso_urb_count; i++) {
685 if (aurb[i].iso_frame_idx == ISO_FRAME_DESC_PER_URB) {
686 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, &aurb[i]);
688 perror("USBDEVFS_SUBMITURB");
689 if (!in || len == 0) {
701 aurb[i].iso_frame_idx = -1;
702 change_iso_inflight(s, p->devep, +1);
710 static int usb_host_handle_data(USBDevice *dev, USBPacket *p)
712 USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
713 struct usbdevfs_urb *urb;
715 int ret, rem, prem, v;
719 trace_usb_host_req_data(s->bus_num, s->addr,
720 p->pid == USB_TOKEN_IN,
721 p->devep, p->iov.size);
723 if (!is_valid(s, p->devep)) {
724 trace_usb_host_req_complete(s->bus_num, s->addr, USB_RET_NAK);
728 if (p->pid == USB_TOKEN_IN) {
729 ep = p->devep | 0x80;
734 if (is_halted(s, p->devep)) {
735 unsigned int arg = ep;
736 ret = ioctl(s->fd, USBDEVFS_CLEAR_HALT, &arg);
738 perror("USBDEVFS_CLEAR_HALT");
739 trace_usb_host_req_complete(s->bus_num, s->addr, USB_RET_NAK);
742 clear_halt(s, p->devep);
745 if (is_isoc(s, p->devep)) {
746 return usb_host_handle_iso_data(s, p, p->pid == USB_TOKEN_IN);
750 prem = p->iov.iov[v].iov_len;
751 pbuf = p->iov.iov[v].iov_base;
756 assert(v < p->iov.niov);
757 prem = p->iov.iov[v].iov_len;
758 pbuf = p->iov.iov[v].iov_base;
761 aurb = async_alloc(s);
766 urb->type = USBDEVFS_URB_TYPE_BULK;
767 urb->usercontext = s;
769 urb->buffer_length = prem;
771 if (urb->buffer_length > MAX_USBFS_BUFFER_SIZE) {
772 urb->buffer_length = MAX_USBFS_BUFFER_SIZE;
774 pbuf += urb->buffer_length;
775 prem -= urb->buffer_length;
776 rem -= urb->buffer_length;
781 trace_usb_host_urb_submit(s->bus_num, s->addr, aurb,
782 urb->buffer_length, aurb->more);
783 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
785 DPRINTF("husb: data submit: ep 0x%x, len %u, more %d, packet %p, aurb %p\n",
786 urb->endpoint, urb->buffer_length, aurb->more, p, aurb);
789 perror("USBDEVFS_SUBMITURB");
794 trace_usb_host_req_complete(s->bus_num, s->addr, USB_RET_NAK);
798 trace_usb_host_req_complete(s->bus_num, s->addr, USB_RET_STALL);
799 return USB_RET_STALL;
804 return USB_RET_ASYNC;
807 static int ctrl_error(void)
809 if (errno == ETIMEDOUT) {
812 return USB_RET_STALL;
816 static int usb_host_set_address(USBHostDevice *s, int addr)
818 trace_usb_host_set_address(s->bus_num, s->addr, addr);
823 static int usb_host_set_config(USBHostDevice *s, int config)
825 trace_usb_host_set_config(s->bus_num, s->addr, config);
827 usb_host_release_interfaces(s);
829 int ret = ioctl(s->fd, USBDEVFS_SETCONFIGURATION, &config);
831 DPRINTF("husb: ctrl set config %d ret %d errno %d\n", config, ret, errno);
836 usb_host_claim_interfaces(s, config);
840 static int usb_host_set_interface(USBHostDevice *s, int iface, int alt)
842 struct usbdevfs_setinterface si;
845 trace_usb_host_set_interface(s->bus_num, s->addr, iface, alt);
847 for (i = 1; i <= MAX_ENDPOINTS; i++) {
849 usb_host_stop_n_free_iso(s, i);
853 si.interface = iface;
855 ret = ioctl(s->fd, USBDEVFS_SETINTERFACE, &si);
857 DPRINTF("husb: ctrl set iface %d altset %d ret %d errno %d\n",
858 iface, alt, ret, errno);
863 usb_linux_update_endp_table(s);
867 static int usb_host_handle_control(USBDevice *dev, USBPacket *p,
868 int request, int value, int index, int length, uint8_t *data)
870 USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
871 struct usbdevfs_urb *urb;
876 * Process certain standard device requests.
877 * These are infrequent and are processed synchronously.
880 /* Note request is (bRequestType << 8) | bRequest */
881 trace_usb_host_req_control(s->bus_num, s->addr, request, value, index);
884 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
885 return usb_host_set_address(s, value);
887 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
888 return usb_host_set_config(s, value & 0xff);
890 case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
891 return usb_host_set_interface(s, index, value);
894 /* The rest are asynchronous */
896 if (length > sizeof(dev->data_buf)) {
897 fprintf(stderr, "husb: ctrl buffer too small (%d > %zu)\n",
898 length, sizeof(dev->data_buf));
899 return USB_RET_STALL;
902 aurb = async_alloc(s);
906 * Setup ctrl transfer.
908 * s->ctrl is laid out such that data buffer immediately follows
909 * 'req' struct which is exactly what usbdevfs expects.
913 urb->type = USBDEVFS_URB_TYPE_CONTROL;
914 urb->endpoint = p->devep;
916 urb->buffer = &dev->setup_buf;
917 urb->buffer_length = length + 8;
919 urb->usercontext = s;
921 trace_usb_host_urb_submit(s->bus_num, s->addr, aurb,
922 urb->buffer_length, aurb->more);
923 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
925 DPRINTF("husb: submit ctrl. len %u aurb %p\n", urb->buffer_length, aurb);
928 DPRINTF("husb: submit failed. errno %d\n", errno);
936 return USB_RET_STALL;
940 return USB_RET_ASYNC;
943 static int usb_linux_get_configuration(USBHostDevice *s)
945 uint8_t configuration;
946 struct usb_ctrltransfer ct;
949 if (usb_fs_type == USB_FS_SYS) {
950 char device_name[32], line[1024];
953 sprintf(device_name, "%d-%s", s->bus_num, s->port);
955 if (!usb_host_read_file(line, sizeof(line), "bConfigurationValue",
959 if (sscanf(line, "%d", &configuration) != 1) {
962 return configuration;
966 ct.bRequestType = USB_DIR_IN;
967 ct.bRequest = USB_REQ_GET_CONFIGURATION;
971 ct.data = &configuration;
974 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
976 perror("usb_linux_get_configuration");
980 /* in address state */
981 if (configuration == 0) {
985 return configuration;
988 static uint8_t usb_linux_get_alt_setting(USBHostDevice *s,
989 uint8_t configuration, uint8_t interface)
992 struct usb_ctrltransfer ct;
995 if (usb_fs_type == USB_FS_SYS) {
996 char device_name[64], line[1024];
999 sprintf(device_name, "%d-%s:%d.%d", s->bus_num, s->port,
1000 (int)configuration, (int)interface);
1002 if (!usb_host_read_file(line, sizeof(line), "bAlternateSetting",
1006 if (sscanf(line, "%d", &alt_setting) != 1) {
1013 ct.bRequestType = USB_DIR_IN | USB_RECIP_INTERFACE;
1014 ct.bRequest = USB_REQ_GET_INTERFACE;
1016 ct.wIndex = interface;
1018 ct.data = &alt_setting;
1020 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
1022 /* Assume alt 0 on error */
1029 /* returns 1 on problem encountered or 0 for success */
1030 static int usb_linux_update_endp_table(USBHostDevice *s)
1032 uint8_t *descriptors;
1033 uint8_t devep, type, configuration, alt_interface;
1034 int interface, length, i;
1036 for (i = 0; i < MAX_ENDPOINTS; i++)
1037 s->endp_table[i].type = INVALID_EP_TYPE;
1039 i = usb_linux_get_configuration(s);
1044 /* get the desired configuration, interface, and endpoint descriptors
1045 * from device description */
1046 descriptors = &s->descr[18];
1047 length = s->descr_len - 18;
1050 if (descriptors[i + 1] != USB_DT_CONFIG ||
1051 descriptors[i + 5] != configuration) {
1052 DPRINTF("invalid descriptor data - configuration\n");
1055 i += descriptors[i];
1057 while (i < length) {
1058 if (descriptors[i + 1] != USB_DT_INTERFACE ||
1059 (descriptors[i + 1] == USB_DT_INTERFACE &&
1060 descriptors[i + 4] == 0)) {
1061 i += descriptors[i];
1065 interface = descriptors[i + 2];
1066 alt_interface = usb_linux_get_alt_setting(s, configuration, interface);
1068 /* the current interface descriptor is the active interface
1069 * and has endpoints */
1070 if (descriptors[i + 3] != alt_interface) {
1071 i += descriptors[i];
1075 /* advance to the endpoints */
1076 while (i < length && descriptors[i +1] != USB_DT_ENDPOINT) {
1077 i += descriptors[i];
1083 while (i < length) {
1084 if (descriptors[i + 1] != USB_DT_ENDPOINT) {
1088 devep = descriptors[i + 2];
1089 if ((devep & 0x0f) == 0) {
1090 fprintf(stderr, "usb-linux: invalid ep descriptor, ep == 0\n");
1094 switch (descriptors[i + 3] & 0x3) {
1096 type = USBDEVFS_URB_TYPE_CONTROL;
1099 type = USBDEVFS_URB_TYPE_ISO;
1100 set_max_packet_size(s, (devep & 0xf), descriptors + i);
1103 type = USBDEVFS_URB_TYPE_BULK;
1106 type = USBDEVFS_URB_TYPE_INTERRUPT;
1109 DPRINTF("usb_host: malformed endpoint type\n");
1110 type = USBDEVFS_URB_TYPE_BULK;
1112 s->endp_table[(devep & 0xf) - 1].type = type;
1113 s->endp_table[(devep & 0xf) - 1].halted = 0;
1115 i += descriptors[i];
1122 * Check if we can safely redirect a usb2 device to a usb1 virtual controller,
1123 * this function assumes this is safe, if:
1124 * 1) There are no isoc endpoints
1125 * 2) There are no interrupt endpoints with a max_packet_size > 64
1126 * Note bulk endpoints with a max_packet_size > 64 in theory also are not
1127 * usb1 compatible, but in practice this seems to work fine.
1129 static int usb_linux_full_speed_compat(USBHostDevice *dev)
1134 * usb_linux_update_endp_table only registers info about ep in the current
1135 * interface altsettings, so we need to parse the descriptors again.
1137 for (i = 0; (i + 5) < dev->descr_len; i += dev->descr[i]) {
1138 if (dev->descr[i + 1] == USB_DT_ENDPOINT) {
1139 switch (dev->descr[i + 3] & 0x3) {
1140 case 0x00: /* CONTROL */
1142 case 0x01: /* ISO */
1144 case 0x02: /* BULK */
1146 case 0x03: /* INTERRUPT */
1147 packet_size = dev->descr[i + 4] + (dev->descr[i + 5] << 8);
1148 if (packet_size > 64)
1157 static int usb_host_open(USBHostDevice *dev, int bus_num,
1158 int addr, char *port, const char *prod_name, int speed)
1163 trace_usb_host_open_started(bus_num, addr);
1165 if (dev->fd != -1) {
1169 if (!usb_host_device_path) {
1170 perror("husb: USB Host Device Path not set");
1173 snprintf(buf, sizeof(buf), "%s/%03d/%03d", usb_host_device_path,
1175 fd = open(buf, O_RDWR | O_NONBLOCK);
1180 DPRINTF("husb: opened %s\n", buf);
1182 dev->bus_num = bus_num;
1184 strcpy(dev->port, port);
1187 /* read the device description */
1188 dev->descr_len = read(fd, dev->descr, sizeof(dev->descr));
1189 if (dev->descr_len <= 0) {
1190 perror("husb: reading device data failed");
1197 printf("=== begin dumping device descriptor data ===\n");
1198 for (x = 0; x < dev->descr_len; x++) {
1199 printf("%02x ", dev->descr[x]);
1201 printf("\n=== end dumping device descriptor data ===\n");
1207 * Initial configuration is -1 which makes us claim first
1208 * available config. We used to start with 1, which does not
1209 * always work. I've seen devices where first config starts
1212 if (!usb_host_claim_interfaces(dev, -1)) {
1216 ret = usb_linux_update_endp_table(dev);
1222 struct usbdevfs_connectinfo ci;
1224 ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci);
1226 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
1231 speed = USB_SPEED_LOW;
1233 speed = USB_SPEED_HIGH;
1236 dev->dev.speed = speed;
1237 dev->dev.speedmask = (1 << speed);
1238 if (dev->dev.speed == USB_SPEED_HIGH && usb_linux_full_speed_compat(dev)) {
1239 dev->dev.speedmask |= USB_SPEED_MASK_FULL;
1242 trace_usb_host_open_success(bus_num, addr);
1244 if (!prod_name || prod_name[0] == '\0') {
1245 snprintf(dev->dev.product_desc, sizeof(dev->dev.product_desc),
1246 "host:%d.%d", bus_num, addr);
1248 pstrcpy(dev->dev.product_desc, sizeof(dev->dev.product_desc),
1252 ret = usb_device_attach(&dev->dev);
1257 /* USB devio uses 'write' flag to check for async completions */
1258 qemu_set_fd_handler(dev->fd, NULL, async_complete, dev);
1263 trace_usb_host_open_failure(bus_num, addr);
1264 if (dev->fd != -1) {
1271 static int usb_host_close(USBHostDevice *dev)
1275 if (dev->fd == -1 || !dev->dev.attached) {
1279 trace_usb_host_close(dev->bus_num, dev->addr);
1281 qemu_set_fd_handler(dev->fd, NULL, NULL, NULL);
1283 for (i = 1; i <= MAX_ENDPOINTS; i++) {
1284 if (is_isoc(dev, i)) {
1285 usb_host_stop_n_free_iso(dev, i);
1288 async_complete(dev);
1290 usb_device_detach(&dev->dev);
1291 ioctl(dev->fd, USBDEVFS_RESET);
1297 static void usb_host_exit_notifier(struct Notifier *n, void *data)
1299 USBHostDevice *s = container_of(n, USBHostDevice, exit);
1302 ioctl(s->fd, USBDEVFS_RESET);
1306 static int usb_host_initfn(USBDevice *dev)
1308 USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
1310 dev->auto_attach = 0;
1312 QTAILQ_INSERT_TAIL(&hostdevs, s, next);
1313 s->exit.notify = usb_host_exit_notifier;
1314 qemu_add_exit_notifier(&s->exit);
1315 usb_host_auto_check(NULL);
1319 static struct USBDeviceInfo usb_host_dev_info = {
1320 .product_desc = "USB Host Device",
1321 .qdev.name = "usb-host",
1322 .qdev.size = sizeof(USBHostDevice),
1323 .init = usb_host_initfn,
1324 .handle_packet = usb_generic_handle_packet,
1325 .cancel_packet = usb_host_async_cancel,
1326 .handle_data = usb_host_handle_data,
1327 .handle_control = usb_host_handle_control,
1328 .handle_reset = usb_host_handle_reset,
1329 .handle_destroy = usb_host_handle_destroy,
1330 .usbdevice_name = "host",
1331 .usbdevice_init = usb_host_device_open,
1332 .qdev.props = (Property[]) {
1333 DEFINE_PROP_UINT32("hostbus", USBHostDevice, match.bus_num, 0),
1334 DEFINE_PROP_UINT32("hostaddr", USBHostDevice, match.addr, 0),
1335 DEFINE_PROP_STRING("hostport", USBHostDevice, match.port),
1336 DEFINE_PROP_HEX32("vendorid", USBHostDevice, match.vendor_id, 0),
1337 DEFINE_PROP_HEX32("productid", USBHostDevice, match.product_id, 0),
1338 DEFINE_PROP_UINT32("isobufs", USBHostDevice, iso_urb_count, 4),
1339 DEFINE_PROP_END_OF_LIST(),
1343 static void usb_host_register_devices(void)
1345 usb_qdev_register(&usb_host_dev_info);
1347 device_init(usb_host_register_devices)
1349 USBDevice *usb_host_device_open(const char *devname)
1351 struct USBAutoFilter filter;
1355 dev = usb_create(NULL /* FIXME */, "usb-host");
1357 if (strstr(devname, "auto:")) {
1358 if (parse_filter(devname, &filter) < 0) {
1362 if ((p = strchr(devname, '.'))) {
1363 filter.bus_num = strtoul(devname, NULL, 0);
1364 filter.addr = strtoul(p + 1, NULL, 0);
1365 filter.vendor_id = 0;
1366 filter.product_id = 0;
1367 } else if ((p = strchr(devname, ':'))) {
1370 filter.vendor_id = strtoul(devname, NULL, 16);
1371 filter.product_id = strtoul(p + 1, NULL, 16);
1377 qdev_prop_set_uint32(&dev->qdev, "hostbus", filter.bus_num);
1378 qdev_prop_set_uint32(&dev->qdev, "hostaddr", filter.addr);
1379 qdev_prop_set_uint32(&dev->qdev, "vendorid", filter.vendor_id);
1380 qdev_prop_set_uint32(&dev->qdev, "productid", filter.product_id);
1381 qdev_init_nofail(&dev->qdev);
1385 qdev_free(&dev->qdev);
1389 int usb_host_device_close(const char *devname)
1392 char product_name[PRODUCT_NAME_SZ];
1396 if (strstr(devname, "auto:")) {
1397 return usb_host_auto_del(devname);
1399 if (usb_host_find_device(&bus_num, &addr, product_name,
1400 sizeof(product_name), devname) < 0) {
1403 s = hostdev_find(bus_num, addr);
1405 usb_device_delete_addr(s->bus_num, s->dev.addr);
1413 static int get_tag_value(char *buf, int buf_size,
1414 const char *str, const char *tag,
1415 const char *stopchars)
1419 p = strstr(str, tag);
1424 while (qemu_isspace(*p)) {
1428 while (*p != '\0' && !strchr(stopchars, *p)) {
1429 if ((q - buf) < (buf_size - 1)) {
1439 * Use /proc/bus/usb/devices or /dev/bus/usb/devices file to determine
1440 * host's USB devices. This is legacy support since many distributions
1441 * are moving to /sys/bus/usb
1443 static int usb_host_scan_dev(void *opaque, USBScanFunc *func)
1448 int bus_num, addr, speed, device_count, class_id, product_id, vendor_id;
1449 char product_name[512];
1452 if (!usb_host_device_path) {
1453 perror("husb: USB Host Device Path not set");
1456 snprintf(line, sizeof(line), "%s/devices", usb_host_device_path);
1457 f = fopen(line, "r");
1459 perror("husb: cannot open devices file");
1464 bus_num = addr = class_id = product_id = vendor_id = 0;
1465 speed = -1; /* Can't get the speed from /[proc|dev]/bus/usb/devices */
1467 if (fgets(line, sizeof(line), f) == NULL) {
1470 if (strlen(line) > 0) {
1471 line[strlen(line) - 1] = '\0';
1473 if (line[0] == 'T' && line[1] == ':') {
1474 if (device_count && (vendor_id || product_id)) {
1475 /* New device. Add the previously discovered device. */
1476 ret = func(opaque, bus_num, addr, 0, class_id, vendor_id,
1477 product_id, product_name, speed);
1482 if (get_tag_value(buf, sizeof(buf), line, "Bus=", " ") < 0) {
1485 bus_num = atoi(buf);
1486 if (get_tag_value(buf, sizeof(buf), line, "Dev#=", " ") < 0) {
1490 if (get_tag_value(buf, sizeof(buf), line, "Spd=", " ") < 0) {
1493 if (!strcmp(buf, "5000")) {
1494 speed = USB_SPEED_SUPER;
1495 } else if (!strcmp(buf, "480")) {
1496 speed = USB_SPEED_HIGH;
1497 } else if (!strcmp(buf, "1.5")) {
1498 speed = USB_SPEED_LOW;
1500 speed = USB_SPEED_FULL;
1502 product_name[0] = '\0';
1507 } else if (line[0] == 'P' && line[1] == ':') {
1508 if (get_tag_value(buf, sizeof(buf), line, "Vendor=", " ") < 0) {
1511 vendor_id = strtoul(buf, NULL, 16);
1512 if (get_tag_value(buf, sizeof(buf), line, "ProdID=", " ") < 0) {
1515 product_id = strtoul(buf, NULL, 16);
1516 } else if (line[0] == 'S' && line[1] == ':') {
1517 if (get_tag_value(buf, sizeof(buf), line, "Product=", "") < 0) {
1520 pstrcpy(product_name, sizeof(product_name), buf);
1521 } else if (line[0] == 'D' && line[1] == ':') {
1522 if (get_tag_value(buf, sizeof(buf), line, "Cls=", " (") < 0) {
1525 class_id = strtoul(buf, NULL, 16);
1529 if (device_count && (vendor_id || product_id)) {
1530 /* Add the last device. */
1531 ret = func(opaque, bus_num, addr, 0, class_id, vendor_id,
1532 product_id, product_name, speed);
1542 * Read sys file-system device file
1544 * @line address of buffer to put file contents in
1545 * @line_size size of line
1546 * @device_file path to device file (printf format string)
1547 * @device_name device being opened (inserted into device_file)
1549 * @return 0 failed, 1 succeeded ('line' contains data)
1551 static int usb_host_read_file(char *line, size_t line_size,
1552 const char *device_file, const char *device_name)
1556 char filename[PATH_MAX];
1558 snprintf(filename, PATH_MAX, USBSYSBUS_PATH "/devices/%s/%s", device_name,
1560 f = fopen(filename, "r");
1562 ret = fgets(line, line_size, f) != NULL;
1570 * Use /sys/bus/usb/devices/ directory to determine host's USB
1573 * This code is based on Robert Schiele's original patches posted to
1574 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1576 static int usb_host_scan_sys(void *opaque, USBScanFunc *func)
1580 int bus_num, addr, speed, class_id, product_id, vendor_id;
1582 char port[MAX_PORTLEN];
1583 char product_name[512];
1586 dir = opendir(USBSYSBUS_PATH "/devices");
1588 perror("husb: cannot open devices directory");
1592 while ((de = readdir(dir))) {
1593 if (de->d_name[0] != '.' && !strchr(de->d_name, ':')) {
1594 if (sscanf(de->d_name, "%d-%7[0-9.]", &bus_num, port) < 2) {
1598 if (!usb_host_read_file(line, sizeof(line), "devnum", de->d_name)) {
1601 if (sscanf(line, "%d", &addr) != 1) {
1604 if (!usb_host_read_file(line, sizeof(line), "bDeviceClass",
1608 if (sscanf(line, "%x", &class_id) != 1) {
1612 if (!usb_host_read_file(line, sizeof(line), "idVendor",
1616 if (sscanf(line, "%x", &vendor_id) != 1) {
1619 if (!usb_host_read_file(line, sizeof(line), "idProduct",
1623 if (sscanf(line, "%x", &product_id) != 1) {
1626 if (!usb_host_read_file(line, sizeof(line), "product",
1630 if (strlen(line) > 0) {
1631 line[strlen(line) - 1] = '\0';
1633 pstrcpy(product_name, sizeof(product_name), line);
1636 if (!usb_host_read_file(line, sizeof(line), "speed", de->d_name)) {
1639 if (!strcmp(line, "5000\n")) {
1640 speed = USB_SPEED_SUPER;
1641 } else if (!strcmp(line, "480\n")) {
1642 speed = USB_SPEED_HIGH;
1643 } else if (!strcmp(line, "1.5\n")) {
1644 speed = USB_SPEED_LOW;
1646 speed = USB_SPEED_FULL;
1649 ret = func(opaque, bus_num, addr, port, class_id, vendor_id,
1650 product_id, product_name, speed);
1664 * Determine how to access the host's USB devices and call the
1665 * specific support function.
1667 static int usb_host_scan(void *opaque, USBScanFunc *func)
1669 Monitor *mon = cur_mon;
1673 const char *fs_type[] = {"unknown", "proc", "dev", "sys"};
1674 char devpath[PATH_MAX];
1676 /* only check the host once */
1678 dir = opendir(USBSYSBUS_PATH "/devices");
1680 /* devices found in /dev/bus/usb/ (yes - not a mistake!) */
1681 strcpy(devpath, USBDEVBUS_PATH);
1682 usb_fs_type = USB_FS_SYS;
1684 DPRINTF(USBDBG_DEVOPENED, USBSYSBUS_PATH);
1687 f = fopen(USBPROCBUS_PATH "/devices", "r");
1689 /* devices found in /proc/bus/usb/ */
1690 strcpy(devpath, USBPROCBUS_PATH);
1691 usb_fs_type = USB_FS_PROC;
1693 DPRINTF(USBDBG_DEVOPENED, USBPROCBUS_PATH);
1696 /* try additional methods if an access method hasn't been found yet */
1697 f = fopen(USBDEVBUS_PATH "/devices", "r");
1699 /* devices found in /dev/bus/usb/ */
1700 strcpy(devpath, USBDEVBUS_PATH);
1701 usb_fs_type = USB_FS_DEV;
1703 DPRINTF(USBDBG_DEVOPENED, USBDEVBUS_PATH);
1709 monitor_printf(mon, "husb: unable to access USB devices\n");
1714 /* the module setting (used later for opening devices) */
1715 usb_host_device_path = g_malloc0(strlen(devpath)+1);
1716 strcpy(usb_host_device_path, devpath);
1718 monitor_printf(mon, "husb: using %s file-system with %s\n",
1719 fs_type[usb_fs_type], usb_host_device_path);
1723 switch (usb_fs_type) {
1726 ret = usb_host_scan_dev(opaque, func);
1729 ret = usb_host_scan_sys(opaque, func);
1738 static QEMUTimer *usb_auto_timer;
1740 static int usb_host_auto_scan(void *opaque, int bus_num, int addr, char *port,
1741 int class_id, int vendor_id, int product_id,
1742 const char *product_name, int speed)
1744 struct USBAutoFilter *f;
1745 struct USBHostDevice *s;
1751 QTAILQ_FOREACH(s, &hostdevs, next) {
1754 if (f->bus_num > 0 && f->bus_num != bus_num) {
1757 if (f->addr > 0 && f->addr != addr) {
1760 if (f->port != NULL && (port == NULL || strcmp(f->port, port) != 0)) {
1764 if (f->vendor_id > 0 && f->vendor_id != vendor_id) {
1768 if (f->product_id > 0 && f->product_id != product_id) {
1771 /* We got a match */
1773 /* Already attached ? */
1777 DPRINTF("husb: auto open: bus_num %d addr %d\n", bus_num, addr);
1779 usb_host_open(s, bus_num, addr, port, product_name, speed);
1786 static void usb_host_auto_check(void *unused)
1788 struct USBHostDevice *s;
1789 int unconnected = 0;
1791 usb_host_scan(NULL, usb_host_auto_scan);
1793 QTAILQ_FOREACH(s, &hostdevs, next) {
1799 if (unconnected == 0) {
1800 /* nothing to watch */
1801 if (usb_auto_timer) {
1802 qemu_del_timer(usb_auto_timer);
1803 trace_usb_host_auto_scan_disabled();
1808 if (!usb_auto_timer) {
1809 usb_auto_timer = qemu_new_timer_ms(rt_clock, usb_host_auto_check, NULL);
1810 if (!usb_auto_timer) {
1813 trace_usb_host_auto_scan_enabled();
1815 qemu_mod_timer(usb_auto_timer, qemu_get_clock_ms(rt_clock) + 2000);
1819 * Autoconnect filter
1821 * auto:bus:dev[:vid:pid]
1822 * auto:bus.dev[:vid:pid]
1824 * bus - bus number (dec, * means any)
1825 * dev - device number (dec, * means any)
1826 * vid - vendor id (hex, * means any)
1827 * pid - product id (hex, * means any)
1829 * See 'lsusb' output.
1831 static int parse_filter(const char *spec, struct USBAutoFilter *f)
1833 enum { BUS, DEV, VID, PID, DONE };
1834 const char *p = spec;
1842 for (i = BUS; i < DONE; i++) {
1843 p = strpbrk(p, ":.");
1853 case BUS: f->bus_num = strtol(p, NULL, 10); break;
1854 case DEV: f->addr = strtol(p, NULL, 10); break;
1855 case VID: f->vendor_id = strtol(p, NULL, 16); break;
1856 case PID: f->product_id = strtol(p, NULL, 16); break;
1861 fprintf(stderr, "husb: invalid auto filter spec %s\n", spec);
1868 /**********************/
1869 /* USB host device info */
1871 struct usb_class_info {
1873 const char *class_name;
1876 static const struct usb_class_info usb_class_info[] = {
1877 { USB_CLASS_AUDIO, "Audio"},
1878 { USB_CLASS_COMM, "Communication"},
1879 { USB_CLASS_HID, "HID"},
1880 { USB_CLASS_HUB, "Hub" },
1881 { USB_CLASS_PHYSICAL, "Physical" },
1882 { USB_CLASS_PRINTER, "Printer" },
1883 { USB_CLASS_MASS_STORAGE, "Storage" },
1884 { USB_CLASS_CDC_DATA, "Data" },
1885 { USB_CLASS_APP_SPEC, "Application Specific" },
1886 { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
1887 { USB_CLASS_STILL_IMAGE, "Still Image" },
1888 { USB_CLASS_CSCID, "Smart Card" },
1889 { USB_CLASS_CONTENT_SEC, "Content Security" },
1893 static const char *usb_class_str(uint8_t class)
1895 const struct usb_class_info *p;
1896 for(p = usb_class_info; p->class != -1; p++) {
1897 if (p->class == class) {
1901 return p->class_name;
1904 static void usb_info_device(Monitor *mon, int bus_num, int addr, char *port,
1905 int class_id, int vendor_id, int product_id,
1906 const char *product_name,
1909 const char *class_str, *speed_str;
1915 case USB_SPEED_FULL:
1918 case USB_SPEED_HIGH:
1921 case USB_SPEED_SUPER:
1929 monitor_printf(mon, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
1930 bus_num, addr, port, speed_str);
1931 class_str = usb_class_str(class_id);
1933 monitor_printf(mon, " %s:", class_str);
1935 monitor_printf(mon, " Class %02x:", class_id);
1937 monitor_printf(mon, " USB device %04x:%04x", vendor_id, product_id);
1938 if (product_name[0] != '\0') {
1939 monitor_printf(mon, ", %s", product_name);
1941 monitor_printf(mon, "\n");
1944 static int usb_host_info_device(void *opaque, int bus_num, int addr,
1945 char *path, int class_id,
1946 int vendor_id, int product_id,
1947 const char *product_name,
1950 Monitor *mon = opaque;
1952 usb_info_device(mon, bus_num, addr, path, class_id, vendor_id, product_id,
1953 product_name, speed);
1957 static void dec2str(int val, char *str, size_t size)
1960 snprintf(str, size, "*");
1962 snprintf(str, size, "%d", val);
1966 static void hex2str(int val, char *str, size_t size)
1969 snprintf(str, size, "*");
1971 snprintf(str, size, "%04x", val);
1975 void usb_host_info(Monitor *mon)
1977 struct USBAutoFilter *f;
1978 struct USBHostDevice *s;
1980 usb_host_scan(mon, usb_host_info_device);
1982 if (QTAILQ_EMPTY(&hostdevs)) {
1986 monitor_printf(mon, " Auto filters:\n");
1987 QTAILQ_FOREACH(s, &hostdevs, next) {
1988 char bus[10], addr[10], vid[10], pid[10];
1990 dec2str(f->bus_num, bus, sizeof(bus));
1991 dec2str(f->addr, addr, sizeof(addr));
1992 hex2str(f->vendor_id, vid, sizeof(vid));
1993 hex2str(f->product_id, pid, sizeof(pid));
1994 monitor_printf(mon, " Bus %s, Addr %s, Port %s, ID %s:%s\n",
1995 bus, addr, f->port ? f->port : "*", vid, pid);