summaryrefslogtreecommitdiff
path: root/tool/mbed/mbed-sdk/libraries/USBHost/USBHost/TARGET_RENESAS/TARGET_RZ_A1H/ohci_wrapp_RZ_A1.c
blob: 0b1b7da656a5828c8d14af98bc354324421046b2 (plain)
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
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
/* Copyright (c) 2010-2011 mbed.org, MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <string.h>
#include "cmsis.h"
#include "cmsis_os.h"
#include "ohci_wrapp_RZ_A1.h"
#include "ohci_wrapp_RZ_A1_local.h"
#include "rza_io_regrw.h"
#include "usb_host_setting.h"

/* ------------------ HcControl Register --------------------- */
#define OR_CONTROL_PLE                  (0x00000004)
#define OR_CONTROL_IE                   (0x00000008)
#define OR_CONTROL_CLE                  (0x00000010)
#define OR_CONTROL_BLE                  (0x00000020)
/* ----------------- HcCommandStatus Register ----------------- */
#define OR_CMD_STATUS_HCR               (0x00000001)
#define OR_CMD_STATUS_CLF               (0x00000002)
#define OR_CMD_STATUS_BLF               (0x00000004)
#define OR_CMD_STATUS_OCR               (0x00000008)
/* --------------- HcInterruptStatus Register ----------------- */
#define OR_INTR_STATUS_WDH              (0x00000002)
#define OR_INTR_STATUS_RHSC             (0x00000040)
/* --------------- HcInterruptEnable Register ----------------- */
#define OR_INTR_ENABLE_WDH              (0x00000002)
#define OR_INTR_ENABLE_RHSC             (0x00000040)
/* -------------- HcRhPortStatus[1:NDP] Register -------------- */
#define OR_RH_PORT_CSC                  (0x00010000)
#define OR_RH_PORT_LSDA                 (0x00000200)
#define OR_RH_PORT_PRS                  (0x00000010)
#define OR_RH_PORT_POCI                 (0x00000008)
#define OR_RH_PORT_CCS                  (0x00000001)

#define ED_FORMAT                       (0x00008000)   /* Format */
#define ED_SKIP                         (0x00004000)   /* Skip this ep in queue */
#define ED_TOGLE_CARRY                  (0x00000002)
#define ED_HALTED                       (0x00000001)

#define TD_SETUP                        (0x00000000)   /* Direction of Setup Packet */
#define TD_OUT                          (0x00080000)   /* Direction Out */
#define TD_TOGGLE_0                     (0x02000000)   /* Toggle 0 */
#define TD_TOGGLE_1                     (0x03000000)   /* Toggle 1 */

/* -------------- USB Standard Requests  -------------- */
#define GET_STATUS                      (0x00)
#define SET_FEATURE                     (0x03)
#define SET_ADDRESS                     (0x05)

#define TD_CTL_MSK_DP                   (0x00180000)
#define TD_CTL_MSK_T                    (0x03000000)
#define TD_CTL_MSK_CC                   (0xF0000000)
#define TD_CTL_MSK_EC                   (0x0C000000)
#define TD_CTL_SHFT_CC                  (28)
#define TD_CTL_SHFT_EC                  (26)
#define TD_CTL_SHFT_T                   (24)
#define ED_SHFT_TOGLE_CARRY             (1)
#define SIG_GEN_LIST_REQ                (1)
#if (ISO_TRANS_MAX_NUM > 0)
#define TD_PSW_MSK_CC                   (0xF000)
#define TD_PSW_SHFT_CC                  (12)
#define TD_CTL_MSK_FC                   (0x07000000)
#define TD_CTL_SHFT_FC                  (24)
#endif

#define CTL_TRANS_TIMEOUT               (1000)
#define BLK_TRANS_TIMEOUT               (5)
#define TOTAL_SEM_NUM                   (5 + (2 * INT_TRANS_MAX_NUM) + (2 * ISO_TRANS_MAX_NUM))

#define PORT_LOW_SPEED                  (0x00000200)
#define PORT_HIGH_SPEED                 (0x00000400)
#define PORT_NUM                        (16 + 1) /* num + root(1) */

typedef struct tag_hctd {
    uint32_t         control;        /* Transfer descriptor control */
    uint8_t          *currBufPtr;    /* Physical address of current buffer pointer */
    struct tag_hctd  *nextTD;        /* Physical pointer to next Transfer Descriptor */
    uint8_t          *bufEnd;        /* Physical address of end of buffer */
} hctd_t;

#if (ISO_TRANS_MAX_NUM > 0)
#define PSW_NUM                         (8)
typedef struct tag_hcisotd {
    uint32_t           control;      /* Transfer descriptor control */
    uint8_t            *bufferPage0; /* Buffer Page 0 */
    struct tag_hcisotd *nextTD;      /* Physical pointer to next Transfer Descriptor */
    uint8_t            *bufEnd;      /* Physical address of end of buffer */
    uint16_t           offsetPSW[PSW_NUM]; /* Offset/PSW */
} hcisotd_t;
#endif

typedef struct tag_hced {
    uint32_t         control;        /* Endpoint descriptor control */
    uint32_t         tailTD;         /* Physical address of tail in Transfer descriptor list */
    uint32_t         headTD;         /* Physcial address of head in Transfer descriptor list */
    struct tag_hced  *nextED;        /* Physical address of next Endpoint descriptor */
} hced_t;

typedef struct tag_hcca {
    uint32_t         IntTable[32];   /* Interrupt Table */
    uint32_t         FrameNumber;    /* Frame Number */
    uint32_t         DoneHead;       /* Done Head */
    volatile uint8_t Reserved[116];  /* Reserved for future use */
    volatile uint8_t Unknown[4];     /* Unused */
} hcca_t;

typedef struct tag_usb_ohci_reg {
    volatile uint32_t HcRevision;
    volatile uint32_t HcControl;
    volatile uint32_t HcCommandStatus;
    volatile uint32_t HcInterruptStatus;
    volatile uint32_t HcInterruptEnable;
    volatile uint32_t HcInterruptDisable;
    volatile uint32_t HcHCCA;
    volatile uint32_t HcPeriodCurrentED;
    volatile uint32_t HcControlHeadED;
    volatile uint32_t HcControlCurrentED;
    volatile uint32_t HcBulkHeadED;
    volatile uint32_t HcBulkCurrentED;
    volatile uint32_t HcDoneHead;
    volatile uint32_t HcFmInterval;
    volatile uint32_t HcFmRemaining;
    volatile uint32_t HcFmNumber;
    volatile uint32_t HcPeriodicStart;
    volatile uint32_t HcLSThreshold;
    volatile uint32_t HcRhDescriptorA;
    volatile uint32_t HcRhDescriptorB;
    volatile uint32_t HcRhStatus;
    volatile uint32_t HcRhPortStatus1;
} usb_ohci_reg_t;

typedef struct tag_genelal_ed {
    osThreadId      tskid;
    osSemaphoreId   semid_wait;
    osSemaphoreId   semid_list;
    void            *p_curr_td;     /* pointer of hctd_t or hcisotd_t */
    hced_t          *p_curr_ed;
    uint32_t        pipe_no;
    uint32_t        trans_wait;
    uint32_t        cycle_time;
    uint8_t         *p_start_buf;
#if (ISO_TRANS_MAX_NUM > 0)
    uint32_t        psw_idx;
#endif
} genelal_ed_t;

typedef struct tag_tdinfo {
    uint32_t         count;
    uint32_t         direction;
    uint32_t         msp;
    uint16_t         devadr;
    uint16_t         speed;         /* 1:Speed = Low */
    uint8_t          endpoint_no;
} tdinfo_t;

typedef struct tag_split_trans {
    uint16_t        root_devadr;
    uint16_t        get_port;
    uint16_t        port_speed;
    uint16_t        reset_port;
    uint32_t        seq_cnt;
    uint32_t        port_sts_bits[PORT_NUM];
} split_trans_t;

static void callback_task(void const * argument);
static void control_ed_task(void const * argument);
static void bulk_ed_task(void const * argument);
static void int_ed_task(void const * argument);
static int32_t int_trans_doing(hced_t *p_ed, uint32_t index);
static int32_t chk_genelal_ed(genelal_ed_t *p_g_ed);
static void chk_genelal_td_done(genelal_ed_t *p_g_ed);
static void chk_split_trans_setting(genelal_ed_t *p_g_ed);
static void set_split_trans_setting(void);
static void control_trans(genelal_ed_t *p_g_ed);
static void bulk_trans(genelal_ed_t *p_g_ed);
static void int_trans_setting(genelal_ed_t *p_g_ed, uint32_t index);
static uint32_t chk_cycle(hced_t *p_ed);
static void int_trans(genelal_ed_t *p_g_ed);
static void get_td_info(genelal_ed_t *p_g_ed, tdinfo_t *p_td_info);
static void set_togle(uint32_t pipe, hctd_t *p_td, hced_t *p_ed);
#if (ISO_TRANS_MAX_NUM > 0)
static void iso_ed_task(void const * argument);
static int32_t iso_trans_doing(hced_t *p_ed, uint32_t index);
static void chk_iso_td_done(genelal_ed_t *p_g_ed);
static int32_t chk_iso_ed(genelal_ed_t *p_g_ed);
static void iso_trans_setting(genelal_ed_t *p_g_ed, uint32_t index);
static uint32_t iso_chk_starting_frame(genelal_ed_t *p_g_ed);
static void iso_trans(genelal_ed_t *p_g_ed);
#endif
static void connect_check(void);

