usb: track configuration and interface count in USBDevice.
[qemu/wangdongxu.git] / hw / usb-desc.c
blobb52561adf8338b034fbd5fec834d2b9bcb68e5fe
1 #include "usb.h"
2 #include "usb-desc.h"
3 #include "trace.h"
5 /* ------------------------------------------------------------------ */
7 static uint8_t usb_lo(uint16_t val)
9 return val & 0xff;
12 static uint8_t usb_hi(uint16_t val)
14 return (val >> 8) & 0xff;
17 int usb_desc_device(const USBDescID *id, const USBDescDevice *dev,
18 uint8_t *dest, size_t len)
20 uint8_t bLength = 0x12;
22 if (len < bLength) {
23 return -1;
26 dest[0x00] = bLength;
27 dest[0x01] = USB_DT_DEVICE;
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;
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;
46 dest[0x11] = dev->bNumConfigurations;
48 return bLength;
51 int usb_desc_device_qualifier(const USBDescDevice *dev,
52 uint8_t *dest, size_t len)
54 uint8_t bLength = 0x0a;
56 if (len < bLength) {
57 return -1;
60 dest[0x00] = bLength;
61 dest[0x01] = USB_DT_DEVICE_QUALIFIER;
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 */
72 return bLength;
75 int usb_desc_config(const USBDescConfig *conf, uint8_t *dest, size_t len)
77 uint8_t bLength = 0x09;
78 uint16_t wTotalLength = 0;
79 int i, rc;
81 if (len < bLength) {
82 return -1;
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;
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;
102 wTotalLength += rc;
105 /* handle normal (ungrouped / no IAD) interfaces if any */
106 for (i = 0; i < conf->nif; i++) {
107 rc = usb_desc_iface(conf->ifs + i, dest + wTotalLength, len - wTotalLength);
108 if (rc < 0) {
109 return rc;
111 wTotalLength += rc;
114 dest[0x02] = usb_lo(wTotalLength);
115 dest[0x03] = usb_hi(wTotalLength);
116 return wTotalLength;
119 int usb_desc_iface_group(const USBDescIfaceAssoc *iad, uint8_t *dest,
120 size_t len)
122 int pos = 0;
123 int i = 0;
125 /* handle interface association descriptor */
126 uint8_t bLength = 0x08;
128 if (len < bLength) {
129 return -1;
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;
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;
148 pos += rc;
151 return pos;
154 int usb_desc_iface(const USBDescIface *iface, uint8_t *dest, size_t len)
156 uint8_t bLength = 0x09;
157 int i, rc, pos = 0;
159 if (len < bLength) {
160 return -1;
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;
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;
179 pos += rc;
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;
187 pos += rc;
190 return pos;
193 int usb_desc_endpoint(const USBDescEndpoint *ep, uint8_t *dest, size_t len)
195 uint8_t bLength = 0x07;
197 if (len < bLength) {
198 return -1;
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;
209 return bLength;
212 int usb_desc_other(const USBDescOther *desc, uint8_t *dest, size_t len)
214 int bLength = desc->length ? desc->length : desc->data[0];
216 if (len < bLength) {
217 return -1;
220 memcpy(dest, desc->data, bLength);
221 return bLength;
224 /* ------------------------------------------------------------------ */
226 static int usb_desc_set_config(USBDevice *dev, int value)
228 int i;
230 if (value == 0) {
231 dev->configuration = 0;
232 dev->ninterfaces = 0;
233 dev->config = NULL;
234 } else {
235 for (i = 0; i < dev->device->bNumConfigurations; i++) {
236 if (dev->device->confs[i].bConfigurationValue == value) {
237 dev->configuration = value;
238 dev->ninterfaces = dev->device->confs[i].bNumInterfaces;
239 dev->config = dev->device->confs + i;
242 if (i < dev->device->bNumConfigurations) {
243 return -1;
246 return 0;
249 static void usb_desc_setdefaults(USBDevice *dev)
251 const USBDesc *desc = dev->info->usb_desc;
253 assert(desc != NULL);
254 switch (dev->speed) {
255 case USB_SPEED_LOW:
256 case USB_SPEED_FULL:
257 dev->device = desc->full;
258 break;
259 case USB_SPEED_HIGH:
260 dev->device = desc->high;
261 break;
263 usb_desc_set_config(dev, 0);
266 void usb_desc_init(USBDevice *dev)
268 const USBDesc *desc = dev->info->usb_desc;
270 assert(desc != NULL);
271 dev->speed = USB_SPEED_FULL;
272 dev->speedmask = 0;
273 if (desc->full) {
274 dev->speedmask |= USB_SPEED_MASK_FULL;
276 if (desc->high) {
277 dev->speedmask |= USB_SPEED_MASK_HIGH;
279 usb_desc_setdefaults(dev);
282 void usb_desc_attach(USBDevice *dev)
284 const USBDesc *desc = dev->info->usb_desc;
286 assert(desc != NULL);
287 if (desc->high && (dev->port->speedmask & USB_SPEED_MASK_HIGH)) {
288 dev->speed = USB_SPEED_HIGH;
289 } else if (desc->full && (dev->port->speedmask & USB_SPEED_MASK_FULL)) {
290 dev->speed = USB_SPEED_FULL;
291 } else {
292 fprintf(stderr, "usb: port/device speed mismatch for \"%s\"\n",
293 dev->info->product_desc);
294 return;
296 usb_desc_setdefaults(dev);
299 void usb_desc_set_string(USBDevice *dev, uint8_t index, const char *str)
301 USBDescString *s;
303 QLIST_FOREACH(s, &dev->strings, next) {
304 if (s->index == index) {
305 break;
308 if (s == NULL) {
309 s = g_malloc0(sizeof(*s));
310 s->index = index;
311 QLIST_INSERT_HEAD(&dev->strings, s, next);
313 g_free(s->str);
314 s->str = g_strdup(str);
317 const char *usb_desc_get_string(USBDevice *dev, uint8_t index)
319 USBDescString *s;
321 QLIST_FOREACH(s, &dev->strings, next) {
322 if (s->index == index) {
323 return s->str;
326 return NULL;
329 int usb_desc_string(USBDevice *dev, int index, uint8_t *dest, size_t len)
331 uint8_t bLength, pos, i;
332 const char *str;
334 if (len < 4) {
335 return -1;
338 if (index == 0) {
339 /* language ids */
340 dest[0] = 4;
341 dest[1] = USB_DT_STRING;
342 dest[2] = 0x09;
343 dest[3] = 0x04;
344 return 4;
347 str = usb_desc_get_string(dev, index);
348 if (str == NULL) {
349 str = dev->info->usb_desc->str[index];
350 if (str == NULL) {
351 return 0;
355 bLength = strlen(str) * 2 + 2;
356 dest[0] = bLength;
357 dest[1] = USB_DT_STRING;
358 i = 0; pos = 2;
359 while (pos+1 < bLength && pos+1 < len) {
360 dest[pos++] = str[i++];
361 dest[pos++] = 0;
363 return pos;
366 int usb_desc_get_descriptor(USBDevice *dev, int value, uint8_t *dest, size_t len)
368 const USBDesc *desc = dev->info->usb_desc;
369 const USBDescDevice *other_dev;
370 uint8_t buf[256];
371 uint8_t type = value >> 8;
372 uint8_t index = value & 0xff;
373 int ret = -1;
375 if (dev->speed == USB_SPEED_HIGH) {
376 other_dev = dev->info->usb_desc->full;
377 } else {
378 other_dev = dev->info->usb_desc->high;
381 switch(type) {
382 case USB_DT_DEVICE:
383 ret = usb_desc_device(&desc->id, dev->device, buf, sizeof(buf));
384 trace_usb_desc_device(dev->addr, len, ret);
385 break;
386 case USB_DT_CONFIG:
387 if (index < dev->device->bNumConfigurations) {
388 ret = usb_desc_config(dev->device->confs + index, buf, sizeof(buf));
390 trace_usb_desc_config(dev->addr, index, len, ret);
391 break;
392 case USB_DT_STRING:
393 ret = usb_desc_string(dev, index, buf, sizeof(buf));
394 trace_usb_desc_string(dev->addr, index, len, ret);
395 break;
397 case USB_DT_DEVICE_QUALIFIER:
398 if (other_dev != NULL) {
399 ret = usb_desc_device_qualifier(other_dev, buf, sizeof(buf));
401 trace_usb_desc_device_qualifier(dev->addr, len, ret);
402 break;
403 case USB_DT_OTHER_SPEED_CONFIG:
404 if (other_dev != NULL && index < other_dev->bNumConfigurations) {
405 ret = usb_desc_config(other_dev->confs + index, buf, sizeof(buf));
406 buf[0x01] = USB_DT_OTHER_SPEED_CONFIG;
408 trace_usb_desc_other_speed_config(dev->addr, index, len, ret);
409 break;
411 case USB_DT_DEBUG:
412 /* ignore silently */
413 break;
415 default:
416 fprintf(stderr, "%s: %d unknown type %d (len %zd)\n", __FUNCTION__,
417 dev->addr, type, len);
418 break;
421 if (ret > 0) {
422 if (ret > len) {
423 ret = len;
425 memcpy(dest, buf, ret);
427 return ret;
430 int usb_desc_handle_control(USBDevice *dev, USBPacket *p,
431 int request, int value, int index, int length, uint8_t *data)
433 const USBDesc *desc = dev->info->usb_desc;
434 int ret = -1;
436 assert(desc != NULL);
437 switch(request) {
438 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
439 dev->addr = value;
440 trace_usb_set_addr(dev->addr);
441 ret = 0;
442 break;
444 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
445 ret = usb_desc_get_descriptor(dev, value, data, length);
446 break;
448 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
449 data[0] = dev->config->bConfigurationValue;
450 ret = 1;
451 break;
452 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
453 ret = usb_desc_set_config(dev, value);
454 trace_usb_set_config(dev->addr, value, ret);
455 break;
457 case DeviceRequest | USB_REQ_GET_STATUS:
458 data[0] = 0;
459 if (dev->config->bmAttributes & 0x40) {
460 data[0] |= 1 << USB_DEVICE_SELF_POWERED;
462 if (dev->remote_wakeup) {
463 data[0] |= 1 << USB_DEVICE_REMOTE_WAKEUP;
465 data[1] = 0x00;
466 ret = 2;
467 break;
468 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
469 if (value == USB_DEVICE_REMOTE_WAKEUP) {
470 dev->remote_wakeup = 0;
471 ret = 0;
473 trace_usb_clear_device_feature(dev->addr, value, ret);
474 break;
475 case DeviceOutRequest | USB_REQ_SET_FEATURE:
476 if (value == USB_DEVICE_REMOTE_WAKEUP) {
477 dev->remote_wakeup = 1;
478 ret = 0;
480 trace_usb_set_device_feature(dev->addr, value, ret);
481 break;
483 return ret;