~jderose/ubuntu/raring/qemu/vde-again

« back to all changes in this revision

Viewing changes to usb-linux.c

  • Committer: Bazaar Package Importer
  • Author(s): Aurelien Jarno, Aurelien Jarno
  • Date: 2009-03-07 06:20:34 UTC
  • mfrom: (1.1.9 upstream)
  • mto: This revision was merged to the branch mainline in revision 7.
  • Revision ID: james.westby@ubuntu.com-20090307062034-i3pead4mw653v2el
Tags: 0.10.0-1
[ Aurelien Jarno ]
* New upstream release:
  - Fix fr-be keyboard mapping (closes: bug#514462).
  - Fix stat64 structure on ppc-linux-user (closes: bug#470231).
  - Add a chroot option (closes: bug#415996).
  - Add evdev support (closes: bug#513210).
  - Fix loop on symlinks in user mode (closes: bug#297572).
  - Bump depends on openbios-sparc.
  - Depends on openbios-ppc.
  - Update 12_signal_powerpc_support.patch.
  - Update 21_net_soopts.patch.
  - Drop 44_socklen_t_check.patch (merged upstream).
  - Drop 49_null_check.patch (merged upstream).
  - Update 64_ppc_asm_constraints.patch.
  - Drop security/CVE-2008-0928-fedora.patch (merged upstream).
  - Drop security/CVE-2007-5730.patch (merged upstream).
* patches/80_stable-branch.patch: add patches from stable branch:
  - Fix race condition between signal handler/execution loop (closes:
    bug#474386, bug#501731).
* debian/copyright: update.
* Compile and install .dtb files:
  - debian/control: build-depends on device-tree-compiler.
  - debian/patches/81_compile_dtb.patch: new patch from upstream.
  - debian/rules: compile and install bamboo.dtb and mpc8544.dtb.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 *
4
4
 * Copyright (c) 2005 Fabrice Bellard
5
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
 *
6
14
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
15
 * of this software and associated documentation files (the "Software"), to deal
8
16
 * in the Software without restriction, including without limitation the rights
21
29
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
30
 * THE SOFTWARE.
23
31
 */
 
32
 
24
33
#include "qemu-common.h"
25
 
#include "hw/usb.h"
 
34
#include "qemu-timer.h"
26
35
#include "console.h"
27
36
 
28
 
#if defined(__linux__)
29
37
#include <dirent.h>
30
38
#include <sys/ioctl.h>
 
39
#include <signal.h>
 
40
 
31
41
#include <linux/usbdevice_fs.h>
32
42
#include <linux/version.h>
33
 
#include <signal.h>
 
43
#include "hw/usb.h"
34
44
 
35
45
/* We redefine it to avoid version problems */
36
46
struct usb_ctrltransfer {
43
53
    void *data;
44
54
};
45
55
 
 
56
struct usb_ctrlrequest {
 
57
    uint8_t bRequestType;
 
58
    uint8_t bRequest;
 
59
    uint16_t wValue;
 
60
    uint16_t wIndex;
 
61
    uint16_t wLength;
 
62
};
 
63
 
46
64
typedef int USBScanFunc(void *opaque, int bus_num, int addr, int class_id,
47
65
                        int vendor_id, int product_id,
48
66
                        const char *product_name, int speed);
49
67
static int usb_host_find_device(int *pbus_num, int *paddr,
50
68
                                char *product_name, int product_name_size,
51
69
                                const char *devname);
52
 
 
53
70
//#define DEBUG
54
 
//#define DEBUG_ISOCH
55
 
//#define USE_ASYNCIO
56
 
 
57
 
#define USBDEVFS_PATH "/proc/bus/usb"
 
71
 
 
72
#ifdef DEBUG
 
73
#define dprintf printf
 
74
#else
 
75
#define dprintf(...)
 
76
#endif
 
77
 
 
78
#define USBDBG_DEVOPENED "husb: opened %s/devices\n"
 
79
 
 
80
#define USBPROCBUS_PATH "/proc/bus/usb"
58
81
#define PRODUCT_NAME_SZ 32
59
 
#define SIG_ISOCOMPLETE (SIGRTMIN+7)
60
82
#define MAX_ENDPOINTS 16
61
 
 
62
 
struct sigaction sigact;
 
83
#define USBDEVBUS_PATH "/dev/bus/usb"
 
84
#define USBSYSBUS_PATH "/sys/bus/usb"
 
85
 
 
86
static char *usb_host_device_path;
 
87
 
 
88
#define USB_FS_NONE 0
 
89
#define USB_FS_PROC 1
 
90
#define USB_FS_DEV 2
 
91
#define USB_FS_SYS 3
 
92
 
 
93
static int usb_fs_type;
63
94
 
64
95
/* endpoint association data */
65
96
struct endp_data {
66
97
    uint8_t type;
67
 
};
68
 
 
69
 
/* FIXME: move USBPacket to PendingURB */
 
98
    uint8_t halted;
 
99
};
 
100
 
 
101
enum {
 
102
    CTRL_STATE_IDLE = 0,
 
103
    CTRL_STATE_SETUP,
 
104
    CTRL_STATE_DATA,
 
105
    CTRL_STATE_ACK
 
106
};
 
107
 
 
108
/*
 
109
 * Control transfer state.
 
110
 * Note that 'buffer' _must_ follow 'req' field because 
 
111
 * we need contigious buffer when we submit control URB.
 
112
 */ 
 
113
struct ctrl_struct {
 
114
    uint16_t len;
 
115
    uint16_t offset;
 
116
    uint8_t  state;
 
117
    struct   usb_ctrlrequest req;
 
118
    uint8_t  buffer[1024];
 
119
};
 
120
 
70
121
typedef struct USBHostDevice {
71
122
    USBDevice dev;
72
 
    int fd;
73
 
    int pipe_fds[2];
74
 
    USBPacket *packet;
 
123
    int       fd;
 
124
 
 
125
    uint8_t   descr[1024];
 
126
    int       descr_len;
 
127
    int       configuration;
 
128
    int       ninterfaces;
 
129
    int       closing;
 
130
 
 
131
    struct ctrl_struct ctrl;
75
132
    struct endp_data endp_table[MAX_ENDPOINTS];
76
 
    int configuration;
77
 
    uint8_t descr[1024];
78
 
    int descr_len;
79
 
    int urbs_ready;
 
133
 
 
134
    /* Host side address */
 
135
    int bus_num;
 
136
    int addr;
 
137
 
 
138
    struct USBHostDevice *next;
80
139
} USBHostDevice;
81
140
 
82
 
typedef struct PendingURB {
83
 
    struct usbdevfs_urb *urb;
84
 
    int status;
85
 
    struct PendingURB *next;
86
 
} PendingURB;
87
 
 
88
 
static PendingURB *pending_urbs = NULL;
89
 
 
90
 
static int add_pending_urb(struct usbdevfs_urb *urb)
91
 
{
92
 
    PendingURB *purb = qemu_mallocz(sizeof(PendingURB));
93
 
    if (purb) {
94
 
        purb->urb = urb;
95
 
        purb->status = 0;
96
 
        purb->next = pending_urbs;
97
 
        pending_urbs = purb;
98
 
        return 1;
99
 
    }
100
 
    return 0;
101
 
}
102
 
 
103
 
static int del_pending_urb(struct usbdevfs_urb *urb)
104
 
{
105
 
    PendingURB *purb = pending_urbs;
106
 
    PendingURB *prev = NULL;
107
 
 
108
 
    while (purb && purb->urb != urb) {
109
 
        prev = purb;
110
 
        purb = purb->next;
111
 
    }
112
 
 
113
 
    if (purb && purb->urb == urb) {
114
 
        if (prev) {
115
 
            prev->next = purb->next;
116
 
        } else {
117
 
            pending_urbs = purb->next;
 
141
static int is_isoc(USBHostDevice *s, int ep)
 
142
{
 
143
    return s->endp_table[ep - 1].type == USBDEVFS_URB_TYPE_ISO;
 
144
}
 
145
 
 
146
static int is_halted(USBHostDevice *s, int ep)
 
147
{
 
148
    return s->endp_table[ep - 1].halted;
 
149
}
 
150
 
 
151
static void clear_halt(USBHostDevice *s, int ep)
 
152
{
 
153
    s->endp_table[ep - 1].halted = 0;
 
154
}
 
155
 
 
156
static void set_halt(USBHostDevice *s, int ep)
 
157
{
 
158
    s->endp_table[ep - 1].halted = 1;
 
159
}
 
160
 
 
161
static USBHostDevice *hostdev_list;
 
162
 
 
163
static void hostdev_link(USBHostDevice *dev)
 
164
{
 
165
    dev->next = hostdev_list;
 
166
    hostdev_list = dev;
 
167
}
 
168
 
 
169
static void hostdev_unlink(USBHostDevice *dev)
 
170
{
 
171
    USBHostDevice *pdev = hostdev_list;
 
172
    USBHostDevice **prev = &hostdev_list;
 
173
 
 
174
    while (pdev) {
 
175
        if (pdev == dev) {
 
176
            *prev = dev->next;
 
177
            return;
118
178
        }
119
 
        qemu_free(purb);
120
 
        return 1;
 
179
 
 
180
        prev = &pdev->next;
 
181
        pdev = pdev->next;
121
182
    }
122
 
    return 0;
123
183
}
124
184
 
125
 
#ifdef USE_ASYNCIO
126
 
static PendingURB *get_pending_urb(struct usbdevfs_urb *urb)
 
185
static USBHostDevice *hostdev_find(int bus_num, int addr)
127
186
{
128
 
    PendingURB *purb = pending_urbs;
129
 
 
130
 
    while (purb && purb->urb != urb) {
131
 
        purb = purb->next;
132
 
    }
133
 
 
134
 
    if (purb && purb->urb == urb) {
135
 
        return purb;
 
187
    USBHostDevice *s = hostdev_list;
 
188
    while (s) {
 
189
        if (s->bus_num == bus_num && s->addr == addr)
 
190
            return s;
 
191
        s = s->next;
136
192
    }
137
193
    return NULL;
138
194
}
139
 
#endif
140
 
 
141
 
static int usb_host_update_interfaces(USBHostDevice *dev, int configuration)
 
195
 
 
196
/* 
 
197
 * Async URB state.
 
198
 * We always allocate one isoc descriptor even for bulk transfers
 
199
 * to simplify allocation and casts. 
 
200
 */
 
201
typedef struct AsyncURB
 
202
{
 
203
    struct usbdevfs_urb urb;
 
204
    struct usbdevfs_iso_packet_desc isocpd;
 
205
 
 
206
    USBPacket     *packet;
 
207
    USBHostDevice *hdev;
 
208
} AsyncURB;
 
209
 
 
210
static AsyncURB *async_alloc(void)
 
211
{
 
212
    return (AsyncURB *) qemu_mallocz(sizeof(AsyncURB));
 
213
}
 
214
 
 
215
static void async_free(AsyncURB *aurb)
 
216
{
 
217
    qemu_free(aurb);
 
218
}
 
219
 
 
220
static void async_complete_ctrl(USBHostDevice *s, USBPacket *p)
 
221
{
 
222
    switch(s->ctrl.state) {
 
223
    case CTRL_STATE_SETUP:
 
224
        if (p->len < s->ctrl.len)
 
225
            s->ctrl.len = p->len;
 
226
        s->ctrl.state = CTRL_STATE_DATA;
 
227
        p->len = 8;
 
228
        break;
 
229
 
 
230
    case CTRL_STATE_ACK:
 
231
        s->ctrl.state = CTRL_STATE_IDLE;
 
232
        p->len = 0;
 
233
        break;
 
234
 
 
235
    default:
 
236
        break;
 
237
    }
 
238
}
 
239
 
 
240
static void async_complete(void *opaque)
 
241
{
 
242
    USBHostDevice *s = opaque;
 
243
    AsyncURB *aurb;
 
244
 
 
245
    while (1) {
 
246
        USBPacket *p;
 
247
 
 
248
        int r = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &aurb);
 
249
        if (r < 0) {
 
250
            if (errno == EAGAIN)
 
251
                return;
 
252
 
 
253
            if (errno == ENODEV && !s->closing) {
 
254
                printf("husb: device %d.%d disconnected\n", s->bus_num, s->addr);
 
255
                usb_device_del_addr(0, s->dev.addr);
 
256
                return;
 
257
            }
 
258
 
 
259
            dprintf("husb: async. reap urb failed errno %d\n", errno);
 
260
            return;
 
261
        }
 
262
 
 
263
        p = aurb->packet;
 
264
 
 
265
        dprintf("husb: async completed. aurb %p status %d alen %d\n", 
 
266
                aurb, aurb->urb.status, aurb->urb.actual_length);
 
267
 
 
268
        if (p) {
 
269
            switch (aurb->urb.status) {
 
270
            case 0:
 
271
                p->len = aurb->urb.actual_length;
 
272
                if (aurb->urb.type == USBDEVFS_URB_TYPE_CONTROL)
 
273
                    async_complete_ctrl(s, p);
 
274
                break;
 
275
 
 
276
            case -EPIPE:
 
277
                set_halt(s, p->devep);
 
278
                /* fall through */
 
279
            default:
 
280
                p->len = USB_RET_NAK;
 
281
                break;
 
282
            }
 
283
 
 
284
            usb_packet_complete(p);
 
285
        }
 
286
 
 
287
        async_free(aurb);
 
288
    }
 
289
}
 
290
 
 
291
static void async_cancel(USBPacket *unused, void *opaque)
 
292
{
 
293
    AsyncURB *aurb = opaque;
 
294
    USBHostDevice *s = aurb->hdev;
 
295
 
 
296
    dprintf("husb: async cancel. aurb %p\n", aurb);
 
297
 
 
298
    /* Mark it as dead (see async_complete above) */
 
299
    aurb->packet = NULL;
 
300
 
 
301
    int r = ioctl(s->fd, USBDEVFS_DISCARDURB, aurb);
 
302
    if (r < 0) {
 
303
        dprintf("husb: async. discard urb failed errno %d\n", errno);
 
304
    }
 
305
}
 
306
 
 
307
static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration)
142
308
{
143
309
    int dev_descr_len, config_descr_len;
144
310
    int interface, nb_interfaces, nb_configurations;
147
313
    if (configuration == 0) /* address state - ignore */
148
314
        return 1;
149
315
 
 
316
    dprintf("husb: claiming interfaces. config %d\n", configuration);
 
317
 
150
318
    i = 0;
151
319
    dev_descr_len = dev->descr[0];
152
320
    if (dev_descr_len > dev->descr_len)
155
323
 
156
324
    i += dev_descr_len;
157
325
    while (i < dev->descr_len) {
158
 
#ifdef DEBUG
159
 
        printf("i is %d, descr_len is %d, dl %d, dt %d\n", i, dev->descr_len,
 
326
        dprintf("husb: i is %d, descr_len is %d, dl %d, dt %d\n", i, dev->descr_len,
160
327
               dev->descr[i], dev->descr[i+1]);
161
 
#endif
 
328
 
162
329
        if (dev->descr[i+1] != USB_DT_CONFIG) {
163
330
            i += dev->descr[i];
164
331
            continue;
165
332
        }
166
333
        config_descr_len = dev->descr[i];
167
334
 
168
 
        if (configuration == dev->descr[i + 5])
 
335
        printf("husb: config #%d need %d\n", dev->descr[i + 5], configuration); 
 
336
 
 
337
        if (configuration < 0 || configuration == dev->descr[i + 5]) {
 
338
            configuration = dev->descr[i + 5];
169
339
            break;
 
340
        }
170
341
 
171
342
        i += config_descr_len;
172
343
    }
173
344
 
174
345
    if (i >= dev->descr_len) {
175
 
        printf("usb_host: error - device has no matching configuration\n");
 
346
        fprintf(stderr, "husb: update iface failed. no matching configuration\n");
176
347
        goto fail;
177
348
    }
178
349
    nb_interfaces = dev->descr[i + 4];
198
369
        ret = ioctl(dev->fd, USBDEVFS_CLAIMINTERFACE, &interface);
199
370
        if (ret < 0) {
200
371
            if (errno == EBUSY) {
201
 
                fprintf(stderr,
202
 
                        "usb_host: warning - device already grabbed\n");
 
372
                printf("husb: update iface. device already grabbed\n");
203
373
            } else {
204
 
                perror("USBDEVFS_CLAIMINTERFACE");
 
374
                perror("husb: failed to claim interface");
205
375
            }
206
376
        fail:
207
377
            return 0;
208
378
        }
209
379
    }
210
380
 
211
 
#ifdef DEBUG
212
 
    printf("usb_host: %d interfaces claimed for configuration %d\n",
 
381
    printf("husb: %d interfaces claimed for configuration %d\n",
213
382
           nb_interfaces, configuration);
214
 
#endif
 
383
 
 
384
    dev->ninterfaces   = nb_interfaces;
 
385
    dev->configuration = configuration;
 
386
    return 1;
 
387
}
 
388
 
 
389
static int usb_host_release_interfaces(USBHostDevice *s)
 
390
{
 
391
    int ret, i;
 
392
 
 
393
    dprintf("husb: releasing interfaces\n");
 
394
 
 
395
    for (i = 0; i < s->ninterfaces; i++) {
 
396
        ret = ioctl(s->fd, USBDEVFS_RELEASEINTERFACE, &i);
 
397
        if (ret < 0) {
 
398
            perror("husb: failed to release interface");
 
399
            return 0;
 
400
        }
 
401
    }
215
402
 
216
403
    return 1;
217
404
}
218
405
 
219
406
static void usb_host_handle_reset(USBDevice *dev)
220
407
{
221
 
#if 0
222
 
    USBHostDevice *s = (USBHostDevice *)dev;
223
 
    /* USBDEVFS_RESET, but not the first time as it has already be
224
 
       done by the host OS */
 
408
    USBHostDevice *s = (USBHostDevice *) dev;
 
409
 
 
410
    dprintf("husb: reset device %u.%u\n", s->bus_num, s->addr);
 
411
 
225
412
    ioctl(s->fd, USBDEVFS_RESET);
226
 
#endif
 
413
 
 
414
    usb_host_claim_interfaces(s, s->configuration);
227
415
}
228
416
 
229
417
static void usb_host_handle_destroy(USBDevice *dev)
230
418
{
231
419
    USBHostDevice *s = (USBHostDevice *)dev;
232
420
 
 
421
    s->closing = 1;
 
422
 
 
423
    qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
 
424
 
 
425
    hostdev_unlink(s);
 
426
 
 
427
    async_complete(s);
 
428
 
233
429
    if (s->fd >= 0)
234
430
        close(s->fd);
 
431
 
235
432
    qemu_free(s);
236
433
}
237
434
 
238
435
static int usb_linux_update_endp_table(USBHostDevice *s);
239
436
 
240
 
static int usb_host_handle_control(USBDevice *dev,
241
 
                                   int request,
242
 
                                   int value,
243
 
                                   int index,
244
 
                                   int length,
245
 
                                   uint8_t *data)
246
 
{
247
 
    USBHostDevice *s = (USBHostDevice *)dev;
248
 
    struct usb_ctrltransfer ct;
 
437
static int usb_host_handle_data(USBHostDevice *s, USBPacket *p)
 
438
{
 
439
    struct usbdevfs_urb *urb;
 
440
    AsyncURB *aurb;
 
441
    int ret;
 
442
 
 
443
    aurb = async_alloc();
 
444
    aurb->hdev   = s;
 
445
    aurb->packet = p;
 
446
 
 
447
    urb = &aurb->urb;
 
448
 
 
449
    if (p->pid == USB_TOKEN_IN)
 
450
        urb->endpoint = p->devep | 0x80;
 
451
    else
 
452
        urb->endpoint = p->devep;
 
453
 
 
454
    if (is_halted(s, p->devep)) {
 
455
        ret = ioctl(s->fd, USBDEVFS_CLEAR_HALT, &urb->endpoint);
 
456
        if (ret < 0) {
 
457
            dprintf("husb: failed to clear halt. ep 0x%x errno %d\n", 
 
458
                   urb->endpoint, errno);
 
459
            return USB_RET_NAK;
 
460
        }
 
461
        clear_halt(s, p->devep);
 
462
    }
 
463
 
 
464
    urb->buffer        = p->data;
 
465
    urb->buffer_length = p->len;
 
466
 
 
467
    if (is_isoc(s, p->devep)) {
 
468
        /* Setup ISOC transfer */
 
469
        urb->type     = USBDEVFS_URB_TYPE_ISO;
 
470
        urb->flags    = USBDEVFS_URB_ISO_ASAP;
 
471
        urb->number_of_packets = 1;
 
472
        urb->iso_frame_desc[0].length = p->len;
 
473
    } else {
 
474
        /* Setup bulk transfer */
 
475
        urb->type     = USBDEVFS_URB_TYPE_BULK;
 
476
    }
 
477
 
 
478
    urb->usercontext = s;
 
479
 
 
480
    ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
 
481
 
 
482
    dprintf("husb: data submit. ep 0x%x len %u aurb %p\n", urb->endpoint, p->len, aurb);
 
483
 
 
484
    if (ret < 0) {
 
485
        dprintf("husb: submit failed. errno %d\n", errno);
 
486
        async_free(aurb);
 
487
 
 
488
        switch(errno) {
 
489
        case ETIMEDOUT:
 
490
            return USB_RET_NAK;
 
491
        case EPIPE:
 
492
        default:
 
493
            return USB_RET_STALL;
 
494
        }
 
495
    }
 
496
 
 
497
    usb_defer_packet(p, async_cancel, aurb);
 
498
    return USB_RET_ASYNC;
 
499
}
 
500
 
 
501
static int ctrl_error(void)
 
502
{
 
503
    if (errno == ETIMEDOUT)
 
504
        return USB_RET_NAK;
 
505
    else 
 
506
        return USB_RET_STALL;
 
507
}
 
508
 
 
509
static int usb_host_set_address(USBHostDevice *s, int addr)
 
510
{
 
511
    dprintf("husb: ctrl set addr %u\n", addr);
 
512
    s->dev.addr = addr;
 
513
    return 0;
 
514
}
 
515
 
 
516
static int usb_host_set_config(USBHostDevice *s, int config)
 
517
{
 
518
    usb_host_release_interfaces(s);
 
519
 
 
520
    int ret = ioctl(s->fd, USBDEVFS_SETCONFIGURATION, &config);
 
521
 
 
522
    dprintf("husb: ctrl set config %d ret %d errno %d\n", config, ret, errno);
 
523
    
 
524
    if (ret < 0)
 
525
        return ctrl_error();
 
526
 
 
527
    usb_host_claim_interfaces(s, config);
 
528
    return 0;
 
529
}
 
530
 
 
531
static int usb_host_set_interface(USBHostDevice *s, int iface, int alt)
 
532
{
249
533
    struct usbdevfs_setinterface si;
250
 
    int intf_update_required = 0;
251
 
    int ret;
252
 
 
253
 
    if (request == (DeviceOutRequest | USB_REQ_SET_ADDRESS)) {
254
 
        /* specific SET_ADDRESS support */
255
 
        dev->addr = value;
256
 
        return 0;
257
 
    } else if (request == ((USB_RECIP_INTERFACE << 8) |
258
 
                           USB_REQ_SET_INTERFACE)) {
259
 
        /* set alternate setting for the interface */
260
 
        si.interface = index;
261
 
        si.altsetting = value;
262
 
        ret = ioctl(s->fd, USBDEVFS_SETINTERFACE, &si);
263
 
        usb_linux_update_endp_table(s);
264
 
    } else if (request == (DeviceOutRequest | USB_REQ_SET_CONFIGURATION)) {
265
 
#ifdef DEBUG
266
 
        printf("usb_host_handle_control: SET_CONFIGURATION request - "
267
 
               "config %d\n", value & 0xff);
268
 
#endif
269
 
        if (s->configuration != (value & 0xff)) {
270
 
            s->configuration = (value & 0xff);
271
 
            intf_update_required = 1;
272
 
        }
273
 
        goto do_request;
274
 
    } else {
275
 
    do_request:
276
 
        ct.bRequestType = request >> 8;
277
 
        ct.bRequest = request;
278
 
        ct.wValue = value;
279
 
        ct.wIndex = index;
280
 
        ct.wLength = length;
281
 
        ct.timeout = 50;
282
 
        ct.data = data;
283
 
        ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
284
 
    }
285
 
 
286
 
    if (ret < 0) {
287
 
        switch(errno) {
288
 
        case ETIMEDOUT:
289
 
            return USB_RET_NAK;
290
 
        default:
291
 
            return USB_RET_STALL;
292
 
        }
293
 
    } else {
294
 
        if (intf_update_required) {
295
 
#ifdef DEBUG
296
 
            printf("usb_host_handle_control: updating interfaces\n");
297
 
#endif
298
 
            usb_host_update_interfaces(s, value & 0xff);
299
 
        }
300
 
        return ret;
301
 
    }
302
 
}
303
 
 
304
 
static int usb_host_handle_isoch(USBDevice *dev, USBPacket *p);
305
 
 
306
 
static int usb_host_handle_data(USBDevice *dev, USBPacket *p)
307
 
{
308
 
    USBHostDevice *s = (USBHostDevice *)dev;
309
 
    struct usbdevfs_bulktransfer bt;
310
 
    int ret;
311
 
    uint8_t devep = p->devep;
312
 
 
313
 
    if (s->endp_table[p->devep - 1].type == USBDEVFS_URB_TYPE_ISO) {
314
 
        return usb_host_handle_isoch(dev, p);
315
 
    }
316
 
 
317
 
    /* XXX: optimize and handle all data types by looking at the
318
 
       config descriptor */
319
 
    if (p->pid == USB_TOKEN_IN)
320
 
        devep |= 0x80;
321
 
    bt.ep = devep;
322
 
    bt.len = p->len;
323
 
    bt.timeout = 50;
324
 
    bt.data = p->data;
325
 
    ret = ioctl(s->fd, USBDEVFS_BULK, &bt);
326
 
    if (ret < 0) {
327
 
        switch(errno) {
328
 
        case ETIMEDOUT:
329
 
            return USB_RET_NAK;
330
 
        case EPIPE:
331
 
        default:
332
 
#ifdef DEBUG
333
 
            printf("handle_data: errno=%d\n", errno);
334
 
#endif
335
 
            return USB_RET_STALL;
336
 
        }
337
 
    } else {
338
 
        return ret;
339
 
    }
340
 
}
341
 
 
342
 
#ifdef USE_ASYNCIO
343
 
static void urb_completion_pipe_read(void *opaque)
344
 
{
345
 
    USBHostDevice *s = opaque;
346
 
    USBPacket *p = s->packet;
347
 
    PendingURB *pending_urb = NULL;
348
 
    struct usbdevfs_urb *purb = NULL;
349
 
    int len, ret;
350
 
 
351
 
    len = read(s->pipe_fds[0], &pending_urb, sizeof(pending_urb));
352
 
    if (len != sizeof(pending_urb)) {
353
 
        printf("urb_completion: error reading pending_urb, len=%d\n", len);
354
 
        return;
355
 
    }
356
 
 
357
 
    /* FIXME: handle pending_urb->status */
358
 
    del_pending_urb(pending_urb->urb);
359
 
 
360
 
    if (!p) {
361
 
        s->urbs_ready++;
362
 
        return;
363
 
    }
364
 
 
365
 
    ret = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &purb);
366
 
    if (ret < 0) {
367
 
        printf("urb_completion: REAPURBNDELAY ioctl=%d errno=%d\n",
368
 
               ret, errno);
369
 
        return;
370
 
    }
371
 
 
372
 
#ifdef DEBUG_ISOCH
373
 
    if (purb == pending_urb->urb) {
374
 
        printf("urb_completion: urb mismatch reaped=%p pending=%p\n",
375
 
               purb, urb);
376
 
    }
377
 
#endif
378
 
 
379
 
    p->len = purb->actual_length;
380
 
    usb_packet_complete(p);
381
 
    qemu_free(purb);
382
 
    s->packet = NULL;
383
 
}
384
 
 
385
 
static void isoch_done(int signum, siginfo_t *info, void *context)
386
 
{
387
 
    struct usbdevfs_urb *urb = (struct usbdevfs_urb *)info->si_addr;
388
 
    USBHostDevice *s = (USBHostDevice *)urb->usercontext;
389
 
    PendingURB *purb;
390
 
 
391
 
    if (info->si_code != SI_ASYNCIO ||
392
 
        info->si_signo != SIG_ISOCOMPLETE) {
393
 
        return;
394
 
    }
395
 
 
396
 
    purb = get_pending_urb(urb);
397
 
    if (purb) {
398
 
        purb->status = info->si_errno;
399
 
        write(s->pipe_fds[1], &purb, sizeof(purb));
400
 
    }
401
 
}
402
 
#endif
403
 
 
404
 
static int usb_host_handle_isoch(USBDevice *dev, USBPacket *p)
405
 
{
406
 
    USBHostDevice *s = (USBHostDevice *)dev;
407
 
    struct usbdevfs_urb *urb, *purb = NULL;
408
 
    int ret;
409
 
    uint8_t devep = p->devep;
410
 
 
411
 
    if (p->pid == USB_TOKEN_IN)
412
 
        devep |= 0x80;
413
 
 
414
 
    urb = qemu_mallocz(sizeof(struct usbdevfs_urb) +
415
 
                       sizeof(struct usbdevfs_iso_packet_desc));
416
 
    if (!urb) {
417
 
        printf("usb_host_handle_isoch: malloc failed\n");
418
 
        return 0;
419
 
    }
420
 
 
421
 
    urb->type = USBDEVFS_URB_TYPE_ISO;
422
 
    urb->endpoint = devep;
423
 
    urb->status = 0;
424
 
    urb->flags = USBDEVFS_URB_ISO_ASAP;
425
 
    urb->buffer = p->data;
426
 
    urb->buffer_length = p->len;
427
 
    urb->actual_length = 0;
428
 
    urb->start_frame = 0;
429
 
    urb->error_count = 0;
430
 
#ifdef USE_ASYNCIO
431
 
    urb->signr = SIG_ISOCOMPLETE;
432
 
#else
433
 
    urb->signr = 0;
434
 
#endif
 
534
    int ret;
 
535
 
 
536
    si.interface  = iface;
 
537
    si.altsetting = alt;
 
538
    ret = ioctl(s->fd, USBDEVFS_SETINTERFACE, &si);
 
539
    
 
540
    dprintf("husb: ctrl set iface %d altset %d ret %d errno %d\n", 
 
541
        iface, alt, ret, errno);
 
542
    
 
543
    if (ret < 0)
 
544
        return ctrl_error();
 
545
 
 
546
    usb_linux_update_endp_table(s);
 
547
    return 0;
 
548
}
 
549
 
 
550
static int usb_host_handle_control(USBHostDevice *s, USBPacket *p)
 
551
{
 
552
    struct usbdevfs_urb *urb;
 
553
    AsyncURB *aurb;
 
554
    int ret, value, index;
 
555
 
 
556
    /* 
 
557
     * Process certain standard device requests.
 
558
     * These are infrequent and are processed synchronously.
 
559
     */
 
560
    value = le16_to_cpu(s->ctrl.req.wValue);
 
561
    index = le16_to_cpu(s->ctrl.req.wIndex);
 
562
 
 
563
    dprintf("husb: ctrl type 0x%x req 0x%x val 0x%x index %u len %u\n",
 
564
        s->ctrl.req.bRequestType, s->ctrl.req.bRequest, value, index, 
 
565
        s->ctrl.len);
 
566
 
 
567
    if (s->ctrl.req.bRequestType == 0) {
 
568
        switch (s->ctrl.req.bRequest) {
 
569
        case USB_REQ_SET_ADDRESS:
 
570
            return usb_host_set_address(s, value);
 
571
 
 
572
        case USB_REQ_SET_CONFIGURATION:
 
573
            return usb_host_set_config(s, value & 0xff);
 
574
        }
 
575
    }
 
576
 
 
577
    if (s->ctrl.req.bRequestType == 1 &&
 
578
                  s->ctrl.req.bRequest == USB_REQ_SET_INTERFACE)
 
579
        return usb_host_set_interface(s, index, value);
 
580
 
 
581
    /* The rest are asynchronous */
 
582
 
 
583
    aurb = async_alloc();
 
584
    aurb->hdev   = s;
 
585
    aurb->packet = p;
 
586
 
 
587
    /* 
 
588
     * Setup ctrl transfer.
 
589
     *
 
590
     * s->ctrl is layed out such that data buffer immediately follows
 
591
     * 'req' struct which is exactly what usbdevfs expects.
 
592
     */ 
 
593
    urb = &aurb->urb;
 
594
 
 
595
    urb->type     = USBDEVFS_URB_TYPE_CONTROL;
 
596
    urb->endpoint = p->devep;
 
597
 
 
598
    urb->buffer        = &s->ctrl.req;
 
599
    urb->buffer_length = 8 + s->ctrl.len;
 
600
 
435
601
    urb->usercontext = s;
436
 
    urb->number_of_packets = 1;
437
 
    urb->iso_frame_desc[0].length = p->len;
438
 
    urb->iso_frame_desc[0].actual_length = 0;
439
 
    urb->iso_frame_desc[0].status = 0;
 
602
 
440
603
    ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
441
 
    if (ret == 0) {
442
 
        if (!add_pending_urb(urb)) {
443
 
            printf("usb_host_handle_isoch: add_pending_urb failed %p\n", urb);
444
 
        }
445
 
    } else {
446
 
        printf("usb_host_handle_isoch: SUBMITURB ioctl=%d errno=%d\n",
447
 
               ret, errno);
448
 
        qemu_free(urb);
 
604
 
 
605
    dprintf("husb: submit ctrl. len %u aurb %p\n", urb->buffer_length, aurb);
 
606
 
 
607
    if (ret < 0) {
 
608
        dprintf("husb: submit failed. errno %d\n", errno);
 
609
        async_free(aurb);
 
610
 
449
611
        switch(errno) {
450
612
        case ETIMEDOUT:
451
613
            return USB_RET_NAK;
454
616
            return USB_RET_STALL;
455
617
        }
456
618
    }
457
 
#ifdef USE_ASYNCIO
458
 
    /* FIXME: handle urbs_ready together with sync io
459
 
     * workaround for injecting the signaled urbs into current frame */
460
 
    if (s->urbs_ready > 0) {
461
 
        ret = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &purb);
462
 
        if (ret == 0) {
463
 
            ret = purb->actual_length;
464
 
            qemu_free(purb);
465
 
            s->urbs_ready--;
466
 
        }
467
 
        return ret;
468
 
    }
469
 
    s->packet = p;
 
619
 
 
620
    usb_defer_packet(p, async_cancel, aurb);
470
621
    return USB_RET_ASYNC;
471
 
#else
472
 
    ret = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &purb);
473
 
    if (ret == 0) {
474
 
        if (del_pending_urb(purb)) {
475
 
            ret = purb->actual_length;
476
 
            qemu_free(purb);
 
622
}
 
623
 
 
624
static int do_token_setup(USBDevice *dev, USBPacket *p)
 
625
{
 
626
    USBHostDevice *s = (USBHostDevice *) dev;
 
627
    int ret = 0;
 
628
 
 
629
    if (p->len != 8)
 
630
        return USB_RET_STALL;
 
631
 
 
632
    memcpy(&s->ctrl.req, p->data, 8);
 
633
    s->ctrl.len    = le16_to_cpu(s->ctrl.req.wLength);
 
634
    s->ctrl.offset = 0;
 
635
    s->ctrl.state  = CTRL_STATE_SETUP;
 
636
 
 
637
    if (s->ctrl.req.bRequestType & USB_DIR_IN) {
 
638
        ret = usb_host_handle_control(s, p);
 
639
        if (ret < 0)
 
640
            return ret;
 
641
 
 
642
        if (ret < s->ctrl.len)
 
643
            s->ctrl.len = ret;
 
644
        s->ctrl.state = CTRL_STATE_DATA;
 
645
    } else {
 
646
        if (s->ctrl.len == 0)
 
647
            s->ctrl.state = CTRL_STATE_ACK;
 
648
        else
 
649
            s->ctrl.state = CTRL_STATE_DATA;
 
650
    }
 
651
 
 
652
    return ret;
 
653
}
 
654
 
 
655
static int do_token_in(USBDevice *dev, USBPacket *p)
 
656
{
 
657
    USBHostDevice *s = (USBHostDevice *) dev;
 
658
    int ret = 0;
 
659
 
 
660
    if (p->devep != 0)
 
661
        return usb_host_handle_data(s, p);
 
662
 
 
663
    switch(s->ctrl.state) {
 
664
    case CTRL_STATE_ACK:
 
665
        if (!(s->ctrl.req.bRequestType & USB_DIR_IN)) {
 
666
            ret = usb_host_handle_control(s, p);
 
667
            if (ret == USB_RET_ASYNC)
 
668
                return USB_RET_ASYNC;
 
669
 
 
670
            s->ctrl.state = CTRL_STATE_IDLE;
 
671
            return ret > 0 ? 0 : ret;
 
672
        }
 
673
 
 
674
        return 0;
 
675
 
 
676
    case CTRL_STATE_DATA:
 
677
        if (s->ctrl.req.bRequestType & USB_DIR_IN) {
 
678
            int len = s->ctrl.len - s->ctrl.offset;
 
679
            if (len > p->len)
 
680
                len = p->len;
 
681
            memcpy(p->data, s->ctrl.buffer + s->ctrl.offset, len);
 
682
            s->ctrl.offset += len;
 
683
            if (s->ctrl.offset >= s->ctrl.len)
 
684
                s->ctrl.state = CTRL_STATE_ACK;
 
685
            return len;
 
686
        }
 
687
 
 
688
        s->ctrl.state = CTRL_STATE_IDLE;
 
689
        return USB_RET_STALL;
 
690
 
 
691
    default:
 
692
        return USB_RET_STALL;
 
693
    }
 
694
}
 
695
 
 
696
static int do_token_out(USBDevice *dev, USBPacket *p)
 
697
{
 
698
    USBHostDevice *s = (USBHostDevice *) dev;
 
699
 
 
700
    if (p->devep != 0)
 
701
        return usb_host_handle_data(s, p);
 
702
 
 
703
    switch(s->ctrl.state) {
 
704
    case CTRL_STATE_ACK:
 
705
        if (s->ctrl.req.bRequestType & USB_DIR_IN) {
 
706
            s->ctrl.state = CTRL_STATE_IDLE;
 
707
            /* transfer OK */
477
708
        } else {
478
 
            printf("usb_host_handle_isoch: del_pending_urb failed %p\n", purb);
479
 
        }
480
 
    } else {
481
 
#ifdef DEBUG_ISOCH
482
 
        printf("usb_host_handle_isoch: REAPURBNDELAY ioctl=%d errno=%d\n",
483
 
               ret, errno);
484
 
#endif
485
 
    }
486
 
    return ret;
487
 
#endif
 
709
            /* ignore additional output */
 
710
        }
 
711
        return 0;
 
712
 
 
713
    case CTRL_STATE_DATA:
 
714
        if (!(s->ctrl.req.bRequestType & USB_DIR_IN)) {
 
715
            int len = s->ctrl.len - s->ctrl.offset;
 
716
            if (len > p->len)
 
717
                len = p->len;
 
718
            memcpy(s->ctrl.buffer + s->ctrl.offset, p->data, len);
 
719
            s->ctrl.offset += len;
 
720
            if (s->ctrl.offset >= s->ctrl.len)
 
721
                s->ctrl.state = CTRL_STATE_ACK;
 
722
            return len;
 
723
        }
 
724
 
 
725
        s->ctrl.state = CTRL_STATE_IDLE;
 
726
        return USB_RET_STALL;
 
727
 
 
728
    default:
 
729
        return USB_RET_STALL;
 
730
    }
 
731
}
 
732
 
 
733
/*
 
734
 * Packet handler.
 
735
 * Called by the HC (host controller).
 
736
 *
 
737
 * Returns length of the transaction or one of the USB_RET_XXX codes.
 
738
 */
 
739
static int usb_host_handle_packet(USBDevice *s, USBPacket *p)
 
740
{
 
741
    switch(p->pid) {
 
742
    case USB_MSG_ATTACH:
 
743
        s->state = USB_STATE_ATTACHED;
 
744
        return 0;
 
745
 
 
746
    case USB_MSG_DETACH:
 
747
        s->state = USB_STATE_NOTATTACHED;
 
748
        return 0;
 
749
 
 
750
    case USB_MSG_RESET:
 
751
        s->remote_wakeup = 0;
 
752
        s->addr = 0;
 
753
        s->state = USB_STATE_DEFAULT;
 
754
        s->handle_reset(s);
 
755
        return 0;
 
756
    }
 
757
 
 
758
    /* Rest of the PIDs must match our address */
 
759
    if (s->state < USB_STATE_DEFAULT || p->devaddr != s->addr)
 
760
        return USB_RET_NODEV;
 
761
 
 
762
    switch (p->pid) {
 
763
    case USB_TOKEN_SETUP:
 
764
        return do_token_setup(s, p);
 
765
 
 
766
    case USB_TOKEN_IN:
 
767
        return do_token_in(s, p);
 
768
 
 
769
    case USB_TOKEN_OUT:
 
770
        return do_token_out(s, p);
 
771
 
 
772
    default:
 
773
        return USB_RET_STALL;
 
774
    }
488
775
}
489
776
 
490
777
/* returns 1 on problem encountered or 0 for success */
521
808
 
522
809
    if (descriptors[i + 1] != USB_DT_CONFIG ||
523
810
        descriptors[i + 5] != configuration) {
524
 
        printf("invalid descriptor data - configuration\n");
 
811
        dprintf("invalid descriptor data - configuration\n");
525
812
        return 1;
526
813
    }
527
814
    i += descriptors[i];
583
870
                type = USBDEVFS_URB_TYPE_INTERRUPT;
584
871
                break;
585
872
            default:
586
 
                printf("usb_host: malformed endpoint type\n");
 
873
                dprintf("usb_host: malformed endpoint type\n");
587
874
                type = USBDEVFS_URB_TYPE_BULK;
588
875
            }
589
876
            s->endp_table[(devep & 0xf) - 1].type = type;
 
877
            s->endp_table[(devep & 0xf) - 1].halted = 0;
590
878
 
591
879
            i += descriptors[i];
592
880
        }
594
882
    return 0;
595
883
}
596
884
 
597
 
/* XXX: exclude high speed devices or implement EHCI */
598
 
USBDevice *usb_host_device_open(const char *devname)
 
885
static USBDevice *usb_host_device_open_addr(int bus_num, int addr, const char *prod_name)
599
886
{
600
887
    int fd = -1, ret;
601
888
    USBHostDevice *dev = NULL;
602
889
    struct usbdevfs_connectinfo ci;
603
890
    char buf[1024];
604
 
    int bus_num, addr;
605
 
    char product_name[PRODUCT_NAME_SZ];
606
891
 
607
892
    dev = qemu_mallocz(sizeof(USBHostDevice));
608
 
    if (!dev)
 
893
 
 
894
    dev->bus_num = bus_num;
 
895
    dev->addr = addr;
 
896
 
 
897
    printf("husb: open device %d.%d\n", bus_num, addr);
 
898
 
 
899
    if (!usb_host_device_path) {
 
900
        perror("husb: USB Host Device Path not set");
609
901
        goto fail;
610
 
 
611
 
#ifdef DEBUG_ISOCH
612
 
    printf("usb_host_device_open %s\n", devname);
613
 
#endif
614
 
    if (usb_host_find_device(&bus_num, &addr,
615
 
                             product_name, sizeof(product_name),
616
 
                             devname) < 0)
617
 
        return NULL;
618
 
 
619
 
    snprintf(buf, sizeof(buf), USBDEVFS_PATH "/%03d/%03d",
 
902
    }
 
903
    snprintf(buf, sizeof(buf), "%s/%03d/%03d", usb_host_device_path,
620
904
             bus_num, addr);
621
905
    fd = open(buf, O_RDWR | O_NONBLOCK);
622
906
    if (fd < 0) {
623
907
        perror(buf);
624
 
        return NULL;
 
908
        goto fail;
625
909
    }
 
910
    dprintf("husb: opened %s\n", buf);
626
911
 
627
912
    /* read the device description */
628
913
    dev->descr_len = read(fd, dev->descr, sizeof(dev->descr));
629
914
    if (dev->descr_len <= 0) {
630
 
        perror("usb_host_device_open: reading device data failed");
 
915
        perror("husb: reading device data failed");
631
916
        goto fail;
632
917
    }
633
918
 
642
927
#endif
643
928
 
644
929
    dev->fd = fd;
645
 
    dev->configuration = 1;
646
930
 
647
 
    /* XXX - do something about initial configuration */
648
 
    if (!usb_host_update_interfaces(dev, 1))
 
931
    /* 
 
932
     * Initial configuration is -1 which makes us claim first 
 
933
     * available config. We used to start with 1, which does not
 
934
     * always work. I've seen devices where first config starts 
 
935
     * with 2.
 
936
     */
 
937
    if (!usb_host_claim_interfaces(dev, -1))
649
938
        goto fail;
650
939
 
651
940
    ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci);
654
943
        goto fail;
655
944
    }
656
945
 
657
 
#ifdef DEBUG
658
 
    printf("host USB device %d.%d grabbed\n", bus_num, addr);
659
 
#endif
 
946
    printf("husb: grabbed usb device %d.%d\n", bus_num, addr);
660
947
 
661
948
    ret = usb_linux_update_endp_table(dev);
662
949
    if (ret)
666
953
        dev->dev.speed = USB_SPEED_LOW;
667
954
    else
668
955
        dev->dev.speed = USB_SPEED_HIGH;
669
 
    dev->dev.handle_packet = usb_generic_handle_packet;
670
956
 
671
 
    dev->dev.handle_reset = usb_host_handle_reset;
672
 
    dev->dev.handle_control = usb_host_handle_control;
673
 
    dev->dev.handle_data = usb_host_handle_data;
 
957
    dev->dev.handle_packet  = usb_host_handle_packet;
 
958
    dev->dev.handle_reset   = usb_host_handle_reset;
674
959
    dev->dev.handle_destroy = usb_host_handle_destroy;
675
960
 
676
 
    if (product_name[0] == '\0')
 
961
    if (!prod_name || prod_name[0] == '\0')
677
962
        snprintf(dev->dev.devname, sizeof(dev->dev.devname),
678
 
                 "host:%s", devname);
 
963
                 "host:%d.%d", bus_num, addr);
679
964
    else
680
965
        pstrcpy(dev->dev.devname, sizeof(dev->dev.devname),
681
 
                product_name);
682
 
 
683
 
#ifdef USE_ASYNCIO
684
 
    /* set up the signal handlers */
685
 
    sigemptyset(&sigact.sa_mask);
686
 
    sigact.sa_sigaction = isoch_done;
687
 
    sigact.sa_flags = SA_SIGINFO;
688
 
    sigact.sa_restorer = 0;
689
 
    ret = sigaction(SIG_ISOCOMPLETE, &sigact, NULL);
690
 
    if (ret < 0) {
691
 
        perror("usb_host_device_open: sigaction failed");
692
 
        goto fail;
693
 
    }
694
 
 
695
 
    if (pipe(dev->pipe_fds) < 0) {
696
 
        perror("usb_host_device_open: pipe creation failed");
697
 
        goto fail;
698
 
    }
699
 
    fcntl(dev->pipe_fds[0], F_SETFL, O_NONBLOCK | O_ASYNC);
700
 
    fcntl(dev->pipe_fds[1], F_SETFL, O_NONBLOCK);
701
 
    qemu_set_fd_handler(dev->pipe_fds[0], urb_completion_pipe_read, NULL, dev);
702
 
#endif
703
 
    dev->urbs_ready = 0;
704
 
    return (USBDevice *)dev;
 
966
                prod_name);
 
967
 
 
968
    /* USB devio uses 'write' flag to check for async completions */
 
969
    qemu_set_fd_handler(dev->fd, NULL, async_complete, dev);
 
970
 
 
971
    hostdev_link(dev);
 
972
 
 
973
    return (USBDevice *) dev;
 
974
 
705
975
fail:
706
976
    if (dev)
707
977
        qemu_free(dev);
 
978
 
708
979
    close(fd);
709
980
    return NULL;
710
981
}
711
982
 
 
983
static int usb_host_auto_add(const char *spec);
 
984
static int usb_host_auto_del(const char *spec);
 
985
 
 
986
USBDevice *usb_host_device_open(const char *devname)
 
987
{
 
988
    int bus_num, addr;
 
989
    char product_name[PRODUCT_NAME_SZ];
 
990
 
 
991
    if (strstr(devname, "auto:")) {
 
992
        usb_host_auto_add(devname);
 
993
        return NULL;
 
994
    }
 
995
 
 
996
    if (usb_host_find_device(&bus_num, &addr, product_name, sizeof(product_name),
 
997
                             devname) < 0)
 
998
        return NULL;
 
999
 
 
1000
    if (hostdev_find(bus_num, addr)) {
 
1001
       term_printf("husb: host usb device %d.%d is already open\n", bus_num, addr);
 
1002
       return NULL;
 
1003
    }
 
1004
 
 
1005
    return usb_host_device_open_addr(bus_num, addr, product_name);
 
1006
}
 
1007
 
 
1008
int usb_host_device_close(const char *devname)
 
1009
{
 
1010
    char product_name[PRODUCT_NAME_SZ];
 
1011
    int bus_num, addr;
 
1012
    USBHostDevice *s;
 
1013
 
 
1014
    if (strstr(devname, "auto:"))
 
1015
        return usb_host_auto_del(devname);
 
1016
 
 
1017
    if (usb_host_find_device(&bus_num, &addr, product_name, sizeof(product_name),
 
1018
                             devname) < 0)
 
1019
        return -1;
 
1020
 
 
1021
    s = hostdev_find(bus_num, addr);
 
1022
    if (s) {
 
1023
        usb_device_del_addr(0, s->dev.addr);
 
1024
        return 0;
 
1025
    }
 
1026
 
 
1027
    return -1;
 
1028
}
 
1029
 
712
1030
static int get_tag_value(char *buf, int buf_size,
713
1031
                         const char *str, const char *tag,
714
1032
                         const char *stopchars)
719
1037
    if (!p)
720
1038
        return -1;
721
1039
    p += strlen(tag);
722
 
    while (isspace(*p))
 
1040
    while (qemu_isspace(*p))
723
1041
        p++;
724
1042
    q = buf;
725
1043
    while (*p != '\0' && !strchr(stopchars, *p)) {
731
1049
    return q - buf;
732
1050
}
733
1051
 
734
 
static int usb_host_scan(void *opaque, USBScanFunc *func)
 
1052
/*
 
1053
 * Use /proc/bus/usb/devices or /dev/bus/usb/devices file to determine
 
1054
 * host's USB devices. This is legacy support since many distributions
 
1055
 * are moving to /sys/bus/usb
 
1056
 */
 
1057
static int usb_host_scan_dev(void *opaque, USBScanFunc *func)
735
1058
{
736
 
    FILE *f;
 
1059
    FILE *f = 0;
737
1060
    char line[1024];
738
1061
    char buf[1024];
739
1062
    int bus_num, addr, speed, device_count, class_id, product_id, vendor_id;
740
 
    int ret;
741
1063
    char product_name[512];
 
1064
    int ret = 0;
742
1065
 
743
 
    f = fopen(USBDEVFS_PATH "/devices", "r");
 
1066
    if (!usb_host_device_path) {
 
1067
        perror("husb: USB Host Device Path not set");
 
1068
        goto the_end;
 
1069
    }
 
1070
    snprintf(line, sizeof(line), "%s/devices", usb_host_device_path);
 
1071
    f = fopen(line, "r");
744
1072
    if (!f) {
745
 
        term_printf("Could not open %s\n", USBDEVFS_PATH "/devices");
746
 
        return 0;
 
1073
        perror("husb: cannot open devices file");
 
1074
        goto the_end;
747
1075
    }
 
1076
 
748
1077
    device_count = 0;
749
1078
    bus_num = addr = speed = class_id = product_id = vendor_id = 0;
750
 
    ret = 0;
751
1079
    for(;;) {
752
1080
        if (fgets(line, sizeof(line), f) == NULL)
753
1081
            break;
804
1132
                   product_id, product_name, speed);
805
1133
    }
806
1134
 the_end:
807
 
    fclose(f);
808
 
    return ret;
 
1135
    if (f)
 
1136
        fclose(f);
 
1137
    return ret;
 
1138
}
 