extern USB_HOST_CFG_PIPETBL_t  usb_host_blk_ep_tbl1[];
extern USB_HOST_CFG_PIPETBL_t  usb_host_int_ep_tbl1[];
#if (ISO_TRANS_MAX_NUM > 0)
extern USB_HOST_CFG_PIPETBL_t  usb_host_iso_ep_tbl1[];
#endif

static usb_ohci_reg_t usb_reg;
static usb_ohci_reg_t *p_usb_reg     = &usb_reg;
static usbisr_fnc_t   *p_usbisr_cb   = NULL;
static osSemaphoreId  semid_cb       = NULL;
static uint32_t       connect_change = 0xFFFFFFFF;
static uint32_t       init_end       = 0;
static genelal_ed_t   ctl_ed;
static genelal_ed_t   blk_ed;
static genelal_ed_t   int_ed[INT_TRANS_MAX_NUM];
static split_trans_t  split_ctl;

#if (ISO_TRANS_MAX_NUM > 0)
static genelal_ed_t   iso_ed[ISO_TRANS_MAX_NUM];
#endif

osSemaphoreDef(ohciwrapp_sem_01);
osSemaphoreDef(ohciwrapp_sem_02);
osSemaphoreDef(ohciwrapp_sem_03);
osSemaphoreDef(ohciwrapp_sem_04);
osSemaphoreDef(ohciwrapp_sem_05);
osSemaphoreDef(ohciwrapp_sem_06);
osSemaphoreDef(ohciwrapp_sem_07);
#if (INT_TRANS_MAX_NUM >= 2)
osSemaphoreDef(ohciwrapp_sem_08);
osSemaphoreDef(ohciwrapp_sem_09);
#endif
#if (INT_TRANS_MAX_NUM >= 3)
osSemaphoreDef(ohciwrapp_sem_10);
osSemaphoreDef(ohciwrapp_sem_11);
#endif
#if (INT_TRANS_MAX_NUM >= 4)
osSemaphoreDef(ohciwrapp_sem_12);
osSemaphoreDef(ohciwrapp_sem_13);
#endif
#if (ISO_TRANS_MAX_NUM >= 1)
osSemaphoreDef(ohciwrapp_sem_14);
osSemaphoreDef(ohciwrapp_sem_15);
#endif
#if (ISO_TRANS_MAX_NUM >= 2)
osSemaphoreDef(ohciwrapp_sem_16);
osSemaphoreDef(ohciwrapp_sem_17);
#endif

osThreadDef(callback_task,   osPriorityHigh,        512);
osThreadDef(control_ed_task, osPriorityNormal,      512);
osThreadDef(bulk_ed_task,    osPriorityNormal,      512);
static void int_ed_task_1(void const * argument) {
    int_ed_task(argument);
}
osThreadDef(int_ed_task_1,   osPriorityNormal,      512);
#if (INT_TRANS_MAX_NUM >= 2)
static void int_ed_task_2(void const * argument) {
    int_ed_task(argument);
}
osThreadDef(int_ed_task_2,   osPriorityNormal,      512);
#endif
#if (INT_TRANS_MAX_NUM >= 3)
static void int_ed_task_3(void const * argument) {
    int_ed_task(argument);
}
osThreadDef(int_ed_task_3,   osPriorityNormal,      512);
#endif
#if (INT_TRANS_MAX_NUM >= 4)
static void int_ed_task_4(void const * argument) {
    int_ed_task(argument);
}
osThreadDef(int_ed_task_4,   osPriorityNormal,      512);
#endif

#if (ISO_TRANS_MAX_NUM >= 1)
static void iso_ed_task_1(void const * argument) {
    iso_ed_task(argument);
}
osThreadDef(iso_ed_task_1,   osPriorityNormal,      512);
#endif
#if (ISO_TRANS_MAX_NUM >= 2)
static void iso_ed_task_2(void const * argument) {
    iso_ed_task(argument);
}
osThreadDef(iso_ed_task_2,   osPriorityNormal,      512);
#endif

void ohciwrapp_init(usbisr_fnc_t *p_usbisr_fnc) {
    static const osSemaphoreDef_t * const sem_def_tbl[TOTAL_SEM_NUM] = {
        osSemaphore(ohciwrapp_sem_01), osSemaphore(ohciwrapp_sem_02), osSemaphore(ohciwrapp_sem_03)
      , osSemaphore(ohciwrapp_sem_04), osSemaphore(ohciwrapp_sem_05), osSemaphore(ohciwrapp_sem_06)
      , osSemaphore(ohciwrapp_sem_07)
#if (INT_TRANS_MAX_NUM >= 2)
      , osSemaphore(ohciwrapp_sem_08), osSemaphore(ohciwrapp_sem_09)
#endif
#if (INT_TRANS_MAX_NUM >= 3)
      , osSemaphore(ohciwrapp_sem_10), osSemaphore(ohciwrapp_sem_11)
#endif
#if (INT_TRANS_MAX_NUM >= 4)
      , osSemaphore(ohciwrapp_sem_12), osSemaphore(ohciwrapp_sem_13)
#endif
#if (ISO_TRANS_MAX_NUM >= 1)
      , osSemaphore(ohciwrapp_sem_14), osSemaphore(ohciwrapp_sem_15)
#endif
#if (ISO_TRANS_MAX_NUM >= 2)
      , osSemaphore(ohciwrapp_sem_16), osSemaphore(ohciwrapp_sem_17)
#endif
    };
    static const osThreadDef_t * const int_tsk_def_tbl[INT_TRANS_MAX_NUM] = {
        osThread(int_ed_task_1)
#if (INT_TRANS_MAX_NUM >= 2)
      , osThread(int_ed_task_2)
#endif
#if (INT_TRANS_MAX_NUM >= 3)
      , osThread(int_ed_task_3)
#endif
#if (INT_TRANS_MAX_NUM >= 4)
      , osThread(int_ed_task_4)
#endif
    };
#if (ISO_TRANS_MAX_NUM > 0)
    static const osThreadDef_t * const iso_tsk_def_tbl[ISO_TRANS_MAX_NUM] = {
        osThread(iso_ed_task_1)
#if (ISO_TRANS_MAX_NUM >= 2)
      , osThread(iso_ed_task_2)
#endif
    };
#endif

    uint32_t cnt;
    uint32_t index = 0;

    /* Disables interrupt for usb */
    GIC_DisableIRQ(USBIXUSBIX);

#if (USB_HOST_CH == 0)
    /* P4_1(USB0_EN) */
    GPIOP4      &= ~0x0002;         /* Outputs low level */
    GPIOPMC4    &= ~0x0002;         /* Port mode */
    GPIOPM4     &= ~0x0002;         /* Output mode */
#endif

    p_usbisr_cb = p_usbisr_fnc;
#if (USB_HOST_HISPEED == 0)
    g_usbx_host_SupportUsbDeviceSpeed = USB_HOST_FULL_SPEED;
#else
    g_usbx_host_SupportUsbDeviceSpeed = USB_HOST_HIGH_SPEED;
#endif
    p_usb_reg->HcRevision         = 0x00000010;
    p_usb_reg->HcControl          = 0x00000000;
    p_usb_reg->HcCommandStatus    = 0x00000000;
    p_usb_reg->HcInterruptStatus  = 0x00000000;
    p_usb_reg->HcInterruptEnable  = 0x00000000;
    p_usb_reg->HcInterruptDisable = 0x00000000;
    p_usb_reg->HcHCCA             = 0x00000000;
    p_usb_reg->HcPeriodCurrentED  = 0x00000000;
    p_usb_reg->HcControlHeadED    = 0x00000000;
    p_usb_reg->HcControlCurrentED = 0x00000000;
    p_usb_reg->HcBulkHeadED       = 0x00000000;
    p_usb_reg->HcBulkCurrentED    = 0x00000000;
    p_usb_reg->HcDoneHead         = 0x00000000;
    p_usb_reg->HcFmInterval       = 0x00002EDF;
    p_usb_reg->HcFmRemaining      = 0x00002EDF;
    p_usb_reg->HcFmNumber         = 0x00000000;
    p_usb_reg->HcPeriodicStart    = 0x00000000;
    p_usb_reg->HcLSThreshold      = 0x00000628;
    p_usb_reg->HcRhDescriptorA    = 0xFF000901;
    p_usb_reg->HcRhDescriptorB    = 0x00020000;
    p_usb_reg->HcRhStatus         = 0x00000000;
    p_usb_reg->HcRhPortStatus1    = 0x00000000;

#if (USB_HOST_CH == 0)
    GPIOP4      |=  0x0002;         /* P4_1 Outputs high level */
    osDelay(5);
    GPIOP4      &= ~0x0002;         /* P4_1 Outputs low level */
    osDelay(10);
#else
    osDelay(15);
#endif

    if (init_end == 0) {
        (void)memset(&ctl_ed, 0, sizeof(ctl_ed));
        (void)memset(&blk_ed, 0, sizeof(blk_ed));
        (void)memset(&int_ed[0], 0, sizeof(int_ed));
#if (ISO_TRANS_MAX_NUM > 0)
        (void)memset(&iso_ed[0], 0, sizeof(iso_ed));
#endif

        /* callback */
        semid_cb = osSemaphoreCreate(sem_def_tbl[index], 0);
        index++;
        (void)osThreadCreate(osThread(callback_task), 0);

        /* control transfer */
        ctl_ed.semid_wait = osSemaphoreCreate(sem_def_tbl[index], 0);
        index++;
        ctl_ed.semid_list = osSemaphoreCreate(sem_def_tbl[index], 0);
        index++;
        ctl_ed.tskid = osThreadCreate(osThread(control_ed_task), 0);

        /* bulk transfer */
        blk_ed.semid_wait = osSemaphoreCreate(sem_def_tbl[index], 0);
        index++;
        blk_ed.semid_list =  osSemaphoreCreate(sem_def_tbl[index], 0);
        index++;
        blk_ed.tskid = osThreadCreate(osThread(bulk_ed_task), 0);

        /* interrupt transfer */
        for (cnt = 0; cnt < INT_TRANS_MAX_NUM; cnt++) {
            int_ed[cnt].semid_wait = osSemaphoreCreate(sem_def_tbl[index], 0);
            index++;
            int_ed[cnt].semid_list = osSemaphoreCreate(sem_def_tbl[index], 0);
            index++;
            int_ed[cnt].tskid = osThreadCreate(int_tsk_def_tbl[cnt], (void *)cnt);
        }

#if (ISO_TRANS_MAX_NUM > 0)
        /* isochronous transfer */
        for (cnt = 0; cnt < ISO_TRANS_MAX_NUM; cnt++) {
            iso_ed[cnt].semid_wait = osSemaphoreCreate(sem_def_tbl[index], 0);
            index++;
            iso_ed[cnt].semid_list = osSemaphoreCreate(sem_def_tbl[index], 0);
            index++;
            iso_ed[cnt].tskid = osThreadCreate(iso_tsk_def_tbl[cnt], (void *)cnt);
        }
#endif
        init_end = 1;
    }
}

