summaryrefslogtreecommitdiff
path: root/Core/EM/UsbRecovery/UsbPeimSrc/UsbPeim.c
blob: 87afc7878082c216354c37ddf8c0ebbc0d6055dd (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
//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2008, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**         5555 Oakbrook Pkwy, Suite 200, Norcross, GA 30093        **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************
//**********************************************************************
//
// $Header: /Alaska/SOURCE/Modules/USBRecovery/UsbPeimSrc/UsbPeim.c 17    11/24/12 5:46a Ryanchou $
//
// $Revision: 17 $
//
// $Date: 11/24/12 5:46a $
//
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/USBRecovery/UsbPeimSrc/UsbPeim.c $
// 
// 17    11/24/12 5:46a Ryanchou
// [TAG]  		EIP103990
// [Category]  	Improvement
// [Description]  	Synchronized with USB PEI module 4.6.3_USB_08.10.24.
// [Files]  		EhciPei.c, EhciPei.h, OhciPei.c, OhciPei.h, UhcPeim.c,
// BotPeim.c, BotPeim.h, PeiAtapi.c, UsbBotPeim.c, UsbBotPeim.h,
// HubPeim.c, UsbPeim.c, XhciPei.c, XhciPei.h, HubPeim.h, PeiUsbLib.c,
// PeiUsbLib.h, UsbPeim.h
// 
// 16    8/23/12 10:01p Wilsonlee
// [TAG]  		EIP97069
// [Category]  	Improvement
// [Description]  	Reset root port algorythm update.
// [Files]  		EhciPei.c, EhciPei.h, OhciPei.c, OhciPei.h, UhcPeim.c,
// UhcPeim.h, UsbPeim.c, usb.h
// 
// 15    1/18/11 1:06a Ryanchou
// [TAG]  		EIP47931
// [Category]  	Improvement
// [Description]  	Added USB 3.0 hub support.
// [Files]  		EhciPei.c, EhciPei.h, HubPeim.c, HubPeim.h, OhciPei.c,
// OhciPei.h, UhcPeim.c, UhcPeim.h, usb.h, UsbHostController.h,
// UsbIoPeim.c, UsbPeim.c, UsbPeim.h, XhciPei.c, XhciPei.h
// 
// 14    10/22/10 2:55a Rameshr
// [TAG]- EIP 43687
// [Category]-IMPROVEMENT
// [Description]- Build warning from UsbRecovery driver -
// UsbHostController.obj : warning LNK4221: no public symbols found;
// archive member will be inaccessible. 
// [Files]- uhcpeim.c, usbpeim.c, UsbHostcontroller.h
// 
// 13    10/12/10 11:20a Olegi
// XHCI support added.
// 
// 12    1/06/10 11:23a Olegi
// Fix for EIP32503: The Interface descriptor of some devices is not
// directly after the configuration descriptor, and the first Endpoint
// descriptor is not directly after the Interface either. Changed the
// descriptior data parser.
// 
// 11    10/20/09 9:17a Olegi
// EIP#28255: Set proper TransactionTranslator field for a non-hispeed USB
// device.
// 
// 10    3/17/09 5:07p Olegi
// Added TransactionTranslator for slow/full speed devices behind USB2
// hub.
// 
// 9     10/21/08 5:57p Michaela
// Added EHCI-related fixes for issues 
// that may occur if EHCI is used before 
// USB Recovery is invoked:
//   Added SDL Tokens: 
//     PEI_EHCI_PCI_BDFS and 
//     PEI_EHCI_MEM_BASE_ADDRESSES. 
// 
// Removed/modified some debugging 
// development code:
//   Removed SDL Tokens: 
//     USBR_DEBUG_SUPPORT and 
//     USBR_SERIAL_PORT_AVAILABLE 
//   Removed Elinks: 
//     UsbRecoveryDebug_DIR and 
//     $(BUILD_DIR)\UsbRecoveryDebugDxe.ffs 
//   Modified SDL Token: 
//     FORCE_RECOVERY to default value of 0. 
// 
// (See this module's Help documentation 
// for more information.)
// 
// 8     7/29/08 5:52p Michaela
// 1) Updated code to move most porting tasks to SDL
// 2) Added more debug break points and improved interactive
//     debugging capability for when a serial port is not available.
// 3) Updated help files
// 
// 7     7/18/08 5:05p Michaela
// 1  File-level debugging is now available
// 2  AMI_USB_DEBUG_INTERFACE.WaitConsoleKey() now returns
//    the keypress so that conditional debugging can
//    be dynamic (alphanumeric keys only)
// 3  Added more function headers.
// 4  Removed code that will never be used (I.e., Bala?).
// 5  Moved typedef, contants and extern declarations
//    into header files.
// 6  Now all controller blocks are enabled for SB700
//    (EHCI controllers route to companion controller
//    by default)
// 7  Removed unused constants and typedefs n OhciPei.h
//    (also reorganized the file to make it more 
//    readable.)
// 8  Renamed many functions/variables according to
//    coding standard.
// 9  Removed code initializing data structures for
//    periodic lists, as this is not needed.
// 10 Removed the CONTROLLER_TYPE SDL token to
//    allow UHCI and OHCI controllers to supported
//    simultaneously. (modified MAKE files 
//    accordingly)
// 
// 6     7/10/08 6:37p Michaela
// Updated to support OHCI controllers
//
// 5     10/23/07 5:41p Ambikas
//
// 4     8/17/07 4:13p Ambikas
//
// 3     4/16/07 12:55p Sivagarn
// - Updated as per coding standard review
// - In previous check-in, TIANO.H file is removed and AMIMAPPING.H file
// is included
//
// 2     3/28/07 3:36a Meenakshim
//
// 1     9/22/06 12:19p Sivagarn
// - Initial Checkin
// - Included Recovery code in Source
//
//*****************************************************************************

//<AMI_FHDR_START>
//----------------------------------------------------------------------------
//
// Name:        UsbPeim.C
//
// Description: This file belongs to "Framework".
//              This file is modified by AMI to include copyright message,
//              appropriate header and integration code.
//              This file contains generic routines needed for USB recovery
//              PEIM
//
//----------------------------------------------------------------------------
//<AMI_FHDR_END>

/*++
   This file contains 'Framework Code' and is licensed as such
   under the terms of your license agreement with Intel or your
   vendor.  This file may not be modified, except as allowed by
   additional terms of your license agreement.
   --*/

/*++

   Copyright (c)  1999 - 2002 Intel Corporation. All rights reserved
   This software and associated documentation (if any) is furnished
   under a license and may only be used or copied in accordance
   with the terms of the license. Except as permitted by such
   license, no part of this software or documentation may be
   reproduced, stored in a retrieval system, or transmitted in any
   form or by any means without the express written consent of
   Intel Corporation.


   Module Name:

   UsbPeim.c

   Abstract:

   Usb Bus PPI

   --*/

#include "UsbPeim.h"
#include "HubPeim.h"
#include "PeiUsbLib.h"


#include EFI_PPI_DEFINITION (Stall)
#include EFI_PPI_DEFINITION (LoadFile)

EFI_GUID gPeiUsbIoPpiGuid = PEI_USB_IO_PPI_GUID;

extern EFI_GUID gPeiStallPpiGuid;


#define PAGESIZE  4096

//static BOOLEAN  mImageInMemory = FALSE;


//
// Driver Entry Point
//


EFI_STATUS
PeimInitializeUsb (
    IN EFI_FFS_FILE_HEADER *FfsHeader,
    IN EFI_PEI_SERVICES    **PeiServices );

//
// UsbIo PPI interface function
//
STATIC PEI_USB_IO_PPI         mUsbIoPpi = {
    PeiUsbControlTransfer,
    PeiUsbBulkTransfer,
    PeiUsbGetInterfaceDescriptor,
    PeiUsbGetEndpointDescriptor,
    PeiUsbPortReset
};

STATIC EFI_PEI_PPI_DESCRIPTOR mUsbIoPpiList = {
    (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
    &gPeiUsbIoPpiGuid,
    NULL
};

//
// Helper functions
//
STATIC
EFI_STATUS
PeiUsbEnumeration (
    IN EFI_PEI_SERVICES            **PeiServices,
    IN PEI_USB_HOST_CONTROLLER_PPI *UsbHcPpi );

STATIC
EFI_STATUS
PeiConfigureUsbDevice (
    IN EFI_PEI_SERVICES **PeiServices,
    IN PEI_USB_DEVICE   *PeiUsbDevice,
    IN UINT8            Port,
    IN OUT UINT8        *DeviceAddress );

STATIC
EFI_STATUS
PeiUsbGetAllConfiguration (
    IN EFI_PEI_SERVICES **PeiServices,
    IN PEI_USB_DEVICE   *PeiUsbDevice );


VOID
ResetRootPort (
    IN EFI_PEI_SERVICES            **PeiServices,
    IN PEI_USB_HOST_CONTROLLER_PPI *UsbHcPpi,
    UINT8                          PortNum );

extern VOID ZeroMem (
    IN VOID  *Buffer,
    IN UINTN Size );



//
// PEIM Entry Point
//
// EFI_PEIM_ENTRY_POINT (PeimInitializeUsb);

EFI_STATUS PeimInitializeUsb (
    IN EFI_FFS_FILE_HEADER *FfsHeader,
    IN EFI_PEI_SERVICES    **PeiServices )
{
    EFI_STATUS Status;
    UINTN      i;
    PEI_USB_HOST_CONTROLLER_PPI *UsbHcPpi;

    // Assign resources and enable UHCI controllers
    //
    // when the image is in flash, the global variable
    // mImageInMemory could not be modified;
    // when the image is successfully loaded into memory,
    // mImageInMemory could be modified to TRUE.
    //

    i = 0;
    while (TRUE) {
        //
        // Get UsbHcPpi at first.
        //
        Status = (**PeiServices).LocatePpi( PeiServices,
            &gPeiUsbHostControllerPpiGuid, i, NULL, &UsbHcPpi );

        if ( EFI_ERROR( Status ) ) {
            break;
        }

        PeiUsbEnumeration( PeiServices, UsbHcPpi ); // 0xff06
        i++;
    }

    return EFI_SUCCESS;
}


STATIC
EFI_STATUS PeiHubEnumeration (
    IN EFI_PEI_SERVICES **PeiServices,
    IN PEI_USB_DEVICE   *PeiUsbDevice,
    IN UINT8            *CurrentAddress )
//
// Since we are not going to support hot-plug, we will do a
// dedicated polling process to get all ports change event
// discovered. This can help to avoid introduce interrupt
// transfer into the system, for each hub, we will wait for
// 3s and if one port is enabled we will not wait for others
// We will start parsing each of them in sequence.
//
{
    UINTN                Delay;
    UINT8                Port;
    EFI_STATUS           Status;
    PEI_USB_IO_PPI       *UsbIoPpi;
    EFI_USB_PORT_STATUS  PortStatus;
    BOOLEAN              PortChanged;
	UINT8                *AllocateAddress;
    PEI_USB_DEVICE       *NewPeiUsbDevice;
    PEI_STALL_PPI        *StallPpi;


    PortChanged = FALSE;

    UsbIoPpi = &PeiUsbDevice->UsbIoPpi;
    Status = (**PeiServices).LocatePpi( PeiServices, &gPeiStallPpiGuid,
        0, NULL, &StallPpi );

    Delay = (HUB_TIME_OUT * STALL_1_MILLI_SECOND / 50000) + 1;

    //
    // Do while will do a 3s scan for the USB HUB for a connected
    // device. If we have found one, the waiting process will be
    // breaked since we assue other ports will be discovered at
    // the same time.
    //
    do {
	    for (Port = 1; Port <= PeiUsbDevice->DownStreamPortNo; Port++) {

            Status = PeiHubGetPortStatus( PeiServices, UsbIoPpi,
	            Port, (UINT32 *) &PortStatus );

            if ( EFI_ERROR( Status ) ) {
                continue;
            }

            if ( IsPortConnectChange( PortStatus.PortChangeStatus ) ) {
                PortChanged = TRUE;
                PeiHubClearPortFeature( PeiServices, UsbIoPpi,
	                Port, EfiUsbPortConnectChange );

                if ( IsPortConnect( PortStatus.PortStatus ) ) {

					if (IsPortEnable(PortStatus.PortStatus) == FALSE) {
	                    //
	                    // First reset and enable this port
	                    //
		                PeiResetHubPort( PeiServices, UsbIoPpi, Port );

		                PeiHubGetPortStatus( PeiServices, UsbIoPpi,
		                    Port, (UINT32 *) &PortStatus );
					}

                    //
                    // Begin to deal with the new device
                    //
	                Status = (*PeiServices)->AllocatePool( PeiServices,
	                    sizeof(PEI_USB_DEVICE), &AllocateAddress );
                    if ( EFI_ERROR( Status ) ) {
                        return EFI_OUT_OF_RESOURCES;
                    }

	                NewPeiUsbDevice = (PEI_USB_DEVICE *)AllocateAddress;

                    ZeroMem( NewPeiUsbDevice, sizeof(PEI_USB_DEVICE) );

                    NewPeiUsbDevice->Signature =
                        PEI_USB_DEVICE_SIGNATURE;
                    NewPeiUsbDevice->DeviceAddress = 0;
                    NewPeiUsbDevice->MaxPacketSize0 = 8;
                    NewPeiUsbDevice->DataToggle = 0;
                    NewPeiUsbDevice->UsbIoPpi = mUsbIoPpi;
                    NewPeiUsbDevice->UsbIoPpiList = mUsbIoPpiList;
                    NewPeiUsbDevice->UsbIoPpiList.Ppi =
                        &NewPeiUsbDevice->UsbIoPpi;
                    NewPeiUsbDevice->UsbHcPpi =
                        PeiUsbDevice->UsbHcPpi;
                    NewPeiUsbDevice->DeviceSpeed =
                        USB_FULL_SPEED_DEVICE;
                    NewPeiUsbDevice->IsHub = 0x0;
                    NewPeiUsbDevice->DownStreamPortNo = 0x0;
                    NewPeiUsbDevice->TransactionTranslator =
	                    (UINT16)(Port << 7) + PeiUsbDevice->DeviceAddress;
					NewPeiUsbDevice->HubDepth = 0x0;
										//(EIP28255+)>
					if ( (PeiUsbDevice->DeviceSpeed == USB_SLOW_SPEED_DEVICE) ||
						(PeiUsbDevice->DeviceSpeed == USB_FULL_SPEED_DEVICE) )
					{
						NewPeiUsbDevice->TransactionTranslator = PeiUsbDevice->TransactionTranslator;
					}
										//<(EIP28255+)
                    if ( IsPortLowSpeedDeviceAttached( PortStatus.PortStatus ) )
                    {
                        NewPeiUsbDevice->DeviceSpeed = USB_SLOW_SPEED_DEVICE;
                    }

                    if ( IsPortHighSpeedDeviceAttached( PortStatus.PortStatus ) )
                    {
                        NewPeiUsbDevice->DeviceSpeed = USB_HIGH_SPEED_DEVICE;
                    }

                    if ( IsPortSuperSpeedDeviceAttached( PortStatus.PortStatus ) )
                    {
                        NewPeiUsbDevice->DeviceSpeed = USB_SUPER_SPEED_DEVICE;
                    }

                    //
                    // Configure that Usb Device
                    //
                    Status = PeiConfigureUsbDevice( PeiServices,
	                    NewPeiUsbDevice, Port, CurrentAddress );

                if ( EFI_ERROR( Status ) ) {
                    PeiHubClearPortFeature( PeiServices, UsbIoPpi,
	                        Port, EfiUsbPortEnable );
                    continue;
                }

                    Status = (**PeiServices).InstallPpi( PeiServices,
                        &NewPeiUsbDevice->UsbIoPpiList );

                    if (NewPeiUsbDevice->InterfaceDesc->InterfaceClass
                        == BASE_CLASS_HUB)
                    {
                        NewPeiUsbDevice->IsHub = 0x1;
						NewPeiUsbDevice->HubDepth = PeiUsbDevice->HubDepth + 1;

                        Status = PeiDoHubConfig( PeiServices,
                            NewPeiUsbDevice );
                        if ( EFI_ERROR( Status ) ) {
                            return Status;
                        }
                        PeiHubEnumeration( PeiServices, NewPeiUsbDevice,
                            CurrentAddress );
                    }
                }

            }
        }

        if (PortChanged) {
            break;
        }
        StallPpi->Stall( PeiServices, StallPpi, 50000 );

    } while (Delay--);

    return EFI_SUCCESS;
}


STATIC
EFI_STATUS PeiUsbEnumeration (
    IN EFI_PEI_SERVICES            **PeiServices,
    IN PEI_USB_HOST_CONTROLLER_PPI *UsbHcPpi )
{
    UINT8                NumOfRootPort;
    EFI_STATUS           Status;
    UINT8                Port;
    EFI_USB_PORT_STATUS  PortStatus;
    PEI_USB_DEVICE       *PeiUsbDevice;
	UINT8                *AllocateAddress;
    UINT8                CurrentAddress;
    PEI_STALL_PPI        *PeiStall;

    (**PeiServices).LocatePpi(
        PeiServices,
        &gPeiStallPpiGuid,
        0,
        NULL,
        &PeiStall
    );

    CurrentAddress = 0;
    UsbHcPpi->GetRootHubPortNumber(
        PeiServices,
        UsbHcPpi,
        (UINT8 *) &NumOfRootPort
    );


    for (Port = 1; Port <= NumOfRootPort; Port++) {
        //
        // First get root port status to detect changes happen
        //
        UsbHcPpi->GetRootHubPortStatus(
            PeiServices,
            UsbHcPpi,
            Port,
            &PortStatus
        );

        if ( IsPortConnectChange( PortStatus.PortChangeStatus ) ) {

            //
            // Changes happen, first clear this change status
            //
            UsbHcPpi->ClearRootHubPortFeature(
                PeiServices,
                UsbHcPpi,
                Port,
                EfiUsbPortConnectChange
            );

            if ( IsPortConnect( PortStatus.PortStatus ) ) {

				if (IsPortEnable(PortStatus.PortStatus) == FALSE) {
	                //
	                // First reset and enable this port
	                //
	                ResetRootPort( PeiServices, UsbHcPpi, Port );

	                UsbHcPpi->GetRootHubPortStatus(
	                    PeiServices,
	                    UsbHcPpi,
	                    Port,
	                    &PortStatus
	                );
				}

                //
                // Connect change happen
                //
                Status = (*PeiServices)->AllocatePool(
                    PeiServices,
                    sizeof(PEI_USB_DEVICE),
                    &AllocateAddress
                         );
                if ( EFI_ERROR( Status ) ) {
                    return EFI_OUT_OF_RESOURCES;
                }

                PeiUsbDevice = (PEI_USB_DEVICE *) ( (UINTN) AllocateAddress );
                ZeroMem( PeiUsbDevice, sizeof(PEI_USB_DEVICE) );

                PeiUsbDevice->Signature = PEI_USB_DEVICE_SIGNATURE;
                PeiUsbDevice->DeviceAddress = 0;
                PeiUsbDevice->MaxPacketSize0 = 8;
                PeiUsbDevice->DataToggle = 0;
                PeiUsbDevice->UsbIoPpi = mUsbIoPpi;
                PeiUsbDevice->UsbIoPpiList = mUsbIoPpiList;
                PeiUsbDevice->UsbIoPpiList.Ppi = &PeiUsbDevice->UsbIoPpi;
                PeiUsbDevice->UsbHcPpi = UsbHcPpi;
                PeiUsbDevice->DeviceSpeed = USB_FULL_SPEED_DEVICE;
                PeiUsbDevice->IsHub = 0x0;
                PeiUsbDevice->DownStreamPortNo = 0x0;
                PeiUsbDevice->TransactionTranslator = 0x0;
				PeiUsbDevice->HubDepth = 0x0;

                if ( IsPortLowSpeedDeviceAttached( PortStatus.PortStatus ) )
                {
                    PeiUsbDevice->DeviceSpeed = USB_SLOW_SPEED_DEVICE;
                }

                if ( IsPortHighSpeedDeviceAttached( PortStatus.PortStatus ) )
                {
                    PeiUsbDevice->DeviceSpeed = USB_HIGH_SPEED_DEVICE;
                }

                if ( IsPortSuperSpeedDeviceAttached( PortStatus.PortStatus ) )
                {
                    PeiUsbDevice->DeviceSpeed = USB_SUPER_SPEED_DEVICE;
                }

                //
                // Delay some times to enable usb devices to initiate.
                //
                PeiStall->Stall(
                    PeiServices,
                    PeiStall,
                    5000
                );

                //
                // Configure that Usb Device
                //
                Status = PeiConfigureUsbDevice(
                    PeiServices,
                    PeiUsbDevice,
                    Port,
                    &CurrentAddress
                         );

                if ( EFI_ERROR( Status ) ) {
                    UsbHcPpi->ClearRootHubPortFeature(
                        PeiServices,
                        UsbHcPpi,
                        Port,
                        EfiUsbPortEnable
                    );
                    continue;
                }

                Status = (**PeiServices).InstallPpi(
                    PeiServices,
                    &PeiUsbDevice->UsbIoPpiList
                         );

                if (PeiUsbDevice->InterfaceDesc->InterfaceClass
                    == BASE_CLASS_HUB)
                {
                    PeiUsbDevice->IsHub = 0x1;

                    Status = PeiDoHubConfig( PeiServices, PeiUsbDevice );
                    if ( EFI_ERROR( Status ) ) {
                        return Status;
                    }

                    PeiHubEnumeration( PeiServices, PeiUsbDevice,
                        &CurrentAddress );
                }
            }
        }
    }

    return EFI_SUCCESS;
}


STATIC EFI_STATUS PeiConfigureUsbDevice (
    IN EFI_PEI_SERVICES **PeiServices,
    IN PEI_USB_DEVICE   *PeiUsbDevice,
    IN UINT8            Port,
    IN OUT UINT8        *DeviceAddress )
{
    EFI_USB_DEVICE_DESCRIPTOR DeviceDescriptor;
    EFI_STATUS     Status;
    PEI_USB_IO_PPI *UsbIoPpi;
    UINT8 i;
    PEI_STALL_PPI  *StallPpi = NULL;
    UINT8 Retry = 2;

    ( **PeiServices ).LocatePpi( PeiServices, &gPeiStallPpiGuid,
        0, NULL, &StallPpi );

    if (PeiUsbDevice->UsbHcPpi->PreConfigureDevice != NULL) {
        Status = PeiUsbDevice->UsbHcPpi->PreConfigureDevice( PeiServices,
            PeiUsbDevice->UsbHcPpi, Port, PeiUsbDevice->DeviceSpeed, 
            PeiUsbDevice->TransactionTranslator);
        if ( EFI_ERROR( Status ) ) {
            return Status;
        }
    }

    UsbIoPpi = &PeiUsbDevice->UsbIoPpi;

    //-----------------------------------------------------------------------
    // Try 5 times to read the first 8 bytes to determine the size
    for (i = 0; i < 5; i++) {
        Status = PeiUsbGetDescriptor( PeiServices,
            UsbIoPpi,
            SET_DESCRIPTOR_TYPE( USB_DT_DEVICE ), // Value = Type << 8 | Index
            0,                                    // Index
            8,                                    // DescriptorLength
            &DeviceDescriptor );
        if ( !EFI_ERROR( Status ) ) {
            break;
        }
        StallPpi->Stall( PeiServices, StallPpi, 100 * 1000 ); // 100msec delay
    }
    if ( EFI_ERROR( Status ) ) {
        return Status;
    }

    //-----------------------------------------------------------------------
    // Set MaxPacketSize0 = 0x40 if packet size is not specified
    PeiUsbDevice->MaxPacketSize0 = (DeviceDescriptor.MaxPacketSize0)
                                   ? DeviceDescriptor.MaxPacketSize0
                                   : 0x40;


    //-----------------------------------------------------------------------
    // Get the entire USB device descriptor
    StallPpi->Stall( PeiServices, StallPpi, 10 * 1000 ); // 10msec delay
    Status = PeiUsbGetDescriptor(
        PeiServices,
        UsbIoPpi,
        SET_DESCRIPTOR_TYPE( USB_DT_DEVICE ),   // Value = Type << 8 | Index
        0,                                      // Index
        sizeof(EFI_USB_DEVICE_DESCRIPTOR),      // DescriptorLength
        &DeviceDescriptor );
        
    if ( EFI_ERROR( Status ) ) {
        return Status;
    }

    //-----------------------------------------------------------------------
    // Get its default configuration and its first interface
    StallPpi->Stall( PeiServices, StallPpi, 10 * 1000 ); // 10msec delay
    Status = PeiUsbGetAllConfiguration(
        PeiServices,
        PeiUsbDevice );
    if ( EFI_ERROR( Status ) ) {
        return Status;
    }

    //-----------------------------------------------------------------------
    // Set the device's address
    StallPpi->Stall( PeiServices, StallPpi, 10 * 1000 ); // 10msec delay
    (*DeviceAddress)++;
    Status = PeiUsbSetDeviceAddress(
        PeiServices,
        UsbIoPpi,
        *DeviceAddress );
    if ( EFI_ERROR( Status ) ) {
        return Status;
    }
    PeiUsbDevice->DeviceAddress = *DeviceAddress;


    StallPpi->Stall( PeiServices, StallPpi, 200 * 1000 ); // 200msec delay

    Status = PeiUsbSetConfiguration(
        PeiServices,
        UsbIoPpi );

    if ( EFI_ERROR( Status ) ) {
        return Status;
    }

    return EFI_SUCCESS;
}


STATIC EFI_STATUS PeiUsbGetAllConfiguration (
    IN EFI_PEI_SERVICES **PeiServices,
    IN PEI_USB_DEVICE   *Device )
{
	EFI_USB_CONFIG_DESCRIPTOR	ConfigDesc = {0};
	EFI_USB_ENDPOINT_DESCRIPTOR	*EndPointDesc = NULL;	//(EIP32503+)
    EFI_STATUS     Status;
    PEI_USB_IO_PPI *UsbIoPpi = &Device->UsbIoPpi;
    UINTN i;
    UINT8          *LastAddress = 0;


    // Here we are parsing the descriptors for the device
    // configurations where the hierarchy of descriptors
    // is as follows:
    //
    // +----------------+
    // | Configuration1 |
    // +----------------+
    //        |         +------------+
    //        +---------| Interface1 |----+------------------+
    //        |         +------------+    |                  |
    //        |                     +-----------+       +-------------+
    //        |                     | Endpoint1 |  ...  | EndpointMax |
    //        |                     +-----------+       +-------------+
    //        |                :
    //        |                :
    //        |                :
    //        |
    //        |         +--------------+
    //        +---------| InterfaceMax |----+------------------+
    //                  +--------------+    |                  |
    //        :                       +-----------+       +-------------+
    //        :                       | Endpoint1 |  ...  | EndpointMax |
    //                                +-----------+       +-------------+
    // +------------------+
    // | ConfigurationMax |
    // +------------------+
    //        |         +------------+
    //        +---------| Interface1 |----+------------------+
    //        |         +------------+    |                  |
    //        |                     +-----------+       +-------------+
    //        |                     | Endpoint1 |  ...  | EndpointMax |
    //        |                     +-----------+       +-------------+
    //        |                :
    //        |                :
    //        |                :
    //        |         +--------------+
    //        +---------| InterfaceMax |----+------------------+
    //                  +--------------+    |                  |
    //                                +-----------+       +-------------+
    //                                | Endpoint1 |  ...  | EndpointMax |
    //                                +-----------+       +-------------+


    //-------------------------------------------------------------
    // Fortunately, we are only interested in the first/default
    // configuration and its first/default interface, so life is
    // simple!
    //-------------------------------------------------------------

    //-------------------------------------------------------------
    // First get the device's 9-byte configuration descriptor to
    // determine the length of all descriptors
    Status = PeiUsbGetDescriptor(
        PeiServices,
        UsbIoPpi,
        USB_DT_CONFIG << 8,
        0,
        9,
        &ConfigDesc );
    if ( EFI_ERROR( Status ) ) {
        return Status;
    }

    Status = (*PeiServices)->AllocatePool( PeiServices,
    	ConfigDesc.TotalLength, &Device->ConfigurationData );
    if ( EFI_ERROR( Status ) ) {
        return Status;
    }
    //-------------------------------------------------------------
    // Get all the descriptors for this configuration using
    // TotalLength from the first 9 bytes previously read.
    // Then, save the Configuration descriptor into the
    // device management structure.
    Status = PeiUsbGetDescriptor(
        PeiServices,
        UsbIoPpi,
        USB_DT_CONFIG << 8,
        0,
        ConfigDesc.TotalLength,
        (VOID *) Device->ConfigurationData );
    if ( EFI_ERROR( Status ) ) {
        return Status;
    }
    Device->ConfigDesc =
        (EFI_USB_CONFIG_DESCRIPTOR *) Device->ConfigurationData;

    LastAddress = Device->ConfigurationData +
                  Device->ConfigDesc->TotalLength - 1;

    if (Device->UsbHcPpi->EnableEndpoints != NULL) {
        Status = Device->UsbHcPpi->EnableEndpoints(PeiServices,
                    Device->UsbHcPpi, Device->ConfigurationData);
    
        if ( EFI_ERROR( Status ) ) {
            return Status;
        }
    }

    //--------------------------------------------------------------
    // Assume the Interface descriptor is directly after the
    // configuration descriptor.
    //--------------------------------------------------------------
    Device->InterfaceDesc = (EFI_USB_INTERFACE_DESCRIPTOR *)
                            ( (UINT8 *) Device->ConfigDesc +
                             Device->ConfigDesc->Length );
										//(EIP32503+)>
	while ((UINT8 *)Device->InterfaceDesc < LastAddress && 
			Device->InterfaceDesc->DescriptorType != USB_DT_INTERFACE)
	{
		Device->InterfaceDesc = (EFI_USB_INTERFACE_DESCRIPTOR *)
                            ( (UINT8 *) Device->InterfaceDesc +
                             Device->InterfaceDesc->Length );
	}
										//<(EIP32503+)
    //--------------------------------------------------------------
    // Assume the first Endpoint descriptor is directly after the
    // Interface descriptor.
    //--------------------------------------------------------------
    									//(EIP32503)>
    EndPointDesc = (EFI_USB_ENDPOINT_DESCRIPTOR *)
                   ( (UINT8 *) Device->InterfaceDesc +
                   Device->InterfaceDesc->Length );

    for (i = 0; i < Device->InterfaceDesc->NumEndpoints && 
		(UINT8 *)EndPointDesc < LastAddress; )
	{
		if(EndPointDesc->DescriptorType == USB_DT_ENDPOINT)
		{
			Device->EndpointDesc[i++] = EndPointDesc;
		}
		EndPointDesc = (EFI_USB_ENDPOINT_DESCRIPTOR *)
    	               ( (UINT8 *) EndPointDesc +
        	           EndPointDesc->Length );
    }
										//<(EIP32503)
    return EFI_SUCCESS;
}


//
// Send reset signal over the given root hub port
//
VOID ResetRootPort (
    IN EFI_PEI_SERVICES            **PeiServices,
    IN PEI_USB_HOST_CONTROLLER_PPI *UsbHcPpi,
    UINT8                          PortNum )
{
    EFI_PEI_STALL_PPI       *PeiStall;
	EFI_USB_PORT_STATUS     PortStatus;
	UINT8                   i;

    ( **PeiServices ).LocatePpi(
        PeiServices,
        &gPeiStallPpiGuid,
        0,
        NULL,
        &PeiStall
    );


    //
    // reset root port
    //
    UsbHcPpi->SetRootHubPortFeature(
        PeiServices,
        UsbHcPpi,
        PortNum,
        EfiUsbPortReset
    );

	for (i = 0; i < 100; i++) {
        UsbHcPpi->GetRootHubPortStatus(
            PeiServices,
            UsbHcPpi,
            PortNum,
            &PortStatus
        );

		if ((PortStatus.PortChangeStatus & USB_PORT_STAT_C_RESET) != 0) {
			break;
		}

	    PeiStall->Stall(
	        PeiServices,
	        PeiStall,
	        1 * 1000            // NVS - Changed to 10 msec (as per AMI USB Code)
	    );
	}

	if ((PortStatus.PortChangeStatus & USB_PORT_STAT_C_RESET) == 0) {
		return;
	}

    //
    // clear reset root port
    //
    UsbHcPpi->ClearRootHubPortFeature(
        PeiServices,
        UsbHcPpi,
        PortNum,
        EfiUsbPortResetChange
    );


    UsbHcPpi->GetRootHubPortStatus(
        PeiServices,
        UsbHcPpi,
        PortNum,
        &PortStatus
    );

	if ((PortStatus.PortChangeStatus & USB_PORT_STAT_ENABLE) == 0) {
		//
		// Set port enable
		//
		UsbHcPpi->SetRootHubPortFeature(
		    PeiServices,
		    UsbHcPpi,
		    PortNum,
		    EfiUsbPortEnable
		);
	    PeiStall->Stall(
	        PeiServices,
	        PeiStall,
	        100 * 1000            // NVS - Changed to 100msec as per AMI USB code
	    );
	}

    return;
}


//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2008, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**         5555 Oakbrook Pkwy, Suite 200, Norcross, GA 30093        **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************