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