desc.c 17.1 KB
Newer Older
G
Gerd Hoffmann 已提交
1 2
#include "hw/usb.h"
#include "hw/usb/desc.h"
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include "trace.h"

/* ------------------------------------------------------------------ */

static uint8_t usb_lo(uint16_t val)
{
    return val & 0xff;
}

static uint8_t usb_hi(uint16_t val)
{
    return (val >> 8) & 0xff;
}

int usb_desc_device(const USBDescID *id, const USBDescDevice *dev,
                    uint8_t *dest, size_t len)
{
    uint8_t bLength = 0x12;
21
    USBDescriptor *d = (void *)dest;
22 23 24 25 26

    if (len < bLength) {
        return -1;
    }

27 28
    d->bLength                     = bLength;
    d->bDescriptorType             = USB_DT_DEVICE;
29

30 31 32 33 34 35
    d->u.device.bcdUSB_lo          = usb_lo(dev->bcdUSB);
    d->u.device.bcdUSB_hi          = usb_hi(dev->bcdUSB);
    d->u.device.bDeviceClass       = dev->bDeviceClass;
    d->u.device.bDeviceSubClass    = dev->bDeviceSubClass;
    d->u.device.bDeviceProtocol    = dev->bDeviceProtocol;
    d->u.device.bMaxPacketSize0    = dev->bMaxPacketSize0;
36

37 38 39 40 41 42 43 44 45
    d->u.device.idVendor_lo        = usb_lo(id->idVendor);
    d->u.device.idVendor_hi        = usb_hi(id->idVendor);
    d->u.device.idProduct_lo       = usb_lo(id->idProduct);
    d->u.device.idProduct_hi       = usb_hi(id->idProduct);
    d->u.device.bcdDevice_lo       = usb_lo(id->bcdDevice);
    d->u.device.bcdDevice_hi       = usb_hi(id->bcdDevice);
    d->u.device.iManufacturer      = id->iManufacturer;
    d->u.device.iProduct           = id->iProduct;
    d->u.device.iSerialNumber      = id->iSerialNumber;
46

47
    d->u.device.bNumConfigurations = dev->bNumConfigurations;
48 49 50 51

    return bLength;
}

52 53 54 55
int usb_desc_device_qualifier(const USBDescDevice *dev,
                              uint8_t *dest, size_t len)
{
    uint8_t bLength = 0x0a;
56
    USBDescriptor *d = (void *)dest;
57 58 59 60 61

    if (len < bLength) {
        return -1;
    }

62 63 64 65 66 67 68 69 70 71 72
    d->bLength                               = bLength;
    d->bDescriptorType                       = USB_DT_DEVICE_QUALIFIER;

    d->u.device_qualifier.bcdUSB_lo          = usb_lo(dev->bcdUSB);
    d->u.device_qualifier.bcdUSB_hi          = usb_hi(dev->bcdUSB);
    d->u.device_qualifier.bDeviceClass       = dev->bDeviceClass;
    d->u.device_qualifier.bDeviceSubClass    = dev->bDeviceSubClass;
    d->u.device_qualifier.bDeviceProtocol    = dev->bDeviceProtocol;
    d->u.device_qualifier.bMaxPacketSize0    = dev->bMaxPacketSize0;
    d->u.device_qualifier.bNumConfigurations = dev->bNumConfigurations;
    d->u.device_qualifier.bReserved          = 0;
73 74 75 76

    return bLength;
}

