-
Notifications
You must be signed in to change notification settings - Fork 24
/
usb_device_dci.c
1389 lines (1285 loc) · 56 KB
/
usb_device_dci.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
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
/*
* Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
* Copyright 2016 - 2017,2019 NXP
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "usb_device_config.h"
#include "usb.h"
#include "usb_device.h"
#include "usb_device_dci.h"
#include "fsl_device_registers.h"
#if ((defined(USB_DEVICE_CONFIG_NUM)) && (USB_DEVICE_CONFIG_NUM > 0U))
#if ((defined(USB_DEVICE_CONFIG_KHCI)) && (USB_DEVICE_CONFIG_KHCI > 0U))
#include "usb_device_khci.h"
#endif
#if ((defined(USB_DEVICE_CONFIG_EHCI)) && (USB_DEVICE_CONFIG_EHCI > 0U))
#include "usb_device_ehci.h"
#endif
#if (((defined(USB_DEVICE_CONFIG_LPCIP3511FS)) && (USB_DEVICE_CONFIG_LPCIP3511FS > 0U)) || \
((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U)))
#include "usb_device_lpcip3511.h"
#endif
#if ((defined(USB_DEVICE_CONFIG_DWC3)) && (USB_DEVICE_CONFIG_DWC3 > 0U))
#include "usb_device_dwc3.h"
#endif
#if (defined(USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE) && (USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE > 0U))
#include "fsl_cache.h"
#endif
/*******************************************************************************
* Definitions
******************************************************************************/
/* Component ID definition, used by tools. */
#ifndef FSL_COMPONENT_ID
#define FSL_COMPONENT_ID "middleware.usb.device_stack"
#endif
#if defined __CORTEX_M && (__CORTEX_M == 7U)
#if (defined(USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE) && (USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE > 0U))
#warning USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE is not supported.
#endif
#endif
/*******************************************************************************
* Prototypes
******************************************************************************/
static usb_status_t USB_DeviceAllocateHandle(uint8_t controllerId, usb_device_struct_t **handle);
static usb_status_t USB_DeviceFreeHandle(usb_device_struct_t *handle);
static usb_status_t USB_DeviceGetControllerInterface(
uint8_t controllerId, const usb_device_controller_interface_struct_t **controllerInterface);
static usb_status_t USB_DeviceTransfer(usb_device_handle handle,
uint8_t endpointAddress,
uint8_t *buffer,
uint32_t length);
static usb_status_t USB_DeviceControl(usb_device_handle handle, usb_device_control_type_t type, void *param);
static usb_status_t USB_DeviceResetNotification(usb_device_struct_t *handle,
usb_device_callback_message_struct_t *message);
#if (defined(USB_DEVICE_CONFIG_LOW_POWER_MODE) && (USB_DEVICE_CONFIG_LOW_POWER_MODE > 0U))
static usb_status_t USB_DeviceSuspendNotification(usb_device_struct_t *handle,
usb_device_callback_message_struct_t *message);
static usb_status_t USB_DeviceResumeNotification(usb_device_struct_t *handle,
usb_device_callback_message_struct_t *message);
#if (defined(USB_DEVICE_CONFIG_LPM_L1) && (USB_DEVICE_CONFIG_LPM_L1 > 0U))
static usb_status_t USB_DeviceSleepNotification(usb_device_struct_t *handle,
usb_device_callback_message_struct_t *message);
#endif
#endif /* USB_DEVICE_CONFIG_LOW_POWER_MODE */
#if (defined(USB_DEVICE_CONFIG_DETACH_ENABLE) && (USB_DEVICE_CONFIG_DETACH_ENABLE > 0U))
static usb_status_t USB_DeviceDetachNotification(usb_device_struct_t *handle,
usb_device_callback_message_struct_t *message);
static usb_status_t USB_DeviceAttachNotification(usb_device_struct_t *handle,
usb_device_callback_message_struct_t *message);
#endif
static usb_status_t USB_DeviceNotification(usb_device_struct_t *handle, usb_device_callback_message_struct_t *message);
/*******************************************************************************
* Variables
******************************************************************************/
USB_GLOBAL static usb_device_struct_t s_UsbDevice[USB_DEVICE_CONFIG_NUM];
/*******************************************************************************
* Code
******************************************************************************/
/*!
* @brief Allocate a device handle.
*
* This function allocates a device handle.
*
* @param controllerId The controller id of the USB IP. Please refer to the enumeration usb_controller_index_t.
* @param handle It is out parameter, is used to return pointer of the device handle to the caller.
*
* @retval kStatus_USB_Success Get a device handle successfully.
* @retval kStatus_USB_Busy Cannot allocate a device handle.
* @retval kStatus_USB_Error The device has been initialized.
*/
static usb_status_t USB_DeviceAllocateHandle(uint8_t controllerId, usb_device_struct_t **handle)
{
uint32_t count;
OSA_SR_ALLOC();
OSA_ENTER_CRITICAL();
/* Check the controller is initialized or not. */
for (count = 0U; count < USB_DEVICE_CONFIG_NUM; count++)
{
if ((NULL != s_UsbDevice[count].controllerHandle) && (controllerId == s_UsbDevice[count].controllerId))
{
OSA_EXIT_CRITICAL();
return kStatus_USB_Error;
}
}
/* Get a free device handle. */
for (count = 0U; count < USB_DEVICE_CONFIG_NUM; count++)
{
if (NULL == s_UsbDevice[count].controllerHandle)
{
s_UsbDevice[count].controllerId = controllerId;
*handle = &s_UsbDevice[count];
OSA_EXIT_CRITICAL();
return kStatus_USB_Success;
}
}
OSA_EXIT_CRITICAL();
return kStatus_USB_Busy;
}
/*!
* @brief Free a device handle.
*
* This function frees a device handle.
*
* @param handle The device handle.
*
* @retval kStatus_USB_Success Free device handle successfully.
*/
static usb_status_t USB_DeviceFreeHandle(usb_device_struct_t *handle)
{
OSA_SR_ALLOC();
OSA_ENTER_CRITICAL();
handle->controllerHandle = NULL;
handle->controllerId = 0U;
OSA_EXIT_CRITICAL();
return kStatus_USB_Success;
}
#if ((defined(USB_DEVICE_CONFIG_KHCI)) && (USB_DEVICE_CONFIG_KHCI > 0U))
/* KHCI device driver interface */
static const usb_device_controller_interface_struct_t s_UsbDeviceKhciInterface = {
USB_DeviceKhciInit, USB_DeviceKhciDeinit, USB_DeviceKhciSend,
USB_DeviceKhciRecv, USB_DeviceKhciCancel, USB_DeviceKhciControl};
#endif
#if ((defined(USB_DEVICE_CONFIG_EHCI)) && (USB_DEVICE_CONFIG_EHCI > 0U))
/* EHCI device driver interface */
static const usb_device_controller_interface_struct_t s_UsbDeviceEhciInterface = {
USB_DeviceEhciInit, USB_DeviceEhciDeinit, USB_DeviceEhciSend,
USB_DeviceEhciRecv, USB_DeviceEhciCancel, USB_DeviceEhciControl};
#endif
#if (((defined(USB_DEVICE_CONFIG_LPCIP3511FS)) && (USB_DEVICE_CONFIG_LPCIP3511FS > 0U)) || \
((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U)))
/* EHCI device driver interface */
static const usb_device_controller_interface_struct_t s_UsbDeviceLpc3511IpInterface = {
USB_DeviceLpc3511IpInit, USB_DeviceLpc3511IpDeinit, USB_DeviceLpc3511IpSend,
USB_DeviceLpc3511IpRecv, USB_DeviceLpc3511IpCancel, USB_DeviceLpc3511IpControl};
#endif
#if ((defined(USB_DEVICE_CONFIG_DWC3)) && (USB_DEVICE_CONFIG_DWC3 > 0U))
/* EHCI device driver interface */
static const usb_device_controller_interface_struct_t s_UsbDeviceDwc3Interface = {
USB_DeviceDwc3Init, USB_DeviceDwc3Deinit, USB_DeviceDwc3Send,
USB_DeviceDwc3Recv, USB_DeviceDwc3Cancel, USB_DeviceDwc3Control};
#endif
/*!
* @brief Get the controller interface handle.
*
* This function is used to get the controller interface handle.
*
* @param controllerId The controller id of the USB IP. Please refer to the enumeration usb_controller_index_t.
* @param controllerInterface It is out parameter, is used to return pointer of the device controller handle to the
* caller.
*
* @retval kStatus_USB_Success Get a device handle successfully.
* @retval kStatus_USB_ControllerNotFound The controller id is invalid.
*/
static usb_status_t USB_DeviceGetControllerInterface(
uint8_t controllerId, const usb_device_controller_interface_struct_t **controllerInterface)
{
usb_status_t error = kStatus_USB_ControllerNotFound;
usb_controller_index_t controlerIndex = (usb_controller_index_t)controllerId;
#if ((defined(USB_DEVICE_CONFIG_KHCI)) && (USB_DEVICE_CONFIG_KHCI > 0U))
/* Get the KHCI controller driver interface */
if ((kUSB_ControllerKhci0 == controlerIndex) || (kUSB_ControllerKhci1 == controlerIndex))
{
*controllerInterface = (const usb_device_controller_interface_struct_t *)&s_UsbDeviceKhciInterface;
error = kStatus_USB_Success;
}
#endif
#if ((defined(USB_DEVICE_CONFIG_EHCI)) && (USB_DEVICE_CONFIG_EHCI > 0U))
/* Get the EHCI controller driver interface */
if ((kUSB_ControllerEhci0 == controlerIndex) || (kUSB_ControllerEhci1 == controlerIndex))
{
*controllerInterface = (const usb_device_controller_interface_struct_t *)&s_UsbDeviceEhciInterface;
error = kStatus_USB_Success;
}
#endif
#if (((defined(USB_DEVICE_CONFIG_LPCIP3511FS)) && (USB_DEVICE_CONFIG_LPCIP3511FS > 0U)) || \
((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U)))
/* Get the EHCI controller driver interface */
if ((kUSB_ControllerLpcIp3511Fs0 == controlerIndex) || (kUSB_ControllerLpcIp3511Fs1 == controlerIndex) ||
(kUSB_ControllerLpcIp3511Hs0 == controlerIndex) || (kUSB_ControllerLpcIp3511Hs1 == controlerIndex))
{
*controllerInterface = (const usb_device_controller_interface_struct_t *)&s_UsbDeviceLpc3511IpInterface;
error = kStatus_USB_Success;
}
#endif
#if ((defined(USB_DEVICE_CONFIG_DWC3)) && (USB_DEVICE_CONFIG_DWC3 > 0U))
/* Get the EHCI controller driver interface */
if ((kUSB_ControllerDwc30 == controlerIndex) || (kUSB_ControllerDwc31 == controlerIndex))
{
*controllerInterface = (const usb_device_controller_interface_struct_t *)&s_UsbDeviceDwc3Interface;
error = kStatus_USB_Success;
}
#endif
return error;
}
/*!
* @brief Start a new transfer.
*
* This function is used to start a new transfer.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param endpointAddress Endpoint address. Bit7 is direction, 0U - USB_OUT, 1U - USB_IN.
* @param buffer The memory address to be transferred, or the memory address to hold the data need to be
* sent.
* @param length The length of the data.
*
* @retval kStatus_USB_Success Get a device handle successfully.
* @retval kStatus_USB_InvalidHandle The device handle is invalid.
* @retval kStatus_USB_ControllerNotFound The controller interface is not found.
* @retval kStatus_USB_Error The device is doing reset.
*/
static usb_status_t USB_DeviceTransfer(usb_device_handle handle,
uint8_t endpointAddress,
uint8_t *buffer,
uint32_t length)
{
usb_device_struct_t *deviceHandle = (usb_device_struct_t *)handle;
usb_status_t status;
uint8_t endpoint = endpointAddress & USB_ENDPOINT_NUMBER_MASK;
uint8_t direction = (endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) >>
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT;
OSA_SR_ALLOC();
if (NULL == deviceHandle)
{
return kStatus_USB_InvalidHandle;
}
if (NULL != deviceHandle->controllerInterface)
{
if (0U != deviceHandle->epCallback[(uint8_t)((uint32_t)endpoint << 1U) | direction].isBusy)
{
return kStatus_USB_Busy;
}
OSA_ENTER_CRITICAL();
deviceHandle->epCallback[(uint8_t)((uint32_t)endpoint << 1U) | direction].isBusy = 1U;
OSA_EXIT_CRITICAL();
if (0U != (endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK))
{
#if (defined(USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE) && (USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE > 0U))
if (0U != length)
{
DCACHE_CleanByRange((uint32_t)buffer, length);
}
#endif
/* Call the controller send interface, the callbackFn is initialized in
USB_DeviceGetControllerInterface */
status = deviceHandle->controllerInterface->deviceSend(deviceHandle->controllerHandle, endpointAddress,
buffer, length);
}
else
{
#if (defined(USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE) && (USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE > 0U))
if (length)
{
DCACHE_CleanInvalidateByRange((uint32_t)buffer, length);
}
#endif
/* Call the controller receive interface, the callbackFn is initialized in
USB_DeviceGetControllerInterface */
status = deviceHandle->controllerInterface->deviceRecv(deviceHandle->controllerHandle, endpointAddress,
buffer, length);
}
if (kStatus_USB_Success != status)
{
OSA_ENTER_CRITICAL();
deviceHandle->epCallback[(uint8_t)((uint32_t)endpoint << 1U) | direction].isBusy = 0U;
OSA_EXIT_CRITICAL();
}
}
else
{
status = kStatus_USB_ControllerNotFound;
}
return status;
}
/*!
* @brief Control the status of the selected item.
*
* This function is used to control the status of the selected item..
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param type The control type, please refer to the enumeration usb_device_control_type_t.
* @param param The param type is determined by the selected item.
*
* @retval kStatus_USB_Success Get a device handle successfully.
* @retval kStatus_USB_InvalidHandle The device handle is invalid.
* @retval kStatus_USB_ControllerNotFound The controller interface is not found.
* @retval kStatus_USB_Error Unsupported type.
* Or, the param is NULL pointer.
*/
static usb_status_t USB_DeviceControl(usb_device_handle handle, usb_device_control_type_t type, void *param)
{
usb_device_struct_t *deviceHandle = (usb_device_struct_t *)handle;
usb_status_t status;
if (NULL == deviceHandle)
{
return kStatus_USB_InvalidHandle;
}
if (NULL != deviceHandle->controllerInterface)
{
/* Call the controller control interface. the controllerInterface is initialized in
USB_DeviceGetControllerInterface */
status = deviceHandle->controllerInterface->deviceControl(deviceHandle->controllerHandle, type, param);
}
else
{
status = kStatus_USB_ControllerNotFound;
}
return status;
}
/*!
* @brief Handle the reset notification.
*
* This function is used to handle the reset notification.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param message The device callback message handle.
*
* @retval kStatus_USB_Success Get a device handle successfully.
*/
static usb_status_t USB_DeviceResetNotification(usb_device_struct_t *handle,
usb_device_callback_message_struct_t *message)
{
uint32_t count;
#if (defined(USB_DEVICE_CONFIG_USE_TASK) && (USB_DEVICE_CONFIG_USE_TASK > 0U))
OSA_SR_ALLOC();
#endif
handle->isResetting = 1U;
#if ((defined(USB_DEVICE_CONFIG_REMOTE_WAKEUP)) && (USB_DEVICE_CONFIG_REMOTE_WAKEUP > 0U))
/* Clear remote wakeup feature */
handle->remotewakeup = 0U;
#endif
#if (defined(USB_DEVICE_CONFIG_USE_TASK) && (USB_DEVICE_CONFIG_USE_TASK > 0U))
OSA_ENTER_CRITICAL();
handle->epCallbackDirectly = 1U;
OSA_EXIT_CRITICAL();
#endif
/* Set the controller to default status. */
(void)USB_DeviceControl(handle, kUSB_DeviceControlSetDefaultStatus, NULL);
#if (defined(USB_DEVICE_CONFIG_USE_TASK) && (USB_DEVICE_CONFIG_USE_TASK > 0U))
OSA_ENTER_CRITICAL();
handle->epCallbackDirectly = 0U;
OSA_EXIT_CRITICAL();
#endif
handle->state = (uint8_t)kUSB_DeviceStateDefault;
handle->deviceAddress = 0U;
for (count = 0U; count < (USB_DEVICE_CONFIG_ENDPOINTS * 2U); count++)
{
handle->epCallback[count].callbackFn = (usb_device_endpoint_callback_t)NULL;
handle->epCallback[count].callbackParam = NULL;
handle->epCallback[count].isBusy = 0U;
}
/* Call device callback to notify the application that the USB bus reset signal detected.
the deviceCallback is the second parameter of USB_DeviceInit */
(void)handle->deviceCallback(handle, kUSB_DeviceEventBusReset, NULL);
handle->isResetting = 0U;
return kStatus_USB_Success;
}
#if (defined(USB_DEVICE_CONFIG_LOW_POWER_MODE) && (USB_DEVICE_CONFIG_LOW_POWER_MODE > 0U))
/*!
* @brief Handle the suspend notification.
*
* This function is used to handle the suspend notification.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param message The device callback message handle.
*
* @return A USB error code or kStatus_USB_Success.
*/
static usb_status_t USB_DeviceSuspendNotification(usb_device_struct_t *handle,
usb_device_callback_message_struct_t *message)
{
/* Call device callback to notify the application that the USB bus suspend signal detected.
the deviceCallback is the second parameter of USB_DeviceInit */
return handle->deviceCallback(handle, kUSB_DeviceEventSuspend, NULL);
}
/*!
* @brief Handle the resume notification.
*
* This function is used to handle the resume notification.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param message The device callback message handle.
*
* @return A USB error code or kStatus_USB_Success.
*/
static usb_status_t USB_DeviceResumeNotification(usb_device_struct_t *handle,
usb_device_callback_message_struct_t *message)
{
/* Call device callback to notify the application that the USB bus resume signal detected.
the deviceCallback is the second parameter of USB_DeviceInit */
return handle->deviceCallback(handle, kUSB_DeviceEventResume, NULL);
}
#if (defined(USB_DEVICE_CONFIG_LPM_L1) && (USB_DEVICE_CONFIG_LPM_L1 > 0U))
/*!
* @brief Handle the suspend notification.
*
* This function is used to handle the suspend notification.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param message The device callback message handle.
*
* @return A USB error code or kStatus_USB_Success.
*/
static usb_status_t USB_DeviceSleepNotification(usb_device_struct_t *handle,
usb_device_callback_message_struct_t *message)
{
/* Call device callback to notify the application that the USB bus suspend signal detected.
the deviceCallback is the second parameter of USB_DeviceInit */
(void)USB_DeviceSetStatus(handle, kUSB_DeviceStatusRemoteWakeup, message->buffer);
return handle->deviceCallback(handle, kUSB_DeviceEventSleeped, NULL);
}
#endif
#endif /* USB_DEVICE_CONFIG_LOW_POWER_MODE */
#if (defined(USB_DEVICE_CONFIG_ERROR_HANDLING) && (USB_DEVICE_CONFIG_ERROR_HANDLING > 0U))
static usb_status_t USB_DeviceErrorNotification(usb_device_struct_t *handle, usb_device_callback_message_struct_t *message)
{
/* Call device callback to notify the application that the USB bus error signal detected.
the deviceCallback is the second parameter of USB_DeviceInit */
return handle->deviceCallback(handle, kUSB_DeviceEventError, NULL);
}
#endif /* USB_DEVICE_CONFIG_ERROR_HANDLING */
#if (defined(USB_DEVICE_CONFIG_DETACH_ENABLE) && (USB_DEVICE_CONFIG_DETACH_ENABLE > 0U))
/*!
* @brief Handle the detach notification.
*
* This function is used to handle the detach notification.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param message The device callback message handle.
*
* @return A USB error code or kStatus_USB_Success.
*/
static usb_status_t USB_DeviceDetachNotification(usb_device_struct_t *handle,
usb_device_callback_message_struct_t *message)
{
/* Call device callback to notify the application that the device is disconnected from a host.
the deviceCallback is the second parameter of USB_DeviceInit */
return handle->deviceCallback(handle, kUSB_DeviceEventDetach, NULL);
}
/*!
* @brief Handle the attach notification.
*
* This function is used to handle the attach notification.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param message The device callback message handle.
*
* @return A USB error code or kStatus_USB_Success.
*/
static usb_status_t USB_DeviceAttachNotification(usb_device_struct_t *handle,
usb_device_callback_message_struct_t *message)
{
/* Call device callback to notify the application that the device is connected to a host.
the deviceCallback is the second parameter of USB_DeviceInit */
return handle->deviceCallback(handle, kUSB_DeviceEventAttach, NULL);
}
#endif
#if (defined(USB_DEVICE_CONFIG_CHARGER_DETECT) && (USB_DEVICE_CONFIG_CHARGER_DETECT > 0U))
/*!
* @brief Handle the DCP detection finished notification.
*
* This function is used to notify detection notification.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param message The device callback message handle.
*
* @return A USB error code or kStatus_USB_Success.
*/
static usb_status_t USB_DeviceDcdDetectFinihsedNotification(usb_device_struct_t *handle,
usb_device_callback_message_struct_t *message)
{
/* Call device callback to notify the application that the DCP facility is detected.
the deviceCallback is the second parameter of USB_DeviceInit */
return handle->deviceCallback(handle, kUSB_DeviceEventDcdDetectionfinished, message->buffer);
}
#endif
/*!
* @brief Handle the attach notification.
*
* This function is used to handle the attach notification.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param message The device callback message handle.
*
* @return A USB error code or kStatus_USB_Success.
*/
static usb_status_t USB_DeviceNotification(usb_device_struct_t *handle, usb_device_callback_message_struct_t *message)
{
uint8_t endpoint = message->code & USB_ENDPOINT_NUMBER_MASK;
uint8_t direction = (message->code & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) >>
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT;
usb_status_t status = kStatus_USB_Error;
usb_device_notification_t deviceNotify = (usb_device_notification_t)message->code;
switch (deviceNotify)
{
case kUSB_DeviceNotifyBusReset:
status = USB_DeviceResetNotification(handle, message);
break;
#if (defined(USB_DEVICE_CONFIG_LOW_POWER_MODE) && (USB_DEVICE_CONFIG_LOW_POWER_MODE > 0U))
case kUSB_DeviceNotifySuspend:
status = USB_DeviceSuspendNotification(handle, message);
break;
case kUSB_DeviceNotifyResume:
status = USB_DeviceResumeNotification(handle, message);
break;
#if (defined(USB_DEVICE_CONFIG_LPM_L1) && (USB_DEVICE_CONFIG_LPM_L1 > 0U))
case kUSB_DeviceNotifyLPMSleep:
status = USB_DeviceSleepNotification(handle, message);
break;
#endif
#endif
#if (defined(USB_DEVICE_CONFIG_ERROR_HANDLING) && (USB_DEVICE_CONFIG_ERROR_HANDLING > 0U))
case kUSB_DeviceNotifyError:
status = USB_DeviceErrorNotification(handle, message);
break;
#endif
#if USB_DEVICE_CONFIG_DETACH_ENABLE
case kUSB_DeviceNotifyDetach:
status = USB_DeviceDetachNotification(handle, message);
break;
case kUSB_DeviceNotifyAttach:
status = USB_DeviceAttachNotification(handle, message);
break;
#endif
#if (defined(USB_DEVICE_CONFIG_CHARGER_DETECT) && (USB_DEVICE_CONFIG_CHARGER_DETECT > 0U))
case kUSB_DeviceNotifyDcdDetectFinished:
status = USB_DeviceDcdDetectFinihsedNotification(handle, message);
break;
#endif
default:
if (endpoint < USB_DEVICE_CONFIG_ENDPOINTS)
{
if (NULL != handle->epCallback[(uint8_t)((uint32_t)endpoint << 1U) | direction].callbackFn)
{
usb_device_endpoint_callback_message_struct_t endpointCallbackMessage;
endpointCallbackMessage.buffer = message->buffer;
endpointCallbackMessage.length = message->length;
endpointCallbackMessage.isSetup = message->isSetup;
if (0U != message->isSetup)
{
handle->epCallback[0].isBusy = 0U;
handle->epCallback[1].isBusy = 0U;
}
else
{
handle->epCallback[(uint8_t)((uint32_t)endpoint << 1U) | direction].isBusy = 0U;
}
/* Call endpoint callback, callbackFn is in the third parameter of USB_DeviceInitEndpoint */
status = handle->epCallback[(uint8_t)((uint32_t)endpoint << 1U) | direction].callbackFn(
handle, &endpointCallbackMessage,
handle->epCallback[(uint8_t)((uint32_t)endpoint << 1U) | direction].callbackParam);
}
}
break;
}
return status;
}
/*!
* @brief Notify the device that the controller status changed.
*
* This function is used to notify the device that the controller status changed.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param message The device callback message handle.
*
* @return A USB error code or kStatus_USB_Success.
*/
usb_status_t USB_DeviceNotificationTrigger(void *handle, void *msg)
{
usb_device_struct_t *deviceHandle = (usb_device_struct_t *)handle;
usb_device_callback_message_struct_t *message = (usb_device_callback_message_struct_t *)msg;
if ((NULL == msg) || (NULL == handle))
{
return kStatus_USB_InvalidHandle;
}
/* The device callback is invalid or not. */
if (NULL == deviceHandle->deviceCallback)
{
return kStatus_USB_Error;
}
#if (defined(USB_DEVICE_CONFIG_USE_TASK) && (USB_DEVICE_CONFIG_USE_TASK > 0U))
if (0U != deviceHandle->epCallbackDirectly)
{
if ((0U != (message->code & USB_ENDPOINT_NUMBER_MASK)) && (0U == (message->code & 0x70U)))
{
return USB_DeviceNotification(deviceHandle, message);
}
}
/* Add the message to message queue when the device task is enabled. */
if (KOSA_StatusSuccess != OSA_MsgQPut(deviceHandle->notificationQueue, (osa_msg_handle_t)message))
{
return kStatus_USB_Busy;
}
return kStatus_USB_Success;
#else
/* Handle the notification by calling USB_DeviceNotification. */
return USB_DeviceNotification(deviceHandle, message);
#endif
}
/*!
* @brief Initialize the USB device stack.
*
* This function initializes the USB device module specified by the controllerId.
*
* @param controllerId The controller id of the USB IP. Please refer to the enumeration usb_controller_index_t.
* @param deviceCallback Function pointer of the device callback.
* @param handle It is out parameter, is used to return pointer of the device handle to the caller.
*
* @retval kStatus_USB_Success The device is initialized successfully.
* @retval kStatus_USB_InvalidHandle The handle is a NULL pointer.
* @retval kStatus_USB_Busy Cannot allocate a device handle.
* @retval kStatus_USB_ControllerNotFound Cannot find the controller according to the controller id.
* @retval kStatus_USB_InvalidControllerInterface The controller driver interfaces is invaild, There is an empty
* interface entity.
* @retval kStatus_USB_Error The macro USB_DEVICE_CONFIG_ENDPOINTS is more than IP's endpoint number.
* Or, the device has been initialized.
* Or, the message queue is created failed.
*/
usb_status_t USB_DeviceInit(uint8_t controllerId, usb_device_callback_t deviceCallback, usb_device_handle *handle)
{
usb_device_struct_t *deviceHandle = NULL;
usb_status_t error;
uint32_t count;
if (NULL == handle)
{
return kStatus_USB_InvalidHandle;
}
/* Allocate a device handle by using the controller id. */
error = USB_DeviceAllocateHandle(controllerId, &deviceHandle);
if (kStatus_USB_Success != error)
{
return error;
}
/* Save the device callback */
deviceHandle->deviceCallback = deviceCallback;
/* Save the controller id */
deviceHandle->controllerId = controllerId;
/* Clear the device address */
deviceHandle->deviceAddress = 0U;
/* Clear the device reset state */
deviceHandle->isResetting = 0U;
/* Initialize the endpoints */
for (count = 0U; count < (USB_DEVICE_CONFIG_ENDPOINTS * 2U); count++)
{
deviceHandle->epCallback[count].callbackFn = (usb_device_endpoint_callback_t)NULL;
deviceHandle->epCallback[count].callbackParam = NULL;
deviceHandle->epCallback[count].isBusy = 0U;
}
/* Get the controller interface according to the controller id */
error = USB_DeviceGetControllerInterface(controllerId, &deviceHandle->controllerInterface);
if (kStatus_USB_Success != error)
{
(void)USB_DeviceFreeHandle(deviceHandle);
return error;
}
if (NULL == deviceHandle->controllerInterface)
{
(void)USB_DeviceFreeHandle(deviceHandle);
return kStatus_USB_ControllerNotFound;
}
if (((usb_device_controller_init_t)NULL == deviceHandle->controllerInterface->deviceInit) ||
((usb_device_controller_deinit_t)NULL == deviceHandle->controllerInterface->deviceDeinit) ||
((usb_device_controller_send_t)NULL == deviceHandle->controllerInterface->deviceSend) ||
((usb_device_controller_recv_t)NULL == deviceHandle->controllerInterface->deviceRecv) ||
((usb_device_controller_cancel_t)NULL == deviceHandle->controllerInterface->deviceCancel) ||
((usb_device_controller_control_t)NULL == deviceHandle->controllerInterface->deviceControl))
{
(void)USB_DeviceFreeHandle(deviceHandle);
return kStatus_USB_InvalidControllerInterface;
}
#if USB_DEVICE_CONFIG_USE_TASK
/* Create a message queue when the device handle is enabled. */
deviceHandle->notificationQueue = (osa_msgq_handle_t)&deviceHandle->notificationQueueBuffer[0];
if (KOSA_StatusSuccess !=
OSA_MsgQCreate(deviceHandle->notificationQueue, USB_DEVICE_CONFIG_MAX_MESSAGES, USB_DEVICE_MESSAGES_SIZE))
{
(void)USB_DeviceDeinit(deviceHandle);
return kStatus_USB_Error;
}
#endif
*handle = deviceHandle;
/* Initialize the controller, the callbackFn is initialized in USB_DeviceGetControllerInterface */
error = deviceHandle->controllerInterface->deviceInit(controllerId, deviceHandle, &deviceHandle->controllerHandle);
if (kStatus_USB_Success != error)
{
(void)USB_DeviceDeinit(deviceHandle);
*handle = NULL;
return error;
}
/* Set the device to deafult state */
deviceHandle->state = (uint8_t)kUSB_DeviceStateDefault;
return error;
}
/*!
* @brief Enable the device functionality.
*
* The function enables the device functionality, so that the device can be recognized by the host when the device
* detects that it has been connected to a host.
*
* @param handle The device handle got from USB_DeviceInit.
*
* @retval kStatus_USB_Success The device is run successfully.
* @retval kStatus_USB_ControllerNotFound Cannot find the controller.
* @retval kStatus_USB_InvalidHandle The device handle is a NULL pointer. Or the controller handle is invalid.
*
*/
usb_status_t USB_DeviceRun(usb_device_handle handle)
{
return USB_DeviceControl(handle, kUSB_DeviceControlRun, NULL);
}
/*!
* @brief Disable the device functionality.
*
* The function disables the device functionality, after this function called, even the device is detached to the host,
* and the device can't work.
*
* @param handle The device handle got from USB_DeviceInit.
*
* @retval kStatus_USB_Success The device is stopped successfully.
* @retval kStatus_USB_ControllerNotFound Cannot find the controller.
* @retval kStatus_USB_InvalidHandle The device handle is a NULL pointer. Or the controller handle is invalid.
*/
usb_status_t USB_DeviceStop(usb_device_handle handle)
{
return USB_DeviceControl(handle, kUSB_DeviceControlStop, NULL);
}
/*!
* @brief De-initialize the device controller.
*
* The function de-initializes the device controller specified by the handle.
*
* @param handle The device handle got from USB_DeviceInit.
*
* @retval kStatus_USB_Success The device is stopped successfully.
* @retval kStatus_USB_InvalidHandle The device handle is a NULL pointer. Or the controller handle is invalid.
*/
usb_status_t USB_DeviceDeinit(usb_device_handle handle)
{
usb_device_struct_t *deviceHandle = (usb_device_struct_t *)handle;
if (NULL == deviceHandle)
{
return kStatus_USB_InvalidHandle;
}
/* De-initialize the controller */
if (NULL != deviceHandle->controllerInterface)
{
/* the callbackFn is initialized in USB_DeviceGetControllerInterface */
(void)deviceHandle->controllerInterface->deviceDeinit(deviceHandle->controllerHandle);
deviceHandle->controllerInterface = (usb_device_controller_interface_struct_t *)NULL;
}
#if USB_DEVICE_CONFIG_USE_TASK
/* Destroy the message queue. */
if (NULL != deviceHandle->notificationQueue)
{
(void)OSA_MsgQDestroy(deviceHandle->notificationQueue);
deviceHandle->notificationQueue = NULL;
}
#endif
/* Free the device handle. */
(void)USB_DeviceFreeHandle(deviceHandle);
return kStatus_USB_Success;
}
/*!
* @brief Send data through a specified endpoint.
*
* The function is used to send data through a specified endpoint.
*
* @param handle The device handle got from USB_DeviceInit.
* @param endpointAddress Endpoint index.
* @param buffer The memory address to hold the data need to be sent.
* @param length The data length need to be sent.
*
* @retval kStatus_USB_Success The send request is sent successfully.
* @retval kStatus_USB_InvalidHandle The handle is a NULL pointer. Or the controller handle is invalid.
* @retval kStatus_USB_Busy Cannot allocate dtds for current transfer in EHCI driver.
* @retval kStatus_USB_ControllerNotFound Cannot find the controller.
* @retval kStatus_USB_Error The device is doing reset.
*
* @note The return value just means if the sending request is successful or not; the transfer done is notified by the
* corresponding callback function.
* Currently, only one transfer request can be supported for one specific endpoint.
* If there is a specific requirement to support multiple transfer requests for one specific endpoint, the application
* should implement a queue in the application level.
* The subsequent transfer could begin only when the previous transfer is done (get notification through the endpoint
* callback).
*/
usb_status_t USB_DeviceSendRequest(usb_device_handle handle, uint8_t endpointAddress, uint8_t *buffer, uint32_t length)
{
return USB_DeviceTransfer(
handle,
(endpointAddress & USB_ENDPOINT_NUMBER_MASK) | (USB_IN << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT),
buffer, length);
}
/*!
* @brief Receive data through a specified endpoint.
*
* The function is used to receive data through a specified endpoint.
*
* @param handle The device handle got from USB_DeviceInit.
* @param endpointAddress Endpoint index.
* @param buffer The memory address to save the received data.
* @param length The data length want to be received.
*
* @retval kStatus_USB_Success The receive request is sent successfully.
* @retval kStatus_USB_InvalidHandle The handle is a NULL pointer. Or the controller handle is invalid.
* @retval kStatus_USB_Busy Cannot allocate dtds for current transfer in EHCI driver.
* @retval kStatus_USB_ControllerNotFound Cannot find the controller.
* @retval kStatus_USB_Error The device is doing reset.
*
* @note The return value just means if the receiving request is successful or not; the transfer done is notified by the
* corresponding callback function.
* Currently, only one transfer request can be supported for one specific endpoint.
* If there is a specific requirement to support multiple transfer requests for one specific endpoint, the application
* should implement a queue in the application level.
* The subsequent transfer could begin only when the previous transfer is done (get notification through the endpoint
* callback).
*/
usb_status_t USB_DeviceRecvRequest(usb_device_handle handle, uint8_t endpointAddress, uint8_t *buffer, uint32_t length)
{
return USB_DeviceTransfer(
handle,
(endpointAddress & USB_ENDPOINT_NUMBER_MASK) | (USB_OUT << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT),
buffer, length);
}
/*!
* @brief Cancel the pending transfer in a specified endpoint.
*
* The function is used to cancel the pending transfer in a specified endpoint.
*
* @param handle The device handle got from USB_DeviceInit.
* @param endpointAddress Endpoint address, bit7 is the direction of endpoint, 1U - IN, abd 0U - OUT.
*
* @retval kStatus_USB_Success The transfer is cancelled.
* @retval kStatus_USB_InvalidHandle The handle is a NULL pointer. Or the controller handle is invalid.
* @retval kStatus_USB_ControllerNotFound Cannot find the controller.
*/
usb_status_t USB_DeviceCancel(usb_device_handle handle, uint8_t endpointAddress)
{
usb_device_struct_t *deviceHandle = (usb_device_struct_t *)handle;
usb_status_t status;
if (NULL == deviceHandle)
{
return kStatus_USB_InvalidHandle;
}
if (NULL != deviceHandle->controllerInterface)
{
/* the callbackFn is initialized in USB_DeviceGetControllerInterface */
status = deviceHandle->controllerInterface->deviceCancel(deviceHandle->controllerHandle, endpointAddress);
}
else
{
status = kStatus_USB_ControllerNotFound;
}
return status;
}
/*!
* @brief Initialize a specified endpoint.
*
* The function is used to initialize a specified endpoint and the corresponding endpoint callback is also initialized.
*
* @param handle The device handle got from USB_DeviceInit.
* @param epInit Endpoint initialization structure. Please refer to the structure usb_device_endpoint_init_struct_t.
* @param epCallback Endpoint callback structure. Please refer to the structure
* usb_device_endpoint_callback_struct_t.
*
* @retval kStatus_USB_Success The endpoint is initialized successfully.
* @retval kStatus_USB_InvalidHandle The handle is a NULL pointer. Or the controller handle is invalid.
* @retval kStatus_USB_InvalidParameter The epInit or epCallback is NULL pointer. Or the endpoint number is
* not less than USB_DEVICE_CONFIG_ENDPOINTS.
* @retval kStatus_USB_Busy The endpoint is busy in EHCI driver.
* @retval kStatus_USB_ControllerNotFound Cannot find the controller.
*/
usb_status_t USB_DeviceInitEndpoint(usb_device_handle handle,
usb_device_endpoint_init_struct_t *epInit,
usb_device_endpoint_callback_struct_t *epCallback)
{
usb_device_struct_t *deviceHandle = (usb_device_struct_t *)handle;
uint8_t endpoint;
uint8_t direction;
if (NULL == deviceHandle)
{
return kStatus_USB_InvalidHandle;
}
if ((NULL == epInit) || (NULL == epCallback))
{
return kStatus_USB_InvalidParameter;
}
endpoint = epInit->endpointAddress & USB_ENDPOINT_NUMBER_MASK;
direction = (epInit->endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) >>
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT;
if (endpoint < USB_DEVICE_CONFIG_ENDPOINTS)