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