~vcs-imports/qemu/git

« back to all changes in this revision

Viewing changes to usb-linux.c

  • Committer: ths
  • Date: 2007-10-08 12:45:38 UTC
  • Revision ID: git-v1:450d4ff553af32fc9d83fef20d7106b0151526b8
CRIS disassembler, originally from binutils, by Edgar E. Iglesias.


git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@3356 c046a42c-6fe2-441c-8c8c-71466251a162

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