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 {
125 uint32_t iso_urb_count;
128 struct endp_data ep_in[MAX_ENDPOINTS];
129 struct endp_data ep_out[MAX_ENDPOINTS];
130 QLIST_HEAD(, AsyncURB) aurbs;
132 /* Host side address */
135 char port[MAX_PORTLEN];
136 struct USBAutoFilter match;
139 QTAILQ_ENTRY(USBHostDevice) next;
142 static QTAILQ_HEAD(, USBHostDevice) hostdevs = QTAILQ_HEAD_INITIALIZER(hostdevs);
144 static int usb_host_close(USBHostDevice *dev);
145 static int parse_filter(const char *spec, struct USBAutoFilter *f);
146 static void usb_host_auto_check(void *unused);
147 static int usb_host_read_file(char *line, size_t line_size,
148 const char *device_file, const char *device_name);
149 static int usb_linux_update_endp_table(USBHostDevice *s);
151 static struct endp_data *get_endp(USBHostDevice *s, int pid, int ep)
153 struct endp_data *eps = pid == USB_TOKEN_IN ? s->ep_in : s->ep_out;
154 assert(pid == USB_TOKEN_IN || pid == USB_TOKEN_OUT);
155 assert(ep > 0 && ep <= MAX_ENDPOINTS);
159 static int is_isoc(USBHostDevice *s, int pid, int ep)
161 return get_endp(s, pid, ep)->type == USBDEVFS_URB_TYPE_ISO;
164 static int is_valid(USBHostDevice *s, int pid, int ep)
166 return get_endp(s, pid, ep)->type != INVALID_EP_TYPE;
169 static int is_halted(USBHostDevice *s, int pid, int ep)
171 return get_endp(s, pid, ep)->halted;
174 static void clear_halt(USBHostDevice *s, int pid, int ep)
176 trace_usb_host_ep_clear_halt(s->bus_num, s->addr, ep);
177 get_endp(s, pid, ep)->halted = 0;
180 static void set_halt(USBHostDevice *s, int pid, int ep)
183 trace_usb_host_ep_set_halt(s->bus_num, s->addr, ep);
184 get_endp(s, pid, ep)->halted = 1;
188 static int is_iso_started(USBHostDevice *s, int pid, int ep)
190 return get_endp(s, pid, ep)->iso_started;
193 static void clear_iso_started(USBHostDevice *s, int pid, int ep)
195 trace_usb_host_ep_stop_iso(s->bus_num, s->addr, ep);
196 get_endp(s, pid, ep)->iso_started = 0;
199 static void set_iso_started(USBHostDevice *s, int pid, int ep)
201 struct endp_data *e = get_endp(s, pid, ep);
203 trace_usb_host_ep_start_iso(s->bus_num, s->addr, ep);
204 if (!e->iso_started) {
210 static int change_iso_inflight(USBHostDevice *s, int pid, int ep, int value)
212 struct endp_data *e = get_endp(s, pid, ep);
214 e->inflight += value;
218 static void set_iso_urb(USBHostDevice *s, int pid, int ep, AsyncURB *iso_urb)
220 get_endp(s, pid, ep)->iso_urb = iso_urb;
223 static AsyncURB *get_iso_urb(USBHostDevice *s, int pid, int ep)
225 return get_endp(s, pid, ep)->iso_urb;
228 static void set_iso_urb_idx(USBHostDevice *s, int pid, int ep, int i)
230 get_endp(s, pid, ep)->iso_urb_idx = i;
233 static int get_iso_urb_idx(USBHostDevice *s, int pid, int ep)
235 return get_endp(s, pid, ep)->iso_urb_idx;
238 static void set_iso_buffer_used(USBHostDevice *s, int pid, int ep, int i)
240 get_endp(s, pid, ep)->iso_buffer_used = i;
243 static int get_iso_buffer_used(USBHostDevice *s, int pid, int ep)
245 return get_endp(s, pid, ep)->iso_buffer_used;
248 static void set_max_packet_size(USBHostDevice *s, int pid, int ep,
251 int raw = descriptor[4] + (descriptor[5] << 8);
252 int size, microframes;
255 switch ((raw >> 11) & 3) {
256 case 1: microframes = 2; break;
257 case 2: microframes = 3; break;
258 default: microframes = 1; break;
260 get_endp(s, pid, ep)->max_packet_size = size * microframes;
263 static int get_max_packet_size(USBHostDevice *s, int pid, int ep)
265 return get_endp(s, pid, ep)->max_packet_size;
270 * We always allocate iso packet descriptors even for bulk transfers
271 * to simplify allocation and casts.
275 struct usbdevfs_urb urb;
276 struct usbdevfs_iso_packet_desc isocpd[ISO_FRAME_DESC_PER_URB];
278 QLIST_ENTRY(AsyncURB) next;
280 /* For regular async urbs */
282 int more; /* large transfer, more urbs follow */
284 /* For buffered iso handling */
285 int iso_frame_idx; /* -1 means in flight */
288 static AsyncURB *async_alloc(USBHostDevice *s)
290 AsyncURB *aurb = g_malloc0(sizeof(AsyncURB));
292 QLIST_INSERT_HEAD(&s->aurbs, aurb, next);
296 static void async_free(AsyncURB *aurb)
298 QLIST_REMOVE(aurb, next);
302 static void do_disconnect(USBHostDevice *s)
305 usb_host_auto_check(NULL);
308 static void async_complete(void *opaque)
310 USBHostDevice *s = opaque;
317 int r = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &aurb);
319 if (errno == EAGAIN) {
321 fprintf(stderr, "husb: %d iso urbs finished at once\n", urbs);
325 if (errno == ENODEV) {
327 trace_usb_host_disconnect(s->bus_num, s->addr);
333 perror("USBDEVFS_REAPURBNDELAY");
337 DPRINTF("husb: async completed. aurb %p status %d alen %d\n",
338 aurb, aurb->urb.status, aurb->urb.actual_length);
340 /* If this is a buffered iso urb mark it as complete and don't do
341 anything else (it is handled further in usb_host_handle_iso_data) */
342 if (aurb->iso_frame_idx == -1) {
344 int pid = (aurb->urb.endpoint & USB_DIR_IN) ?
345 USB_TOKEN_IN : USB_TOKEN_OUT;
346 int ep = aurb->urb.endpoint & 0xf;
347 if (aurb->urb.status == -EPIPE) {
348 set_halt(s, pid, ep);
350 aurb->iso_frame_idx = 0;
352 inflight = change_iso_inflight(s, pid, ep, -1);
353 if (inflight == 0 && is_iso_started(s, pid, ep)) {
354 fprintf(stderr, "husb: out of buffers for iso stream\n");
360 trace_usb_host_urb_complete(s->bus_num, s->addr, aurb, aurb->urb.status,
361 aurb->urb.actual_length, aurb->more);
364 switch (aurb->urb.status) {
366 p->result += aurb->urb.actual_length;
370 set_halt(s, p->pid, p->devep);
371 p->result = USB_RET_STALL;
375 p->result = USB_RET_NAK;
379 if (aurb->urb.type == USBDEVFS_URB_TYPE_CONTROL) {
380 trace_usb_host_req_complete(s->bus_num, s->addr, p->result);
381 usb_generic_async_ctrl_complete(&s->dev, p);
382 } else if (!aurb->more) {
383 trace_usb_host_req_complete(s->bus_num, s->addr, p->result);
384 usb_packet_complete(&s->dev, p);
392 static void usb_host_async_cancel(USBDevice *dev, USBPacket *p)
394 USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
397 QLIST_FOREACH(aurb, &s->aurbs, next) {
398 if (p != aurb->packet) {
402 DPRINTF("husb: async cancel: packet %p, aurb %p\n", p, aurb);
404 /* Mark it as dead (see async_complete above) */
407 int r = ioctl(s->fd, USBDEVFS_DISCARDURB, aurb);
409 DPRINTF("husb: async. discard urb failed errno %d\n", errno);
414 static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration)
416 const char *op = NULL;
417 int dev_descr_len, config_descr_len;
418 int interface, nb_interfaces;
421 if (configuration == 0) { /* address state - ignore */
422 dev->ninterfaces = 0;
423 dev->configuration = 0;
427 DPRINTF("husb: claiming interfaces. config %d\n", configuration);
430 dev_descr_len = dev->descr[0];
431 if (dev_descr_len > dev->descr_len) {
432 fprintf(stderr, "husb: update iface failed. descr too short\n");
437 while (i < dev->descr_len) {
438 DPRINTF("husb: i is %d, descr_len is %d, dl %d, dt %d\n",
440 dev->descr[i], dev->descr[i+1]);
442 if (dev->descr[i+1] != USB_DT_CONFIG) {
446 config_descr_len = dev->descr[i];
448 DPRINTF("husb: config #%d need %d\n", dev->descr[i + 5], configuration);
450 if (configuration == dev->descr[i + 5]) {
451 configuration = dev->descr[i + 5];
455 i += config_descr_len;
458 if (i >= dev->descr_len) {
460 "husb: update iface failed. no matching configuration\n");
463 nb_interfaces = dev->descr[i + 4];
465 #ifdef USBDEVFS_DISCONNECT
466 /* earlier Linux 2.4 do not support that */
468 struct usbdevfs_ioctl ctrl;
469 for (interface = 0; interface < nb_interfaces; interface++) {
470 ctrl.ioctl_code = USBDEVFS_DISCONNECT;
471 ctrl.ifno = interface;
473 op = "USBDEVFS_DISCONNECT";
474 ret = ioctl(dev->fd, USBDEVFS_IOCTL, &ctrl);
475 if (ret < 0 && errno != ENODATA) {
482 /* XXX: only grab if all interfaces are free */
483 for (interface = 0; interface < nb_interfaces; interface++) {
484 op = "USBDEVFS_CLAIMINTERFACE";
485 ret = ioctl(dev->fd, USBDEVFS_CLAIMINTERFACE, &interface);
491 trace_usb_host_claim_interfaces(dev->bus_num, dev->addr,
492 nb_interfaces, configuration);
494 dev->ninterfaces = nb_interfaces;
495 dev->configuration = configuration;
499 if (errno == ENODEV) {
506 static int usb_host_release_interfaces(USBHostDevice *s)
510 trace_usb_host_release_interfaces(s->bus_num, s->addr);
512 for (i = 0; i < s->ninterfaces; i++) {
513 ret = ioctl(s->fd, USBDEVFS_RELEASEINTERFACE, &i);
515 perror("USBDEVFS_RELEASEINTERFACE");
522 static void usb_host_handle_reset(USBDevice *dev)
524 USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
526 trace_usb_host_reset(s->bus_num, s->addr);
528 ioctl(s->fd, USBDEVFS_RESET);
530 usb_host_claim_interfaces(s, 0);
531 usb_linux_update_endp_table(s);
534 static void usb_host_handle_destroy(USBDevice *dev)
536 USBHostDevice *s = (USBHostDevice *)dev;
539 if (s->hub_fd != -1) {
542 QTAILQ_REMOVE(&hostdevs, s, next);
543 qemu_remove_exit_notifier(&s->exit);
546 /* iso data is special, we need to keep enough urbs in flight to make sure
547 that the controller never runs out of them, otherwise the device will
548 likely suffer a buffer underrun / overrun. */
549 static AsyncURB *usb_host_alloc_iso(USBHostDevice *s, int pid, uint8_t ep)
552 int i, j, len = get_max_packet_size(s, pid, ep);
554 aurb = g_malloc0(s->iso_urb_count * sizeof(*aurb));
555 for (i = 0; i < s->iso_urb_count; i++) {
556 aurb[i].urb.endpoint = ep;
557 aurb[i].urb.buffer_length = ISO_FRAME_DESC_PER_URB * len;
558 aurb[i].urb.buffer = g_malloc(aurb[i].urb.buffer_length);
559 aurb[i].urb.type = USBDEVFS_URB_TYPE_ISO;
560 aurb[i].urb.flags = USBDEVFS_URB_ISO_ASAP;
561 aurb[i].urb.number_of_packets = ISO_FRAME_DESC_PER_URB;
562 for (j = 0 ; j < ISO_FRAME_DESC_PER_URB; j++)
563 aurb[i].urb.iso_frame_desc[j].length = len;
564 if (pid == USB_TOKEN_IN) {
565 aurb[i].urb.endpoint |= 0x80;
566 /* Mark as fully consumed (idle) */
567 aurb[i].iso_frame_idx = ISO_FRAME_DESC_PER_URB;
570 set_iso_urb(s, pid, ep, aurb);
575 static void usb_host_stop_n_free_iso(USBHostDevice *s, int pid, uint8_t ep)
578 int i, ret, killed = 0, free = 1;
580 aurb = get_iso_urb(s, pid, ep);
585 for (i = 0; i < s->iso_urb_count; i++) {
587 if (aurb[i].iso_frame_idx == -1) {
588 ret = ioctl(s->fd, USBDEVFS_DISCARDURB, &aurb[i]);
590 perror("USBDEVFS_DISCARDURB");
598 /* Make sure any urbs we've killed are reaped before we free them */
603 for (i = 0; i < s->iso_urb_count; i++) {
604 g_free(aurb[i].urb.buffer);
610 printf("husb: leaking iso urbs because of discard failure\n");
611 set_iso_urb(s, pid, ep, NULL);
612 set_iso_urb_idx(s, pid, ep, 0);
613 clear_iso_started(s, pid, ep);
616 static int urb_status_to_usb_ret(int status)
620 return USB_RET_STALL;
626 static int usb_host_handle_iso_data(USBHostDevice *s, USBPacket *p, int in)
629 int i, j, ret, max_packet_size, offset, len = 0;
632 max_packet_size = get_max_packet_size(s, p->pid, p->devep);
633 if (max_packet_size == 0)
636 aurb = get_iso_urb(s, p->pid, p->devep);
638 aurb = usb_host_alloc_iso(s, p->pid, p->devep);
641 i = get_iso_urb_idx(s, p->pid, p->devep);
642 j = aurb[i].iso_frame_idx;
643 if (j >= 0 && j < ISO_FRAME_DESC_PER_URB) {
645 /* Check urb status */
646 if (aurb[i].urb.status) {
647 len = urb_status_to_usb_ret(aurb[i].urb.status);
648 /* Move to the next urb */
649 aurb[i].iso_frame_idx = ISO_FRAME_DESC_PER_URB - 1;
650 /* Check frame status */
651 } else if (aurb[i].urb.iso_frame_desc[j].status) {
652 len = urb_status_to_usb_ret(
653 aurb[i].urb.iso_frame_desc[j].status);
654 /* Check the frame fits */
655 } else if (aurb[i].urb.iso_frame_desc[j].actual_length
657 printf("husb: received iso data is larger then packet\n");
659 /* All good copy data over */
661 len = aurb[i].urb.iso_frame_desc[j].actual_length;
662 buf = aurb[i].urb.buffer +
663 j * aurb[i].urb.iso_frame_desc[0].length;
664 usb_packet_copy(p, buf, len);
668 offset = (j == 0) ? 0 : get_iso_buffer_used(s, p->pid, p->devep);
670 /* Check the frame fits */
671 if (len > max_packet_size) {
672 printf("husb: send iso data is larger then max packet size\n");
676 /* All good copy data over */
677 usb_packet_copy(p, aurb[i].urb.buffer + offset, len);
678 aurb[i].urb.iso_frame_desc[j].length = len;
680 set_iso_buffer_used(s, p->pid, p->devep, offset);
682 /* Start the stream once we have buffered enough data */
683 if (!is_iso_started(s, p->pid, p->devep) && i == 1 && j == 8) {
684 set_iso_started(s, p->pid, p->devep);
687 aurb[i].iso_frame_idx++;
688 if (aurb[i].iso_frame_idx == ISO_FRAME_DESC_PER_URB) {
689 i = (i + 1) % s->iso_urb_count;
690 set_iso_urb_idx(s, p->pid, p->devep, i);
694 set_iso_started(s, p->pid, p->devep);
696 DPRINTF("hubs: iso out error no free buffer, dropping packet\n");
700 if (is_iso_started(s, p->pid, p->devep)) {
701 /* (Re)-submit all fully consumed / filled urbs */
702 for (i = 0; i < s->iso_urb_count; i++) {
703 if (aurb[i].iso_frame_idx == ISO_FRAME_DESC_PER_URB) {
704 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, &aurb[i]);
706 perror("USBDEVFS_SUBMITURB");
707 if (!in || len == 0) {
719 aurb[i].iso_frame_idx = -1;
720 change_iso_inflight(s, p->pid, p->devep, 1);
728 static int usb_host_handle_data(USBDevice *dev, USBPacket *p)
730 USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
731 struct usbdevfs_urb *urb;
733 int ret, rem, prem, v;
737 trace_usb_host_req_data(s->bus_num, s->addr,
738 p->pid == USB_TOKEN_IN,
739 p->devep, p->iov.size);
741 if (!is_valid(s, p->pid, p->devep)) {
742 trace_usb_host_req_complete(s->bus_num, s->addr, USB_RET_NAK);
746 if (p->pid == USB_TOKEN_IN) {
747 ep = p->devep | 0x80;
752 if (is_halted(s, p->pid, p->devep)) {
753 unsigned int arg = ep;
754 ret = ioctl(s->fd, USBDEVFS_CLEAR_HALT, &arg);
756 perror("USBDEVFS_CLEAR_HALT");
757 trace_usb_host_req_complete(s->bus_num, s->addr, USB_RET_NAK);
760 clear_halt(s, p->pid, p->devep);
763 if (is_isoc(s, p->pid, p->devep)) {
764 return usb_host_handle_iso_data(s, p, p->pid == USB_TOKEN_IN);
768 prem = p->iov.iov[v].iov_len;
769 pbuf = p->iov.iov[v].iov_base;
774 assert(v < p->iov.niov);
775 prem = p->iov.iov[v].iov_len;
776 pbuf = p->iov.iov[v].iov_base;
779 aurb = async_alloc(s);
784 urb->type = USBDEVFS_URB_TYPE_BULK;
785 urb->usercontext = s;
787 urb->buffer_length = prem;
789 if (urb->buffer_length > MAX_USBFS_BUFFER_SIZE) {
790 urb->buffer_length = MAX_USBFS_BUFFER_SIZE;
792 pbuf += urb->buffer_length;
793 prem -= urb->buffer_length;
794 rem -= urb->buffer_length;
799 trace_usb_host_urb_submit(s->bus_num, s->addr, aurb,
800 urb->buffer_length, aurb->more);
801 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
803 DPRINTF("husb: data submit: ep 0x%x, len %u, more %d, packet %p, aurb %p\n",
804 urb->endpoint, urb->buffer_length, aurb->more, p, aurb);
807 perror("USBDEVFS_SUBMITURB");
812 trace_usb_host_req_complete(s->bus_num, s->addr, USB_RET_NAK);
816 trace_usb_host_req_complete(s->bus_num, s->addr, USB_RET_STALL);
817 return USB_RET_STALL;
822 return USB_RET_ASYNC;
825 static int ctrl_error(void)
827 if (errno == ETIMEDOUT) {
830 return USB_RET_STALL;
834 static int usb_host_set_address(USBHostDevice *s, int addr)
836 trace_usb_host_set_address(s->bus_num, s->addr, addr);
841 static int usb_host_set_config(USBHostDevice *s, int config)
843 trace_usb_host_set_config(s->bus_num, s->addr, config);
845 usb_host_release_interfaces(s);
847 int ret = ioctl(s->fd, USBDEVFS_SETCONFIGURATION, &config);
849 DPRINTF("husb: ctrl set config %d ret %d errno %d\n", config, ret, errno);
854 usb_host_claim_interfaces(s, config);
855 usb_linux_update_endp_table(s);
859 static int usb_host_set_interface(USBHostDevice *s, int iface, int alt)
861 struct usbdevfs_setinterface si;
864 trace_usb_host_set_interface(s->bus_num, s->addr, iface, alt);
866 for (i = 1; i <= MAX_ENDPOINTS; i++) {
867 if (is_isoc(s, USB_TOKEN_IN, i)) {
868 usb_host_stop_n_free_iso(s, USB_TOKEN_IN, i);
870 if (is_isoc(s, USB_TOKEN_OUT, i)) {
871 usb_host_stop_n_free_iso(s, USB_TOKEN_OUT, i);
875 si.interface = iface;
877 ret = ioctl(s->fd, USBDEVFS_SETINTERFACE, &si);
879 DPRINTF("husb: ctrl set iface %d altset %d ret %d errno %d\n",
880 iface, alt, ret, errno);
885 usb_linux_update_endp_table(s);
889 static int usb_host_handle_control(USBDevice *dev, USBPacket *p,
890 int request, int value, int index, int length, uint8_t *data)
892 USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
893 struct usbdevfs_urb *urb;
898 * Process certain standard device requests.
899 * These are infrequent and are processed synchronously.
902 /* Note request is (bRequestType << 8) | bRequest */
903 trace_usb_host_req_control(s->bus_num, s->addr, request, value, index);
906 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
907 return usb_host_set_address(s, value);
909 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
910 return usb_host_set_config(s, value & 0xff);
912 case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
913 return usb_host_set_interface(s, index, value);
916 /* The rest are asynchronous */
918 if (length > sizeof(dev->data_buf)) {
919 fprintf(stderr, "husb: ctrl buffer too small (%d > %zu)\n",
920 length, sizeof(dev->data_buf));
921 return USB_RET_STALL;
924 aurb = async_alloc(s);
928 * Setup ctrl transfer.
930 * s->ctrl is laid out such that data buffer immediately follows
931 * 'req' struct which is exactly what usbdevfs expects.
935 urb->type = USBDEVFS_URB_TYPE_CONTROL;
936 urb->endpoint = p->devep;
938 urb->buffer = &dev->setup_buf;
939 urb->buffer_length = length + 8;
941 urb->usercontext = s;
943 trace_usb_host_urb_submit(s->bus_num, s->addr, aurb,
944 urb->buffer_length, aurb->more);
945 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
947 DPRINTF("husb: submit ctrl. len %u aurb %p\n", urb->buffer_length, aurb);
950 DPRINTF("husb: submit failed. errno %d\n", errno);
958 return USB_RET_STALL;
962 return USB_RET_ASYNC;
965 static uint8_t usb_linux_get_alt_setting(USBHostDevice *s,
966 uint8_t configuration, uint8_t interface)
969 struct usb_ctrltransfer ct;
972 if (usb_fs_type == USB_FS_SYS) {
973 char device_name[64], line[1024];
976 sprintf(device_name, "%d-%s:%d.%d", s->bus_num, s->port,
977 (int)configuration, (int)interface);
979 if (!usb_host_read_file(line, sizeof(line), "bAlternateSetting",
983 if (sscanf(line, "%d", &alt_setting) != 1) {
990 ct.bRequestType = USB_DIR_IN | USB_RECIP_INTERFACE;
991 ct.bRequest = USB_REQ_GET_INTERFACE;
993 ct.wIndex = interface;
995 ct.data = &alt_setting;
997 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
999 /* Assume alt 0 on error */
1006 /* returns 1 on problem encountered or 0 for success */
1007 static int usb_linux_update_endp_table(USBHostDevice *s)
1009 uint8_t *descriptors;
1010 uint8_t devep, type, alt_interface;
1011 int interface, length, i, ep, pid;
1012 struct endp_data *epd;
1014 for (i = 0; i < MAX_ENDPOINTS; i++) {
1015 s->ep_in[i].type = INVALID_EP_TYPE;
1016 s->ep_out[i].type = INVALID_EP_TYPE;
1019 if (s->configuration == 0) {
1020 /* not configured yet -- leave all endpoints disabled */
1024 /* get the desired configuration, interface, and endpoint descriptors
1025 * from device description */
1026 descriptors = &s->descr[18];
1027 length = s->descr_len - 18;
1030 if (descriptors[i + 1] != USB_DT_CONFIG ||
1031 descriptors[i + 5] != s->configuration) {
1032 fprintf(stderr, "invalid descriptor data - configuration %d\n",
1036 i += descriptors[i];
1038 while (i < length) {
1039 if (descriptors[i + 1] != USB_DT_INTERFACE ||
1040 (descriptors[i + 1] == USB_DT_INTERFACE &&
1041 descriptors[i + 4] == 0)) {
1042 i += descriptors[i];
1046 interface = descriptors[i + 2];
1047 alt_interface = usb_linux_get_alt_setting(s, s->configuration,
1050 /* the current interface descriptor is the active interface
1051 * and has endpoints */
1052 if (descriptors[i + 3] != alt_interface) {
1053 i += descriptors[i];
1057 /* advance to the endpoints */
1058 while (i < length && descriptors[i +1] != USB_DT_ENDPOINT) {
1059 i += descriptors[i];
1065 while (i < length) {
1066 if (descriptors[i + 1] != USB_DT_ENDPOINT) {
1070 devep = descriptors[i + 2];
1071 pid = (devep & USB_DIR_IN) ? USB_TOKEN_IN : USB_TOKEN_OUT;
1074 fprintf(stderr, "usb-linux: invalid ep descriptor, ep == 0\n");
1078 switch (descriptors[i + 3] & 0x3) {
1080 type = USBDEVFS_URB_TYPE_CONTROL;
1083 type = USBDEVFS_URB_TYPE_ISO;
1084 set_max_packet_size(s, pid, ep, descriptors + i);
1087 type = USBDEVFS_URB_TYPE_BULK;
1090 type = USBDEVFS_URB_TYPE_INTERRUPT;
1093 DPRINTF("usb_host: malformed endpoint type\n");
1094 type = USBDEVFS_URB_TYPE_BULK;
1096 epd = get_endp(s, pid, ep);
1097 assert(epd->type == INVALID_EP_TYPE);
1101 i += descriptors[i];
1108 * Check if we can safely redirect a usb2 device to a usb1 virtual controller,
1109 * this function assumes this is safe, if:
1110 * 1) There are no isoc endpoints
1111 * 2) There are no interrupt endpoints with a max_packet_size > 64
1112 * Note bulk endpoints with a max_packet_size > 64 in theory also are not
1113 * usb1 compatible, but in practice this seems to work fine.
1115 static int usb_linux_full_speed_compat(USBHostDevice *dev)
1120 * usb_linux_update_endp_table only registers info about ep in the current
1121 * interface altsettings, so we need to parse the descriptors again.
1123 for (i = 0; (i + 5) < dev->descr_len; i += dev->descr[i]) {
1124 if (dev->descr[i + 1] == USB_DT_ENDPOINT) {
1125 switch (dev->descr[i + 3] & 0x3) {
1126 case 0x00: /* CONTROL */
1128 case 0x01: /* ISO */
1130 case 0x02: /* BULK */
1132 case 0x03: /* INTERRUPT */
1133 packet_size = dev->descr[i + 4] + (dev->descr[i + 5] << 8);
1134 if (packet_size > 64)
1143 static int usb_host_open(USBHostDevice *dev, int bus_num,
1144 int addr, char *port, const char *prod_name, int speed)
1149 trace_usb_host_open_started(bus_num, addr);
1151 if (dev->fd != -1) {
1155 if (!usb_host_device_path) {
1156 perror("husb: USB Host Device Path not set");
1159 snprintf(buf, sizeof(buf), "%s/%03d/%03d", usb_host_device_path,
1161 fd = open(buf, O_RDWR | O_NONBLOCK);
1166 DPRINTF("husb: opened %s\n", buf);
1168 dev->bus_num = bus_num;
1170 strcpy(dev->port, port);
1173 /* read the device description */
1174 dev->descr_len = read(fd, dev->descr, sizeof(dev->descr));
1175 if (dev->descr_len <= 0) {
1176 perror("husb: reading device data failed");
1183 printf("=== begin dumping device descriptor data ===\n");
1184 for (x = 0; x < dev->descr_len; x++) {
1185 printf("%02x ", dev->descr[x]);
1187 printf("\n=== end dumping device descriptor data ===\n");
1192 /* start unconfigured -- we'll wait for the guest to set a configuration */
1193 if (!usb_host_claim_interfaces(dev, 0)) {
1197 ret = usb_linux_update_endp_table(dev);
1203 struct usbdevfs_connectinfo ci;
1205 ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci);
1207 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
1212 speed = USB_SPEED_LOW;
1214 speed = USB_SPEED_HIGH;
1217 dev->dev.speed = speed;
1218 dev->dev.speedmask = (1 << speed);
1219 if (dev->dev.speed == USB_SPEED_HIGH && usb_linux_full_speed_compat(dev)) {
1220 dev->dev.speedmask |= USB_SPEED_MASK_FULL;
1223 trace_usb_host_open_success(bus_num, addr);
1225 if (!prod_name || prod_name[0] == '\0') {
1226 snprintf(dev->dev.product_desc, sizeof(dev->dev.product_desc),
1227 "host:%d.%d", bus_num, addr);
1229 pstrcpy(dev->dev.product_desc, sizeof(dev->dev.product_desc),
1233 ret = usb_device_attach(&dev->dev);
1238 /* USB devio uses 'write' flag to check for async completions */
1239 qemu_set_fd_handler(dev->fd, NULL, async_complete, dev);
1244 trace_usb_host_open_failure(bus_num, addr);
1245 if (dev->fd != -1) {
1252 static int usb_host_close(USBHostDevice *dev)
1256 if (dev->fd == -1 || !dev->dev.attached) {
1260 trace_usb_host_close(dev->bus_num, dev->addr);
1262 qemu_set_fd_handler(dev->fd, NULL, NULL, NULL);
1264 for (i = 1; i <= MAX_ENDPOINTS; i++) {
1265 if (is_isoc(dev, USB_TOKEN_IN, i)) {
1266 usb_host_stop_n_free_iso(dev, USB_TOKEN_IN, i);
1268 if (is_isoc(dev, USB_TOKEN_OUT, i)) {
1269 usb_host_stop_n_free_iso(dev, USB_TOKEN_OUT, i);
1272 async_complete(dev);
1274 usb_device_detach(&dev->dev);
1275 ioctl(dev->fd, USBDEVFS_RESET);
1281 static void usb_host_exit_notifier(struct Notifier *n, void *data)
1283 USBHostDevice *s = container_of(n, USBHostDevice, exit);
1286 ioctl(s->fd, USBDEVFS_RESET);
1290 static int usb_host_initfn(USBDevice *dev)
1292 USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
1294 dev->auto_attach = 0;
1298 QTAILQ_INSERT_TAIL(&hostdevs, s, next);
1299 s->exit.notify = usb_host_exit_notifier;
1300 qemu_add_exit_notifier(&s->exit);
1301 usb_host_auto_check(NULL);
1303 #ifdef USBDEVFS_CLAIM_PORT
1304 if (s->match.bus_num != 0 && s->match.port != NULL) {
1305 char *h, hub_name[64], line[1024];
1306 int hub_addr, portnr, ret;
1308 snprintf(hub_name, sizeof(hub_name), "%d-%s",
1309 s->match.bus_num, s->match.port);
1311 /* try strip off last ".$portnr" to get hub */
1312 h = strrchr(hub_name, '.');
1317 /* no dot in there -> it is the root hub */
1318 snprintf(hub_name, sizeof(hub_name), "usb%d",
1320 portnr = atoi(s->match.port);
1323 if (!usb_host_read_file(line, sizeof(line), "devnum",
1327 if (sscanf(line, "%d", &hub_addr) != 1) {
1331 if (!usb_host_device_path) {
1334 snprintf(line, sizeof(line), "%s/%03d/%03d",
1335 usb_host_device_path, s->match.bus_num, hub_addr);
1336 s->hub_fd = open(line, O_RDWR | O_NONBLOCK);
1337 if (s->hub_fd < 0) {
1341 ret = ioctl(s->hub_fd, USBDEVFS_CLAIM_PORT, &portnr);
1348 trace_usb_host_claim_port(s->match.bus_num, hub_addr, portnr);
1356 static struct USBDeviceInfo usb_host_dev_info = {
1357 .product_desc = "USB Host Device",
1358 .qdev.name = "usb-host",
1359 .qdev.size = sizeof(USBHostDevice),
1360 .init = usb_host_initfn,
1361 .handle_packet = usb_generic_handle_packet,
1362 .cancel_packet = usb_host_async_cancel,
1363 .handle_data = usb_host_handle_data,
1364 .handle_control = usb_host_handle_control,
1365 .handle_reset = usb_host_handle_reset,
1366 .handle_destroy = usb_host_handle_destroy,
1367 .usbdevice_name = "host",
1368 .usbdevice_init = usb_host_device_open,
1369 .qdev.props = (Property[]) {
1370 DEFINE_PROP_UINT32("hostbus", USBHostDevice, match.bus_num, 0),
1371 DEFINE_PROP_UINT32("hostaddr", USBHostDevice, match.addr, 0),
1372 DEFINE_PROP_STRING("hostport", USBHostDevice, match.port),
1373 DEFINE_PROP_HEX32("vendorid", USBHostDevice, match.vendor_id, 0),
1374 DEFINE_PROP_HEX32("productid", USBHostDevice, match.product_id, 0),
1375 DEFINE_PROP_UINT32("isobufs", USBHostDevice, iso_urb_count, 4),
1376 DEFINE_PROP_END_OF_LIST(),
1380 static void usb_host_register_devices(void)
1382 usb_qdev_register(&usb_host_dev_info);
1384 device_init(usb_host_register_devices)
1386 USBDevice *usb_host_device_open(const char *devname)
1388 struct USBAutoFilter filter;
1392 dev = usb_create(NULL /* FIXME */, "usb-host");
1394 if (strstr(devname, "auto:")) {
1395 if (parse_filter(devname, &filter) < 0) {
1399 if ((p = strchr(devname, '.'))) {
1400 filter.bus_num = strtoul(devname, NULL, 0);
1401 filter.addr = strtoul(p + 1, NULL, 0);
1402 filter.vendor_id = 0;
1403 filter.product_id = 0;
1404 } else if ((p = strchr(devname, ':'))) {
1407 filter.vendor_id = strtoul(devname, NULL, 16);
1408 filter.product_id = strtoul(p + 1, NULL, 16);
1414 qdev_prop_set_uint32(&dev->qdev, "hostbus", filter.bus_num);
1415 qdev_prop_set_uint32(&dev->qdev, "hostaddr", filter.addr);
1416 qdev_prop_set_uint32(&dev->qdev, "vendorid", filter.vendor_id);
1417 qdev_prop_set_uint32(&dev->qdev, "productid", filter.product_id);
1418 qdev_init_nofail(&dev->qdev);
1422 qdev_free(&dev->qdev);
1426 int usb_host_device_close(const char *devname)
1429 char product_name[PRODUCT_NAME_SZ];
1433 if (strstr(devname, "auto:")) {
1434 return usb_host_auto_del(devname);
1436 if (usb_host_find_device(&bus_num, &addr, product_name,
1437 sizeof(product_name), devname) < 0) {
1440 s = hostdev_find(bus_num, addr);
1442 usb_device_delete_addr(s->bus_num, s->dev.addr);
1450 static int get_tag_value(char *buf, int buf_size,
1451 const char *str, const char *tag,
1452 const char *stopchars)
1456 p = strstr(str, tag);
1461 while (qemu_isspace(*p)) {
1465 while (*p != '\0' && !strchr(stopchars, *p)) {
1466 if ((q - buf) < (buf_size - 1)) {
1476 * Use /proc/bus/usb/devices or /dev/bus/usb/devices file to determine
1477 * host's USB devices. This is legacy support since many distributions
1478 * are moving to /sys/bus/usb
1480 static int usb_host_scan_dev(void *opaque, USBScanFunc *func)
1485 int bus_num, addr, speed, device_count, class_id, product_id, vendor_id;
1486 char product_name[512];
1489 if (!usb_host_device_path) {
1490 perror("husb: USB Host Device Path not set");
1493 snprintf(line, sizeof(line), "%s/devices", usb_host_device_path);
1494 f = fopen(line, "r");
1496 perror("husb: cannot open devices file");
1501 bus_num = addr = class_id = product_id = vendor_id = 0;
1502 speed = -1; /* Can't get the speed from /[proc|dev]/bus/usb/devices */
1504 if (fgets(line, sizeof(line), f) == NULL) {
1507 if (strlen(line) > 0) {
1508 line[strlen(line) - 1] = '\0';
1510 if (line[0] == 'T' && line[1] == ':') {
1511 if (device_count && (vendor_id || product_id)) {
1512 /* New device. Add the previously discovered device. */
1513 ret = func(opaque, bus_num, addr, 0, class_id, vendor_id,
1514 product_id, product_name, speed);
1519 if (get_tag_value(buf, sizeof(buf), line, "Bus=", " ") < 0) {
1522 bus_num = atoi(buf);
1523 if (get_tag_value(buf, sizeof(buf), line, "Dev#=", " ") < 0) {
1527 if (get_tag_value(buf, sizeof(buf), line, "Spd=", " ") < 0) {
1530 if (!strcmp(buf, "5000")) {
1531 speed = USB_SPEED_SUPER;
1532 } else if (!strcmp(buf, "480")) {
1533 speed = USB_SPEED_HIGH;
1534 } else if (!strcmp(buf, "1.5")) {
1535 speed = USB_SPEED_LOW;
1537 speed = USB_SPEED_FULL;
1539 product_name[0] = '\0';
1544 } else if (line[0] == 'P' && line[1] == ':') {
1545 if (get_tag_value(buf, sizeof(buf), line, "Vendor=", " ") < 0) {
1548 vendor_id = strtoul(buf, NULL, 16);
1549 if (get_tag_value(buf, sizeof(buf), line, "ProdID=", " ") < 0) {
1552 product_id = strtoul(buf, NULL, 16);
1553 } else if (line[0] == 'S' && line[1] == ':') {
1554 if (get_tag_value(buf, sizeof(buf), line, "Product=", "") < 0) {
1557 pstrcpy(product_name, sizeof(product_name), buf);
1558 } else if (line[0] == 'D' && line[1] == ':') {
1559 if (get_tag_value(buf, sizeof(buf), line, "Cls=", " (") < 0) {
1562 class_id = strtoul(buf, NULL, 16);
1566 if (device_count && (vendor_id || product_id)) {
1567 /* Add the last device. */
1568 ret = func(opaque, bus_num, addr, 0, class_id, vendor_id,
1569 product_id, product_name, speed);
1579 * Read sys file-system device file
1581 * @line address of buffer to put file contents in
1582 * @line_size size of line
1583 * @device_file path to device file (printf format string)
1584 * @device_name device being opened (inserted into device_file)
1586 * @return 0 failed, 1 succeeded ('line' contains data)
1588 static int usb_host_read_file(char *line, size_t line_size,
1589 const char *device_file, const char *device_name)
1593 char filename[PATH_MAX];
1595 snprintf(filename, PATH_MAX, USBSYSBUS_PATH "/devices/%s/%s", device_name,
1597 f = fopen(filename, "r");
1599 ret = fgets(line, line_size, f) != NULL;
1607 * Use /sys/bus/usb/devices/ directory to determine host's USB
1610 * This code is based on Robert Schiele's original patches posted to
1611 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1613 static int usb_host_scan_sys(void *opaque, USBScanFunc *func)
1617 int bus_num, addr, speed, class_id, product_id, vendor_id;
1619 char port[MAX_PORTLEN];
1620 char product_name[512];
1623 dir = opendir(USBSYSBUS_PATH "/devices");
1625 perror("husb: cannot open devices directory");
1629 while ((de = readdir(dir))) {
1630 if (de->d_name[0] != '.' && !strchr(de->d_name, ':')) {
1631 if (sscanf(de->d_name, "%d-%7[0-9.]", &bus_num, port) < 2) {
1635 if (!usb_host_read_file(line, sizeof(line), "devnum", de->d_name)) {
1638 if (sscanf(line, "%d", &addr) != 1) {
1641 if (!usb_host_read_file(line, sizeof(line), "bDeviceClass",
1645 if (sscanf(line, "%x", &class_id) != 1) {
1649 if (!usb_host_read_file(line, sizeof(line), "idVendor",
1653 if (sscanf(line, "%x", &vendor_id) != 1) {
1656 if (!usb_host_read_file(line, sizeof(line), "idProduct",
1660 if (sscanf(line, "%x", &product_id) != 1) {
1663 if (!usb_host_read_file(line, sizeof(line), "product",
1667 if (strlen(line) > 0) {
1668 line[strlen(line) - 1] = '\0';
1670 pstrcpy(product_name, sizeof(product_name), line);
1673 if (!usb_host_read_file(line, sizeof(line), "speed", de->d_name)) {
1676 if (!strcmp(line, "5000\n")) {
1677 speed = USB_SPEED_SUPER;
1678 } else if (!strcmp(line, "480\n")) {
1679 speed = USB_SPEED_HIGH;
1680 } else if (!strcmp(line, "1.5\n")) {
1681 speed = USB_SPEED_LOW;
1683 speed = USB_SPEED_FULL;
1686 ret = func(opaque, bus_num, addr, port, class_id, vendor_id,
1687 product_id, product_name, speed);
1701 * Determine how to access the host's USB devices and call the
1702 * specific support function.
1704 static int usb_host_scan(void *opaque, USBScanFunc *func)
1706 Monitor *mon = cur_mon;
1710 const char *fs_type[] = {"unknown", "proc", "dev", "sys"};
1711 char devpath[PATH_MAX];
1713 /* only check the host once */
1715 dir = opendir(USBSYSBUS_PATH "/devices");
1717 /* devices found in /dev/bus/usb/ (yes - not a mistake!) */
1718 strcpy(devpath, USBDEVBUS_PATH);
1719 usb_fs_type = USB_FS_SYS;
1721 DPRINTF(USBDBG_DEVOPENED, USBSYSBUS_PATH);
1724 f = fopen(USBPROCBUS_PATH "/devices", "r");
1726 /* devices found in /proc/bus/usb/ */
1727 strcpy(devpath, USBPROCBUS_PATH);
1728 usb_fs_type = USB_FS_PROC;
1730 DPRINTF(USBDBG_DEVOPENED, USBPROCBUS_PATH);
1733 /* try additional methods if an access method hasn't been found yet */
1734 f = fopen(USBDEVBUS_PATH "/devices", "r");
1736 /* devices found in /dev/bus/usb/ */
1737 strcpy(devpath, USBDEVBUS_PATH);
1738 usb_fs_type = USB_FS_DEV;
1740 DPRINTF(USBDBG_DEVOPENED, USBDEVBUS_PATH);
1746 monitor_printf(mon, "husb: unable to access USB devices\n");
1751 /* the module setting (used later for opening devices) */
1752 usb_host_device_path = g_malloc0(strlen(devpath)+1);
1753 strcpy(usb_host_device_path, devpath);
1755 monitor_printf(mon, "husb: using %s file-system with %s\n",
1756 fs_type[usb_fs_type], usb_host_device_path);
1760 switch (usb_fs_type) {
1763 ret = usb_host_scan_dev(opaque, func);
1766 ret = usb_host_scan_sys(opaque, func);
1775 static QEMUTimer *usb_auto_timer;
1777 static int usb_host_auto_scan(void *opaque, int bus_num, int addr, char *port,
1778 int class_id, int vendor_id, int product_id,
1779 const char *product_name, int speed)
1781 struct USBAutoFilter *f;
1782 struct USBHostDevice *s;
1788 QTAILQ_FOREACH(s, &hostdevs, next) {
1791 if (f->bus_num > 0 && f->bus_num != bus_num) {
1794 if (f->addr > 0 && f->addr != addr) {
1797 if (f->port != NULL && (port == NULL || strcmp(f->port, port) != 0)) {
1801 if (f->vendor_id > 0 && f->vendor_id != vendor_id) {
1805 if (f->product_id > 0 && f->product_id != product_id) {
1808 /* We got a match */
1810 if (s->errcount >= 3) {
1814 /* Already attached ? */
1818 DPRINTF("husb: auto open: bus_num %d addr %d\n", bus_num, addr);
1820 if (usb_host_open(s, bus_num, addr, port, product_name, speed) < 0) {
1829 static void usb_host_auto_check(void *unused)
1831 struct USBHostDevice *s;
1832 int unconnected = 0;
1834 usb_host_scan(NULL, usb_host_auto_scan);
1836 QTAILQ_FOREACH(s, &hostdevs, next) {
1846 if (unconnected == 0) {
1847 /* nothing to watch */
1848 if (usb_auto_timer) {
1849 qemu_del_timer(usb_auto_timer);
1850 trace_usb_host_auto_scan_disabled();
1855 if (!usb_auto_timer) {
1856 usb_auto_timer = qemu_new_timer_ms(rt_clock, usb_host_auto_check, NULL);
1857 if (!usb_auto_timer) {
1860 trace_usb_host_auto_scan_enabled();
1862 qemu_mod_timer(usb_auto_timer, qemu_get_clock_ms(rt_clock) + 2000);
1866 * Autoconnect filter
1868 * auto:bus:dev[:vid:pid]
1869 * auto:bus.dev[:vid:pid]
1871 * bus - bus number (dec, * means any)
1872 * dev - device number (dec, * means any)
1873 * vid - vendor id (hex, * means any)
1874 * pid - product id (hex, * means any)
1876 * See 'lsusb' output.
1878 static int parse_filter(const char *spec, struct USBAutoFilter *f)
1880 enum { BUS, DEV, VID, PID, DONE };
1881 const char *p = spec;
1889 for (i = BUS; i < DONE; i++) {
1890 p = strpbrk(p, ":.");
1900 case BUS: f->bus_num = strtol(p, NULL, 10); break;
1901 case DEV: f->addr = strtol(p, NULL, 10); break;
1902 case VID: f->vendor_id = strtol(p, NULL, 16); break;
1903 case PID: f->product_id = strtol(p, NULL, 16); break;
1908 fprintf(stderr, "husb: invalid auto filter spec %s\n", spec);
1915 /**********************/
1916 /* USB host device info */
1918 struct usb_class_info {
1920 const char *class_name;
1923 static const struct usb_class_info usb_class_info[] = {
1924 { USB_CLASS_AUDIO, "Audio"},
1925 { USB_CLASS_COMM, "Communication"},
1926 { USB_CLASS_HID, "HID"},
1927 { USB_CLASS_HUB, "Hub" },
1928 { USB_CLASS_PHYSICAL, "Physical" },
1929 { USB_CLASS_PRINTER, "Printer" },
1930 { USB_CLASS_MASS_STORAGE, "Storage" },
1931 { USB_CLASS_CDC_DATA, "Data" },
1932 { USB_CLASS_APP_SPEC, "Application Specific" },
1933 { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
1934 { USB_CLASS_STILL_IMAGE, "Still Image" },
1935 { USB_CLASS_CSCID, "Smart Card" },
1936 { USB_CLASS_CONTENT_SEC, "Content Security" },
1940 static const char *usb_class_str(uint8_t class)
1942 const struct usb_class_info *p;
1943 for(p = usb_class_info; p->class != -1; p++) {
1944 if (p->class == class) {
1948 return p->class_name;
1951 static void usb_info_device(Monitor *mon, int bus_num, int addr, char *port,
1952 int class_id, int vendor_id, int product_id,
1953 const char *product_name,
1956 const char *class_str, *speed_str;
1962 case USB_SPEED_FULL:
1965 case USB_SPEED_HIGH:
1968 case USB_SPEED_SUPER:
1976 monitor_printf(mon, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
1977 bus_num, addr, port, speed_str);
1978 class_str = usb_class_str(class_id);
1980 monitor_printf(mon, " %s:", class_str);
1982 monitor_printf(mon, " Class %02x:", class_id);
1984 monitor_printf(mon, " USB device %04x:%04x", vendor_id, product_id);
1985 if (product_name[0] != '\0') {
1986 monitor_printf(mon, ", %s", product_name);
1988 monitor_printf(mon, "\n");
1991 static int usb_host_info_device(void *opaque, int bus_num, int addr,
1992 char *path, int class_id,
1993 int vendor_id, int product_id,
1994 const char *product_name,
1997 Monitor *mon = opaque;
1999 usb_info_device(mon, bus_num, addr, path, class_id, vendor_id, product_id,
2000 product_name, speed);
2004 static void dec2str(int val, char *str, size_t size)
2007 snprintf(str, size, "*");
2009 snprintf(str, size, "%d", val);
2013 static void hex2str(int val, char *str, size_t size)
2016 snprintf(str, size, "*");
2018 snprintf(str, size, "%04x", val);
2022 void usb_host_info(Monitor *mon)
2024 struct USBAutoFilter *f;
2025 struct USBHostDevice *s;
2027 usb_host_scan(mon, usb_host_info_device);
2029 if (QTAILQ_EMPTY(&hostdevs)) {
2033 monitor_printf(mon, " Auto filters:\n");
2034 QTAILQ_FOREACH(s, &hostdevs, next) {
2035 char bus[10], addr[10], vid[10], pid[10];
2037 dec2str(f->bus_num, bus, sizeof(bus));
2038 dec2str(f->addr, addr, sizeof(addr));
2039 hex2str(f->vendor_id, vid, sizeof(vid));
2040 hex2str(f->product_id, pid, sizeof(pid));
2041 monitor_printf(mon, " Bus %s, Addr %s, Port %s, ID %s:%s\n",
2042 bus, addr, f->port ? f->port : "*", vid, pid);