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;
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
;
51 int usb_desc_device_qualifier(const USBDescDevice
*dev
,
52 uint8_t *dest
, size_t len
)
54 uint8_t bLength
= 0x0a;
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 */
75 int usb_desc_config(const USBDescConfig
*conf
, uint8_t *dest
, size_t len
)
77 uint8_t bLength
= 0x09;
78 uint16_t wTotalLength
= 0;
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
]),
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
);
114 dest
[0x02] = usb_lo(wTotalLength
);
115 dest
[0x03] = usb_hi(wTotalLength
);
119 int usb_desc_iface_group(const USBDescIfaceAssoc
*iad
, uint8_t *dest
,
125 /* handle interface association descriptor */
126 uint8_t bLength
= 0x08;
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
;
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
);
154 int usb_desc_iface(const USBDescIface
*iface
, uint8_t *dest
, size_t len
)
156 uint8_t bLength
= 0x09;
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
;
174 for (i
= 0; i
< iface
->ndesc
; i
++) {
175 rc
= usb_desc_other(iface
->descs
+ i
, dest
+ pos
, len
- pos
);
182 for (i
= 0; i
< iface
->bNumEndpoints
; i
++) {
183 rc
= usb_desc_endpoint(iface
->eps
+ i
, dest
+ pos
, len
- pos
);
193 int usb_desc_endpoint(const USBDescEndpoint
*ep
, uint8_t *dest
, size_t len
)
195 uint8_t bLength
= 0x07;
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
;
212 int usb_desc_other(const USBDescOther
*desc
, uint8_t *dest
, size_t len
)
214 int bLength
= desc
->length
? desc
->length
: desc
->data
[0];
220 memcpy(dest
, desc
->data
, bLength
);
224 /* ------------------------------------------------------------------ */
226 static void usb_desc_setdefaults(USBDevice
*dev
)
228 const USBDesc
*desc
= dev
->info
->usb_desc
;
230 assert(desc
!= NULL
);
231 switch (dev
->speed
) {
234 dev
->device
= desc
->full
;
237 dev
->device
= desc
->high
;
240 dev
->config
= dev
->device
->confs
;
243 void usb_desc_init(USBDevice
*dev
)
245 dev
->speed
= USB_SPEED_FULL
;
246 usb_desc_setdefaults(dev
);
249 void usb_desc_attach(USBDevice
*dev
)
251 const USBDesc
*desc
= dev
->info
->usb_desc
;
253 assert(desc
!= NULL
);
254 if (desc
->high
&& (dev
->port
->speedmask
& USB_SPEED_MASK_HIGH
)) {
255 dev
->speed
= USB_SPEED_HIGH
;
256 } else if (desc
->full
&& (dev
->port
->speedmask
& USB_SPEED_MASK_FULL
)) {
257 dev
->speed
= USB_SPEED_FULL
;
259 fprintf(stderr
, "usb: port/device speed mismatch for \"%s\"\n",
260 dev
->info
->product_desc
);
263 usb_desc_setdefaults(dev
);
266 void usb_desc_set_string(USBDevice
*dev
, uint8_t index
, const char *str
)
270 QLIST_FOREACH(s
, &dev
->strings
, next
) {
271 if (s
->index
== index
) {
276 s
= qemu_mallocz(sizeof(*s
));
278 QLIST_INSERT_HEAD(&dev
->strings
, s
, next
);
281 s
->str
= qemu_strdup(str
);
284 const char *usb_desc_get_string(USBDevice
*dev
, uint8_t index
)
288 QLIST_FOREACH(s
, &dev
->strings
, next
) {
289 if (s
->index
== index
) {
296 int usb_desc_string(USBDevice
*dev
, int index
, uint8_t *dest
, size_t len
)
298 uint8_t bLength
, pos
, i
;
308 dest
[1] = USB_DT_STRING
;
314 str
= usb_desc_get_string(dev
, index
);
316 str
= dev
->info
->usb_desc
->str
[index
];
322 bLength
= strlen(str
) * 2 + 2;
324 dest
[1] = USB_DT_STRING
;
326 while (pos
+1 < bLength
&& pos
+1 < len
) {
327 dest
[pos
++] = str
[i
++];
333 int usb_desc_get_descriptor(USBDevice
*dev
, int value
, uint8_t *dest
, size_t len
)
335 const USBDesc
*desc
= dev
->info
->usb_desc
;
336 const USBDescDevice
*other_dev
;
338 uint8_t type
= value
>> 8;
339 uint8_t index
= value
& 0xff;
342 if (dev
->speed
== USB_SPEED_HIGH
) {
343 other_dev
= dev
->info
->usb_desc
->full
;
345 other_dev
= dev
->info
->usb_desc
->high
;
350 ret
= usb_desc_device(&desc
->id
, dev
->device
, buf
, sizeof(buf
));
351 trace_usb_desc_device(dev
->addr
, len
, ret
);
354 if (index
< dev
->device
->bNumConfigurations
) {
355 ret
= usb_desc_config(dev
->device
->confs
+ index
, buf
, sizeof(buf
));
357 trace_usb_desc_config(dev
->addr
, index
, len
, ret
);
360 ret
= usb_desc_string(dev
, index
, buf
, sizeof(buf
));
361 trace_usb_desc_string(dev
->addr
, index
, len
, ret
);
364 case USB_DT_DEVICE_QUALIFIER
:
365 if (other_dev
!= NULL
) {
366 ret
= usb_desc_device_qualifier(other_dev
, buf
, sizeof(buf
));
368 trace_usb_desc_device_qualifier(dev
->addr
, len
, ret
);
370 case USB_DT_OTHER_SPEED_CONFIG
:
371 if (other_dev
!= NULL
&& index
< other_dev
->bNumConfigurations
) {
372 ret
= usb_desc_config(other_dev
->confs
+ index
, buf
, sizeof(buf
));
373 buf
[0x01] = USB_DT_OTHER_SPEED_CONFIG
;
375 trace_usb_desc_other_speed_config(dev
->addr
, index
, len
, ret
);
379 fprintf(stderr
, "%s: %d unknown type %d (len %zd)\n", __FUNCTION__
,
380 dev
->addr
, type
, len
);
388 memcpy(dest
, buf
, ret
);
393 int usb_desc_handle_control(USBDevice
*dev
, USBPacket
*p
,
394 int request
, int value
, int index
, int length
, uint8_t *data
)
396 const USBDesc
*desc
= dev
->info
->usb_desc
;
399 assert(desc
!= NULL
);
401 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
403 trace_usb_set_addr(dev
->addr
);
407 case DeviceRequest
| USB_REQ_GET_DESCRIPTOR
:
408 ret
= usb_desc_get_descriptor(dev
, value
, data
, length
);
411 case DeviceRequest
| USB_REQ_GET_CONFIGURATION
:
412 data
[0] = dev
->config
->bConfigurationValue
;
415 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
416 for (i
= 0; i
< dev
->device
->bNumConfigurations
; i
++) {
417 if (dev
->device
->confs
[i
].bConfigurationValue
== value
) {
418 dev
->config
= dev
->device
->confs
+ i
;
422 trace_usb_set_config(dev
->addr
, value
, ret
);
425 case DeviceRequest
| USB_REQ_GET_STATUS
:
427 if (dev
->config
->bmAttributes
& 0x40) {
428 data
[0] |= 1 << USB_DEVICE_SELF_POWERED
;
430 if (dev
->remote_wakeup
) {
431 data
[0] |= 1 << USB_DEVICE_REMOTE_WAKEUP
;
436 case DeviceOutRequest
| USB_REQ_CLEAR_FEATURE
:
437 if (value
== USB_DEVICE_REMOTE_WAKEUP
) {
438 dev
->remote_wakeup
= 0;
441 trace_usb_clear_device_feature(dev
->addr
, value
, ret
);
443 case DeviceOutRequest
| USB_REQ_SET_FEATURE
:
444 if (value
== USB_DEVICE_REMOTE_WAKEUP
) {
445 dev
->remote_wakeup
= 1;
448 trace_usb_set_device_feature(dev
->addr
, value
, ret
);