1139
 
 
1140
/*
 
1141
 * Read sys file-system device file
 
1142
 *
 
1143
 * @line address of buffer to put file contents in
 
1144
 * @line_size size of line
 
1145
 * @device_file path to device file (printf format string)
 
1146
 * @device_name device being opened (inserted into device_file)
 
1147
 *
 
1148
 * @return 0 failed, 1 succeeded ('line' contains data)
 
1149
 */
 
1150
static int usb_host_read_file(char *line, size_t line_size, const char *device_file, const char *device_name)
 
1151
{
 
1152
    FILE *f;
 
1153
    int ret = 0;
 
1154
    char filename[PATH_MAX];
 
1155
 
 
1156
    snprintf(filename, PATH_MAX, USBSYSBUS_PATH "/devices/%s/%s", device_name,
 
1157
             device_file);
 
1158
    f = fopen(filename, "r");
 
1159
    if (f) {
 
1160
        fgets(line, line_size, f);
 
1161
        fclose(f);
 
1162
        ret = 1;
 
1163
    } else {
 
1164
        term_printf("husb: could not open %s\n", filename);
 
1165
    }
 
1166
 
 
1167
    return ret;
 
1168
}
 
1169
 
 
1170
/*
 
1171
 * Use /sys/bus/usb/devices/ directory to determine host's USB
 
1172
 * devices.
 
1173
 *
 
1174
 * This code is based on Robert Schiele's original patches posted to
 
1175
 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
 
1176
 */
 
