Statistics
| Branch: | Revision:

root / usb-linux.c @ 50b7963e

History | View | Annotate | Download (48.6 kB)

1
/*
2
 * Linux host USB redirector
3
 *
4
 * Copyright (c) 2005 Fabrice Bellard
5
 *
6
 * Copyright (c) 2008 Max Krasnyansky
7
 *      Support for host device auto connect & disconnect
8
 *      Major rewrite to support fully async operation
9
 *
10
 * Copyright 2008 TJ <linux@tjworld.net>
11
 *      Added flexible support for /dev/bus/usb /sys/bus/usb/devices in addition
12
 *      to the legacy /proc/bus/usb USB device discovery and handling
13
 *
14
 * Permission is hereby granted, free of charge, to any person obtaining a copy
15
 * of this software and associated documentation files (the "Software"), to deal
16
 * in the Software without restriction, including without limitation the rights
17
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18
 * copies of the Software, and to permit persons to whom the Software is
19
 * furnished to do so, subject to the following conditions:
20
 *
21
 * The above copyright notice and this permission notice shall be included in
22
 * all copies or substantial portions of the Software.
23
 *
24
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30
 * THE SOFTWARE.
31
 */
32

    
33
#include "qemu-common.h"
34
#include "qemu-timer.h"
35
#include "monitor.h"
36
#include "sysemu.h"
37

    
38
#include <dirent.h>
39
#include <sys/ioctl.h>
40
#include <signal.h>
41

    
42
#include <linux/usbdevice_fs.h>
43
#include <linux/version.h>
44
#include "hw/usb.h"
45

    
46
/* We redefine it to avoid version problems */
47
struct usb_ctrltransfer {
48
    uint8_t  bRequestType;
49
    uint8_t  bRequest;
50
    uint16_t wValue;
51
    uint16_t wIndex;
52
    uint16_t wLength;
53
    uint32_t timeout;
54
    void *data;
55
};
56

    
57
typedef int USBScanFunc(void *opaque, int bus_num, int addr, int devpath,
58
                        int class_id, int vendor_id, int product_id,
59
                        const char *product_name, int speed);
60

    
61
//#define DEBUG
62

    
63
#ifdef DEBUG
64
#define DPRINTF printf
65
#else
66
#define DPRINTF(...)
67
#endif
68

    
69
#define USBDBG_DEVOPENED "husb: opened %s/devices\n"
70

    
71
#define USBPROCBUS_PATH "/proc/bus/usb"
72
#define PRODUCT_NAME_SZ 32
73
#define MAX_ENDPOINTS 15
74
#define 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
typedef struct AsyncURB AsyncURB;
92

    
93
struct endp_data {
94
    uint8_t type;
95
    uint8_t halted;
96
    uint8_t iso_started;
97
    AsyncURB *iso_urb;
98
    int iso_urb_idx;
99
    int iso_buffer_used;
100
    int max_packet_size;
101
};
102

    
103
struct USBAutoFilter {
104
    uint32_t bus_num;
105
    uint32_t addr;
106
    uint32_t vendor_id;
107
    uint32_t product_id;
108
};
109

    
110
typedef struct USBHostDevice {
111
    USBDevice dev;
112
    int       fd;
113

    
114
    uint8_t   descr[1024];
115
    int       descr_len;
116
    int       configuration;
117
    int       ninterfaces;
118
    int       closing;
119
    Notifier  exit;
120

    
121
    struct endp_data endp_table[MAX_ENDPOINTS];
122

    
123
    /* Host side address */
124
    int bus_num;
125
    int addr;
126
    int devpath;
127
    struct USBAutoFilter match;
128

    
129
    QTAILQ_ENTRY(USBHostDevice) next;
130
} USBHostDevice;
131

    
132
static QTAILQ_HEAD(, USBHostDevice) hostdevs = QTAILQ_HEAD_INITIALIZER(hostdevs);
133

    
134
static int usb_host_close(USBHostDevice *dev);
135
static int parse_filter(const char *spec, struct USBAutoFilter *f);
136
static void usb_host_auto_check(void *unused);
137
static int usb_host_read_file(char *line, size_t line_size,
138
                            const char *device_file, const char *device_name);
139

    
140
static int is_isoc(USBHostDevice *s, int ep)
141
{
142
    return s->endp_table[ep - 1].type == USBDEVFS_URB_TYPE_ISO;
143
}
144

    
145
static int is_valid(USBHostDevice *s, int ep)
146
{
147
    return s->endp_table[ep - 1].type != INVALID_EP_TYPE;
148
}
149

    
150
static int is_halted(USBHostDevice *s, int ep)
151
{
152
    return s->endp_table[ep - 1].halted;
153
}
154

    
155
static void clear_halt(USBHostDevice *s, int ep)
156
{
157
    s->endp_table[ep - 1].halted = 0;
158
}
159

    
160
static void set_halt(USBHostDevice *s, int ep)
161
{
162
    s->endp_table[ep - 1].halted = 1;
163
}
164

    
165
static int is_iso_started(USBHostDevice *s, int ep)
166
{
167
    return s->endp_table[ep - 1].iso_started;
168
}
169

    
170
static void clear_iso_started(USBHostDevice *s, int ep)
171
{
172
    s->endp_table[ep - 1].iso_started = 0;
173
}
174

    
175
static void set_iso_started(USBHostDevice *s, int ep)
176
{
177
    s->endp_table[ep - 1].iso_started = 1;
178
}
179

    
180
static void set_iso_urb(USBHostDevice *s, int ep, AsyncURB *iso_urb)
181
{
182
    s->endp_table[ep - 1].iso_urb = iso_urb;
183
}
184

    
185
static AsyncURB *get_iso_urb(USBHostDevice *s, int ep)
186
{
187
    return s->endp_table[ep - 1].iso_urb;
188
}
189

    
190
static void set_iso_urb_idx(USBHostDevice *s, int ep, int i)
191
{
192
    s->endp_table[ep - 1].iso_urb_idx = i;
193
}
194

    
195
static int get_iso_urb_idx(USBHostDevice *s, int ep)
196
{
197
    return s->endp_table[ep - 1].iso_urb_idx;
198
}
199

    
200
static void set_iso_buffer_used(USBHostDevice *s, int ep, int i)
201
{
202
    s->endp_table[ep - 1].iso_buffer_used = i;
203
}
204

    
205
static int get_iso_buffer_used(USBHostDevice *s, int ep)
206
{
207
    return s->endp_table[ep - 1].iso_buffer_used;
208
}
209

    
210
static int get_max_packet_size(USBHostDevice *s, int ep)
211
{
212
    return s->endp_table[ep - 1].max_packet_size;
213
}
214

    
215
/*
216
 * Async URB state.
217
 * We always allocate iso packet descriptors even for bulk transfers
218
 * to simplify allocation and casts.
219
 */
