]>
Commit | Line | Data |
---|---|---|
bb36d470 FB |
1 | /* |
2 | * Linux host USB redirector | |
3 | * | |
4 | * Copyright (c) 2005 Fabrice Bellard | |
5fafdf24 | 5 | * |
64838171 AL |
6 | * Copyright (c) 2008 Max Krasnyansky |
7 | * Support for host device auto connect & disconnect | |
5d0c5750 | 8 | * Major rewrite to support fully async operation |
4b096fc9 | 9 | * |
0f431527 AL |
10 | * Copyright 2008 TJ <[email protected]> |
11 | * Added flexible support for /dev/bus/usb /sys/bus/usb/devices in addition | |
12 | * to the legacy /proc/bus/usb USB device discovery and handling | |
13 | * | |
bb36d470 FB |
14 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
15 | * of this software and associated documentation files (the "Software"), to deal | |
16 | * in the Software without restriction, including without limitation the rights | |
17 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
18 | * copies of the Software, and to permit persons to whom the Software is | |
19 | * furnished to do so, subject to the following conditions: | |
20 | * | |
21 | * The above copyright notice and this permission notice shall be included in | |
22 | * all copies or substantial portions of the Software. | |
23 | * | |
24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
25 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
26 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
27 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
28 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
29 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
30 | * THE SOFTWARE. | |
31 | */ | |
446ab128 | 32 | |
87ecb68b | 33 | #include "qemu-common.h" |
1f3870ab | 34 | #include "qemu-timer.h" |
376253ec | 35 | #include "monitor.h" |
bb36d470 | 36 | |
bb36d470 FB |
37 | #include <dirent.h> |
38 | #include <sys/ioctl.h> | |
b9dc033c | 39 | #include <signal.h> |
bb36d470 | 40 | |
446ab128 AL |
41 | #include <linux/usbdevice_fs.h> |
42 | #include <linux/version.h> | |
43 | #include "hw/usb.h" | |
bb36d470 | 44 | |
d9cf1578 BS |
45 | /* We redefine it to avoid version problems */ |
46 | struct usb_ctrltransfer { | |
47 | uint8_t bRequestType; | |
48 | uint8_t bRequest; | |
49 | uint16_t wValue; | |
50 | uint16_t wIndex; | |
51 | uint16_t wLength; | |
52 | uint32_t timeout; | |
53 | void *data; | |
54 | }; | |
55 | ||
56 | struct usb_ctrlrequest { | |
57 | uint8_t bRequestType; | |
58 | uint8_t bRequest; | |
59 | uint16_t wValue; | |
60 | uint16_t wIndex; | |
61 | uint16_t wLength; | |
62 | }; | |
63 | ||
a594cfbf | 64 | typedef int USBScanFunc(void *opaque, int bus_num, int addr, int class_id, |
5fafdf24 | 65 | int vendor_id, int product_id, |
a594cfbf | 66 | const char *product_name, int speed); |
26a9e82a | 67 | |
0745eb1e | 68 | //#define DEBUG |
64838171 AL |
69 | |
70 | #ifdef DEBUG | |
d0f2c4c6 | 71 | #define DPRINTF printf |
64838171 | 72 | #else |
d0f2c4c6 | 73 | #define DPRINTF(...) |
64838171 | 74 | #endif |
bb36d470 | 75 | |
5be8e1f2 BS |
76 | #define USBDBG_DEVOPENED "husb: opened %s/devices\n" |
77 | ||
0f431527 | 78 | #define USBPROCBUS_PATH "/proc/bus/usb" |
1f6e24e7 | 79 | #define PRODUCT_NAME_SZ 32 |
b9dc033c | 80 | #define MAX_ENDPOINTS 16 |
0f431527 AL |
81 | #define USBDEVBUS_PATH "/dev/bus/usb" |
82 | #define USBSYSBUS_PATH "/sys/bus/usb" | |
83 | ||
84 | static char *usb_host_device_path; | |
85 | ||
86 | #define USB_FS_NONE 0 | |
87 | #define USB_FS_PROC 1 | |
88 | #define USB_FS_DEV 2 | |
89 | #define USB_FS_SYS 3 | |
90 | ||
91 | static int usb_fs_type; | |
bb36d470 | 92 | |
b9dc033c AZ |
93 | /* endpoint association data */ |
94 | struct endp_data { | |
95 | uint8_t type; | |
64838171 | 96 | uint8_t halted; |
b9dc033c AZ |
97 | }; |
98 | ||
446ab128 AL |
99 | enum { |
100 | CTRL_STATE_IDLE = 0, | |
101 | CTRL_STATE_SETUP, | |
102 | CTRL_STATE_DATA, | |
103 | CTRL_STATE_ACK | |
104 | }; | |
105 | ||
106 | /* | |
107 | * Control transfer state. | |
2791104c | 108 | * Note that 'buffer' _must_ follow 'req' field because |
446ab128 | 109 | * we need contigious buffer when we submit control URB. |
2791104c | 110 | */ |
446ab128 AL |
111 | struct ctrl_struct { |
112 | uint16_t len; | |
113 | uint16_t offset; | |
114 | uint8_t state; | |
115 | struct usb_ctrlrequest req; | |
fd7a446f | 116 | uint8_t buffer[8192]; |
446ab128 AL |
117 | }; |
118 | ||
26a9e82a GH |
119 | struct USBAutoFilter { |
120 | uint32_t bus_num; | |
121 | uint32_t addr; | |
122 | uint32_t vendor_id; | |
123 | uint32_t product_id; | |
124 | }; | |
125 | ||
bb36d470 FB |
126 | typedef struct USBHostDevice { |
127 | USBDevice dev; | |
64838171 AL |
128 | int fd; |
129 | ||
130 | uint8_t descr[1024]; | |
131 | int descr_len; | |
132 | int configuration; | |
446ab128 | 133 | int ninterfaces; |
24772c1e | 134 | int closing; |
64838171 | 135 | |
446ab128 | 136 | struct ctrl_struct ctrl; |
b9dc033c | 137 | struct endp_data endp_table[MAX_ENDPOINTS]; |
4b096fc9 | 138 | |
4b096fc9 AL |
139 | /* Host side address */ |
140 | int bus_num; | |
141 | int addr; | |
26a9e82a | 142 | struct USBAutoFilter match; |
4b096fc9 | 143 | |
26a9e82a | 144 | QTAILQ_ENTRY(USBHostDevice) next; |
bb36d470 FB |
145 | } USBHostDevice; |
146 | ||
26a9e82a GH |
147 | static QTAILQ_HEAD(, USBHostDevice) hostdevs = QTAILQ_HEAD_INITIALIZER(hostdevs); |
148 | ||
149 | static int usb_host_close(USBHostDevice *dev); | |
150 | static int parse_filter(const char *spec, struct USBAutoFilter *f); | |
151 | static void usb_host_auto_check(void *unused); | |
152 | ||
64838171 AL |
153 | static int is_isoc(USBHostDevice *s, int ep) |
154 | { | |
155 | return s->endp_table[ep - 1].type == USBDEVFS_URB_TYPE_ISO; | |
156 | } | |
157 | ||
158 | static int is_halted(USBHostDevice *s, int ep) | |
159 | { | |
160 | return s->endp_table[ep - 1].halted; | |
161 | } | |
162 | ||
163 | static void clear_halt(USBHostDevice *s, int ep) | |
164 | { | |
165 | s->endp_table[ep - 1].halted = 0; | |
166 | } | |
167 | ||
168 | static void set_halt(USBHostDevice *s, int ep) | |
169 | { | |
170 | s->endp_table[ep - 1].halted = 1; | |
171 | } | |
172 | ||
2791104c | 173 | /* |
64838171 AL |
174 | * Async URB state. |
175 | * We always allocate one isoc descriptor even for bulk transfers | |
2791104c | 176 | * to simplify allocation and casts. |
64838171 AL |
177 | */ |
178 | typedef struct AsyncURB | |
179 | { | |
180 | struct usbdevfs_urb urb; | |
181 | struct usbdevfs_iso_packet_desc isocpd; | |
b9dc033c | 182 | |
64838171 AL |
183 | USBPacket *packet; |
184 | USBHostDevice *hdev; | |
185 | } AsyncURB; | |
b9dc033c | 186 | |
64838171 | 187 | static AsyncURB *async_alloc(void) |
b9dc033c | 188 | { |
64838171 | 189 | return (AsyncURB *) qemu_mallocz(sizeof(AsyncURB)); |
b9dc033c AZ |
190 | } |
191 | ||
64838171 | 192 | static void async_free(AsyncURB *aurb) |
b9dc033c | 193 | { |
64838171 AL |
194 | qemu_free(aurb); |
195 | } | |
b9dc033c | 196 | |
446ab128 AL |
197 | static void async_complete_ctrl(USBHostDevice *s, USBPacket *p) |
198 | { | |
199 | switch(s->ctrl.state) { | |
200 | case CTRL_STATE_SETUP: | |
201 | if (p->len < s->ctrl.len) | |
202 | s->ctrl.len = p->len; | |
203 | s->ctrl.state = CTRL_STATE_DATA; | |
204 | p->len = 8; | |
205 | break; | |
206 | ||
207 | case CTRL_STATE_ACK: | |
208 | s->ctrl.state = CTRL_STATE_IDLE; | |
209 | p->len = 0; | |
210 | break; | |
211 | ||
212 | default: | |
213 | break; | |
214 | } | |
215 | } | |
216 | ||
64838171 AL |
217 | static void async_complete(void *opaque) |
218 | { | |
219 | USBHostDevice *s = opaque; | |
220 | AsyncURB *aurb; | |
221 | ||
222 | while (1) { | |
2791104c | 223 | USBPacket *p; |
b9dc033c | 224 | |
2791104c | 225 | int r = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &aurb); |
64838171 | 226 | if (r < 0) { |
2791104c | 227 | if (errno == EAGAIN) { |
64838171 | 228 | return; |
2791104c | 229 | } |
24772c1e | 230 | if (errno == ENODEV && !s->closing) { |
2791104c DA |
231 | printf("husb: device %d.%d disconnected\n", |
232 | s->bus_num, s->addr); | |
26a9e82a GH |
233 | usb_host_close(s); |
234 | usb_host_auto_check(NULL); | |
64838171 AL |
235 | return; |
236 | } | |
237 | ||
d0f2c4c6 | 238 | DPRINTF("husb: async. reap urb failed errno %d\n", errno); |
64838171 | 239 | return; |
b9dc033c | 240 | } |
64838171 AL |
241 | |
242 | p = aurb->packet; | |
243 | ||
2791104c | 244 | DPRINTF("husb: async completed. aurb %p status %d alen %d\n", |
64838171 AL |
245 | aurb, aurb->urb.status, aurb->urb.actual_length); |
246 | ||
2791104c | 247 | if (p) { |
64838171 AL |
248 | switch (aurb->urb.status) { |
249 | case 0: | |
250 | p->len = aurb->urb.actual_length; | |
2791104c | 251 | if (aurb->urb.type == USBDEVFS_URB_TYPE_CONTROL) { |
446ab128 | 252 | async_complete_ctrl(s, p); |
2791104c | 253 | } |
64838171 AL |
254 | break; |
255 | ||
256 | case -EPIPE: | |
257 | set_halt(s, p->devep); | |
2791104c DA |
258 | p->len = USB_RET_STALL; |
259 | break; | |
dcc7e25f | 260 | |
64838171 AL |
261 | default: |
262 | p->len = USB_RET_NAK; | |
263 | break; | |
264 | } | |
265 | ||
266 | usb_packet_complete(p); | |
2791104c | 267 | } |
64838171 AL |
268 | |
269 | async_free(aurb); | |
b9dc033c | 270 | } |
b9dc033c AZ |
271 | } |
272 | ||
64838171 | 273 | static void async_cancel(USBPacket *unused, void *opaque) |
b9dc033c | 274 | { |
64838171 AL |
275 | AsyncURB *aurb = opaque; |
276 | USBHostDevice *s = aurb->hdev; | |
b9dc033c | 277 | |
d0f2c4c6 | 278 | DPRINTF("husb: async cancel. aurb %p\n", aurb); |
64838171 AL |
279 | |
280 | /* Mark it as dead (see async_complete above) */ | |
281 | aurb->packet = NULL; | |
b9dc033c | 282 | |
64838171 AL |
283 | int r = ioctl(s->fd, USBDEVFS_DISCARDURB, aurb); |
284 | if (r < 0) { | |
d0f2c4c6 | 285 | DPRINTF("husb: async. discard urb failed errno %d\n", errno); |
b9dc033c | 286 | } |
b9dc033c AZ |
287 | } |
288 | ||
446ab128 | 289 | static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration) |
b9dc033c AZ |
290 | { |
291 | int dev_descr_len, config_descr_len; | |
d4c4e6fd | 292 | int interface, nb_interfaces; |
b9dc033c AZ |
293 | int ret, i; |
294 | ||
295 | if (configuration == 0) /* address state - ignore */ | |
296 | return 1; | |
297 | ||
d0f2c4c6 | 298 | DPRINTF("husb: claiming interfaces. config %d\n", configuration); |
446ab128 | 299 | |
b9dc033c AZ |
300 | i = 0; |
301 | dev_descr_len = dev->descr[0]; | |
2791104c | 302 | if (dev_descr_len > dev->descr_len) { |
b9dc033c | 303 | goto fail; |
2791104c | 304 | } |
b9dc033c AZ |
305 | |
306 | i += dev_descr_len; | |
307 | while (i < dev->descr_len) { | |
2791104c DA |
308 | DPRINTF("husb: i is %d, descr_len is %d, dl %d, dt %d\n", |
309 | i, dev->descr_len, | |
b9dc033c | 310 | dev->descr[i], dev->descr[i+1]); |
64838171 | 311 | |
b9dc033c AZ |
312 | if (dev->descr[i+1] != USB_DT_CONFIG) { |
313 | i += dev->descr[i]; | |
314 | continue; | |
315 | } | |
316 | config_descr_len = dev->descr[i]; | |
317 | ||
2791104c | 318 | printf("husb: config #%d need %d\n", dev->descr[i + 5], configuration); |
1f3870ab | 319 | |
446ab128 AL |
320 | if (configuration < 0 || configuration == dev->descr[i + 5]) { |
321 | configuration = dev->descr[i + 5]; | |
b9dc033c | 322 | break; |
446ab128 | 323 | } |
b9dc033c AZ |
324 | |
325 | i += config_descr_len; | |
326 | } | |
327 | ||
328 | if (i >= dev->descr_len) { | |
2791104c DA |
329 | fprintf(stderr, |
330 | "husb: update iface failed. no matching configuration\n"); | |
b9dc033c AZ |
331 | goto fail; |
332 | } | |
333 | nb_interfaces = dev->descr[i + 4]; | |
334 | ||
335 | #ifdef USBDEVFS_DISCONNECT | |
336 | /* earlier Linux 2.4 do not support that */ | |
337 | { | |
338 | struct usbdevfs_ioctl ctrl; | |
339 | for (interface = 0; interface < nb_interfaces; interface++) { | |
340 | ctrl.ioctl_code = USBDEVFS_DISCONNECT; | |
341 | ctrl.ifno = interface; | |
342 | ret = ioctl(dev->fd, USBDEVFS_IOCTL, &ctrl); | |
343 | if (ret < 0 && errno != ENODATA) { | |
344 | perror("USBDEVFS_DISCONNECT"); | |
345 | goto fail; | |
346 | } | |
347 | } | |
348 | } | |
349 | #endif | |
350 | ||
351 | /* XXX: only grab if all interfaces are free */ | |
352 | for (interface = 0; interface < nb_interfaces; interface++) { | |
353 | ret = ioctl(dev->fd, USBDEVFS_CLAIMINTERFACE, &interface); | |
354 | if (ret < 0) { | |
355 | if (errno == EBUSY) { | |
64838171 | 356 | printf("husb: update iface. device already grabbed\n"); |
b9dc033c | 357 | } else { |
64838171 | 358 | perror("husb: failed to claim interface"); |
b9dc033c AZ |
359 | } |
360 | fail: | |
361 | return 0; | |
362 | } | |
363 | } | |
364 | ||
64838171 | 365 | printf("husb: %d interfaces claimed for configuration %d\n", |
b9dc033c | 366 | nb_interfaces, configuration); |
b9dc033c | 367 | |
446ab128 AL |
368 | dev->ninterfaces = nb_interfaces; |
369 | dev->configuration = configuration; | |
370 | return 1; | |
371 | } | |
372 | ||
373 | static int usb_host_release_interfaces(USBHostDevice *s) | |
374 | { | |
375 | int ret, i; | |
376 | ||
d0f2c4c6 | 377 | DPRINTF("husb: releasing interfaces\n"); |
446ab128 AL |
378 | |
379 | for (i = 0; i < s->ninterfaces; i++) { | |
380 | ret = ioctl(s->fd, USBDEVFS_RELEASEINTERFACE, &i); | |
381 | if (ret < 0) { | |
382 | perror("husb: failed to release interface"); | |
383 | return 0; | |
384 | } | |
385 | } | |
386 | ||
b9dc033c AZ |
387 | return 1; |
388 | } | |
389 | ||
059809e4 | 390 | static void usb_host_handle_reset(USBDevice *dev) |
bb36d470 | 391 | { |
26a9e82a | 392 | USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev); |
64838171 | 393 | |
d0f2c4c6 | 394 | DPRINTF("husb: reset device %u.%u\n", s->bus_num, s->addr); |
64838171 | 395 | |
bb36d470 | 396 | ioctl(s->fd, USBDEVFS_RESET); |
446ab128 AL |
397 | |
398 | usb_host_claim_interfaces(s, s->configuration); | |
5fafdf24 | 399 | } |
bb36d470 | 400 | |
059809e4 FB |
401 | static void usb_host_handle_destroy(USBDevice *dev) |
402 | { | |
403 | USBHostDevice *s = (USBHostDevice *)dev; | |
404 | ||
26a9e82a GH |
405 | usb_host_close(s); |
406 | QTAILQ_REMOVE(&hostdevs, s, next); | |
059809e4 FB |
407 | } |
408 | ||
b9dc033c AZ |
409 | static int usb_linux_update_endp_table(USBHostDevice *s); |
410 | ||
446ab128 | 411 | static int usb_host_handle_data(USBHostDevice *s, USBPacket *p) |
bb36d470 | 412 | { |
64838171 | 413 | struct usbdevfs_urb *urb; |
446ab128 | 414 | AsyncURB *aurb; |
bb36d470 FB |
415 | int ret; |
416 | ||
64838171 | 417 | aurb = async_alloc(); |
64838171 AL |
418 | aurb->hdev = s; |
419 | aurb->packet = p; | |
420 | ||
421 | urb = &aurb->urb; | |
b9dc033c | 422 | |
2791104c DA |
423 | if (p->pid == USB_TOKEN_IN) { |
424 | urb->endpoint = p->devep | 0x80; | |
425 | } else { | |
426 | urb->endpoint = p->devep; | |
427 | } | |
64838171 AL |
428 | |
429 | if (is_halted(s, p->devep)) { | |
2791104c | 430 | ret = ioctl(s->fd, USBDEVFS_CLEAR_HALT, &urb->endpoint); |
64838171 | 431 | if (ret < 0) { |
2791104c | 432 | DPRINTF("husb: failed to clear halt. ep 0x%x errno %d\n", |
64838171 | 433 | urb->endpoint, errno); |
bb36d470 | 434 | return USB_RET_NAK; |
bb36d470 | 435 | } |
64838171 | 436 | clear_halt(s, p->devep); |
4d043a09 AZ |
437 | } |
438 | ||
64838171 AL |
439 | urb->buffer = p->data; |
440 | urb->buffer_length = p->len; | |
b9dc033c | 441 | |
64838171 AL |
442 | if (is_isoc(s, p->devep)) { |
443 | /* Setup ISOC transfer */ | |
444 | urb->type = USBDEVFS_URB_TYPE_ISO; | |
445 | urb->flags = USBDEVFS_URB_ISO_ASAP; | |
446 | urb->number_of_packets = 1; | |
447 | urb->iso_frame_desc[0].length = p->len; | |
448 | } else { | |
449 | /* Setup bulk transfer */ | |
450 | urb->type = USBDEVFS_URB_TYPE_BULK; | |
b9dc033c AZ |
451 | } |
452 | ||
64838171 | 453 | urb->usercontext = s; |
b9dc033c | 454 | |
64838171 | 455 | ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb); |
b9dc033c | 456 | |
2791104c DA |
457 | DPRINTF("husb: data submit. ep 0x%x len %u aurb %p\n", |
458 | urb->endpoint, p->len, aurb); | |
b9dc033c | 459 | |
64838171 | 460 | if (ret < 0) { |
d0f2c4c6 | 461 | DPRINTF("husb: submit failed. errno %d\n", errno); |
64838171 | 462 | async_free(aurb); |
b9dc033c | 463 | |
b9dc033c AZ |
464 | switch(errno) { |
465 | case ETIMEDOUT: | |
466 | return USB_RET_NAK; | |
467 | case EPIPE: | |
468 | default: | |
469 | return USB_RET_STALL; | |
470 | } | |
471 | } | |
64838171 AL |
472 | |
473 | usb_defer_packet(p, async_cancel, aurb); | |
b9dc033c | 474 | return USB_RET_ASYNC; |
b9dc033c AZ |
475 | } |
476 | ||
446ab128 AL |
477 | static int ctrl_error(void) |
478 | { | |
2791104c | 479 | if (errno == ETIMEDOUT) { |
446ab128 | 480 | return USB_RET_NAK; |
2791104c | 481 | } else { |
446ab128 | 482 | return USB_RET_STALL; |
2791104c | 483 | } |
446ab128 AL |
484 | } |
485 | ||
486 | static int usb_host_set_address(USBHostDevice *s, int addr) | |
487 | { | |
d0f2c4c6 | 488 | DPRINTF("husb: ctrl set addr %u\n", addr); |
446ab128 AL |
489 | s->dev.addr = addr; |
490 | return 0; | |
491 | } | |
492 | ||
493 | static int usb_host_set_config(USBHostDevice *s, int config) | |
494 | { | |
495 | usb_host_release_interfaces(s); | |
496 | ||
497 | int ret = ioctl(s->fd, USBDEVFS_SETCONFIGURATION, &config); | |
2791104c | 498 | |
d0f2c4c6 | 499 | DPRINTF("husb: ctrl set config %d ret %d errno %d\n", config, ret, errno); |
2791104c DA |
500 | |
501 | if (ret < 0) { | |
446ab128 | 502 | return ctrl_error(); |
2791104c | 503 | } |
446ab128 AL |
504 | usb_host_claim_interfaces(s, config); |
505 | return 0; | |
506 | } | |
507 | ||
508 | static int usb_host_set_interface(USBHostDevice *s, int iface, int alt) | |
509 | { | |
510 | struct usbdevfs_setinterface si; | |
511 | int ret; | |
512 | ||
513 | si.interface = iface; | |
514 | si.altsetting = alt; | |
515 | ret = ioctl(s->fd, USBDEVFS_SETINTERFACE, &si); | |
446ab128 | 516 | |
2791104c DA |
517 | DPRINTF("husb: ctrl set iface %d altset %d ret %d errno %d\n", |
518 | iface, alt, ret, errno); | |
519 | ||
520 | if (ret < 0) { | |
521 | return ctrl_error(); | |
522 | } | |
446ab128 AL |
523 | usb_linux_update_endp_table(s); |
524 | return 0; | |
525 | } | |
526 | ||
527 | static int usb_host_handle_control(USBHostDevice *s, USBPacket *p) | |
528 | { | |
529 | struct usbdevfs_urb *urb; | |
530 | AsyncURB *aurb; | |
531 | int ret, value, index; | |
c4c0e236 | 532 | int buffer_len; |
446ab128 | 533 | |
2791104c | 534 | /* |
446ab128 AL |
535 | * Process certain standard device requests. |
536 | * These are infrequent and are processed synchronously. | |
537 | */ | |
538 | value = le16_to_cpu(s->ctrl.req.wValue); | |
539 | index = le16_to_cpu(s->ctrl.req.wIndex); | |
540 | ||
d0f2c4c6 | 541 | DPRINTF("husb: ctrl type 0x%x req 0x%x val 0x%x index %u len %u\n", |
2791104c DA |
542 | s->ctrl.req.bRequestType, s->ctrl.req.bRequest, value, index, |
543 | s->ctrl.len); | |
446ab128 AL |
544 | |
545 | if (s->ctrl.req.bRequestType == 0) { | |
546 | switch (s->ctrl.req.bRequest) { | |
547 | case USB_REQ_SET_ADDRESS: | |
548 | return usb_host_set_address(s, value); | |
549 | ||
550 | case USB_REQ_SET_CONFIGURATION: | |
551 | return usb_host_set_config(s, value & 0xff); | |
552 | } | |
553 | } | |
554 | ||
555 | if (s->ctrl.req.bRequestType == 1 && | |
2791104c | 556 | s->ctrl.req.bRequest == USB_REQ_SET_INTERFACE) { |
446ab128 | 557 | return usb_host_set_interface(s, index, value); |
2791104c | 558 | } |
446ab128 AL |
559 | |
560 | /* The rest are asynchronous */ | |
561 | ||
c4c0e236 JP |
562 | buffer_len = 8 + s->ctrl.len; |
563 | if (buffer_len > sizeof(s->ctrl.buffer)) { | |
b2e3b6e9 | 564 | fprintf(stderr, "husb: ctrl buffer too small (%u > %zu)\n", |
565 | buffer_len, sizeof(s->ctrl.buffer)); | |
566 | return USB_RET_STALL; | |
c4c0e236 JP |
567 | } |
568 | ||
446ab128 | 569 | aurb = async_alloc(); |
446ab128 AL |
570 | aurb->hdev = s; |
571 | aurb->packet = p; | |
572 | ||
2791104c | 573 | /* |
446ab128 AL |
574 | * Setup ctrl transfer. |
575 | * | |
576 | * s->ctrl is layed out such that data buffer immediately follows | |
577 | * 'req' struct which is exactly what usbdevfs expects. | |
2791104c | 578 | */ |
446ab128 AL |
579 | urb = &aurb->urb; |
580 | ||
581 | urb->type = USBDEVFS_URB_TYPE_CONTROL; | |
582 | urb->endpoint = p->devep; | |
583 | ||
584 | urb->buffer = &s->ctrl.req; | |
c4c0e236 | 585 | urb->buffer_length = buffer_len; |
446ab128 AL |
586 | |
587 | urb->usercontext = s; | |
588 | ||
589 | ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb); | |
590 | ||
d0f2c4c6 | 591 | DPRINTF("husb: submit ctrl. len %u aurb %p\n", urb->buffer_length, aurb); |
446ab128 AL |
592 | |
593 | if (ret < 0) { | |
d0f2c4c6 | 594 | DPRINTF("husb: submit failed. errno %d\n", errno); |
446ab128 AL |
595 | async_free(aurb); |
596 | ||
597 | switch(errno) { | |
598 | case ETIMEDOUT: | |
599 | return USB_RET_NAK; | |
600 | case EPIPE: | |
601 | default: | |
602 | return USB_RET_STALL; | |
603 | } | |
604 | } | |
605 | ||
606 | usb_defer_packet(p, async_cancel, aurb); | |
607 | return USB_RET_ASYNC; | |
608 | } | |
609 | ||
610 | static int do_token_setup(USBDevice *dev, USBPacket *p) | |
611 | { | |
612 | USBHostDevice *s = (USBHostDevice *) dev; | |
613 | int ret = 0; | |
614 | ||
2791104c | 615 | if (p->len != 8) { |
446ab128 | 616 | return USB_RET_STALL; |
2791104c DA |
617 | } |
618 | ||
446ab128 AL |
619 | memcpy(&s->ctrl.req, p->data, 8); |
620 | s->ctrl.len = le16_to_cpu(s->ctrl.req.wLength); | |
621 | s->ctrl.offset = 0; | |
622 | s->ctrl.state = CTRL_STATE_SETUP; | |
623 | ||
624 | if (s->ctrl.req.bRequestType & USB_DIR_IN) { | |
625 | ret = usb_host_handle_control(s, p); | |
2791104c | 626 | if (ret < 0) { |
446ab128 | 627 | return ret; |
2791104c | 628 | } |
446ab128 | 629 | |
2791104c | 630 | if (ret < s->ctrl.len) { |
446ab128 | 631 | s->ctrl.len = ret; |
2791104c | 632 | } |
446ab128 AL |
633 | s->ctrl.state = CTRL_STATE_DATA; |
634 | } else { | |
2791104c | 635 | if (s->ctrl.len == 0) { |
446ab128 | 636 | s->ctrl.state = CTRL_STATE_ACK; |
2791104c | 637 | } else { |
446ab128 | 638 | s->ctrl.state = CTRL_STATE_DATA; |
2791104c | 639 | } |
446ab128 AL |
640 | } |
641 | ||
642 | return ret; | |
643 | } | |
644 | ||
645 | static int do_token_in(USBDevice *dev, USBPacket *p) | |
646 | { | |
647 | USBHostDevice *s = (USBHostDevice *) dev; | |
648 | int ret = 0; | |
649 | ||
2791104c | 650 | if (p->devep != 0) { |
446ab128 | 651 | return usb_host_handle_data(s, p); |
2791104c | 652 | } |
446ab128 AL |
653 | |
654 | switch(s->ctrl.state) { | |
655 | case CTRL_STATE_ACK: | |
656 | if (!(s->ctrl.req.bRequestType & USB_DIR_IN)) { | |
657 | ret = usb_host_handle_control(s, p); | |
2791104c | 658 | if (ret == USB_RET_ASYNC) { |
446ab128 | 659 | return USB_RET_ASYNC; |
2791104c | 660 | } |
446ab128 AL |
661 | s->ctrl.state = CTRL_STATE_IDLE; |
662 | return ret > 0 ? 0 : ret; | |
663 | } | |
664 | ||
665 | return 0; | |
666 | ||
667 | case CTRL_STATE_DATA: | |
668 | if (s->ctrl.req.bRequestType & USB_DIR_IN) { | |
669 | int len = s->ctrl.len - s->ctrl.offset; | |
2791104c | 670 | if (len > p->len) { |
446ab128 | 671 | len = p->len; |
2791104c | 672 | } |
446ab128 AL |
673 | memcpy(p->data, s->ctrl.buffer + s->ctrl.offset, len); |
674 | s->ctrl.offset += len; | |
2791104c | 675 | if (s->ctrl.offset >= s->ctrl.len) { |
446ab128 | 676 | s->ctrl.state = CTRL_STATE_ACK; |
2791104c | 677 | } |
446ab128 AL |
678 | return len; |
679 | } | |
680 | ||
681 | s->ctrl.state = CTRL_STATE_IDLE; | |
682 | return USB_RET_STALL; | |
683 | ||
684 | default: | |
685 | return USB_RET_STALL; | |
686 | } | |
687 | } | |
688 | ||
689 | static int do_token_out(USBDevice *dev, USBPacket *p) | |
690 | { | |
691 | USBHostDevice *s = (USBHostDevice *) dev; | |
692 | ||
2791104c | 693 | if (p->devep != 0) { |
446ab128 | 694 | return usb_host_handle_data(s, p); |
2791104c | 695 | } |
446ab128 AL |
696 | |
697 | switch(s->ctrl.state) { | |
698 | case CTRL_STATE_ACK: | |
699 | if (s->ctrl.req.bRequestType & USB_DIR_IN) { | |
700 | s->ctrl.state = CTRL_STATE_IDLE; | |
701 | /* transfer OK */ | |
702 | } else { | |
703 | /* ignore additional output */ | |
704 | } | |
705 | return 0; | |
706 | ||
707 | case CTRL_STATE_DATA: | |
708 | if (!(s->ctrl.req.bRequestType & USB_DIR_IN)) { | |
709 | int len = s->ctrl.len - s->ctrl.offset; | |
2791104c | 710 | if (len > p->len) { |
446ab128 | 711 | len = p->len; |
2791104c | 712 | } |
446ab128 AL |
713 | memcpy(s->ctrl.buffer + s->ctrl.offset, p->data, len); |
714 | s->ctrl.offset += len; | |
2791104c | 715 | if (s->ctrl.offset >= s->ctrl.len) { |
446ab128 | 716 | s->ctrl.state = CTRL_STATE_ACK; |
2791104c | 717 | } |
446ab128 AL |
718 | return len; |
719 | } | |
720 | ||
721 | s->ctrl.state = CTRL_STATE_IDLE; | |
722 | return USB_RET_STALL; | |
723 | ||
724 | default: | |
725 | return USB_RET_STALL; | |
726 | } | |
727 | } | |
728 | ||
729 | /* | |
730 | * Packet handler. | |
731 | * Called by the HC (host controller). | |
732 | * | |
733 | * Returns length of the transaction or one of the USB_RET_XXX codes. | |
734 | */ | |
d9cf1578 | 735 | static int usb_host_handle_packet(USBDevice *s, USBPacket *p) |
446ab128 AL |
736 | { |
737 | switch(p->pid) { | |
738 | case USB_MSG_ATTACH: | |
739 | s->state = USB_STATE_ATTACHED; | |
740 | return 0; | |
741 | ||
742 | case USB_MSG_DETACH: | |
743 | s->state = USB_STATE_NOTATTACHED; | |
744 | return 0; | |
745 | ||
746 | case USB_MSG_RESET: | |
747 | s->remote_wakeup = 0; | |
748 | s->addr = 0; | |
749 | s->state = USB_STATE_DEFAULT; | |
806b6024 | 750 | s->info->handle_reset(s); |
446ab128 AL |
751 | return 0; |
752 | } | |
753 | ||
754 | /* Rest of the PIDs must match our address */ | |
2791104c | 755 | if (s->state < USB_STATE_DEFAULT || p->devaddr != s->addr) { |
446ab128 | 756 | return USB_RET_NODEV; |
2791104c | 757 | } |
446ab128 AL |
758 | |
759 | switch (p->pid) { | |
760 | case USB_TOKEN_SETUP: | |
761 | return do_token_setup(s, p); | |
762 | ||
763 | case USB_TOKEN_IN: | |
764 | return do_token_in(s, p); | |
765 | ||
766 | case USB_TOKEN_OUT: | |
767 | return do_token_out(s, p); | |
2791104c | 768 | |
446ab128 AL |
769 | default: |
770 | return USB_RET_STALL; | |
771 | } | |
772 | } | |
773 | ||
b9dc033c AZ |
774 | /* returns 1 on problem encountered or 0 for success */ |
775 | static int usb_linux_update_endp_table(USBHostDevice *s) | |
776 | { | |
777 | uint8_t *descriptors; | |
778 | uint8_t devep, type, configuration, alt_interface; | |
e41b3910 | 779 | struct usb_ctrltransfer ct; |
b9dc033c AZ |
780 | int interface, ret, length, i; |
781 | ||
782 | ct.bRequestType = USB_DIR_IN; | |
783 | ct.bRequest = USB_REQ_GET_CONFIGURATION; | |
784 | ct.wValue = 0; | |
785 | ct.wIndex = 0; | |
786 | ct.wLength = 1; | |
787 | ct.data = &configuration; | |
788 | ct.timeout = 50; | |
789 | ||
790 | ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct); | |
791 | if (ret < 0) { | |
792 | perror("usb_linux_update_endp_table"); | |
793 | return 1; | |
794 | } | |
795 | ||
796 | /* in address state */ | |
2791104c | 797 | if (configuration == 0) { |
b9dc033c | 798 | return 1; |
2791104c | 799 | } |
b9dc033c AZ |
800 | |
801 | /* get the desired configuration, interface, and endpoint descriptors | |
802 | * from device description */ | |
803 | descriptors = &s->descr[18]; | |
804 | length = s->descr_len - 18; | |
805 | i = 0; | |
806 | ||
807 | if (descriptors[i + 1] != USB_DT_CONFIG || | |
808 | descriptors[i + 5] != configuration) { | |
d0f2c4c6 | 809 | DPRINTF("invalid descriptor data - configuration\n"); |
b9dc033c AZ |
810 | return 1; |
811 | } | |
812 | i += descriptors[i]; | |
813 | ||
814 | while (i < length) { | |
815 | if (descriptors[i + 1] != USB_DT_INTERFACE || | |
816 | (descriptors[i + 1] == USB_DT_INTERFACE && | |
817 | descriptors[i + 4] == 0)) { | |
818 | i += descriptors[i]; | |
819 | continue; | |
820 | } | |
821 | ||
822 | interface = descriptors[i + 2]; | |
823 | ||
824 | ct.bRequestType = USB_DIR_IN | USB_RECIP_INTERFACE; | |
825 | ct.bRequest = USB_REQ_GET_INTERFACE; | |
826 | ct.wValue = 0; | |
827 | ct.wIndex = interface; | |
828 | ct.wLength = 1; | |
829 | ct.data = &alt_interface; | |
830 | ct.timeout = 50; | |
831 | ||
832 | ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct); | |
833 | if (ret < 0) { | |
d55ebf55 | 834 | alt_interface = interface; |
b9dc033c AZ |
835 | } |
836 | ||
837 | /* the current interface descriptor is the active interface | |
838 | * and has endpoints */ | |
839 | if (descriptors[i + 3] != alt_interface) { | |
840 | i += descriptors[i]; | |
841 | continue; | |
842 | } | |
843 | ||
844 | /* advance to the endpoints */ | |
2791104c | 845 | while (i < length && descriptors[i +1] != USB_DT_ENDPOINT) { |
b9dc033c | 846 | i += descriptors[i]; |
2791104c | 847 | } |
b9dc033c AZ |
848 | |
849 | if (i >= length) | |
850 | break; | |
851 | ||
852 | while (i < length) { | |
2791104c | 853 | if (descriptors[i + 1] != USB_DT_ENDPOINT) { |
b9dc033c | 854 | break; |
2791104c | 855 | } |
b9dc033c AZ |
856 | |
857 | devep = descriptors[i + 2]; | |
858 | switch (descriptors[i + 3] & 0x3) { | |
859 | case 0x00: | |
860 | type = USBDEVFS_URB_TYPE_CONTROL; | |
861 | break; | |
862 | case 0x01: | |
863 | type = USBDEVFS_URB_TYPE_ISO; | |
864 | break; | |
865 | case 0x02: | |
866 | type = USBDEVFS_URB_TYPE_BULK; | |
867 | break; | |
868 | case 0x03: | |
869 | type = USBDEVFS_URB_TYPE_INTERRUPT; | |
870 | break; | |
ddbda432 AL |
871 | default: |
872 | DPRINTF("usb_host: malformed endpoint type\n"); | |
873 | type = USBDEVFS_URB_TYPE_BULK; | |
b9dc033c AZ |
874 | } |
875 | s->endp_table[(devep & 0xf) - 1].type = type; | |
64838171 | 876 | s->endp_table[(devep & 0xf) - 1].halted = 0; |
b9dc033c AZ |
877 | |
878 | i += descriptors[i]; | |
879 | } | |
880 | } | |
881 | return 0; | |
882 | } | |
883 | ||
26a9e82a GH |
884 | static int usb_host_open(USBHostDevice *dev, int bus_num, |
885 | int addr, const char *prod_name) | |
bb36d470 | 886 | { |
b9dc033c | 887 | int fd = -1, ret; |
bb36d470 | 888 | struct usbdevfs_connectinfo ci; |
a594cfbf | 889 | char buf[1024]; |
1f3870ab | 890 | |
2791104c | 891 | if (dev->fd != -1) { |
26a9e82a | 892 | goto fail; |
2791104c | 893 | } |
64838171 | 894 | printf("husb: open device %d.%d\n", bus_num, addr); |
3b46e624 | 895 | |
0f431527 AL |
896 | if (!usb_host_device_path) { |
897 | perror("husb: USB Host Device Path not set"); | |
898 | goto fail; | |
899 | } | |
900 | snprintf(buf, sizeof(buf), "%s/%03d/%03d", usb_host_device_path, | |
a594cfbf | 901 | bus_num, addr); |
b9dc033c | 902 | fd = open(buf, O_RDWR | O_NONBLOCK); |
bb36d470 | 903 | if (fd < 0) { |
a594cfbf | 904 | perror(buf); |
1f3870ab | 905 | goto fail; |
bb36d470 | 906 | } |
d0f2c4c6 | 907 | DPRINTF("husb: opened %s\n", buf); |
bb36d470 | 908 | |
806b6024 GH |
909 | dev->bus_num = bus_num; |
910 | dev->addr = addr; | |
22f84e73 | 911 | dev->fd = fd; |
806b6024 | 912 | |
b9dc033c AZ |
913 | /* read the device description */ |
914 | dev->descr_len = read(fd, dev->descr, sizeof(dev->descr)); | |
915 | if (dev->descr_len <= 0) { | |
64838171 | 916 | perror("husb: reading device data failed"); |
bb36d470 FB |
917 | goto fail; |
918 | } | |
3b46e624 | 919 | |
b9dc033c | 920 | #ifdef DEBUG |
868bfe2b | 921 | { |
b9dc033c AZ |
922 | int x; |
923 | printf("=== begin dumping device descriptor data ===\n"); | |
2791104c | 924 | for (x = 0; x < dev->descr_len; x++) { |
b9dc033c | 925 | printf("%02x ", dev->descr[x]); |
2791104c | 926 | } |
b9dc033c | 927 | printf("\n=== end dumping device descriptor data ===\n"); |
bb36d470 | 928 | } |
a594cfbf FB |
929 | #endif |
930 | ||
b9dc033c | 931 | |
2791104c DA |
932 | /* |
933 | * Initial configuration is -1 which makes us claim first | |
446ab128 | 934 | * available config. We used to start with 1, which does not |
2791104c | 935 | * always work. I've seen devices where first config starts |
446ab128 AL |
936 | * with 2. |
937 | */ | |
2791104c | 938 | if (!usb_host_claim_interfaces(dev, -1)) { |
b9dc033c | 939 | goto fail; |
2791104c | 940 | } |
bb36d470 FB |
941 | |
942 | ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci); | |
943 | if (ret < 0) { | |
046833ea | 944 | perror("usb_host_device_open: USBDEVFS_CONNECTINFO"); |
bb36d470 FB |
945 | goto fail; |
946 | } | |
947 | ||
64838171 | 948 | printf("husb: grabbed usb device %d.%d\n", bus_num, addr); |
bb36d470 | 949 | |
b9dc033c | 950 | ret = usb_linux_update_endp_table(dev); |
2791104c | 951 | if (ret) { |
bb36d470 | 952 | goto fail; |
2791104c | 953 | } |
b9dc033c | 954 | |
2791104c | 955 | if (ci.slow) { |
bb36d470 | 956 | dev->dev.speed = USB_SPEED_LOW; |
2791104c | 957 | } else { |
bb36d470 | 958 | dev->dev.speed = USB_SPEED_HIGH; |
2791104c | 959 | } |
bb36d470 | 960 | |
2791104c | 961 | if (!prod_name || prod_name[0] == '\0') { |
0fe6d12e | 962 | snprintf(dev->dev.product_desc, sizeof(dev->dev.product_desc), |
4b096fc9 | 963 | "host:%d.%d", bus_num, addr); |
2791104c | 964 | } else { |
0fe6d12e | 965 | pstrcpy(dev->dev.product_desc, sizeof(dev->dev.product_desc), |
4b096fc9 | 966 | prod_name); |
2791104c | 967 | } |
1f6e24e7 | 968 | |
64838171 AL |
969 | /* USB devio uses 'write' flag to check for async completions */ |
970 | qemu_set_fd_handler(dev->fd, NULL, async_complete, dev); | |
1f3870ab | 971 | |
26a9e82a GH |
972 | usb_device_attach(&dev->dev); |
973 | return 0; | |
4b096fc9 | 974 | |
b9dc033c | 975 | fail: |
26a9e82a | 976 | dev->fd = -1; |
2791104c | 977 | if (fd != -1) { |
806b6024 | 978 | close(fd); |
2791104c | 979 | } |
26a9e82a GH |
980 | return -1; |
981 | } | |
982 | ||
983 | static int usb_host_close(USBHostDevice *dev) | |
984 | { | |
2791104c | 985 | if (dev->fd == -1) { |
26a9e82a | 986 | return -1; |
2791104c | 987 | } |
26a9e82a GH |
988 | |
989 | qemu_set_fd_handler(dev->fd, NULL, NULL, NULL); | |
990 | dev->closing = 1; | |
991 | async_complete(dev); | |
992 | dev->closing = 0; | |
993 | usb_device_detach(&dev->dev); | |
994 | close(dev->fd); | |
995 | dev->fd = -1; | |
996 | return 0; | |
997 | } | |
998 | ||
999 | static int usb_host_initfn(USBDevice *dev) | |
1000 | { | |
1001 | USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev); | |
1002 | ||
1003 | dev->auto_attach = 0; | |
1004 | s->fd = -1; | |
1005 | QTAILQ_INSERT_TAIL(&hostdevs, s, next); | |
1006 | usb_host_auto_check(NULL); | |
1007 | return 0; | |
a594cfbf | 1008 | } |
bb36d470 | 1009 | |
806b6024 | 1010 | static struct USBDeviceInfo usb_host_dev_info = { |
06384698 | 1011 | .product_desc = "USB Host Device", |
556cd098 | 1012 | .qdev.name = "usb-host", |
806b6024 GH |
1013 | .qdev.size = sizeof(USBHostDevice), |
1014 | .init = usb_host_initfn, | |
1015 | .handle_packet = usb_host_handle_packet, | |
1016 | .handle_reset = usb_host_handle_reset, | |
806b6024 | 1017 | .handle_destroy = usb_host_handle_destroy, |
26a9e82a GH |
1018 | .usbdevice_name = "host", |
1019 | .usbdevice_init = usb_host_device_open, | |
1020 | .qdev.props = (Property[]) { | |
1021 | DEFINE_PROP_UINT32("hostbus", USBHostDevice, match.bus_num, 0), | |
1022 | DEFINE_PROP_UINT32("hostaddr", USBHostDevice, match.addr, 0), | |
1023 | DEFINE_PROP_HEX32("vendorid", USBHostDevice, match.vendor_id, 0), | |
1024 | DEFINE_PROP_HEX32("productid", USBHostDevice, match.product_id, 0), | |
1025 | DEFINE_PROP_END_OF_LIST(), | |
1026 | }, | |
806b6024 GH |
1027 | }; |
1028 | ||
1029 | static void usb_host_register_devices(void) | |
1030 | { | |
1031 | usb_qdev_register(&usb_host_dev_info); | |
1032 | } | |
1033 | device_init(usb_host_register_devices) | |
1034 | ||
4b096fc9 AL |
1035 | USBDevice *usb_host_device_open(const char *devname) |
1036 | { | |
0745eb1e | 1037 | struct USBAutoFilter filter; |
26a9e82a | 1038 | USBDevice *dev; |
26a9e82a GH |
1039 | char *p; |
1040 | ||
556cd098 | 1041 | dev = usb_create(NULL /* FIXME */, "usb-host"); |
4b096fc9 | 1042 | |
5d0c5750 | 1043 | if (strstr(devname, "auto:")) { |
2791104c | 1044 | if (parse_filter(devname, &filter) < 0) { |
26a9e82a | 1045 | goto fail; |
2791104c | 1046 | } |
26a9e82a GH |
1047 | } else { |
1048 | if ((p = strchr(devname, '.'))) { | |
0745eb1e MA |
1049 | filter.bus_num = strtoul(devname, NULL, 0); |
1050 | filter.addr = strtoul(p + 1, NULL, 0); | |
1051 | filter.vendor_id = 0; | |
1052 | filter.product_id = 0; | |
26a9e82a | 1053 | } else if ((p = strchr(devname, ':'))) { |
0745eb1e MA |
1054 | filter.bus_num = 0; |
1055 | filter.addr = 0; | |
26a9e82a | 1056 | filter.vendor_id = strtoul(devname, NULL, 16); |
0745eb1e | 1057 | filter.product_id = strtoul(p + 1, NULL, 16); |
26a9e82a GH |
1058 | } else { |
1059 | goto fail; | |
1060 | } | |
5d0c5750 | 1061 | } |
4b096fc9 | 1062 | |
0745eb1e MA |
1063 | qdev_prop_set_uint32(&dev->qdev, "hostbus", filter.bus_num); |
1064 | qdev_prop_set_uint32(&dev->qdev, "hostaddr", filter.addr); | |
26a9e82a GH |
1065 | qdev_prop_set_uint32(&dev->qdev, "vendorid", filter.vendor_id); |
1066 | qdev_prop_set_uint32(&dev->qdev, "productid", filter.product_id); | |
beb6f0de | 1067 | qdev_init_nofail(&dev->qdev); |
26a9e82a | 1068 | return dev; |
5d0c5750 | 1069 | |
26a9e82a GH |
1070 | fail: |
1071 | qdev_free(&dev->qdev); | |
1072 | return NULL; | |
4b096fc9 | 1073 | } |
5d0c5750 AL |
1074 | |
1075 | int usb_host_device_close(const char *devname) | |
1076 | { | |
26a9e82a | 1077 | #if 0 |
5d0c5750 AL |
1078 | char product_name[PRODUCT_NAME_SZ]; |
1079 | int bus_num, addr; | |
1080 | USBHostDevice *s; | |
1081 | ||
2791104c | 1082 | if (strstr(devname, "auto:")) { |
5d0c5750 | 1083 | return usb_host_auto_del(devname); |
2791104c DA |
1084 | } |
1085 | if (usb_host_find_device(&bus_num, &addr, product_name, | |
1086 | sizeof(product_name), devname) < 0) { | |
5d0c5750 | 1087 | return -1; |
2791104c | 1088 | } |
5d0c5750 AL |
1089 | s = hostdev_find(bus_num, addr); |
1090 | if (s) { | |
a5d2f727 | 1091 | usb_device_delete_addr(s->bus_num, s->dev.addr); |
5d0c5750 AL |
1092 | return 0; |
1093 | } | |
26a9e82a | 1094 | #endif |
5d0c5750 AL |
1095 | |
1096 | return -1; | |
1097 | } | |
a5d2f727 | 1098 | |
a594cfbf | 1099 | static int get_tag_value(char *buf, int buf_size, |
5fafdf24 | 1100 | const char *str, const char *tag, |
a594cfbf FB |
1101 | const char *stopchars) |
1102 | { | |
1103 | const char *p; | |
1104 | char *q; | |
1105 | p = strstr(str, tag); | |
2791104c | 1106 | if (!p) { |
a594cfbf | 1107 | return -1; |
2791104c | 1108 | } |
a594cfbf | 1109 | p += strlen(tag); |
2791104c | 1110 | while (qemu_isspace(*p)) { |
a594cfbf | 1111 | p++; |
2791104c | 1112 | } |
a594cfbf FB |
1113 | q = buf; |
1114 | while (*p != '\0' && !strchr(stopchars, *p)) { | |
2791104c | 1115 | if ((q - buf) < (buf_size - 1)) { |
a594cfbf | 1116 | *q++ = *p; |
2791104c | 1117 | } |
a594cfbf FB |
1118 | p++; |
1119 | } | |
1120 | *q = '\0'; | |
1121 | return q - buf; | |
bb36d470 FB |
1122 | } |
1123 | ||
0f431527 AL |
1124 | /* |
1125 | * Use /proc/bus/usb/devices or /dev/bus/usb/devices file to determine | |
1126 | * host's USB devices. This is legacy support since many distributions | |
1127 | * are moving to /sys/bus/usb | |
1128 | */ | |
1129 | static int usb_host_scan_dev(void *opaque, USBScanFunc *func) | |
bb36d470 | 1130 | { |
660f11be | 1131 | FILE *f = NULL; |
a594cfbf | 1132 | char line[1024]; |
bb36d470 | 1133 | char buf[1024]; |
a594cfbf | 1134 | int bus_num, addr, speed, device_count, class_id, product_id, vendor_id; |
a594cfbf | 1135 | char product_name[512]; |
0f431527 | 1136 | int ret = 0; |
3b46e624 | 1137 | |
0f431527 AL |
1138 | if (!usb_host_device_path) { |
1139 | perror("husb: USB Host Device Path not set"); | |
1140 | goto the_end; | |
1141 | } | |
1142 | snprintf(line, sizeof(line), "%s/devices", usb_host_device_path); | |
1143 | f = fopen(line, "r"); | |
a594cfbf | 1144 | if (!f) { |
0f431527 AL |
1145 | perror("husb: cannot open devices file"); |
1146 | goto the_end; | |
a594cfbf | 1147 | } |
0f431527 | 1148 | |
a594cfbf FB |
1149 | device_count = 0; |
1150 | bus_num = addr = speed = class_id = product_id = vendor_id = 0; | |
bb36d470 | 1151 | for(;;) { |
2791104c | 1152 | if (fgets(line, sizeof(line), f) == NULL) { |
bb36d470 | 1153 | break; |
2791104c DA |
1154 | } |
1155 | if (strlen(line) > 0) { | |
a594cfbf | 1156 | line[strlen(line) - 1] = '\0'; |
2791104c | 1157 | } |
a594cfbf | 1158 | if (line[0] == 'T' && line[1] == ':') { |
38ca0f6d PB |
1159 | if (device_count && (vendor_id || product_id)) { |
1160 | /* New device. Add the previously discovered device. */ | |
5fafdf24 | 1161 | ret = func(opaque, bus_num, addr, class_id, vendor_id, |
a594cfbf | 1162 | product_id, product_name, speed); |
2791104c | 1163 | if (ret) { |
a594cfbf | 1164 | goto the_end; |
2791104c | 1165 | } |
a594cfbf | 1166 | } |
2791104c | 1167 | if (get_tag_value(buf, sizeof(buf), line, "Bus=", " ") < 0) { |
a594cfbf | 1168 | goto fail; |
2791104c | 1169 | } |
a594cfbf | 1170 | bus_num = atoi(buf); |
2791104c | 1171 | if (get_tag_value(buf, sizeof(buf), line, "Dev#=", " ") < 0) { |
a594cfbf | 1172 | goto fail; |
2791104c | 1173 | } |
a594cfbf | 1174 | addr = atoi(buf); |
2791104c | 1175 | if (get_tag_value(buf, sizeof(buf), line, "Spd=", " ") < 0) { |
a594cfbf | 1176 | goto fail; |
2791104c DA |
1177 | } |
1178 | if (!strcmp(buf, "480")) { | |
a594cfbf | 1179 | speed = USB_SPEED_HIGH; |
2791104c | 1180 | } else if (!strcmp(buf, "1.5")) { |
a594cfbf | 1181 | speed = USB_SPEED_LOW; |
2791104c | 1182 | } else { |
a594cfbf | 1183 | speed = USB_SPEED_FULL; |
2791104c | 1184 | } |
a594cfbf FB |
1185 | product_name[0] = '\0'; |
1186 | class_id = 0xff; | |
1187 | device_count++; | |
1188 | product_id = 0; | |
1189 | vendor_id = 0; | |
1190 | } else if (line[0] == 'P' && line[1] == ':') { | |
2791104c | 1191 | if (get_tag_value(buf, sizeof(buf), line, "Vendor=", " ") < 0) { |
a594cfbf | 1192 | goto fail; |
2791104c | 1193 | } |
a594cfbf | 1194 | vendor_id = strtoul(buf, NULL, 16); |
2791104c | 1195 | if (get_tag_value(buf, sizeof(buf), line, "ProdID=", " ") < 0) { |
a594cfbf | 1196 | goto fail; |
2791104c | 1197 | } |
a594cfbf FB |
1198 | product_id = strtoul(buf, NULL, 16); |
1199 | } else if (line[0] == 'S' && line[1] == ':') { | |
2791104c | 1200 | if (get_tag_value(buf, sizeof(buf), line, "Product=", "") < 0) { |
a594cfbf | 1201 | goto fail; |
2791104c | 1202 | } |
a594cfbf FB |
1203 | pstrcpy(product_name, sizeof(product_name), buf); |
1204 | } else if (line[0] == 'D' && line[1] == ':') { | |
2791104c | 1205 | if (get_tag_value(buf, sizeof(buf), line, "Cls=", " (") < 0) { |
a594cfbf | 1206 | goto fail; |
2791104c | 1207 | } |
a594cfbf | 1208 | class_id = strtoul(buf, NULL, 16); |
bb36d470 | 1209 | } |
a594cfbf FB |
1210 | fail: ; |
1211 | } | |
38ca0f6d PB |
1212 | if (device_count && (vendor_id || product_id)) { |
1213 | /* Add the last device. */ | |
5fafdf24 | 1214 | ret = func(opaque, bus_num, addr, class_id, vendor_id, |
a594cfbf | 1215 | product_id, product_name, speed); |
bb36d470 | 1216 | } |
a594cfbf | 1217 | the_end: |
2791104c | 1218 | if (f) { |
0f431527 | 1219 | fclose(f); |
2791104c | 1220 | } |
0f431527 AL |
1221 | return ret; |
1222 | } | |
1223 | ||
1224 | /* | |
1225 | * Read sys file-system device file | |
1226 | * | |
1227 | * @line address of buffer to put file contents in | |
1228 | * @line_size size of line | |
1229 | * @device_file path to device file (printf format string) | |
1230 | * @device_name device being opened (inserted into device_file) | |
1231 | * | |
1232 | * @return 0 failed, 1 succeeded ('line' contains data) | |
1233 | */ | |
2791104c DA |
1234 | static int usb_host_read_file(char *line, size_t line_size, |
1235 | const char *device_file, const char *device_name) | |
0f431527 AL |
1236 | { |
1237 | FILE *f; | |
1238 | int ret = 0; | |
1239 | char filename[PATH_MAX]; | |
1240 | ||
b4e237aa BS |
1241 | snprintf(filename, PATH_MAX, USBSYSBUS_PATH "/devices/%s/%s", device_name, |
1242 | device_file); | |
0f431527 AL |
1243 | f = fopen(filename, "r"); |
1244 | if (f) { | |
9f99cee7 | 1245 | ret = fgets(line, line_size, f) != NULL; |
0f431527 | 1246 | fclose(f); |
0f431527 AL |
1247 | } |
1248 | ||
1249 | return ret; | |
1250 | } | |
1251 | ||
1252 | /* | |
1253 | * Use /sys/bus/usb/devices/ directory to determine host's USB | |
1254 | * devices. | |
1255 | * | |
1256 | * This code is based on Robert Schiele's original patches posted to | |
1257 | * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950 | |
1258 | */ | |
1259 | static int usb_host_scan_sys(void *opaque, USBScanFunc *func) | |
1260 | { | |
660f11be | 1261 | DIR *dir = NULL; |
0f431527 AL |
1262 | char line[1024]; |
1263 | int bus_num, addr, speed, class_id, product_id, vendor_id; | |
1264 | int ret = 0; | |
1265 | char product_name[512]; | |
1266 | struct dirent *de; | |
1267 | ||
1268 | dir = opendir(USBSYSBUS_PATH "/devices"); | |
1269 | if (!dir) { | |
1270 | perror("husb: cannot open devices directory"); | |
1271 | goto the_end; | |
1272 | } | |
1273 | ||
1274 | while ((de = readdir(dir))) { | |
1275 | if (de->d_name[0] != '.' && !strchr(de->d_name, ':')) { | |
1276 | char *tmpstr = de->d_name; | |
2791104c | 1277 | if (!strncmp(de->d_name, "usb", 3)) { |
0f431527 | 1278 | tmpstr += 3; |
2791104c | 1279 | } |
0f431527 AL |
1280 | bus_num = atoi(tmpstr); |
1281 | ||
2791104c | 1282 | if (!usb_host_read_file(line, sizeof(line), "devnum", de->d_name)) { |
0f431527 | 1283 | goto the_end; |
2791104c DA |
1284 | } |
1285 | if (sscanf(line, "%d", &addr) != 1) { | |
0f431527 | 1286 | goto the_end; |
2791104c | 1287 | } |
b4e237aa | 1288 | if (!usb_host_read_file(line, sizeof(line), "bDeviceClass", |
2791104c | 1289 | de->d_name)) { |
0f431527 | 1290 | goto the_end; |
2791104c DA |
1291 | } |
1292 | if (sscanf(line, "%x", &class_id) != 1) { | |
0f431527 | 1293 | goto the_end; |
2791104c | 1294 | } |
0f431527 | 1295 | |
2791104c DA |
1296 | if (!usb_host_read_file(line, sizeof(line), "idVendor", |
1297 | de->d_name)) { | |
0f431527 | 1298 | goto the_end; |
2791104c DA |
1299 | } |
1300 | if (sscanf(line, "%x", &vendor_id) != 1) { | |
0f431527 | 1301 | goto the_end; |
2791104c | 1302 | } |
b4e237aa | 1303 | if (!usb_host_read_file(line, sizeof(line), "idProduct", |
2791104c | 1304 | de->d_name)) { |
0f431527 | 1305 | goto the_end; |
2791104c DA |
1306 | } |
1307 | if (sscanf(line, "%x", &product_id) != 1) { | |
0f431527 | 1308 | goto the_end; |
2791104c | 1309 | } |
b4e237aa BS |
1310 | if (!usb_host_read_file(line, sizeof(line), "product", |
1311 | de->d_name)) { | |
0f431527 AL |
1312 | *product_name = 0; |
1313 | } else { | |
2791104c | 1314 | if (strlen(line) > 0) { |
0f431527 | 1315 | line[strlen(line) - 1] = '\0'; |
2791104c | 1316 | } |
0f431527 AL |
1317 | pstrcpy(product_name, sizeof(product_name), line); |
1318 | } | |
1319 | ||
2791104c | 1320 | if (!usb_host_read_file(line, sizeof(line), "speed", de->d_name)) { |
0f431527 | 1321 | goto the_end; |
2791104c DA |
1322 | } |
1323 | if (!strcmp(line, "480\n")) { | |
0f431527 | 1324 | speed = USB_SPEED_HIGH; |
2791104c | 1325 | } else if (!strcmp(line, "1.5\n")) { |
0f431527 | 1326 | speed = USB_SPEED_LOW; |
2791104c | 1327 | } else { |
0f431527 | 1328 | speed = USB_SPEED_FULL; |
2791104c | 1329 | } |
0f431527 AL |
1330 | |
1331 | ret = func(opaque, bus_num, addr, class_id, vendor_id, | |
1332 | product_id, product_name, speed); | |
2791104c | 1333 | if (ret) { |
0f431527 | 1334 | goto the_end; |
2791104c | 1335 | } |
0f431527 AL |
1336 | } |
1337 | } | |
1338 | the_end: | |
2791104c | 1339 | if (dir) { |
0f431527 | 1340 | closedir(dir); |
2791104c | 1341 | } |
0f431527 AL |
1342 | return ret; |
1343 | } | |
1344 | ||
1345 | /* | |
1346 | * Determine how to access the host's USB devices and call the | |
1347 | * specific support function. | |
1348 | */ | |
1349 | static int usb_host_scan(void *opaque, USBScanFunc *func) | |
1350 | { | |
376253ec | 1351 | Monitor *mon = cur_mon; |
660f11be BS |
1352 | FILE *f = NULL; |
1353 | DIR *dir = NULL; | |
0f431527 | 1354 | int ret = 0; |
0f431527 AL |
1355 | const char *fs_type[] = {"unknown", "proc", "dev", "sys"}; |
1356 | char devpath[PATH_MAX]; | |
1357 | ||
1358 | /* only check the host once */ | |
1359 | if (!usb_fs_type) { | |
55496240 MM |
1360 | dir = opendir(USBSYSBUS_PATH "/devices"); |
1361 | if (dir) { | |
1362 | /* devices found in /dev/bus/usb/ (yes - not a mistake!) */ | |
1363 | strcpy(devpath, USBDEVBUS_PATH); | |
1364 | usb_fs_type = USB_FS_SYS; | |
1365 | closedir(dir); | |
d0f2c4c6 | 1366 | DPRINTF(USBDBG_DEVOPENED, USBSYSBUS_PATH); |
55496240 MM |
1367 | goto found_devices; |
1368 | } | |
0f431527 AL |
1369 | f = fopen(USBPROCBUS_PATH "/devices", "r"); |
1370 | if (f) { | |
1371 | /* devices found in /proc/bus/usb/ */ | |
1372 | strcpy(devpath, USBPROCBUS_PATH); | |
1373 | usb_fs_type = USB_FS_PROC; | |
1374 | fclose(f); | |
d0f2c4c6 | 1375 | DPRINTF(USBDBG_DEVOPENED, USBPROCBUS_PATH); |
f16a0db3 | 1376 | goto found_devices; |
0f431527 AL |
1377 | } |
1378 | /* try additional methods if an access method hasn't been found yet */ | |
1379 | f = fopen(USBDEVBUS_PATH "/devices", "r"); | |
f16a0db3 | 1380 | if (f) { |
0f431527 AL |
1381 | /* devices found in /dev/bus/usb/ */ |
1382 | strcpy(devpath, USBDEVBUS_PATH); | |
1383 | usb_fs_type = USB_FS_DEV; | |
1384 | fclose(f); | |
d0f2c4c6 | 1385 | DPRINTF(USBDBG_DEVOPENED, USBDEVBUS_PATH); |
f16a0db3 | 1386 | goto found_devices; |
0f431527 | 1387 | } |
f16a0db3 | 1388 | found_devices: |
22babebb | 1389 | if (!usb_fs_type) { |
2791104c | 1390 | if (mon) { |
eba6fe87 | 1391 | monitor_printf(mon, "husb: unable to access USB devices\n"); |
2791104c | 1392 | } |
f16a0db3 | 1393 | return -ENOENT; |
0f431527 AL |
1394 | } |
1395 | ||
1396 | /* the module setting (used later for opening devices) */ | |
1397 | usb_host_device_path = qemu_mallocz(strlen(devpath)+1); | |
1eec614b | 1398 | strcpy(usb_host_device_path, devpath); |
2791104c | 1399 | if (mon) { |
eba6fe87 GH |
1400 | monitor_printf(mon, "husb: using %s file-system with %s\n", |
1401 | fs_type[usb_fs_type], usb_host_device_path); | |
2791104c | 1402 | } |
0f431527 AL |
1403 | } |
1404 | ||
1405 | switch (usb_fs_type) { | |
1406 | case USB_FS_PROC: | |
1407 | case USB_FS_DEV: | |
1408 | ret = usb_host_scan_dev(opaque, func); | |
1409 | break; | |
1410 | case USB_FS_SYS: | |
1411 | ret = usb_host_scan_sys(opaque, func); | |
1412 | break; | |
f16a0db3 AL |
1413 | default: |
1414 | ret = -EINVAL; | |
1415 | break; | |
0f431527 | 1416 | } |
a594cfbf | 1417 | return ret; |
bb36d470 FB |
1418 | } |
1419 | ||
4b096fc9 | 1420 | static QEMUTimer *usb_auto_timer; |
4b096fc9 AL |
1421 | |
1422 | static int usb_host_auto_scan(void *opaque, int bus_num, int addr, | |
26a9e82a GH |
1423 | int class_id, int vendor_id, int product_id, |
1424 | const char *product_name, int speed) | |
4b096fc9 AL |
1425 | { |
1426 | struct USBAutoFilter *f; | |
26a9e82a | 1427 | struct USBHostDevice *s; |
4b096fc9 AL |
1428 | |
1429 | /* Ignore hubs */ | |
1430 | if (class_id == 9) | |
1431 | return 0; | |
1432 | ||
26a9e82a GH |
1433 | QTAILQ_FOREACH(s, &hostdevs, next) { |
1434 | f = &s->match; | |
1435 | ||
2791104c | 1436 | if (f->bus_num > 0 && f->bus_num != bus_num) { |
4b096fc9 | 1437 | continue; |
2791104c DA |
1438 | } |
1439 | if (f->addr > 0 && f->addr != addr) { | |
4b096fc9 | 1440 | continue; |
2791104c | 1441 | } |
4b096fc9 | 1442 | |
2791104c | 1443 | if (f->vendor_id > 0 && f->vendor_id != vendor_id) { |
4b096fc9 | 1444 | continue; |
2791104c | 1445 | } |
4b096fc9 | 1446 | |
2791104c | 1447 | if (f->product_id > 0 && f->product_id != product_id) { |
4b096fc9 | 1448 | continue; |
2791104c | 1449 | } |
4b096fc9 AL |
1450 | /* We got a match */ |
1451 | ||
33e66b86 | 1452 | /* Already attached ? */ |
2791104c | 1453 | if (s->fd != -1) { |
4b096fc9 | 1454 | return 0; |
2791104c | 1455 | } |
d0f2c4c6 | 1456 | DPRINTF("husb: auto open: bus_num %d addr %d\n", bus_num, addr); |
4b096fc9 | 1457 | |
2791104c | 1458 | usb_host_open(s, bus_num, addr, product_name); |
4b096fc9 AL |
1459 | } |
1460 | ||
1461 | return 0; | |
1462 | } | |
1463 | ||
26a9e82a | 1464 | static void usb_host_auto_check(void *unused) |
4b096fc9 | 1465 | { |
26a9e82a GH |
1466 | struct USBHostDevice *s; |
1467 | int unconnected = 0; | |
1468 | ||
4b096fc9 | 1469 | usb_host_scan(NULL, usb_host_auto_scan); |
26a9e82a GH |
1470 | |
1471 | QTAILQ_FOREACH(s, &hostdevs, next) { | |
2791104c | 1472 | if (s->fd == -1) { |
26a9e82a | 1473 | unconnected++; |
2791104c | 1474 | } |
26a9e82a GH |
1475 | } |
1476 | ||
1477 | if (unconnected == 0) { | |
1478 | /* nothing to watch */ | |
2791104c | 1479 | if (usb_auto_timer) { |
26a9e82a | 1480 | qemu_del_timer(usb_auto_timer); |
2791104c | 1481 | } |
26a9e82a GH |
1482 | return; |
1483 | } | |
1484 | ||
1485 | if (!usb_auto_timer) { | |
1486 | usb_auto_timer = qemu_new_timer(rt_clock, usb_host_auto_check, NULL); | |
2791104c | 1487 | if (!usb_auto_timer) { |
26a9e82a | 1488 | return; |
2791104c | 1489 | } |
26a9e82a | 1490 | } |
4b096fc9 AL |
1491 | qemu_mod_timer(usb_auto_timer, qemu_get_clock(rt_clock) + 2000); |
1492 | } | |
1493 | ||
1494 | /* | |
5d0c5750 AL |
1495 | * Autoconnect filter |
1496 | * Format: | |
1497 | * auto:bus:dev[:vid:pid] | |
1498 | * auto:bus.dev[:vid:pid] | |
1499 | * | |
1500 | * bus - bus number (dec, * means any) | |
1501 | * dev - device number (dec, * means any) | |
1502 | * vid - vendor id (hex, * means any) | |
1503 | * pid - product id (hex, * means any) | |
1504 | * | |
1505 | * See 'lsusb' output. | |
4b096fc9 | 1506 | */ |
5d0c5750 | 1507 | static int parse_filter(const char *spec, struct USBAutoFilter *f) |
4b096fc9 | 1508 | { |
5d0c5750 AL |
1509 | enum { BUS, DEV, VID, PID, DONE }; |
1510 | const char *p = spec; | |
1511 | int i; | |
1512 | ||
0745eb1e MA |
1513 | f->bus_num = 0; |
1514 | f->addr = 0; | |
1515 | f->vendor_id = 0; | |
1516 | f->product_id = 0; | |
5d0c5750 AL |
1517 | |
1518 | for (i = BUS; i < DONE; i++) { | |
2791104c DA |
1519 | p = strpbrk(p, ":."); |
1520 | if (!p) { | |
1521 | break; | |
1522 | } | |
5d0c5750 | 1523 | p++; |
5d0c5750 | 1524 | |
2791104c DA |
1525 | if (*p == '*') { |
1526 | continue; | |
1527 | } | |
5d0c5750 AL |
1528 | switch(i) { |
1529 | case BUS: f->bus_num = strtol(p, NULL, 10); break; | |
1530 | case DEV: f->addr = strtol(p, NULL, 10); break; | |
1531 | case VID: f->vendor_id = strtol(p, NULL, 16); break; | |
1532 | case PID: f->product_id = strtol(p, NULL, 16); break; | |
1533 | } | |
1534 | } | |
1535 | ||
1536 | if (i < DEV) { | |
1537 | fprintf(stderr, "husb: invalid auto filter spec %s\n", spec); | |
1538 | return -1; | |
1539 | } | |
1540 | ||
1541 | return 0; | |
1542 | } | |
1543 | ||
a594cfbf FB |
1544 | /**********************/ |
1545 | /* USB host device info */ | |
1546 | ||
1547 | struct usb_class_info { | |
1548 | int class; | |
1549 | const char *class_name; | |
1550 | }; | |
1551 | ||
1552 | static const struct usb_class_info usb_class_info[] = { | |
1553 | { USB_CLASS_AUDIO, "Audio"}, | |
1554 | { USB_CLASS_COMM, "Communication"}, | |
1555 | { USB_CLASS_HID, "HID"}, | |
1556 | { USB_CLASS_HUB, "Hub" }, | |
1557 | { USB_CLASS_PHYSICAL, "Physical" }, | |
1558 | { USB_CLASS_PRINTER, "Printer" }, | |
1559 | { USB_CLASS_MASS_STORAGE, "Storage" }, | |
1560 | { USB_CLASS_CDC_DATA, "Data" }, | |
1561 | { USB_CLASS_APP_SPEC, "Application Specific" }, | |
1562 | { USB_CLASS_VENDOR_SPEC, "Vendor Specific" }, | |
1563 | { USB_CLASS_STILL_IMAGE, "Still Image" }, | |
b9dc033c | 1564 | { USB_CLASS_CSCID, "Smart Card" }, |
a594cfbf FB |
1565 | { USB_CLASS_CONTENT_SEC, "Content Security" }, |
1566 | { -1, NULL } | |
1567 | }; | |
1568 | ||
1569 | static const char *usb_class_str(uint8_t class) | |
bb36d470 | 1570 | { |
a594cfbf FB |
1571 | const struct usb_class_info *p; |
1572 | for(p = usb_class_info; p->class != -1; p++) { | |
2791104c | 1573 | if (p->class == class) { |
a594cfbf | 1574 | break; |
2791104c | 1575 | } |
bb36d470 | 1576 | } |
a594cfbf FB |
1577 | return p->class_name; |
1578 | } | |
1579 | ||
179da8af | 1580 | static void usb_info_device(Monitor *mon, int bus_num, int addr, int class_id, |
9596ebb7 PB |
1581 | int vendor_id, int product_id, |
1582 | const char *product_name, | |
1583 | int speed) | |
a594cfbf FB |
1584 | { |
1585 | const char *class_str, *speed_str; | |
1586 | ||
1587 | switch(speed) { | |
5fafdf24 TS |
1588 | case USB_SPEED_LOW: |
1589 | speed_str = "1.5"; | |
a594cfbf | 1590 | break; |
5fafdf24 TS |
1591 | case USB_SPEED_FULL: |
1592 | speed_str = "12"; | |
a594cfbf | 1593 | break; |
5fafdf24 TS |
1594 | case USB_SPEED_HIGH: |
1595 | speed_str = "480"; | |
a594cfbf FB |
1596 | break; |
1597 | default: | |
5fafdf24 | 1598 | speed_str = "?"; |
a594cfbf FB |
1599 | break; |
1600 | } | |
1601 | ||
376253ec | 1602 | monitor_printf(mon, " Device %d.%d, speed %s Mb/s\n", |
a594cfbf FB |
1603 | bus_num, addr, speed_str); |
1604 | class_str = usb_class_str(class_id); | |
2791104c | 1605 | if (class_str) { |
376253ec | 1606 | monitor_printf(mon, " %s:", class_str); |
2791104c | 1607 | } else { |
376253ec | 1608 | monitor_printf(mon, " Class %02x:", class_id); |
2791104c | 1609 | } |
376253ec | 1610 | monitor_printf(mon, " USB device %04x:%04x", vendor_id, product_id); |
2791104c | 1611 | if (product_name[0] != '\0') { |
376253ec | 1612 | monitor_printf(mon, ", %s", product_name); |
2791104c | 1613 | } |
376253ec | 1614 | monitor_printf(mon, "\n"); |
a594cfbf FB |
1615 | } |
1616 | ||
5fafdf24 | 1617 | static int usb_host_info_device(void *opaque, int bus_num, int addr, |
a594cfbf | 1618 | int class_id, |
5fafdf24 | 1619 | int vendor_id, int product_id, |
a594cfbf FB |
1620 | const char *product_name, |
1621 | int speed) | |
1622 | { | |
179da8af BS |
1623 | Monitor *mon = opaque; |
1624 | ||
1625 | usb_info_device(mon, bus_num, addr, class_id, vendor_id, product_id, | |
a594cfbf FB |
1626 | product_name, speed); |
1627 | return 0; | |
1628 | } | |
1629 | ||
ac4ffb5a | 1630 | static void dec2str(int val, char *str, size_t size) |
5d0c5750 | 1631 | { |
2791104c | 1632 | if (val == 0) { |
ac4ffb5a | 1633 | snprintf(str, size, "*"); |
2791104c DA |
1634 | } else { |
1635 | snprintf(str, size, "%d", val); | |
1636 | } | |
5d0c5750 AL |
1637 | } |
1638 | ||
ac4ffb5a | 1639 | static void hex2str(int val, char *str, size_t size) |
5d0c5750 | 1640 | { |
2791104c | 1641 | if (val == 0) { |
ac4ffb5a | 1642 | snprintf(str, size, "*"); |
2791104c | 1643 | } else { |
26a9e82a | 1644 | snprintf(str, size, "%04x", val); |
2791104c | 1645 | } |
5d0c5750 AL |
1646 | } |
1647 | ||
376253ec | 1648 | void usb_host_info(Monitor *mon) |
a594cfbf | 1649 | { |
5d0c5750 | 1650 | struct USBAutoFilter *f; |
26a9e82a | 1651 | struct USBHostDevice *s; |
5d0c5750 | 1652 | |
179da8af | 1653 | usb_host_scan(mon, usb_host_info_device); |
5d0c5750 | 1654 | |
2791104c | 1655 | if (QTAILQ_EMPTY(&hostdevs)) { |
26a9e82a | 1656 | return; |
2791104c DA |
1657 | } |
1658 | ||
26a9e82a GH |
1659 | monitor_printf(mon, " Auto filters:\n"); |
1660 | QTAILQ_FOREACH(s, &hostdevs, next) { | |
5d0c5750 | 1661 | char bus[10], addr[10], vid[10], pid[10]; |
26a9e82a | 1662 | f = &s->match; |
ac4ffb5a AL |
1663 | dec2str(f->bus_num, bus, sizeof(bus)); |
1664 | dec2str(f->addr, addr, sizeof(addr)); | |
1665 | hex2str(f->vendor_id, vid, sizeof(vid)); | |
1666 | hex2str(f->product_id, pid, sizeof(pid)); | |
376253ec AL |
1667 | monitor_printf(mon, " Device %s.%s ID %s:%s\n", |
1668 | bus, addr, vid, pid); | |
5d0c5750 | 1669 | } |
bb36d470 | 1670 | } |