Statistics
| Branch: | Revision:

root / hw / usb-desc.c @ 83a53bbc

History | View | Annotate | Download (14.9 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
    /* handle grouped interfaces if any*/
95
    for (i = 0; i < conf->nif_groups; i++) {
96
        rc = usb_desc_iface_group(&(conf->if_groups[i]),
97
                                  dest + wTotalLength,
98
                                  len - wTotalLength);
99
        if (rc < 0) {
100
            return rc;
101
        }
102
        wTotalLength += rc;
103
    }
104

    
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);
108
        if (rc < 0) {
109
            return rc;
110
        }
111
        wTotalLength += rc;
112
    }
113

    
114
    dest[0x02] = usb_lo(wTotalLength);
115
    dest[0x03] = usb_hi(wTotalLength);
116
    return wTotalLength;
117
}
118

    
119
int usb_desc_iface_group(const USBDescIfaceAssoc *iad, uint8_t *dest,
120
                         size_t len)
121
{
122
    int pos = 0;
123
    int i = 0;
124

    
125
    /* handle interface association descriptor */
126
    uint8_t bLength = 0x08;
127

    
128
    if (len < bLength) {
129
        return -1;
130
    }
131

    
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;
140
    pos += bLength;
141

    
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);
145
        if (rc < 0) {
146
            return rc;
147
        }
148
        pos += rc;
149
    }
150

    
151
    return pos;
152
}
153

    
154
int usb_desc_iface(const USBDescIface *iface, uint8_t *dest, size_t len)
155
{
156
    uint8_t bLength = 0x09;
157
    int i, rc, pos = 0;
158

    
159
    if (len < bLength) {
160
        return -1;
161
    }
162

    
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;
172
    pos += bLength;
173

    
174
    for (i = 0; i < iface->ndesc; i++) {
175
        rc = usb_desc_other(iface->descs + i, dest + pos, len - pos);
176
        if (rc < 0) {
177
            return rc;
178
        }
179
        pos += rc;
180
    }
181

    
182
    for (i = 0; i < iface->bNumEndpoints; i++) {
183
        rc = usb_desc_endpoint(iface->eps + i, dest + pos, len - pos);
184
        if (rc < 0) {
185
            return rc;
186
        }
187
        pos += rc;
188
    }
189

    
190
    return pos;
191
}
192

    
193
int usb_desc_endpoint(const USBDescEndpoint *ep, uint8_t *dest, size_t len)
194
{
195
    uint8_t bLength = ep->is_audio ? 0x09 : 0x07;
196
    uint8_t extralen = ep->extra ? ep->extra[0] : 0;
197

    
198
    if (len < bLength + extralen) {
199
        return -1;
200
    }
201

    
202
    dest[0x00] = bLength;
203
    dest[0x01] = USB_DT_ENDPOINT;
204
    dest[0x02] = ep->bEndpointAddress;
205
    dest[0x03] = ep->bmAttributes;
206
    dest[0x04] = usb_lo(ep->wMaxPacketSize);
207
    dest[0x05] = usb_hi(ep->wMaxPacketSize);
208
    dest[0x06] = ep->bInterval;
209
    if (ep->is_audio) {
210
        dest[0x07] = ep->bRefresh;
211
        dest[0x08] = ep->bSynchAddress;
212
    }
213
    if (ep->extra) {
214
        memcpy(dest + bLength, ep->extra, extralen);
215
    }
216

    
217
    return bLength + extralen;
218
}
219

    
220
int usb_desc_other(const USBDescOther *desc, uint8_t *dest, size_t len)
221
{
222
    int bLength = desc->length ? desc->length : desc->data[0];
223

    
224
    if (len < bLength) {
225
        return -1;
226
    }
227

    
228
    memcpy(dest, desc->data, bLength);
229
    return bLength;
230
}
231

    
232
/* ------------------------------------------------------------------ */
233

    
234
static void usb_desc_ep_init(USBDevice *dev)
235
{
236
    const USBDescIface *iface;
237
    int i, e, pid, ep;
238

    
239
    usb_ep_init(dev);
240
    for (i = 0; i < dev->ninterfaces; i++) {
241
        iface = dev->ifaces[i];
242
        if (iface == NULL) {
243
            continue;
244
        }
245
        for (e = 0; e < iface->bNumEndpoints; e++) {
246
            pid = (iface->eps[e].bEndpointAddress & USB_DIR_IN) ?
247
                USB_TOKEN_IN : USB_TOKEN_OUT;
248
            ep = iface->eps[e].bEndpointAddress & 0x0f;
249
            usb_ep_set_type(dev, pid, ep, iface->eps[e].bmAttributes & 0x03);
250
            usb_ep_set_ifnum(dev, pid, ep, iface->bInterfaceNumber);
251
        }
252
    }
253
}
254

    
255
static const USBDescIface *usb_desc_find_interface(USBDevice *dev,
256
                                                   int nif, int alt)
