3 Provides some data structure definitions used by the XHCI host controller driver.
5 Copyright (c) 2011 - 2012, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21 #include <Protocol/Usb2HostController.h>
22 #include <Protocol/PciIo.h>
24 #include <Guid/EventGroup.h>
26 #include <Library/BaseLib.h>
27 #include <Library/BaseMemoryLib.h>
28 #include <Library/UefiDriverEntryPoint.h>
29 #include <Library/UefiBootServicesTableLib.h>
30 #include <Library/MemoryAllocationLib.h>
31 #include <Library/UefiLib.h>
32 #include <Library/DebugLib.h>
33 #include <Library/ReportStatusCodeLib.h>
35 #include <IndustryStandard/Pci.h>
37 typedef struct _USB_XHCI_INSTANCE USB_XHCI_INSTANCE
;
38 typedef struct _USB_DEV_CONTEXT USB_DEV_CONTEXT
;
41 #include "XhciSched.h"
42 #include "ComponentName.h"
45 // Convert millisecond to microsecond.
47 #define XHC_1_MILLISECOND (1000)
49 // XHC generic timeout experience values.
50 // The unit is microsecond, setting it as 10ms.
52 #define XHC_GENERIC_TIMEOUT (10 * 1000)
54 // XHC reset timeout experience values.
55 // The unit is microsecond, setting it as 1s.
57 #define XHC_RESET_TIMEOUT (1000 * 1000)
59 // XHC delay experience value for polling operation.
60 // The unit is microsecond, set it as 1ms.
62 #define XHC_POLL_DELAY (1000)
64 // XHC async transfer timer interval, set by experience.
65 // The unit is 100us, takes 50ms as interval.
67 #define XHC_ASYNC_TIMER_INTERVAL EFI_TIMER_PERIOD_MILLISECONDS(50)
70 // XHC raises TPL to TPL_NOTIFY to serialize all its operations
71 // to protect shared data structures.
73 #define XHC_TPL TPL_NOTIFY
75 #define CMD_RING_TRB_NUMBER 0x100
76 #define TR_RING_TRB_NUMBER 0x100
77 #define ERST_NUMBER 0x01
78 #define EVENT_RING_TRB_NUMBER 0x200
84 #define INT_INTER_ASYNC 4
87 // Iterate through the doule linked list. This is delete-safe.
88 // Don't touch NextEntry
90 #define EFI_LIST_FOR_EACH_SAFE(Entry, NextEntry, ListHead) \
91 for (Entry = (ListHead)->ForwardLink, NextEntry = Entry->ForwardLink;\
92 Entry != (ListHead); Entry = NextEntry, NextEntry = Entry->ForwardLink)
94 #define EFI_LIST_CONTAINER(Entry, Type, Field) BASE_CR(Entry, Type, Field)
96 #define XHC_LOW_32BIT(Addr64) ((UINT32)(((UINTN)(Addr64)) & 0xFFFFFFFF))
97 #define XHC_HIGH_32BIT(Addr64) ((UINT32)(RShiftU64((UINT64)(UINTN)(Addr64), 32) & 0xFFFFFFFF))
98 #define XHC_BIT_IS_SET(Data, Bit) ((BOOLEAN)(((Data) & (Bit)) == (Bit)))
100 #define XHC_REG_BIT_IS_SET(Xhc, Offset, Bit) \
101 (XHC_BIT_IS_SET(XhcReadOpReg ((Xhc), (Offset)), (Bit)))
103 #define XHCI_IS_DATAIN(EndpointAddr) XHC_BIT_IS_SET((EndpointAddr), 0x80)
105 #define XHCI_INSTANCE_SIG SIGNATURE_32 ('x', 'h', 'c', 'i')
106 #define XHC_FROM_THIS(a) CR(a, USB_XHCI_INSTANCE, Usb2Hc, XHCI_INSTANCE_SIG)
108 #define USB_DESC_TYPE_HUB 0x29
109 #define USB_DESC_TYPE_HUB_SUPER_SPEED 0x2a
112 // The RequestType in EFI_USB_DEVICE_REQUEST is composed of
113 // three fields: One bit direction, 2 bit type, and 5 bit
116 #define USB_REQUEST_TYPE(Dir, Type, Target) \
117 ((UINT8)((((Dir) == EfiUsbDataIn ? 0x01 : 0) << 7) | (Type) | (Target)))
120 // Xhci Data and Ctrl Structures
135 UINT8 HubContrCurrent
;
137 } EFI_USB_HUB_DESCRIPTOR
;
140 struct _USB_DEV_CONTEXT
{
142 // Whether this entry in UsbDevContext array is used or not.
146 // The slot id assigned to the new device through XHCI's Enable_Slot cmd.
150 // The route string presented an attached usb device.
152 USB_DEV_ROUTE RouteString
;
154 // The route string of parent device if it exists. Otherwise it's zero.
156 USB_DEV_ROUTE ParentRouteString
;
158 // The actual device address assigned by XHCI through Address_Device command.
162 // The requested device address from UsbBus driver through Set_Address standard usb request.
163 // As XHCI spec replaces this request with Address_Device command, we have to record the
164 // requested device address and establish a mapping relationship with the actual device address.
165 // Then UsbBus driver just need to be aware of the requested device address to access usb device
166 // through EFI_USB2_HC_PROTOCOL. Xhci driver would be responsible for translating it to actual
167 // device address and access the actual device.
171 // The pointer to the input device context.
175 // The pointer to the output device context.
179 // The transfer queue for every endpoint.
181 VOID
*EndpointTransferRing
[31];
183 // The device descriptor which is stored to support XHCI's Evaluate_Context cmd.
185 EFI_USB_DEVICE_DESCRIPTOR DevDesc
;
187 // As a usb device may include multiple configuration descriptors, we dynamically allocate an array
189 // Note that every configuration descriptor stored here includes those lower level descriptors,
190 // such as Interface descriptor, Endpoint descriptor, and so on.
191 // These information is used to support XHCI's Config_Endpoint cmd.
193 EFI_USB_CONFIG_DESCRIPTOR
**ConfDesc
;
196 struct _USB_XHCI_INSTANCE
{
198 EFI_PCI_IO_PROTOCOL
*PciIo
;
199 UINT64 OriginalPciAttributes
;
201 EFI_USB2_HC_PROTOCOL Usb2Hc
;
203 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
206 // ExitBootServicesEvent is used to set OS semaphore and
207 // stop the XHC DMA operation after exit boot service.
209 EFI_EVENT ExitBootServiceEvent
;
211 LIST_ENTRY AsyncIntTransfers
;
213 UINT8 CapLength
; ///< Capability Register Length
214 XHC_HCSPARAMS1 HcSParams1
; ///< Structural Parameters 1
215 XHC_HCSPARAMS2 HcSParams2
; ///< Structural Parameters 2
216 XHC_HCCPARAMS HcCParams
; ///< Capability Parameters
217 UINT32 DBOff
; ///< Doorbell Offset
218 UINT32 RTSOff
; ///< Runtime Register Space Offset
222 UINT32 MaxScratchpadBufs
;
223 UINT32 ExtCapRegBase
;
224 UINT32 UsbLegSupOffset
;
230 TRANSFER_RING CmdRing
;
234 EVENT_RING EventRing
;
238 EFI_UNICODE_STRING_TABLE
*ControllerNameTable
;
241 // Store device contexts managed by XHCI instance
242 // The array supports up to 255 devices, entry 0 is reserved and should not be used.
244 USB_DEV_CONTEXT UsbDevContext
[256];
248 extern EFI_DRIVER_BINDING_PROTOCOL gXhciDriverBinding
;
249 extern EFI_COMPONENT_NAME_PROTOCOL gXhciComponentName
;
250 extern EFI_COMPONENT_NAME2_PROTOCOL gXhciComponentName2
;
253 Test to see if this driver supports ControllerHandle. Any
254 ControllerHandle that has Usb2HcProtocol installed will
257 @param This Protocol instance pointer.
258 @param Controller Handle of device to test.
259 @param RemainingDevicePath Not used.
261 @return EFI_SUCCESS This driver supports this device.
262 @return EFI_UNSUPPORTED This driver does not support this device.
267 XhcDriverBindingSupported (
268 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
269 IN EFI_HANDLE Controller
,
270 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
274 Starting the Usb XHCI Driver.
276 @param This Protocol instance pointer.
277 @param Controller Handle of device to test.
278 @param RemainingDevicePath Not used.
280 @return EFI_SUCCESS supports this device.
281 @return EFI_UNSUPPORTED do not support this device.
282 @return EFI_DEVICE_ERROR cannot be started due to device Error.
283 @return EFI_OUT_OF_RESOURCES cannot allocate resources.
288 XhcDriverBindingStart (
289 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
290 IN EFI_HANDLE Controller
,
291 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
295 Stop this driver on ControllerHandle. Support stoping any child handles
296 created by this driver.
298 @param This Protocol instance pointer.
299 @param Controller Handle of device to stop driver on.
300 @param NumberOfChildren Number of Children in the ChildHandleBuffer.
301 @param ChildHandleBuffer List of handles for the children we need to stop.
303 @return EFI_SUCCESS Success.
304 @return EFI_DEVICE_ERROR Fail.
309 XhcDriverBindingStop (
310 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
311 IN EFI_HANDLE Controller
,
312 IN UINTN NumberOfChildren
,
313 IN EFI_HANDLE
*ChildHandleBuffer
317 Retrieves the capability of root hub ports.
319 @param This The EFI_USB2_HC_PROTOCOL instance.
320 @param MaxSpeed Max speed supported by the controller.
321 @param PortNumber Number of the root hub ports.
322 @param Is64BitCapable Whether the controller supports 64-bit memory
325 @retval EFI_SUCCESS Host controller capability were retrieved successfully.
326 @retval EFI_INVALID_PARAMETER Either of the three capability pointer is NULL.
332 IN EFI_USB2_HC_PROTOCOL
*This
,
334 OUT UINT8
*PortNumber
,
335 OUT UINT8
*Is64BitCapable
339 Provides software reset for the USB host controller.
341 @param This This EFI_USB2_HC_PROTOCOL instance.
342 @param Attributes A bit mask of the reset operation to perform.
344 @retval EFI_SUCCESS The reset operation succeeded.
345 @retval EFI_INVALID_PARAMETER Attributes is not valid.
346 @retval EFI_UNSUPPOURTED The type of reset specified by Attributes is
347 not currently supported by the host controller.
348 @retval EFI_DEVICE_ERROR Host controller isn't halted to reset.
354 IN EFI_USB2_HC_PROTOCOL
*This
,
359 Retrieve the current state of the USB host controller.
361 @param This This EFI_USB2_HC_PROTOCOL instance.
362 @param State Variable to return the current host controller
365 @retval EFI_SUCCESS Host controller state was returned in State.
366 @retval EFI_INVALID_PARAMETER State is NULL.
367 @retval EFI_DEVICE_ERROR An error was encountered while attempting to
368 retrieve the host controller's current state.
374 IN EFI_USB2_HC_PROTOCOL
*This
,
375 OUT EFI_USB_HC_STATE
*State
379 Sets the USB host controller to a specific state.
381 @param This This EFI_USB2_HC_PROTOCOL instance.
382 @param State The state of the host controller that will be set.
384 @retval EFI_SUCCESS The USB host controller was successfully placed
385 in the state specified by State.
386 @retval EFI_INVALID_PARAMETER State is invalid.
387 @retval EFI_DEVICE_ERROR Failed to set the state due to device error.
393 IN EFI_USB2_HC_PROTOCOL
*This
,
394 IN EFI_USB_HC_STATE State
398 Retrieves the current status of a USB root hub port.
400 @param This This EFI_USB2_HC_PROTOCOL instance.
401 @param PortNumber The root hub port to retrieve the state from.
402 This value is zero-based.
403 @param PortStatus Variable to receive the port state.
405 @retval EFI_SUCCESS The status of the USB root hub port specified.
406 by PortNumber was returned in PortStatus.
407 @retval EFI_INVALID_PARAMETER PortNumber is invalid.
408 @retval EFI_DEVICE_ERROR Can't read register.
413 XhcGetRootHubPortStatus (
414 IN EFI_USB2_HC_PROTOCOL
*This
,
416 OUT EFI_USB_PORT_STATUS
*PortStatus
420 Sets a feature for the specified root hub port.
422 @param This This EFI_USB2_HC_PROTOCOL instance.
423 @param PortNumber Root hub port to set.
424 @param PortFeature Feature to set.
426 @retval EFI_SUCCESS The feature specified by PortFeature was set.
427 @retval EFI_INVALID_PARAMETER PortNumber is invalid or PortFeature is invalid.
428 @retval EFI_DEVICE_ERROR Can't read register.
433 XhcSetRootHubPortFeature (
434 IN EFI_USB2_HC_PROTOCOL
*This
,
436 IN EFI_USB_PORT_FEATURE PortFeature
440 Clears a feature for the specified root hub port.
442 @param This A pointer to the EFI_USB2_HC_PROTOCOL instance.
443 @param PortNumber Specifies the root hub port whose feature is
444 requested to be cleared.
445 @param PortFeature Indicates the feature selector associated with the
446 feature clear request.
448 @retval EFI_SUCCESS The feature specified by PortFeature was cleared
449 for the USB root hub port specified by PortNumber.
450 @retval EFI_INVALID_PARAMETER PortNumber is invalid or PortFeature is invalid.
451 @retval EFI_DEVICE_ERROR Can't read register.
456 XhcClearRootHubPortFeature (
457 IN EFI_USB2_HC_PROTOCOL
*This
,
459 IN EFI_USB_PORT_FEATURE PortFeature
463 Submits control transfer to a target USB device.
465 @param This This EFI_USB2_HC_PROTOCOL instance.
466 @param DeviceAddress The target device address.
467 @param DeviceSpeed Target device speed.
468 @param MaximumPacketLength Maximum packet size the default control transfer
469 endpoint is capable of sending or receiving.
470 @param Request USB device request to send.
471 @param TransferDirection Specifies the data direction for the data stage
472 @param Data Data buffer to be transmitted or received from USB
474 @param DataLength The size (in bytes) of the data buffer.
475 @param Timeout Indicates the maximum timeout, in millisecond.
476 @param Translator Transaction translator to be used by this device.
477 @param TransferResult Return the result of this control transfer.
479 @retval EFI_SUCCESS Transfer was completed successfully.
480 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resources.
481 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
482 @retval EFI_TIMEOUT Transfer failed due to timeout.
483 @retval EFI_DEVICE_ERROR Transfer failed due to host controller or device error.
489 IN EFI_USB2_HC_PROTOCOL
*This
,
490 IN UINT8 DeviceAddress
,
491 IN UINT8 DeviceSpeed
,
492 IN UINTN MaximumPacketLength
,
493 IN EFI_USB_DEVICE_REQUEST
*Request
,
494 IN EFI_USB_DATA_DIRECTION TransferDirection
,
496 IN OUT UINTN
*DataLength
,
498 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
499 OUT UINT32
*TransferResult
503 Submits bulk transfer to a bulk endpoint of a USB device.
505 @param This This EFI_USB2_HC_PROTOCOL instance.
506 @param DeviceAddress Target device address.
507 @param EndPointAddress Endpoint number and its direction in bit 7.
508 @param DeviceSpeed Device speed, Low speed device doesn't support bulk
510 @param MaximumPacketLength Maximum packet size the endpoint is capable of
511 sending or receiving.
512 @param DataBuffersNumber Number of data buffers prepared for the transfer.
513 @param Data Array of pointers to the buffers of data to transmit
514 from or receive into.
515 @param DataLength The lenght of the data buffer.
516 @param DataToggle On input, the initial data toggle for the transfer;
517 On output, it is updated to to next data toggle to
518 use of the subsequent bulk transfer.
519 @param Timeout Indicates the maximum time, in millisecond, which
520 the transfer is allowed to complete.
521 @param Translator A pointr to the transaction translator data.
522 @param TransferResult A pointer to the detailed result information of the
525 @retval EFI_SUCCESS The transfer was completed successfully.
526 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resource.
527 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
528 @retval EFI_TIMEOUT The transfer failed due to timeout.
529 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.
535 IN EFI_USB2_HC_PROTOCOL
*This
,
536 IN UINT8 DeviceAddress
,
537 IN UINT8 EndPointAddress
,
538 IN UINT8 DeviceSpeed
,
539 IN UINTN MaximumPacketLength
,
540 IN UINT8 DataBuffersNumber
,
541 IN OUT VOID
*Data
[EFI_USB_MAX_BULK_BUFFER_NUM
],
542 IN OUT UINTN
*DataLength
,
543 IN OUT UINT8
*DataToggle
,
545 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
546 OUT UINT32
*TransferResult
550 Submits an asynchronous interrupt transfer to an
551 interrupt endpoint of a USB device.
553 @param This This EFI_USB2_HC_PROTOCOL instance.
554 @param DeviceAddress Target device address.
555 @param EndPointAddress Endpoint number and its direction encoded in bit 7
556 @param DeviceSpeed Indicates device speed.
557 @param MaximumPacketLength Maximum packet size the target endpoint is capable
558 @param IsNewTransfer If TRUE, to submit an new asynchronous interrupt
559 transfer If FALSE, to remove the specified
560 asynchronous interrupt.
561 @param DataToggle On input, the initial data toggle to use; on output,
562 it is updated to indicate the next data toggle.
563 @param PollingInterval The he interval, in milliseconds, that the transfer
565 @param DataLength The length of data to receive at the rate specified
567 @param Translator Transaction translator to use.
568 @param CallBackFunction Function to call at the rate specified by
570 @param Context Context to CallBackFunction.
572 @retval EFI_SUCCESS The request has been successfully submitted or canceled.
573 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
574 @retval EFI_OUT_OF_RESOURCES The request failed due to a lack of resources.
575 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.
580 XhcAsyncInterruptTransfer (
581 IN EFI_USB2_HC_PROTOCOL
*This
,
582 IN UINT8 DeviceAddress
,
583 IN UINT8 EndPointAddress
,
584 IN UINT8 DeviceSpeed
,
585 IN UINTN MaximumPacketLength
,
586 IN BOOLEAN IsNewTransfer
,
587 IN OUT UINT8
*DataToggle
,
588 IN UINTN PollingInterval
,
590 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
591 IN EFI_ASYNC_USB_TRANSFER_CALLBACK CallBackFunction
,
592 IN VOID
*Context OPTIONAL
596 Submits synchronous interrupt transfer to an interrupt endpoint
599 @param This This EFI_USB2_HC_PROTOCOL instance.
600 @param DeviceAddress Target device address.
601 @param EndPointAddress Endpoint number and its direction encoded in bit 7
602 @param DeviceSpeed Indicates device speed.
603 @param MaximumPacketLength Maximum packet size the target endpoint is capable
604 of sending or receiving.
605 @param Data Buffer of data that will be transmitted to USB
606 device or received from USB device.
607 @param DataLength On input, the size, in bytes, of the data buffer; On
608 output, the number of bytes transferred.
609 @param DataToggle On input, the initial data toggle to use; on output,
610 it is updated to indicate the next data toggle.
611 @param Timeout Maximum time, in second, to complete.
612 @param Translator Transaction translator to use.
613 @param TransferResult Variable to receive the transfer result.
615 @return EFI_SUCCESS The transfer was completed successfully.
616 @return EFI_OUT_OF_RESOURCES The transfer failed due to lack of resource.
617 @return EFI_INVALID_PARAMETER Some parameters are invalid.
618 @return EFI_TIMEOUT The transfer failed due to timeout.
619 @return EFI_DEVICE_ERROR The failed due to host controller or device error
624 XhcSyncInterruptTransfer (
625 IN EFI_USB2_HC_PROTOCOL
*This
,
626 IN UINT8 DeviceAddress
,
627 IN UINT8 EndPointAddress
,
628 IN UINT8 DeviceSpeed
,
629 IN UINTN MaximumPacketLength
,
631 IN OUT UINTN
*DataLength
,
632 IN OUT UINT8
*DataToggle
,
634 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
635 OUT UINT32
*TransferResult
639 Submits isochronous transfer to a target USB device.
641 @param This This EFI_USB2_HC_PROTOCOL instance.
642 @param DeviceAddress Target device address.
643 @param EndPointAddress End point address with its direction.
644 @param DeviceSpeed Device speed, Low speed device doesn't support this
646 @param MaximumPacketLength Maximum packet size that the endpoint is capable of
647 sending or receiving.
648 @param DataBuffersNumber Number of data buffers prepared for the transfer.
649 @param Data Array of pointers to the buffers of data that will
650 be transmitted to USB device or received from USB
652 @param DataLength The size, in bytes, of the data buffer.
653 @param Translator Transaction translator to use.
654 @param TransferResult Variable to receive the transfer result.
656 @return EFI_UNSUPPORTED Isochronous transfer is unsupported.
661 XhcIsochronousTransfer (
662 IN EFI_USB2_HC_PROTOCOL
*This
,
663 IN UINT8 DeviceAddress
,
664 IN UINT8 EndPointAddress
,
665 IN UINT8 DeviceSpeed
,
666 IN UINTN MaximumPacketLength
,
667 IN UINT8 DataBuffersNumber
,
668 IN OUT VOID
*Data
[EFI_USB_MAX_ISO_BUFFER_NUM
],
670 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
671 OUT UINT32
*TransferResult
675 Submits Async isochronous transfer to a target USB device.
677 @param This This EFI_USB2_HC_PROTOCOL instance.
678 @param DeviceAddress Target device address.
679 @param EndPointAddress End point address with its direction.
680 @param DeviceSpeed Device speed, Low speed device doesn't support this
682 @param MaximumPacketLength Maximum packet size that the endpoint is capable of
683 sending or receiving.
684 @param DataBuffersNumber Number of data buffers prepared for the transfer.
685 @param Data Array of pointers to the buffers of data that will
686 be transmitted to USB device or received from USB
688 @param DataLength The size, in bytes, of the data buffer.
689 @param Translator Transaction translator to use.
690 @param IsochronousCallBack Function to be called when the transfer complete.
691 @param Context Context passed to the call back function as
694 @return EFI_UNSUPPORTED Isochronous transfer isn't supported.
699 XhcAsyncIsochronousTransfer (
700 IN EFI_USB2_HC_PROTOCOL
*This
,
701 IN UINT8 DeviceAddress
,
702 IN UINT8 EndPointAddress
,
703 IN UINT8 DeviceSpeed
,
704 IN UINTN MaximumPacketLength
,
705 IN UINT8 DataBuffersNumber
,
706 IN OUT VOID
*Data
[EFI_USB_MAX_ISO_BUFFER_NUM
],
708 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
709 IN EFI_ASYNC_USB_TRANSFER_CALLBACK IsochronousCallBack
,