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