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