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