77 78 79 80
int usb_desc_config(const USBDescConfig *conf, uint8_t *dest, size_t len)
{
    uint8_t  bLength = 0x09;
    uint16_t wTotalLength = 0;
81
    USBDescriptor *d = (void *)dest;
82
    int i, rc;
83 84 85 86 87

    if (len < bLength) {
        return -1;
    }

88 89 90 91 92 93 94 95
    d->bLength                      = bLength;
    d->bDescriptorType              = USB_DT_CONFIG;

    d->u.config.bNumInterfaces      = conf->bNumInterfaces;
    d->u.config.bConfigurationValue = conf->bConfigurationValue;
    d->u.config.iConfiguration      = conf->iConfiguration;
    d->u.config.bmAttributes        = conf->bmAttributes;
    d->u.config.bMaxPower           = conf->bMaxPower;
96 97
    wTotalLength += bLength;

98
    /* handle grouped interfaces if any */
99 100 101 102 103 104 105 106 107 108 109
    for (i = 0; i < conf->nif_groups; i++) {
        rc = usb_desc_iface_group(&(conf->if_groups[i]),
                                  dest + wTotalLength,
                                  len - wTotalLength);
        if (rc < 0) {
            return rc;
        }
        wTotalLength += rc;
    }

    /* handle normal (ungrouped / no IAD) interfaces if any */
110
    for (i = 0; i < conf->nif; i++) {
111 112 113 114 115 116 117
        rc = usb_desc_iface(conf->ifs + i, dest + wTotalLength, len - wTotalLength);
        if (rc < 0) {
            return rc;
        }
        wTotalLength += rc;
    }

118 119
    d->u.config.wTotalLength_lo = usb_lo(wTotalLength);
    d->u.config.wTotalLength_hi = usb_hi(wTotalLength);
120 121 122
    return wTotalLength;
}

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
int usb_desc_iface_group(const USBDescIfaceAssoc *iad, uint8_t *dest,
                         size_t len)
{
    int pos = 0;
    int i = 0;

    /* handle interface association descriptor */
    uint8_t bLength = 0x08;

    if (len < bLength) {
        return -1;
    }

    dest[0x00] = bLength;
    dest[0x01] = USB_DT_INTERFACE_ASSOC;
    dest[0x02] = iad->bFirstInterface;
    dest[0x03] = iad->bInterfaceCount;
    dest[0x04] = iad->bFunctionClass;
    dest[0x05] = iad->bFunctionSubClass;
    dest[0x06] = iad->bFunctionProtocol;
    dest[0x07] = iad->iFunction;
    pos += bLength;

    /* handle associated interfaces in this group */
    for (i = 0; i < iad->nif; i++) {
        int rc = usb_desc_iface(&(iad->ifs[i]), dest + pos, len - pos);
        if (rc < 0) {
            return rc;
        }
        pos += rc;
    }

    return pos;
}

158 159 160 161
int usb_desc_iface(const USBDescIface *iface, uint8_t *dest, size_t len)
{
    uint8_t bLength = 0x09;
    int i, rc, pos = 0;
162
    USBDescriptor *d = (void *)dest;
163 164 165 166 167

    if (len < bLength) {
        return -1;
    }

168 169 170 171 172 173 174 175 176 177
    d->bLength                        = bLength;
    d->bDescriptorType                = USB_DT_INTERFACE;

    d->u.interface.bInterfaceNumber   = iface->bInterfaceNumber;
    d->u.interface.bAlternateSetting  = iface->bAlternateSetting;
    d->u.interface.bNumEndpoints      = iface->bNumEndpoints;
    d->u.interface.bInterfaceClass    = iface->bInterfaceClass;
    d->u.interface.bInterfaceSubClass = iface->bInterfaceSubClass;
    d->u.interface.bInterfaceProtocol = iface->bInterfaceProtocol;
    d->u.interface.iInterface         = iface->iInterface;
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
    pos += bLength;

    for (i = 0; i < iface->ndesc; i++) {
        rc = usb_desc_other(iface->descs + i, dest + pos, len - pos);
        if (rc < 0) {
            return rc;
        }
        pos += rc;
    }

    for (i = 0; i < iface->bNumEndpoints; i++) {
        rc = usb_desc_endpoint(iface->eps + i, dest + pos, len - pos);
        if (rc < 0) {
            return rc;
        }
        pos += rc;
    }

    return pos;
}

int usb_desc_endpoint(const USBDescEndpoint *ep, uint8_t *dest, size_t len)
{
G
Gerd Hoffmann 已提交
201 202
    uint8_t bLength = ep->is_audio ? 0x09 : 0x07;
    uint8_t extralen = ep->extra ? ep->extra[0] : 0;
203
    USBDescriptor *d = (void *)dest;
204

G
Gerd Hoffmann 已提交
205
    if (len < bLength + extralen) {
206 207 208
        return -1;
    }

209 210 211 212 213 214 215 216
    d->bLength                      = bLength;
    d->bDescriptorType              = USB_DT_ENDPOINT;

    d->u.endpoint.bEndpointAddress  = ep->bEndpointAddress;
    d->u.endpoint.bmAttributes      = ep->bmAttributes;
    d->u.endpoint.wMaxPacketSize_lo = usb_lo(ep->wMaxPacketSize);
    d->u.endpoint.wMaxPacketSize_hi = usb_hi(ep->wMaxPacketSize);
    d->u.endpoint.bInterval         = ep->bInterval;
G
Gerd Hoffmann 已提交
217
    if (ep->is_audio) {
218 219
        d->u.endpoint.bRefresh      = ep->bRefresh;
        d->u.endpoint.bSynchAddress = ep->bSynchAddress;
G
Gerd Hoffmann 已提交
220 221 222 223
    }
    if (ep->extra) {
        memcpy(dest + bLength, ep->extra, extralen);
    }
224

G
Gerd Hoffmann 已提交
225
    return bLength + extralen;
226 227 228 229 230 231 232 233 234 235 236 237 238 239
}