257
{
258
    const USBDescIface *iface;
259
    int g, i;
260

    
261
    if (!dev->config) {
262
        return NULL;
263
    }
264
    for (g = 0; g < dev->config->nif_groups; g++) {
265
        for (i = 0; i < dev->config->if_groups[g].nif; i++) {
266
            iface = &dev->config->if_groups[g].ifs[i];
267
            if (iface->bInterfaceNumber == nif &&
268
                iface->bAlternateSetting == alt) {
269
                return iface;
270
            }
271
        }
272
    }
273
    for (i = 0; i < dev->config->nif; i++) {
274
        iface = &dev->config->ifs[i];
275
        if (iface->bInterfaceNumber == nif &&
276
            iface->bAlternateSetting == alt) {
277
            return iface;
278
        }
279
    }
280
    return NULL;
281
}
282

    
283
static int usb_desc_set_interface(USBDevice *dev, int index, int value)
284
{
285
    const USBDescIface *iface;
286
    int old;
287

    
288
    iface = usb_desc_find_interface(dev, index, value);
289
    if (iface == NULL) {
290
        return -1;
291
    }
292

    
293
    old = dev->altsetting[index];
294
    dev->altsetting[index] = value;
295
    dev->ifaces[index] = iface;
296
    usb_desc_ep_init(dev);
297

    
298
    if (dev->info->set_interface && old != value) {
299
        dev->info->set_interface(dev, index, old, value);
300
    }
301
    return 0;
302
}
303

    
304
static int usb_desc_set_config(USBDevice *dev, int value)
305
{
306
    int i;
307

    
308
    if (value == 0) {
309
        dev->configuration = 0;
310
        dev->ninterfaces   = 0;
311
        dev->config = NULL;
312
    } else {
313
        for (i = 0; i < dev->device->bNumConfigurations; i++) {
314
            if (dev->device->confs[i].bConfigurationValue == value) {
315
                dev->configuration = value;
316
                dev->ninterfaces   = dev->device->confs[i].bNumInterfaces;
317
                dev->config = dev->device->confs + i;
318
                assert(dev->ninterfaces <= USB_MAX_INTERFACES);
319
            }
320
        }
321
        if (i < dev->device->bNumConfigurations) {
322
            return -1;
323
        }
324
    }
325

    
326
    for (i = 0; i < dev->ninterfaces; i++) {
327
        usb_desc_set_interface(dev, i, 0);
328
    }
329
    for (; i < USB_MAX_INTERFACES; i++) {
330
        dev->altsetting[i] = 0;
331
        dev->ifaces[i] = NULL;
332
    }
333

    
334
    return 0;
335
}
336

    
337
static void usb_desc_setdefaults(USBDevice *dev)
338
{
339
    const USBDesc *desc = dev->info->usb_desc;
340

    
341
    assert(desc != NULL);
342
    switch (dev->speed) {
343
    case USB_SPEED_LOW:
344
    case USB_SPEED_FULL:
345
        dev->device = desc->full;
346
        break;
347
    case USB_SPEED_HIGH:
348
        dev->device = desc->high;
349
        break;
350
    }
351
    usb_desc_set_config(dev, 0);
352
}
353

    
354
void usb_desc_init(USBDevice *dev)
355
{
356
    const USBDesc *desc = dev->info->usb_desc;
357

    
358
    assert(desc != NULL);
359
    dev->speed = USB_SPEED_FULL;
360
    dev->speedmask = 0;
361
    if (desc->full) {
362
        dev->speedmask |= USB_SPEED_MASK_FULL;
363
    }
364
    if (desc->high) {
365
        dev->speedmask |= USB_SPEED_MASK_HIGH;
366
    }
367
    usb_desc_setdefaults(dev);
368
}
369

    
370
void usb_desc_attach(USBDevice *dev)
371
{
372
    const USBDesc *desc = dev->info->usb_desc;
373

    
374
    assert(desc != NULL);
375
    if (desc->high && (dev->port->speedmask & USB_SPEED_MASK_HIGH)) {
376
        dev->speed = USB_SPEED_HIGH;
377
    } else if (desc->full && (dev->port->speedmask & USB_SPEED_MASK_FULL)) {
378
        dev->speed = USB_SPEED_FULL;
379
    } else {
380
        fprintf(stderr, "usb: port/device speed mismatch for \"%s\"\n",
381
                dev->info->product_desc);
382
        return;
383
    }
384
    usb_desc_setdefaults(dev);
385
}
386

    
387
void usb_desc_set_string(USBDevice *dev, uint8_t index, const char *str)
388
{
389
    USBDescString *s;
390

    
391
    QLIST_FOREACH(s, &dev->strings, next) {
392
        if (s->index == index) {
393
            break;
394
        }
395
    }
396
    if (s == NULL) {
397
        s = g_malloc0(sizeof(*s));
398
        s->index = index;
399
        QLIST_INSERT_HEAD(&dev->strings, s, next);
400
    }
401
    g_free(s->str);
402
    s->str = g_strdup(str);
403
}
404

    
405
const char *usb_desc_get_string(USBDevice *dev, uint8_t index)
406
{
407
    USBDescString *s;
408

    
409
    QLIST_FOREACH(s, &dev->strings, next) {
410
        if (s->index == index) {
411
            return s->str;
412
        }
413
    }
414
    return NULL;
415
}
416

    
417
int usb_desc_string(USBDevice *dev, int index, uint8_t *dest, size_t len)
418
{
419
    uint8_t bLength, pos, i;
420
    const char *str;
421

    
422
    if (len < 4) {
423
        return -1;
424
    }
425

    
426
    if (index == 0) {
427
        /* language ids */
428
        dest[0] = 4;
429
        dest[1] = USB_DT_STRING;
430
        dest[2] = 0x09;
431
        dest[3] = 0x04;
432
        return 4;
433
    }
434

    
435
    str = usb_desc_get_string(dev, index);
436
    if (str == NULL) {
437
        str = dev->info->usb_desc->str[index];
438
        if (str == NULL) {
439
            return 0;
440
        }
441
    }
442

    
443
    bLength = strlen(str) * 2 + 2;
444
    dest[0] = bLength;
445
    dest[1] = USB_DT_STRING;
446
    i = 0; pos = 2;
447
    while (pos+1 < bLength && pos+1 < len) {
448
        dest[pos++] = str[i++];
449
        dest[pos++] = 0;
450
    }
451
    return pos;
452
}
453

    
454
int usb_desc_get_descriptor(USBDevice *dev, int value, uint8_t *dest, size_t len)
455
{
456
    const USBDesc *desc = dev->info->usb_desc;
457
    const USBDescDevice *other_dev;
458
    uint8_t buf[256];
459
    uint8_t type = value >> 8;
460
    uint8_t index = value & 0xff;
461
    int ret = -1;
462

    
463
    if (dev->speed == USB_SPEED_HIGH) {
464
        other_dev = dev->info->usb_desc->full;
465
    } else {
466
        other_dev = dev->info->usb_desc->high;
467
    }
468

    
469
    switch(type) {
470
    case USB_DT_DEVICE:
471
        ret = usb_desc_device(&desc->id, dev->device, buf, sizeof(buf));
472
        trace_usb_desc_device(dev->addr, len, ret);
473
        break;
474
    case USB_DT_CONFIG:
475
        if (index < dev->device->bNumConfigurations) {
476
            ret = usb_desc_config(dev->device->confs + index, buf, sizeof(buf));
477
        }
478
        trace_usb_desc_config(dev->addr, index, len, ret);
479
        break;
480
    case USB_DT_STRING:
481
        ret = usb_desc_string(dev, index, buf, sizeof(buf));
482
        trace_usb_desc_string(dev->addr, index, len, ret);
483
        break;
484

    
485
    case USB_DT_DEVICE_QUALIFIER:
486
        if (other_dev != NULL) {
487
            ret = usb_desc_device_qualifier(other_dev, buf, sizeof(buf));
488
        }
489
        trace_usb_desc_device_qualifier(dev->addr, len, ret);
490
        break;
491
    case USB_DT_OTHER_SPEED_CONFIG:
492
        if (other_dev != NULL && index < other_dev->bNumConfigurations) {
493
            ret = usb_desc_config(other_dev->confs + index, buf, sizeof(buf));
494
            buf[0x01] = USB_DT_OTHER_SPEED_CONFIG;
495
        }
496
        trace_usb_desc_other_speed_config(dev->addr, index, len, ret);
497
        break;
498

    
499
    case USB_DT_DEBUG:
500
        /* ignore silently */
501
        break;
502

    
503
    default:
504
        fprintf(stderr, "%s: %d unknown type %d (len %zd)\n", __FUNCTION__,
505
                dev->addr, type, len);
506
        break;
507
    }
508

    
509
    if (ret > 0) {
510
        if (ret > len) {
511
            ret = len;
512
        }
513
        memcpy(dest, buf, ret);
514
    }
515
    return ret;
516
}
517

    
518
int usb_desc_handle_control(USBDevice *dev, USBPacket *p,
519
        int request, int value, int index, int length, uint8_t *data)