1177
static int usb_host_scan_sys(void *opaque, USBScanFunc *func)
 
1178
{
 
1179
    DIR *dir = 0;
 
1180
    char line[1024];
 
1181
    int bus_num, addr, speed, class_id, product_id, vendor_id;
 
1182
    int ret = 0;
 
1183
    char product_name[512];
 
1184
    struct dirent *de;
 
1185
 
 
1186
    dir = opendir(USBSYSBUS_PATH "/devices");
 
1187
    if (!dir) {
 
1188
        perror("husb: cannot open devices directory");
 
1189
        goto the_end;
 
1190
    }
 
1191
 
 
1192
    while ((de = readdir(dir))) {
 
1193
        if (de->d_name[0] != '.' && !strchr(de->d_name, ':')) {
 
1194
            char *tmpstr = de->d_name;
 
1195
            if (!strncmp(de->d_name, "usb", 3))
 
1196
                tmpstr += 3;
 
1197
            bus_num = atoi(tmpstr);
 
1198
 
 
1199
            if (!usb_host_read_file(line, sizeof(line), "devnum", de->d_name))
 
1200
                goto the_end;
 
1201
            if (sscanf(line, "%d", &addr) != 1)
 
1202
                goto the_end;
 
1203
 
 
1204
            if (!usb_host_read_file(line, sizeof(line), "bDeviceClass",
 
1205
                                    de->d_name))
 
1206
                goto the_end;
 
1207
            if (sscanf(line, "%x", &class_id) != 1)
 
1208
                goto the_end;
 
1209
 
 
1210
            if (!usb_host_read_file(line, sizeof(line), "idVendor", de->d_name))
 
1211
                goto the_end;
 
1212
            if (sscanf(line, "%x", &vendor_id) != 1)
 
1213
                goto the_end;
 
1214
 
 
1215
            if (!usb_host_read_file(line, sizeof(line), "idProduct",
 
1216
                                    de->d_name))
 
1217
                goto the_end;
 
1218
            if (sscanf(line, "%x", &product_id) != 1)
 
1219
                goto the_end;
 
1220
 
 
1221
            if (!usb_host_read_file(line, sizeof(line), "product",
 
1222
                                    de->d_name)) {
 
1223
                *product_name = 0;
 
1224
            } else {
 
1225
                if (strlen(line) > 0)
 
1226
                    line[strlen(line) - 1] = '\0';
 
1227
                pstrcpy(product_name, sizeof(product_name), line);
 
1228
            }
 
1229
 
 
1230
            if (!usb_host_read_file(line, sizeof(line), "speed", de->d_name))
 
1231
                goto the_end;
 
1232
            if (!strcmp(line, "480\n"))
 
1233
                speed = USB_SPEED_HIGH;
 
1234
            else if (!strcmp(line, "1.5\n"))
 
1235
                speed = USB_SPEED_LOW;
 
1236
            else
 
1237
                speed = USB_SPEED_FULL;
 
1238
 
 
1239
            ret = func(opaque, bus_num, addr, class_id, vendor_id,
 
1240
                       product_id, product_name, speed);
 
1241
            if (ret)
 
1242
                goto the_end;
 
1243
        }
 
1244
    }
 
1245
 the_end:
 
1246
    if (dir)
 
1247
        closedir(dir);
 
1248
    return ret;
 
1249
}
 
