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

P
pbrook 已提交
33
#include "qemu-common.h"
34
#include "qemu-timer.h"
A
aliguori 已提交
35
#include "monitor.h"
36
#include "sysemu.h"
B
bellard 已提交
37 38 39 40

#include <dirent.h>
#include <sys/ioctl.h>

41 42 43
#include <linux/usbdevice_fs.h>
#include <linux/version.h>
#include "hw/usb.h"
B
bellard 已提交
44

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

56
typedef int USBScanFunc(void *opaque, int bus_num, int addr, char *port,
57
                        int class_id, int vendor_id, int product_id,
B
bellard 已提交
58
                        const char *product_name, int speed);
59

60
//#define DEBUG
61 62

#ifdef DEBUG
M
malc 已提交
63
#define DPRINTF printf
64
#else
M
malc 已提交
65
#define DPRINTF(...)
66
#endif
B
bellard 已提交
67

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

70
#define USBPROCBUS_PATH "/proc/bus/usb"
71
#define PRODUCT_NAME_SZ 32
72
#define MAX_ENDPOINTS 15
73
#define MAX_PORTLEN 16
74 75 76 77 78 79 80 81 82 83 84
#define USBDEVBUS_PATH "/dev/bus/usb"
#define USBSYSBUS_PATH "/sys/bus/usb"

static char *usb_host_device_path;

#define USB_FS_NONE 0
#define USB_FS_PROC 1
#define USB_FS_DEV 2
#define USB_FS_SYS 3

static int usb_fs_type;
B
bellard 已提交
85

86
/* endpoint association data */
87
#define ISO_FRAME_DESC_PER_URB 32
88
#define INVALID_EP_TYPE 255
89

G
Gerd Hoffmann 已提交
90 91 92
/* devio.c limits single requests to 16k */
#define MAX_USBFS_BUFFER_SIZE 16384

93 94
typedef struct AsyncURB AsyncURB;

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

106 107 108
struct USBAutoFilter {
    uint32_t bus_num;
    uint32_t addr;
G
Gerd Hoffmann 已提交
109
    char     *port;
110 111 112 113
    uint32_t vendor_id;
    uint32_t product_id;
};

B
bellard 已提交
114 115
typedef struct USBHostDevice {
    USBDevice dev;
116 117
    int       fd;

118
    uint8_t   descr[8192];
119 120
    int       descr_len;
    int       configuration;
121
    int       ninterfaces;
122
    int       closing;
123
    uint32_t  iso_urb_count;
124
    Notifier  exit;
125

126
    struct endp_data endp_table[MAX_ENDPOINTS];
G
Gerd Hoffmann 已提交
127
    QLIST_HEAD(, AsyncURB) aurbs;
128 129 130 131

    /* Host side address */
    int bus_num;
    int addr;
132
    char port[MAX_PORTLEN];
133
    struct USBAutoFilter match;
134

135
    QTAILQ_ENTRY(USBHostDevice) next;
B
bellard 已提交
136 137
} USBHostDevice;

138 139 140 141 142
static QTAILQ_HEAD(, USBHostDevice) hostdevs = QTAILQ_HEAD_INITIALIZER(hostdevs);

static int usb_host_close(USBHostDevice *dev);
static int parse_filter(const char *spec, struct USBAutoFilter *f);
static void usb_host_auto_check(void *unused);
143 144
static int usb_host_read_file(char *line, size_t line_size,
                            const char *device_file, const char *device_name);
145

G
Gerd Hoffmann 已提交
146 147 148 149 150
static struct endp_data *get_endp(USBHostDevice *s, int ep)
{
    return s->endp_table + ep - 1;
}

151 152
static int is_isoc(USBHostDevice *s, int ep)
{
G
Gerd Hoffmann 已提交
153
    return get_endp(s, ep)->type == USBDEVFS_URB_TYPE_ISO;
154 155
}

156 157
static int is_valid(USBHostDevice *s, int ep)
{
G
Gerd Hoffmann 已提交
158
    return get_endp(s, ep)->type != INVALID_EP_TYPE;
159 160
}

161 162
static int is_halted(USBHostDevice *s, int ep)
{
G
Gerd Hoffmann 已提交
163
    return get_endp(s, ep)->halted;
164 165 166 167
}

static void clear_halt(USBHostDevice *s, int ep)
{
G
Gerd Hoffmann 已提交
168
    get_endp(s, ep)->halted = 0;
169 170 171 172
}

static void set_halt(USBHostDevice *s, int ep)
{
G
Gerd Hoffmann 已提交
173
    get_endp(s, ep)->halted = 1;
174 175
}

176 177
static int is_iso_started(USBHostDevice *s, int ep)
{
G
Gerd Hoffmann 已提交
178
    return get_endp(s, ep)->iso_started;
179 180 181 182
}

static void clear_iso_started(USBHostDevice *s, int ep)
{
G
Gerd Hoffmann 已提交
183
    get_endp(s, ep)->iso_started = 0;
184 185 186 187
}

static void set_iso_started(USBHostDevice *s, int ep)
{
188 189 190 191 192 193 194 195 196 197 198 199 200
    struct endp_data *e = get_endp(s, ep);
    if (!e->iso_started) {
        e->iso_started = 1;
        e->inflight = 0;
    }
}

static int change_iso_inflight(USBHostDevice *s, int ep, int value)
{
    struct endp_data *e = get_endp(s, ep);

    e->inflight += value;
    return e->inflight;
201 202
}

203 204
static void set_iso_urb(USBHostDevice *s, int ep, AsyncURB *iso_urb)
{
G
Gerd Hoffmann 已提交
205
    get_endp(s, ep)->iso_urb = iso_urb;
206 207 208 209
}

static AsyncURB *get_iso_urb(USBHostDevice *s, int ep)
{
G
Gerd Hoffmann 已提交
210
    return get_endp(s, ep)->iso_urb;
211 212 213 214
}

static void set_iso_urb_idx(USBHostDevice *s, int ep, int i)
{
G
Gerd Hoffmann 已提交
215
    get_endp(s, ep)->iso_urb_idx = i;
216 217 218 219
}

static int get_iso_urb_idx(USBHostDevice *s, int ep)
{
G
Gerd Hoffmann 已提交
220
    return get_endp(s, ep)->iso_urb_idx;
221 222
}

223 224
static void set_iso_buffer_used(USBHostDevice *s, int ep, int i)
{
G
Gerd Hoffmann 已提交
225
    get_endp(s, ep)->iso_buffer_used = i;
226 227 228 229
}

static int get_iso_buffer_used(USBHostDevice *s, int ep)
{
G
Gerd Hoffmann 已提交
230
    return get_endp(s, ep)->iso_buffer_used;
231 232
}

233 234 235 236 237 238 239 240 241 242 243
static void set_max_packet_size(USBHostDevice *s, int ep, uint8_t *descriptor)
{
    int raw = descriptor[4] + (descriptor[5] << 8);
    int size, microframes;

    size = raw & 0x7ff;
    switch ((raw >> 11) & 3) {
    case 1:  microframes = 2; break;
    case 2:  microframes = 3; break;
    default: microframes = 1; break;
    }
G
Gerd Hoffmann 已提交
244
    get_endp(s, ep)->max_packet_size = size * microframes;
245 246
}

247 248
static int get_max_packet_size(USBHostDevice *s, int ep)
{
G
Gerd Hoffmann 已提交
249
    return get_endp(s, ep)->max_packet_size;
250 251
}

252
/*
253
 * Async URB state.
254
 * We always allocate iso packet descriptors even for bulk transfers
255
 * to simplify allocation and casts.
256
 */
257
struct AsyncURB
258 259
{
    struct usbdevfs_urb urb;
260
    struct usbdevfs_iso_packet_desc isocpd[ISO_FRAME_DESC_PER_URB];
G
Gerd Hoffmann 已提交
261 262
    USBHostDevice *hdev;
    QLIST_ENTRY(AsyncURB) next;
263

264
    /* For regular async urbs */
265
    USBPacket     *packet;
G
Gerd Hoffmann 已提交
266
    int more; /* large transfer, more urbs follow */
267 268 269 270

    /* For buffered iso handling */
    int iso_frame_idx; /* -1 means in flight */
};
271

G
Gerd Hoffmann 已提交
272
static AsyncURB *async_alloc(USBHostDevice *s)
273
{
G
Gerd Hoffmann 已提交
274 275 276 277
    AsyncURB *aurb = qemu_mallocz(sizeof(AsyncURB));
    aurb->hdev = s;
    QLIST_INSERT_HEAD(&s->aurbs, aurb, next);
    return aurb;
278 279
}

280
static void async_free(AsyncURB *aurb)
281
{
G
Gerd Hoffmann 已提交
282
    QLIST_REMOVE(aurb, next);
283 284
    qemu_free(aurb);
}
285