uint32_t ohciwrapp_reg_r(uint32_t reg_ofs) {
    if (init_end == 0) {
        return 0;
    }

    return *(uint32_t *)((uint8_t *)p_usb_reg + reg_ofs);
}

void ohciwrapp_reg_w(uint32_t reg_ofs, uint32_t set_data) {
    uint32_t cnt;
    uint32_t last_data;
    hcca_t   *p_hcca;

    if (init_end == 0) {
        return;
    }

    switch (reg_ofs) {
        case OHCI_REG_CONTROL:
            last_data            = p_usb_reg->HcControl;
            p_usb_reg->HcControl = (set_data & 0x000007FF);
            if ((last_data & OR_CONTROL_CLE) != (set_data & OR_CONTROL_CLE)) {
                /* change CLE */
                if ((set_data & OR_CONTROL_CLE) != 0) {
                    (void)osSemaphoreRelease(ctl_ed.semid_list);
                } else {
                    if (ctl_ed.trans_wait == 1) {
                        ctl_ed.trans_wait = 0;
                        (void)osSemaphoreRelease(ctl_ed.semid_wait);
                    }
                    (void)osSemaphoreWait(ctl_ed.semid_list, osWaitForever);
                }
            }
            if ((last_data & OR_CONTROL_BLE) != (set_data & OR_CONTROL_BLE)) {
                /* change BLE */
                if ((set_data & OR_CONTROL_BLE) != 0) {
                    (void)osSemaphoreRelease(blk_ed.semid_list);
                } else {
                    if (blk_ed.trans_wait == 1) {
                        blk_ed.trans_wait = 0;
                        (void)osSemaphoreRelease(blk_ed.semid_wait);
                    }
                    (void)osSemaphoreWait(blk_ed.semid_list, osWaitForever);
                }
            }
#if (ISO_TRANS_MAX_NUM > 0)
            if ((last_data & OR_CONTROL_IE) != (set_data & OR_CONTROL_IE)) {
                /* change IE */
                for (cnt = 0; cnt < ISO_TRANS_MAX_NUM; cnt++) {
                    if ((set_data & OR_CONTROL_IE) != 0) {
                        (void)osSemaphoreRelease(iso_ed[cnt].semid_list);
                    } else {
                        if (iso_ed[cnt].trans_wait == 1) {
                            iso_ed[cnt].trans_wait = 0;
                            (void)osSemaphoreRelease(iso_ed[cnt].semid_wait);
                        }
                        (void)osSemaphoreWait(iso_ed[cnt].semid_list, osWaitForever);
                    }
                }
            }
#endif
            if ((last_data & OR_CONTROL_PLE) != (set_data & OR_CONTROL_PLE)) {
                /* change PLE */
                for (cnt = 0; cnt < INT_TRANS_MAX_NUM; cnt++) {
                    if ((set_data & OR_CONTROL_PLE) != 0) {
                        (void)osSemaphoreRelease(int_ed[cnt].semid_list);
                    } else {
                        if (int_ed[cnt].trans_wait == 1) {
                            int_ed[cnt].trans_wait = 0;
                            (void)osSemaphoreRelease(int_ed[cnt].semid_wait);
                        }
                        (void)osSemaphoreWait(int_ed[cnt].semid_list, osWaitForever);
                    }
                }
            }
            break;
        case OHCI_REG_COMMANDSTATUS:
            if ((set_data & OR_CMD_STATUS_HCR) != 0) {    /* HostController Reset */
                p_usb_reg->HcCommandStatus |= OR_CMD_STATUS_HCR;
                if (usbx_api_host_init(16, g_usbx_host_SupportUsbDeviceSpeed, USBHCLOCK_X1_48MHZ) == USB_HOST_ATTACH) {
                    ohciwrapp_loc_Connect(1);
                }
                p_usb_reg->HcCommandStatus &= ~OR_CMD_STATUS_HCR;
            }
            if ((set_data & OR_CMD_STATUS_CLF) != 0) {
                p_usb_reg->HcCommandStatus |= OR_CMD_STATUS_CLF;
                osSignalSet(ctl_ed.tskid, SIG_GEN_LIST_REQ);
            }
            if ((set_data & OR_CMD_STATUS_BLF) != 0) {
                p_usb_reg->HcCommandStatus |= OR_CMD_STATUS_BLF;
                osSignalSet(blk_ed.tskid, SIG_GEN_LIST_REQ);
            }
            if ((set_data & OR_CMD_STATUS_OCR) != 0) {
                p_usb_reg->HcCommandStatus |= OR_CMD_STATUS_OCR;
            } else {
                p_usb_reg->HcCommandStatus &= ~OR_CMD_STATUS_OCR;
            }
            break;
        case OHCI_REG_INTERRUPTSTATUS:
            if (((p_usb_reg->HcInterruptStatus & OR_INTR_STATUS_WDH) != 0)
             && ((set_data & OR_INTR_STATUS_WDH) != 0)) {
                if (p_usb_reg->HcDoneHead != 0x00000000) {
                    p_hcca                       =  (hcca_t *)p_usb_reg->HcHCCA;
                    p_hcca->DoneHead             =  p_usb_reg->HcDoneHead;
                    p_usb_reg->HcDoneHead        =  0x00000000;
                    p_usb_reg->HcInterruptStatus |= OR_INTR_STATUS_WDH;
                    (void)osSemaphoreRelease(semid_cb);
                } else {
                    p_usb_reg->HcInterruptStatus &= ~OR_INTR_STATUS_WDH;
                }
            }
            if ((set_data & OR_INTR_STATUS_RHSC) != 0) {
                p_usb_reg->HcInterruptStatus &= ~OR_INTR_STATUS_RHSC;
            }
            break;
        case OHCI_REG_INTERRUPTENABLE:
        case OHCI_REG_INTERRUPTDISABLE:
        case OHCI_REG_HCCA:
        case OHCI_REG_CONTROLHEADED:
        case OHCI_REG_CONTROLCURRENTED:
        case OHCI_REG_BULKHEADED:
        case OHCI_REG_BULKCURRENTED:
        case OHCI_REG_FMINTERVAL:
        case OHCI_REG_FMREMAINING:
        case OHCI_REG_PERIODICSTART:
        case OHCI_REG_LSTHRESHOLD:
        case OHCI_REG_RHDESCRIPTORA:
        case OHCI_REG_RHDESCRIPTORB:
        case OHCI_REG_RHSTATUS:
            *(uint32_t *)((uint8_t *)p_usb_reg + reg_ofs) = set_data;
            break;
        case OHCI_REG_RHPORTSTATUS1:
            p_usb_reg->HcRhPortStatus1 &= ~(set_data & 0xFFFF0000);
            if ((set_data & OR_RH_PORT_PRS) != 0) {    /* Set Port Reset */
                p_usb_reg->HcRhPortStatus1 |= OR_RH_PORT_PRS;
                usbx_host_UsbBusReset();
                p_usb_reg->HcRhPortStatus1 &= ~OR_RH_PORT_PRS;
            }
            break;
        case OHCI_REG_REVISION:
        case OHCI_REG_PERIODCURRENTED:
        case OHCI_REG_DONEHEADED:
        case OHCI_REG_FMNUMBER:
        default:
            /* Do Nothing */
            break;
    }
}

