root / hw / usb-desc.c @ fef13fa8
History | View | Annotate | Download (10.2 kB)
1 |
#include "usb.h" |
---|---|
2 |
#include "usb-desc.h" |
3 |
#include "trace.h" |
4 |
|
5 |
/* ------------------------------------------------------------------ */
|
6 |
|
7 |
static uint8_t usb_lo(uint16_t val)
|
8 |
{ |
9 |
return val & 0xff; |
10 |
} |
11 |
|
12 |
static uint8_t usb_hi(uint16_t val)
|
13 |
{ |
14 |
return (val >> 8) & 0xff; |
15 |
} |
16 |
|
17 |
int usb_desc_device(const USBDescID *id, const USBDescDevice *dev, |
18 |
uint8_t *dest, size_t len) |
19 |
{ |
20 |
uint8_t bLength = 0x12;
|
21 |
|
22 |
if (len < bLength) {
|
23 |
return -1; |
24 |
} |
25 |
|
26 |
dest[0x00] = bLength;
|
27 |
dest[0x01] = USB_DT_DEVICE;
|
28 |
|
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;
|
35 |
|
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;
|
45 |
|
46 |
dest[0x11] = dev->bNumConfigurations;
|
47 |
|
48 |
return bLength;
|
49 |
} |
50 |
|
51 |
int usb_desc_device_qualifier(const USBDescDevice *dev, |
52 |
uint8_t *dest, size_t len) |
53 |
{ |
54 |
uint8_t bLength = 0x0a;
|
55 |
|
56 |
if (len < bLength) {
|
57 |
return -1; |
58 |
} |
59 |
|
60 |
dest[0x00] = bLength;
|
61 |
dest[0x01] = USB_DT_DEVICE_QUALIFIER;
|
62 |
|
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 */ |
71 |
|
72 |
return bLength;
|
73 |
} |
74 |
|
75 |
int usb_desc_config(const USBDescConfig *conf, uint8_t *dest, size_t len) |
76 |
{ |
77 |
uint8_t bLength = 0x09;
|
78 |
uint16_t wTotalLength = 0;
|
79 |
int i, rc;
|
80 |
|
81 |
if (len < bLength) {
|
82 |
return -1; |
83 |
} |
84 |
|
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; |
93 |
|
94 |
for (i = 0; i < conf->nif; i++) { |
95 |
rc = usb_desc_iface(conf->ifs + i, dest + wTotalLength, len - wTotalLength); |
96 |
if (rc < 0) { |
97 |
return rc;
|
98 |
} |
99 |
wTotalLength += rc; |
100 |
} |
101 |
|
102 |
dest[0x02] = usb_lo(wTotalLength);
|
103 |
dest[0x03] = usb_hi(wTotalLength);
|
104 |
return wTotalLength;
|
105 |
} |
106 |
|
107 |
int usb_desc_iface(const USBDescIface *iface, uint8_t *dest, size_t len) |
108 |
{ |
109 |
uint8_t bLength = 0x09;
|
110 |
int i, rc, pos = 0; |
111 |
|
112 |
if (len < bLength) {
|
113 |
return -1; |
114 |
} |
115 |
|
116 |
dest[0x00] = bLength;
|
117 |
dest[0x01] = USB_DT_INTERFACE;
|
118 |
dest[0x02] = iface->bInterfaceNumber;
|
119 |
dest[0x03] = iface->bAlternateSetting;
|
120 |
dest[0x04] = iface->bNumEndpoints;
|
121 |
dest[0x05] = iface->bInterfaceClass;
|
122 |
dest[0x06] = iface->bInterfaceSubClass;
|
123 |
dest[0x07] = iface->bInterfaceProtocol;
|
124 |
dest[0x08] = iface->iInterface;
|
125 |
pos += bLength; |
126 |
|
127 |
for (i = 0; i < iface->ndesc; i++) { |
128 |
rc = usb_desc_other(iface->descs + i, dest + pos, len - pos); |
129 |
if (rc < 0) { |
130 |
return rc;
|
131 |
} |
132 |
pos += rc; |
133 |
} |
134 |
|
135 |
for (i = 0; i < iface->bNumEndpoints; i++) { |
136 |
rc = usb_desc_endpoint(iface->eps + i, dest + pos, len - pos); |
137 |
if (rc < 0) { |
138 |
return rc;
|
139 |
} |
140 |
pos += rc; |
141 |
} |
142 |
|
143 |
return pos;
|
144 |
} |
145 |
|
146 |
int usb_desc_endpoint(const USBDescEndpoint *ep, uint8_t *dest, size_t len) |
147 |
{ |
148 |
uint8_t bLength = 0x07;
|
149 |
|
150 |
if (len < bLength) {
|
151 |
return -1; |
152 |
} |
153 |
|
154 |
dest[0x00] = bLength;
|
155 |
dest[0x01] = USB_DT_ENDPOINT;
|
156 |
dest[0x02] = ep->bEndpointAddress;
|
157 |
dest[0x03] = ep->bmAttributes;
|
158 |
dest[0x04] = usb_lo(ep->wMaxPacketSize);
|
159 |
dest[0x05] = usb_hi(ep->wMaxPacketSize);
|
160 |
dest[0x06] = ep->bInterval;
|
161 |
|
162 |
return bLength;
|
163 |
} |
164 |
|
165 |
int usb_desc_other(const USBDescOther *desc, uint8_t *dest, size_t len) |
166 |
{ |
167 |
int bLength = desc->length ? desc->length : desc->data[0]; |
168 |
|
169 |
if (len < bLength) {
|
170 |
return -1; |
171 |
} |
172 |
|
173 |
memcpy(dest, desc->data, bLength); |
174 |
return bLength;
|
175 |
} |
176 |
|
177 |
/* ------------------------------------------------------------------ */
|
178 |
|
179 |
static void usb_desc_setdefaults(USBDevice *dev) |
180 |
{ |
181 |
const USBDesc *desc = dev->info->usb_desc;
|
182 |
|
183 |
assert(desc != NULL);
|
184 |
switch (dev->speed) {
|
185 |
case USB_SPEED_LOW:
|
186 |
case USB_SPEED_FULL:
|
187 |
dev->device = desc->full; |
188 |
break;
|
189 |
case USB_SPEED_HIGH:
|
190 |
dev->device = desc->high; |
191 |
break;
|
192 |
} |
193 |
dev->config = dev->device->confs; |
194 |
} |
195 |
|
196 |
void usb_desc_init(USBDevice *dev)
|
197 |
{ |
198 |
dev->speed = USB_SPEED_FULL; |
199 |
usb_desc_setdefaults(dev); |
200 |
} |
201 |
|
202 |
void usb_desc_attach(USBDevice *dev)
|
203 |
{ |
204 |
const USBDesc *desc = dev->info->usb_desc;
|
205 |
|
206 |
assert(desc != NULL);
|
207 |
if (desc->high && (dev->port->speedmask & USB_SPEED_MASK_HIGH)) {
|
208 |
dev->speed = USB_SPEED_HIGH; |
209 |
} else if (desc->full && (dev->port->speedmask & USB_SPEED_MASK_FULL)) { |
210 |
dev->speed = USB_SPEED_FULL; |
211 |
} else {
|
212 |
fprintf(stderr, "usb: port/device speed mismatch for \"%s\"\n",
|
213 |
dev->info->product_desc); |
214 |
return;
|
215 |
} |
216 |
usb_desc_setdefaults(dev); |
217 |
} |
218 |
|
219 |
void usb_desc_set_string(USBDevice *dev, uint8_t index, const char *str) |
220 |
{ |
221 |
USBDescString *s; |
222 |
|
223 |
QLIST_FOREACH(s, &dev->strings, next) { |
224 |
if (s->index == index) {
|
225 |
break;
|
226 |
} |
227 |
} |
228 |
if (s == NULL) { |
229 |
s = qemu_mallocz(sizeof(*s));
|
230 |
s->index = index; |
231 |
QLIST_INSERT_HEAD(&dev->strings, s, next); |
232 |
} |
233 |
qemu_free(s->str); |
234 |
s->str = qemu_strdup(str); |
235 |
} |
236 |
|
237 |
const char *usb_desc_get_string(USBDevice *dev, uint8_t index) |
238 |
{ |
239 |
USBDescString *s; |
240 |
|
241 |
QLIST_FOREACH(s, &dev->strings, next) { |
242 |
if (s->index == index) {
|
243 |
return s->str;
|
244 |
} |
245 |
} |
246 |
return NULL; |
247 |
} |
248 |
|
249 |
int usb_desc_string(USBDevice *dev, int index, uint8_t *dest, size_t len) |
250 |
{ |
251 |
uint8_t bLength, pos, i; |
252 |
const char *str; |
253 |
|
254 |
if (len < 4) { |
255 |
return -1; |
256 |
} |
257 |
|
258 |
if (index == 0) { |
259 |
/* language ids */
|
260 |
dest[0] = 4; |
261 |
dest[1] = USB_DT_STRING;
|
262 |
dest[2] = 0x09; |
263 |
dest[3] = 0x04; |
264 |
return 4; |
265 |
} |
266 |
|
267 |
str = usb_desc_get_string(dev, index); |
268 |
if (str == NULL) { |
269 |
str = dev->info->usb_desc->str[index]; |
270 |
if (str == NULL) { |
271 |
return 0; |
272 |
} |
273 |
} |
274 |
|
275 |
bLength = strlen(str) * 2 + 2; |
276 |
dest[0] = bLength;
|
277 |
dest[1] = USB_DT_STRING;
|
278 |
i = 0; pos = 2; |
279 |
while (pos+1 < bLength && pos+1 < len) { |
280 |
dest[pos++] = str[i++]; |
281 |
dest[pos++] = 0;
|
282 |
} |
283 |
return pos;
|
284 |
} |
285 |
|
286 |
int usb_desc_get_descriptor(USBDevice *dev, int value, uint8_t *dest, size_t len) |
287 |
{ |
288 |
const USBDesc *desc = dev->info->usb_desc;
|
289 |
const USBDescDevice *other_dev;
|
290 |
uint8_t buf[256];
|
291 |
uint8_t type = value >> 8;
|
292 |
uint8_t index = value & 0xff;
|
293 |
int ret = -1; |
294 |
|
295 |
if (dev->speed == USB_SPEED_HIGH) {
|
296 |
other_dev = dev->info->usb_desc->full; |
297 |
} else {
|
298 |
other_dev = dev->info->usb_desc->high; |
299 |
} |
300 |
|
301 |
switch(type) {
|
302 |
case USB_DT_DEVICE:
|
303 |
ret = usb_desc_device(&desc->id, dev->device, buf, sizeof(buf));
|
304 |
trace_usb_desc_device(dev->addr, len, ret); |
305 |
break;
|
306 |
case USB_DT_CONFIG:
|
307 |
if (index < dev->device->bNumConfigurations) {
|
308 |
ret = usb_desc_config(dev->device->confs + index, buf, sizeof(buf));
|
309 |
} |
310 |
trace_usb_desc_config(dev->addr, index, len, ret); |
311 |
break;
|
312 |
case USB_DT_STRING:
|
313 |
ret = usb_desc_string(dev, index, buf, sizeof(buf));
|
314 |
trace_usb_desc_string(dev->addr, index, len, ret); |
315 |
break;
|
316 |
|
317 |
case USB_DT_DEVICE_QUALIFIER:
|
318 |
if (other_dev != NULL) { |
319 |
ret = usb_desc_device_qualifier(other_dev, buf, sizeof(buf));
|
320 |
} |
321 |
trace_usb_desc_device_qualifier(dev->addr, len, ret); |
322 |
break;
|
323 |
case USB_DT_OTHER_SPEED_CONFIG:
|
324 |
if (other_dev != NULL && index < other_dev->bNumConfigurations) { |
325 |
ret = usb_desc_config(other_dev->confs + index, buf, sizeof(buf));
|
326 |
buf[0x01] = USB_DT_OTHER_SPEED_CONFIG;
|
327 |
} |
328 |
trace_usb_desc_other_speed_config(dev->addr, index, len, ret); |
329 |
break;
|
330 |
|
331 |
default:
|
332 |
fprintf(stderr, "%s: %d unknown type %d (len %zd)\n", __FUNCTION__,
|
333 |
dev->addr, type, len); |
334 |
break;
|
335 |
} |
336 |
|
337 |
if (ret > 0) { |
338 |
if (ret > len) {
|
339 |
ret = len; |
340 |
} |
341 |
memcpy(dest, buf, ret); |
342 |
} |
343 |
return ret;
|
344 |
} |
345 |
|
346 |
int usb_desc_handle_control(USBDevice *dev, int request, int value, |
347 |
int index, int length, uint8_t *data) |
348 |
{ |
349 |
const USBDesc *desc = dev->info->usb_desc;
|
350 |
int i, ret = -1; |
351 |
|
352 |
assert(desc != NULL);
|
353 |
switch(request) {
|
354 |
case DeviceOutRequest | USB_REQ_SET_ADDRESS:
|
355 |
dev->addr = value; |
356 |
trace_usb_set_addr(dev->addr); |
357 |
ret = 0;
|
358 |
break;
|
359 |
|
360 |
case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
|
361 |
ret = usb_desc_get_descriptor(dev, value, data, length); |
362 |
break;
|
363 |
|
364 |
case DeviceRequest | USB_REQ_GET_CONFIGURATION:
|
365 |
data[0] = dev->config->bConfigurationValue;
|
366 |
ret = 1;
|
367 |
break;
|
368 |
case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
|
369 |
for (i = 0; i < dev->device->bNumConfigurations; i++) { |
370 |
if (dev->device->confs[i].bConfigurationValue == value) {
|
371 |
dev->config = dev->device->confs + i; |
372 |
ret = 0;
|
373 |
} |
374 |
} |
375 |
trace_usb_set_config(dev->addr, value, ret); |
376 |
break;
|
377 |
|
378 |
case DeviceRequest | USB_REQ_GET_STATUS:
|
379 |
data[0] = 0; |
380 |
if (dev->config->bmAttributes & 0x40) { |
381 |
data[0] |= 1 << USB_DEVICE_SELF_POWERED; |
382 |
} |
383 |
if (dev->remote_wakeup) {
|
384 |
data[0] |= 1 << USB_DEVICE_REMOTE_WAKEUP; |
385 |
} |
386 |
data[1] = 0x00; |
387 |
ret = 2;
|
388 |
break;
|
389 |
case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
|
390 |
if (value == USB_DEVICE_REMOTE_WAKEUP) {
|
391 |
dev->remote_wakeup = 0;
|
392 |
ret = 0;
|
393 |
} |
394 |
trace_usb_clear_device_feature(dev->addr, value, ret); |
395 |
break;
|
396 |
case DeviceOutRequest | USB_REQ_SET_FEATURE:
|
397 |
if (value == USB_DEVICE_REMOTE_WAKEUP) {
|
398 |
dev->remote_wakeup = 1;
|
399 |
ret = 0;
|
400 |
} |
401 |
trace_usb_set_device_feature(dev->addr, value, ret); |
402 |
break;
|
403 |
} |
404 |
return ret;
|
405 |
} |