1 #include <ctype.h> 2 3 #include "hw/usb.h" 4 #include "hw/usb/desc.h" 5 #include "trace.h" 6 7 /* ------------------------------------------------------------------ */ 8 9 static uint8_t usb_lo(uint16_t val) 10 { 11 return val & 0xff; 12 } 13 14 static uint8_t usb_hi(uint16_t val) 15 { 16 return (val >> 8) & 0xff; 17 } 18 19 int usb_desc_device(const USBDescID *id, const USBDescDevice *dev, 20 uint8_t *dest, size_t len) 21 { 22 uint8_t bLength = 0x12; 23 USBDescriptor *d = (void *)dest; 24 25 if (len < bLength) { 26 return -1; 27 } 28 29 d->bLength = bLength; 30 d->bDescriptorType = USB_DT_DEVICE; 31 32 d->u.device.bcdUSB_lo = usb_lo(dev->bcdUSB); 33 d->u.device.bcdUSB_hi = usb_hi(dev->bcdUSB); 34 d->u.device.bDeviceClass = dev->bDeviceClass; 35 d->u.device.bDeviceSubClass = dev->bDeviceSubClass; 36 d->u.device.bDeviceProtocol = dev->bDeviceProtocol; 37 d->u.device.bMaxPacketSize0 = dev->bMaxPacketSize0; 38 39 d->u.device.idVendor_lo = usb_lo(id->idVendor); 40 d->u.device.idVendor_hi = usb_hi(id->idVendor); 41 d->u.device.idProduct_lo = usb_lo(id->idProduct); 42 d->u.device.idProduct_hi = usb_hi(id->idProduct); 43 d->u.device.bcdDevice_lo = usb_lo(id->bcdDevice); 44 d->u.device.bcdDevice_hi = usb_hi(id->bcdDevice); 45 d->u.device.iManufacturer = id->iManufacturer; 46 d->u.device.iProduct = id->iProduct; 47 d->u.device.iSerialNumber = id->iSerialNumber; 48 49 d->u.device.bNumConfigurations = dev->bNumConfigurations; 50 51 return bLength; 52 } 53 54 int usb_desc_device_qualifier(const USBDescDevice *dev, 55 uint8_t *dest, size_t len) 56 { 57 uint8_t bLength = 0x0a; 58 USBDescriptor *d = (void *)dest; 59 60 if (len < bLength) { 61 return -1; 62 } 63 64 d->bLength = bLength; 65 d->bDescriptorType = USB_DT_DEVICE_QUALIFIER; 66 67 d->u.device_qualifier.bcdUSB_lo = usb_lo(dev->bcdUSB); 68 d->u.device_qualifier.bcdUSB_hi = usb_hi(dev->bcdUSB); 69 d->u.device_qualifier.bDeviceClass = dev->bDeviceClass; 70 d->u.device_qualifier.bDeviceSubClass = dev->bDeviceSubClass; 71 d->u.device_qualifier.bDeviceProtocol = dev->bDeviceProtocol; 72 d->u.device_qualifier.bMaxPacketSize0 = dev->bMaxPacketSize0; 73 d->u.device_qualifier.bNumConfigurations = dev->bNumConfigurations; 74 d->u.device_qualifier.bReserved = 0; 75 76 return bLength; 77 } 78 79 int usb_desc_config(const USBDescConfig *conf, uint8_t *dest, size_t len) 80 { 81 uint8_t bLength = 0x09; 82 uint16_t wTotalLength = 0; 83 USBDescriptor *d = (void *)dest; 84 int i, rc; 85 86 if (len < bLength) { 87 return -1; 88 } 89 90 d->bLength = bLength; 91 d->bDescriptorType = USB_DT_CONFIG; 92 93 d->u.config.bNumInterfaces = conf->bNumInterfaces; 94 d->u.config.bConfigurationValue = conf->bConfigurationValue; 95 d->u.config.iConfiguration = conf->iConfiguration; 96 d->u.config.bmAttributes = conf->bmAttributes; 97 d->u.config.bMaxPower = conf->bMaxPower; 98 wTotalLength += bLength; 99 100 /* handle grouped interfaces if any */ 101 for (i = 0; i < conf->nif_groups; i++) { 102 rc = usb_desc_iface_group(&(conf->if_groups[i]), 103 dest + wTotalLength, 104 len - wTotalLength); 105 if (rc < 0) { 106 return rc; 107 } 108 wTotalLength += rc; 109 } 110 111 /* handle normal (ungrouped / no IAD) interfaces if any */ 112 for (i = 0; i < conf->nif; i++) { 113 rc = usb_desc_iface(conf->ifs + i, dest + wTotalLength, len - wTotalLength); 114 if (rc < 0) { 115 return rc; 116 } 117 wTotalLength += rc; 118 } 119 120 d->u.config.wTotalLength_lo = usb_lo(wTotalLength); 121 d->u.config.wTotalLength_hi = usb_hi(wTotalLength); 122 return wTotalLength; 123 } 124 125 int usb_desc_iface_group(const USBDescIfaceAssoc *iad, uint8_t *dest, 126 size_t len) 127 { 128 int pos = 0; 129 int i = 0; 130 131 /* handle interface association descriptor */ 132 uint8_t bLength = 0x08; 133 134 if (len < bLength) { 135 return -1; 136 } 137 138 dest[0x00] = bLength; 139 dest[0x01] = USB_DT_INTERFACE_ASSOC; 140 dest[0x02] = iad->bFirstInterface; 141 dest[0x03] = iad->bInterfaceCount; 142 dest[0x04] = iad->bFunctionClass; 143 dest[0x05] = iad->bFunctionSubClass; 144 dest[0x06] = iad->bFunctionProtocol; 145 dest[0x07] = iad->iFunction; 146 pos += bLength; 147 148 /* handle associated interfaces in this group */ 149 for (i = 0; i < iad->nif; i++) { 150 int rc = usb_desc_iface(&(iad->ifs[i]), dest + pos, len - pos); 151 if (rc < 0) { 152 return rc; 153 } 154 pos += rc; 155 } 156 157 return pos; 158 } 159 160 int usb_desc_iface(const USBDescIface *iface, uint8_t *dest, size_t len) 161 { 162 uint8_t bLength = 0x09; 163 int i, rc, pos = 0; 164 USBDescriptor *d = (void *)dest; 165 166 if (len < bLength) { 167 return -1; 168 } 169 170 d->bLength = bLength; 171 d->bDescriptorType = USB_DT_INTERFACE; 172 173 d->u.interface.bInterfaceNumber = iface->bInterfaceNumber; 174 d->u.interface.bAlternateSetting = iface->bAlternateSetting; 175 d->u.interface.bNumEndpoints = iface->bNumEndpoints; 176 d->u.interface.bInterfaceClass = iface->bInterfaceClass; 177 d->u.interface.bInterfaceSubClass = iface->bInterfaceSubClass; 178 d->u.interface.bInterfaceProtocol = iface->bInterfaceProtocol; 179 d->u.interface.iInterface = iface->iInterface; 180 pos += bLength; 181 182 for (i = 0; i < iface->ndesc; i++) { 183 rc = usb_desc_other(iface->descs + i, dest + pos, len - pos); 184 if (rc < 0) { 185 return rc; 186 } 187 pos += rc; 188 } 189 190 for (i = 0; i < iface->bNumEndpoints; i++) { 191 rc = usb_desc_endpoint(iface->eps + i, dest + pos, len - pos); 192 if (rc < 0) { 193 return rc; 194 } 195 pos += rc; 196 } 197 198 return pos; 199 } 200 201 int usb_desc_endpoint(const USBDescEndpoint *ep, uint8_t *dest, size_t len) 202 { 203 uint8_t bLength = ep->is_audio ? 0x09 : 0x07; 204 uint8_t extralen = ep->extra ? ep->extra[0] : 0; 205 USBDescriptor *d = (void *)dest; 206 207 if (len < bLength + extralen) { 208 return -1; 209 } 210 211 d->bLength = bLength; 212 d->bDescriptorType = USB_DT_ENDPOINT; 213 214 d->u.endpoint.bEndpointAddress = ep->bEndpointAddress; 215 d->u.endpoint.bmAttributes = ep->bmAttributes; 216 d->u.endpoint.wMaxPacketSize_lo = usb_lo(ep->wMaxPacketSize); 217 d->u.endpoint.wMaxPacketSize_hi = usb_hi(ep->wMaxPacketSize); 218 d->u.endpoint.bInterval = ep->bInterval; 219 if (ep->is_audio) { 220 d->u.endpoint.bRefresh = ep->bRefresh; 221 d->u.endpoint.bSynchAddress = ep->bSynchAddress; 222 } 223 if (ep->extra) { 224 memcpy(dest + bLength, ep->extra, extralen); 225 } 226 227 return bLength + extralen; 228 } 229 230 int usb_desc_other(const USBDescOther *desc, uint8_t *dest, size_t len) 231 { 232 int bLength = desc->length ? desc->length : desc->data[0]; 233 234 if (len < bLength) { 235 return -1; 236 } 237 238 memcpy(dest, desc->data, bLength); 239 return bLength; 240 } 241 242 /* ------------------------------------------------------------------ */ 243 244 static void usb_desc_ep_init(USBDevice *dev) 245 { 246 const USBDescIface *iface; 247 int i, e, pid, ep; 248 249 usb_ep_init(dev); 250 for (i = 0; i < dev->ninterfaces; i++) { 251 iface = dev->ifaces[i]; 252 if (iface == NULL) { 253 continue; 254 } 255 for (e = 0; e < iface->bNumEndpoints; e++) { 256 pid = (iface->eps[e].bEndpointAddress & USB_DIR_IN) ? 257 USB_TOKEN_IN : USB_TOKEN_OUT; 258 ep = iface->eps[e].bEndpointAddress & 0x0f; 259 usb_ep_set_type(dev, pid, ep, iface->eps[e].bmAttributes & 0x03); 260 usb_ep_set_ifnum(dev, pid, ep, iface->bInterfaceNumber); 261 usb_ep_set_max_packet_size(dev, pid, ep, 262 iface->eps[e].wMaxPacketSize); 263 } 264 } 265 } 266 267 static const USBDescIface *usb_desc_find_interface(USBDevice *dev, 268 int nif, int alt) 269 { 270 const USBDescIface *iface; 271 int g, i; 272 273 if (!dev->config) { 274 return NULL; 275 } 276 for (g = 0; g < dev->config->nif_groups; g++) { 277 for (i = 0; i < dev->config->if_groups[g].nif; i++) { 278 iface = &dev->config->if_groups[g].ifs[i]; 279 if (iface->bInterfaceNumber == nif && 280 iface->bAlternateSetting == alt) { 281 return iface; 282 } 283 } 284 } 285 for (i = 0; i < dev->config->nif; i++) { 286 iface = &dev->config->ifs[i]; 287 if (iface->bInterfaceNumber == nif && 288 iface->bAlternateSetting == alt) { 289 return iface; 290 } 291 } 292 return NULL; 293 } 294 295 static int usb_desc_set_interface(USBDevice *dev, int index, int value) 296 { 297 const USBDescIface *iface; 298 int old; 299 300 iface = usb_desc_find_interface(dev, index, value); 301 if (iface == NULL) { 302 return -1; 303 } 304 305 old = dev->altsetting[index]; 306 dev->altsetting[index] = value; 307 dev->ifaces[index] = iface; 308 usb_desc_ep_init(dev); 309 310 if (old != value) { 311 usb_device_set_interface(dev, index, old, value); 312 } 313 return 0; 314 } 315 316 static int usb_desc_set_config(USBDevice *dev, int value) 317 { 318 int i; 319 320 if (value == 0) { 321 dev->configuration = 0; 322 dev->ninterfaces = 0; 323 dev->config = NULL; 324 } else { 325 for (i = 0; i < dev->device->bNumConfigurations; i++) { 326 if (dev->device->confs[i].bConfigurationValue == value) { 327 dev->configuration = value; 328 dev->ninterfaces = dev->device->confs[i].bNumInterfaces; 329 dev->config = dev->device->confs + i; 330 assert(dev->ninterfaces <= USB_MAX_INTERFACES); 331 } 332 } 333 if (i < dev->device->bNumConfigurations) { 334 return -1; 335 } 336 } 337 338 for (i = 0; i < dev->ninterfaces; i++) { 339 usb_desc_set_interface(dev, i, 0); 340 } 341 for (; i < USB_MAX_INTERFACES; i++) { 342 dev->altsetting[i] = 0; 343 dev->ifaces[i] = NULL; 344 } 345 346 return 0; 347 } 348 349 static void usb_desc_setdefaults(USBDevice *dev) 350 { 351 const USBDesc *desc = usb_device_get_usb_desc(dev); 352 353 assert(desc != NULL); 354 switch (dev->speed) { 355 case USB_SPEED_LOW: 356 case USB_SPEED_FULL: 357 dev->device = desc->full; 358 break; 359 case USB_SPEED_HIGH: 360 dev->device = desc->high; 361 break; 362 } 363 usb_desc_set_config(dev, 0); 364 } 365 366 void usb_desc_init(USBDevice *dev) 367 { 368 const USBDesc *desc = usb_device_get_usb_desc(dev); 369 370 assert(desc != NULL); 371 dev->speed = USB_SPEED_FULL; 372 dev->speedmask = 0; 373 if (desc->full) { 374 dev->speedmask |= USB_SPEED_MASK_FULL; 375 } 376 if (desc->high) { 377 dev->speedmask |= USB_SPEED_MASK_HIGH; 378 } 379 usb_desc_setdefaults(dev); 380 } 381 382 void usb_desc_attach(USBDevice *dev) 383 { 384 const USBDesc *desc = usb_device_get_usb_desc(dev); 385 386 assert(desc != NULL); 387 if (desc->high && (dev->port->speedmask & USB_SPEED_MASK_HIGH)) { 388 dev->speed = USB_SPEED_HIGH; 389 } else if (desc->full && (dev->port->speedmask & USB_SPEED_MASK_FULL)) { 390 dev->speed = USB_SPEED_FULL; 391 } else { 392 fprintf(stderr, "usb: port/device speed mismatch for \"%s\"\n", 393 usb_device_get_product_desc(dev)); 394 return; 395 } 396 usb_desc_setdefaults(dev); 397 } 398 399 void usb_desc_set_string(USBDevice *dev, uint8_t index, const char *str) 400 { 401 USBDescString *s; 402 403 QLIST_FOREACH(s, &dev->strings, next) { 404 if (s->index == index) { 405 break; 406 } 407 } 408 if (s == NULL) { 409 s = g_malloc0(sizeof(*s)); 410 s->index = index; 411 QLIST_INSERT_HEAD(&dev->strings, s, next); 412 } 413 g_free(s->str); 414 s->str = g_strdup(str); 415 } 416 417 /* 418 * This function creates a serial number for a usb device. 419 * The serial number should: 420 * (a) Be unique within the virtual machine. 421 * (b) Be constant, so you don't get a new one each 422 * time the guest is started. 423 * So we are using the physical location to generate a serial number 424 * from it. It has three pieces: First a fixed, device-specific 425 * prefix. Second the device path of the host controller (which is 426 * the pci address in most cases). Third the physical port path. 427 * Results in serial numbers like this: "314159-0000:00:1d.7-3". 428 */ 429 void usb_desc_create_serial(USBDevice *dev) 430 { 431 DeviceState *hcd = dev->qdev.parent_bus->parent; 432 const USBDesc *desc = usb_device_get_usb_desc(dev); 433 int index = desc->id.iSerialNumber; 434 char serial[64]; 435 char *path; 436 int dst; 437 438 assert(index != 0 && desc->str[index] != NULL); 439 dst = snprintf(serial, sizeof(serial), "%s", desc->str[index]); 440 path = qdev_get_dev_path(hcd); 441 if (path) { 442 dst += snprintf(serial+dst, sizeof(serial)-dst, "-%s", path); 443 } 444 dst += snprintf(serial+dst, sizeof(serial)-dst, "-%s", dev->port->path); 445 usb_desc_set_string(dev, index, serial); 446 } 447 448 const char *usb_desc_get_string(USBDevice *dev, uint8_t index) 449 { 450 USBDescString *s; 451 452 QLIST_FOREACH(s, &dev->strings, next) { 453 if (s->index == index) { 454 return s->str; 455 } 456 } 457 return NULL; 458 } 459 460 int usb_desc_string(USBDevice *dev, int index, uint8_t *dest, size_t len) 461 { 462 uint8_t bLength, pos, i; 463 const char *str; 464 465 if (len < 4) { 466 return -1; 467 } 468 469 if (index == 0) { 470 /* language ids */ 471 dest[0] = 4; 472 dest[1] = USB_DT_STRING; 473 dest[2] = 0x09; 474 dest[3] = 0x04; 475 return 4; 476 } 477 478 str = usb_desc_get_string(dev, index); 479 if (str == NULL) { 480 str = usb_device_get_usb_desc(dev)->str[index]; 481 if (str == NULL) { 482 return 0; 483 } 484 } 485 486 bLength = strlen(str) * 2 + 2; 487 dest[0] = bLength; 488 dest[1] = USB_DT_STRING; 489 i = 0; pos = 2; 490 while (pos+1 < bLength && pos+1 < len) { 491 dest[pos++] = str[i++]; 492 dest[pos++] = 0; 493 } 494 return pos; 495 } 496 497 int usb_desc_get_descriptor(USBDevice *dev, int value, uint8_t *dest, size_t len) 498 { 499 const USBDesc *desc = usb_device_get_usb_desc(dev); 500 const USBDescDevice *other_dev; 501 uint8_t buf[256]; 502 uint8_t type = value >> 8; 503 uint8_t index = value & 0xff; 504 int ret = -1; 505 506 if (dev->speed == USB_SPEED_HIGH) { 507 other_dev = usb_device_get_usb_desc(dev)->full; 508 } else { 509 other_dev = usb_device_get_usb_desc(dev)->high; 510 } 511 512 switch(type) { 513 case USB_DT_DEVICE: 514 ret = usb_desc_device(&desc->id, dev->device, buf, sizeof(buf)); 515 trace_usb_desc_device(dev->addr, len, ret); 516 break; 517 case USB_DT_CONFIG: 518 if (index < dev->device->bNumConfigurations) { 519 ret = usb_desc_config(dev->device->confs + index, buf, sizeof(buf)); 520 } 521 trace_usb_desc_config(dev->addr, index, len, ret); 522 break; 523 case USB_DT_STRING: 524 ret = usb_desc_string(dev, index, buf, sizeof(buf)); 525 trace_usb_desc_string(dev->addr, index, len, ret); 526 break; 527 528 case USB_DT_DEVICE_QUALIFIER: 529 if (other_dev != NULL) { 530 ret = usb_desc_device_qualifier(other_dev, buf, sizeof(buf)); 531 } 532 trace_usb_desc_device_qualifier(dev->addr, len, ret); 533 break; 534 case USB_DT_OTHER_SPEED_CONFIG: 535 if (other_dev != NULL && index < other_dev->bNumConfigurations) { 536 ret = usb_desc_config(other_dev->confs + index, buf, sizeof(buf)); 537 buf[0x01] = USB_DT_OTHER_SPEED_CONFIG; 538 } 539 trace_usb_desc_other_speed_config(dev->addr, index, len, ret); 540 break; 541 542 case USB_DT_DEBUG: 543 /* ignore silently */ 544 break; 545 546 default: 547 fprintf(stderr, "%s: %d unknown type %d (len %zd)\n", __FUNCTION__, 548 dev->addr, type, len); 549 break; 550 } 551 552 if (ret > 0) { 553 if (ret > len) { 554 ret = len; 555 } 556 memcpy(dest, buf, ret); 557 } 558 return ret; 559 } 560 561 int usb_desc_handle_control(USBDevice *dev, USBPacket *p, 562 int request, int value, int index, int length, uint8_t *data) 563 { 564 const USBDesc *desc = usb_device_get_usb_desc(dev); 565 int ret = -1; 566 567 assert(desc != NULL); 568 switch(request) { 569 case DeviceOutRequest | USB_REQ_SET_ADDRESS: 570 dev->addr = value; 571 trace_usb_set_addr(dev->addr); 572 ret = 0; 573 break; 574 575 case DeviceRequest | USB_REQ_GET_DESCRIPTOR: 576 ret = usb_desc_get_descriptor(dev, value, data, length); 577 break; 578 579 case DeviceRequest | USB_REQ_GET_CONFIGURATION: 580 /* 581 * 9.4.2: 0 should be returned if the device is unconfigured, otherwise 582 * the non zero value of bConfigurationValue. 583 */ 584 data[0] = dev->config ? dev->config->bConfigurationValue : 0; 585 ret = 1; 586 break; 587 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION: 588 ret = usb_desc_set_config(dev, value); 589 trace_usb_set_config(dev->addr, value, ret); 590 break; 591 592 case DeviceRequest | USB_REQ_GET_STATUS: { 593 const USBDescConfig *config = dev->config ? 594 dev->config : &dev->device->confs[0]; 595 596 data[0] = 0; 597 /* 598 * Default state: Device behavior when this request is received while 599 * the device is in the Default state is not specified. 600 * We return the same value that a configured device would return if 601 * it used the first configuration. 602 */ 603 if (config->bmAttributes & 0x40) { 604 data[0] |= 1 << USB_DEVICE_SELF_POWERED; 605 } 606 if (dev->remote_wakeup) { 607 data[0] |= 1 << USB_DEVICE_REMOTE_WAKEUP; 608 } 609 data[1] = 0x00; 610 ret = 2; 611 break; 612 } 613 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE: 614 if (value == USB_DEVICE_REMOTE_WAKEUP) { 615 dev->remote_wakeup = 0; 616 ret = 0; 617 } 618 trace_usb_clear_device_feature(dev->addr, value, ret); 619 break; 620 case DeviceOutRequest | USB_REQ_SET_FEATURE: 621 if (value == USB_DEVICE_REMOTE_WAKEUP) { 622 dev->remote_wakeup = 1; 623 ret = 0; 624 } 625 trace_usb_set_device_feature(dev->addr, value, ret); 626 break; 627 628 case InterfaceRequest | USB_REQ_GET_INTERFACE: 629 if (index < 0 || index >= dev->ninterfaces) { 630 break; 631 } 632 data[0] = dev->altsetting[index]; 633 ret = 1; 634 break; 635 case InterfaceOutRequest | USB_REQ_SET_INTERFACE: 636 ret = usb_desc_set_interface(dev, index, value); 637 trace_usb_set_interface(dev->addr, index, value, ret); 638 break; 639 640 } 641 return ret; 642 } 643