static void callback_task(void const * argument) {
    usbisr_fnc_t *p_wk_cb = p_usbisr_cb;

    if (p_wk_cb == NULL) {
        return;
    }

    while (1) {
        osSemaphoreWait(semid_cb, osWaitForever);
        if (connect_change != 0xFFFFFFFF) {
            connect_change = 0xFFFFFFFF;
            connect_check();
        }
        p_wk_cb();
    }
}

static void control_ed_task(void const * argument) {
    while (1) {
        osSignalWait(SIG_GEN_LIST_REQ, osWaitForever);
        (void)osSemaphoreWait(ctl_ed.semid_list, osWaitForever);
        while ((p_usb_reg->HcControl & OR_CONTROL_CLE) != 0) {
            if ((p_usb_reg->HcControlCurrentED == 0)
             && ((p_usb_reg->HcCommandStatus & OR_CMD_STATUS_CLF) != 0)) {
                p_usb_reg->HcControlCurrentED =  p_usb_reg->HcControlHeadED;
                p_usb_reg->HcCommandStatus    &= ~OR_CMD_STATUS_CLF;
            }
            if (p_usb_reg->HcControlCurrentED != 0) {
                ctl_ed.p_curr_ed = (hced_t *)p_usb_reg->HcControlCurrentED;
                if (chk_genelal_ed(&ctl_ed) != 0) {
                    control_trans(&ctl_ed);
                    p_usb_reg->HcCommandStatus |= OR_CMD_STATUS_CLF;
                }
                p_usb_reg->HcControlCurrentED = (uint32_t)ctl_ed.p_curr_ed->nextED;
            } else {
                break;
            }
        }
        if ((p_usb_reg->HcCommandStatus & OR_CMD_STATUS_CLF) != 0) {
            osSignalSet(ctl_ed.tskid, SIG_GEN_LIST_REQ);
        }
        (void)osSemaphoreRelease(ctl_ed.semid_list);
    }
}

static void bulk_ed_task(void const * argument) {
    while (1) {
        osSignalWait(SIG_GEN_LIST_REQ, osWaitForever);
        (void)osSemaphoreWait(blk_ed.semid_list, osWaitForever);
        while ((p_usb_reg->HcControl & OR_CONTROL_BLE) != 0) {
            if ((p_usb_reg->HcBulkCurrentED == 0)
             && ((p_usb_reg->HcCommandStatus & OR_CMD_STATUS_BLF) != 0)) {
                p_usb_reg->HcBulkCurrentED =  p_usb_reg->HcBulkHeadED;
                p_usb_reg->HcCommandStatus &= ~OR_CMD_STATUS_BLF;
            }
            if (p_usb_reg->HcBulkCurrentED != 0) {
                blk_ed.p_curr_ed = (hced_t *)p_usb_reg->HcBulkCurrentED;
                if (chk_genelal_ed(&blk_ed) != 0) {
                    bulk_trans(&blk_ed);
                    p_usb_reg->HcCommandStatus |= OR_CMD_STATUS_BLF;
                }
                p_usb_reg->HcBulkCurrentED = (uint32_t)blk_ed.p_curr_ed->nextED;
            } else {
                break;
            }
        }
        if ((p_usb_reg->HcCommandStatus & OR_CMD_STATUS_BLF) != 0) {
            osSignalSet(blk_ed.tskid, SIG_GEN_LIST_REQ);
        }
        (void)osSemaphoreRelease(blk_ed.semid_list);
    }
}

static void int_ed_task(void const * argument) {
    genelal_ed_t *p_int_ed = &int_ed[(uint32_t)argument];
    uint32_t     cnt;
    uint32_t     wait_cnt = 0;
    hcca_t       *p_hcca;
    hced_t       *p_ed;

    while (1) {
        (void)osSemaphoreWait(p_int_ed->semid_list, osWaitForever);
        if (p_int_ed->p_curr_ed == NULL) {
            for (cnt = 0; (cnt < 32) && ((p_usb_reg->HcControl & OR_CONTROL_PLE) != 0)
                                                 && (p_int_ed->p_curr_ed == NULL); cnt++) {
                p_hcca = (hcca_t *)p_usb_reg->HcHCCA;
                p_ed   = (hced_t *)p_hcca->IntTable[cnt];
                while ((p_ed != NULL) && ((p_usb_reg->HcControl & OR_CONTROL_PLE) != 0)
                                                        && (p_int_ed->p_curr_ed == NULL)) {
                    if (int_trans_doing(p_ed, (uint32_t)argument) == 0) {
                        p_int_ed->p_curr_ed = p_ed;
                        if (chk_genelal_ed(p_int_ed) != 0) {
                            int_trans_setting(p_int_ed, (uint32_t)argument);
                        } else {
                            p_int_ed->p_curr_ed = NULL;
                        }
                    }
                    p_ed = p_ed->nextED;
                }
            }
        }
        if (p_int_ed->p_curr_ed != NULL) {
            while ((p_usb_reg->HcControl & OR_CONTROL_PLE) != 0) {
                if (chk_genelal_ed(p_int_ed) != 0) {
                    int_trans(p_int_ed);
                    (void)osSemaphoreWait(p_int_ed->semid_wait, osWaitForever);
                    usbx_host_stop_transfer(p_int_ed->pipe_no);
                    wait_cnt = p_int_ed->cycle_time;
                } else {
                    if (wait_cnt > 0) {
                        wait_cnt--;
                    } else {
                        p_int_ed->p_curr_ed = NULL;
                    }
                    break;
                }
            }
        }
        (void)osSemaphoreRelease(p_int_ed->semid_list);
        if (p_int_ed->p_curr_ed == NULL) {
            osDelay(10);
        } else {
            osDelay(1);
        }
    }
}

static int32_t int_trans_doing(hced_t *p_ed, uint32_t index) {
    uint32_t cnt;
    int32_t  ret = 0;

    for (cnt = 0; cnt < INT_TRANS_MAX_NUM; cnt++) {
        if ((index != cnt) && (int_ed[cnt].p_curr_ed == p_ed)) {
            ret = 1;
        }
    }

    return ret;
}

static int32_t chk_genelal_ed(genelal_ed_t *p_g_ed){
    int32_t ret   = 0;
    hced_t  *p_ed = p_g_ed->p_curr_ed;

    if (((p_ed->control & ED_SKIP)   != 0)
     || ((p_ed->control & ED_FORMAT) != 0)
     || ((p_ed->headTD & ED_HALTED)  != 0)
     || ((p_ed->tailTD & 0xFFFFFFF0) == (p_ed->headTD & 0xFFFFFFF0))) {
        /* Do Nothing */
    } else if ((p_ed->control & 0x0000007F) > 10) {
        p_ed->headTD |= ED_HALTED;
    } else {
        p_g_ed->p_curr_td = (void *)(p_ed->headTD & 0xFFFFFFF0);
        if (p_g_ed->p_curr_td == NULL) {
            p_ed->headTD |= ED_HALTED;
        } else {
            hctd_t *p_td = (hctd_t *)p_g_ed->p_curr_td;

            p_g_ed->p_start_buf = p_td->currBufPtr;
            ret = 1;
        }
    }

    return ret;
}

