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