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