]> git.proxmox.com Git - qemu.git/blame - hw/usb/host-linux.c
usb-host: don't dereference invalid iovecs
[qemu.git] / hw / usb / host-linux.c
CommitLineData
bb36d470
FB
1/*
2 * Linux host USB redirector
3 *
4 * Copyright (c) 2005 Fabrice Bellard
5fafdf24 5 *
64838171
AL
6 * Copyright (c) 2008 Max Krasnyansky
7 * Support for host device auto connect & disconnect
5d0c5750 8 * Major rewrite to support fully async operation
4b096fc9 9 *
0f431527
AL
10 * Copyright 2008 TJ <linux@tjworld.net>
11 * Added flexible support for /dev/bus/usb /sys/bus/usb/devices in addition
12 * to the legacy /proc/bus/usb USB device discovery and handling
13 *
bb36d470
FB
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this software and associated documentation files (the "Software"), to deal
16 * in the Software without restriction, including without limitation the rights
17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 * copies of the Software, and to permit persons to whom the Software is
19 * furnished to do so, subject to the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30 * THE SOFTWARE.
31 */
446ab128 32
87ecb68b 33#include "qemu-common.h"
1f3870ab 34#include "qemu-timer.h"
376253ec 35#include "monitor.h"
b373a63a 36#include "sysemu.h"
e6a2f500 37#include "trace.h"
bb36d470 38
bb36d470
FB
39#include <dirent.h>
40#include <sys/ioctl.h>
bb36d470 41
446ab128
AL
42#include <linux/usbdevice_fs.h>
43#include <linux/version.h>
44#include "hw/usb.h"
96dd9aac 45#include "hw/usb/desc.h"
bb36d470 46
d9cf1578
BS
47/* We redefine it to avoid version problems */
48struct usb_ctrltransfer {
49 uint8_t bRequestType;
50 uint8_t bRequest;
51 uint16_t wValue;
52 uint16_t wIndex;
53 uint16_t wLength;
54 uint32_t timeout;
55 void *data;
56};
57
ba9acab9 58typedef int USBScanFunc(void *opaque, int bus_num, int addr, const char *port,
0f5160d1 59 int class_id, int vendor_id, int product_id,
a594cfbf 60 const char *product_name, int speed);
26a9e82a 61
0745eb1e 62//#define DEBUG
64838171
AL
63
64#ifdef DEBUG
d0f2c4c6 65#define DPRINTF printf
64838171 66#else
d0f2c4c6 67#define DPRINTF(...)
64838171 68#endif
bb36d470 69
1f6e24e7 70#define PRODUCT_NAME_SZ 32
5557d820 71#define MAX_PORTLEN 16
bb36d470 72
b9dc033c 73/* endpoint association data */
060dc841 74#define ISO_FRAME_DESC_PER_URB 32
060dc841 75
71138531
GH
76/* devio.c limits single requests to 16k */
77#define MAX_USBFS_BUFFER_SIZE 16384
78
060dc841
HG
79typedef struct AsyncURB AsyncURB;
80
b9dc033c 81struct endp_data {
64838171 82 uint8_t halted;
bb6d5498 83 uint8_t iso_started;
060dc841
HG
84 AsyncURB *iso_urb;
85 int iso_urb_idx;
bb6d5498 86 int iso_buffer_used;
82887262 87 int inflight;
b9dc033c
AZ
88};
89
26a9e82a
GH
90struct USBAutoFilter {
91 uint32_t bus_num;
92 uint32_t addr;
9056a297 93 char *port;
26a9e82a
GH
94 uint32_t vendor_id;
95 uint32_t product_id;
96};
97
39c20577
GH
98enum USBHostDeviceOptions {
99 USB_HOST_OPT_PIPELINE,
100};
101
bb36d470
FB
102typedef struct USBHostDevice {
103 USBDevice dev;
64838171 104 int fd;
9516bb47 105 int hub_fd;
c75fead6 106 int hub_port;
64838171 107
f8ddbfbc 108 uint8_t descr[8192];
64838171 109 int descr_len;
24772c1e 110 int closing;
b81bcd8a 111 uint32_t iso_urb_count;
39c20577 112 uint32_t options;
b373a63a 113 Notifier exit;
64838171 114
d8e17efd
GH
115 struct endp_data ep_in[USB_MAX_ENDPOINTS];
116 struct endp_data ep_out[USB_MAX_ENDPOINTS];
7a8fc83f 117 QLIST_HEAD(, AsyncURB) aurbs;
4b096fc9 118
4b096fc9
AL
119 /* Host side address */
120 int bus_num;
121 int addr;
5557d820 122 char port[MAX_PORTLEN];
26a9e82a 123 struct USBAutoFilter match;
65bb3a5c 124 int32_t bootindex;
3ee886c5 125 int seen, errcount;
4b096fc9 126
26a9e82a 127 QTAILQ_ENTRY(USBHostDevice) next;
bb36d470
FB
128} USBHostDevice;
129
26a9e82a
GH
130static QTAILQ_HEAD(, USBHostDevice) hostdevs = QTAILQ_HEAD_INITIALIZER(hostdevs);
131
132static int usb_host_close(USBHostDevice *dev);
133static int parse_filter(const char *spec, struct USBAutoFilter *f);
134static void usb_host_auto_check(void *unused);
2cc59d8c
HG
135static int usb_host_read_file(char *line, size_t line_size,
136 const char *device_file, const char *device_name);
9b87e19b 137static int usb_linux_update_endp_table(USBHostDevice *s);
26a9e82a 138
d8e17efd
GH
139static int usb_host_usbfs_type(USBHostDevice *s, USBPacket *p)
140{
141 static const int usbfs[] = {
142 [USB_ENDPOINT_XFER_CONTROL] = USBDEVFS_URB_TYPE_CONTROL,
143 [USB_ENDPOINT_XFER_ISOC] = USBDEVFS_URB_TYPE_ISO,
144 [USB_ENDPOINT_XFER_BULK] = USBDEVFS_URB_TYPE_BULK,
145 [USB_ENDPOINT_XFER_INT] = USBDEVFS_URB_TYPE_INTERRUPT,
146 };
079d0b7f 147 uint8_t type = p->ep->type;
d8e17efd
GH
148 assert(type < ARRAY_SIZE(usbfs));
149 return usbfs[type];
150}
151
c7662daa
GH
152static int usb_host_do_reset(USBHostDevice *dev)
153{
154 struct timeval s, e;
155 uint32_t usecs;
156 int ret;
157
158 gettimeofday(&s, NULL);
159 ret = ioctl(dev->fd, USBDEVFS_RESET);
160 gettimeofday(&e, NULL);
161 usecs = (e.tv_sec - s.tv_sec) * 1000000;
162 usecs += e.tv_usec - s.tv_usec;
163 if (usecs > 1000000) {
164 /* more than a second, something is fishy, broken usb device? */
165 fprintf(stderr, "husb: device %d:%d reset took %d.%06d seconds\n",
166 dev->bus_num, dev->addr, usecs / 1000000, usecs % 1000000);
167 }
168 return ret;
169}
170
c0e5750b 171static struct endp_data *get_endp(USBHostDevice *s, int pid, int ep)
ca3a36cf 172{
c0e5750b
GH
173 struct endp_data *eps = pid == USB_TOKEN_IN ? s->ep_in : s->ep_out;
174 assert(pid == USB_TOKEN_IN || pid == USB_TOKEN_OUT);
d8e17efd 175 assert(ep > 0 && ep <= USB_MAX_ENDPOINTS);
c0e5750b 176 return eps + ep - 1;
ca3a36cf
GH
177}
178
c0e5750b 179static int is_isoc(USBHostDevice *s, int pid, int ep)
64838171 180{
d8e17efd 181 return usb_ep_get_type(&s->dev, pid, ep) == USB_ENDPOINT_XFER_ISOC;
64838171
AL
182}
183
c0e5750b 184static int is_valid(USBHostDevice *s, int pid, int ep)
a0b5fece 185{
d8e17efd 186 return usb_ep_get_type(&s->dev, pid, ep) != USB_ENDPOINT_XFER_INVALID;
a0b5fece
HG
187}
188
c0e5750b 189static int is_halted(USBHostDevice *s, int pid, int ep)
64838171 190{
c0e5750b 191 return get_endp(s, pid, ep)->halted;
64838171
AL
192}
193
c0e5750b 194static void clear_halt(USBHostDevice *s, int pid, int ep)
64838171 195{
e6a2f500 196 trace_usb_host_ep_clear_halt(s->bus_num, s->addr, ep);
c0e5750b 197 get_endp(s, pid, ep)->halted = 0;
64838171
AL
198}
199
c0e5750b 200static void set_halt(USBHostDevice *s, int pid, int ep)
64838171 201{
c0e5750b
GH
202 if (ep != 0) {
203 trace_usb_host_ep_set_halt(s->bus_num, s->addr, ep);
204 get_endp(s, pid, ep)->halted = 1;
205 }
64838171
AL
206}
207
c0e5750b 208static int is_iso_started(USBHostDevice *s, int pid, int ep)
bb6d5498 209{
c0e5750b 210 return get_endp(s, pid, ep)->iso_started;
bb6d5498
HG
211}
212
c0e5750b 213static void clear_iso_started(USBHostDevice *s, int pid, int ep)
bb6d5498 214{
e6a2f500 215 trace_usb_host_ep_stop_iso(s->bus_num, s->addr, ep);
c0e5750b 216 get_endp(s, pid, ep)->iso_started = 0;
bb6d5498
HG
217}
218
c0e5750b 219static void set_iso_started(USBHostDevice *s, int pid, int ep)
bb6d5498 220{
c0e5750b 221 struct endp_data *e = get_endp(s, pid, ep);
e6a2f500
GH
222
223 trace_usb_host_ep_start_iso(s->bus_num, s->addr, ep);
82887262
GH
224 if (!e->iso_started) {
225 e->iso_started = 1;
226 e->inflight = 0;
227 }
228}
229
c0e5750b 230static int change_iso_inflight(USBHostDevice *s, int pid, int ep, int value)
82887262 231{
c0e5750b 232 struct endp_data *e = get_endp(s, pid, ep);
82887262
GH
233
234 e->inflight += value;
235 return e->inflight;
bb6d5498
HG
236}
237
c0e5750b 238static void set_iso_urb(USBHostDevice *s, int pid, int ep, AsyncURB *iso_urb)
060dc841 239{
c0e5750b 240 get_endp(s, pid, ep)->iso_urb = iso_urb;
060dc841
HG
241}
242
c0e5750b 243static AsyncURB *get_iso_urb(USBHostDevice *s, int pid, int ep)
060dc841 244{
c0e5750b 245 return get_endp(s, pid, ep)->iso_urb;
060dc841
HG
246}
247
c0e5750b 248static void set_iso_urb_idx(USBHostDevice *s, int pid, int ep, int i)
060dc841 249{
c0e5750b 250 get_endp(s, pid, ep)->iso_urb_idx = i;
060dc841
HG
251}
252
c0e5750b 253static int get_iso_urb_idx(USBHostDevice *s, int pid, int ep)
060dc841 254{
c0e5750b 255 return get_endp(s, pid, ep)->iso_urb_idx;
060dc841
HG
256}
257
c0e5750b 258static void set_iso_buffer_used(USBHostDevice *s, int pid, int ep, int i)
bb6d5498 259{
c0e5750b 260 get_endp(s, pid, ep)->iso_buffer_used = i;
bb6d5498
HG
261}
262
c0e5750b 263static int get_iso_buffer_used(USBHostDevice *s, int pid, int ep)
bb6d5498 264{
c0e5750b 265 return get_endp(s, pid, ep)->iso_buffer_used;
bb6d5498
HG
266}
267
2791104c 268/*
64838171 269 * Async URB state.
060dc841 270 * We always allocate iso packet descriptors even for bulk transfers
2791104c 271 * to simplify allocation and casts.
64838171 272 */
060dc841 273struct AsyncURB
64838171
AL
274{
275 struct usbdevfs_urb urb;
060dc841 276 struct usbdevfs_iso_packet_desc isocpd[ISO_FRAME_DESC_PER_URB];
7a8fc83f
GH
277 USBHostDevice *hdev;
278 QLIST_ENTRY(AsyncURB) next;
b9dc033c 279
060dc841 280 /* For regular async urbs */
64838171 281 USBPacket *packet;
71138531 282 int more; /* large transfer, more urbs follow */
060dc841
HG
283
284 /* For buffered iso handling */
285 int iso_frame_idx; /* -1 means in flight */
286};
b9dc033c 287
7a8fc83f 288static AsyncURB *async_alloc(USBHostDevice *s)
b9dc033c 289{
7267c094 290 AsyncURB *aurb = g_malloc0(sizeof(AsyncURB));
7a8fc83f
GH
291 aurb->hdev = s;
292 QLIST_INSERT_HEAD(&s->aurbs, aurb, next);
293 return aurb;
b9dc033c
AZ
294}
295
64838171 296static void async_free(AsyncURB *aurb)
b9dc033c 297{
7a8fc83f 298 QLIST_REMOVE(aurb, next);
7267c094 299 g_free(aurb);
64838171 300}
b9dc033c 301
41c01ee7
GH
302static void do_disconnect(USBHostDevice *s)
303{
41c01ee7
GH
304 usb_host_close(s);
305 usb_host_auto_check(NULL);
306}
307
64838171
AL
308static void async_complete(void *opaque)
309{
310 USBHostDevice *s = opaque;
311 AsyncURB *aurb;
82887262 312 int urbs = 0;
64838171
AL
313
314 while (1) {
2791104c 315 USBPacket *p;
b9dc033c 316
2791104c 317 int r = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &aurb);
64838171 318 if (r < 0) {
2791104c 319 if (errno == EAGAIN) {
82887262
GH
320 if (urbs > 2) {
321 fprintf(stderr, "husb: %d iso urbs finished at once\n", urbs);
322 }
64838171 323 return;
2791104c 324 }
40197c35
GH
325 if (errno == ENODEV) {
326 if (!s->closing) {
327 trace_usb_host_disconnect(s->bus_num, s->addr);
328 do_disconnect(s);
329 }
64838171
AL
330 return;
331 }
332
e6a2f500 333 perror("USBDEVFS_REAPURBNDELAY");
64838171 334 return;
b9dc033c 335 }
64838171 336
2791104c 337 DPRINTF("husb: async completed. aurb %p status %d alen %d\n",
64838171
AL
338 aurb, aurb->urb.status, aurb->urb.actual_length);
339
060dc841
HG
340 /* If this is a buffered iso urb mark it as complete and don't do
341 anything else (it is handled further in usb_host_handle_iso_data) */
342 if (aurb->iso_frame_idx == -1) {
82887262 343 int inflight;
c0e5750b
GH
344 int pid = (aurb->urb.endpoint & USB_DIR_IN) ?
345 USB_TOKEN_IN : USB_TOKEN_OUT;
346 int ep = aurb->urb.endpoint & 0xf;
060dc841 347 if (aurb->urb.status == -EPIPE) {
c0e5750b 348 set_halt(s, pid, ep);
060dc841
HG
349 }
350 aurb->iso_frame_idx = 0;
82887262 351 urbs++;
c0e5750b
GH
352 inflight = change_iso_inflight(s, pid, ep, -1);
353 if (inflight == 0 && is_iso_started(s, pid, ep)) {
82887262
GH
354 fprintf(stderr, "husb: out of buffers for iso stream\n");
355 }
060dc841
HG
356 continue;
357 }
358
359 p = aurb->packet;
e6a2f500
GH
360 trace_usb_host_urb_complete(s->bus_num, s->addr, aurb, aurb->urb.status,
361 aurb->urb.actual_length, aurb->more);
060dc841 362
2791104c 363 if (p) {
64838171
AL
364 switch (aurb->urb.status) {
365 case 0:
4f4321c1 366 p->result += aurb->urb.actual_length;
64838171
AL
367 break;
368
369 case -EPIPE:
079d0b7f 370 set_halt(s, p->pid, p->ep->nr);
4f4321c1 371 p->result = USB_RET_STALL;
2791104c 372 break;
dcc7e25f 373
4d819a9b
HG
374 case -EOVERFLOW:
375 p->result = USB_RET_BABBLE;
376 break;
377
64838171 378 default:
d61000a8 379 p->result = USB_RET_IOERROR;
64838171
AL
380 break;
381 }
382
50b7963e 383 if (aurb->urb.type == USBDEVFS_URB_TYPE_CONTROL) {
19b89252 384 trace_usb_host_req_complete(s->bus_num, s->addr, p, p->result);
50b7963e 385 usb_generic_async_ctrl_complete(&s->dev, p);
71138531 386 } else if (!aurb->more) {
19b89252 387 trace_usb_host_req_complete(s->bus_num, s->addr, p, p->result);
50b7963e
HG
388 usb_packet_complete(&s->dev, p);
389 }
2791104c 390 }
64838171
AL
391
392 async_free(aurb);
b9dc033c 393 }
b9dc033c
AZ
394}
395
eb5e680a 396static void usb_host_async_cancel(USBDevice *dev, USBPacket *p)
b9dc033c 397{
eb5e680a 398 USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
227ebeb5 399 AsyncURB *aurb;
b9dc033c 400
19b89252 401 trace_usb_host_req_canceled(s->bus_num, s->addr, p);
6aebe407 402
227ebeb5
GH
403 QLIST_FOREACH(aurb, &s->aurbs, next) {
404 if (p != aurb->packet) {
405 continue;
406 }
64838171 407
6aebe407 408 trace_usb_host_urb_canceled(s->bus_num, s->addr, aurb);
b9dc033c 409
227ebeb5
GH
410 /* Mark it as dead (see async_complete above) */
411 aurb->packet = NULL;
412
413 int r = ioctl(s->fd, USBDEVFS_DISCARDURB, aurb);
414 if (r < 0) {
415 DPRINTF("husb: async. discard urb failed errno %d\n", errno);
416 }
b9dc033c 417 }
b9dc033c
AZ
418}
419
097db438
GH
420static int usb_host_open_device(int bus, int addr)
421{
422 const char *usbfs = NULL;
423 char filename[32];
424 struct stat st;
425 int fd, rc;
426
427 rc = stat("/dev/bus/usb", &st);
428 if (rc == 0 && S_ISDIR(st.st_mode)) {
429 /* udev-created device nodes available */
430 usbfs = "/dev/bus/usb";
431 } else {
432 /* fallback: usbfs mounted below /proc */
433 usbfs = "/proc/bus/usb";
434 }
435
436 snprintf(filename, sizeof(filename), "%s/%03d/%03d",
437 usbfs, bus, addr);
438 fd = open(filename, O_RDWR | O_NONBLOCK);
439 if (fd < 0) {
440 fprintf(stderr, "husb: open %s: %s\n", filename, strerror(errno));
441 }
442 return fd;
443}
444
e6274727
GH
445static int usb_host_claim_port(USBHostDevice *s)
446{
447#ifdef USBDEVFS_CLAIM_PORT
448 char *h, hub_name[64], line[1024];
c75fead6 449 int hub_addr, ret;
e6274727
GH
450
451 snprintf(hub_name, sizeof(hub_name), "%d-%s",
452 s->match.bus_num, s->match.port);
453
454 /* try strip off last ".$portnr" to get hub */
455 h = strrchr(hub_name, '.');
456 if (h != NULL) {
c75fead6 457 s->hub_port = atoi(h+1);
e6274727
GH
458 *h = '\0';
459 } else {
460 /* no dot in there -> it is the root hub */
461 snprintf(hub_name, sizeof(hub_name), "usb%d",
462 s->match.bus_num);
c75fead6 463 s->hub_port = atoi(s->match.port);
e6274727
GH
464 }
465
466 if (!usb_host_read_file(line, sizeof(line), "devnum",
467 hub_name)) {
468 return -1;
469 }
470 if (sscanf(line, "%d", &hub_addr) != 1) {
471 return -1;
472 }
473
097db438 474 s->hub_fd = usb_host_open_device(s->match.bus_num, hub_addr);
e6274727
GH
475 if (s->hub_fd < 0) {
476 return -1;
477 }
478
c75fead6 479 ret = ioctl(s->hub_fd, USBDEVFS_CLAIM_PORT, &s->hub_port);
e6274727
GH
480 if (ret < 0) {
481 close(s->hub_fd);
482 s->hub_fd = -1;
483 return -1;
484 }
485
c75fead6 486 trace_usb_host_claim_port(s->match.bus_num, hub_addr, s->hub_port);
e6274727
GH
487 return 0;
488#else
489 return -1;
490#endif
491}
492
c75fead6
GH
493static void usb_host_release_port(USBHostDevice *s)
494{
495 if (s->hub_fd == -1) {
496 return;
497 }
498#ifdef USBDEVFS_RELEASE_PORT
499 ioctl(s->hub_fd, USBDEVFS_RELEASE_PORT, &s->hub_port);
500#endif
501 close(s->hub_fd);
502 s->hub_fd = -1;
503}
504
e6274727
GH
505static int usb_host_disconnect_ifaces(USBHostDevice *dev, int nb_interfaces)
506{
507 /* earlier Linux 2.4 do not support that */
508#ifdef USBDEVFS_DISCONNECT
509 struct usbdevfs_ioctl ctrl;
510 int ret, interface;
511
512 for (interface = 0; interface < nb_interfaces; interface++) {
513 ctrl.ioctl_code = USBDEVFS_DISCONNECT;
514 ctrl.ifno = interface;
515 ctrl.data = 0;
516 ret = ioctl(dev->fd, USBDEVFS_IOCTL, &ctrl);
517 if (ret < 0 && errno != ENODATA) {
518 perror("USBDEVFS_DISCONNECT");
519 return -1;
520 }
521 }
522#endif
523 return 0;
524}
525
0fcc3bfc
GH
526static int usb_linux_get_num_interfaces(USBHostDevice *s)
527{
528 char device_name[64], line[1024];
529 int num_interfaces = 0;
530
0fcc3bfc
GH
531 sprintf(device_name, "%d-%s", s->bus_num, s->port);
532 if (!usb_host_read_file(line, sizeof(line), "bNumInterfaces",
533 device_name)) {
534 return -1;
535 }
536 if (sscanf(line, "%d", &num_interfaces) != 1) {
537 return -1;
538 }
539 return num_interfaces;
540}
541
446ab128 542static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration)
b9dc033c 543{
41c01ee7 544 const char *op = NULL;
b9dc033c 545 int dev_descr_len, config_descr_len;
d4c4e6fd 546 int interface, nb_interfaces;
b9dc033c
AZ
547 int ret, i;
548
1de14d43
GH
549 for (i = 0; i < USB_MAX_INTERFACES; i++) {
550 dev->dev.altsetting[i] = 0;
551 }
552
eb7700bb 553 if (configuration == 0) { /* address state - ignore */
65360511
GH
554 dev->dev.ninterfaces = 0;
555 dev->dev.configuration = 0;
b9dc033c 556 return 1;
eb7700bb 557 }
b9dc033c 558
d0f2c4c6 559 DPRINTF("husb: claiming interfaces. config %d\n", configuration);
446ab128 560
b9dc033c
AZ
561 i = 0;
562 dev_descr_len = dev->descr[0];
2791104c 563 if (dev_descr_len > dev->descr_len) {
61c1117f
HG
564 fprintf(stderr, "husb: update iface failed. descr too short\n");
565 return 0;
2791104c 566 }
b9dc033c
AZ
567
568 i += dev_descr_len;
569 while (i < dev->descr_len) {
2791104c
DA
570 DPRINTF("husb: i is %d, descr_len is %d, dl %d, dt %d\n",
571 i, dev->descr_len,
b9dc033c 572 dev->descr[i], dev->descr[i+1]);
64838171 573
b9dc033c
AZ
574 if (dev->descr[i+1] != USB_DT_CONFIG) {
575 i += dev->descr[i];
576 continue;
577 }
578 config_descr_len = dev->descr[i];
579
e6a2f500 580 DPRINTF("husb: config #%d need %d\n", dev->descr[i + 5], configuration);
1f3870ab 581
eb7700bb 582 if (configuration == dev->descr[i + 5]) {
446ab128 583 configuration = dev->descr[i + 5];
b9dc033c 584 break;
446ab128 585 }
b9dc033c
AZ
586
587 i += config_descr_len;
588 }
589
590 if (i >= dev->descr_len) {
2791104c
DA
591 fprintf(stderr,
592 "husb: update iface failed. no matching configuration\n");
61c1117f 593 return 0;
b9dc033c
AZ
594 }
595 nb_interfaces = dev->descr[i + 4];
596
e6274727
GH
597 if (usb_host_disconnect_ifaces(dev, nb_interfaces) < 0) {
598 goto fail;
b9dc033c 599 }
b9dc033c
AZ
600
601 /* XXX: only grab if all interfaces are free */
602 for (interface = 0; interface < nb_interfaces; interface++) {
41c01ee7 603 op = "USBDEVFS_CLAIMINTERFACE";
b9dc033c
AZ
604 ret = ioctl(dev->fd, USBDEVFS_CLAIMINTERFACE, &interface);
605 if (ret < 0) {
41c01ee7 606 goto fail;
b9dc033c
AZ
607 }
608 }
609
e6a2f500
GH
610 trace_usb_host_claim_interfaces(dev->bus_num, dev->addr,
611 nb_interfaces, configuration);
b9dc033c 612
65360511
GH
613 dev->dev.ninterfaces = nb_interfaces;
614 dev->dev.configuration = configuration;
446ab128 615 return 1;
41c01ee7
GH
616
617fail:
618 if (errno == ENODEV) {
619 do_disconnect(dev);
620 }
621 perror(op);
622 return 0;
446ab128
AL
623}
624
625static int usb_host_release_interfaces(USBHostDevice *s)
626{
627 int ret, i;
628
e6a2f500 629 trace_usb_host_release_interfaces(s->bus_num, s->addr);
446ab128 630
65360511 631 for (i = 0; i < s->dev.ninterfaces; i++) {
446ab128
AL
632 ret = ioctl(s->fd, USBDEVFS_RELEASEINTERFACE, &i);
633 if (ret < 0) {
e6a2f500 634 perror("USBDEVFS_RELEASEINTERFACE");
446ab128
AL
635 return 0;
636 }
637 }
b9dc033c
AZ
638 return 1;
639}
640
059809e4 641static void usb_host_handle_reset(USBDevice *dev)
bb36d470 642{
26a9e82a 643 USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
64838171 644
e6a2f500 645 trace_usb_host_reset(s->bus_num, s->addr);
64838171 646
c7662daa 647 usb_host_do_reset(s);;
446ab128 648
eb7700bb 649 usb_host_claim_interfaces(s, 0);
9b87e19b 650 usb_linux_update_endp_table(s);
5fafdf24 651}
bb36d470 652
059809e4
FB
653static void usb_host_handle_destroy(USBDevice *dev)
654{
655 USBHostDevice *s = (USBHostDevice *)dev;
656
c75fead6 657 usb_host_release_port(s);
26a9e82a
GH
658 usb_host_close(s);
659 QTAILQ_REMOVE(&hostdevs, s, next);
b373a63a 660 qemu_remove_exit_notifier(&s->exit);
059809e4
FB
661}
662
060dc841
HG
663/* iso data is special, we need to keep enough urbs in flight to make sure
664 that the controller never runs out of them, otherwise the device will
665 likely suffer a buffer underrun / overrun. */
c0e5750b 666static AsyncURB *usb_host_alloc_iso(USBHostDevice *s, int pid, uint8_t ep)
060dc841
HG
667{
668 AsyncURB *aurb;
f003397c 669 int i, j, len = usb_ep_get_max_packet_size(&s->dev, pid, ep);
060dc841 670
7267c094 671 aurb = g_malloc0(s->iso_urb_count * sizeof(*aurb));
b81bcd8a 672 for (i = 0; i < s->iso_urb_count; i++) {
060dc841
HG
673 aurb[i].urb.endpoint = ep;
674 aurb[i].urb.buffer_length = ISO_FRAME_DESC_PER_URB * len;
7267c094 675 aurb[i].urb.buffer = g_malloc(aurb[i].urb.buffer_length);
060dc841
HG
676 aurb[i].urb.type = USBDEVFS_URB_TYPE_ISO;
677 aurb[i].urb.flags = USBDEVFS_URB_ISO_ASAP;
678 aurb[i].urb.number_of_packets = ISO_FRAME_DESC_PER_URB;
679 for (j = 0 ; j < ISO_FRAME_DESC_PER_URB; j++)
680 aurb[i].urb.iso_frame_desc[j].length = len;
c0e5750b 681 if (pid == USB_TOKEN_IN) {
060dc841
HG
682 aurb[i].urb.endpoint |= 0x80;
683 /* Mark as fully consumed (idle) */
684 aurb[i].iso_frame_idx = ISO_FRAME_DESC_PER_URB;
685 }
686 }
c0e5750b 687 set_iso_urb(s, pid, ep, aurb);
060dc841
HG
688
689 return aurb;
690}
691
c0e5750b 692static void usb_host_stop_n_free_iso(USBHostDevice *s, int pid, uint8_t ep)
060dc841
HG
693{
694 AsyncURB *aurb;
695 int i, ret, killed = 0, free = 1;
696
c0e5750b 697 aurb = get_iso_urb(s, pid, ep);
060dc841
HG
698 if (!aurb) {
699 return;
700 }
701
b81bcd8a 702 for (i = 0; i < s->iso_urb_count; i++) {
060dc841
HG
703 /* in flight? */
704 if (aurb[i].iso_frame_idx == -1) {
705 ret = ioctl(s->fd, USBDEVFS_DISCARDURB, &aurb[i]);
706 if (ret < 0) {
e6a2f500 707 perror("USBDEVFS_DISCARDURB");
060dc841
HG
708 free = 0;
709 continue;
710 }
711 killed++;
712 }
713 }
714
715 /* Make sure any urbs we've killed are reaped before we free them */
716 if (killed) {
717 async_complete(s);
718 }
719
b81bcd8a 720 for (i = 0; i < s->iso_urb_count; i++) {
7267c094 721 g_free(aurb[i].urb.buffer);
060dc841
HG
722 }
723
724 if (free)
7267c094 725 g_free(aurb);
060dc841
HG
726 else
727 printf("husb: leaking iso urbs because of discard failure\n");
c0e5750b
GH
728 set_iso_urb(s, pid, ep, NULL);
729 set_iso_urb_idx(s, pid, ep, 0);
730 clear_iso_started(s, pid, ep);
060dc841
HG
731}
732
733static int urb_status_to_usb_ret(int status)
734{
735 switch (status) {
736 case -EPIPE:
737 return USB_RET_STALL;
4d819a9b
HG
738 case -EOVERFLOW:
739 return USB_RET_BABBLE;
060dc841 740 default:
d61000a8 741 return USB_RET_IOERROR;
060dc841
HG
742 }
743}
744
bb6d5498 745static int usb_host_handle_iso_data(USBHostDevice *s, USBPacket *p, int in)
060dc841
HG
746{
747 AsyncURB *aurb;
bb6d5498 748 int i, j, ret, max_packet_size, offset, len = 0;
4f4321c1 749 uint8_t *buf;
975f2998 750
079d0b7f 751 max_packet_size = p->ep->max_packet_size;
975f2998
HG
752 if (max_packet_size == 0)
753 return USB_RET_NAK;
060dc841 754
079d0b7f 755 aurb = get_iso_urb(s, p->pid, p->ep->nr);
060dc841 756 if (!aurb) {
079d0b7f 757 aurb = usb_host_alloc_iso(s, p->pid, p->ep->nr);
060dc841
HG
758 }
759
079d0b7f 760 i = get_iso_urb_idx(s, p->pid, p->ep->nr);
060dc841
HG
761 j = aurb[i].iso_frame_idx;
762 if (j >= 0 && j < ISO_FRAME_DESC_PER_URB) {
bb6d5498
HG
763 if (in) {
764 /* Check urb status */
765 if (aurb[i].urb.status) {
766 len = urb_status_to_usb_ret(aurb[i].urb.status);
767 /* Move to the next urb */
768 aurb[i].iso_frame_idx = ISO_FRAME_DESC_PER_URB - 1;
769 /* Check frame status */
770 } else if (aurb[i].urb.iso_frame_desc[j].status) {
771 len = urb_status_to_usb_ret(
772 aurb[i].urb.iso_frame_desc[j].status);
773 /* Check the frame fits */
4f4321c1
GH
774 } else if (aurb[i].urb.iso_frame_desc[j].actual_length
775 > p->iov.size) {
bb6d5498 776 printf("husb: received iso data is larger then packet\n");
4d819a9b 777 len = USB_RET_BABBLE;
bb6d5498
HG
778 /* All good copy data over */
779 } else {
780 len = aurb[i].urb.iso_frame_desc[j].actual_length;
4f4321c1
GH
781 buf = aurb[i].urb.buffer +
782 j * aurb[i].urb.iso_frame_desc[0].length;
783 usb_packet_copy(p, buf, len);
bb6d5498 784 }
060dc841 785 } else {
4f4321c1 786 len = p->iov.size;
079d0b7f 787 offset = (j == 0) ? 0 : get_iso_buffer_used(s, p->pid, p->ep->nr);
bb6d5498
HG
788
789 /* Check the frame fits */
790 if (len > max_packet_size) {
791 printf("husb: send iso data is larger then max packet size\n");
792 return USB_RET_NAK;
793 }
794
795 /* All good copy data over */
4f4321c1 796 usb_packet_copy(p, aurb[i].urb.buffer + offset, len);
bb6d5498
HG
797 aurb[i].urb.iso_frame_desc[j].length = len;
798 offset += len;
079d0b7f 799 set_iso_buffer_used(s, p->pid, p->ep->nr, offset);
bb6d5498
HG
800
801 /* Start the stream once we have buffered enough data */
079d0b7f
GH
802 if (!is_iso_started(s, p->pid, p->ep->nr) && i == 1 && j == 8) {
803 set_iso_started(s, p->pid, p->ep->nr);
bb6d5498 804 }
060dc841
HG
805 }
806 aurb[i].iso_frame_idx++;
807 if (aurb[i].iso_frame_idx == ISO_FRAME_DESC_PER_URB) {
b81bcd8a 808 i = (i + 1) % s->iso_urb_count;
079d0b7f 809 set_iso_urb_idx(s, p->pid, p->ep->nr, i);
060dc841 810 }
bb6d5498
HG
811 } else {
812 if (in) {
079d0b7f 813 set_iso_started(s, p->pid, p->ep->nr);
bb6d5498
HG
814 } else {
815 DPRINTF("hubs: iso out error no free buffer, dropping packet\n");
816 }
060dc841
HG
817 }
818
079d0b7f 819 if (is_iso_started(s, p->pid, p->ep->nr)) {
bb6d5498 820 /* (Re)-submit all fully consumed / filled urbs */
b81bcd8a 821 for (i = 0; i < s->iso_urb_count; i++) {
bb6d5498
HG
822 if (aurb[i].iso_frame_idx == ISO_FRAME_DESC_PER_URB) {
823 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, &aurb[i]);
824 if (ret < 0) {
e6a2f500 825 perror("USBDEVFS_SUBMITURB");
bb6d5498
HG
826 if (!in || len == 0) {
827 switch(errno) {
828 case ETIMEDOUT:
829 len = USB_RET_NAK;
0225e254 830 break;
bb6d5498
HG
831 case EPIPE:
832 default:
833 len = USB_RET_STALL;
834 }
060dc841 835 }
bb6d5498 836 break;
060dc841 837 }
bb6d5498 838 aurb[i].iso_frame_idx = -1;
079d0b7f 839 change_iso_inflight(s, p->pid, p->ep->nr, 1);
060dc841 840 }
060dc841
HG
841 }
842 }
843
844 return len;
845}
846
50b7963e 847static int usb_host_handle_data(USBDevice *dev, USBPacket *p)
bb36d470 848{
50b7963e 849 USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
64838171 850 struct usbdevfs_urb *urb;
446ab128 851 AsyncURB *aurb;
b621bab4 852 int ret, rem, prem, v;
71138531 853 uint8_t *pbuf;
060dc841 854 uint8_t ep;
b9dc033c 855
19b89252 856 trace_usb_host_req_data(s->bus_num, s->addr, p,
e6a2f500 857 p->pid == USB_TOKEN_IN,
079d0b7f 858 p->ep->nr, p->iov.size);
e6a2f500 859
079d0b7f 860 if (!is_valid(s, p->pid, p->ep->nr)) {
19b89252 861 trace_usb_host_req_complete(s->bus_num, s->addr, p, USB_RET_NAK);
a0b5fece
HG
862 return USB_RET_NAK;
863 }
864
2791104c 865 if (p->pid == USB_TOKEN_IN) {
079d0b7f 866 ep = p->ep->nr | 0x80;
2791104c 867 } else {
079d0b7f 868 ep = p->ep->nr;
2791104c 869 }
64838171 870
079d0b7f 871 if (is_halted(s, p->pid, p->ep->nr)) {
9b87e19b
GH
872 unsigned int arg = ep;
873 ret = ioctl(s->fd, USBDEVFS_CLEAR_HALT, &arg);
64838171 874 if (ret < 0) {
e6a2f500 875 perror("USBDEVFS_CLEAR_HALT");
19b89252 876 trace_usb_host_req_complete(s->bus_num, s->addr, p, USB_RET_NAK);
bb36d470 877 return USB_RET_NAK;
bb36d470 878 }
079d0b7f 879 clear_halt(s, p->pid, p->ep->nr);
4d043a09
AZ
880 }
881
079d0b7f 882 if (is_isoc(s, p->pid, p->ep->nr)) {
bb6d5498
HG
883 return usb_host_handle_iso_data(s, p, p->pid == USB_TOKEN_IN);
884 }
060dc841 885
b621bab4 886 v = 0;
818d59dc
GH
887 prem = 0;
888 pbuf = NULL;
b621bab4 889 rem = p->iov.size;
71138531 890 while (rem) {
b621bab4 891 if (prem == 0) {
b621bab4
GH
892 assert(v < p->iov.niov);
893 prem = p->iov.iov[v].iov_len;
894 pbuf = p->iov.iov[v].iov_base;
895 assert(prem <= rem);
818d59dc 896 v++;
b621bab4 897 }
71138531
GH
898 aurb = async_alloc(s);
899 aurb->packet = p;
900
901 urb = &aurb->urb;
902 urb->endpoint = ep;
d8e17efd 903 urb->type = usb_host_usbfs_type(s, p);
71138531
GH
904 urb->usercontext = s;
905 urb->buffer = pbuf;
b621bab4 906 urb->buffer_length = prem;
71138531 907
b621bab4 908 if (urb->buffer_length > MAX_USBFS_BUFFER_SIZE) {
71138531 909 urb->buffer_length = MAX_USBFS_BUFFER_SIZE;
71138531
GH
910 }
911 pbuf += urb->buffer_length;
b621bab4 912 prem -= urb->buffer_length;
71138531 913 rem -= urb->buffer_length;
b621bab4
GH
914 if (rem) {
915 aurb->more = 1;
916 }
b9dc033c 917
e6a2f500
GH
918 trace_usb_host_urb_submit(s->bus_num, s->addr, aurb,
919 urb->buffer_length, aurb->more);
71138531 920 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
b9dc033c 921
71138531
GH
922 DPRINTF("husb: data submit: ep 0x%x, len %u, more %d, packet %p, aurb %p\n",
923 urb->endpoint, urb->buffer_length, aurb->more, p, aurb);
b9dc033c 924
71138531 925 if (ret < 0) {
e6a2f500 926 perror("USBDEVFS_SUBMITURB");
71138531 927 async_free(aurb);
b9dc033c 928
71138531
GH
929 switch(errno) {
930 case ETIMEDOUT:
19b89252
GH
931 trace_usb_host_req_complete(s->bus_num, s->addr, p,
932 USB_RET_NAK);
71138531
GH
933 return USB_RET_NAK;
934 case EPIPE:
935 default:
19b89252
GH
936 trace_usb_host_req_complete(s->bus_num, s->addr, p,
937 USB_RET_STALL);
71138531
GH
938 return USB_RET_STALL;
939 }
b9dc033c
AZ
940 }
941 }
64838171 942
b9dc033c 943 return USB_RET_ASYNC;
b9dc033c
AZ
944}
945
446ab128
AL
946static int ctrl_error(void)
947{
2791104c 948 if (errno == ETIMEDOUT) {
446ab128 949 return USB_RET_NAK;
2791104c 950 } else {
446ab128 951 return USB_RET_STALL;
2791104c 952 }
446ab128
AL
953}
954
955static int usb_host_set_address(USBHostDevice *s, int addr)
956{
e6a2f500 957 trace_usb_host_set_address(s->bus_num, s->addr, addr);
446ab128
AL
958 s->dev.addr = addr;
959 return 0;
960}
961
962static int usb_host_set_config(USBHostDevice *s, int config)
963{
0fcc3bfc
GH
964 int ret, first = 1;
965
e6a2f500
GH
966 trace_usb_host_set_config(s->bus_num, s->addr, config);
967
446ab128
AL
968 usb_host_release_interfaces(s);
969
0fcc3bfc
GH
970again:
971 ret = ioctl(s->fd, USBDEVFS_SETCONFIGURATION, &config);
2791104c 972
d0f2c4c6 973 DPRINTF("husb: ctrl set config %d ret %d errno %d\n", config, ret, errno);
2791104c 974
0fcc3bfc
GH
975 if (ret < 0 && errno == EBUSY && first) {
976 /* happens if usb device is in use by host drivers */
977 int count = usb_linux_get_num_interfaces(s);
978 if (count > 0) {
979 DPRINTF("husb: busy -> disconnecting %d interfaces\n", count);
980 usb_host_disconnect_ifaces(s, count);
981 first = 0;
982 goto again;
983 }
984 }
985
2791104c 986 if (ret < 0) {
446ab128 987 return ctrl_error();
2791104c 988 }
446ab128 989 usb_host_claim_interfaces(s, config);
eb7700bb 990 usb_linux_update_endp_table(s);
446ab128
AL
991 return 0;
992}
993
994static int usb_host_set_interface(USBHostDevice *s, int iface, int alt)
995{
996 struct usbdevfs_setinterface si;
060dc841
HG
997 int i, ret;
998
e6a2f500
GH
999 trace_usb_host_set_interface(s->bus_num, s->addr, iface, alt);
1000
d8e17efd 1001 for (i = 1; i <= USB_MAX_ENDPOINTS; i++) {
c0e5750b
GH
1002 if (is_isoc(s, USB_TOKEN_IN, i)) {
1003 usb_host_stop_n_free_iso(s, USB_TOKEN_IN, i);
1004 }
1005 if (is_isoc(s, USB_TOKEN_OUT, i)) {
1006 usb_host_stop_n_free_iso(s, USB_TOKEN_OUT, i);
060dc841
HG
1007 }
1008 }
446ab128 1009
1de14d43
GH
1010 if (iface >= USB_MAX_INTERFACES) {
1011 return USB_RET_STALL;
1012 }
1013
446ab128
AL
1014 si.interface = iface;
1015 si.altsetting = alt;
1016 ret = ioctl(s->fd, USBDEVFS_SETINTERFACE, &si);
446ab128 1017
2791104c
DA
1018 DPRINTF("husb: ctrl set iface %d altset %d ret %d errno %d\n",
1019 iface, alt, ret, errno);
1020
1021 if (ret < 0) {
1022 return ctrl_error();
1023 }
1de14d43
GH
1024
1025 s->dev.altsetting[iface] = alt;
446ab128
AL
1026 usb_linux_update_endp_table(s);
1027 return 0;
1028}
1029
50b7963e
HG
1030static int usb_host_handle_control(USBDevice *dev, USBPacket *p,
1031 int request, int value, int index, int length, uint8_t *data)
446ab128 1032{
50b7963e 1033 USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
446ab128
AL
1034 struct usbdevfs_urb *urb;
1035 AsyncURB *aurb;
50b7963e 1036 int ret;
446ab128 1037
2791104c 1038 /*
446ab128
AL
1039 * Process certain standard device requests.
1040 * These are infrequent and are processed synchronously.
1041 */
446ab128 1042
50b7963e 1043 /* Note request is (bRequestType << 8) | bRequest */
19b89252 1044 trace_usb_host_req_control(s->bus_num, s->addr, p, request, value, index);
446ab128 1045
50b7963e
HG
1046 switch (request) {
1047 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
e382e751 1048 ret = usb_host_set_address(s, value);
19b89252 1049 trace_usb_host_req_emulated(s->bus_num, s->addr, p, ret);
e382e751 1050 return ret;
446ab128 1051
50b7963e 1052 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
e382e751 1053 ret = usb_host_set_config(s, value & 0xff);
19b89252 1054 trace_usb_host_req_emulated(s->bus_num, s->addr, p, ret);
e382e751 1055 return ret;
446ab128 1056
50b7963e 1057 case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
e382e751 1058 ret = usb_host_set_interface(s, index, value);
19b89252 1059 trace_usb_host_req_emulated(s->bus_num, s->addr, p, ret);
e382e751 1060 return ret;
2791104c 1061 }
446ab128
AL
1062
1063 /* The rest are asynchronous */
1064
50b7963e
HG
1065 if (length > sizeof(dev->data_buf)) {
1066 fprintf(stderr, "husb: ctrl buffer too small (%d > %zu)\n",
1067 length, sizeof(dev->data_buf));
b2e3b6e9 1068 return USB_RET_STALL;
c4c0e236
JP
1069 }
1070
7a8fc83f 1071 aurb = async_alloc(s);
446ab128
AL
1072 aurb->packet = p;
1073
2791104c 1074 /*
446ab128
AL
1075 * Setup ctrl transfer.
1076 *
a0102082 1077 * s->ctrl is laid out such that data buffer immediately follows
446ab128 1078 * 'req' struct which is exactly what usbdevfs expects.
2791104c 1079 */
446ab128
AL
1080 urb = &aurb->urb;
1081
1082 urb->type = USBDEVFS_URB_TYPE_CONTROL;
079d0b7f 1083 urb->endpoint = p->ep->nr;
446ab128 1084
50b7963e
HG
1085 urb->buffer = &dev->setup_buf;
1086 urb->buffer_length = length + 8;
446ab128
AL
1087
1088 urb->usercontext = s;
1089
e6a2f500
GH
1090 trace_usb_host_urb_submit(s->bus_num, s->addr, aurb,
1091 urb->buffer_length, aurb->more);
446ab128
AL
1092 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
1093
d0f2c4c6 1094 DPRINTF("husb: submit ctrl. len %u aurb %p\n", urb->buffer_length, aurb);
446ab128
AL
1095
1096 if (ret < 0) {
d0f2c4c6 1097 DPRINTF("husb: submit failed. errno %d\n", errno);
446ab128
AL
1098 async_free(aurb);
1099
1100 switch(errno) {
1101 case ETIMEDOUT:
1102 return USB_RET_NAK;
1103 case EPIPE:
1104 default:
1105 return USB_RET_STALL;
1106 }
1107 }
1108
446ab128
AL
1109 return USB_RET_ASYNC;
1110}
1111
71d71bbd
HG
1112/* returns 1 on problem encountered or 0 for success */
1113static int usb_linux_update_endp_table(USBHostDevice *s)
1114{
96dd9aac
GH
1115 static const char *tname[] = {
1116 [USB_ENDPOINT_XFER_CONTROL] = "control",
1117 [USB_ENDPOINT_XFER_ISOC] = "isoc",
1118 [USB_ENDPOINT_XFER_BULK] = "bulk",
1119 [USB_ENDPOINT_XFER_INT] = "int",
1120 };
1121 uint8_t devep, type;
1122 uint16_t mps, v, p;
1123 int ep, pid;
1124 unsigned int i, configuration = -1, interface = -1, altsetting = -1;
c0e5750b 1125 struct endp_data *epd;
96dd9aac
GH
1126 USBDescriptor *d;
1127 bool active = false;
71d71bbd 1128
d8e17efd 1129 usb_ep_init(&s->dev);
a0b5fece 1130
96dd9aac
GH
1131 for (i = 0;; i += d->bLength) {
1132 if (i+2 >= s->descr_len) {
1133 break;
b9dc033c 1134 }
96dd9aac
GH
1135 d = (void *)(s->descr + i);
1136 if (d->bLength < 2) {
1137 trace_usb_host_parse_error(s->bus_num, s->addr,
1138 "descriptor too short");
1139 goto error;
b9dc033c 1140 }
96dd9aac
GH
1141 if (i + d->bLength > s->descr_len) {
1142 trace_usb_host_parse_error(s->bus_num, s->addr,
1143 "descriptor too long");
1144 goto error;
2791104c 1145 }
96dd9aac
GH
1146 switch (d->bDescriptorType) {
1147 case 0:
1148 trace_usb_host_parse_error(s->bus_num, s->addr,
1149 "invalid descriptor type");
1150 goto error;
1151 case USB_DT_DEVICE:
1152 if (d->bLength < 0x12) {
1153 trace_usb_host_parse_error(s->bus_num, s->addr,
1154 "device descriptor too short");
1155 goto error;
1156 }
1157 v = (d->u.device.idVendor_hi << 8) | d->u.device.idVendor_lo;
1158 p = (d->u.device.idProduct_hi << 8) | d->u.device.idProduct_lo;
1159 trace_usb_host_parse_device(s->bus_num, s->addr, v, p);
b9dc033c 1160 break;
96dd9aac
GH
1161 case USB_DT_CONFIG:
1162 if (d->bLength < 0x09) {
1163 trace_usb_host_parse_error(s->bus_num, s->addr,
1164 "config descriptor too short");
1165 goto error;
2791104c 1166 }
96dd9aac
GH
1167 configuration = d->u.config.bConfigurationValue;
1168 active = (configuration == s->dev.configuration);
1169 trace_usb_host_parse_config(s->bus_num, s->addr,
1170 configuration, active);
1171 break;
1172 case USB_DT_INTERFACE:
1173 if (d->bLength < 0x09) {
1174 trace_usb_host_parse_error(s->bus_num, s->addr,
1175 "interface descriptor too short");
1176 goto error;
1177 }
1178 interface = d->u.interface.bInterfaceNumber;
1179 altsetting = d->u.interface.bAlternateSetting;
1180 active = (configuration == s->dev.configuration) &&
1181 (altsetting == s->dev.altsetting[interface]);
1182 trace_usb_host_parse_interface(s->bus_num, s->addr,
1183 interface, altsetting, active);
1184 break;
1185 case USB_DT_ENDPOINT:
1186 if (d->bLength < 0x07) {
1187 trace_usb_host_parse_error(s->bus_num, s->addr,
1188 "endpoint descriptor too short");
1189 goto error;
1190 }
1191 devep = d->u.endpoint.bEndpointAddress;
c0e5750b
GH
1192 pid = (devep & USB_DIR_IN) ? USB_TOKEN_IN : USB_TOKEN_OUT;
1193 ep = devep & 0xf;
1194 if (ep == 0) {
96dd9aac
GH
1195 trace_usb_host_parse_error(s->bus_num, s->addr,
1196 "invalid endpoint address");
1197 goto error;
130314f8
HG
1198 }
1199
96dd9aac
GH
1200 type = d->u.endpoint.bmAttributes & 0x3;
1201 mps = d->u.endpoint.wMaxPacketSize_lo |
1202 (d->u.endpoint.wMaxPacketSize_hi << 8);
1203 trace_usb_host_parse_endpoint(s->bus_num, s->addr, ep,
1204 (devep & USB_DIR_IN) ? "in" : "out",
1205 tname[type], active);
1206
1207 if (active) {
1208 usb_ep_set_max_packet_size(&s->dev, pid, ep, mps);
1209 assert(usb_ep_get_type(&s->dev, pid, ep) ==
1210 USB_ENDPOINT_XFER_INVALID);
1211 usb_ep_set_type(&s->dev, pid, ep, type);
1212 usb_ep_set_ifnum(&s->dev, pid, ep, interface);
1213 if ((s->options & (1 << USB_HOST_OPT_PIPELINE)) &&
1214 (type == USB_ENDPOINT_XFER_BULK)) {
1215 usb_ep_set_pipeline(&s->dev, pid, ep, true);
1216 }
d8e17efd 1217
96dd9aac
GH
1218 epd = get_endp(s, pid, ep);
1219 epd->halted = 0;
1220 }
b9dc033c 1221
96dd9aac
GH
1222 break;
1223 default:
1224 trace_usb_host_parse_unknown(s->bus_num, s->addr,
1225 d->bLength, d->bDescriptorType);
1226 break;
b9dc033c
AZ
1227 }
1228 }
1229 return 0;
96dd9aac
GH
1230
1231error:
1232 usb_ep_init(&s->dev);
1233 return 1;
b9dc033c
AZ
1234}
1235
e4b17767
HG
1236/*
1237 * Check if we can safely redirect a usb2 device to a usb1 virtual controller,
1238 * this function assumes this is safe, if:
1239 * 1) There are no isoc endpoints
1240 * 2) There are no interrupt endpoints with a max_packet_size > 64
1241 * Note bulk endpoints with a max_packet_size > 64 in theory also are not
1242 * usb1 compatible, but in practice this seems to work fine.
1243 */
1244static int usb_linux_full_speed_compat(USBHostDevice *dev)
1245{
1246 int i, packet_size;
1247
1248 /*
1249 * usb_linux_update_endp_table only registers info about ep in the current
1250 * interface altsettings, so we need to parse the descriptors again.
1251 */
1252 for (i = 0; (i + 5) < dev->descr_len; i += dev->descr[i]) {
1253 if (dev->descr[i + 1] == USB_DT_ENDPOINT) {
1254 switch (dev->descr[i + 3] & 0x3) {
1255 case 0x00: /* CONTROL */
1256 break;
1257 case 0x01: /* ISO */
1258 return 0;
1259 case 0x02: /* BULK */
1260 break;
1261 case 0x03: /* INTERRUPT */
1262 packet_size = dev->descr[i + 4] + (dev->descr[i + 5] << 8);
1263 if (packet_size > 64)
1264 return 0;
1265 break;
1266 }
1267 }
1268 }
1269 return 1;
1270}
1271
26a9e82a 1272static int usb_host_open(USBHostDevice *dev, int bus_num,
ba9acab9
GH
1273 int addr, const char *port,
1274 const char *prod_name, int speed)
bb36d470 1275{
b9dc033c 1276 int fd = -1, ret;
1f3870ab 1277
e6a2f500
GH
1278 trace_usb_host_open_started(bus_num, addr);
1279
2791104c 1280 if (dev->fd != -1) {
26a9e82a 1281 goto fail;
2791104c 1282 }
3b46e624 1283
097db438 1284 fd = usb_host_open_device(bus_num, addr);
bb36d470 1285 if (fd < 0) {
1f3870ab 1286 goto fail;
bb36d470 1287 }
d0f2c4c6 1288 DPRINTF("husb: opened %s\n", buf);
bb36d470 1289
806b6024
GH
1290 dev->bus_num = bus_num;
1291 dev->addr = addr;
5557d820 1292 strcpy(dev->port, port);
22f84e73 1293 dev->fd = fd;
806b6024 1294
b9dc033c
AZ
1295 /* read the device description */
1296 dev->descr_len = read(fd, dev->descr, sizeof(dev->descr));
1297 if (dev->descr_len <= 0) {
64838171 1298 perror("husb: reading device data failed");
bb36d470
FB
1299 goto fail;
1300 }
3b46e624 1301
b9dc033c 1302#ifdef DEBUG
868bfe2b 1303 {
b9dc033c
AZ
1304 int x;
1305 printf("=== begin dumping device descriptor data ===\n");
2791104c 1306 for (x = 0; x < dev->descr_len; x++) {
b9dc033c 1307 printf("%02x ", dev->descr[x]);
2791104c 1308 }
b9dc033c 1309 printf("\n=== end dumping device descriptor data ===\n");
bb36d470 1310 }
a594cfbf
FB
1311#endif
1312
b9dc033c 1313
eb7700bb
GH
1314 /* start unconfigured -- we'll wait for the guest to set a configuration */
1315 if (!usb_host_claim_interfaces(dev, 0)) {
b9dc033c 1316 goto fail;
2791104c 1317 }
bb36d470 1318
b9dc033c 1319 ret = usb_linux_update_endp_table(dev);
2791104c 1320 if (ret) {
bb36d470 1321 goto fail;
2791104c 1322 }
b9dc033c 1323
3991c35e
HG
1324 if (speed == -1) {
1325 struct usbdevfs_connectinfo ci;
1326
1327 ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci);
1328 if (ret < 0) {
1329 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
1330 goto fail;
1331 }
1332
1333 if (ci.slow) {
1334 speed = USB_SPEED_LOW;
1335 } else {
1336 speed = USB_SPEED_HIGH;
1337 }
2791104c 1338 }
3991c35e 1339 dev->dev.speed = speed;
ba3f9bfb 1340 dev->dev.speedmask = (1 << speed);
e4b17767
HG
1341 if (dev->dev.speed == USB_SPEED_HIGH && usb_linux_full_speed_compat(dev)) {
1342 dev->dev.speedmask |= USB_SPEED_MASK_FULL;
1343 }
3991c35e 1344
e6a2f500 1345 trace_usb_host_open_success(bus_num, addr);
bb36d470 1346
2791104c 1347 if (!prod_name || prod_name[0] == '\0') {
0fe6d12e 1348 snprintf(dev->dev.product_desc, sizeof(dev->dev.product_desc),
4b096fc9 1349 "host:%d.%d", bus_num, addr);
2791104c 1350 } else {
0fe6d12e 1351 pstrcpy(dev->dev.product_desc, sizeof(dev->dev.product_desc),
4b096fc9 1352 prod_name);
2791104c 1353 }
1f6e24e7 1354
fa19bf83
HG
1355 ret = usb_device_attach(&dev->dev);
1356 if (ret) {
1357 goto fail;
1358 }
1359
64838171
AL
1360 /* USB devio uses 'write' flag to check for async completions */
1361 qemu_set_fd_handler(dev->fd, NULL, async_complete, dev);
1f3870ab 1362
26a9e82a 1363 return 0;
4b096fc9 1364
b9dc033c 1365fail:
e6a2f500 1366 trace_usb_host_open_failure(bus_num, addr);
1f45a81b
GH
1367 if (dev->fd != -1) {
1368 close(dev->fd);
1369 dev->fd = -1;
2791104c 1370 }
26a9e82a
GH
1371 return -1;
1372}
1373
1374static int usb_host_close(USBHostDevice *dev)
1375{
060dc841
HG
1376 int i;
1377
39fba3ad 1378 if (dev->fd == -1) {
26a9e82a 1379 return -1;
2791104c 1380 }
26a9e82a 1381
e6a2f500
GH
1382 trace_usb_host_close(dev->bus_num, dev->addr);
1383
26a9e82a
GH
1384 qemu_set_fd_handler(dev->fd, NULL, NULL, NULL);
1385 dev->closing = 1;
d8e17efd 1386 for (i = 1; i <= USB_MAX_ENDPOINTS; i++) {
c0e5750b
GH
1387 if (is_isoc(dev, USB_TOKEN_IN, i)) {
1388 usb_host_stop_n_free_iso(dev, USB_TOKEN_IN, i);
1389 }
1390 if (is_isoc(dev, USB_TOKEN_OUT, i)) {
1391 usb_host_stop_n_free_iso(dev, USB_TOKEN_OUT, i);
060dc841
HG
1392 }
1393 }
26a9e82a
GH
1394 async_complete(dev);
1395 dev->closing = 0;
39fba3ad
GH
1396 if (dev->dev.attached) {
1397 usb_device_detach(&dev->dev);
1398 }
c7662daa 1399 usb_host_do_reset(dev);
26a9e82a
GH
1400 close(dev->fd);
1401 dev->fd = -1;
1402 return 0;
1403}
1404
9e8dd451 1405static void usb_host_exit_notifier(struct Notifier *n, void *data)
b373a63a
SH
1406{
1407 USBHostDevice *s = container_of(n, USBHostDevice, exit);
1408
c75fead6 1409 usb_host_release_port(s);
b373a63a 1410 if (s->fd != -1) {
c7662daa 1411 usb_host_do_reset(s);;
b373a63a
SH
1412 }
1413}
1414
26a9e82a
GH
1415static int usb_host_initfn(USBDevice *dev)
1416{
1417 USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
1418
1419 dev->auto_attach = 0;
1420 s->fd = -1;
9516bb47
GH
1421 s->hub_fd = -1;
1422
26a9e82a 1423 QTAILQ_INSERT_TAIL(&hostdevs, s, next);
b373a63a
SH
1424 s->exit.notify = usb_host_exit_notifier;
1425 qemu_add_exit_notifier(&s->exit);
26a9e82a 1426 usb_host_auto_check(NULL);
9516bb47 1427
9516bb47 1428 if (s->match.bus_num != 0 && s->match.port != NULL) {
e6274727 1429 usb_host_claim_port(s);
9516bb47 1430 }
65bb3a5c 1431 add_boot_device_path(s->bootindex, &dev->qdev, NULL);
26a9e82a 1432 return 0;
a594cfbf 1433}
bb36d470 1434
d6791578
GH
1435static const VMStateDescription vmstate_usb_host = {
1436 .name = "usb-host",
1437 .unmigratable = 1,
1438};
1439
39bffca2
AL
1440static Property usb_host_dev_properties[] = {
1441 DEFINE_PROP_UINT32("hostbus", USBHostDevice, match.bus_num, 0),
1442 DEFINE_PROP_UINT32("hostaddr", USBHostDevice, match.addr, 0),
1443 DEFINE_PROP_STRING("hostport", USBHostDevice, match.port),
1444 DEFINE_PROP_HEX32("vendorid", USBHostDevice, match.vendor_id, 0),
1445 DEFINE_PROP_HEX32("productid", USBHostDevice, match.product_id, 0),
1446 DEFINE_PROP_UINT32("isobufs", USBHostDevice, iso_urb_count, 4),
65bb3a5c 1447 DEFINE_PROP_INT32("bootindex", USBHostDevice, bootindex, -1),
39c20577
GH
1448 DEFINE_PROP_BIT("pipeline", USBHostDevice, options,
1449 USB_HOST_OPT_PIPELINE, true),
39bffca2
AL
1450 DEFINE_PROP_END_OF_LIST(),
1451};
1452
62aed765
AL
1453static void usb_host_class_initfn(ObjectClass *klass, void *data)
1454{
39bffca2 1455 DeviceClass *dc = DEVICE_CLASS(klass);
62aed765
AL
1456 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
1457
1458 uc->init = usb_host_initfn;
1459 uc->product_desc = "USB Host Device";
62aed765
AL
1460 uc->cancel_packet = usb_host_async_cancel;
1461 uc->handle_data = usb_host_handle_data;
1462 uc->handle_control = usb_host_handle_control;
1463 uc->handle_reset = usb_host_handle_reset;
1464 uc->handle_destroy = usb_host_handle_destroy;
39bffca2
AL
1465 dc->vmsd = &vmstate_usb_host;
1466 dc->props = usb_host_dev_properties;
62aed765
AL
1467}
1468
39bffca2
AL
1469static TypeInfo usb_host_dev_info = {
1470 .name = "usb-host",
1471 .parent = TYPE_USB_DEVICE,
1472 .instance_size = sizeof(USBHostDevice),
1473 .class_init = usb_host_class_initfn,
806b6024
GH
1474};
1475
83f7d43a 1476static void usb_host_register_types(void)
806b6024 1477{
39bffca2 1478 type_register_static(&usb_host_dev_info);
ba02430f 1479 usb_legacy_register("usb-host", "host", usb_host_device_open);
806b6024 1480}
83f7d43a
AF
1481
1482type_init(usb_host_register_types)
806b6024 1483
3741715c 1484USBDevice *usb_host_device_open(USBBus *bus, const char *devname)
4b096fc9 1485{
0745eb1e 1486 struct USBAutoFilter filter;
26a9e82a 1487 USBDevice *dev;
26a9e82a
GH
1488 char *p;
1489
3741715c 1490 dev = usb_create(bus, "usb-host");
4b096fc9 1491
5d0c5750 1492 if (strstr(devname, "auto:")) {
2791104c 1493 if (parse_filter(devname, &filter) < 0) {
26a9e82a 1494 goto fail;
2791104c 1495 }
26a9e82a
GH
1496 } else {
1497 if ((p = strchr(devname, '.'))) {
0745eb1e
MA
1498 filter.bus_num = strtoul(devname, NULL, 0);
1499 filter.addr = strtoul(p + 1, NULL, 0);
1500 filter.vendor_id = 0;
1501 filter.product_id = 0;
26a9e82a 1502 } else if ((p = strchr(devname, ':'))) {
0745eb1e
MA
1503 filter.bus_num = 0;
1504 filter.addr = 0;
26a9e82a 1505 filter.vendor_id = strtoul(devname, NULL, 16);
0745eb1e 1506 filter.product_id = strtoul(p + 1, NULL, 16);
26a9e82a
GH
1507 } else {
1508 goto fail;
1509 }
5d0c5750 1510 }
4b096fc9 1511
0745eb1e
MA
1512 qdev_prop_set_uint32(&dev->qdev, "hostbus", filter.bus_num);
1513 qdev_prop_set_uint32(&dev->qdev, "hostaddr", filter.addr);
26a9e82a
GH
1514 qdev_prop_set_uint32(&dev->qdev, "vendorid", filter.vendor_id);
1515 qdev_prop_set_uint32(&dev->qdev, "productid", filter.product_id);
beb6f0de 1516 qdev_init_nofail(&dev->qdev);
26a9e82a 1517 return dev;
5d0c5750 1518
26a9e82a
GH
1519fail:
1520 qdev_free(&dev->qdev);
1521 return NULL;
4b096fc9 1522}
5d0c5750
AL
1523
1524int usb_host_device_close(const char *devname)
1525{
26a9e82a 1526#if 0
5d0c5750
AL
1527 char product_name[PRODUCT_NAME_SZ];
1528 int bus_num, addr;
1529 USBHostDevice *s;
1530
2791104c 1531 if (strstr(devname, "auto:")) {
5d0c5750 1532 return usb_host_auto_del(devname);
2791104c
DA
1533 }
1534 if (usb_host_find_device(&bus_num, &addr, product_name,
1535 sizeof(product_name), devname) < 0) {
5d0c5750 1536 return -1;
2791104c 1537 }
5d0c5750
AL
1538 s = hostdev_find(bus_num, addr);
1539 if (s) {
a5d2f727 1540 usb_device_delete_addr(s->bus_num, s->dev.addr);
5d0c5750
AL
1541 return 0;
1542 }
26a9e82a 1543#endif
5d0c5750
AL
1544
1545 return -1;
1546}
a5d2f727 1547
0f431527
AL
1548/*
1549 * Read sys file-system device file
1550 *
1551 * @line address of buffer to put file contents in
1552 * @line_size size of line
1553 * @device_file path to device file (printf format string)
1554 * @device_name device being opened (inserted into device_file)
1555 *
1556 * @return 0 failed, 1 succeeded ('line' contains data)
1557 */
2791104c
DA
1558static int usb_host_read_file(char *line, size_t line_size,
1559 const char *device_file, const char *device_name)
0f431527
AL
1560{
1561 FILE *f;
1562 int ret = 0;
1563 char filename[PATH_MAX];
1564
097db438 1565 snprintf(filename, PATH_MAX, "/sys/bus/usb/devices/%s/%s", device_name,
b4e237aa 1566 device_file);
0f431527
AL
1567 f = fopen(filename, "r");
1568 if (f) {
9f99cee7 1569 ret = fgets(line, line_size, f) != NULL;
0f431527 1570 fclose(f);
0f431527
AL
1571 }
1572
1573 return ret;
1574}
1575
1576/*
1577 * Use /sys/bus/usb/devices/ directory to determine host's USB
1578 * devices.
1579 *
1580 * This code is based on Robert Schiele's original patches posted to
1581 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1582 */
097db438 1583static int usb_host_scan(void *opaque, USBScanFunc *func)
0f431527 1584{
660f11be 1585 DIR *dir = NULL;
0f431527 1586 char line[1024];
5557d820 1587 int bus_num, addr, speed, class_id, product_id, vendor_id;
0f431527 1588 int ret = 0;
5557d820 1589 char port[MAX_PORTLEN];
0f431527
AL
1590 char product_name[512];
1591 struct dirent *de;
1592
097db438 1593 dir = opendir("/sys/bus/usb/devices");
0f431527 1594 if (!dir) {
097db438
GH
1595 perror("husb: opendir /sys/bus/usb/devices");
1596 fprintf(stderr, "husb: please make sure sysfs is mounted at /sys\n");
0f431527
AL
1597 goto the_end;
1598 }
1599
1600 while ((de = readdir(dir))) {
1601 if (de->d_name[0] != '.' && !strchr(de->d_name, ':')) {
5557d820
GH
1602 if (sscanf(de->d_name, "%d-%7[0-9.]", &bus_num, port) < 2) {
1603 continue;
0f5160d1 1604 }
0f431527 1605
2791104c 1606 if (!usb_host_read_file(line, sizeof(line), "devnum", de->d_name)) {
0f431527 1607 goto the_end;
2791104c
DA
1608 }
1609 if (sscanf(line, "%d", &addr) != 1) {
0f431527 1610 goto the_end;
2791104c 1611 }
b4e237aa 1612 if (!usb_host_read_file(line, sizeof(line), "bDeviceClass",
2791104c 1613 de->d_name)) {
0f431527 1614 goto the_end;
2791104c
DA
1615 }
1616 if (sscanf(line, "%x", &class_id) != 1) {
0f431527 1617 goto the_end;
2791104c 1618 }
0f431527 1619
2791104c
DA
1620 if (!usb_host_read_file(line, sizeof(line), "idVendor",
1621 de->d_name)) {
0f431527 1622 goto the_end;
2791104c
DA
1623 }
1624 if (sscanf(line, "%x", &vendor_id) != 1) {
0f431527 1625 goto the_end;
2791104c 1626 }
b4e237aa 1627 if (!usb_host_read_file(line, sizeof(line), "idProduct",
2791104c 1628 de->d_name)) {
0f431527 1629 goto the_end;
2791104c
DA
1630 }
1631 if (sscanf(line, "%x", &product_id) != 1) {
0f431527 1632 goto the_end;
2791104c 1633 }
b4e237aa
BS
1634 if (!usb_host_read_file(line, sizeof(line), "product",
1635 de->d_name)) {
0f431527
AL
1636 *product_name = 0;
1637 } else {
2791104c 1638 if (strlen(line) > 0) {
0f431527 1639 line[strlen(line) - 1] = '\0';
2791104c 1640 }
0f431527
AL
1641 pstrcpy(product_name, sizeof(product_name), line);
1642 }
1643
2791104c 1644 if (!usb_host_read_file(line, sizeof(line), "speed", de->d_name)) {
0f431527 1645 goto the_end;
2791104c 1646 }
f264cfbf
HG
1647 if (!strcmp(line, "5000\n")) {
1648 speed = USB_SPEED_SUPER;
1649 } else if (!strcmp(line, "480\n")) {
0f431527 1650 speed = USB_SPEED_HIGH;
2791104c 1651 } else if (!strcmp(line, "1.5\n")) {
0f431527 1652 speed = USB_SPEED_LOW;
2791104c 1653 } else {
0f431527 1654 speed = USB_SPEED_FULL;
2791104c 1655 }
0f431527 1656
5557d820 1657 ret = func(opaque, bus_num, addr, port, class_id, vendor_id,
0f431527 1658 product_id, product_name, speed);
2791104c 1659 if (ret) {
0f431527 1660 goto the_end;
2791104c 1661 }
0f431527
AL
1662 }
1663 }
1664 the_end:
2791104c 1665 if (dir) {
0f431527 1666 closedir(dir);
2791104c 1667 }
0f431527
AL
1668 return ret;
1669}
1670
4b096fc9 1671static QEMUTimer *usb_auto_timer;
4b096fc9 1672
ba9acab9
GH
1673static int usb_host_auto_scan(void *opaque, int bus_num,
1674 int addr, const char *port,
26a9e82a
GH
1675 int class_id, int vendor_id, int product_id,
1676 const char *product_name, int speed)
4b096fc9
AL
1677{
1678 struct USBAutoFilter *f;
26a9e82a 1679 struct USBHostDevice *s;
4b096fc9
AL
1680
1681 /* Ignore hubs */
1682 if (class_id == 9)
1683 return 0;
1684
26a9e82a
GH
1685 QTAILQ_FOREACH(s, &hostdevs, next) {
1686 f = &s->match;
1687
2791104c 1688 if (f->bus_num > 0 && f->bus_num != bus_num) {
4b096fc9 1689 continue;
2791104c
DA
1690 }
1691 if (f->addr > 0 && f->addr != addr) {
4b096fc9 1692 continue;
2791104c 1693 }
9056a297
GH
1694 if (f->port != NULL && (port == NULL || strcmp(f->port, port) != 0)) {
1695 continue;
1696 }
4b096fc9 1697
2791104c 1698 if (f->vendor_id > 0 && f->vendor_id != vendor_id) {
4b096fc9 1699 continue;
2791104c 1700 }
4b096fc9 1701
2791104c 1702 if (f->product_id > 0 && f->product_id != product_id) {
4b096fc9 1703 continue;
2791104c 1704 }
4b096fc9 1705 /* We got a match */
3ee886c5
GH
1706 s->seen++;
1707 if (s->errcount >= 3) {
1708 return 0;
1709 }
4b096fc9 1710
33e66b86 1711 /* Already attached ? */
2791104c 1712 if (s->fd != -1) {
4b096fc9 1713 return 0;
2791104c 1714 }
d0f2c4c6 1715 DPRINTF("husb: auto open: bus_num %d addr %d\n", bus_num, addr);
4b096fc9 1716
3ee886c5
GH
1717 if (usb_host_open(s, bus_num, addr, port, product_name, speed) < 0) {
1718 s->errcount++;
1719 }
97f86166 1720 break;
4b096fc9
AL
1721 }
1722
1723 return 0;
1724}
1725
26a9e82a 1726static void usb_host_auto_check(void *unused)
4b096fc9 1727{
26a9e82a
GH
1728 struct USBHostDevice *s;
1729 int unconnected = 0;
1730
4b096fc9 1731 usb_host_scan(NULL, usb_host_auto_scan);
26a9e82a
GH
1732
1733 QTAILQ_FOREACH(s, &hostdevs, next) {
2791104c 1734 if (s->fd == -1) {
26a9e82a 1735 unconnected++;
2791104c 1736 }
3ee886c5
GH
1737 if (s->seen == 0) {
1738 s->errcount = 0;
1739 }
1740 s->seen = 0;
26a9e82a
GH
1741 }
1742
1743 if (unconnected == 0) {
1744 /* nothing to watch */
2791104c 1745 if (usb_auto_timer) {
26a9e82a 1746 qemu_del_timer(usb_auto_timer);
e6a2f500 1747 trace_usb_host_auto_scan_disabled();
2791104c 1748 }
26a9e82a
GH
1749 return;
1750 }
1751
1752 if (!usb_auto_timer) {
7bd427d8 1753 usb_auto_timer = qemu_new_timer_ms(rt_clock, usb_host_auto_check, NULL);
2791104c 1754 if (!usb_auto_timer) {
26a9e82a 1755 return;
2791104c 1756 }
e6a2f500 1757 trace_usb_host_auto_scan_enabled();
26a9e82a 1758 }
7bd427d8 1759 qemu_mod_timer(usb_auto_timer, qemu_get_clock_ms(rt_clock) + 2000);
4b096fc9
AL
1760}
1761
1762/*
5d0c5750
AL
1763 * Autoconnect filter
1764 * Format:
1765 * auto:bus:dev[:vid:pid]
1766 * auto:bus.dev[:vid:pid]
1767 *
1768 * bus - bus number (dec, * means any)
1769 * dev - device number (dec, * means any)
1770 * vid - vendor id (hex, * means any)
1771 * pid - product id (hex, * means any)
1772 *
1773 * See 'lsusb' output.
4b096fc9 1774 */
5d0c5750 1775static int parse_filter(const char *spec, struct USBAutoFilter *f)
4b096fc9 1776{
5d0c5750
AL
1777 enum { BUS, DEV, VID, PID, DONE };
1778 const char *p = spec;
1779 int i;
1780
0745eb1e
MA
1781 f->bus_num = 0;
1782 f->addr = 0;
1783 f->vendor_id = 0;
1784 f->product_id = 0;
5d0c5750
AL
1785
1786 for (i = BUS; i < DONE; i++) {
2791104c
DA
1787 p = strpbrk(p, ":.");
1788 if (!p) {
1789 break;
1790 }
5d0c5750 1791 p++;
5d0c5750 1792
2791104c
DA
1793 if (*p == '*') {
1794 continue;
1795 }
5d0c5750
AL
1796 switch(i) {
1797 case BUS: f->bus_num = strtol(p, NULL, 10); break;
1798 case DEV: f->addr = strtol(p, NULL, 10); break;
1799 case VID: f->vendor_id = strtol(p, NULL, 16); break;
1800 case PID: f->product_id = strtol(p, NULL, 16); break;
1801 }
1802 }
1803
1804 if (i < DEV) {
1805 fprintf(stderr, "husb: invalid auto filter spec %s\n", spec);
1806 return -1;
1807 }
1808
1809 return 0;
1810}
1811
a594cfbf
FB
1812/**********************/
1813/* USB host device info */
1814
1815struct usb_class_info {
1816 int class;
1817 const char *class_name;
1818};
1819
1820static const struct usb_class_info usb_class_info[] = {
1821 { USB_CLASS_AUDIO, "Audio"},
1822 { USB_CLASS_COMM, "Communication"},
1823 { USB_CLASS_HID, "HID"},
1824 { USB_CLASS_HUB, "Hub" },
1825 { USB_CLASS_PHYSICAL, "Physical" },
1826 { USB_CLASS_PRINTER, "Printer" },
1827 { USB_CLASS_MASS_STORAGE, "Storage" },
1828 { USB_CLASS_CDC_DATA, "Data" },
1829 { USB_CLASS_APP_SPEC, "Application Specific" },
1830 { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
1831 { USB_CLASS_STILL_IMAGE, "Still Image" },
b9dc033c 1832 { USB_CLASS_CSCID, "Smart Card" },
a594cfbf
FB
1833 { USB_CLASS_CONTENT_SEC, "Content Security" },
1834 { -1, NULL }
1835};
1836
1837static const char *usb_class_str(uint8_t class)
bb36d470 1838{
a594cfbf
FB
1839 const struct usb_class_info *p;
1840 for(p = usb_class_info; p->class != -1; p++) {
2791104c 1841 if (p->class == class) {
a594cfbf 1842 break;
2791104c 1843 }
bb36d470 1844 }
a594cfbf
FB
1845 return p->class_name;
1846}
1847
ba9acab9
GH
1848static void usb_info_device(Monitor *mon, int bus_num,
1849 int addr, const char *port,
5557d820 1850 int class_id, int vendor_id, int product_id,
9596ebb7
PB
1851 const char *product_name,
1852 int speed)
a594cfbf
FB
1853{
1854 const char *class_str, *speed_str;
1855
1856 switch(speed) {
5fafdf24
TS
1857 case USB_SPEED_LOW:
1858 speed_str = "1.5";
a594cfbf 1859 break;
5fafdf24
TS
1860 case USB_SPEED_FULL:
1861 speed_str = "12";
a594cfbf 1862 break;
5fafdf24
TS
1863 case USB_SPEED_HIGH:
1864 speed_str = "480";
a594cfbf 1865 break;
f264cfbf
HG
1866 case USB_SPEED_SUPER:
1867 speed_str = "5000";
1868 break;
a594cfbf 1869 default:
5fafdf24 1870 speed_str = "?";
a594cfbf
FB
1871 break;
1872 }
1873
5557d820
GH
1874 monitor_printf(mon, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
1875 bus_num, addr, port, speed_str);
a594cfbf 1876 class_str = usb_class_str(class_id);
2791104c 1877 if (class_str) {
376253ec 1878 monitor_printf(mon, " %s:", class_str);
2791104c 1879 } else {
376253ec 1880 monitor_printf(mon, " Class %02x:", class_id);
2791104c 1881 }
376253ec 1882 monitor_printf(mon, " USB device %04x:%04x", vendor_id, product_id);
2791104c 1883 if (product_name[0] != '\0') {
376253ec 1884 monitor_printf(mon, ", %s", product_name);
2791104c 1885 }
376253ec 1886 monitor_printf(mon, "\n");
a594cfbf
FB
1887}
1888
5fafdf24 1889static int usb_host_info_device(void *opaque, int bus_num, int addr,
ba9acab9 1890 const char *path, int class_id,
5fafdf24 1891 int vendor_id, int product_id,
a594cfbf
FB
1892 const char *product_name,
1893 int speed)
1894{
179da8af
BS
1895 Monitor *mon = opaque;
1896
5557d820 1897 usb_info_device(mon, bus_num, addr, path, class_id, vendor_id, product_id,
a594cfbf
FB
1898 product_name, speed);
1899 return 0;
1900}
1901
ac4ffb5a 1902static void dec2str(int val, char *str, size_t size)
5d0c5750 1903{
2791104c 1904 if (val == 0) {
ac4ffb5a 1905 snprintf(str, size, "*");
2791104c
DA
1906 } else {
1907 snprintf(str, size, "%d", val);
1908 }
5d0c5750
AL
1909}
1910
ac4ffb5a 1911static void hex2str(int val, char *str, size_t size)
5d0c5750 1912{
2791104c 1913 if (val == 0) {
ac4ffb5a 1914 snprintf(str, size, "*");
2791104c 1915 } else {
26a9e82a 1916 snprintf(str, size, "%04x", val);
2791104c 1917 }
5d0c5750
AL
1918}
1919
376253ec 1920void usb_host_info(Monitor *mon)
a594cfbf 1921{
5d0c5750 1922 struct USBAutoFilter *f;
26a9e82a 1923 struct USBHostDevice *s;
5d0c5750 1924
179da8af 1925 usb_host_scan(mon, usb_host_info_device);
5d0c5750 1926
2791104c 1927 if (QTAILQ_EMPTY(&hostdevs)) {
26a9e82a 1928 return;
2791104c
DA
1929 }
1930
26a9e82a
GH
1931 monitor_printf(mon, " Auto filters:\n");
1932 QTAILQ_FOREACH(s, &hostdevs, next) {
5d0c5750 1933 char bus[10], addr[10], vid[10], pid[10];
26a9e82a 1934 f = &s->match;
ac4ffb5a
AL
1935 dec2str(f->bus_num, bus, sizeof(bus));
1936 dec2str(f->addr, addr, sizeof(addr));
1937 hex2str(f->vendor_id, vid, sizeof(vid));
1938 hex2str(f->product_id, pid, sizeof(pid));
9056a297
GH
1939 monitor_printf(mon, " Bus %s, Addr %s, Port %s, ID %s:%s\n",
1940 bus, addr, f->port ? f->port : "*", vid, pid);
5d0c5750 1941 }
bb36d470 1942}