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