]>
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 <linux@tjworld.net> |
11 | * Added flexible support for /dev/bus/usb /sys/bus/usb/devices in addition | |
12 | * to the legacy /proc/bus/usb USB device discovery and handling | |
13 | * | |
bb36d470 FB |
14 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
15 | * of this software and associated documentation files (the "Software"), to deal | |
16 | * in the Software without restriction, including without limitation the rights | |
17 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
18 | * copies of the Software, and to permit persons to whom the Software is | |
19 | * furnished to do so, subject to the following conditions: | |
20 | * | |
21 | * The above copyright notice and this permission notice shall be included in | |
22 | * all copies or substantial portions of the Software. | |
23 | * | |
24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
25 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
26 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
27 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
28 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
29 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
30 | * THE SOFTWARE. | |
31 | */ | |
446ab128 | 32 | |
87ecb68b | 33 | #include "qemu-common.h" |
1f3870ab | 34 | #include "qemu-timer.h" |
376253ec | 35 | #include "monitor.h" |
b373a63a | 36 | #include "sysemu.h" |
e6a2f500 | 37 | #include "trace.h" |
bb36d470 | 38 | |
bb36d470 FB |
39 | #include <dirent.h> |
40 | #include <sys/ioctl.h> | |
bb36d470 | 41 | |
446ab128 AL |
42 | #include <linux/usbdevice_fs.h> |
43 | #include <linux/version.h> | |
44 | #include "hw/usb.h" | |
bb36d470 | 45 | |
d9cf1578 BS |
46 | /* We redefine it to avoid version problems */ |
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; |
c75fead6 | 119 | int hub_port; |
64838171 | 120 | |
f8ddbfbc | 121 | uint8_t descr[8192]; |
64838171 AL |
122 | int descr_len; |
123 | int configuration; | |
446ab128 | 124 | int ninterfaces; |
24772c1e | 125 | int closing; |
b81bcd8a | 126 | uint32_t iso_urb_count; |
b373a63a | 127 | Notifier exit; |
64838171 | 128 | |
c0e5750b GH |
129 | struct endp_data ep_in[MAX_ENDPOINTS]; |
130 | struct endp_data ep_out[MAX_ENDPOINTS]; | |
7a8fc83f | 131 | QLIST_HEAD(, AsyncURB) aurbs; |
4b096fc9 | 132 | |
4b096fc9 AL |
133 | /* Host side address */ |
134 | int bus_num; | |
135 | int addr; | |
5557d820 | 136 | char port[MAX_PORTLEN]; |
26a9e82a | 137 | struct USBAutoFilter match; |
3ee886c5 | 138 | int seen, errcount; |
4b096fc9 | 139 | |
26a9e82a | 140 | QTAILQ_ENTRY(USBHostDevice) next; |
bb36d470 FB |
141 | } USBHostDevice; |
142 | ||
26a9e82a GH |
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); | |
2cc59d8c HG |
148 | static int usb_host_read_file(char *line, size_t line_size, |
149 | const char *device_file, const char *device_name); | |
9b87e19b | 150 | static int usb_linux_update_endp_table(USBHostDevice *s); |
26a9e82a | 151 | |
c7662daa GH |
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 | } | |
170 | ||
c0e5750b | 171 | static struct endp_data *get_endp(USBHostDevice *s, int pid, int ep) |
ca3a36cf | 172 | { |
c0e5750b GH |
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; | |
ca3a36cf GH |
177 | } |
178 | ||
c0e5750b | 179 | static int is_isoc(USBHostDevice *s, int pid, int ep) |
64838171 | 180 | { |
c0e5750b | 181 | return get_endp(s, pid, ep)->type == USBDEVFS_URB_TYPE_ISO; |
64838171 AL |
182 | } |
183 | ||
c0e5750b | 184 | static int is_valid(USBHostDevice *s, int pid, int ep) |
a0b5fece | 185 | { |
c0e5750b | 186 | return get_endp(s, pid, ep)->type != INVALID_EP_TYPE; |
a0b5fece HG |
187 | } |
188 | ||
c0e5750b | 189 | static int is_halted(USBHostDevice *s, int pid, int ep) |
64838171 | 190 | { |
c0e5750b | 191 | return get_endp(s, pid, ep)->halted; |
64838171 AL |
192 | } |
193 | ||
c0e5750b | 194 | static void clear_halt(USBHostDevice *s, int pid, int ep) |
64838171 | 195 | { |
e6a2f500 | 196 | trace_usb_host_ep_clear_halt(s->bus_num, s->addr, ep); |
c0e5750b | 197 | get_endp(s, pid, ep)->halted = 0; |
64838171 AL |
198 | } |
199 | ||
c0e5750b | 200 | static void set_halt(USBHostDevice *s, int pid, int ep) |
64838171 | 201 | { |
c0e5750b GH |
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 | } | |
64838171 AL |
206 | } |
207 | ||
c0e5750b | 208 | static int is_iso_started(USBHostDevice *s, int pid, int ep) |
bb6d5498 | 209 | { |
c0e5750b | 210 | return get_endp(s, pid, ep)->iso_started; |
bb6d5498 HG |
211 | } |
212 | ||
c0e5750b | 213 | static void clear_iso_started(USBHostDevice *s, int pid, int ep) |
bb6d5498 | 214 | { |
e6a2f500 | 215 | trace_usb_host_ep_stop_iso(s->bus_num, s->addr, ep); |
c0e5750b | 216 | get_endp(s, pid, ep)->iso_started = 0; |
bb6d5498 HG |
217 | } |
218 | ||
c0e5750b | 219 | static void set_iso_started(USBHostDevice *s, int pid, int ep) |
bb6d5498 | 220 | { |
c0e5750b | 221 | struct endp_data *e = get_endp(s, pid, ep); |
e6a2f500 GH |
222 | |
223 | trace_usb_host_ep_start_iso(s->bus_num, s->addr, ep); | |
82887262 GH |
224 | if (!e->iso_started) { |
225 | e->iso_started = 1; | |
226 | e->inflight = 0; | |
227 | } | |
228 | } | |
229 | ||
c0e5750b | 230 | static int change_iso_inflight(USBHostDevice *s, int pid, int ep, int value) |
82887262 | 231 | { |
c0e5750b | 232 | struct endp_data *e = get_endp(s, pid, ep); |
82887262 GH |
233 | |
234 | e->inflight += value; | |
235 | return e->inflight; | |
bb6d5498 HG |
236 | } |
237 | ||
c0e5750b | 238 | static void set_iso_urb(USBHostDevice *s, int pid, int ep, AsyncURB *iso_urb) |
060dc841 | 239 | { |
c0e5750b | 240 | get_endp(s, pid, ep)->iso_urb = iso_urb; |
060dc841 HG |
241 | } |
242 | ||
c0e5750b | 243 | static AsyncURB *get_iso_urb(USBHostDevice *s, int pid, int ep) |
060dc841 | 244 | { |
c0e5750b | 245 | return get_endp(s, pid, ep)->iso_urb; |
060dc841 HG |
246 | } |
247 | ||
c0e5750b | 248 | static void set_iso_urb_idx(USBHostDevice *s, int pid, int ep, int i) |
060dc841 | 249 | { |
c0e5750b | 250 | get_endp(s, pid, ep)->iso_urb_idx = i; |
060dc841 HG |
251 | } |
252 | ||
c0e5750b | 253 | static int get_iso_urb_idx(USBHostDevice *s, int pid, int ep) |
060dc841 | 254 | { |
c0e5750b | 255 | return get_endp(s, pid, ep)->iso_urb_idx; |
060dc841 HG |
256 | } |
257 | ||
c0e5750b | 258 | static void set_iso_buffer_used(USBHostDevice *s, int pid, int ep, int i) |
bb6d5498 | 259 | { |
c0e5750b | 260 | get_endp(s, pid, ep)->iso_buffer_used = i; |
bb6d5498 HG |
261 | } |
262 | ||
c0e5750b | 263 | static int get_iso_buffer_used(USBHostDevice *s, int pid, int ep) |
bb6d5498 | 264 | { |
c0e5750b | 265 | return get_endp(s, pid, ep)->iso_buffer_used; |
bb6d5498 HG |
266 | } |
267 | ||
c0e5750b GH |
268 | static void set_max_packet_size(USBHostDevice *s, int pid, int ep, |
269 | uint8_t *descriptor) | |
6dfcdccb GH |
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 | } | |
c0e5750b | 280 | get_endp(s, pid, ep)->max_packet_size = size * microframes; |
6dfcdccb GH |
281 | } |
282 | ||
c0e5750b | 283 | static int get_max_packet_size(USBHostDevice *s, int pid, int ep) |
060dc841 | 284 | { |
c0e5750b | 285 | return get_endp(s, pid, ep)->max_packet_size; |
060dc841 HG |
286 | } |
287 | ||
2791104c | 288 | /* |
64838171 | 289 | * Async URB state. |
060dc841 | 290 | * We always allocate iso packet descriptors even for bulk transfers |
2791104c | 291 | * to simplify allocation and casts. |
64838171 | 292 | */ |
060dc841 | 293 | struct AsyncURB |
64838171 AL |
294 | { |
295 | struct usbdevfs_urb urb; | |
060dc841 | 296 | struct usbdevfs_iso_packet_desc isocpd[ISO_FRAME_DESC_PER_URB]; |
7a8fc83f GH |
297 | USBHostDevice *hdev; |
298 | QLIST_ENTRY(AsyncURB) next; | |
b9dc033c | 299 | |
060dc841 | 300 | /* For regular async urbs */ |
64838171 | 301 | USBPacket *packet; |
71138531 | 302 | int more; /* large transfer, more urbs follow */ |
060dc841 HG |
303 | |
304 | /* For buffered iso handling */ | |
305 | int iso_frame_idx; /* -1 means in flight */ | |
306 | }; | |
b9dc033c | 307 | |
7a8fc83f | 308 | static AsyncURB *async_alloc(USBHostDevice *s) |
b9dc033c | 309 | { |
7267c094 | 310 | AsyncURB *aurb = g_malloc0(sizeof(AsyncURB)); |
7a8fc83f GH |
311 | aurb->hdev = s; |
312 | QLIST_INSERT_HEAD(&s->aurbs, aurb, next); | |
313 | return aurb; | |
b9dc033c AZ |
314 | } |
315 | ||
64838171 | 316 | static void async_free(AsyncURB *aurb) |
b9dc033c | 317 | { |
7a8fc83f | 318 | QLIST_REMOVE(aurb, next); |
7267c094 | 319 | g_free(aurb); |
64838171 | 320 | } |
b9dc033c | 321 | |
41c01ee7 GH |
322 | static void do_disconnect(USBHostDevice *s) |
323 | { | |
41c01ee7 GH |
324 | usb_host_close(s); |
325 | usb_host_auto_check(NULL); | |
326 | } | |
327 | ||
64838171 AL |
328 | static void async_complete(void *opaque) |
329 | { | |
330 | USBHostDevice *s = opaque; | |
331 | AsyncURB *aurb; | |
82887262 | 332 | int urbs = 0; |
64838171 AL |
333 | |
334 | while (1) { | |
2791104c | 335 | USBPacket *p; |
b9dc033c | 336 | |
2791104c | 337 | int r = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &aurb); |
64838171 | 338 | if (r < 0) { |
2791104c | 339 | if (errno == EAGAIN) { |
82887262 GH |
340 | if (urbs > 2) { |
341 | fprintf(stderr, "husb: %d iso urbs finished at once\n", urbs); | |
342 | } | |
64838171 | 343 | return; |
2791104c | 344 | } |
40197c35 GH |
345 | if (errno == ENODEV) { |
346 | if (!s->closing) { | |
347 | trace_usb_host_disconnect(s->bus_num, s->addr); | |
348 | do_disconnect(s); | |
349 | } | |
64838171 AL |
350 | return; |
351 | } | |
352 | ||
e6a2f500 | 353 | perror("USBDEVFS_REAPURBNDELAY"); |
64838171 | 354 | return; |
b9dc033c | 355 | } |
64838171 | 356 | |
2791104c | 357 | DPRINTF("husb: async completed. aurb %p status %d alen %d\n", |
64838171 AL |
358 | aurb, aurb->urb.status, aurb->urb.actual_length); |
359 | ||
060dc841 HG |
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) { | |
82887262 | 363 | int inflight; |
c0e5750b GH |
364 | int pid = (aurb->urb.endpoint & USB_DIR_IN) ? |
365 | USB_TOKEN_IN : USB_TOKEN_OUT; | |
366 | int ep = aurb->urb.endpoint & 0xf; | |
060dc841 | 367 | if (aurb->urb.status == -EPIPE) { |
c0e5750b | 368 | set_halt(s, pid, ep); |
060dc841 HG |
369 | } |
370 | aurb->iso_frame_idx = 0; | |
82887262 | 371 | urbs++; |
c0e5750b GH |
372 | inflight = change_iso_inflight(s, pid, ep, -1); |
373 | if (inflight == 0 && is_iso_started(s, pid, ep)) { | |
82887262 GH |
374 | fprintf(stderr, "husb: out of buffers for iso stream\n"); |
375 | } | |
060dc841 HG |
376 | continue; |
377 | } | |
378 | ||
379 | p = aurb->packet; | |
e6a2f500 GH |
380 | trace_usb_host_urb_complete(s->bus_num, s->addr, aurb, aurb->urb.status, |
381 | aurb->urb.actual_length, aurb->more); | |
060dc841 | 382 | |
2791104c | 383 | if (p) { |
64838171 AL |
384 | switch (aurb->urb.status) { |
385 | case 0: | |
4f4321c1 | 386 | p->result += aurb->urb.actual_length; |
64838171 AL |
387 | break; |
388 | ||
389 | case -EPIPE: | |
c0e5750b | 390 | set_halt(s, p->pid, p->devep); |
4f4321c1 | 391 | p->result = USB_RET_STALL; |
2791104c | 392 | break; |
dcc7e25f | 393 | |
64838171 | 394 | default: |
4f4321c1 | 395 | p->result = USB_RET_NAK; |
64838171 AL |
396 | break; |
397 | } | |
398 | ||
50b7963e | 399 | if (aurb->urb.type == USBDEVFS_URB_TYPE_CONTROL) { |
e6a2f500 | 400 | trace_usb_host_req_complete(s->bus_num, s->addr, p->result); |
50b7963e | 401 | usb_generic_async_ctrl_complete(&s->dev, p); |
71138531 | 402 | } else if (!aurb->more) { |
e6a2f500 | 403 | trace_usb_host_req_complete(s->bus_num, s->addr, p->result); |
50b7963e HG |
404 | usb_packet_complete(&s->dev, p); |
405 | } | |
2791104c | 406 | } |
64838171 AL |
407 | |
408 | async_free(aurb); | |
b9dc033c | 409 | } |
b9dc033c AZ |
410 | } |
411 | ||
eb5e680a | 412 | static void usb_host_async_cancel(USBDevice *dev, USBPacket *p) |
b9dc033c | 413 | { |
eb5e680a | 414 | USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev); |
227ebeb5 | 415 | AsyncURB *aurb; |
b9dc033c | 416 | |
227ebeb5 GH |
417 | QLIST_FOREACH(aurb, &s->aurbs, next) { |
418 | if (p != aurb->packet) { | |
419 | continue; | |
420 | } | |
64838171 | 421 | |
227ebeb5 | 422 | DPRINTF("husb: async cancel: packet %p, aurb %p\n", p, aurb); |
b9dc033c | 423 | |
227ebeb5 GH |
424 | /* Mark it as dead (see async_complete above) */ |
425 | aurb->packet = NULL; | |
426 | ||
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 | } | |
b9dc033c | 431 | } |
b9dc033c AZ |
432 | } |
433 | ||
e6274727 GH |
434 | static int usb_host_claim_port(USBHostDevice *s) |
435 | { | |
436 | #ifdef USBDEVFS_CLAIM_PORT | |
437 | char *h, hub_name[64], line[1024]; | |
c75fead6 | 438 | int hub_addr, ret; |
e6274727 GH |
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) { | |
c75fead6 | 446 | s->hub_port = atoi(h+1); |
e6274727 GH |
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); | |
c75fead6 | 452 | s->hub_port = atoi(s->match.port); |
e6274727 GH |
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 | ||
c75fead6 | 473 | ret = ioctl(s->hub_fd, USBDEVFS_CLAIM_PORT, &s->hub_port); |
e6274727 GH |
474 | if (ret < 0) { |
475 | close(s->hub_fd); | |
476 | s->hub_fd = -1; | |
477 | return -1; | |
478 | } | |
479 | ||
c75fead6 | 480 | trace_usb_host_claim_port(s->match.bus_num, hub_addr, s->hub_port); |
e6274727 GH |
481 | return 0; |
482 | #else | |
483 | return -1; | |
484 | #endif | |
485 | } | |
486 | ||
c75fead6 GH |
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 | ||
e6274727 GH |
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 | ||
0fcc3bfc GH |
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 | ||
446ab128 | 540 | static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration) |
b9dc033c | 541 | { |
41c01ee7 | 542 | const char *op = NULL; |
b9dc033c | 543 | int dev_descr_len, config_descr_len; |
d4c4e6fd | 544 | int interface, nb_interfaces; |
b9dc033c AZ |
545 | int ret, i; |
546 | ||
eb7700bb GH |
547 | if (configuration == 0) { /* address state - ignore */ |
548 | dev->ninterfaces = 0; | |
549 | dev->configuration = 0; | |
b9dc033c | 550 | return 1; |
eb7700bb | 551 | } |
b9dc033c | 552 | |
d0f2c4c6 | 553 | DPRINTF("husb: claiming interfaces. config %d\n", configuration); |
446ab128 | 554 | |
b9dc033c AZ |
555 | i = 0; |
556 | dev_descr_len = dev->descr[0]; | |
2791104c | 557 | if (dev_descr_len > dev->descr_len) { |
61c1117f HG |
558 | fprintf(stderr, "husb: update iface failed. descr too short\n"); |
559 | return 0; | |
2791104c | 560 | } |
b9dc033c AZ |
561 | |
562 | i += dev_descr_len; | |
563 | while (i < dev->descr_len) { | |
2791104c DA |
564 | DPRINTF("husb: i is %d, descr_len is %d, dl %d, dt %d\n", |
565 | i, dev->descr_len, | |
b9dc033c | 566 | dev->descr[i], dev->descr[i+1]); |
64838171 | 567 | |
b9dc033c AZ |
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 | ||
e6a2f500 | 574 | DPRINTF("husb: config #%d need %d\n", dev->descr[i + 5], configuration); |
1f3870ab | 575 | |
eb7700bb | 576 | if (configuration == dev->descr[i + 5]) { |
446ab128 | 577 | configuration = dev->descr[i + 5]; |
b9dc033c | 578 | break; |
446ab128 | 579 | } |
b9dc033c AZ |
580 | |
581 | i += config_descr_len; | |
582 | } | |
583 | ||
584 | if (i >= dev->descr_len) { | |
2791104c DA |
585 | fprintf(stderr, |
586 | "husb: update iface failed. no matching configuration\n"); | |
61c1117f | 587 | return 0; |
b9dc033c AZ |
588 | } |
589 | nb_interfaces = dev->descr[i + 4]; | |
590 | ||
e6274727 GH |
591 | if (usb_host_disconnect_ifaces(dev, nb_interfaces) < 0) { |
592 | goto fail; | |
b9dc033c | 593 | } |
b9dc033c AZ |
594 | |
595 | /* XXX: only grab if all interfaces are free */ | |
596 | for (interface = 0; interface < nb_interfaces; interface++) { | |
41c01ee7 | 597 | op = "USBDEVFS_CLAIMINTERFACE"; |
b9dc033c AZ |
598 | ret = ioctl(dev->fd, USBDEVFS_CLAIMINTERFACE, &interface); |
599 | if (ret < 0) { | |
41c01ee7 | 600 | goto fail; |
b9dc033c AZ |
601 | } |
602 | } | |
603 | ||
e6a2f500 GH |
604 | trace_usb_host_claim_interfaces(dev->bus_num, dev->addr, |
605 | nb_interfaces, configuration); | |
b9dc033c | 606 | |
446ab128 AL |
607 | dev->ninterfaces = nb_interfaces; |
608 | dev->configuration = configuration; | |
609 | return 1; | |
41c01ee7 GH |
610 | |
611 | fail: | |
612 | if (errno == ENODEV) { | |
613 | do_disconnect(dev); | |
614 | } | |
615 | perror(op); | |
616 | return 0; | |
446ab128 AL |
617 | } |
618 | ||
619 | static int usb_host_release_interfaces(USBHostDevice *s) | |
620 | { | |
621 | int ret, i; | |
622 | ||
e6a2f500 | 623 | trace_usb_host_release_interfaces(s->bus_num, s->addr); |
446ab128 AL |
624 | |
625 | for (i = 0; i < s->ninterfaces; i++) { | |
626 | ret = ioctl(s->fd, USBDEVFS_RELEASEINTERFACE, &i); | |
627 | if (ret < 0) { | |
e6a2f500 | 628 | perror("USBDEVFS_RELEASEINTERFACE"); |
446ab128 AL |
629 | return 0; |
630 | } | |
631 | } | |
b9dc033c AZ |
632 | return 1; |
633 | } | |
634 | ||
059809e4 | 635 | static void usb_host_handle_reset(USBDevice *dev) |
bb36d470 | 636 | { |
26a9e82a | 637 | USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev); |
64838171 | 638 | |
e6a2f500 | 639 | trace_usb_host_reset(s->bus_num, s->addr); |
64838171 | 640 | |
c7662daa | 641 | usb_host_do_reset(s);; |
446ab128 | 642 | |
eb7700bb | 643 | usb_host_claim_interfaces(s, 0); |
9b87e19b | 644 | usb_linux_update_endp_table(s); |
5fafdf24 | 645 | } |
bb36d470 | 646 | |
059809e4 FB |
647 | static void usb_host_handle_destroy(USBDevice *dev) |
648 | { | |
649 | USBHostDevice *s = (USBHostDevice *)dev; | |
650 | ||
c75fead6 | 651 | usb_host_release_port(s); |
26a9e82a GH |
652 | usb_host_close(s); |
653 | QTAILQ_REMOVE(&hostdevs, s, next); | |
b373a63a | 654 | qemu_remove_exit_notifier(&s->exit); |
059809e4 FB |
655 | } |
656 | ||
060dc841 HG |
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. */ | |
c0e5750b | 660 | static AsyncURB *usb_host_alloc_iso(USBHostDevice *s, int pid, uint8_t ep) |
060dc841 HG |
661 | { |
662 | AsyncURB *aurb; | |
c0e5750b | 663 | int i, j, len = get_max_packet_size(s, pid, ep); |
060dc841 | 664 | |
7267c094 | 665 | aurb = g_malloc0(s->iso_urb_count * sizeof(*aurb)); |
b81bcd8a | 666 | for (i = 0; i < s->iso_urb_count; i++) { |
060dc841 HG |
667 | aurb[i].urb.endpoint = ep; |
668 | aurb[i].urb.buffer_length = ISO_FRAME_DESC_PER_URB * len; | |
7267c094 | 669 | aurb[i].urb.buffer = g_malloc(aurb[i].urb.buffer_length); |
060dc841 HG |
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; | |
c0e5750b | 675 | if (pid == USB_TOKEN_IN) { |
060dc841 HG |
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 | } | |
c0e5750b | 681 | set_iso_urb(s, pid, ep, aurb); |
060dc841 HG |
682 | |
683 | return aurb; | |
684 | } | |
685 | ||
c0e5750b | 686 | static void usb_host_stop_n_free_iso(USBHostDevice *s, int pid, uint8_t ep) |
060dc841 HG |
687 | { |
688 | AsyncURB *aurb; | |
689 | int i, ret, killed = 0, free = 1; | |
690 | ||
c0e5750b | 691 | aurb = get_iso_urb(s, pid, ep); |
060dc841 HG |
692 | if (!aurb) { |
693 | return; | |
694 | } | |
695 | ||
b81bcd8a | 696 | for (i = 0; i < s->iso_urb_count; i++) { |
060dc841 HG |
697 | /* in flight? */ |
698 | if (aurb[i].iso_frame_idx == -1) { | |
699 | ret = ioctl(s->fd, USBDEVFS_DISCARDURB, &aurb[i]); | |
700 | if (ret < 0) { | |
e6a2f500 | 701 | perror("USBDEVFS_DISCARDURB"); |
060dc841 HG |
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 | ||
b81bcd8a | 714 | for (i = 0; i < s->iso_urb_count; i++) { |
7267c094 | 715 | g_free(aurb[i].urb.buffer); |
060dc841 HG |
716 | } |
717 | ||
718 | if (free) | |
7267c094 | 719 | g_free(aurb); |
060dc841 HG |
720 | else |
721 | printf("husb: leaking iso urbs because of discard failure\n"); | |
c0e5750b GH |
722 | set_iso_urb(s, pid, ep, NULL); |
723 | set_iso_urb_idx(s, pid, ep, 0); | |
724 | clear_iso_started(s, pid, ep); | |
060dc841 HG |
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 | ||
bb6d5498 | 737 | static int usb_host_handle_iso_data(USBHostDevice *s, USBPacket *p, int in) |
060dc841 HG |
738 | { |
739 | AsyncURB *aurb; | |
bb6d5498 | 740 | int i, j, ret, max_packet_size, offset, len = 0; |
4f4321c1 | 741 | uint8_t *buf; |
975f2998 | 742 | |
c0e5750b | 743 | max_packet_size = get_max_packet_size(s, p->pid, p->devep); |
975f2998 HG |
744 | if (max_packet_size == 0) |
745 | return USB_RET_NAK; | |
060dc841 | 746 | |
c0e5750b | 747 | aurb = get_iso_urb(s, p->pid, p->devep); |
060dc841 | 748 | if (!aurb) { |
c0e5750b | 749 | aurb = usb_host_alloc_iso(s, p->pid, p->devep); |
060dc841 HG |
750 | } |
751 | ||
c0e5750b | 752 | i = get_iso_urb_idx(s, p->pid, p->devep); |
060dc841 HG |
753 | j = aurb[i].iso_frame_idx; |
754 | if (j >= 0 && j < ISO_FRAME_DESC_PER_URB) { | |
bb6d5498 HG |
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 */ | |
4f4321c1 GH |
766 | } else if (aurb[i].urb.iso_frame_desc[j].actual_length |
767 | > p->iov.size) { | |
bb6d5498 HG |
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; | |
4f4321c1 GH |
773 | buf = aurb[i].urb.buffer + |
774 | j * aurb[i].urb.iso_frame_desc[0].length; | |
775 | usb_packet_copy(p, buf, len); | |
bb6d5498 | 776 | } |
060dc841 | 777 | } else { |
4f4321c1 | 778 | len = p->iov.size; |
c0e5750b | 779 | offset = (j == 0) ? 0 : get_iso_buffer_used(s, p->pid, p->devep); |
bb6d5498 HG |
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 */ | |
4f4321c1 | 788 | usb_packet_copy(p, aurb[i].urb.buffer + offset, len); |
bb6d5498 HG |
789 | aurb[i].urb.iso_frame_desc[j].length = len; |
790 | offset += len; | |
c0e5750b | 791 | set_iso_buffer_used(s, p->pid, p->devep, offset); |
bb6d5498 HG |
792 | |
793 | /* Start the stream once we have buffered enough data */ | |
c0e5750b GH |
794 | if (!is_iso_started(s, p->pid, p->devep) && i == 1 && j == 8) { |
795 | set_iso_started(s, p->pid, p->devep); | |
bb6d5498 | 796 | } |
060dc841 HG |
797 | } |
798 | aurb[i].iso_frame_idx++; | |
799 | if (aurb[i].iso_frame_idx == ISO_FRAME_DESC_PER_URB) { | |
b81bcd8a | 800 | i = (i + 1) % s->iso_urb_count; |
c0e5750b | 801 | set_iso_urb_idx(s, p->pid, p->devep, i); |
060dc841 | 802 | } |
bb6d5498 HG |
803 | } else { |
804 | if (in) { | |
c0e5750b | 805 | set_iso_started(s, p->pid, p->devep); |
bb6d5498 HG |
806 | } else { |
807 | DPRINTF("hubs: iso out error no free buffer, dropping packet\n"); | |
808 | } | |
060dc841 HG |
809 | } |
810 | ||
c0e5750b | 811 | if (is_iso_started(s, p->pid, p->devep)) { |
bb6d5498 | 812 | /* (Re)-submit all fully consumed / filled urbs */ |
b81bcd8a | 813 | for (i = 0; i < s->iso_urb_count; i++) { |
bb6d5498 HG |
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) { | |
e6a2f500 | 817 | perror("USBDEVFS_SUBMITURB"); |
bb6d5498 HG |
818 | if (!in || len == 0) { |
819 | switch(errno) { | |
820 | case ETIMEDOUT: | |
821 | len = USB_RET_NAK; | |
0225e254 | 822 | break; |
bb6d5498 HG |
823 | case EPIPE: |
824 | default: | |
825 | len = USB_RET_STALL; | |
826 | } | |
060dc841 | 827 | } |
bb6d5498 | 828 | break; |
060dc841 | 829 | } |
bb6d5498 | 830 | aurb[i].iso_frame_idx = -1; |
c0e5750b | 831 | change_iso_inflight(s, p->pid, p->devep, 1); |
060dc841 | 832 | } |
060dc841 HG |
833 | } |
834 | } | |
835 | ||
836 | return len; | |
837 | } | |
838 | ||
50b7963e | 839 | static int usb_host_handle_data(USBDevice *dev, USBPacket *p) |
bb36d470 | 840 | { |
50b7963e | 841 | USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev); |
64838171 | 842 | struct usbdevfs_urb *urb; |
446ab128 | 843 | AsyncURB *aurb; |
b621bab4 | 844 | int ret, rem, prem, v; |
71138531 | 845 | uint8_t *pbuf; |
060dc841 | 846 | uint8_t ep; |
b9dc033c | 847 | |
e6a2f500 GH |
848 | trace_usb_host_req_data(s->bus_num, s->addr, |
849 | p->pid == USB_TOKEN_IN, | |
850 | p->devep, p->iov.size); | |
851 | ||
c0e5750b | 852 | if (!is_valid(s, p->pid, p->devep)) { |
e6a2f500 | 853 | trace_usb_host_req_complete(s->bus_num, s->addr, USB_RET_NAK); |
a0b5fece HG |
854 | return USB_RET_NAK; |
855 | } | |
856 | ||
2791104c | 857 | if (p->pid == USB_TOKEN_IN) { |
060dc841 | 858 | ep = p->devep | 0x80; |
2791104c | 859 | } else { |
060dc841 | 860 | ep = p->devep; |
2791104c | 861 | } |
64838171 | 862 | |
c0e5750b | 863 | if (is_halted(s, p->pid, p->devep)) { |
9b87e19b GH |
864 | unsigned int arg = ep; |
865 | ret = ioctl(s->fd, USBDEVFS_CLEAR_HALT, &arg); | |
64838171 | 866 | if (ret < 0) { |
e6a2f500 GH |
867 | perror("USBDEVFS_CLEAR_HALT"); |
868 | trace_usb_host_req_complete(s->bus_num, s->addr, USB_RET_NAK); | |
bb36d470 | 869 | return USB_RET_NAK; |
bb36d470 | 870 | } |
c0e5750b | 871 | clear_halt(s, p->pid, p->devep); |
4d043a09 AZ |
872 | } |
873 | ||
c0e5750b | 874 | if (is_isoc(s, p->pid, p->devep)) { |
bb6d5498 HG |
875 | return usb_host_handle_iso_data(s, p, p->pid == USB_TOKEN_IN); |
876 | } | |
060dc841 | 877 | |
b621bab4 GH |
878 | v = 0; |
879 | prem = p->iov.iov[v].iov_len; | |
880 | pbuf = p->iov.iov[v].iov_base; | |
881 | rem = p->iov.size; | |
71138531 | 882 | while (rem) { |
b621bab4 GH |
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 | } | |
71138531 GH |
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; | |
b621bab4 | 898 | urb->buffer_length = prem; |
71138531 | 899 | |
b621bab4 | 900 | if (urb->buffer_length > MAX_USBFS_BUFFER_SIZE) { |
71138531 | 901 | urb->buffer_length = MAX_USBFS_BUFFER_SIZE; |
71138531 GH |
902 | } |
903 | pbuf += urb->buffer_length; | |
b621bab4 | 904 | prem -= urb->buffer_length; |
71138531 | 905 | rem -= urb->buffer_length; |
b621bab4 GH |
906 | if (rem) { |
907 | aurb->more = 1; | |
908 | } | |
b9dc033c | 909 | |
e6a2f500 GH |
910 | trace_usb_host_urb_submit(s->bus_num, s->addr, aurb, |
911 | urb->buffer_length, aurb->more); | |
71138531 | 912 | ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb); |
b9dc033c | 913 | |
71138531 GH |
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); | |
b9dc033c | 916 | |
71138531 | 917 | if (ret < 0) { |
e6a2f500 | 918 | perror("USBDEVFS_SUBMITURB"); |
71138531 | 919 | async_free(aurb); |
b9dc033c | 920 | |
71138531 GH |
921 | switch(errno) { |
922 | case ETIMEDOUT: | |
e6a2f500 | 923 | trace_usb_host_req_complete(s->bus_num, s->addr, USB_RET_NAK); |
71138531 GH |
924 | return USB_RET_NAK; |
925 | case EPIPE: | |
926 | default: | |
e6a2f500 | 927 | trace_usb_host_req_complete(s->bus_num, s->addr, USB_RET_STALL); |
71138531 GH |
928 | return USB_RET_STALL; |
929 | } | |
b9dc033c AZ |
930 | } |
931 | } | |
64838171 | 932 | |
b9dc033c | 933 | return USB_RET_ASYNC; |
b9dc033c AZ |
934 | } |
935 | ||
446ab128 AL |
936 | static int ctrl_error(void) |
937 | { | |
2791104c | 938 | if (errno == ETIMEDOUT) { |
446ab128 | 939 | return USB_RET_NAK; |
2791104c | 940 | } else { |
446ab128 | 941 | return USB_RET_STALL; |
2791104c | 942 | } |
446ab128 AL |
943 | } |
944 | ||
945 | static int usb_host_set_address(USBHostDevice *s, int addr) | |
946 | { | |
e6a2f500 | 947 | trace_usb_host_set_address(s->bus_num, s->addr, addr); |
446ab128 AL |
948 | s->dev.addr = addr; |
949 | return 0; | |
950 | } | |
951 | ||
952 | static int usb_host_set_config(USBHostDevice *s, int config) | |
953 | { | |
0fcc3bfc GH |
954 | int ret, first = 1; |
955 | ||
e6a2f500 GH |
956 | trace_usb_host_set_config(s->bus_num, s->addr, config); |
957 | ||
446ab128 AL |
958 | usb_host_release_interfaces(s); |
959 | ||
0fcc3bfc GH |
960 | again: |
961 | ret = ioctl(s->fd, USBDEVFS_SETCONFIGURATION, &config); | |
2791104c | 962 | |
d0f2c4c6 | 963 | DPRINTF("husb: ctrl set config %d ret %d errno %d\n", config, ret, errno); |
2791104c | 964 | |
0fcc3bfc GH |
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 | ||
2791104c | 976 | if (ret < 0) { |
446ab128 | 977 | return ctrl_error(); |
2791104c | 978 | } |
446ab128 | 979 | usb_host_claim_interfaces(s, config); |
eb7700bb | 980 | usb_linux_update_endp_table(s); |
446ab128 AL |
981 | return 0; |
982 | } | |
983 | ||
984 | static int usb_host_set_interface(USBHostDevice *s, int iface, int alt) | |
985 | { | |
986 | struct usbdevfs_setinterface si; | |
060dc841 HG |
987 | int i, ret; |
988 | ||
e6a2f500 GH |
989 | trace_usb_host_set_interface(s->bus_num, s->addr, iface, alt); |
990 | ||
3a4854b3 | 991 | for (i = 1; i <= MAX_ENDPOINTS; i++) { |
c0e5750b GH |
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); | |
060dc841 HG |
997 | } |
998 | } | |
446ab128 AL |
999 | |
1000 | si.interface = iface; | |
1001 | si.altsetting = alt; | |
1002 | ret = ioctl(s->fd, USBDEVFS_SETINTERFACE, &si); | |
446ab128 | 1003 | |
2791104c DA |
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 | } | |
446ab128 AL |
1010 | usb_linux_update_endp_table(s); |
1011 | return 0; | |
1012 | } | |
1013 | ||
50b7963e HG |
1014 | static int usb_host_handle_control(USBDevice *dev, USBPacket *p, |
1015 | int request, int value, int index, int length, uint8_t *data) | |
446ab128 | 1016 | { |
50b7963e | 1017 | USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev); |
446ab128 AL |
1018 | struct usbdevfs_urb *urb; |
1019 | AsyncURB *aurb; | |
50b7963e | 1020 | int ret; |
446ab128 | 1021 | |
2791104c | 1022 | /* |
446ab128 AL |
1023 | * Process certain standard device requests. |
1024 | * These are infrequent and are processed synchronously. | |
1025 | */ | |
446ab128 | 1026 | |
50b7963e | 1027 | /* Note request is (bRequestType << 8) | bRequest */ |
e6a2f500 | 1028 | trace_usb_host_req_control(s->bus_num, s->addr, request, value, index); |
446ab128 | 1029 | |
50b7963e HG |
1030 | switch (request) { |
1031 | case DeviceOutRequest | USB_REQ_SET_ADDRESS: | |
1032 | return usb_host_set_address(s, value); | |
446ab128 | 1033 | |
50b7963e HG |
1034 | case DeviceOutRequest | USB_REQ_SET_CONFIGURATION: |
1035 | return usb_host_set_config(s, value & 0xff); | |
446ab128 | 1036 | |
50b7963e | 1037 | case InterfaceOutRequest | USB_REQ_SET_INTERFACE: |
446ab128 | 1038 | return usb_host_set_interface(s, index, value); |
2791104c | 1039 | } |
446ab128 AL |
1040 | |
1041 | /* The rest are asynchronous */ | |
1042 | ||
50b7963e HG |
1043 | if (length > sizeof(dev->data_buf)) { |
1044 | fprintf(stderr, "husb: ctrl buffer too small (%d > %zu)\n", | |
1045 | length, sizeof(dev->data_buf)); | |
b2e3b6e9 | 1046 | return USB_RET_STALL; |
c4c0e236 JP |
1047 | } |
1048 | ||
7a8fc83f | 1049 | aurb = async_alloc(s); |
446ab128 AL |
1050 | aurb->packet = p; |
1051 | ||
2791104c | 1052 | /* |
446ab128 AL |
1053 | * Setup ctrl transfer. |
1054 | * | |
a0102082 | 1055 | * s->ctrl is laid out such that data buffer immediately follows |
446ab128 | 1056 | * 'req' struct which is exactly what usbdevfs expects. |
2791104c | 1057 | */ |
446ab128 AL |
1058 | urb = &aurb->urb; |
1059 | ||
1060 | urb->type = USBDEVFS_URB_TYPE_CONTROL; | |
1061 | urb->endpoint = p->devep; | |
1062 | ||
50b7963e HG |
1063 | urb->buffer = &dev->setup_buf; |
1064 | urb->buffer_length = length + 8; | |
446ab128 AL |
1065 | |
1066 | urb->usercontext = s; | |
1067 | ||
e6a2f500 GH |
1068 | trace_usb_host_urb_submit(s->bus_num, s->addr, aurb, |
1069 | urb->buffer_length, aurb->more); | |
446ab128 AL |
1070 | ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb); |
1071 | ||
d0f2c4c6 | 1072 | DPRINTF("husb: submit ctrl. len %u aurb %p\n", urb->buffer_length, aurb); |
446ab128 AL |
1073 | |
1074 | if (ret < 0) { | |
d0f2c4c6 | 1075 | DPRINTF("husb: submit failed. errno %d\n", errno); |
446ab128 AL |
1076 | async_free(aurb); |
1077 | ||
1078 | switch(errno) { | |
1079 | case ETIMEDOUT: | |
1080 | return USB_RET_NAK; | |
1081 | case EPIPE: | |
1082 | default: | |
1083 | return USB_RET_STALL; | |
1084 | } | |
1085 | } | |
1086 | ||
446ab128 AL |
1087 | return USB_RET_ASYNC; |
1088 | } | |
1089 | ||
ed3a328d HG |
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 | ||
c43831fb HG |
1097 | if (usb_fs_type == USB_FS_SYS) { |
1098 | char device_name[64], line[1024]; | |
1099 | int alt_setting; | |
1100 | ||
5557d820 | 1101 | sprintf(device_name, "%d-%s:%d.%d", s->bus_num, s->port, |
c43831fb HG |
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: | |
ed3a328d HG |
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 | ||
71d71bbd HG |
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; | |
eb7700bb | 1135 | uint8_t devep, type, alt_interface; |
c0e5750b GH |
1136 | int interface, length, i, ep, pid; |
1137 | struct endp_data *epd; | |
71d71bbd | 1138 | |
c0e5750b GH |
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 | } | |
a0b5fece | 1143 | |
eb7700bb GH |
1144 | if (s->configuration == 0) { |
1145 | /* not configured yet -- leave all endpoints disabled */ | |
1146 | return 0; | |
1147 | } | |
71d71bbd | 1148 | |
b9dc033c AZ |
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 | ||
b9dc033c | 1155 | while (i < length) { |
7279a85f CBB |
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 | ||
b9dc033c AZ |
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]; | |
eb7700bb GH |
1175 | alt_interface = usb_linux_get_alt_setting(s, s->configuration, |
1176 | interface); | |
b9dc033c AZ |
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 */ | |
2791104c | 1186 | while (i < length && descriptors[i +1] != USB_DT_ENDPOINT) { |
b9dc033c | 1187 | i += descriptors[i]; |
2791104c | 1188 | } |
b9dc033c AZ |
1189 | |
1190 | if (i >= length) | |
1191 | break; | |
1192 | ||
1193 | while (i < length) { | |
2791104c | 1194 | if (descriptors[i + 1] != USB_DT_ENDPOINT) { |
b9dc033c | 1195 | break; |
2791104c | 1196 | } |
b9dc033c AZ |
1197 | |
1198 | devep = descriptors[i + 2]; | |
c0e5750b GH |
1199 | pid = (devep & USB_DIR_IN) ? USB_TOKEN_IN : USB_TOKEN_OUT; |
1200 | ep = devep & 0xf; | |
1201 | if (ep == 0) { | |
130314f8 HG |
1202 | fprintf(stderr, "usb-linux: invalid ep descriptor, ep == 0\n"); |
1203 | return 1; | |
1204 | } | |
1205 | ||
b9dc033c AZ |
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; | |
c0e5750b | 1212 | set_max_packet_size(s, pid, ep, descriptors + i); |
b9dc033c AZ |
1213 | break; |
1214 | case 0x02: | |
1215 | type = USBDEVFS_URB_TYPE_BULK; | |
1216 | break; | |
1217 | case 0x03: | |
1218 | type = USBDEVFS_URB_TYPE_INTERRUPT; | |
1219 | break; | |
ddbda432 AL |
1220 | default: |
1221 | DPRINTF("usb_host: malformed endpoint type\n"); | |
1222 | type = USBDEVFS_URB_TYPE_BULK; | |
b9dc033c | 1223 | } |
c0e5750b GH |
1224 | epd = get_endp(s, pid, ep); |
1225 | assert(epd->type == INVALID_EP_TYPE); | |
1226 | epd->type = type; | |
1227 | epd->halted = 0; | |
b9dc033c AZ |
1228 | |
1229 | i += descriptors[i]; | |
1230 | } | |
1231 | } | |
1232 | return 0; | |
1233 | } | |
1234 | ||
e4b17767 HG |
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 | ||
26a9e82a | 1271 | static int usb_host_open(USBHostDevice *dev, int bus_num, |
ba9acab9 GH |
1272 | int addr, const char *port, |
1273 | const char *prod_name, int speed) | |
bb36d470 | 1274 | { |
b9dc033c | 1275 | int fd = -1, ret; |
a594cfbf | 1276 | char buf[1024]; |
1f3870ab | 1277 | |
e6a2f500 GH |
1278 | trace_usb_host_open_started(bus_num, addr); |
1279 | ||
2791104c | 1280 | if (dev->fd != -1) { |
26a9e82a | 1281 | goto fail; |
2791104c | 1282 | } |
3b46e624 | 1283 | |
0f431527 AL |
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, | |
a594cfbf | 1289 | bus_num, addr); |
b9dc033c | 1290 | fd = open(buf, O_RDWR | O_NONBLOCK); |
bb36d470 | 1291 | if (fd < 0) { |
a594cfbf | 1292 | perror(buf); |
1f3870ab | 1293 | goto fail; |
bb36d470 | 1294 | } |
d0f2c4c6 | 1295 | DPRINTF("husb: opened %s\n", buf); |
bb36d470 | 1296 | |
806b6024 GH |
1297 | dev->bus_num = bus_num; |
1298 | dev->addr = addr; | |
5557d820 | 1299 | strcpy(dev->port, port); |
22f84e73 | 1300 | dev->fd = fd; |
806b6024 | 1301 | |
b9dc033c AZ |
1302 | /* read the device description */ |
1303 | dev->descr_len = read(fd, dev->descr, sizeof(dev->descr)); | |
1304 | if (dev->descr_len <= 0) { | |
64838171 | 1305 | perror("husb: reading device data failed"); |
bb36d470 FB |
1306 | goto fail; |
1307 | } | |
3b46e624 | 1308 | |
b9dc033c | 1309 | #ifdef DEBUG |
868bfe2b | 1310 | { |
b9dc033c AZ |
1311 | int x; |
1312 | printf("=== begin dumping device descriptor data ===\n"); | |
2791104c | 1313 | for (x = 0; x < dev->descr_len; x++) { |
b9dc033c | 1314 | printf("%02x ", dev->descr[x]); |
2791104c | 1315 | } |
b9dc033c | 1316 | printf("\n=== end dumping device descriptor data ===\n"); |
bb36d470 | 1317 | } |
a594cfbf FB |
1318 | #endif |
1319 | ||
b9dc033c | 1320 | |
eb7700bb GH |
1321 | /* start unconfigured -- we'll wait for the guest to set a configuration */ |
1322 | if (!usb_host_claim_interfaces(dev, 0)) { | |
b9dc033c | 1323 | goto fail; |
2791104c | 1324 | } |
bb36d470 | 1325 | |
b9dc033c | 1326 | ret = usb_linux_update_endp_table(dev); |
2791104c | 1327 | if (ret) { |
bb36d470 | 1328 | goto fail; |
2791104c | 1329 | } |
b9dc033c | 1330 | |
3991c35e HG |
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 | } | |
2791104c | 1345 | } |
3991c35e | 1346 | dev->dev.speed = speed; |
ba3f9bfb | 1347 | dev->dev.speedmask = (1 << speed); |
e4b17767 HG |
1348 | if (dev->dev.speed == USB_SPEED_HIGH && usb_linux_full_speed_compat(dev)) { |
1349 | dev->dev.speedmask |= USB_SPEED_MASK_FULL; | |
1350 | } | |
3991c35e | 1351 | |
e6a2f500 | 1352 | trace_usb_host_open_success(bus_num, addr); |
bb36d470 | 1353 | |
2791104c | 1354 | if (!prod_name || prod_name[0] == '\0') { |
0fe6d12e | 1355 | snprintf(dev->dev.product_desc, sizeof(dev->dev.product_desc), |
4b096fc9 | 1356 | "host:%d.%d", bus_num, addr); |
2791104c | 1357 | } else { |
0fe6d12e | 1358 | pstrcpy(dev->dev.product_desc, sizeof(dev->dev.product_desc), |
4b096fc9 | 1359 | prod_name); |
2791104c | 1360 | } |
1f6e24e7 | 1361 | |
fa19bf83 HG |
1362 | ret = usb_device_attach(&dev->dev); |
1363 | if (ret) { | |
1364 | goto fail; | |
1365 | } | |
1366 | ||
64838171 AL |
1367 | /* USB devio uses 'write' flag to check for async completions */ |
1368 | qemu_set_fd_handler(dev->fd, NULL, async_complete, dev); | |
1f3870ab | 1369 | |
26a9e82a | 1370 | return 0; |
4b096fc9 | 1371 | |
b9dc033c | 1372 | fail: |
e6a2f500 | 1373 | trace_usb_host_open_failure(bus_num, addr); |
1f45a81b GH |
1374 | if (dev->fd != -1) { |
1375 | close(dev->fd); | |
1376 | dev->fd = -1; | |
2791104c | 1377 | } |
26a9e82a GH |
1378 | return -1; |
1379 | } | |
1380 | ||
1381 | static int usb_host_close(USBHostDevice *dev) | |
1382 | { | |
060dc841 HG |
1383 | int i; |
1384 | ||
39fba3ad | 1385 | if (dev->fd == -1) { |
26a9e82a | 1386 | return -1; |
2791104c | 1387 | } |
26a9e82a | 1388 | |
e6a2f500 GH |
1389 | trace_usb_host_close(dev->bus_num, dev->addr); |
1390 | ||
26a9e82a GH |
1391 | qemu_set_fd_handler(dev->fd, NULL, NULL, NULL); |
1392 | dev->closing = 1; | |
3a4854b3 | 1393 | for (i = 1; i <= MAX_ENDPOINTS; i++) { |
c0e5750b GH |
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); | |
060dc841 HG |
1399 | } |
1400 | } | |
26a9e82a GH |
1401 | async_complete(dev); |
1402 | dev->closing = 0; | |
39fba3ad GH |
1403 | if (dev->dev.attached) { |
1404 | usb_device_detach(&dev->dev); | |
1405 | } | |
c7662daa | 1406 | usb_host_do_reset(dev); |
26a9e82a GH |
1407 | close(dev->fd); |
1408 | dev->fd = -1; | |
1409 | return 0; | |
1410 | } | |
1411 | ||
9e8dd451 | 1412 | static void usb_host_exit_notifier(struct Notifier *n, void *data) |
b373a63a SH |
1413 | { |
1414 | USBHostDevice *s = container_of(n, USBHostDevice, exit); | |
1415 | ||
c75fead6 | 1416 | usb_host_release_port(s); |
b373a63a | 1417 | if (s->fd != -1) { |
c7662daa | 1418 | usb_host_do_reset(s);; |
b373a63a SH |
1419 | } |
1420 | } | |
1421 | ||
26a9e82a GH |
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; | |
9516bb47 GH |
1428 | s->hub_fd = -1; |
1429 | ||
26a9e82a | 1430 | QTAILQ_INSERT_TAIL(&hostdevs, s, next); |
b373a63a SH |
1431 | s->exit.notify = usb_host_exit_notifier; |
1432 | qemu_add_exit_notifier(&s->exit); | |
26a9e82a | 1433 | usb_host_auto_check(NULL); |
9516bb47 | 1434 | |
9516bb47 | 1435 | if (s->match.bus_num != 0 && s->match.port != NULL) { |
e6274727 | 1436 | usb_host_claim_port(s); |
9516bb47 | 1437 | } |
26a9e82a | 1438 | return 0; |
a594cfbf | 1439 | } |
bb36d470 | 1440 | |
d6791578 GH |
1441 | static const VMStateDescription vmstate_usb_host = { |
1442 | .name = "usb-host", | |
1443 | .unmigratable = 1, | |
1444 | }; | |
1445 | ||
806b6024 | 1446 | static struct USBDeviceInfo usb_host_dev_info = { |
06384698 | 1447 | .product_desc = "USB Host Device", |
556cd098 | 1448 | .qdev.name = "usb-host", |
806b6024 | 1449 | .qdev.size = sizeof(USBHostDevice), |
d6791578 | 1450 | .qdev.vmsd = &vmstate_usb_host, |
806b6024 | 1451 | .init = usb_host_initfn, |
50b7963e | 1452 | .handle_packet = usb_generic_handle_packet, |
eb5e680a | 1453 | .cancel_packet = usb_host_async_cancel, |
50b7963e HG |
1454 | .handle_data = usb_host_handle_data, |
1455 | .handle_control = usb_host_handle_control, | |
806b6024 | 1456 | .handle_reset = usb_host_handle_reset, |
806b6024 | 1457 | .handle_destroy = usb_host_handle_destroy, |
26a9e82a GH |
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), | |
9056a297 | 1463 | DEFINE_PROP_STRING("hostport", USBHostDevice, match.port), |
26a9e82a GH |
1464 | DEFINE_PROP_HEX32("vendorid", USBHostDevice, match.vendor_id, 0), |
1465 | DEFINE_PROP_HEX32("productid", USBHostDevice, match.product_id, 0), | |
b81bcd8a | 1466 | DEFINE_PROP_UINT32("isobufs", USBHostDevice, iso_urb_count, 4), |
26a9e82a GH |
1467 | DEFINE_PROP_END_OF_LIST(), |
1468 | }, | |
806b6024 GH |
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) | |
1476 | ||
4b096fc9 AL |
1477 | USBDevice *usb_host_device_open(const char *devname) |
1478 | { | |
0745eb1e | 1479 | struct USBAutoFilter filter; |
26a9e82a | 1480 | USBDevice *dev; |
26a9e82a GH |
1481 | char *p; |
1482 | ||
556cd098 | 1483 | dev = usb_create(NULL /* FIXME */, "usb-host"); |
4b096fc9 | 1484 | |
5d0c5750 | 1485 | if (strstr(devname, "auto:")) { |
2791104c | 1486 | if (parse_filter(devname, &filter) < 0) { |
26a9e82a | 1487 | goto fail; |
2791104c | 1488 | } |
26a9e82a GH |
1489 | } else { |
1490 | if ((p = strchr(devname, '.'))) { | |
0745eb1e MA |
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; | |
26a9e82a | 1495 | } else if ((p = strchr(devname, ':'))) { |
0745eb1e MA |
1496 | filter.bus_num = 0; |
1497 | filter.addr = 0; | |
26a9e82a | 1498 | filter.vendor_id = strtoul(devname, NULL, 16); |
0745eb1e | 1499 | filter.product_id = strtoul(p + 1, NULL, 16); |
26a9e82a GH |
1500 | } else { |
1501 | goto fail; | |
1502 | } | |
5d0c5750 | 1503 | } |
4b096fc9 | 1504 | |
0745eb1e MA |
1505 | qdev_prop_set_uint32(&dev->qdev, "hostbus", filter.bus_num); |
1506 | qdev_prop_set_uint32(&dev->qdev, "hostaddr", filter.addr); | |
26a9e82a GH |
1507 | qdev_prop_set_uint32(&dev->qdev, "vendorid", filter.vendor_id); |
1508 | qdev_prop_set_uint32(&dev->qdev, "productid", filter.product_id); | |
beb6f0de | 1509 | qdev_init_nofail(&dev->qdev); |
26a9e82a | 1510 | return dev; |
5d0c5750 | 1511 | |
26a9e82a GH |
1512 | fail: |
1513 | qdev_free(&dev->qdev); | |
1514 | return NULL; | |
4b096fc9 | 1515 | } |
5d0c5750 AL |
1516 | |
1517 | int usb_host_device_close(const char *devname) | |
1518 | { | |
26a9e82a | 1519 | #if 0 |
5d0c5750 AL |
1520 | char product_name[PRODUCT_NAME_SZ]; |
1521 | int bus_num, addr; | |
1522 | USBHostDevice *s; | |
1523 | ||
2791104c | 1524 | if (strstr(devname, "auto:")) { |
5d0c5750 | 1525 | return usb_host_auto_del(devname); |
2791104c DA |
1526 | } |
1527 | if (usb_host_find_device(&bus_num, &addr, product_name, | |
1528 | sizeof(product_name), devname) < 0) { | |
5d0c5750 | 1529 | return -1; |
2791104c | 1530 | } |
5d0c5750 AL |
1531 | s = hostdev_find(bus_num, addr); |
1532 | if (s) { | |
a5d2f727 | 1533 | usb_device_delete_addr(s->bus_num, s->dev.addr); |
5d0c5750 AL |
1534 | return 0; |
1535 | } | |
26a9e82a | 1536 | #endif |
5d0c5750 AL |
1537 | |
1538 | return -1; | |
1539 | } | |
a5d2f727 | 1540 | |
a594cfbf | 1541 | static int get_tag_value(char *buf, int buf_size, |
5fafdf24 | 1542 | const char *str, const char *tag, |
a594cfbf FB |
1543 | const char *stopchars) |
1544 | { | |
1545 | const char *p; | |
1546 | char *q; | |
1547 | p = strstr(str, tag); | |
2791104c | 1548 | if (!p) { |
a594cfbf | 1549 | return -1; |
2791104c | 1550 | } |
a594cfbf | 1551 | p += strlen(tag); |
2791104c | 1552 | while (qemu_isspace(*p)) { |
a594cfbf | 1553 | p++; |
2791104c | 1554 | } |
a594cfbf FB |
1555 | q = buf; |
1556 | while (*p != '\0' && !strchr(stopchars, *p)) { | |
2791104c | 1557 | if ((q - buf) < (buf_size - 1)) { |
a594cfbf | 1558 | *q++ = *p; |
2791104c | 1559 | } |
a594cfbf FB |
1560 | p++; |
1561 | } | |
1562 | *q = '\0'; | |
1563 | return q - buf; | |
bb36d470 FB |
1564 | } |
1565 | ||
0f431527 AL |
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) | |
bb36d470 | 1572 | { |
660f11be | 1573 | FILE *f = NULL; |
a594cfbf | 1574 | char line[1024]; |
bb36d470 | 1575 | char buf[1024]; |
0c402e5a GH |
1576 | int bus_num, addr, speed, device_count; |
1577 | int class_id, product_id, vendor_id, port; | |
a594cfbf | 1578 | char product_name[512]; |
0f431527 | 1579 | int ret = 0; |
3b46e624 | 1580 | |
0f431527 AL |
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"); | |
a594cfbf | 1587 | if (!f) { |
0f431527 AL |
1588 | perror("husb: cannot open devices file"); |
1589 | goto the_end; | |
a594cfbf | 1590 | } |
0f431527 | 1591 | |
a594cfbf | 1592 | device_count = 0; |
0c402e5a | 1593 | bus_num = addr = class_id = product_id = vendor_id = port = 0; |
3991c35e | 1594 | speed = -1; /* Can't get the speed from /[proc|dev]/bus/usb/devices */ |
bb36d470 | 1595 | for(;;) { |
2791104c | 1596 | if (fgets(line, sizeof(line), f) == NULL) { |
bb36d470 | 1597 | break; |
2791104c DA |
1598 | } |
1599 | if (strlen(line) > 0) { | |
a594cfbf | 1600 | line[strlen(line) - 1] = '\0'; |
2791104c | 1601 | } |
a594cfbf | 1602 | if (line[0] == 'T' && line[1] == ':') { |
38ca0f6d PB |
1603 | if (device_count && (vendor_id || product_id)) { |
1604 | /* New device. Add the previously discovered device. */ | |
0cd0fd08 GH |
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, | |
a594cfbf | 1611 | product_id, product_name, speed); |
2791104c | 1612 | if (ret) { |
a594cfbf | 1613 | goto the_end; |
2791104c | 1614 | } |
a594cfbf | 1615 | } |
2791104c | 1616 | if (get_tag_value(buf, sizeof(buf), line, "Bus=", " ") < 0) { |
a594cfbf | 1617 | goto fail; |
2791104c | 1618 | } |
a594cfbf | 1619 | bus_num = atoi(buf); |
0c402e5a GH |
1620 | if (get_tag_value(buf, sizeof(buf), line, "Port=", " ") < 0) { |
1621 | goto fail; | |
1622 | } | |
1623 | port = atoi(buf); | |
2791104c | 1624 | if (get_tag_value(buf, sizeof(buf), line, "Dev#=", " ") < 0) { |
a594cfbf | 1625 | goto fail; |
2791104c | 1626 | } |
a594cfbf | 1627 | addr = atoi(buf); |
2791104c | 1628 | if (get_tag_value(buf, sizeof(buf), line, "Spd=", " ") < 0) { |
a594cfbf | 1629 | goto fail; |
2791104c | 1630 | } |
f264cfbf HG |
1631 | if (!strcmp(buf, "5000")) { |
1632 | speed = USB_SPEED_SUPER; | |
1633 | } else if (!strcmp(buf, "480")) { | |
a594cfbf | 1634 | speed = USB_SPEED_HIGH; |
2791104c | 1635 | } else if (!strcmp(buf, "1.5")) { |
a594cfbf | 1636 | speed = USB_SPEED_LOW; |
2791104c | 1637 | } else { |
a594cfbf | 1638 | speed = USB_SPEED_FULL; |
2791104c | 1639 | } |
a594cfbf FB |
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] == ':') { | |
2791104c | 1646 | if (get_tag_value(buf, sizeof(buf), line, "Vendor=", " ") < 0) { |
a594cfbf | 1647 | goto fail; |
2791104c | 1648 | } |
a594cfbf | 1649 | vendor_id = strtoul(buf, NULL, 16); |
2791104c | 1650 | if (get_tag_value(buf, sizeof(buf), line, "ProdID=", " ") < 0) { |
a594cfbf | 1651 | goto fail; |
2791104c | 1652 | } |
a594cfbf FB |
1653 | product_id = strtoul(buf, NULL, 16); |
1654 | } else if (line[0] == 'S' && line[1] == ':') { | |
2791104c | 1655 | if (get_tag_value(buf, sizeof(buf), line, "Product=", "") < 0) { |
a594cfbf | 1656 | goto fail; |
2791104c | 1657 | } |
a594cfbf FB |
1658 | pstrcpy(product_name, sizeof(product_name), buf); |
1659 | } else if (line[0] == 'D' && line[1] == ':') { | |
2791104c | 1660 | if (get_tag_value(buf, sizeof(buf), line, "Cls=", " (") < 0) { |
a594cfbf | 1661 | goto fail; |
2791104c | 1662 | } |
a594cfbf | 1663 | class_id = strtoul(buf, NULL, 16); |
bb36d470 | 1664 | } |
a594cfbf FB |
1665 | fail: ; |
1666 | } | |
38ca0f6d PB |
1667 | if (device_count && (vendor_id || product_id)) { |
1668 | /* Add the last device. */ | |
0c402e5a GH |
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, | |
a594cfbf | 1675 | product_id, product_name, speed); |
bb36d470 | 1676 | } |
a594cfbf | 1677 | the_end: |
2791104c | 1678 | if (f) { |
0f431527 | 1679 | fclose(f); |
2791104c | 1680 | } |
0f431527 AL |
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 | */ | |
2791104c DA |
1694 | static int usb_host_read_file(char *line, size_t line_size, |
1695 | const char *device_file, const char *device_name) | |
0f431527 AL |
1696 | { |
1697 | FILE *f; | |
1698 | int ret = 0; | |
1699 | char filename[PATH_MAX]; | |
1700 | ||
b4e237aa BS |
1701 | snprintf(filename, PATH_MAX, USBSYSBUS_PATH "/devices/%s/%s", device_name, |
1702 | device_file); | |
0f431527 AL |
1703 | f = fopen(filename, "r"); |
1704 | if (f) { | |
9f99cee7 | 1705 | ret = fgets(line, line_size, f) != NULL; |
0f431527 | 1706 | fclose(f); |
0f431527 AL |
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 | { | |
660f11be | 1721 | DIR *dir = NULL; |
0f431527 | 1722 | char line[1024]; |
5557d820 | 1723 | int bus_num, addr, speed, class_id, product_id, vendor_id; |
0f431527 | 1724 | int ret = 0; |
5557d820 | 1725 | char port[MAX_PORTLEN]; |
0f431527 AL |
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, ':')) { | |
5557d820 GH |
1737 | if (sscanf(de->d_name, "%d-%7[0-9.]", &bus_num, port) < 2) { |
1738 | continue; | |
0f5160d1 | 1739 | } |
0f431527 | 1740 | |
2791104c | 1741 | if (!usb_host_read_file(line, sizeof(line), "devnum", de->d_name)) { |
0f431527 | 1742 | goto the_end; |
2791104c DA |
1743 | } |
1744 | if (sscanf(line, "%d", &addr) != 1) { | |
0f431527 | 1745 | goto the_end; |
2791104c | 1746 | } |
b4e237aa | 1747 | if (!usb_host_read_file(line, sizeof(line), "bDeviceClass", |
2791104c | 1748 | de->d_name)) { |
0f431527 | 1749 | goto the_end; |
2791104c DA |
1750 | } |
1751 | if (sscanf(line, "%x", &class_id) != 1) { | |
0f431527 | 1752 | goto the_end; |
2791104c | 1753 | } |
0f431527 | 1754 | |
2791104c DA |
1755 | if (!usb_host_read_file(line, sizeof(line), "idVendor", |
1756 | de->d_name)) { | |
0f431527 | 1757 | goto the_end; |
2791104c DA |
1758 | } |
1759 | if (sscanf(line, "%x", &vendor_id) != 1) { | |
0f431527 | 1760 | goto the_end; |
2791104c | 1761 | } |
b4e237aa | 1762 | if (!usb_host_read_file(line, sizeof(line), "idProduct", |
2791104c | 1763 | de->d_name)) { |
0f431527 | 1764 | goto the_end; |
2791104c DA |
1765 | } |
1766 | if (sscanf(line, "%x", &product_id) != 1) { | |
0f431527 | 1767 | goto the_end; |
2791104c | 1768 | } |
b4e237aa BS |
1769 | if (!usb_host_read_file(line, sizeof(line), "product", |
1770 | de->d_name)) { | |
0f431527 AL |
1771 | *product_name = 0; |
1772 | } else { | |
2791104c | 1773 | if (strlen(line) > 0) { |
0f431527 | 1774 | line[strlen(line) - 1] = '\0'; |
2791104c | 1775 | } |
0f431527 AL |
1776 | pstrcpy(product_name, sizeof(product_name), line); |
1777 | } | |
1778 | ||
2791104c | 1779 | if (!usb_host_read_file(line, sizeof(line), "speed", de->d_name)) { |
0f431527 | 1780 | goto the_end; |
2791104c | 1781 | } |
f264cfbf HG |
1782 | if (!strcmp(line, "5000\n")) { |
1783 | speed = USB_SPEED_SUPER; | |
1784 | } else if (!strcmp(line, "480\n")) { | |
0f431527 | 1785 | speed = USB_SPEED_HIGH; |
2791104c | 1786 | } else if (!strcmp(line, "1.5\n")) { |
0f431527 | 1787 | speed = USB_SPEED_LOW; |
2791104c | 1788 | } else { |
0f431527 | 1789 | speed = USB_SPEED_FULL; |
2791104c | 1790 | } |
0f431527 | 1791 | |
5557d820 | 1792 | ret = func(opaque, bus_num, addr, port, class_id, vendor_id, |
0f431527 | 1793 | product_id, product_name, speed); |
2791104c | 1794 | if (ret) { |
0f431527 | 1795 | goto the_end; |
2791104c | 1796 | } |
0f431527 AL |
1797 | } |
1798 | } | |
1799 | the_end: | |
2791104c | 1800 | if (dir) { |
0f431527 | 1801 | closedir(dir); |
2791104c | 1802 | } |
0f431527 AL |
1803 | return ret; |
1804 | } | |
1805 | ||
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 | { | |
376253ec | 1812 | Monitor *mon = cur_mon; |
660f11be BS |
1813 | FILE *f = NULL; |
1814 | DIR *dir = NULL; | |
0f431527 | 1815 | int ret = 0; |
0f431527 AL |
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) { | |
55496240 MM |
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); | |
d0f2c4c6 | 1827 | DPRINTF(USBDBG_DEVOPENED, USBSYSBUS_PATH); |
55496240 MM |
1828 | goto found_devices; |
1829 | } | |
0f431527 AL |
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); | |
d0f2c4c6 | 1836 | DPRINTF(USBDBG_DEVOPENED, USBPROCBUS_PATH); |
f16a0db3 | 1837 | goto found_devices; |
0f431527 AL |
1838 | } |
1839 | /* try additional methods if an access method hasn't been found yet */ | |
1840 | f = fopen(USBDEVBUS_PATH "/devices", "r"); | |
f16a0db3 | 1841 | if (f) { |
0f431527 AL |
1842 | /* devices found in /dev/bus/usb/ */ |
1843 | strcpy(devpath, USBDEVBUS_PATH); | |
1844 | usb_fs_type = USB_FS_DEV; | |
1845 | fclose(f); | |
d0f2c4c6 | 1846 | DPRINTF(USBDBG_DEVOPENED, USBDEVBUS_PATH); |
f16a0db3 | 1847 | goto found_devices; |
0f431527 | 1848 | } |
f16a0db3 | 1849 | found_devices: |
22babebb | 1850 | if (!usb_fs_type) { |
2791104c | 1851 | if (mon) { |
eba6fe87 | 1852 | monitor_printf(mon, "husb: unable to access USB devices\n"); |
2791104c | 1853 | } |
f16a0db3 | 1854 | return -ENOENT; |
0f431527 AL |
1855 | } |
1856 | ||
1857 | /* the module setting (used later for opening devices) */ | |
7267c094 | 1858 | usb_host_device_path = g_malloc0(strlen(devpath)+1); |
1eec614b | 1859 | strcpy(usb_host_device_path, devpath); |
2791104c | 1860 | if (mon) { |
eba6fe87 GH |
1861 | monitor_printf(mon, "husb: using %s file-system with %s\n", |
1862 | fs_type[usb_fs_type], usb_host_device_path); | |
2791104c | 1863 | } |
0f431527 AL |
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; | |
f16a0db3 AL |
1874 | default: |
1875 | ret = -EINVAL; | |
1876 | break; | |
0f431527 | 1877 | } |
a594cfbf | 1878 | return ret; |
bb36d470 FB |
1879 | } |
1880 | ||
4b096fc9 | 1881 | static QEMUTimer *usb_auto_timer; |
4b096fc9 | 1882 | |
ba9acab9 GH |
1883 | static int usb_host_auto_scan(void *opaque, int bus_num, |
1884 | int addr, const char *port, | |
26a9e82a GH |
1885 | int class_id, int vendor_id, int product_id, |
1886 | const char *product_name, int speed) | |
4b096fc9 AL |
1887 | { |
1888 | struct USBAutoFilter *f; | |
26a9e82a | 1889 | struct USBHostDevice *s; |
4b096fc9 AL |
1890 | |
1891 | /* Ignore hubs */ | |
1892 | if (class_id == 9) | |
1893 | return 0; | |
1894 | ||
26a9e82a GH |
1895 | QTAILQ_FOREACH(s, &hostdevs, next) { |
1896 | f = &s->match; | |
1897 | ||
2791104c | 1898 | if (f->bus_num > 0 && f->bus_num != bus_num) { |
4b096fc9 | 1899 | continue; |
2791104c DA |
1900 | } |
1901 | if (f->addr > 0 && f->addr != addr) { | |
4b096fc9 | 1902 | continue; |
2791104c | 1903 | } |
9056a297 GH |
1904 | if (f->port != NULL && (port == NULL || strcmp(f->port, port) != 0)) { |
1905 | continue; | |
1906 | } | |
4b096fc9 | 1907 | |
2791104c | 1908 | if (f->vendor_id > 0 && f->vendor_id != vendor_id) { |
4b096fc9 | 1909 | continue; |
2791104c | 1910 | } |
4b096fc9 | 1911 | |
2791104c | 1912 | if (f->product_id > 0 && f->product_id != product_id) { |
4b096fc9 | 1913 | continue; |
2791104c | 1914 | } |
4b096fc9 | 1915 | /* We got a match */ |
3ee886c5 GH |
1916 | s->seen++; |
1917 | if (s->errcount >= 3) { | |
1918 | return 0; | |
1919 | } | |
4b096fc9 | 1920 | |
33e66b86 | 1921 | /* Already attached ? */ |
2791104c | 1922 | if (s->fd != -1) { |
4b096fc9 | 1923 | return 0; |
2791104c | 1924 | } |
d0f2c4c6 | 1925 | DPRINTF("husb: auto open: bus_num %d addr %d\n", bus_num, addr); |
4b096fc9 | 1926 | |
3ee886c5 GH |
1927 | if (usb_host_open(s, bus_num, addr, port, product_name, speed) < 0) { |
1928 | s->errcount++; | |
1929 | } | |
97f86166 | 1930 | break; |
4b096fc9 AL |
1931 | } |
1932 | ||
1933 | return 0; | |
1934 | } | |
1935 | ||
26a9e82a | 1936 | static void usb_host_auto_check(void *unused) |
4b096fc9 | 1937 | { |
26a9e82a GH |
1938 | struct USBHostDevice *s; |
1939 | int unconnected = 0; | |
1940 | ||
4b096fc9 | 1941 | usb_host_scan(NULL, usb_host_auto_scan); |
26a9e82a GH |
1942 | |
1943 | QTAILQ_FOREACH(s, &hostdevs, next) { | |
2791104c | 1944 | if (s->fd == -1) { |
26a9e82a | 1945 | unconnected++; |
2791104c | 1946 | } |
3ee886c5 GH |
1947 | if (s->seen == 0) { |
1948 | s->errcount = 0; | |
1949 | } | |
1950 | s->seen = 0; | |
26a9e82a GH |
1951 | } |
1952 | ||
1953 | if (unconnected == 0) { | |
1954 | /* nothing to watch */ | |
2791104c | 1955 | if (usb_auto_timer) { |
26a9e82a | 1956 | qemu_del_timer(usb_auto_timer); |
e6a2f500 | 1957 | trace_usb_host_auto_scan_disabled(); |
2791104c | 1958 | } |
26a9e82a GH |
1959 | return; |
1960 | } | |
1961 | ||
1962 | if (!usb_auto_timer) { | |
7bd427d8 | 1963 | usb_auto_timer = qemu_new_timer_ms(rt_clock, usb_host_auto_check, NULL); |
2791104c | 1964 | if (!usb_auto_timer) { |
26a9e82a | 1965 | return; |
2791104c | 1966 | } |
e6a2f500 | 1967 | trace_usb_host_auto_scan_enabled(); |
26a9e82a | 1968 | } |
7bd427d8 | 1969 | qemu_mod_timer(usb_auto_timer, qemu_get_clock_ms(rt_clock) + 2000); |
4b096fc9 AL |
1970 | } |
1971 | ||
1972 | /* | |
5d0c5750 AL |
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. | |
4b096fc9 | 1984 | */ |
5d0c5750 | 1985 | static int parse_filter(const char *spec, struct USBAutoFilter *f) |
4b096fc9 | 1986 | { |
5d0c5750 AL |
1987 | enum { BUS, DEV, VID, PID, DONE }; |
1988 | const char *p = spec; | |
1989 | int i; | |
1990 | ||
0745eb1e MA |
1991 | f->bus_num = 0; |
1992 | f->addr = 0; | |
1993 | f->vendor_id = 0; | |
1994 | f->product_id = 0; | |
5d0c5750 AL |
1995 | |
1996 | for (i = BUS; i < DONE; i++) { | |
2791104c DA |
1997 | p = strpbrk(p, ":."); |
1998 | if (!p) { | |
1999 | break; | |
2000 | } | |
5d0c5750 | 2001 | p++; |
5d0c5750 | 2002 | |
2791104c DA |
2003 | if (*p == '*') { |
2004 | continue; | |
2005 | } | |
5d0c5750 AL |
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 | ||
a594cfbf FB |
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" }, | |
b9dc033c | 2042 | { USB_CLASS_CSCID, "Smart Card" }, |
a594cfbf FB |
2043 | { USB_CLASS_CONTENT_SEC, "Content Security" }, |
2044 | { -1, NULL } | |
2045 | }; | |
2046 | ||
2047 | static const char *usb_class_str(uint8_t class) | |
bb36d470 | 2048 | { |
a594cfbf FB |
2049 | const struct usb_class_info *p; |
2050 | for(p = usb_class_info; p->class != -1; p++) { | |
2791104c | 2051 | if (p->class == class) { |
a594cfbf | 2052 | break; |
2791104c | 2053 | } |
bb36d470 | 2054 | } |
a594cfbf FB |
2055 | return p->class_name; |
2056 | } | |
2057 | ||
ba9acab9 GH |
2058 | static void usb_info_device(Monitor *mon, int bus_num, |
2059 | int addr, const char *port, | |
5557d820 | 2060 | int class_id, int vendor_id, int product_id, |
9596ebb7 PB |
2061 | const char *product_name, |
2062 | int speed) | |
a594cfbf FB |
2063 | { |
2064 | const char *class_str, *speed_str; | |
2065 | ||
2066 | switch(speed) { | |
5fafdf24 TS |
2067 | case USB_SPEED_LOW: |
2068 | speed_str = "1.5"; | |
a594cfbf | 2069 | break; |
5fafdf24 TS |
2070 | case USB_SPEED_FULL: |
2071 | speed_str = "12"; | |
a594cfbf | 2072 | break; |
5fafdf24 TS |
2073 | case USB_SPEED_HIGH: |
2074 | speed_str = "480"; | |
a594cfbf | 2075 | break; |
f264cfbf HG |
2076 | case USB_SPEED_SUPER: |
2077 | speed_str = "5000"; | |
2078 | break; | |
a594cfbf | 2079 | default: |
5fafdf24 | 2080 | speed_str = "?"; |
a594cfbf FB |
2081 | break; |
2082 | } | |
2083 | ||
5557d820 GH |
2084 | monitor_printf(mon, " Bus %d, Addr %d, Port %s, Speed %s Mb/s\n", |
2085 | bus_num, addr, port, speed_str); | |
a594cfbf | 2086 | class_str = usb_class_str(class_id); |
2791104c | 2087 | if (class_str) { |
376253ec | 2088 | monitor_printf(mon, " %s:", class_str); |
2791104c | 2089 | } else { |
376253ec | 2090 | monitor_printf(mon, " Class %02x:", class_id); |
2791104c | 2091 | } |
376253ec | 2092 | monitor_printf(mon, " USB device %04x:%04x", vendor_id, product_id); |
2791104c | 2093 | if (product_name[0] != '\0') { |
376253ec | 2094 | monitor_printf(mon, ", %s", product_name); |
2791104c | 2095 | } |
376253ec | 2096 | monitor_printf(mon, "\n"); |
a594cfbf FB |
2097 | } |
2098 | ||
5fafdf24 | 2099 | static int usb_host_info_device(void *opaque, int bus_num, int addr, |
ba9acab9 | 2100 | const char *path, int class_id, |
5fafdf24 | 2101 | int vendor_id, int product_id, |
a594cfbf FB |
2102 | const char *product_name, |
2103 | int speed) | |
2104 | { | |
179da8af BS |
2105 | Monitor *mon = opaque; |
2106 | ||
5557d820 | 2107 | usb_info_device(mon, bus_num, addr, path, class_id, vendor_id, product_id, |
a594cfbf FB |
2108 | product_name, speed); |
2109 | return 0; | |
2110 | } | |
2111 | ||
ac4ffb5a | 2112 | static void dec2str(int val, char *str, size_t size) |
5d0c5750 | 2113 | { |
2791104c | 2114 | if (val == 0) { |
ac4ffb5a | 2115 | snprintf(str, size, "*"); |
2791104c DA |
2116 | } else { |
2117 | snprintf(str, size, "%d", val); | |
2118 | } | |
5d0c5750 AL |
2119 | } |
2120 | ||
ac4ffb5a | 2121 | static void hex2str(int val, char *str, size_t size) |
5d0c5750 | 2122 | { |
2791104c | 2123 | if (val == 0) { |
ac4ffb5a | 2124 | snprintf(str, size, "*"); |
2791104c | 2125 | } else { |
26a9e82a | 2126 | snprintf(str, size, "%04x", val); |
2791104c | 2127 | } |
5d0c5750 AL |
2128 | } |
2129 | ||
376253ec | 2130 | void usb_host_info(Monitor *mon) |
a594cfbf | 2131 | { |
5d0c5750 | 2132 | struct USBAutoFilter *f; |
26a9e82a | 2133 | struct USBHostDevice *s; |
5d0c5750 | 2134 | |
179da8af | 2135 | usb_host_scan(mon, usb_host_info_device); |
5d0c5750 | 2136 | |
2791104c | 2137 | if (QTAILQ_EMPTY(&hostdevs)) { |
26a9e82a | 2138 | return; |
2791104c DA |
2139 | } |
2140 | ||
26a9e82a GH |
2141 | monitor_printf(mon, " Auto filters:\n"); |
2142 | QTAILQ_FOREACH(s, &hostdevs, next) { | |
5d0c5750 | 2143 | char bus[10], addr[10], vid[10], pid[10]; |
26a9e82a | 2144 | f = &s->match; |
ac4ffb5a AL |
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)); | |
9056a297 GH |
2149 | monitor_printf(mon, " Bus %s, Addr %s, Port %s, ID %s:%s\n", |
2150 | bus, addr, f->port ? f->port : "*", vid, pid); | |
5d0c5750 | 2151 | } |
bb36d470 | 2152 | } |