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