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
204 // Stands for different functions of USB device
206 struct _USB_INTERFACE
{
209 USB_INTERFACE_DESC
*IfDesc
;
210 USB_INTERFACE_SETTING
*IfSetting
;
213 // Handles and protocols
216 EFI_USB_IO_PROTOCOL UsbIo
;
217 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
221 // Hub device special data
229 // Data used only by normal hub devices
231 USB_ENDPOINT_DESC
*HubEp
;
235 // Data used only by root hub to hand over device to
236 // companion UHCI driver if low/full speed devices are
237 // connected to EHCI.
243 // Stands for the current USB Bus
247 EFI_USB_BUS_PROTOCOL BusId
;
250 // Managed USB host controller
252 EFI_HANDLE HostHandle
;
253 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
254 EFI_USB2_HC_PROTOCOL
*Usb2Hc
;
255 EFI_USB_HC_PROTOCOL
*UsbHc
;
258 // Recorded the max supported usb devices.
259 // XHCI can support up to 255 devices.
260 // EHCI/UHCI/OHCI supports up to 127 devices.
264 // An array of device that is on the bus. Devices[0] is
265 // for root hub. Device with address i is at Devices[i].
267 USB_DEVICE
*Devices
[256];
270 // USB Bus driver need to control the recursive connect policy of the bus, only those wanted
271 // usb child device will be recursively connected.
273 // WantedUsbIoDPList tracks the Usb child devices which user want to recursivly fully connecte,
274 // every wanted child device is stored in a item of the WantedUsbIoDPList, whose structrure is
275 // DEVICE_PATH_LIST_ITEM
277 LIST_ENTRY WantedUsbIoDPList
;
286 USB_HUB_GET_PORT_STATUS GetPortStatus
;
287 USB_HUB_CLEAR_PORT_CHANGE ClearPortChange
;
288 USB_HUB_SET_PORT_FEATURE SetPortFeature
;
289 USB_HUB_CLEAR_PORT_FEATURE ClearPortFeature
;
290 USB_HUB_RESET_PORT ResetPort
;
291 USB_HUB_RELEASE Release
;
294 #define USB_US_LAND_ID 0x0409
296 #define DEVICE_PATH_LIST_ITEM_SIGNATURE SIGNATURE_32('d','p','l','i')
297 typedef struct _DEVICE_PATH_LIST_ITEM
{
300 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
301 } DEVICE_PATH_LIST_ITEM
;
304 USB_CLASS_DEVICE_PATH UsbClass
;
305 EFI_DEVICE_PATH_PROTOCOL End
;
306 } USB_CLASS_FORMAT_DEVICE_PATH
;
309 Free a DEVICE_PATH_LIST_ITEM list.
311 @param UsbIoDPList a DEVICE_PATH_LIST_ITEM list pointer.
313 @retval EFI_INVALID_PARAMETER If parameters are invalid, return this value.
314 @retval EFI_SUCCESS If free operation is successful, return this value.
319 UsbBusFreeUsbDPList (
320 IN LIST_ENTRY
*UsbIoDPList
324 Store a wanted usb child device info (its Usb part of device path) which is indicated by
325 RemainingDevicePath in a Usb bus which is indicated by UsbBusId.
327 @param UsbBusId Point to EFI_USB_BUS_PROTOCOL interface.
328 @param RemainingDevicePath The remaining device patch.
330 @retval EFI_SUCCESS Add operation is successful.
331 @retval EFI_INVALID_PARAMETER The parameters are invalid.
336 UsbBusAddWantedUsbIoDP (
337 IN EFI_USB_BUS_PROTOCOL
*UsbBusId
,
338 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
342 Check whether a usb child device is the wanted device in a bus.
344 @param Bus The Usb bus's private data pointer.
345 @param UsbIf The usb child device inferface.
347 @retval True If a usb child device is the wanted device in a bus.
348 @retval False If a usb child device is *NOT* the wanted device in a bus.
353 UsbBusIsWantedUsbIO (
355 IN USB_INTERFACE
*UsbIf
359 Recursively connnect every wanted usb child device to ensure they all fully connected.
360 Check all the child Usb IO handles in this bus, recursively connecte if it is wanted usb child device.
362 @param UsbBusId Point to EFI_USB_BUS_PROTOCOL interface.
364 @retval EFI_SUCCESS Connect is done successfully.
365 @retval EFI_INVALID_PARAMETER The parameter is invalid.
370 UsbBusRecursivelyConnectWantedUsbIo (
371 IN EFI_USB_BUS_PROTOCOL
*UsbBusId
375 USB_IO function to execute a control transfer. This
376 function will execute the USB transfer. If transfer
377 successes, it will sync the internal state of USB bus
380 @param This The USB_IO instance
381 @param Request The control transfer request
382 @param Direction Direction for data stage
383 @param Timeout The time to wait before timeout
384 @param Data The buffer holding the data
385 @param DataLength Then length of the data
386 @param UsbStatus USB result
388 @retval EFI_INVALID_PARAMETER The parameters are invalid
389 @retval EFI_SUCCESS The control transfer succeded.
390 @retval Others Failed to execute the transfer
395 UsbIoControlTransfer (
396 IN EFI_USB_IO_PROTOCOL
*This
,
397 IN EFI_USB_DEVICE_REQUEST
*Request
,
398 IN EFI_USB_DATA_DIRECTION Direction
,
400 IN OUT VOID
*Data
, OPTIONAL
401 IN UINTN DataLength
, OPTIONAL
402 OUT UINT32
*UsbStatus
406 Execute a bulk transfer to the device endpoint.
408 @param This The USB IO instance.
409 @param Endpoint The device endpoint.
410 @param Data The data to transfer.
411 @param DataLength The length of the data to transfer.
412 @param Timeout Time to wait before timeout.
413 @param UsbStatus The result of USB transfer.
415 @retval EFI_SUCCESS The bulk transfer is OK.
416 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
417 @retval Others Failed to execute transfer, reason returned in
424 IN EFI_USB_IO_PROTOCOL
*This
,
427 IN OUT UINTN
*DataLength
,
429 OUT UINT32
*UsbStatus
433 Execute a synchronous interrupt transfer.
435 @param This The USB IO instance.
436 @param Endpoint The device endpoint.
437 @param Data The data to transfer.
438 @param DataLength The length of the data to transfer.
439 @param Timeout Time to wait before timeout.
440 @param UsbStatus The result of USB transfer.
442 @retval EFI_SUCCESS The synchronous interrupt transfer is OK.
443 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
444 @retval Others Failed to execute transfer, reason returned in
450 UsbIoSyncInterruptTransfer (
451 IN EFI_USB_IO_PROTOCOL
*This
,
454 IN OUT UINTN
*DataLength
,
456 OUT UINT32
*UsbStatus
460 Queue a new asynchronous interrupt transfer, or remove the old
461 request if (IsNewTransfer == FALSE).
463 @param This The USB_IO instance.
464 @param Endpoint The device endpoint.
465 @param IsNewTransfer Whether this is a new request, if it's old, remove
467 @param PollInterval The interval to poll the transfer result, (in ms).
468 @param DataLength The length of perodic data transfer.
469 @param Callback The function to call periodicaly when transfer is
471 @param Context The context to the callback.
473 @retval EFI_SUCCESS New transfer is queued or old request is removed.
474 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
475 @retval Others Failed to queue the new request or remove the old
481 UsbIoAsyncInterruptTransfer (
482 IN EFI_USB_IO_PROTOCOL
*This
,
484 IN BOOLEAN IsNewTransfer
,
485 IN UINTN PollInterval
, OPTIONAL
486 IN UINTN DataLength
, OPTIONAL
487 IN EFI_ASYNC_USB_TRANSFER_CALLBACK Callback
, OPTIONAL
488 IN VOID
*Context OPTIONAL
492 Execute a synchronous isochronous transfer.
494 @param This The USB IO instance.
495 @param DeviceEndpoint The device endpoint.
496 @param Data The data to transfer.
497 @param DataLength The length of the data to transfer.
498 @param UsbStatus The result of USB transfer.
500 @retval EFI_UNSUPPORTED Currently isochronous transfer isn't supported.
505 UsbIoIsochronousTransfer (
506 IN EFI_USB_IO_PROTOCOL
*This
,
507 IN UINT8 DeviceEndpoint
,
514 Queue an asynchronous isochronous transfer.
516 @param This The USB_IO instance.
517 @param DeviceEndpoint The device endpoint.
518 @param Data The data to transfer.
519 @param DataLength The length of perodic data transfer.
520 @param IsochronousCallBack The function to call periodicaly when transfer is
522 @param Context The context to the callback.
524 @retval EFI_UNSUPPORTED Currently isochronous transfer isn't supported.
529 UsbIoAsyncIsochronousTransfer (
530 IN EFI_USB_IO_PROTOCOL
*This
,
531 IN UINT8 DeviceEndpoint
,
534 IN EFI_ASYNC_USB_TRANSFER_CALLBACK IsochronousCallBack
,
535 IN VOID
*Context OPTIONAL
539 Retrieve the device descriptor of the device.
541 @param This The USB IO instance.
542 @param Descriptor The variable to receive the device descriptor.
544 @retval EFI_SUCCESS The device descriptor is returned.
545 @retval EFI_INVALID_PARAMETER The parameter is invalid.
550 UsbIoGetDeviceDescriptor (
551 IN EFI_USB_IO_PROTOCOL
*This
,
552 OUT EFI_USB_DEVICE_DESCRIPTOR
*Descriptor
556 Return the configuration descriptor of the current active configuration.
558 @param This The USB IO instance.
559 @param Descriptor The USB configuration descriptor.
561 @retval EFI_SUCCESS The active configuration descriptor is returned.
562 @retval EFI_INVALID_PARAMETER Some parameter is invalid.
563 @retval EFI_NOT_FOUND Currently no active configuration is selected.
568 UsbIoGetActiveConfigDescriptor (
569 IN EFI_USB_IO_PROTOCOL
*This
,
570 OUT EFI_USB_CONFIG_DESCRIPTOR
*Descriptor
574 Retrieve the active interface setting descriptor for this USB IO instance.
576 @param This The USB IO instance.
577 @param Descriptor The variable to receive active interface setting.
579 @retval EFI_SUCCESS The active interface setting is returned.
580 @retval EFI_INVALID_PARAMETER Some parameter is invalid.
585 UsbIoGetInterfaceDescriptor (
586 IN EFI_USB_IO_PROTOCOL
*This
,
587 OUT EFI_USB_INTERFACE_DESCRIPTOR
*Descriptor
591 Retrieve the endpoint descriptor from this interface setting.
593 @param This The USB IO instance.
594 @param Index The index (start from zero) of the endpoint to
596 @param Descriptor The variable to receive the descriptor.
598 @retval EFI_SUCCESS The endpoint descriptor is returned.
599 @retval EFI_INVALID_PARAMETER Some parameter is invalid.
604 UsbIoGetEndpointDescriptor (
605 IN EFI_USB_IO_PROTOCOL
*This
,
607 OUT EFI_USB_ENDPOINT_DESCRIPTOR
*Descriptor
611 Retrieve the supported language ID table from the device.
613 @param This The USB IO instance.
614 @param LangIDTable The table to return the language IDs.
615 @param TableSize The size, in bytes, of the table LangIDTable.
617 @retval EFI_SUCCESS The language ID is return.
622 UsbIoGetSupportedLanguages (
623 IN EFI_USB_IO_PROTOCOL
*This
,
624 OUT UINT16
**LangIDTable
,
625 OUT UINT16
*TableSize
629 Retrieve an indexed string in the language of LangID.
631 @param This The USB IO instance.
632 @param LangID The language ID of the string to retrieve.
633 @param StringIndex The index of the string.
634 @param String The variable to receive the string.
636 @retval EFI_SUCCESS The string is returned.
637 @retval EFI_NOT_FOUND No such string existed.
642 UsbIoGetStringDescriptor (
643 IN EFI_USB_IO_PROTOCOL
*This
,
645 IN UINT8 StringIndex
,
650 Reset the device, then if that succeeds, reconfigure the
651 device with its address and current active configuration.
653 @param This The USB IO instance.
655 @retval EFI_SUCCESS The device is reset and configured.
656 @retval Others Failed to reset the device.
662 IN EFI_USB_IO_PROTOCOL
*This
666 Install Usb Bus Protocol on host controller, and start the Usb bus.
668 @param This The USB bus driver binding instance.
669 @param Controller The controller to check.
670 @param RemainingDevicePath The remaining device patch.
672 @retval EFI_SUCCESS The controller is controlled by the usb bus.
673 @retval EFI_ALREADY_STARTED The controller is already controlled by the usb bus.
674 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
679 UsbBusBuildProtocol (
680 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
681 IN EFI_HANDLE Controller
,
682 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
686 The USB bus driver entry pointer.
688 @param ImageHandle The driver image handle.
689 @param SystemTable The system table.
691 @return EFI_SUCCESS The component name protocol is installed.
692 @return Others Failed to init the usb driver.
697 UsbBusDriverEntryPoint (
698 IN EFI_HANDLE ImageHandle
,
699 IN EFI_SYSTEM_TABLE
*SystemTable
703 Check whether USB bus driver support this device.
705 @param This The USB bus driver binding protocol.
706 @param Controller The controller handle to check.
707 @param RemainingDevicePath The remaining device path.
709 @retval EFI_SUCCESS The bus supports this controller.
710 @retval EFI_UNSUPPORTED This device isn't supported.
715 UsbBusControllerDriverSupported (
716 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
717 IN EFI_HANDLE Controller
,
718 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
722 Start to process the controller.
724 @param This The USB bus driver binding instance.
725 @param Controller The controller to check.
726 @param RemainingDevicePath The remaining device patch.
728 @retval EFI_SUCCESS The controller is controlled by the usb bus.
729 @retval EFI_ALREADY_STARTED The controller is already controlled by the usb
731 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
736 UsbBusControllerDriverStart (
737 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
738 IN EFI_HANDLE Controller
,
739 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
743 Stop handle the controller by this USB bus driver.
745 @param This The USB bus driver binding protocol.
746 @param Controller The controller to release.
747 @param NumberOfChildren The child of USB bus that opened controller
749 @param ChildHandleBuffer The array of child handle.
751 @retval EFI_SUCCESS The controller or children are stopped.
752 @retval EFI_DEVICE_ERROR Failed to stop the driver.
757 UsbBusControllerDriverStop (
758 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
759 IN EFI_HANDLE Controller
,
760 IN UINTN NumberOfChildren
,
761 IN EFI_HANDLE
*ChildHandleBuffer
764 extern EFI_USB_IO_PROTOCOL mUsbIoProtocol
;
765 extern EFI_DRIVER_BINDING_PROTOCOL mUsbBusDriverBinding
;
766 extern EFI_COMPONENT_NAME_PROTOCOL mUsbBusComponentName
;
767 extern EFI_COMPONENT_NAME2_PROTOCOL mUsbBusComponentName2
;