1250
 
 
1251
/*
 
1252
 * Determine how to access the host's USB devices and call the
 
1253
 * specific support function.
 
1254
 */
 
1255
static int usb_host_scan(void *opaque, USBScanFunc *func)
 
1256
{
 
1257
    FILE *f = 0;
 
1258
    DIR *dir = 0;
 
1259
    int ret = 0;
 
1260
    const char *fs_type[] = {"unknown", "proc", "dev", "sys"};
 
1261
    char devpath[PATH_MAX];
 
1262
 
 
1263
    /* only check the host once */
 
1264
    if (!usb_fs_type) {
 
1265
        f = fopen(USBPROCBUS_PATH "/devices", "r");
 
1266
        if (f) {
 
1267
            /* devices found in /proc/bus/usb/ */
 
1268
            strcpy(devpath, USBPROCBUS_PATH);
 
1269
            usb_fs_type = USB_FS_PROC;
 
1270
            fclose(f);
 
1271
            dprintf(USBDBG_DEVOPENED, USBPROCBUS_PATH);
 
1272
            goto found_devices;
 
1273
        }
 
1274
        /* try additional methods if an access method hasn't been found yet */
 
1275
        f = fopen(USBDEVBUS_PATH "/devices", "r");
 
1276
        if (f) {
 
1277
            /* devices found in /dev/bus/usb/ */
 
1278
            strcpy(devpath, USBDEVBUS_PATH);
 
1279
            usb_fs_type = USB_FS_DEV;
 
1280
            fclose(f);
 
1281
            dprintf(USBDBG_DEVOPENED, USBDEVBUS_PATH);
 
1282
            goto found_devices;
 
1283
        }
 
1284
        dir = opendir(USBSYSBUS_PATH "/devices");
 
1285
        if (dir) {
 
1286
            /* devices found in /dev/bus/usb/ (yes - not a mistake!) */
 
1287
            strcpy(devpath, USBDEVBUS_PATH);
 
1288
            usb_fs_type = USB_FS_SYS;
 
1289
            closedir(dir);
 
1290
            dprintf(USBDBG_DEVOPENED, USBSYSBUS_PATH);
 
1291
            goto found_devices;
 
1292
        }
 
1293
    found_devices:
 
1294
        if (!usb_fs_type) {
 
1295
            term_printf("husb: unable to access USB devices\n");
 
1296
            return -ENOENT;
 
1297
        }
 
1298
 
 
1299
        /* the module setting (used later for opening devices) */
 
1300
        usb_host_device_path = qemu_mallocz(strlen(devpath)+1);
 
1301
        strcpy(usb_host_device_path, devpath);
 
1302
        term_printf("husb: using %s file-system with %s\n", fs_type[usb_fs_type], usb_host_device_path);
 
1303
    }
 
1304
 
 
1305
    switch (usb_fs_type) {
 
1306
    case USB_FS_PROC:
 
1307
    case USB_FS_DEV:
 
1308
        ret = usb_host_scan_dev(opaque, func);
 
1309
        break;
 
1310
    case USB_FS_SYS:
 
1311
        ret = usb_host_scan_sys(opaque, func);
 
1312
        break;
 
1313
    default:
 
1314
        ret = -EINVAL;
 
1315
        break;
 
1316
    }
 
1317
    return ret;
 
1318
}
 
