usb-desc: audio endpoint support
[qemu.git] / hw / usb-desc.c
blob9c3866180e0b797cf9d14890ba2b372f9d9913cf
1 #include "usb.h"
2 #include "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;
22 if (len < bLength) {
23 return -1;
26 dest[0x00] = bLength;
27 dest[0x01] = USB_DT_DEVICE;
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;
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;
46 dest[0x11] = dev->bNumConfigurations;
48 return bLength;
51 int usb_desc_device_qualifier(const USBDescDevice *dev,
52 uint8_t *dest, size_t len)
54 uint8_t bLength = 0x0a;
56 if (len < bLength) {
57 return -1;
60 dest[0x00] = bLength;
61 dest[0x01] = USB_DT_DEVICE_QUALIFIER;
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 */
72 return bLength;
75 int usb_desc_config(const USBDescConfig *conf, uint8_t *dest, size_t len)
77 uint8_t bLength = 0x09;
78 uint16_t wTotalLength = 0;
79 int i, rc;
81 if (len < bLength) {
82 return -1;
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;
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;
102 wTotalLength += rc;
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;
111 wTotalLength += rc;
114 dest[0x02] = usb_lo(wTotalLength);
115 dest[0x03] = usb_hi(wTotalLength);
116 return wTotalLength;
119 int usb_desc_iface_group(const USBDescIfaceAssoc *iad, uint8_t *dest,
120 size_t len)
122 int pos = 0;
123 int i = 0;
125 /* handle interface association descriptor */
126 uint8_t bLength = 0x08;
128 if (len < bLength) {
129 return -1;
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;
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;
148 pos += rc;
151 return pos;
154 int usb_desc_iface(const USBDescIface *iface, uint8_t *dest, size_t len)
156 uint8_t bLength = 0x09;
157 int i, rc, pos = 0;
159 if (len < bLength) {
160 return -1;
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;
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;
179 pos += rc;
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;
187 pos += rc;
190 return pos;
193 int usb_desc_endpoint(const USBDescEndpoint *ep, uint8_t *dest, size_t len)
195 uint8_t bLength = ep->is_audio ? 0x09 : 0x07;
196 uint8_t extralen = ep->extra ? ep->extra[0] : 0;
198 if (len < bLength + extralen) {
199 return -1;
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;
213 if (ep->extra) {
214 memcpy(dest + bLength, ep->extra, extralen);
217 return bLength + extralen;
220 int usb_desc_other(const USBDescOther *desc, uint8_t *dest, size_t len)
222 int bLength = desc->length ? desc->length : desc->data[0];
224 if (len < bLength) {
225 return -1;
228 memcpy(dest, desc->data, bLength);
229 return bLength;
232 /* ------------------------------------------------------------------ */
234 static const USBDescIface *usb_desc_find_interface(USBDevice *dev,
235 int nif, int alt)
237 const USBDescIface *iface;
238 int g, i;
240 if (!dev->config) {
241 return NULL;
243 for (g = 0; g < dev->config->nif_groups; g++) {
244 for (i = 0; i < dev->config->if_groups[g].nif; i++) {
245 iface = &dev->config->if_groups[g].ifs[i];
246 if (iface->bInterfaceNumber == nif &&
247 iface->bAlternateSetting == alt) {
248 return iface;
252 for (i = 0; i < dev->config->nif; i++) {
253 iface = &dev->config->ifs[i];
254 if (iface->bInterfaceNumber == nif &&
255 iface->bAlternateSetting == alt) {
256 return iface;
259 return NULL;
262 static int usb_desc_set_interface(USBDevice *dev, int index, int value)
264 const USBDescIface *iface;
265 int old;
267 iface = usb_desc_find_interface(dev, index, value);
268 if (iface == NULL) {
269 return -1;
272 old = dev->altsetting[index];
273 dev->altsetting[index] = value;
274 dev->ifaces[index] = iface;
276 if (dev->info->set_interface && old != value) {
277 dev->info->set_interface(dev, index, old, value);
279 return 0;
282 static int usb_desc_set_config(USBDevice *dev, int value)
284 int i;
286 if (value == 0) {
287 dev->configuration = 0;
288 dev->ninterfaces = 0;
289 dev->config = NULL;
290 } else {
291 for (i = 0; i < dev->device->bNumConfigurations; i++) {
292 if (dev->device->confs[i].bConfigurationValue == value) {
293 dev->configuration = value;
294 dev->ninterfaces = dev->device->confs[i].bNumInterfaces;
295 dev->config = dev->device->confs + i;
296 assert(dev->ninterfaces <= USB_MAX_INTERFACES);
299 if (i < dev->device->bNumConfigurations) {
300 return -1;
304 for (i = 0; i < dev->ninterfaces; i++) {
305 usb_desc_set_interface(dev, i, 0);
307 for (; i < USB_MAX_INTERFACES; i++) {
308 dev->altsetting[i] = 0;
309 dev->ifaces[i] = NULL;
312 return 0;
315 static void usb_desc_setdefaults(USBDevice *dev)
317 const USBDesc *desc = dev->info->usb_desc;
319 assert(desc != NULL);
320 switch (dev->speed) {
321 case USB_SPEED_LOW:
322 case USB_SPEED_FULL:
323 dev->device = desc->full;
324 break;
325 case USB_SPEED_HIGH:
326 dev->device = desc->high;
327 break;
329 usb_desc_set_config(dev, 0);
332 void usb_desc_init(USBDevice *dev)
334 const USBDesc *desc = dev->info->usb_desc;
336 assert(desc != NULL);
337 dev->speed = USB_SPEED_FULL;
338 dev->speedmask = 0;
339 if (desc->full) {
340 dev->speedmask |= USB_SPEED_MASK_FULL;
342 if (desc->high) {
343 dev->speedmask |= USB_SPEED_MASK_HIGH;
345 usb_desc_setdefaults(dev);
348 void usb_desc_attach(USBDevice *dev)
350 const USBDesc *desc = dev->info->usb_desc;
352 assert(desc != NULL);
353 if (desc->high && (dev->port->speedmask & USB_SPEED_MASK_HIGH)) {
354 dev->speed = USB_SPEED_HIGH;
355 } else if (desc->full && (dev->port->speedmask & USB_SPEED_MASK_FULL)) {
356 dev->speed = USB_SPEED_FULL;
357 } else {
358 fprintf(stderr, "usb: port/device speed mismatch for \"%s\"\n",
359 dev->info->product_desc);
360 return;
362 usb_desc_setdefaults(dev);
365 void usb_desc_set_string(USBDevice *dev, uint8_t index, const char *str)
367 USBDescString *s;
369 QLIST_FOREACH(s, &dev->strings, next) {
370 if (s->index == index) {
371 break;
374 if (s == NULL) {
375 s = g_malloc0(sizeof(*s));
376 s->index = index;
377 QLIST_INSERT_HEAD(&dev->strings, s, next);
379 g_free(s->str);
380 s->str = g_strdup(str);
383 const char *usb_desc_get_string(USBDevice *dev, uint8_t index)
385 USBDescString *s;
387 QLIST_FOREACH(s, &dev->strings, next) {
388 if (s->index == index) {
389 return s->str;
392 return NULL;
395 int usb_desc_string(USBDevice *dev, int index, uint8_t *dest, size_t len)
397 uint8_t bLength, pos, i;
398 const char *str;
400 if (len < 4) {
401 return -1;
404 if (index == 0) {
405 /* language ids */
406 dest[0] = 4;
407 dest[1] = USB_DT_STRING;
408 dest[2] = 0x09;
409 dest[3] = 0x04;
410 return 4;
413 str = usb_desc_get_string(dev, index);
414 if (str == NULL) {
415 str = dev->info->usb_desc->str[index];
416 if (str == NULL) {
417 return 0;
421 bLength = strlen(str) * 2 + 2;
422 dest[0] = bLength;
423 dest[1] = USB_DT_STRING;
424 i = 0; pos = 2;
425 while (pos+1 < bLength && pos+1 < len) {
426 dest[pos++] = str[i++];
427 dest[pos++] = 0;
429 return pos;
432 int usb_desc_get_descriptor(USBDevice *dev, int value, uint8_t *dest, size_t len)
434 const USBDesc *desc = dev->info->usb_desc;
435 const USBDescDevice *other_dev;
436 uint8_t buf[256];
437 uint8_t type = value >> 8;
438 uint8_t index = value & 0xff;
439 int ret = -1;
441 if (dev->speed == USB_SPEED_HIGH) {
442 other_dev = dev->info->usb_desc->full;
443 } else {
444 other_dev = dev->info->usb_desc->high;
447 switch(type) {
448 case USB_DT_DEVICE:
449 ret = usb_desc_device(&desc->id, dev->device, buf, sizeof(buf));
450 trace_usb_desc_device(dev->addr, len, ret);
451 break;
452 case USB_DT_CONFIG:
453 if (index < dev->device->bNumConfigurations) {
454 ret = usb_desc_config(dev->device->confs + index, buf, sizeof(buf));
456 trace_usb_desc_config(dev->addr, index, len, ret);
457 break;
458 case USB_DT_STRING:
459 ret = usb_desc_string(dev, index, buf, sizeof(buf));
460 trace_usb_desc_string(dev->addr, index, len, ret);
461 break;
463 case USB_DT_DEVICE_QUALIFIER:
464 if (other_dev != NULL) {
465 ret = usb_desc_device_qualifier(other_dev, buf, sizeof(buf));
467 trace_usb_desc_device_qualifier(dev->addr, len, ret);
468 break;
469 case USB_DT_OTHER_SPEED_CONFIG:
470 if (other_dev != NULL && index < other_dev->bNumConfigurations) {
471 ret = usb_desc_config(other_dev->confs + index, buf, sizeof(buf));
472 buf[0x01] = USB_DT_OTHER_SPEED_CONFIG;
474 trace_usb_desc_other_speed_config(dev->addr, index, len, ret);
475 break;
477 case USB_DT_DEBUG:
478 /* ignore silently */
479 break;
481 default:
482 fprintf(stderr, "%s: %d unknown type %d (len %zd)\n", __FUNCTION__,
483 dev->addr, type, len);
484 break;
487 if (ret > 0) {
488 if (ret > len) {
489 ret = len;
491 memcpy(dest, buf, ret);
493 return ret;
496 int usb_desc_handle_control(USBDevice *dev, USBPacket *p,
497 int request, int value, int index, int length, uint8_t *data)
499 const USBDesc *desc = dev->info->usb_desc;
500 int ret = -1;
502 assert(desc != NULL);
503 switch(request) {
504 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
505 dev->addr = value;
506 trace_usb_set_addr(dev->addr);
507 ret = 0;
508 break;
510 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
511 ret = usb_desc_get_descriptor(dev, value, data, length);
512 break;
514 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
515 data[0] = dev->config->bConfigurationValue;
516 ret = 1;
517 break;
518 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
519 ret = usb_desc_set_config(dev, value);
520 trace_usb_set_config(dev->addr, value, ret);
521 break;
523 case DeviceRequest | USB_REQ_GET_STATUS:
524 data[0] = 0;
525 if (dev->config->bmAttributes & 0x40) {
526 data[0] |= 1 << USB_DEVICE_SELF_POWERED;
528 if (dev->remote_wakeup) {
529 data[0] |= 1 << USB_DEVICE_REMOTE_WAKEUP;
531 data[1] = 0x00;
532 ret = 2;
533 break;
534 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
535 if (value == USB_DEVICE_REMOTE_WAKEUP) {
536 dev->remote_wakeup = 0;
537 ret = 0;
539 trace_usb_clear_device_feature(dev->addr, value, ret);
540 break;
541 case DeviceOutRequest | USB_REQ_SET_FEATURE:
542 if (value == USB_DEVICE_REMOTE_WAKEUP) {
543 dev->remote_wakeup = 1;
544 ret = 0;
546 trace_usb_set_device_feature(dev->addr, value, ret);
547 break;
549 case InterfaceRequest | USB_REQ_GET_INTERFACE:
550 if (index < 0 || index >= dev->ninterfaces) {
551 break;
553 data[0] = dev->altsetting[index];
554 ret = 1;
555 break;
556 case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
557 ret = usb_desc_set_interface(dev, index, value);
558 trace_usb_set_interface(dev->addr, index, value, ret);
559 break;
562 return ret;