4 #include "hw/usb/desc.h"
7 /* ------------------------------------------------------------------ */
9 static uint8_t usb_lo(uint16_t val
)
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
;
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
;
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
;
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;
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
;
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
]),
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
);
120 d
->u
.config
.wTotalLength_lo
= usb_lo(wTotalLength
);
121 d
->u
.config
.wTotalLength_hi
= usb_hi(wTotalLength
);
125 int usb_desc_iface_group(const USBDescIfaceAssoc
*iad
, uint8_t *dest
,
131 /* handle interface association descriptor */
132 uint8_t bLength
= 0x08;
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
;
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
);
160 int usb_desc_iface(const USBDescIface
*iface
, uint8_t *dest
, size_t len
)
162 uint8_t bLength
= 0x09;
164 USBDescriptor
*d
= (void *)dest
;
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
;
182 for (i
= 0; i
< iface
->ndesc
; i
++) {
183 rc
= usb_desc_other(iface
->descs
+ i
, dest
+ pos
, len
- pos
);
190 for (i
= 0; i
< iface
->bNumEndpoints
; i
++) {
191 rc
= usb_desc_endpoint(iface
->eps
+ i
, dest
+ pos
, len
- 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
) {
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
;
220 d
->u
.endpoint
.bRefresh
= ep
->bRefresh
;
221 d
->u
.endpoint
.bSynchAddress
= ep
->bSynchAddress
;
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];
238 memcpy(dest
, desc
->data
, bLength
);
242 /* ------------------------------------------------------------------ */
244 static void usb_desc_ep_init(USBDevice
*dev
)
246 const USBDescIface
*iface
;
250 for (i
= 0; i
< dev
->ninterfaces
; i
++) {
251 iface
= dev
->ifaces
[i
];
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
,
270 const USBDescIface
*iface
;
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
) {
285 for (i
= 0; i
< dev
->config
->nif
; i
++) {
286 iface
= &dev
->config
->ifs
[i
];
287 if (iface
->bInterfaceNumber
== nif
&&
288 iface
->bAlternateSetting
== alt
) {
295 static int usb_desc_set_interface(USBDevice
*dev
, int index
, int value
)
297 const USBDescIface
*iface
;
300 iface
= usb_desc_find_interface(dev
, index
, value
);
305 old
= dev
->altsetting
[index
];
306 dev
->altsetting
[index
] = value
;
307 dev
->ifaces
[index
] = iface
;
308 usb_desc_ep_init(dev
);
311 usb_device_set_interface(dev
, index
, old
, value
);
316 static int usb_desc_set_config(USBDevice
*dev
, int value
)
321 dev
->configuration
= 0;
322 dev
->ninterfaces
= 0;
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
) {
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
;
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
) {
357 dev
->device
= desc
->full
;
360 dev
->device
= desc
->high
;
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
;
374 dev
->speedmask
|= USB_SPEED_MASK_FULL
;
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
;
392 fprintf(stderr
, "usb: port/device speed mismatch for \"%s\"\n",
393 usb_device_get_product_desc(dev
));
396 usb_desc_setdefaults(dev
);
399 void usb_desc_set_string(USBDevice
*dev
, uint8_t index
, const char *str
)
403 QLIST_FOREACH(s
, &dev
->strings
, next
) {
404 if (s
->index
== index
) {
409 s
= g_malloc0(sizeof(*s
));
411 QLIST_INSERT_HEAD(&dev
->strings
, s
, next
);
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
;
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
);
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
)
452 QLIST_FOREACH(s
, &dev
->strings
, next
) {
453 if (s
->index
== index
) {
460 int usb_desc_string(USBDevice
*dev
, int index
, uint8_t *dest
, size_t len
)
462 uint8_t bLength
, pos
, i
;
472 dest
[1] = USB_DT_STRING
;
478 str
= usb_desc_get_string(dev
, index
);
480 str
= usb_device_get_usb_desc(dev
)->str
[index
];
486 bLength
= strlen(str
) * 2 + 2;
488 dest
[1] = USB_DT_STRING
;
490 while (pos
+1 < bLength
&& pos
+1 < len
) {
491 dest
[pos
++] = str
[i
++];
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
;
502 uint8_t type
= value
>> 8;
503 uint8_t index
= value
& 0xff;
506 if (dev
->speed
== USB_SPEED_HIGH
) {
507 other_dev
= usb_device_get_usb_desc(dev
)->full
;
509 other_dev
= usb_device_get_usb_desc(dev
)->high
;
514 ret
= usb_desc_device(&desc
->id
, dev
->device
, buf
, sizeof(buf
));
515 trace_usb_desc_device(dev
->addr
, len
, ret
);
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
);
524 ret
= usb_desc_string(dev
, index
, buf
, sizeof(buf
));
525 trace_usb_desc_string(dev
->addr
, index
, len
, ret
);
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
);
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
);
543 /* ignore silently */
547 fprintf(stderr
, "%s: %d unknown type %d (len %zd)\n", __FUNCTION__
,
548 dev
->addr
, type
, len
);
556 memcpy(dest
, buf
, 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
);
567 assert(desc
!= NULL
);
569 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
571 trace_usb_set_addr(dev
->addr
);
575 case DeviceRequest
| USB_REQ_GET_DESCRIPTOR
:
576 ret
= usb_desc_get_descriptor(dev
, value
, data
, length
);
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;
587 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
588 ret
= usb_desc_set_config(dev
, value
);
589 trace_usb_set_config(dev
->addr
, value
, ret
);
592 case DeviceRequest
| USB_REQ_GET_STATUS
: {
593 const USBDescConfig
*config
= dev
->config
?
594 dev
->config
: &dev
->device
->confs
[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
;
613 case DeviceOutRequest
| USB_REQ_CLEAR_FEATURE
:
614 if (value
== USB_DEVICE_REMOTE_WAKEUP
) {
615 dev
->remote_wakeup
= 0;
618 trace_usb_clear_device_feature(dev
->addr
, value
, ret
);
620 case DeviceOutRequest
| USB_REQ_SET_FEATURE
:
621 if (value
== USB_DEVICE_REMOTE_WAKEUP
) {
622 dev
->remote_wakeup
= 1;
625 trace_usb_set_device_feature(dev
->addr
, value
, ret
);
628 case InterfaceRequest
| USB_REQ_GET_INTERFACE
:
629 if (index
< 0 || index
>= dev
->ninterfaces
) {
632 data
[0] = dev
->altsetting
[index
];
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
);