520
{
521
    const USBDesc *desc = dev->info->usb_desc;
522
    int ret = -1;
523

    
524
    assert(desc != NULL);
525
    switch(request) {
526
    case DeviceOutRequest | USB_REQ_SET_ADDRESS:
527
        dev->addr = value;
528
        trace_usb_set_addr(dev->addr);
529
        ret = 0;
530
        break;
531

    
532
    case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
533
        ret = usb_desc_get_descriptor(dev, value, data, length);
534
        break;
535

    
536
    case DeviceRequest | USB_REQ_GET_CONFIGURATION:
537
        data[0] = dev->config->bConfigurationValue;
538
        ret = 1;
539
        break;
540
    case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
541
        ret = usb_desc_set_config(dev, value);
542
        trace_usb_set_config(dev->addr, value, ret);
543
        break;
544

    
545
    case DeviceRequest | USB_REQ_GET_STATUS:
546
        data[0] = 0;
547
        if (dev->config->bmAttributes & 0x40) {
548
            data[0] |= 1 << USB_DEVICE_SELF_POWERED;
549
        }
550
        if (dev->remote_wakeup) {
551
            data[0] |= 1 << USB_DEVICE_REMOTE_WAKEUP;
552
        }
553
        data[1] = 0x00;
554
        ret = 2;
555
        break;
556
    case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
557
        if (value == USB_DEVICE_REMOTE_WAKEUP) {
558
            dev->remote_wakeup = 0;
559
            ret = 0;
560
        }
561
        trace_usb_clear_device_feature(dev->addr, value, ret);
562
        break;
563
    case DeviceOutRequest | USB_REQ_SET_FEATURE:
564
        if (value == USB_DEVICE_REMOTE_WAKEUP) {
565
            dev->remote_wakeup = 1;
566
            ret = 0;
567
        }
568
        trace_usb_set_device_feature(dev->addr, value, ret);
569
        break;
570

    
571
    case InterfaceRequest | USB_REQ_GET_INTERFACE:
572
        if (index < 0 || index >= dev->ninterfaces) {
573
            break;
574
        }
575
        data[0] = dev->altsetting[index];
576
        ret = 1;
577
        break;
578
    case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
579
        ret = usb_desc_set_interface(dev, index, value);
580
        trace_usb_set_interface(dev->addr, index, value, ret);
581
        break;
582

    
583
    }
584
    return ret;
585
}