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