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