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