220
struct AsyncURB
221
{
222
    struct usbdevfs_urb urb;
223
    struct usbdevfs_iso_packet_desc isocpd[ISO_FRAME_DESC_PER_URB];
224

    
225
    /* For regular async urbs */
226
    USBPacket     *packet;
227
    USBHostDevice *hdev;
228

    
229
    /* For buffered iso handling */
230
    int iso_frame_idx; /* -1 means in flight */
231
};
232

    
233
static AsyncURB *async_alloc(void)
234
{
235
    return (AsyncURB *) qemu_mallocz(sizeof(AsyncURB));
236
}
237

    
238
static void async_free(AsyncURB *aurb)
239
{
240
    qemu_free(aurb);
241
}
242

    
243
static void async_complete(void *opaque)
244
{
245
    USBHostDevice *s = opaque;
246
    AsyncURB *aurb;
247

    
248
    while (1) {
249
        USBPacket *p;
250

    
251
        int r = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &aurb);
252
        if (r < 0) {
253
            if (errno == EAGAIN) {
254
                return;
255
            }
256
            if (errno == ENODEV && !s->closing) {
257
                printf("husb: device %d.%d disconnected\n",
258
                       s->bus_num, s->addr);
259
                usb_host_close(s);
260
                usb_host_auto_check(NULL);
261
                return;
262
            }
263

    
264
            DPRINTF("husb: async. reap urb failed errno %d\n", errno);
265
            return;
266
        }
267

    
268
        DPRINTF("husb: async completed. aurb %p status %d alen %d\n",
269
                aurb, aurb->urb.status, aurb->urb.actual_length);
270

    
271
        /* If this is a buffered iso urb mark it as complete and don't do
272
           anything else (it is handled further in usb_host_handle_iso_data) */
273
        if (aurb->iso_frame_idx == -1) {
274
            if (aurb->urb.status == -EPIPE) {
275
                set_halt(s, aurb->urb.endpoint & 0xf);
276
            }
277
            aurb->iso_frame_idx = 0;
278
            continue;
279
        }
280

    
281
        p = aurb->packet;
282

    
283
        if (p) {
284
            switch (aurb->urb.status) {
285
            case 0:
286
                p->len = aurb->urb.actual_length;
287
                break;
288

    
289
            case -EPIPE:
290
                set_halt(s, p->devep);
291
                p->len = USB_RET_STALL;
292
                break;
293

    
294
            default:
295
                p->len = USB_RET_NAK;
296
                break;
297
            }
298

    
299
            if (aurb->urb.type == USBDEVFS_URB_TYPE_CONTROL) {
300
                usb_generic_async_ctrl_complete(&s->dev, p);
301
            } else {
302
                usb_packet_complete(&s->dev, p);
303
            }
304
        }
305

    
306
        async_free(aurb);
307
    }
308
}
309

    
310
static void async_cancel(USBPacket *unused, void *opaque)
311
{
312
    AsyncURB *aurb = opaque;
313
    USBHostDevice *s = aurb->hdev;
314

    
315
    DPRINTF("husb: async cancel. aurb %p\n", aurb);
316

    
317
    /* Mark it as dead (see async_complete above) */
318
    aurb->packet = NULL;
319

    
320
    int r = ioctl(s->fd, USBDEVFS_DISCARDURB, aurb);
321
    if (r < 0) {
322
        DPRINTF("husb: async. discard urb failed errno %d\n", errno);
323
    }
324
}
325

    
326
static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration)
327
{
328
    int dev_descr_len, config_descr_len;
329
    int interface, nb_interfaces;
330
    int ret, i;
331

    
332
    if (configuration == 0) /* address state - ignore */
333
        return 1;
334

    
335
    DPRINTF("husb: claiming interfaces. config %d\n", configuration);
336

    
337
    i = 0;
338
    dev_descr_len = dev->descr[0];
339
    if (dev_descr_len > dev->descr_len) {
340
        goto fail;
341
    }
342

    
343
    i += dev_descr_len;
344
    while (i < dev->descr_len) {
345
        DPRINTF("husb: i is %d, descr_len is %d, dl %d, dt %d\n",
346
                i, dev->descr_len,
347
               dev->descr[i], dev->descr[i+1]);
348

    
349
        if (dev->descr[i+1] != USB_DT_CONFIG) {
350
            i += dev->descr[i];
351
            continue;
352
        }
353
        config_descr_len = dev->descr[i];
354

    
355
        printf("husb: config #%d need %d\n", dev->descr[i + 5], configuration);
356

    
357
        if (configuration < 0 || configuration == dev->descr[i + 5]) {
358
            configuration = dev->descr[i + 5];
359
            break;
360
        }
361

    
362
        i += config_descr_len;
363
    }
364

    
365
    if (i >= dev->descr_len) {
366
        fprintf(stderr,
367
                "husb: update iface failed. no matching configuration\n");
368
        goto fail;
369
    }
370
    nb_interfaces = dev->descr[i + 4];
371

    
372
#ifdef USBDEVFS_DISCONNECT
373
    /* earlier Linux 2.4 do not support that */
374
    {
375
        struct usbdevfs_ioctl ctrl;
376
        for (interface = 0; interface < nb_interfaces; interface++) {
377
            ctrl.ioctl_code = USBDEVFS_DISCONNECT;
378
            ctrl.ifno = interface;
379
            ctrl.data = 0;
380
            ret = ioctl(dev->fd, USBDEVFS_IOCTL, &ctrl);
381
            if (ret < 0 && errno != ENODATA) {
382
                perror("USBDEVFS_DISCONNECT");
383
                goto fail;
384
            }
385
        }
386
    }
387
#endif
388

    
389
    /* XXX: only grab if all interfaces are free */
390
    for (interface = 0; interface < nb_interfaces; interface++) {
391
        ret = ioctl(dev->fd, USBDEVFS_CLAIMINTERFACE, &interface);
392
        if (ret < 0) {
393
            if (errno == EBUSY) {
394
                printf("husb: update iface. device already grabbed\n");
395
            } else {
396
                perror("husb: failed to claim interface");
397
            }
398
        fail:
399
            return 0;
400
        }
401
    }
402

    
403
    printf("husb: %d interfaces claimed for configuration %d\n",
404
           nb_interfaces, configuration);
405

    
406
    dev->ninterfaces   = nb_interfaces;
407
    dev->configuration = configuration;
408
    return 1;
409
}
410

    
411
static int usb_host_release_interfaces(USBHostDevice *s)
412
{
413
    int ret, i;
414

    
415
    DPRINTF("husb: releasing interfaces\n");
416

    
417
    for (i = 0; i < s->ninterfaces; i++) {
418
        ret = ioctl(s->fd, USBDEVFS_RELEASEINTERFACE, &i);
419
        if (ret < 0) {
420
            perror("husb: failed to release interface");
421
            return 0;
422
        }
423
    }
424

    
425
    return 1;
426
}
427

    
428
static void usb_host_handle_reset(USBDevice *dev)
429
{
430
    USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
431

    
432
    DPRINTF("husb: reset device %u.%u\n", s->bus_num, s->addr);
433

    
434
    ioctl(s->fd, USBDEVFS_RESET);
435

    
436
    usb_host_claim_interfaces(s, s->configuration);
437
}
438

    
439
static void usb_host_handle_destroy(USBDevice *dev)
440
{
441
    USBHostDevice *s = (USBHostDevice *)dev;
442

    
443
    usb_host_close(s);
444
    QTAILQ_REMOVE(&hostdevs, s, next);
445
    qemu_remove_exit_notifier(&s->exit);
446
}
447

    
448
static int usb_linux_update_endp_table(USBHostDevice *s);
449

    
450
/* iso data is special, we need to keep enough urbs in flight to make sure
451
   that the controller never runs out of them, otherwise the device will
452
   likely suffer a buffer underrun / overrun. */
