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