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