286 287 288 289 290 291 292 293
static void do_disconnect(USBHostDevice *s)
{
    printf("husb: device %d.%d disconnected\n",
           s->bus_num, s->addr);
    usb_host_close(s);
    usb_host_auto_check(NULL);
}

294 295 296 297
static void async_complete(void *opaque)
{
    USBHostDevice *s = opaque;
    AsyncURB *aurb;
298
    int urbs = 0;
299 300

    while (1) {
301
        USBPacket *p;
302

303
        int r = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &aurb);
304
        if (r < 0) {
305
            if (errno == EAGAIN) {
306 307 308
                if (urbs > 2) {
                    fprintf(stderr, "husb: %d iso urbs finished at once\n", urbs);
                }
309
                return;
310
            }
311
            if (errno == ENODEV && !s->closing) {
312
                do_disconnect(s);
313 314 315
                return;
            }

M
malc 已提交
316
            DPRINTF("husb: async. reap urb failed errno %d\n", errno);
317
            return;
318
        }
319

320
        DPRINTF("husb: async completed. aurb %p status %d alen %d\n",
321 322
                aurb, aurb->urb.status, aurb->urb.actual_length);

323 324 325
        /* If this is a buffered iso urb mark it as complete and don't do
           anything else (it is handled further in usb_host_handle_iso_data) */
        if (aurb->iso_frame_idx == -1) {
326
            int inflight;
327 328 329 330
            if (aurb->urb.status == -EPIPE) {
                set_halt(s, aurb->urb.endpoint & 0xf);
            }
            aurb->iso_frame_idx = 0;
331 332 333 334 335
            urbs++;
            inflight = change_iso_inflight(s, aurb->urb.endpoint & 0xf, -1);
            if (inflight == 0 && is_iso_started(s, aurb->urb.endpoint & 0xf)) {
                fprintf(stderr, "husb: out of buffers for iso stream\n");
            }
336 337 338 339 340
            continue;
        }

        p = aurb->packet;

341
        if (p) {
342 343
            switch (aurb->urb.status) {
            case 0:
G
Gerd Hoffmann 已提交
344
                p->len += aurb->urb.actual_length;
345 346 347 348
                break;

            case -EPIPE:
                set_halt(s, p->devep);
349 350
                p->len = USB_RET_STALL;
                break;
351

352 353 354 355 356
            default:
                p->len = USB_RET_NAK;
                break;
            }

357 358
            if (aurb->urb.type == USBDEVFS_URB_TYPE_CONTROL) {
                usb_generic_async_ctrl_complete(&s->dev, p);
G
Gerd Hoffmann 已提交
359
            } else if (!aurb->more) {
360 361
                usb_packet_complete(&s->dev, p);
            }
362
        }
363 364

        async_free(aurb);
365 366 367
    }
}

368
static void usb_host_async_cancel(USBDevice *dev, USBPacket *p)
369
{
370
    USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
371
    AsyncURB *aurb;
372

373 374 375 376
    QLIST_FOREACH(aurb, &s->aurbs, next) {
        if (p != aurb->packet) {
            continue;
        }
377

378
        DPRINTF("husb: async cancel: packet %p, aurb %p\n", p, aurb);
379

380 381 382 383 384 385 386
        /* Mark it as dead (see async_complete above) */
        aurb->packet = NULL;

        int r = ioctl(s->fd, USBDEVFS_DISCARDURB, aurb);
        if (r < 0) {
            DPRINTF("husb: async. discard urb failed errno %d\n", errno);
        }
387 388 389
    }
}

390
static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration)
391
{
392
    const char *op = NULL;
393
    int dev_descr_len, config_descr_len;
394
    int interface, nb_interfaces;
395 396 397 398 399
    int ret, i;

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

M
malc 已提交
400
    DPRINTF("husb: claiming interfaces. config %d\n", configuration);
401

402 403
    i = 0;
    dev_descr_len = dev->descr[0];
404
    if (dev_descr_len > dev->descr_len) {
405 406
        fprintf(stderr, "husb: update iface failed. descr too short\n");
        return 0;
407
    }
408 409 410

    i += dev_descr_len;
    while (i < dev->descr_len) {
411 412
        DPRINTF("husb: i is %d, descr_len is %d, dl %d, dt %d\n",
                i, dev->descr_len,
413
               dev->descr[i], dev->descr[i+1]);
414

415 416 417 418 419 420
        if (dev->descr[i+1] != USB_DT_CONFIG) {
            i += dev->descr[i];
            continue;
        }
        config_descr_len = dev->descr[i];

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

423 424
        if (configuration < 0 || configuration == dev->descr[i + 5]) {
            configuration = dev->descr[i + 5];
425
            break;
426
        }
427 428 429 430 431

        i += config_descr_len;
    }

    if (i >= dev->descr_len) {
432 433
        fprintf(stderr,
                "husb: update iface failed. no matching configuration\n");
434
        return 0;
435 436 437 438 439 440 441 442 443 444
    }
    nb_interfaces = dev->descr[i + 4];

#ifdef USBDEVFS_DISCONNECT
    /* earlier Linux 2.4 do not support that */
    {
        struct usbdevfs_ioctl ctrl;
        for (interface = 0; interface < nb_interfaces; interface++) {
            ctrl.ioctl_code = USBDEVFS_DISCONNECT;
            ctrl.ifno = interface;
445
            ctrl.data = 0;
446
            op = "USBDEVFS_DISCONNECT";
447 448 449 450 451 452 453 454 455 456
            ret = ioctl(dev->fd, USBDEVFS_IOCTL, &ctrl);
            if (ret < 0 && errno != ENODATA) {
                goto fail;
            }
        }
    }
#endif

    /* XXX: only grab if all interfaces are free */
    for (interface = 0; interface < nb_interfaces; interface++) {
457
        op = "USBDEVFS_CLAIMINTERFACE";
458 459 460
        ret = ioctl(dev->fd, USBDEVFS_CLAIMINTERFACE, &interface);
        if (ret < 0) {
            if (errno == EBUSY) {
461
                printf("husb: update iface. device already grabbed\n");
462
            } else {
463
                perror("husb: failed to claim interface");
464
            }
465
            goto fail;
466 467 468
        }
    }

469
    printf("husb: %d interfaces claimed for configuration %d\n",
470 471
           nb_interfaces, configuration);

472 473 474
    dev->ninterfaces   = nb_interfaces;
    dev->configuration = configuration;
    return 1;
475 476 477 478 479 480 481

fail:
    if (errno == ENODEV) {
        do_disconnect(dev);
    }
    perror(op);
    return 0;
482 483 484 485 486 487
}

static int usb_host_release_interfaces(USBHostDevice *s)
{
    int ret, i;

M
malc 已提交
488
    DPRINTF("husb: releasing interfaces\n");
489 490 491 492 493 494 495 496 497

    for (i = 0; i < s->ninterfaces; i++) {
        ret = ioctl(s->fd, USBDEVFS_RELEASEINTERFACE, &i);
        if (ret < 0) {
            perror("husb: failed to release interface");
            return 0;
        }
    }

498 499 500
    return 1;
}

B
bellard 已提交
501
static void usb_host_handle_reset(USBDevice *dev)
B
bellard 已提交
502
{
503
    USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
504

M
malc 已提交
505
    DPRINTF("husb: reset device %u.%u\n", s->bus_num, s->addr);
506

B
bellard 已提交
507
    ioctl(s->fd, USBDEVFS_RESET);
508 509

    usb_host_claim_interfaces(s, s->configuration);
510
}
B
bellard 已提交
511

B
bellard 已提交
512 513 514 515
static void usb_host_handle_destroy(USBDevice *dev)
{
    USBHostDevice *s = (USBHostDevice *)dev;

516 517
    usb_host_close(s);
    QTAILQ_REMOVE(&hostdevs, s, next);
518
    qemu_remove_exit_notifier(&s->exit);
B
bellard 已提交
519 520
}

521 522
static int usb_linux_update_endp_table(USBHostDevice *s);

523 524 525 526 527 528 529 530
/* iso data is special, we need to keep enough urbs in flight to make sure
   that the controller never runs out of them, otherwise the device will
   likely suffer a buffer underrun / overrun. */
