blob: b82c397ef9e82bd3ac89228b91200bee4462ab45 [file] [log] [blame]
Gerd Hoffmann9d55d1a2012-04-20 12:33:30 +02001#include <ctype.h>
2
Gerd Hoffmannf1ae32a2012-03-07 14:55:18 +01003#include "hw/usb.h"
4#include "hw/usb/desc.h"
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +01005#include "trace.h"
6
7/* ------------------------------------------------------------------ */
8
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +01009int usb_desc_device(const USBDescID *id, const USBDescDevice *dev,
Gerd Hoffmann5319dc72013-11-20 07:32:31 +010010 bool msos, uint8_t *dest, size_t len)
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +010011{
12 uint8_t bLength = 0x12;
Gerd Hoffmannd3f904e2012-03-29 12:04:54 +020013 USBDescriptor *d = (void *)dest;
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +010014
15 if (len < bLength) {
16 return -1;
17 }
18
Gerd Hoffmannd3f904e2012-03-29 12:04:54 +020019 d->bLength = bLength;
20 d->bDescriptorType = USB_DT_DEVICE;
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +010021
Gerd Hoffmann5319dc72013-11-20 07:32:31 +010022 if (msos && dev->bcdUSB < 0x0200) {
23 /*
24 * Version 2.0+ required for microsoft os descriptors to work.
25 * Done this way so msos-desc compat property will handle both
26 * the version and the new descriptors being present.
27 */
28 d->u.device.bcdUSB_lo = usb_lo(0x0200);
29 d->u.device.bcdUSB_hi = usb_hi(0x0200);
30 } else {
31 d->u.device.bcdUSB_lo = usb_lo(dev->bcdUSB);
32 d->u.device.bcdUSB_hi = usb_hi(dev->bcdUSB);
33 }
Gerd Hoffmannd3f904e2012-03-29 12:04:54 +020034 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;
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +010038
Gerd Hoffmannd3f904e2012-03-29 12:04:54 +020039 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;
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +010048
Gerd Hoffmannd3f904e2012-03-29 12:04:54 +020049 d->u.device.bNumConfigurations = dev->bNumConfigurations;
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +010050
51 return bLength;
52}
53
Gerd Hoffmann25620cb2010-12-08 17:35:22 +010054int usb_desc_device_qualifier(const USBDescDevice *dev,
55 uint8_t *dest, size_t len)
56{
57 uint8_t bLength = 0x0a;
Gerd Hoffmann3cfeee62012-03-29 12:15:01 +020058 USBDescriptor *d = (void *)dest;
Gerd Hoffmann25620cb2010-12-08 17:35:22 +010059
60 if (len < bLength) {
61 return -1;
62 }
63
Gerd Hoffmann3cfeee62012-03-29 12:15:01 +020064 d->bLength = bLength;
65 d->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
Gerd Hoffmann25620cb2010-12-08 17:35:22 +010066
Gerd Hoffmann3cfeee62012-03-29 12:15:01 +020067 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;
Gerd Hoffmann25620cb2010-12-08 17:35:22 +010075
76 return bLength;
77}
78
Gerd Hoffmannb43a2852012-08-28 17:28:03 +020079int usb_desc_config(const USBDescConfig *conf, int flags,
80 uint8_t *dest, size_t len)
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +010081{
82 uint8_t bLength = 0x09;
83 uint16_t wTotalLength = 0;
Gerd Hoffmann0a263db2012-03-29 12:24:08 +020084 USBDescriptor *d = (void *)dest;
Brad Hardsfef13fa2011-04-03 15:33:20 +100085 int i, rc;
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +010086
87 if (len < bLength) {
88 return -1;
89 }
90
Gerd Hoffmann0a263db2012-03-29 12:24:08 +020091 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;
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +010099 wTotalLength += bLength;
100
Gerd Hoffmann0a263db2012-03-29 12:24:08 +0200101 /* handle grouped interfaces if any */
Brad Hards6e625fc2011-04-03 15:33:21 +1000102 for (i = 0; i < conf->nif_groups; i++) {
Gerd Hoffmannb43a2852012-08-28 17:28:03 +0200103 rc = usb_desc_iface_group(&(conf->if_groups[i]), flags,
Brad Hards6e625fc2011-04-03 15:33:21 +1000104 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 */
Brad Hardsfef13fa2011-04-03 15:33:20 +1000113 for (i = 0; i < conf->nif; i++) {
Gerd Hoffmannb43a2852012-08-28 17:28:03 +0200114 rc = usb_desc_iface(conf->ifs + i, flags,
115 dest + wTotalLength, len - wTotalLength);
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +0100116 if (rc < 0) {
117 return rc;
118 }
119 wTotalLength += rc;
120 }
121
Gerd Hoffmann0a263db2012-03-29 12:24:08 +0200122 d->u.config.wTotalLength_lo = usb_lo(wTotalLength);
123 d->u.config.wTotalLength_hi = usb_hi(wTotalLength);
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +0100124 return wTotalLength;
125}
126
Gerd Hoffmannb43a2852012-08-28 17:28:03 +0200127int usb_desc_iface_group(const USBDescIfaceAssoc *iad, int flags,
128 uint8_t *dest, size_t len)
Brad Hards6e625fc2011-04-03 15:33:21 +1000129{
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++) {
Gerd Hoffmannb43a2852012-08-28 17:28:03 +0200152 int rc = usb_desc_iface(&(iad->ifs[i]), flags, dest + pos, len - pos);
Brad Hards6e625fc2011-04-03 15:33:21 +1000153 if (rc < 0) {
154 return rc;
155 }
156 pos += rc;
157 }
158
159 return pos;
160}
161
Gerd Hoffmannb43a2852012-08-28 17:28:03 +0200162int usb_desc_iface(const USBDescIface *iface, int flags,
163 uint8_t *dest, size_t len)
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +0100164{
165 uint8_t bLength = 0x09;
166 int i, rc, pos = 0;
Gerd Hoffmannfeafd792012-03-29 12:30:33 +0200167 USBDescriptor *d = (void *)dest;
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +0100168
169 if (len < bLength) {
170 return -1;
171 }
172
Gerd Hoffmannfeafd792012-03-29 12:30:33 +0200173 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;
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +0100183 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++) {
Gerd Hoffmannb43a2852012-08-28 17:28:03 +0200194 rc = usb_desc_endpoint(iface->eps + i, flags, dest + pos, len - pos);
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +0100195 if (rc < 0) {
196 return rc;
197 }
198 pos += rc;
199 }
200
201 return pos;
202}
203
Gerd Hoffmannb43a2852012-08-28 17:28:03 +0200204int usb_desc_endpoint(const USBDescEndpoint *ep, int flags,
205 uint8_t *dest, size_t len)
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +0100206{
Gerd Hoffmanncc5f1392011-08-10 14:10:04 +0200207 uint8_t bLength = ep->is_audio ? 0x09 : 0x07;
208 uint8_t extralen = ep->extra ? ep->extra[0] : 0;
Gerd Hoffmannb43a2852012-08-28 17:28:03 +0200209 uint8_t superlen = (flags & USB_DESC_FLAG_SUPER) ? 0x06 : 0;
Gerd Hoffmanne36a20d2012-03-29 16:01:21 +0200210 USBDescriptor *d = (void *)dest;
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +0100211
Gerd Hoffmannb43a2852012-08-28 17:28:03 +0200212 if (len < bLength + extralen + superlen) {
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +0100213 return -1;
214 }
215
Gerd Hoffmanne36a20d2012-03-29 16:01:21 +0200216 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;
Gerd Hoffmanncc5f1392011-08-10 14:10:04 +0200224 if (ep->is_audio) {
Gerd Hoffmanne36a20d2012-03-29 16:01:21 +0200225 d->u.endpoint.bRefresh = ep->bRefresh;
226 d->u.endpoint.bSynchAddress = ep->bSynchAddress;
Gerd Hoffmanncc5f1392011-08-10 14:10:04 +0200227 }
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +0100228
Gerd Hoffmannb43a2852012-08-28 17:28:03 +0200229 if (superlen) {
Gerd Hoffmann2e5df362013-01-28 15:52:57 +0100230 USBDescriptor *d = (void *)(dest + bLength);
Gerd Hoffmannb43a2852012-08-28 17:28:03 +0200231
232 d->bLength = 0x06;
233 d->bDescriptorType = USB_DT_ENDPOINT_COMPANION;
234
235 d->u.super_endpoint.bMaxBurst = ep->bMaxBurst;
236 d->u.super_endpoint.bmAttributes = ep->bmAttributes_super;
237 d->u.super_endpoint.wBytesPerInterval_lo =
238 usb_lo(ep->wBytesPerInterval);
239 d->u.super_endpoint.wBytesPerInterval_hi =
240 usb_hi(ep->wBytesPerInterval);
241 }
242
Gerd Hoffmann2e5df362013-01-28 15:52:57 +0100243 if (ep->extra) {
244 memcpy(dest + bLength + superlen, ep->extra, extralen);
245 }
246
Gerd Hoffmannb43a2852012-08-28 17:28:03 +0200247 return bLength + extralen + superlen;
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +0100248}
249
250int usb_desc_other(const USBDescOther *desc, uint8_t *dest, size_t len)
251{
252 int bLength = desc->length ? desc->length : desc->data[0];
253
254 if (len < bLength) {
255 return -1;
256 }
257
258 memcpy(dest, desc->data, bLength);
259 return bLength;
260}
261
Gerd Hoffmann20774692012-08-28 17:46:29 +0200262static int usb_desc_cap_usb2_ext(const USBDesc *desc, uint8_t *dest, size_t len)
263{
264 uint8_t bLength = 0x07;
265 USBDescriptor *d = (void *)dest;
266
267 if (len < bLength) {
268 return -1;
269 }
270
271 d->bLength = bLength;
272 d->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
273 d->u.cap.bDevCapabilityType = USB_DEV_CAP_USB2_EXT;
274
275 d->u.cap.u.usb2_ext.bmAttributes_1 = (1 << 1); /* LPM */
276 d->u.cap.u.usb2_ext.bmAttributes_2 = 0;
277 d->u.cap.u.usb2_ext.bmAttributes_3 = 0;
278 d->u.cap.u.usb2_ext.bmAttributes_4 = 0;
279
280 return bLength;
281}
282
283static int usb_desc_cap_super(const USBDesc *desc, uint8_t *dest, size_t len)
284{
285 uint8_t bLength = 0x0a;
286 USBDescriptor *d = (void *)dest;
287
288 if (len < bLength) {
289 return -1;
290 }
291
292 d->bLength = bLength;
293 d->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
294 d->u.cap.bDevCapabilityType = USB_DEV_CAP_SUPERSPEED;
295
296 d->u.cap.u.super.bmAttributes = 0;
297 d->u.cap.u.super.wSpeedsSupported_lo = 0;
298 d->u.cap.u.super.wSpeedsSupported_hi = 0;
299 d->u.cap.u.super.bFunctionalitySupport = 0;
300 d->u.cap.u.super.bU1DevExitLat = 0x0a;
301 d->u.cap.u.super.wU2DevExitLat_lo = 0x20;
302 d->u.cap.u.super.wU2DevExitLat_hi = 0;
303
304 if (desc->full) {
305 d->u.cap.u.super.wSpeedsSupported_lo |= (1 << 1);
306 d->u.cap.u.super.bFunctionalitySupport = 1;
307 }
308 if (desc->high) {
309 d->u.cap.u.super.wSpeedsSupported_lo |= (1 << 2);
310 if (!d->u.cap.u.super.bFunctionalitySupport) {
311 d->u.cap.u.super.bFunctionalitySupport = 2;
312 }
313 }
314 if (desc->super) {
315 d->u.cap.u.super.wSpeedsSupported_lo |= (1 << 3);
316 if (!d->u.cap.u.super.bFunctionalitySupport) {
317 d->u.cap.u.super.bFunctionalitySupport = 3;
318 }
319 }
320
321 return bLength;
322}
323
324static int usb_desc_bos(const USBDesc *desc, uint8_t *dest, size_t len)
325{
326 uint8_t bLength = 0x05;
327 uint16_t wTotalLength = 0;
328 uint8_t bNumDeviceCaps = 0;
329 USBDescriptor *d = (void *)dest;
330 int rc;
331
332 if (len < bLength) {
333 return -1;
334 }
335
336 d->bLength = bLength;
337 d->bDescriptorType = USB_DT_BOS;
338
339 wTotalLength += bLength;
340
341 if (desc->high != NULL) {
342 rc = usb_desc_cap_usb2_ext(desc, dest + wTotalLength,
343 len - wTotalLength);
344 if (rc < 0) {
345 return rc;
346 }
347 wTotalLength += rc;
348 bNumDeviceCaps++;
349 }
350
351 if (desc->super != NULL) {
352 rc = usb_desc_cap_super(desc, dest + wTotalLength,
353 len - wTotalLength);
354 if (rc < 0) {
355 return rc;
356 }
357 wTotalLength += rc;
358 bNumDeviceCaps++;
359 }
360
361 d->u.bos.wTotalLength_lo = usb_lo(wTotalLength);
362 d->u.bos.wTotalLength_hi = usb_hi(wTotalLength);
363 d->u.bos.bNumDeviceCaps = bNumDeviceCaps;
364 return wTotalLength;
365}
366
Gerd Hoffmann132a3f52010-11-26 12:25:32 +0100367/* ------------------------------------------------------------------ */
368
Gerd Hoffmann83a53bb2011-08-30 16:42:03 +0200369static void usb_desc_ep_init(USBDevice *dev)
370{
371 const USBDescIface *iface;
372 int i, e, pid, ep;
373
374 usb_ep_init(dev);
375 for (i = 0; i < dev->ninterfaces; i++) {
376 iface = dev->ifaces[i];
377 if (iface == NULL) {
378 continue;
379 }
380 for (e = 0; e < iface->bNumEndpoints; e++) {
381 pid = (iface->eps[e].bEndpointAddress & USB_DIR_IN) ?
382 USB_TOKEN_IN : USB_TOKEN_OUT;
383 ep = iface->eps[e].bEndpointAddress & 0x0f;
384 usb_ep_set_type(dev, pid, ep, iface->eps[e].bmAttributes & 0x03);
385 usb_ep_set_ifnum(dev, pid, ep, iface->bInterfaceNumber);
Gerd Hoffmannf0033972011-08-31 16:09:27 +0200386 usb_ep_set_max_packet_size(dev, pid, ep,
387 iface->eps[e].wMaxPacketSize);
Hans de Goede04b300f2013-11-19 14:36:56 +0100388 usb_ep_set_max_streams(dev, pid, ep,
389 iface->eps[e].bmAttributes_super);
Gerd Hoffmann83a53bb2011-08-30 16:42:03 +0200390 }
391 }
392}
393
Gerd Hoffmann1de14d42011-08-30 13:21:27 +0200394static const USBDescIface *usb_desc_find_interface(USBDevice *dev,
395 int nif, int alt)
396{
397 const USBDescIface *iface;
398 int g, i;
399
400 if (!dev->config) {
401 return NULL;
402 }
403 for (g = 0; g < dev->config->nif_groups; g++) {
404 for (i = 0; i < dev->config->if_groups[g].nif; i++) {
405 iface = &dev->config->if_groups[g].ifs[i];
406 if (iface->bInterfaceNumber == nif &&
407 iface->bAlternateSetting == alt) {
408 return iface;
409 }
410 }
411 }
412 for (i = 0; i < dev->config->nif; i++) {
413 iface = &dev->config->ifs[i];
414 if (iface->bInterfaceNumber == nif &&
415 iface->bAlternateSetting == alt) {
416 return iface;
417 }
418 }
419 return NULL;
420}
421
422static int usb_desc_set_interface(USBDevice *dev, int index, int value)
423{
424 const USBDescIface *iface;
425 int old;
426
427 iface = usb_desc_find_interface(dev, index, value);
428 if (iface == NULL) {
429 return -1;
430 }
431
432 old = dev->altsetting[index];
433 dev->altsetting[index] = value;
434 dev->ifaces[index] = iface;
Gerd Hoffmann83a53bb2011-08-30 16:42:03 +0200435 usb_desc_ep_init(dev);
Gerd Hoffmann1de14d42011-08-30 13:21:27 +0200436
Anthony Liguori62aed762011-12-15 14:53:10 -0600437 if (old != value) {
438 usb_device_set_interface(dev, index, old, value);
Gerd Hoffmann1de14d42011-08-30 13:21:27 +0200439 }
440 return 0;
441}
442
Gerd Hoffmann65360512011-08-30 11:11:29 +0200443static int usb_desc_set_config(USBDevice *dev, int value)
444{
445 int i;
446
447 if (value == 0) {
448 dev->configuration = 0;
449 dev->ninterfaces = 0;
450 dev->config = NULL;
451 } else {
452 for (i = 0; i < dev->device->bNumConfigurations; i++) {
453 if (dev->device->confs[i].bConfigurationValue == value) {
454 dev->configuration = value;
455 dev->ninterfaces = dev->device->confs[i].bNumInterfaces;
456 dev->config = dev->device->confs + i;
Gerd Hoffmann1de14d42011-08-30 13:21:27 +0200457 assert(dev->ninterfaces <= USB_MAX_INTERFACES);
Gerd Hoffmann65360512011-08-30 11:11:29 +0200458 }
459 }
460 if (i < dev->device->bNumConfigurations) {
461 return -1;
462 }
463 }
Gerd Hoffmann1de14d42011-08-30 13:21:27 +0200464
465 for (i = 0; i < dev->ninterfaces; i++) {
466 usb_desc_set_interface(dev, i, 0);
467 }
468 for (; i < USB_MAX_INTERFACES; i++) {
469 dev->altsetting[i] = 0;
470 dev->ifaces[i] = NULL;
471 }
472
Gerd Hoffmann65360512011-08-30 11:11:29 +0200473 return 0;
474}
475
Gerd Hoffmann32d41912010-12-03 18:07:20 +0100476static void usb_desc_setdefaults(USBDevice *dev)
Gerd Hoffmanna980a062010-11-26 20:20:41 +0100477{
Anthony Liguori62aed762011-12-15 14:53:10 -0600478 const USBDesc *desc = usb_device_get_usb_desc(dev);
Gerd Hoffmanna980a062010-11-26 20:20:41 +0100479
480 assert(desc != NULL);
Gerd Hoffmann32d41912010-12-03 18:07:20 +0100481 switch (dev->speed) {
482 case USB_SPEED_LOW:
483 case USB_SPEED_FULL:
484 dev->device = desc->full;
485 break;
486 case USB_SPEED_HIGH:
487 dev->device = desc->high;
488 break;
Gerd Hoffmann6d51b2b2012-08-28 17:28:50 +0200489 case USB_SPEED_SUPER:
490 dev->device = desc->super;
491 break;
Gerd Hoffmann32d41912010-12-03 18:07:20 +0100492 }
Gerd Hoffmann65360512011-08-30 11:11:29 +0200493 usb_desc_set_config(dev, 0);
Gerd Hoffmanna980a062010-11-26 20:20:41 +0100494}
495
Gerd Hoffmann32d41912010-12-03 18:07:20 +0100496void usb_desc_init(USBDevice *dev)
497{
Anthony Liguori62aed762011-12-15 14:53:10 -0600498 const USBDesc *desc = usb_device_get_usb_desc(dev);
Hans de Goedeba3f9bf2011-05-27 14:27:18 +0200499
500 assert(desc != NULL);
Gerd Hoffmann32d41912010-12-03 18:07:20 +0100501 dev->speed = USB_SPEED_FULL;
Hans de Goedeba3f9bf2011-05-27 14:27:18 +0200502 dev->speedmask = 0;
503 if (desc->full) {
504 dev->speedmask |= USB_SPEED_MASK_FULL;
505 }
506 if (desc->high) {
507 dev->speedmask |= USB_SPEED_MASK_HIGH;
508 }
Gerd Hoffmann6d51b2b2012-08-28 17:28:50 +0200509 if (desc->super) {
510 dev->speedmask |= USB_SPEED_MASK_SUPER;
511 }
Gerd Hoffmann5319dc72013-11-20 07:32:31 +0100512 if (desc->msos && (dev->flags & (1 << USB_DEV_FLAG_MSOS_DESC_ENABLE))) {
513 dev->flags |= (1 << USB_DEV_FLAG_MSOS_DESC_IN_USE);
514 usb_desc_set_string(dev, 0xee, "MSFT100Q");
515 }
Gerd Hoffmann32d41912010-12-03 18:07:20 +0100516 usb_desc_setdefaults(dev);
517}
518
519void usb_desc_attach(USBDevice *dev)
520{
Gerd Hoffmann32d41912010-12-03 18:07:20 +0100521 usb_desc_setdefaults(dev);
522}
523
Gerd Hoffmann132a3f52010-11-26 12:25:32 +0100524void usb_desc_set_string(USBDevice *dev, uint8_t index, const char *str)
525{
526 USBDescString *s;
527
528 QLIST_FOREACH(s, &dev->strings, next) {
529 if (s->index == index) {
530 break;
531 }
532 }
533 if (s == NULL) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500534 s = g_malloc0(sizeof(*s));
Gerd Hoffmann132a3f52010-11-26 12:25:32 +0100535 s->index = index;
536 QLIST_INSERT_HEAD(&dev->strings, s, next);
537 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500538 g_free(s->str);
539 s->str = g_strdup(str);
Gerd Hoffmann132a3f52010-11-26 12:25:32 +0100540}
541
Gerd Hoffmann9d55d1a2012-04-20 12:33:30 +0200542/*
543 * This function creates a serial number for a usb device.
544 * The serial number should:
545 * (a) Be unique within the virtual machine.
546 * (b) Be constant, so you don't get a new one each
547 * time the guest is started.
548 * So we are using the physical location to generate a serial number
549 * from it. It has three pieces: First a fixed, device-specific
550 * prefix. Second the device path of the host controller (which is
551 * the pci address in most cases). Third the physical port path.
552 * Results in serial numbers like this: "314159-0000:00:1d.7-3".
553 */
554void usb_desc_create_serial(USBDevice *dev)
555{
556 DeviceState *hcd = dev->qdev.parent_bus->parent;
557 const USBDesc *desc = usb_device_get_usb_desc(dev);
558 int index = desc->id.iSerialNumber;
559 char serial[64];
Anthony Liguori09e5ab62012-02-03 12:28:43 -0600560 char *path;
Gerd Hoffmann9d55d1a2012-04-20 12:33:30 +0200561 int dst;
562
Gerd Hoffmann71938a02013-06-12 13:01:49 +0200563 if (dev->serial) {
564 /* 'serial' usb bus property has priority if present */
565 usb_desc_set_string(dev, index, dev->serial);
566 return;
567 }
568
Gerd Hoffmann9d55d1a2012-04-20 12:33:30 +0200569 assert(index != 0 && desc->str[index] != NULL);
570 dst = snprintf(serial, sizeof(serial), "%s", desc->str[index]);
Anthony Liguori09e5ab62012-02-03 12:28:43 -0600571 path = qdev_get_dev_path(hcd);
572 if (path) {
Gerd Hoffmann9d55d1a2012-04-20 12:33:30 +0200573 dst += snprintf(serial+dst, sizeof(serial)-dst, "-%s", path);
574 }
575 dst += snprintf(serial+dst, sizeof(serial)-dst, "-%s", dev->port->path);
576 usb_desc_set_string(dev, index, serial);
577}
578
Gerd Hoffmann132a3f52010-11-26 12:25:32 +0100579const char *usb_desc_get_string(USBDevice *dev, uint8_t index)
580{
581 USBDescString *s;
582
583 QLIST_FOREACH(s, &dev->strings, next) {
584 if (s->index == index) {
585 return s->str;
586 }
587 }
588 return NULL;
589}
590
591int usb_desc_string(USBDevice *dev, int index, uint8_t *dest, size_t len)
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +0100592{
593 uint8_t bLength, pos, i;
Gerd Hoffmann132a3f52010-11-26 12:25:32 +0100594 const char *str;
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +0100595
596 if (len < 4) {
597 return -1;
598 }
599
600 if (index == 0) {
601 /* language ids */
602 dest[0] = 4;
603 dest[1] = USB_DT_STRING;
604 dest[2] = 0x09;
605 dest[3] = 0x04;
606 return 4;
607 }
608
Gerd Hoffmann132a3f52010-11-26 12:25:32 +0100609 str = usb_desc_get_string(dev, index);
610 if (str == NULL) {
Anthony Liguori62aed762011-12-15 14:53:10 -0600611 str = usb_device_get_usb_desc(dev)->str[index];
Gerd Hoffmann132a3f52010-11-26 12:25:32 +0100612 if (str == NULL) {
613 return 0;
614 }
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +0100615 }
Gerd Hoffmann132a3f52010-11-26 12:25:32 +0100616
617 bLength = strlen(str) * 2 + 2;
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +0100618 dest[0] = bLength;
619 dest[1] = USB_DT_STRING;
620 i = 0; pos = 2;
621 while (pos+1 < bLength && pos+1 < len) {
Gerd Hoffmann132a3f52010-11-26 12:25:32 +0100622 dest[pos++] = str[i++];
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +0100623 dest[pos++] = 0;
624 }
625 return pos;
626}
627
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100628int usb_desc_get_descriptor(USBDevice *dev, USBPacket *p,
629 int value, uint8_t *dest, size_t len)
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +0100630{
Gerd Hoffmann5319dc72013-11-20 07:32:31 +0100631 bool msos = (dev->flags & (1 << USB_DEV_FLAG_MSOS_DESC_IN_USE));
Anthony Liguori62aed762011-12-15 14:53:10 -0600632 const USBDesc *desc = usb_device_get_usb_desc(dev);
Gerd Hoffmann25620cb2010-12-08 17:35:22 +0100633 const USBDescDevice *other_dev;
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +0100634 uint8_t buf[256];
635 uint8_t type = value >> 8;
636 uint8_t index = value & 0xff;
Gerd Hoffmannb43a2852012-08-28 17:28:03 +0200637 int flags, ret = -1;
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +0100638
Gerd Hoffmann25620cb2010-12-08 17:35:22 +0100639 if (dev->speed == USB_SPEED_HIGH) {
Anthony Liguori62aed762011-12-15 14:53:10 -0600640 other_dev = usb_device_get_usb_desc(dev)->full;
Gerd Hoffmann25620cb2010-12-08 17:35:22 +0100641 } else {
Anthony Liguori62aed762011-12-15 14:53:10 -0600642 other_dev = usb_device_get_usb_desc(dev)->high;
Gerd Hoffmann25620cb2010-12-08 17:35:22 +0100643 }
644
Gerd Hoffmannb43a2852012-08-28 17:28:03 +0200645 flags = 0;
646 if (dev->device->bcdUSB >= 0x0300) {
647 flags |= USB_DESC_FLAG_SUPER;
648 }
649
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +0100650 switch(type) {
651 case USB_DT_DEVICE:
Gerd Hoffmann5319dc72013-11-20 07:32:31 +0100652 ret = usb_desc_device(&desc->id, dev->device, msos, buf, sizeof(buf));
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +0100653 trace_usb_desc_device(dev->addr, len, ret);
654 break;
655 case USB_DT_CONFIG:
Gerd Hoffmanna980a062010-11-26 20:20:41 +0100656 if (index < dev->device->bNumConfigurations) {
Gerd Hoffmannb43a2852012-08-28 17:28:03 +0200657 ret = usb_desc_config(dev->device->confs + index, flags,
658 buf, sizeof(buf));
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +0100659 }
660 trace_usb_desc_config(dev->addr, index, len, ret);
661 break;
662 case USB_DT_STRING:
Gerd Hoffmann132a3f52010-11-26 12:25:32 +0100663 ret = usb_desc_string(dev, index, buf, sizeof(buf));
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +0100664 trace_usb_desc_string(dev->addr, index, len, ret);
665 break;
Gerd Hoffmann25620cb2010-12-08 17:35:22 +0100666 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) {
Gerd Hoffmannb43a2852012-08-28 17:28:03 +0200674 ret = usb_desc_config(other_dev->confs + index, flags,
675 buf, sizeof(buf));
Gerd Hoffmann25620cb2010-12-08 17:35:22 +0100676 buf[0x01] = USB_DT_OTHER_SPEED_CONFIG;
677 }
678 trace_usb_desc_other_speed_config(dev->addr, index, len, ret);
679 break;
Gerd Hoffmann20774692012-08-28 17:46:29 +0200680 case USB_DT_BOS:
681 ret = usb_desc_bos(desc, buf, sizeof(buf));
682 trace_usb_desc_bos(dev->addr, len, ret);
683 break;
Gerd Hoffmann25620cb2010-12-08 17:35:22 +0100684
Gerd Hoffmanna7fb71d2011-06-23 17:15:43 +0200685 case USB_DT_DEBUG:
686 /* ignore silently */
687 break;
688
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +0100689 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);
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100700 p->actual_length = ret;
701 ret = 0;
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +0100702 }
703 return ret;
704}
705
Hans de Goede007fd622011-02-02 16:33:13 +0100706int usb_desc_handle_control(USBDevice *dev, USBPacket *p,
707 int request, int value, int index, int length, uint8_t *data)
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +0100708{
Gerd Hoffmann5319dc72013-11-20 07:32:31 +0100709 bool msos = (dev->flags & (1 << USB_DEV_FLAG_MSOS_DESC_IN_USE));
Anthony Liguori62aed762011-12-15 14:53:10 -0600710 const USBDesc *desc = usb_device_get_usb_desc(dev);
Gerd Hoffmann65360512011-08-30 11:11:29 +0200711 int ret = -1;
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +0100712
713 assert(desc != NULL);
714 switch(request) {
Gerd Hoffmann41c6abb2010-11-26 12:35:10 +0100715 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
716 dev->addr = value;
717 trace_usb_set_addr(dev->addr);
718 ret = 0;
719 break;
720
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +0100721 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100722 ret = usb_desc_get_descriptor(dev, p, value, data, length);
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +0100723 break;
Gerd Hoffmanna980a062010-11-26 20:20:41 +0100724
725 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
Alon Levy8db36e92012-02-26 17:09:21 +0100726 /*
727 * 9.4.2: 0 should be returned if the device is unconfigured, otherwise
728 * the non zero value of bConfigurationValue.
729 */
730 data[0] = dev->config ? dev->config->bConfigurationValue : 0;
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100731 p->actual_length = 1;
732 ret = 0;
Gerd Hoffmanna980a062010-11-26 20:20:41 +0100733 break;
734 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
Gerd Hoffmann65360512011-08-30 11:11:29 +0200735 ret = usb_desc_set_config(dev, value);
Gerd Hoffmanna980a062010-11-26 20:20:41 +0100736 trace_usb_set_config(dev->addr, value, ret);
737 break;
Gerd Hoffmanned5a83d2010-11-30 17:35:34 +0100738
Alon Levy8db36e92012-02-26 17:09:21 +0100739 case DeviceRequest | USB_REQ_GET_STATUS: {
740 const USBDescConfig *config = dev->config ?
741 dev->config : &dev->device->confs[0];
742
Gerd Hoffmanned5a83d2010-11-30 17:35:34 +0100743 data[0] = 0;
Alon Levy8db36e92012-02-26 17:09:21 +0100744 /*
745 * Default state: Device behavior when this request is received while
746 * the device is in the Default state is not specified.
747 * We return the same value that a configured device would return if
748 * it used the first configuration.
749 */
Pantelis Koukousoulasbd939762013-12-16 09:42:49 +0200750 if (config->bmAttributes & USB_CFG_ATT_SELFPOWER) {
Gerd Hoffmanned5a83d2010-11-30 17:35:34 +0100751 data[0] |= 1 << USB_DEVICE_SELF_POWERED;
752 }
753 if (dev->remote_wakeup) {
754 data[0] |= 1 << USB_DEVICE_REMOTE_WAKEUP;
755 }
756 data[1] = 0x00;
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100757 p->actual_length = 2;
758 ret = 0;
Gerd Hoffmanned5a83d2010-11-30 17:35:34 +0100759 break;
Alon Levy8db36e92012-02-26 17:09:21 +0100760 }
Gerd Hoffmanned5a83d2010-11-30 17:35:34 +0100761 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
762 if (value == USB_DEVICE_REMOTE_WAKEUP) {
763 dev->remote_wakeup = 0;
764 ret = 0;
765 }
766 trace_usb_clear_device_feature(dev->addr, value, ret);
767 break;
768 case DeviceOutRequest | USB_REQ_SET_FEATURE:
769 if (value == USB_DEVICE_REMOTE_WAKEUP) {
770 dev->remote_wakeup = 1;
771 ret = 0;
772 }
773 trace_usb_set_device_feature(dev->addr, value, ret);
774 break;
Gerd Hoffmann1de14d42011-08-30 13:21:27 +0200775
776 case InterfaceRequest | USB_REQ_GET_INTERFACE:
777 if (index < 0 || index >= dev->ninterfaces) {
778 break;
779 }
780 data[0] = dev->altsetting[index];
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100781 p->actual_length = 1;
782 ret = 0;
Gerd Hoffmann1de14d42011-08-30 13:21:27 +0200783 break;
784 case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
785 ret = usb_desc_set_interface(dev, index, value);
786 trace_usb_set_interface(dev->addr, index, value, ret);
787 break;
788
Gerd Hoffmann5319dc72013-11-20 07:32:31 +0100789 case VendorDeviceRequest | 'Q':
790 if (msos) {
791 ret = usb_desc_msos(desc, p, index, data, length);
792 trace_usb_desc_msos(dev->addr, index, length, ret);
793 }
794 break;
795 case VendorInterfaceRequest | 'Q':
796 if (msos) {
797 ret = usb_desc_msos(desc, p, index, data, length);
798 trace_usb_desc_msos(dev->addr, index, length, ret);
799 }
800 break;
801
Gerd Hoffmann37fb59d2010-11-17 11:03:53 +0100802 }
803 return ret;
804}