]> git.proxmox.com Git - mirror_qemu.git/blame - usb-linux.c
Init dumb display if no others available.
[mirror_qemu.git] / usb-linux.c
CommitLineData
bb36d470
FB
1/*
2 * Linux host USB redirector
3 *
4 * Copyright (c) 2005 Fabrice Bellard
5fafdf24 5 *
bb36d470
FB
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24#include "vl.h"
25
26#if defined(__linux__)
27#include <dirent.h>
28#include <sys/ioctl.h>
29#include <linux/usbdevice_fs.h>
30#include <linux/version.h>
b9dc033c 31#include <signal.h>
bb36d470
FB
32
33/* We redefine it to avoid version problems */
34struct usb_ctrltransfer {
35 uint8_t bRequestType;
36 uint8_t bRequest;
37 uint16_t wValue;
38 uint16_t wIndex;
39 uint16_t wLength;
40 uint32_t timeout;
41 void *data;
42};
43
a594cfbf 44typedef int USBScanFunc(void *opaque, int bus_num, int addr, int class_id,
5fafdf24 45 int vendor_id, int product_id,
a594cfbf 46 const char *product_name, int speed);
5fafdf24 47static int usb_host_find_device(int *pbus_num, int *paddr,
1f6e24e7 48 char *product_name, int product_name_size,
a594cfbf 49 const char *devname);
bb36d470 50
a594cfbf 51//#define DEBUG
b9dc033c
AZ
52//#define DEBUG_ISOCH
53//#define USE_ASYNCIO
bb36d470
FB
54
55#define USBDEVFS_PATH "/proc/bus/usb"
1f6e24e7 56#define PRODUCT_NAME_SZ 32
b9dc033c
AZ
57#define SIG_ISOCOMPLETE (SIGRTMIN+7)
58#define MAX_ENDPOINTS 16
bb36d470 59
b9dc033c
AZ
60struct sigaction sigact;
61
62/* endpoint association data */
63struct endp_data {
64 uint8_t type;
65};
66
67/* FIXME: move USBPacket to PendingURB */
bb36d470
FB
68typedef struct USBHostDevice {
69 USBDevice dev;
70 int fd;
046833ea 71 int pipe_fds[2];
b9dc033c
AZ
72 USBPacket *packet;
73 struct endp_data endp_table[MAX_ENDPOINTS];
74 int configuration;
75 uint8_t descr[1024];
76 int descr_len;
77 int urbs_ready;
bb36d470
FB
78} USBHostDevice;
79
b9dc033c
AZ
80typedef struct PendingURB {
81 struct usbdevfs_urb *urb;
b9dc033c
AZ
82 int status;
83 struct PendingURB *next;
84} PendingURB;
85
4d043a09 86static PendingURB *pending_urbs = NULL;
b9dc033c 87
4d043a09 88static int add_pending_urb(struct usbdevfs_urb *urb)
b9dc033c
AZ
89{
90 PendingURB *purb = qemu_mallocz(sizeof(PendingURB));
91 if (purb) {
92 purb->urb = urb;
b9dc033c
AZ
93 purb->status = 0;
94 purb->next = pending_urbs;
95 pending_urbs = purb;
96 return 1;
97 }
98 return 0;
99}
100
4d043a09 101static int del_pending_urb(struct usbdevfs_urb *urb)
b9dc033c
AZ
102{
103 PendingURB *purb = pending_urbs;
104 PendingURB *prev = NULL;
105
106 while (purb && purb->urb != urb) {
107 prev = purb;
108 purb = purb->next;
109 }
110
111 if (purb && purb->urb == urb) {
112 if (prev) {
113 prev->next = purb->next;
114 } else {
115 pending_urbs = purb->next;
116 }
117 qemu_free(purb);
118 return 1;
119 }
120 return 0;
121}
122
4d043a09
AZ
123#ifdef USE_ASYNCIO
124static PendingURB *get_pending_urb(struct usbdevfs_urb *urb)
b9dc033c
AZ
125{
126 PendingURB *purb = pending_urbs;
127
128 while (purb && purb->urb != urb) {
129 purb = purb->next;
130 }
131
132 if (purb && purb->urb == urb) {
133 return purb;
134 }
135 return NULL;
136}
4d043a09 137#endif
b9dc033c
AZ
138
139static int usb_host_update_interfaces(USBHostDevice *dev, int configuration)
140{
141 int dev_descr_len, config_descr_len;
142 int interface, nb_interfaces, nb_configurations;
143 int ret, i;
144
145 if (configuration == 0) /* address state - ignore */
146 return 1;
147
148 i = 0;
149 dev_descr_len = dev->descr[0];
150 if (dev_descr_len > dev->descr_len)
151 goto fail;
152 nb_configurations = dev->descr[17];
153
154 i += dev_descr_len;
155 while (i < dev->descr_len) {
156#ifdef DEBUG
157 printf("i is %d, descr_len is %d, dl %d, dt %d\n", i, dev->descr_len,
158 dev->descr[i], dev->descr[i+1]);
159#endif
160 if (dev->descr[i+1] != USB_DT_CONFIG) {
161 i += dev->descr[i];
162 continue;
163 }
164 config_descr_len = dev->descr[i];
165
166 if (configuration == dev->descr[i + 5])
167 break;
168
169 i += config_descr_len;
170 }
171
172 if (i >= dev->descr_len) {
173 printf("usb_host: error - device has no matching configuration\n");
174 goto fail;
175 }
176 nb_interfaces = dev->descr[i + 4];
177
178#ifdef USBDEVFS_DISCONNECT
179 /* earlier Linux 2.4 do not support that */
180 {
181 struct usbdevfs_ioctl ctrl;
182 for (interface = 0; interface < nb_interfaces; interface++) {
183 ctrl.ioctl_code = USBDEVFS_DISCONNECT;
184 ctrl.ifno = interface;
185 ret = ioctl(dev->fd, USBDEVFS_IOCTL, &ctrl);
186 if (ret < 0 && errno != ENODATA) {
187 perror("USBDEVFS_DISCONNECT");
188 goto fail;
189 }
190 }
191 }
192#endif
193
194 /* XXX: only grab if all interfaces are free */
195 for (interface = 0; interface < nb_interfaces; interface++) {
196 ret = ioctl(dev->fd, USBDEVFS_CLAIMINTERFACE, &interface);
197 if (ret < 0) {
198 if (errno == EBUSY) {
199 fprintf(stderr,
200 "usb_host: warning - device already grabbed\n");
201 } else {
202 perror("USBDEVFS_CLAIMINTERFACE");
203 }
204 fail:
205 return 0;
206 }
207 }
208
209#ifdef DEBUG
210 printf("usb_host: %d interfaces claimed for configuration %d\n",
211 nb_interfaces, configuration);
212#endif
213
214 return 1;
215}
216
059809e4 217static void usb_host_handle_reset(USBDevice *dev)
bb36d470
FB
218{
219#if 0
220 USBHostDevice *s = (USBHostDevice *)dev;
221 /* USBDEVFS_RESET, but not the first time as it has already be
222 done by the host OS */
223 ioctl(s->fd, USBDEVFS_RESET);
224#endif
5fafdf24 225}
bb36d470 226
059809e4
FB
227static void usb_host_handle_destroy(USBDevice *dev)
228{
229 USBHostDevice *s = (USBHostDevice *)dev;
230
231 if (s->fd >= 0)
232 close(s->fd);
233 qemu_free(s);
234}
235
b9dc033c
AZ
236static int usb_linux_update_endp_table(USBHostDevice *s);
237
bb36d470
FB
238static int usb_host_handle_control(USBDevice *dev,
239 int request,
240 int value,
241 int index,
242 int length,
243 uint8_t *data)
244{
245 USBHostDevice *s = (USBHostDevice *)dev;
246 struct usb_ctrltransfer ct;
b9dc033c
AZ
247 struct usbdevfs_setinterface si;
248 int intf_update_required = 0;
bb36d470
FB
249 int ret;
250
251 if (request == (DeviceOutRequest | USB_REQ_SET_ADDRESS)) {
252 /* specific SET_ADDRESS support */
253 dev->addr = value;
254 return 0;
b9dc033c
AZ
255 } else if (request == ((USB_RECIP_INTERFACE << 8) |
256 USB_REQ_SET_INTERFACE)) {
257 /* set alternate setting for the interface */
258 si.interface = index;
259 si.altsetting = value;
260 ret = ioctl(s->fd, USBDEVFS_SETINTERFACE, &si);
261 usb_linux_update_endp_table(s);
262 } else if (request == (DeviceOutRequest | USB_REQ_SET_CONFIGURATION)) {
263#ifdef DEBUG
264 printf("usb_host_handle_control: SET_CONFIGURATION request - "
265 "config %d\n", value & 0xff);
266#endif
267 if (s->configuration != (value & 0xff)) {
268 s->configuration = (value & 0xff);
269 intf_update_required = 1;
270 }
271 goto do_request;
bb36d470 272 } else {
b9dc033c 273 do_request:
bb36d470
FB
274 ct.bRequestType = request >> 8;
275 ct.bRequest = request;
276 ct.wValue = value;
277 ct.wIndex = index;
278 ct.wLength = length;
279 ct.timeout = 50;
280 ct.data = data;
281 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
b9dc033c
AZ
282 }
283
284 if (ret < 0) {
285 switch(errno) {
286 case ETIMEDOUT:
287 return USB_RET_NAK;
288 default:
289 return USB_RET_STALL;
bb36d470 290 }
b9dc033c
AZ
291 } else {
292 if (intf_update_required) {
293#ifdef DEBUG
294 printf("usb_host_handle_control: updating interfaces\n");
295#endif
296 usb_host_update_interfaces(s, value & 0xff);
297 }
298 return ret;
299 }
bb36d470
FB
300}
301
b9dc033c
AZ
302static int usb_host_handle_isoch(USBDevice *dev, USBPacket *p);
303
4d611c9a 304static int usb_host_handle_data(USBDevice *dev, USBPacket *p)
bb36d470
FB
305{
306 USBHostDevice *s = (USBHostDevice *)dev;
307 struct usbdevfs_bulktransfer bt;
308 int ret;
4d611c9a 309 uint8_t devep = p->devep;
bb36d470 310
b9dc033c
AZ
311 if (s->endp_table[p->devep - 1].type == USBDEVFS_URB_TYPE_ISO) {
312 return usb_host_handle_isoch(dev, p);
313 }
314
bb36d470
FB
315 /* XXX: optimize and handle all data types by looking at the
316 config descriptor */
4d611c9a 317 if (p->pid == USB_TOKEN_IN)
bb36d470
FB
318 devep |= 0x80;
319 bt.ep = devep;
4d611c9a 320 bt.len = p->len;
bb36d470 321 bt.timeout = 50;
4d611c9a 322 bt.data = p->data;
bb36d470
FB
323 ret = ioctl(s->fd, USBDEVFS_BULK, &bt);
324 if (ret < 0) {
325 switch(errno) {
326 case ETIMEDOUT:
327 return USB_RET_NAK;
328 case EPIPE:
329 default:
330#ifdef DEBUG
331 printf("handle_data: errno=%d\n", errno);
332#endif
333 return USB_RET_STALL;
334 }
335 } else {
336 return ret;
337 }
338}
339
4d043a09 340#ifdef USE_ASYNCIO
046833ea 341static void urb_completion_pipe_read(void *opaque)
4d043a09 342{
046833ea 343 USBHostDevice *s = opaque;
4d043a09 344 USBPacket *p = s->packet;
046833ea
AZ
345 PendingURB *pending_urb = NULL;
346 struct usbdevfs_urb *purb = NULL;
347 int len, ret;
348
349 len = read(s->pipe_fds[0], &pending_urb, sizeof(pending_urb));
350 if (len != sizeof(pending_urb)) {
351 printf("urb_completion: error reading pending_urb, len=%d\n", len);
352 return;
353 }
4d043a09 354
046833ea 355 /* FIXME: handle pending_urb->status */
4d043a09
AZ
356 del_pending_urb(pending_urb->urb);
357
358 if (!p) {
359 s->urbs_ready++;
360 return;
361 }
362
363 ret = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &purb);
364 if (ret < 0) {
046833ea 365 printf("urb_completion: REAPURBNDELAY ioctl=%d errno=%d\n",
4d043a09
AZ
366 ret, errno);
367 return;
368 }
369
370#ifdef DEBUG_ISOCH
371 if (purb == pending_urb->urb) {
046833ea 372 printf("urb_completion: urb mismatch reaped=%p pending=%p\n",
4d043a09
AZ
373 purb, urb);
374 }
375#endif
376
377 p->len = purb->actual_length;
378 usb_packet_complete(p);
379 qemu_free(purb);
380 s->packet = NULL;
381}
b9dc033c 382
4d043a09
AZ
383static void isoch_done(int signum, siginfo_t *info, void *context)
384{
b9dc033c
AZ
385 struct usbdevfs_urb *urb = (struct usbdevfs_urb *)info->si_addr;
386 USBHostDevice *s = (USBHostDevice *)urb->usercontext;
387 PendingURB *purb;
388
389 if (info->si_code != SI_ASYNCIO ||
390 info->si_signo != SIG_ISOCOMPLETE) {
391 return;
392 }
393
394 purb = get_pending_urb(urb);
395 if (purb) {
046833ea
AZ
396 purb->status = info->si_errno;
397 write(s->pipe_fds[1], &purb, sizeof(purb));
b9dc033c
AZ
398 }
399}
4d043a09 400#endif
b9dc033c
AZ
401
402static int usb_host_handle_isoch(USBDevice *dev, USBPacket *p)
403{
404 USBHostDevice *s = (USBHostDevice *)dev;
405 struct usbdevfs_urb *urb, *purb = NULL;
406 int ret;
407 uint8_t devep = p->devep;
408
409 if (p->pid == USB_TOKEN_IN)
410 devep |= 0x80;
411
412 urb = qemu_mallocz(sizeof(struct usbdevfs_urb) +
413 sizeof(struct usbdevfs_iso_packet_desc));
414 if (!urb) {
415 printf("usb_host_handle_isoch: malloc failed\n");
416 return 0;
417 }
418
419 urb->type = USBDEVFS_URB_TYPE_ISO;
420 urb->endpoint = devep;
421 urb->status = 0;
422 urb->flags = USBDEVFS_URB_ISO_ASAP;
423 urb->buffer = p->data;
424 urb->buffer_length = p->len;
425 urb->actual_length = 0;
426 urb->start_frame = 0;
427 urb->error_count = 0;
428#ifdef USE_ASYNCIO
429 urb->signr = SIG_ISOCOMPLETE;
430#else
431 urb->signr = 0;
432#endif
433 urb->usercontext = s;
434 urb->number_of_packets = 1;
435 urb->iso_frame_desc[0].length = p->len;
436 urb->iso_frame_desc[0].actual_length = 0;
437 urb->iso_frame_desc[0].status = 0;
438 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
439 if (ret == 0) {
440 if (!add_pending_urb(urb)) {
441 printf("usb_host_handle_isoch: add_pending_urb failed %p\n", urb);
442 }
443 } else {
444 printf("usb_host_handle_isoch: SUBMITURB ioctl=%d errno=%d\n",
445 ret, errno);
446 qemu_free(urb);
447 switch(errno) {
448 case ETIMEDOUT:
449 return USB_RET_NAK;
450 case EPIPE:
451 default:
452 return USB_RET_STALL;
453 }
454 }
455#ifdef USE_ASYNCIO
456 /* FIXME: handle urbs_ready together with sync io
457 * workaround for injecting the signaled urbs into current frame */
458 if (s->urbs_ready > 0) {
459 ret = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &purb);
460 if (ret == 0) {
461 ret = purb->actual_length;
462 qemu_free(purb);
463 s->urbs_ready--;
464 }
465 return ret;
466 }
467 s->packet = p;
468 return USB_RET_ASYNC;
469#else
470 ret = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &purb);
471 if (ret == 0) {
472 if (del_pending_urb(purb)) {
473 ret = purb->actual_length;
474 qemu_free(purb);
475 } else {
476 printf("usb_host_handle_isoch: del_pending_urb failed %p\n", purb);
477 }
478 } else {
479#ifdef DEBUG_ISOCH
480 printf("usb_host_handle_isoch: REAPURBNDELAY ioctl=%d errno=%d\n",
481 ret, errno);
482#endif
483 }
484 return ret;
485#endif
486}
487
b9dc033c
AZ
488/* returns 1 on problem encountered or 0 for success */
489static int usb_linux_update_endp_table(USBHostDevice *s)
490{
491 uint8_t *descriptors;
492 uint8_t devep, type, configuration, alt_interface;
493 struct usb_ctrltransfer ct;
494 int interface, ret, length, i;
495
496 ct.bRequestType = USB_DIR_IN;
497 ct.bRequest = USB_REQ_GET_CONFIGURATION;
498 ct.wValue = 0;
499 ct.wIndex = 0;
500 ct.wLength = 1;
501 ct.data = &configuration;
502 ct.timeout = 50;
503
504 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
505 if (ret < 0) {
506 perror("usb_linux_update_endp_table");
507 return 1;
508 }
509
510 /* in address state */
511 if (configuration == 0)
512 return 1;
513
514 /* get the desired configuration, interface, and endpoint descriptors
515 * from device description */
516 descriptors = &s->descr[18];
517 length = s->descr_len - 18;
518 i = 0;
519
520 if (descriptors[i + 1] != USB_DT_CONFIG ||
521 descriptors[i + 5] != configuration) {
522 printf("invalid descriptor data - configuration\n");
523 return 1;
524 }
525 i += descriptors[i];
526
527 while (i < length) {
528 if (descriptors[i + 1] != USB_DT_INTERFACE ||
529 (descriptors[i + 1] == USB_DT_INTERFACE &&
530 descriptors[i + 4] == 0)) {
531 i += descriptors[i];
532 continue;
533 }
534
535 interface = descriptors[i + 2];
536
537 ct.bRequestType = USB_DIR_IN | USB_RECIP_INTERFACE;
538 ct.bRequest = USB_REQ_GET_INTERFACE;
539 ct.wValue = 0;
540 ct.wIndex = interface;
541 ct.wLength = 1;
542 ct.data = &alt_interface;
543 ct.timeout = 50;
544
545 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
546 if (ret < 0) {
547 perror("usb_linux_update_endp_table");
548 return 1;
549 }
550
551 /* the current interface descriptor is the active interface
552 * and has endpoints */
553 if (descriptors[i + 3] != alt_interface) {
554 i += descriptors[i];
555 continue;
556 }
557
558 /* advance to the endpoints */
559 while (i < length && descriptors[i +1] != USB_DT_ENDPOINT)
560 i += descriptors[i];
561
562 if (i >= length)
563 break;
564
565 while (i < length) {
566 if (descriptors[i + 1] != USB_DT_ENDPOINT)
567 break;
568
569 devep = descriptors[i + 2];
570 switch (descriptors[i + 3] & 0x3) {
571 case 0x00:
572 type = USBDEVFS_URB_TYPE_CONTROL;
573 break;
574 case 0x01:
575 type = USBDEVFS_URB_TYPE_ISO;
576 break;
577 case 0x02:
578 type = USBDEVFS_URB_TYPE_BULK;
579 break;
580 case 0x03:
581 type = USBDEVFS_URB_TYPE_INTERRUPT;
582 break;
583 default:
584 printf("usb_host: malformed endpoint type\n");
585 type = USBDEVFS_URB_TYPE_BULK;
586 }
587 s->endp_table[(devep & 0xf) - 1].type = type;
588
589 i += descriptors[i];
590 }
591 }
592 return 0;
593}
594
bb36d470 595/* XXX: exclude high speed devices or implement EHCI */
a594cfbf 596USBDevice *usb_host_device_open(const char *devname)
bb36d470 597{
b9dc033c
AZ
598 int fd = -1, ret;
599 USBHostDevice *dev = NULL;
bb36d470 600 struct usbdevfs_connectinfo ci;
a594cfbf 601 char buf[1024];
a594cfbf 602 int bus_num, addr;
1f6e24e7 603 char product_name[PRODUCT_NAME_SZ];
bb36d470 604
b9dc033c
AZ
605 dev = qemu_mallocz(sizeof(USBHostDevice));
606 if (!dev)
607 goto fail;
608
609#ifdef DEBUG_ISOCH
610 printf("usb_host_device_open %s\n", devname);
611#endif
5fafdf24 612 if (usb_host_find_device(&bus_num, &addr,
1f6e24e7 613 product_name, sizeof(product_name),
5fafdf24 614 devname) < 0)
a594cfbf 615 return NULL;
3b46e624 616
5fafdf24 617 snprintf(buf, sizeof(buf), USBDEVFS_PATH "/%03d/%03d",
a594cfbf 618 bus_num, addr);
b9dc033c 619 fd = open(buf, O_RDWR | O_NONBLOCK);
bb36d470 620 if (fd < 0) {
a594cfbf
FB
621 perror(buf);
622 return NULL;
bb36d470
FB
623 }
624
b9dc033c
AZ
625 /* read the device description */
626 dev->descr_len = read(fd, dev->descr, sizeof(dev->descr));
627 if (dev->descr_len <= 0) {
046833ea 628 perror("usb_host_device_open: reading device data failed");
bb36d470
FB
629 goto fail;
630 }
3b46e624 631
b9dc033c 632#ifdef DEBUG
868bfe2b 633 {
b9dc033c
AZ
634 int x;
635 printf("=== begin dumping device descriptor data ===\n");
636 for (x = 0; x < dev->descr_len; x++)
637 printf("%02x ", dev->descr[x]);
638 printf("\n=== end dumping device descriptor data ===\n");
bb36d470 639 }
a594cfbf
FB
640#endif
641
b9dc033c
AZ
642 dev->fd = fd;
643 dev->configuration = 1;
644
645 /* XXX - do something about initial configuration */
646 if (!usb_host_update_interfaces(dev, 1))
647 goto fail;
bb36d470
FB
648
649 ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci);
650 if (ret < 0) {
046833ea 651 perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
bb36d470
FB
652 goto fail;
653 }
654
655#ifdef DEBUG
a594cfbf 656 printf("host USB device %d.%d grabbed\n", bus_num, addr);
3b46e624 657#endif
bb36d470 658
b9dc033c
AZ
659 ret = usb_linux_update_endp_table(dev);
660 if (ret)
bb36d470 661 goto fail;
b9dc033c 662
bb36d470
FB
663 if (ci.slow)
664 dev->dev.speed = USB_SPEED_LOW;
665 else
666 dev->dev.speed = USB_SPEED_HIGH;
a594cfbf 667 dev->dev.handle_packet = usb_generic_handle_packet;
bb36d470
FB
668
669 dev->dev.handle_reset = usb_host_handle_reset;
670 dev->dev.handle_control = usb_host_handle_control;
671 dev->dev.handle_data = usb_host_handle_data;
059809e4 672 dev->dev.handle_destroy = usb_host_handle_destroy;
1f6e24e7
FB
673
674 if (product_name[0] == '\0')
675 snprintf(dev->dev.devname, sizeof(dev->dev.devname),
676 "host:%s", devname);
677 else
678 pstrcpy(dev->dev.devname, sizeof(dev->dev.devname),
679 product_name);
680
b9dc033c
AZ
681#ifdef USE_ASYNCIO
682 /* set up the signal handlers */
683 sigemptyset(&sigact.sa_mask);
684 sigact.sa_sigaction = isoch_done;
685 sigact.sa_flags = SA_SIGINFO;
686 sigact.sa_restorer = 0;
687 ret = sigaction(SIG_ISOCOMPLETE, &sigact, NULL);
688 if (ret < 0) {
046833ea
AZ
689 perror("usb_host_device_open: sigaction failed");
690 goto fail;
691 }
692
693 if (pipe(dev->pipe_fds) < 0) {
694 perror("usb_host_device_open: pipe creation failed");
695 goto fail;
b9dc033c 696 }
046833ea
AZ
697 fcntl(dev->pipe_fds[0], F_SETFL, O_NONBLOCK | O_ASYNC);
698 fcntl(dev->pipe_fds[1], F_SETFL, O_NONBLOCK);
699 qemu_set_fd_handler(dev->pipe_fds[0], urb_completion_pipe_read, NULL, dev);
b9dc033c
AZ
700#endif
701 dev->urbs_ready = 0;
a594cfbf 702 return (USBDevice *)dev;
b9dc033c
AZ
703fail:
704 if (dev)
705 qemu_free(dev);
706 close(fd);
707 return NULL;
a594cfbf 708}
bb36d470 709
a594cfbf 710static int get_tag_value(char *buf, int buf_size,
5fafdf24 711 const char *str, const char *tag,
a594cfbf
FB
712 const char *stopchars)
713{
714 const char *p;
715 char *q;
716 p = strstr(str, tag);
717 if (!p)
718 return -1;
719 p += strlen(tag);
720 while (isspace(*p))
721 p++;
722 q = buf;
723 while (*p != '\0' && !strchr(stopchars, *p)) {
724 if ((q - buf) < (buf_size - 1))
725 *q++ = *p;
726 p++;
727 }
728 *q = '\0';
729 return q - buf;
bb36d470
FB
730}
731
a594cfbf 732static int usb_host_scan(void *opaque, USBScanFunc *func)
bb36d470 733{
a594cfbf
FB
734 FILE *f;
735 char line[1024];
bb36d470 736 char buf[1024];
a594cfbf
FB
737 int bus_num, addr, speed, device_count, class_id, product_id, vendor_id;
738 int ret;
739 char product_name[512];
3b46e624 740
a594cfbf
FB
741 f = fopen(USBDEVFS_PATH "/devices", "r");
742 if (!f) {
743 term_printf("Could not open %s\n", USBDEVFS_PATH "/devices");
744 return 0;
745 }
746 device_count = 0;
747 bus_num = addr = speed = class_id = product_id = vendor_id = 0;
748 ret = 0;
bb36d470 749 for(;;) {
a594cfbf 750 if (fgets(line, sizeof(line), f) == NULL)
bb36d470 751 break;
a594cfbf
FB
752 if (strlen(line) > 0)
753 line[strlen(line) - 1] = '\0';
754 if (line[0] == 'T' && line[1] == ':') {
38ca0f6d
PB
755 if (device_count && (vendor_id || product_id)) {
756 /* New device. Add the previously discovered device. */
5fafdf24 757 ret = func(opaque, bus_num, addr, class_id, vendor_id,
a594cfbf
FB
758 product_id, product_name, speed);
759 if (ret)
760 goto the_end;
761 }
762 if (get_tag_value(buf, sizeof(buf), line, "Bus=", " ") < 0)
763 goto fail;
764 bus_num = atoi(buf);
765 if (get_tag_value(buf, sizeof(buf), line, "Dev#=", " ") < 0)
766 goto fail;
767 addr = atoi(buf);
768 if (get_tag_value(buf, sizeof(buf), line, "Spd=", " ") < 0)
769 goto fail;
770 if (!strcmp(buf, "480"))
771 speed = USB_SPEED_HIGH;
772 else if (!strcmp(buf, "1.5"))
773 speed = USB_SPEED_LOW;
774 else
775 speed = USB_SPEED_FULL;
776 product_name[0] = '\0';
777 class_id = 0xff;
778 device_count++;
779 product_id = 0;
780 vendor_id = 0;
781 } else if (line[0] == 'P' && line[1] == ':') {
782 if (get_tag_value(buf, sizeof(buf), line, "Vendor=", " ") < 0)
783 goto fail;
784 vendor_id = strtoul(buf, NULL, 16);
785 if (get_tag_value(buf, sizeof(buf), line, "ProdID=", " ") < 0)
786 goto fail;
787 product_id = strtoul(buf, NULL, 16);
788 } else if (line[0] == 'S' && line[1] == ':') {
789 if (get_tag_value(buf, sizeof(buf), line, "Product=", "") < 0)
790 goto fail;
791 pstrcpy(product_name, sizeof(product_name), buf);
792 } else if (line[0] == 'D' && line[1] == ':') {
793 if (get_tag_value(buf, sizeof(buf), line, "Cls=", " (") < 0)
794 goto fail;
795 class_id = strtoul(buf, NULL, 16);
bb36d470 796 }
a594cfbf
FB
797 fail: ;
798 }
38ca0f6d
PB
799 if (device_count && (vendor_id || product_id)) {
800 /* Add the last device. */
5fafdf24 801 ret = func(opaque, bus_num, addr, class_id, vendor_id,
a594cfbf 802 product_id, product_name, speed);
bb36d470 803 }
a594cfbf
FB
804 the_end:
805 fclose(f);
806 return ret;
bb36d470
FB
807}
808
a594cfbf
FB
809typedef struct FindDeviceState {
810 int vendor_id;
811 int product_id;
812 int bus_num;
813 int addr;
1f6e24e7 814 char product_name[PRODUCT_NAME_SZ];
a594cfbf
FB
815} FindDeviceState;
816
5fafdf24 817static int usb_host_find_device_scan(void *opaque, int bus_num, int addr,
a594cfbf 818 int class_id,
5fafdf24 819 int vendor_id, int product_id,
a594cfbf 820 const char *product_name, int speed)
bb36d470 821{
a594cfbf 822 FindDeviceState *s = opaque;
1f6e24e7
FB
823 if ((vendor_id == s->vendor_id &&
824 product_id == s->product_id) ||
825 (bus_num == s->bus_num &&
826 addr == s->addr)) {
827 pstrcpy(s->product_name, PRODUCT_NAME_SZ, product_name);
a594cfbf
FB
828 s->bus_num = bus_num;
829 s->addr = addr;
830 return 1;
831 } else {
832 return 0;
833 }
834}
bb36d470 835
5fafdf24
TS
836/* the syntax is :
837 'bus.addr' (decimal numbers) or
a594cfbf 838 'vendor_id:product_id' (hexa numbers) */
5fafdf24 839static int usb_host_find_device(int *pbus_num, int *paddr,
1f6e24e7 840 char *product_name, int product_name_size,
a594cfbf
FB
841 const char *devname)
842{
843 const char *p;
844 int ret;
845 FindDeviceState fs;
846
847 p = strchr(devname, '.');
848 if (p) {
849 *pbus_num = strtoul(devname, NULL, 0);
850 *paddr = strtoul(p + 1, NULL, 0);
1f6e24e7
FB
851 fs.bus_num = *pbus_num;
852 fs.addr = *paddr;
853 ret = usb_host_scan(&fs, usb_host_find_device_scan);
854 if (ret)
855 pstrcpy(product_name, product_name_size, fs.product_name);
a594cfbf
FB
856 return 0;
857 }
858 p = strchr(devname, ':');
859 if (p) {
860 fs.vendor_id = strtoul(devname, NULL, 16);
861 fs.product_id = strtoul(p + 1, NULL, 16);
862 ret = usb_host_scan(&fs, usb_host_find_device_scan);
863 if (ret) {
864 *pbus_num = fs.bus_num;
865 *paddr = fs.addr;
1f6e24e7 866 pstrcpy(product_name, product_name_size, fs.product_name);
a594cfbf 867 return 0;
bb36d470
FB
868 }
869 }
a594cfbf 870 return -1;
bb36d470
FB
871}
872
a594cfbf
FB
873/**********************/
874/* USB host device info */
875
876struct usb_class_info {
877 int class;
878 const char *class_name;
879};
880
881static const struct usb_class_info usb_class_info[] = {
882 { USB_CLASS_AUDIO, "Audio"},
883 { USB_CLASS_COMM, "Communication"},
884 { USB_CLASS_HID, "HID"},
885 { USB_CLASS_HUB, "Hub" },
886 { USB_CLASS_PHYSICAL, "Physical" },
887 { USB_CLASS_PRINTER, "Printer" },
888 { USB_CLASS_MASS_STORAGE, "Storage" },
889 { USB_CLASS_CDC_DATA, "Data" },
890 { USB_CLASS_APP_SPEC, "Application Specific" },
891 { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
892 { USB_CLASS_STILL_IMAGE, "Still Image" },
b9dc033c 893 { USB_CLASS_CSCID, "Smart Card" },
a594cfbf
FB
894 { USB_CLASS_CONTENT_SEC, "Content Security" },
895 { -1, NULL }
896};
897
898static const char *usb_class_str(uint8_t class)
bb36d470 899{
a594cfbf
FB
900 const struct usb_class_info *p;
901 for(p = usb_class_info; p->class != -1; p++) {
902 if (p->class == class)
903 break;
bb36d470 904 }
a594cfbf
FB
905 return p->class_name;
906}
907
908void usb_info_device(int bus_num, int addr, int class_id,
5fafdf24 909 int vendor_id, int product_id,
a594cfbf
FB
910 const char *product_name,
911 int speed)
912{
913 const char *class_str, *speed_str;
914
915 switch(speed) {
5fafdf24
TS
916 case USB_SPEED_LOW:
917 speed_str = "1.5";
a594cfbf 918 break;
5fafdf24
TS
919 case USB_SPEED_FULL:
920 speed_str = "12";
a594cfbf 921 break;
5fafdf24
TS
922 case USB_SPEED_HIGH:
923 speed_str = "480";
a594cfbf
FB
924 break;
925 default:
5fafdf24 926 speed_str = "?";
a594cfbf
FB
927 break;
928 }
929
5fafdf24 930 term_printf(" Device %d.%d, speed %s Mb/s\n",
a594cfbf
FB
931 bus_num, addr, speed_str);
932 class_str = usb_class_str(class_id);
5fafdf24 933 if (class_str)
a594cfbf
FB
934 term_printf(" %s:", class_str);
935 else
936 term_printf(" Class %02x:", class_id);
937 term_printf(" USB device %04x:%04x", vendor_id, product_id);
938 if (product_name[0] != '\0')
939 term_printf(", %s", product_name);
940 term_printf("\n");
941}
942
5fafdf24 943static int usb_host_info_device(void *opaque, int bus_num, int addr,
a594cfbf 944 int class_id,
5fafdf24 945 int vendor_id, int product_id,
a594cfbf
FB
946 const char *product_name,
947 int speed)
948{
949 usb_info_device(bus_num, addr, class_id, vendor_id, product_id,
950 product_name, speed);
951 return 0;
952}
953
954void usb_host_info(void)
955{
956 usb_host_scan(NULL, usb_host_info_device);
bb36d470
FB
957}
958
959#else
960
a594cfbf
FB
961void usb_host_info(void)
962{
963 term_printf("USB host devices not supported\n");
964}
965
bb36d470 966/* XXX: modify configure to compile the right host driver */
a594cfbf 967USBDevice *usb_host_device_open(const char *devname)
bb36d470
FB
968{
969 return NULL;
970}
971
972#endif