static AsyncURB *usb_host_alloc_iso(USBHostDevice *s, uint8_t ep, int in)
{
    AsyncURB *aurb;
    int i, j, len = get_max_packet_size(s, ep);

531 532
    aurb = qemu_mallocz(s->iso_urb_count * sizeof(*aurb));
    for (i = 0; i < s->iso_urb_count; i++) {
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
        aurb[i].urb.endpoint      = ep;
        aurb[i].urb.buffer_length = ISO_FRAME_DESC_PER_URB * len;
        aurb[i].urb.buffer        = qemu_malloc(aurb[i].urb.buffer_length);
        aurb[i].urb.type          = USBDEVFS_URB_TYPE_ISO;
        aurb[i].urb.flags         = USBDEVFS_URB_ISO_ASAP;
        aurb[i].urb.number_of_packets = ISO_FRAME_DESC_PER_URB;
        for (j = 0 ; j < ISO_FRAME_DESC_PER_URB; j++)
            aurb[i].urb.iso_frame_desc[j].length = len;
        if (in) {
            aurb[i].urb.endpoint |= 0x80;
            /* Mark as fully consumed (idle) */
            aurb[i].iso_frame_idx = ISO_FRAME_DESC_PER_URB;
        }
    }
    set_iso_urb(s, ep, aurb);

    return aurb;
}

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

    aurb = get_iso_urb(s, ep);
    if (!aurb) {
        return;
    }

562
    for (i = 0; i < s->iso_urb_count; i++) {
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
        /* in flight? */
        if (aurb[i].iso_frame_idx == -1) {
            ret = ioctl(s->fd, USBDEVFS_DISCARDURB, &aurb[i]);
            if (ret < 0) {
                printf("husb: discard isoc in urb failed errno %d\n", errno);
                free = 0;
                continue;
            }
            killed++;
        }
    }

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

580
    for (i = 0; i < s->iso_urb_count; i++) {
581 582 583 584 585 586 587 588
        qemu_free(aurb[i].urb.buffer);
    }

    if (free)
        qemu_free(aurb);
    else
        printf("husb: leaking iso urbs because of discard failure\n");
    set_iso_urb(s, ep, NULL);
589 590
    set_iso_urb_idx(s, ep, 0);
    clear_iso_started(s, ep);
591 592 593 594 595 596 597 598 599 600 601 602
}

static int urb_status_to_usb_ret(int status)
{
    switch (status) {
    case -EPIPE:
        return USB_RET_STALL;
    default:
        return USB_RET_NAK;
    }
}

603
static int usb_host_handle_iso_data(USBHostDevice *s, USBPacket *p, int in)
604 605
{
    AsyncURB *aurb;
606
    int i, j, ret, max_packet_size, offset, len = 0;
607 608 609 610

    max_packet_size = get_max_packet_size(s, p->devep);
    if (max_packet_size == 0)
        return USB_RET_NAK;
611 612 613

    aurb = get_iso_urb(s, p->devep);
    if (!aurb) {
614
        aurb = usb_host_alloc_iso(s, p->devep, in);
615 616 617 618 619
    }

    i = get_iso_urb_idx(s, p->devep);
    j = aurb[i].iso_frame_idx;
    if (j >= 0 && j < ISO_FRAME_DESC_PER_URB) {
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
        if (in) {
            /* Check urb status  */
            if (aurb[i].urb.status) {
                len = urb_status_to_usb_ret(aurb[i].urb.status);
                /* Move to the next urb */
                aurb[i].iso_frame_idx = ISO_FRAME_DESC_PER_URB - 1;
            /* Check frame status */
            } else if (aurb[i].urb.iso_frame_desc[j].status) {
                len = urb_status_to_usb_ret(
                                        aurb[i].urb.iso_frame_desc[j].status);
            /* Check the frame fits */
            } else if (aurb[i].urb.iso_frame_desc[j].actual_length > p->len) {
                printf("husb: received iso data is larger then packet\n");
                len = USB_RET_NAK;
            /* All good copy data over */
            } else {
                len = aurb[i].urb.iso_frame_desc[j].actual_length;
                memcpy(p->data,
                       aurb[i].urb.buffer +
                           j * aurb[i].urb.iso_frame_desc[0].length,
                       len);
            }
642
        } else {
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
            len = p->len;
            offset = (j == 0) ? 0 : get_iso_buffer_used(s, p->devep);

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

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

            /* Start the stream once we have buffered enough data */
            if (!is_iso_started(s, p->devep) && i == 1 && j == 8) {
                set_iso_started(s, p->devep);
            }
662 663 664
        }
        aurb[i].iso_frame_idx++;
        if (aurb[i].iso_frame_idx == ISO_FRAME_DESC_PER_URB) {
665
            i = (i + 1) % s->iso_urb_count;
666 667
            set_iso_urb_idx(s, p->devep, i);
        }
668 669 670 671 672 673
    } else {
        if (in) {
            set_iso_started(s, p->devep);
        } else {
            DPRINTF("hubs: iso out error no free buffer, dropping packet\n");
        }
674 675
    }

676 677
    if (is_iso_started(s, p->devep)) {
        /* (Re)-submit all fully consumed / filled urbs */
678
        for (i = 0; i < s->iso_urb_count; i++) {
679 680 681 682 683 684 685 686
            if (aurb[i].iso_frame_idx == ISO_FRAME_DESC_PER_URB) {
                ret = ioctl(s->fd, USBDEVFS_SUBMITURB, &aurb[i]);
                if (ret < 0) {
                    printf("husb error submitting iso urb %d: %d\n", i, errno);
                    if (!in || len == 0) {
                        switch(errno) {
                        case ETIMEDOUT:
                            len = USB_RET_NAK;
687
                            break;
688 689 690 691
                        case EPIPE:
                        default:
                            len = USB_RET_STALL;
                        }
692
                    }
693
                    break;
694
                }
695
                aurb[i].iso_frame_idx = -1;
696
                change_iso_inflight(s, p->devep, +1);
697 698 699 700 701 702 703
            }
        }
    }

    return len;
}

704
static int usb_host_handle_data(USBDevice *dev, USBPacket *p)
B
bellard 已提交
705
{
706
    USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
707
    struct usbdevfs_urb *urb;
708
    AsyncURB *aurb;
G
Gerd Hoffmann 已提交
709 710
    int ret, rem;
    uint8_t *pbuf;
711
    uint8_t ep;
712

713 714 715 716
    if (!is_valid(s, p->devep)) {
        return USB_RET_NAK;
    }

717
    if (p->pid == USB_TOKEN_IN) {
718
        ep = p->devep | 0x80;
719
    } else {
720
        ep = p->devep;
721
    }
722 723

    if (is_halted(s, p->devep)) {
724
        ret = ioctl(s->fd, USBDEVFS_CLEAR_HALT, &ep);
725
        if (ret < 0) {
726
            DPRINTF("husb: failed to clear halt. ep 0x%x errno %d\n",
727
                   ep, errno);
B
bellard 已提交
728 729
            return USB_RET_NAK;
        }
730
        clear_halt(s, p->devep);
731 732
    }

733 734 735
    if (is_isoc(s, p->devep)) {
        return usb_host_handle_iso_data(s, p, p->pid == USB_TOKEN_IN);
    }
736

G
Gerd Hoffmann 已提交
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
    rem = p->len;
    pbuf = p->data;
    p->len = 0;
    while (rem) {
        aurb = async_alloc(s);
        aurb->packet = p;

        urb = &aurb->urb;
        urb->endpoint      = ep;
        urb->type          = USBDEVFS_URB_TYPE_BULK;
        urb->usercontext   = s;
        urb->buffer        = pbuf;

        if (rem > MAX_USBFS_BUFFER_SIZE) {
            urb->buffer_length = MAX_USBFS_BUFFER_SIZE;
            aurb->more         = 1;
        } else {
            urb->buffer_length = rem;
            aurb->more         = 0;
        }
        pbuf += urb->buffer_length;
        rem  -= urb->buffer_length;
759

G
Gerd Hoffmann 已提交
760
        ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb);
761

G
Gerd Hoffmann 已提交
762 763
        DPRINTF("husb: data submit: ep 0x%x, len %u, more %d, packet %p, aurb %p\n",
                urb->endpoint, urb->buffer_length, aurb->more, p, aurb);
764

G
Gerd Hoffmann 已提交
765 766 767
        if (ret < 0) {
            DPRINTF("husb: submit failed. errno %d\n", errno);
            async_free(aurb);
768

G
Gerd Hoffmann 已提交
769 770 771 772 773 774 775
            switch(errno) {
            case ETIMEDOUT:
                return USB_RET_NAK;
            case EPIPE:
            default:
                return USB_RET_STALL;
            }
776 777
        }
    }
778

779 780 781
    return USB_RET_ASYNC;
}

782 783
static int ctrl_error(void)
{
784
    if (errno == ETIMEDOUT) {
785
        return USB_RET_NAK;
786
    } else {
787
        return USB_RET_STALL;
788
    }
789 790 791 792
}

static int usb_host_set_address(USBHostDevice *s, int addr)
{
M
malc 已提交
793
    DPRINTF("husb: ctrl set addr %u\n", addr);
794 795 796 797 798 799 800 801 802
    s->dev.addr = addr;
    return 0;
}

