3 Usb Bus Driver Binding and Bus IO Protocol.
5 Copyright (c) 2004 - 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.
16 #ifndef _EFI_USB_BUS_H_
17 #define _EFI_USB_BUS_H_
22 #include <Protocol/Usb2HostController.h>
23 #include <Protocol/UsbHostController.h>
24 #include <Protocol/UsbIo.h>
25 #include <Protocol/DevicePath.h>
27 #include <Library/BaseLib.h>
28 #include <Library/DebugLib.h>
29 #include <Library/BaseMemoryLib.h>
30 #include <Library/UefiDriverEntryPoint.h>
31 #include <Library/UefiBootServicesTableLib.h>
32 #include <Library/UefiLib.h>
33 #include <Library/DevicePathLib.h>
34 #include <Library/MemoryAllocationLib.h>
35 #include <Library/ReportStatusCodeLib.h>
38 #include <IndustryStandard/Usb.h>
40 typedef struct _USB_DEVICE USB_DEVICE
;
41 typedef struct _USB_INTERFACE USB_INTERFACE
;
42 typedef struct _USB_BUS USB_BUS
;
43 typedef struct _USB_HUB_API USB_HUB_API
;
46 #include "UsbUtility.h"
49 #include "UsbEnumer.h"
52 // USB bus timeout experience values
55 #define USB_MAX_LANG_ID 16
56 #define USB_MAX_INTERFACE 16
57 #define USB_MAX_DEVICES 128
59 #define USB_BUS_1_MILLISECOND 1000
62 // Roothub and hub's polling interval, set by experience,
63 // The unit of roothub is 100us, means 100ms as interval, and
64 // the unit of hub is 1ms, means 64ms as interval.
66 #define USB_ROOTHUB_POLL_INTERVAL (100 * 10000U)
67 #define USB_HUB_POLL_INTERVAL 64
70 // Wait for port stable to work, refers to specification
73 #define USB_WAIT_PORT_STABLE_STALL (100 * USB_BUS_1_MILLISECOND)
76 // Wait for port statue reg change, set by experience
78 #define USB_WAIT_PORT_STS_CHANGE_STALL (100)
81 // Wait for set device address, refers to specification
82 // [USB20-9.2.6.3, it says 2ms]
84 #define USB_SET_DEVICE_ADDRESS_STALL (2 * USB_BUS_1_MILLISECOND)
87 // Wait for retry max packet size, set by experience
89 #define USB_RETRY_MAX_PACK_SIZE_STALL (100 * USB_BUS_1_MILLISECOND)
92 // Wait for hub port power-on, refers to specification
95 #define USB_SET_PORT_POWER_STALL (2 * USB_BUS_1_MILLISECOND)
98 // Wait for port reset, refers to specification
99 // [USB20-7.1.7.5, it says 10ms for hub and 50ms for
102 // According to USB2.0, Chapter 11.5.1.5 Resetting,
103 // the worst case for TDRST is 20ms
105 #define USB_SET_PORT_RESET_STALL (20 * USB_BUS_1_MILLISECOND)
106 #define USB_SET_ROOT_PORT_RESET_STALL (50 * USB_BUS_1_MILLISECOND)
109 // Wait for port recovery to accept SetAddress, refers to specification
110 // [USB20-7.1.7.5, it says 10 ms for TRSTRCY]
112 #define USB_SET_PORT_RECOVERY_STALL (10 * USB_BUS_1_MILLISECOND)
115 // Wait for clear roothub port reset, set by experience
117 #define USB_CLR_ROOT_PORT_RESET_STALL (20 * USB_BUS_1_MILLISECOND)
120 // Wait for set roothub port enable, set by experience
122 #define USB_SET_ROOT_PORT_ENABLE_STALL (20 * USB_BUS_1_MILLISECOND)
125 // Send general device request timeout.
127 // The USB Specification 2.0, section 11.24.1 recommends a value of
128 // 50 milliseconds. We use a value of 500 milliseconds to work
129 // around slower hubs and devices.
131 #define USB_GENERAL_DEVICE_REQUEST_TIMEOUT 500
134 // Send clear feature request timeout, set by experience
136 #define USB_CLEAR_FEATURE_REQUEST_TIMEOUT 10
139 // Bus raises TPL to TPL_NOTIFY to serialize all its operations
140 // to protect shared data structures.
142 #define USB_BUS_TPL TPL_NOTIFY
144 #define USB_INTERFACE_SIGNATURE SIGNATURE_32 ('U', 'S', 'B', 'I')
145 #define USB_BUS_SIGNATURE SIGNATURE_32 ('U', 'S', 'B', 'B')
147 #define USB_BIT(a) ((UINTN)(1 << (a)))
148 #define USB_BIT_IS_SET(Data, Bit) ((BOOLEAN)(((Data) & (Bit)) == (Bit)))
150 #define USB_INTERFACE_FROM_USBIO(a) \
151 CR(a, USB_INTERFACE, UsbIo, USB_INTERFACE_SIGNATURE)
153 #define USB_BUS_FROM_THIS(a) \
154 CR(a, USB_BUS, BusId, USB_BUS_SIGNATURE)
157 // Used to locate USB_BUS
158 // UsbBusProtocol is the private protocol.
159 // gEfiCallerIdGuid will be used as its protocol guid.
161 typedef struct _EFI_USB_BUS_PROTOCOL
{
163 } EFI_USB_BUS_PROTOCOL
;
167 // Stands for the real USB device. Each device may
168 // has several seperately working interfaces.
174 // Configuration information
181 // The device's descriptors and its configuration
183 USB_DEVICE_DESC
*DevDesc
;
184 USB_CONFIG_DESC
*ActiveConfig
;
186 UINT16 LangId
[USB_MAX_LANG_ID
];
189 UINT8 NumOfInterface
;
190 USB_INTERFACE
*Interfaces
[USB_MAX_INTERFACE
];
193 // Parent child relationship
195 EFI_USB2_HC_TRANSACTION_TRANSLATOR Translator
;
198 USB_INTERFACE
*ParentIf
;
199 UINT8 ParentPort
; // Start at 0
201 BOOLEAN DisconnectFail
;
205 // Stands for different functions of USB device
207 struct _USB_INTERFACE
{
210 USB_INTERFACE_DESC
*IfDesc
;
211 USB_INTERFACE_SETTING
*IfSetting
;
214 // Handles and protocols
217 EFI_USB_IO_PROTOCOL UsbIo
;
218 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
222 // Hub device special data
230 // Data used only by normal hub devices
232 USB_ENDPOINT_DESC
*HubEp
;
236 // Data used only by root hub to hand over device to
237 // companion UHCI driver if low/full speed devices are
238 // connected to EHCI.
244 // Stands for the current USB Bus
248 EFI_USB_BUS_PROTOCOL BusId
;
251 // Managed USB host controller
253 EFI_HANDLE HostHandle
;
254 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
255 EFI_USB2_HC_PROTOCOL
*Usb2Hc
;
256 EFI_USB_HC_PROTOCOL
*UsbHc
;
259 // Recorded the max supported usb devices.
260 // XHCI can support up to 255 devices.
261 // EHCI/UHCI/OHCI supports up to 127 devices.
265 // An array of device that is on the bus. Devices[0] is
266 // for root hub. Device with address i is at Devices[i].
268 USB_DEVICE
*Devices
[256];
271 // USB Bus driver need to control the recursive connect policy of the bus, only those wanted
272 // usb child device will be recursively connected.
274 // WantedUsbIoDPList tracks the Usb child devices which user want to recursivly fully connecte,
275 // every wanted child device is stored in a item of the WantedUsbIoDPList, whose structrure is
276 // DEVICE_PATH_LIST_ITEM
278 LIST_ENTRY WantedUsbIoDPList
;
287 USB_HUB_GET_PORT_STATUS GetPortStatus
;
288 USB_HUB_CLEAR_PORT_CHANGE ClearPortChange
;
289 USB_HUB_SET_PORT_FEATURE SetPortFeature
;
290 USB_HUB_CLEAR_PORT_FEATURE ClearPortFeature
;
291 USB_HUB_RESET_PORT ResetPort
;
292 USB_HUB_RELEASE Release
;
295 #define USB_US_LAND_ID 0x0409
297 #define DEVICE_PATH_LIST_ITEM_SIGNATURE SIGNATURE_32('d','p','l','i')
298 typedef struct _DEVICE_PATH_LIST_ITEM
{
301 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
302 } DEVICE_PATH_LIST_ITEM
;
305 USB_CLASS_DEVICE_PATH UsbClass
;
306 EFI_DEVICE_PATH_PROTOCOL End
;
307 } USB_CLASS_FORMAT_DEVICE_PATH
;
310 Free a DEVICE_PATH_LIST_ITEM list.
312 @param UsbIoDPList a DEVICE_PATH_LIST_ITEM list pointer.
314 @retval EFI_INVALID_PARAMETER If parameters are invalid, return this value.
315 @retval EFI_SUCCESS If free operation is successful, return this value.
320 UsbBusFreeUsbDPList (
321 IN LIST_ENTRY
*UsbIoDPList
325 Store a wanted usb child device info (its Usb part of device path) which is indicated by
326 RemainingDevicePath in a Usb bus which is indicated by UsbBusId.
328 @param UsbBusId Point to EFI_USB_BUS_PROTOCOL interface.
329 @param RemainingDevicePath The remaining device patch.
331 @retval EFI_SUCCESS Add operation is successful.
332 @retval EFI_INVALID_PARAMETER The parameters are invalid.
337 UsbBusAddWantedUsbIoDP (
338 IN EFI_USB_BUS_PROTOCOL
*UsbBusId
,
339 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
343 Check whether a usb child device is the wanted device in a bus.
345 @param Bus The Usb bus's private data pointer.
346 @param UsbIf The usb child device inferface.
348 @retval True If a usb child device is the wanted device in a bus.
349 @retval False If a usb child device is *NOT* the wanted device in a bus.
354 UsbBusIsWantedUsbIO (
356 IN USB_INTERFACE
*UsbIf
360 Recursively connnect every wanted usb child device to ensure they all fully connected.
361 Check all the child Usb IO handles in this bus, recursively connecte if it is wanted usb child device.
363 @param UsbBusId Point to EFI_USB_BUS_PROTOCOL interface.
365 @retval EFI_SUCCESS Connect is done successfully.
366 @retval EFI_INVALID_PARAMETER The parameter is invalid.
371 UsbBusRecursivelyConnectWantedUsbIo (
372 IN EFI_USB_BUS_PROTOCOL
*UsbBusId
376 USB_IO function to execute a control transfer. This
377 function will execute the USB transfer. If transfer
378 successes, it will sync the internal state of USB bus
381 @param This The USB_IO instance
382 @param Request The control transfer request
383 @param Direction Direction for data stage
384 @param Timeout The time to wait before timeout
385 @param Data The buffer holding the data
386 @param DataLength Then length of the data
387 @param UsbStatus USB result
389 @retval EFI_INVALID_PARAMETER The parameters are invalid
390 @retval EFI_SUCCESS The control transfer succeded.
391 @retval Others Failed to execute the transfer
396 UsbIoControlTransfer (
397 IN EFI_USB_IO_PROTOCOL
*This
,
398 IN EFI_USB_DEVICE_REQUEST
*Request
,
399 IN EFI_USB_DATA_DIRECTION Direction
,
401 IN OUT VOID
*Data
, OPTIONAL
402 IN UINTN DataLength
, OPTIONAL
403 OUT UINT32
*UsbStatus
407 Execute a bulk transfer to the device endpoint.
409 @param This The USB IO instance.
410 @param Endpoint The device endpoint.
411 @param Data The data to transfer.
412 @param DataLength The length of the data to transfer.
413 @param Timeout Time to wait before timeout.
414 @param UsbStatus The result of USB transfer.
416 @retval EFI_SUCCESS The bulk transfer is OK.
417 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
418 @retval Others Failed to execute transfer, reason returned in
425 IN EFI_USB_IO_PROTOCOL
*This
,
428 IN OUT UINTN
*DataLength
,
430 OUT UINT32
*UsbStatus
434 Execute a synchronous interrupt transfer.
436 @param This The USB IO instance.
437 @param Endpoint The device endpoint.
438 @param Data The data to transfer.
439 @param DataLength The length of the data to transfer.
440 @param Timeout Time to wait before timeout.
441 @param UsbStatus The result of USB transfer.
443 @retval EFI_SUCCESS The synchronous interrupt transfer is OK.
444 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
445 @retval Others Failed to execute transfer, reason returned in
451 UsbIoSyncInterruptTransfer (
452 IN EFI_USB_IO_PROTOCOL
*This
,
455 IN OUT UINTN
*DataLength
,
457 OUT UINT32
*UsbStatus
461 Queue a new asynchronous interrupt transfer, or remove the old
462 request if (IsNewTransfer == FALSE).
464 @param This The USB_IO instance.
465 @param Endpoint The device endpoint.
466 @param IsNewTransfer Whether this is a new request, if it's old, remove
468 @param PollInterval The interval to poll the transfer result, (in ms).
469 @param DataLength The length of perodic data transfer.
470 @param Callback The function to call periodicaly when transfer is
472 @param Context The context to the callback.
474 @retval EFI_SUCCESS New transfer is queued or old request is removed.
475 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
476 @retval Others Failed to queue the new request or remove the old
482 UsbIoAsyncInterruptTransfer (
483 IN EFI_USB_IO_PROTOCOL
*This
,
485 IN BOOLEAN IsNewTransfer
,
486 IN UINTN PollInterval
, OPTIONAL
487 IN UINTN DataLength
, OPTIONAL
488 IN EFI_ASYNC_USB_TRANSFER_CALLBACK Callback
, OPTIONAL
489 IN VOID
*Context OPTIONAL
493 Execute a synchronous isochronous transfer.
495 @param This The USB IO instance.
496 @param DeviceEndpoint The device endpoint.
497 @param Data The data to transfer.
498 @param DataLength The length of the data to transfer.
499 @param UsbStatus The result of USB transfer.
501 @retval EFI_UNSUPPORTED Currently isochronous transfer isn't supported.
506 UsbIoIsochronousTransfer (
507 IN EFI_USB_IO_PROTOCOL
*This
,
508 IN UINT8 DeviceEndpoint
,
515 Queue an asynchronous isochronous transfer.
517 @param This The USB_IO instance.
518 @param DeviceEndpoint The device endpoint.
519 @param Data The data to transfer.
520 @param DataLength The length of perodic data transfer.
521 @param IsochronousCallBack The function to call periodicaly when transfer is
523 @param Context The context to the callback.
525 @retval EFI_UNSUPPORTED Currently isochronous transfer isn't supported.
530 UsbIoAsyncIsochronousTransfer (
531 IN EFI_USB_IO_PROTOCOL
*This
,
532 IN UINT8 DeviceEndpoint
,
535 IN EFI_ASYNC_USB_TRANSFER_CALLBACK IsochronousCallBack
,
536 IN VOID
*Context OPTIONAL
540 Retrieve the device descriptor of the device.
542 @param This The USB IO instance.
543 @param Descriptor The variable to receive the device descriptor.
545 @retval EFI_SUCCESS The device descriptor is returned.
546 @retval EFI_INVALID_PARAMETER The parameter is invalid.
551 UsbIoGetDeviceDescriptor (
552 IN EFI_USB_IO_PROTOCOL
*This
,
553 OUT EFI_USB_DEVICE_DESCRIPTOR
*Descriptor
557 Return the configuration descriptor of the current active configuration.
559 @param This The USB IO instance.
560 @param Descriptor The USB configuration descriptor.
562 @retval EFI_SUCCESS The active configuration descriptor is returned.
563 @retval EFI_INVALID_PARAMETER Some parameter is invalid.
564 @retval EFI_NOT_FOUND Currently no active configuration is selected.
569 UsbIoGetActiveConfigDescriptor (
570 IN EFI_USB_IO_PROTOCOL
*This
,
571 OUT EFI_USB_CONFIG_DESCRIPTOR
*Descriptor
575 Retrieve the active interface setting descriptor for this USB IO instance.
577 @param This The USB IO instance.
578 @param Descriptor The variable to receive active interface setting.
580 @retval EFI_SUCCESS The active interface setting is returned.
581 @retval EFI_INVALID_PARAMETER Some parameter is invalid.
586 UsbIoGetInterfaceDescriptor (
587 IN EFI_USB_IO_PROTOCOL
*This
,
588 OUT EFI_USB_INTERFACE_DESCRIPTOR
*Descriptor
592 Retrieve the endpoint descriptor from this interface setting.
594 @param This The USB IO instance.
595 @param Index The index (start from zero) of the endpoint to
597 @param Descriptor The variable to receive the descriptor.
599 @retval EFI_SUCCESS The endpoint descriptor is returned.
600 @retval EFI_INVALID_PARAMETER Some parameter is invalid.
605 UsbIoGetEndpointDescriptor (
606 IN EFI_USB_IO_PROTOCOL
*This
,
608 OUT EFI_USB_ENDPOINT_DESCRIPTOR
*Descriptor
612 Retrieve the supported language ID table from the device.
614 @param This The USB IO instance.
615 @param LangIDTable The table to return the language IDs.
616 @param TableSize The size, in bytes, of the table LangIDTable.
618 @retval EFI_SUCCESS The language ID is return.
623 UsbIoGetSupportedLanguages (
624 IN EFI_USB_IO_PROTOCOL
*This
,
625 OUT UINT16
**LangIDTable
,
626 OUT UINT16
*TableSize
630 Retrieve an indexed string in the language of LangID.
632 @param This The USB IO instance.
633 @param LangID The language ID of the string to retrieve.
634 @param StringIndex The index of the string.
635 @param String The variable to receive the string.
637 @retval EFI_SUCCESS The string is returned.
638 @retval EFI_NOT_FOUND No such string existed.
643 UsbIoGetStringDescriptor (
644 IN EFI_USB_IO_PROTOCOL
*This
,
646 IN UINT8 StringIndex
,
651 Reset the device, then if that succeeds, reconfigure the
652 device with its address and current active configuration.
654 @param This The USB IO instance.
656 @retval EFI_SUCCESS The device is reset and configured.
657 @retval Others Failed to reset the device.
663 IN EFI_USB_IO_PROTOCOL
*This
667 Install Usb Bus Protocol on host controller, and start the Usb bus.
669 @param This The USB bus driver binding instance.
670 @param Controller The controller to check.
671 @param RemainingDevicePath The remaining device patch.
673 @retval EFI_SUCCESS The controller is controlled by the usb bus.
674 @retval EFI_ALREADY_STARTED The controller is already controlled by the usb bus.
675 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
680 UsbBusBuildProtocol (
681 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
682 IN EFI_HANDLE Controller
,
683 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
687 The USB bus driver entry pointer.
689 @param ImageHandle The driver image handle.
690 @param SystemTable The system table.
692 @return EFI_SUCCESS The component name protocol is installed.
693 @return Others Failed to init the usb driver.
698 UsbBusDriverEntryPoint (
699 IN EFI_HANDLE ImageHandle
,
700 IN EFI_SYSTEM_TABLE
*SystemTable
704 Check whether USB bus driver support this device.
706 @param This The USB bus driver binding protocol.
707 @param Controller The controller handle to check.
708 @param RemainingDevicePath The remaining device path.
710 @retval EFI_SUCCESS The bus supports this controller.
711 @retval EFI_UNSUPPORTED This device isn't supported.
716 UsbBusControllerDriverSupported (
717 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
718 IN EFI_HANDLE Controller
,
719 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
723 Start to process the controller.
725 @param This The USB bus driver binding instance.
726 @param Controller The controller to check.
727 @param RemainingDevicePath The remaining device patch.
729 @retval EFI_SUCCESS The controller is controlled by the usb bus.
730 @retval EFI_ALREADY_STARTED The controller is already controlled by the usb
732 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
737 UsbBusControllerDriverStart (
738 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
739 IN EFI_HANDLE Controller
,
740 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
744 Stop handle the controller by this USB bus driver.
746 @param This The USB bus driver binding protocol.
747 @param Controller The controller to release.
748 @param NumberOfChildren The child of USB bus that opened controller
750 @param ChildHandleBuffer The array of child handle.
752 @retval EFI_SUCCESS The controller or children are stopped.
753 @retval EFI_DEVICE_ERROR Failed to stop the driver.
758 UsbBusControllerDriverStop (
759 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
760 IN EFI_HANDLE Controller
,
761 IN UINTN NumberOfChildren
,
762 IN EFI_HANDLE
*ChildHandleBuffer
765 extern EFI_USB_IO_PROTOCOL mUsbIoProtocol
;
766 extern EFI_DRIVER_BINDING_PROTOCOL mUsbBusDriverBinding
;
767 extern EFI_COMPONENT_NAME_PROTOCOL mUsbBusComponentName
;
768 extern EFI_COMPONENT_NAME2_PROTOCOL mUsbBusComponentName2
;