2 PEIM to produce gPeiUsb2HostControllerPpiGuid based on gPeiUsbControllerPpiGuid
3 which is used to enable recovery function from USB Drivers.
5 Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions
9 of the BSD License which accompanies this distribution. The
10 full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21 Delete a single asynchronous interrupt transfer for
22 the device and endpoint.
24 @param Ehc The EHCI device.
25 @param Data Current data not associated with a QTD.
26 @param DataLen The length of the data.
27 @param PktId Packet ID to use in the QTD.
28 @param Toggle Data toggle to use in the QTD.
29 @param MaxPacket Maximu packet length of the endpoint.
31 @retval the pointer to the created QTD or NULL if failed to create one.
36 IN PEI_USB2_HC_DEV
*Ehc
,
52 Qtd
= UsbHcAllocateMem (Ehc
, Ehc
->MemPool
, sizeof (PEI_EHC_QTD
));
58 Qtd
->Signature
= EHC_QTD_SIG
;
62 InitializeListHead (&Qtd
->QtdList
);
65 QtdHw
->NextQtd
= QTD_LINK (NULL
, TRUE
);
66 QtdHw
->AltNext
= QTD_LINK (NULL
, TRUE
);
67 QtdHw
->Status
= QTD_STAT_ACTIVE
;
69 QtdHw
->ErrCnt
= QTD_MAX_ERR
;
71 QtdHw
->TotalBytes
= 0;
72 QtdHw
->DataToggle
= Toggle
;
75 // Fill in the buffer points
80 for (Index
= 0; Index
<= QTD_MAX_BUFFER
; Index
++) {
82 // Set the buffer point (Check page 41 EHCI Spec 1.0). No need to
83 // compute the offset and clear Reserved fields. This is already
84 // done in the data point.
86 QtdHw
->Page
[Index
] = EHC_LOW_32BIT (Data
);
87 QtdHw
->PageHigh
[Index
] = EHC_HIGH_32BIT (Data
);
89 ThisBufLen
= QTD_BUF_LEN
- (EHC_LOW_32BIT (Data
) & QTD_BUF_MASK
);
91 if (Len
+ ThisBufLen
>= DataLen
) {
101 // Need to fix the last pointer if the Qtd can't hold all the
102 // user's data to make sure that the length is in the unit of
103 // max packets. If it can hold all the data, there is no such
107 Len
= Len
- Len
% MaxPacket
;
110 QtdHw
->TotalBytes
= (UINT32
) Len
;
118 Initialize the queue head for interrupt transfer,
119 that is, initialize the following three fields:
120 1. SplitXState in the Status field.
121 2. Microframe S-mask.
122 3. Microframe C-mask.
124 @param Ep The queue head's related endpoint.
125 @param QhHw The queue head to initialize.
135 // Because UEFI interface can't utilitize an endpoint with
136 // poll rate faster than 1ms, only need to set one bit in
137 // the queue head. simple. But it may be changed later. If
138 // sub-1ms interrupt is supported, need to update the S-Mask
141 if (Ep
->DevSpeed
== EFI_USB_SPEED_HIGH
) {
142 QhHw
->SMask
= QH_MICROFRAME_0
;
147 // For low/full speed device, the transfer must go through
148 // the split transaction. Need to update three fields
149 // 1. SplitXState in the status
150 // 2. Microframe S-Mask
151 // 3. Microframe C-Mask
152 // UEFI USB doesn't exercise admission control. It simplely
153 // schedule the high speed transactions in microframe 0, and
154 // full/low speed transactions at microframe 1. This also
155 // avoid the use of FSTN.
157 QhHw
->SMask
= QH_MICROFRAME_1
;
158 QhHw
->CMask
= QH_MICROFRAME_3
| QH_MICROFRAME_4
| QH_MICROFRAME_5
;
162 Allocate and initialize a EHCI queue head.
164 @param Ehci The EHCI device.
165 @param Ep The endpoint to create queue head for.
167 @retval the pointer to the created queue head or NULL if failed to create one.
172 IN PEI_USB2_HC_DEV
*Ehci
,
179 Qh
= UsbHcAllocateMem (Ehci
, Ehci
->MemPool
, sizeof (PEI_EHC_QH
));
185 Qh
->Signature
= EHC_QH_SIG
;
187 Qh
->Interval
= Ep
->PollRate
;
189 InitializeListHead (&Qh
->Qtds
);
192 QhHw
->HorizonLink
= QH_LINK (NULL
, 0, TRUE
);
193 QhHw
->DeviceAddr
= Ep
->DevAddr
;
195 QhHw
->EpNum
= Ep
->EpAddr
;
196 QhHw
->EpSpeed
= Ep
->DevSpeed
;
198 QhHw
->ReclaimHead
= 0;
199 QhHw
->MaxPacketLen
= (UINT32
) Ep
->MaxPacket
;
201 QhHw
->NakReload
= QH_NAK_RELOAD
;
202 QhHw
->HubAddr
= Ep
->HubAddr
;
203 QhHw
->PortNum
= Ep
->HubPort
;
204 QhHw
->Multiplier
= 1;
205 QhHw
->DataToggle
= Ep
->Toggle
;
207 if (Ep
->DevSpeed
!= EFI_USB_SPEED_HIGH
) {
208 QhHw
->Status
|= QTD_STAT_DO_SS
;
212 case EHC_CTRL_TRANSFER
:
214 // Special initialization for the control transfer:
215 // 1. Control transfer initialize data toggle from each QTD
216 // 2. Set the Control Endpoint Flag (C) for low/full speed endpoint.
220 if (Ep
->DevSpeed
!= EFI_USB_SPEED_HIGH
) {
225 case EHC_INT_TRANSFER_ASYNC
:
226 case EHC_INT_TRANSFER_SYNC
:
228 // Special initialization for the interrupt transfer
229 // to set the S-Mask and C-Mask
232 EhcInitIntQh (Ep
, QhHw
);
235 case EHC_BULK_TRANSFER
:
236 if ((Ep
->DevSpeed
== EFI_USB_SPEED_HIGH
) && (Ep
->Direction
== EfiUsbDataOut
)) {
237 QhHw
->Status
|= QTD_STAT_DO_PING
;
247 Convert the poll interval from application to that
248 be used by EHCI interface data structure. Only need
249 to get the max 2^n that is less than interval. UEFI
250 can't support high speed endpoint with a interval less
251 than 8 microframe because interval is specified in
252 the unit of ms (millisecond).
254 @param Interval The interval to convert.
256 @retval The converted interval.
271 // Find the index (1 based) of the highest non-zero bit
275 while (Interval
!= 0) {
280 return (UINTN
)1 << (BitCount
- 1);
286 @param Ehc The EHCI device.
287 @param Qtds The list head of the QTD.
292 IN PEI_USB2_HC_DEV
*Ehc
,
293 IN EFI_LIST_ENTRY
*Qtds
296 EFI_LIST_ENTRY
*Entry
;
297 EFI_LIST_ENTRY
*Next
;
300 EFI_LIST_FOR_EACH_SAFE (Entry
, Next
, Qtds
) {
301 Qtd
= EFI_LIST_CONTAINER (Entry
, PEI_EHC_QTD
, QtdList
);
303 RemoveEntryList (&Qtd
->QtdList
);
304 UsbHcFreeMem (Ehc
->MemPool
, Qtd
, sizeof (PEI_EHC_QTD
));
309 Free an allocated URB. It is possible for it to be partially inited.
311 @param Ehc The EHCI device.
312 @param Urb The URB to free.
317 IN PEI_USB2_HC_DEV
*Ehc
,
321 if (Urb
->Qh
!= NULL
) {
323 // Ensure that this queue head has been unlinked from the
324 // schedule data structures. Free all the associated QTDs
326 EhcFreeQtds (Ehc
, &Urb
->Qh
->Qtds
);
327 UsbHcFreeMem (Ehc
->MemPool
, Urb
->Qh
, sizeof (PEI_EHC_QH
));
332 Create a list of QTDs for the URB.
334 @param Ehc The EHCI device.
335 @param Urb The URB to create QTDs for.
337 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for QTD.
338 @retval EFI_SUCCESS The QTDs are allocated for the URB.
343 IN PEI_USB2_HC_DEV
*Ehc
,
350 PEI_EHC_QTD
*StatusQtd
;
351 PEI_EHC_QTD
*NextQtd
;
352 EFI_LIST_ENTRY
*Entry
;
358 ASSERT ((Urb
!= NULL
) && (Urb
->Qh
!= NULL
));
361 // EHCI follows the alternet next QTD pointer if it meets
362 // a short read and the AlterNext pointer is valid. UEFI
363 // EHCI driver should terminate the transfer except the
370 AlterNext
= QTD_LINK (NULL
, TRUE
);
372 if (Ep
->Direction
== EfiUsbDataIn
) {
373 AlterNext
= QTD_LINK (Ehc
->ShortReadStop
, FALSE
);
377 // Build the Setup and status packets for control transfer
379 if (Urb
->Ep
.Type
== EHC_CTRL_TRANSFER
) {
380 Len
= sizeof (EFI_USB_DEVICE_REQUEST
);
381 Qtd
= EhcCreateQtd (Ehc
, Urb
->RequestPhy
, Len
, QTD_PID_SETUP
, 0, Ep
->MaxPacket
);
384 return EFI_OUT_OF_RESOURCES
;
387 InsertTailList (&Qh
->Qtds
, &Qtd
->QtdList
);
390 // Create the status packet now. Set the AlterNext to it. So, when
391 // EHCI meets a short control read, it can resume at the status stage.
392 // Use the opposite direction of the data stage, or IN if there is
395 if (Ep
->Direction
== EfiUsbDataIn
) {
396 Pid
= QTD_PID_OUTPUT
;
401 StatusQtd
= EhcCreateQtd (Ehc
, NULL
, 0, Pid
, 1, Ep
->MaxPacket
);
403 if (StatusQtd
== NULL
) {
407 if (Ep
->Direction
== EfiUsbDataIn
) {
408 AlterNext
= QTD_LINK (StatusQtd
, FALSE
);
415 // Build the data packets for all the transfers
417 if (Ep
->Direction
== EfiUsbDataIn
) {
420 Pid
= QTD_PID_OUTPUT
;
426 while (Len
< Urb
->DataLen
) {
429 (UINT8
*) Urb
->DataPhy
+ Len
,
440 Qtd
->QtdHw
.AltNext
= AlterNext
;
441 InsertTailList (&Qh
->Qtds
, &Qtd
->QtdList
);
444 // Switch the Toggle bit if odd number of packets are included in the QTD.
446 if (((Qtd
->DataLen
+ Ep
->MaxPacket
- 1) / Ep
->MaxPacket
) % 2) {
447 Toggle
= (UINT8
) (1 - Toggle
);
454 // Insert the status packet for control transfer
456 if (Ep
->Type
== EHC_CTRL_TRANSFER
) {
457 InsertTailList (&Qh
->Qtds
, &StatusQtd
->QtdList
);
461 // OK, all the QTDs needed are created. Now, fix the NextQtd point
463 EFI_LIST_FOR_EACH (Entry
, &Qh
->Qtds
) {
464 Qtd
= EFI_LIST_CONTAINER (Entry
, PEI_EHC_QTD
, QtdList
);
467 // break if it is the last entry on the list
469 if (Entry
->ForwardLink
== &Qh
->Qtds
) {
473 NextQtd
= EFI_LIST_CONTAINER (Entry
->ForwardLink
, PEI_EHC_QTD
, QtdList
);
474 Qtd
->QtdHw
.NextQtd
= QTD_LINK (NextQtd
, FALSE
);
478 // Link the QTDs to the queue head
480 NextQtd
= EFI_LIST_CONTAINER (Qh
->Qtds
.ForwardLink
, PEI_EHC_QTD
, QtdList
);
481 Qh
->QhHw
.NextQtd
= QTD_LINK (NextQtd
, FALSE
);
485 EhcFreeQtds (Ehc
, &Qh
->Qtds
);
486 return EFI_OUT_OF_RESOURCES
;
490 Create a new URB and its associated QTD.
492 @param Ehc The EHCI device.
493 @param DevAddr The device address.
494 @param EpAddr Endpoint addrress & its direction.
495 @param DevSpeed The device speed.
496 @param Toggle Initial data toggle to use.
497 @param MaxPacket The max packet length of the endpoint.
498 @param Hub The transaction translator to use.
499 @param Type The transaction type.
500 @param Request The standard USB request for control transfer.
501 @param Data The user data to transfer.
502 @param DataLen The length of data buffer.
503 @param Callback The function to call when data is transferred.
504 @param Context The context to the callback.
505 @param Interval The interval for interrupt transfer.
507 @retval the pointer to the created URB or NULL.
512 IN PEI_USB2_HC_DEV
*Ehc
,
518 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Hub
,
520 IN EFI_USB_DEVICE_REQUEST
*Request
,
523 IN EFI_ASYNC_USB_TRANSFER_CALLBACK Callback
,
529 EFI_PHYSICAL_ADDRESS PhyAddr
;
539 Urb
->Signature
= EHC_URB_SIG
;
540 InitializeListHead (&Urb
->UrbList
);
543 Ep
->DevAddr
= DevAddr
;
544 Ep
->EpAddr
= (UINT8
) (EpAddr
& 0x0F);
545 Ep
->Direction
= (((EpAddr
& 0x80) != 0) ? EfiUsbDataIn
: EfiUsbDataOut
);
546 Ep
->DevSpeed
= DevSpeed
;
547 Ep
->MaxPacket
= MaxPacket
;
552 if (DevSpeed
!= EFI_USB_SPEED_HIGH
) {
553 ASSERT (Hub
!= NULL
);
555 Ep
->HubAddr
= Hub
->TranslatorHubAddress
;
556 Ep
->HubPort
= Hub
->TranslatorPortNumber
;
561 Ep
->PollRate
= EhcConvertPollRate (Interval
);
563 Urb
->Request
= Request
;
565 Urb
->DataLen
= DataLen
;
566 Urb
->Callback
= Callback
;
567 Urb
->Context
= Context
;
568 Urb
->Qh
= EhcCreateQh (Ehc
, &Urb
->Ep
);
570 if (Urb
->Qh
== NULL
) {
575 // Map the request and user data
577 if (Request
!= NULL
) {
578 Len
= sizeof (EFI_USB_DEVICE_REQUEST
);
579 PhyAddr
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Request
;
580 if ( (Len
!= sizeof (EFI_USB_DEVICE_REQUEST
))) {
584 Urb
->RequestPhy
= (VOID
*) ((UINTN
) PhyAddr
);
585 Urb
->RequestMap
= Map
;
590 PhyAddr
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Data
;
591 if ( (Len
!= DataLen
)) {
595 Urb
->DataPhy
= (VOID
*) ((UINTN
) PhyAddr
);
599 Status
= EhcCreateQtds (Ehc
, Urb
);
601 if (EFI_ERROR (Status
)) {
608 EhcFreeUrb (Ehc
, Urb
);