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