static void chk_genelal_td_done(genelal_ed_t *p_g_ed) {
    hcca_t   *p_hcca;
    hctd_t   *p_td = (hctd_t *)p_g_ed->p_curr_td;
    uint32_t ConditionCode = RZA_IO_RegRead_32(&p_td->control, TD_CTL_SHFT_CC, TD_CTL_MSK_CC);

    if ((ConditionCode != TD_CC_NOT_ACCESSED_1) && (ConditionCode != TD_CC_NOT_ACCESSED_2)) {
        p_g_ed->p_curr_ed->headTD = ((uint32_t)p_td->nextTD & 0xFFFFFFF0)
                                  | (p_g_ed->p_curr_ed->headTD & 0x0000000F);
        p_td->nextTD              = (hctd_t *)p_usb_reg->HcDoneHead;
        p_usb_reg->HcDoneHead     = (uint32_t)p_g_ed->p_curr_td;
        if ((p_usb_reg->HcInterruptStatus & OR_INTR_STATUS_WDH) == 0) {
            p_hcca                       =  (hcca_t *)p_usb_reg->HcHCCA;
            p_hcca->DoneHead             =  p_usb_reg->HcDoneHead;
            p_usb_reg->HcDoneHead        =  0x00000000;
            p_usb_reg->HcInterruptStatus |= OR_INTR_STATUS_WDH;
            (void)osSemaphoreRelease(semid_cb);
        }
    }
}

static void chk_split_trans_setting(genelal_ed_t *p_g_ed) {
    uint8_t   *p_buf;
    tdinfo_t  td_info;
    hctd_t    *p_td = (hctd_t *)p_g_ed->p_curr_td;

    /* Hi-Speed mode only */
    if (g_usbx_host_UsbDeviceSpeed != USB_HOST_HIGH_SPEED) {
        return;
    }

    if (RZA_IO_RegRead_32(&p_td->control, TD_CTL_SHFT_CC, TD_CTL_MSK_CC) != TD_CC_NOERROR) {
        return;
    }

    get_td_info(p_g_ed, &td_info);
    p_buf = p_g_ed->p_start_buf;

    if (td_info.direction == 0) {
        uint8_t  bRequest = p_buf[1];
        uint16_t wValue   = (p_buf[3] << 8) + p_buf[2];
        uint16_t wIndx    = (p_buf[5] << 8) + p_buf[4];
        uint16_t devadd;

        if ((td_info.devadr == 0) && (bRequest == SET_ADDRESS)) {
            /* SET_ADDRESS */
            usbx_host_get_devadd(USB_HOST_DEVICE_0, &devadd);
            usbx_host_set_devadd(wValue, &devadd);
            if (split_ctl.root_devadr == 0) {
                split_ctl.root_devadr = wValue; /* New Address */
            }
        } else if ((td_info.devadr == split_ctl.root_devadr) && (bRequest == SET_FEATURE)
                && (wValue == 0x0004) && (split_ctl.root_devadr != 0)) {
            /* SET_FEATURE PORT_RESET */
            split_ctl.reset_port = (wIndx & 0x00FF);
        } else if ((td_info.devadr == split_ctl.root_devadr) && (bRequest == GET_STATUS)) {
            /* GET_STATUS */
            split_ctl.get_port = wIndx;
            split_ctl.seq_cnt = 1;
        } else {
            /* Do Nothing */
        }
    } else if (td_info.direction == 2) {
        if ((td_info.devadr == split_ctl.root_devadr) && (split_ctl.seq_cnt == 1)) {
            if (split_ctl.get_port < PORT_NUM) {
                split_ctl.port_sts_bits[split_ctl.get_port] = (p_buf[1] << 8) + p_buf[0];
            }
            split_ctl.seq_cnt = 0;
        }
    } else {
        /* Do Nothing */
    }
}

static void set_split_trans_setting(void) {
    uint16_t port_speed;
    uint16_t devadd;

    if ((split_ctl.root_devadr != 0) && (split_ctl.reset_port != 0) && (split_ctl.reset_port < PORT_NUM)) {
        usbx_host_get_devadd(USB_HOST_DEVICE_0, &devadd);
        RZA_IO_RegWrite_16(&devadd, split_ctl.root_devadr, USB_DEVADDn_UPPHUB_SHIFT, USB_DEVADDn_UPPHUB);
        RZA_IO_RegWrite_16(&devadd, split_ctl.reset_port, USB_DEVADDn_HUBPORT_SHIFT, USB_DEVADDn_HUBPORT);
        if ((split_ctl.port_sts_bits[split_ctl.reset_port] & PORT_HIGH_SPEED) != 0) {
            port_speed = USB_HOST_HIGH_SPEED;
        } else if ((split_ctl.port_sts_bits[split_ctl.reset_port] & PORT_LOW_SPEED) != 0) {
            port_speed = USB_HOST_LOW_SPEED;
        } else {
            port_speed = USB_HOST_FULL_SPEED;
        }
        RZA_IO_RegWrite_16(&devadd, port_speed, USB_DEVADDn_USBSPD_SHIFT, USB_DEVADDn_USBSPD);
        usbx_host_set_devadd(USB_HOST_DEVICE_0, &devadd);
        split_ctl.reset_port = 0;
    }
}

static void control_trans(genelal_ed_t *p_g_ed) {
    hctd_t   *p_td = (hctd_t *)p_g_ed->p_curr_td;
    tdinfo_t td_info;
    uint16_t devadd;

    get_td_info(p_g_ed, &td_info);

    if (g_usbx_host_UsbDeviceSpeed == USB_HOST_HIGH_SPEED) {
        if (td_info.devadr == 0) {
            set_split_trans_setting();
        }
    } else {
        /* When a non-Hi-Speed, the communication speed is determined from the TD. */
        usbx_host_get_devadd(USB_HOST_DEVICE_0, &devadd);
        if (td_info.speed == 1) {
            RZA_IO_RegWrite_16(&devadd, USB_HOST_LOW_SPEED, USB_DEVADDn_USBSPD_SHIFT, USB_DEVADDn_USBSPD);
        } else {
            RZA_IO_RegWrite_16(&devadd, USB_HOST_FULL_SPEED, USB_DEVADDn_USBSPD_SHIFT, USB_DEVADDn_USBSPD);
        }
        usbx_host_set_devadd(td_info.devadr, &devadd);
    }

    USB20X.DCPMAXP  = (td_info.devadr << 12) + td_info.msp;
    if (td_info.direction == 0) {
        g_usbx_host_CmdStage = (USB_HOST_STAGE_SETUP | USB_HOST_CMD_IDLE);
    } else  if (td_info.count != 0) {
        g_usbx_host_CmdStage = (USB_HOST_STAGE_DATA | USB_HOST_CMD_IDLE);
    } else {
        g_usbx_host_CmdStage = (USB_HOST_STAGE_STATUS | USB_HOST_CMD_IDLE);
    }
    g_usbx_host_pipe_status[USB_HOST_PIPE0]  = USB_HOST_PIPE_WAIT;
    p_g_ed->pipe_no    = USB_HOST_PIPE0;

    p_g_ed->trans_wait = 1;

    if (td_info.direction == 0) {
        uint16_t Req  = (p_td->currBufPtr[1] << 8) + p_td->currBufPtr[0];
        uint16_t Val  = (p_td->currBufPtr[3] << 8) + p_td->currBufPtr[2];
        uint16_t Indx = (p_td->currBufPtr[5] << 8) + p_td->currBufPtr[4];
        uint16_t Len  = (p_td->currBufPtr[7] << 8) + p_td->currBufPtr[6];

        g_usbx_host_data_pointer[USB_HOST_PIPE0] = p_td->bufEnd;
        usbx_host_SetupStage(Req, Val, Indx, Len);
    } else if (td_info.direction == 1) {
        usbx_host_CtrlWriteStart(td_info.count, p_td->currBufPtr);
    } else {
        usbx_host_CtrlReadStart(td_info.count, p_td->currBufPtr);
    }

    (void)osSemaphoreWait(p_g_ed->semid_wait, CTL_TRANS_TIMEOUT);
    if (p_g_ed->trans_wait == 1) {
        p_g_ed->trans_wait = 0;
        RZA_IO_RegWrite_32(&p_td->control, TD_CC_DEVICENOTRESPONDING, TD_CTL_SHFT_CC, TD_CTL_MSK_CC);
    }

    g_usbx_host_CmdStage &= (~USB_HOST_CMD_FIELD);
    g_usbx_host_CmdStage |= USB_HOST_CMD_IDLE;
    g_usbx_host_pipe_status[USB_HOST_PIPE0] = USB_HOST_PIPE_IDLE;
}

