summaryrefslogtreecommitdiff
path: root/SamsungPlatformPkg/ExynosPkg/Exynos5250/Drivers/OhciDxe/OhciSched.c
blob: a8f99a3d42c5c640704e59684e06c05bc2c62759 (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
/** @file

  The EHCI register operation routines.

Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution.  The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php

THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/

#include "Ohci.h"


/**
  Create Frame List Structure.

  @param  Ohc                    OHCI device.

  @retval EFI_OUT_OF_RESOURCES   Can't allocate memory resources.
  @retval EFI_UNSUPPORTED        Map memory fail.
  @retval EFI_SUCCESS            Success.

**/
EFI_STATUS
OhciInitFrameList (
  IN USB_HC_DEV         *Ohc
  )
{
  EFI_PHYSICAL_ADDRESS  MappedAddr;
  EFI_STATUS            Status;
  VOID                  *Buffer;
  VOID                  *Mapping;
  UINTN                 Pages;
  UINTN                 Bytes;
  UINTN                 Index;
  EFI_PHYSICAL_ADDRESS  PhyAddr;

  DEBUG((EFI_D_INIT, "+++OhciInitFrameList()\n"));

  //
  // The Frame List is a common buffer that will be
  // accessed by both the cpu and the usb bus master
  // at the same time. The Frame List ocupies 4K bytes,
  // and must be aligned on 4-Kbyte boundaries.
  //
  Bytes = 4096;
  Pages = EFI_SIZE_TO_PAGES (Bytes);

  Status = Ohc->PciIo->AllocateBuffer (
                         Ohc->PciIo,
                         AllocateAnyPages,
                         EfiBootServicesData,
                         Pages,
                         &Buffer,
                         0
                         );

  if (EFI_ERROR (Status)) {
    DEBUG((EFI_D_ERROR, "FAIL! AllocateBuffer\n"));
    return EFI_OUT_OF_RESOURCES;
  }

  Status = Ohc->PciIo->Map (
                         Ohc->PciIo,
                         EfiPciIoOperationBusMasterCommonBuffer,
                         Buffer,
                         &Bytes,
                         &MappedAddr,
                         &Mapping
                         );

#if 1
  if (EFI_ERROR (Status) || (Bytes != 4096)) {
    DEBUG((EFI_D_ERROR, "FAIL! Map\n"));
    Status = EFI_UNSUPPORTED;
    return Status;
  }

  Ohc->Hcca           = (UINT32 *) (UINTN) Buffer;
  Ohc->HccaMapping    = Mapping;

  OhciWriteReg(Ohc, HC_HCCA_OFFSET, (UINT32)Buffer);

  return EFI_SUCCESS;

#else
  if (EFI_ERROR (Status) || (Bytes != 4096)) {
    DEBUG((EFI_D_ERROR, "FAIL! Map\n"));
    Status = EFI_UNSUPPORTED;
    goto ON_ERROR;
  }


  Ohc->FrameBase           = (UINT32 *) (UINTN) Buffer;
  Ohc->FrameMapping        = Mapping;

  //
  // Tell the Host Controller where the Frame List lies,
  // by set the Frame List Base Address Register.
  //
  //OhciSetFrameListBaseAddr (Ohc->PciIo, (VOID *) (UINTN) MappedAddr);

  //
  // Allocate the QH used by sync interrupt/control/bulk transfer.
  // FS ctrl/bulk queue head is set to loopback so additional BW
  // can be reclaimed. Notice, LS don't support bulk transfer and
  // also doesn't support BW reclamation.
  //
  Ohc->SyncIntQh  = OhciCreateQh (Ohc, 1);
  Ohc->CtrlQh     = OhciCreateQh (Ohc, 1);
  Ohc->BulkQh     = OhciCreateQh (Ohc, 1);

  if ((Ohc->SyncIntQh == NULL) || (Ohc->CtrlQh == NULL) || (Ohc->BulkQh == NULL)) {
    Ohc->PciIo->Unmap (Ohc->PciIo, Mapping);
    DEBUG((EFI_D_ERROR, "FAIL! NULL1\n"));
    Status = EFI_OUT_OF_RESOURCES;
    goto ON_ERROR;
  }

  //
  //                                                +-------------+
  //                                                |             |
  // Link the three together: SyncIntQh->CtrlQh->BulkQh <---------+
  // Each frame entry is linked to this sequence of QH. These QH
  // will remain on the schedul, never got removed
  //
  PhyAddr = UsbHcGetPciAddressForHostMem (Ohc->MemPool, Ohc->CtrlQh, sizeof (OHCI_QH_HW));
  Ohc->SyncIntQh->QhHw.HorizonLink  = QH_HLINK (PhyAddr, FALSE);
  Ohc->SyncIntQh->NextQh            = Ohc->CtrlQh;

  PhyAddr = UsbHcGetPciAddressForHostMem (Ohc->MemPool, Ohc->BulkQh, sizeof (OHCI_QH_HW));
  Ohc->CtrlQh->QhHw.HorizonLink     = QH_HLINK (PhyAddr, FALSE);
  Ohc->CtrlQh->NextQh               = Ohc->BulkQh;

  //
  // Some old platform such as Intel's Tiger 4 has a difficult time
  // in supporting the full speed bandwidth reclamation in the previous
  // mentioned form. Most new platforms don't suffer it.
  //
  Ohc->BulkQh->QhHw.HorizonLink     = QH_HLINK (PhyAddr, FALSE);

  Ohc->BulkQh->NextQh               = NULL;

  Ohc->FrameBaseHostAddr = AllocateZeroPool (4096);
  if (Ohc->FrameBaseHostAddr == NULL) {
    DEBUG((EFI_D_ERROR, "FAIL! NULL2\n"));
    Status = EFI_OUT_OF_RESOURCES;
    goto ON_ERROR;
  }

  PhyAddr = UsbHcGetPciAddressForHostMem (Ohc->MemPool, Ohc->SyncIntQh, sizeof (OHCI_QH_HW));
  for (Index = 0; Index < OHCI_FRAME_NUM; Index++) {
    Ohc->FrameBase[Index] = QH_HLINK (PhyAddr, FALSE);
    Ohc->FrameBaseHostAddr[Index] = (UINT32)(UINTN)Ohc->SyncIntQh;
  }

  DEBUG((EFI_D_INIT, "---OhciInitFrameList()\n"));

  return EFI_SUCCESS;

ON_ERROR:
  if (Ohc->SyncIntQh != NULL) {
    UsbHcFreeMem (Ohc->MemPool, Ohc->SyncIntQh, sizeof (OHCI_QH_SW));
  }

  if (Ohc->CtrlQh != NULL) {
    UsbHcFreeMem (Ohc->MemPool, Ohc->CtrlQh, sizeof (OHCI_QH_SW));
  }

  if (Ohc->BulkQh != NULL) {
    UsbHcFreeMem (Ohc->MemPool, Ohc->BulkQh, sizeof (OHCI_QH_SW));
  }

  Ohc->PciIo->FreeBuffer (Ohc->PciIo, Pages, Buffer);
  return Status;
#endif
}


/**
  Destory FrameList buffer.

  @param  Ohc                    The OHCI device.

**/
VOID
OhciDestoryFrameList (
  IN USB_HC_DEV           *Ohc
  )
{
  //
  // Unmap the common buffer for framelist entry,
  // and free the common buffer.
  // Ohci's frame list occupy 4k memory.
  //
  DEBUG((EFI_D_INIT, "+++OhciDestoryFrameList()\n"));

#if 1
#else
  Ohc->PciIo->Unmap (Ohc->PciIo, Ohc->FrameMapping);

  Ohc->PciIo->FreeBuffer (
                Ohc->PciIo,
                EFI_SIZE_TO_PAGES (4096),
                (VOID *) Ohc->FrameBase
                );

  if (Ohc->FrameBaseHostAddr != NULL) {
    FreePool (Ohc->FrameBaseHostAddr);
  }

  if (Ohc->SyncIntQh != NULL) {
    UsbHcFreeMem (Ohc->MemPool, Ohc->SyncIntQh, sizeof (OHCI_QH_SW));
  }

  if (Ohc->CtrlQh != NULL) {
    UsbHcFreeMem (Ohc->MemPool, Ohc->CtrlQh, sizeof (OHCI_QH_SW));
  }

  if (Ohc->BulkQh != NULL) {
    UsbHcFreeMem (Ohc->MemPool, Ohc->BulkQh, sizeof (OHCI_QH_SW));
  }

  Ohc->FrameBase           = NULL;
  Ohc->FrameBaseHostAddr   = NULL;
  Ohc->SyncIntQh           = NULL;
  Ohc->CtrlQh              = NULL;
  Ohc->BulkQh              = NULL;
#endif
	DEBUG((EFI_D_INIT, "---OhciDestoryFrameList()\n"));
}


/**
  Convert the poll rate to the maxium 2^n that is smaller
  than Interval.

  @param  Interval               The poll rate to convert.

  @return The converted poll rate.

**/
UINTN
OhciConvertPollRate (
  IN  UINTN               Interval
  )
{
  UINTN                   BitCount;

  ASSERT (Interval != 0);

  //
  // Find the index (1 based) of the highest non-zero bit
  //
  BitCount = 0;

  while (Interval != 0) {
    Interval >>= 1;
    BitCount++;
  }

  return (UINTN)1 << (BitCount - 1);
}


/**
  Link a queue head (for asynchronous interrupt transfer) to
  the frame list.

  @param  Ohc                    The OHCI device.
  @param  Qh                     The queue head to link into.

**/
VOID
OhciLinkQhToFrameList (
  USB_HC_DEV              *Ohc,
  OHCI_QH_SW              *Qh
  )
{
  UINTN                   Index;
  OHCI_QH_SW              *Prev;
  OHCI_QH_SW              *Next;
  EFI_PHYSICAL_ADDRESS    PhyAddr;
  EFI_PHYSICAL_ADDRESS    QhPciAddr;

#if 1
#else
  ASSERT ((Ohc->FrameBase != NULL) && (Qh != NULL));

  QhPciAddr = UsbHcGetPciAddressForHostMem (Ohc->MemPool, Qh, sizeof (OHCI_QH_HW));

  for (Index = 0; Index < OHCI_FRAME_NUM; Index += Qh->Interval) {
    //
    // First QH can't be NULL because we always keep static queue
    // heads on the frame list
    //
    ASSERT (!LINK_TERMINATED (Ohc->FrameBase[Index]));
    Next  = (OHCI_QH_SW*)(UINTN)Ohc->FrameBaseHostAddr[Index];
    Prev  = NULL;

    //
    // Now, insert the queue head (Qh) into this frame:
    // 1. Find a queue head with the same poll interval, just insert
    //    Qh after this queue head, then we are done.
    //
    // 2. Find the position to insert the queue head into:
    //      Previous head's interval is bigger than Qh's
    //      Next head's interval is less than Qh's
    //    Then, insert the Qh between then
    //
    // This method is very much the same as that used by EHCI.
    // Because each QH's interval is round down to 2^n, poll
    // rate is correct.
    //
    while (Next->Interval > Qh->Interval) {
      Prev  = Next;
      Next  = Next->NextQh;
      ASSERT (Next != NULL);
    }

    //
    // The entry may have been linked into the frame by early insertation.
    // For example: if insert a Qh with Qh.Interval == 4, and there is a Qh
    // with Qh.Interval == 8 on the frame. If so, we are done with this frame.
    // It isn't necessary to compare all the QH with the same interval to
    // Qh. This is because if there is other QH with the same interval, Qh
    // should has been inserted after that at FrameBase[0] and at FrameBase[0] it is
    // impossible (Next == Qh)
    //
    if (Next == Qh) {
      continue;
    }

    if (Next->Interval == Qh->Interval) {
      //
      // If there is a QH with the same interval, it locates at
      // FrameBase[0], and we can simply insert it after this QH. We
      // are all done.
      //
      ASSERT ((Index == 0) && (Qh->NextQh == NULL));

      Prev                    = Next;
      Next                    = Next->NextQh;

      Qh->NextQh              = Next;
      Prev->NextQh            = Qh;

      Qh->QhHw.HorizonLink    = Prev->QhHw.HorizonLink;

      Prev->QhHw.HorizonLink  = QH_HLINK (QhPciAddr, FALSE);
      break;
    }

    //
    // OK, find the right position, insert it in. If Qh's next
    // link has already been set, it is in position. This is
    // guarranted by 2^n polling interval.
    //
    if (Qh->NextQh == NULL) {
      Qh->NextQh            = Next;
      PhyAddr = UsbHcGetPciAddressForHostMem (Ohc->MemPool, Next, sizeof (OHCI_QH_HW));
      Qh->QhHw.HorizonLink  = QH_HLINK (PhyAddr, FALSE);
    }

    if (Prev == NULL) {
      Ohc->FrameBase[Index]           = QH_HLINK (QhPciAddr, FALSE);
      Ohc->FrameBaseHostAddr[Index]   = (UINT32)(UINTN)Qh;
    } else {
      Prev->NextQh            = Qh;
      Prev->QhHw.HorizonLink  = QH_HLINK (QhPciAddr, FALSE);
    }
  }
#endif
}


/**
  Unlink QH from the frame list is easier: find all
  the precedence node, and pointer there next to QhSw's
  next.

  @param  Ohc                    The OHCI device.
  @param  Qh                     The queue head to unlink.

**/
VOID
OhciUnlinkQhFromFrameList (
  USB_HC_DEV              *Ohc,
  OHCI_QH_SW              *Qh
  )
{
  UINTN                   Index;
  OHCI_QH_SW              *Prev;
  OHCI_QH_SW              *This;
#if 1
#else
  ASSERT ((Ohc->FrameBase != NULL) && (Qh != NULL));

  for (Index = 0; Index < OHCI_FRAME_NUM; Index += Qh->Interval) {
    //
    // Frame link can't be NULL because we always keep static
    // queue heads on the frame list
    //
    ASSERT (!LINK_TERMINATED (Ohc->FrameBase[Index]));
    This  = (OHCI_QH_SW*)(UINTN)Ohc->FrameBaseHostAddr[Index];
    Prev  = NULL;

    //
    // Walk through the frame's QH list to find the
    // queue head to remove
    //
    while ((This != NULL) && (This != Qh)) {
      Prev  = This;
      This  = This->NextQh;
    }

    //
    // Qh may have already been unlinked from this frame
    // by early action.
    //
    if (This == NULL) {
      continue;
    }

    if (Prev == NULL) {
      //
      // Qh is the first entry in the frame
      //
      Ohc->FrameBase[Index]           = Qh->QhHw.HorizonLink;
      Ohc->FrameBaseHostAddr[Index]   = (UINT32)(UINTN)Qh->NextQh;
    } else {
      Prev->NextQh            = Qh->NextQh;
      Prev->QhHw.HorizonLink  = Qh->QhHw.HorizonLink;
    }
  }
#endif
}


/**
  Check TDs Results.

  @param  Ohc                    This OHCI device.
  @param  Td                     OHCI_TD_HW to check.
  @param  IsLow                  Is Low Speed Device.
  @param  QhResult               Return the result of this TD list.

  @return Whether the TD's result is finialized.

**/
BOOLEAN
OhciCheckTdStatus (
  IN  USB_HC_DEV          *Ohc,
  IN  OHCI_ED_HW          *Ed,
  IN  BOOLEAN             IsLow,
  OUT OHCI_QH_RESULT      *QhResult,
  IN  UINT8               Class
  )
{
  UINTN                   Len;
  UINT8                   State;
  BOOLEAN                 Finished;
  OHCI_TD_SW              *CurTdSw;
  OHCI_TD_HW              *TdHw;

  Finished             = TRUE;

  if(Class == HC_CLASS_CONTROL)
    CurTdSw = Ohc->CtrlQh;
  else // HC_CLASS_INTERRUPT
    CurTdSw = Ohc->IntrQh;

  TdHw = CurTdSw->TdHw;
  //
  // Initialize the data toggle to that of the first
  // TD. The next toggle to use is either:
  // 1. first TD's toggle if no TD is executed OK
  // 2. the next toggle of last executed-OK TD
  //
  QhResult->Result     = EFI_USB_NOERROR;
  QhResult->NextToggle = (UINT8)(TdHw->gtd_info.b.data_toggle & 1);
  QhResult->Complete   = 0;

#if 1
	if(Class == HC_CLASS_CONTROL)
	{
		while(Ohc->Hcca->done_head != Ohc->LastTd)
		{
			DEBUG((EFI_D_ERROR, ".\n"));
			OhciWriteReg(Ohc, HC_INT_STATUS_OFFSET, 0x2);
		}
	}
	else
	{
		OhciWriteReg(Ohc, HC_INT_STATUS_OFFSET, 0x2);
		if(Ohc->Hcca->done_head != Ohc->LastTd)
		{
			Finished = FALSE;
			QhResult->Complete = 0;
			return Finished;
		}
	}

	Ohc->Hcca->done_head = NULL;
	DEBUG((EFI_D_ERROR, ">\n"));

  while (TdHw != NULL) {
    if(TdHw->current_buf_ptr == NULL)
    {
			if(TdHw->gtd_info.b.error_count != 0)
			{
				QhResult->Result |= EFI_USB_ERR_NOTEXECUTE;
				DEBUG((EFI_D_ERROR, "FAIL! EXECUTE ERROR1\n"));
				Finished = TRUE;
				OhciDumpSWTds(Ohc->CtrlQh);
				OhcDumpRegs(Ohc);
				goto ON_EXIT;
			}

			if(TdHw->gtd_info.b.condition_code != 0)
			{
				switch(TdHw->gtd_info.b.condition_code)
				{
					case 4: //stall
						QhResult->Result |= EFI_USB_ERR_STALL;
						DEBUG((EFI_D_ERROR, "FAIL! EFI_USB_ERR_STALL\n"));
						break;
					default :
						QhResult->Result |= EFI_USB_ERR_NOTEXECUTE;
						DEBUG((EFI_D_ERROR, "FAIL! EXECUTE ERROR2\n"));
						break;
				}

				Finished = TRUE;
				OhciDumpSWTds(Ohc->CtrlQh);
				OhcDumpRegs(Ohc);
				goto ON_EXIT;
			}

			QhResult->NextToggle = (TdHw->gtd_info.b.data_toggle & 1) ? 0 : 1;
			//
	    // This TD is finished OK or met short packet read. Update the
	    // transfer length if it isn't a SETUP.
	    //

	    if (TdHw->gtd_info.b.pid != SETUP_PACKET_ID) {
	      QhResult->Complete += CurTdSw->DataLen;
	    }

	    //
	    // Short packet condition for full speed input TD, also
	    // terminate the transfer
	    //
	    /*
	    if (!IsLow && (TdHw->ShortPacket == 1) && (Len < Td->DataLen)) {
	      DEBUG ((EFI_D_INFO, "OhciCheckTdStatus: short packet read occured\n"));

	      Finished = TRUE;
	      goto ON_EXIT;
	    }*/
		} // if(TdHw->current_buf_ptr == NULL)
		/*else
		{
			QhResult->Result |= EFI_USB_ERR_NOTEXECUTE;
			DEBUG((EFI_D_ERROR, "FAIL! EXECUTE ERROR3\n"));
			Finished = TRUE;
			goto ON_EXIT;
		}*/
    CurTdSw = CurTdSw->NextTd;
		TdHw = CurTdSw->TdHw;
  }

	if(!Finished)
	{
		OhciDumpSWTds(Ohc->CtrlQh);
		OhcDumpRegs(Ohc);
	}
#else
  while (Td != NULL) {
    TdHw  = &Td->TdHw;
    State = (UINT8)TdHw->Status;

    //
    // OHCI will set STALLED bit when it abort the execution
    // of TD list. There are several reasons:
    //   1. BABBLE error happened
    //   2. Received a STALL response
    //   3. Error count decreased to zero.
    //
    // It also set CRC/Timeout/NAK/Buffer Error/BitStuff Error
    // bits when corresponding conditions happen. But these
    // conditions are not deadly, that is a TD can successfully
    // completes even these bits are set. But it is likely that
    // upper layer won't distinguish these condtions. So, only
    // set these bits when TD is actually halted.
    //
    if ((State & USBTD_STALLED) != 0) {
      if ((State & USBTD_BABBLE) != 0) {
        QhResult->Result |= EFI_USB_ERR_BABBLE;

      } else if (TdHw->ErrorCount != 0) {
        QhResult->Result |= EFI_USB_ERR_STALL;
      }

      if ((State & USBTD_CRC) != 0) {
        QhResult->Result |= EFI_USB_ERR_CRC;
      }

      if ((State & USBTD_BUFFERR) != 0) {
        QhResult->Result |= EFI_USB_ERR_BUFFER;
      }

      if ((Td->TdHw.Status & USBTD_BITSTUFF) != 0) {
        QhResult->Result |= EFI_USB_ERR_BITSTUFF;
      }

      if (TdHw->ErrorCount == 0) {
        QhResult->Result |= EFI_USB_ERR_TIMEOUT;
      }

      Finished = TRUE;
      goto ON_EXIT;

    } else if ((State & USBTD_ACTIVE) != 0) {
      //
      // The TD is still active, no need to check further.
      //
      QhResult->Result |= EFI_USB_ERR_NOTEXECUTE;

      Finished = FALSE;
      goto ON_EXIT;

    } else {
      //
      // Update the next data toggle, it is always the
      // next to the last known-good TD's data toggle if
      // any TD is executed OK
      //
      QhResult->NextToggle = (UINT8) (1 - (UINT8)TdHw->DataToggle);

      //
      // This TD is finished OK or met short packet read. Update the
      // transfer length if it isn't a SETUP.
      //
      Len = (TdHw->ActualLen + 1) & 0x7FF;

      if (TdHw->PidCode != SETUP_PACKET_ID) {
        QhResult->Complete += Len;
      }

      //
      // Short packet condition for full speed input TD, also
      // terminate the transfer
      //
      if (!IsLow && (TdHw->ShortPacket == 1) && (Len < Td->DataLen)) {
        DEBUG ((EFI_D_INFO, "OhciCheckTdStatus: short packet read occured\n"));

        Finished = TRUE;
        goto ON_EXIT;
      }
    }

    Td = Td->NextTd;
  }
#endif

ON_EXIT:
  //
  // Check whether HC is halted. Don't move this up. It must be
  // called after data toggle is successfully updated.
  //
  if (!OhciIsHcWorking (Ohc->PciIo)) {
    QhResult->Result |= EFI_USB_ERR_SYSTEM;
    Finished  = TRUE;
  }

  if (Finished) {
    Ohc->PciIo->Flush (Ohc->PciIo);
  }

  OhciAckAllInterrupt (Ohc);
  return Finished;
}


/**
  Check the result of the transfer.

  @param  Ohc                    The OHCI device.
  @param  Qh                     The queue head of the transfer.
  @param  Td                     The first TDs of the transfer.
  @param  TimeOut                TimeOut value in milliseconds.
  @param  IsLow                  Is Low Speed Device.
  @param  QhResult               The variable to return result.

  @retval EFI_SUCCESS            The transfer finished with success.
  @retval EFI_DEVICE_ERROR       Transfer failed.

**/
EFI_STATUS
OhciExecuteTransfer (
  IN  USB_HC_DEV          *Ohc,
  IN  OHCI_ED_HW          *Ed,
  IN  UINTN               TimeOut,
  IN  BOOLEAN             IsLow,
  OUT OHCI_QH_RESULT      *QhResult
  )
{
  UINTN                   Index;
  UINTN                   Delay;
  BOOLEAN                 Finished;
  EFI_STATUS              Status;

  DEBUG((EFI_D_INIT, "+++OhciExecuteTransfer()\n"));

  Finished = FALSE;
  Status   = EFI_SUCCESS;
  Delay    = (TimeOut * OHC_1_MILLISECOND / OHC_SYNC_POLL_INTERVAL) + 1;

  for (Index = 0; Index < Delay; Index++) {
    Finished = OhciCheckTdStatus (Ohc, Ed, IsLow, QhResult, HC_CLASS_CONTROL);

    //
    // Transfer is OK or some error occured (TD inactive)
    //
    if (Finished) {
      DEBUG((EFI_D_INIT, "SUCCESS! OhciExecuteTransfer\n"));
      break;
    }

    gBS->Stall (OHC_SYNC_POLL_INTERVAL);
  }

  if (!Finished) {
    DEBUG ((EFI_D_ERROR, "OhciExecuteTransfer: execution not finished for %dms\n", (UINT32)TimeOut));
    //OhciDumpQh (Qh);
    //OhciDumpTds (Td);

    Status = EFI_TIMEOUT;

  } else if (QhResult->Result != EFI_USB_NOERROR) {
    DEBUG ((EFI_D_ERROR, "OhciExecuteTransfer: execution failed with result %x\n", QhResult->Result));
    //OhciDumpQh (Qh);
    //OhciDumpTds (Td);

    Status = EFI_DEVICE_ERROR;
  }

	DEBUG((EFI_D_INIT, "---OhciExecuteTransfer()\n"));

  return Status;
}


/**
  Update Async Request, QH and TDs.

  @param  Ohc                    The OHCI device.
  @param  AsyncReq               The OHCI asynchronous transfer to update.
  @param  Result                 Transfer reslut.
  @param  NextToggle             The toggle of next data.

**/
VOID
OhciUpdateAsyncReq (
  IN USB_HC_DEV          *Ohc,
  IN OHCI_ASYNC_REQUEST  *AsyncReq,
  IN UINT32              Result,
  IN UINT32              NextToggle
  )
{
  OHCI_ED_HW              *EdHw;
  OHCI_TD_SW              *FirstTd;
  OHCI_TD_SW              *Td;
	OHCI_TD_HW              *TdHw;

	UINT8 *DataPtr, *DataPhy;

	//DEBUG((EFI_D_INIT, "+++OhciUpdateAsyncReq()\n"));

  EdHw        = AsyncReq->EdHw;
  FirstTd     = AsyncReq->TdSw;

  if (Result == EFI_USB_NOERROR) {
    //
    // The last transfer succeeds. Then we need to update
    // the Qh and Td for next round of transfer.
    // 1. Update the TD's data toggle
    // 2. Activate all the TDs
    // 3. Link the TD to the queue head again since during
    //    execution, queue head's TD pointer is changed by
    //    hardware.
    //

    for (Td = FirstTd; Td != NULL; Td = Td->NextTd) {
			TdHw = Td->TdHw;
			if(TdHw != NULL)
			{
			  //
			  // Allocate and map source data buffer for bus master access.
			  //
			  DataPtr = UsbHcAllocateMem (Ohc->MemPool, Td->DataLen);

			  if (DataPtr == NULL) {
					DEBUG((EFI_D_ERROR, "FAIL! UsbHcAllocateMem\n"));
			    return;
			  }

			  DataPhy = (UINT8 *) (UINTN) UsbHcGetPciAddressForHostMem (Ohc->MemPool, DataPtr, Td->DataLen);

				Ohc->Destory = Td->Data;
				Ohc->DestroySize = Td->DataLen;
				TdHw->current_buf_ptr = DataPhy;
				TdHw->buffer_end = DataPhy + Td->DataLen - 1;
      	TdHw->gtd_info.b.data_toggle = NextToggle + 2;

				if(Td->NextTd)
				{
					TdHw->next_td = Td->NextTd->TdHw;
				}

	      NextToggle = NextToggle ? 0 : 1;
			}
    }

		EdHw->head_td_ptr = FirstTd->TdHw;

		//OhciDumpEd(EdHw);

    OhciLinkTdToQh (Ohc, EdHw, HC_CLASS_INTERRUPT);

		//DEBUG((EFI_D_INIT, "---OhciUpdateAsyncReq()\n"));

    return ;
  }
}


/**
  Create Async Request node, and Link to List.

  @param  Ohc                    The OHCI device.
  @param  Qh                     The queue head of the transfer.
  @param  FirstTd                First TD of the transfer.
  @param  DevAddr                Device Address.
  @param  EndPoint               EndPoint Address.
  @param  DataLen                Data length.
  @param  Interval               Polling Interval when inserted to frame list.
  @param  Data                   Data buffer, unmapped.
  @param  Callback               Callback after interrupt transfeer.
  @param  Context                Callback Context passed as function parameter.
  @param  IsLow                  Is Low Speed.

  @retval EFI_SUCCESS            An asynchronous transfer is created.
  @retval EFI_INVALID_PARAMETER  Paremeter is error.
  @retval EFI_OUT_OF_RESOURCES   Failed because of resource shortage.

**/
EFI_STATUS
OhciCreateAsyncReq (
  IN USB_HC_DEV                       *Ohc,
  IN OHCI_ED_HW                       *EdHw,
  IN OHCI_TD_SW                       *TdSw,
  IN UINT8                            DevAddr,
  IN UINT8                            EndPoint,
  IN UINTN                            DataLen,
  IN UINTN                            Interval,
  IN UINT8                            *Data,
  IN EFI_ASYNC_USB_TRANSFER_CALLBACK  Callback,
  IN VOID                             *Context,
  IN BOOLEAN                          IsLow
  )
{
  OHCI_ASYNC_REQUEST      *AsyncReq;

	DEBUG((EFI_D_INIT, "+++OhciCreateAsyncReq()\n"));

  AsyncReq = AllocatePool (sizeof (OHCI_ASYNC_REQUEST));

  if (AsyncReq == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }

  //
  // Fill Request field. Data is allocated host memory, not mapped
  //
  AsyncReq->Signature   = OHCI_ASYNC_INT_SIGNATURE;
  AsyncReq->DevAddr     = DevAddr;
  AsyncReq->EndPoint    = EndPoint;
  AsyncReq->DataLen     = DataLen;
  AsyncReq->Interval    = OhciConvertPollRate(Interval);
  AsyncReq->Data        = Data;
  AsyncReq->Callback    = Callback;
  AsyncReq->Context     = Context;
  AsyncReq->EdHw        = EdHw;
  AsyncReq->TdSw				= TdSw;
  AsyncReq->IsLow       = IsLow;

  //
  // Insert the new interrupt transfer to the head of the list.
  // The interrupt transfer's monitor function scans the whole
  // list from head to tail. The new interrupt transfer MUST be
  // added to the head of the list.
  //
  InsertHeadList (&(Ohc->AsyncIntList), &(AsyncReq->Link));

	DEBUG((EFI_D_INIT, "---OhciCreateAsyncReq()\n"));

  return EFI_SUCCESS;
}


/**
  Free an asynchronous request's resource such as memory.

  @param  Ohc                    The OHCI device.
  @param  AsyncReq               The asynchronous request to free.

**/
VOID
OhciFreeAsyncReq (
  IN USB_HC_DEV           *Ohc,
  IN OHCI_ASYNC_REQUEST   *AsyncReq
  )
{
  ASSERT ((Ohc != NULL) && (AsyncReq != NULL));

	DEBUG((EFI_D_ERROR, "+++OhciFreeAsyncReq()\n"));

  OhciDestoryTds (Ohc, AsyncReq->TdSw);

  if (AsyncReq->Data != NULL) {
    UsbHcFreeMem (Ohc->MemPool, AsyncReq->Data, AsyncReq->DataLen);
  }

  gBS->FreePool (AsyncReq);

	DEBUG((EFI_D_ERROR, "+++OhciFreeAsyncReq()\n"));
}


/**
  Unlink an asynchronous request's from OHC's asynchronus list.
  also remove the queue head from the frame list. If FreeNow,
  release its resource also. Otherwise, add the request to the
  OHC's recycle list to wait for a while before release the memory.
  Until then, hardware won't hold point to the request.

  @param  Ohc                    The OHCI device.
  @param  AsyncReq               The asynchronous request to free.
  @param  FreeNow                If TRUE, free the resource immediately, otherwise
                                 add the request to recycle wait list.

**/
VOID
OhciUnlinkAsyncReq (
  IN USB_HC_DEV           *Ohc,
  IN OHCI_ASYNC_REQUEST   *AsyncReq,
  IN BOOLEAN              FreeNow
  )
{
  ASSERT ((Ohc != NULL) && (AsyncReq != NULL));

	DEBUG((EFI_D_INIT, "+++OhciUnlinkAsyncReq()\n"));
#if 1
#else
  RemoveEntryList (&(AsyncReq->Link));
  OhciUnlinkQhFromFrameList (Ohc, AsyncReq->QhSw);

  if (FreeNow) {
    OhciFreeAsyncReq (Ohc, AsyncReq);
  } else {
    //
    // To sychronize with hardware, mark the queue head as inactive
    // then add AsyncReq to OHC's recycle list
    //
    AsyncReq->QhSw->QhHw.VerticalLink = QH_VLINK (NULL, TRUE);
    AsyncReq->Recycle = Ohc->RecycleWait;
    Ohc->RecycleWait  = AsyncReq;
  }
#endif
	DEBUG((EFI_D_INIT, "---OhciUnlinkAsyncReq()\n"));
}


/**
  Delete Async Interrupt QH and TDs.

  @param  Ohc                    The OHCI device.
  @param  DevAddr                Device Address.
  @param  EndPoint               EndPoint Address.
  @param  Toggle                 The next data toggle to use.

  @retval EFI_SUCCESS            The request is deleted.
  @retval EFI_INVALID_PARAMETER  Paremeter is error.
  @retval EFI_NOT_FOUND          The asynchronous isn't found.

**/
EFI_STATUS
OhciRemoveAsyncReq (
  IN  USB_HC_DEV          *Ohc,
  IN  UINT8               DevAddr,
  IN  UINT8               EndPoint,
  OUT UINT8               *Toggle
  )
{
  EFI_STATUS          Status;
  OHCI_ASYNC_REQUEST  *AsyncReq;
  OHCI_QH_RESULT      QhResult;
  LIST_ENTRY          *Link;
  BOOLEAN             Found;

  Status = EFI_SUCCESS;

  //
  // If no asynchronous interrupt transaction exists
  //
  if (IsListEmpty (&(Ohc->AsyncIntList))) {
    return EFI_SUCCESS;
  }

  //
  // Find the asynchronous transfer to this device/endpoint pair
  //
  Found = FALSE;
  Link  = Ohc->AsyncIntList.ForwardLink;

  do {
    AsyncReq  = OHCI_ASYNC_INT_FROM_LINK (Link);
    Link      = Link->ForwardLink;

    if ((AsyncReq->DevAddr == DevAddr) && (AsyncReq->EndPoint == EndPoint)) {
      Found = TRUE;
      break;
    }

  } while (Link != &(Ohc->AsyncIntList));

  if (!Found) {
    return EFI_NOT_FOUND;
  }

  //
  // Check the result of the async transfer then update it
  // to get the next data toggle to use.
  //
  OhciCheckTdStatus (Ohc, AsyncReq->EdHw, AsyncReq->IsLow, &QhResult, HC_CLASS_INTERRUPT);
  *Toggle = QhResult.NextToggle;

  //
  // Don't release the request now, keep it to synchronize with hardware.
  //
  OhciUnlinkAsyncReq (Ohc, AsyncReq, FALSE);
  return Status;
}


/**
  Recycle the asynchronouse request. When a queue head
  is unlinked from frame list, host controller hardware
  may still hold a cached pointer to it. To synchronize
  with hardware, the request is released in two steps:
  first it is linked to the OHC's RecycleWait list. At
  the next time OhciMonitorAsyncReqList is fired, it is
  moved to OHC's Recylelist. Then, at another timer
  activation, all the requests on Recycle list is freed.
  This guarrantes that each unlink queue head keeps
  existing for at least 50ms, far enough for the hardware
  to clear its cache.

  @param  Ohc                    The OHCI device.

**/
VOID
OhciRecycleAsyncReq (
  IN USB_HC_DEV           *Ohc
  )
{
  OHCI_ASYNC_REQUEST      *Req;
  OHCI_ASYNC_REQUEST      *Next;

  Req = Ohc->Recycle;

  while (Req != NULL) {
    Next = Req->Recycle;
    OhciFreeAsyncReq (Ohc, Req);
    Req  = Next;
  }

  Ohc->Recycle     = Ohc->RecycleWait;
  Ohc->RecycleWait = NULL;
}



/**
  Release all the asynchronous transfers on the lsit.

  @param  Ohc                    The OHCI device.

**/
VOID
OhciFreeAllAsyncReq (
  IN USB_HC_DEV           *Ohc
  )
{
  LIST_ENTRY              *Head;
  OHCI_ASYNC_REQUEST      *AsyncReq;

	DEBUG((EFI_D_INIT, "+++OhciFreeAllAsyncReq()\n"));

  //
  // Call OhciRecycleAsyncReq twice. The requests on Recycle
  // will be released at the first call; The requests on
  // RecycleWait will be released at the second call.
  //
  OhciRecycleAsyncReq (Ohc);
  OhciRecycleAsyncReq (Ohc);

  Head = &(Ohc->AsyncIntList);

  if (IsListEmpty (Head)) {
    return;
  }

  while (!IsListEmpty (Head)) {
    AsyncReq  = OHCI_ASYNC_INT_FROM_LINK (Head->ForwardLink);
    OhciUnlinkAsyncReq (Ohc, AsyncReq, TRUE);
  }

	DEBUG((EFI_D_INIT, "---OhciFreeAllAsyncReq()\n"));
}


/**
  Interrupt transfer periodic check handler.

  @param  Event                  The event of the time.
  @param  Context                Context of the event, pointer to USB_HC_DEV.

**/
VOID
EFIAPI
OhciMonitorAsyncReqList (
  IN EFI_EVENT            Event,
  IN VOID                 *Context
  )
{
  OHCI_ASYNC_REQUEST      *AsyncReq;
  LIST_ENTRY              *Link;
  USB_HC_DEV              *Ohc;
  VOID                    *Data;
  BOOLEAN                 Finished;
  OHCI_QH_RESULT          QhResult;
	UINT32									Inputs;

	DEBUG((EFI_D_INFO, "++%a : %d\n", __FUNCTION__, __LINE__));

  Ohc = (USB_HC_DEV *) Context;

  //
  // Recycle the asynchronous requests expired, and promote
  // requests waiting to be recycled the next time when this
  // timer expires
  //

	if(Ohc->Destory)
	{
	  DEBUG((EFI_D_ERROR, "%a (Destoryed) : %d\n", __FUNCTION__, __LINE__));
		UsbHcFreeMem (Ohc->MemPool, Ohc->Destory, Ohc->DestroySize);
		Ohc->Destory = NULL;
		Ohc->DestroySize = 0;
	}
	//OhciRecycleAsyncReq (Ohc);

  if (IsListEmpty (&(Ohc->AsyncIntList))) {
	  DEBUG((EFI_D_ERROR, "%a (IsListEmpty) : %d\n", __FUNCTION__, __LINE__));
    return ;
  }

  //
  // This loop must be delete safe
  //
  Link = Ohc->AsyncIntList.ForwardLink;

  do {
    AsyncReq  = OHCI_ASYNC_INT_FROM_LINK (Link);
    Link      = Link->ForwardLink;

    Finished = OhciCheckTdStatus (Ohc, AsyncReq->EdHw, AsyncReq->IsLow, &QhResult, HC_CLASS_INTERRUPT);

    if (!Finished) {
      continue;
    }

    //
    // Copy the data to temporary buffer if there are some
    // data transferred. We may have zero-length packet
    //
    Data = NULL;

    if (QhResult.Complete != 0) {
      Data = AllocatePool (QhResult.Complete);

      if (Data == NULL) {
        return ;
      }

      CopyMem (Data, AsyncReq->TdSw->Data, QhResult.Complete);

      Inputs = QhResult.Complete;
      DEBUG((EFI_D_INIT, "INPUT : "));
      while(Inputs--)
      {
        DEBUG((EFI_D_INIT, "0x%02X ", ((UINT8*)Data)[QhResult.Complete - Inputs -1]));
      }
      DEBUG((EFI_D_INIT, "\n"));
    }

    OhciUpdateAsyncReq (Ohc, AsyncReq, QhResult.Result, QhResult.NextToggle);

    //
    // Now, either transfer is SUCCESS or met errors since
    // we have skipped to next transfer earlier if current
    // transfer is still active.
    //
    if (QhResult.Result == EFI_USB_NOERROR) {
      AsyncReq->Callback (Data, QhResult.Complete, AsyncReq->Context, QhResult.Result);
    } else {
      //
      // Leave error recovery to its related device driver.
      // A common case of the error recovery is to re-submit
      // the interrupt transfer. When an interrupt transfer
      // is re-submitted, its position in the linked list is
      // changed. It is inserted to the head of the linked
      // list, while this function scans the whole list from
      // head to tail. Thus, the re-submitted interrupt transfer's
      // callback function will not be called again in this round.
      //
      AsyncReq->Callback (NULL, 0, AsyncReq->Context, QhResult.Result);
    }

    if (Data != NULL) {
      gBS->FreePool (Data);
    }
  } while (Link != &(Ohc->AsyncIntList));

	DEBUG((EFI_D_INFO, "--%a : %d\n", __FUNCTION__, __LINE__));
}