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