usb: use USBDescriptor for device qualifier descriptors.
[qemu-kvm.git] / hw / usb / desc.c
blob5cecb25f2f51917fb932d99ba74b279bc872c17e
1 #include "hw/usb.h"
2 #include "hw/usb/desc.h"
3 #include "trace.h"
5 /* ------------------------------------------------------------------ */
7 static uint8_t usb_lo(uint16_t val)
9 return val & 0xff;
12 static uint8_t usb_hi(uint16_t val)
14 return (val >> 8) & 0xff;
17 int usb_desc_device(const USBDescID *id, const USBDescDevice *dev,
18 uint8_t *dest, size_t len)
20 uint8_t bLength = 0x12;
21 USBDescriptor *d = (void *)dest;
23 if (len < bLength) {
24 return -1;
27 d->bLength = bLength;
28 d->bDescriptorType = USB_DT_DEVICE;
30 d->u.device.bcdUSB_lo = usb_lo(dev->bcdUSB);
31 d->u.device.bcdUSB_hi = usb_hi(dev->bcdUSB);
32 d->u.device.bDeviceClass = dev->bDeviceClass;
33 d->u.device.bDeviceSubClass = dev->bDeviceSubClass;
34 d->u.device.bDeviceProtocol = dev->bDeviceProtocol;
35 d->u.device.bMaxPacketSize0 = dev->bMaxPacketSize0;
37 d->u.device.idVendor_lo = usb_lo(id->idVendor);
38 d->u.device.idVendor_hi = usb_hi(id->idVendor);
39 d->u.device.idProduct_lo = usb_lo(id->idProduct);
40 d->u.device.idProduct_hi = usb_hi(id->idProduct);
41 d->u.device.bcdDevice_lo = usb_lo(id->bcdDevice);
42 d->u.device.bcdDevice_hi = usb_hi(id->bcdDevice);
43 d->u.device.iManufacturer = id->iManufacturer;
44 d->u.device.iProduct = id->iProduct;
45 d->u.device.iSerialNumber = id->iSerialNumber;
47 d->u.device.bNumConfigurations = dev->bNumConfigurations;
49 return bLength;
52 int usb_desc_device_qualifier(const USBDescDevice *dev,
53 uint8_t *dest, size_t len)
55 uint8_t bLength = 0x0a;
56 USBDescriptor *d = (void *)dest;
58 if (len < bLength) {
59 return -1;
62 d->bLength = bLength;
63 d->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
65 d->u.device_qualifier.bcdUSB_lo = usb_lo(dev->bcdUSB);
66 d->u.device_qualifier.bcdUSB_hi = usb_hi(dev->bcdUSB);
67 d->u.device_qualifier.bDeviceClass = dev->bDeviceClass;
68 d->u.device_qualifier.bDeviceSubClass = dev->bDeviceSubClass;
69 d->u.device_qualifier.bDeviceProtocol = dev->bDeviceProtocol;
70 d->u.device_qualifier.bMaxPacketSize0 = dev->bMaxPacketSize0;
71 d->u.device_qualifier.bNumConfigurations = dev->bNumConfigurations;
72 d->u.device_qualifier.bReserved = 0;
74 return bLength;
77 int usb_desc_config(const USBDescConfig *conf, uint8_t *dest, size_t len)
79 uint8_t bLength = 0x09;
80 uint16_t wTotalLength = 0;
81 int i, rc;
83 if (len < bLength) {
84 return -1;
87 dest[0x00] = bLength;
88 dest[0x01] = USB_DT_CONFIG;
89 dest[0x04] = conf->bNumInterfaces;
90 dest[0x05] = conf->bConfigurationValue;
91 dest[0x06] = conf->iConfiguration;
92 dest[0x07] = conf->bmAttributes;
93 dest[0x08] = conf->bMaxPower;
94 wTotalLength += bLength;
96 /* handle grouped interfaces if any*/
97 for (i = 0; i < conf->nif_groups; i++) {
98 rc = usb_desc_iface_group(&(conf->if_groups[i]),
99 dest + wTotalLength,
100 len - wTotalLength);
101 if (rc < 0) {
102 return rc;
104 wTotalLength += rc;
107 /* handle normal (ungrouped / no IAD) interfaces if any */
108 for (i = 0; i < conf->nif; i++) {
109 rc = usb_desc_iface(conf->ifs + i, dest + wTotalLength, len - wTotalLength);
110 if (rc < 0) {
111 return rc;
113 wTotalLength += rc;
116 dest[0x02] = usb_lo(wTotalLength);
117 dest[0x03] = usb_hi(wTotalLength);
118 return wTotalLength;
121 int usb_desc_iface_group(const USBDescIfaceAssoc *iad, uint8_t *dest,
122 size_t len)
124 int pos = 0;
125 int i = 0;
127 /* handle interface association descriptor */
128 uint8_t bLength = 0x08;
130 if (len < bLength) {
131 return -1;
134 dest[0x00] = bLength;
135 dest[0x01] = USB_DT_INTERFACE_ASSOC;
136 dest[0x02] = iad->bFirstInterface;
137 dest[0x03] = iad->bInterfaceCount;
138 dest[0x04] = iad->bFunctionClass;
139 dest[0x05] = iad->bFunctionSubClass;
140 dest[0x06] = iad->bFunctionProtocol;
141 dest[0x07] = iad->iFunction;
142 pos += bLength;
144 /* handle associated interfaces in this group */
145 for (i = 0; i < iad->nif; i++) {
146 int rc = usb_desc_iface(&(iad->ifs[i]), dest + pos, len - pos);
147 if (rc < 0) {
148 return rc;
150 pos += rc;
153 return pos;
156 int usb_desc_iface(const USBDescIface *iface, uint8_t *dest, size_t len)
158 uint8_t bLength = 0x09;
159 int i, rc, pos = 0;
161 if (len < bLength) {
162 return -1;
165 dest[0x00] = bLength;
166 dest[0x01] = USB_DT_INTERFACE;
167 dest[0x02] = iface->bInterfaceNumber;
168 dest[0x03] = iface->bAlternateSetting;
169 dest[0x04] = iface->bNumEndpoints;
170 dest[0x05] = iface->bInterfaceClass;
171 dest[0x06] = iface->bInterfaceSubClass;
172 dest[0x07] = iface->bInterfaceProtocol;
173 dest[0x08] = iface->iInterface;
174 pos += bLength;
176 for (i = 0; i < iface->ndesc; i++) {
177 rc = usb_desc_other(iface->descs + i, dest + pos, len - pos);
178 if (rc < 0) {
179 return rc;
181 pos += rc;
184 for (i = 0; i < iface->bNumEndpoints; i++) {
185 rc = usb_desc_endpoint(iface->eps + i, dest + pos, len - pos);
186 if (rc < 0) {
187 return rc;
189 pos += rc;
192 return pos;
195 int usb_desc_endpoint(const USBDescEndpoint *ep, uint8_t *dest, size_t len)
197 uint8_t bLength = ep->is_audio ? 0x09 : 0x07;
198 uint8_t extralen = ep->extra ? ep->extra[0] : 0;
200 if (len < bLength + extralen) {
201 return -1;
204 dest[0x00] = bLength;
205 dest[0x01] = USB_DT_ENDPOINT;
206 dest[0x02] = ep->bEndpointAddress;
207 dest[0x03] = ep->bmAttributes;
208 dest[0x04] = usb_lo(ep->wMaxPacketSize);
209 dest[0x05] = usb_hi(ep->wMaxPacketSize);
210 dest[0x06] = ep->bInterval;
211 if (ep->is_audio) {
212 dest[0x07] = ep->bRefresh;
213 dest[0x08] = ep->bSynchAddress;
215 if (ep->extra) {
216 memcpy(dest + bLength, ep->extra, extralen);
219 return bLength + extralen;
222 int usb_desc_other(const USBDescOther *desc, uint8_t *dest, size_t len)
224 int bLength = desc->length ? desc->length : desc->data[0];
226 if (len < bLength) {
227 return -1;
230 memcpy(dest, desc->data, bLength);
231 return bLength;
234 /* ------------------------------------------------------------------ */
236 static void usb_desc_ep_init(USBDevice *dev)
238 const USBDescIface *iface;
239 int i, e, pid, ep;
241 usb_ep_init(dev);
242 for (i = 0; i < dev->ninterfaces; i++) {
243 iface = dev->ifaces[i];
244 if (iface == NULL) {
245 continue;
247 for (e = 0; e < iface->bNumEndpoints; e++) {
248 pid = (iface->eps[e].bEndpointAddress & USB_DIR_IN) ?
249 USB_TOKEN_IN : USB_TOKEN_OUT;
250 ep = iface->eps[e].bEndpointAddress & 0x0f;
251 usb_ep_set_type(dev, pid, ep, iface->eps[e].bmAttributes & 0x03);
252 usb_ep_set_ifnum(dev, pid, ep, iface->bInterfaceNumber);
253 usb_ep_set_max_packet_size(dev, pid, ep,
254 iface->eps[e].wMaxPacketSize);
259 static const USBDescIface *usb_desc_find_interface(USBDevice *dev,
260 int nif, int alt)
262 const USBDescIface *iface;
263 int g, i;
265 if (!dev->config) {
266 return NULL;
268 for (g = 0; g < dev->config->nif_groups; g++) {
269 for (i = 0; i < dev->config->if_groups[g].nif; i++) {
270 iface = &dev->config->if_groups[g].ifs[i];
271 if (iface->bInterfaceNumber == nif &&
272 iface->bAlternateSetting == alt) {
273 return iface;
277 for (i = 0; i < dev->config->nif; i++) {
278 iface = &dev->config->ifs[i];
279 if (iface->bInterfaceNumber == nif &&
280 iface->bAlternateSetting == alt) {
281 return iface;
284 return NULL;
287 static int usb_desc_set_interface(USBDevice *dev, int index, int value)
289 const USBDescIface *iface;
290 int old;
292 iface = usb_desc_find_interface(dev, index, value);
293 if (iface == NULL) {
294 return -1;
297 old = dev->altsetting[index];
298 dev->altsetting[index] = value;
299 dev->ifaces[index] = iface;
300 usb_desc_ep_init(dev);
302 if (old != value) {
303 usb_device_set_interface(dev, index, old, value);
305 return 0;
308 static int usb_desc_set_config(USBDevice *dev, int value)
310 int i;
312 if (value == 0) {
313 dev->configuration = 0;
314 dev->ninterfaces = 0;
315 dev->config = NULL;
316 } else {
317 for (i = 0; i < dev->device->bNumConfigurations; i++) {
318 if (dev->device->confs[i].bConfigurationValue == value) {
319 dev->configuration = value;
320 dev->ninterfaces = dev->device->confs[i].bNumInterfaces;
321 dev->config = dev->device->confs + i;
322 assert(dev->ninterfaces <= USB_MAX_INTERFACES);
325 if (i < dev->device->bNumConfigurations) {
326 return -1;
330 for (i = 0; i < dev->ninterfaces; i++) {
331 usb_desc_set_interface(dev, i, 0);
333 for (; i < USB_MAX_INTERFACES; i++) {
334 dev->altsetting[i] = 0;
335 dev->ifaces[i] = NULL;
338 return 0;
341 static void usb_desc_setdefaults(USBDevice *dev)
343 const USBDesc *desc = usb_device_get_usb_desc(dev);
345 assert(desc != NULL);
346 switch (dev->speed) {
347 case USB_SPEED_LOW:
348 case USB_SPEED_FULL:
349 dev->device = desc->full;
350 break;
351 case USB_SPEED_HIGH:
352 dev->device = desc->high;
353 break;
355 usb_desc_set_config(dev, 0);
358 void usb_desc_init(USBDevice *dev)
360 const USBDesc *desc = usb_device_get_usb_desc(dev);
362 assert(desc != NULL);
363 dev->speed = USB_SPEED_FULL;
364 dev->speedmask = 0;
365 if (desc->full) {
366 dev->speedmask |= USB_SPEED_MASK_FULL;
368 if (desc->high) {
369 dev->speedmask |= USB_SPEED_MASK_HIGH;
371 usb_desc_setdefaults(dev);
374 void usb_desc_attach(USBDevice *dev)
376 const USBDesc *desc = usb_device_get_usb_desc(dev);
378 assert(desc != NULL);
379 if (desc->high && (dev->port->speedmask & USB_SPEED_MASK_HIGH)) {
380 dev->speed = USB_SPEED_HIGH;
381 } else if (desc->full && (dev->port->speedmask & USB_SPEED_MASK_FULL)) {
382 dev->speed = USB_SPEED_FULL;
383 } else {
384 fprintf(stderr, "usb: port/device speed mismatch for \"%s\"\n",
385 usb_device_get_product_desc(dev));
386 return;
388 usb_desc_setdefaults(dev);
391 void usb_desc_set_string(USBDevice *dev, uint8_t index, const char *str)
393 USBDescString *s;
395 QLIST_FOREACH(s, &dev->strings, next) {
396 if (s->index == index) {
397 break;
400 if (s == NULL) {
401 s = g_malloc0(sizeof(*s));
402 s->index = index;
403 QLIST_INSERT_HEAD(&dev->strings, s, next);
405 g_free(s->str);
406 s->str = g_strdup(str);
409 const char *usb_desc_get_string(USBDevice *dev, uint8_t index)
411 USBDescString *s;
413 QLIST_FOREACH(s, &dev->strings, next) {
414 if (s->index == index) {
415 return s->str;
418 return NULL;
421 int usb_desc_string(USBDevice *dev, int index, uint8_t *dest, size_t len)
423 uint8_t bLength, pos, i;
424 const char *str;
426 if (len < 4) {
427 return -1;
430 if (index == 0) {
431 /* language ids */
432 dest[0] = 4;
433 dest[1] = USB_DT_STRING;
434 dest[2] = 0x09;
435 dest[3] = 0x04;
436 return 4;
439 str = usb_desc_get_string(dev, index);
440 if (str == NULL) {
441 str = usb_device_get_usb_desc(dev)->str[index];
442 if (str == NULL) {
443 return 0;
447 bLength = strlen(str) * 2 + 2;
448 dest[0] = bLength;
449 dest[1] = USB_DT_STRING;
450 i = 0; pos = 2;
451 while (pos+1 < bLength && pos+1 < len) {
452 dest[pos++] = str[i++];
453 dest[pos++] = 0;
455 return pos;
458 int usb_desc_get_descriptor(USBDevice *dev, int value, uint8_t *dest, size_t len)
460 const USBDesc *desc = usb_device_get_usb_desc(dev);
461 const USBDescDevice *other_dev;
462 uint8_t buf[256];
463 uint8_t type = value >> 8;
464 uint8_t index = value & 0xff;
465 int ret = -1;
467 if (dev->speed == USB_SPEED_HIGH) {
468 other_dev = usb_device_get_usb_desc(dev)->full;
469 } else {
470 other_dev = usb_device_get_usb_desc(dev)->high;
473 switch(type) {
474 case USB_DT_DEVICE:
475 ret = usb_desc_device(&desc->id, dev->device, buf, sizeof(buf));
476 trace_usb_desc_device(dev->addr, len, ret);
477 break;
478 case USB_DT_CONFIG:
479 if (index < dev->device->bNumConfigurations) {
480 ret = usb_desc_config(dev->device->confs + index, buf, sizeof(buf));
482 trace_usb_desc_config(dev->addr, index, len, ret);
483 break;
484 case USB_DT_STRING:
485 ret = usb_desc_string(dev, index, buf, sizeof(buf));
486 trace_usb_desc_string(dev->addr, index, len, ret);
487 break;
489 case USB_DT_DEVICE_QUALIFIER:
490 if (other_dev != NULL) {
491 ret = usb_desc_device_qualifier(other_dev, buf, sizeof(buf));
493 trace_usb_desc_device_qualifier(dev->addr, len, ret);
494 break;
495 case USB_DT_OTHER_SPEED_CONFIG:
496 if (other_dev != NULL && index < other_dev->bNumConfigurations) {
497 ret = usb_desc_config(other_dev->confs + index, buf, sizeof(buf));
498 buf[0x01] = USB_DT_OTHER_SPEED_CONFIG;
500 trace_usb_desc_other_speed_config(dev->addr, index, len, ret);
501 break;
503 case USB_DT_DEBUG:
504 /* ignore silently */
505 break;
507 default:
508 fprintf(stderr, "%s: %d unknown type %d (len %zd)\n", __FUNCTION__,
509 dev->addr, type, len);
510 break;
513 if (ret > 0) {
514 if (ret > len) {
515 ret = len;
517 memcpy(dest, buf, ret);
519 return ret;
522 int usb_desc_handle_control(USBDevice *dev, USBPacket *p,
523 int request, int value, int index, int length, uint8_t *data)
525 const USBDesc *desc = usb_device_get_usb_desc(dev);
526 int ret = -1;
528 assert(desc != NULL);
529 switch(request) {
530 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
531 dev->addr = value;
532 trace_usb_set_addr(dev->addr);
533 ret = 0;
534 break;
536 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
537 ret = usb_desc_get_descriptor(dev, value, data, length);
538 break;
540 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
542 * 9.4.2: 0 should be returned if the device is unconfigured, otherwise
543 * the non zero value of bConfigurationValue.
545 data[0] = dev->config ? dev->config->bConfigurationValue : 0;
546 ret = 1;
547 break;
548 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
549 ret = usb_desc_set_config(dev, value);
550 trace_usb_set_config(dev->addr, value, ret);
551 break;
553 case DeviceRequest | USB_REQ_GET_STATUS: {
554 const USBDescConfig *config = dev->config ?
555 dev->config : &dev->device->confs[0];
557 data[0] = 0;
559 * Default state: Device behavior when this request is received while
560 * the device is in the Default state is not specified.
561 * We return the same value that a configured device would return if
562 * it used the first configuration.
564 if (config->bmAttributes & 0x40) {
565 data[0] |= 1 << USB_DEVICE_SELF_POWERED;
567 if (dev->remote_wakeup) {
568 data[0] |= 1 << USB_DEVICE_REMOTE_WAKEUP;
570 data[1] = 0x00;
571 ret = 2;
572 break;
574 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
575 if (value == USB_DEVICE_REMOTE_WAKEUP) {
576 dev->remote_wakeup = 0;
577 ret = 0;
579 trace_usb_clear_device_feature(dev->addr, value, ret);
580 break;
581 case DeviceOutRequest | USB_REQ_SET_FEATURE:
582 if (value == USB_DEVICE_REMOTE_WAKEUP) {
583 dev->remote_wakeup = 1;
584 ret = 0;
586 trace_usb_set_device_feature(dev->addr, value, ret);
587 break;
589 case InterfaceRequest | USB_REQ_GET_INTERFACE:
590 if (index < 0 || index >= dev->ninterfaces) {
591 break;
593 data[0] = dev->altsetting[index];
594 ret = 1;
595 break;
596 case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
597 ret = usb_desc_set_interface(dev, index, value);
598 trace_usb_set_interface(dev->addr, index, value, ret);
599 break;
602 return ret;