3 This file contains URB request, each request is warpped in a
4 URB (Usb Request Block).
6 Copyright (c) 2007 - 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 of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21 Create a single QTD to hold the data.
23 @param Ehc The EHCI device.
24 @param Data The cpu memory address of current data not associated with a QTD.
25 @param DataPhy The pci bus address of 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 @return Created QTD or NULL if failed to create one.
53 Qtd
= UsbHcAllocateMem (Ehc
->MemPool
, sizeof (EHC_QTD
));
59 Qtd
->Signature
= EHC_QTD_SIG
;
63 InitializeListHead (&Qtd
->QtdList
);
66 QtdHw
->NextQtd
= QTD_LINK (NULL
, TRUE
);
67 QtdHw
->AltNext
= QTD_LINK (NULL
, TRUE
);
68 QtdHw
->Status
= QTD_STAT_ACTIVE
;
70 QtdHw
->ErrCnt
= QTD_MAX_ERR
;
72 QtdHw
->TotalBytes
= 0;
73 QtdHw
->DataToggle
= Toggle
;
76 // Fill in the buffer points
81 for (Index
= 0; Index
<= QTD_MAX_BUFFER
; Index
++) {
83 // Set the buffer point (Check page 41 EHCI Spec 1.0). No need to
84 // compute the offset and clear Reserved fields. This is already
85 // done in the data point.
87 QtdHw
->Page
[Index
] = EHC_LOW_32BIT (DataPhy
);
88 QtdHw
->PageHigh
[Index
] = EHC_HIGH_32BIT (DataPhy
);
90 ThisBufLen
= QTD_BUF_LEN
- (EHC_LOW_32BIT (DataPhy
) & QTD_BUF_MASK
);
92 if (Len
+ ThisBufLen
>= DataLen
) {
99 DataPhy
+= ThisBufLen
;
103 // Need to fix the last pointer if the Qtd can't hold all the
104 // user's data to make sure that the length is in the unit of
105 // max packets. If it can hold all the data, there is no such
109 Len
= Len
- Len
% MaxPacket
;
112 QtdHw
->TotalBytes
= (UINT32
) Len
;
122 Initialize the queue head for interrupt transfer,
123 that is, initialize the following three fields:
124 1. SplitXState in the Status field
128 @param Ep The queue head's related endpoint.
129 @param QhHw The queue head to initialize.
139 // Because UEFI interface can't utilitize an endpoint with
140 // poll rate faster than 1ms, only need to set one bit in
141 // the queue head. simple. But it may be changed later. If
142 // sub-1ms interrupt is supported, need to update the S-Mask
145 if (Ep
->DevSpeed
== EFI_USB_SPEED_HIGH
) {
146 QhHw
->SMask
= QH_MICROFRAME_0
;
151 // For low/full speed device, the transfer must go through
152 // the split transaction. Need to update three fields
153 // 1. SplitXState in the status
154 // 2. Microframe S-Mask
155 // 3. Microframe C-Mask
156 // UEFI USB doesn't exercise admission control. It simplely
157 // schedule the high speed transactions in microframe 0, and
158 // full/low speed transactions at microframe 1. This also
159 // avoid the use of FSTN.
161 QhHw
->SMask
= QH_MICROFRAME_1
;
162 QhHw
->CMask
= QH_MICROFRAME_3
| QH_MICROFRAME_4
| QH_MICROFRAME_5
;
168 Allocate and initialize a EHCI queue head.
170 @param Ehci The EHCI device.
171 @param Ep The endpoint to create queue head for.
173 @return Created queue head or NULL if failed to create one.
178 IN USB2_HC_DEV
*Ehci
,
185 Qh
= UsbHcAllocateMem (Ehci
->MemPool
, sizeof (EHC_QH
));
191 Qh
->Signature
= EHC_QH_SIG
;
193 Qh
->Interval
= Ep
->PollRate
;
195 InitializeListHead (&Qh
->Qtds
);
198 QhHw
->HorizonLink
= QH_LINK (NULL
, 0, TRUE
);
199 QhHw
->DeviceAddr
= Ep
->DevAddr
;
201 QhHw
->EpNum
= Ep
->EpAddr
;
202 QhHw
->EpSpeed
= Ep
->DevSpeed
;
204 QhHw
->ReclaimHead
= 0;
205 QhHw
->MaxPacketLen
= (UINT32
) Ep
->MaxPacket
;
207 QhHw
->NakReload
= QH_NAK_RELOAD
;
208 QhHw
->HubAddr
= Ep
->HubAddr
;
209 QhHw
->PortNum
= Ep
->HubPort
;
210 QhHw
->Multiplier
= 1;
211 QhHw
->DataToggle
= Ep
->Toggle
;
213 if (Ep
->DevSpeed
!= EFI_USB_SPEED_HIGH
) {
214 QhHw
->Status
|= QTD_STAT_DO_SS
;
218 case EHC_CTRL_TRANSFER
:
220 // Special initialization for the control transfer:
221 // 1. Control transfer initialize data toggle from each QTD
222 // 2. Set the Control Endpoint Flag (C) for low/full speed endpoint.
226 if (Ep
->DevSpeed
!= EFI_USB_SPEED_HIGH
) {
231 case EHC_INT_TRANSFER_ASYNC
:
232 case EHC_INT_TRANSFER_SYNC
:
234 // Special initialization for the interrupt transfer
235 // to set the S-Mask and C-Mask
238 EhcInitIntQh (Ep
, QhHw
);
241 case EHC_BULK_TRANSFER
:
242 if ((Ep
->DevSpeed
== EFI_USB_SPEED_HIGH
) && (Ep
->Direction
== EfiUsbDataOut
)) {
243 QhHw
->Status
|= QTD_STAT_DO_PING
;
254 Convert the poll interval from application to that
255 be used by EHCI interface data structure. Only need
256 to get the max 2^n that is less than interval. UEFI
257 can't support high speed endpoint with a interval less
258 than 8 microframe because interval is specified in
259 the unit of ms (millisecond).
261 @param Interval The interval to convert.
263 @return The converted interval.
278 // Find the index (1 based) of the highest non-zero bit
282 while (Interval
!= 0) {
287 return (UINTN
)1 << (BitCount
- 1);
294 @param Ehc The EHCI device.
295 @param Qtds The list head of the QTD.
308 EFI_LIST_FOR_EACH_SAFE (Entry
, Next
, Qtds
) {
309 Qtd
= EFI_LIST_CONTAINER (Entry
, EHC_QTD
, QtdList
);
311 RemoveEntryList (&Qtd
->QtdList
);
312 UsbHcFreeMem (Ehc
->MemPool
, Qtd
, sizeof (EHC_QTD
));
318 Free an allocated URB. It is possible for it to be partially inited.
320 @param Ehc The EHCI device.
321 @param Urb The URB to free.
330 EFI_PCI_IO_PROTOCOL
*PciIo
;
334 if (Urb
->RequestPhy
!= NULL
) {
335 PciIo
->Unmap (PciIo
, Urb
->RequestMap
);
338 if (Urb
->DataMap
!= NULL
) {
339 PciIo
->Unmap (PciIo
, Urb
->DataMap
);
342 if (Urb
->AllocateCommonBuffer
) {
345 EFI_SIZE_TO_PAGES (Urb
->DataLen
),
350 if (Urb
->Qh
!= NULL
) {
352 // Ensure that this queue head has been unlinked from the
353 // schedule data structures. Free all the associated QTDs
355 EhcFreeQtds (Ehc
, &Urb
->Qh
->Qtds
);
356 UsbHcFreeMem (Ehc
->MemPool
, Urb
->Qh
, sizeof (EHC_QH
));
364 Create a list of QTDs for the URB.
366 @param Ehc The EHCI device.
367 @param Urb The URB to create QTDs for.
369 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for QTD.
370 @retval EFI_SUCCESS The QTDs are allocated for the URB.
389 EFI_PHYSICAL_ADDRESS PhyAddr
;
391 ASSERT ((Urb
!= NULL
) && (Urb
->Qh
!= NULL
));
394 // EHCI follows the alternet next QTD pointer if it meets
395 // a short read and the AlterNext pointer is valid. UEFI
396 // EHCI driver should terminate the transfer except the
403 AlterNext
= QTD_LINK (NULL
, TRUE
);
405 PhyAddr
= UsbHcGetPciAddressForHostMem (Ehc
->MemPool
, Ehc
->ShortReadStop
, sizeof (EHC_QTD
));
406 if (Ep
->Direction
== EfiUsbDataIn
) {
407 AlterNext
= QTD_LINK (PhyAddr
, FALSE
);
411 // Build the Setup and status packets for control transfer
413 if (Urb
->Ep
.Type
== EHC_CTRL_TRANSFER
) {
414 Len
= sizeof (EFI_USB_DEVICE_REQUEST
);
415 Qtd
= EhcCreateQtd (Ehc
, (UINT8
*)Urb
->Request
, (UINT8
*)Urb
->RequestPhy
, Len
, QTD_PID_SETUP
, 0, Ep
->MaxPacket
);
418 return EFI_OUT_OF_RESOURCES
;
421 InsertTailList (&Qh
->Qtds
, &Qtd
->QtdList
);
424 // Create the status packet now. Set the AlterNext to it. So, when
425 // EHCI meets a short control read, it can resume at the status stage.
426 // Use the opposite direction of the data stage, or IN if there is
429 if (Ep
->Direction
== EfiUsbDataIn
) {
430 Pid
= QTD_PID_OUTPUT
;
435 StatusQtd
= EhcCreateQtd (Ehc
, NULL
, NULL
, 0, Pid
, 1, Ep
->MaxPacket
);
437 if (StatusQtd
== NULL
) {
441 if (Ep
->Direction
== EfiUsbDataIn
) {
442 PhyAddr
= UsbHcGetPciAddressForHostMem (Ehc
->MemPool
, StatusQtd
, sizeof (EHC_QTD
));
443 AlterNext
= QTD_LINK (PhyAddr
, FALSE
);
450 // Build the data packets for all the transfers
452 if (Ep
->Direction
== EfiUsbDataIn
) {
455 Pid
= QTD_PID_OUTPUT
;
461 while (Len
< Urb
->DataLen
) {
464 (UINT8
*) Urb
->Data
+ Len
,
465 (UINT8
*) Urb
->DataPhy
+ Len
,
476 Qtd
->QtdHw
.AltNext
= AlterNext
;
477 InsertTailList (&Qh
->Qtds
, &Qtd
->QtdList
);
480 // Switch the Toggle bit if odd number of packets are included in the QTD.
482 if (((Qtd
->DataLen
+ Ep
->MaxPacket
- 1) / Ep
->MaxPacket
) % 2) {
483 Toggle
= (UINT8
) (1 - Toggle
);
490 // Insert the status packet for control transfer
492 if (Ep
->Type
== EHC_CTRL_TRANSFER
) {
493 InsertTailList (&Qh
->Qtds
, &StatusQtd
->QtdList
);
497 // OK, all the QTDs needed are created. Now, fix the NextQtd point
499 EFI_LIST_FOR_EACH (Entry
, &Qh
->Qtds
) {
500 Qtd
= EFI_LIST_CONTAINER (Entry
, EHC_QTD
, QtdList
);
503 // break if it is the last entry on the list
505 if (Entry
->ForwardLink
== &Qh
->Qtds
) {
509 NextQtd
= EFI_LIST_CONTAINER (Entry
->ForwardLink
, EHC_QTD
, QtdList
);
510 PhyAddr
= UsbHcGetPciAddressForHostMem (Ehc
->MemPool
, NextQtd
, sizeof (EHC_QTD
));
511 Qtd
->QtdHw
.NextQtd
= QTD_LINK (PhyAddr
, FALSE
);
515 // Link the QTDs to the queue head
517 NextQtd
= EFI_LIST_CONTAINER (Qh
->Qtds
.ForwardLink
, EHC_QTD
, QtdList
);
518 PhyAddr
= UsbHcGetPciAddressForHostMem (Ehc
->MemPool
, NextQtd
, sizeof (EHC_QTD
));
519 Qh
->QhHw
.NextQtd
= QTD_LINK (PhyAddr
, FALSE
);
523 EhcFreeQtds (Ehc
, &Qh
->Qtds
);
524 return EFI_OUT_OF_RESOURCES
;
529 Create a new URB and its associated QTD.
531 @param Ehc The EHCI device.
532 @param DevAddr The device address.
533 @param EpAddr Endpoint addrress & its direction.
534 @param DevSpeed The device speed.
535 @param Toggle Initial data toggle to use.
536 @param MaxPacket The max packet length of the endpoint.
537 @param Hub The transaction translator to use.
538 @param Type The transaction type.
539 @param Request The standard USB request for control transfer.
540 @param AllocateCommonBuffer Indicate whether need to allocate common buffer for data transfer.
541 @param Data The user data to transfer, NULL if AllocateCommonBuffer is TRUE.
542 @param DataLen The length of data buffer.
543 @param Callback The function to call when data is transferred.
544 @param Context The context to the callback.
545 @param Interval The interval for interrupt transfer.
547 @return Created URB or NULL.
558 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Hub
,
560 IN EFI_USB_DEVICE_REQUEST
*Request
,
561 IN BOOLEAN AllocateCommonBuffer
,
564 IN EFI_ASYNC_USB_TRANSFER_CALLBACK Callback
,
570 EFI_PHYSICAL_ADDRESS PhyAddr
;
571 EFI_PCI_IO_PROTOCOL_OPERATION MapOp
;
572 EFI_PCI_IO_PROTOCOL
*PciIo
;
578 Urb
= AllocateZeroPool (sizeof (URB
));
584 Urb
->Signature
= EHC_URB_SIG
;
585 InitializeListHead (&Urb
->UrbList
);
588 Ep
->DevAddr
= DevAddr
;
589 Ep
->EpAddr
= (UINT8
) (EpAddr
& 0x0F);
590 Ep
->Direction
= (((EpAddr
& 0x80) != 0) ? EfiUsbDataIn
: EfiUsbDataOut
);
591 Ep
->DevSpeed
= DevSpeed
;
592 Ep
->MaxPacket
= MaxPacket
;
597 if (DevSpeed
!= EFI_USB_SPEED_HIGH
) {
598 ASSERT (Hub
!= NULL
);
600 Ep
->HubAddr
= Hub
->TranslatorHubAddress
;
601 Ep
->HubPort
= Hub
->TranslatorPortNumber
;
606 Ep
->PollRate
= EhcConvertPollRate (Interval
);
608 Urb
->Request
= Request
;
609 if (AllocateCommonBuffer
) {
610 ASSERT (Data
== NULL
);
611 Status
= Ehc
->PciIo
->AllocateBuffer (
615 EFI_SIZE_TO_PAGES (DataLen
),
619 if (EFI_ERROR (Status
) || (Data
== NULL
)) {
625 Urb
->DataLen
= DataLen
;
626 Urb
->AllocateCommonBuffer
= AllocateCommonBuffer
;
627 Urb
->Callback
= Callback
;
628 Urb
->Context
= Context
;
631 Urb
->Qh
= EhcCreateQh (Ehc
, &Urb
->Ep
);
633 if (Urb
->Qh
== NULL
) {
638 // Map the request and user data
640 if (Request
!= NULL
) {
641 Len
= sizeof (EFI_USB_DEVICE_REQUEST
);
642 MapOp
= EfiPciIoOperationBusMasterRead
;
643 Status
= PciIo
->Map (PciIo
, MapOp
, Request
, &Len
, &PhyAddr
, &Map
);
645 if (EFI_ERROR (Status
) || (Len
!= sizeof (EFI_USB_DEVICE_REQUEST
))) {
649 Urb
->RequestPhy
= (VOID
*) ((UINTN
) PhyAddr
);
650 Urb
->RequestMap
= Map
;
656 if (Urb
->AllocateCommonBuffer
) {
657 MapOp
= EfiPciIoOperationBusMasterCommonBuffer
;
659 if (Ep
->Direction
== EfiUsbDataIn
) {
660 MapOp
= EfiPciIoOperationBusMasterWrite
;
662 MapOp
= EfiPciIoOperationBusMasterRead
;
666 Status
= PciIo
->Map (PciIo
, MapOp
, Data
, &Len
, &PhyAddr
, &Map
);
668 if (EFI_ERROR (Status
) || (Len
!= DataLen
)) {
672 Urb
->DataPhy
= (VOID
*) ((UINTN
) PhyAddr
);
676 Status
= EhcCreateQtds (Ehc
, Urb
);
678 if (EFI_ERROR (Status
)) {
685 EhcFreeUrb (Ehc
, Urb
);