]> git.proxmox.com Git - mirror_qemu.git/blame - hw/usb/desc.c
usb: use USBDescriptor for device qualifier descriptors.
[mirror_qemu.git] / hw / usb / desc.c
CommitLineData
f1ae32a1
GH
1#include "hw/usb.h"
2#include "hw/usb/desc.h"
37fb59d3
GH
3#include "trace.h"
4
5/* ------------------------------------------------------------------ */
6
7static uint8_t usb_lo(uint16_t val)
8{
9 return val & 0xff;
10}
11
12static uint8_t usb_hi(uint16_t val)
13{
14 return (val >> 8) & 0xff;
15}
16
17int usb_desc_device(const USBDescID *id, const USBDescDevice *dev,
18 uint8_t *dest, size_t len)
19{
20 uint8_t bLength = 0x12;
d3f904ea 21 USBDescriptor *d = (void *)dest;
37fb59d3
GH
22
23 if (len < bLength) {
24 return -1;
25 }
26
d3f904ea
GH
27 d->bLength = bLength;
28 d->bDescriptorType = USB_DT_DEVICE;
37fb59d3 29
d3f904ea
GH
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;
37fb59d3 36
d3f904ea
GH
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;
37fb59d3 46
d3f904ea 47 d->u.device.bNumConfigurations = dev->bNumConfigurations;
37fb59d3
GH
48
49 return bLength;
50}
51
25620cba
GH
52int usb_desc_device_qualifier(const USBDescDevice *dev,
53 uint8_t *dest, size_t len)
54{
55 uint8_t bLength = 0x0a;
3cfeee61 56 USBDescriptor *d = (void *)dest;
25620cba
GH
57
58 if (len < bLength) {
59 return -1;
60 }
61
3cfeee61
GH
62 d->bLength = bLength;
63 d->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
64
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;
25620cba
GH
73
74 return bLength;
75}
76
37fb59d3
GH
77int usb_desc_config(const USBDescConfig *conf, uint8_t *dest, size_t len)
78{
79 uint8_t bLength = 0x09;
80 uint16_t wTotalLength = 0;
fef13fa8 81 int i, rc;
37fb59d3
GH
82
83 if (len < bLength) {
84 return -1;
85 }
86
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;
95
6e625fc7
BH
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;
103 }
104 wTotalLength += rc;
105 }
106
107 /* handle normal (ungrouped / no IAD) interfaces if any */
fef13fa8 108 for (i = 0; i < conf->nif; i++) {
37fb59d3
GH
109 rc = usb_desc_iface(conf->ifs + i, dest + wTotalLength, len - wTotalLength);
110 if (rc < 0) {
111 return rc;
112 }
113 wTotalLength += rc;
114 }
115
116 dest[0x02] = usb_lo(wTotalLength);
117 dest[0x03] = usb_hi(wTotalLength);
118 return wTotalLength;
119}
120
6e625fc7
BH
121int usb_desc_iface_group(const USBDescIfaceAssoc *iad, uint8_t *dest,
122 size_t len)
123{
124 int pos = 0;
125 int i = 0;
126
127 /* handle interface association descriptor */
128 uint8_t bLength = 0x08;
129
130 if (len < bLength) {
131 return -1;
132 }
133
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;
143
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;
149 }
150 pos += rc;
151 }
152
153 return pos;
154}
155
37fb59d3
GH
156int usb_desc_iface(const USBDescIface *iface, uint8_t *dest, size_t len)
157{
158 uint8_t bLength = 0x09;
159 int i, rc, pos = 0;
160
161 if (len < bLength) {
162 return -1;
163 }
164
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;
175
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;
180 }
181 pos += rc;
182 }
183
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;
188 }
189 pos += rc;
190 }
191
192 return pos;
193}
194
195int usb_desc_endpoint(const USBDescEndpoint *ep, uint8_t *dest, size_t len)
196{
cc5f1395
GH
197 uint8_t bLength = ep->is_audio ? 0x09 : 0x07;
198 uint8_t extralen = ep->extra ? ep->extra[0] : 0;
37fb59d3 199
cc5f1395 200 if (len < bLength + extralen) {
37fb59d3
GH
201 return -1;
202 }
203
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;
cc5f1395
GH
211 if (ep->is_audio) {
212 dest[0x07] = ep->bRefresh;
213 dest[0x08] = ep->bSynchAddress;
214 }
215 if (ep->extra) {
216 memcpy(dest + bLength, ep->extra, extralen);
217 }
37fb59d3 218
cc5f1395 219 return bLength + extralen;
37fb59d3
GH
220}
221
222int usb_desc_other(const USBDescOther *desc, uint8_t *dest, size_t len)
223{
224 int bLength = desc->length ? desc->length : desc->data[0];
225
226 if (len < bLength) {
227 return -1;
228 }
229
230 memcpy(dest, desc->data, bLength);
231 return bLength;
232}
233
132a3f55
GH
234/* ------------------------------------------------------------------ */
235
83a53bbc
GH
236static void usb_desc_ep_init(USBDevice *dev)
237{
238 const USBDescIface *iface;
239 int i, e, pid, ep;
240
241 usb_ep_init(dev);
242 for (i = 0; i < dev->ninterfaces; i++) {
243 iface = dev->ifaces[i];
244 if (iface == NULL) {
245 continue;
246 }
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);
f003397c
GH
253 usb_ep_set_max_packet_size(dev, pid, ep,
254 iface->eps[e].wMaxPacketSize);
83a53bbc
GH
255 }
256 }
257}
258
1de14d43
GH
259static const USBDescIface *usb_desc_find_interface(USBDevice *dev,
260 int nif, int alt)
261{
262 const USBDescIface *iface;
263 int g, i;
264
265 if (!dev->config) {
266 return NULL;
267 }
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;
274 }
275 }
276 }
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;
282 }
283 }
284 return NULL;
285}
286
287static int usb_desc_set_interface(USBDevice *dev, int index, int value)
288{
289 const USBDescIface *iface;
290 int old;
291
292 iface = usb_desc_find_interface(dev, index, value);
293 if (iface == NULL) {
294 return -1;
295 }
296
297 old = dev->altsetting[index];
298 dev->altsetting[index] = value;
299 dev->ifaces[index] = iface;
83a53bbc 300 usb_desc_ep_init(dev);
1de14d43 301
62aed765
AL
302 if (old != value) {
303 usb_device_set_interface(dev, index, old, value);
1de14d43
GH
304 }
305 return 0;
306}
307
65360511
GH
308static int usb_desc_set_config(USBDevice *dev, int value)
309{
310 int i;
311
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;
1de14d43 322 assert(dev->ninterfaces <= USB_MAX_INTERFACES);
65360511
GH
323 }
324 }
325 if (i < dev->device->bNumConfigurations) {
326 return -1;
327 }
328 }
1de14d43
GH
329
330 for (i = 0; i < dev->ninterfaces; i++) {
331 usb_desc_set_interface(dev, i, 0);
332 }
333 for (; i < USB_MAX_INTERFACES; i++) {
334 dev->altsetting[i] = 0;
335 dev->ifaces[i] = NULL;
336 }
337
65360511
GH
338 return 0;
339}
340
32d41919 341static void usb_desc_setdefaults(USBDevice *dev)
a980a065 342{
62aed765 343 const USBDesc *desc = usb_device_get_usb_desc(dev);
a980a065
GH
344
345 assert(desc != NULL);
32d41919
GH
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;
354 }
65360511 355 usb_desc_set_config(dev, 0);
a980a065
GH
356}
357
32d41919
GH
358void usb_desc_init(USBDevice *dev)
359{
62aed765 360 const USBDesc *desc = usb_device_get_usb_desc(dev);
ba3f9bfb
HG
361
362 assert(desc != NULL);
32d41919 363 dev->speed = USB_SPEED_FULL;
ba3f9bfb
HG
364 dev->speedmask = 0;
365 if (desc->full) {
366 dev->speedmask |= USB_SPEED_MASK_FULL;
367 }
368 if (desc->high) {
369 dev->speedmask |= USB_SPEED_MASK_HIGH;
370 }
32d41919
GH
371 usb_desc_setdefaults(dev);
372}
373
374void usb_desc_attach(USBDevice *dev)
375{
62aed765 376 const USBDesc *desc = usb_device_get_usb_desc(dev);
32d41919
GH
377
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",
62aed765 385 usb_device_get_product_desc(dev));
32d41919
GH
386 return;
387 }
388 usb_desc_setdefaults(dev);
389}
390
132a3f55
GH
391void usb_desc_set_string(USBDevice *dev, uint8_t index, const char *str)
392{
393 USBDescString *s;
394
395 QLIST_FOREACH(s, &dev->strings, next) {
396 if (s->index == index) {
397 break;
398 }
399 }
400 if (s == NULL) {
7267c094 401 s = g_malloc0(sizeof(*s));
132a3f55
GH
402 s->index = index;
403 QLIST_INSERT_HEAD(&dev->strings, s, next);
404 }
7267c094
AL
405 g_free(s->str);
406 s->str = g_strdup(str);
132a3f55
GH
407}
408
409const char *usb_desc_get_string(USBDevice *dev, uint8_t index)
410{
411 USBDescString *s;
412
413 QLIST_FOREACH(s, &dev->strings, next) {
414 if (s->index == index) {
415 return s->str;
416 }
417 }
418 return NULL;
419}
420
421int usb_desc_string(USBDevice *dev, int index, uint8_t *dest, size_t len)
37fb59d3
GH
422{
423 uint8_t bLength, pos, i;
132a3f55 424 const char *str;
37fb59d3
GH
425
426 if (len < 4) {
427 return -1;
428 }
429
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;
437 }
438
132a3f55
GH
439 str = usb_desc_get_string(dev, index);
440 if (str == NULL) {
62aed765 441 str = usb_device_get_usb_desc(dev)->str[index];
132a3f55
GH
442 if (str == NULL) {
443 return 0;
444 }
37fb59d3 445 }
132a3f55
GH
446
447 bLength = strlen(str) * 2 + 2;
37fb59d3
GH
448 dest[0] = bLength;
449 dest[1] = USB_DT_STRING;
450 i = 0; pos = 2;
451 while (pos+1 < bLength && pos+1 < len) {
132a3f55 452 dest[pos++] = str[i++];
37fb59d3
GH
453 dest[pos++] = 0;
454 }
455 return pos;
456}
457
37fb59d3
GH
458int usb_desc_get_descriptor(USBDevice *dev, int value, uint8_t *dest, size_t len)
459{
62aed765 460 const USBDesc *desc = usb_device_get_usb_desc(dev);
25620cba 461 const USBDescDevice *other_dev;
37fb59d3
GH
462 uint8_t buf[256];
463 uint8_t type = value >> 8;
464 uint8_t index = value & 0xff;
465 int ret = -1;
466
25620cba 467 if (dev->speed == USB_SPEED_HIGH) {
62aed765 468 other_dev = usb_device_get_usb_desc(dev)->full;
25620cba 469 } else {
62aed765 470 other_dev = usb_device_get_usb_desc(dev)->high;
25620cba
GH
471 }
472
37fb59d3
GH
473 switch(type) {
474 case USB_DT_DEVICE:
a980a065 475 ret = usb_desc_device(&desc->id, dev->device, buf, sizeof(buf));
37fb59d3
GH
476 trace_usb_desc_device(dev->addr, len, ret);
477 break;
478 case USB_DT_CONFIG:
a980a065
GH
479 if (index < dev->device->bNumConfigurations) {
480 ret = usb_desc_config(dev->device->confs + index, buf, sizeof(buf));
37fb59d3
GH
481 }
482 trace_usb_desc_config(dev->addr, index, len, ret);
483 break;
484 case USB_DT_STRING:
132a3f55 485 ret = usb_desc_string(dev, index, buf, sizeof(buf));
37fb59d3
GH
486 trace_usb_desc_string(dev->addr, index, len, ret);
487 break;
25620cba
GH
488
489 case USB_DT_DEVICE_QUALIFIER:
490 if (other_dev != NULL) {
491 ret = usb_desc_device_qualifier(other_dev, buf, sizeof(buf));
492 }
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;
499 }
500 trace_usb_desc_other_speed_config(dev->addr, index, len, ret);
501 break;
502
a7fb71d1
GH
503 case USB_DT_DEBUG:
504 /* ignore silently */
505 break;
506
37fb59d3
GH
507 default:
508 fprintf(stderr, "%s: %d unknown type %d (len %zd)\n", __FUNCTION__,
509 dev->addr, type, len);
510 break;
511 }
512
513 if (ret > 0) {
514 if (ret > len) {
515 ret = len;
516 }
517 memcpy(dest, buf, ret);
518 }
519 return ret;
520}
521
007fd62f
HG
522int usb_desc_handle_control(USBDevice *dev, USBPacket *p,
523 int request, int value, int index, int length, uint8_t *data)
37fb59d3 524{
62aed765 525 const USBDesc *desc = usb_device_get_usb_desc(dev);
65360511 526 int ret = -1;
37fb59d3
GH
527
528 assert(desc != NULL);
529 switch(request) {
41c6abbd
GH
530 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
531 dev->addr = value;
532 trace_usb_set_addr(dev->addr);
533 ret = 0;
534 break;
535
37fb59d3
GH
536 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
537 ret = usb_desc_get_descriptor(dev, value, data, length);
538 break;
a980a065
GH
539
540 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
8db36e9d
AL
541 /*
542 * 9.4.2: 0 should be returned if the device is unconfigured, otherwise
543 * the non zero value of bConfigurationValue.
544 */
545 data[0] = dev->config ? dev->config->bConfigurationValue : 0;
a980a065
GH
546 ret = 1;
547 break;
548 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
65360511 549 ret = usb_desc_set_config(dev, value);
a980a065
GH
550 trace_usb_set_config(dev->addr, value, ret);
551 break;
ed5a83dd 552
8db36e9d
AL
553 case DeviceRequest | USB_REQ_GET_STATUS: {
554 const USBDescConfig *config = dev->config ?
555 dev->config : &dev->device->confs[0];
556
ed5a83dd 557 data[0] = 0;
8db36e9d
AL
558 /*
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.
563 */
564 if (config->bmAttributes & 0x40) {
ed5a83dd
GH
565 data[0] |= 1 << USB_DEVICE_SELF_POWERED;
566 }
567 if (dev->remote_wakeup) {
568 data[0] |= 1 << USB_DEVICE_REMOTE_WAKEUP;
569 }
570 data[1] = 0x00;
571 ret = 2;
572 break;
8db36e9d 573 }
ed5a83dd
GH
574 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
575 if (value == USB_DEVICE_REMOTE_WAKEUP) {
576 dev->remote_wakeup = 0;
577 ret = 0;
578 }
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;
585 }
586 trace_usb_set_device_feature(dev->addr, value, ret);
587 break;
1de14d43
GH
588
589 case InterfaceRequest | USB_REQ_GET_INTERFACE:
590 if (index < 0 || index >= dev->ninterfaces) {
591 break;
592 }
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;
600
37fb59d3
GH
601 }
602 return ret;
603}