This repository has been archived by the owner on Apr 16, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 257
/
usb_standard.c
627 lines (527 loc) · 14.8 KB
/
usb_standard.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
// clang-format off
/** @defgroup usb_standard_file Generic USB Standard Request Interface
@ingroup USB
@brief <b>Generic USB Standard Request Interface</b>
@version 1.0.0
@author @htmlonly © @endhtmlonly 2010
Gareth McMullin <[email protected]>
@date 10 March 2013
LGPL License Terms @ref lgpl_license
*/
/*
* This file is part of the libopencm3 project.
*
* Copyright (C) 2010 Gareth McMullin <[email protected]>
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
/**@{*/
#include <string.h>
#include <libopencm3/usb/usbd.h>
#include "usb_private.h"
#include "util.h"
int usbd_register_set_config_callback(usbd_device *usbd_dev,
usbd_set_config_callback callback)
{
int i;
for (i = 0; i < MAX_USER_SET_CONFIG_CALLBACK; i++) {
if (usbd_dev->user_callback_set_config[i]) {
if (usbd_dev->user_callback_set_config[i] == callback) {
return 0;
}
continue;
}
usbd_dev->user_callback_set_config[i] = callback;
return 0;
}
return -1;
}
void usbd_register_set_altsetting_callback(usbd_device *usbd_dev,
usbd_set_altsetting_callback callback)
{
usbd_dev->user_callback_set_altsetting = callback;
}
static uint16_t build_config_descriptor(usbd_device *usbd_dev,
uint8_t index, uint8_t *buf, uint16_t len)
{
uint8_t *tmpbuf = buf;
const struct usb_config_descriptor *cfg = &usbd_dev->config[index];
uint16_t count, total = 0, totallen = 0;
uint16_t i, j, k;
memcpy(buf, cfg, count = MIN(len, cfg->bLength));
buf += count;
len -= count;
total += count;
totallen += cfg->bLength;
/* For each interface... */
for (i = 0; i < cfg->bNumInterfaces; i++) {
/* Interface Association Descriptor, if any */
if (cfg->interface[i].iface_assoc) {
const struct usb_iface_assoc_descriptor *assoc =
cfg->interface[i].iface_assoc;
memcpy(buf, assoc, count = MIN(len, assoc->bLength));
buf += count;
len -= count;
total += count;
totallen += assoc->bLength;
}
/* For each alternate setting... */
for (j = 0; j < cfg->interface[i].num_altsetting; j++) {
const struct usb_interface_descriptor *iface =
&cfg->interface[i].altsetting[j];
/* Copy interface descriptor. */
memcpy(buf, iface, count = MIN(len, iface->bLength));
buf += count;
len -= count;
total += count;
totallen += iface->bLength;
/* Copy extra bytes (function descriptors). */
if (iface->extra) {
memcpy(buf, iface->extra,
count = MIN(len, iface->extralen));
buf += count;
len -= count;
total += count;
totallen += iface->extralen;
}
/* For each endpoint... */
for (k = 0; k < iface->bNumEndpoints; k++) {
const struct usb_endpoint_descriptor *ep =
&iface->endpoint[k];
memcpy(buf, ep, count = MIN(len, ep->bLength));
buf += count;
len -= count;
total += count;
totallen += ep->bLength;
/* Copy extra bytes (class specific). */
if (ep->extra) {
memcpy(buf, ep->extra,
count = MIN(len, ep->extralen));
buf += count;
len -= count;
total += count;
totallen += ep->extralen;
}
}
}
}
/* Fill in wTotalLength.
* Note that tmpbuf is sometimes not halfword-aligned */
memcpy((tmpbuf + 2), &totallen, sizeof(uint16_t));
return total;
}
static int usb_descriptor_type(uint16_t wValue)
{
return wValue >> 8;
}
static int usb_descriptor_index(uint16_t wValue)
{
return wValue & 0xFF;
}
static enum usbd_request_return_codes
usb_standard_get_descriptor(usbd_device *usbd_dev,
struct usb_setup_data *req,
uint8_t **buf, uint16_t *len)
{
wait_random();
int i, array_idx, descr_idx;
struct usb_string_descriptor *sd;
descr_idx = usb_descriptor_index(req->wValue);
switch (usb_descriptor_type(req->wValue)) {
case USB_DT_DEVICE:
*buf = (uint8_t *) usbd_dev->desc;
*len = MIN_8bits(*len, usbd_dev->desc->bLength);
return USBD_REQ_HANDLED;
case USB_DT_CONFIGURATION:
*buf = usbd_dev->ctrl_buf;
*len = build_config_descriptor(usbd_dev, descr_idx, *buf, *len);
return USBD_REQ_HANDLED;
case USB_DT_STRING:
sd = (struct usb_string_descriptor *)usbd_dev->ctrl_buf;
if (descr_idx == 0) {
/* Send sane Language ID descriptor... */
sd->wData[0] = USB_LANGID_ENGLISH_US;
sd->bLength = sizeof(sd->bLength) +
sizeof(sd->bDescriptorType) +
sizeof(sd->wData[0]);
*len = MIN_8bits(*len, sd->bLength);
} else {
array_idx = descr_idx - 1;
if (!usbd_dev->strings) {
/* Device doesn't support strings. */
return USBD_REQ_NOTSUPP;
}
/* Check that string index is in range. */
if (array_idx >= usbd_dev->num_strings) {
return USBD_REQ_NOTSUPP;
}
/* Strings with Language ID differnet from
* USB_LANGID_ENGLISH_US are not supported */
if (req->wIndex != USB_LANGID_ENGLISH_US) {
return USBD_REQ_NOTSUPP;
}
/* This string is returned as UTF16, hence the
* multiplication
*/
sd->bLength = strlen(usbd_dev->strings[array_idx]) * 2 +
sizeof(sd->bLength) +
sizeof(sd->bDescriptorType);
*len = MIN_8bits(*len, sd->bLength);
for (i = 0; i < (*len / 2) - 1; i++) {
sd->wData[i] =
usbd_dev->strings[array_idx][i];
}
}
sd->bDescriptorType = USB_DT_STRING;
*buf = (uint8_t *)sd;
return USBD_REQ_HANDLED;
}
return USBD_REQ_NOTSUPP;
}
static enum usbd_request_return_codes
usb_standard_set_address(usbd_device *usbd_dev,
struct usb_setup_data *req, uint8_t **buf,
uint16_t *len)
{
(void)req;
(void)buf;
(void)len;
/* The actual address is only latched at the STATUS IN stage. */
if ((req->bmRequestType != 0) || (req->wValue >= 128)) {
return USBD_REQ_NOTSUPP;
}
usbd_dev->current_address = req->wValue;
/*
* Special workaround for STM32F10[57] that require the address
* to be set here. This is undocumented!
*/
if (usbd_dev->driver->set_address_before_status) {
usbd_dev->driver->set_address(usbd_dev, req->wValue);
}
return USBD_REQ_HANDLED;
}
static enum usbd_request_return_codes
usb_standard_set_configuration(usbd_device *usbd_dev,
struct usb_setup_data *req,
uint8_t **buf, uint16_t *len)
{
unsigned i;
int found_index = -1;
const struct usb_config_descriptor *cfg;
(void)req;
(void)buf;
(void)len;
if (req->wValue > 0) {
for (i = 0; i < usbd_dev->desc->bNumConfigurations; i++) {
if (req->wValue
== usbd_dev->config[i].bConfigurationValue) {
found_index = i;
break;
}
}
if (found_index < 0) {
return USBD_REQ_NOTSUPP;
}
}
usbd_dev->current_config = found_index + 1;
if (usbd_dev->current_config > 0) {
cfg = &usbd_dev->config[usbd_dev->current_config - 1];
/* reset all alternate settings configuration */
for (i = 0; i < cfg->bNumInterfaces; i++) {
if (cfg->interface[i].cur_altsetting) {
*cfg->interface[i].cur_altsetting = 0;
}
}
}
/* Reset all endpoints. */
usbd_dev->driver->ep_reset(usbd_dev);
if (usbd_dev->user_callback_set_config[0]) {
/*
* Flush control callbacks. These will be reregistered
* by the user handler.
*/
for (i = 0; i < MAX_USER_CONTROL_CALLBACK; i++) {
usbd_dev->user_control_callback[i].cb = NULL;
}
for (i = 0; i < MAX_USER_SET_CONFIG_CALLBACK; i++) {
if (usbd_dev->user_callback_set_config[i]) {
usbd_dev->user_callback_set_config[i](usbd_dev,
req->wValue);
}
}
}
return USBD_REQ_HANDLED;
}
static enum usbd_request_return_codes
usb_standard_get_configuration(usbd_device *usbd_dev,
struct usb_setup_data *req,
uint8_t **buf, uint16_t *len)
{
(void)req;
if (*len > 1) {
*len = 1;
}
if (usbd_dev->current_config > 0) {
const struct usb_config_descriptor *cfg =
&usbd_dev->config[usbd_dev->current_config - 1];
(*buf)[0] = cfg->bConfigurationValue;
} else {
(*buf)[0] = 0;
}
return USBD_REQ_HANDLED;
}
static enum usbd_request_return_codes
usb_standard_set_interface(usbd_device *usbd_dev,
struct usb_setup_data *req,
uint8_t **buf, uint16_t *len)
{
const struct usb_config_descriptor *cfx =
&usbd_dev->config[usbd_dev->current_config - 1];
const struct usb_interface *iface;
(void)buf;
if (req->wIndex >= cfx->bNumInterfaces) {
return USBD_REQ_NOTSUPP;
}
iface = &cfx->interface[req->wIndex];
if (req->wValue >= iface->num_altsetting) {
return USBD_REQ_NOTSUPP;
}
if (iface->cur_altsetting) {
*iface->cur_altsetting = req->wValue;
} else if (req->wValue > 0) {
return USBD_REQ_NOTSUPP;
}
if (usbd_dev->user_callback_set_altsetting) {
usbd_dev->user_callback_set_altsetting(usbd_dev,
req->wIndex,
req->wValue);
}
*len = 0;
return USBD_REQ_HANDLED;
}
static enum usbd_request_return_codes
usb_standard_get_interface(usbd_device *usbd_dev,
struct usb_setup_data *req,
uint8_t **buf, uint16_t *len)
{
uint8_t *cur_altsetting;
const struct usb_config_descriptor *cfx =
&usbd_dev->config[usbd_dev->current_config - 1];
if (req->wIndex >= cfx->bNumInterfaces) {
return USBD_REQ_NOTSUPP;
}
*len = 1;
cur_altsetting = cfx->interface[req->wIndex].cur_altsetting;
(*buf)[0] = (cur_altsetting) ? *cur_altsetting : 0;
return USBD_REQ_HANDLED;
}
static enum usbd_request_return_codes
usb_standard_device_get_status(usbd_device *usbd_dev,
struct usb_setup_data *req,
uint8_t **buf, uint16_t *len)
{
(void)usbd_dev;
(void)req;
/* bit 0: self powered */
/* bit 1: remote wakeup */
if (*len > 2) {
*len = 2;
}
(*buf)[0] = 0;
(*buf)[1] = 0;
return USBD_REQ_HANDLED;
}
static enum usbd_request_return_codes
usb_standard_interface_get_status(usbd_device *usbd_dev,
struct usb_setup_data *req,
uint8_t **buf, uint16_t *len)
{
(void)usbd_dev;
(void)req;
/* not defined */
if (*len > 2) {
*len = 2;
}
(*buf)[0] = 0;
(*buf)[1] = 0;
return USBD_REQ_HANDLED;
}
static enum usbd_request_return_codes
usb_standard_endpoint_get_status(usbd_device *usbd_dev,
struct usb_setup_data *req,
uint8_t **buf, uint16_t *len)
{
(void)req;
if (*len > 2) {
*len = 2;
}
(*buf)[0] = usbd_ep_stall_get(usbd_dev, req->wIndex) ? 1 : 0;
(*buf)[1] = 0;
return USBD_REQ_HANDLED;
}
static enum usbd_request_return_codes
usb_standard_endpoint_stall(usbd_device *usbd_dev,
struct usb_setup_data *req,
uint8_t **buf, uint16_t *len)
{
(void)buf;
(void)len;
usbd_ep_stall_set(usbd_dev, req->wIndex, 1);
return USBD_REQ_HANDLED;
}
static enum usbd_request_return_codes
usb_standard_endpoint_unstall(usbd_device *usbd_dev,
struct usb_setup_data *req,
uint8_t **buf, uint16_t *len)
{
(void)buf;
(void)len;
usbd_ep_stall_set(usbd_dev, req->wIndex, 0);
return USBD_REQ_HANDLED;
}
/* Do not appear to belong to the API, so are omitted from docs */
/**@}*/
enum usbd_request_return_codes
_usbd_standard_request_device(usbd_device *usbd_dev,
struct usb_setup_data *req, uint8_t **buf,
uint16_t *len)
{
enum usbd_request_return_codes (*command)(usbd_device *usbd_dev,
struct usb_setup_data *req,
uint8_t **buf, uint16_t *len) = NULL;
switch (req->bRequest) {
case USB_REQ_CLEAR_FEATURE:
case USB_REQ_SET_FEATURE:
if (req->wValue == USB_FEAT_DEVICE_REMOTE_WAKEUP) {
/* Device wakeup code goes here. */
}
if (req->wValue == USB_FEAT_TEST_MODE) {
/* Test mode code goes here. */
}
break;
case USB_REQ_SET_ADDRESS:
/*
* SET ADDRESS is an exception.
* It is only processed at STATUS stage.
*/
command = usb_standard_set_address;
break;
case USB_REQ_SET_CONFIGURATION:
command = usb_standard_set_configuration;
break;
case USB_REQ_GET_CONFIGURATION:
command = usb_standard_get_configuration;
break;
case USB_REQ_GET_DESCRIPTOR:
command = usb_standard_get_descriptor;
break;
case USB_REQ_GET_STATUS:
/*
* GET_STATUS always responds with zero reply.
* The application may override this behaviour.
*/
command = usb_standard_device_get_status;
break;
case USB_REQ_SET_DESCRIPTOR:
/* SET_DESCRIPTOR is optional and not implemented. */
break;
}
if (!command) {
return USBD_REQ_NOTSUPP;
}
return command(usbd_dev, req, buf, len);
}
enum usbd_request_return_codes
_usbd_standard_request_interface(usbd_device *usbd_dev,
struct usb_setup_data *req, uint8_t **buf,
uint16_t *len)
{
enum usbd_request_return_codes (*command)(usbd_device *usbd_dev,
struct usb_setup_data *req,
uint8_t **buf, uint16_t *len) = NULL;
switch (req->bRequest) {
case USB_REQ_CLEAR_FEATURE:
case USB_REQ_SET_FEATURE:
/* not defined */
break;
case USB_REQ_GET_INTERFACE:
command = usb_standard_get_interface;
break;
case USB_REQ_SET_INTERFACE:
command = usb_standard_set_interface;
break;
case USB_REQ_GET_STATUS:
command = usb_standard_interface_get_status;
break;
}
if (!command) {
return USBD_REQ_NOTSUPP;
}
return command(usbd_dev, req, buf, len);
}
enum usbd_request_return_codes
_usbd_standard_request_endpoint(usbd_device *usbd_dev,
struct usb_setup_data *req, uint8_t **buf,
uint16_t *len)
{
enum usbd_request_return_codes (*command) (usbd_device *usbd_dev,
struct usb_setup_data *req,
uint8_t **buf, uint16_t *len) = NULL;
switch (req->bRequest) {
case USB_REQ_CLEAR_FEATURE:
if (req->wValue == USB_FEAT_ENDPOINT_HALT) {
command = usb_standard_endpoint_unstall;
}
break;
case USB_REQ_SET_FEATURE:
if (req->wValue == USB_FEAT_ENDPOINT_HALT) {
command = usb_standard_endpoint_stall;
}
break;
case USB_REQ_GET_STATUS:
command = usb_standard_endpoint_get_status;
break;
case USB_REQ_SET_SYNCH_FRAME:
/* FIXME: SYNCH_FRAME is not implemented. */
/*
* SYNCH_FRAME is used for synchronization of isochronous
* endpoints which are not yet implemented.
*/
break;
}
if (!command) {
return USBD_REQ_NOTSUPP;
}
return command(usbd_dev, req, buf, len);
}
enum usbd_request_return_codes
_usbd_standard_request(usbd_device *usbd_dev, struct usb_setup_data *req,
uint8_t **buf, uint16_t *len)
{
/* FIXME: Have class/vendor requests as well. */
if ((req->bmRequestType & USB_REQ_TYPE_TYPE) != USB_REQ_TYPE_STANDARD) {
return USBD_REQ_NOTSUPP;
}
switch (req->bmRequestType & USB_REQ_TYPE_RECIPIENT) {
case USB_REQ_TYPE_DEVICE:
return _usbd_standard_request_device(usbd_dev, req, buf, len);
case USB_REQ_TYPE_INTERFACE:
return _usbd_standard_request_interface(usbd_dev, req,
buf, len);
case USB_REQ_TYPE_ENDPOINT:
return _usbd_standard_request_endpoint(usbd_dev, req, buf, len);
default:
return USBD_REQ_NOTSUPP;
}
}