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