453
static AsyncURB *usb_host_alloc_iso(USBHostDevice *s, uint8_t ep, int in)
454
{
455
    AsyncURB *aurb;
456
    int i, j, len = get_max_packet_size(s, ep);
457

    
458
    aurb = qemu_mallocz(ISO_URB_COUNT * sizeof(*aurb));
459
    for (i = 0; i < ISO_URB_COUNT; i++) {
460
        aurb[i].urb.endpoint      = ep;
461
        aurb[i].urb.buffer_length = ISO_FRAME_DESC_PER_URB * len;
462
        aurb[i].urb.buffer        = qemu_malloc(aurb[i].urb.buffer_length);
463
        aurb[i].urb.type          = USBDEVFS_URB_TYPE_ISO;
464
        aurb[i].urb.flags         = USBDEVFS_URB_ISO_ASAP;
465
        aurb[i].urb.number_of_packets = ISO_FRAME_DESC_PER_URB;
466
        for (j = 0 ; j < ISO_FRAME_DESC_PER_URB; j++)
467
            aurb[i].urb.iso_frame_desc[j].length = len;
468
        if (in) {
469
            aurb[i].urb.endpoint |= 0x80;
470
            /* Mark as fully consumed (idle) */
471
            aurb[i].iso_frame_idx = ISO_FRAME_DESC_PER_URB;
472
        }
473
    }
474
    set_iso_urb(s, ep, aurb);
475

    
476
    return aurb;
477
}
478

    
479
static void usb_host_stop_n_free_iso(USBHostDevice *s, uint8_t ep)
480
{
481
    AsyncURB *aurb;
482
    int i, ret, killed = 0, free = 1;
483

    
484
    aurb = get_iso_urb(s, ep);
485
    if (!aurb) {
486
        return;
487
    }
488

    
489
    for (i = 0; i < ISO_URB_COUNT; i++) {
490
        /* in flight? */
491
        if (aurb[i].iso_frame_idx == -1) {
492
            ret = ioctl(s->fd, USBDEVFS_DISCARDURB, &aurb[i]);
493
            if (ret < 0) {
494
                printf("husb: discard isoc in urb failed errno %d\n", errno);
495
                free = 0;
496
                continue;
497
            }
498
            killed++;
499
        }
500
    }
501

    
502
    /* Make sure any urbs we've killed are reaped before we free them */
503
    if (killed) {
504
        async_complete(s);
505
    }
506

    
507
    for (i = 0; i < ISO_URB_COUNT; i++) {
508
        qemu_free(aurb[i].urb.buffer);
509
    }
510

    
511
    if (free)
512
        qemu_free(aurb);
513
    else
514
        printf("husb: leaking iso urbs because of discard failure\n");
515
    set_iso_urb(s, ep, NULL);
516
    set_iso_urb_idx(s, ep, 0);
517
    clear_iso_started(s, ep);
518
}
519

    
520
static int urb_status_to_usb_ret(int status)
521
{
522
    switch (status) {
523
    case -EPIPE:
524
        return USB_RET_STALL;
525
    default:
526
        return USB_RET_NAK;
527
    }
528
}
529

    
530
static int usb_host_handle_iso_data(USBHostDevice *s, USBPacket *p, int in)
531
{
532
    AsyncURB *aurb;
533
    int i, j, ret, max_packet_size, offset, len = 0;
534

    
535
    max_packet_size = get_max_packet_size(s, p->devep);
536
    if (max_packet_size == 0)
537
        return USB_RET_NAK;
538

    
539
    aurb = get_iso_urb(s, p->devep);
540
    if (!aurb) {
541
        aurb = usb_host_alloc_iso(s, p->devep, in);
542
    }
543

    
544
    i = get_iso_urb_idx(s, p->devep);
545
    j = aurb[i].iso_frame_idx;
546
    if (j >= 0 && j < ISO_FRAME_DESC_PER_URB) {
547
        if (in) {
548
            /* Check urb status  */
549
            if (aurb[i].urb.status) {
550
                len = urb_status_to_usb_ret(aurb[i].urb.status);
551
                /* Move to the next urb */
552
                aurb[i].iso_frame_idx = ISO_FRAME_DESC_PER_URB - 1;
553
            /* Check frame status */
554
            } else if (aurb[i].urb.iso_frame_desc[j].status) {
555
                len = urb_status_to_usb_ret(
556
                                        aurb[i].urb.iso_frame_desc[j].status);
557
            /* Check the frame fits */
558
            } else if (aurb[i].urb.iso_frame_desc[j].actual_length > p->len) {
559
                printf("husb: received iso data is larger then packet\n");
560
                len = USB_RET_NAK;
561
            /* All good copy data over */
562
            } else {
563
                len = aurb[i].urb.iso_frame_desc[j].actual_length;
564
                memcpy(p->data,
565
                       aurb[i].urb.buffer +
566
                           j * aurb[i].urb.iso_frame_desc[0].length,
567
                       len);
568
            }
569
        } else {
570
            len = p->len;
571
            offset = (j == 0) ? 0 : get_iso_buffer_used(s, p->devep);
572

    
573
            /* Check the frame fits */
574
            if (len > max_packet_size) {
575
                printf("husb: send iso data is larger then max packet size\n");
576
                return USB_RET_NAK;
577
            }
578

    
579
            /* All good copy data over */
580
            memcpy(aurb[i].urb.buffer + offset, p->data, len);
581
            aurb[i].urb.iso_frame_desc[j].length = len;
582
            offset += len;
583
            set_iso_buffer_used(s, p->devep, offset);
584

    
585
            /* Start the stream once we have buffered enough data */
586
            if (!is_iso_started(s, p->devep) && i == 1 && j == 8) {
587
                set_iso_started(s, p->devep);
588
            }
589
        }
590
        aurb[i].iso_frame_idx++;
591
        if (aurb[i].iso_frame_idx == ISO_FRAME_DESC_PER_URB) {
592
            i = (i + 1) % ISO_URB_COUNT;
593
            set_iso_urb_idx(s, p->devep, i);
594
        }
595
    } else {
596
        if (in) {
597
            set_iso_started(s, p->devep);
598
        } else {
599
            DPRINTF("hubs: iso out error no free buffer, dropping packet\n");
600
        }
601
    }
602

    
603
    if (is_iso_started(s, p->devep)) {
604
        /* (Re)-submit all fully consumed / filled urbs */
605
        for (i = 0; i < ISO_URB_COUNT; i++) {
606
            if (aurb[i].iso_frame_idx == ISO_FRAME_DESC_PER_URB) {
607
                ret = ioctl(s->fd, USBDEVFS_SUBMITURB, &aurb[i]);
608
                if (ret < 0) {
609
                    printf("husb error submitting iso urb %d: %d\n", i, errno);
610
                    if (!in || len == 0) {
611
                        switch(errno) {
612
                        case ETIMEDOUT:
613
                            len = USB_RET_NAK;
614
                            break;
615
                        case EPIPE:
616
                        default:
617
                            len = USB_RET_STALL;
618
                        }
619
                    }
620
                    break;
621
                }
622
                aurb[i].iso_frame_idx = -1;
623
            }
624
        }
625
    }
626

    
627
    return len;
628
}
629

    
630
static int usb_host_handle_data(USBDevice *dev, USBPacket *p)
631
{
632
    USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
633
    struct usbdevfs_urb *urb;
634
    AsyncURB *aurb;
635
    int ret;
636
    uint8_t ep;
637

    
638
    if (!is_valid(s, p->devep)) {
639
        return USB_RET_NAK;
640
    }
641

    
642
    if (p->pid == USB_TOKEN_IN) {
643
        ep = p->devep | 0x80;
644
    } else {
645
        ep = p->devep;
646
    }
647

    
648
    if (is_halted(s, p->devep)) {
649
        ret = ioctl(s->fd, USBDEVFS_CLEAR_HALT, &ep);
650
        if (ret < 0) {
651
            DPRINTF("husb: failed to clear halt. ep 0x%x errno %d\n",
652
                   ep, errno);
653
            return USB_RET_NAK;
654
        }
655
        clear_halt(s, p->devep);
656
    }
657

    
658
    if (is_isoc(s, p->devep)) {
659
        return usb_host_handle_iso_data(s, p, p->pid == USB_TOKEN_IN);
660
    }
661

    
662
    aurb = async_alloc();
663
    aurb->hdev   = s;
664
    aurb->packet = p;
665

    
666
    urb = &aurb->urb;
667

    
668
    urb->endpoint      = ep;
669
    urb->buffer        = p->data;
670
    urb->buffer_length = p->len;
671
    urb->type          = USBDEVFS_URB_TYPE_BULK;
672
    urb->usercontext   = s;
673

    
674
    ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
675

    
676
    DPRINTF("husb: data submit. ep 0x%x len %u aurb %p\n",
677
            urb->endpoint, p->len, aurb);
678

    
679
    if (ret < 0) {
680
        DPRINTF("husb: submit failed. errno %d\n", errno);
681
        async_free(aurb);
682

    
683
        switch(errno) {
684
        case ETIMEDOUT:
685
            return USB_RET_NAK;
686
        case EPIPE:
687
        default:
688
            return USB_RET_STALL;
689
        }
690
    }
691

    
692
    usb_defer_packet(p, async_cancel, aurb);
693
    return USB_RET_ASYNC;
694
}
695

    
696
static int ctrl_error(void)
697
{
698
    if (errno == ETIMEDOUT) {
699
        return USB_RET_NAK;
700
    } else {
701
        return USB_RET_STALL;
702
    }
703
}
704

    
705
static int usb_host_set_address(USBHostDevice *s, int addr)
706
{
707
    DPRINTF("husb: ctrl set addr %u\n", addr);
708
    s->dev.addr = addr;
709
    return 0;
710
}
711

    
712
static int usb_host_set_config(USBHostDevice *s, int config)
713
{
714
    usb_host_release_interfaces(s);
715

    
716
    int ret = ioctl(s->fd, USBDEVFS_SETCONFIGURATION, &config);
717

    
718
    DPRINTF("husb: ctrl set config %d ret %d errno %d\n", config, ret, errno);
719

    
720
    if (ret < 0) {
721
        return ctrl_error();
722
    }
723
    usb_host_claim_interfaces(s, config);
724
    return 0;
725
}
726

    
727
static int usb_host_set_interface(USBHostDevice *s, int iface, int alt)
728
{
729
    struct usbdevfs_setinterface si;
730
    int i, ret;
731

    
732
    for (i = 1; i <= MAX_ENDPOINTS; i++) {
733
        if (is_isoc(s, i)) {
734
            usb_host_stop_n_free_iso(s, i);
735
        }
736
    }
737

    
738
    si.interface  = iface;
739
    si.altsetting = alt;
740
    ret = ioctl(s->fd, USBDEVFS_SETINTERFACE, &si);
741

    
742
    DPRINTF("husb: ctrl set iface %d altset %d ret %d errno %d\n",
743
            iface, alt, ret, errno);
744

    
745
    if (ret < 0) {
746
        return ctrl_error();
747
    }
748
    usb_linux_update_endp_table(s);
749
    return 0;
750
}
751

    
752
static int usb_host_handle_control(USBDevice *dev, USBPacket *p,
753
               int request, int value, int index, int length, uint8_t *data)
