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