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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
|
/**
* @file usbd_core.c
* @brief
*
* Copyright (c) 2022 sakumisu
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
*/
#include "usbd_core.h"
#define USBD_EP_CALLBACK_LIST_SEARCH 0
#define USBD_EP_CALLBACK_ARR_SEARCH 1
#define USBD_EP_CALLBACK_SEARCH_METHOD USBD_EP_CALLBACK_ARR_SEARCH
/* general descriptor field offsets */
#define DESC_bLength 0 /** Length offset */
#define DESC_bDescriptorType 1 /** Descriptor type offset */
/* config descriptor field offsets */
#define CONF_DESC_wTotalLength 2 /** Total length offset */
#define CONF_DESC_bConfigurationValue 5 /** Configuration value offset */
#define CONF_DESC_bmAttributes 7 /** configuration characteristics */
/* interface descriptor field offsets */
#define INTF_DESC_bInterfaceNumber 2 /** Interface number offset */
#define INTF_DESC_bAlternateSetting 3 /** Alternate setting offset */
#define USB_EP_OUT_NUM 8
#define USB_EP_IN_NUM 8
struct usbd_core_cfg_priv {
/** Setup packet */
USB_MEM_ALIGN32 struct usb_setup_packet setup;
/** Pointer to data buffer */
uint8_t *ep0_data_buf;
/** Remaining bytes in buffer */
uint32_t ep0_data_buf_residue;
/** Total length of control transfer */
uint32_t ep0_data_buf_len;
/** Zero length packet flag of control transfer */
bool zlp_flag;
/** Pointer to registered descriptors */
const uint8_t *descriptors;
/* Buffer used for storing standard, class and vendor request data */
USB_MEM_ALIGN32 uint8_t req_data[CONFIG_USBDEV_REQUEST_BUFFER_LEN];
#if USBD_EP_CALLBACK_SEARCH_METHOD == USBD_EP_CALLBACK_ARR_SEARCH
usbd_endpoint_callback in_ep_cb[USB_EP_IN_NUM];
usbd_endpoint_callback out_ep_cb[USB_EP_OUT_NUM];
#endif
/** Variable to check whether the usb has been configured */
bool configured;
/** Currently selected configuration */
uint8_t configuration;
/** Remote wakeup feature status */
uint16_t remote_wakeup;
} usbd_core_cfg;
static usb_slist_t usbd_class_head = USB_SLIST_OBJECT_INIT(usbd_class_head);
static struct usb_msosv1_descriptor *msosv1_desc;
static struct usb_msosv2_descriptor *msosv2_desc;
static struct usb_bos_descriptor *bos_desc;
#ifdef CONFIG_USBDEV_TEST_MODE
void usbd_set_feature(uint16_t index, uint16_t value);
void usbd_clear_feature(uint16_t index, uint16_t value);
#endif
/**
* @brief print the contents of a setup packet
*
* @param [in] setup The setup packet
*
*/
static void usbd_print_setup(struct usb_setup_packet *setup)
{
USB_LOG_INFO("Setup: "
"bmRequestType 0x%02x, bRequest 0x%02x, wValue 0x%04x, wIndex 0x%04x, wLength 0x%04x\r\n",
setup->bmRequestType,
setup->bRequest,
setup->wValue,
setup->wIndex,
setup->wLength);
}
/**
* @brief Check if the device is in Configured state
*
* @return true if Configured, false otherwise.
*/
static bool is_device_configured(void)
{
return (usbd_core_cfg.configuration != 0);
}
/**
* @brief Check if the interface of given number is valid
*
* @param [in] interface Number of the addressed interface
*
* This function searches through descriptor and checks
* is the Host has addressed valid interface.
*
* @return true if interface exists - valid
*/
static bool is_interface_valid(uint8_t interface)
{
const uint8_t *p = (uint8_t *)usbd_core_cfg.descriptors;
const struct usb_configuration_descriptor *cfg_descr;
/* Search through descriptor for matching interface */
while (p[DESC_bLength] != 0U) {
if (p[DESC_bDescriptorType] == USB_DESCRIPTOR_TYPE_CONFIGURATION) {
cfg_descr = (const struct usb_configuration_descriptor *)p;
if (interface < cfg_descr->bNumInterfaces) {
return true;
}
}
p += p[DESC_bLength];
}
return false;
}
/**
* @brief Check if the endpoint of given address is valid
*
* @param [in] ep Address of the Endpoint
*
* This function checks if the Endpoint of given address
* is valid for the configured device. Valid Endpoint is
* either Control Endpoint or one used by the device.
*
* @return true if endpoint exists - valid
*/
static bool is_ep_valid(uint8_t ep)
{
/* Check if its Endpoint 0 */
if ((ep & 0x7f) == 0) {
return true;
}
return true;
}
#if USBD_EP_CALLBACK_SEARCH_METHOD == USBD_EP_CALLBACK_ARR_SEARCH
static void usbd_ep_callback_register(void)
{
usb_slist_t *i, *j, *k;
usb_slist_for_each(i, &usbd_class_head)
{
usbd_class_t *devclass = usb_slist_entry(i, struct usbd_class, list);
usb_slist_for_each(j, &devclass->intf_list)
{
usbd_interface_t *intf = usb_slist_entry(j, struct usbd_interface, list);
usb_slist_for_each(k, &intf->ep_list)
{
usbd_endpoint_t *ept = usb_slist_entry(k, struct usbd_endpoint, list);
if (ept->ep_cb) {
if (ept->ep_addr & 0x80) {
usbd_core_cfg.in_ep_cb[ept->ep_addr & 0x7f] = ept->ep_cb;
} else {
usbd_core_cfg.out_ep_cb[ept->ep_addr & 0x7f] = ept->ep_cb;
}
}
}
}
}
}
#endif
/**
* @brief configure and enable endpoint
*
* This function sets endpoint configuration according to one specified in USB
* endpoint descriptor and then enables it for data transfers.
*
* @param [in] ep_desc Endpoint descriptor byte array
*
* @return true if successfully configured and enabled
*/
static bool usbd_set_endpoint(const struct usb_endpoint_descriptor *ep_desc)
{
struct usbd_endpoint_cfg ep_cfg;
ep_cfg.ep_addr = ep_desc->bEndpointAddress;
ep_cfg.ep_mps = ep_desc->wMaxPacketSize & USB_MAXPACKETSIZE_MASK;
ep_cfg.ep_type = ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK;
USB_LOG_INFO("Open endpoint:0x%x type:%u mps:%u\r\n",
ep_cfg.ep_addr, ep_cfg.ep_type, ep_cfg.ep_mps);
return usbd_ep_open(&ep_cfg) == 0 ? true : false;
}
/**
* @brief Disable endpoint for transferring data
*
* This function cancels transfers that are associated with endpoint and
* disabled endpoint itself.
*
* @param [in] ep_desc Endpoint descriptor byte array
*
* @return true if successfully deconfigured and disabled
*/
static bool usbd_reset_endpoint(const struct usb_endpoint_descriptor *ep_desc)
{
struct usbd_endpoint_cfg ep_cfg;
ep_cfg.ep_addr = ep_desc->bEndpointAddress;
ep_cfg.ep_mps = ep_desc->wMaxPacketSize & USB_MAXPACKETSIZE_MASK;
ep_cfg.ep_type = ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK;
USB_LOG_INFO("Close endpoint:0x%x type:%u\r\n",
ep_cfg.ep_addr, ep_cfg.ep_type);
return usbd_ep_close(ep_cfg.ep_addr) == 0 ? true : false;
}
/**
* @brief get specified USB descriptor
*
* This function parses the list of installed USB descriptors and attempts
* to find the specified USB descriptor.
*
* @param [in] type_index Type and index of the descriptor
* @param [out] data Descriptor data
* @param [out] len Descriptor length
*
* @return true if the descriptor was found, false otherwise
*/
static bool usbd_get_descriptor(uint16_t type_index, uint8_t **data, uint32_t *len)
{
uint8_t type = 0U;
uint8_t index = 0U;
uint8_t *p = NULL;
uint32_t cur_index = 0U;
bool found = false;
type = GET_DESC_TYPE(type_index);
index = GET_DESC_INDEX(type_index);
if ((type == USB_DESCRIPTOR_TYPE_STRING) && (index == USB_OSDESC_STRING_DESC_INDEX)) {
USB_LOG_INFO("read MS OS 2.0 descriptor string\r\n");
if (!msosv1_desc) {
return false;
}
*data = (uint8_t *)msosv1_desc->string;
*len = msosv1_desc->string_len;
return true;
} else if (type == USB_DESCRIPTOR_TYPE_BINARY_OBJECT_STORE) {
USB_LOG_INFO("read BOS descriptor string\r\n");
if (!bos_desc) {
return false;
}
*data = bos_desc->string;
*len = bos_desc->string_len;
return true;
}
/*
* Invalid types of descriptors,
* see USB Spec. Revision 2.0, 9.4.3 Get Descriptor
*/
else if ((type == USB_DESCRIPTOR_TYPE_INTERFACE) || (type == USB_DESCRIPTOR_TYPE_ENDPOINT) ||
#ifndef CONFIG_USB_HS
(type > USB_DESCRIPTOR_TYPE_ENDPOINT)) {
#else
(type > USB_DESCRIPTOR_TYPE_OTHER_SPEED)) {
#endif
return false;
}
p = (uint8_t *)usbd_core_cfg.descriptors;
cur_index = 0U;
while (p[DESC_bLength] != 0U) {
if (p[DESC_bDescriptorType] == type) {
if (cur_index == index) {
found = true;
break;
}
cur_index++;
}
/* skip to next descriptor */
p += p[DESC_bLength];
}
if (found) {
/* set data pointer */
*data = p;
/* get length from structure */
if ((type == USB_DESCRIPTOR_TYPE_CONFIGURATION) || ((type == USB_DESCRIPTOR_TYPE_OTHER_SPEED))) {
/* configuration descriptor is an
* exception, length is at offset
* 2 and 3
*/
*len = (p[CONF_DESC_wTotalLength]) |
(p[CONF_DESC_wTotalLength + 1] << 8);
} else {
/* normally length is at offset 0 */
*len = p[DESC_bLength];
}
} else {
/* nothing found */
USB_LOG_ERR("descriptor <type:%x,index:%x> not found!\r\n", type, index);
}
return found;
}
/**
* @brief set USB configuration
*
* This function configures the device according to the specified configuration
* index and alternate setting by parsing the installed USB descriptor list.
* A configuration index of 0 unconfigures the device.
*
* @param [in] config_index Configuration index
* @param [in] alt_setting Alternate setting number
*
* @return true if successfully configured false if error or unconfigured
*/
static bool usbd_set_configuration(uint8_t config_index, uint8_t alt_setting)
{
uint8_t *p = (uint8_t *)usbd_core_cfg.descriptors;
uint8_t cur_alt_setting = 0xFF;
uint8_t cur_config = 0xFF;
bool found = false;
if (config_index == 0U) {
/* TODO: unconfigure device */
USB_LOG_ERR("Device not configured - invalid configuration\r\n");
return true;
}
/* configure endpoints for this configuration/altsetting */
while (p[DESC_bLength] != 0U) {
switch (p[DESC_bDescriptorType]) {
case USB_DESCRIPTOR_TYPE_CONFIGURATION:
/* remember current configuration index */
cur_config = p[CONF_DESC_bConfigurationValue];
if (cur_config == config_index) {
found = true;
}
break;
case USB_DESCRIPTOR_TYPE_INTERFACE:
/* remember current alternate setting */
cur_alt_setting =
p[INTF_DESC_bAlternateSetting];
break;
case USB_DESCRIPTOR_TYPE_ENDPOINT:
if ((cur_config != config_index) ||
(cur_alt_setting != alt_setting)) {
break;
}
found = usbd_set_endpoint((struct usb_endpoint_descriptor *)p);
break;
default:
break;
}
/* skip to next descriptor */
p += p[DESC_bLength];
}
return found;
}
/**
* @brief set USB interface
*
* @param [in] iface Interface index
* @param [in] alt_setting Alternate setting number
*
* @return true if successfully configured false if error or unconfigured
*/
static bool usbd_set_interface(uint8_t iface, uint8_t alt_setting)
{
const uint8_t *p = usbd_core_cfg.descriptors;
const uint8_t *if_desc = NULL;
struct usb_endpoint_descriptor *ep_desc;
uint8_t cur_alt_setting = 0xFF;
uint8_t cur_iface = 0xFF;
bool ret = false;
USB_LOG_DBG("iface %u alt_setting %u\r\n", iface, alt_setting);
while (p[DESC_bLength] != 0U) {
switch (p[DESC_bDescriptorType]) {
case USB_DESCRIPTOR_TYPE_INTERFACE:
/* remember current alternate setting */
cur_alt_setting = p[INTF_DESC_bAlternateSetting];
cur_iface = p[INTF_DESC_bInterfaceNumber];
if (cur_iface == iface &&
cur_alt_setting == alt_setting) {
if_desc = (void *)p;
}
USB_LOG_DBG("Current iface %u alt setting %u",
cur_iface, cur_alt_setting);
break;
case USB_DESCRIPTOR_TYPE_ENDPOINT:
if (cur_iface == iface) {
ep_desc = (struct usb_endpoint_descriptor *)p;
if (cur_alt_setting != alt_setting) {
ret = usbd_reset_endpoint(ep_desc);
} else {
ret = usbd_set_endpoint(ep_desc);
}
}
break;
default:
break;
}
/* skip to next descriptor */
p += p[DESC_bLength];
}
usbd_event_notify_handler(USBD_EVENT_SET_INTERFACE, (void *)if_desc);
return ret;
}
/**
* @brief handle a standard device request
*
* @param [in] setup The setup packet
* @param [in,out] data Data buffer
* @param [in,out] len Pointer to data length
*
* @return true if the request was handled successfully
*/
static bool usbd_std_device_req_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
uint16_t value = setup->wValue;
#ifdef CONFIG_USBDEV_TEST_MODE
uint16_t index = setup->wIndex;
#endif
bool ret = true;
switch (setup->bRequest) {
case USB_REQUEST_GET_STATUS:
USB_LOG_DBG("REQ_GET_STATUS\r\n");
/* bit 0: self-powered */
/* bit 1: remote wakeup */
*data = (uint8_t *)&usbd_core_cfg.remote_wakeup;
*len = 2;
break;
case USB_REQUEST_CLEAR_FEATURE:
USB_LOG_DBG("REQ_CLEAR_FEATURE\r\n");
#ifdef CONFIG_USBDEV_TEST_MODE
/* process for feature */
usbd_clear_feature(index, value);
#endif
if (value == USB_FEATURE_REMOTE_WAKEUP) {
usbd_core_cfg.remote_wakeup = 0;
usbd_event_notify_handler(USBD_EVENT_CLEAR_REMOTE_WAKEUP, NULL);
}
break;
case USB_REQUEST_SET_FEATURE:
USB_LOG_DBG("REQ_SET_FEATURE\r\n");
#ifdef CONFIG_USBDEV_TEST_MODE
/* process for feature */
usbd_set_feature(index, value);
#endif
if (value == USB_FEATURE_REMOTE_WAKEUP) {
usbd_core_cfg.remote_wakeup = 1;
usbd_event_notify_handler(USBD_EVENT_SET_REMOTE_WAKEUP, NULL);
}
break;
case USB_REQUEST_SET_ADDRESS:
USB_LOG_DBG("REQ_SET_ADDRESS, addr 0x%x\r\n", value);
usbd_set_address(value);
break;
case USB_REQUEST_GET_DESCRIPTOR:
USB_LOG_DBG("REQ_GET_DESCRIPTOR\r\n");
ret = usbd_get_descriptor(value, data, len);
break;
case USB_REQUEST_SET_DESCRIPTOR:
USB_LOG_DBG("Device req 0x%02x not implemented\r\n", setup->bRequest);
ret = false;
break;
case USB_REQUEST_GET_CONFIGURATION:
USB_LOG_DBG("REQ_GET_CONFIGURATION\r\n");
/* indicate if we are configured */
*data = (uint8_t *)&usbd_core_cfg.configuration;
*len = 1;
break;
case USB_REQUEST_SET_CONFIGURATION:
value &= 0xFF;
USB_LOG_DBG("REQ_SET_CONFIGURATION, conf 0x%x\r\n", value);
if (!usbd_set_configuration(value, 0)) {
USB_LOG_DBG("USB Set Configuration failed\r\n");
ret = false;
} else {
/* configuration successful,
* update current configuration
*/
usbd_core_cfg.configuration = value;
usbd_core_cfg.configured = true;
usbd_event_notify_handler(USBD_EVENT_CONFIGURED, NULL);
}
break;
case USB_REQUEST_GET_INTERFACE:
break;
case USB_REQUEST_SET_INTERFACE:
break;
default:
USB_LOG_ERR("Illegal device req 0x%02x\r\n", setup->bRequest);
ret = false;
break;
}
return ret;
}
/**
* @brief handle a standard interface request
*
* @param [in] setup The setup packet
* @param [in,out] data Data buffer
* @param [in,out] len Pointer to data length
*
* @return true if the request was handled successfully
*/
static bool usbd_std_interface_req_handler(struct usb_setup_packet *setup,
uint8_t **data, uint32_t *len)
{
/** The device must be configured to accept standard interface
* requests and the addressed Interface must be valid.
*/
if (!is_device_configured() ||
(!is_interface_valid((uint8_t)setup->wIndex))) {
return false;
}
switch (setup->bRequest) {
case USB_REQUEST_GET_STATUS:
/* no bits specified */
*data = (uint8_t *)&usbd_core_cfg.remote_wakeup;
*len = 2;
break;
case USB_REQUEST_CLEAR_FEATURE:
case USB_REQUEST_SET_FEATURE:
/* not defined for interface */
return false;
case USB_REQUEST_GET_INTERFACE:
/** This handler is called for classes that does not support
* alternate Interfaces so always return 0. Classes that
* support alternative interfaces handles GET_INTERFACE
* in custom_handler.
*/
(*data)[0] = 0;
*len = 1;
break;
case USB_REQUEST_SET_INTERFACE:
USB_LOG_DBG("REQ_SET_INTERFACE\r\n");
usbd_set_interface(setup->wIndex, setup->wValue);
break;
default:
USB_LOG_ERR("Illegal interface req 0x%02x\r\n", setup->bRequest);
return false;
}
return true;
}
/**
* @brief handle a standard endpoint request
*
* @param [in] setup The setup packet
* @param [in,out] data Data buffer
* @param [in,out] len Pointer to data length
*
* @return true if the request was handled successfully
*/
static bool usbd_std_endpoint_req_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
uint8_t ep = (uint8_t)setup->wIndex;
/* Check if request addresses valid Endpoint */
if (!is_ep_valid(ep)) {
return false;
}
switch (setup->bRequest) {
case USB_REQUEST_GET_STATUS:
/** This request is valid for Control Endpoints when
* the device is not yet configured. For other
* Endpoints the device must be configured.
* Firstly check if addressed ep is Control Endpoint.
* If no then the device must be in Configured state
* to accept the request.
*/
if (((ep & 0x7f) == 0) || is_device_configured()) {
/* bit 0 - Endpoint halted or not */
usbd_ep_is_stalled(ep, (uint8_t *)&usbd_core_cfg.remote_wakeup);
*data = (uint8_t *)&usbd_core_cfg.remote_wakeup;
*len = 2;
break;
}
return false;
case USB_REQUEST_CLEAR_FEATURE:
if (setup->wValue == USB_FEATURE_ENDPOINT_HALT) {
/** This request is valid for Control Endpoints when
* the device is not yet configured. For other
* Endpoints the device must be configured.
* Firstly check if addressed ep is Control Endpoint.
* If no then the device must be in Configured state
* to accept the request.
*/
if (((ep & 0x7f) == 0) || is_device_configured()) {
USB_LOG_ERR("ep:%x clear halt\r\n", ep);
usbd_ep_clear_stall(ep);
usbd_event_notify_handler(USBD_EVENT_CLEAR_HALT, NULL);
break;
}
}
/* only ENDPOINT_HALT defined for endpoints */
return false;
case USB_REQUEST_SET_FEATURE:
if (setup->wValue == USB_FEATURE_ENDPOINT_HALT) {
/** This request is valid for Control Endpoints when
* the device is not yet configured. For other
* Endpoints the device must be configured.
* Firstly check if addressed ep is Control Endpoint.
* If no then the device must be in Configured state
* to accept the request.
*/
if (((ep & 0x7f) == 0) || is_device_configured()) {
/* set HALT by stalling */
USB_LOG_ERR("ep:%x set halt\r\n", ep);
usbd_ep_set_stall(ep);
usbd_event_notify_handler(USBD_EVENT_SET_HALT, NULL);
break;
}
}
/* only ENDPOINT_HALT defined for endpoints */
return false;
case USB_REQUEST_SYNCH_FRAME:
/* For Synch Frame request the device must be configured */
if (is_device_configured()) {
/* Not supported, return false anyway */
USB_LOG_DBG("ep req 0x%02x not implemented\r\n", setup->bRequest);
}
return false;
default:
USB_LOG_ERR("Illegal ep req 0x%02x\r\n", setup->bRequest);
return false;
}
return true;
}
/**
* @brief default handler for standard ('chapter 9') requests
*
* If a custom request handler was installed, this handler is called first.
*
* @param [in] setup The setup packet
* @param [in,out] data Data buffer
* @param [in,out] len Pointer to data length
*
* @return true if the request was handled successfully
*/
static int usbd_standard_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
int rc = 0;
switch (setup->bmRequestType & USB_REQUEST_RECIPIENT_MASK) {
case USB_REQUEST_RECIPIENT_DEVICE:
if (usbd_std_device_req_handler(setup, data, len) == false) {
rc = -1;
}
break;
case USB_REQUEST_RECIPIENT_INTERFACE:
if (usbd_std_interface_req_handler(setup, data, len) == false) {
rc = -1;
}
break;
case USB_REQUEST_RECIPIENT_ENDPOINT:
if (usbd_std_endpoint_req_handler(setup, data, len) == false) {
rc = -1;
}
break;
default:
rc = -1;
}
return rc;
}
/**
* @brief handler for class requests
*
* If a custom request handler was installed, this handler is called first.
*
* @param [in] setup The setup packet
* @param [in,out] data Data buffer
* @param [in,out] len Pointer to data length
*
* @return true if the request was handled successfully
*/
static int usbd_class_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
USB_LOG_DBG("bRequest 0x%02x, wIndex 0x%04x\r\n", setup->bRequest, setup->wIndex);
usb_slist_t *i, *j;
if ((setup->bmRequestType & USB_REQUEST_RECIPIENT_MASK) == USB_REQUEST_RECIPIENT_INTERFACE) {
usb_slist_for_each(i, &usbd_class_head)
{
usbd_class_t *devclass = usb_slist_entry(i, struct usbd_class, list);
usb_slist_for_each(j, &devclass->intf_list)
{
usbd_interface_t *intf = usb_slist_entry(j, struct usbd_interface, list);
if (intf->class_handler && (intf->intf_num == (setup->wIndex & 0xFF))) {
return intf->class_handler(setup, data, len);
}
}
}
} else if ((setup->bmRequestType & USB_REQUEST_RECIPIENT_MASK) == USB_REQUEST_RECIPIENT_ENDPOINT) {
usb_slist_for_each(i, &usbd_class_head)
{
usbd_class_t *devclass = usb_slist_entry(i, struct usbd_class, list);
usb_slist_for_each(j, &devclass->intf_list)
{
usbd_interface_t *intf = usb_slist_entry(j, struct usbd_interface, list);
if (intf->custom_handler && (intf->intf_num == ((setup->wIndex >> 8) & 0xFF))) {
return intf->custom_handler(setup, data, len);
}
}
}
}
return -1;
}
/**
* @brief handler for vendor requests
*
* If a custom request handler was installed, this handler is called first.
*
* @param [in] setup The setup packet
* @param [in,out] data Data buffer
* @param [in,out] len Pointer to data length
*
* @return true if the request was handled successfully
*/
static int usbd_vendor_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
USB_LOG_DBG("bRequest 0x%02x, wValue0x%04x, wIndex 0x%04x\r\n", setup->bRequest, setup->wValue, setup->wIndex);
// if((setup->bmRequestType & USB_REQUEST_RECIPIENT_MASK) != USB_REQUEST_RECIPIENT_DEVICE)
// {
// return -1;
// }
if (msosv1_desc) {
if (setup->bRequest == msosv1_desc->vendor_code) {
switch (setup->wIndex) {
case 0x04:
USB_LOG_INFO("get Compat ID\r\n");
*data = (uint8_t *)msosv1_desc->compat_id;
*len = msosv1_desc->compat_id_len;
return 0;
case 0x05:
USB_LOG_INFO("get Compat id properties\r\n");
*data = (uint8_t *)msosv1_desc->comp_id_property;
*len = msosv1_desc->comp_id_property_len;
return 0;
default:
USB_LOG_ERR("unknown vendor code\r\n");
return -1;
}
}
} else if (msosv2_desc) {
if (setup->bRequest == msosv2_desc->vendor_code) {
switch (setup->wIndex) {
case WINUSB_REQUEST_GET_DESCRIPTOR_SET:
USB_LOG_INFO("GET MS OS 2.0 Descriptor\r\n");
*data = (uint8_t *)msosv2_desc->compat_id;
*len = msosv2_desc->compat_id_len;
return 0;
default:
USB_LOG_ERR("unknown vendor code\r\n");
return -1;
}
}
}
usb_slist_t *i, *j;
usb_slist_for_each(i, &usbd_class_head)
{
usbd_class_t *devclass = usb_slist_entry(i, struct usbd_class, list);
usb_slist_for_each(j, &devclass->intf_list)
{
usbd_interface_t *intf = usb_slist_entry(j, struct usbd_interface, list);
if (intf->vendor_handler && !intf->vendor_handler(setup, data, len)) {
return 0;
}
}
}
return -1;
}
/**
* @brief handler for special requests
*
* If a custom request handler was installed, this handler is called first.
*
* @param [in] setup The setup packet
* @param [in,out] data Data buffer
* @param [in,out] len Pointer to data length
*
* @return true if the request was handled successfully
*/
static int usbd_custom_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
USB_LOG_DBG("bRequest 0x%02x, wIndex 0x%04x\r\n", setup->bRequest, setup->wIndex);
if ((setup->bmRequestType & USB_REQUEST_RECIPIENT_MASK) != USB_REQUEST_RECIPIENT_INTERFACE) {
return -1;
}
usb_slist_t *i, *j;
usb_slist_for_each(i, &usbd_class_head)
{
usbd_class_t *devclass = usb_slist_entry(i, struct usbd_class, list);
usb_slist_for_each(j, &devclass->intf_list)
{
usbd_interface_t *intf = usb_slist_entry(j, struct usbd_interface, list);
if (intf->custom_handler && (intf->intf_num == (setup->wIndex & 0xFF))) {
return intf->custom_handler(setup, data, len);
}
}
}
return -1;
}
/**
* @brief handle a request by calling one of the installed request handlers
*
* Local function to handle a request by calling one of the installed request
* handlers. In case of data going from host to device, the data is at *ppbData.
* In case of data going from device to host, the handler can either choose to
* write its data at *ppbData or update the data pointer.
*
* @param [in] setup The setup packet
* @param [in,out] data Data buffer
* @param [in,out] len Pointer to data length
*
* @return true if the request was handles successfully
*/
static bool usbd_setup_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
uint8_t type = setup->bmRequestType & USB_REQUEST_TYPE_MASK;
if (!usbd_custom_request_handler(setup, data, len)) {
return true;
}
if (type == USB_REQUEST_STANDARD) {
if (usbd_standard_request_handler(setup, data, len) < 0) {
USB_LOG_ERR("standard request error\r\n");
usbd_print_setup(setup);
return false;
}
} else if (type == USB_REQUEST_CLASS) {
if (usbd_class_request_handler(setup, data, len) < 0) {
USB_LOG_ERR("class request error\r\n");
usbd_print_setup(setup);
return false;
}
} else if (type == USB_REQUEST_VENDOR) {
if (usbd_vendor_request_handler(setup, data, len) < 0) {
USB_LOG_ERR("vendor request error\r\n");
usbd_print_setup(setup);
return false;
}
} else {
return false;
}
return true;
}
/**
* @brief send data or status to host
*
* @return N/A
*/
static void usbd_send_to_host(uint16_t len)
{
uint32_t chunk = 0U;
if (usbd_core_cfg.zlp_flag == false) {
chunk = usbd_core_cfg.ep0_data_buf_residue;
if (usbd_ep_write(USB_CONTROL_IN_EP0, usbd_core_cfg.ep0_data_buf, usbd_core_cfg.ep0_data_buf_residue, &chunk) < 0) {
USB_LOG_ERR("USB write data failed\r\n");
return;
}
usbd_core_cfg.ep0_data_buf += chunk;
usbd_core_cfg.ep0_data_buf_residue -= chunk;
/*
* Set ZLP flag when host asks for a bigger length and the
* last chunk is wMaxPacketSize long, to indicate the last
* packet.
*/
if (!usbd_core_cfg.ep0_data_buf_residue && (len > usbd_core_cfg.ep0_data_buf_len) && !(usbd_core_cfg.ep0_data_buf_len % USB_CTRL_EP_MPS)) {
/* Transfers a zero-length packet next*/
usbd_core_cfg.zlp_flag = true;
}
} else {
usbd_core_cfg.zlp_flag = false;
USB_LOG_DBG("send zlp\r\n");
if (usbd_ep_write(USB_CONTROL_IN_EP0, NULL, 0, NULL) < 0) {
USB_LOG_ERR("USB write zlp failed\r\n");
return;
}
}
}
static void usbd_ep0_setup_handler(void)
{
struct usb_setup_packet *setup = &usbd_core_cfg.setup;
/*
* OUT transfer, Setup packet,
* reset request message state machine
*/
if (usbd_ep_read(USB_CONTROL_OUT_EP0, (uint8_t *)setup,
sizeof(struct usb_setup_packet), NULL) < 0) {
USB_LOG_ERR("Read Setup Packet failed\r\n");
usbd_ep_set_stall(USB_CONTROL_IN_EP0);
return;
}
#ifdef CONFIG_USBDEV_SETUP_LOG_PRINT
usbd_print_setup(setup);
#endif
if (setup->wLength > CONFIG_USBDEV_REQUEST_BUFFER_LEN) {
if ((setup->bmRequestType & USB_REQUEST_DIR_MASK) == USB_REQUEST_DIR_OUT) {
USB_LOG_ERR("Request buffer too small\r\n");
usbd_ep_set_stall(USB_CONTROL_IN_EP0);
return;
}
}
usbd_core_cfg.ep0_data_buf = usbd_core_cfg.req_data;
usbd_core_cfg.ep0_data_buf_residue = setup->wLength;
usbd_core_cfg.ep0_data_buf_len = setup->wLength;
usbd_core_cfg.zlp_flag = false;
/* this maybe set code in class request code */
if (setup->wLength &&
(setup->bmRequestType & USB_REQUEST_DIR_MASK) == USB_REQUEST_DIR_OUT) {
USB_LOG_DBG("prepare to out data\r\n");
return;
}
/* Ask installed handler to process request */
if (!usbd_setup_request_handler(setup, &usbd_core_cfg.ep0_data_buf, &usbd_core_cfg.ep0_data_buf_len)) {
usbd_ep_set_stall(USB_CONTROL_IN_EP0);
return;
}
/* Send smallest of requested and offered length */
usbd_core_cfg.ep0_data_buf_residue = MIN(usbd_core_cfg.ep0_data_buf_len,
setup->wLength);
#ifdef CONFIG_USB_DCACHE_ENABLE
/* check if the data buf addr uses usbd_core_cfg.req_data */
if (((unsigned long)usbd_core_cfg.ep0_data_buf) != ((unsigned long)usbd_core_cfg.req_data)) {
/*copy data buf from misalign32 addr to align32 addr*/
memcpy(usbd_core_cfg.req_data, usbd_core_cfg.ep0_data_buf, usbd_core_cfg.ep0_data_buf_residue);
usbd_core_cfg.ep0_data_buf = usbd_core_cfg.req_data;
}
#endif
/*Send data or status to host*/
usbd_send_to_host(setup->wLength);
}
static void usbd_ep0_out_handler(void)
{
uint32_t chunk = 0U;
struct usb_setup_packet *setup = &usbd_core_cfg.setup;
/* OUT transfer, status packets */
if (usbd_core_cfg.ep0_data_buf_residue == 0) {
/* absorb zero-length status message */
USB_LOG_DBG("recv status\r\n");
if (usbd_ep_read(USB_CONTROL_OUT_EP0,
NULL,
0, NULL) < 0) {
USB_LOG_ERR("Read DATA Packet failed\r\n");
usbd_ep_set_stall(USB_CONTROL_IN_EP0);
}
return;
}
/* OUT transfer, data packets */
if (usbd_ep_read(USB_CONTROL_OUT_EP0,
usbd_core_cfg.ep0_data_buf,
usbd_core_cfg.ep0_data_buf_residue, &chunk) < 0) {
USB_LOG_ERR("Read DATA Packet failed\r\n");
usbd_ep_set_stall(USB_CONTROL_IN_EP0);
return;
}
usbd_core_cfg.ep0_data_buf += chunk;
usbd_core_cfg.ep0_data_buf_residue -= chunk;
if (usbd_core_cfg.ep0_data_buf_residue == 0) {
/* Received all, send data to handler */
usbd_core_cfg.ep0_data_buf = usbd_core_cfg.req_data;
if (!usbd_setup_request_handler(setup, &usbd_core_cfg.ep0_data_buf, &usbd_core_cfg.ep0_data_buf_len)) {
usbd_ep_set_stall(USB_CONTROL_IN_EP0);
return;
}
/*Send status to host*/
usbd_send_to_host(setup->wLength);
} else {
}
}
static void usbd_ep0_in_handler(void)
{
struct usb_setup_packet *setup = &usbd_core_cfg.setup;
/* Send more data if available */
if (usbd_core_cfg.ep0_data_buf_residue != 0 || usbd_core_cfg.zlp_flag == true) {
usbd_send_to_host(setup->wLength);
} else {
/*ep0 tx has completed,and no data to send,so do nothing*/
}
}
static void usbd_ep_out_handler(uint8_t ep)
{
#if USBD_EP_CALLBACK_SEARCH_METHOD == USBD_EP_CALLBACK_LIST_SEARCH
usb_slist_t *i, *j, *k;
usb_slist_for_each(i, &usbd_class_head)
{
usbd_class_t *devclass = usb_slist_entry(i, struct usbd_class, list);
usb_slist_for_each(j, &devclass->intf_list)
{
usbd_interface_t *intf = usb_slist_entry(j, struct usbd_interface, list);
usb_slist_for_each(k, &intf->ep_list)
{
usbd_endpoint_t *ept = usb_slist_entry(k, struct usbd_endpoint, list);
if ((ept->ep_addr == ep) && ept->ep_cb) {
ept->ep_cb(ep);
}
}
}
}
#else
if (usbd_core_cfg.out_ep_cb[ep & 0x7f]) {
usbd_core_cfg.out_ep_cb[ep & 0x7f](ep);
}
#endif
}
static void usbd_ep_in_handler(uint8_t ep)
{
#if USBD_EP_CALLBACK_SEARCH_METHOD == USBD_EP_CALLBACK_LIST_SEARCH
usb_slist_t *i, *j, *k;
usb_slist_for_each(i, &usbd_class_head)
{
usbd_class_t *devclass = usb_slist_entry(i, struct usbd_class, list);
usb_slist_for_each(j, &devclass->intf_list)
{
usbd_interface_t *intf = usb_slist_entry(j, struct usbd_interface, list);
usb_slist_for_each(k, &intf->ep_list)
{
usbd_endpoint_t *ept = usb_slist_entry(k, struct usbd_endpoint, list);
if ((ept->ep_addr == ep) && ept->ep_cb) {
ept->ep_cb(ep);
}
}
}
}
#else
if (usbd_core_cfg.in_ep_cb[ep & 0x7f]) {
usbd_core_cfg.in_ep_cb[ep & 0x7f](ep);
}
#endif
}
static void usbd_class_event_notify_handler(uint8_t event, void *arg)
{
usb_slist_t *i;
usb_slist_for_each(i, &usbd_class_head)
{
usbd_class_t *devclass = usb_slist_entry(i, struct usbd_class, list);
usbd_interface_t *intf = usb_slist_first_entry(&devclass->intf_list, struct usbd_interface, list);
if (intf->notify_handler) {
intf->notify_handler(event, arg);
}
}
}
void usbd_event_notify_handler(uint8_t event, void *arg)
{
switch (event) {
case USBD_EVENT_RESET:
usbd_set_address(0);
usbd_core_cfg.configured = 0;
usbd_core_cfg.configuration = 0;
struct usbd_endpoint_cfg ep0_cfg;
ep0_cfg.ep_mps = USB_CTRL_EP_MPS;
ep0_cfg.ep_type = USB_ENDPOINT_TYPE_CONTROL;
ep0_cfg.ep_addr = USB_CONTROL_IN_EP0;
/*set USB_CONTROL_IN_EP0 nak*/
usbd_ep_open(&ep0_cfg);
ep0_cfg.ep_addr = USB_CONTROL_OUT_EP0;
/*set USB_CONTROL_OUT_EP0 ack to prepare receiving setup data*/
usbd_ep_open(&ep0_cfg);
#if USBD_EP_CALLBACK_SEARCH_METHOD == USBD_EP_CALLBACK_ARR_SEARCH
usbd_ep_callback_register();
#endif
case USBD_EVENT_ERROR:
case USBD_EVENT_SOF:
case USBD_EVENT_CONNECTED:
case USBD_EVENT_CONFIGURED:
case USBD_EVENT_SUSPEND:
case USBD_EVENT_DISCONNECTED:
case USBD_EVENT_RESUME:
case USBD_EVENT_SET_INTERFACE:
case USBD_EVENT_SET_REMOTE_WAKEUP:
case USBD_EVENT_CLEAR_REMOTE_WAKEUP:
case USBD_EVENT_SET_HALT:
case USBD_EVENT_CLEAR_HALT:
usbd_class_event_notify_handler(event, arg);
break;
case USBD_EVENT_SETUP_NOTIFY:
usbd_ep0_setup_handler();
break;
case USBD_EVENT_EP0_IN_NOTIFY:
usbd_ep0_in_handler();
break;
case USBD_EVENT_EP0_OUT_NOTIFY:
usbd_ep0_out_handler();
break;
case USBD_EVENT_EP_IN_NOTIFY:
usbd_ep_in_handler((uint8_t)(unsigned long)arg);
break;
case USBD_EVENT_EP_OUT_NOTIFY:
usbd_ep_out_handler((uint8_t)(unsigned long)arg);
break;
default:
USB_LOG_ERR("USB unknown event: %d\r\n", event);
break;
}
}
void usbd_desc_register(const uint8_t *desc)
{
usbd_core_cfg.descriptors = desc;
}
/* Register MS OS Descriptors version 1 */
void usbd_msosv1_desc_register(struct usb_msosv1_descriptor *desc)
{
msosv1_desc = desc;
}
/* Register MS OS Descriptors version 2 */
void usbd_msosv2_desc_register(struct usb_msosv2_descriptor *desc)
{
msosv2_desc = desc;
}
void usbd_bos_desc_register(struct usb_bos_descriptor *desc)
{
bos_desc = desc;
}
void usbd_class_register(usbd_class_t *devclass)
{
usb_slist_add_tail(&usbd_class_head, &devclass->list);
usb_slist_init(&devclass->intf_list);
}
void usbd_class_add_interface(usbd_class_t *devclass, usbd_interface_t *intf)
{
static uint8_t intf_offset = 0;
intf->intf_num = intf_offset;
usb_slist_add_tail(&devclass->intf_list, &intf->list);
usb_slist_init(&intf->ep_list);
intf_offset++;
}
void usbd_interface_add_endpoint(usbd_interface_t *intf, usbd_endpoint_t *ep)
{
usb_slist_add_tail(&intf->ep_list, &ep->list);
}
bool usb_device_is_configured(void)
{
return usbd_core_cfg.configured;
}
int usbd_initialize(void)
{
usb_dc_init();
return 0;
}
#ifdef CONFIG_USBDEV_TEST_MODE
__WEAK void usbd_set_feature(uint16_t index, uint16_t value)
{
}
__WEAK void usbd_clear_feature(uint16_t index, uint16_t value)
{
}
#endif
|