754
{
755
    USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
756
    struct usbdevfs_urb *urb;
757
    AsyncURB *aurb;
758
    int ret;
759

    
760
    /*
761
     * Process certain standard device requests.
762
     * These are infrequent and are processed synchronously.
763
     */
764

    
765
    /* Note request is (bRequestType << 8) | bRequest */
766
    DPRINTF("husb: ctrl type 0x%x req 0x%x val 0x%x index %u len %u\n",
767
            request >> 8, request & 0xff, value, index, length);
768

    
769
    switch (request) {
770
    case DeviceOutRequest | USB_REQ_SET_ADDRESS:
771
        return usb_host_set_address(s, value);
772

    
773
    case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
774
        return usb_host_set_config(s, value & 0xff);
775

    
776
    case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
777
        return usb_host_set_interface(s, index, value);
778
    }
779

    
780
    /* The rest are asynchronous */
781

    
782
    if (length > sizeof(dev->data_buf)) {
783
        fprintf(stderr, "husb: ctrl buffer too small (%d > %zu)\n",
784
                length, sizeof(dev->data_buf));
785
        return USB_RET_STALL;
786
    }
787

    
788
    aurb = async_alloc();
789
    aurb->hdev   = s;
790
    aurb->packet = p;
791

    
792
    /*
793
     * Setup ctrl transfer.
794
     *
795
     * s->ctrl is laid out such that data buffer immediately follows
796
     * 'req' struct which is exactly what usbdevfs expects.
797
     */
798
    urb = &aurb->urb;
799

    
800
    urb->type     = USBDEVFS_URB_TYPE_CONTROL;
801
    urb->endpoint = p->devep;
802

    
803
    urb->buffer        = &dev->setup_buf;
804
    urb->buffer_length = length + 8;
805

    
806
    urb->usercontext = s;
807

    
808
    ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
809

    
810
    DPRINTF("husb: submit ctrl. len %u aurb %p\n", urb->buffer_length, aurb);
811

    
812
    if (ret < 0) {
813
        DPRINTF("husb: submit failed. errno %d\n", errno);
814
        async_free(aurb);
815

    
816
        switch(errno) {
817
        case ETIMEDOUT:
818
            return USB_RET_NAK;
819
        case EPIPE:
820
        default:
821
            return USB_RET_STALL;
822
        }
823
    }
824

    
825
    usb_defer_packet(p, async_cancel, aurb);
826
    return USB_RET_ASYNC;
827
}
828

    
829
static int usb_linux_get_configuration(USBHostDevice *s)
830
{
831
    uint8_t configuration;
832
    struct usb_ctrltransfer ct;
833
    int ret;
834

    
835
    if (usb_fs_type == USB_FS_SYS) {
836
        char device_name[32], line[1024];
837
        int configuration;
838

    
839
        sprintf(device_name, "%d-%d", s->bus_num, s->devpath);
840

    
841
        if (!usb_host_read_file(line, sizeof(line), "bConfigurationValue",
842
                                device_name)) {
843
            goto usbdevfs;
844
        }
845
        if (sscanf(line, "%d", &configuration) != 1) {
846
            goto usbdevfs;
847
        }
848
        return configuration;
849
    }
850

    
851
usbdevfs:
852
    ct.bRequestType = USB_DIR_IN;
853
    ct.bRequest = USB_REQ_GET_CONFIGURATION;
854
    ct.wValue = 0;
855
    ct.wIndex = 0;
856
    ct.wLength = 1;
857
    ct.data = &configuration;
858
    ct.timeout = 50;
859

    
860
    ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
861
    if (ret < 0) {
862
        perror("usb_linux_get_configuration");
863
        return -1;
864
    }
865

    
866
    /* in address state */
867
    if (configuration == 0) {
868
        return -1;
869
    }
870

    
871
    return configuration;
872
}
873

    
874
static uint8_t usb_linux_get_alt_setting(USBHostDevice *s,
875
    uint8_t configuration, uint8_t interface)
876
{
877
    uint8_t alt_setting;
878
    struct usb_ctrltransfer ct;
879
    int ret;
880

    
881
    if (usb_fs_type == USB_FS_SYS) {
882
        char device_name[64], line[1024];
883
        int alt_setting;
884

    
885
        sprintf(device_name, "%d-%d:%d.%d", s->bus_num, s->devpath,
886
                (int)configuration, (int)interface);
887

    
888
        if (!usb_host_read_file(line, sizeof(line), "bAlternateSetting",
889
                                device_name)) {
890
            goto usbdevfs;
891
        }
892
        if (sscanf(line, "%d", &alt_setting) != 1) {
893
            goto usbdevfs;
894
        }
895
        return alt_setting;
896
    }
897

    
898
usbdevfs:
899
    ct.bRequestType = USB_DIR_IN | USB_RECIP_INTERFACE;
900
    ct.bRequest = USB_REQ_GET_INTERFACE;
901
    ct.wValue = 0;
902
    ct.wIndex = interface;
903
    ct.wLength = 1;
904
    ct.data = &alt_setting;
905
    ct.timeout = 50;
906
    ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
907
    if (ret < 0) {
908
        /* Assume alt 0 on error */
909
        return 0;
910
    }
911

    
912
    return alt_setting;
913
}
914

    
915
/* returns 1 on problem encountered or 0 for success */
916
static int usb_linux_update_endp_table(USBHostDevice *s)
917
{
918
    uint8_t *descriptors;
919
    uint8_t devep, type, configuration, alt_interface;
920
    int interface, length, i;
921

    
922
    for (i = 0; i < MAX_ENDPOINTS; i++)
923
        s->endp_table[i].type = INVALID_EP_TYPE;
924

    
925
    i = usb_linux_get_configuration(s);
926
    if (i < 0)
927
        return 1;
928
    configuration = i;
929

    
930
    /* get the desired configuration, interface, and endpoint descriptors
931
     * from device description */
932
    descriptors = &s->descr[18];
933
    length = s->descr_len - 18;
934
    i = 0;
935

    
936
    if (descriptors[i + 1] != USB_DT_CONFIG ||
937
        descriptors[i + 5] != configuration) {
938
        DPRINTF("invalid descriptor data - configuration\n");
939
        return 1;
940
    }
941
    i += descriptors[i];
942

    
943
    while (i < length) {
944
        if (descriptors[i + 1] != USB_DT_INTERFACE ||
945
            (descriptors[i + 1] == USB_DT_INTERFACE &&
946
             descriptors[i + 4] == 0)) {
947
            i += descriptors[i];
948
            continue;
949
        }
950

    
951
        interface = descriptors[i + 2];
952
        alt_interface = usb_linux_get_alt_setting(s, configuration, interface);
953

    
954
        /* the current interface descriptor is the active interface
955
         * and has endpoints */
956
        if (descriptors[i + 3] != alt_interface) {
957
            i += descriptors[i];
958
            continue;
959
        }
960

    
961
        /* advance to the endpoints */
962
        while (i < length && descriptors[i +1] != USB_DT_ENDPOINT) {
963
            i += descriptors[i];
964
        }
965

    
966
        if (i >= length)
967
            break;
968

    
969
        while (i < length) {
970
            if (descriptors[i + 1] != USB_DT_ENDPOINT) {
971
                break;
972
            }
973

    
974
            devep = descriptors[i + 2];
975
            switch (descriptors[i + 3] & 0x3) {
976
            case 0x00:
977
                type = USBDEVFS_URB_TYPE_CONTROL;
978
                break;
979
            case 0x01:
980
                type = USBDEVFS_URB_TYPE_ISO;
981
                s->endp_table[(devep & 0xf) - 1].max_packet_size =
982
                    descriptors[i + 4] + (descriptors[i + 5] << 8);
983
                break;
984
            case 0x02:
985
                type = USBDEVFS_URB_TYPE_BULK;
986
                break;
987
            case 0x03:
988
                type = USBDEVFS_URB_TYPE_INTERRUPT;
989
                break;
990
            default:
991
                DPRINTF("usb_host: malformed endpoint type\n");
992
                type = USBDEVFS_URB_TYPE_BULK;
993
            }
994
            s->endp_table[(devep & 0xf) - 1].type = type;
995
            s->endp_table[(devep & 0xf) - 1].halted = 0;
996

    
997
            i += descriptors[i];
998
        }
999
    }
1000
    return 0;
1001
}
1002

    
1003
static int usb_host_open(USBHostDevice *dev, int bus_num,
1004
                         int addr, int devpath, const char *prod_name)