static int usb_host_set_config(USBHostDevice *s, int config)
{
    usb_host_release_interfaces(s);

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

M
malc 已提交
804
    DPRINTF("husb: ctrl set config %d ret %d errno %d\n", config, ret, errno);
805 806

    if (ret < 0) {
807
        return ctrl_error();
808
    }
809 810 811 812 813 814 815
    usb_host_claim_interfaces(s, config);
    return 0;
}

static int usb_host_set_interface(USBHostDevice *s, int iface, int alt)
{
    struct usbdevfs_setinterface si;
816 817
    int i, ret;

818
    for (i = 1; i <= MAX_ENDPOINTS; i++) {
819 820 821 822
        if (is_isoc(s, i)) {
            usb_host_stop_n_free_iso(s, i);
        }
    }
823 824 825 826 827

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

828 829 830 831 832 833
    DPRINTF("husb: ctrl set iface %d altset %d ret %d errno %d\n",
            iface, alt, ret, errno);

    if (ret < 0) {
        return ctrl_error();
    }
834 835 836 837
    usb_linux_update_endp_table(s);
    return 0;
}

838 839
static int usb_host_handle_control(USBDevice *dev, USBPacket *p,
               int request, int value, int index, int length, uint8_t *data)
840
{
841
    USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);
842 843
    struct usbdevfs_urb *urb;
    AsyncURB *aurb;
844
    int ret;
845

846
    /*
847 848 849 850
     * Process certain standard device requests.
     * These are infrequent and are processed synchronously.
     */

851
    /* Note request is (bRequestType << 8) | bRequest */
M
malc 已提交
852
    DPRINTF("husb: ctrl type 0x%x req 0x%x val 0x%x index %u len %u\n",
853
            request >> 8, request & 0xff, value, index, length);
854

855 856 857
    switch (request) {
    case DeviceOutRequest | USB_REQ_SET_ADDRESS:
        return usb_host_set_address(s, value);
858

859 860
    case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
        return usb_host_set_config(s, value & 0xff);
861

862
    case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
863
        return usb_host_set_interface(s, index, value);
864
    }
865 866 867

    /* The rest are asynchronous */

868 869 870
    if (length > sizeof(dev->data_buf)) {
        fprintf(stderr, "husb: ctrl buffer too small (%d > %zu)\n",
                length, sizeof(dev->data_buf));
871
        return USB_RET_STALL;
J
Jim Paris 已提交
872 873
    }

G
Gerd Hoffmann 已提交
874
    aurb = async_alloc(s);
875 876
    aurb->packet = p;

877
    /*
878 879
     * Setup ctrl transfer.
     *
880
     * s->ctrl is laid out such that data buffer immediately follows
881
     * 'req' struct which is exactly what usbdevfs expects.
882
     */
883 884 885 886 887
    urb = &aurb->urb;

    urb->type     = USBDEVFS_URB_TYPE_CONTROL;
    urb->endpoint = p->devep;

888 889
    urb->buffer        = &dev->setup_buf;
    urb->buffer_length = length + 8;
890 891 892 893 894

    urb->usercontext = s;

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

M
malc 已提交
895
    DPRINTF("husb: submit ctrl. len %u aurb %p\n", urb->buffer_length, aurb);
896 897

    if (ret < 0) {
M
malc 已提交
898
        DPRINTF("husb: submit failed. errno %d\n", errno);
899 900 901 902 903 904 905 906 907 908 909 910 911 912
        async_free(aurb);

        switch(errno) {
        case ETIMEDOUT:
            return USB_RET_NAK;
        case EPIPE:
        default:
            return USB_RET_STALL;
        }
    }

    return USB_RET_ASYNC;
}

913
static int usb_linux_get_configuration(USBHostDevice *s)
914
{
915
    uint8_t configuration;
P
pbrook 已提交
916
    struct usb_ctrltransfer ct;
917
    int ret;
918

919 920 921 922
    if (usb_fs_type == USB_FS_SYS) {
        char device_name[32], line[1024];
        int configuration;

923
        sprintf(device_name, "%d-%s", s->bus_num, s->port);
924 925 926 927 928 929 930 931 932 933 934 935

        if (!usb_host_read_file(line, sizeof(line), "bConfigurationValue",
                                device_name)) {
            goto usbdevfs;
        }
        if (sscanf(line, "%d", &configuration) != 1) {
            goto usbdevfs;
        }
        return configuration;
    }

usbdevfs:
936 937 938 939 940 941 942 943 944 945
    ct.bRequestType = USB_DIR_IN;
    ct.bRequest = USB_REQ_GET_CONFIGURATION;
    ct.wValue = 0;
    ct.wIndex = 0;
    ct.wLength = 1;
    ct.data = &configuration;
    ct.timeout = 50;

    ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
    if (ret < 0) {
946 947
        perror("usb_linux_get_configuration");
        return -1;
948 949 950
    }

    /* in address state */
951
    if (configuration == 0) {
952
        return -1;
953
    }
954

955 956 957
    return configuration;
}

958 959 960 961 962 963 964
static uint8_t usb_linux_get_alt_setting(USBHostDevice *s,
    uint8_t configuration, uint8_t interface)
{
    uint8_t alt_setting;
    struct usb_ctrltransfer ct;
    int ret;

965 966 967 968
    if (usb_fs_type == USB_FS_SYS) {
        char device_name[64], line[1024];
        int alt_setting;

969
        sprintf(device_name, "%d-%s:%d.%d", s->bus_num, s->port,
970 971 972 973 974 975 976 977 978 979 980 981 982
                (int)configuration, (int)interface);

        if (!usb_host_read_file(line, sizeof(line), "bAlternateSetting",
                                device_name)) {
            goto usbdevfs;
        }
        if (sscanf(line, "%d", &alt_setting) != 1) {
            goto usbdevfs;
        }
        return alt_setting;
    }

usbdevfs:
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
    ct.bRequestType = USB_DIR_IN | USB_RECIP_INTERFACE;
    ct.bRequest = USB_REQ_GET_INTERFACE;
    ct.wValue = 0;
    ct.wIndex = interface;
    ct.wLength = 1;
    ct.data = &alt_setting;
    ct.timeout = 50;
    ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct);
    if (ret < 0) {
        /* Assume alt 0 on error */
        return 0;
    }

    return alt_setting;
}

999 1000 1001 1002 1003
/* returns 1 on problem encountered or 0 for success */
static int usb_linux_update_endp_table(USBHostDevice *s)
{
    uint8_t *descriptors;
    uint8_t devep, type, configuration, alt_interface;
1004
    int interface, length, i;
1005

1006 1007 1008
    for (i = 0; i < MAX_ENDPOINTS; i++)
        s->endp_table[i].type = INVALID_EP_TYPE;

1009 1010 1011 1012 1013
    i = usb_linux_get_configuration(s);
    if (i < 0)
        return 1;
    configuration = i;

1014 1015 1016 1017 1018 1019 1020 1021
    /* get the desired configuration, interface, and endpoint descriptors
     * from device description */
    descriptors = &s->descr[18];
    length = s->descr_len - 18;
    i = 0;

    if (descriptors[i + 1] != USB_DT_CONFIG ||
        descriptors[i + 5] != configuration) {
M
malc 已提交
1022
        DPRINTF("invalid descriptor data - configuration\n");
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
        return 1;
    }
    i += descriptors[i];

    while (i < length) {
        if (descriptors[i + 1] != USB_DT_INTERFACE ||
            (descriptors[i + 1] == USB_DT_INTERFACE &&
             descriptors[i + 4] == 0)) {
            i += descriptors[i];
            continue;
        }

        interface = descriptors[i + 2];
1036
        alt_interface = usb_linux_get_alt_setting(s, configuration, interface);
1037 1038 1039 1040 1041 1042 1043 1044 1045

        /* the current interface descriptor is the active interface
         * and has endpoints */
        if (descriptors[i + 3] != alt_interface) {
            i += descriptors[i];
            continue;
        }

        /* advance to the endpoints */
1046
        while (i < length && descriptors[i +1] != USB_DT_ENDPOINT) {
1047
            i += descriptors[i];
1048
        }
1049 1050 1051 1052 1053

        if (i >= length)
            break;

        while (i < length) {
1054
            if (descriptors[i + 1] != USB_DT_ENDPOINT) {
1055
                break;
1056
            }
1057 1058

            devep = descriptors[i + 2];
H
Hans de Goede 已提交
1059 1060 1061 1062 1063
            if ((devep & 0x0f) == 0) {
                fprintf(stderr, "usb-linux: invalid ep descriptor, ep == 0\n");
                return 1;
            }

1064 1065 1066 1067 1068 1069
            switch (descriptors[i + 3] & 0x3) {
            case 0x00:
                type = USBDEVFS_URB_TYPE_CONTROL;
                break;
            case 0x01:
                type = USBDEVFS_URB_TYPE_ISO;
1070
                set_max_packet_size(s, (devep & 0xf), descriptors + i);
1071 1072 1073 1074 1075 1076 1077
                break;
            case 0x02:
                type = USBDEVFS_URB_TYPE_BULK;
                break;
            case 0x03:
                type = USBDEVFS_URB_TYPE_INTERRUPT;
                break;
1078 1079 1080
            default:
                DPRINTF("usb_host: malformed endpoint type\n");
                type = USBDEVFS_URB_TYPE_BULK;
1081 1082
            }
            s->endp_table[(devep & 0xf) - 1].type = type;
1083
            s->endp_table[(devep & 0xf) - 1].halted = 0;
1084 1085 1086 1087 1088 1089 1090

            i += descriptors[i];
        }
    }
    return 0;
}

