]> git.proxmox.com Git - qemu.git/blame - hw/usb-desc.c
usb: track altsetting in USBDevice
[qemu.git] / hw / usb-desc.c
CommitLineData
37fb59d3
GH
1#include "usb.h"
2#include "usb-desc.h"
3#include "trace.h"
4
5/* ------------------------------------------------------------------ */
6
7static uint8_t usb_lo(uint16_t val)
8{
9 return val & 0xff;
10}
11
12static uint8_t usb_hi(uint16_t val)
13{
14 return (val >> 8) & 0xff;
15}
16
17int usb_desc_device(const USBDescID *id, const USBDescDevice *dev,
18 uint8_t *dest, size_t len)
19{
20 uint8_t bLength = 0x12;
21
22 if (len < bLength) {
23 return -1;
24 }
25
26 dest[0x00] = bLength;
27 dest[0x01] = USB_DT_DEVICE;
28
29 dest[0x02] = usb_lo(dev->bcdUSB);
30 dest[0x03] = usb_hi(dev->bcdUSB);
31 dest[0x04] = dev->bDeviceClass;
32 dest[0x05] = dev->bDeviceSubClass;
33 dest[0x06] = dev->bDeviceProtocol;
34 dest[0x07] = dev->bMaxPacketSize0;
35
36 dest[0x08] = usb_lo(id->idVendor);
37 dest[0x09] = usb_hi(id->idVendor);
38 dest[0x0a] = usb_lo(id->idProduct);
39 dest[0x0b] = usb_hi(id->idProduct);
40 dest[0x0c] = usb_lo(id->bcdDevice);
41 dest[0x0d] = usb_hi(id->bcdDevice);
42 dest[0x0e] = id->iManufacturer;
43 dest[0x0f] = id->iProduct;
44 dest[0x10] = id->iSerialNumber;
45
46 dest[0x11] = dev->bNumConfigurations;
47
48 return bLength;
49}
50
25620cba
GH
51int usb_desc_device_qualifier(const USBDescDevice *dev,
52 uint8_t *dest, size_t len)
53{
54 uint8_t bLength = 0x0a;
55
56 if (len < bLength) {
57 return -1;
58 }
59
60 dest[0x00] = bLength;
61 dest[0x01] = USB_DT_DEVICE_QUALIFIER;
62
63 dest[0x02] = usb_lo(dev->bcdUSB);
64 dest[0x03] = usb_hi(dev->bcdUSB);
65 dest[0x04] = dev->bDeviceClass;
66 dest[0x05] = dev->bDeviceSubClass;
67 dest[0x06] = dev->bDeviceProtocol;
68 dest[0x07] = dev->bMaxPacketSize0;
69 dest[0x08] = dev->bNumConfigurations;
70 dest[0x09] = 0; /* reserved */
71
72 return bLength;
73}
74
37fb59d3
GH
75int usb_desc_config(const USBDescConfig *conf, uint8_t *dest, size_t len)
76{
77 uint8_t bLength = 0x09;
78 uint16_t wTotalLength = 0;
fef13fa8 79 int i, rc;
37fb59d3
GH
80
81 if (len < bLength) {
82 return -1;
83 }
84
85 dest[0x00] = bLength;
86 dest[0x01] = USB_DT_CONFIG;
87 dest[0x04] = conf->bNumInterfaces;
88 dest[0x05] = conf->bConfigurationValue;
89 dest[0x06] = conf->iConfiguration;
90 dest[0x07] = conf->bmAttributes;
91 dest[0x08] = conf->bMaxPower;
92 wTotalLength += bLength;
93
6e625fc7
BH
94 /* handle grouped interfaces if any*/
95 for (i = 0; i < conf->nif_groups; i++) {
96 rc = usb_desc_iface_group(&(conf->if_groups[i]),
97 dest + wTotalLength,
98 len - wTotalLength);
99 if (rc < 0) {
100 return rc;
101 }
102 wTotalLength += rc;
103 }
104
105 /* handle normal (ungrouped / no IAD) interfaces if any */
fef13fa8 106 for (i = 0; i < conf->nif; i++) {
37fb59d3
GH
107 rc = usb_desc_iface(conf->ifs + i, dest + wTotalLength, len - wTotalLength);
108 if (rc < 0) {
109 return rc;
110 }
111 wTotalLength += rc;
112 }
113
114 dest[0x02] = usb_lo(wTotalLength);
115 dest[0x03] = usb_hi(wTotalLength);
116 return wTotalLength;
117}
118
6e625fc7
BH
119int usb_desc_iface_group(const USBDescIfaceAssoc *iad, uint8_t *dest,
120 size_t len)
121{
122 int pos = 0;
123 int i = 0;
124
125 /* handle interface association descriptor */
126 uint8_t bLength = 0x08;
127
128 if (len < bLength) {
129 return -1;
130 }
131
132 dest[0x00] = bLength;
133 dest[0x01] = USB_DT_INTERFACE_ASSOC;
134 dest[0x02] = iad->bFirstInterface;
135 dest[0x03] = iad->bInterfaceCount;
136 dest[0x04] = iad->bFunctionClass;
137 dest[0x05] = iad->bFunctionSubClass;
138 dest[0x06] = iad->bFunctionProtocol;
139 dest[0x07] = iad->iFunction;
140 pos += bLength;
141
142 /* handle associated interfaces in this group */
143 for (i = 0; i < iad->nif; i++) {
144 int rc = usb_desc_iface(&(iad->ifs[i]), dest + pos, len - pos);
145 if (rc < 0) {
146 return rc;
147 }
148 pos += rc;
149 }
150
151 return pos;
152}
153
37fb59d3
GH
154int usb_desc_iface(const USBDescIface *iface, uint8_t *dest, size_t len)
155{
156 uint8_t bLength = 0x09;
157 int i, rc, pos = 0;
158
159 if (len < bLength) {
160 return -1;
161 }
162
163 dest[0x00] = bLength;
164 dest[0x01] = USB_DT_INTERFACE;
165 dest[0x02] = iface->bInterfaceNumber;
166 dest[0x03] = iface->bAlternateSetting;
167 dest[0x04] = iface->bNumEndpoints;
168 dest[0x05] = iface->bInterfaceClass;
169 dest[0x06] = iface->bInterfaceSubClass;
170 dest[0x07] = iface->bInterfaceProtocol;
171 dest[0x08] = iface->iInterface;
172 pos += bLength;
173
174 for (i = 0; i < iface->ndesc; i++) {
175 rc = usb_desc_other(iface->descs + i, dest + pos, len - pos);
176 if (rc < 0) {
177 return rc;
178 }
179 pos += rc;
180 }
181
182 for (i = 0; i < iface->bNumEndpoints; i++) {
183 rc = usb_desc_endpoint(iface->eps + i, dest + pos, len - pos);
184 if (rc < 0) {
185 return rc;
186 }
187 pos += rc;
188 }
189
190 return pos;
191}
192
193int usb_desc_endpoint(const USBDescEndpoint *ep, uint8_t *dest, size_t len)
194{
195 uint8_t bLength = 0x07;
196
197 if (len < bLength) {
198 return -1;
199 }
200
201 dest[0x00] = bLength;
202 dest[0x01] = USB_DT_ENDPOINT;
203 dest[0x02] = ep->bEndpointAddress;
204 dest[0x03] = ep->bmAttributes;
205 dest[0x04] = usb_lo(ep->wMaxPacketSize);
206 dest[0x05] = usb_hi(ep->wMaxPacketSize);
207 dest[0x06] = ep->bInterval;
208
209 return bLength;
210}
211
212int usb_desc_other(const USBDescOther *desc, uint8_t *dest, size_t len)
213{
214 int bLength = desc->length ? desc->length : desc->data[0];
215
216 if (len < bLength) {
217 return -1;
218 }
219
220 memcpy(dest, desc->data, bLength);
221 return bLength;
222}
223
132a3f55
GH
224/* ------------------------------------------------------------------ */
225
1de14d43
GH
226static const USBDescIface *usb_desc_find_interface(USBDevice *dev,
227 int nif, int alt)
228{
229 const USBDescIface *iface;
230 int g, i;
231
232 if (!dev->config) {
233 return NULL;
234 }
235 for (g = 0; g < dev->config->nif_groups; g++) {
236 for (i = 0; i < dev->config->if_groups[g].nif; i++) {
237 iface = &dev->config->if_groups[g].ifs[i];
238 if (iface->bInterfaceNumber == nif &&
239 iface->bAlternateSetting == alt) {
240 return iface;
241 }
242 }
243 }
244 for (i = 0; i < dev->config->nif; i++) {
245 iface = &dev->config->ifs[i];
246 if (iface->bInterfaceNumber == nif &&
247 iface->bAlternateSetting == alt) {
248 return iface;
249 }
250 }
251 return NULL;
252}
253
254static int usb_desc_set_interface(USBDevice *dev, int index, int value)
255{
256 const USBDescIface *iface;
257 int old;
258
259 iface = usb_desc_find_interface(dev, index, value);
260 if (iface == NULL) {
261 return -1;
262 }
263
264 old = dev->altsetting[index];
265 dev->altsetting[index] = value;
266 dev->ifaces[index] = iface;
267
268 if (dev->info->set_interface && old != value) {
269 dev->info->set_interface(dev, index, old, value);
270 }
271 return 0;
272}
273
65360511
GH
274static int usb_desc_set_config(USBDevice *dev, int value)
275{
276 int i;
277
278 if (value == 0) {
279 dev->configuration = 0;
280 dev->ninterfaces = 0;
281 dev->config = NULL;
282 } else {
283 for (i = 0; i < dev->device->bNumConfigurations; i++) {
284 if (dev->device->confs[i].bConfigurationValue == value) {
285 dev->configuration = value;
286 dev->ninterfaces = dev->device->confs[i].bNumInterfaces;
287 dev->config = dev->device->confs + i;
1de14d43 288 assert(dev->ninterfaces <= USB_MAX_INTERFACES);
65360511
GH
289 }
290 }
291 if (i < dev->device->bNumConfigurations) {
292 return -1;
293 }
294 }
1de14d43
GH
295
296 for (i = 0; i < dev->ninterfaces; i++) {
297 usb_desc_set_interface(dev, i, 0);
298 }
299 for (; i < USB_MAX_INTERFACES; i++) {
300 dev->altsetting[i] = 0;
301 dev->ifaces[i] = NULL;
302 }
303
65360511
GH
304 return 0;
305}
306
32d41919 307static void usb_desc_setdefaults(USBDevice *dev)
a980a065
GH
308{
309 const USBDesc *desc = dev->info->usb_desc;
310
311 assert(desc != NULL);
32d41919
GH
312 switch (dev->speed) {
313 case USB_SPEED_LOW:
314 case USB_SPEED_FULL:
315 dev->device = desc->full;
316 break;
317 case USB_SPEED_HIGH:
318 dev->device = desc->high;
319 break;
320 }
65360511 321 usb_desc_set_config(dev, 0);
a980a065
GH
322}
323
32d41919
GH
324void usb_desc_init(USBDevice *dev)
325{
ba3f9bfb
HG
326 const USBDesc *desc = dev->info->usb_desc;
327
328 assert(desc != NULL);
32d41919 329 dev->speed = USB_SPEED_FULL;
ba3f9bfb
HG
330 dev->speedmask = 0;
331 if (desc->full) {
332 dev->speedmask |= USB_SPEED_MASK_FULL;
333 }
334 if (desc->high) {
335 dev->speedmask |= USB_SPEED_MASK_HIGH;
336 }
32d41919
GH
337 usb_desc_setdefaults(dev);
338}
339
340void usb_desc_attach(USBDevice *dev)
341{
342 const USBDesc *desc = dev->info->usb_desc;
343
344 assert(desc != NULL);
345 if (desc->high && (dev->port->speedmask & USB_SPEED_MASK_HIGH)) {
346 dev->speed = USB_SPEED_HIGH;
347 } else if (desc->full && (dev->port->speedmask & USB_SPEED_MASK_FULL)) {
348 dev->speed = USB_SPEED_FULL;
349 } else {
350 fprintf(stderr, "usb: port/device speed mismatch for \"%s\"\n",
351 dev->info->product_desc);
352 return;
353 }
354 usb_desc_setdefaults(dev);
355}
356
132a3f55
GH
357void usb_desc_set_string(USBDevice *dev, uint8_t index, const char *str)
358{
359 USBDescString *s;
360
361 QLIST_FOREACH(s, &dev->strings, next) {
362 if (s->index == index) {
363 break;
364 }
365 }
366 if (s == NULL) {
7267c094 367 s = g_malloc0(sizeof(*s));
132a3f55
GH
368 s->index = index;
369 QLIST_INSERT_HEAD(&dev->strings, s, next);
370 }
7267c094
AL
371 g_free(s->str);
372 s->str = g_strdup(str);
132a3f55
GH
373}
374
375const char *usb_desc_get_string(USBDevice *dev, uint8_t index)
376{
377 USBDescString *s;
378
379 QLIST_FOREACH(s, &dev->strings, next) {
380 if (s->index == index) {
381 return s->str;
382 }
383 }
384 return NULL;
385}
386
387int usb_desc_string(USBDevice *dev, int index, uint8_t *dest, size_t len)
37fb59d3
GH
388{
389 uint8_t bLength, pos, i;
132a3f55 390 const char *str;
37fb59d3
GH
391
392 if (len < 4) {
393 return -1;
394 }
395
396 if (index == 0) {
397 /* language ids */
398 dest[0] = 4;
399 dest[1] = USB_DT_STRING;
400 dest[2] = 0x09;
401 dest[3] = 0x04;
402 return 4;
403 }
404
132a3f55
GH
405 str = usb_desc_get_string(dev, index);
406 if (str == NULL) {
407 str = dev->info->usb_desc->str[index];
408 if (str == NULL) {
409 return 0;
410 }
37fb59d3 411 }
132a3f55
GH
412
413 bLength = strlen(str) * 2 + 2;
37fb59d3
GH
414 dest[0] = bLength;
415 dest[1] = USB_DT_STRING;
416 i = 0; pos = 2;
417 while (pos+1 < bLength && pos+1 < len) {
132a3f55 418 dest[pos++] = str[i++];
37fb59d3
GH
419 dest[pos++] = 0;
420 }
421 return pos;
422}
423
37fb59d3
GH
424int usb_desc_get_descriptor(USBDevice *dev, int value, uint8_t *dest, size_t len)
425{
426 const USBDesc *desc = dev->info->usb_desc;
25620cba 427 const USBDescDevice *other_dev;
37fb59d3
GH
428 uint8_t buf[256];
429 uint8_t type = value >> 8;
430 uint8_t index = value & 0xff;
431 int ret = -1;
432
25620cba
GH
433 if (dev->speed == USB_SPEED_HIGH) {
434 other_dev = dev->info->usb_desc->full;
435 } else {
436 other_dev = dev->info->usb_desc->high;
437 }
438
37fb59d3
GH
439 switch(type) {
440 case USB_DT_DEVICE:
a980a065 441 ret = usb_desc_device(&desc->id, dev->device, buf, sizeof(buf));
37fb59d3
GH
442 trace_usb_desc_device(dev->addr, len, ret);
443 break;
444 case USB_DT_CONFIG:
a980a065
GH
445 if (index < dev->device->bNumConfigurations) {
446 ret = usb_desc_config(dev->device->confs + index, buf, sizeof(buf));
37fb59d3
GH
447 }
448 trace_usb_desc_config(dev->addr, index, len, ret);
449 break;
450 case USB_DT_STRING:
132a3f55 451 ret = usb_desc_string(dev, index, buf, sizeof(buf));
37fb59d3
GH
452 trace_usb_desc_string(dev->addr, index, len, ret);
453 break;
25620cba
GH
454
455 case USB_DT_DEVICE_QUALIFIER:
456 if (other_dev != NULL) {
457 ret = usb_desc_device_qualifier(other_dev, buf, sizeof(buf));
458 }
459 trace_usb_desc_device_qualifier(dev->addr, len, ret);
460 break;
461 case USB_DT_OTHER_SPEED_CONFIG:
462 if (other_dev != NULL && index < other_dev->bNumConfigurations) {
463 ret = usb_desc_config(other_dev->confs + index, buf, sizeof(buf));
464 buf[0x01] = USB_DT_OTHER_SPEED_CONFIG;
465 }
466 trace_usb_desc_other_speed_config(dev->addr, index, len, ret);
467 break;
468
a7fb71d1
GH
469 case USB_DT_DEBUG:
470 /* ignore silently */
471 break;
472
37fb59d3
GH
473 default:
474 fprintf(stderr, "%s: %d unknown type %d (len %zd)\n", __FUNCTION__,
475 dev->addr, type, len);
476 break;
477 }
478
479 if (ret > 0) {
480 if (ret > len) {
481 ret = len;
482 }
483 memcpy(dest, buf, ret);
484 }
485 return ret;
486}
487
007fd62f
HG
488int usb_desc_handle_control(USBDevice *dev, USBPacket *p,
489 int request, int value, int index, int length, uint8_t *data)
37fb59d3
GH
490{
491 const USBDesc *desc = dev->info->usb_desc;
65360511 492 int ret = -1;
37fb59d3
GH
493
494 assert(desc != NULL);
495 switch(request) {
41c6abbd
GH
496 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
497 dev->addr = value;
498 trace_usb_set_addr(dev->addr);
499 ret = 0;
500 break;
501
37fb59d3
GH
502 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
503 ret = usb_desc_get_descriptor(dev, value, data, length);
504 break;
a980a065
GH
505
506 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
507 data[0] = dev->config->bConfigurationValue;
508 ret = 1;
509 break;
510 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
65360511 511 ret = usb_desc_set_config(dev, value);
a980a065
GH
512 trace_usb_set_config(dev->addr, value, ret);
513 break;
ed5a83dd
GH
514
515 case DeviceRequest | USB_REQ_GET_STATUS:
516 data[0] = 0;
517 if (dev->config->bmAttributes & 0x40) {
518 data[0] |= 1 << USB_DEVICE_SELF_POWERED;
519 }
520 if (dev->remote_wakeup) {
521 data[0] |= 1 << USB_DEVICE_REMOTE_WAKEUP;
522 }
523 data[1] = 0x00;
524 ret = 2;
525 break;
526 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
527 if (value == USB_DEVICE_REMOTE_WAKEUP) {
528 dev->remote_wakeup = 0;
529 ret = 0;
530 }
531 trace_usb_clear_device_feature(dev->addr, value, ret);
532 break;
533 case DeviceOutRequest | USB_REQ_SET_FEATURE:
534 if (value == USB_DEVICE_REMOTE_WAKEUP) {
535 dev->remote_wakeup = 1;
536 ret = 0;
537 }
538 trace_usb_set_device_feature(dev->addr, value, ret);
539 break;
1de14d43
GH
540
541 case InterfaceRequest | USB_REQ_GET_INTERFACE:
542 if (index < 0 || index >= dev->ninterfaces) {
543 break;
544 }
545 data[0] = dev->altsetting[index];
546 ret = 1;
547 break;
548 case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
549 ret = usb_desc_set_interface(dev, index, value);
550 trace_usb_set_interface(dev->addr, index, value, ret);
551 break;
552
37fb59d3
GH
553 }
554 return ret;
555}