static void bulk_trans(genelal_ed_t *p_g_ed) {
    hctd_t                 *p_td = (hctd_t *)p_g_ed->p_curr_td;
    hced_t                 *p_ed = p_g_ed->p_curr_ed;
    tdinfo_t               td_info;
    USB_HOST_CFG_PIPETBL_t *user_table = &usb_host_blk_ep_tbl1[0];
    uint8_t                wk_table[6];

    get_td_info(p_g_ed, &td_info);

    wk_table[0] = 0;
    wk_table[1] = USB_HOST_ENDPOINT_DESC;
    wk_table[2] = td_info.endpoint_no;
    if (td_info.direction == 2) {
        wk_table[2] |= USB_HOST_EP_IN;
    }
    wk_table[3] = USB_HOST_EP_BULK;
    wk_table[4] = (uint8_t)td_info.msp;
    wk_table[5] = (uint8_t)(td_info.msp >> 8);
    p_g_ed->pipe_no    = user_table->pipe_number;
    usbx_api_host_SetEndpointTable(td_info.devadr, user_table, wk_table);

    set_togle(p_g_ed->pipe_no, p_td, p_ed);

    p_g_ed->trans_wait = 1;
    if (td_info.direction == 1) {
        usbx_host_start_send_transfer(p_g_ed->pipe_no, td_info.count, p_td->currBufPtr);
    } else {
        usbx_host_start_receive_transfer(p_g_ed->pipe_no, td_info.count, p_td->currBufPtr);
    }

    (void)osSemaphoreWait(p_g_ed->semid_wait, BLK_TRANS_TIMEOUT);
    usbx_host_stop_transfer(p_g_ed->pipe_no);
}

static void int_trans_setting(genelal_ed_t *p_g_ed, uint32_t index) {
    hctd_t                 *p_td = (hctd_t *)p_g_ed->p_curr_td;
    hced_t                 *p_ed = p_g_ed->p_curr_ed;
    tdinfo_t               td_info;
    USB_HOST_CFG_PIPETBL_t *user_table = &usb_host_int_ep_tbl1[index];
    uint8_t                wk_table[6];
    uint32_t               cycle_time;
    uint16_t               devadd;

    get_td_info(p_g_ed, &td_info);

    wk_table[0] = 0;
    wk_table[1] = USB_HOST_ENDPOINT_DESC;
    wk_table[2] = td_info.endpoint_no;
    if (td_info.direction == 2) {
        wk_table[2] |= USB_HOST_EP_IN;
    }
    wk_table[3] = USB_HOST_EP_INT;
    wk_table[4] = (uint8_t)td_info.msp;
    wk_table[5] = (uint8_t)(td_info.msp >> 8);
    cycle_time  = chk_cycle(p_ed);
    p_g_ed->cycle_time = cycle_time;
    user_table->pipe_cycle = 0;
    while (cycle_time > 1) {
        cycle_time >>= 1;
        user_table->pipe_cycle++;
    }
    if (g_usbx_host_UsbDeviceSpeed == USB_HOST_HIGH_SPEED) {
        usbx_host_get_devadd(td_info.devadr, &devadd);
        if (RZA_IO_RegRead_16(&devadd, USB_DEVADDn_USBSPD_SHIFT, USB_DEVADDn_USBSPD) == USB_HOST_HIGH_SPEED) {
            user_table->pipe_cycle += 3;
            if (user_table->pipe_cycle > 7) {
                user_table->pipe_cycle = 7;
            }
        }
    }

    p_g_ed->pipe_no    = user_table->pipe_number;
    usbx_api_host_SetEndpointTable(td_info.devadr, user_table, wk_table);

    set_togle(p_g_ed->pipe_no, p_td, p_ed);
}

static uint32_t chk_cycle(hced_t *p_ed) {
    uint32_t     cnt;
    uint32_t     hit_cnt    = 0;
    uint32_t     cycle_time = 1;
    hcca_t       *p_hcca;
    hced_t       *p_wk_ed;

    p_hcca = (hcca_t *)p_usb_reg->HcHCCA;

    for (cnt = 0; cnt < 32; cnt++) {
        p_wk_ed = (hced_t *)p_hcca->IntTable[cnt];
        while (p_wk_ed != NULL) {
            if (p_wk_ed == p_ed) {
                hit_cnt++;
                break;
            }
            p_wk_ed = p_wk_ed->nextED;
        }
    }
    if (hit_cnt < 2) {
        cycle_time = 32;
    } else if (hit_cnt < 4) {
        cycle_time = 16;
    } else if (hit_cnt < 8) {
        cycle_time = 8;
    } else if (hit_cnt < 16) {
        cycle_time = 4;
    } else if (hit_cnt < 32) {
        cycle_time = 2;
    } else{
        cycle_time = 1;
    }

    return cycle_time;
}

static void int_trans(genelal_ed_t *p_g_ed) {
    hctd_t   *p_td = (hctd_t *)p_g_ed->p_curr_td;
    tdinfo_t td_info;

    get_td_info(p_g_ed, &td_info);
    p_g_ed->trans_wait = 1;
    if (td_info.direction == 1) {
        usbx_host_start_send_transfer(p_g_ed->pipe_no, td_info.count, p_td->currBufPtr);
    } else {
        usbx_host_start_receive_transfer(p_g_ed->pipe_no, td_info.count, p_td->currBufPtr);
    }
}

static void get_td_info(genelal_ed_t *p_g_ed, tdinfo_t *p_td_info) {
    hced_t *p_ed = p_g_ed->p_curr_ed;

    p_td_info->endpoint_no = (uint8_t)((p_ed->control >> 7) & 0x0000000F);
    p_td_info->msp         = (p_ed->control >> 16) & 0x000007FF;
    p_td_info->devadr      = p_ed->control & 0x0000000F;
    p_td_info->speed       = (p_ed->control >> 13) & 0x00000001;
    p_td_info->direction   = (p_ed->control >> 11) & 0x00000003;

    if ((p_ed->control & ED_FORMAT) == 0) {
        hctd_t *p_td = (hctd_t *)p_g_ed->p_curr_td;

        if ((p_td_info->direction == 0) || (p_td_info->direction == 3)) {
            if ((p_td->control & TD_CTL_MSK_DP) == TD_SETUP) {
                p_td_info->direction = 0;
            } else if ((p_td->control & TD_CTL_MSK_DP) == TD_OUT) {
                p_td_info->direction = 1;
            } else {
                p_td_info->direction = 2;
            }
        }
        if (p_td->currBufPtr != NULL) {
            p_td_info->count = (uint32_t)p_td->bufEnd - (uint32_t)p_td->currBufPtr + 1;
        } else {
            p_td_info->count     = 0;
        }
    } else {
#if (ISO_TRANS_MAX_NUM > 0)
        hcisotd_t *p_isotd = (hcisotd_t *)p_g_ed->p_curr_td;

        if ((p_td_info->direction == 0) || (p_td_info->direction == 3)) {
            if ((p_isotd->control & TD_CTL_MSK_DP) == TD_SETUP) {
                p_td_info->direction = 0;
            } else if ((p_isotd->control & TD_CTL_MSK_DP) == TD_OUT) {
                p_td_info->direction = 1;
            } else {
                p_td_info->direction = 2;
            }
        }
#endif
    }
}

static void set_togle(uint32_t pipe, hctd_t *p_td, hced_t *p_ed) {
    if ((p_td->control & TD_CTL_MSK_T) == TD_TOGGLE_0) {
        usbx_host_set_sqclr(pipe);
    } else if ((p_td->control & TD_CTL_MSK_T) == TD_TOGGLE_1) {
        usbx_host_set_sqset(pipe);
    } else if ((p_ed->headTD & ED_TOGLE_CARRY) == 0) {
        usbx_host_set_sqclr(pipe);
    } else {
        usbx_host_set_sqset(pipe);
    }
}

#if (ISO_TRANS_MAX_NUM > 0)
static void iso_ed_task(void const * argument) {
    genelal_ed_t *p_iso_ed = &iso_ed[(uint32_t)argument];
    uint32_t     wait_cnt = 0;
    uint32_t     wait_time = 0;
    hcca_t       *p_hcca;
    hced_t       *p_ed;

    while (1) {
        (void)osSemaphoreWait(p_iso_ed->semid_list, osWaitForever);
        if (p_iso_ed->p_curr_ed == NULL) {
            p_hcca = (hcca_t *)p_usb_reg->HcHCCA;
            p_ed   = (hced_t *)p_hcca->IntTable[0];
            while ((p_ed != NULL) && ((p_usb_reg->HcControl & OR_CONTROL_IE) != 0)
                                                    && (p_iso_ed->p_curr_ed == NULL)) {
                if (iso_trans_doing(p_ed, (uint32_t)argument) == 0) {
                    p_iso_ed->p_curr_ed = p_ed;
                    if (chk_iso_ed(p_iso_ed) != 0) {
                        iso_trans_setting(p_iso_ed, (uint32_t)argument);
                    } else {
                        p_iso_ed->p_curr_ed = NULL;
                    }
                }
                p_ed = p_ed->nextED;
            }
            p_iso_ed->psw_idx = 0;
        }
        if (p_iso_ed->p_curr_ed != NULL) {
            while ((p_usb_reg->HcControl & OR_CONTROL_IE) != 0) {
                if (chk_iso_ed(p_iso_ed) != 0) {
                    wait_time = iso_chk_starting_frame(p_iso_ed);
                    if (wait_time != 0) {
                        osDelay(wait_time);
                        p_usb_reg->HcFmNumber += wait_time;
                        p_usb_reg->HcFmNumber &= 0x0000FFFF;
                    }
                    p_iso_ed->psw_idx   = 0;
                    iso_trans(p_iso_ed);
                    (void)osSemaphoreWait(p_iso_ed->semid_wait, osWaitForever);
                    usbx_host_stop_transfer(p_iso_ed->pipe_no);
                    wait_cnt = 1;
                } else {
                    if (wait_cnt > 0) {
                        wait_cnt--;
                    } else {
                        p_iso_ed->p_curr_ed = NULL;
                    }
                    break;
                }
            }
        }
        (void)osSemaphoreRelease(p_iso_ed->semid_list);
        if (p_iso_ed->p_curr_ed == NULL) {
            osDelay(10);
        } else {
            osDelay(1);
        }
    }
}

