3 Usb Bus Driver Binding and Bus IO Protocol.
5 Copyright (c) 2004 - 2010, Intel Corporation
6 All rights reserved. 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>
37 #include <IndustryStandard/Usb.h>
39 typedef struct _USB_DEVICE USB_DEVICE
;
40 typedef struct _USB_INTERFACE USB_INTERFACE
;
41 typedef struct _USB_BUS USB_BUS
;
42 typedef struct _USB_HUB_API USB_HUB_API
;
45 #include "UsbUtility.h"
48 #include "UsbEnumer.h"
51 // USB bus timeout experience values
54 #define USB_MAX_LANG_ID 16
55 #define USB_MAX_INTERFACE 16
56 #define USB_MAX_DEVICES 128
58 #define USB_BUS_1_MILLISECOND 1000
61 // Roothub and hub's polling interval, set by experience,
62 // The unit of roothub is 100us, means 1s as interval, and
63 // the unit of hub is 1ms, means 64ms as interval.
65 #define USB_ROOTHUB_POLL_INTERVAL (1000 * 10000U)
66 #define USB_HUB_POLL_INTERVAL 64
69 // Wait for port stable to work, refers to specification
72 #define USB_WAIT_PORT_STABLE_STALL (100 * USB_BUS_1_MILLISECOND)
75 // Wait for port statue reg change, set by experience
77 #define USB_WAIT_PORT_STS_CHANGE_STALL (5 * USB_BUS_1_MILLISECOND)
80 // Wait for set device address, refers to specification
81 // [USB20-9.2.6.3, it says 2ms]
83 #define USB_SET_DEVICE_ADDRESS_STALL (20 * USB_BUS_1_MILLISECOND)
86 // Wait for retry max packet size, set by experience
88 #define USB_RETRY_MAX_PACK_SIZE_STALL (100 * USB_BUS_1_MILLISECOND)
91 // Wait for hub port power-on, refers to specification
94 #define USB_SET_PORT_POWER_STALL (2 * USB_BUS_1_MILLISECOND)
97 // Wait for port reset, refers to specification
98 // [USB20-7.1.7.5, it says 10ms for hub and 50ms for
101 #define USB_SET_PORT_RESET_STALL (20 * USB_BUS_1_MILLISECOND)
102 #define USB_SET_ROOT_PORT_RESET_STALL (50 * USB_BUS_1_MILLISECOND)
105 // Wait for clear roothub port reset, set by experience
107 #define USB_CLR_ROOT_PORT_RESET_STALL (20 * USB_BUS_1_MILLISECOND)
110 // Wait for set roothub port enable, set by experience
112 #define USB_SET_ROOT_PORT_ENABLE_STALL (20 * USB_BUS_1_MILLISECOND)
115 // Send general device request timeout.
117 // The USB Specification 2.0, section 11.24.1 recommends a value of
118 // 50 milliseconds. We use a value of 100 milliseconds to work
119 // around slower hubs and devices.
121 #define USB_GENERAL_DEVICE_REQUEST_TIMEOUT 100
124 // Send clear feature request timeout, set by experience
126 #define USB_CLEAR_FEATURE_REQUEST_TIMEOUT 10
129 // Bus raises TPL to TPL_NOTIFY to serialize all its operations
130 // to protect shared data structures.
132 #define USB_BUS_TPL TPL_NOTIFY
134 #define USB_INTERFACE_SIGNATURE SIGNATURE_32 ('U', 'S', 'B', 'I')
135 #define USB_BUS_SIGNATURE SIGNATURE_32 ('U', 'S', 'B', 'B')
137 #define USB_BIT(a) ((UINTN)(1 << (a)))
138 #define USB_BIT_IS_SET(Data, Bit) ((BOOLEAN)(((Data) & (Bit)) == (Bit)))
140 #define EFI_USB_BUS_PROTOCOL_GUID \
141 {0x2B2F68CC, 0x0CD2, 0x44cf, {0x8E, 0x8B, 0xBB, 0xA2, 0x0B, 0x1B, 0x5B, 0x75}}
143 #define USB_INTERFACE_FROM_USBIO(a) \
144 CR(a, USB_INTERFACE, UsbIo, USB_INTERFACE_SIGNATURE)
146 #define USB_BUS_FROM_THIS(a) \
147 CR(a, USB_BUS, BusId, USB_BUS_SIGNATURE)
150 // Used to locate USB_BUS
152 typedef struct _EFI_USB_BUS_PROTOCOL
{
154 } EFI_USB_BUS_PROTOCOL
;
158 // Stands for the real USB device. Each device may
159 // has several seperately working interfaces.
165 // Configuration information
172 // The device's descriptors and its configuration
174 USB_DEVICE_DESC
*DevDesc
;
175 USB_CONFIG_DESC
*ActiveConfig
;
177 UINT16 LangId
[USB_MAX_LANG_ID
];
180 UINT8 NumOfInterface
;
181 USB_INTERFACE
*Interfaces
[USB_MAX_INTERFACE
];
184 // Parent child relationship
186 EFI_USB2_HC_TRANSACTION_TRANSLATOR Translator
;
189 USB_INTERFACE
*ParentIf
;
190 UINT8 ParentPort
; // Start at 0
194 // Stands for different functions of USB device
196 struct _USB_INTERFACE
{
199 USB_INTERFACE_DESC
*IfDesc
;
200 USB_INTERFACE_SETTING
*IfSetting
;
203 // Handles and protocols
206 EFI_USB_IO_PROTOCOL UsbIo
;
207 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
211 // Hub device special data
219 // Data used only by normal hub devices
221 USB_ENDPOINT_DESC
*HubEp
;
225 // Data used only by root hub to hand over device to
226 // companion UHCI driver if low/full speed devices are
227 // connected to EHCI.
233 // Stands for the current USB Bus
237 EFI_USB_BUS_PROTOCOL BusId
;
240 // Managed USB host controller
242 EFI_HANDLE HostHandle
;
243 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
244 EFI_USB2_HC_PROTOCOL
*Usb2Hc
;
245 EFI_USB_HC_PROTOCOL
*UsbHc
;
248 // An array of device that is on the bus. Devices[0] is
249 // for root hub. Device with address i is at Devices[i].
251 USB_DEVICE
*Devices
[USB_MAX_DEVICES
];
254 // USB Bus driver need to control the recursive connect policy of the bus, only those wanted
255 // usb child device will be recursively connected.
257 // WantedUsbIoDPList tracks the Usb child devices which user want to recursivly fully connecte,
258 // every wanted child device is stored in a item of the WantedUsbIoDPList, whose structrure is
259 // DEVICE_PATH_LIST_ITEM
261 LIST_ENTRY WantedUsbIoDPList
;
270 USB_HUB_GET_PORT_STATUS GetPortStatus
;
271 USB_HUB_CLEAR_PORT_CHANGE ClearPortChange
;
272 USB_HUB_SET_PORT_FEATURE SetPortFeature
;
273 USB_HUB_CLEAR_PORT_FEATURE ClearPortFeature
;
274 USB_HUB_RESET_PORT ResetPort
;
275 USB_HUB_RELEASE Release
;
278 #define USB_US_LAND_ID 0x0409
280 #define DEVICE_PATH_LIST_ITEM_SIGNATURE SIGNATURE_32('d','p','l','i')
281 typedef struct _DEVICE_PATH_LIST_ITEM
{
284 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
285 } DEVICE_PATH_LIST_ITEM
;
288 USB_CLASS_DEVICE_PATH UsbClass
;
289 EFI_DEVICE_PATH_PROTOCOL End
;
290 } USB_CLASS_FORMAT_DEVICE_PATH
;
293 Free a DEVICE_PATH_LIST_ITEM list.
295 @param UsbIoDPList a DEVICE_PATH_LIST_ITEM list pointer.
297 @retval EFI_INVALID_PARAMETER If parameters are invalid, return this value.
298 @retval EFI_SUCCESS If free operation is successful, return this value.
303 UsbBusFreeUsbDPList (
304 IN LIST_ENTRY
*UsbIoDPList
308 Store a wanted usb child device info (its Usb part of device path) which is indicated by
309 RemainingDevicePath in a Usb bus which is indicated by UsbBusId.
311 @param UsbBusId Point to EFI_USB_BUS_PROTOCOL interface.
312 @param RemainingDevicePath The remaining device patch.
314 @retval EFI_SUCCESS Add operation is successful.
315 @retval EFI_INVALID_PARAMETER The parameters are invalid.
320 UsbBusAddWantedUsbIoDP (
321 IN EFI_USB_BUS_PROTOCOL
*UsbBusId
,
322 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
326 Check whether a usb child device is the wanted device in a bus.
328 @param Bus The Usb bus's private data pointer.
329 @param UsbIf The usb child device inferface.
331 @retval True If a usb child device is the wanted device in a bus.
332 @retval False If a usb child device is *NOT* the wanted device in a bus.
337 UsbBusIsWantedUsbIO (
339 IN USB_INTERFACE
*UsbIf
343 Recursively connnect every wanted usb child device to ensure they all fully connected.
344 Check all the child Usb IO handles in this bus, recursively connecte if it is wanted usb child device.
346 @param UsbBusId Point to EFI_USB_BUS_PROTOCOL interface.
348 @retval EFI_SUCCESS Connect is done successfully.
349 @retval EFI_INVALID_PARAMETER The parameter is invalid.
354 UsbBusRecursivelyConnectWantedUsbIo (
355 IN EFI_USB_BUS_PROTOCOL
*UsbBusId
359 USB_IO function to execute a control transfer. This
360 function will execute the USB transfer. If transfer
361 successes, it will sync the internal state of USB bus
364 @param This The USB_IO instance
365 @param Request The control transfer request
366 @param Direction Direction for data stage
367 @param Timeout The time to wait before timeout
368 @param Data The buffer holding the data
369 @param DataLength Then length of the data
370 @param UsbStatus USB result
372 @retval EFI_INVALID_PARAMETER The parameters are invalid
373 @retval EFI_SUCCESS The control transfer succeded.
374 @retval Others Failed to execute the transfer
379 UsbIoControlTransfer (
380 IN EFI_USB_IO_PROTOCOL
*This
,
381 IN EFI_USB_DEVICE_REQUEST
*Request
,
382 IN EFI_USB_DATA_DIRECTION Direction
,
384 IN OUT VOID
*Data
, OPTIONAL
385 IN UINTN DataLength
, OPTIONAL
386 OUT UINT32
*UsbStatus
390 Execute a bulk transfer to the device endpoint.
392 @param This The USB IO instance.
393 @param Endpoint The device endpoint.
394 @param Data The data to transfer.
395 @param DataLength The length of the data to transfer.
396 @param Timeout Time to wait before timeout.
397 @param UsbStatus The result of USB transfer.
399 @retval EFI_SUCCESS The bulk transfer is OK.
400 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
401 @retval Others Failed to execute transfer, reason returned in
408 IN EFI_USB_IO_PROTOCOL
*This
,
411 IN OUT UINTN
*DataLength
,
413 OUT UINT32
*UsbStatus
417 Execute a synchronous interrupt transfer.
419 @param This The USB IO instance.
420 @param Endpoint The device endpoint.
421 @param Data The data to transfer.
422 @param DataLength The length of the data to transfer.
423 @param Timeout Time to wait before timeout.
424 @param UsbStatus The result of USB transfer.
426 @retval EFI_SUCCESS The synchronous interrupt transfer is OK.
427 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
428 @retval Others Failed to execute transfer, reason returned in
434 UsbIoSyncInterruptTransfer (
435 IN EFI_USB_IO_PROTOCOL
*This
,
438 IN OUT UINTN
*DataLength
,
440 OUT UINT32
*UsbStatus
444 Queue a new asynchronous interrupt transfer, or remove the old
445 request if (IsNewTransfer == FALSE).
447 @param This The USB_IO instance.
448 @param Endpoint The device endpoint.
449 @param IsNewTransfer Whether this is a new request, if it's old, remove
451 @param PollInterval The interval to poll the transfer result, (in ms).
452 @param DataLength The length of perodic data transfer.
453 @param Callback The function to call periodicaly when transfer is
455 @param Context The context to the callback.
457 @retval EFI_SUCCESS New transfer is queued or old request is removed.
458 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
459 @retval Others Failed to queue the new request or remove the old
465 UsbIoAsyncInterruptTransfer (
466 IN EFI_USB_IO_PROTOCOL
*This
,
468 IN BOOLEAN IsNewTransfer
,
469 IN UINTN PollInterval
, OPTIONAL
470 IN UINTN DataLength
, OPTIONAL
471 IN EFI_ASYNC_USB_TRANSFER_CALLBACK Callback
, OPTIONAL
472 IN VOID
*Context OPTIONAL
476 Execute a synchronous isochronous transfer.
478 @param This The USB IO instance.
479 @param DeviceEndpoint The device endpoint.
480 @param Data The data to transfer.
481 @param DataLength The length of the data to transfer.
482 @param UsbStatus The result of USB transfer.
484 @retval EFI_UNSUPPORTED Currently isochronous transfer isn't supported.
489 UsbIoIsochronousTransfer (
490 IN EFI_USB_IO_PROTOCOL
*This
,
491 IN UINT8 DeviceEndpoint
,
498 Queue an asynchronous isochronous transfer.
500 @param This The USB_IO instance.
501 @param DeviceEndpoint The device endpoint.
502 @param Data The data to transfer.
503 @param DataLength The length of perodic data transfer.
504 @param IsochronousCallBack The function to call periodicaly when transfer is
506 @param Context The context to the callback.
508 @retval EFI_UNSUPPORTED Currently isochronous transfer isn't supported.
513 UsbIoAsyncIsochronousTransfer (
514 IN EFI_USB_IO_PROTOCOL
*This
,
515 IN UINT8 DeviceEndpoint
,
518 IN EFI_ASYNC_USB_TRANSFER_CALLBACK IsochronousCallBack
,
519 IN VOID
*Context OPTIONAL
523 Retrieve the device descriptor of the device.
525 @param This The USB IO instance.
526 @param Descriptor The variable to receive the device descriptor.
528 @retval EFI_SUCCESS The device descriptor is returned.
529 @retval EFI_INVALID_PARAMETER The parameter is invalid.
534 UsbIoGetDeviceDescriptor (
535 IN EFI_USB_IO_PROTOCOL
*This
,
536 OUT EFI_USB_DEVICE_DESCRIPTOR
*Descriptor
540 Return the configuration descriptor of the current active configuration.
542 @param This The USB IO instance.
543 @param Descriptor The USB configuration descriptor.
545 @retval EFI_SUCCESS The active configuration descriptor is returned.
546 @retval EFI_INVALID_PARAMETER Some parameter is invalid.
547 @retval EFI_NOT_FOUND Currently no active configuration is selected.
552 UsbIoGetActiveConfigDescriptor (
553 IN EFI_USB_IO_PROTOCOL
*This
,
554 OUT EFI_USB_CONFIG_DESCRIPTOR
*Descriptor
558 Retrieve the active interface setting descriptor for this USB IO instance.
560 @param This The USB IO instance.
561 @param Descriptor The variable to receive active interface setting.
563 @retval EFI_SUCCESS The active interface setting is returned.
564 @retval EFI_INVALID_PARAMETER Some parameter is invalid.
569 UsbIoGetInterfaceDescriptor (
570 IN EFI_USB_IO_PROTOCOL
*This
,
571 OUT EFI_USB_INTERFACE_DESCRIPTOR
*Descriptor
575 Retrieve the endpoint descriptor from this interface setting.
577 @param This The USB IO instance.
578 @param Index The index (start from zero) of the endpoint to
580 @param Descriptor The variable to receive the descriptor.
582 @retval EFI_SUCCESS The endpoint descriptor is returned.
583 @retval EFI_INVALID_PARAMETER Some parameter is invalid.
588 UsbIoGetEndpointDescriptor (
589 IN EFI_USB_IO_PROTOCOL
*This
,
591 OUT EFI_USB_ENDPOINT_DESCRIPTOR
*Descriptor
595 Retrieve the supported language ID table from the device.
597 @param This The USB IO instance.
598 @param LangIDTable The table to return the language IDs.
599 @param TableSize The number of supported languanges.
601 @retval EFI_SUCCESS The language ID is return.
606 UsbIoGetSupportedLanguages (
607 IN EFI_USB_IO_PROTOCOL
*This
,
608 OUT UINT16
**LangIDTable
,
609 OUT UINT16
*TableSize
613 Retrieve an indexed string in the language of LangID.
615 @param This The USB IO instance.
616 @param LangID The language ID of the string to retrieve.
617 @param StringIndex The index of the string.
618 @param String The variable to receive the string.
620 @retval EFI_SUCCESS The string is returned.
621 @retval EFI_NOT_FOUND No such string existed.
626 UsbIoGetStringDescriptor (
627 IN EFI_USB_IO_PROTOCOL
*This
,
629 IN UINT8 StringIndex
,
634 Reset the device, then if that succeeds, reconfigure the
635 device with its address and current active configuration.
637 @param This The USB IO instance.
639 @retval EFI_SUCCESS The device is reset and configured.
640 @retval Others Failed to reset the device.
646 IN EFI_USB_IO_PROTOCOL
*This
650 Install Usb Bus Protocol on host controller, and start the Usb bus.
652 @param This The USB bus driver binding instance.
653 @param Controller The controller to check.
654 @param RemainingDevicePath The remaining device patch.
656 @retval EFI_SUCCESS The controller is controlled by the usb bus.
657 @retval EFI_ALREADY_STARTED The controller is already controlled by the usb bus.
658 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
663 UsbBusBuildProtocol (
664 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
665 IN EFI_HANDLE Controller
,
666 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
670 The USB bus driver entry pointer.
672 @param ImageHandle The driver image handle.
673 @param SystemTable The system table.
675 @return EFI_SUCCESS The component name protocol is installed.
676 @return Others Failed to init the usb driver.
681 UsbBusDriverEntryPoint (
682 IN EFI_HANDLE ImageHandle
,
683 IN EFI_SYSTEM_TABLE
*SystemTable
687 Check whether USB bus driver support this device.
689 @param This The USB bus driver binding protocol.
690 @param Controller The controller handle to check.
691 @param RemainingDevicePath The remaining device path.
693 @retval EFI_SUCCESS The bus supports this controller.
694 @retval EFI_UNSUPPORTED This device isn't supported.
699 UsbBusControllerDriverSupported (
700 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
701 IN EFI_HANDLE Controller
,
702 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
706 Start to process the controller.
708 @param This The USB bus driver binding instance.
709 @param Controller The controller to check.
710 @param RemainingDevicePath The remaining device patch.
712 @retval EFI_SUCCESS The controller is controlled by the usb bus.
713 @retval EFI_ALREADY_STARTED The controller is already controlled by the usb
715 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
720 UsbBusControllerDriverStart (
721 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
722 IN EFI_HANDLE Controller
,
723 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
727 Stop handle the controller by this USB bus driver.
729 @param This The USB bus driver binding protocol.
730 @param Controller The controller to release.
731 @param NumberOfChildren The child of USB bus that opened controller
733 @param ChildHandleBuffer The array of child handle.
735 @retval EFI_SUCCESS The controller or children are stopped.
736 @retval EFI_DEVICE_ERROR Failed to stop the driver.
741 UsbBusControllerDriverStop (
742 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
743 IN EFI_HANDLE Controller
,
744 IN UINTN NumberOfChildren
,
745 IN EFI_HANDLE
*ChildHandleBuffer
748 extern EFI_USB_IO_PROTOCOL mUsbIoProtocol
;
749 extern EFI_DRIVER_BINDING_PROTOCOL mUsbBusDriverBinding
;
750 extern EFI_COMPONENT_NAME_PROTOCOL mUsbBusComponentName
;
751 extern EFI_COMPONENT_NAME2_PROTOCOL mUsbBusComponentName2
;