1005
{
1006
    int fd = -1, ret;
1007
    struct usbdevfs_connectinfo ci;
1008
    char buf[1024];
1009

    
1010
    if (dev->fd != -1) {
1011
        goto fail;
1012
    }
1013
    printf("husb: open device %d.%d\n", bus_num, addr);
1014

    
1015
    if (!usb_host_device_path) {
1016
        perror("husb: USB Host Device Path not set");
1017
        goto fail;
1018
    }
1019
    snprintf(buf, sizeof(buf), "%s/%03d/%03d", usb_host_device_path,
1020
             bus_num, addr);
1021
    fd = open(buf, O_RDWR | O_NONBLOCK);
1022
    if (fd < 0) {
1023
        perror(buf);
1024
        goto fail;
1025
    }
1026
    DPRINTF("husb: opened %s\n", buf);
1027

    
1028
    dev->bus_num = bus_num;
1029
    dev->addr = addr;
1030
    dev->devpath = devpath;
1031
    dev->fd = fd;
1032

    
1033
    /* read the device description */
1034
    dev->descr_len = read(fd, dev->descr, sizeof(dev->descr));
1035
    if (dev->descr_len <= 0) {
1036
        perror("husb: reading device data failed");
1037
        goto fail;
1038
    }
1039

    
1040
#ifdef DEBUG
1041
    {
1042
        int x;
1043
        printf("=== begin dumping device descriptor data ===\n");
1044
        for (x = 0; x < dev->descr_len; x++) {
1045
            printf("%02x ", dev->descr[x]);
1046
        }
1047
        printf("\n=== end dumping device descriptor data ===\n");
1048
    }
1049
#endif
1050

    
1051

    
1052
    /*
1053
     * Initial configuration is -1 which makes us claim first
1054
     * available config. We used to start with 1, which does not
1055
     * always work. I've seen devices where first config starts
1056
     * with 2.
1057
     */
1058
    if (!usb_host_claim_interfaces(dev, -1)) {
1059
        goto fail;
1060
    }
1061

    
1062
    ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci);
1063
    if (ret < 0) {
1064
        perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
1065
        goto fail;
1066
    }
1067

    
1068
    printf("husb: grabbed usb device %d.%d\n", bus_num, addr);
1069

    
1070
    ret = usb_linux_update_endp_table(dev);
1071
    if (ret) {
1072
        goto fail;
1073
    }
1074

    
1075
    if (ci.slow) {
1076
        dev->dev.speed = USB_SPEED_LOW;
1077
    } else {
1078
        dev->dev.speed = USB_SPEED_HIGH;
1079
    }
1080

    
1081
    if (!prod_name || prod_name[0] == '\0') {
1082
        snprintf(dev->dev.product_desc, sizeof(dev->dev.product_desc),
1083
                 "host:%d.%d", bus_num, addr);
1084
    } else {
1085
        pstrcpy(dev->dev.product_desc, sizeof(dev->dev.product_desc),
1086
                prod_name);
1087
    }
1088

    
1089
    /* USB devio uses 'write' flag to check for async completions */
1090
    qemu_set_fd_handler(dev->fd, NULL, async_complete, dev);
1091

    
1092
    usb_device_attach(&dev->dev);
1093
    return 0;
1094

    
1095
fail:
1096
    dev->fd = -1;
1097
    if (fd != -1) {
1098
        close(fd);
1099
    }
1100
    return -1;
