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