-
Notifications
You must be signed in to change notification settings - Fork 0
/
usb_control.c
537 lines (452 loc) · 15.1 KB
/
usb_control.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
/** @file usb_control.c
*
*/
#include "autoconf.h"
#include "api/usb.h"
#include "api/usb_control.h"
#include "libc/stdio.h"
#include "libc/nostd.h"
#include "libc/string.h"
#include "libc/syscall.h"
#include "libc/sanhandlers.h"
#define USB_CTRL_DEBUG 0
#if USB_CTRL_DEBUG
#define log_printf(...) aprintf(__VA_ARGS__)
#else
#define log_printf(...) {};
#endif
/*
* This flag inform the stack of the current USB Control state.
* At the end of the enumeration, when the first upper stack command
* execution (first DFU command, first SCSI command, and so on), this
* flag must be set to true to inform the control stack that the
* enumeration stack is complete.
*
* Why doing this:
* 1) the enumeration state sequence is variable depending on the host
* OS stack and its end can't be detected at this layer of the USB
* stack
* 2) USB reset reception in the enumeration phase and the nominal phase
* has different consequences. In enumeration phase, the reset request
* is usually a 'standard' behavior of some host stacks to ensure
* synchronization of host and device stacks. On nominal phase, USB
* reset is the consequence of an error and has to be handled on the
* overall device software, which is, on devices such as Wookey, clearly
* more complex than a basic stack reset and enumeration.
*
* How handling this ?
* Whatever the stack being executed above the USB driver low level control
* stack, this stack should inform the driver that it has received it first
* command (DFU, SCSI, HID...). Receiving this command means that the
* enumeration is fully complete.
*
* TODO: By now, the low level control library and the driver are hosted
* in the same dir. The goal is to implement a real 'libcontrol' and
* 'libbulk' libraries, independent of the USB device driver.
*/
static bool usb_init_phase_done = false;
bool usb_ctrl_is_initphase_done(void)
{
return usb_init_phase_done;
}
void usb_ctrl_set_initphase_done(void)
{
usb_init_phase_done = true;
}
//#include "usb_device.h"
usb_ctrl_device_descriptor_t usb_ctrl_device_desc;
usb_ctrl_full_configuration_descriptor_t usb_ctrl_conf_desc;
static void usb_ctrl_init_structures(void)
{
memset((void*)&usb_ctrl_device_desc, 0, sizeof(usb_ctrl_device_descriptor_t));
memset((void*)&usb_ctrl_conf_desc, 0, sizeof(usb_ctrl_full_configuration_descriptor_t));
}
/**
* \brief Send device descriptor.
*/
static void usb_ctrl_device_desc_rqst_handler(uint16_t wLength){
log_printf("wLength:%d - sizeof(usb_ctrl_device_desc):%d\n", wLength, sizeof(usb_ctrl_device_desc));
if ( wLength == 0 ){
usb_driver_setup_send_status(0);
usb_driver_setup_read_status();
goto end;
}
if ( wLength > sizeof(usb_ctrl_device_desc)){
usb_driver_setup_send((uint8_t *)&usb_ctrl_device_desc, sizeof(usb_ctrl_device_desc), EP0);
} else {
usb_driver_setup_send((uint8_t *)&usb_ctrl_device_desc, wLength, EP0);
}
usb_driver_setup_read_status();
end:
return;
}
/**
* \brief Send configuration descriptor.
*
* @param wLength Descriptor length.
*/
static void usb_ctrl_configuration_desc_rqst_handler(uint16_t wLength){
log_printf("wLength:%d - sizeof(usb_ctrl_device_desc):%d\n", wLength, usb_ctrl_conf_desc.config_desc.wTotalLength);
if ( wLength == 0 ) {
usb_driver_setup_send_status(0);
usb_driver_setup_read_status();
goto end;
}
if ( wLength > usb_ctrl_conf_desc.config_desc.wTotalLength ) {
usb_driver_setup_send((uint8_t *)&usb_ctrl_conf_desc,
usb_ctrl_conf_desc.config_desc.wTotalLength , EP0);
} else {
usb_driver_setup_send((uint8_t *)&usb_ctrl_conf_desc, wLength, EP0);
}
usb_driver_setup_read_status();
end:
return;
}
/**
* \brief Default Class request handler.
*
* Class request handling (Run-Time Mode).
*
* @param packet Setup packet
*/
static void usb_ctrl_default_class_rqst_handler(struct usb_setup_packet *packet __attribute__((unused))){
log_printf("No class rqst handler is defined for class \n", packet->bRequest);
}
/**
* \brief Default Vendor request handler.
*
* Not implemented.
*/
static void usb_ctrl_default_vendor_rqst_handler( __attribute__((unused)) struct usb_setup_packet *packet) {
aprintf("[Vendor rqst: Not implemented\n");
}
static void usb_ctrl_default_set_interface_rqst_handler(int iface){
aprintf("Set interface (%x - not implemented yet)\n", iface);
/* Program EP 0*/
/* Unmask interrupt in OTG_FS_DAINTMSK reg */
/* Set up Data FIFO RAM (?) */
/* Send status IN packet */
if (iface == 1) {
usb_driver_setup_send_status(0);
} else {
/* FIXME We should handle multiple interface */
log_printf("%d 1\n", iface);
usb_driver_stall_in(EP0);
}
}
static void usb_ctrl_default_set_configuration_rqst_handler(int conf){
if (conf == 1) {
usb_driver_setup_send_status(0);
} else {
/* FIXME We should handle multiple configuration */
log_printf("SET_CONFIGURATION %d 1\n", conf);
usb_driver_stall_in(EP0);
}
}
/**
* \brief Send functional descriptor.
*/
static void usb_ctrl_default_functional_desc_rqst_handler(uint16_t wLength){
aprintf("usb_ctrl_cbs.functional_rqst_handler is not defined, wlength:%d\n", wLength);
}
static void usb_ctrl_default_reset_handler(void) {
return;
}
/**
* \brief usb_ctrl_callbacks defined at usb class level and called from usb_ctrl
*/
usb_ctrl_callbacks_t usb_ctrl_callbacks = {
.class_rqst_handler = usb_ctrl_default_class_rqst_handler,
.vendor_rqst_handler = usb_ctrl_default_vendor_rqst_handler,
.set_configuration_rqst_handler = usb_ctrl_default_set_configuration_rqst_handler,
.set_interface_rqst_handler = usb_ctrl_default_set_interface_rqst_handler,
.functional_rqst_handler = usb_ctrl_default_functional_desc_rqst_handler,
.reset_handler = usb_ctrl_default_reset_handler,
};
/* Register our callbacks */
ADD_GLOB_HANDLER(usb_ctrl_default_class_rqst_handler)
ADD_GLOB_HANDLER(usb_ctrl_default_vendor_rqst_handler)
ADD_GLOB_HANDLER(usb_ctrl_default_set_configuration_rqst_handler)
ADD_GLOB_HANDLER(usb_ctrl_default_set_interface_rqst_handler)
ADD_GLOB_HANDLER(usb_ctrl_default_functional_desc_rqst_handler)
ADD_GLOB_HANDLER(usb_ctrl_default_reset_handler)
void usb_ctrl_handle_reset(void)
{
/* calling default (or configured) reset handler. This
* handler is called only for reset requests sent after
* the enumeration step (reset due to error) */
/* Sanity check our callback before calling it */
if(handler_sanity_check_with_panic((physaddr_t)usb_ctrl_callbacks.reset_handler)){
return;
}
else{
usb_ctrl_callbacks.reset_handler();
}
return;
}
/**
* \brief Class request handler.
*
* Class request handling (Run-Time Mode).
*
* @param packet Setup packet
*/
static void usb_ctrl_class_rqst_handler(struct usb_setup_packet *packet){
/* Sanity check our callback before calling it */
if(handler_sanity_check_with_panic((physaddr_t)usb_ctrl_callbacks.class_rqst_handler)){
return;
}
else{
usb_ctrl_callbacks.class_rqst_handler(packet);
}
}
/**
* \brief Vendor request handler.
*
* Class request handling (Run-Time Mode).
*
* @param packet Setup packet
*/
static void usb_ctrl_vendor_rqst_handler(struct usb_setup_packet *packet){
/* Sanity check our callback before calling it */
if(handler_sanity_check_with_panic((physaddr_t)usb_ctrl_callbacks.vendor_rqst_handler)){
return;
}
else{
usb_ctrl_callbacks.vendor_rqst_handler(packet);
}
return;
}
static void usb_ctrl_set_interface_rqst_handler(int iface){
/* Sanity check our callback before calling it */
if(handler_sanity_check_with_panic((physaddr_t)usb_ctrl_callbacks.set_interface_rqst_handler)){
return;
}
else{
usb_ctrl_callbacks.set_interface_rqst_handler(iface);
}
}
static void usb_ctrl_set_configuration_rqst_handler(int conf){
/* Sanity check our callback before calling it */
if(handler_sanity_check_with_panic((physaddr_t)usb_ctrl_callbacks.set_configuration_rqst_handler)){
return;
}
else{
usb_ctrl_callbacks.set_configuration_rqst_handler(conf);
}
}
static void usb_ctrl_functional_desc_rqst_handler(uint16_t wLength){
/* Sanity check our callback before calling it */
if(handler_sanity_check_with_panic((physaddr_t)usb_ctrl_callbacks.functional_rqst_handler)){
return;
}
else{
usb_ctrl_callbacks.functional_rqst_handler(wLength);
}
}
static void mass_storage_mft_string_desc_rqst_handler(uint16_t wLength){
printf("MFT String not supported (Stalling), wLength:\n", wLength);
usb_driver_stall_in(EP0);
}
/**
* \brief Send string descriptor.
*
* @param index String index.
*/
static void usb_ctrl_string_desc_rqst_handler(uint8_t index, uint16_t wLength){
uint32_t i;
uint32_t len;
usb_string_descriptor_t string_desc;
if ( wLength == 0 ) {
usb_driver_setup_send_status(0);
usb_driver_setup_read_status();
goto end;
}
string_desc.bDescriptorType = USB_DESC_STRING;
switch (index) {
case 0:
string_desc.bLength = 4;
string_desc.wString[0] = LANGUAGE_ENGLISH;
break;
case CONFIG_USB_DEV_MANUFACTURER_INDEX:
len = sizeof(CONFIG_USB_DEV_MANUFACTURER);
string_desc.bLength = 2 + 2 * len;
for (i = 0; i < len; i++)
string_desc.wString[i] = CONFIG_USB_DEV_MANUFACTURER[i];
break;
case CONFIG_USB_DEV_PRODNAME_INDEX:
len = sizeof(CONFIG_USB_DEV_PRODNAME);
string_desc.bLength = 2 + 2 * len;
for (i = 0; i < len; i++)
string_desc.wString[i] = CONFIG_USB_DEV_PRODNAME[i];
break;
case CONFIG_USB_DEV_SERIAL_INDEX:
len = sizeof(CONFIG_USB_DEV_SERIAL);
string_desc.bLength = 2 + 2 * len;
for (i = 0; i < len; i++) {
string_desc.wString[i] = CONFIG_USB_DEV_SERIAL[i];
}
break;
case STRING_MICROSOFT_INDEX:
log_printf("STRING_MICROSOFT_INDEX");
/* Sanity check our callback before calling it */
if(handler_sanity_check_with_panic((physaddr_t)usb_ctrl_callbacks.mft_string_rqst_handler)){
return;
}
else{
usb_ctrl_callbacks.mft_string_rqst_handler(wLength);
}
break;
default:
/* TODO: send error status */
aprintf("Invalid string index\n");
usb_driver_stall_in(EP0);
goto end;
}
if ( wLength > string_desc.bLength) {
usb_driver_setup_send((uint8_t *)&string_desc, string_desc.bLength, EP0);
} else {
usb_driver_setup_send((uint8_t *)&string_desc, wLength, EP0);
}
usb_driver_setup_read_status();
end:
return;
}
static void print_setup_packet(struct usb_setup_packet *packet __attribute__((unused))){
log_printf("bmRequestType:%x, bRequest:%x, wValue:%x, wIndex:%x, wLength:%x\n",
packet->bmRequestType,
packet->bRequest,
packet->wValue,
packet->wIndex,
packet->wLength);
}
/**
* \brief Switch function to send descriptors.
*
* @param packet Setup packet
*/
static void usb_ctrl_get_descriptor_rqst_handler(struct usb_setup_packet *packet){
print_setup_packet(packet);
switch (packet->wValue >> 8) {
case USB_DESC_DEVICE:
log_printf("Device descriptor Rqst\n");
usb_ctrl_device_desc_rqst_handler(packet->wLength);
break;
case USB_DESC_CONFIG:
log_printf("Configuration descriptor Rqst\n");
usb_ctrl_configuration_desc_rqst_handler(packet->wLength);
break;
case USB_DESC_STRING:
log_printf("String descriptor Rqst, Index: %x\n", (packet->wValue & 0xff));
usb_ctrl_string_desc_rqst_handler(packet->wValue & 0xff, packet->wLength);
break;
case USB_DESC_FUNCT:
log_printf("Functional descriptor Rqst\n");
usb_ctrl_functional_desc_rqst_handler(packet->wLength);
break;
case USB_DESC_DEVQUAL:
usb_driver_stall_out(0);
break;
default:
/* In case of unsupported descriptor request, we send a
* dummy packet to avoid waiting a timeout from the
* host before sending another request.
*/
usb_driver_setup_send_status(0);
usb_driver_setup_read_status();
aprintf("Unhandled descriptor Rqst: %x\n", packet->wValue >> 8);
break;
}
return;
}
/**
* \brief Standard request handler.
*
* Standard request handling (Run-Time Mode).
*
* @param packet Setup packet
*/
static void usb_ctrl_standard_rqst_handler(struct usb_setup_packet *packet){
switch (packet->bRequest) {
case USB_RQST_SET_ADDRESS:
usb_driver_set_address(packet->wValue);
usb_driver_setup_send_status(0);
break;
case USB_RQST_GET_DESCRIPTOR:
usb_ctrl_get_descriptor_rqst_handler(packet);
break;
case USB_RQST_SET_CONFIGURATION:
usb_ctrl_set_configuration_rqst_handler(packet->wValue);
break;
case USB_RQST_SET_INTERFACE: // TODO > DocID018909 Rev12 p1354/1744
usb_ctrl_set_interface_rqst_handler(packet->wValue);
break;
case USB_RQST_GET_STATUS:
// FIXME Work around
log_printf("USB_RQST_GET_STATUS\n");
usb_driver_setup_send("\x00\x00", 2, EP0);
usb_driver_setup_read_status();
break;
default:
aprintf("Unhandled std request: %x\n", packet->bRequest);
usb_driver_stall_in(EP0);
break;
}
return;
}
/**
* \brief USB control function.
*
* Handles request from host.
*
* @param packet Setup packet
*/
void usb_ctrl_handler(struct usb_setup_packet *packet){
switch ((packet->bmRequestType >> 5) & 0x3) {
case USB_RQST_TYPE_STANDARD:
usb_ctrl_standard_rqst_handler(packet);
break;
case USB_RQST_TYPE_CLASS:
usb_ctrl_class_rqst_handler(packet);
break;
case USB_RQST_TYPE_VENDOR:
usb_ctrl_vendor_rqst_handler(packet);
break;
default:
aprintf("Unhandled request type: %x\n",(packet->bmRequestType >> 5) & 0x3);
break;
}
return;
}
/**
* \brief Initialization of usb ctrl callbacks functions.
* @param callbacks
*/
void usb_ctrl_init( usb_ctrl_callbacks_t cbs,
usb_ctrl_device_descriptor_t device_desc,
usb_ctrl_full_configuration_descriptor_t conf_desc ){
usb_ctrl_init_structures();
if (cbs.class_rqst_handler != NULL){
usb_ctrl_callbacks.class_rqst_handler = cbs.class_rqst_handler;
}
if (cbs.vendor_rqst_handler != NULL){
usb_ctrl_callbacks.vendor_rqst_handler = cbs.vendor_rqst_handler;
}
if (cbs.set_configuration_rqst_handler != NULL){
usb_ctrl_callbacks.set_configuration_rqst_handler = cbs.set_configuration_rqst_handler;
}
if (cbs.set_interface_rqst_handler != NULL){
usb_ctrl_callbacks.set_interface_rqst_handler = cbs.set_interface_rqst_handler;
}
if (cbs.functional_rqst_handler != NULL){
usb_ctrl_callbacks.functional_rqst_handler = cbs.functional_rqst_handler;
}
if (cbs.mft_string_rqst_handler != NULL){
usb_ctrl_callbacks.mft_string_rqst_handler = cbs.mft_string_rqst_handler;
}
if (cbs.reset_handler != NULL){
usb_ctrl_callbacks.reset_handler = cbs.reset_handler;
}
usb_ctrl_device_desc = device_desc;
usb_ctrl_conf_desc = conf_desc;
}