static int32_t iso_trans_doing(hced_t *p_ed, uint32_t index) {
    uint32_t cnt;
    int32_t  ret = 0;

    for (cnt = 0; cnt < ISO_TRANS_MAX_NUM; cnt++) {
        if ((index != cnt) && (iso_ed[cnt].p_curr_ed == p_ed)) {
            ret = 1;
        }
    }

    return ret;
}

static void chk_iso_td_done(genelal_ed_t *p_g_ed) {
    hcca_t    *p_hcca;
    hcisotd_t *p_isotd = (hcisotd_t *)p_g_ed->p_curr_td;
    uint32_t  ConditionCode = RZA_IO_RegRead_32(&p_isotd->control, TD_CTL_SHFT_CC, TD_CTL_MSK_CC);

    if ((ConditionCode != TD_CC_NOT_ACCESSED_1) && (ConditionCode != TD_CC_NOT_ACCESSED_2)) {
        p_g_ed->p_curr_ed->headTD = ((uint32_t)p_isotd->nextTD & 0xFFFFFFF0)
                                  | (p_g_ed->p_curr_ed->headTD & 0x0000000F);
        p_isotd->nextTD           = (hcisotd_t *)p_usb_reg->HcDoneHead;
        p_usb_reg->HcDoneHead     = (uint32_t)p_g_ed->p_curr_td;
        if ((p_usb_reg->HcInterruptStatus & OR_INTR_STATUS_WDH) == 0) {
            p_hcca                       =  (hcca_t *)p_usb_reg->HcHCCA;
            p_hcca->DoneHead             =  p_usb_reg->HcDoneHead;
            p_usb_reg->HcDoneHead        =  0x00000000;
            p_usb_reg->HcInterruptStatus |= OR_INTR_STATUS_WDH;
            (void)osSemaphoreRelease(semid_cb);
        }
    }
}

static int32_t chk_iso_ed(genelal_ed_t *p_g_ed){
    int32_t ret   = 0;
    hced_t  *p_ed = p_g_ed->p_curr_ed;

    if (((p_ed->control & ED_SKIP)   != 0)
     || ((p_ed->control & ED_FORMAT) == 0)
     || ((p_ed->headTD & ED_HALTED)  != 0)
     || ((p_ed->tailTD & 0xFFFFFFF0) == (p_ed->headTD & 0xFFFFFFF0))) {
        /* Do Nothing */
    } else if ((p_ed->control & 0x0000007F) > 10) {
        p_ed->headTD |= ED_HALTED;
    } else {
        p_g_ed->p_curr_td = (void *)(p_ed->headTD & 0xFFFFFFF0);
        if (p_g_ed->p_curr_td == NULL) {
            p_ed->headTD |= ED_HALTED;
        } else {
            hcisotd_t *p_isotd = (hcisotd_t *)p_g_ed->p_curr_td;

            p_g_ed->p_start_buf = p_isotd->bufferPage0;
            ret = 1;
        }
    }

    return ret;
}

static void iso_trans_setting(genelal_ed_t *p_g_ed, uint32_t index) {
    tdinfo_t               td_info;
    USB_HOST_CFG_PIPETBL_t *user_table = &usb_host_iso_ep_tbl1[index];
    uint8_t                wk_table[6];
    uint16_t               devadd;

    get_td_info(p_g_ed, &td_info);

    wk_table[0] = 0;
    wk_table[1] = USB_HOST_ENDPOINT_DESC;
    wk_table[2] = td_info.endpoint_no;
    if (td_info.direction == 2) {
        wk_table[2] |= USB_HOST_EP_IN;
    }
    wk_table[3] = USB_HOST_EP_ISO;
    wk_table[4] = (uint8_t)td_info.msp;
    wk_table[5] = (uint8_t)(td_info.msp >> 8);
    p_g_ed->cycle_time = 1;
    user_table->pipe_cycle = 0;
    if (g_usbx_host_UsbDeviceSpeed == USB_HOST_HIGH_SPEED) {
        usbx_host_get_devadd(td_info.devadr, &devadd);
        if (RZA_IO_RegRead_16(&devadd, USB_DEVADDn_USBSPD_SHIFT, USB_DEVADDn_USBSPD) == USB_HOST_HIGH_SPEED) {
            user_table->pipe_cycle += 3;
        }
    }

    p_g_ed->pipe_no    = user_table->pipe_number;
    usbx_api_host_SetEndpointTable(td_info.devadr, user_table, wk_table);
}

static uint32_t iso_chk_starting_frame(genelal_ed_t *p_g_ed) {
    hcisotd_t *p_isotd = (hcisotd_t *)p_g_ed->p_curr_td;
    uint32_t  starting_frame = p_isotd->control & 0x0000FFFF;
    uint32_t  wait_time = 0;

    if ((p_g_ed->psw_idx == 0) && (starting_frame > p_usb_reg->HcFmNumber)) {
        wait_time = starting_frame - p_usb_reg->HcFmNumber;
    }

    return wait_time;
}

static void iso_trans(genelal_ed_t *p_g_ed) {
    hcisotd_t *p_isotd = (hcisotd_t *)p_g_ed->p_curr_td;
    tdinfo_t  td_info;
    uint32_t  buff_addr;
    uint32_t  data_size;

    if (((uint32_t)p_isotd->offsetPSW[p_g_ed->psw_idx] & 0x00001000) == 0) {
        buff_addr = (uint32_t)p_isotd->bufferPage0 & 0xFFFFF000;
    } else {
        buff_addr = (uint32_t)p_isotd->bufEnd & 0xFFFFF000;
    }
    buff_addr |= (uint32_t)p_isotd->offsetPSW[p_g_ed->psw_idx] & 0x00000FFF;

    if (p_g_ed->psw_idx < RZA_IO_RegRead_32(&p_isotd->control, TD_CTL_SHFT_FC, TD_CTL_MSK_FC)) {
        data_size = p_isotd->offsetPSW[p_g_ed->psw_idx + 1] - p_isotd->offsetPSW[p_g_ed->psw_idx];
    } else {
        data_size = (uint32_t)p_isotd->bufEnd - buff_addr + 1;
    }
    p_isotd->offsetPSW[p_g_ed->psw_idx] = (uint16_t)data_size;

    get_td_info(p_g_ed, &td_info);
    p_g_ed->trans_wait = 1;
    if (td_info.direction == 1) {
        usbx_host_start_send_transfer(p_g_ed->pipe_no, data_size, (uint8_t *)buff_addr);
    } else {
        usbx_host_start_receive_transfer(p_g_ed->pipe_no, data_size, (uint8_t *)buff_addr);
    }
}
#endif

