qemu/hw/usb/core.c
<<
>>
Prefs
   1/*
   2 * QEMU USB emulation
   3 *
   4 * Copyright (c) 2005 Fabrice Bellard
   5 *
   6 * 2008 Generic packet handler rewrite by Max Krasnyansky
   7 *
   8 * Permission is hereby granted, free of charge, to any person obtaining a copy
   9 * of this software and associated documentation files (the "Software"), to deal
  10 * in the Software without restriction, including without limitation the rights
  11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12 * copies of the Software, and to permit persons to whom the Software is
  13 * furnished to do so, subject to the following conditions:
  14 *
  15 * The above copyright notice and this permission notice shall be included in
  16 * all copies or substantial portions of the Software.
  17 *
  18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24 * THE SOFTWARE.
  25 */
  26#include "qemu/osdep.h"
  27#include "hw/usb.h"
  28#include "qemu/iov.h"
  29#include "trace.h"
  30
  31void usb_pick_speed(USBPort *port)
  32{
  33    static const int speeds[] = {
  34        USB_SPEED_SUPER,
  35        USB_SPEED_HIGH,
  36        USB_SPEED_FULL,
  37        USB_SPEED_LOW,
  38    };
  39    USBDevice *udev = port->dev;
  40    int i;
  41
  42    for (i = 0; i < ARRAY_SIZE(speeds); i++) {
  43        if ((udev->speedmask & (1 << speeds[i])) &&
  44            (port->speedmask & (1 << speeds[i]))) {
  45            udev->speed = speeds[i];
  46            return;
  47        }
  48    }
  49}
  50
  51void usb_attach(USBPort *port)
  52{
  53    USBDevice *dev = port->dev;
  54
  55    assert(dev != NULL);
  56    assert(dev->attached);
  57    assert(dev->state == USB_STATE_NOTATTACHED);
  58    usb_pick_speed(port);
  59    port->ops->attach(port);
  60    dev->state = USB_STATE_ATTACHED;
  61    usb_device_handle_attach(dev);
  62}
  63
  64void usb_detach(USBPort *port)
  65{
  66    USBDevice *dev = port->dev;
  67
  68    assert(dev != NULL);
  69    assert(dev->state != USB_STATE_NOTATTACHED);
  70    port->ops->detach(port);
  71    dev->state = USB_STATE_NOTATTACHED;
  72}
  73
  74void usb_port_reset(USBPort *port)
  75{
  76    USBDevice *dev = port->dev;
  77
  78    assert(dev != NULL);
  79    usb_detach(port);
  80    usb_attach(port);
  81    usb_device_reset(dev);
  82}
  83
  84void usb_device_reset(USBDevice *dev)
  85{
  86    if (dev == NULL || !dev->attached) {
  87        return;
  88    }
  89    usb_device_handle_reset(dev);
  90    dev->remote_wakeup = 0;
  91    dev->addr = 0;
  92    dev->state = USB_STATE_DEFAULT;
  93}
  94
  95void usb_wakeup(USBEndpoint *ep, unsigned int stream)
  96{
  97    USBDevice *dev = ep->dev;
  98    USBBus *bus = usb_bus_from_device(dev);
  99
 100    if (!qdev_hotplug) {
 101        /*
 102         * This is machine init cold plug.  No need to wakeup anyone,
 103         * all devices will be reset anyway.  And trying to wakeup can
 104         * cause problems due to hitting uninitialized devices.
 105         */
 106        return;
 107    }
 108    if (dev->remote_wakeup && dev->port && dev->port->ops->wakeup) {
 109        dev->port->ops->wakeup(dev->port);
 110    }
 111    if (bus->ops->wakeup_endpoint) {
 112        bus->ops->wakeup_endpoint(bus, ep, stream);
 113    }
 114}
 115
 116/**********************/
 117
 118/* generic USB device helpers (you are not forced to use them when
 119   writing your USB device driver, but they help handling the
 120   protocol)
 121*/
 122
 123#define SETUP_STATE_IDLE  0
 124#define SETUP_STATE_SETUP 1
 125#define SETUP_STATE_DATA  2
 126#define SETUP_STATE_ACK   3
 127#define SETUP_STATE_PARAM 4
 128
 129static void do_token_setup(USBDevice *s, USBPacket *p)
 130{
 131    int request, value, index;
 132
 133    if (p->iov.size != 8) {
 134        p->status = USB_RET_STALL;
 135        return;
 136    }
 137
 138    usb_packet_copy(p, s->setup_buf, p->iov.size);
 139    s->setup_index = 0;
 140    p->actual_length = 0;
 141    s->setup_len   = (s->setup_buf[7] << 8) | s->setup_buf[6];
 142    if (s->setup_len > sizeof(s->data_buf)) {
 143        fprintf(stderr,
 144                "usb_generic_handle_packet: ctrl buffer too small (%d > %zu)\n",
 145                s->setup_len, sizeof(s->data_buf));
 146        p->status = USB_RET_STALL;
 147        return;
 148    }
 149
 150    request = (s->setup_buf[0] << 8) | s->setup_buf[1];
 151    value   = (s->setup_buf[3] << 8) | s->setup_buf[2];
 152    index   = (s->setup_buf[5] << 8) | s->setup_buf[4];
 153
 154    if (s->setup_buf[0] & USB_DIR_IN) {
 155        usb_device_handle_control(s, p, request, value, index,
 156                                  s->setup_len, s->data_buf);
 157        if (p->status == USB_RET_ASYNC) {
 158            s->setup_state = SETUP_STATE_SETUP;
 159        }
 160        if (p->status != USB_RET_SUCCESS) {
 161            return;
 162        }
 163
 164        if (p->actual_length < s->setup_len) {
 165            s->setup_len = p->actual_length;
 166        }
 167        s->setup_state = SETUP_STATE_DATA;
 168    } else {
 169        if (s->setup_len == 0)
 170            s->setup_state = SETUP_STATE_ACK;
 171        else
 172            s->setup_state = SETUP_STATE_DATA;
 173    }
 174
 175    p->actual_length = 8;
 176}
 177
 178static void do_token_in(USBDevice *s, USBPacket *p)
 179{
 180    int request, value, index;
 181
 182    assert(p->ep->nr == 0);
 183
 184    request = (s->setup_buf[0] << 8) | s->setup_buf[1];
 185    value   = (s->setup_buf[3] << 8) | s->setup_buf[2];
 186    index   = (s->setup_buf[5] << 8) | s->setup_buf[4];
 187
 188    switch(s->setup_state) {
 189    case SETUP_STATE_ACK:
 190        if (!(s->setup_buf[0] & USB_DIR_IN)) {
 191            usb_device_handle_control(s, p, request, value, index,
 192                                      s->setup_len, s->data_buf);
 193            if (p->status == USB_RET_ASYNC) {
 194                return;
 195            }
 196            s->setup_state = SETUP_STATE_IDLE;
 197            p->actual_length = 0;
 198        }
 199        break;
 200
 201    case SETUP_STATE_DATA:
 202        if (s->setup_buf[0] & USB_DIR_IN) {
 203            int len = s->setup_len - s->setup_index;
 204            if (len > p->iov.size) {
 205                len = p->iov.size;
 206            }
 207            usb_packet_copy(p, s->data_buf + s->setup_index, len);
 208            s->setup_index += len;
 209            if (s->setup_index >= s->setup_len) {
 210                s->setup_state = SETUP_STATE_ACK;
 211            }
 212            return;
 213        }
 214        s->setup_state = SETUP_STATE_IDLE;
 215        p->status = USB_RET_STALL;
 216        break;
 217
 218    default:
 219        p->status = USB_RET_STALL;
 220    }
 221}
 222
 223static void do_token_out(USBDevice *s, USBPacket *p)
 224{
 225    assert(p->ep->nr == 0);
 226
 227    switch(s->setup_state) {
 228    case SETUP_STATE_ACK:
 229        if (s->setup_buf[0] & USB_DIR_IN) {
 230            s->setup_state = SETUP_STATE_IDLE;
 231            /* transfer OK */
 232        } else {
 233            /* ignore additional output */
 234        }
 235        break;
 236
 237    case SETUP_STATE_DATA:
 238        if (!(s->setup_buf[0] & USB_DIR_IN)) {
 239            int len = s->setup_len - s->setup_index;
 240            if (len > p->iov.size) {
 241                len = p->iov.size;
 242            }
 243            usb_packet_copy(p, s->data_buf + s->setup_index, len);
 244            s->setup_index += len;
 245            if (s->setup_index >= s->setup_len) {
 246                s->setup_state = SETUP_STATE_ACK;
 247            }
 248            return;
 249        }
 250        s->setup_state = SETUP_STATE_IDLE;
 251        p->status = USB_RET_STALL;
 252        break;
 253
 254    default:
 255        p->status = USB_RET_STALL;
 256    }
 257}
 258
 259static void do_parameter(USBDevice *s, USBPacket *p)
 260{
 261    int i, request, value, index;
 262
 263    for (i = 0; i < 8; i++) {
 264        s->setup_buf[i] = p->parameter >> (i*8);
 265    }
 266
 267    s->setup_state = SETUP_STATE_PARAM;
 268    s->setup_len   = (s->setup_buf[7] << 8) | s->setup_buf[6];
 269    s->setup_index = 0;
 270
 271    request = (s->setup_buf[0] << 8) | s->setup_buf[1];
 272    value   = (s->setup_buf[3] << 8) | s->setup_buf[2];
 273    index   = (s->setup_buf[5] << 8) | s->setup_buf[4];
 274
 275    if (s->setup_len > sizeof(s->data_buf)) {
 276        fprintf(stderr,
 277                "usb_generic_handle_packet: ctrl buffer too small (%d > %zu)\n",
 278                s->setup_len, sizeof(s->data_buf));
 279        p->status = USB_RET_STALL;
 280        return;
 281    }
 282
 283    if (p->pid == USB_TOKEN_OUT) {
 284        usb_packet_copy(p, s->data_buf, s->setup_len);
 285    }
 286
 287    usb_device_handle_control(s, p, request, value, index,
 288                              s->setup_len, s->data_buf);
 289    if (p->status == USB_RET_ASYNC) {
 290        return;
 291    }
 292
 293    if (p->actual_length < s->setup_len) {
 294        s->setup_len = p->actual_length;
 295    }
 296    if (p->pid == USB_TOKEN_IN) {
 297        p->actual_length = 0;
 298        usb_packet_copy(p, s->data_buf, s->setup_len);
 299    }
 300}
 301
 302/* ctrl complete function for devices which use usb_generic_handle_packet and
 303   may return USB_RET_ASYNC from their handle_control callback. Device code
 304   which does this *must* call this function instead of the normal
 305   usb_packet_complete to complete their async control packets. */
 306void usb_generic_async_ctrl_complete(USBDevice *s, USBPacket *p)
 307{
 308    if (p->status < 0) {
 309        s->setup_state = SETUP_STATE_IDLE;
 310    }
 311
 312    switch (s->setup_state) {
 313    case SETUP_STATE_SETUP:
 314        if (p->actual_length < s->setup_len) {
 315            s->setup_len = p->actual_length;
 316        }
 317        s->setup_state = SETUP_STATE_DATA;
 318        p->actual_length = 8;
 319        break;
 320
 321    case SETUP_STATE_ACK:
 322        s->setup_state = SETUP_STATE_IDLE;
 323        p->actual_length = 0;
 324        break;
 325
 326    case SETUP_STATE_PARAM:
 327        if (p->actual_length < s->setup_len) {
 328            s->setup_len = p->actual_length;
 329        }
 330        if (p->pid == USB_TOKEN_IN) {
 331            p->actual_length = 0;
 332            usb_packet_copy(p, s->data_buf, s->setup_len);
 333        }
 334        break;
 335
 336    default:
 337        break;
 338    }
 339    usb_packet_complete(s, p);
 340}
 341
 342USBDevice *usb_find_device(USBPort *port, uint8_t addr)
 343{
 344    USBDevice *dev = port->dev;
 345
 346    if (dev == NULL || !dev->attached || dev->state != USB_STATE_DEFAULT) {
 347        return NULL;
 348    }
 349    if (dev->addr == addr) {
 350        return dev;
 351    }
 352    return usb_device_find_device(dev, addr);
 353}
 354
 355static void usb_process_one(USBPacket *p)
 356{
 357    USBDevice *dev = p->ep->dev;
 358
 359    /*
 360     * Handlers expect status to be initialized to USB_RET_SUCCESS, but it
 361     * can be USB_RET_NAK here from a previous usb_process_one() call,
 362     * or USB_RET_ASYNC from going through usb_queue_one().
 363     */
 364    p->status = USB_RET_SUCCESS;
 365
 366    if (p->ep->nr == 0) {
 367        /* control pipe */
 368        if (p->parameter) {
 369            do_parameter(dev, p);
 370            return;
 371        }
 372        switch (p->pid) {
 373        case USB_TOKEN_SETUP:
 374            do_token_setup(dev, p);
 375            break;
 376        case USB_TOKEN_IN:
 377            do_token_in(dev, p);
 378            break;
 379        case USB_TOKEN_OUT:
 380            do_token_out(dev, p);
 381            break;
 382        default:
 383            p->status = USB_RET_STALL;
 384        }
 385    } else {
 386        /* data pipe */
 387        usb_device_handle_data(dev, p);
 388    }
 389}
 390
 391static void usb_queue_one(USBPacket *p)
 392{
 393    usb_packet_set_state(p, USB_PACKET_QUEUED);
 394    QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue);
 395    p->status = USB_RET_ASYNC;
 396}
 397
 398/* Hand over a packet to a device for processing.  p->status ==
 399   USB_RET_ASYNC indicates the processing isn't finished yet, the
 400   driver will call usb_packet_complete() when done processing it. */
 401void usb_handle_packet(USBDevice *dev, USBPacket *p)
 402{
 403    if (dev == NULL) {
 404        p->status = USB_RET_NODEV;
 405        return;
 406    }
 407    assert(dev == p->ep->dev);
 408    assert(dev->state == USB_STATE_DEFAULT);
 409    usb_packet_check_state(p, USB_PACKET_SETUP);
 410    assert(p->ep != NULL);
 411
 412    /* Submitting a new packet clears halt */
 413    if (p->ep->halted) {
 414        assert(QTAILQ_EMPTY(&p->ep->queue));
 415        p->ep->halted = false;
 416    }
 417
 418    if (QTAILQ_EMPTY(&p->ep->queue) || p->ep->pipeline || p->stream) {
 419        usb_process_one(p);
 420        if (p->status == USB_RET_ASYNC) {
 421            /* hcd drivers cannot handle async for isoc */
 422            assert(p->ep->type != USB_ENDPOINT_XFER_ISOC);
 423            /* using async for interrupt packets breaks migration */
 424            assert(p->ep->type != USB_ENDPOINT_XFER_INT ||
 425                   (dev->flags & (1 << USB_DEV_FLAG_IS_HOST)));
 426            usb_packet_set_state(p, USB_PACKET_ASYNC);
 427            QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue);
 428        } else if (p->status == USB_RET_ADD_TO_QUEUE) {
 429            usb_queue_one(p);
 430        } else {
 431            /*
 432             * When pipelining is enabled usb-devices must always return async,
 433             * otherwise packets can complete out of order!
 434             */
 435            assert(p->stream || !p->ep->pipeline ||
 436                   QTAILQ_EMPTY(&p->ep->queue));
 437            if (p->status != USB_RET_NAK) {
 438                usb_packet_set_state(p, USB_PACKET_COMPLETE);
 439            }
 440        }
 441    } else {
 442        usb_queue_one(p);
 443    }
 444}
 445
 446void usb_packet_complete_one(USBDevice *dev, USBPacket *p)
 447{
 448    USBEndpoint *ep = p->ep;
 449
 450    assert(p->stream || QTAILQ_FIRST(&ep->queue) == p);
 451    assert(p->status != USB_RET_ASYNC && p->status != USB_RET_NAK);
 452
 453    if (p->status != USB_RET_SUCCESS ||
 454            (p->short_not_ok && (p->actual_length < p->iov.size))) {
 455        ep->halted = true;
 456    }
 457    usb_packet_set_state(p, USB_PACKET_COMPLETE);
 458    QTAILQ_REMOVE(&ep->queue, p, queue);
 459    dev->port->ops->complete(dev->port, p);
 460}
 461
 462/* Notify the controller that an async packet is complete.  This should only
 463   be called for packets previously deferred by returning USB_RET_ASYNC from
 464   handle_packet. */
 465void usb_packet_complete(USBDevice *dev, USBPacket *p)
 466{
 467    USBEndpoint *ep = p->ep;
 468
 469    usb_packet_check_state(p, USB_PACKET_ASYNC);
 470    usb_packet_complete_one(dev, p);
 471
 472    while (!QTAILQ_EMPTY(&ep->queue)) {
 473        p = QTAILQ_FIRST(&ep->queue);
 474        if (ep->halted) {
 475            /* Empty the queue on a halt */
 476            p->status = USB_RET_REMOVE_FROM_QUEUE;
 477            dev->port->ops->complete(dev->port, p);
 478            continue;
 479        }
 480        if (p->state == USB_PACKET_ASYNC) {
 481            break;
 482        }
 483        usb_packet_check_state(p, USB_PACKET_QUEUED);
 484        usb_process_one(p);
 485        if (p->status == USB_RET_ASYNC) {
 486            usb_packet_set_state(p, USB_PACKET_ASYNC);
 487            break;
 488        }
 489        usb_packet_complete_one(ep->dev, p);
 490    }
 491}
 492
 493/* Cancel an active packet.  The packed must have been deferred by
 494   returning USB_RET_ASYNC from handle_packet, and not yet
 495   completed.  */
 496void usb_cancel_packet(USBPacket * p)
 497{
 498    bool callback = (p->state == USB_PACKET_ASYNC);
 499    assert(usb_packet_is_inflight(p));
 500    usb_packet_set_state(p, USB_PACKET_CANCELED);
 501    QTAILQ_REMOVE(&p->ep->queue, p, queue);
 502    if (callback) {
 503        usb_device_cancel_packet(p->ep->dev, p);
 504    }
 505}
 506
 507
 508void usb_packet_init(USBPacket *p)
 509{
 510    qemu_iovec_init(&p->iov, 1);
 511}
 512
 513static const char *usb_packet_state_name(USBPacketState state)
 514{
 515    static const char *name[] = {
 516        [USB_PACKET_UNDEFINED] = "undef",
 517        [USB_PACKET_SETUP]     = "setup",
 518        [USB_PACKET_QUEUED]    = "queued",
 519        [USB_PACKET_ASYNC]     = "async",
 520        [USB_PACKET_COMPLETE]  = "complete",
 521        [USB_PACKET_CANCELED]  = "canceled",
 522    };
 523    if (state < ARRAY_SIZE(name)) {
 524        return name[state];
 525    }
 526    return "INVALID";
 527}
 528
 529void usb_packet_check_state(USBPacket *p, USBPacketState expected)
 530{
 531    USBDevice *dev;
 532    USBBus *bus;
 533
 534    if (p->state == expected) {
 535        return;
 536    }
 537    dev = p->ep->dev;
 538    bus = usb_bus_from_device(dev);
 539    trace_usb_packet_state_fault(bus->busnr, dev->port->path, p->ep->nr, p,
 540                                 usb_packet_state_name(p->state),
 541                                 usb_packet_state_name(expected));
 542    assert(!"usb packet state check failed");
 543}
 544
 545void usb_packet_set_state(USBPacket *p, USBPacketState state)
 546{
 547    if (p->ep) {
 548        USBDevice *dev = p->ep->dev;
 549        USBBus *bus = usb_bus_from_device(dev);
 550        trace_usb_packet_state_change(bus->busnr, dev->port->path, p->ep->nr, p,
 551                                      usb_packet_state_name(p->state),
 552                                      usb_packet_state_name(state));
 553    } else {
 554        trace_usb_packet_state_change(-1, "", -1, p,
 555                                      usb_packet_state_name(p->state),
 556                                      usb_packet_state_name(state));
 557    }
 558    p->state = state;
 559}
 560
 561void usb_packet_setup(USBPacket *p, int pid,
 562                      USBEndpoint *ep, unsigned int stream,
 563                      uint64_t id, bool short_not_ok, bool int_req)
 564{
 565    assert(!usb_packet_is_inflight(p));
 566    assert(p->iov.iov != NULL);
 567    p->id = id;
 568    p->pid = pid;
 569    p->ep = ep;
 570    p->stream = stream;
 571    p->status = USB_RET_SUCCESS;
 572    p->actual_length = 0;
 573    p->parameter = 0;
 574    p->short_not_ok = short_not_ok;
 575    p->int_req = int_req;
 576    p->combined = NULL;
 577    qemu_iovec_reset(&p->iov);
 578    usb_packet_set_state(p, USB_PACKET_SETUP);
 579}
 580
 581void usb_packet_addbuf(USBPacket *p, void *ptr, size_t len)
 582{
 583    qemu_iovec_add(&p->iov, ptr, len);
 584}
 585
 586void usb_packet_copy(USBPacket *p, void *ptr, size_t bytes)
 587{
 588    QEMUIOVector *iov = p->combined ? &p->combined->iov : &p->iov;
 589
 590    assert(p->actual_length >= 0);
 591    assert(p->actual_length + bytes <= iov->size);
 592    switch (p->pid) {
 593    case USB_TOKEN_SETUP:
 594    case USB_TOKEN_OUT:
 595        iov_to_buf(iov->iov, iov->niov, p->actual_length, ptr, bytes);
 596        break;
 597    case USB_TOKEN_IN:
 598        iov_from_buf(iov->iov, iov->niov, p->actual_length, ptr, bytes);
 599        break;
 600    default:
 601        fprintf(stderr, "%s: invalid pid: %x\n", __func__, p->pid);
 602        abort();
 603    }
 604    p->actual_length += bytes;
 605}
 606
 607void usb_packet_skip(USBPacket *p, size_t bytes)
 608{
 609    QEMUIOVector *iov = p->combined ? &p->combined->iov : &p->iov;
 610
 611    assert(p->actual_length >= 0);
 612    assert(p->actual_length + bytes <= iov->size);
 613    if (p->pid == USB_TOKEN_IN) {
 614        iov_memset(iov->iov, iov->niov, p->actual_length, 0, bytes);
 615    }
 616    p->actual_length += bytes;
 617}
 618
 619size_t usb_packet_size(USBPacket *p)
 620{
 621    return p->combined ? p->combined->iov.size : p->iov.size;
 622}
 623
 624void usb_packet_cleanup(USBPacket *p)
 625{
 626    assert(!usb_packet_is_inflight(p));
 627    qemu_iovec_destroy(&p->iov);
 628}
 629
 630void usb_ep_reset(USBDevice *dev)
 631{
 632    int ep;
 633
 634    dev->ep_ctl.nr = 0;
 635    dev->ep_ctl.type = USB_ENDPOINT_XFER_CONTROL;
 636    dev->ep_ctl.ifnum = 0;
 637    dev->ep_ctl.max_packet_size = 64;
 638    dev->ep_ctl.max_streams = 0;
 639    dev->ep_ctl.dev = dev;
 640    dev->ep_ctl.pipeline = false;
 641    for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
 642        dev->ep_in[ep].nr = ep + 1;
 643        dev->ep_out[ep].nr = ep + 1;
 644        dev->ep_in[ep].pid = USB_TOKEN_IN;
 645        dev->ep_out[ep].pid = USB_TOKEN_OUT;
 646        dev->ep_in[ep].type = USB_ENDPOINT_XFER_INVALID;
 647        dev->ep_out[ep].type = USB_ENDPOINT_XFER_INVALID;
 648        dev->ep_in[ep].ifnum = USB_INTERFACE_INVALID;
 649        dev->ep_out[ep].ifnum = USB_INTERFACE_INVALID;
 650        dev->ep_in[ep].max_packet_size = 0;
 651        dev->ep_out[ep].max_packet_size = 0;
 652        dev->ep_in[ep].max_streams = 0;
 653        dev->ep_out[ep].max_streams = 0;
 654        dev->ep_in[ep].dev = dev;
 655        dev->ep_out[ep].dev = dev;
 656        dev->ep_in[ep].pipeline = false;
 657        dev->ep_out[ep].pipeline = false;
 658    }
 659}
 660
 661void usb_ep_init(USBDevice *dev)
 662{
 663    int ep;
 664
 665    usb_ep_reset(dev);
 666    QTAILQ_INIT(&dev->ep_ctl.queue);
 667    for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
 668        QTAILQ_INIT(&dev->ep_in[ep].queue);
 669        QTAILQ_INIT(&dev->ep_out[ep].queue);
 670    }
 671}
 672
 673void usb_ep_dump(USBDevice *dev)
 674{
 675    static const char *tname[] = {
 676        [USB_ENDPOINT_XFER_CONTROL] = "control",
 677        [USB_ENDPOINT_XFER_ISOC]    = "isoc",
 678        [USB_ENDPOINT_XFER_BULK]    = "bulk",
 679        [USB_ENDPOINT_XFER_INT]     = "int",
 680    };
 681    int ifnum, ep, first;
 682
 683    fprintf(stderr, "Device \"%s\", config %d\n",
 684            dev->product_desc, dev->configuration);
 685    for (ifnum = 0; ifnum < 16; ifnum++) {
 686        first = 1;
 687        for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
 688            if (dev->ep_in[ep].type != USB_ENDPOINT_XFER_INVALID &&
 689                dev->ep_in[ep].ifnum == ifnum) {
 690                if (first) {
 691                    first = 0;
 692                    fprintf(stderr, "  Interface %d, alternative %d\n",
 693                            ifnum, dev->altsetting[ifnum]);
 694                }
 695                fprintf(stderr, "    Endpoint %d, IN, %s, %d max\n", ep,
 696                        tname[dev->ep_in[ep].type],
 697                        dev->ep_in[ep].max_packet_size);
 698            }
 699            if (dev->ep_out[ep].type != USB_ENDPOINT_XFER_INVALID &&
 700                dev->ep_out[ep].ifnum == ifnum) {
 701                if (first) {
 702                    first = 0;
 703                    fprintf(stderr, "  Interface %d, alternative %d\n",
 704                            ifnum, dev->altsetting[ifnum]);
 705                }
 706                fprintf(stderr, "    Endpoint %d, OUT, %s, %d max\n", ep,
 707                        tname[dev->ep_out[ep].type],
 708                        dev->ep_out[ep].max_packet_size);
 709            }
 710        }
 711    }
 712    fprintf(stderr, "--\n");
 713}
 714
 715struct USBEndpoint *usb_ep_get(USBDevice *dev, int pid, int ep)
 716{
 717    struct USBEndpoint *eps;
 718
 719    assert(dev != NULL);
 720    if (ep == 0) {
 721        return &dev->ep_ctl;
 722    }
 723    assert(pid == USB_TOKEN_IN || pid == USB_TOKEN_OUT);
 724    assert(ep > 0 && ep <= USB_MAX_ENDPOINTS);
 725    eps = (pid == USB_TOKEN_IN) ? dev->ep_in : dev->ep_out;
 726    return eps + ep - 1;
 727}
 728
 729uint8_t usb_ep_get_type(USBDevice *dev, int pid, int ep)
 730{
 731    struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
 732    return uep->type;
 733}
 734
 735void usb_ep_set_type(USBDevice *dev, int pid, int ep, uint8_t type)
 736{
 737    struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
 738    uep->type = type;
 739}
 740
 741void usb_ep_set_ifnum(USBDevice *dev, int pid, int ep, uint8_t ifnum)
 742{
 743    struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
 744    uep->ifnum = ifnum;
 745}
 746
 747void usb_ep_set_max_packet_size(USBDevice *dev, int pid, int ep,
 748                                uint16_t raw)
 749{
 750    struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
 751    int size, microframes;
 752
 753    size = raw & 0x7ff;
 754    switch ((raw >> 11) & 3) {
 755    case 1:
 756        microframes = 2;
 757        break;
 758    case 2:
 759        microframes = 3;
 760        break;
 761    default:
 762        microframes = 1;
 763        break;
 764    }
 765    uep->max_packet_size = size * microframes;
 766}
 767
 768void usb_ep_set_max_streams(USBDevice *dev, int pid, int ep, uint8_t raw)
 769{
 770    struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
 771    int MaxStreams;
 772
 773    MaxStreams = raw & 0x1f;
 774    if (MaxStreams) {
 775        uep->max_streams = 1 << MaxStreams;
 776    } else {
 777        uep->max_streams = 0;
 778    }
 779}
 780
 781void usb_ep_set_halted(USBDevice *dev, int pid, int ep, bool halted)
 782{
 783    struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
 784    uep->halted = halted;
 785}
 786
 787USBPacket *usb_ep_find_packet_by_id(USBDevice *dev, int pid, int ep,
 788                                    uint64_t id)
 789{
 790    struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
 791    USBPacket *p;
 792
 793    QTAILQ_FOREACH(p, &uep->queue, queue) {
 794        if (p->id == id) {
 795            return p;
 796        }
 797    }
 798
 799    return NULL;
 800}
 801