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