static void connect_check(void) {
    uint32_t type = 0;
    uint16_t stat;
    uint16_t devadd = 0;
    uint32_t wk_HcRhPortStatus1 = p_usb_reg->HcRhPortStatus1;

    if (usbx_host_CheckAttach() == USB_HOST_ATTACH) {
        type = 1;
    }

    if ((((wk_HcRhPortStatus1 & OR_RH_PORT_CCS) == 0) && (type == 0))
     || (((wk_HcRhPortStatus1 & OR_RH_PORT_CCS) != 0) && (type != 0))) {
        return;
    }

    if (type == 0) {
        usbx_host_UsbDetach();
        wk_HcRhPortStatus1 &= ~OR_RH_PORT_CCS;
    } else {
        usbx_host_UsbAttach();
        stat = usbx_host_UsbBusReset();
        RZA_IO_RegWrite_16(&devadd, 0, USB_DEVADDn_UPPHUB_SHIFT, USB_DEVADDn_UPPHUB);
        RZA_IO_RegWrite_16(&devadd, 0, USB_DEVADDn_HUBPORT_SHIFT, USB_DEVADDn_HUBPORT);
        if (stat == USB_HOST_HSMODE) {
            wk_HcRhPortStatus1 &= ~OR_RH_PORT_LSDA;
            RZA_IO_RegWrite_16(&USB20X.SOFCFG, 0, USB_SOFCFG_TRNENSEL_SHIFT, USB_SOFCFG_TRNENSEL);
            g_usbx_host_UsbDeviceSpeed = USB_HOST_HIGH_SPEED;
        } else if (stat == USB_HOST_FSMODE) {
            wk_HcRhPortStatus1 &= ~OR_RH_PORT_LSDA;
            RZA_IO_RegWrite_16(&USB20X.SOFCFG, 0, USB_SOFCFG_TRNENSEL_SHIFT, USB_SOFCFG_TRNENSEL);
            g_usbx_host_UsbDeviceSpeed = USB_HOST_FULL_SPEED;
        } else {
            wk_HcRhPortStatus1 |= OR_RH_PORT_LSDA;
            RZA_IO_RegWrite_16(&USB20X.SOFCFG, 1, USB_SOFCFG_TRNENSEL_SHIFT, USB_SOFCFG_TRNENSEL);
            g_usbx_host_UsbDeviceSpeed = USB_HOST_LOW_SPEED;
        }
        RZA_IO_RegWrite_16(&devadd, g_usbx_host_UsbDeviceSpeed, USB_DEVADDn_USBSPD_SHIFT, USB_DEVADDn_USBSPD);
        usbx_host_init_pipe_status();
        usbx_host_set_devadd(USB_HOST_DEVICE_0, &devadd);
        wk_HcRhPortStatus1 |= OR_RH_PORT_CCS;
    }
    wk_HcRhPortStatus1           |= OR_RH_PORT_CSC;
    p_usb_reg->HcRhPortStatus1   =  wk_HcRhPortStatus1;
    p_usb_reg->HcInterruptStatus |= OR_INTR_STATUS_RHSC;
    (void)memset(&split_ctl, 0, sizeof(split_ctl));
}

void ohciwrapp_loc_Connect(uint32_t type) {
    uint32_t cnt;

    connect_change = type;
    if (type == 0) {
        if (ctl_ed.trans_wait == 1) {
            ohciwrapp_loc_TransEnd(ctl_ed.pipe_no, TD_CC_DEVICENOTRESPONDING);
        }
        if (blk_ed.trans_wait == 1) {
            ohciwrapp_loc_TransEnd(blk_ed.pipe_no, TD_CC_DEVICENOTRESPONDING);
        }
        for (cnt = 0; cnt< INT_TRANS_MAX_NUM; cnt++) {
            if (int_ed[cnt].trans_wait == 1) {
                ohciwrapp_loc_TransEnd(int_ed[cnt].pipe_no, TD_CC_DEVICENOTRESPONDING);
            }
        }
#if (ISO_TRANS_MAX_NUM > 0)
        for (cnt = 0; cnt< ISO_TRANS_MAX_NUM; cnt++) {
            if (iso_ed[cnt].trans_wait == 1) {
                hced_t  *p_ed = iso_ed[cnt].p_curr_ed;

                p_ed->headTD |= ED_HALTED;
                ohciwrapp_loc_TransEnd(iso_ed[cnt].pipe_no, TD_CC_DEVICENOTRESPONDING);
            }
        }
#endif
    }
    (void)osSemaphoreRelease(semid_cb);
}

void ohciwrapp_loc_TransEnd(uint32_t pipe, uint32_t ConditionCode) {
    uint32_t     periodic = 0;
    uint32_t     cnt;
    uint32_t     sqmon;
    hced_t       *p_ed;
    genelal_ed_t *p_wait_ed = NULL;

    if (ctl_ed.pipe_no == pipe) {
        p_wait_ed = &ctl_ed;
    } else if (blk_ed.pipe_no == pipe) {
        p_wait_ed = &blk_ed;
    } else {
#if (ISO_TRANS_MAX_NUM > 0)
        if (p_wait_ed == NULL) {
            for (cnt = 0; cnt< ISO_TRANS_MAX_NUM; cnt++) {
                if (iso_ed[cnt].pipe_no == pipe) {
                    p_wait_ed = &iso_ed[cnt];
                    break;
                }
            }
        }
#endif
        if (p_wait_ed == NULL) {
            for (cnt = 0; cnt< INT_TRANS_MAX_NUM; cnt++) {
                if (int_ed[cnt].pipe_no == pipe) {
                    p_wait_ed = &int_ed[cnt];
                    periodic = 1;
                    break;
                }
            }
        }
    }

    if (p_wait_ed == NULL) {
        return;
    }
    p_ed  = p_wait_ed->p_curr_ed;
    if (p_ed == NULL) {
        return;
    }

    if ((p_ed->control & ED_FORMAT) == 0) {
        hctd_t    *p_td = (hctd_t *)p_wait_ed->p_curr_td;

        if (p_td != NULL) {
            if (ConditionCode == TD_CC_NOERROR) {
                /* ErrorCount */
                RZA_IO_RegWrite_32(&p_td->control, 0, TD_CTL_SHFT_EC, TD_CTL_MSK_EC);

                /* CurrentBufferPointer */
                p_td->currBufPtr += ((uint32_t)p_td->bufEnd - (uint32_t)p_td->currBufPtr + 1) - g_usbx_host_data_count[pipe];
            } else {
                /* ErrorCount */
                RZA_IO_RegWrite_32(&p_td->control, 3, TD_CTL_SHFT_EC, TD_CTL_MSK_EC);
            }

            /* DataToggle */
            sqmon = usbx_host_get_sqmon(pipe);
            RZA_IO_RegWrite_32(&p_td->control, sqmon, TD_CTL_SHFT_T, TD_CTL_MSK_T);
            if (sqmon == 0) {
                p_ed->headTD &= ~ED_TOGLE_CARRY;
            } else {
                p_ed->headTD |= ED_TOGLE_CARRY;
            }

            /* ConditionCode */
            RZA_IO_RegWrite_32(&p_td->control, ConditionCode, TD_CTL_SHFT_CC, TD_CTL_MSK_CC);

            if (p_wait_ed == &ctl_ed) {
                chk_split_trans_setting(&ctl_ed);
            }
            chk_genelal_td_done(p_wait_ed);

            if (periodic != 0) {
                if (chk_genelal_ed(p_wait_ed) != 0) {
                    int_trans(p_wait_ed);
                } else {
                    p_wait_ed->trans_wait = 0;
                    (void)osSemaphoreRelease(p_wait_ed->semid_wait);
                }
            } else {
                p_wait_ed->trans_wait = 0;
                (void)osSemaphoreRelease(p_wait_ed->semid_wait);
            }
        }
    } else {
#if (ISO_TRANS_MAX_NUM > 0)
        hcisotd_t *p_isotd = (hcisotd_t *)p_wait_ed->p_curr_td;
        uint32_t  next_trans = 0;

        if (p_isotd != NULL) {
            usbx_host_stop_transfer(pipe);
            if (p_usb_reg->HcFmNumber < 0x0000FFFF) {
                p_usb_reg->HcFmNumber++;
            } else {
                p_usb_reg->HcFmNumber = 0;
            }

            /* Size of packet */
            p_isotd->offsetPSW[p_wait_ed->psw_idx] -= g_usbx_host_data_count[pipe];

            /* ConditionCode */
            RZA_IO_RegWrite_32(&p_isotd->control, ConditionCode, TD_CTL_SHFT_CC, TD_CTL_MSK_CC);
            RZA_IO_RegWrite_16(&p_isotd->offsetPSW[p_wait_ed->psw_idx],
                               (uint16_t)ConditionCode, TD_PSW_SHFT_CC, TD_PSW_MSK_CC);

            if (usbx_host_CheckAttach() != USB_HOST_ATTACH) {
                p_ed->headTD  |= ED_HALTED;
            }
            if (p_wait_ed->psw_idx >= RZA_IO_RegRead_32(&p_isotd->control, TD_CTL_SHFT_FC, TD_CTL_MSK_FC)) {
                p_wait_ed->psw_idx = 0;
                chk_iso_td_done(p_wait_ed);
            } else {
                p_wait_ed->psw_idx++;
            }
            if (chk_iso_ed(p_wait_ed) != 0) {
                if (iso_chk_starting_frame(p_wait_ed) == 0) {
                    iso_trans(p_wait_ed);
                    next_trans = 1;
                }
            }
            if (next_trans == 0) {
                p_wait_ed->trans_wait = 0;
                (void)osSemaphoreRelease(p_wait_ed->semid_wait);
            }
        }
#endif
    }

}