]> git.proxmox.com Git - mirror_qemu.git/blame - hw/usb/desc.c
usb-redir: simplify packet copy
[mirror_qemu.git] / hw / usb / desc.c
CommitLineData
9d55d1ad
GH
1#include <ctype.h>
2
f1ae32a1
GH
3#include "hw/usb.h"
4#include "hw/usb/desc.h"
37fb59d3
GH
5#include "trace.h"
6
7/* ------------------------------------------------------------------ */
8
9static uint8_t usb_lo(uint16_t val)
10{
11 return val & 0xff;
12}
13
14static uint8_t usb_hi(uint16_t val)
15{
16 return (val >> 8) & 0xff;
17}
18
19int usb_desc_device(const USBDescID *id, const USBDescDevice *dev,
20 uint8_t *dest, size_t len)
21{
22 uint8_t bLength = 0x12;
d3f904ea 23 USBDescriptor *d = (void *)dest;
37fb59d3
GH
24
25 if (len < bLength) {
26 return -1;
27 }
28
d3f904ea
GH
29 d->bLength = bLength;
30 d->bDescriptorType = USB_DT_DEVICE;
37fb59d3 31
d3f904ea
GH
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;
37fb59d3 38
d3f904ea
GH
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;
37fb59d3 48
d3f904ea 49 d->u.device.bNumConfigurations = dev->bNumConfigurations;
37fb59d3
GH
50
51 return bLength;
52}
53
25620cba
GH
54int usb_desc_device_qualifier(const USBDescDevice *dev,
55 uint8_t *dest, size_t len)
56{
57 uint8_t bLength = 0x0a;
3cfeee61 58 USBDescriptor *d = (void *)dest;
25620cba
GH
59
60 if (len < bLength) {
61 return -1;
62 }
63
3cfeee61
GH
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;
25620cba
GH
75
76 return bLength;
77}
78
b43a2851
GH
79int usb_desc_config(const USBDescConfig *conf, int flags,
80 uint8_t *dest, size_t len)
37fb59d3
GH
81{
82 uint8_t bLength = 0x09;
83 uint16_t wTotalLength = 0;
0a263db1 84 USBDescriptor *d = (void *)dest;
fef13fa8 85 int i, rc;
37fb59d3
GH
86
87 if (len < bLength) {
88 return -1;
89 }
90
0a263db1
GH
91 d->bLength = bLength;
92 d->bDescriptorType = USB_DT_CONFIG;
93
94 d->u.config.bNumInterfaces = conf->bNumInterfaces;
95 d->u.config.bConfigurationValue = conf->bConfigurationValue;
96 d->u.config.iConfiguration = conf->iConfiguration;
97 d->u.config.bmAttributes = conf->bmAttributes;
98 d->u.config.bMaxPower = conf->bMaxPower;
37fb59d3
GH
99 wTotalLength += bLength;
100
0a263db1 101 /* handle grouped interfaces if any */
6e625fc7 102 for (i = 0; i < conf->nif_groups; i++) {
b43a2851 103 rc = usb_desc_iface_group(&(conf->if_groups[i]), flags,
6e625fc7
BH
104 dest + wTotalLength,
105 len - wTotalLength);
106 if (rc < 0) {
107 return rc;
108 }
109 wTotalLength += rc;
110 }
111
112 /* handle normal (ungrouped / no IAD) interfaces if any */
fef13fa8 113 for (i = 0; i < conf->nif; i++) {
b43a2851
GH
114 rc = usb_desc_iface(conf->ifs + i, flags,
115 dest + wTotalLength, len - wTotalLength);
37fb59d3
GH
116 if (rc < 0) {
117 return rc;
118 }
119 wTotalLength += rc;
120 }
121
0a263db1
GH
122 d->u.config.wTotalLength_lo = usb_lo(wTotalLength);
123 d->u.config.wTotalLength_hi = usb_hi(wTotalLength);
37fb59d3
GH
124 return wTotalLength;
125}
126
b43a2851
GH
127int usb_desc_iface_group(const USBDescIfaceAssoc *iad, int flags,
128 uint8_t *dest, size_t len)
6e625fc7
BH
129{
130 int pos = 0;
131 int i = 0;
132
133 /* handle interface association descriptor */
134 uint8_t bLength = 0x08;
135
136 if (len < bLength) {
137 return -1;
138 }
139
140 dest[0x00] = bLength;
141 dest[0x01] = USB_DT_INTERFACE_ASSOC;
142 dest[0x02] = iad->bFirstInterface;
143 dest[0x03] = iad->bInterfaceCount;
144 dest[0x04] = iad->bFunctionClass;
145 dest[0x05] = iad->bFunctionSubClass;
146 dest[0x06] = iad->bFunctionProtocol;
147 dest[0x07] = iad->iFunction;
148 pos += bLength;
149
150 /* handle associated interfaces in this group */
151 for (i = 0; i < iad->nif; i++) {
b43a2851 152 int rc = usb_desc_iface(&(iad->ifs[i]), flags, dest + pos, len - pos);
6e625fc7
BH
153 if (rc < 0) {
154 return rc;
155 }
156 pos += rc;
157 }
158
159 return pos;
160}
161
b43a2851
GH
162int usb_desc_iface(const USBDescIface *iface, int flags,
163 uint8_t *dest, size_t len)
37fb59d3
GH
164{
165 uint8_t bLength = 0x09;
166 int i, rc, pos = 0;
feafd797 167 USBDescriptor *d = (void *)dest;
37fb59d3
GH
168
169 if (len < bLength) {
170 return -1;
171 }
172
feafd797
GH
173 d->bLength = bLength;
174 d->bDescriptorType = USB_DT_INTERFACE;
175
176 d->u.interface.bInterfaceNumber = iface->bInterfaceNumber;
177 d->u.interface.bAlternateSetting = iface->bAlternateSetting;
178 d->u.interface.bNumEndpoints = iface->bNumEndpoints;
179 d->u.interface.bInterfaceClass = iface->bInterfaceClass;
180 d->u.interface.bInterfaceSubClass = iface->bInterfaceSubClass;
181 d->u.interface.bInterfaceProtocol = iface->bInterfaceProtocol;
182 d->u.interface.iInterface = iface->iInterface;
37fb59d3
GH
183 pos += bLength;
184
185 for (i = 0; i < iface->ndesc; i++) {
186 rc = usb_desc_other(iface->descs + i, dest + pos, len - pos);
187 if (rc < 0) {
188 return rc;
189 }
190 pos += rc;
191 }
192
193 for (i = 0; i < iface->bNumEndpoints; i++) {
b43a2851 194 rc = usb_desc_endpoint(iface->eps + i, flags, dest + pos, len - pos);
37fb59d3
GH
195 if (rc < 0) {
196 return rc;
197 }
198 pos += rc;
199 }
200
201 return pos;
202}
203
b43a2851
GH
204int usb_desc_endpoint(const USBDescEndpoint *ep, int flags,
205 uint8_t *dest, size_t len)
37fb59d3 206{
cc5f1395
GH
207 uint8_t bLength = ep->is_audio ? 0x09 : 0x07;
208 uint8_t extralen = ep->extra ? ep->extra[0] : 0;
b43a2851 209 uint8_t superlen = (flags & USB_DESC_FLAG_SUPER) ? 0x06 : 0;
e36a20d3 210 USBDescriptor *d = (void *)dest;
37fb59d3 211
b43a2851 212 if (len < bLength + extralen + superlen) {
37fb59d3
GH
213 return -1;
214 }
215
e36a20d3
GH
216 d->bLength = bLength;
217 d->bDescriptorType = USB_DT_ENDPOINT;
218
219 d->u.endpoint.bEndpointAddress = ep->bEndpointAddress;
220 d->u.endpoint.bmAttributes = ep->bmAttributes;
221 d->u.endpoint.wMaxPacketSize_lo = usb_lo(ep->wMaxPacketSize);
222 d->u.endpoint.wMaxPacketSize_hi = usb_hi(ep->wMaxPacketSize);
223 d->u.endpoint.bInterval = ep->bInterval;
cc5f1395 224 if (ep->is_audio) {
e36a20d3
GH
225 d->u.endpoint.bRefresh = ep->bRefresh;
226 d->u.endpoint.bSynchAddress = ep->bSynchAddress;
cc5f1395
GH
227 }
228 if (ep->extra) {
229 memcpy(dest + bLength, ep->extra, extralen);
230 }
37fb59d3 231
b43a2851
GH
232 if (superlen) {
233 USBDescriptor *d = (void *)(dest + bLength + extralen);
234
235 d->bLength = 0x06;
236 d->bDescriptorType = USB_DT_ENDPOINT_COMPANION;
237
238 d->u.super_endpoint.bMaxBurst = ep->bMaxBurst;
239 d->u.super_endpoint.bmAttributes = ep->bmAttributes_super;
240 d->u.super_endpoint.wBytesPerInterval_lo =
241 usb_lo(ep->wBytesPerInterval);
242 d->u.super_endpoint.wBytesPerInterval_hi =
243 usb_hi(ep->wBytesPerInterval);
244 }
245
246 return bLength + extralen + superlen;
37fb59d3
GH
247}
248
249int usb_desc_other(const USBDescOther *desc, uint8_t *dest, size_t len)
250{
251 int bLength = desc->length ? desc->length : desc->data[0];
252
253 if (len < bLength) {
254 return -1;
255 }
256
257 memcpy(dest, desc->data, bLength);
258 return bLength;
259}
260
2077469b
GH
261static int usb_desc_cap_usb2_ext(const USBDesc *desc, uint8_t *dest, size_t len)
262{
263 uint8_t bLength = 0x07;
264 USBDescriptor *d = (void *)dest;
265
266 if (len < bLength) {
267 return -1;
268 }
269
270 d->bLength = bLength;
271 d->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
272 d->u.cap.bDevCapabilityType = USB_DEV_CAP_USB2_EXT;
273
274 d->u.cap.u.usb2_ext.bmAttributes_1 = (1 << 1); /* LPM */
275 d->u.cap.u.usb2_ext.bmAttributes_2 = 0;
276 d->u.cap.u.usb2_ext.bmAttributes_3 = 0;
277 d->u.cap.u.usb2_ext.bmAttributes_4 = 0;
278
279 return bLength;
280}
281
282static int usb_desc_cap_super(const USBDesc *desc, uint8_t *dest, size_t len)
283{
284 uint8_t bLength = 0x0a;
285 USBDescriptor *d = (void *)dest;
286
287 if (len < bLength) {
288 return -1;
289 }
290
291 d->bLength = bLength;
292 d->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
293 d->u.cap.bDevCapabilityType = USB_DEV_CAP_SUPERSPEED;
294
295 d->u.cap.u.super.bmAttributes = 0;
296 d->u.cap.u.super.wSpeedsSupported_lo = 0;
297 d->u.cap.u.super.wSpeedsSupported_hi = 0;
298 d->u.cap.u.super.bFunctionalitySupport = 0;
299 d->u.cap.u.super.bU1DevExitLat = 0x0a;
300 d->u.cap.u.super.wU2DevExitLat_lo = 0x20;
301 d->u.cap.u.super.wU2DevExitLat_hi = 0;
302
303 if (desc->full) {
304 d->u.cap.u.super.wSpeedsSupported_lo |= (1 << 1);
305 d->u.cap.u.super.bFunctionalitySupport = 1;
306 }
307 if (desc->high) {
308 d->u.cap.u.super.wSpeedsSupported_lo |= (1 << 2);
309 if (!d->u.cap.u.super.bFunctionalitySupport) {
310 d->u.cap.u.super.bFunctionalitySupport = 2;
311 }
312 }
313 if (desc->super) {
314 d->u.cap.u.super.wSpeedsSupported_lo |= (1 << 3);
315 if (!d->u.cap.u.super.bFunctionalitySupport) {
316 d->u.cap.u.super.bFunctionalitySupport = 3;
317 }
318 }
319
320 return bLength;
321}
322
323static int usb_desc_bos(const USBDesc *desc, uint8_t *dest, size_t len)
324{
325 uint8_t bLength = 0x05;
326 uint16_t wTotalLength = 0;
327 uint8_t bNumDeviceCaps = 0;
328 USBDescriptor *d = (void *)dest;
329 int rc;
330
331 if (len < bLength) {
332 return -1;
333 }
334
335 d->bLength = bLength;
336 d->bDescriptorType = USB_DT_BOS;
337
338 wTotalLength += bLength;
339
340 if (desc->high != NULL) {
341 rc = usb_desc_cap_usb2_ext(desc, dest + wTotalLength,
342 len - wTotalLength);
343 if (rc < 0) {
344 return rc;
345 }
346 wTotalLength += rc;
347 bNumDeviceCaps++;
348 }
349
350 if (desc->super != NULL) {
351 rc = usb_desc_cap_super(desc, dest + wTotalLength,
352 len - wTotalLength);
353 if (rc < 0) {
354 return rc;
355 }
356 wTotalLength += rc;
357 bNumDeviceCaps++;
358 }
359
360 d->u.bos.wTotalLength_lo = usb_lo(wTotalLength);
361 d->u.bos.wTotalLength_hi = usb_hi(wTotalLength);
362 d->u.bos.bNumDeviceCaps = bNumDeviceCaps;
363 return wTotalLength;
364}
365
132a3f55
GH
366/* ------------------------------------------------------------------ */
367
83a53bbc
GH
368static void usb_desc_ep_init(USBDevice *dev)
369{
370 const USBDescIface *iface;
371 int i, e, pid, ep;
372
373 usb_ep_init(dev);
374 for (i = 0; i < dev->ninterfaces; i++) {
375 iface = dev->ifaces[i];
376 if (iface == NULL) {
377 continue;
378 }
379 for (e = 0; e < iface->bNumEndpoints; e++) {
380 pid = (iface->eps[e].bEndpointAddress & USB_DIR_IN) ?
381 USB_TOKEN_IN : USB_TOKEN_OUT;
382 ep = iface->eps[e].bEndpointAddress & 0x0f;
383 usb_ep_set_type(dev, pid, ep, iface->eps[e].bmAttributes & 0x03);
384 usb_ep_set_ifnum(dev, pid, ep, iface->bInterfaceNumber);
f003397c
GH
385 usb_ep_set_max_packet_size(dev, pid, ep,
386 iface->eps[e].wMaxPacketSize);
83a53bbc
GH
387 }
388 }
389}
390
1de14d43
GH
391static const USBDescIface *usb_desc_find_interface(USBDevice *dev,
392 int nif, int alt)
393{
394 const USBDescIface *iface;
395 int g, i;
396
397 if (!dev->config) {
398 return NULL;
399 }
400 for (g = 0; g < dev->config->nif_groups; g++) {
401 for (i = 0; i < dev->config->if_groups[g].nif; i++) {
402 iface = &dev->config->if_groups[g].ifs[i];
403 if (iface->bInterfaceNumber == nif &&
404 iface->bAlternateSetting == alt) {
405 return iface;
406 }
407 }
408 }
409 for (i = 0; i < dev->config->nif; i++) {
410 iface = &dev->config->ifs[i];
411 if (iface->bInterfaceNumber == nif &&
412 iface->bAlternateSetting == alt) {
413 return iface;
414 }
415 }
416 return NULL;
417}
418
419static int usb_desc_set_interface(USBDevice *dev, int index, int value)
420{
421 const USBDescIface *iface;
422 int old;
423
424 iface = usb_desc_find_interface(dev, index, value);
425 if (iface == NULL) {
426 return -1;
427 }
428
429 old = dev->altsetting[index];
430 dev->altsetting[index] = value;
431 dev->ifaces[index] = iface;
83a53bbc 432 usb_desc_ep_init(dev);
1de14d43 433
62aed765
AL
434 if (old != value) {
435 usb_device_set_interface(dev, index, old, value);
1de14d43
GH
436 }
437 return 0;
438}
439
65360511
GH
440static int usb_desc_set_config(USBDevice *dev, int value)
441{
442 int i;
443
444 if (value == 0) {
445 dev->configuration = 0;
446 dev->ninterfaces = 0;
447 dev->config = NULL;
448 } else {
449 for (i = 0; i < dev->device->bNumConfigurations; i++) {
450 if (dev->device->confs[i].bConfigurationValue == value) {
451 dev->configuration = value;
452 dev->ninterfaces = dev->device->confs[i].bNumInterfaces;
453 dev->config = dev->device->confs + i;
1de14d43 454 assert(dev->ninterfaces <= USB_MAX_INTERFACES);
65360511
GH
455 }
456 }
457 if (i < dev->device->bNumConfigurations) {
458 return -1;
459 }
460 }
1de14d43
GH
461
462 for (i = 0; i < dev->ninterfaces; i++) {
463 usb_desc_set_interface(dev, i, 0);
464 }
465 for (; i < USB_MAX_INTERFACES; i++) {
466 dev->altsetting[i] = 0;
467 dev->ifaces[i] = NULL;
468 }
469
65360511
GH
470 return 0;
471}
472
32d41919 473static void usb_desc_setdefaults(USBDevice *dev)
a980a065 474{
62aed765 475 const USBDesc *desc = usb_device_get_usb_desc(dev);
a980a065
GH
476
477 assert(desc != NULL);
32d41919
GH
478 switch (dev->speed) {
479 case USB_SPEED_LOW:
480 case USB_SPEED_FULL:
481 dev->device = desc->full;
482 break;
483 case USB_SPEED_HIGH:
484 dev->device = desc->high;
485 break;
6d51b2bb
GH
486 case USB_SPEED_SUPER:
487 dev->device = desc->super;
488 break;
32d41919 489 }
65360511 490 usb_desc_set_config(dev, 0);
a980a065
GH
491}
492
32d41919
GH
493void usb_desc_init(USBDevice *dev)
494{
62aed765 495 const USBDesc *desc = usb_device_get_usb_desc(dev);
ba3f9bfb
HG
496
497 assert(desc != NULL);
32d41919 498 dev->speed = USB_SPEED_FULL;
ba3f9bfb
HG
499 dev->speedmask = 0;
500 if (desc->full) {
501 dev->speedmask |= USB_SPEED_MASK_FULL;
502 }
503 if (desc->high) {
504 dev->speedmask |= USB_SPEED_MASK_HIGH;
505 }
6d51b2bb
GH
506 if (desc->super) {
507 dev->speedmask |= USB_SPEED_MASK_SUPER;
508 }
32d41919
GH
509 usb_desc_setdefaults(dev);
510}
511
512void usb_desc_attach(USBDevice *dev)
513{
62aed765 514 const USBDesc *desc = usb_device_get_usb_desc(dev);
32d41919
GH
515
516 assert(desc != NULL);
6d51b2bb
GH
517 if (desc->super && (dev->port->speedmask & USB_SPEED_MASK_SUPER)) {
518 dev->speed = USB_SPEED_SUPER;
519 } else if (desc->high && (dev->port->speedmask & USB_SPEED_MASK_HIGH)) {
32d41919
GH
520 dev->speed = USB_SPEED_HIGH;
521 } else if (desc->full && (dev->port->speedmask & USB_SPEED_MASK_FULL)) {
522 dev->speed = USB_SPEED_FULL;
523 } else {
524 fprintf(stderr, "usb: port/device speed mismatch for \"%s\"\n",
62aed765 525 usb_device_get_product_desc(dev));
32d41919
GH
526 return;
527 }
528 usb_desc_setdefaults(dev);
529}
530
132a3f55
GH
531void usb_desc_set_string(USBDevice *dev, uint8_t index, const char *str)
532{
533 USBDescString *s;
534
535 QLIST_FOREACH(s, &dev->strings, next) {
536 if (s->index == index) {
537 break;
538 }
539 }
540 if (s == NULL) {
7267c094 541 s = g_malloc0(sizeof(*s));
132a3f55
GH
542 s->index = index;
543 QLIST_INSERT_HEAD(&dev->strings, s, next);
544 }
7267c094
AL
545 g_free(s->str);
546 s->str = g_strdup(str);
132a3f55
GH
547}
548
9d55d1ad
GH
549/*
550 * This function creates a serial number for a usb device.
551 * The serial number should:
552 * (a) Be unique within the virtual machine.
553 * (b) Be constant, so you don't get a new one each
554 * time the guest is started.
555 * So we are using the physical location to generate a serial number
556 * from it. It has three pieces: First a fixed, device-specific
557 * prefix. Second the device path of the host controller (which is
558 * the pci address in most cases). Third the physical port path.
559 * Results in serial numbers like this: "314159-0000:00:1d.7-3".
560 */
561void usb_desc_create_serial(USBDevice *dev)
562{
563 DeviceState *hcd = dev->qdev.parent_bus->parent;
564 const USBDesc *desc = usb_device_get_usb_desc(dev);
565 int index = desc->id.iSerialNumber;
566 char serial[64];
09e5ab63 567 char *path;
9d55d1ad
GH
568 int dst;
569
570 assert(index != 0 && desc->str[index] != NULL);
571 dst = snprintf(serial, sizeof(serial), "%s", desc->str[index]);
09e5ab63
AL
572 path = qdev_get_dev_path(hcd);
573 if (path) {
9d55d1ad
GH
574 dst += snprintf(serial+dst, sizeof(serial)-dst, "-%s", path);
575 }
576 dst += snprintf(serial+dst, sizeof(serial)-dst, "-%s", dev->port->path);
577 usb_desc_set_string(dev, index, serial);
578}
579
132a3f55
GH
580const char *usb_desc_get_string(USBDevice *dev, uint8_t index)
581{
582 USBDescString *s;
583
584 QLIST_FOREACH(s, &dev->strings, next) {
585 if (s->index == index) {
586 return s->str;
587 }
588 }
589 return NULL;
590}
591
592int usb_desc_string(USBDevice *dev, int index, uint8_t *dest, size_t len)
37fb59d3
GH
593{
594 uint8_t bLength, pos, i;
132a3f55 595 const char *str;
37fb59d3
GH
596
597 if (len < 4) {
598 return -1;
599 }
600
601 if (index == 0) {
602 /* language ids */
603 dest[0] = 4;
604 dest[1] = USB_DT_STRING;
605 dest[2] = 0x09;
606 dest[3] = 0x04;
607 return 4;
608 }
609
132a3f55
GH
610 str = usb_desc_get_string(dev, index);
611 if (str == NULL) {
62aed765 612 str = usb_device_get_usb_desc(dev)->str[index];
132a3f55
GH
613 if (str == NULL) {
614 return 0;
615 }
37fb59d3 616 }
132a3f55
GH
617
618 bLength = strlen(str) * 2 + 2;
37fb59d3
GH
619 dest[0] = bLength;
620 dest[1] = USB_DT_STRING;
621 i = 0; pos = 2;
622 while (pos+1 < bLength && pos+1 < len) {
132a3f55 623 dest[pos++] = str[i++];
37fb59d3
GH
624 dest[pos++] = 0;
625 }
626 return pos;
627}
628
9a77a0f5
HG
629int usb_desc_get_descriptor(USBDevice *dev, USBPacket *p,
630 int value, uint8_t *dest, size_t len)
37fb59d3 631{
62aed765 632 const USBDesc *desc = usb_device_get_usb_desc(dev);
25620cba 633 const USBDescDevice *other_dev;
37fb59d3
GH
634 uint8_t buf[256];
635 uint8_t type = value >> 8;
636 uint8_t index = value & 0xff;
b43a2851 637 int flags, ret = -1;
37fb59d3 638
25620cba 639 if (dev->speed == USB_SPEED_HIGH) {
62aed765 640 other_dev = usb_device_get_usb_desc(dev)->full;
25620cba 641 } else {
62aed765 642 other_dev = usb_device_get_usb_desc(dev)->high;
25620cba
GH
643 }
644
b43a2851
GH
645 flags = 0;
646 if (dev->device->bcdUSB >= 0x0300) {
647 flags |= USB_DESC_FLAG_SUPER;
648 }
649
37fb59d3
GH
650 switch(type) {
651 case USB_DT_DEVICE:
a980a065 652 ret = usb_desc_device(&desc->id, dev->device, buf, sizeof(buf));
37fb59d3
GH
653 trace_usb_desc_device(dev->addr, len, ret);
654 break;
655 case USB_DT_CONFIG:
a980a065 656 if (index < dev->device->bNumConfigurations) {
b43a2851
GH
657 ret = usb_desc_config(dev->device->confs + index, flags,
658 buf, sizeof(buf));
37fb59d3
GH
659 }
660 trace_usb_desc_config(dev->addr, index, len, ret);
661 break;
662 case USB_DT_STRING:
132a3f55 663 ret = usb_desc_string(dev, index, buf, sizeof(buf));
37fb59d3
GH
664 trace_usb_desc_string(dev->addr, index, len, ret);
665 break;
25620cba
GH
666 case USB_DT_DEVICE_QUALIFIER:
667 if (other_dev != NULL) {
668 ret = usb_desc_device_qualifier(other_dev, buf, sizeof(buf));
669 }
670 trace_usb_desc_device_qualifier(dev->addr, len, ret);
671 break;
672 case USB_DT_OTHER_SPEED_CONFIG:
673 if (other_dev != NULL && index < other_dev->bNumConfigurations) {
b43a2851
GH
674 ret = usb_desc_config(other_dev->confs + index, flags,
675 buf, sizeof(buf));
25620cba
GH
676 buf[0x01] = USB_DT_OTHER_SPEED_CONFIG;
677 }
678 trace_usb_desc_other_speed_config(dev->addr, index, len, ret);
679 break;
2077469b
GH
680 case USB_DT_BOS:
681 ret = usb_desc_bos(desc, buf, sizeof(buf));
682 trace_usb_desc_bos(dev->addr, len, ret);
683 break;
25620cba 684
a7fb71d1
GH
685 case USB_DT_DEBUG:
686 /* ignore silently */
687 break;
688
37fb59d3
GH
689 default:
690 fprintf(stderr, "%s: %d unknown type %d (len %zd)\n", __FUNCTION__,
691 dev->addr, type, len);
692 break;
693 }
694
695 if (ret > 0) {
696 if (ret > len) {
697 ret = len;
698 }
699 memcpy(dest, buf, ret);
9a77a0f5
HG
700 p->actual_length = ret;
701 ret = 0;
37fb59d3
GH
702 }
703 return ret;
704}
705
007fd62f
HG
706int usb_desc_handle_control(USBDevice *dev, USBPacket *p,
707 int request, int value, int index, int length, uint8_t *data)
37fb59d3 708{
62aed765 709 const USBDesc *desc = usb_device_get_usb_desc(dev);
65360511 710 int ret = -1;
37fb59d3
GH
711
712 assert(desc != NULL);
713 switch(request) {
41c6abbd
GH
714 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
715 dev->addr = value;
716 trace_usb_set_addr(dev->addr);
717 ret = 0;
718 break;
719
37fb59d3 720 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
9a77a0f5 721 ret = usb_desc_get_descriptor(dev, p, value, data, length);
37fb59d3 722 break;
a980a065
GH
723
724 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
8db36e9d
AL
725 /*
726 * 9.4.2: 0 should be returned if the device is unconfigured, otherwise
727 * the non zero value of bConfigurationValue.
728 */
729 data[0] = dev->config ? dev->config->bConfigurationValue : 0;
9a77a0f5
HG
730 p->actual_length = 1;
731 ret = 0;
a980a065
GH
732 break;
733 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
65360511 734 ret = usb_desc_set_config(dev, value);
a980a065
GH
735 trace_usb_set_config(dev->addr, value, ret);
736 break;
ed5a83dd 737
8db36e9d
AL
738 case DeviceRequest | USB_REQ_GET_STATUS: {
739 const USBDescConfig *config = dev->config ?
740 dev->config : &dev->device->confs[0];
741
ed5a83dd 742 data[0] = 0;
8db36e9d
AL
743 /*
744 * Default state: Device behavior when this request is received while
745 * the device is in the Default state is not specified.
746 * We return the same value that a configured device would return if
747 * it used the first configuration.
748 */
749 if (config->bmAttributes & 0x40) {
ed5a83dd
GH
750 data[0] |= 1 << USB_DEVICE_SELF_POWERED;
751 }
752 if (dev->remote_wakeup) {
753 data[0] |= 1 << USB_DEVICE_REMOTE_WAKEUP;
754 }
755 data[1] = 0x00;
9a77a0f5
HG
756 p->actual_length = 2;
757 ret = 0;
ed5a83dd 758 break;
8db36e9d 759 }
ed5a83dd
GH
760 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
761 if (value == USB_DEVICE_REMOTE_WAKEUP) {
762 dev->remote_wakeup = 0;
763 ret = 0;
764 }
765 trace_usb_clear_device_feature(dev->addr, value, ret);
766 break;
767 case DeviceOutRequest | USB_REQ_SET_FEATURE:
768 if (value == USB_DEVICE_REMOTE_WAKEUP) {
769 dev->remote_wakeup = 1;
770 ret = 0;
771 }
772 trace_usb_set_device_feature(dev->addr, value, ret);
773 break;
1de14d43
GH
774
775 case InterfaceRequest | USB_REQ_GET_INTERFACE:
776 if (index < 0 || index >= dev->ninterfaces) {
777 break;
778 }
779 data[0] = dev->altsetting[index];
9a77a0f5
HG
780 p->actual_length = 1;
781 ret = 0;
1de14d43
GH
782 break;
783 case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
784 ret = usb_desc_set_interface(dev, index, value);
785 trace_usb_set_interface(dev->addr, index, value, ret);
786 break;
787
37fb59d3
GH
788 }
789 return ret;
790}