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