qemu-ga: add guest-fstrim command
[qemu/opensuse.git] / hw / usb / desc.c
blob0a9d3c9f6055b007c25bd6eda38cec442a1b1d98
1 #include <ctype.h>
3 #include "hw/usb.h"
4 #include "hw/usb/desc.h"
5 #include "trace.h"
7 /* ------------------------------------------------------------------ */
9 static uint8_t usb_lo(uint16_t val)
11 return val & 0xff;
14 static uint8_t usb_hi(uint16_t val)
16 return (val >> 8) & 0xff;
19 int usb_desc_device(const USBDescID *id, const USBDescDevice *dev,
20 uint8_t *dest, size_t len)
22 uint8_t bLength = 0x12;
23 USBDescriptor *d = (void *)dest;
25 if (len < bLength) {
26 return -1;
29 d->bLength = bLength;
30 d->bDescriptorType = USB_DT_DEVICE;
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;
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;
49 d->u.device.bNumConfigurations = dev->bNumConfigurations;
51 return bLength;
54 int usb_desc_device_qualifier(const USBDescDevice *dev,
55 uint8_t *dest, size_t len)
57 uint8_t bLength = 0x0a;
58 USBDescriptor *d = (void *)dest;
60 if (len < bLength) {
61 return -1;
64 d->bLength = bLength;
65 d->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
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;
76 return bLength;
79 int usb_desc_config(const USBDescConfig *conf, uint8_t *dest, size_t len)
81 uint8_t bLength = 0x09;
82 uint16_t wTotalLength = 0;
83 USBDescriptor *d = (void *)dest;
84 int i, rc;
86 if (len < bLength) {
87 return -1;
90 d->bLength = bLength;
91 d->bDescriptorType = USB_DT_CONFIG;
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;
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;
108 wTotalLength += rc;
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;
117 wTotalLength += rc;
120 d->u.config.wTotalLength_lo = usb_lo(wTotalLength);
121 d->u.config.wTotalLength_hi = usb_hi(wTotalLength);
122 return wTotalLength;
125 int usb_desc_iface_group(const USBDescIfaceAssoc *iad, uint8_t *dest,
126 size_t len)
128 int pos = 0;
129 int i = 0;
131 /* handle interface association descriptor */
132 uint8_t bLength = 0x08;
134 if (len < bLength) {
135 return -1;
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;
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;
154 pos += rc;
157 return pos;
160 int usb_desc_iface(const USBDescIface *iface, uint8_t *dest, size_t len)
162 uint8_t bLength = 0x09;
163 int i, rc, pos = 0;
164 USBDescriptor *d = (void *)dest;
166 if (len < bLength) {
167 return -1;
170 d->bLength = bLength;
171 d->bDescriptorType = USB_DT_INTERFACE;
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;
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;
187 pos += rc;
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;
195 pos += rc;
198 return pos;
201 int usb_desc_endpoint(const USBDescEndpoint *ep, uint8_t *dest, size_t len)
203 uint8_t bLength = ep->is_audio ? 0x09 : 0x07;
204 uint8_t extralen = ep->extra ? ep->extra[0] : 0;
205 USBDescriptor *d = (void *)dest;
207 if (len < bLength + extralen) {
208 return -1;
211 d->bLength = bLength;
212 d->bDescriptorType = USB_DT_ENDPOINT;
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;
223 if (ep->extra) {
224 memcpy(dest + bLength, ep->extra, extralen);
227 return bLength + extralen;
230 int usb_desc_other(const USBDescOther *desc, uint8_t *dest, size_t len)
232 int bLength = desc->length ? desc->length : desc->data[0];
234 if (len < bLength) {
235 return -1;
238 memcpy(dest, desc->data, bLength);
239 return bLength;
242 /* ------------------------------------------------------------------ */
244 static void usb_desc_ep_init(USBDevice *dev)
246 const USBDescIface *iface;
247 int i, e, pid, ep;
249 usb_ep_init(dev);
250 for (i = 0; i < dev->ninterfaces; i++) {
251 iface = dev->ifaces[i];
252 if (iface == NULL) {
253 continue;
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);
267 static const USBDescIface *usb_desc_find_interface(USBDevice *dev,
268 int nif, int alt)
270 const USBDescIface *iface;
271 int g, i;
273 if (!dev->config) {
274 return NULL;
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;
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;
292 return NULL;
295 static int usb_desc_set_interface(USBDevice *dev, int index, int value)
297 const USBDescIface *iface;
298 int old;
300 iface = usb_desc_find_interface(dev, index, value);
301 if (iface == NULL) {
302 return -1;
305 old = dev->altsetting[index];
306 dev->altsetting[index] = value;
307 dev->ifaces[index] = iface;
308 usb_desc_ep_init(dev);
310 if (old != value) {
311 usb_device_set_interface(dev, index, old, value);
313 return 0;
316 static int usb_desc_set_config(USBDevice *dev, int value)
318 int i;
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);
333 if (i < dev->device->bNumConfigurations) {
334 return -1;
338 for (i = 0; i < dev->ninterfaces; i++) {
339 usb_desc_set_interface(dev, i, 0);
341 for (; i < USB_MAX_INTERFACES; i++) {
342 dev->altsetting[i] = 0;
343 dev->ifaces[i] = NULL;
346 return 0;
349 static void usb_desc_setdefaults(USBDevice *dev)
351 const USBDesc *desc = usb_device_get_usb_desc(dev);
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;
363 usb_desc_set_config(dev, 0);
366 void usb_desc_init(USBDevice *dev)
368 const USBDesc *desc = usb_device_get_usb_desc(dev);
370 assert(desc != NULL);
371 dev->speed = USB_SPEED_FULL;
372 dev->speedmask = 0;
373 if (desc->full) {
374 dev->speedmask |= USB_SPEED_MASK_FULL;
376 if (desc->high) {
377 dev->speedmask |= USB_SPEED_MASK_HIGH;
379 usb_desc_setdefaults(dev);
382 void usb_desc_attach(USBDevice *dev)
384 const USBDesc *desc = usb_device_get_usb_desc(dev);
386 assert(desc != NULL);
387 if (desc->high && (dev->port->speedmask & USB_SPEED_MASK_HIGH)) {
388 dev->speed = USB_SPEED_HIGH;
389 } else if (desc->full && (dev->port->speedmask & USB_SPEED_MASK_FULL)) {
390 dev->speed = USB_SPEED_FULL;
391 } else {
392 fprintf(stderr, "usb: port/device speed mismatch for \"%s\"\n",
393 usb_device_get_product_desc(dev));
394 return;
396 usb_desc_setdefaults(dev);
399 void usb_desc_set_string(USBDevice *dev, uint8_t index, const char *str)
401 USBDescString *s;
403 QLIST_FOREACH(s, &dev->strings, next) {
404 if (s->index == index) {
405 break;
408 if (s == NULL) {
409 s = g_malloc0(sizeof(*s));
410 s->index = index;
411 QLIST_INSERT_HEAD(&dev->strings, s, next);
413 g_free(s->str);
414 s->str = g_strdup(str);
418 * This function creates a serial number for a usb device.
419 * The serial number should:
420 * (a) Be unique within the virtual machine.
421 * (b) Be constant, so you don't get a new one each
422 * time the guest is started.
423 * So we are using the physical location to generate a serial number
424 * from it. It has three pieces: First a fixed, device-specific
425 * prefix. Second the device path of the host controller (which is
426 * the pci address in most cases). Third the physical port path.
427 * Results in serial numbers like this: "314159-0000:00:1d.7-3".
429 void usb_desc_create_serial(USBDevice *dev)
431 DeviceState *hcd = dev->qdev.parent_bus->parent;
432 const USBDesc *desc = usb_device_get_usb_desc(dev);
433 int index = desc->id.iSerialNumber;
434 char serial[64];
435 char *path;
436 int dst;
438 assert(index != 0 && desc->str[index] != NULL);
439 dst = snprintf(serial, sizeof(serial), "%s", desc->str[index]);
440 path = qdev_get_dev_path(hcd);
441 if (path) {
442 dst += snprintf(serial+dst, sizeof(serial)-dst, "-%s", path);
444 dst += snprintf(serial+dst, sizeof(serial)-dst, "-%s", dev->port->path);
445 usb_desc_set_string(dev, index, serial);
448 const char *usb_desc_get_string(USBDevice *dev, uint8_t index)
450 USBDescString *s;
452 QLIST_FOREACH(s, &dev->strings, next) {
453 if (s->index == index) {
454 return s->str;
457 return NULL;
460 int usb_desc_string(USBDevice *dev, int index, uint8_t *dest, size_t len)
462 uint8_t bLength, pos, i;
463 const char *str;
465 if (len < 4) {
466 return -1;
469 if (index == 0) {
470 /* language ids */
471 dest[0] = 4;
472 dest[1] = USB_DT_STRING;
473 dest[2] = 0x09;
474 dest[3] = 0x04;
475 return 4;
478 str = usb_desc_get_string(dev, index);
479 if (str == NULL) {
480 str = usb_device_get_usb_desc(dev)->str[index];
481 if (str == NULL) {
482 return 0;
486 bLength = strlen(str) * 2 + 2;
487 dest[0] = bLength;
488 dest[1] = USB_DT_STRING;
489 i = 0; pos = 2;
490 while (pos+1 < bLength && pos+1 < len) {
491 dest[pos++] = str[i++];
492 dest[pos++] = 0;
494 return pos;
497 int usb_desc_get_descriptor(USBDevice *dev, int value, uint8_t *dest, size_t len)
499 const USBDesc *desc = usb_device_get_usb_desc(dev);
500 const USBDescDevice *other_dev;
501 uint8_t buf[256];
502 uint8_t type = value >> 8;
503 uint8_t index = value & 0xff;
504 int ret = -1;
506 if (dev->speed == USB_SPEED_HIGH) {
507 other_dev = usb_device_get_usb_desc(dev)->full;
508 } else {
509 other_dev = usb_device_get_usb_desc(dev)->high;
512 switch(type) {
513 case USB_DT_DEVICE:
514 ret = usb_desc_device(&desc->id, dev->device, buf, sizeof(buf));
515 trace_usb_desc_device(dev->addr, len, ret);
516 break;
517 case USB_DT_CONFIG:
518 if (index < dev->device->bNumConfigurations) {
519 ret = usb_desc_config(dev->device->confs + index, buf, sizeof(buf));
521 trace_usb_desc_config(dev->addr, index, len, ret);
522 break;
523 case USB_DT_STRING:
524 ret = usb_desc_string(dev, index, buf, sizeof(buf));
525 trace_usb_desc_string(dev->addr, index, len, ret);
526 break;
528 case USB_DT_DEVICE_QUALIFIER:
529 if (other_dev != NULL) {
530 ret = usb_desc_device_qualifier(other_dev, buf, sizeof(buf));
532 trace_usb_desc_device_qualifier(dev->addr, len, ret);
533 break;
534 case USB_DT_OTHER_SPEED_CONFIG:
535 if (other_dev != NULL && index < other_dev->bNumConfigurations) {
536 ret = usb_desc_config(other_dev->confs + index, buf, sizeof(buf));
537 buf[0x01] = USB_DT_OTHER_SPEED_CONFIG;
539 trace_usb_desc_other_speed_config(dev->addr, index, len, ret);
540 break;
542 case USB_DT_DEBUG:
543 /* ignore silently */
544 break;
546 default:
547 fprintf(stderr, "%s: %d unknown type %d (len %zd)\n", __FUNCTION__,
548 dev->addr, type, len);
549 break;
552 if (ret > 0) {
553 if (ret > len) {
554 ret = len;
556 memcpy(dest, buf, ret);
558 return ret;
561 int usb_desc_handle_control(USBDevice *dev, USBPacket *p,
562 int request, int value, int index, int length, uint8_t *data)
564 const USBDesc *desc = usb_device_get_usb_desc(dev);
565 int ret = -1;
567 assert(desc != NULL);
568 switch(request) {
569 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
570 dev->addr = value;
571 trace_usb_set_addr(dev->addr);
572 ret = 0;
573 break;
575 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
576 ret = usb_desc_get_descriptor(dev, value, data, length);
577 break;
579 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
581 * 9.4.2: 0 should be returned if the device is unconfigured, otherwise
582 * the non zero value of bConfigurationValue.
584 data[0] = dev->config ? dev->config->bConfigurationValue : 0;
585 ret = 1;
586 break;
587 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
588 ret = usb_desc_set_config(dev, value);
589 trace_usb_set_config(dev->addr, value, ret);
590 break;
592 case DeviceRequest | USB_REQ_GET_STATUS: {
593 const USBDescConfig *config = dev->config ?
594 dev->config : &dev->device->confs[0];
596 data[0] = 0;
598 * Default state: Device behavior when this request is received while
599 * the device is in the Default state is not specified.
600 * We return the same value that a configured device would return if
601 * it used the first configuration.
603 if (config->bmAttributes & 0x40) {
604 data[0] |= 1 << USB_DEVICE_SELF_POWERED;
606 if (dev->remote_wakeup) {
607 data[0] |= 1 << USB_DEVICE_REMOTE_WAKEUP;
609 data[1] = 0x00;
610 ret = 2;
611 break;
613 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
614 if (value == USB_DEVICE_REMOTE_WAKEUP) {
615 dev->remote_wakeup = 0;
616 ret = 0;
618 trace_usb_clear_device_feature(dev->addr, value, ret);
619 break;
620 case DeviceOutRequest | USB_REQ_SET_FEATURE:
621 if (value == USB_DEVICE_REMOTE_WAKEUP) {
622 dev->remote_wakeup = 1;
623 ret = 0;
625 trace_usb_set_device_feature(dev->addr, value, ret);
626 break;
628 case InterfaceRequest | USB_REQ_GET_INTERFACE:
629 if (index < 0 || index >= dev->ninterfaces) {
630 break;
632 data[0] = dev->altsetting[index];
633 ret = 1;
634 break;
635 case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
636 ret = usb_desc_set_interface(dev, index, value);
637 trace_usb_set_interface(dev->addr, index, value, ret);
638 break;
641 return ret;