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