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