int usb_desc_other(const USBDescOther *desc, uint8_t *dest, size_t len)
{
    int bLength = desc->length ? desc->length : desc->data[0];

    if (len < bLength) {
        return -1;
    }

    memcpy(dest, desc->data, bLength);
    return bLength;
}

240 241
/* ------------------------------------------------------------------ */

G
Gerd Hoffmann 已提交
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
static void usb_desc_ep_init(USBDevice *dev)
{
    const USBDescIface *iface;
    int i, e, pid, ep;

    usb_ep_init(dev);
    for (i = 0; i < dev->ninterfaces; i++) {
        iface = dev->ifaces[i];
        if (iface == NULL) {
            continue;
        }
        for (e = 0; e < iface->bNumEndpoints; e++) {
            pid = (iface->eps[e].bEndpointAddress & USB_DIR_IN) ?
                USB_TOKEN_IN : USB_TOKEN_OUT;
            ep = iface->eps[e].bEndpointAddress & 0x0f;
            usb_ep_set_type(dev, pid, ep, iface->eps[e].bmAttributes & 0x03);
            usb_ep_set_ifnum(dev, pid, ep, iface->bInterfaceNumber);
259 260
            usb_ep_set_max_packet_size(dev, pid, ep,
                                       iface->eps[e].wMaxPacketSize);
G
Gerd Hoffmann 已提交
261 262 263 264
        }
    }
}

265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
static const USBDescIface *usb_desc_find_interface(USBDevice *dev,
                                                   int nif, int alt)
{
    const USBDescIface *iface;
    int g, i;

    if (!dev->config) {
        return NULL;
    }
    for (g = 0; g < dev->config->nif_groups; g++) {
        for (i = 0; i < dev->config->if_groups[g].nif; i++) {
            iface = &dev->config->if_groups[g].ifs[i];
            if (iface->bInterfaceNumber == nif &&
                iface->bAlternateSetting == alt) {
                return iface;
            }
        }
    }
    for (i = 0; i < dev->config->nif; i++) {
        iface = &dev->config->ifs[i];
        if (iface->bInterfaceNumber == nif &&
            iface->bAlternateSetting == alt) {
            return iface;
        }
    }
    return NULL;
}

static int usb_desc_set_interface(USBDevice *dev, int index, int value)
{
    const USBDescIface *iface;
    int old;

    iface = usb_desc_find_interface(dev, index, value);
    if (iface == NULL) {
        return -1;
    }

    old = dev->altsetting[index];
    dev->altsetting[index] = value;
    dev->ifaces[index] = iface;
G
Gerd Hoffmann 已提交
306
    usb_desc_ep_init(dev);
307

308 309
    if (old != value) {
        usb_device_set_interface(dev, index, old, value);
310 311 312 313
    }
    return 0;
}

314 315 316 317 318 319 320 321 322 323 324 325 326 327
static int usb_desc_set_config(USBDevice *dev, int value)
{
    int i;

    if (value == 0) {
        dev->configuration = 0;
        dev->ninterfaces   = 0;
        dev->config = NULL;
    } else {
        for (i = 0; i < dev->device->bNumConfigurations; i++) {
            if (dev->device->confs[i].bConfigurationValue == value) {
                dev->configuration = value;
                dev->ninterfaces   = dev->device->confs[i].bNumInterfaces;
                dev->config = dev->device->confs + i;
328
                assert(dev->ninterfaces <= USB_MAX_INTERFACES);
329 330 331 332 333 334
            }
        }
        if (i < dev->device->bNumConfigurations) {
            return -1;
        }
    }
335 336 337 338 339 340 341 342 343

    for (i = 0; i < dev->ninterfaces; i++) {
        usb_desc_set_interface(dev, i, 0);
    }
    for (; i < USB_MAX_INTERFACES; i++) {
        dev->altsetting[i] = 0;
        dev->ifaces[i] = NULL;
    }

344 345 346
    return 0;
}

G
Gerd Hoffmann 已提交
347
static void usb_desc_setdefaults(USBDevice *dev)
348
{
349
    const USBDesc *desc = usb_device_get_usb_desc(dev);
350 351

    assert(desc != NULL);
G
Gerd Hoffmann 已提交
352 353 354 355 356 357 358 359 360
    switch (dev->speed) {
    case USB_SPEED_LOW:
    case USB_SPEED_FULL:
        dev->device = desc->full;
        break;
    case USB_SPEED_HIGH:
        dev->device = desc->high;
        break;
    }
361
    usb_desc_set_config(dev, 0);
362 363
}