1319
 
 
1320
struct USBAutoFilter {
 
1321
    struct USBAutoFilter *next;
 
1322
    int bus_num;
 
1323
    int addr;
 
1324
    int vendor_id;
 
1325
    int product_id;
 
1326
};
 
1327
 
 
1328
static QEMUTimer *usb_auto_timer;
 
1329
static struct USBAutoFilter *usb_auto_filter;
 
1330
 
 
1331
static int usb_host_auto_scan(void *opaque, int bus_num, int addr,
 
1332
                     int class_id, int vendor_id, int product_id,
 
1333
                     const char *product_name, int speed)
 
1334
{
 
1335
    struct USBAutoFilter *f;
 
1336
    struct USBDevice *dev;
 
1337
 
 
1338
    /* Ignore hubs */
 
1339
    if (class_id == 9)
 
1340
        return 0;
 
1341
 
 
1342
    for (f = usb_auto_filter; f; f = f->next) {
 
1343
        if (f->bus_num >= 0 && f->bus_num != bus_num)
 
1344
            continue;
 
1345
 
 
1346
        if (f->addr >= 0 && f->addr != addr)
 
1347
            continue;
 
1348
 
 
1349
        if (f->vendor_id >= 0 && f->vendor_id != vendor_id)
 
1350
            continue;
 
1351
 
 
1352
        if (f->product_id >= 0 && f->product_id != product_id)
 
1353
            continue;
 
1354
 
 
1355
        /* We got a match */
 
1356
 
 
1357
        /* Allredy attached ? */
 
1358
        if (hostdev_find(bus_num, addr))
 
1359
            return 0;
 
1360
 
 
1361
        dprintf("husb: auto open: bus_num %d addr %d\n", bus_num, addr);
 
1362
 
 
1363
        dev = usb_host_device_open_addr(bus_num, addr, product_name);
 
1364
        if (dev)
 
1365
            usb_device_add_dev(dev);
 
1366
    }
 
1367
 
 
1368
    return 0;
 
1369
}
 
