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