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