1370
 
 
1371
static void usb_host_auto_timer(void *unused)
 
1372
{
 
1373
    usb_host_scan(NULL, usb_host_auto_scan);
 
1374
    qemu_mod_timer(usb_auto_timer, qemu_get_clock(rt_clock) + 2000);
 
1375
}
 
1376
 
 
1377
/*
 
1378
 * Autoconnect filter
 
1379
 * Format:
 
1380
 *    auto:bus:dev[:vid:pid]
 
1381
 *    auto:bus.dev[:vid:pid]
 
1382
 *
 
1383
 *    bus  - bus number    (dec, * means any)
 
1384
 *    dev  - device number (dec, * means any)
 
1385
 *    vid  - vendor id     (hex, * means any)
 
1386
 *    pid  - product id    (hex, * means any)
 
1387
 *
 
1388
 *    See 'lsusb' output.
 
1389
 */
 
1390
static int parse_filter(const char *spec, struct USBAutoFilter *f)
 
1391
{
 
1392
    enum { BUS, DEV, VID, PID, DONE };
 
1393
    const char *p = spec;
 
1394
    int i;
 
1395
 
 
1396
    f->bus_num    = -1;
 
1397
    f->addr       = -1;
 
1398
    f->vendor_id  = -1;
 
1399
    f->product_id = -1;
 
1400
 
 
1401
    for (i = BUS; i < DONE; i++) {
 
1402
        p = strpbrk(p, ":.");
 
1403
        if (!p) break;
 
1404
        p++;
 
1405
 
 
1406
        if (*p == '*')
 
1407
            continue;
 
1408
 
 
1409
        switch(i) {
 
1410
        case BUS: f->bus_num = strtol(p, NULL, 10);    break;
 
1411
        case DEV: f->addr    = strtol(p, NULL, 10);    break;
 
1412
        case VID: f->vendor_id  = strtol(p, NULL, 16); break;
 
1413
        case PID: f->product_id = strtol(p, NULL, 16); break;
 
1414
        }
 
1415
    }
 
1416
 
 
1417
    if (i < DEV) {
 
1418
        fprintf(stderr, "husb: invalid auto filter spec %s\n", spec);
 
1419
        return -1;
 
1420
    }
 
1421
 
 
1422
    return 0;
 
1423
}
 