1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
/*
 * Check if we can safely redirect a usb2 device to a usb1 virtual controller,
 * this function assumes this is safe, if:
 * 1) There are no isoc endpoints
 * 2) There are no interrupt endpoints with a max_packet_size > 64
 * Note bulk endpoints with a max_packet_size > 64 in theory also are not
 * usb1 compatible, but in practice this seems to work fine.
 */
static int usb_linux_full_speed_compat(USBHostDevice *dev)
{
    int i, packet_size;

    /*
     * usb_linux_update_endp_table only registers info about ep in the current
     * interface altsettings, so we need to parse the descriptors again.
     */
    for (i = 0; (i + 5) < dev->descr_len; i += dev->descr[i]) {
        if (dev->descr[i + 1] == USB_DT_ENDPOINT) {
            switch (dev->descr[i + 3] & 0x3) {
            case 0x00: /* CONTROL */
                break;
            case 0x01: /* ISO */
                return 0;
            case 0x02: /* BULK */
                break;
            case 0x03: /* INTERRUPT */
                packet_size = dev->descr[i + 4] + (dev->descr[i + 5] << 8);
                if (packet_size > 64)
                    return 0;
                break;
            }
        }
    }
    return 1;
}

1127
static int usb_host_open(USBHostDevice *dev, int bus_num,
1128
                        int addr, char *port, const char *prod_name, int speed)
B
bellard 已提交
1129
{
1130
    int fd = -1, ret;
B
bellard 已提交
1131
    char buf[1024];
1132

1133
    if (dev->fd != -1) {
1134
        goto fail;
1135
    }
1136
    printf("husb: open device %d.%d\n", bus_num, addr);
1137

1138 1139 1140 1141 1142
    if (!usb_host_device_path) {
        perror("husb: USB Host Device Path not set");
        goto fail;
    }
    snprintf(buf, sizeof(buf), "%s/%03d/%03d", usb_host_device_path,
B
bellard 已提交
1143
             bus_num, addr);
1144
    fd = open(buf, O_RDWR | O_NONBLOCK);
B
bellard 已提交
1145
    if (fd < 0) {
B
bellard 已提交
1146
        perror(buf);
1147
        goto fail;
B
bellard 已提交
1148
    }
M
malc 已提交
1149
    DPRINTF("husb: opened %s\n", buf);
B
bellard 已提交
1150

1151 1152
    dev->bus_num = bus_num;
    dev->addr = addr;
1153
    strcpy(dev->port, port);
1154
    dev->fd = fd;
1155

1156 1157 1158
    /* read the device description */
    dev->descr_len = read(fd, dev->descr, sizeof(dev->descr));
    if (dev->descr_len <= 0) {
1159
        perror("husb: reading device data failed");
B
bellard 已提交
1160 1161
        goto fail;
    }
1162

1163
#ifdef DEBUG
B
bellard 已提交
1164
    {
1165 1166
        int x;
        printf("=== begin dumping device descriptor data ===\n");
1167
        for (x = 0; x < dev->descr_len; x++) {
1168
            printf("%02x ", dev->descr[x]);
1169
        }
1170
        printf("\n=== end dumping device descriptor data ===\n");
B
bellard 已提交
1171
    }
B
bellard 已提交
1172 1173
#endif

1174

1175 1176
    /*
     * Initial configuration is -1 which makes us claim first
1177
     * available config. We used to start with 1, which does not
1178
     * always work. I've seen devices where first config starts
1179 1180
     * with 2.
     */
1181
    if (!usb_host_claim_interfaces(dev, -1)) {
1182
        goto fail;
1183
    }
B
bellard 已提交
1184

1185
    ret = usb_linux_update_endp_table(dev);
1186
    if (ret) {
B
bellard 已提交
1187
        goto fail;
1188
    }
1189

1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
    if (speed == -1) {
        struct usbdevfs_connectinfo ci;

        ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci);
        if (ret < 0) {
            perror("usb_host_device_open: USBDEVFS_CONNECTINFO");
            goto fail;
        }

        if (ci.slow) {
            speed = USB_SPEED_LOW;
        } else {
            speed = USB_SPEED_HIGH;
        }
1204
    }
1205
    dev->dev.speed = speed;
H
Hans de Goede 已提交
1206
    dev->dev.speedmask = (1 << speed);
1207 1208 1209
    if (dev->dev.speed == USB_SPEED_HIGH && usb_linux_full_speed_compat(dev)) {
        dev->dev.speedmask |= USB_SPEED_MASK_FULL;
    }
1210 1211

    printf("husb: grabbed usb device %d.%d\n", bus_num, addr);
B
bellard 已提交
1212

1213
    if (!prod_name || prod_name[0] == '\0') {
1214
        snprintf(dev->dev.product_desc, sizeof(dev->dev.product_desc),
1215
                 "host:%d.%d", bus_num, addr);
1216
    } else {
1217
        pstrcpy(dev->dev.product_desc, sizeof(dev->dev.product_desc),
1218
                prod_name);
1219
    }
1220

1221 1222 1223 1224 1225
    ret = usb_device_attach(&dev->dev);
    if (ret) {
        goto fail;
    }

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

1229
    return 0;
1230

1231
fail:
1232 1233 1234
    if (dev->fd != -1) {
        close(dev->fd);
        dev->fd = -1;
1235
    }
1236 1237 1238 1239 1240
    return -1;
}

static int usb_host_close(USBHostDevice *dev)
{
1241 1242
    int i;

1243
    if (dev->fd == -1 || !dev->dev.attached) {
1244
        return -1;
1245
    }
1246 1247 1248

    qemu_set_fd_handler(dev->fd, NULL, NULL, NULL);
    dev->closing = 1;
1249
    for (i = 1; i <= MAX_ENDPOINTS; i++) {
1250 1251 1252 1253
        if (is_isoc(dev, i)) {
            usb_host_stop_n_free_iso(dev, i);
        }
    }
1254 1255 1256
    async_complete(dev);
    dev->closing = 0;
    usb_device_detach(&dev->dev);
1257
    ioctl(dev->fd, USBDEVFS_RESET);
1258 1259 1260 1261 1262
    close(dev->fd);
    dev->fd = -1;
    return 0;
}

1263 1264 1265 1266 1267 1268 1269 1270 1271
static void usb_host_exit_notifier(struct Notifier* n)
{
    USBHostDevice *s = container_of(n, USBHostDevice, exit);

    if (s->fd != -1) {
        ioctl(s->fd, USBDEVFS_RESET);
    }
}

1272 1273 1274 1275 1276 1277 1278
static int usb_host_initfn(USBDevice *dev)
{
    USBHostDevice *s = DO_UPCAST(USBHostDevice, dev, dev);

    dev->auto_attach = 0;
    s->fd = -1;
    QTAILQ_INSERT_TAIL(&hostdevs, s, next);
1279 1280
    s->exit.notify = usb_host_exit_notifier;
    qemu_add_exit_notifier(&s->exit);
1281 1282
    usb_host_auto_check(NULL);
    return 0;
B
bellard 已提交
1283
}
B
bellard 已提交
1284

1285
static struct USBDeviceInfo usb_host_dev_info = {
1286
    .product_desc   = "USB Host Device",
1287
    .qdev.name      = "usb-host",
1288 1289
    .qdev.size      = sizeof(USBHostDevice),
    .init           = usb_host_initfn,
1290
    .handle_packet  = usb_generic_handle_packet,
1291
    .cancel_packet  = usb_host_async_cancel,
1292 1293
    .handle_data    = usb_host_handle_data,
    .handle_control = usb_host_handle_control,
1294 1295
    .handle_reset   = usb_host_handle_reset,
    .handle_destroy = usb_host_handle_destroy,
1296 1297 1298 1299 1300
    .usbdevice_name = "host",
    .usbdevice_init = usb_host_device_open,
    .qdev.props     = (Property[]) {
        DEFINE_PROP_UINT32("hostbus",  USBHostDevice, match.bus_num,    0),
        DEFINE_PROP_UINT32("hostaddr", USBHostDevice, match.addr,       0),
G
Gerd Hoffmann 已提交
1301
        DEFINE_PROP_STRING("hostport", USBHostDevice, match.port),
1302 1303
        DEFINE_PROP_HEX32("vendorid",  USBHostDevice, match.vendor_id,  0),
        DEFINE_PROP_HEX32("productid", USBHostDevice, match.product_id, 0),
1304
        DEFINE_PROP_UINT32("isobufs",  USBHostDevice, iso_urb_count,    4),
1305 1306
        DEFINE_PROP_END_OF_LIST(),
    },
1307 1308 1309 1310 1311 1312 1313 1314
};