1101
}
1102

    
1103
static int usb_host_close(USBHostDevice *dev)
1104
{
1105
    int i;
1106

    
1107
    if (dev->fd == -1) {
1108
        return -1;
1109
    }
1110

    
1111
    qemu_set_fd_handler(dev->fd, NULL, NULL, NULL);
1112
    dev->closing = 1;
1113
    for (i = 1; i <= MAX_ENDPOINTS; i++) {
1114
        if (is_isoc(dev, i)) {
1115
            usb_host_stop_n_free_iso(dev, i);
1116
        }
1117
    }
1118
    async_complete(dev);
1119
    dev->closing = 0;
1120
    usb_device_detach(&dev->dev);
1121
    ioctl(dev->fd, USBDEVFS_RESET);
1122
    close(dev->fd);
1123
    dev->fd = -1;
1124
    return 0;
1125
}
1126

    
1127
static void usb_host_exit_notifier(struct Notifier* n)
1128
{
1129
    USBHostDevice *s = container_of(n, USBHostDevice, exit);
1130

    
1131
    if (s->fd != -1) {
1132
        ioctl(s->fd, USBDEVFS_RESET);
1133
    }
1134
}
1135

    
1136
static int usb_host_initfn(USBDevice *dev)
1137
{
1138
    USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
1139

    
1140
    dev->auto_attach = 0;
1141
    s->fd = -1;
1142
    QTAILQ_INSERT_TAIL(&hostdevs, s, next);
1143
    s->exit.notify = usb_host_exit_notifier;
1144
    qemu_add_exit_notifier(&s->exit);
1145
    usb_host_auto_check(NULL);
1146
    return 0;
1147
}
1148

    
1149
static struct USBDeviceInfo usb_host_dev_info = {
1150
    .product_desc   = "USB Host Device",
1151
    .qdev.name      = "usb-host",
1152
    .qdev.size      = sizeof(USBHostDevice),
1153
    .init           = usb_host_initfn,
1154
    .handle_packet  = usb_generic_handle_packet,
1155
    .handle_data    = usb_host_handle_data,
1156
    .handle_control = usb_host_handle_control,
1157
    .handle_reset   = usb_host_handle_reset,
1158
    .handle_destroy = usb_host_handle_destroy,
1159
    .usbdevice_name = "host",
1160
    .usbdevice_init = usb_host_device_open,
1161
    .qdev.props     = (Property[]) {
1162
        DEFINE_PROP_UINT32("hostbus",  USBHostDevice, match.bus_num,    0),
1163
        DEFINE_PROP_UINT32("hostaddr", USBHostDevice, match.addr,       0),
1164
        DEFINE_PROP_HEX32("vendorid",  USBHostDevice, match.vendor_id,  0),
1165
        DEFINE_PROP_HEX32("productid", USBHostDevice, match.product_id, 0),
1166
        DEFINE_PROP_END_OF_LIST(),
1167
    },
1168
};
1169

    
1170
static void usb_host_register_devices(void)
1171
{
1172
    usb_qdev_register(&usb_host_dev_info);
1173
}
1174
device_init(usb_host_register_devices)
1175

    
1176
USBDevice *usb_host_device_open(const char *devname)
1177
{
1178
    struct USBAutoFilter filter;
1179
    USBDevice *dev;
1180
    char *p;
1181

    
1182
    dev = usb_create(NULL /* FIXME */, "usb-host");
1183

    
1184
    if (strstr(devname, "auto:")) {
1185
        if (parse_filter(devname, &filter) < 0) {
1186
            goto fail;
1187
        }
1188
    } else {
1189
        if ((p = strchr(devname, '.'))) {
1190
            filter.bus_num    = strtoul(devname, NULL, 0);
1191
            filter.addr       = strtoul(p + 1, NULL, 0);
1192
            filter.vendor_id  = 0;
1193
            filter.product_id = 0;
1194
        } else if ((p = strchr(devname, ':'))) {
1195
            filter.bus_num    = 0;
1196
            filter.addr       = 0;
1197
            filter.vendor_id  = strtoul(devname, NULL, 16);
1198
            filter.product_id = strtoul(p + 1, NULL, 16);
1199
        } else {
1200
            goto fail;
1201
        }
1202
    }
1203

    
1204
    qdev_prop_set_uint32(&dev->qdev, "hostbus",   filter.bus_num);
1205
    qdev_prop_set_uint32(&dev->qdev, "hostaddr",  filter.addr);
1206
    qdev_prop_set_uint32(&dev->qdev, "vendorid",  filter.vendor_id);
1207
    qdev_prop_set_uint32(&dev->qdev, "productid", filter.product_id);
1208
    qdev_init_nofail(&dev->qdev);
1209
    return dev;
1210

    
1211
fail:
1212
    qdev_free(&dev->qdev);
1213
    return NULL;
1214
}
1215

    
1216
int usb_host_device_close(const char *devname)
1217
{
1218
#if 0
1219
    char product_name[PRODUCT_NAME_SZ];
1220
    int bus_num, addr;
1221
    USBHostDevice *s;
1222

1223
    if (strstr(devname, "auto:")) {
1224
        return usb_host_auto_del(devname);
1225
    }
1226
    if (usb_host_find_device(&bus_num, &addr, product_name,
1227
                                    sizeof(product_name), devname) < 0) {
1228
        return -1;
1229
    }
1230
    s = hostdev_find(bus_num, addr);
1231
    if (s) {
1232
        usb_device_delete_addr(s->bus_num, s->dev.addr);
1233
        return 0;
1234
    }
1235
#endif
1236

    
1237
    return -1;
1238
}
1239

    
1240
static int get_tag_value(char *buf, int buf_size,
1241
                         const char *str, const char *tag,
1242
                         const char *stopchars)
1243
{
1244
    const char *p;
1245
    char *q;
1246
    p = strstr(str, tag);
1247
    if (!p) {
1248
        return -1;
1249
    }
1250
    p += strlen(tag);
1251
    while (qemu_isspace(*p)) {
1252
        p++;
1253
    }
1254
    q = buf;
1255
    while (*p != '\0' && !strchr(stopchars, *p)) {
1256
        if ((q - buf) < (buf_size - 1)) {
1257
            *q++ = *p;
1258
        }
1259
        p++;
1260
    }
1261
    *q = '\0';
1262
    return q - buf;
1263
}
1264

    
1265
/*
1266
 * Use /proc/bus/usb/devices or /dev/bus/usb/devices file to determine
1267
 * host's USB devices. This is legacy support since many distributions
1268
 * are moving to /sys/bus/usb
1269
 */
1270
static int usb_host_scan_dev(void *opaque, USBScanFunc *func)
1271
{
1272
    FILE *f = NULL;
1273
    char line[1024];
1274
    char buf[1024];
1275
    int bus_num, addr, speed, device_count, class_id, product_id, vendor_id;
1276
    char product_name[512];
1277
    int ret = 0;
1278

    
1279
    if (!usb_host_device_path) {
1280
        perror("husb: USB Host Device Path not set");
1281
        goto the_end;
1282
    }
1283
    snprintf(line, sizeof(line), "%s/devices", usb_host_device_path);
1284
    f = fopen(line, "r");
1285
    if (!f) {
1286
        perror("husb: cannot open devices file");
1287
        goto the_end;
1288
    }
1289

    
1290
    device_count = 0;
1291
    bus_num = addr = speed = class_id = product_id = vendor_id = 0;
1292
    for(;;) {
1293
        if (fgets(line, sizeof(line), f) == NULL) {
1294
            break;
1295
        }
1296
        if (strlen(line) > 0) {
1297
            line[strlen(line) - 1] = '\0';
1298
        }
1299
        if (line[0] == 'T' && line[1] == ':') {
1300
            if (device_count && (vendor_id || product_id)) {
1301
                /* New device.  Add the previously discovered device.  */
1302
                ret = func(opaque, bus_num, addr, 0, class_id, vendor_id,
1303
                           product_id, product_name, speed);
1304
                if (ret) {
1305
                    goto the_end;
1306
                }
1307
            }
1308
            if (get_tag_value(buf, sizeof(buf), line, "Bus=", " ") < 0) {
1309
                goto fail;
1310
            }
1311
            bus_num = atoi(buf);
1312
            if (get_tag_value(buf, sizeof(buf), line, "Dev#=", " ") < 0) {
1313
                goto fail;
1314
            }
1315
            addr = atoi(buf);
1316
            if (get_tag_value(buf, sizeof(buf), line, "Spd=", " ") < 0) {
1317
                goto fail;
1318
            }
1319
            if (!strcmp(buf, "480")) {
1320
                speed = USB_SPEED_HIGH;
1321
            } else if (!strcmp(buf, "1.5")) {
1322
                speed = USB_SPEED_LOW;
1323
            } else {
1324
                speed = USB_SPEED_FULL;
1325
            }
1326
            product_name[0] = '\0';
1327
            class_id = 0xff;
1328
            device_count++;
1329
            product_id = 0;
1330
            vendor_id = 0;
1331
        } else if (line[0] == 'P' && line[1] == ':') {
1332
            if (get_tag_value(buf, sizeof(buf), line, "Vendor=", " ") < 0) {
1333
                goto fail;
1334
            }
1335
            vendor_id = strtoul(buf, NULL, 16);
1336
            if (get_tag_value(buf, sizeof(buf), line, "ProdID=", " ") < 0) {
1337
                goto fail;
1338
            }
1339
            product_id = strtoul(buf, NULL, 16);
1340
        } else if (line[0] == 'S' && line[1] == ':') {
1341
            if (get_tag_value(buf, sizeof(buf), line, "Product=", "") < 0) {
1342
                goto fail;
1343
            }
1344
            pstrcpy(product_name, sizeof(product_name), buf);
1345
        } else if (line[0] == 'D' && line[1] == ':') {
1346
            if (get_tag_value(buf, sizeof(buf), line, "Cls=", " (") < 0) {
1347
                goto fail;
1348
            }
1349
            class_id = strtoul(buf, NULL, 16);
1350
        }
1351
    fail: ;
1352
    }
1353
    if (device_count && (vendor_id || product_id)) {
1354
        /* Add the last device.  */
1355
        ret = func(opaque, bus_num, addr, 0, class_id, vendor_id,
1356
                   product_id, product_name, speed);
1357
    }
1358
 the_end:
1359
    if (f) {
1360
        fclose(f);
1361
    }
1362
    return ret;
1363
}
1364

    
1365
/*
1366
 * Read sys file-system device file
1367
 *
1368
 * @line address of buffer to put file contents in
1369
 * @line_size size of line
1370
 * @device_file path to device file (printf format string)
1371
 * @device_name device being opened (inserted into device_file)
1372
 *
1373
 * @return 0 failed, 1 succeeded ('line' contains data)
1374
 */