G
Gerd Hoffmann 已提交
364 365
void usb_desc_init(USBDevice *dev)
{
366
    const USBDesc *desc = usb_device_get_usb_desc(dev);
H
Hans de Goede 已提交
367 368

    assert(desc != NULL);
G
Gerd Hoffmann 已提交
369
    dev->speed = USB_SPEED_FULL;
H
Hans de Goede 已提交
370 371 372 373 374 375 376
    dev->speedmask = 0;
    if (desc->full) {
        dev->speedmask |= USB_SPEED_MASK_FULL;
    }
    if (desc->high) {
        dev->speedmask |= USB_SPEED_MASK_HIGH;
    }
G
Gerd Hoffmann 已提交
377 378 379 380 381
    usb_desc_setdefaults(dev);
}

void usb_desc_attach(USBDevice *dev)
{
382
    const USBDesc *desc = usb_device_get_usb_desc(dev);
G
Gerd Hoffmann 已提交
383 384 385 386 387 388 389 390

    assert(desc != NULL);
    if (desc->high && (dev->port->speedmask & USB_SPEED_MASK_HIGH)) {
        dev->speed = USB_SPEED_HIGH;
    } else if (desc->full && (dev->port->speedmask & USB_SPEED_MASK_FULL)) {
        dev->speed = USB_SPEED_FULL;
    } else {
        fprintf(stderr, "usb: port/device speed mismatch for \"%s\"\n",
391
                usb_device_get_product_desc(dev));
G
Gerd Hoffmann 已提交
392 393 394 395 396
        return;
    }
    usb_desc_setdefaults(dev);
}

397 398 399 400 401 402 403 404 405 406
void usb_desc_set_string(USBDevice *dev, uint8_t index, const char *str)
{
    USBDescString *s;

    QLIST_FOREACH(s, &dev->strings, next) {
        if (s->index == index) {
            break;
        }
    }
    if (s == NULL) {
407
        s = g_malloc0(sizeof(*s));
408 409 410
        s->index = index;
        QLIST_INSERT_HEAD(&dev->strings, s, next);
    }
411 412
    g_free(s->str);
    s->str = g_strdup(str);
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
}

const char *usb_desc_get_string(USBDevice *dev, uint8_t index)
{
    USBDescString *s;

    QLIST_FOREACH(s, &dev->strings, next) {
        if (s->index == index) {
            return s->str;
        }
    }
    return NULL;
}

int usb_desc_string(USBDevice *dev, int index, uint8_t *dest, size_t len)
428 429
{
    uint8_t bLength, pos, i;
430
    const char *str;
431 432 433 434 435 436 437 438 439 440 441 442 443 444

    if (len < 4) {
        return -1;
    }

    if (index == 0) {
        /* language ids */
        dest[0] = 4;
        dest[1] = USB_DT_STRING;
        dest[2] = 0x09;
        dest[3] = 0x04;
        return 4;
    }

445 446
    str = usb_desc_get_string(dev, index);
    if (str == NULL) {
447
        str = usb_device_get_usb_desc(dev)->str[index];
448 449 450
        if (str == NULL) {
            return 0;
        }
451
    }
452 453

    bLength = strlen(str) * 2 + 2;
454 455 456 457
    dest[0] = bLength;
    dest[1] = USB_DT_STRING;
    i = 0; pos = 2;
    while (pos+1 < bLength && pos+1 < len) {
458
        dest[pos++] = str[i++];
459 460 461 462 463 464 465
        dest[pos++] = 0;
    }
    return pos;
}

