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