1424
 
 
1425
static int match_filter(const struct USBAutoFilter *f1, 
 
1426
                        const struct USBAutoFilter *f2)
 
1427
{
 
1428
    return f1->bus_num    == f2->bus_num &&
 
1429
           f1->addr       == f2->addr &&
 
1430
           f1->vendor_id  == f2->vendor_id &&
 
1431
           f1->product_id == f2->product_id;
 
1432
}
 
1433
 
 
1434
static int usb_host_auto_add(const char *spec)
 
1435
{
 
1436
    struct USBAutoFilter filter, *f;
 
1437
 
 
1438
    if (parse_filter(spec, &filter) < 0)
 
1439
        return -1;
 
1440
 
 
1441
    f = qemu_mallocz(sizeof(*f));
 
1442
 
 
1443
    *f = filter; 
 
1444
 
 
1445
    if (!usb_auto_filter) {
 
1446
        /*
 
1447
         * First entry. Init and start the monitor.
 
1448
         * Right now we're using timer to check for new devices.
 
1449
         * If this turns out to be too expensive we can move that into a 
 
1450
         * separate thread.
 
1451
         */
 
1452
        usb_auto_timer = qemu_new_timer(rt_clock, usb_host_auto_timer, NULL);
 
1453
        if (!usb_auto_timer) {
 
1454
            fprintf(stderr, "husb: failed to allocate auto scan timer\n");
 
1455
            qemu_free(f);
 
1456
            return -1;
 
1457
        }
 
1458
 
 
1459
        /* Check for new devices every two seconds */
 
1460
        qemu_mod_timer(usb_auto_timer, qemu_get_clock(rt_clock) + 2000);
 
1461
    }
 
1462
 
 
1463
    dprintf("husb: added auto filter: bus_num %d addr %d vid %d pid %d\n",
 
1464
        f->bus_num, f->addr, f->vendor_id, f->product_id);
 
1465
 
 
1466
    f->next = usb_auto_filter;
 
1467
    usb_auto_filter = f;
 
1468
 
 
1469
    return 0;
 
1470
}
 
