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