]>
Commit | Line | Data |
---|---|---|
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 | ||
37fb59d3 | 9 | int usb_desc_device(const USBDescID *id, const USBDescDevice *dev, |
5319dc7b | 10 | bool msos, uint8_t *dest, size_t len) |
37fb59d3 GH |
11 | { |
12 | uint8_t bLength = 0x12; | |
d3f904ea | 13 | USBDescriptor *d = (void *)dest; |
37fb59d3 GH |
14 | |
15 | if (len < bLength) { | |
16 | return -1; | |
17 | } | |
18 | ||
d3f904ea GH |
19 | d->bLength = bLength; |
20 | d->bDescriptorType = USB_DT_DEVICE; | |
37fb59d3 | 21 | |
5319dc7b GH |
22 | 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 | } | |
d3f904ea GH |
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 |
54 | int 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 |
79 | int 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 |
127 | int 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 |
162 | int 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 |
204 | int 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 | 227 | } |
37fb59d3 | 228 | |
b43a2851 | 229 | if (superlen) { |
2e5df36d | 230 | USBDescriptor *d = (void *)(dest + bLength); |
b43a2851 GH |
231 | |
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 | ||
2e5df36d GH |
243 | if (ep->extra) { |
244 | memcpy(dest + bLength + superlen, ep->extra, extralen); | |
245 | } | |
246 | ||
b43a2851 | 247 | return bLength + extralen + superlen; |
37fb59d3 GH |
248 | } |
249 | ||
250 | int 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 | ||
2077469b GH |
262 | static 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 | ||
283 | static 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 | ||
324 | static 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 | ||
132a3f55 GH |
367 | /* ------------------------------------------------------------------ */ |
368 | ||
83a53bbc GH |
369 | static 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); | |
f003397c GH |
386 | usb_ep_set_max_packet_size(dev, pid, ep, |
387 | iface->eps[e].wMaxPacketSize); | |
04b300f8 HG |
388 | usb_ep_set_max_streams(dev, pid, ep, |
389 | iface->eps[e].bmAttributes_super); | |
83a53bbc GH |
390 | } |
391 | } | |
392 | } | |
393 | ||
1de14d43 GH |
394 | static 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 | ||
422 | static 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; | |
83a53bbc | 435 | usb_desc_ep_init(dev); |
1de14d43 | 436 | |
62aed765 AL |
437 | if (old != value) { |
438 | usb_device_set_interface(dev, index, old, value); | |
1de14d43 GH |
439 | } |
440 | return 0; | |
441 | } | |
442 | ||
65360511 GH |
443 | static 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; | |
1de14d43 | 457 | assert(dev->ninterfaces <= USB_MAX_INTERFACES); |
65360511 GH |
458 | } |
459 | } | |
460 | if (i < dev->device->bNumConfigurations) { | |
461 | return -1; | |
462 | } | |
463 | } | |
1de14d43 GH |
464 | |
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 | ||
65360511 GH |
473 | return 0; |
474 | } | |
475 | ||
32d41919 | 476 | static void usb_desc_setdefaults(USBDevice *dev) |
a980a065 | 477 | { |
62aed765 | 478 | const USBDesc *desc = usb_device_get_usb_desc(dev); |
a980a065 GH |
479 | |
480 | assert(desc != NULL); | |
32d41919 GH |
481 | 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; | |
6d51b2bb GH |
489 | case USB_SPEED_SUPER: |
490 | dev->device = desc->super; | |
491 | break; | |
32d41919 | 492 | } |
65360511 | 493 | usb_desc_set_config(dev, 0); |
a980a065 GH |
494 | } |
495 | ||
32d41919 GH |
496 | void usb_desc_init(USBDevice *dev) |
497 | { | |
62aed765 | 498 | const USBDesc *desc = usb_device_get_usb_desc(dev); |
ba3f9bfb HG |
499 | |
500 | assert(desc != NULL); | |
32d41919 | 501 | dev->speed = USB_SPEED_FULL; |
ba3f9bfb HG |
502 | 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 | } | |
6d51b2bb GH |
509 | if (desc->super) { |
510 | dev->speedmask |= USB_SPEED_MASK_SUPER; | |
511 | } | |
5319dc7b GH |
512 | 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 | } | |
32d41919 GH |
516 | usb_desc_setdefaults(dev); |
517 | } | |
518 | ||
519 | void usb_desc_attach(USBDevice *dev) | |
520 | { | |
62aed765 | 521 | const USBDesc *desc = usb_device_get_usb_desc(dev); |
32d41919 GH |
522 | |
523 | assert(desc != NULL); | |
6d51b2bb GH |
524 | if (desc->super && (dev->port->speedmask & USB_SPEED_MASK_SUPER)) { |
525 | dev->speed = USB_SPEED_SUPER; | |
526 | } else if (desc->high && (dev->port->speedmask & USB_SPEED_MASK_HIGH)) { | |
32d41919 GH |
527 | dev->speed = USB_SPEED_HIGH; |
528 | } else if (desc->full && (dev->port->speedmask & USB_SPEED_MASK_FULL)) { | |
529 | dev->speed = USB_SPEED_FULL; | |
530 | } else { | |
32d41919 GH |
531 | return; |
532 | } | |
533 | usb_desc_setdefaults(dev); | |
534 | } | |
535 | ||
132a3f55 GH |
536 | void usb_desc_set_string(USBDevice *dev, uint8_t index, const char *str) |
537 | { | |
538 | USBDescString *s; | |
539 | ||
540 | QLIST_FOREACH(s, &dev->strings, next) { | |
541 | if (s->index == index) { | |
542 | break; | |
543 | } | |
544 | } | |
545 | if (s == NULL) { | |
7267c094 | 546 | s = g_malloc0(sizeof(*s)); |
132a3f55 GH |
547 | s->index = index; |
548 | QLIST_INSERT_HEAD(&dev->strings, s, next); | |
549 | } | |
7267c094 AL |
550 | g_free(s->str); |
551 | s->str = g_strdup(str); | |
132a3f55 GH |
552 | } |
553 | ||
9d55d1ad GH |
554 | /* |
555 | * This function creates a serial number for a usb device. | |
556 | * The serial number should: | |
557 | * (a) Be unique within the virtual machine. | |
558 | * (b) Be constant, so you don't get a new one each | |
559 | * time the guest is started. | |
560 | * So we are using the physical location to generate a serial number | |
561 | * from it. It has three pieces: First a fixed, device-specific | |
562 | * prefix. Second the device path of the host controller (which is | |
563 | * the pci address in most cases). Third the physical port path. | |
564 | * Results in serial numbers like this: "314159-0000:00:1d.7-3". | |
565 | */ | |
566 | void usb_desc_create_serial(USBDevice *dev) | |
567 | { | |
568 | DeviceState *hcd = dev->qdev.parent_bus->parent; | |
569 | const USBDesc *desc = usb_device_get_usb_desc(dev); | |
570 | int index = desc->id.iSerialNumber; | |
571 | char serial[64]; | |
09e5ab63 | 572 | char *path; |
9d55d1ad GH |
573 | int dst; |
574 | ||
71938a09 GH |
575 | if (dev->serial) { |
576 | /* 'serial' usb bus property has priority if present */ | |
577 | usb_desc_set_string(dev, index, dev->serial); | |
578 | return; | |
579 | } | |
580 | ||
9d55d1ad GH |
581 | assert(index != 0 && desc->str[index] != NULL); |
582 | dst = snprintf(serial, sizeof(serial), "%s", desc->str[index]); | |
09e5ab63 AL |
583 | path = qdev_get_dev_path(hcd); |
584 | if (path) { | |
9d55d1ad GH |
585 | dst += snprintf(serial+dst, sizeof(serial)-dst, "-%s", path); |
586 | } | |
587 | dst += snprintf(serial+dst, sizeof(serial)-dst, "-%s", dev->port->path); | |
588 | usb_desc_set_string(dev, index, serial); | |
589 | } | |
590 | ||
132a3f55 GH |
591 | const char *usb_desc_get_string(USBDevice *dev, uint8_t index) |
592 | { | |
593 | USBDescString *s; | |
594 | ||
595 | QLIST_FOREACH(s, &dev->strings, next) { | |
596 | if (s->index == index) { | |
597 | return s->str; | |
598 | } | |
599 | } | |
600 | return NULL; | |
601 | } | |
602 | ||
603 | int usb_desc_string(USBDevice *dev, int index, uint8_t *dest, size_t len) | |
37fb59d3 GH |
604 | { |
605 | uint8_t bLength, pos, i; | |
132a3f55 | 606 | const char *str; |
37fb59d3 GH |
607 | |
608 | if (len < 4) { | |
609 | return -1; | |
610 | } | |
611 | ||
612 | if (index == 0) { | |
613 | /* language ids */ | |
614 | dest[0] = 4; | |
615 | dest[1] = USB_DT_STRING; | |
616 | dest[2] = 0x09; | |
617 | dest[3] = 0x04; | |
618 | return 4; | |
619 | } | |
620 | ||
132a3f55 GH |
621 | str = usb_desc_get_string(dev, index); |
622 | if (str == NULL) { | |
62aed765 | 623 | str = usb_device_get_usb_desc(dev)->str[index]; |
132a3f55 GH |
624 | if (str == NULL) { |
625 | return 0; | |
626 | } | |
37fb59d3 | 627 | } |
132a3f55 GH |
628 | |
629 | bLength = strlen(str) * 2 + 2; | |
37fb59d3 GH |
630 | dest[0] = bLength; |
631 | dest[1] = USB_DT_STRING; | |
632 | i = 0; pos = 2; | |
633 | while (pos+1 < bLength && pos+1 < len) { | |
132a3f55 | 634 | dest[pos++] = str[i++]; |
37fb59d3 GH |
635 | dest[pos++] = 0; |
636 | } | |
637 | return pos; | |
638 | } | |
639 | ||
9a77a0f5 HG |
640 | int usb_desc_get_descriptor(USBDevice *dev, USBPacket *p, |
641 | int value, uint8_t *dest, size_t len) | |
37fb59d3 | 642 | { |
5319dc7b | 643 | bool msos = (dev->flags & (1 << USB_DEV_FLAG_MSOS_DESC_IN_USE)); |
62aed765 | 644 | const USBDesc *desc = usb_device_get_usb_desc(dev); |
25620cba | 645 | const USBDescDevice *other_dev; |
37fb59d3 GH |
646 | uint8_t buf[256]; |
647 | uint8_t type = value >> 8; | |
648 | uint8_t index = value & 0xff; | |
b43a2851 | 649 | int flags, ret = -1; |
37fb59d3 | 650 | |
25620cba | 651 | if (dev->speed == USB_SPEED_HIGH) { |
62aed765 | 652 | other_dev = usb_device_get_usb_desc(dev)->full; |
25620cba | 653 | } else { |
62aed765 | 654 | other_dev = usb_device_get_usb_desc(dev)->high; |
25620cba GH |
655 | } |
656 | ||
b43a2851 GH |
657 | flags = 0; |
658 | if (dev->device->bcdUSB >= 0x0300) { | |
659 | flags |= USB_DESC_FLAG_SUPER; | |
660 | } | |
661 | ||
37fb59d3 GH |
662 | switch(type) { |
663 | case USB_DT_DEVICE: | |
5319dc7b | 664 | ret = usb_desc_device(&desc->id, dev->device, msos, buf, sizeof(buf)); |
37fb59d3 GH |
665 | trace_usb_desc_device(dev->addr, len, ret); |
666 | break; | |
667 | case USB_DT_CONFIG: | |
a980a065 | 668 | if (index < dev->device->bNumConfigurations) { |
b43a2851 GH |
669 | ret = usb_desc_config(dev->device->confs + index, flags, |
670 | buf, sizeof(buf)); | |
37fb59d3 GH |
671 | } |
672 | trace_usb_desc_config(dev->addr, index, len, ret); | |
673 | break; | |
674 | case USB_DT_STRING: | |
132a3f55 | 675 | ret = usb_desc_string(dev, index, buf, sizeof(buf)); |
37fb59d3 GH |
676 | trace_usb_desc_string(dev->addr, index, len, ret); |
677 | break; | |
25620cba GH |
678 | case USB_DT_DEVICE_QUALIFIER: |
679 | if (other_dev != NULL) { | |
680 | ret = usb_desc_device_qualifier(other_dev, buf, sizeof(buf)); | |
681 | } | |
682 | trace_usb_desc_device_qualifier(dev->addr, len, ret); | |
683 | break; | |
684 | case USB_DT_OTHER_SPEED_CONFIG: | |
685 | if (other_dev != NULL && index < other_dev->bNumConfigurations) { | |
b43a2851 GH |
686 | ret = usb_desc_config(other_dev->confs + index, flags, |
687 | buf, sizeof(buf)); | |
25620cba GH |
688 | buf[0x01] = USB_DT_OTHER_SPEED_CONFIG; |
689 | } | |
690 | trace_usb_desc_other_speed_config(dev->addr, index, len, ret); | |
691 | break; | |
2077469b GH |
692 | case USB_DT_BOS: |
693 | ret = usb_desc_bos(desc, buf, sizeof(buf)); | |
694 | trace_usb_desc_bos(dev->addr, len, ret); | |
695 | break; | |
25620cba | 696 | |
a7fb71d1 GH |
697 | case USB_DT_DEBUG: |
698 | /* ignore silently */ | |
699 | break; | |
700 | ||
37fb59d3 GH |
701 | default: |
702 | fprintf(stderr, "%s: %d unknown type %d (len %zd)\n", __FUNCTION__, | |
703 | dev->addr, type, len); | |
704 | break; | |
705 | } | |
706 | ||
707 | if (ret > 0) { | |
708 | if (ret > len) { | |
709 | ret = len; | |
710 | } | |
711 | memcpy(dest, buf, ret); | |
9a77a0f5 HG |
712 | p->actual_length = ret; |
713 | ret = 0; | |
37fb59d3 GH |
714 | } |
715 | return ret; | |
716 | } | |
717 | ||
007fd62f HG |
718 | int usb_desc_handle_control(USBDevice *dev, USBPacket *p, |
719 | int request, int value, int index, int length, uint8_t *data) | |
37fb59d3 | 720 | { |
5319dc7b | 721 | bool msos = (dev->flags & (1 << USB_DEV_FLAG_MSOS_DESC_IN_USE)); |
62aed765 | 722 | const USBDesc *desc = usb_device_get_usb_desc(dev); |
65360511 | 723 | int ret = -1; |
37fb59d3 GH |
724 | |
725 | assert(desc != NULL); | |
726 | switch(request) { | |
41c6abbd GH |
727 | case DeviceOutRequest | USB_REQ_SET_ADDRESS: |
728 | dev->addr = value; | |
729 | trace_usb_set_addr(dev->addr); | |
730 | ret = 0; | |
731 | break; | |
732 | ||
37fb59d3 | 733 | case DeviceRequest | USB_REQ_GET_DESCRIPTOR: |
9a77a0f5 | 734 | ret = usb_desc_get_descriptor(dev, p, value, data, length); |
37fb59d3 | 735 | break; |
a980a065 GH |
736 | |
737 | case DeviceRequest | USB_REQ_GET_CONFIGURATION: | |
8db36e9d AL |
738 | /* |
739 | * 9.4.2: 0 should be returned if the device is unconfigured, otherwise | |
740 | * the non zero value of bConfigurationValue. | |
741 | */ | |
742 | data[0] = dev->config ? dev->config->bConfigurationValue : 0; | |
9a77a0f5 HG |
743 | p->actual_length = 1; |
744 | ret = 0; | |
a980a065 GH |
745 | break; |
746 | case DeviceOutRequest | USB_REQ_SET_CONFIGURATION: | |
65360511 | 747 | ret = usb_desc_set_config(dev, value); |
a980a065 GH |
748 | trace_usb_set_config(dev->addr, value, ret); |
749 | break; | |
ed5a83dd | 750 | |
8db36e9d AL |
751 | case DeviceRequest | USB_REQ_GET_STATUS: { |
752 | const USBDescConfig *config = dev->config ? | |
753 | dev->config : &dev->device->confs[0]; | |
754 | ||
ed5a83dd | 755 | data[0] = 0; |
8db36e9d AL |
756 | /* |
757 | * Default state: Device behavior when this request is received while | |
758 | * the device is in the Default state is not specified. | |
759 | * We return the same value that a configured device would return if | |
760 | * it used the first configuration. | |
761 | */ | |
bd93976a | 762 | if (config->bmAttributes & USB_CFG_ATT_SELFPOWER) { |
ed5a83dd GH |
763 | data[0] |= 1 << USB_DEVICE_SELF_POWERED; |
764 | } | |
765 | if (dev->remote_wakeup) { | |
766 | data[0] |= 1 << USB_DEVICE_REMOTE_WAKEUP; | |
767 | } | |
768 | data[1] = 0x00; | |
9a77a0f5 HG |
769 | p->actual_length = 2; |
770 | ret = 0; | |
ed5a83dd | 771 | break; |
8db36e9d | 772 | } |
ed5a83dd GH |
773 | case DeviceOutRequest | USB_REQ_CLEAR_FEATURE: |
774 | if (value == USB_DEVICE_REMOTE_WAKEUP) { | |
775 | dev->remote_wakeup = 0; | |
776 | ret = 0; | |
777 | } | |
778 | trace_usb_clear_device_feature(dev->addr, value, ret); | |
779 | break; | |
780 | case DeviceOutRequest | USB_REQ_SET_FEATURE: | |
781 | if (value == USB_DEVICE_REMOTE_WAKEUP) { | |
782 | dev->remote_wakeup = 1; | |
783 | ret = 0; | |
784 | } | |
785 | trace_usb_set_device_feature(dev->addr, value, ret); | |
786 | break; | |
1de14d43 GH |
787 | |
788 | case InterfaceRequest | USB_REQ_GET_INTERFACE: | |
789 | if (index < 0 || index >= dev->ninterfaces) { | |
790 | break; | |
791 | } | |
792 | data[0] = dev->altsetting[index]; | |
9a77a0f5 HG |
793 | p->actual_length = 1; |
794 | ret = 0; | |
1de14d43 GH |
795 | break; |
796 | case InterfaceOutRequest | USB_REQ_SET_INTERFACE: | |
797 | ret = usb_desc_set_interface(dev, index, value); | |
798 | trace_usb_set_interface(dev->addr, index, value, ret); | |
799 | break; | |
800 | ||
5319dc7b GH |
801 | case VendorDeviceRequest | 'Q': |
802 | if (msos) { | |
803 | ret = usb_desc_msos(desc, p, index, data, length); | |
804 | trace_usb_desc_msos(dev->addr, index, length, ret); | |
805 | } | |
806 | break; | |
807 | case VendorInterfaceRequest | 'Q': | |
808 | if (msos) { | |
809 | ret = usb_desc_msos(desc, p, index, data, length); | |
810 | trace_usb_desc_msos(dev->addr, index, length, ret); | |
811 | } | |
812 | break; | |
813 | ||
37fb59d3 GH |
814 | } |
815 | return ret; | |
816 | } |