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