3 This file contains URB request, each request is warpped in a
4 URB (Usb Request Block).
6 Copyright (c) 2007, Intel Corporation
7 All rights reserved. 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 Current data not associated with a QTD.
25 @param DataLen The length of the data.
26 @param PktId Packet ID to use in the QTD.
27 @param Toggle Data toggle to use in the QTD.
28 @param MaxPacket Maximu packet length of the endpoint.
30 @return Created QTD or NULL if failed to create one.
51 Qtd
= UsbHcAllocateMem (Ehc
->MemPool
, sizeof (EHC_QTD
));
57 Qtd
->Signature
= EHC_QTD_SIG
;
61 InitializeListHead (&Qtd
->QtdList
);
64 QtdHw
->NextQtd
= QTD_LINK (NULL
, TRUE
);
65 QtdHw
->AltNext
= QTD_LINK (NULL
, TRUE
);
66 QtdHw
->Status
= QTD_STAT_ACTIVE
;
68 QtdHw
->ErrCnt
= QTD_MAX_ERR
;
70 QtdHw
->TotalBytes
= 0;
71 QtdHw
->DataToggle
= Toggle
;
74 // Fill in the buffer points
79 for (Index
= 0; Index
<= QTD_MAX_BUFFER
; Index
++) {
81 // Set the buffer point (Check page 41 EHCI Spec 1.0). No need to
82 // compute the offset and clear Reserved fields. This is already
83 // done in the data point.
85 QtdHw
->Page
[Index
] = EHC_LOW_32BIT (Data
);
86 QtdHw
->PageHigh
[Index
] = EHC_HIGH_32BIT (Data
);
88 ThisBufLen
= QTD_BUF_LEN
- (EHC_LOW_32BIT (Data
) & QTD_BUF_MASK
);
90 if (Len
+ ThisBufLen
>= DataLen
) {
100 // Need to fix the last pointer if the Qtd can't hold all the
101 // user's data to make sure that the length is in the unit of
102 // max packets. If it can hold all the data, there is no such
106 Len
= Len
- Len
% MaxPacket
;
109 QtdHw
->TotalBytes
= (UINT32
) Len
;
119 Initialize the queue head for interrupt transfer,
120 that is, initialize the following three fields:
121 1. SplitXState in the Status field
125 @param Ep The queue head's related endpoint.
126 @param QhHw The queue head to initialize.
138 // Because UEFI interface can't utilitize an endpoint with
139 // poll rate faster than 1ms, only need to set one bit in
140 // the queue head. simple. But it may be changed later. If
141 // sub-1ms interrupt is supported, need to update the S-Mask
144 if (Ep
->DevSpeed
== EFI_USB_SPEED_HIGH
) {
145 QhHw
->SMask
= QH_MICROFRAME_0
;
150 // For low/full speed device, the transfer must go through
151 // the split transaction. Need to update three fields
152 // 1. SplitXState in the status
153 // 2. Microframe S-Mask
154 // 3. Microframe C-Mask
155 // UEFI USB doesn't exercise admission control. It simplely
156 // schedule the high speed transactions in microframe 0, and
157 // full/low speed transactions at microframe 1. This also
158 // avoid the use of FSTN.
160 QhHw
->SMask
= QH_MICROFRAME_1
;
161 QhHw
->CMask
= QH_MICROFRAME_3
| QH_MICROFRAME_4
| QH_MICROFRAME_5
;
167 Allocate and initialize a EHCI queue head.
169 @param Ehci The EHCI device.
170 @param Ep The endpoint to create queue head for.
172 @return Created queue head or NULL if failed to create one.
177 IN USB2_HC_DEV
*Ehci
,
184 Qh
= UsbHcAllocateMem (Ehci
->MemPool
, sizeof (EHC_QH
));
190 Qh
->Signature
= EHC_QH_SIG
;
192 Qh
->Interval
= Ep
->PollRate
;
194 InitializeListHead (&Qh
->Qtds
);
197 QhHw
->HorizonLink
= QH_LINK (NULL
, 0, TRUE
);
198 QhHw
->DeviceAddr
= Ep
->DevAddr
;
200 QhHw
->EpNum
= Ep
->EpAddr
;
201 QhHw
->EpSpeed
= Ep
->DevSpeed
;
203 QhHw
->ReclaimHead
= 0;
204 QhHw
->MaxPacketLen
= (UINT32
) Ep
->MaxPacket
;
206 QhHw
->NakReload
= QH_NAK_RELOAD
;
207 QhHw
->HubAddr
= Ep
->HubAddr
;
208 QhHw
->PortNum
= Ep
->HubPort
;
209 QhHw
->Multiplier
= 1;
210 QhHw
->DataToggle
= Ep
->Toggle
;
212 if (Ep
->DevSpeed
!= EFI_USB_SPEED_HIGH
) {
213 QhHw
->Status
|= QTD_STAT_DO_SS
;
217 case EHC_CTRL_TRANSFER
:
219 // Special initialization for the control transfer:
220 // 1. Control transfer initialize data toggle from each QTD
221 // 2. Set the Control Endpoint Flag (C) for low/full speed endpoint.
225 if (Ep
->DevSpeed
!= EFI_USB_SPEED_HIGH
) {
230 case EHC_INT_TRANSFER_ASYNC
:
231 case EHC_INT_TRANSFER_SYNC
:
233 // Special initialization for the interrupt transfer
234 // to set the S-Mask and C-Mask
237 EhcInitIntQh (Ep
, QhHw
);
240 case EHC_BULK_TRANSFER
:
241 if ((Ep
->DevSpeed
== EFI_USB_SPEED_HIGH
) && (Ep
->Direction
== EfiUsbDataOut
)) {
242 QhHw
->Status
|= QTD_STAT_DO_PING
;
253 Convert the poll interval from application to that
254 be used by EHCI interface data structure. Only need
255 to get the max 2^n that is less than interval. UEFI
256 can't support high speed endpoint with a interval less
257 than 8 microframe because interval is specified in
258 the unit of ms (millisecond).
260 @param Interval The interval to convert.
262 @return The converted interval.
277 // Find the index (1 based) of the highest non-zero bit
281 while (Interval
!= 0) {
286 return (UINTN
)1 << (BitCount
- 1);
293 @param Ehc The EHCI device.
294 @param Qtds The list head of the QTD.
309 EFI_LIST_FOR_EACH_SAFE (Entry
, Next
, Qtds
) {
310 Qtd
= EFI_LIST_CONTAINER (Entry
, EHC_QTD
, QtdList
);
312 RemoveEntryList (&Qtd
->QtdList
);
313 UsbHcFreeMem (Ehc
->MemPool
, Qtd
, sizeof (EHC_QTD
));
319 Free an allocated URB. It is possible for it to be partially inited.
321 @param Ehc The EHCI device.
322 @param Urb The URB to free.
333 EFI_PCI_IO_PROTOCOL
*PciIo
;
337 if (Urb
->RequestPhy
!= NULL
) {
338 PciIo
->Unmap (PciIo
, Urb
->RequestMap
);
341 if (Urb
->DataMap
!= NULL
) {
342 PciIo
->Unmap (PciIo
, Urb
->DataMap
);
345 if (Urb
->Qh
!= NULL
) {
347 // Ensure that this queue head has been unlinked from the
348 // schedule data structures. Free all the associated QTDs
350 EhcFreeQtds (Ehc
, &Urb
->Qh
->Qtds
);
351 UsbHcFreeMem (Ehc
->MemPool
, Urb
->Qh
, sizeof (EHC_QH
));
359 Create a list of QTDs for the URB.
361 @param Ehc The EHCI device.
362 @param Urb The URB to create QTDs for.
364 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for QTD.
365 @retval EFI_SUCCESS The QTDs are allocated for the URB.
385 ASSERT ((Urb
!= NULL
) && (Urb
->Qh
!= NULL
));
388 // EHCI follows the alternet next QTD pointer if it meets
389 // a short read and the AlterNext pointer is valid. UEFI
390 // EHCI driver should terminate the transfer except the
397 AlterNext
= QTD_LINK (NULL
, TRUE
);
399 if (Ep
->Direction
== EfiUsbDataIn
) {
400 AlterNext
= QTD_LINK (Ehc
->ShortReadStop
, FALSE
);
404 // Build the Setup and status packets for control transfer
406 if (Urb
->Ep
.Type
== EHC_CTRL_TRANSFER
) {
407 Len
= sizeof (EFI_USB_DEVICE_REQUEST
);
408 Qtd
= EhcCreateQtd (Ehc
, Urb
->RequestPhy
, Len
, QTD_PID_SETUP
, 0, Ep
->MaxPacket
);
411 return EFI_OUT_OF_RESOURCES
;
414 InsertTailList (&Qh
->Qtds
, &Qtd
->QtdList
);
417 // Create the status packet now. Set the AlterNext to it. So, when
418 // EHCI meets a short control read, it can resume at the status stage.
419 // Use the opposite direction of the data stage, or IN if there is
422 if (Ep
->Direction
== EfiUsbDataIn
) {
423 Pid
= QTD_PID_OUTPUT
;
428 StatusQtd
= EhcCreateQtd (Ehc
, NULL
, 0, Pid
, 1, Ep
->MaxPacket
);
430 if (StatusQtd
== NULL
) {
434 if (Ep
->Direction
== EfiUsbDataIn
) {
435 AlterNext
= QTD_LINK (StatusQtd
, FALSE
);
442 // Build the data packets for all the transfers
444 if (Ep
->Direction
== EfiUsbDataIn
) {
447 Pid
= QTD_PID_OUTPUT
;
453 while (Len
< Urb
->DataLen
) {
456 (UINT8
*) Urb
->DataPhy
+ Len
,
467 Qtd
->QtdHw
.AltNext
= AlterNext
;
468 InsertTailList (&Qh
->Qtds
, &Qtd
->QtdList
);
471 // Switch the Toggle bit if odd number of packets are included in the QTD.
473 if (((Qtd
->DataLen
+ Ep
->MaxPacket
- 1) / Ep
->MaxPacket
) % 2) {
474 Toggle
= (UINT8
) (1 - Toggle
);
481 // Insert the status packet for control transfer
483 if (Ep
->Type
== EHC_CTRL_TRANSFER
) {
484 InsertTailList (&Qh
->Qtds
, &StatusQtd
->QtdList
);
488 // OK, all the QTDs needed are created. Now, fix the NextQtd point
490 EFI_LIST_FOR_EACH (Entry
, &Qh
->Qtds
) {
491 Qtd
= EFI_LIST_CONTAINER (Entry
, EHC_QTD
, QtdList
);
494 // break if it is the last entry on the list
496 if (Entry
->ForwardLink
== &Qh
->Qtds
) {
500 NextQtd
= EFI_LIST_CONTAINER (Entry
->ForwardLink
, EHC_QTD
, QtdList
);
501 Qtd
->QtdHw
.NextQtd
= QTD_LINK (NextQtd
, FALSE
);
505 // Link the QTDs to the queue head
507 NextQtd
= EFI_LIST_CONTAINER (Qh
->Qtds
.ForwardLink
, EHC_QTD
, QtdList
);
508 Qh
->QhHw
.NextQtd
= QTD_LINK (NextQtd
, FALSE
);
512 EhcFreeQtds (Ehc
, &Qh
->Qtds
);
513 return EFI_OUT_OF_RESOURCES
;
518 Create a new URB and its associated QTD.
520 @param Ehc The EHCI device.
521 @param DevAddr The device address.
522 @param EpAddr Endpoint addrress & its direction.
523 @param DevSpeed The device speed.
524 @param Toggle Initial data toggle to use.
525 @param MaxPacket The max packet length of the endpoint.
526 @param Hub The transaction translator to use.
527 @param Type The transaction type.
528 @param Request The standard USB request for control transfer.
529 @param Data The user data to transfer.
530 @param DataLen The length of data buffer.
531 @param Callback The function to call when data is transferred.
532 @param Context The context to the callback.
533 @param Interval The interval for interrupt transfer.
535 @return Created URB or NULL.
546 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Hub
,
548 IN EFI_USB_DEVICE_REQUEST
*Request
,
551 IN EFI_ASYNC_USB_TRANSFER_CALLBACK Callback
,
557 EFI_PHYSICAL_ADDRESS PhyAddr
;
558 EFI_PCI_IO_PROTOCOL_OPERATION MapOp
;
559 EFI_PCI_IO_PROTOCOL
*PciIo
;
565 Urb
= AllocateZeroPool (sizeof (URB
));
571 Urb
->Signature
= EHC_URB_SIG
;
572 InitializeListHead (&Urb
->UrbList
);
575 Ep
->DevAddr
= DevAddr
;
576 Ep
->EpAddr
= (UINT8
) (EpAddr
& 0x0F);
577 Ep
->Direction
= (((EpAddr
& 0x80) != 0) ? EfiUsbDataIn
: EfiUsbDataOut
);
578 Ep
->DevSpeed
= DevSpeed
;
579 Ep
->MaxPacket
= MaxPacket
;
584 if (DevSpeed
!= EFI_USB_SPEED_HIGH
) {
585 ASSERT (Hub
!= NULL
);
587 Ep
->HubAddr
= Hub
->TranslatorHubAddress
;
588 Ep
->HubPort
= Hub
->TranslatorPortNumber
;
593 Ep
->PollRate
= EhcConvertPollRate (Interval
);
595 Urb
->Request
= Request
;
597 Urb
->DataLen
= DataLen
;
598 Urb
->Callback
= Callback
;
599 Urb
->Context
= Context
;
602 Urb
->Qh
= EhcCreateQh (Ehc
, &Urb
->Ep
);
604 if (Urb
->Qh
== NULL
) {
609 // Map the request and user data
611 if (Request
!= NULL
) {
612 Len
= sizeof (EFI_USB_DEVICE_REQUEST
);
613 MapOp
= EfiPciIoOperationBusMasterRead
;
614 Status
= PciIo
->Map (PciIo
, MapOp
, Request
, &Len
, &PhyAddr
, &Map
);
616 if (EFI_ERROR (Status
) || (Len
!= sizeof (EFI_USB_DEVICE_REQUEST
))) {
620 Urb
->RequestPhy
= (VOID
*) ((UINTN
) PhyAddr
);
621 Urb
->RequestMap
= Map
;
627 if (Ep
->Direction
== EfiUsbDataIn
) {
628 MapOp
= EfiPciIoOperationBusMasterWrite
;
630 MapOp
= EfiPciIoOperationBusMasterRead
;
633 Status
= PciIo
->Map (PciIo
, MapOp
, Data
, &Len
, &PhyAddr
, &Map
);
635 if (EFI_ERROR (Status
) || (Len
!= DataLen
)) {
639 Urb
->DataPhy
= (VOID
*) ((UINTN
) PhyAddr
);
643 Status
= EhcCreateQtds (Ehc
, Urb
);
645 if (EFI_ERROR (Status
)) {
652 EhcFreeUrb (Ehc
, Urb
);