]> git.proxmox.com Git - qemu.git/blob - usb-linux.c
make qemu_thread_create block all signals
[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
37 #include <dirent.h>
38 #include <sys/ioctl.h>
39 #include <signal.h>
40
41 #include <linux/usbdevice_fs.h>
42 #include <linux/version.h>
43 #include "hw/usb.h"
44
45 /* We redefine it to avoid version problems */
46 struct usb_ctrltransfer {
47 uint8_t bRequestType;
48 uint8_t bRequest;
49 uint16_t wValue;
50 uint16_t wIndex;
51 uint16_t wLength;
52 uint32_t timeout;
53 void *data;
54 };
55
56 struct usb_ctrlrequest {
57 uint8_t bRequestType;
58 uint8_t bRequest;
59 uint16_t wValue;
60 uint16_t wIndex;
61 uint16_t wLength;
62 };
63
64 typedef int USBScanFunc(void *opaque, int bus_num, int addr, int class_id,
65 int vendor_id, int product_id,
66 const char *product_name, int speed);
67
68 //#define DEBUG
69
70 #ifdef DEBUG
71 #define DPRINTF printf
72 #else
73 #define DPRINTF(...)
74 #endif
75
76 #define USBDBG_DEVOPENED "husb: opened %s/devices\n"
77
78 #define USBPROCBUS_PATH "/proc/bus/usb"
79 #define PRODUCT_NAME_SZ 32
80 #define MAX_ENDPOINTS 16
81 #define USBDEVBUS_PATH "/dev/bus/usb"
82 #define USBSYSBUS_PATH "/sys/bus/usb"
83
84 static char *usb_host_device_path;
85
86 #define USB_FS_NONE 0
87 #define USB_FS_PROC 1
88 #define USB_FS_DEV 2
89 #define USB_FS_SYS 3
90
91 static int usb_fs_type;
92
93 /* endpoint association data */
94 struct endp_data {
95 uint8_t type;
96 uint8_t halted;
97 };
98
99 enum {
100 CTRL_STATE_IDLE = 0,
101 CTRL_STATE_SETUP,
102 CTRL_STATE_DATA,
103 CTRL_STATE_ACK
104 };
105
106 /*
107 * Control transfer state.
108 * Note that 'buffer' _must_ follow 'req' field because
109 * we need contigious buffer when we submit control URB.
110 */
111 struct ctrl_struct {
112 uint16_t len;
113 uint16_t offset;
114 uint8_t state;
115 struct usb_ctrlrequest req;
116 uint8_t buffer[8192];
117 };
118
119 struct USBAutoFilter {
120 uint32_t bus_num;
121 uint32_t addr;
122 uint32_t vendor_id;
123 uint32_t product_id;
124 };
125
126 typedef struct USBHostDevice {
127 USBDevice dev;
128 int fd;
129
130 uint8_t descr[1024];
131 int descr_len;
132 int configuration;
133 int ninterfaces;
134 int closing;
135
136 struct ctrl_struct ctrl;
137 struct endp_data endp_table[MAX_ENDPOINTS];
138
139 /* Host side address */
140 int bus_num;
141 int addr;
142 struct USBAutoFilter match;
143
144 QTAILQ_ENTRY(USBHostDevice) next;
145 } USBHostDevice;
146
147 static QTAILQ_HEAD(, USBHostDevice) hostdevs = QTAILQ_HEAD_INITIALIZER(hostdevs);
148
149 static int usb_host_close(USBHostDevice *dev);
150 static int parse_filter(const char *spec, struct USBAutoFilter *f);
151 static void usb_host_auto_check(void *unused);
152
153 static int is_isoc(USBHostDevice *s, int ep)
154 {
155 return s->endp_table[ep - 1].type == USBDEVFS_URB_TYPE_ISO;
156 }
157
158 static int is_halted(USBHostDevice *s, int ep)
159 {
160 return s->endp_table[ep - 1].halted;
161 }
162
163 static void clear_halt(USBHostDevice *s, int ep)
164 {
165 s->endp_table[ep - 1].halted = 0;
166 }
167
168 static void set_halt(USBHostDevice *s, int ep)
169 {
170 s->endp_table[ep - 1].halted = 1;
171 }
172
173 /*
174 * Async URB state.
175 * We always allocate one isoc descriptor even for bulk transfers
176 * to simplify allocation and casts.
177 */
178 typedef struct AsyncURB
179 {
180 struct usbdevfs_urb urb;
181 struct usbdevfs_iso_packet_desc isocpd;
182
183 USBPacket *packet;
184 USBHostDevice *hdev;
185 } AsyncURB;
186
187 static AsyncURB *async_alloc(void)
188 {
189 return (AsyncURB *) qemu_mallocz(sizeof(AsyncURB));
190 }
191
192 static void async_free(AsyncURB *aurb)
193 {
194 qemu_free(aurb);
195 }
196
197 static void async_complete_ctrl(USBHostDevice *s, USBPacket *p)
198 {
199 switch(s->ctrl.state) {
200 case CTRL_STATE_SETUP:
201 if (p->len < s->ctrl.len)
202 s->ctrl.len = p->len;
203 s->ctrl.state = CTRL_STATE_DATA;
204 p->len = 8;
205 break;
206
207 case CTRL_STATE_ACK:
208 s->ctrl.state = CTRL_STATE_IDLE;
209 p->len = 0;
210 break;
211
212 default:
213 break;
214 }
215 }
216
217 static void async_complete(void *opaque)
218 {
219 USBHostDevice *s = opaque;
220 AsyncURB *aurb;
221
222 while (1) {
223 USBPacket *p;
224
225 int r = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &aurb);
226 if (r < 0) {
227 if (errno == EAGAIN) {
228 return;
229 }
230 if (errno == ENODEV && !s->closing) {
231 printf("husb: device %d.%d disconnected\n",
232 s->bus_num, s->addr);
233 usb_host_close(s);
234 usb_host_auto_check(NULL);
235 return;
236 }
237
238 DPRINTF("husb: async. reap urb failed errno %d\n", errno);
239 return;
240 }
241
242 p = aurb->packet;
243
244 DPRINTF("husb: async completed. aurb %p status %d alen %d\n",
245 aurb, aurb->urb.status, aurb->urb.actual_length);
246
247 if (p) {
248 switch (aurb->urb.status) {
249 case 0:
250 p->len = aurb->urb.actual_length;
251 if (aurb->urb.type == USBDEVFS_URB_TYPE_CONTROL) {
252 async_complete_ctrl(s, p);
253 }
254 break;
255
256 case -EPIPE:
257 set_halt(s, p->devep);
258 p->len = USB_RET_STALL;
259 break;
260
261 default:
262 p->len = USB_RET_NAK;
263 break;
264 }
265
266 usb_packet_complete(p);
267 }
268
269 async_free(aurb);
270 }
271 }
272
273 static void async_cancel(USBPacket *unused, void *opaque)
274 {
275 AsyncURB *aurb = opaque;
276 USBHostDevice *s = aurb->hdev;
277
278 DPRINTF("husb: async cancel. aurb %p\n", aurb);
279
280 /* Mark it as dead (see async_complete above) */
281 aurb->packet = NULL;
282
283 int r = ioctl(s->fd, USBDEVFS_DISCARDURB, aurb);
284 if (r < 0) {
285 DPRINTF("husb: async. discard urb failed errno %d\n", errno);
286 }
287 }
288
289 static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration)
290 {
291 int dev_descr_len, config_descr_len;
292 int interface, nb_interfaces;
293 int ret, i;
294
295 if (configuration == 0) /* address state - ignore */
296 return 1;
297
298 DPRINTF("husb: claiming interfaces. config %d\n", configuration);
299
300 i = 0;
301 dev_descr_len = dev->descr[0];
302 if (dev_descr_len > dev->descr_len) {
303 goto fail;
304 }
305
306 i += dev_descr_len;
307 while (i < dev->descr_len) {
308 DPRINTF("husb: i is %d, descr_len is %d, dl %d, dt %d\n",
309 i, dev->descr_len,
310 dev->descr[i], dev->descr[i+1]);
311
312 if (dev->descr[i+1] != USB_DT_CONFIG) {
313 i += dev->descr[i];
314 continue;
315 }
316 config_descr_len = dev->descr[i];
317
318 printf("husb: config #%d need %d\n", dev->descr[i + 5], configuration);
319
320 if (configuration < 0 || configuration == dev->descr[i + 5]) {
321 configuration = dev->descr[i + 5];
322 break;
323 }
324
325 i += config_descr_len;
326 }
327
328 if (i >= dev->descr_len) {
329 fprintf(stderr,
330 "husb: update iface failed. no matching configuration\n");
331 goto fail;
332 }
333 nb_interfaces = dev->descr[i + 4];
334
335 #ifdef USBDEVFS_DISCONNECT
336 /* earlier Linux 2.4 do not support that */
337 {
338 struct usbdevfs_ioctl ctrl;
339 for (interface = 0; interface < nb_interfaces; interface++) {
340 ctrl.ioctl_code = USBDEVFS_DISCONNECT;
341 ctrl.ifno = interface;
342 ret = ioctl(dev->fd, USBDEVFS_IOCTL, &ctrl);
343 if (ret < 0 && errno != ENODATA) {
344 perror("USBDEVFS_DISCONNECT");
345 goto fail;
346 }
347 }
348 }
349 #endif
350
351 /* XXX: only grab if all interfaces are free */
352 for (interface = 0; interface < nb_interfaces; interface++) {
353 ret = ioctl(dev->fd, USBDEVFS_CLAIMINTERFACE, &interface);
354 if (ret < 0) {
355 if (errno == EBUSY) {
356 printf("husb: update iface. device already grabbed\n");
357 } else {
358 perror("husb: failed to claim interface");
359 }
360 fail:
361 return 0;
362 }
363 }
364
365 printf("husb: %d interfaces claimed for configuration %d\n",
366 nb_interfaces, configuration);
367
368 dev->ninterfaces = nb_interfaces;
369 dev->configuration = configuration;
370 return 1;
371 }
372
373 static int usb_host_release_interfaces(USBHostDevice *s)
374 {
375 int ret, i;
376
377 DPRINTF("husb: releasing interfaces\n");
378
379 for (i = 0; i < s->ninterfaces; i++) {
380 ret = ioctl(s->fd, USBDEVFS_RELEASEINTERFACE, &i);
381 if (ret < 0) {
382 perror("husb: failed to release interface");
383 return 0;
384 }
385 }
386
387 return 1;
388 }
389
390 static void usb_host_handle_reset(USBDevice *dev)
391 {
392 USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
393
394 DPRINTF("husb: reset device %u.%u\n", s->bus_num, s->addr);
395
396 ioctl(s->fd, USBDEVFS_RESET);
397
398 usb_host_claim_interfaces(s, s->configuration);
399 }
400
401 static void usb_host_handle_destroy(USBDevice *dev)
402 {
403 USBHostDevice *s = (USBHostDevice *)dev;
404
405 usb_host_close(s);
406 QTAILQ_REMOVE(&hostdevs, s, next);
407 }
408
409 static int usb_linux_update_endp_table(USBHostDevice *s);
410
411 static int usb_host_handle_data(USBHostDevice *s, USBPacket *p)
412 {
413 struct usbdevfs_urb *urb;
414 AsyncURB *aurb;
415 int ret;
416
417 aurb = async_alloc();
418 aurb->hdev = s;
419 aurb->packet = p;
420
421 urb = &aurb->urb;
422
423 if (p->pid == USB_TOKEN_IN) {
424 urb->endpoint = p->devep | 0x80;
425 } else {
426 urb->endpoint = p->devep;
427 }
428
429 if (is_halted(s, p->devep)) {
430 ret = ioctl(s->fd, USBDEVFS_CLEAR_HALT, &urb->endpoint);
431 if (ret < 0) {
432 DPRINTF("husb: failed to clear halt. ep 0x%x errno %d\n",
433 urb->endpoint, errno);
434 return USB_RET_NAK;
435 }
436 clear_halt(s, p->devep);
437 }
438
439 urb->buffer = p->data;
440 urb->buffer_length = p->len;
441
442 if (is_isoc(s, p->devep)) {
443 /* Setup ISOC transfer */
444 urb->type = USBDEVFS_URB_TYPE_ISO;
445 urb->flags = USBDEVFS_URB_ISO_ASAP;
446 urb->number_of_packets = 1;
447 urb->iso_frame_desc[0].length = p->len;
448 } else {
449 /* Setup bulk transfer */
450 urb->type = USBDEVFS_URB_TYPE_BULK;
451 }
452
453 urb->usercontext = s;
454
455 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
456
457 DPRINTF("husb: data submit. ep 0x%x len %u aurb %p\n",
458 urb->endpoint, p->len, aurb);
459
460 if (ret < 0) {
461 DPRINTF("husb: submit failed. errno %d\n", errno);
462 async_free(aurb);
463
464 switch(errno) {
465 case ETIMEDOUT:
466 return USB_RET_NAK;
467 case EPIPE:
468 default:
469 return USB_RET_STALL;
470 }
471 }
472
473 usb_defer_packet(p, async_cancel, aurb);
474 return USB_RET_ASYNC;
475 }
476
477 static int ctrl_error(void)
478 {
479 if (errno == ETIMEDOUT) {
480 return USB_RET_NAK;
481 } else {
482 return USB_RET_STALL;
483 }
484 }
485
486 static int usb_host_set_address(USBHostDevice *s, int addr)
487 {
488 DPRINTF("husb: ctrl set addr %u\n", addr);
489 s->dev.addr = addr;
490 return 0;
491 }
492
493 static int usb_host_set_config(USBHostDevice *s, int config)
494 {
495 usb_host_release_interfaces(s);
496
497 int ret = ioctl(s->fd, USBDEVFS_SETCONFIGURATION, &config);
498
499 DPRINTF("husb: ctrl set config %d ret %d errno %d\n", config, ret, errno);
500
501 if (ret < 0) {
502 return ctrl_error();
503 }
504 usb_host_claim_interfaces(s, config);
505 return 0;
506 }
507
508 static int usb_host_set_interface(USBHostDevice *s, int iface, int alt)
509 {
510 struct usbdevfs_setinterface si;
511 int ret;
512
513 si.interface = iface;
514 si.altsetting = alt;
515 ret = ioctl(s->fd, USBDEVFS_SETINTERFACE, &si);
516
517 DPRINTF("husb: ctrl set iface %d altset %d ret %d errno %d\n",
518 iface, alt, ret, errno);
519
520 if (ret < 0) {
521 return ctrl_error();
522 }
523 usb_linux_update_endp_table(s);
524 return 0;
525 }
526
527 static int usb_host_handle_control(USBHostDevice *s, USBPacket *p)
528 {
529 struct usbdevfs_urb *urb;
530 AsyncURB *aurb;
531 int ret, value, index;
532 int buffer_len;
533
534 /*
535 * Process certain standard device requests.
536 * These are infrequent and are processed synchronously.
537 */
538 value = le16_to_cpu(s->ctrl.req.wValue);
539 index = le16_to_cpu(s->ctrl.req.wIndex);
540
541 DPRINTF("husb: ctrl type 0x%x req 0x%x val 0x%x index %u len %u\n",
542 s->ctrl.req.bRequestType, s->ctrl.req.bRequest, value, index,
543 s->ctrl.len);
544
545 if (s->ctrl.req.bRequestType == 0) {
546 switch (s->ctrl.req.bRequest) {
547 case USB_REQ_SET_ADDRESS:
548 return usb_host_set_address(s, value);
549
550 case USB_REQ_SET_CONFIGURATION:
551 return usb_host_set_config(s, value & 0xff);
552 }
553 }
554
555 if (s->ctrl.req.bRequestType == 1 &&
556 s->ctrl.req.bRequest == USB_REQ_SET_INTERFACE) {
557 return usb_host_set_interface(s, index, value);
558 }
559
560 /* The rest are asynchronous */
561
562 buffer_len = 8 + s->ctrl.len;
563 if (buffer_len > sizeof(s->ctrl.buffer)) {
564 fprintf(stderr, "husb: ctrl buffer too small (%u > %zu)\n",
565 buffer_len, sizeof(s->ctrl.buffer));
566 return USB_RET_STALL;
567 }
568
569 aurb = async_alloc();
570 aurb->hdev = s;
571 aurb->packet = p;
572
573 /*
574 * Setup ctrl transfer.
575 *
576 * s->ctrl is layed out such that data buffer immediately follows
577 * 'req' struct which is exactly what usbdevfs expects.
578 */
579 urb = &aurb->urb;
580
581 urb->type = USBDEVFS_URB_TYPE_CONTROL;
582 urb->endpoint = p->devep;
583
584 urb->buffer = &s->ctrl.req;
585 urb->buffer_length = buffer_len;
586
587 urb->usercontext = s;
588
589 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
590
591 DPRINTF("husb: submit ctrl. len %u aurb %p\n", urb->buffer_length, aurb);
592
593 if (ret < 0) {
594 DPRINTF("husb: submit failed. errno %d\n", errno);
595 async_free(aurb);
596
597 switch(errno) {
598 case ETIMEDOUT:
599 return USB_RET_NAK;
600 case EPIPE:
601 default:
602 return USB_RET_STALL;
603 }
604 }
605
606 usb_defer_packet(p, async_cancel, aurb);
607 return USB_RET_ASYNC;
608 }
609
610 static int do_token_setup(USBDevice *dev, USBPacket *p)
611 {
612 USBHostDevice *s = (USBHostDevice *) dev;
613 int ret = 0;
614
615 if (p->len != 8) {
616 return USB_RET_STALL;
617 }
618
619 memcpy(&s->ctrl.req, p->data, 8);
620 s->ctrl.len = le16_to_cpu(s->ctrl.req.wLength);
621 s->ctrl.offset = 0;
622 s->ctrl.state = CTRL_STATE_SETUP;
623
624 if (s->ctrl.req.bRequestType & USB_DIR_IN) {
625 ret = usb_host_handle_control(s, p);
626 if (ret < 0) {
627 return ret;
628 }
629
630 if (ret < s->ctrl.len) {
631 s->ctrl.len = ret;
632 }
633 s->ctrl.state = CTRL_STATE_DATA;
634 } else {
635 if (s->ctrl.len == 0) {
636 s->ctrl.state = CTRL_STATE_ACK;
637 } else {
638 s->ctrl.state = CTRL_STATE_DATA;
639 }
640 }
641
642 return ret;
643 }
644
645 static int do_token_in(USBDevice *dev, USBPacket *p)
646 {
647 USBHostDevice *s = (USBHostDevice *) dev;
648 int ret = 0;
649
650 if (p->devep != 0) {
651 return usb_host_handle_data(s, p);
652 }
653
654 switch(s->ctrl.state) {
655 case CTRL_STATE_ACK:
656 if (!(s->ctrl.req.bRequestType & USB_DIR_IN)) {
657 ret = usb_host_handle_control(s, p);
658 if (ret == USB_RET_ASYNC) {
659 return USB_RET_ASYNC;
660 }
661 s->ctrl.state = CTRL_STATE_IDLE;
662 return ret > 0 ? 0 : ret;
663 }
664
665 return 0;
666
667 case CTRL_STATE_DATA:
668 if (s->ctrl.req.bRequestType & USB_DIR_IN) {
669 int len = s->ctrl.len - s->ctrl.offset;
670 if (len > p->len) {
671 len = p->len;
672 }
673 memcpy(p->data, s->ctrl.buffer + s->ctrl.offset, len);
674 s->ctrl.offset += len;
675 if (s->ctrl.offset >= s->ctrl.len) {
676 s->ctrl.state = CTRL_STATE_ACK;
677 }
678 return len;
679 }
680
681 s->ctrl.state = CTRL_STATE_IDLE;
682 return USB_RET_STALL;
683
684 default:
685 return USB_RET_STALL;
686 }
687 }
688
689 static int do_token_out(USBDevice *dev, USBPacket *p)
690 {
691 USBHostDevice *s = (USBHostDevice *) dev;
692
693 if (p->devep != 0) {
694 return usb_host_handle_data(s, p);
695 }
696
697 switch(s->ctrl.state) {
698 case CTRL_STATE_ACK:
699 if (s->ctrl.req.bRequestType & USB_DIR_IN) {
700 s->ctrl.state = CTRL_STATE_IDLE;
701 /* transfer OK */
702 } else {
703 /* ignore additional output */
704 }
705 return 0;
706
707 case CTRL_STATE_DATA:
708 if (!(s->ctrl.req.bRequestType & USB_DIR_IN)) {
709 int len = s->ctrl.len - s->ctrl.offset;
710 if (len > p->len) {
711 len = p->len;
712 }
713 memcpy(s->ctrl.buffer + s->ctrl.offset, p->data, len);
714 s->ctrl.offset += len;
715 if (s->ctrl.offset >= s->ctrl.len) {
716 s->ctrl.state = CTRL_STATE_ACK;
717 }
718 return len;
719 }
720
721 s->ctrl.state = CTRL_STATE_IDLE;
722 return USB_RET_STALL;
723
724 default:
725 return USB_RET_STALL;
726 }
727 }
728
729 /*
730 * Packet handler.
731 * Called by the HC (host controller).
732 *
733 * Returns length of the transaction or one of the USB_RET_XXX codes.
734 */
735 static int usb_host_handle_packet(USBDevice *s, USBPacket *p)
736 {
737 switch(p->pid) {
738 case USB_MSG_ATTACH:
739 s->state = USB_STATE_ATTACHED;
740 return 0;
741
742 case USB_MSG_DETACH:
743 s->state = USB_STATE_NOTATTACHED;
744 return 0;
745
746 case USB_MSG_RESET:
747 s->remote_wakeup = 0;
748 s->addr = 0;
749 s->state = USB_STATE_DEFAULT;
750 s->info->handle_reset(s);
751 return 0;
752 }
753
754 /* Rest of the PIDs must match our address */
755 if (s->state < USB_STATE_DEFAULT || p->devaddr != s->addr) {
756 return USB_RET_NODEV;
757 }
758
759 switch (p->pid) {
760 case USB_TOKEN_SETUP:
761 return do_token_setup(s, p);
762
763 case USB_TOKEN_IN:
764 return do_token_in(s, p);
765
766 case USB_TOKEN_OUT:
767 return do_token_out(s, p);
768
769 default:
770 return USB_RET_STALL;
771 }
772 }
773
774 /* returns 1 on problem encountered or 0 for success */
775 static int usb_linux_update_endp_table(USBHostDevice *s)
776 {
777 uint8_t *descriptors;
778 uint8_t devep, type, configuration, alt_interface;
779 struct usb_ctrltransfer ct;
780 int interface, ret, length, i;
781
782 ct.bRequestType = USB_DIR_IN;
783 ct.bRequest = USB_REQ_GET_CONFIGURATION;
784 ct.wValue = 0;
785 ct.wIndex = 0;
786 ct.wLength = 1;
787 ct.data = &configuration;
788 ct.timeout = 50;
789
790 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
791 if (ret < 0) {
792 perror("usb_linux_update_endp_table");
793 return 1;
794 }
795
796 /* in address state */
797 if (configuration == 0) {
798 return 1;
799 }
800
801 /* get the desired configuration, interface, and endpoint descriptors
802 * from device description */
803 descriptors = &s->descr[18];
804 length = s->descr_len - 18;
805 i = 0;
806
807 if (descriptors[i + 1] != USB_DT_CONFIG ||
808 descriptors[i + 5] != configuration) {
809 DPRINTF("invalid descriptor data - configuration\n");
810 return 1;
811 }
812 i += descriptors[i];
813
814 while (i < length) {
815 if (descriptors[i + 1] != USB_DT_INTERFACE ||
816 (descriptors[i + 1] == USB_DT_INTERFACE &&
817 descriptors[i + 4] == 0)) {
818 i += descriptors[i];
819 continue;
820 }
821
822 interface = descriptors[i + 2];
823
824 ct.bRequestType = USB_DIR_IN | USB_RECIP_INTERFACE;
825 ct.bRequest = USB_REQ_GET_INTERFACE;
826 ct.wValue = 0;
827 ct.wIndex = interface;
828 ct.wLength = 1;
829 ct.data = &alt_interface;
830 ct.timeout = 50;
831
832 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
833 if (ret < 0) {
834 alt_interface = interface;
835 }
836
837 /* the current interface descriptor is the active interface
838 * and has endpoints */
839 if (descriptors[i + 3] != alt_interface) {
840 i += descriptors[i];
841 continue;
842 }
843
844 /* advance to the endpoints */
845 while (i < length && descriptors[i +1] != USB_DT_ENDPOINT) {
846 i += descriptors[i];
847 }
848
849 if (i >= length)
850 break;
851
852 while (i < length) {
853 if (descriptors[i + 1] != USB_DT_ENDPOINT) {
854 break;
855 }
856
857 devep = descriptors[i + 2];
858 switch (descriptors[i + 3] & 0x3) {
859 case 0x00:
860 type = USBDEVFS_URB_TYPE_CONTROL;
861 break;
862 case 0x01:
863 type = USBDEVFS_URB_TYPE_ISO;
864 break;
865 case 0x02:
866 type = USBDEVFS_URB_TYPE_BULK;
867 break;
868 case 0x03:
869 type = USBDEVFS_URB_TYPE_INTERRUPT;
870 break;
871 default:
872 DPRINTF("usb_host: malformed endpoint type\n");
873 type = USBDEVFS_URB_TYPE_BULK;
874 }
875 s->endp_table[(devep & 0xf) - 1].type = type;
876 s->endp_table[(devep & 0xf) - 1].halted = 0;
877
878 i += descriptors[i];
879 }
880 }
881 return 0;
882 }
883
884 static int usb_host_open(USBHostDevice *dev, int bus_num,
885 int addr, const char *prod_name)
886 {
887 int fd = -1, ret;
888 struct usbdevfs_connectinfo ci;
889 char buf[1024];
890
891 if (dev->fd != -1) {
892 goto fail;
893 }
894 printf("husb: open device %d.%d\n", bus_num, addr);
895
896 if (!usb_host_device_path) {
897 perror("husb: USB Host Device Path not set");
898 goto fail;
899 }
900 snprintf(buf, sizeof(buf), "%s/%03d/%03d", usb_host_device_path,
901 bus_num, addr);
902 fd = open(buf, O_RDWR | O_NONBLOCK);
903 if (fd < 0) {
904 perror(buf);
905 goto fail;
906 }
907 DPRINTF("husb: opened %s\n", buf);
908
909 dev->bus_num = bus_num;
910 dev->addr = addr;
911 dev->fd = fd;
912
913 /* read the device description */
914 dev->descr_len = read(fd, dev->descr, sizeof(dev->descr));
915 if (dev->descr_len <= 0) {
916 perror("husb: reading device data failed");
917 goto fail;
918 }
919
920 #ifdef DEBUG
921 {
922 int x;
923 printf("=== begin dumping device descriptor data ===\n");
924 for (x = 0; x < dev->descr_len; x++) {
925 printf("%02x ", dev->descr[x]);
926 }
927 printf("\n=== end dumping device descriptor data ===\n");
928 }
929 #endif
930
931
932 /*
933 * Initial configuration is -1 which makes us claim first
934 * available config. We used to start with 1, which does not
935 * always work. I've seen devices where first config starts
936 * with 2.
937 */
938 if (!usb_host_claim_interfaces(dev, -1)) {
939 goto fail;
940 }
941
942 ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci);
943 if (ret < 0) {
944 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
945 goto fail;
946 }
947
948 printf("husb: grabbed usb device %d.%d\n", bus_num, addr);
949
950 ret = usb_linux_update_endp_table(dev);
951 if (ret) {
952 goto fail;
953 }
954
955 if (ci.slow) {
956 dev->dev.speed = USB_SPEED_LOW;
957 } else {
958 dev->dev.speed = USB_SPEED_HIGH;
959 }
960
961 if (!prod_name || prod_name[0] == '\0') {
962 snprintf(dev->dev.product_desc, sizeof(dev->dev.product_desc),
963 "host:%d.%d", bus_num, addr);
964 } else {
965 pstrcpy(dev->dev.product_desc, sizeof(dev->dev.product_desc),
966 prod_name);
967 }
968
969 /* USB devio uses 'write' flag to check for async completions */
970 qemu_set_fd_handler(dev->fd, NULL, async_complete, dev);
971
972 usb_device_attach(&dev->dev);
973 return 0;
974
975 fail:
976 dev->fd = -1;
977 if (fd != -1) {
978 close(fd);
979 }
980 return -1;
981 }
982
983 static int usb_host_close(USBHostDevice *dev)
984 {
985 if (dev->fd == -1) {
986 return -1;
987 }
988
989 qemu_set_fd_handler(dev->fd, NULL, NULL, NULL);
990 dev->closing = 1;
991 async_complete(dev);
992 dev->closing = 0;
993 usb_device_detach(&dev->dev);
994 close(dev->fd);
995 dev->fd = -1;
996 return 0;
997 }
998
999 static int usb_host_initfn(USBDevice *dev)
1000 {
1001 USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
1002
1003 dev->auto_attach = 0;
1004 s->fd = -1;
1005 QTAILQ_INSERT_TAIL(&hostdevs, s, next);
1006 usb_host_auto_check(NULL);
1007 return 0;
1008 }
1009
1010 static struct USBDeviceInfo usb_host_dev_info = {
1011 .product_desc = "USB Host Device",
1012 .qdev.name = "usb-host",
1013 .qdev.size = sizeof(USBHostDevice),
1014 .init = usb_host_initfn,
1015 .handle_packet = usb_host_handle_packet,
1016 .handle_reset = usb_host_handle_reset,
1017 .handle_destroy = usb_host_handle_destroy,
1018 .usbdevice_name = "host",
1019 .usbdevice_init = usb_host_device_open,
1020 .qdev.props = (Property[]) {
1021 DEFINE_PROP_UINT32("hostbus", USBHostDevice, match.bus_num, 0),
1022 DEFINE_PROP_UINT32("hostaddr", USBHostDevice, match.addr, 0),
1023 DEFINE_PROP_HEX32("vendorid", USBHostDevice, match.vendor_id, 0),
1024 DEFINE_PROP_HEX32("productid", USBHostDevice, match.product_id, 0),
1025 DEFINE_PROP_END_OF_LIST(),
1026 },
1027 };
1028
1029 static void usb_host_register_devices(void)
1030 {
1031 usb_qdev_register(&usb_host_dev_info);
1032 }
1033 device_init(usb_host_register_devices)
1034
1035 USBDevice *usb_host_device_open(const char *devname)
1036 {
1037 struct USBAutoFilter filter;
1038 USBDevice *dev;
1039 char *p;
1040
1041 dev = usb_create(NULL /* FIXME */, "usb-host");
1042
1043 if (strstr(devname, "auto:")) {
1044 if (parse_filter(devname, &filter) < 0) {
1045 goto fail;
1046 }
1047 } else {
1048 if ((p = strchr(devname, '.'))) {
1049 filter.bus_num = strtoul(devname, NULL, 0);
1050 filter.addr = strtoul(p + 1, NULL, 0);
1051 filter.vendor_id = 0;
1052 filter.product_id = 0;
1053 } else if ((p = strchr(devname, ':'))) {
1054 filter.bus_num = 0;
1055 filter.addr = 0;
1056 filter.vendor_id = strtoul(devname, NULL, 16);
1057 filter.product_id = strtoul(p + 1, NULL, 16);
1058 } else {
1059 goto fail;
1060 }
1061 }
1062
1063 qdev_prop_set_uint32(&dev->qdev, "hostbus", filter.bus_num);
1064 qdev_prop_set_uint32(&dev->qdev, "hostaddr", filter.addr);
1065 qdev_prop_set_uint32(&dev->qdev, "vendorid", filter.vendor_id);
1066 qdev_prop_set_uint32(&dev->qdev, "productid", filter.product_id);
1067 qdev_init_nofail(&dev->qdev);
1068 return dev;
1069
1070 fail:
1071 qdev_free(&dev->qdev);
1072 return NULL;
1073 }
1074
1075 int usb_host_device_close(const char *devname)
1076 {
1077 #if 0
1078 char product_name[PRODUCT_NAME_SZ];
1079 int bus_num, addr;
1080 USBHostDevice *s;
1081
1082 if (strstr(devname, "auto:")) {
1083 return usb_host_auto_del(devname);
1084 }
1085 if (usb_host_find_device(&bus_num, &addr, product_name,
1086 sizeof(product_name), devname) < 0) {
1087 return -1;
1088 }
1089 s = hostdev_find(bus_num, addr);
1090 if (s) {
1091 usb_device_delete_addr(s->bus_num, s->dev.addr);
1092 return 0;
1093 }
1094 #endif
1095
1096 return -1;
1097 }
1098
1099 static int get_tag_value(char *buf, int buf_size,
1100 const char *str, const char *tag,
1101 const char *stopchars)
1102 {
1103 const char *p;
1104 char *q;
1105 p = strstr(str, tag);
1106 if (!p) {
1107 return -1;
1108 }
1109 p += strlen(tag);
1110 while (qemu_isspace(*p)) {
1111 p++;
1112 }
1113 q = buf;
1114 while (*p != '\0' && !strchr(stopchars, *p)) {
1115 if ((q - buf) < (buf_size - 1)) {
1116 *q++ = *p;
1117 }
1118 p++;
1119 }
1120 *q = '\0';
1121 return q - buf;
1122 }
1123
1124 /*
1125 * Use /proc/bus/usb/devices or /dev/bus/usb/devices file to determine
1126 * host's USB devices. This is legacy support since many distributions
1127 * are moving to /sys/bus/usb
1128 */
1129 static int usb_host_scan_dev(void *opaque, USBScanFunc *func)
1130 {
1131 FILE *f = NULL;
1132 char line[1024];
1133 char buf[1024];
1134 int bus_num, addr, speed, device_count, class_id, product_id, vendor_id;
1135 char product_name[512];
1136 int ret = 0;
1137
1138 if (!usb_host_device_path) {
1139 perror("husb: USB Host Device Path not set");
1140 goto the_end;
1141 }
1142 snprintf(line, sizeof(line), "%s/devices", usb_host_device_path);
1143 f = fopen(line, "r");
1144 if (!f) {
1145 perror("husb: cannot open devices file");
1146 goto the_end;
1147 }
1148
1149 device_count = 0;
1150 bus_num = addr = speed = class_id = product_id = vendor_id = 0;
1151 for(;;) {
1152 if (fgets(line, sizeof(line), f) == NULL) {
1153 break;
1154 }
1155 if (strlen(line) > 0) {
1156 line[strlen(line) - 1] = '\0';
1157 }
1158 if (line[0] == 'T' && line[1] == ':') {
1159 if (device_count && (vendor_id || product_id)) {
1160 /* New device. Add the previously discovered device. */
1161 ret = func(opaque, bus_num, addr, class_id, vendor_id,
1162 product_id, product_name, speed);
1163 if (ret) {
1164 goto the_end;
1165 }
1166 }
1167 if (get_tag_value(buf, sizeof(buf), line, "Bus=", " ") < 0) {
1168 goto fail;
1169 }
1170 bus_num = atoi(buf);
1171 if (get_tag_value(buf, sizeof(buf), line, "Dev#=", " ") < 0) {
1172 goto fail;
1173 }
1174 addr = atoi(buf);
1175 if (get_tag_value(buf, sizeof(buf), line, "Spd=", " ") < 0) {
1176 goto fail;
1177 }
1178 if (!strcmp(buf, "480")) {
1179 speed = USB_SPEED_HIGH;
1180 } else if (!strcmp(buf, "1.5")) {
1181 speed = USB_SPEED_LOW;
1182 } else {
1183 speed = USB_SPEED_FULL;
1184 }
1185 product_name[0] = '\0';
1186 class_id = 0xff;
1187 device_count++;
1188 product_id = 0;
1189 vendor_id = 0;
1190 } else if (line[0] == 'P' && line[1] == ':') {
1191 if (get_tag_value(buf, sizeof(buf), line, "Vendor=", " ") < 0) {
1192 goto fail;
1193 }
1194 vendor_id = strtoul(buf, NULL, 16);
1195 if (get_tag_value(buf, sizeof(buf), line, "ProdID=", " ") < 0) {
1196 goto fail;
1197 }
1198 product_id = strtoul(buf, NULL, 16);
1199 } else if (line[0] == 'S' && line[1] == ':') {
1200 if (get_tag_value(buf, sizeof(buf), line, "Product=", "") < 0) {
1201 goto fail;
1202 }
1203 pstrcpy(product_name, sizeof(product_name), buf);
1204 } else if (line[0] == 'D' && line[1] == ':') {
1205 if (get_tag_value(buf, sizeof(buf), line, "Cls=", " (") < 0) {
1206 goto fail;
1207 }
1208 class_id = strtoul(buf, NULL, 16);
1209 }
1210 fail: ;
1211 }
1212 if (device_count && (vendor_id || product_id)) {
1213 /* Add the last device. */
1214 ret = func(opaque, bus_num, addr, class_id, vendor_id,
1215 product_id, product_name, speed);
1216 }
1217 the_end:
1218 if (f) {
1219 fclose(f);
1220 }
1221 return ret;
1222 }
1223
1224 /*
1225 * Read sys file-system device file
1226 *
1227 * @line address of buffer to put file contents in
1228 * @line_size size of line
1229 * @device_file path to device file (printf format string)
1230 * @device_name device being opened (inserted into device_file)
1231 *
1232 * @return 0 failed, 1 succeeded ('line' contains data)
1233 */
1234 static int usb_host_read_file(char *line, size_t line_size,
1235 const char *device_file, const char *device_name)
1236 {
1237 FILE *f;
1238 int ret = 0;
1239 char filename[PATH_MAX];
1240
1241 snprintf(filename, PATH_MAX, USBSYSBUS_PATH "/devices/%s/%s", device_name,
1242 device_file);
1243 f = fopen(filename, "r");
1244 if (f) {
1245 ret = fgets(line, line_size, f) != NULL;
1246 fclose(f);
1247 }
1248
1249 return ret;
1250 }
1251
1252 /*
1253 * Use /sys/bus/usb/devices/ directory to determine host's USB
1254 * devices.
1255 *
1256 * This code is based on Robert Schiele's original patches posted to
1257 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1258 */
1259 static int usb_host_scan_sys(void *opaque, USBScanFunc *func)
1260 {
1261 DIR *dir = NULL;
1262 char line[1024];
1263 int bus_num, addr, speed, class_id, product_id, vendor_id;
1264 int ret = 0;
1265 char product_name[512];
1266 struct dirent *de;
1267
1268 dir = opendir(USBSYSBUS_PATH "/devices");
1269 if (!dir) {
1270 perror("husb: cannot open devices directory");
1271 goto the_end;
1272 }
1273
1274 while ((de = readdir(dir))) {
1275 if (de->d_name[0] != '.' && !strchr(de->d_name, ':')) {
1276 char *tmpstr = de->d_name;
1277 if (!strncmp(de->d_name, "usb", 3)) {
1278 tmpstr += 3;
1279 }
1280 bus_num = atoi(tmpstr);
1281
1282 if (!usb_host_read_file(line, sizeof(line), "devnum", de->d_name)) {
1283 goto the_end;
1284 }
1285 if (sscanf(line, "%d", &addr) != 1) {
1286 goto the_end;
1287 }
1288 if (!usb_host_read_file(line, sizeof(line), "bDeviceClass",
1289 de->d_name)) {
1290 goto the_end;
1291 }
1292 if (sscanf(line, "%x", &class_id) != 1) {
1293 goto the_end;
1294 }
1295
1296 if (!usb_host_read_file(line, sizeof(line), "idVendor",
1297 de->d_name)) {
1298 goto the_end;
1299 }
1300 if (sscanf(line, "%x", &vendor_id) != 1) {
1301 goto the_end;
1302 }
1303 if (!usb_host_read_file(line, sizeof(line), "idProduct",
1304 de->d_name)) {
1305 goto the_end;
1306 }
1307 if (sscanf(line, "%x", &product_id) != 1) {
1308 goto the_end;
1309 }
1310 if (!usb_host_read_file(line, sizeof(line), "product",
1311 de->d_name)) {
1312 *product_name = 0;
1313 } else {
1314 if (strlen(line) > 0) {
1315 line[strlen(line) - 1] = '\0';
1316 }
1317 pstrcpy(product_name, sizeof(product_name), line);
1318 }
1319
1320 if (!usb_host_read_file(line, sizeof(line), "speed", de->d_name)) {
1321 goto the_end;
1322 }
1323 if (!strcmp(line, "480\n")) {
1324 speed = USB_SPEED_HIGH;
1325 } else if (!strcmp(line, "1.5\n")) {
1326 speed = USB_SPEED_LOW;
1327 } else {
1328 speed = USB_SPEED_FULL;
1329 }
1330
1331 ret = func(opaque, bus_num, addr, class_id, vendor_id,
1332 product_id, product_name, speed);
1333 if (ret) {
1334 goto the_end;
1335 }
1336 }
1337 }
1338 the_end:
1339 if (dir) {
1340 closedir(dir);
1341 }
1342 return ret;
1343 }
1344
1345 /*
1346 * Determine how to access the host's USB devices and call the
1347 * specific support function.
1348 */
1349 static int usb_host_scan(void *opaque, USBScanFunc *func)
1350 {
1351 Monitor *mon = cur_mon;
1352 FILE *f = NULL;
1353 DIR *dir = NULL;
1354 int ret = 0;
1355 const char *fs_type[] = {"unknown", "proc", "dev", "sys"};
1356 char devpath[PATH_MAX];
1357
1358 /* only check the host once */
1359 if (!usb_fs_type) {
1360 dir = opendir(USBSYSBUS_PATH "/devices");
1361 if (dir) {
1362 /* devices found in /dev/bus/usb/ (yes - not a mistake!) */
1363 strcpy(devpath, USBDEVBUS_PATH);
1364 usb_fs_type = USB_FS_SYS;
1365 closedir(dir);
1366 DPRINTF(USBDBG_DEVOPENED, USBSYSBUS_PATH);
1367 goto found_devices;
1368 }
1369 f = fopen(USBPROCBUS_PATH "/devices", "r");
1370 if (f) {
1371 /* devices found in /proc/bus/usb/ */
1372 strcpy(devpath, USBPROCBUS_PATH);
1373 usb_fs_type = USB_FS_PROC;
1374 fclose(f);
1375 DPRINTF(USBDBG_DEVOPENED, USBPROCBUS_PATH);
1376 goto found_devices;
1377 }
1378 /* try additional methods if an access method hasn't been found yet */
1379 f = fopen(USBDEVBUS_PATH "/devices", "r");
1380 if (f) {
1381 /* devices found in /dev/bus/usb/ */
1382 strcpy(devpath, USBDEVBUS_PATH);
1383 usb_fs_type = USB_FS_DEV;
1384 fclose(f);
1385 DPRINTF(USBDBG_DEVOPENED, USBDEVBUS_PATH);
1386 goto found_devices;
1387 }
1388 found_devices:
1389 if (!usb_fs_type) {
1390 if (mon) {
1391 monitor_printf(mon, "husb: unable to access USB devices\n");
1392 }
1393 return -ENOENT;
1394 }
1395
1396 /* the module setting (used later for opening devices) */
1397 usb_host_device_path = qemu_mallocz(strlen(devpath)+1);
1398 strcpy(usb_host_device_path, devpath);
1399 if (mon) {
1400 monitor_printf(mon, "husb: using %s file-system with %s\n",
1401 fs_type[usb_fs_type], usb_host_device_path);
1402 }
1403 }
1404
1405 switch (usb_fs_type) {
1406 case USB_FS_PROC:
1407 case USB_FS_DEV:
1408 ret = usb_host_scan_dev(opaque, func);
1409 break;
1410 case USB_FS_SYS:
1411 ret = usb_host_scan_sys(opaque, func);
1412 break;
1413 default:
1414 ret = -EINVAL;
1415 break;
1416 }
1417 return ret;
1418 }
1419
1420 static QEMUTimer *usb_auto_timer;
1421
1422 static int usb_host_auto_scan(void *opaque, int bus_num, int addr,
1423 int class_id, int vendor_id, int product_id,
1424 const char *product_name, int speed)
1425 {
1426 struct USBAutoFilter *f;
1427 struct USBHostDevice *s;
1428
1429 /* Ignore hubs */
1430 if (class_id == 9)
1431 return 0;
1432
1433 QTAILQ_FOREACH(s, &hostdevs, next) {
1434 f = &s->match;
1435
1436 if (f->bus_num > 0 && f->bus_num != bus_num) {
1437 continue;
1438 }
1439 if (f->addr > 0 && f->addr != addr) {
1440 continue;
1441 }
1442
1443 if (f->vendor_id > 0 && f->vendor_id != vendor_id) {
1444 continue;
1445 }
1446
1447 if (f->product_id > 0 && f->product_id != product_id) {
1448 continue;
1449 }
1450 /* We got a match */
1451
1452 /* Already attached ? */
1453 if (s->fd != -1) {
1454 return 0;
1455 }
1456 DPRINTF("husb: auto open: bus_num %d addr %d\n", bus_num, addr);
1457
1458 usb_host_open(s, bus_num, addr, product_name);
1459 }
1460
1461 return 0;
1462 }
1463
1464 static void usb_host_auto_check(void *unused)
1465 {
1466 struct USBHostDevice *s;
1467 int unconnected = 0;
1468
1469 usb_host_scan(NULL, usb_host_auto_scan);
1470
1471 QTAILQ_FOREACH(s, &hostdevs, next) {
1472 if (s->fd == -1) {
1473 unconnected++;
1474 }
1475 }
1476
1477 if (unconnected == 0) {
1478 /* nothing to watch */
1479 if (usb_auto_timer) {
1480 qemu_del_timer(usb_auto_timer);
1481 }
1482 return;
1483 }
1484
1485 if (!usb_auto_timer) {
1486 usb_auto_timer = qemu_new_timer(rt_clock, usb_host_auto_check, NULL);
1487 if (!usb_auto_timer) {
1488 return;
1489 }
1490 }
1491 qemu_mod_timer(usb_auto_timer, qemu_get_clock(rt_clock) + 2000);
1492 }
1493
1494 /*
1495 * Autoconnect filter
1496 * Format:
1497 * auto:bus:dev[:vid:pid]
1498 * auto:bus.dev[:vid:pid]
1499 *
1500 * bus - bus number (dec, * means any)
1501 * dev - device number (dec, * means any)
1502 * vid - vendor id (hex, * means any)
1503 * pid - product id (hex, * means any)
1504 *
1505 * See 'lsusb' output.
1506 */
1507 static int parse_filter(const char *spec, struct USBAutoFilter *f)
1508 {
1509 enum { BUS, DEV, VID, PID, DONE };
1510 const char *p = spec;
1511 int i;
1512
1513 f->bus_num = 0;
1514 f->addr = 0;
1515 f->vendor_id = 0;
1516 f->product_id = 0;
1517
1518 for (i = BUS; i < DONE; i++) {
1519 p = strpbrk(p, ":.");
1520 if (!p) {
1521 break;
1522 }
1523 p++;
1524
1525 if (*p == '*') {
1526 continue;
1527 }
1528 switch(i) {
1529 case BUS: f->bus_num = strtol(p, NULL, 10); break;
1530 case DEV: f->addr = strtol(p, NULL, 10); break;
1531 case VID: f->vendor_id = strtol(p, NULL, 16); break;
1532 case PID: f->product_id = strtol(p, NULL, 16); break;
1533 }
1534 }
1535
1536 if (i < DEV) {
1537 fprintf(stderr, "husb: invalid auto filter spec %s\n", spec);
1538 return -1;
1539 }
1540
1541 return 0;
1542 }
1543
1544 /**********************/
1545 /* USB host device info */
1546
1547 struct usb_class_info {
1548 int class;
1549 const char *class_name;
1550 };
1551
1552 static const struct usb_class_info usb_class_info[] = {
1553 { USB_CLASS_AUDIO, "Audio"},
1554 { USB_CLASS_COMM, "Communication"},
1555 { USB_CLASS_HID, "HID"},
1556 { USB_CLASS_HUB, "Hub" },
1557 { USB_CLASS_PHYSICAL, "Physical" },
1558 { USB_CLASS_PRINTER, "Printer" },
1559 { USB_CLASS_MASS_STORAGE, "Storage" },
1560 { USB_CLASS_CDC_DATA, "Data" },
1561 { USB_CLASS_APP_SPEC, "Application Specific" },
1562 { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
1563 { USB_CLASS_STILL_IMAGE, "Still Image" },
1564 { USB_CLASS_CSCID, "Smart Card" },
1565 { USB_CLASS_CONTENT_SEC, "Content Security" },
1566 { -1, NULL }
1567 };
1568
1569 static const char *usb_class_str(uint8_t class)
1570 {
1571 const struct usb_class_info *p;
1572 for(p = usb_class_info; p->class != -1; p++) {
1573 if (p->class == class) {
1574 break;
1575 }
1576 }
1577 return p->class_name;
1578 }
1579
1580 static void usb_info_device(Monitor *mon, int bus_num, int addr, int class_id,
1581 int vendor_id, int product_id,
1582 const char *product_name,
1583 int speed)
1584 {
1585 const char *class_str, *speed_str;
1586
1587 switch(speed) {
1588 case USB_SPEED_LOW:
1589 speed_str = "1.5";
1590 break;
1591 case USB_SPEED_FULL:
1592 speed_str = "12";
1593 break;
1594 case USB_SPEED_HIGH:
1595 speed_str = "480";
1596 break;
1597 default:
1598 speed_str = "?";
1599 break;
1600 }
1601
1602 monitor_printf(mon, " Device %d.%d, speed %s Mb/s\n",
1603 bus_num, addr, speed_str);
1604 class_str = usb_class_str(class_id);
1605 if (class_str) {
1606 monitor_printf(mon, " %s:", class_str);
1607 } else {
1608 monitor_printf(mon, " Class %02x:", class_id);
1609 }
1610 monitor_printf(mon, " USB device %04x:%04x", vendor_id, product_id);
1611 if (product_name[0] != '\0') {
1612 monitor_printf(mon, ", %s", product_name);
1613 }
1614 monitor_printf(mon, "\n");
1615 }
1616
1617 static int usb_host_info_device(void *opaque, int bus_num, int addr,
1618 int class_id,
1619 int vendor_id, int product_id,
1620 const char *product_name,
1621 int speed)
1622 {
1623 Monitor *mon = opaque;
1624
1625 usb_info_device(mon, bus_num, addr, class_id, vendor_id, product_id,
1626 product_name, speed);
1627 return 0;
1628 }
1629
1630 static void dec2str(int val, char *str, size_t size)
1631 {
1632 if (val == 0) {
1633 snprintf(str, size, "*");
1634 } else {
1635 snprintf(str, size, "%d", val);
1636 }
1637 }
1638
1639 static void hex2str(int val, char *str, size_t size)
1640 {
1641 if (val == 0) {
1642 snprintf(str, size, "*");
1643 } else {
1644 snprintf(str, size, "%04x", val);
1645 }
1646 }
1647
1648 void usb_host_info(Monitor *mon)
1649 {
1650 struct USBAutoFilter *f;
1651 struct USBHostDevice *s;
1652
1653 usb_host_scan(mon, usb_host_info_device);
1654
1655 if (QTAILQ_EMPTY(&hostdevs)) {
1656 return;
1657 }
1658
1659 monitor_printf(mon, " Auto filters:\n");
1660 QTAILQ_FOREACH(s, &hostdevs, next) {
1661 char bus[10], addr[10], vid[10], pid[10];
1662 f = &s->match;
1663 dec2str(f->bus_num, bus, sizeof(bus));
1664 dec2str(f->addr, addr, sizeof(addr));
1665 hex2str(f->vendor_id, vid, sizeof(vid));
1666 hex2str(f->product_id, pid, sizeof(pid));
1667 monitor_printf(mon, " Device %s.%s ID %s:%s\n",
1668 bus, addr, vid, pid);
1669 }
1670 }