2 #include "hw/usb/desc.h"
5 /* ------------------------------------------------------------------ */
7 static uint8_t usb_lo(uint16_t val
)
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;
21 USBDescriptor
*d
= (void *)dest
;
28 d
->bDescriptorType
= USB_DT_DEVICE
;
30 d
->u
.device
.bcdUSB_lo
= usb_lo(dev
->bcdUSB
);
31 d
->u
.device
.bcdUSB_hi
= usb_hi(dev
->bcdUSB
);
32 d
->u
.device
.bDeviceClass
= dev
->bDeviceClass
;
33 d
->u
.device
.bDeviceSubClass
= dev
->bDeviceSubClass
;
34 d
->u
.device
.bDeviceProtocol
= dev
->bDeviceProtocol
;
35 d
->u
.device
.bMaxPacketSize0
= dev
->bMaxPacketSize0
;
37 d
->u
.device
.idVendor_lo
= usb_lo(id
->idVendor
);
38 d
->u
.device
.idVendor_hi
= usb_hi(id
->idVendor
);
39 d
->u
.device
.idProduct_lo
= usb_lo(id
->idProduct
);
40 d
->u
.device
.idProduct_hi
= usb_hi(id
->idProduct
);
41 d
->u
.device
.bcdDevice_lo
= usb_lo(id
->bcdDevice
);
42 d
->u
.device
.bcdDevice_hi
= usb_hi(id
->bcdDevice
);
43 d
->u
.device
.iManufacturer
= id
->iManufacturer
;
44 d
->u
.device
.iProduct
= id
->iProduct
;
45 d
->u
.device
.iSerialNumber
= id
->iSerialNumber
;
47 d
->u
.device
.bNumConfigurations
= dev
->bNumConfigurations
;
52 int usb_desc_device_qualifier(const USBDescDevice
*dev
,
53 uint8_t *dest
, size_t len
)
55 uint8_t bLength
= 0x0a;
56 USBDescriptor
*d
= (void *)dest
;
63 d
->bDescriptorType
= USB_DT_DEVICE_QUALIFIER
;
65 d
->u
.device_qualifier
.bcdUSB_lo
= usb_lo(dev
->bcdUSB
);
66 d
->u
.device_qualifier
.bcdUSB_hi
= usb_hi(dev
->bcdUSB
);
67 d
->u
.device_qualifier
.bDeviceClass
= dev
->bDeviceClass
;
68 d
->u
.device_qualifier
.bDeviceSubClass
= dev
->bDeviceSubClass
;
69 d
->u
.device_qualifier
.bDeviceProtocol
= dev
->bDeviceProtocol
;
70 d
->u
.device_qualifier
.bMaxPacketSize0
= dev
->bMaxPacketSize0
;
71 d
->u
.device_qualifier
.bNumConfigurations
= dev
->bNumConfigurations
;
72 d
->u
.device_qualifier
.bReserved
= 0;
77 int usb_desc_config(const USBDescConfig
*conf
, uint8_t *dest
, size_t len
)
79 uint8_t bLength
= 0x09;
80 uint16_t wTotalLength
= 0;
88 dest
[0x01] = USB_DT_CONFIG
;
89 dest
[0x04] = conf
->bNumInterfaces
;
90 dest
[0x05] = conf
->bConfigurationValue
;
91 dest
[0x06] = conf
->iConfiguration
;
92 dest
[0x07] = conf
->bmAttributes
;
93 dest
[0x08] = conf
->bMaxPower
;
94 wTotalLength
+= bLength
;
96 /* handle grouped interfaces if any*/
97 for (i
= 0; i
< conf
->nif_groups
; i
++) {
98 rc
= usb_desc_iface_group(&(conf
->if_groups
[i
]),
107 /* handle normal (ungrouped / no IAD) interfaces if any */
108 for (i
= 0; i
< conf
->nif
; i
++) {
109 rc
= usb_desc_iface(conf
->ifs
+ i
, dest
+ wTotalLength
, len
- wTotalLength
);
116 dest
[0x02] = usb_lo(wTotalLength
);
117 dest
[0x03] = usb_hi(wTotalLength
);
121 int usb_desc_iface_group(const USBDescIfaceAssoc
*iad
, uint8_t *dest
,
127 /* handle interface association descriptor */
128 uint8_t bLength
= 0x08;
134 dest
[0x00] = bLength
;
135 dest
[0x01] = USB_DT_INTERFACE_ASSOC
;
136 dest
[0x02] = iad
->bFirstInterface
;
137 dest
[0x03] = iad
->bInterfaceCount
;
138 dest
[0x04] = iad
->bFunctionClass
;
139 dest
[0x05] = iad
->bFunctionSubClass
;
140 dest
[0x06] = iad
->bFunctionProtocol
;
141 dest
[0x07] = iad
->iFunction
;
144 /* handle associated interfaces in this group */
145 for (i
= 0; i
< iad
->nif
; i
++) {
146 int rc
= usb_desc_iface(&(iad
->ifs
[i
]), dest
+ pos
, len
- pos
);
156 int usb_desc_iface(const USBDescIface
*iface
, uint8_t *dest
, size_t len
)
158 uint8_t bLength
= 0x09;
165 dest
[0x00] = bLength
;
166 dest
[0x01] = USB_DT_INTERFACE
;
167 dest
[0x02] = iface
->bInterfaceNumber
;
168 dest
[0x03] = iface
->bAlternateSetting
;
169 dest
[0x04] = iface
->bNumEndpoints
;
170 dest
[0x05] = iface
->bInterfaceClass
;
171 dest
[0x06] = iface
->bInterfaceSubClass
;
172 dest
[0x07] = iface
->bInterfaceProtocol
;
173 dest
[0x08] = iface
->iInterface
;
176 for (i
= 0; i
< iface
->ndesc
; i
++) {
177 rc
= usb_desc_other(iface
->descs
+ i
, dest
+ pos
, len
- pos
);
184 for (i
= 0; i
< iface
->bNumEndpoints
; i
++) {
185 rc
= usb_desc_endpoint(iface
->eps
+ i
, dest
+ pos
, len
- pos
);
195 int usb_desc_endpoint(const USBDescEndpoint
*ep
, uint8_t *dest
, size_t len
)
197 uint8_t bLength
= ep
->is_audio
? 0x09 : 0x07;
198 uint8_t extralen
= ep
->extra
? ep
->extra
[0] : 0;
200 if (len
< bLength
+ extralen
) {
204 dest
[0x00] = bLength
;
205 dest
[0x01] = USB_DT_ENDPOINT
;
206 dest
[0x02] = ep
->bEndpointAddress
;
207 dest
[0x03] = ep
->bmAttributes
;
208 dest
[0x04] = usb_lo(ep
->wMaxPacketSize
);
209 dest
[0x05] = usb_hi(ep
->wMaxPacketSize
);
210 dest
[0x06] = ep
->bInterval
;
212 dest
[0x07] = ep
->bRefresh
;
213 dest
[0x08] = ep
->bSynchAddress
;
216 memcpy(dest
+ bLength
, ep
->extra
, extralen
);
219 return bLength
+ extralen
;
222 int usb_desc_other(const USBDescOther
*desc
, uint8_t *dest
, size_t len
)
224 int bLength
= desc
->length
? desc
->length
: desc
->data
[0];
230 memcpy(dest
, desc
->data
, bLength
);
234 /* ------------------------------------------------------------------ */
236 static void usb_desc_ep_init(USBDevice
*dev
)
238 const USBDescIface
*iface
;
242 for (i
= 0; i
< dev
->ninterfaces
; i
++) {
243 iface
= dev
->ifaces
[i
];
247 for (e
= 0; e
< iface
->bNumEndpoints
; e
++) {
248 pid
= (iface
->eps
[e
].bEndpointAddress
& USB_DIR_IN
) ?
249 USB_TOKEN_IN
: USB_TOKEN_OUT
;
250 ep
= iface
->eps
[e
].bEndpointAddress
& 0x0f;
251 usb_ep_set_type(dev
, pid
, ep
, iface
->eps
[e
].bmAttributes
& 0x03);
252 usb_ep_set_ifnum(dev
, pid
, ep
, iface
->bInterfaceNumber
);
253 usb_ep_set_max_packet_size(dev
, pid
, ep
,
254 iface
->eps
[e
].wMaxPacketSize
);
259 static const USBDescIface
*usb_desc_find_interface(USBDevice
*dev
,
262 const USBDescIface
*iface
;
268 for (g
= 0; g
< dev
->config
->nif_groups
; g
++) {
269 for (i
= 0; i
< dev
->config
->if_groups
[g
].nif
; i
++) {
270 iface
= &dev
->config
->if_groups
[g
].ifs
[i
];
271 if (iface
->bInterfaceNumber
== nif
&&
272 iface
->bAlternateSetting
== alt
) {
277 for (i
= 0; i
< dev
->config
->nif
; i
++) {
278 iface
= &dev
->config
->ifs
[i
];
279 if (iface
->bInterfaceNumber
== nif
&&
280 iface
->bAlternateSetting
== alt
) {
287 static int usb_desc_set_interface(USBDevice
*dev
, int index
, int value
)
289 const USBDescIface
*iface
;
292 iface
= usb_desc_find_interface(dev
, index
, value
);
297 old
= dev
->altsetting
[index
];
298 dev
->altsetting
[index
] = value
;
299 dev
->ifaces
[index
] = iface
;
300 usb_desc_ep_init(dev
);
303 usb_device_set_interface(dev
, index
, old
, value
);
308 static int usb_desc_set_config(USBDevice
*dev
, int value
)
313 dev
->configuration
= 0;
314 dev
->ninterfaces
= 0;
317 for (i
= 0; i
< dev
->device
->bNumConfigurations
; i
++) {
318 if (dev
->device
->confs
[i
].bConfigurationValue
== value
) {
319 dev
->configuration
= value
;
320 dev
->ninterfaces
= dev
->device
->confs
[i
].bNumInterfaces
;
321 dev
->config
= dev
->device
->confs
+ i
;
322 assert(dev
->ninterfaces
<= USB_MAX_INTERFACES
);
325 if (i
< dev
->device
->bNumConfigurations
) {
330 for (i
= 0; i
< dev
->ninterfaces
; i
++) {
331 usb_desc_set_interface(dev
, i
, 0);
333 for (; i
< USB_MAX_INTERFACES
; i
++) {
334 dev
->altsetting
[i
] = 0;
335 dev
->ifaces
[i
] = NULL
;
341 static void usb_desc_setdefaults(USBDevice
*dev
)
343 const USBDesc
*desc
= usb_device_get_usb_desc(dev
);
345 assert(desc
!= NULL
);
346 switch (dev
->speed
) {
349 dev
->device
= desc
->full
;
352 dev
->device
= desc
->high
;
355 usb_desc_set_config(dev
, 0);
358 void usb_desc_init(USBDevice
*dev
)
360 const USBDesc
*desc
= usb_device_get_usb_desc(dev
);
362 assert(desc
!= NULL
);
363 dev
->speed
= USB_SPEED_FULL
;
366 dev
->speedmask
|= USB_SPEED_MASK_FULL
;
369 dev
->speedmask
|= USB_SPEED_MASK_HIGH
;
371 usb_desc_setdefaults(dev
);
374 void usb_desc_attach(USBDevice
*dev
)
376 const USBDesc
*desc
= usb_device_get_usb_desc(dev
);
378 assert(desc
!= NULL
);
379 if (desc
->high
&& (dev
->port
->speedmask
& USB_SPEED_MASK_HIGH
)) {
380 dev
->speed
= USB_SPEED_HIGH
;
381 } else if (desc
->full
&& (dev
->port
->speedmask
& USB_SPEED_MASK_FULL
)) {
382 dev
->speed
= USB_SPEED_FULL
;
384 fprintf(stderr
, "usb: port/device speed mismatch for \"%s\"\n",
385 usb_device_get_product_desc(dev
));
388 usb_desc_setdefaults(dev
);
391 void usb_desc_set_string(USBDevice
*dev
, uint8_t index
, const char *str
)
395 QLIST_FOREACH(s
, &dev
->strings
, next
) {
396 if (s
->index
== index
) {
401 s
= g_malloc0(sizeof(*s
));
403 QLIST_INSERT_HEAD(&dev
->strings
, s
, next
);
406 s
->str
= g_strdup(str
);
409 const char *usb_desc_get_string(USBDevice
*dev
, uint8_t index
)
413 QLIST_FOREACH(s
, &dev
->strings
, next
) {
414 if (s
->index
== index
) {
421 int usb_desc_string(USBDevice
*dev
, int index
, uint8_t *dest
, size_t len
)
423 uint8_t bLength
, pos
, i
;
433 dest
[1] = USB_DT_STRING
;
439 str
= usb_desc_get_string(dev
, index
);
441 str
= usb_device_get_usb_desc(dev
)->str
[index
];
447 bLength
= strlen(str
) * 2 + 2;
449 dest
[1] = USB_DT_STRING
;
451 while (pos
+1 < bLength
&& pos
+1 < len
) {
452 dest
[pos
++] = str
[i
++];
458 int usb_desc_get_descriptor(USBDevice
*dev
, int value
, uint8_t *dest
, size_t len
)
460 const USBDesc
*desc
= usb_device_get_usb_desc(dev
);
461 const USBDescDevice
*other_dev
;
463 uint8_t type
= value
>> 8;
464 uint8_t index
= value
& 0xff;
467 if (dev
->speed
== USB_SPEED_HIGH
) {
468 other_dev
= usb_device_get_usb_desc(dev
)->full
;
470 other_dev
= usb_device_get_usb_desc(dev
)->high
;
475 ret
= usb_desc_device(&desc
->id
, dev
->device
, buf
, sizeof(buf
));
476 trace_usb_desc_device(dev
->addr
, len
, ret
);
479 if (index
< dev
->device
->bNumConfigurations
) {
480 ret
= usb_desc_config(dev
->device
->confs
+ index
, buf
, sizeof(buf
));
482 trace_usb_desc_config(dev
->addr
, index
, len
, ret
);
485 ret
= usb_desc_string(dev
, index
, buf
, sizeof(buf
));
486 trace_usb_desc_string(dev
->addr
, index
, len
, ret
);
489 case USB_DT_DEVICE_QUALIFIER
:
490 if (other_dev
!= NULL
) {
491 ret
= usb_desc_device_qualifier(other_dev
, buf
, sizeof(buf
));
493 trace_usb_desc_device_qualifier(dev
->addr
, len
, ret
);
495 case USB_DT_OTHER_SPEED_CONFIG
:
496 if (other_dev
!= NULL
&& index
< other_dev
->bNumConfigurations
) {
497 ret
= usb_desc_config(other_dev
->confs
+ index
, buf
, sizeof(buf
));
498 buf
[0x01] = USB_DT_OTHER_SPEED_CONFIG
;
500 trace_usb_desc_other_speed_config(dev
->addr
, index
, len
, ret
);
504 /* ignore silently */
508 fprintf(stderr
, "%s: %d unknown type %d (len %zd)\n", __FUNCTION__
,
509 dev
->addr
, type
, len
);
517 memcpy(dest
, buf
, ret
);
522 int usb_desc_handle_control(USBDevice
*dev
, USBPacket
*p
,
523 int request
, int value
, int index
, int length
, uint8_t *data
)
525 const USBDesc
*desc
= usb_device_get_usb_desc(dev
);
528 assert(desc
!= NULL
);
530 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
532 trace_usb_set_addr(dev
->addr
);
536 case DeviceRequest
| USB_REQ_GET_DESCRIPTOR
:
537 ret
= usb_desc_get_descriptor(dev
, value
, data
, length
);
540 case DeviceRequest
| USB_REQ_GET_CONFIGURATION
:
542 * 9.4.2: 0 should be returned if the device is unconfigured, otherwise
543 * the non zero value of bConfigurationValue.
545 data
[0] = dev
->config
? dev
->config
->bConfigurationValue
: 0;
548 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
549 ret
= usb_desc_set_config(dev
, value
);
550 trace_usb_set_config(dev
->addr
, value
, ret
);
553 case DeviceRequest
| USB_REQ_GET_STATUS
: {
554 const USBDescConfig
*config
= dev
->config
?
555 dev
->config
: &dev
->device
->confs
[0];
559 * Default state: Device behavior when this request is received while
560 * the device is in the Default state is not specified.
561 * We return the same value that a configured device would return if
562 * it used the first configuration.
564 if (config
->bmAttributes
& 0x40) {
565 data
[0] |= 1 << USB_DEVICE_SELF_POWERED
;
567 if (dev
->remote_wakeup
) {
568 data
[0] |= 1 << USB_DEVICE_REMOTE_WAKEUP
;
574 case DeviceOutRequest
| USB_REQ_CLEAR_FEATURE
:
575 if (value
== USB_DEVICE_REMOTE_WAKEUP
) {
576 dev
->remote_wakeup
= 0;
579 trace_usb_clear_device_feature(dev
->addr
, value
, ret
);
581 case DeviceOutRequest
| USB_REQ_SET_FEATURE
:
582 if (value
== USB_DEVICE_REMOTE_WAKEUP
) {
583 dev
->remote_wakeup
= 1;
586 trace_usb_set_device_feature(dev
->addr
, value
, ret
);
589 case InterfaceRequest
| USB_REQ_GET_INTERFACE
:
590 if (index
< 0 || index
>= dev
->ninterfaces
) {
593 data
[0] = dev
->altsetting
[index
];
596 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
597 ret
= usb_desc_set_interface(dev
, index
, value
);
598 trace_usb_set_interface(dev
->addr
, index
, value
, ret
);