static void usb_host_register_devices(void)
{
    usb_qdev_register(&usb_host_dev_info);
}
device_init(usb_host_register_devices)

1315 1316
USBDevice *usb_host_device_open(const char *devname)
{
1317
    struct USBAutoFilter filter;
1318 1319 1320
    USBDevice *dev;
    char *p;

1321
    dev = usb_create(NULL /* FIXME */, "usb-host");
1322

1323
    if (strstr(devname, "auto:")) {
1324
        if (parse_filter(devname, &filter) < 0) {
1325
            goto fail;
1326
        }
1327 1328
    } else {
        if ((p = strchr(devname, '.'))) {
1329 1330 1331 1332
            filter.bus_num    = strtoul(devname, NULL, 0);
            filter.addr       = strtoul(p + 1, NULL, 0);
            filter.vendor_id  = 0;
            filter.product_id = 0;
1333
        } else if ((p = strchr(devname, ':'))) {
1334 1335
            filter.bus_num    = 0;
            filter.addr       = 0;
1336
            filter.vendor_id  = strtoul(devname, NULL, 16);
1337
            filter.product_id = strtoul(p + 1, NULL, 16);
1338 1339 1340
        } else {
            goto fail;
        }
1341
    }
1342

1343 1344
    qdev_prop_set_uint32(&dev->qdev, "hostbus",   filter.bus_num);
    qdev_prop_set_uint32(&dev->qdev, "hostaddr",  filter.addr);
1345 1346
    qdev_prop_set_uint32(&dev->qdev, "vendorid",  filter.vendor_id);
    qdev_prop_set_uint32(&dev->qdev, "productid", filter.product_id);
K
Kevin Wolf 已提交
1347
    qdev_init_nofail(&dev->qdev);
1348
    return dev;
1349

1350 1351 1352
fail:
    qdev_free(&dev->qdev);
    return NULL;
1353
}
1354 1355 1356

int usb_host_device_close(const char *devname)
{
1357
#if 0
1358 1359 1360 1361
    char product_name[PRODUCT_NAME_SZ];
    int bus_num, addr;
    USBHostDevice *s;

1362
    if (strstr(devname, "auto:")) {
1363
        return usb_host_auto_del(devname);
1364 1365 1366
    }
    if (usb_host_find_device(&bus_num, &addr, product_name,
                                    sizeof(product_name), devname) < 0) {
1367
        return -1;
1368
    }
1369 1370
    s = hostdev_find(bus_num, addr);
    if (s) {
1371
        usb_device_delete_addr(s->bus_num, s->dev.addr);
1372 1373
        return 0;
    }
1374
#endif
1375 1376 1377

    return -1;
}
1378

B
bellard 已提交
1379
static int get_tag_value(char *buf, int buf_size,
1380
                         const char *str, const char *tag,
B
bellard 已提交
1381 1382 1383 1384 1385
                         const char *stopchars)
{
    const char *p;
    char *q;
    p = strstr(str, tag);
1386
    if (!p) {
B
bellard 已提交
1387
        return -1;
1388
    }
B
bellard 已提交
1389
    p += strlen(tag);
1390
    while (qemu_isspace(*p)) {
B
bellard 已提交
1391
        p++;
1392
    }
B
bellard 已提交
1393 1394
    q = buf;
    while (*p != '\0' && !strchr(stopchars, *p)) {
1395
        if ((q - buf) < (buf_size - 1)) {
B
bellard 已提交
1396
            *q++ = *p;
1397
        }
B
bellard 已提交
1398 1399 1400 1401
        p++;
    }
    *q = '\0';
    return q - buf;
B
bellard 已提交
1402 1403
}

1404 1405 1406 1407 1408 1409
/*
 * Use /proc/bus/usb/devices or /dev/bus/usb/devices file to determine
 * host's USB devices. This is legacy support since many distributions
 * are moving to /sys/bus/usb
 */
static int usb_host_scan_dev(void *opaque, USBScanFunc *func)
B
bellard 已提交
1410
{
1411
    FILE *f = NULL;
B
bellard 已提交
1412
    char line[1024];
B
bellard 已提交
1413
    char buf[1024];
B
bellard 已提交
1414 1415
    int bus_num, addr, speed, device_count, class_id, product_id, vendor_id;
    char product_name[512];
1416
    int ret = 0;
1417

1418 1419 1420 1421 1422 1423
    if (!usb_host_device_path) {
        perror("husb: USB Host Device Path not set");
        goto the_end;
    }
    snprintf(line, sizeof(line), "%s/devices", usb_host_device_path);
    f = fopen(line, "r");
B
bellard 已提交
1424
    if (!f) {
1425 1426
        perror("husb: cannot open devices file");
        goto the_end;
B
bellard 已提交
1427
    }
1428

B
bellard 已提交
1429
    device_count = 0;
1430 1431
    bus_num = addr = class_id = product_id = vendor_id = 0;
    speed = -1; /* Can't get the speed from /[proc|dev]/bus/usb/devices */
B
bellard 已提交
1432
    for(;;) {
1433
        if (fgets(line, sizeof(line), f) == NULL) {
B
bellard 已提交
1434
            break;
1435 1436
        }
        if (strlen(line) > 0) {
B
bellard 已提交
1437
            line[strlen(line) - 1] = '\0';
1438
        }
B
bellard 已提交
1439
        if (line[0] == 'T' && line[1] == ':') {
1440 1441
            if (device_count && (vendor_id || product_id)) {
                /* New device.  Add the previously discovered device.  */
1442
                ret = func(opaque, bus_num, addr, 0, class_id, vendor_id,
B
bellard 已提交
1443
                           product_id, product_name, speed);
1444
                if (ret) {
B
bellard 已提交
1445
                    goto the_end;
1446
                }
B
bellard 已提交
1447
            }
1448
            if (get_tag_value(buf, sizeof(buf), line, "Bus=", " ") < 0) {
B
bellard 已提交
1449
                goto fail;
1450
            }
B
bellard 已提交
1451
            bus_num = atoi(buf);
1452
            if (get_tag_value(buf, sizeof(buf), line, "Dev#=", " ") < 0) {
B
bellard 已提交
1453
                goto fail;
1454
            }
B
bellard 已提交
1455
            addr = atoi(buf);
1456
            if (get_tag_value(buf, sizeof(buf), line, "Spd=", " ") < 0) {
B
bellard 已提交
1457
                goto fail;
1458
            }
1459 1460 1461
            if (!strcmp(buf, "5000")) {
                speed = USB_SPEED_SUPER;
            } else if (!strcmp(buf, "480")) {
B
bellard 已提交
1462
                speed = USB_SPEED_HIGH;
1463
            } else if (!strcmp(buf, "1.5")) {
B
bellard 已提交
1464
                speed = USB_SPEED_LOW;
1465
            } else {
B
bellard 已提交
1466
                speed = USB_SPEED_FULL;
1467
            }
B
bellard 已提交
1468 1469 1470 1471 1472 1473
            product_name[0] = '\0';
            class_id = 0xff;
            device_count++;
            product_id = 0;
            vendor_id = 0;
        } else if (line[0] == 'P' && line[1] == ':') {
1474
            if (get_tag_value(buf, sizeof(buf), line, "Vendor=", " ") < 0) {
B
bellard 已提交
1475
                goto fail;
1476
            }
B
bellard 已提交
1477
            vendor_id = strtoul(buf, NULL, 16);
1478
            if (get_tag_value(buf, sizeof(buf), line, "ProdID=", " ") < 0) {
B
bellard 已提交
1479
                goto fail;
1480
            }
B
bellard 已提交
1481 1482
            product_id = strtoul(buf, NULL, 16);
        } else if (line[0] == 'S' && line[1] == ':') {
1483
            if (get_tag_value(buf, sizeof(buf), line, "Product=", "") < 0) {
B
bellard 已提交
1484
                goto fail;
1485
            }
B
bellard 已提交
1486 1487
            pstrcpy(product_name, sizeof(product_name), buf);
        } else if (line[0] == 'D' && line[1] == ':') {
1488
            if (get_tag_value(buf, sizeof(buf), line, "Cls=", " (") < 0) {
B
bellard 已提交
1489
                goto fail;
1490
            }
B
bellard 已提交
1491
            class_id = strtoul(buf, NULL, 16);
B
bellard 已提交
1492
        }
B
bellard 已提交
1493 1494
    fail: ;
    }
1495 1496
    if (device_count && (vendor_id || product_id)) {
        /* Add the last device.  */
1497
        ret = func(opaque, bus_num, addr, 0, class_id, vendor_id,
B
bellard 已提交
1498
                   product_id, product_name, speed);
B
bellard 已提交
1499
    }
B
bellard 已提交
1500
 the_end:
1501
    if (f) {
1502
        fclose(f);
1503
    }
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
    return ret;
}