1375
static int usb_host_read_file(char *line, size_t line_size,
1376
                              const char *device_file, const char *device_name)
1377
{
1378
    FILE *f;
1379
    int ret = 0;
1380
    char filename[PATH_MAX];
1381

    
1382
    snprintf(filename, PATH_MAX, USBSYSBUS_PATH "/devices/%s/%s", device_name,
1383
             device_file);
1384
    f = fopen(filename, "r");
1385
    if (f) {
1386
        ret = fgets(line, line_size, f) != NULL;
1387
        fclose(f);
1388
    }
1389

    
1390
    return ret;
1391
}
1392

    
1393
/*
1394
 * Use /sys/bus/usb/devices/ directory to determine host's USB
1395
 * devices.
1396
 *
1397
 * This code is based on Robert Schiele's original patches posted to
1398
 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
1399
 */
1400
static int usb_host_scan_sys(void *opaque, USBScanFunc *func)
1401
{
1402
    DIR *dir = NULL;
1403
    char line[1024];
1404
    int bus_num, addr, devpath, speed, class_id, product_id, vendor_id;
1405
    int ret = 0;
1406
    char product_name[512];
1407
    struct dirent *de;
1408

    
1409
    dir = opendir(USBSYSBUS_PATH "/devices");
1410
    if (!dir) {
1411
        perror("husb: cannot open devices directory");
1412
        goto the_end;
1413
    }
1414

    
1415
    while ((de = readdir(dir))) {
1416
        if (de->d_name[0] != '.' && !strchr(de->d_name, ':')) {
1417
            char *tmpstr = de->d_name;
1418
            if (!strncmp(de->d_name, "usb", 3)) {
1419
                tmpstr += 3;
1420
            }
1421
            if (sscanf(tmpstr, "%d-%d", &bus_num, &devpath) < 1) {
1422
                goto the_end;
1423
            }
1424

    
1425
            if (!usb_host_read_file(line, sizeof(line), "devnum", de->d_name)) {
1426
                goto the_end;
1427
            }
1428
            if (sscanf(line, "%d", &addr) != 1) {
1429
                goto the_end;
1430
            }
1431
            if (!usb_host_read_file(line, sizeof(line), "bDeviceClass",
1432
                                    de->d_name)) {
1433
                goto the_end;
1434
            }
1435
            if (sscanf(line, "%x", &class_id) != 1) {
1436
                goto the_end;
1437
            }
1438

    
1439
            if (!usb_host_read_file(line, sizeof(line), "idVendor",
1440
                                    de->d_name)) {
1441
                goto the_end;
1442
            }
1443
            if (sscanf(line, "%x", &vendor_id) != 1) {
1444
                goto the_end;
1445
            }
1446
            if (!usb_host_read_file(line, sizeof(line), "idProduct",
1447
                                    de->d_name)) {
1448
                goto the_end;
1449
            }
1450
            if (sscanf(line, "%x", &product_id) != 1) {
1451
                goto the_end;
1452
            }
1453
            if (!usb_host_read_file(line, sizeof(line), "product",
1454
                                    de->d_name)) {
1455
                *product_name = 0;
1456
            } else {
1457
                if (strlen(line) > 0) {
1458
                    line[strlen(line) - 1] = '\0';
1459
                }
1460
                pstrcpy(product_name, sizeof(product_name), line);
1461
            }
1462

    
1463
            if (!usb_host_read_file(line, sizeof(line), "speed", de->d_name)) {
1464
                goto the_end;
1465
            }
1466
            if (!strcmp(line, "480\n")) {
1467
                speed = USB_SPEED_HIGH;
1468
            } else if (!strcmp(line, "1.5\n")) {
1469
                speed = USB_SPEED_LOW;
1470
            } else {
1471
                speed = USB_SPEED_FULL;
1472
            }
1473

    
1474
            ret = func(opaque, bus_num, addr, devpath, class_id, vendor_id,
1475
                       product_id, product_name, speed);
1476
            if (ret) {
1477
                goto the_end;
1478
            }
1479
        }
1480
    }
1481
 the_end:
1482
    if (dir) {
1483
        closedir(dir);
1484
    }
1485
    return ret;
1486
}
1487

    
1488
/*
1489
 * Determine how to access the host's USB devices and call the
1490
 * specific support function.
1491
 */
1492
static int usb_host_scan(void *opaque, USBScanFunc *func)
1493
{
1494
    Monitor *mon = cur_mon;
1495
    FILE *f = NULL;
1496
    DIR *dir = NULL;
1497
    int ret = 0;
1498
    const char *fs_type[] = {"unknown", "proc", "dev", "sys"};
1499
    char devpath[PATH_MAX];
1500

    
1501
    /* only check the host once */
1502
    if (!usb_fs_type) {
1503
        dir = opendir(USBSYSBUS_PATH "/devices");
1504
        if (dir) {
1505
            /* devices found in /dev/bus/usb/ (yes - not a mistake!) */
1506
            strcpy(devpath, USBDEVBUS_PATH);
1507
            usb_fs_type = USB_FS_SYS;
1508
            closedir(dir);
1509
            DPRINTF(USBDBG_DEVOPENED, USBSYSBUS_PATH);
1510
            goto found_devices;
1511
        }
1512
        f = fopen(USBPROCBUS_PATH "/devices", "r");
1513
        if (f) {
1514
            /* devices found in /proc/bus/usb/ */
1515
            strcpy(devpath, USBPROCBUS_PATH);
1516
            usb_fs_type = USB_FS_PROC;
1517
            fclose(f);
1518
            DPRINTF(USBDBG_DEVOPENED, USBPROCBUS_PATH);
1519
            goto found_devices;
1520
        }
1521
        /* try additional methods if an access method hasn't been found yet */
1522
        f = fopen(USBDEVBUS_PATH "/devices", "r");
1523
        if (f) {
1524
            /* devices found in /dev/bus/usb/ */
1525
            strcpy(devpath, USBDEVBUS_PATH);
1526
            usb_fs_type = USB_FS_DEV;
1527
            fclose(f);
1528
            DPRINTF(USBDBG_DEVOPENED, USBDEVBUS_PATH);
1529
            goto found_devices;
1530
        }
1531
    found_devices:
1532
        if (!usb_fs_type) {
1533
            if (mon) {
1534
                monitor_printf(mon, "husb: unable to access USB devices\n");
1535
            }
1536
            return -ENOENT;
1537
        }
1538

    
1539
        /* the module setting (used later for opening devices) */
1540
        usb_host_device_path = qemu_mallocz(strlen(devpath)+1);
1541
        strcpy(usb_host_device_path, devpath);
1542
        if (mon) {
1543
            monitor_printf(mon, "husb: using %s file-system with %s\n",
1544
                           fs_type[usb_fs_type], usb_host_device_path);
1545
        }
1546
    }
1547

    
1548
    switch (usb_fs_type) {
1549
    case USB_FS_PROC:
1550
    case USB_FS_DEV:
1551
        ret = usb_host_scan_dev(opaque, func);
1552
        break;
1553
    case USB_FS_SYS:
1554
        ret = usb_host_scan_sys(opaque, func);
1555
        break;
1556
    default:
1557
        ret = -EINVAL;
1558
        break;
1559
    }
1560
    return ret;
1561
}
1562

    
1563
static QEMUTimer *usb_auto_timer;
1564

    
1565
static int usb_host_auto_scan(void *opaque, int bus_num, int addr, int devpath,
1566
                              int class_id, int vendor_id, int product_id,
1567
                              const char *product_name, int speed)