1471
 
 
1472
static int usb_host_auto_del(const char *spec)
 
1473
{
 
1474
    struct USBAutoFilter *pf = usb_auto_filter;
 
1475
    struct USBAutoFilter **prev = &usb_auto_filter;
 
1476
    struct USBAutoFilter filter;
 
1477
 
 
1478
    if (parse_filter(spec, &filter) < 0)
 
1479
        return -1;
 
1480
 
 
1481
    while (pf) {
 
1482
        if (match_filter(pf, &filter)) {
 
1483
            dprintf("husb: removed auto filter: bus_num %d addr %d vid %d pid %d\n",
 
1484
                     pf->bus_num, pf->addr, pf->vendor_id, pf->product_id);
 
1485
 
 
1486
            *prev = pf->next;
 
1487
 
 
1488
            if (!usb_auto_filter) {
 
1489
                /* No more filters. Stop scanning. */
 
1490
                qemu_del_timer(usb_auto_timer);
 
1491
                qemu_free_timer(usb_auto_timer);
 
1492
            }
 
1493
 
 
1494
            return 0;
 
1495
        }
 
1496
 
 
1497
        prev = &pf->next;
 
1498
        pf   = pf->next;
 
1499
    }
 
1500
 
 
1501
    return -1;
809
1502
}
810
1503
 
811
1504
typedef struct FindDeviceState {
857
1550
            pstrcpy(product_name, product_name_size, fs.product_name);
858
1551
        return 0;
859
1552
    }
 
1553
 
860
1554
    p = strchr(devname, ':');
861
1555
    if (p) {
862
1556
        fs.vendor_id = strtoul(devname, NULL, 16);
953
1647
    return 0;
954
1648
}
955
1649
 
 
1650
static void dec2str(int val, char *str, size_t size)
 
1651
{
 
1652
    if (val == -1)
 
1653
        snprintf(str, size, "*");
 
1654
    else
 
1655
        snprintf(str, size, "%d", val); 
 
1656
}
 
1657
 
 
1658
static void hex2str(int val, char *str, size_t size)
 
1659
{
 
1660
    if (val == -1)
 
1661
        snprintf(str, size, "*");
 
1662
    else
 
1663
        snprintf(str, size, "%x", val);
 
1664
}
 
1665
 
956
1666
void usb_host_info(void)
957
1667
{
 
1668
    struct USBAutoFilter *f;
 
1669
 
958
1670
    usb_host_scan(NULL, usb_host_info_device);
959
 
}
960
 
 
961
 
#else
962
 
 
963
 
void usb_host_info(void)
964
 
{
965
 
    term_printf("USB host devices not supported\n");
966
 
}
967
 
 
968
 
/* XXX: modify configure to compile the right host driver */
969
 
USBDevice *usb_host_device_open(const char *devname)
970
 
{
971
 
    return NULL;
972
 
}
973
 
 
974
 
#endif
 
1671
 
 
1672
    if (usb_auto_filter)
 
1673
        term_printf("  Auto filters:\n");
 
1674
    for (f = usb_auto_filter; f; f = f->next) {
 
1675
        char bus[10], addr[10], vid[10], pid[10];
 
1676
        dec2str(f->bus_num, bus, sizeof(bus));
 
1677
        dec2str(f->addr, addr, sizeof(addr));
 
1678
        hex2str(f->vendor_id, vid, sizeof(vid));
 
1679
        hex2str(f->product_id, pid, sizeof(pid));
 
1680
        term_printf("    Device %s.%s ID %s:%s\n", bus, addr, vid, pid);
 
1681
    }
 
1682
}