/*
 * Read sys file-system device file
 *
 * @line address of buffer to put file contents in
 * @line_size size of line
 * @device_file path to device file (printf format string)
 * @device_name device being opened (inserted into device_file)
 *
 * @return 0 failed, 1 succeeded ('line' contains data)
 */
1517 1518
static int usb_host_read_file(char *line, size_t line_size,
                              const char *device_file, const char *device_name)
1519 1520 1521 1522 1523
{
    FILE *f;
    int ret = 0;
    char filename[PATH_MAX];

1524 1525
    snprintf(filename, PATH_MAX, USBSYSBUS_PATH "/devices/%s/%s", device_name,
             device_file);
1526 1527
    f = fopen(filename, "r");
    if (f) {
1528
        ret = fgets(line, line_size, f) != NULL;
1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543
        fclose(f);
    }

    return ret;
}

/*
 * Use /sys/bus/usb/devices/ directory to determine host's USB
 * devices.
 *
 * This code is based on Robert Schiele's original patches posted to
 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950
 */
static int usb_host_scan_sys(void *opaque, USBScanFunc *func)
{
1544
    DIR *dir = NULL;
1545
    char line[1024];
1546
    int bus_num, addr, speed, class_id, product_id, vendor_id;
1547
    int ret = 0;
1548
    char port[MAX_PORTLEN];
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
    char product_name[512];
    struct dirent *de;

    dir = opendir(USBSYSBUS_PATH "/devices");
    if (!dir) {
        perror("husb: cannot open devices directory");
        goto the_end;
    }

    while ((de = readdir(dir))) {
        if (de->d_name[0] != '.' && !strchr(de->d_name, ':')) {
1560 1561
            if (sscanf(de->d_name, "%d-%7[0-9.]", &bus_num, port) < 2) {
                continue;
1562
            }
1563

1564
            if (!usb_host_read_file(line, sizeof(line), "devnum", de->d_name)) {
1565
                goto the_end;
1566 1567
            }
            if (sscanf(line, "%d", &addr) != 1) {
1568
                goto the_end;
1569
            }
1570
            if (!usb_host_read_file(line, sizeof(line), "bDeviceClass",
1571
                                    de->d_name)) {
1572
                goto the_end;
1573 1574
            }
            if (sscanf(line, "%x", &class_id) != 1) {
1575
                goto the_end;
1576
            }
1577

1578 1579
            if (!usb_host_read_file(line, sizeof(line), "idVendor",
                                    de->d_name)) {
1580
                goto the_end;
1581 1582
            }
            if (sscanf(line, "%x", &vendor_id) != 1) {
1583
                goto the_end;
1584
            }
1585
            if (!usb_host_read_file(line, sizeof(line), "idProduct",
1586
                                    de->d_name)) {
1587
                goto the_end;
1588 1589
            }
            if (sscanf(line, "%x", &product_id) != 1) {
1590
                goto the_end;
1591
            }
1592 1593
            if (!usb_host_read_file(line, sizeof(line), "product",
                                    de->d_name)) {
1594 1595
                *product_name = 0;
            } else {
1596
                if (strlen(line) > 0) {
1597
                    line[strlen(line) - 1] = '\0';
1598
                }
1599 1600 1601
                pstrcpy(product_name, sizeof(product_name), line);
            }

1602
            if (!usb_host_read_file(line, sizeof(line), "speed", de->d_name)) {
1603
                goto the_end;
1604
            }
1605 1606 1607
            if (!strcmp(line, "5000\n")) {
                speed = USB_SPEED_SUPER;
            } else if (!strcmp(line, "480\n")) {
1608
                speed = USB_SPEED_HIGH;
1609
            } else if (!strcmp(line, "1.5\n")) {
1610
                speed = USB_SPEED_LOW;
1611
            } else {
1612
                speed = USB_SPEED_FULL;
1613
            }
1614

1615
            ret = func(opaque, bus_num, addr, port, class_id, vendor_id,
1616
                       product_id, product_name, speed);
1617
            if (ret) {
1618
                goto the_end;
1619
            }
1620 1621 1622
        }
    }
 the_end:
1623
    if (dir) {
1624
        closedir(dir);
1625
    }
1626 1627 1628 1629 1630 1631 1632 1633 1634
    return ret;
}

/*
 * Determine how to access the host's USB devices and call the
 * specific support function.
 */
static int usb_host_scan(void *opaque, USBScanFunc *func)
{
A
aliguori 已提交
1635
    Monitor *mon = cur_mon;
1636 1637
    FILE *f = NULL;
    DIR *dir = NULL;
1638 1639 1640 1641 1642 1643
    int ret = 0;
    const char *fs_type[] = {"unknown", "proc", "dev", "sys"};
    char devpath[PATH_MAX];

    /* only check the host once */
    if (!usb_fs_type) {
1644 1645 1646 1647 1648 1649
        dir = opendir(USBSYSBUS_PATH "/devices");
        if (dir) {
            /* devices found in /dev/bus/usb/ (yes - not a mistake!) */
            strcpy(devpath, USBDEVBUS_PATH);
            usb_fs_type = USB_FS_SYS;
            closedir(dir);
M
malc 已提交
1650
            DPRINTF(USBDBG_DEVOPENED, USBSYSBUS_PATH);
1651 1652
            goto found_devices;
        }
1653 1654 1655 1656 1657 1658
        f = fopen(USBPROCBUS_PATH "/devices", "r");
        if (f) {
            /* devices found in /proc/bus/usb/ */
            strcpy(devpath, USBPROCBUS_PATH);
            usb_fs_type = USB_FS_PROC;
            fclose(f);
M
malc 已提交
1659
            DPRINTF(USBDBG_DEVOPENED, USBPROCBUS_PATH);
1660
            goto found_devices;
1661 1662 1663
        }
        /* try additional methods if an access method hasn't been found yet */
        f = fopen(USBDEVBUS_PATH "/devices", "r");
1664
        if (f) {
1665 1666 1667 1668
            /* devices found in /dev/bus/usb/ */
            strcpy(devpath, USBDEVBUS_PATH);
            usb_fs_type = USB_FS_DEV;
            fclose(f);
M
malc 已提交
1669
            DPRINTF(USBDBG_DEVOPENED, USBDEVBUS_PATH);
1670
            goto found_devices;
1671
        }
1672
    found_devices:
1673
        if (!usb_fs_type) {
1674
            if (mon) {
1675
                monitor_printf(mon, "husb: unable to access USB devices\n");
1676
            }
1677
            return -ENOENT;
1678 1679 1680 1681
        }

        /* the module setting (used later for opening devices) */
        usb_host_device_path = qemu_mallocz(strlen(devpath)+1);
1682
        strcpy(usb_host_device_path, devpath);
1683
        if (mon) {
1684 1685
            monitor_printf(mon, "husb: using %s file-system with %s\n",
                           fs_type[usb_fs_type], usb_host_device_path);
1686
        }
1687 1688 1689 1690 1691 1692 1693 1694 1695 1696
    }

    switch (usb_fs_type) {
    case USB_FS_PROC:
    case USB_FS_DEV:
        ret = usb_host_scan_dev(opaque, func);
        break;
    case USB_FS_SYS:
        ret = usb_host_scan_sys(opaque, func);
        break;
1697 1698 1699
    default:
        ret = -EINVAL;
        break;
1700
    }
B
bellard 已提交
1701
    return ret;
B
bellard 已提交
1702 1703
}

1704 1705
static QEMUTimer *usb_auto_timer;

1706
static int usb_host_auto_scan(void *opaque, int bus_num, int addr, char *port,
1707 1708
                              int class_id, int vendor_id, int product_id,
                              const char *product_name, int speed)
