hyper-v: introduce Hyper-V support infrastructure.
[qemu.git] / usb-linux.c
1 /*
2 * Linux host USB redirector
3 *
4 * Copyright (c) 2005 Fabrice Bellard
5 *
6 * Copyright (c) 2008 Max Krasnyansky
7 * Support for host device auto connect & disconnect
8 * Major rewrite to support fully async operation
9 *
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
13 *
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:
20 *
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
23 *
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.
31 */
32
33 #include "qemu-common.h"
34 #include "qemu-timer.h"
35 #include "monitor.h"
36 #include "sysemu.h"
37 #include "trace.h"
38
39 #include <dirent.h>
40 #include <sys/ioctl.h>
41
42 #include <linux/usbdevice_fs.h>
43 #include <linux/version.h>
44 #include "hw/usb.h"
45
46 /* We redefine it to avoid version problems */
47 struct usb_ctrltransfer {
48 uint8_t bRequestType;
49 uint8_t bRequest;
50 uint16_t wValue;
51 uint16_t wIndex;
52 uint16_t wLength;
53 uint32_t timeout;
54 void *data;
55 };
56
57 typedef int USBScanFunc(void *opaque, int bus_num, int addr, const char *port,
58 int class_id, int vendor_id, int product_id,
59 const char *product_name, int speed);
60
61 //#define DEBUG
62
63 #ifdef DEBUG
64 #define DPRINTF printf
65 #else
66 #define DPRINTF(...)
67 #endif
68
69 #define USBDBG_DEVOPENED "husb: opened %s/devices\n"
70
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"
77
78 static char *usb_host_device_path;
79
80 #define USB_FS_NONE 0
81 #define USB_FS_PROC 1
82 #define USB_FS_DEV 2
83 #define USB_FS_SYS 3
84
85 static int usb_fs_type;
86
87 /* endpoint association data */
88 #define ISO_FRAME_DESC_PER_URB 32
89 #define INVALID_EP_TYPE 255
90
91 /* devio.c limits single requests to 16k */
92 #define MAX_USBFS_BUFFER_SIZE 16384
93
94 typedef struct AsyncURB AsyncURB;
95
96 struct endp_data {
97 uint8_t type;
98 uint8_t halted;
99 uint8_t iso_started;
100 AsyncURB *iso_urb;
101 int iso_urb_idx;
102 int iso_buffer_used;
103 int max_packet_size;
104 int inflight;
105 };
106
107 struct USBAutoFilter {
108 uint32_t bus_num;
109 uint32_t addr;
110 char *port;
111 uint32_t vendor_id;
112 uint32_t product_id;
113 };
114
115 typedef struct USBHostDevice {
116 USBDevice dev;
117 int fd;
118 int hub_fd;
119 int hub_port;
120
121 uint8_t descr[8192];
122 int descr_len;
123 int configuration;
124 int ninterfaces;
125 int closing;
126 uint32_t iso_urb_count;
127 Notifier exit;
128
129 struct endp_data ep_in[MAX_ENDPOINTS];
130 struct endp_data ep_out[MAX_ENDPOINTS];
131 QLIST_HEAD(, AsyncURB) aurbs;
132
133 /* Host side address */
134 int bus_num;
135 int addr;
136 char port[MAX_PORTLEN];
137 struct USBAutoFilter match;
138 int seen, errcount;
139
140 QTAILQ_ENTRY(USBHostDevice) next;
141 } USBHostDevice;
142
143 static QTAILQ_HEAD(, USBHostDevice) hostdevs = QTAILQ_HEAD_INITIALIZER(hostdevs);
144
145 static int usb_host_close(USBHostDevice *dev);
146 static int parse_filter(const char *spec, struct USBAutoFilter *f);
147 static void usb_host_auto_check(void *unused);
148 static int usb_host_read_file(char *line, size_t line_size,
149 const char *device_file, const char *device_name);
150 static int usb_linux_update_endp_table(USBHostDevice *s);
151
152 static int usb_host_do_reset(USBHostDevice *dev)
153 {
154 struct timeval s, e;
155 uint32_t usecs;
156 int ret;
157
158 gettimeofday(&s, NULL);
159 ret = ioctl(dev->fd, USBDEVFS_RESET);
160 gettimeofday(&e, NULL);
161 usecs = (e.tv_sec - s.tv_sec) * 1000000;
162 usecs += e.tv_usec - s.tv_usec;
163 if (usecs > 1000000) {
164 /* more than a second, something is fishy, broken usb device? */
165 fprintf(stderr, "husb: device %d:%d reset took %d.%06d seconds\n",
166 dev->bus_num, dev->addr, usecs / 1000000, usecs % 1000000);
167 }
168 return ret;
169 }
170
171 static struct endp_data *get_endp(USBHostDevice *s, int pid, int ep)
172 {
173 struct endp_data *eps = pid == USB_TOKEN_IN ? s->ep_in : s->ep_out;
174 assert(pid == USB_TOKEN_IN || pid == USB_TOKEN_OUT);
175 assert(ep > 0 && ep <= MAX_ENDPOINTS);
176 return eps + ep - 1;
177 }
178
179 static int is_isoc(USBHostDevice *s, int pid, int ep)
180 {
181 return get_endp(s, pid, ep)->type == USBDEVFS_URB_TYPE_ISO;
182 }
183
184 static int is_valid(USBHostDevice *s, int pid, int ep)
185 {
186 return get_endp(s, pid, ep)->type != INVALID_EP_TYPE;
187 }
188
189 static int is_halted(USBHostDevice *s, int pid, int ep)
190 {
191 return get_endp(s, pid, ep)->halted;
192 }
193
194 static void clear_halt(USBHostDevice *s, int pid, int ep)
195 {
196 trace_usb_host_ep_clear_halt(s->bus_num, s->addr, ep);
197 get_endp(s, pid, ep)->halted = 0;
198 }
199
200 static void set_halt(USBHostDevice *s, int pid, int ep)
201 {
202 if (ep != 0) {
203 trace_usb_host_ep_set_halt(s->bus_num, s->addr, ep);
204 get_endp(s, pid, ep)->halted = 1;
205 }
206 }
207
208 static int is_iso_started(USBHostDevice *s, int pid, int ep)
209 {
210 return get_endp(s, pid, ep)->iso_started;
211 }
212
213 static void clear_iso_started(USBHostDevice *s, int pid, int ep)
214 {
215 trace_usb_host_ep_stop_iso(s->bus_num, s->addr, ep);
216 get_endp(s, pid, ep)->iso_started = 0;
217 }
218
219 static void set_iso_started(USBHostDevice *s, int pid, int ep)
220 {
221 struct endp_data *e = get_endp(s, pid, ep);
222
223 trace_usb_host_ep_start_iso(s->bus_num, s->addr, ep);
224 if (!e->iso_started) {
225 e->iso_started = 1;
226 e->inflight = 0;
227 }
228 }
229
230 static int change_iso_inflight(USBHostDevice *s, int pid, int ep, int value)
231 {
232 struct endp_data *e = get_endp(s, pid, ep);
233
234 e->inflight += value;
235 return e->inflight;
236 }
237
238 static void set_iso_urb(USBHostDevice *s, int pid, int ep, AsyncURB *iso_urb)
239 {
240 get_endp(s, pid, ep)->iso_urb = iso_urb;
241 }
242
243 static AsyncURB *get_iso_urb(USBHostDevice *s, int pid, int ep)
244 {
245 return get_endp(s, pid, ep)->iso_urb;
246 }
247
248 static void set_iso_urb_idx(USBHostDevice *s, int pid, int ep, int i)
249 {
250 get_endp(s, pid, ep)->iso_urb_idx = i;
251 }
252
253 static int get_iso_urb_idx(USBHostDevice *s, int pid, int ep)
254 {
255 return get_endp(s, pid, ep)->iso_urb_idx;
256 }
257
258 static void set_iso_buffer_used(USBHostDevice *s, int pid, int ep, int i)
259 {
260 get_endp(s, pid, ep)->iso_buffer_used = i;
261 }
262
263 static int get_iso_buffer_used(USBHostDevice *s, int pid, int ep)
264 {
265 return get_endp(s, pid, ep)->iso_buffer_used;
266 }
267
268 static void set_max_packet_size(USBHostDevice *s, int pid, int ep,
269 uint8_t *descriptor)
270 {
271 int raw = descriptor[4] + (descriptor[5] << 8);
272 int size, microframes;
273
274 size = raw & 0x7ff;
275 switch ((raw >> 11) & 3) {
276 case 1: microframes = 2; break;
277 case 2: microframes = 3; break;
278 default: microframes = 1; break;
279 }
280 get_endp(s, pid, ep)->max_packet_size = size * microframes;
281 }
282
283 static int get_max_packet_size(USBHostDevice *s, int pid, int ep)
284 {
285 return get_endp(s, pid, ep)->max_packet_size;
286 }
287
288 /*
289 * Async URB state.
290 * We always allocate iso packet descriptors even for bulk transfers
291 * to simplify allocation and casts.
292 */
293 struct AsyncURB
294 {
295 struct usbdevfs_urb urb;
296 struct usbdevfs_iso_packet_desc isocpd[ISO_FRAME_DESC_PER_URB];
297 USBHostDevice *hdev;
298 QLIST_ENTRY(AsyncURB) next;
299
300 /* For regular async urbs */
301 USBPacket *packet;
302 int more; /* large transfer, more urbs follow */
303
304 /* For buffered iso handling */
305 int iso_frame_idx; /* -1 means in flight */
306 };
307
308 static AsyncURB *async_alloc(USBHostDevice *s)
309 {
310 AsyncURB *aurb = g_malloc0(sizeof(AsyncURB));
311 aurb->hdev = s;
312 QLIST_INSERT_HEAD(&s->aurbs, aurb, next);
313 return aurb;
314 }
315
316 static void async_free(AsyncURB *aurb)
317 {
318 QLIST_REMOVE(aurb, next);
319 g_free(aurb);
320 }
321
322 static void do_disconnect(USBHostDevice *s)
323 {
324 usb_host_close(s);
325 usb_host_auto_check(NULL);
326 }
327
328 static void async_complete(void *opaque)
329 {
330 USBHostDevice *s = opaque;
331 AsyncURB *aurb;
332 int urbs = 0;
333
334 while (1) {
335 USBPacket *p;
336
337 int r = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &aurb);
338 if (r < 0) {
339 if (errno == EAGAIN) {
340 if (urbs > 2) {
341 fprintf(stderr, "husb: %d iso urbs finished at once\n", urbs);
342 }
343 return;
344 }
345 if (errno == ENODEV) {
346 if (!s->closing) {
347 trace_usb_host_disconnect(s->bus_num, s->addr);
348 do_disconnect(s);
349 }
350 return;
351 }
352
353 perror("USBDEVFS_REAPURBNDELAY");
354 return;
355 }
356
357 DPRINTF("husb: async completed. aurb %p status %d alen %d\n",
358 aurb, aurb->urb.status, aurb->urb.actual_length);
359
360 /* If this is a buffered iso urb mark it as complete and don't do
361 anything else (it is handled further in usb_host_handle_iso_data) */
362 if (aurb->iso_frame_idx == -1) {
363 int inflight;
364 int pid = (aurb->urb.endpoint & USB_DIR_IN) ?
365 USB_TOKEN_IN : USB_TOKEN_OUT;
366 int ep = aurb->urb.endpoint & 0xf;
367 if (aurb->urb.status == -EPIPE) {
368 set_halt(s, pid, ep);
369 }
370 aurb->iso_frame_idx = 0;
371 urbs++;
372 inflight = change_iso_inflight(s, pid, ep, -1);
373 if (inflight == 0 && is_iso_started(s, pid, ep)) {
374 fprintf(stderr, "husb: out of buffers for iso stream\n");
375 }
376 continue;
377 }
378
379 p = aurb->packet;
380 trace_usb_host_urb_complete(s->bus_num, s->addr, aurb, aurb->urb.status,
381 aurb->urb.actual_length, aurb->more);
382
383 if (p) {
384 switch (aurb->urb.status) {
385 case 0:
386 p->result += aurb->urb.actual_length;
387 break;
388
389 case -EPIPE:
390 set_halt(s, p->pid, p->devep);
391 p->result = USB_RET_STALL;
392 break;
393
394 default:
395 p->result = USB_RET_NAK;
396 break;
397 }
398
399 if (aurb->urb.type == USBDEVFS_URB_TYPE_CONTROL) {
400 trace_usb_host_req_complete(s->bus_num, s->addr, p->result);
401 usb_generic_async_ctrl_complete(&s->dev, p);
402 } else if (!aurb->more) {
403 trace_usb_host_req_complete(s->bus_num, s->addr, p->result);
404 usb_packet_complete(&s->dev, p);
405 }
406 }
407
408 async_free(aurb);
409 }
410 }
411
412 static void usb_host_async_cancel(USBDevice *dev, USBPacket *p)
413 {
414 USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
415 AsyncURB *aurb;
416
417 QLIST_FOREACH(aurb, &s->aurbs, next) {
418 if (p != aurb->packet) {
419 continue;
420 }
421
422 DPRINTF("husb: async cancel: packet %p, aurb %p\n", p, aurb);
423
424 /* Mark it as dead (see async_complete above) */
425 aurb->packet = NULL;
426
427 int r = ioctl(s->fd, USBDEVFS_DISCARDURB, aurb);
428 if (r < 0) {
429 DPRINTF("husb: async. discard urb failed errno %d\n", errno);
430 }
431 }
432 }
433
434 static int usb_host_claim_port(USBHostDevice *s)
435 {
436 #ifdef USBDEVFS_CLAIM_PORT
437 char *h, hub_name[64], line[1024];
438 int hub_addr, ret;
439
440 snprintf(hub_name, sizeof(hub_name), "%d-%s",
441 s->match.bus_num, s->match.port);
442
443 /* try strip off last ".$portnr" to get hub */
444 h = strrchr(hub_name, '.');
445 if (h != NULL) {
446 s->hub_port = atoi(h+1);
447 *h = '\0';
448 } else {
449 /* no dot in there -> it is the root hub */
450 snprintf(hub_name, sizeof(hub_name), "usb%d",
451 s->match.bus_num);
452 s->hub_port = atoi(s->match.port);
453 }
454
455 if (!usb_host_read_file(line, sizeof(line), "devnum",
456 hub_name)) {
457 return -1;
458 }
459 if (sscanf(line, "%d", &hub_addr) != 1) {
460 return -1;
461 }
462
463 if (!usb_host_device_path) {
464 return -1;
465 }
466 snprintf(line, sizeof(line), "%s/%03d/%03d",
467 usb_host_device_path, s->match.bus_num, hub_addr);
468 s->hub_fd = open(line, O_RDWR | O_NONBLOCK);
469 if (s->hub_fd < 0) {
470 return -1;
471 }
472
473 ret = ioctl(s->hub_fd, USBDEVFS_CLAIM_PORT, &s->hub_port);
474 if (ret < 0) {
475 close(s->hub_fd);
476 s->hub_fd = -1;
477 return -1;
478 }
479
480 trace_usb_host_claim_port(s->match.bus_num, hub_addr, s->hub_port);
481 return 0;
482 #else
483 return -1;
484 #endif
485 }
486
487 static void usb_host_release_port(USBHostDevice *s)
488 {
489 if (s->hub_fd == -1) {
490 return;
491 }
492 #ifdef USBDEVFS_RELEASE_PORT
493 ioctl(s->hub_fd, USBDEVFS_RELEASE_PORT, &s->hub_port);
494 #endif
495 close(s->hub_fd);
496 s->hub_fd = -1;
497 }
498
499 static int usb_host_disconnect_ifaces(USBHostDevice *dev, int nb_interfaces)
500 {
501 /* earlier Linux 2.4 do not support that */
502 #ifdef USBDEVFS_DISCONNECT
503 struct usbdevfs_ioctl ctrl;
504 int ret, interface;
505
506 for (interface = 0; interface < nb_interfaces; interface++) {
507 ctrl.ioctl_code = USBDEVFS_DISCONNECT;
508 ctrl.ifno = interface;
509 ctrl.data = 0;
510 ret = ioctl(dev->fd, USBDEVFS_IOCTL, &ctrl);
511 if (ret < 0 && errno != ENODATA) {
512 perror("USBDEVFS_DISCONNECT");
513 return -1;
514 }
515 }
516 #endif
517 return 0;
518 }
519
520 static int usb_linux_get_num_interfaces(USBHostDevice *s)
521 {
522 char device_name[64], line[1024];
523 int num_interfaces = 0;
524
525 if (usb_fs_type != USB_FS_SYS) {
526 return -1;
527 }
528
529 sprintf(device_name, "%d-%s", s->bus_num, s->port);
530 if (!usb_host_read_file(line, sizeof(line), "bNumInterfaces",
531 device_name)) {
532 return -1;
533 }
534 if (sscanf(line, "%d", &num_interfaces) != 1) {
535 return -1;
536 }
537 return num_interfaces;
538 }
539
540 static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration)
541 {
542 const char *op = NULL;
543 int dev_descr_len, config_descr_len;
544 int interface, nb_interfaces;
545 int ret, i;
546
547 if (configuration == 0) { /* address state - ignore */
548 dev->ninterfaces = 0;
549 dev->configuration = 0;
550 return 1;
551 }
552
553 DPRINTF("husb: claiming interfaces. config %d\n", configuration);
554
555 i = 0;
556 dev_descr_len = dev->descr[0];
557 if (dev_descr_len > dev->descr_len) {
558 fprintf(stderr, "husb: update iface failed. descr too short\n");
559 return 0;
560 }
561
562 i += dev_descr_len;
563 while (i < dev->descr_len) {
564 DPRINTF("husb: i is %d, descr_len is %d, dl %d, dt %d\n",
565 i, dev->descr_len,
566 dev->descr[i], dev->descr[i+1]);
567
568 if (dev->descr[i+1] != USB_DT_CONFIG) {
569 i += dev->descr[i];
570 continue;
571 }
572 config_descr_len = dev->descr[i];
573
574 DPRINTF("husb: config #%d need %d\n", dev->descr[i + 5], configuration);
575
576 if (configuration == dev->descr[i + 5]) {
577 configuration = dev->descr[i + 5];
578 break;
579 }
580
581 i += config_descr_len;
582 }
583
584 if (i >= dev->descr_len) {
585 fprintf(stderr,
586 "husb: update iface failed. no matching configuration\n");
587 return 0;
588 }
589 nb_interfaces = dev->descr[i + 4];
590
591 if (usb_host_disconnect_ifaces(dev, nb_interfaces) < 0) {
592 goto fail;
593 }
594
595 /* XXX: only grab if all interfaces are free */
596 for (interface = 0; interface < nb_interfaces; interface++) {
597 op = "USBDEVFS_CLAIMINTERFACE";
598 ret = ioctl(dev->fd, USBDEVFS_CLAIMINTERFACE, &interface);
599 if (ret < 0) {
600 goto fail;
601 }
602 }
603
604 trace_usb_host_claim_interfaces(dev->bus_num, dev->addr,
605 nb_interfaces, configuration);
606
607 dev->ninterfaces = nb_interfaces;
608 dev->configuration = configuration;
609 return 1;
610
611 fail:
612 if (errno == ENODEV) {
613 do_disconnect(dev);
614 }
615 perror(op);
616 return 0;
617 }
618
619 static int usb_host_release_interfaces(USBHostDevice *s)
620 {
621 int ret, i;
622
623 trace_usb_host_release_interfaces(s->bus_num, s->addr);
624
625 for (i = 0; i < s->ninterfaces; i++) {
626 ret = ioctl(s->fd, USBDEVFS_RELEASEINTERFACE, &i);
627 if (ret < 0) {
628 perror("USBDEVFS_RELEASEINTERFACE");
629 return 0;
630 }
631 }
632 return 1;
633 }
634
635 static void usb_host_handle_reset(USBDevice *dev)
636 {
637 USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
638
639 trace_usb_host_reset(s->bus_num, s->addr);
640
641 usb_host_do_reset(s);;
642
643 usb_host_claim_interfaces(s, 0);
644 usb_linux_update_endp_table(s);
645 }
646
647 static void usb_host_handle_destroy(USBDevice *dev)
648 {
649 USBHostDevice *s = (USBHostDevice *)dev;
650
651 usb_host_release_port(s);
652 usb_host_close(s);
653 QTAILQ_REMOVE(&hostdevs, s, next);
654 qemu_remove_exit_notifier(&s->exit);
655 }
656
657 /* iso data is special, we need to keep enough urbs in flight to make sure
658 that the controller never runs out of them, otherwise the device will
659 likely suffer a buffer underrun / overrun. */
660 static AsyncURB *usb_host_alloc_iso(USBHostDevice *s, int pid, uint8_t ep)
661 {
662 AsyncURB *aurb;
663 int i, j, len = get_max_packet_size(s, pid, ep);
664
665 aurb = g_malloc0(s->iso_urb_count * sizeof(*aurb));
666 for (i = 0; i < s->iso_urb_count; i++) {
667 aurb[i].urb.endpoint = ep;
668 aurb[i].urb.buffer_length = ISO_FRAME_DESC_PER_URB * len;
669 aurb[i].urb.buffer = g_malloc(aurb[i].urb.buffer_length);
670 aurb[i].urb.type = USBDEVFS_URB_TYPE_ISO;
671 aurb[i].urb.flags = USBDEVFS_URB_ISO_ASAP;
672 aurb[i].urb.number_of_packets = ISO_FRAME_DESC_PER_URB;
673 for (j = 0 ; j < ISO_FRAME_DESC_PER_URB; j++)
674 aurb[i].urb.iso_frame_desc[j].length = len;
675 if (pid == USB_TOKEN_IN) {
676 aurb[i].urb.endpoint |= 0x80;
677 /* Mark as fully consumed (idle) */
678 aurb[i].iso_frame_idx = ISO_FRAME_DESC_PER_URB;
679 }
680 }
681 set_iso_urb(s, pid, ep, aurb);
682
683 return aurb;
684 }
685
686 static void usb_host_stop_n_free_iso(USBHostDevice *s, int pid, uint8_t ep)
687 {
688 AsyncURB *aurb;
689 int i, ret, killed = 0, free = 1;
690
691 aurb = get_iso_urb(s, pid, ep);
692 if (!aurb) {
693 return;
694 }
695
696 for (i = 0; i < s->iso_urb_count; i++) {
697 /* in flight? */
698 if (aurb[i].iso_frame_idx == -1) {
699 ret = ioctl(s->fd, USBDEVFS_DISCARDURB, &aurb[i]);
700 if (ret < 0) {
701 perror("USBDEVFS_DISCARDURB");
702 free = 0;
703 continue;
704 }
705 killed++;
706 }
707 }
708
709 /* Make sure any urbs we've killed are reaped before we free them */
710 if (killed) {
711 async_complete(s);
712 }
713
714 for (i = 0; i < s->iso_urb_count; i++) {
715 g_free(aurb[i].urb.buffer);
716 }
717
718 if (free)
719 g_free(aurb);
720 else
721 printf("husb: leaking iso urbs because of discard failure\n");
722 set_iso_urb(s, pid, ep, NULL);
723 set_iso_urb_idx(s, pid, ep, 0);
724 clear_iso_started(s, pid, ep);
725 }
726
727 static int urb_status_to_usb_ret(int status)
728 {
729 switch (status) {
730 case -EPIPE:
731 return USB_RET_STALL;
732 default:
733 return USB_RET_NAK;
734 }
735 }
736
737 static int usb_host_handle_iso_data(USBHostDevice *s, USBPacket *p, int in)
738 {
739 AsyncURB *aurb;
740 int i, j, ret, max_packet_size, offset, len = 0;
741 uint8_t *buf;
742
743 max_packet_size = get_max_packet_size(s, p->pid, p->devep);
744 if (max_packet_size == 0)
745 return USB_RET_NAK;
746
747 aurb = get_iso_urb(s, p->pid, p->devep);
748 if (!aurb) {
749 aurb = usb_host_alloc_iso(s, p->pid, p->devep);
750 }
751
752 i = get_iso_urb_idx(s, p->pid, p->devep);
753 j = aurb[i].iso_frame_idx;
754 if (j >= 0 && j < ISO_FRAME_DESC_PER_URB) {
755 if (in) {
756 /* Check urb status */
757 if (aurb[i].urb.status) {
758 len = urb_status_to_usb_ret(aurb[i].urb.status);
759 /* Move to the next urb */
760 aurb[i].iso_frame_idx = ISO_FRAME_DESC_PER_URB - 1;
761 /* Check frame status */
762 } else if (aurb[i].urb.iso_frame_desc[j].status) {
763 len = urb_status_to_usb_ret(
764 aurb[i].urb.iso_frame_desc[j].status);
765 /* Check the frame fits */
766 } else if (aurb[i].urb.iso_frame_desc[j].actual_length
767 > p->iov.size) {
768 printf("husb: received iso data is larger then packet\n");
769 len = USB_RET_NAK;
770 /* All good copy data over */
771 } else {
772 len = aurb[i].urb.iso_frame_desc[j].actual_length;
773 buf = aurb[i].urb.buffer +
774 j * aurb[i].urb.iso_frame_desc[0].length;
775 usb_packet_copy(p, buf, len);
776 }
777 } else {
778 len = p->iov.size;
779 offset = (j == 0) ? 0 : get_iso_buffer_used(s, p->pid, p->devep);
780
781 /* Check the frame fits */
782 if (len > max_packet_size) {
783 printf("husb: send iso data is larger then max packet size\n");
784 return USB_RET_NAK;
785 }
786
787 /* All good copy data over */
788 usb_packet_copy(p, aurb[i].urb.buffer + offset, len);
789 aurb[i].urb.iso_frame_desc[j].length = len;
790 offset += len;
791 set_iso_buffer_used(s, p->pid, p->devep, offset);
792
793 /* Start the stream once we have buffered enough data */
794 if (!is_iso_started(s, p->pid, p->devep) && i == 1 && j == 8) {
795 set_iso_started(s, p->pid, p->devep);
796 }
797 }
798 aurb[i].iso_frame_idx++;
799 if (aurb[i].iso_frame_idx == ISO_FRAME_DESC_PER_URB) {
800 i = (i + 1) % s->iso_urb_count;
801 set_iso_urb_idx(s, p->pid, p->devep, i);
802 }
803 } else {
804 if (in) {
805 set_iso_started(s, p->pid, p->devep);
806 } else {
807 DPRINTF("hubs: iso out error no free buffer, dropping packet\n");
808 }
809 }
810
811 if (is_iso_started(s, p->pid, p->devep)) {
812 /* (Re)-submit all fully consumed / filled urbs */
813 for (i = 0; i < s->iso_urb_count; i++) {
814 if (aurb[i].iso_frame_idx == ISO_FRAME_DESC_PER_URB) {
815 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, &aurb[i]);
816 if (ret < 0) {
817 perror("USBDEVFS_SUBMITURB");
818 if (!in || len == 0) {
819 switch(errno) {
820 case ETIMEDOUT:
821 len = USB_RET_NAK;
822 break;
823 case EPIPE:
824 default:
825 len = USB_RET_STALL;
826 }
827 }
828 break;
829 }
830 aurb[i].iso_frame_idx = -1;
831 change_iso_inflight(s, p->pid, p->devep, 1);
832 }
833 }
834 }
835
836 return len;
837 }
838
839 static int usb_host_handle_data(USBDevice *dev, USBPacket *p)
840 {
841 USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
842 struct usbdevfs_urb *urb;
843 AsyncURB *aurb;
844 int ret, rem, prem, v;
845 uint8_t *pbuf;
846 uint8_t ep;
847
848 trace_usb_host_req_data(s->bus_num, s->addr,
849 p->pid == USB_TOKEN_IN,
850 p->devep, p->iov.size);
851
852 if (!is_valid(s, p->pid, p->devep)) {
853 trace_usb_host_req_complete(s->bus_num, s->addr, USB_RET_NAK);
854 return USB_RET_NAK;
855 }
856
857 if (p->pid == USB_TOKEN_IN) {
858 ep = p->devep | 0x80;
859 } else {
860 ep = p->devep;
861 }
862
863 if (is_halted(s, p->pid, p->devep)) {
864 unsigned int arg = ep;
865 ret = ioctl(s->fd, USBDEVFS_CLEAR_HALT, &arg);
866 if (ret < 0) {
867 perror("USBDEVFS_CLEAR_HALT");
868 trace_usb_host_req_complete(s->bus_num, s->addr, USB_RET_NAK);
869 return USB_RET_NAK;
870 }
871 clear_halt(s, p->pid, p->devep);
872 }
873
874 if (is_isoc(s, p->pid, p->devep)) {
875 return usb_host_handle_iso_data(s, p, p->pid == USB_TOKEN_IN);
876 }
877
878 v = 0;
879 prem = p->iov.iov[v].iov_len;
880 pbuf = p->iov.iov[v].iov_base;
881 rem = p->iov.size;
882 while (rem) {
883 if (prem == 0) {
884 v++;
885 assert(v < p->iov.niov);
886 prem = p->iov.iov[v].iov_len;
887 pbuf = p->iov.iov[v].iov_base;
888 assert(prem <= rem);
889 }
890 aurb = async_alloc(s);
891 aurb->packet = p;
892
893 urb = &aurb->urb;
894 urb->endpoint = ep;
895 urb->type = USBDEVFS_URB_TYPE_BULK;
896 urb->usercontext = s;
897 urb->buffer = pbuf;
898 urb->buffer_length = prem;
899
900 if (urb->buffer_length > MAX_USBFS_BUFFER_SIZE) {
901 urb->buffer_length = MAX_USBFS_BUFFER_SIZE;
902 }
903 pbuf += urb->buffer_length;
904 prem -= urb->buffer_length;
905 rem -= urb->buffer_length;
906 if (rem) {
907 aurb->more = 1;
908 }
909
910 trace_usb_host_urb_submit(s->bus_num, s->addr, aurb,
911 urb->buffer_length, aurb->more);
912 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
913
914 DPRINTF("husb: data submit: ep 0x%x, len %u, more %d, packet %p, aurb %p\n",
915 urb->endpoint, urb->buffer_length, aurb->more, p, aurb);
916
917 if (ret < 0) {
918 perror("USBDEVFS_SUBMITURB");
919 async_free(aurb);
920
921 switch(errno) {
922 case ETIMEDOUT:
923 trace_usb_host_req_complete(s->bus_num, s->addr, USB_RET_NAK);
924 return USB_RET_NAK;
925 case EPIPE:
926 default:
927 trace_usb_host_req_complete(s->bus_num, s->addr, USB_RET_STALL);
928 return USB_RET_STALL;
929 }
930 }
931 }
932
933 return USB_RET_ASYNC;
934 }
935
936 static int ctrl_error(void)
937 {
938 if (errno == ETIMEDOUT) {
939 return USB_RET_NAK;
940 } else {
941 return USB_RET_STALL;
942 }
943 }
944
945 static int usb_host_set_address(USBHostDevice *s, int addr)
946 {
947 trace_usb_host_set_address(s->bus_num, s->addr, addr);
948 s->dev.addr = addr;
949 return 0;
950 }
951
952 static int usb_host_set_config(USBHostDevice *s, int config)
953 {
954 int ret, first = 1;
955
956 trace_usb_host_set_config(s->bus_num, s->addr, config);
957
958 usb_host_release_interfaces(s);
959
960 again:
961 ret = ioctl(s->fd, USBDEVFS_SETCONFIGURATION, &config);
962
963 DPRINTF("husb: ctrl set config %d ret %d errno %d\n", config, ret, errno);
964
965 if (ret < 0 && errno == EBUSY && first) {
966 /* happens if usb device is in use by host drivers */
967 int count = usb_linux_get_num_interfaces(s);
968 if (count > 0) {
969 DPRINTF("husb: busy -> disconnecting %d interfaces\n", count);
970 usb_host_disconnect_ifaces(s, count);
971 first = 0;
972 goto again;
973 }
974 }
975
976 if (ret < 0) {
977 return ctrl_error();
978 }
979 usb_host_claim_interfaces(s, config);
980 usb_linux_update_endp_table(s);
981 return 0;
982 }
983
984 static int usb_host_set_interface(USBHostDevice *s, int iface, int alt)
985 {
986 struct usbdevfs_setinterface si;
987 int i, ret;
988
989 trace_usb_host_set_interface(s->bus_num, s->addr, iface, alt);
990
991 for (i = 1; i <= MAX_ENDPOINTS; i++) {
992 if (is_isoc(s, USB_TOKEN_IN, i)) {
993 usb_host_stop_n_free_iso(s, USB_TOKEN_IN, i);
994 }
995 if (is_isoc(s, USB_TOKEN_OUT, i)) {
996 usb_host_stop_n_free_iso(s, USB_TOKEN_OUT, i);
997 }
998 }
999
1000 si.interface = iface;
1001 si.altsetting = alt;
1002 ret = ioctl(s->fd, USBDEVFS_SETINTERFACE, &si);
1003
1004 DPRINTF("husb: ctrl set iface %d altset %d ret %d errno %d\n",
1005 iface, alt, ret, errno);
1006
1007 if (ret < 0) {
1008 return ctrl_error();
1009 }
1010 usb_linux_update_endp_table(s);
1011 return 0;
1012 }
1013
1014 static int usb_host_handle_control(USBDevice *dev, USBPacket *p,
1015 int request, int value, int index, int length, uint8_t *data)
1016 {
1017 USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
1018 struct usbdevfs_urb *urb;
1019 AsyncURB *aurb;
1020 int ret;
1021
1022 /*
1023 * Process certain standard device requests.
1024 * These are infrequent and are processed synchronously.
1025 */
1026
1027 /* Note request is (bRequestType << 8) | bRequest */
1028 trace_usb_host_req_control(s->bus_num, s->addr, request, value, index);
1029
1030 switch (request) {
1031 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
1032 return usb_host_set_address(s, value);
1033
1034 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
1035 return usb_host_set_config(s, value & 0xff);
1036
1037 case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
1038 return usb_host_set_interface(s, index, value);
1039 }
1040
1041 /* The rest are asynchronous */
1042
1043 if (length > sizeof(dev->data_buf)) {
1044 fprintf(stderr, "husb: ctrl buffer too small (%d > %zu)\n",
1045 length, sizeof(dev->data_buf));
1046 return USB_RET_STALL;
1047 }
1048
1049 aurb = async_alloc(s);
1050 aurb->packet = p;
1051
1052 /*
1053 * Setup ctrl transfer.
1054 *
1055 * s->ctrl is laid out such that data buffer immediately follows
1056 * 'req' struct which is exactly what usbdevfs expects.
1057 */
1058 urb = &aurb->urb;
1059
1060 urb->type = USBDEVFS_URB_TYPE_CONTROL;
1061 urb->endpoint = p->devep;
1062
1063 urb->buffer = &dev->setup_buf;
1064 urb->buffer_length = length + 8;
1065
1066 urb->usercontext = s;
1067
1068 trace_usb_host_urb_submit(s->bus_num, s->addr, aurb,
1069 urb->buffer_length, aurb->more);
1070 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
1071
1072 DPRINTF("husb: submit ctrl. len %u aurb %p\n", urb->buffer_length, aurb);
1073
1074 if (ret < 0) {
1075 DPRINTF("husb: submit failed. errno %d\n", errno);
1076 async_free(aurb);
1077
1078 switch(errno) {
1079 case ETIMEDOUT:
1080 return USB_RET_NAK;
1081 case EPIPE:
1082 default:
1083 return USB_RET_STALL;
1084 }
1085 }
1086
1087 return USB_RET_ASYNC;
1088 }
1089
1090 static uint8_t usb_linux_get_alt_setting(USBHostDevice *s,
1091 uint8_t configuration, uint8_t interface)
1092 {
1093 uint8_t alt_setting;
1094 struct usb_ctrltransfer ct;
1095 int ret;
1096
1097 if (usb_fs_type == USB_FS_SYS) {
1098 char device_name[64], line[1024];
1099 int alt_setting;
1100
1101 sprintf(device_name, "%d-%s:%d.%d", s->bus_num, s->port,
1102 (int)configuration, (int)interface);
1103
1104 if (!usb_host_read_file(line, sizeof(line), "bAlternateSetting",
1105 device_name)) {
1106 goto usbdevfs;
1107 }
1108 if (sscanf(line, "%d", &alt_setting) != 1) {
1109 goto usbdevfs;
1110 }
1111 return alt_setting;
1112 }
1113
1114 usbdevfs:
1115 ct.bRequestType = USB_DIR_IN | USB_RECIP_INTERFACE;
1116 ct.bRequest = USB_REQ_GET_INTERFACE;
1117 ct.wValue = 0;
1118 ct.wIndex = interface;
1119 ct.wLength = 1;
1120 ct.data = &alt_setting;
1121 ct.timeout = 50;
1122 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
1123 if (ret < 0) {
1124 /* Assume alt 0 on error */
1125 return 0;
1126 }
1127
1128 return alt_setting;
1129 }
1130
1131 /* returns 1 on problem encountered or 0 for success */
1132 static int usb_linux_update_endp_table(USBHostDevice *s)
1133 {
1134 uint8_t *descriptors;
1135 uint8_t devep, type, alt_interface;
1136 int interface, length, i, ep, pid;
1137 struct endp_data *epd;
1138
1139 for (i = 0; i < MAX_ENDPOINTS; i++) {
1140 s->ep_in[i].type = INVALID_EP_TYPE;
1141 s->ep_out[i].type = INVALID_EP_TYPE;
1142 }
1143
1144 if (s->configuration == 0) {
1145 /* not configured yet -- leave all endpoints disabled */
1146 return 0;
1147 }
1148
1149 /* get the desired configuration, interface, and endpoint descriptors
1150 * from device description */
1151 descriptors = &s->descr[18];
1152 length = s->descr_len - 18;
1153 i = 0;
1154
1155 while (i < length) {
1156 if (descriptors[i + 1] != USB_DT_CONFIG) {
1157 fprintf(stderr, "invalid descriptor data\n");
1158 return 1;
1159 } else if (descriptors[i + 5] != s->configuration) {
1160 DPRINTF("not requested configuration %d\n", s->configuration);
1161 i += (descriptors[i + 3] << 8) + descriptors[i + 2];
1162 continue;
1163 }
1164
1165 i += descriptors[i];
1166
1167 if (descriptors[i + 1] != USB_DT_INTERFACE ||
1168 (descriptors[i + 1] == USB_DT_INTERFACE &&
1169 descriptors[i + 4] == 0)) {
1170 i += descriptors[i];
1171 continue;
1172 }
1173
1174 interface = descriptors[i + 2];
1175 alt_interface = usb_linux_get_alt_setting(s, s->configuration,
1176 interface);
1177
1178 /* the current interface descriptor is the active interface
1179 * and has endpoints */
1180 if (descriptors[i + 3] != alt_interface) {
1181 i += descriptors[i];
1182 continue;
1183 }
1184
1185 /* advance to the endpoints */
1186 while (i < length && descriptors[i +1] != USB_DT_ENDPOINT) {
1187 i += descriptors[i];
1188 }
1189
1190 if (i >= length)
1191 break;
1192
1193 while (i < length) {
1194 if (descriptors[i + 1] != USB_DT_ENDPOINT) {
1195 break;
1196 }
1197
1198 devep = descriptors[i + 2];
1199 pid = (devep & USB_DIR_IN) ? USB_TOKEN_IN : USB_TOKEN_OUT;
1200 ep = devep & 0xf;
1201 if (ep == 0) {
1202 fprintf(stderr, "usb-linux: invalid ep descriptor, ep == 0\n");
1203 return 1;
1204 }
1205
1206 switch (descriptors[i + 3] & 0x3) {
1207 case 0x00:
1208 type = USBDEVFS_URB_TYPE_CONTROL;
1209 break;
1210 case 0x01:
1211 type = USBDEVFS_URB_TYPE_ISO;
1212 set_max_packet_size(s, pid, ep, descriptors + i);
1213 break;
1214 case 0x02:
1215 type = USBDEVFS_URB_TYPE_BULK;
1216 break;
1217 case 0x03:
1218 type = USBDEVFS_URB_TYPE_INTERRUPT;
1219 break;
1220 default:
1221 DPRINTF("usb_host: malformed endpoint type\n");
1222 type = USBDEVFS_URB_TYPE_BULK;
1223 }
1224 epd = get_endp(s, pid, ep);
1225 assert(epd->type == INVALID_EP_TYPE);
1226 epd->type = type;
1227 epd->halted = 0;
1228
1229 i += descriptors[i];
1230 }
1231 }
1232 return 0;
1233 }
1234
1235 /*
1236 * Check if we can safely redirect a usb2 device to a usb1 virtual controller,
1237 * this function assumes this is safe, if:
1238 * 1) There are no isoc endpoints
1239 * 2) There are no interrupt endpoints with a max_packet_size > 64
1240 * Note bulk endpoints with a max_packet_size > 64 in theory also are not
1241 * usb1 compatible, but in practice this seems to work fine.
1242 */
1243 static int usb_linux_full_speed_compat(USBHostDevice *dev)
1244 {
1245 int i, packet_size;
1246
1247 /*
1248 * usb_linux_update_endp_table only registers info about ep in the current
1249 * interface altsettings, so we need to parse the descriptors again.
1250 */
1251 for (i = 0; (i + 5) < dev->descr_len; i += dev->descr[i]) {
1252 if (dev->descr[i + 1] == USB_DT_ENDPOINT) {
1253 switch (dev->descr[i + 3] & 0x3) {
1254 case 0x00: /* CONTROL */
1255 break;
1256 case 0x01: /* ISO */
1257 return 0;
1258 case 0x02: /* BULK */
1259 break;
1260 case 0x03: /* INTERRUPT */
1261 packet_size = dev->descr[i + 4] + (dev->descr[i + 5] << 8);
1262 if (packet_size > 64)
1263 return 0;
1264 break;
1265 }
1266 }
1267 }
1268 return 1;
1269 }
1270
1271 static int usb_host_open(USBHostDevice *dev, int bus_num,
1272 int addr, const char *port,
1273 const char *prod_name, int speed)
1274 {
1275 int fd = -1, ret;
1276 char buf[1024];
1277
1278 trace_usb_host_open_started(bus_num, addr);
1279
1280 if (dev->fd != -1) {
1281 goto fail;
1282 }
1283
1284 if (!usb_host_device_path) {
1285 perror("husb: USB Host Device Path not set");
1286 goto fail;
1287 }
1288 snprintf(buf, sizeof(buf), "%s/%03d/%03d", usb_host_device_path,
1289 bus_num, addr);
1290 fd = open(buf, O_RDWR | O_NONBLOCK);
1291 if (fd < 0) {
1292 perror(buf);
1293 goto fail;
1294 }
1295 DPRINTF("husb: opened %s\n", buf);
1296
1297 dev->bus_num = bus_num;
1298 dev->addr = addr;
1299 strcpy(dev->port, port);
1300 dev->fd = fd;
1301
1302 /* read the device description */
1303 dev->descr_len = read(fd, dev->descr, sizeof(dev->descr));
1304 if (dev->descr_len <= 0) {
1305 perror("husb: reading device data failed");
1306 goto fail;
1307 }
1308
1309 #ifdef DEBUG
1310 {
1311 int x;
1312 printf("=== begin dumping device descriptor data ===\n");
1313 for (x = 0; x < dev->descr_len; x++) {
1314 printf("%02x ", dev->descr[x]);
1315 }
1316 printf("\n=== end dumping device descriptor data ===\n");
1317 }
1318 #endif
1319
1320
1321 /* start unconfigured -- we'll wait for the guest to set a configuration */
1322 if (!usb_host_claim_interfaces(dev, 0)) {
1323 goto fail;
1324 }
1325
1326 ret = usb_linux_update_endp_table(dev);
1327 if (ret) {
1328 goto fail;
1329 }
1330
1331 if (speed == -1) {
1332 struct usbdevfs_connectinfo ci;
1333
1334 ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci);
1335 if (ret < 0) {
1336 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
1337 goto fail;
1338 }
1339
1340 if (ci.slow) {
1341 speed = USB_SPEED_LOW;
1342 } else {
1343 speed = USB_SPEED_HIGH;
1344 }
1345 }
1346 dev->dev.speed = speed;
1347 dev->dev.speedmask = (1 << speed);
1348 if (dev->dev.speed == USB_SPEED_HIGH && usb_linux_full_speed_compat(dev)) {
1349 dev->dev.speedmask |= USB_SPEED_MASK_FULL;
1350 }
1351
1352 trace_usb_host_open_success(bus_num, addr);
1353
1354 if (!prod_name || prod_name[0] == '\0') {
1355 snprintf(dev->dev.product_desc, sizeof(dev->dev.product_desc),
1356 "host:%d.%d", bus_num, addr);
1357 } else {
1358 pstrcpy(dev->dev.product_desc, sizeof(dev->dev.product_desc),
1359 prod_name);
1360 }
1361
1362 ret = usb_device_attach(&dev->dev);
1363 if (ret) {
1364 goto fail;
1365 }
1366
1367 /* USB devio uses 'write' flag to check for async completions */
1368 qemu_set_fd_handler(dev->fd, NULL, async_complete, dev);
1369
1370 return 0;
1371
1372 fail:
1373 trace_usb_host_open_failure(bus_num, addr);
1374 if (dev->fd != -1) {
1375 close(dev->fd);
1376 dev->fd = -1;
1377 }
1378 return -1;
1379 }
1380
1381 static int usb_host_close(USBHostDevice *dev)
1382 {
1383 int i;
1384
1385 if (dev->fd == -1) {
1386 return -1;
1387 }
1388
1389 trace_usb_host_close(dev->bus_num, dev->addr);
1390
1391 qemu_set_fd_handler(dev->fd, NULL, NULL, NULL);
1392 dev->closing = 1;
1393 for (i = 1; i <= MAX_ENDPOINTS; i++) {
1394 if (is_isoc(dev, USB_TOKEN_IN, i)) {
1395 usb_host_stop_n_free_iso(dev, USB_TOKEN_IN, i);
1396 }
1397 if (is_isoc(dev, USB_TOKEN_OUT, i)) {
1398 usb_host_stop_n_free_iso(dev, USB_TOKEN_OUT, i);
1399 }
1400 }
1401 async_complete(dev);
1402 dev->closing = 0;
1403 if (dev->dev.attached) {
1404 usb_device_detach(&dev->dev);
1405 }
1406 usb_host_do_reset(dev);
1407 close(dev->fd);
1408 dev->fd = -1;
1409 return 0;
1410 }
1411
1412 static void usb_host_exit_notifier(struct Notifier *n, void *data)
1413 {
1414 USBHostDevice *s = container_of(n, USBHostDevice, exit);
1415
1416 usb_host_release_port(s);
1417 if (s->fd != -1) {
1418 usb_host_do_reset(s);;
1419 }
1420 }
1421
1422 static int usb_host_initfn(USBDevice *dev)
1423 {
1424 USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
1425
1426 dev->auto_attach = 0;
1427 s->fd = -1;
1428 s->hub_fd = -1;
1429
1430 QTAILQ_INSERT_TAIL(&hostdevs, s, next);
1431 s->exit.notify = usb_host_exit_notifier;
1432 qemu_add_exit_notifier(&s->exit);
1433 usb_host_auto_check(NULL);
1434
1435 if (s->match.bus_num != 0 && s->match.port != NULL) {
1436 usb_host_claim_port(s);
1437 }
1438 return 0;
1439 }
1440
1441 static const VMStateDescription vmstate_usb_host = {
1442 .name = "usb-host",
1443 .unmigratable = 1,
1444 };
1445
1446 static struct USBDeviceInfo usb_host_dev_info = {
1447 .product_desc = "USB Host Device",
1448 .qdev.name = "usb-host",
1449 .qdev.size = sizeof(USBHostDevice),
1450 .qdev.vmsd = &vmstate_usb_host,
1451 .init = usb_host_initfn,
1452 .handle_packet = usb_generic_handle_packet,
1453 .cancel_packet = usb_host_async_cancel,
1454 .handle_data = usb_host_handle_data,
1455 .handle_control = usb_host_handle_control,
1456 .handle_reset = usb_host_handle_reset,
1457 .handle_destroy = usb_host_handle_destroy,
1458 .usbdevice_name = "host",
1459 .usbdevice_init = usb_host_device_open,
1460 .qdev.props = (Property[]) {
1461 DEFINE_PROP_UINT32("hostbus", USBHostDevice, match.bus_num, 0),
1462 DEFINE_PROP_UINT32("hostaddr", USBHostDevice, match.addr, 0),
1463 DEFINE_PROP_STRING("hostport", USBHostDevice, match.port),
1464 DEFINE_PROP_HEX32("vendorid", USBHostDevice, match.vendor_id, 0),
1465 DEFINE_PROP_HEX32("productid", USBHostDevice, match.product_id, 0),
1466 DEFINE_PROP_UINT32("isobufs", USBHostDevice, iso_urb_count, 4),
1467 DEFINE_PROP_END_OF_LIST(),
1468 },
1469 };
1470
1471 static void usb_host_register_devices(void)
1472 {
1473 usb_qdev_register(&usb_host_dev_info);
1474 }
1475 device_init(usb_host_register_devices)
1476
1477 USBDevice *usb_host_device_open(const char *devname)
1478 {
1479 struct USBAutoFilter filter;
1480 USBDevice *dev;
1481 char *p;
1482
1483 dev = usb_create(NULL /* FIXME */, "usb-host");
1484
1485 if (strstr(devname, "auto:")) {
1486 if (parse_filter(devname, &filter) < 0) {
1487 goto fail;
1488 }
1489 } else {
1490 if ((p = strchr(devname, '.'))) {
1491 filter.bus_num = strtoul(devname, NULL, 0);
1492 filter.addr = strtoul(p + 1, NULL, 0);
1493 filter.vendor_id = 0;
1494 filter.product_id = 0;
1495 } else if ((p = strchr(devname, ':'))) {
1496 filter.bus_num = 0;
1497 filter.addr = 0;
1498 filter.vendor_id = strtoul(devname, NULL, 16);
1499 filter.product_id = strtoul(p + 1, NULL, 16);
1500 } else {
1501 goto fail;
1502 }
1503 }
1504
1505 qdev_prop_set_uint32(&dev->qdev, "hostbus", filter.bus_num);
1506 qdev_prop_set_uint32(&dev->qdev, "hostaddr", filter.addr);
1507 qdev_prop_set_uint32(&dev->qdev, "vendorid", filter.vendor_id);
1508 qdev_prop_set_uint32(&dev->qdev, "productid", filter.product_id);
1509 qdev_init_nofail(&dev->qdev);
1510 return dev;
1511
1512 fail:
1513 qdev_free(&dev->qdev);
1514 return NULL;
1515 }
1516
1517 int usb_host_device_close(const char *devname)
1518 {
1519 #if 0
1520 char product_name[PRODUCT_NAME_SZ];
1521 int bus_num, addr;
1522 USBHostDevice *s;
1523
1524 if (strstr(devname, "auto:")) {
1525 return usb_host_auto_del(devname);
1526 }
1527 if (usb_host_find_device(&bus_num, &addr, product_name,
1528 sizeof(product_name), devname) < 0) {
1529 return -1;
1530 }
1531 s = hostdev_find(bus_num, addr);
1532 if (s) {
1533 usb_device_delete_addr(s->bus_num, s->dev.addr);
1534 return 0;
1535 }
1536 #endif
1537
1538 return -1;
1539 }
1540
1541 static int get_tag_value(char *buf, int buf_size,
1542 const char *str, const char *tag,
1543 const char *stopchars)
1544 {
1545 const char *p;
1546 char *q;
1547 p = strstr(str, tag);
1548 if (!p) {
1549 return -1;
1550 }
1551 p += strlen(tag);
1552 while (qemu_isspace(*p)) {
1553 p++;
1554 }
1555 q = buf;
1556 while (*p != '\0' && !strchr(stopchars, *p)) {
1557 if ((q - buf) < (buf_size - 1)) {
1558 *q++ = *p;
1559 }
1560 p++;
1561 }
1562 *q = '\0';
1563 return q - buf;
1564 }
1565
1566 /*
1567 * Use /proc/bus/usb/devices or /dev/bus/usb/devices file to determine
1568 * host's USB devices. This is legacy support since many distributions
1569 * are moving to /sys/bus/usb
1570 */
1571 static int usb_host_scan_dev(void *opaque, USBScanFunc *func)
1572 {
1573 FILE *f = NULL;
1574 char line[1024];
1575 char buf[1024];
1576 int bus_num, addr, speed, device_count;
1577 int class_id, product_id, vendor_id, port;
1578 char product_name[512];
1579 int ret = 0;
1580
1581 if (!usb_host_device_path) {
1582 perror("husb: USB Host Device Path not set");
1583 goto the_end;
1584 }
1585 snprintf(line, sizeof(line), "%s/devices", usb_host_device_path);
1586 f = fopen(line, "r");
1587 if (!f) {
1588 perror("husb: cannot open devices file");
1589 goto the_end;
1590 }
1591
1592 device_count = 0;
1593 bus_num = addr = class_id = product_id = vendor_id = port = 0;
1594 speed = -1; /* Can't get the speed from /[proc|dev]/bus/usb/devices */
1595 for(;;) {
1596 if (fgets(line, sizeof(line), f) == NULL) {
1597 break;
1598 }
1599 if (strlen(line) > 0) {
1600 line[strlen(line) - 1] = '\0';
1601 }
1602 if (line[0] == 'T' && line[1] == ':') {
1603 if (device_count && (vendor_id || product_id)) {
1604 /* New device. Add the previously discovered device. */
1605 if (port > 0) {
1606 snprintf(buf, sizeof(buf), "%d", port);
1607 } else {
1608 snprintf(buf, sizeof(buf), "?");
1609 }
1610 ret = func(opaque, bus_num, addr, buf, class_id, vendor_id,
1611 product_id, product_name, speed);
1612 if (ret) {
1613 goto the_end;
1614 }
1615 }
1616 if (get_tag_value(buf, sizeof(buf), line, "Bus=", " ") < 0) {
1617 goto fail;
1618 }
1619 bus_num = atoi(buf);
1620 if (get_tag_value(buf, sizeof(buf), line, "Port=", " ") < 0) {
1621 goto fail;
1622 }
1623 port = atoi(buf);
1624 if (get_tag_value(buf, sizeof(buf), line, "Dev#=", " ") < 0) {
1625 goto fail;
1626 }
1627 addr = atoi(buf);
1628 if (get_tag_value(buf, sizeof(buf), line, "Spd=", " ") < 0) {
1629 goto fail;
1630 }
1631 if (!strcmp(buf, "5000")) {
1632 speed = USB_SPEED_SUPER;
1633 } else if (!strcmp(buf, "480")) {
1634 speed = USB_SPEED_HIGH;
1635 } else if (!strcmp(buf, "1.5")) {
1636 speed = USB_SPEED_LOW;
1637 } else {
1638 speed = USB_SPEED_FULL;
1639 }
1640 product_name[0] = '\0';
1641 class_id = 0xff;
1642 device_count++;
1643 product_id = 0;
1644 vendor_id = 0;
1645 } else if (line[0] == 'P' && line[1] == ':') {
1646 if (get_tag_value(buf, sizeof(buf), line, "Vendor=", " ") < 0) {
1647 goto fail;
1648 }
1649 vendor_id = strtoul(buf, NULL, 16);
1650 if (get_tag_value(buf, sizeof(buf), line, "ProdID=", " ") < 0) {
1651 goto fail;
1652 }
1653 product_id = strtoul(buf, NULL, 16);
1654 } else if (line[0] == 'S' && line[1] == ':') {
1655 if (get_tag_value(buf, sizeof(buf), line, "Product=", "") < 0) {
1656 goto fail;
1657 }
1658 pstrcpy(product_name, sizeof(product_name), buf);
1659 } else if (line[0] == 'D' && line[1] == ':') {
1660 if (get_tag_value(buf, sizeof(buf), line, "Cls=", " (") < 0) {
1661 goto fail;
1662 }
1663 class_id = strtoul(buf, NULL, 16);
1664 }
1665 fail: ;
1666 }
1667 if (device_count && (vendor_id || product_id)) {
1668 /* Add the last device. */
1669 if (port > 0) {
1670 snprintf(buf, sizeof(buf), "%d", port);
1671 } else {
1672 snprintf(buf, sizeof(buf), "?");
1673 }
1674 ret = func(opaque, bus_num, addr, buf, class_id, vendor_id,
1675 product_id, product_name, speed);
1676 }
1677 the_end:
1678 if (f) {
1679 fclose(f);
1680 }
1681 return ret;
1682 }
1683
1684 /*
1685 * Read sys file-system device file
1686 *
1687 * @line address of buffer to put file contents in
1688 * @line_size size of line
1689 * @device_file path to device file (printf format string)
1690 * @device_name device being opened (inserted into device_file)
1691 *
1692 * @return 0 failed, 1 succeeded ('line' contains data)
1693 */
1694 static int usb_host_read_file(char *line, size_t line_size,
1695 const char *device_file, const char *device_name)
1696 {
1697 FILE *f;
1698 int ret = 0;
1699 char filename[PATH_MAX];
1700
1701 snprintf(filename, PATH_MAX, USBSYSBUS_PATH "/devices/%s/%s", device_name,
1702 device_file);
1703 f = fopen(filename, "r");
1704 if (f) {
1705 ret = fgets(line, line_size, f) != NULL;
1706 fclose(f);
1707 }
1708
1709 return ret;
1710 }
1711
1712 /*
1713 * Use /sys/bus/usb/devices/ directory to determine host's USB
1714 * devices.
1715 *
1716 * This code is based on Robert Schiele's original patches posted to
1717 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1718 */
1719 static int usb_host_scan_sys(void *opaque, USBScanFunc *func)
1720 {
1721 DIR *dir = NULL;
1722 char line[1024];
1723 int bus_num, addr, speed, class_id, product_id, vendor_id;
1724 int ret = 0;
1725 char port[MAX_PORTLEN];
1726 char product_name[512];
1727 struct dirent *de;
1728
1729 dir = opendir(USBSYSBUS_PATH "/devices");
1730 if (!dir) {
1731 perror("husb: cannot open devices directory");
1732 goto the_end;
1733 }
1734
1735 while ((de = readdir(dir))) {
1736 if (de->d_name[0] != '.' && !strchr(de->d_name, ':')) {
1737 if (sscanf(de->d_name, "%d-%7[0-9.]", &bus_num, port) < 2) {
1738 continue;
1739 }
1740
1741 if (!usb_host_read_file(line, sizeof(line), "devnum", de->d_name)) {
1742 goto the_end;
1743 }
1744 if (sscanf(line, "%d", &addr) != 1) {
1745 goto the_end;
1746 }
1747 if (!usb_host_read_file(line, sizeof(line), "bDeviceClass",
1748 de->d_name)) {
1749 goto the_end;
1750 }
1751 if (sscanf(line, "%x", &class_id) != 1) {
1752 goto the_end;
1753 }
1754
1755 if (!usb_host_read_file(line, sizeof(line), "idVendor",
1756 de->d_name)) {
1757 goto the_end;
1758 }
1759 if (sscanf(line, "%x", &vendor_id) != 1) {
1760 goto the_end;
1761 }
1762 if (!usb_host_read_file(line, sizeof(line), "idProduct",
1763 de->d_name)) {
1764 goto the_end;
1765 }
1766 if (sscanf(line, "%x", &product_id) != 1) {
1767 goto the_end;
1768 }
1769 if (!usb_host_read_file(line, sizeof(line), "product",
1770 de->d_name)) {
1771 *product_name = 0;
1772 } else {
1773 if (strlen(line) > 0) {
1774 line[strlen(line) - 1] = '\0';
1775 }
1776 pstrcpy(product_name, sizeof(product_name), line);
1777 }
1778
1779 if (!usb_host_read_file(line, sizeof(line), "speed", de->d_name)) {
1780 goto the_end;
1781 }
1782 if (!strcmp(line, "5000\n")) {
1783 speed = USB_SPEED_SUPER;
1784 } else if (!strcmp(line, "480\n")) {
1785 speed = USB_SPEED_HIGH;
1786 } else if (!strcmp(line, "1.5\n")) {
1787 speed = USB_SPEED_LOW;
1788 } else {
1789 speed = USB_SPEED_FULL;
1790 }
1791
1792 ret = func(opaque, bus_num, addr, port, class_id, vendor_id,
1793 product_id, product_name, speed);
1794 if (ret) {
1795 goto the_end;
1796 }
1797 }
1798 }
1799 the_end:
1800 if (dir) {
1801 closedir(dir);
1802 }
1803 return ret;
1804 }
1805
1806 /*
1807 * Determine how to access the host's USB devices and call the
1808 * specific support function.
1809 */
1810 static int usb_host_scan(void *opaque, USBScanFunc *func)
1811 {
1812 Monitor *mon = cur_mon;
1813 FILE *f = NULL;
1814 DIR *dir = NULL;
1815 int ret = 0;
1816 const char *fs_type[] = {"unknown", "proc", "dev", "sys"};
1817 char devpath[PATH_MAX];
1818
1819 /* only check the host once */
1820 if (!usb_fs_type) {
1821 dir = opendir(USBSYSBUS_PATH "/devices");
1822 if (dir) {
1823 /* devices found in /dev/bus/usb/ (yes - not a mistake!) */
1824 strcpy(devpath, USBDEVBUS_PATH);
1825 usb_fs_type = USB_FS_SYS;
1826 closedir(dir);
1827 DPRINTF(USBDBG_DEVOPENED, USBSYSBUS_PATH);
1828 goto found_devices;
1829 }
1830 f = fopen(USBPROCBUS_PATH "/devices", "r");
1831 if (f) {
1832 /* devices found in /proc/bus/usb/ */
1833 strcpy(devpath, USBPROCBUS_PATH);
1834 usb_fs_type = USB_FS_PROC;
1835 fclose(f);
1836 DPRINTF(USBDBG_DEVOPENED, USBPROCBUS_PATH);
1837 goto found_devices;
1838 }
1839 /* try additional methods if an access method hasn't been found yet */
1840 f = fopen(USBDEVBUS_PATH "/devices", "r");
1841 if (f) {
1842 /* devices found in /dev/bus/usb/ */
1843 strcpy(devpath, USBDEVBUS_PATH);
1844 usb_fs_type = USB_FS_DEV;
1845 fclose(f);
1846 DPRINTF(USBDBG_DEVOPENED, USBDEVBUS_PATH);
1847 goto found_devices;
1848 }
1849 found_devices:
1850 if (!usb_fs_type) {
1851 if (mon) {
1852 monitor_printf(mon, "husb: unable to access USB devices\n");
1853 }
1854 return -ENOENT;
1855 }
1856
1857 /* the module setting (used later for opening devices) */
1858 usb_host_device_path = g_malloc0(strlen(devpath)+1);
1859 strcpy(usb_host_device_path, devpath);
1860 if (mon) {
1861 monitor_printf(mon, "husb: using %s file-system with %s\n",
1862 fs_type[usb_fs_type], usb_host_device_path);
1863 }
1864 }
1865
1866 switch (usb_fs_type) {
1867 case USB_FS_PROC:
1868 case USB_FS_DEV:
1869 ret = usb_host_scan_dev(opaque, func);
1870 break;
1871 case USB_FS_SYS:
1872 ret = usb_host_scan_sys(opaque, func);
1873 break;
1874 default:
1875 ret = -EINVAL;
1876 break;
1877 }
1878 return ret;
1879 }
1880
1881 static QEMUTimer *usb_auto_timer;
1882
1883 static int usb_host_auto_scan(void *opaque, int bus_num,
1884 int addr, const char *port,
1885 int class_id, int vendor_id, int product_id,
1886 const char *product_name, int speed)
1887 {
1888 struct USBAutoFilter *f;
1889 struct USBHostDevice *s;
1890
1891 /* Ignore hubs */
1892 if (class_id == 9)
1893 return 0;
1894
1895 QTAILQ_FOREACH(s, &hostdevs, next) {
1896 f = &s->match;
1897
1898 if (f->bus_num > 0 && f->bus_num != bus_num) {
1899 continue;
1900 }
1901 if (f->addr > 0 && f->addr != addr) {
1902 continue;
1903 }
1904 if (f->port != NULL && (port == NULL || strcmp(f->port, port) != 0)) {
1905 continue;
1906 }
1907
1908 if (f->vendor_id > 0 && f->vendor_id != vendor_id) {
1909 continue;
1910 }
1911
1912 if (f->product_id > 0 && f->product_id != product_id) {
1913 continue;
1914 }
1915 /* We got a match */
1916 s->seen++;
1917 if (s->errcount >= 3) {
1918 return 0;
1919 }
1920
1921 /* Already attached ? */
1922 if (s->fd != -1) {
1923 return 0;
1924 }
1925 DPRINTF("husb: auto open: bus_num %d addr %d\n", bus_num, addr);
1926
1927 if (usb_host_open(s, bus_num, addr, port, product_name, speed) < 0) {
1928 s->errcount++;
1929 }
1930 break;
1931 }
1932
1933 return 0;
1934 }
1935
1936 static void usb_host_auto_check(void *unused)
1937 {
1938 struct USBHostDevice *s;
1939 int unconnected = 0;
1940
1941 usb_host_scan(NULL, usb_host_auto_scan);
1942
1943 QTAILQ_FOREACH(s, &hostdevs, next) {
1944 if (s->fd == -1) {
1945 unconnected++;
1946 }
1947 if (s->seen == 0) {
1948 s->errcount = 0;
1949 }
1950 s->seen = 0;
1951 }
1952
1953 if (unconnected == 0) {
1954 /* nothing to watch */
1955 if (usb_auto_timer) {
1956 qemu_del_timer(usb_auto_timer);
1957 trace_usb_host_auto_scan_disabled();
1958 }
1959 return;
1960 }
1961
1962 if (!usb_auto_timer) {
1963 usb_auto_timer = qemu_new_timer_ms(rt_clock, usb_host_auto_check, NULL);
1964 if (!usb_auto_timer) {
1965 return;
1966 }
1967 trace_usb_host_auto_scan_enabled();
1968 }
1969 qemu_mod_timer(usb_auto_timer, qemu_get_clock_ms(rt_clock) + 2000);
1970 }
1971
1972 /*
1973 * Autoconnect filter
1974 * Format:
1975 * auto:bus:dev[:vid:pid]
1976 * auto:bus.dev[:vid:pid]
1977 *
1978 * bus - bus number (dec, * means any)
1979 * dev - device number (dec, * means any)
1980 * vid - vendor id (hex, * means any)
1981 * pid - product id (hex, * means any)
1982 *
1983 * See 'lsusb' output.
1984 */
1985 static int parse_filter(const char *spec, struct USBAutoFilter *f)
1986 {
1987 enum { BUS, DEV, VID, PID, DONE };
1988 const char *p = spec;
1989 int i;
1990
1991 f->bus_num = 0;
1992 f->addr = 0;
1993 f->vendor_id = 0;
1994 f->product_id = 0;
1995
1996 for (i = BUS; i < DONE; i++) {
1997 p = strpbrk(p, ":.");
1998 if (!p) {
1999 break;
2000 }
2001 p++;
2002
2003 if (*p == '*') {
2004 continue;
2005 }
2006 switch(i) {
2007 case BUS: f->bus_num = strtol(p, NULL, 10); break;
2008 case DEV: f->addr = strtol(p, NULL, 10); break;
2009 case VID: f->vendor_id = strtol(p, NULL, 16); break;
2010 case PID: f->product_id = strtol(p, NULL, 16); break;
2011 }
2012 }
2013
2014 if (i < DEV) {
2015 fprintf(stderr, "husb: invalid auto filter spec %s\n", spec);
2016 return -1;
2017 }
2018
2019 return 0;
2020 }
2021
2022 /**********************/
2023 /* USB host device info */
2024
2025 struct usb_class_info {
2026 int class;
2027 const char *class_name;
2028 };
2029
2030 static const struct usb_class_info usb_class_info[] = {
2031 { USB_CLASS_AUDIO, "Audio"},
2032 { USB_CLASS_COMM, "Communication"},
2033 { USB_CLASS_HID, "HID"},
2034 { USB_CLASS_HUB, "Hub" },
2035 { USB_CLASS_PHYSICAL, "Physical" },
2036 { USB_CLASS_PRINTER, "Printer" },
2037 { USB_CLASS_MASS_STORAGE, "Storage" },
2038 { USB_CLASS_CDC_DATA, "Data" },
2039 { USB_CLASS_APP_SPEC, "Application Specific" },
2040 { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
2041 { USB_CLASS_STILL_IMAGE, "Still Image" },
2042 { USB_CLASS_CSCID, "Smart Card" },
2043 { USB_CLASS_CONTENT_SEC, "Content Security" },
2044 { -1, NULL }
2045 };
2046
2047 static const char *usb_class_str(uint8_t class)
2048 {
2049 const struct usb_class_info *p;
2050 for(p = usb_class_info; p->class != -1; p++) {
2051 if (p->class == class) {
2052 break;
2053 }
2054 }
2055 return p->class_name;
2056 }
2057
2058 static void usb_info_device(Monitor *mon, int bus_num,
2059 int addr, const char *port,
2060 int class_id, int vendor_id, int product_id,
2061 const char *product_name,
2062 int speed)
2063 {
2064 const char *class_str, *speed_str;
2065
2066 switch(speed) {
2067 case USB_SPEED_LOW:
2068 speed_str = "1.5";
2069 break;
2070 case USB_SPEED_FULL:
2071 speed_str = "12";
2072 break;
2073 case USB_SPEED_HIGH:
2074 speed_str = "480";
2075 break;
2076 case USB_SPEED_SUPER:
2077 speed_str = "5000";
2078 break;
2079 default:
2080 speed_str = "?";
2081 break;
2082 }
2083
2084 monitor_printf(mon, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
2085 bus_num, addr, port, speed_str);
2086 class_str = usb_class_str(class_id);
2087 if (class_str) {
2088 monitor_printf(mon, " %s:", class_str);
2089 } else {
2090 monitor_printf(mon, " Class %02x:", class_id);
2091 }
2092 monitor_printf(mon, " USB device %04x:%04x", vendor_id, product_id);
2093 if (product_name[0] != '\0') {
2094 monitor_printf(mon, ", %s", product_name);
2095 }
2096 monitor_printf(mon, "\n");
2097 }
2098
2099 static int usb_host_info_device(void *opaque, int bus_num, int addr,
2100 const char *path, int class_id,
2101 int vendor_id, int product_id,
2102 const char *product_name,
2103 int speed)
2104 {
2105 Monitor *mon = opaque;
2106
2107 usb_info_device(mon, bus_num, addr, path, class_id, vendor_id, product_id,
2108 product_name, speed);
2109 return 0;
2110 }
2111
2112 static void dec2str(int val, char *str, size_t size)
2113 {
2114 if (val == 0) {
2115 snprintf(str, size, "*");
2116 } else {
2117 snprintf(str, size, "%d", val);
2118 }
2119 }
2120
2121 static void hex2str(int val, char *str, size_t size)
2122 {
2123 if (val == 0) {
2124 snprintf(str, size, "*");
2125 } else {
2126 snprintf(str, size, "%04x", val);
2127 }
2128 }
2129
2130 void usb_host_info(Monitor *mon)
2131 {
2132 struct USBAutoFilter *f;
2133 struct USBHostDevice *s;
2134
2135 usb_host_scan(mon, usb_host_info_device);
2136
2137 if (QTAILQ_EMPTY(&hostdevs)) {
2138 return;
2139 }
2140
2141 monitor_printf(mon, " Auto filters:\n");
2142 QTAILQ_FOREACH(s, &hostdevs, next) {
2143 char bus[10], addr[10], vid[10], pid[10];
2144 f = &s->match;
2145 dec2str(f->bus_num, bus, sizeof(bus));
2146 dec2str(f->addr, addr, sizeof(addr));
2147 hex2str(f->vendor_id, vid, sizeof(vid));
2148 hex2str(f->product_id, pid, sizeof(pid));
2149 monitor_printf(mon, " Bus %s, Addr %s, Port %s, ID %s:%s\n",
2150 bus, addr, f->port ? f->port : "*", vid, pid);
2151 }
2152 }