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