1709 1710
{
    struct USBAutoFilter *f;
1711
    struct USBHostDevice *s;
1712 1713 1714 1715 1716

    /* Ignore hubs */
    if (class_id == 9)
        return 0;

1717 1718 1719
    QTAILQ_FOREACH(s, &hostdevs, next) {
        f = &s->match;

1720
        if (f->bus_num > 0 && f->bus_num != bus_num) {
1721
            continue;
1722 1723
        }
        if (f->addr > 0 && f->addr != addr) {
1724
            continue;
1725
        }
G
Gerd Hoffmann 已提交
1726 1727 1728
        if (f->port != NULL && (port == NULL || strcmp(f->port, port) != 0)) {
            continue;
        }
1729

1730
        if (f->vendor_id > 0 && f->vendor_id != vendor_id) {
1731
            continue;
1732
        }
1733

1734
        if (f->product_id > 0 && f->product_id != product_id) {
1735
            continue;
1736
        }
1737 1738
        /* We got a match */

1739
        /* Already attached ? */
1740
        if (s->fd != -1) {
1741
            return 0;
1742
        }
M
malc 已提交
1743
        DPRINTF("husb: auto open: bus_num %d addr %d\n", bus_num, addr);
1744

1745
        usb_host_open(s, bus_num, addr, port, product_name, speed);
1746
        break;
1747 1748 1749 1750 1751
    }

    return 0;
}

1752
static void usb_host_auto_check(void *unused)
1753
{
1754 1755 1756
    struct USBHostDevice *s;
    int unconnected = 0;

1757
    usb_host_scan(NULL, usb_host_auto_scan);
1758 1759

    QTAILQ_FOREACH(s, &hostdevs, next) {
1760
        if (s->fd == -1) {
1761
            unconnected++;
1762
        }
1763 1764 1765 1766
    }

    if (unconnected == 0) {
        /* nothing to watch */
1767
        if (usb_auto_timer) {
1768
            qemu_del_timer(usb_auto_timer);
1769
        }
1770 1771 1772 1773
        return;
    }

    if (!usb_auto_timer) {
1774
        usb_auto_timer = qemu_new_timer_ms(rt_clock, usb_host_auto_check, NULL);
1775
        if (!usb_auto_timer) {
1776
            return;
1777
        }
1778
    }
1779
    qemu_mod_timer(usb_auto_timer, qemu_get_clock_ms(rt_clock) + 2000);
1780 1781 1782
}

/*
1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793
 * Autoconnect filter
 * Format:
 *    auto:bus:dev[:vid:pid]
 *    auto:bus.dev[:vid:pid]
 *
 *    bus  - bus number    (dec, * means any)
 *    dev  - device number (dec, * means any)
 *    vid  - vendor id     (hex, * means any)
 *    pid  - product id    (hex, * means any)
 *
 *    See 'lsusb' output.
1794
 */
1795
static int parse_filter(const char *spec, struct USBAutoFilter *f)
1796
{
1797 1798 1799 1800
    enum { BUS, DEV, VID, PID, DONE };
    const char *p = spec;
    int i;

1801 1802 1803 1804
    f->bus_num    = 0;
    f->addr       = 0;
    f->vendor_id  = 0;
    f->product_id = 0;
1805 1806

    for (i = BUS; i < DONE; i++) {
1807 1808 1809 1810
        p = strpbrk(p, ":.");
        if (!p) {
            break;
        }
1811 1812
        p++;

1813 1814 1815
        if (*p == '*') {
            continue;
        }
1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831
        switch(i) {
        case BUS: f->bus_num = strtol(p, NULL, 10);    break;
        case DEV: f->addr    = strtol(p, NULL, 10);    break;
        case VID: f->vendor_id  = strtol(p, NULL, 16); break;
        case PID: f->product_id = strtol(p, NULL, 16); break;
        }
    }

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

    return 0;
}

B
bellard 已提交
1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851
/**********************/
/* USB host device info */

struct usb_class_info {
    int class;
    const char *class_name;
};

static const struct usb_class_info usb_class_info[] = {
    { USB_CLASS_AUDIO, "Audio"},
    { USB_CLASS_COMM, "Communication"},
    { USB_CLASS_HID, "HID"},
    { USB_CLASS_HUB, "Hub" },
    { USB_CLASS_PHYSICAL, "Physical" },
    { USB_CLASS_PRINTER, "Printer" },
    { USB_CLASS_MASS_STORAGE, "Storage" },
    { USB_CLASS_CDC_DATA, "Data" },
    { USB_CLASS_APP_SPEC, "Application Specific" },
    { USB_CLASS_VENDOR_SPEC, "Vendor Specific" },
    { USB_CLASS_STILL_IMAGE, "Still Image" },
1852
    { USB_CLASS_CSCID, "Smart Card" },
B
bellard 已提交
1853 1854 1855 1856 1857
    { USB_CLASS_CONTENT_SEC, "Content Security" },
    { -1, NULL }
};

static const char *usb_class_str(uint8_t class)
B
bellard 已提交
1858
{
B
bellard 已提交
1859 1860
    const struct usb_class_info *p;
    for(p = usb_class_info; p->class != -1; p++) {
1861
        if (p->class == class) {
B
bellard 已提交
1862
            break;
1863
        }
B
bellard 已提交
1864
    }
B
bellard 已提交
1865 1866 1867
    return p->class_name;
}

1868 1869
static void usb_info_device(Monitor *mon, int bus_num, int addr, char *port,
                            int class_id, int vendor_id, int product_id,
1870 1871
                            const char *product_name,
                            int speed)
B
bellard 已提交
1872 1873 1874 1875
{
    const char *class_str, *speed_str;

    switch(speed) {
1876 1877
    case USB_SPEED_LOW:
        speed_str = "1.5";
B
bellard 已提交
1878
        break;
1879 1880
    case USB_SPEED_FULL:
        speed_str = "12";
B
bellard 已提交
1881
        break;
1882 1883
    case USB_SPEED_HIGH:
        speed_str = "480";
B
bellard 已提交
1884
        break;
1885 1886 1887
    case USB_SPEED_SUPER:
        speed_str = "5000";
        break;
B
bellard 已提交
1888
    default:
1889
        speed_str = "?";
B
bellard 已提交
1890 1891 1892
        break;
    }

1893 1894
    monitor_printf(mon, "  Bus %d, Addr %d, Port %s, Speed %s Mb/s\n",
                   bus_num, addr, port, speed_str);
B
bellard 已提交
1895
    class_str = usb_class_str(class_id);
1896
    if (class_str) {
A
aliguori 已提交
1897
        monitor_printf(mon, "    %s:", class_str);
1898
    } else {
A
aliguori 已提交
1899
        monitor_printf(mon, "    Class %02x:", class_id);
1900
    }
A
aliguori 已提交
1901
    monitor_printf(mon, " USB device %04x:%04x", vendor_id, product_id);
1902
    if (product_name[0] != '\0') {
A
aliguori 已提交
1903
        monitor_printf(mon, ", %s", product_name);
1904
    }
A
aliguori 已提交
1905
    monitor_printf(mon, "\n");
B
bellard 已提交
1906 1907
}

1908
static int usb_host_info_device(void *opaque, int bus_num, int addr,
1909
                                char *path, int class_id,
1910
                                int vendor_id, int product_id,
B
bellard 已提交
1911 1912 1913
                                const char *product_name,
                                int speed)
{
1914 1915
    Monitor *mon = opaque;

1916
    usb_info_device(mon, bus_num, addr, path, class_id, vendor_id, product_id,
B
bellard 已提交
1917 1918 1919 1920
                    product_name, speed);
    return 0;
}

A
aliguori 已提交
1921
static void dec2str(int val, char *str, size_t size)
1922
{
1923
    if (val == 0) {
A
aliguori 已提交
1924
        snprintf(str, size, "*");
1925 1926 1927
    } else {
        snprintf(str, size, "%d", val);
    }
1928 1929
}

A
aliguori 已提交
1930
static void hex2str(int val, char *str, size_t size)
1931
{
1932
    if (val == 0) {
A
aliguori 已提交
1933
        snprintf(str, size, "*");
1934
    } else {
1935
        snprintf(str, size, "%04x", val);
1936
    }
1937 1938
}

A
aliguori 已提交
1939
void usb_host_info(Monitor *mon)
B
bellard 已提交
1940
{
1941
    struct USBAutoFilter *f;
1942
    struct USBHostDevice *s;
1943

1944
    usb_host_scan(mon, usb_host_info_device);
1945

1946
    if (QTAILQ_EMPTY(&hostdevs)) {
1947
        return;
1948 1949
    }

1950 1951
    monitor_printf(mon, "  Auto filters:\n");
    QTAILQ_FOREACH(s, &hostdevs, next) {
1952
        char bus[10], addr[10], vid[10], pid[10];
1953
        f = &s->match;
A
aliguori 已提交
1954 1955 1956 1957
        dec2str(f->bus_num, bus, sizeof(bus));
        dec2str(f->addr, addr, sizeof(addr));
        hex2str(f->vendor_id, vid, sizeof(vid));
        hex2str(f->product_id, pid, sizeof(pid));
G
Gerd Hoffmann 已提交
1958 1959
        monitor_printf(mon, "    Bus %s, Addr %s, Port %s, ID %s:%s\n",
                       bus, addr, f->port ? f->port : "*", vid, pid);
1960
    }
B
bellard 已提交
1961
}