int usb_desc_get_descriptor(USBDevice *dev, int value, uint8_t *dest, size_t len)
{
466
    const USBDesc *desc = usb_device_get_usb_desc(dev);
467
    const USBDescDevice *other_dev;
468 469 470 471 472
    uint8_t buf[256];
    uint8_t type = value >> 8;
    uint8_t index = value & 0xff;
    int ret = -1;

473
    if (dev->speed == USB_SPEED_HIGH) {
474
        other_dev = usb_device_get_usb_desc(dev)->full;
475
    } else {
476
        other_dev = usb_device_get_usb_desc(dev)->high;
477 478
    }

479 480
    switch(type) {
    case USB_DT_DEVICE:
481
        ret = usb_desc_device(&desc->id, dev->device, buf, sizeof(buf));
482 483 484
        trace_usb_desc_device(dev->addr, len, ret);
        break;
    case USB_DT_CONFIG:
485 486
        if (index < dev->device->bNumConfigurations) {
            ret = usb_desc_config(dev->device->confs + index, buf, sizeof(buf));
487 488 489 490
        }
        trace_usb_desc_config(dev->addr, index, len, ret);
        break;
    case USB_DT_STRING:
491
        ret = usb_desc_string(dev, index, buf, sizeof(buf));
492 493
        trace_usb_desc_string(dev->addr, index, len, ret);
        break;
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508

    case USB_DT_DEVICE_QUALIFIER:
        if (other_dev != NULL) {
            ret = usb_desc_device_qualifier(other_dev, buf, sizeof(buf));
        }
        trace_usb_desc_device_qualifier(dev->addr, len, ret);
        break;
    case USB_DT_OTHER_SPEED_CONFIG:
        if (other_dev != NULL && index < other_dev->bNumConfigurations) {
            ret = usb_desc_config(other_dev->confs + index, buf, sizeof(buf));
            buf[0x01] = USB_DT_OTHER_SPEED_CONFIG;
        }
        trace_usb_desc_other_speed_config(dev->addr, index, len, ret);
        break;

G
Gerd Hoffmann 已提交
509 510 511 512
    case USB_DT_DEBUG:
        /* ignore silently */
        break;

513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
    default:
        fprintf(stderr, "%s: %d unknown type %d (len %zd)\n", __FUNCTION__,
                dev->addr, type, len);
        break;
    }

    if (ret > 0) {
        if (ret > len) {
            ret = len;
        }
        memcpy(dest, buf, ret);
    }
    return ret;
}

528 529
int usb_desc_handle_control(USBDevice *dev, USBPacket *p,
        int request, int value, int index, int length, uint8_t *data)
530
{
531
    const USBDesc *desc = usb_device_get_usb_desc(dev);
532
    int ret = -1;
533 534 535

    assert(desc != NULL);
    switch(request) {
536 537 538 539 540 541
    case DeviceOutRequest | USB_REQ_SET_ADDRESS:
        dev->addr = value;
        trace_usb_set_addr(dev->addr);
        ret = 0;
        break;

542 543 544
    case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
        ret = usb_desc_get_descriptor(dev, value, data, length);
        break;
545 546

    case DeviceRequest | USB_REQ_GET_CONFIGURATION:
547 548 549 550 551
        /*
         * 9.4.2: 0 should be returned if the device is unconfigured, otherwise
         * the non zero value of bConfigurationValue.
         */
        data[0] = dev->config ? dev->config->bConfigurationValue : 0;
552 553 554
        ret = 1;
        break;
    case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
555
        ret = usb_desc_set_config(dev, value);
556 557
        trace_usb_set_config(dev->addr, value, ret);
        break;
558

559 560 561 562
    case DeviceRequest | USB_REQ_GET_STATUS: {
        const USBDescConfig *config = dev->config ?
            dev->config : &dev->device->confs[0];

563
        data[0] = 0;
564 565 566 567 568 569 570
        /*
         * Default state: Device behavior when this request is received while
         *                the device is in the Default state is not specified.
         * We return the same value that a configured device would return if
         * it used the first configuration.
         */
        if (config->bmAttributes & 0x40) {
571 572 573 574 575 576 577 578
            data[0] |= 1 << USB_DEVICE_SELF_POWERED;
        }
        if (dev->remote_wakeup) {
            data[0] |= 1 << USB_DEVICE_REMOTE_WAKEUP;
        }
        data[1] = 0x00;
        ret = 2;
        break;
579
    }
580 581 582 583 584 585 586 587 588 589 590 591 592 593
    case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
        if (value == USB_DEVICE_REMOTE_WAKEUP) {
            dev->remote_wakeup = 0;
            ret = 0;
        }
        trace_usb_clear_device_feature(dev->addr, value, ret);
        break;
    case DeviceOutRequest | USB_REQ_SET_FEATURE:
        if (value == USB_DEVICE_REMOTE_WAKEUP) {
            dev->remote_wakeup = 1;
            ret = 0;
        }
        trace_usb_set_device_feature(dev->addr, value, ret);
        break;
594 595 596 597 598 599 600 601 602 603 604 605 606

    case InterfaceRequest | USB_REQ_GET_INTERFACE:
        if (index < 0 || index >= dev->ninterfaces) {
            break;
        }
        data[0] = dev->altsetting[index];
        ret = 1;
        break;
    case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
        ret = usb_desc_set_interface(dev, index, value);
        trace_usb_set_interface(dev->addr, index, value, ret);
        break;

607 608 609
    }
    return ret;
}