3 This file contains URB request, each request is warpped in a
4 URB (Usb Request Block).
6 Copyright (c) 2007 - 2009, 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.
136 // Because UEFI interface can't utilitize an endpoint with
137 // poll rate faster than 1ms, only need to set one bit in
138 // the queue head. simple. But it may be changed later. If
139 // sub-1ms interrupt is supported, need to update the S-Mask
142 if (Ep
->DevSpeed
== EFI_USB_SPEED_HIGH
) {
143 QhHw
->SMask
= QH_MICROFRAME_0
;
148 // For low/full speed device, the transfer must go through
149 // the split transaction. Need to update three fields
150 // 1. SplitXState in the status
151 // 2. Microframe S-Mask
152 // 3. Microframe C-Mask
153 // UEFI USB doesn't exercise admission control. It simplely
154 // schedule the high speed transactions in microframe 0, and
155 // full/low speed transactions at microframe 1. This also
156 // avoid the use of FSTN.
158 QhHw
->SMask
= QH_MICROFRAME_1
;
159 QhHw
->CMask
= QH_MICROFRAME_3
| QH_MICROFRAME_4
| QH_MICROFRAME_5
;
165 Allocate and initialize a EHCI queue head.
167 @param Ehci The EHCI device.
168 @param Ep The endpoint to create queue head for.
170 @return Created queue head or NULL if failed to create one.
175 IN USB2_HC_DEV
*Ehci
,
182 Qh
= UsbHcAllocateMem (Ehci
->MemPool
, sizeof (EHC_QH
));
188 Qh
->Signature
= EHC_QH_SIG
;
190 Qh
->Interval
= Ep
->PollRate
;
192 InitializeListHead (&Qh
->Qtds
);
195 QhHw
->HorizonLink
= QH_LINK (NULL
, 0, TRUE
);
196 QhHw
->DeviceAddr
= Ep
->DevAddr
;
198 QhHw
->EpNum
= Ep
->EpAddr
;
199 QhHw
->EpSpeed
= Ep
->DevSpeed
;
201 QhHw
->ReclaimHead
= 0;
202 QhHw
->MaxPacketLen
= (UINT32
) Ep
->MaxPacket
;
204 QhHw
->NakReload
= QH_NAK_RELOAD
;
205 QhHw
->HubAddr
= Ep
->HubAddr
;
206 QhHw
->PortNum
= Ep
->HubPort
;
207 QhHw
->Multiplier
= 1;
208 QhHw
->DataToggle
= Ep
->Toggle
;
210 if (Ep
->DevSpeed
!= EFI_USB_SPEED_HIGH
) {
211 QhHw
->Status
|= QTD_STAT_DO_SS
;
215 case EHC_CTRL_TRANSFER
:
217 // Special initialization for the control transfer:
218 // 1. Control transfer initialize data toggle from each QTD
219 // 2. Set the Control Endpoint Flag (C) for low/full speed endpoint.
223 if (Ep
->DevSpeed
!= EFI_USB_SPEED_HIGH
) {
228 case EHC_INT_TRANSFER_ASYNC
:
229 case EHC_INT_TRANSFER_SYNC
:
231 // Special initialization for the interrupt transfer
232 // to set the S-Mask and C-Mask
235 EhcInitIntQh (Ep
, QhHw
);
238 case EHC_BULK_TRANSFER
:
239 if ((Ep
->DevSpeed
== EFI_USB_SPEED_HIGH
) && (Ep
->Direction
== EfiUsbDataOut
)) {
240 QhHw
->Status
|= QTD_STAT_DO_PING
;
251 Convert the poll interval from application to that
252 be used by EHCI interface data structure. Only need
253 to get the max 2^n that is less than interval. UEFI
254 can't support high speed endpoint with a interval less
255 than 8 microframe because interval is specified in
256 the unit of ms (millisecond).
258 @param Interval The interval to convert.
260 @return The converted interval.
275 // Find the index (1 based) of the highest non-zero bit
279 while (Interval
!= 0) {
284 return (UINTN
)1 << (BitCount
- 1);
291 @param Ehc The EHCI device.
292 @param Qtds The list head of the QTD.
305 EFI_LIST_FOR_EACH_SAFE (Entry
, Next
, Qtds
) {
306 Qtd
= EFI_LIST_CONTAINER (Entry
, EHC_QTD
, QtdList
);
308 RemoveEntryList (&Qtd
->QtdList
);
309 UsbHcFreeMem (Ehc
->MemPool
, Qtd
, sizeof (EHC_QTD
));
315 Free an allocated URB. It is possible for it to be partially inited.
317 @param Ehc The EHCI device.
318 @param Urb The URB to free.
327 EFI_PCI_IO_PROTOCOL
*PciIo
;
331 if (Urb
->RequestPhy
!= NULL
) {
332 PciIo
->Unmap (PciIo
, Urb
->RequestMap
);
335 if (Urb
->DataMap
!= NULL
) {
336 PciIo
->Unmap (PciIo
, Urb
->DataMap
);
339 if (Urb
->Qh
!= NULL
) {
341 // Ensure that this queue head has been unlinked from the
342 // schedule data structures. Free all the associated QTDs
344 EhcFreeQtds (Ehc
, &Urb
->Qh
->Qtds
);
345 UsbHcFreeMem (Ehc
->MemPool
, Urb
->Qh
, sizeof (EHC_QH
));
353 Create a list of QTDs for the URB.
355 @param Ehc The EHCI device.
356 @param Urb The URB to create QTDs for.
358 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for QTD.
359 @retval EFI_SUCCESS The QTDs are allocated for the URB.
379 ASSERT ((Urb
!= NULL
) && (Urb
->Qh
!= NULL
));
382 // EHCI follows the alternet next QTD pointer if it meets
383 // a short read and the AlterNext pointer is valid. UEFI
384 // EHCI driver should terminate the transfer except the
391 AlterNext
= QTD_LINK (NULL
, TRUE
);
393 if (Ep
->Direction
== EfiUsbDataIn
) {
394 AlterNext
= QTD_LINK (Ehc
->ShortReadStop
, FALSE
);
398 // Build the Setup and status packets for control transfer
400 if (Urb
->Ep
.Type
== EHC_CTRL_TRANSFER
) {
401 Len
= sizeof (EFI_USB_DEVICE_REQUEST
);
402 Qtd
= EhcCreateQtd (Ehc
, Urb
->RequestPhy
, Len
, QTD_PID_SETUP
, 0, Ep
->MaxPacket
);
405 return EFI_OUT_OF_RESOURCES
;
408 InsertTailList (&Qh
->Qtds
, &Qtd
->QtdList
);
411 // Create the status packet now. Set the AlterNext to it. So, when
412 // EHCI meets a short control read, it can resume at the status stage.
413 // Use the opposite direction of the data stage, or IN if there is
416 if (Ep
->Direction
== EfiUsbDataIn
) {
417 Pid
= QTD_PID_OUTPUT
;
422 StatusQtd
= EhcCreateQtd (Ehc
, NULL
, 0, Pid
, 1, Ep
->MaxPacket
);
424 if (StatusQtd
== NULL
) {
428 if (Ep
->Direction
== EfiUsbDataIn
) {
429 AlterNext
= QTD_LINK (StatusQtd
, FALSE
);
436 // Build the data packets for all the transfers
438 if (Ep
->Direction
== EfiUsbDataIn
) {
441 Pid
= QTD_PID_OUTPUT
;
447 while (Len
< Urb
->DataLen
) {
450 (UINT8
*) Urb
->DataPhy
+ Len
,
461 Qtd
->QtdHw
.AltNext
= AlterNext
;
462 InsertTailList (&Qh
->Qtds
, &Qtd
->QtdList
);
465 // Switch the Toggle bit if odd number of packets are included in the QTD.
467 if (((Qtd
->DataLen
+ Ep
->MaxPacket
- 1) / Ep
->MaxPacket
) % 2) {
468 Toggle
= (UINT8
) (1 - Toggle
);
475 // Insert the status packet for control transfer
477 if (Ep
->Type
== EHC_CTRL_TRANSFER
) {
478 InsertTailList (&Qh
->Qtds
, &StatusQtd
->QtdList
);
482 // OK, all the QTDs needed are created. Now, fix the NextQtd point
484 EFI_LIST_FOR_EACH (Entry
, &Qh
->Qtds
) {
485 Qtd
= EFI_LIST_CONTAINER (Entry
, EHC_QTD
, QtdList
);
488 // break if it is the last entry on the list
490 if (Entry
->ForwardLink
== &Qh
->Qtds
) {
494 NextQtd
= EFI_LIST_CONTAINER (Entry
->ForwardLink
, EHC_QTD
, QtdList
);
495 Qtd
->QtdHw
.NextQtd
= QTD_LINK (NextQtd
, FALSE
);
499 // Link the QTDs to the queue head
501 NextQtd
= EFI_LIST_CONTAINER (Qh
->Qtds
.ForwardLink
, EHC_QTD
, QtdList
);
502 Qh
->QhHw
.NextQtd
= QTD_LINK (NextQtd
, FALSE
);
506 EhcFreeQtds (Ehc
, &Qh
->Qtds
);
507 return EFI_OUT_OF_RESOURCES
;
512 Create a new URB and its associated QTD.
514 @param Ehc The EHCI device.
515 @param DevAddr The device address.
516 @param EpAddr Endpoint addrress & its direction.
517 @param DevSpeed The device speed.
518 @param Toggle Initial data toggle to use.
519 @param MaxPacket The max packet length of the endpoint.
520 @param Hub The transaction translator to use.
521 @param Type The transaction type.
522 @param Request The standard USB request for control transfer.
523 @param Data The user data to transfer.
524 @param DataLen The length of data buffer.
525 @param Callback The function to call when data is transferred.
526 @param Context The context to the callback.
527 @param Interval The interval for interrupt transfer.
529 @return Created URB or NULL.
540 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Hub
,
542 IN EFI_USB_DEVICE_REQUEST
*Request
,
545 IN EFI_ASYNC_USB_TRANSFER_CALLBACK Callback
,
551 EFI_PHYSICAL_ADDRESS PhyAddr
;
552 EFI_PCI_IO_PROTOCOL_OPERATION MapOp
;
553 EFI_PCI_IO_PROTOCOL
*PciIo
;
559 Urb
= AllocateZeroPool (sizeof (URB
));
565 Urb
->Signature
= EHC_URB_SIG
;
566 InitializeListHead (&Urb
->UrbList
);
569 Ep
->DevAddr
= DevAddr
;
570 Ep
->EpAddr
= (UINT8
) (EpAddr
& 0x0F);
571 Ep
->Direction
= (((EpAddr
& 0x80) != 0) ? EfiUsbDataIn
: EfiUsbDataOut
);
572 Ep
->DevSpeed
= DevSpeed
;
573 Ep
->MaxPacket
= MaxPacket
;
578 if (DevSpeed
!= EFI_USB_SPEED_HIGH
) {
579 ASSERT (Hub
!= NULL
);
581 Ep
->HubAddr
= Hub
->TranslatorHubAddress
;
582 Ep
->HubPort
= Hub
->TranslatorPortNumber
;
587 Ep
->PollRate
= EhcConvertPollRate (Interval
);
589 Urb
->Request
= Request
;
591 Urb
->DataLen
= DataLen
;
592 Urb
->Callback
= Callback
;
593 Urb
->Context
= Context
;
596 Urb
->Qh
= EhcCreateQh (Ehc
, &Urb
->Ep
);
598 if (Urb
->Qh
== NULL
) {
603 // Map the request and user data
605 if (Request
!= NULL
) {
606 Len
= sizeof (EFI_USB_DEVICE_REQUEST
);
607 MapOp
= EfiPciIoOperationBusMasterRead
;
608 Status
= PciIo
->Map (PciIo
, MapOp
, Request
, &Len
, &PhyAddr
, &Map
);
610 if (EFI_ERROR (Status
) || (Len
!= sizeof (EFI_USB_DEVICE_REQUEST
))) {
614 Urb
->RequestPhy
= (VOID
*) ((UINTN
) PhyAddr
);
615 Urb
->RequestMap
= Map
;
621 if (Ep
->Direction
== EfiUsbDataIn
) {
622 MapOp
= EfiPciIoOperationBusMasterWrite
;
624 MapOp
= EfiPciIoOperationBusMasterRead
;
627 Status
= PciIo
->Map (PciIo
, MapOp
, Data
, &Len
, &PhyAddr
, &Map
);
629 if (EFI_ERROR (Status
) || (Len
!= DataLen
)) {
633 Urb
->DataPhy
= (VOID
*) ((UINTN
) PhyAddr
);
637 Status
= EhcCreateQtds (Ehc
, Urb
);
639 if (EFI_ERROR (Status
)) {
646 EhcFreeUrb (Ehc
, Urb
);