1568
{
1569
    struct USBAutoFilter *f;
1570
    struct USBHostDevice *s;
1571

    
1572
    /* Ignore hubs */
1573
    if (class_id == 9)
1574
        return 0;
1575

    
1576
    QTAILQ_FOREACH(s, &hostdevs, next) {
1577
        f = &s->match;
1578

    
1579
        if (f->bus_num > 0 && f->bus_num != bus_num) {
1580
            continue;
1581
        }
1582
        if (f->addr > 0 && f->addr != addr) {
1583
            continue;
1584
        }
1585

    
1586
        if (f->vendor_id > 0 && f->vendor_id != vendor_id) {
1587
            continue;
1588
        }
1589

    
1590
        if (f->product_id > 0 && f->product_id != product_id) {
1591
            continue;
1592
        }
1593
        /* We got a match */
1594

    
1595
        /* Already attached ? */
1596
        if (s->fd != -1) {
1597
            return 0;
1598
        }
1599
        DPRINTF("husb: auto open: bus_num %d addr %d\n", bus_num, addr);
1600

    
1601
        usb_host_open(s, bus_num, addr, devpath, product_name);
1602
    }
1603

    
1604
    return 0;
1605
}
1606

    
1607
static void usb_host_auto_check(void *unused)
1608
{
1609
    struct USBHostDevice *s;
1610
    int unconnected = 0;
1611

    
1612
    usb_host_scan(NULL, usb_host_auto_scan);
1613

    
1614
    QTAILQ_FOREACH(s, &hostdevs, next) {
1615
        if (s->fd == -1) {
1616
            unconnected++;
1617
        }
1618
    }
1619

    
1620
    if (unconnected == 0) {
1621
        /* nothing to watch */
1622
        if (usb_auto_timer) {
1623
            qemu_del_timer(usb_auto_timer);
1624
        }
1625
        return;
1626
    }
1627

    
1628
    if (!usb_auto_timer) {
1629
        usb_auto_timer = qemu_new_timer_ms(rt_clock, usb_host_auto_check, NULL);
1630
        if (!usb_auto_timer) {
1631
            return;
1632
        }
1633
    }
1634
    qemu_mod_timer(usb_auto_timer, qemu_get_clock_ms(rt_clock) + 2000);
1635
}
1636

    
1637
/*
1638
 * Autoconnect filter
1639
 * Format:
1640
 *    auto:bus:dev[:vid:pid]
1641
 *    auto:bus.dev[:vid:pid]
1642
 *
1643
 *    bus  - bus number    (dec, * means any)
1644
 *    dev  - device number (dec, * means any)
1645
 *    vid  - vendor id     (hex, * means any)
1646
 *    pid  - product id    (hex, * means any)
1647
 *
1648
 *    See 'lsusb' output.
1649
 */
1650
static int parse_filter(const char *spec, struct USBAutoFilter *f)
1651
{
1652
    enum { BUS, DEV, VID, PID, DONE };
1653
    const char *p = spec;
1654
    int i;
1655

    
1656
    f->bus_num    = 0;
1657
    f->addr       = 0;
1658
    f->vendor_id  = 0;
1659
    f->product_id = 0;
1660

    
1661
    for (i = BUS; i < DONE; i++) {
1662
        p = strpbrk(p, ":.");
1663
        if (!p) {
1664
            break;
1665
        }
1666
        p++;
1667

    
1668
        if (*p == '*') {
1669
            continue;
1670
        }
1671
        switch(i) {
1672
        case BUS: f->bus_num = strtol(p, NULL, 10);    break;
1673
        case DEV: f->addr    = strtol(p, NULL, 10);    break;
1674
        case VID: f->vendor_id  = strtol(p, NULL, 16); break;
1675
        case PID: f->product_id = strtol(p, NULL, 16); break;
1676
        }
1677
    }
1678

    
1679
    if (i < DEV) {
1680
        fprintf(stderr, "husb: invalid auto filter spec %s\n", spec);
1681
        return -1;
1682
    }
1683

    
1684
    return 0;
1685
}
1686

    
1687
/**********************/
1688
/* USB host device info */
1689

    
1690
struct usb_class_info {
1691
    int class;
1692
    const char *class_name;
1693
};
1694

    
1695
static const struct usb_class_info usb_class_info[] = {
1696
    { USB_CLASS_AUDIO, "Audio"},
1697
    { USB_CLASS_COMM, "Communication"},
1698
    { USB_CLASS_HID, "HID"},
1699
    { USB_CLASS_HUB, "Hub" },
1700
    { USB_CLASS_PHYSICAL, "Physical" },
1701
    { USB_CLASS_PRINTER, "Printer" },
1702
    { USB_CLASS_MASS_STORAGE, "Storage" },
1703
    { USB_CLASS_CDC_DATA, "Data" },
1704
    { USB_CLASS_APP_SPEC, "Application Specific" },
1705
    { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
1706
    { USB_CLASS_STILL_IMAGE, "Still Image" },
1707
    { USB_CLASS_CSCID, "Smart Card" },
1708
    { USB_CLASS_CONTENT_SEC, "Content Security" },
1709
    { -1, NULL }
1710
};
1711

    
1712
static const char *usb_class_str(uint8_t class)
1713
{
1714
    const struct usb_class_info *p;
1715
    for(p = usb_class_info; p->class != -1; p++) {
1716
        if (p->class == class) {
1717
            break;
1718
        }
1719
    }
1720
    return p->class_name;
1721
}
1722

    
1723
static void usb_info_device(Monitor *mon, int bus_num, int addr, int class_id,
1724
                            int vendor_id, int product_id,
1725
                            const char *product_name,
1726
                            int speed)
1727
{
1728
    const char *class_str, *speed_str;
1729

    
1730
    switch(speed) {
1731
    case USB_SPEED_LOW:
1732
        speed_str = "1.5";
1733
        break;
1734
    case USB_SPEED_FULL:
1735
        speed_str = "12";
1736
        break;
1737
    case USB_SPEED_HIGH:
1738
        speed_str = "480";
1739
        break;
1740
    default:
1741
        speed_str = "?";
1742
        break;
1743
    }
1744

    
1745
    monitor_printf(mon, "  Device %d.%d, speed %s Mb/s\n",
1746
                bus_num, addr, speed_str);
1747
    class_str = usb_class_str(class_id);
1748
    if (class_str) {
1749
        monitor_printf(mon, "    %s:", class_str);
1750
    } else {
1751
        monitor_printf(mon, "    Class %02x:", class_id);
1752
    }
1753
    monitor_printf(mon, " USB device %04x:%04x", vendor_id, product_id);
1754
    if (product_name[0] != '\0') {
1755
        monitor_printf(mon, ", %s", product_name);
1756
    }
1757
    monitor_printf(mon, "\n");
1758
}
1759

    
1760
static int usb_host_info_device(void *opaque, int bus_num, int addr,
1761
                                int devpath, int class_id,
1762
                                int vendor_id, int product_id,
1763
                                const char *product_name,
1764
                                int speed)
1765
{
1766
    Monitor *mon = opaque;
1767

    
1768
    usb_info_device(mon, bus_num, addr, class_id, vendor_id, product_id,
1769
                    product_name, speed);
1770
    return 0;
1771
}
1772

    
1773
static void dec2str(int val, char *str, size_t size)
1774
{
1775
    if (val == 0) {
1776
        snprintf(str, size, "*");
1777
    } else {
1778
        snprintf(str, size, "%d", val);
1779
    }
1780
}
1781

    
1782
static void hex2str(int val, char *str, size_t size)
1783
{
1784
    if (val == 0) {
1785
        snprintf(str, size, "*");
1786
    } else {
1787
        snprintf(str, size, "%04x", val);
1788
    }
1789
}
1790

    
1791
void usb_host_info(Monitor *mon)
1792
{
1793
    struct USBAutoFilter *f;
1794
    struct USBHostDevice *s;
1795

    
1796
    usb_host_scan(mon, usb_host_info_device);
1797

    
1798
    if (QTAILQ_EMPTY(&hostdevs)) {
1799
        return;
1800
    }
1801

    
1802
    monitor_printf(mon, "  Auto filters:\n");
1803
    QTAILQ_FOREACH(s, &hostdevs, next) {
1804
        char bus[10], addr[10], vid[10], pid[10];
1805
        f = &s->match;
1806
        dec2str(f->bus_num, bus, sizeof(bus));
1807
        dec2str(f->addr, addr, sizeof(addr));
1808
        hex2str(f->vendor_id, vid, sizeof(vid));
1809
        hex2str(f->product_id, pid, sizeof(pid));
1810
        monitor_printf(mon, "    Device %s.%s ID %s:%s\n",
1811
                       bus, addr, vid, pid);
1812
    }
1813
}