3 Usb Bus Driver Binding and Bus IO Protocol.
5 Copyright (c) 2004 - 2011, 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 1s as interval, and
64 // the unit of hub is 1ms, means 64ms as interval.
66 #define USB_ROOTHUB_POLL_INTERVAL (1000 * 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 (5 * USB_BUS_1_MILLISECOND)
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 (20 * 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 #define USB_SET_PORT_RESET_STALL (20 * USB_BUS_1_MILLISECOND)
103 #define USB_SET_ROOT_PORT_RESET_STALL (50 * USB_BUS_1_MILLISECOND)
106 // Wait for clear roothub port reset, set by experience
108 #define USB_CLR_ROOT_PORT_RESET_STALL (20 * USB_BUS_1_MILLISECOND)
111 // Wait for set roothub port enable, set by experience
113 #define USB_SET_ROOT_PORT_ENABLE_STALL (20 * USB_BUS_1_MILLISECOND)
116 // Send general device request timeout.
118 // The USB Specification 2.0, section 11.24.1 recommends a value of
119 // 50 milliseconds. We use a value of 100 milliseconds to work
120 // around slower hubs and devices.
122 #define USB_GENERAL_DEVICE_REQUEST_TIMEOUT 100
125 // Send clear feature request timeout, set by experience
127 #define USB_CLEAR_FEATURE_REQUEST_TIMEOUT 10
130 // Bus raises TPL to TPL_NOTIFY to serialize all its operations
131 // to protect shared data structures.
133 #define USB_BUS_TPL TPL_NOTIFY
135 #define USB_INTERFACE_SIGNATURE SIGNATURE_32 ('U', 'S', 'B', 'I')
136 #define USB_BUS_SIGNATURE SIGNATURE_32 ('U', 'S', 'B', 'B')
138 #define USB_BIT(a) ((UINTN)(1 << (a)))
139 #define USB_BIT_IS_SET(Data, Bit) ((BOOLEAN)(((Data) & (Bit)) == (Bit)))
141 #define EFI_USB_BUS_PROTOCOL_GUID \
142 {0x2B2F68CC, 0x0CD2, 0x44cf, {0x8E, 0x8B, 0xBB, 0xA2, 0x0B, 0x1B, 0x5B, 0x75}}
144 #define USB_INTERFACE_FROM_USBIO(a) \
145 CR(a, USB_INTERFACE, UsbIo, USB_INTERFACE_SIGNATURE)
147 #define USB_BUS_FROM_THIS(a) \
148 CR(a, USB_BUS, BusId, USB_BUS_SIGNATURE)
151 // Used to locate USB_BUS
153 typedef struct _EFI_USB_BUS_PROTOCOL
{
155 } EFI_USB_BUS_PROTOCOL
;
159 // Stands for the real USB device. Each device may
160 // has several seperately working interfaces.
166 // Configuration information
173 // The device's descriptors and its configuration
175 USB_DEVICE_DESC
*DevDesc
;
176 USB_CONFIG_DESC
*ActiveConfig
;
178 UINT16 LangId
[USB_MAX_LANG_ID
];
181 UINT8 NumOfInterface
;
182 USB_INTERFACE
*Interfaces
[USB_MAX_INTERFACE
];
185 // Parent child relationship
187 EFI_USB2_HC_TRANSACTION_TRANSLATOR Translator
;
190 USB_INTERFACE
*ParentIf
;
191 UINT8 ParentPort
; // Start at 0
195 // Stands for different functions of USB device
197 struct _USB_INTERFACE
{
200 USB_INTERFACE_DESC
*IfDesc
;
201 USB_INTERFACE_SETTING
*IfSetting
;
204 // Handles and protocols
207 EFI_USB_IO_PROTOCOL UsbIo
;
208 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
212 // Hub device special data
220 // Data used only by normal hub devices
222 USB_ENDPOINT_DESC
*HubEp
;
226 // Data used only by root hub to hand over device to
227 // companion UHCI driver if low/full speed devices are
228 // connected to EHCI.
234 // Stands for the current USB Bus
238 EFI_USB_BUS_PROTOCOL BusId
;
241 // Managed USB host controller
243 EFI_HANDLE HostHandle
;
244 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
245 EFI_USB2_HC_PROTOCOL
*Usb2Hc
;
246 EFI_USB_HC_PROTOCOL
*UsbHc
;
249 // An array of device that is on the bus. Devices[0] is
250 // for root hub. Device with address i is at Devices[i].
252 USB_DEVICE
*Devices
[USB_MAX_DEVICES
];
255 // USB Bus driver need to control the recursive connect policy of the bus, only those wanted
256 // usb child device will be recursively connected.
258 // WantedUsbIoDPList tracks the Usb child devices which user want to recursivly fully connecte,
259 // every wanted child device is stored in a item of the WantedUsbIoDPList, whose structrure is
260 // DEVICE_PATH_LIST_ITEM
262 LIST_ENTRY WantedUsbIoDPList
;
271 USB_HUB_GET_PORT_STATUS GetPortStatus
;
272 USB_HUB_CLEAR_PORT_CHANGE ClearPortChange
;
273 USB_HUB_SET_PORT_FEATURE SetPortFeature
;
274 USB_HUB_CLEAR_PORT_FEATURE ClearPortFeature
;
275 USB_HUB_RESET_PORT ResetPort
;
276 USB_HUB_RELEASE Release
;
279 #define USB_US_LAND_ID 0x0409
281 #define DEVICE_PATH_LIST_ITEM_SIGNATURE SIGNATURE_32('d','p','l','i')
282 typedef struct _DEVICE_PATH_LIST_ITEM
{
285 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
286 } DEVICE_PATH_LIST_ITEM
;
289 USB_CLASS_DEVICE_PATH UsbClass
;
290 EFI_DEVICE_PATH_PROTOCOL End
;
291 } USB_CLASS_FORMAT_DEVICE_PATH
;
294 Free a DEVICE_PATH_LIST_ITEM list.
296 @param UsbIoDPList a DEVICE_PATH_LIST_ITEM list pointer.
298 @retval EFI_INVALID_PARAMETER If parameters are invalid, return this value.
299 @retval EFI_SUCCESS If free operation is successful, return this value.
304 UsbBusFreeUsbDPList (
305 IN LIST_ENTRY
*UsbIoDPList
309 Store a wanted usb child device info (its Usb part of device path) which is indicated by
310 RemainingDevicePath in a Usb bus which is indicated by UsbBusId.
312 @param UsbBusId Point to EFI_USB_BUS_PROTOCOL interface.
313 @param RemainingDevicePath The remaining device patch.
315 @retval EFI_SUCCESS Add operation is successful.
316 @retval EFI_INVALID_PARAMETER The parameters are invalid.
321 UsbBusAddWantedUsbIoDP (
322 IN EFI_USB_BUS_PROTOCOL
*UsbBusId
,
323 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
327 Check whether a usb child device is the wanted device in a bus.
329 @param Bus The Usb bus's private data pointer.
330 @param UsbIf The usb child device inferface.
332 @retval True If a usb child device is the wanted device in a bus.
333 @retval False If a usb child device is *NOT* the wanted device in a bus.
338 UsbBusIsWantedUsbIO (
340 IN USB_INTERFACE
*UsbIf
344 Recursively connnect every wanted usb child device to ensure they all fully connected.
345 Check all the child Usb IO handles in this bus, recursively connecte if it is wanted usb child device.
347 @param UsbBusId Point to EFI_USB_BUS_PROTOCOL interface.
349 @retval EFI_SUCCESS Connect is done successfully.
350 @retval EFI_INVALID_PARAMETER The parameter is invalid.
355 UsbBusRecursivelyConnectWantedUsbIo (
356 IN EFI_USB_BUS_PROTOCOL
*UsbBusId
360 USB_IO function to execute a control transfer. This
361 function will execute the USB transfer. If transfer
362 successes, it will sync the internal state of USB bus
365 @param This The USB_IO instance
366 @param Request The control transfer request
367 @param Direction Direction for data stage
368 @param Timeout The time to wait before timeout
369 @param Data The buffer holding the data
370 @param DataLength Then length of the data
371 @param UsbStatus USB result
373 @retval EFI_INVALID_PARAMETER The parameters are invalid
374 @retval EFI_SUCCESS The control transfer succeded.
375 @retval Others Failed to execute the transfer
380 UsbIoControlTransfer (
381 IN EFI_USB_IO_PROTOCOL
*This
,
382 IN EFI_USB_DEVICE_REQUEST
*Request
,
383 IN EFI_USB_DATA_DIRECTION Direction
,
385 IN OUT VOID
*Data
, OPTIONAL
386 IN UINTN DataLength
, OPTIONAL
387 OUT UINT32
*UsbStatus
391 Execute a bulk transfer to the device endpoint.
393 @param This The USB IO instance.
394 @param Endpoint The device endpoint.
395 @param Data The data to transfer.
396 @param DataLength The length of the data to transfer.
397 @param Timeout Time to wait before timeout.
398 @param UsbStatus The result of USB transfer.
400 @retval EFI_SUCCESS The bulk transfer is OK.
401 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
402 @retval Others Failed to execute transfer, reason returned in
409 IN EFI_USB_IO_PROTOCOL
*This
,
412 IN OUT UINTN
*DataLength
,
414 OUT UINT32
*UsbStatus
418 Execute a synchronous interrupt transfer.
420 @param This The USB IO instance.
421 @param Endpoint The device endpoint.
422 @param Data The data to transfer.
423 @param DataLength The length of the data to transfer.
424 @param Timeout Time to wait before timeout.
425 @param UsbStatus The result of USB transfer.
427 @retval EFI_SUCCESS The synchronous interrupt transfer is OK.
428 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
429 @retval Others Failed to execute transfer, reason returned in
435 UsbIoSyncInterruptTransfer (
436 IN EFI_USB_IO_PROTOCOL
*This
,
439 IN OUT UINTN
*DataLength
,
441 OUT UINT32
*UsbStatus
445 Queue a new asynchronous interrupt transfer, or remove the old
446 request if (IsNewTransfer == FALSE).
448 @param This The USB_IO instance.
449 @param Endpoint The device endpoint.
450 @param IsNewTransfer Whether this is a new request, if it's old, remove
452 @param PollInterval The interval to poll the transfer result, (in ms).
453 @param DataLength The length of perodic data transfer.
454 @param Callback The function to call periodicaly when transfer is
456 @param Context The context to the callback.
458 @retval EFI_SUCCESS New transfer is queued or old request is removed.
459 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
460 @retval Others Failed to queue the new request or remove the old
466 UsbIoAsyncInterruptTransfer (
467 IN EFI_USB_IO_PROTOCOL
*This
,
469 IN BOOLEAN IsNewTransfer
,
470 IN UINTN PollInterval
, OPTIONAL
471 IN UINTN DataLength
, OPTIONAL
472 IN EFI_ASYNC_USB_TRANSFER_CALLBACK Callback
, OPTIONAL
473 IN VOID
*Context OPTIONAL
477 Execute a synchronous isochronous transfer.
479 @param This The USB IO instance.
480 @param DeviceEndpoint The device endpoint.
481 @param Data The data to transfer.
482 @param DataLength The length of the data to transfer.
483 @param UsbStatus The result of USB transfer.
485 @retval EFI_UNSUPPORTED Currently isochronous transfer isn't supported.
490 UsbIoIsochronousTransfer (
491 IN EFI_USB_IO_PROTOCOL
*This
,
492 IN UINT8 DeviceEndpoint
,
499 Queue an asynchronous isochronous transfer.
501 @param This The USB_IO instance.
502 @param DeviceEndpoint The device endpoint.
503 @param Data The data to transfer.
504 @param DataLength The length of perodic data transfer.
505 @param IsochronousCallBack The function to call periodicaly when transfer is
507 @param Context The context to the callback.
509 @retval EFI_UNSUPPORTED Currently isochronous transfer isn't supported.
514 UsbIoAsyncIsochronousTransfer (
515 IN EFI_USB_IO_PROTOCOL
*This
,
516 IN UINT8 DeviceEndpoint
,
519 IN EFI_ASYNC_USB_TRANSFER_CALLBACK IsochronousCallBack
,
520 IN VOID
*Context OPTIONAL
524 Retrieve the device descriptor of the device.
526 @param This The USB IO instance.
527 @param Descriptor The variable to receive the device descriptor.
529 @retval EFI_SUCCESS The device descriptor is returned.
530 @retval EFI_INVALID_PARAMETER The parameter is invalid.
535 UsbIoGetDeviceDescriptor (
536 IN EFI_USB_IO_PROTOCOL
*This
,
537 OUT EFI_USB_DEVICE_DESCRIPTOR
*Descriptor
541 Return the configuration descriptor of the current active configuration.
543 @param This The USB IO instance.
544 @param Descriptor The USB configuration descriptor.
546 @retval EFI_SUCCESS The active configuration descriptor is returned.
547 @retval EFI_INVALID_PARAMETER Some parameter is invalid.
548 @retval EFI_NOT_FOUND Currently no active configuration is selected.
553 UsbIoGetActiveConfigDescriptor (
554 IN EFI_USB_IO_PROTOCOL
*This
,
555 OUT EFI_USB_CONFIG_DESCRIPTOR
*Descriptor
559 Retrieve the active interface setting descriptor for this USB IO instance.
561 @param This The USB IO instance.
562 @param Descriptor The variable to receive active interface setting.
564 @retval EFI_SUCCESS The active interface setting is returned.
565 @retval EFI_INVALID_PARAMETER Some parameter is invalid.
570 UsbIoGetInterfaceDescriptor (
571 IN EFI_USB_IO_PROTOCOL
*This
,
572 OUT EFI_USB_INTERFACE_DESCRIPTOR
*Descriptor
576 Retrieve the endpoint descriptor from this interface setting.
578 @param This The USB IO instance.
579 @param Index The index (start from zero) of the endpoint to
581 @param Descriptor The variable to receive the descriptor.
583 @retval EFI_SUCCESS The endpoint descriptor is returned.
584 @retval EFI_INVALID_PARAMETER Some parameter is invalid.
589 UsbIoGetEndpointDescriptor (
590 IN EFI_USB_IO_PROTOCOL
*This
,
592 OUT EFI_USB_ENDPOINT_DESCRIPTOR
*Descriptor
596 Retrieve the supported language ID table from the device.
598 @param This The USB IO instance.
599 @param LangIDTable The table to return the language IDs.
600 @param TableSize The size, in bytes, of the table LangIDTable.
602 @retval EFI_SUCCESS The language ID is return.
607 UsbIoGetSupportedLanguages (
608 IN EFI_USB_IO_PROTOCOL
*This
,
609 OUT UINT16
**LangIDTable
,
610 OUT UINT16
*TableSize
614 Retrieve an indexed string in the language of LangID.
616 @param This The USB IO instance.
617 @param LangID The language ID of the string to retrieve.
618 @param StringIndex The index of the string.
619 @param String The variable to receive the string.
621 @retval EFI_SUCCESS The string is returned.
622 @retval EFI_NOT_FOUND No such string existed.
627 UsbIoGetStringDescriptor (
628 IN EFI_USB_IO_PROTOCOL
*This
,
630 IN UINT8 StringIndex
,
635 Reset the device, then if that succeeds, reconfigure the
636 device with its address and current active configuration.
638 @param This The USB IO instance.
640 @retval EFI_SUCCESS The device is reset and configured.
641 @retval Others Failed to reset the device.
647 IN EFI_USB_IO_PROTOCOL
*This
651 Install Usb Bus Protocol on host controller, and start the Usb bus.
653 @param This The USB bus driver binding instance.
654 @param Controller The controller to check.
655 @param RemainingDevicePath The remaining device patch.
657 @retval EFI_SUCCESS The controller is controlled by the usb bus.
658 @retval EFI_ALREADY_STARTED The controller is already controlled by the usb bus.
659 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
664 UsbBusBuildProtocol (
665 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
666 IN EFI_HANDLE Controller
,
667 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
671 The USB bus driver entry pointer.
673 @param ImageHandle The driver image handle.
674 @param SystemTable The system table.
676 @return EFI_SUCCESS The component name protocol is installed.
677 @return Others Failed to init the usb driver.
682 UsbBusDriverEntryPoint (
683 IN EFI_HANDLE ImageHandle
,
684 IN EFI_SYSTEM_TABLE
*SystemTable
688 Check whether USB bus driver support this device.
690 @param This The USB bus driver binding protocol.
691 @param Controller The controller handle to check.
692 @param RemainingDevicePath The remaining device path.
694 @retval EFI_SUCCESS The bus supports this controller.
695 @retval EFI_UNSUPPORTED This device isn't supported.
700 UsbBusControllerDriverSupported (
701 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
702 IN EFI_HANDLE Controller
,
703 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
707 Start to process the controller.
709 @param This The USB bus driver binding instance.
710 @param Controller The controller to check.
711 @param RemainingDevicePath The remaining device patch.
713 @retval EFI_SUCCESS The controller is controlled by the usb bus.
714 @retval EFI_ALREADY_STARTED The controller is already controlled by the usb
716 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
721 UsbBusControllerDriverStart (
722 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
723 IN EFI_HANDLE Controller
,
724 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
728 Stop handle the controller by this USB bus driver.
730 @param This The USB bus driver binding protocol.
731 @param Controller The controller to release.
732 @param NumberOfChildren The child of USB bus that opened controller
734 @param ChildHandleBuffer The array of child handle.
736 @retval EFI_SUCCESS The controller or children are stopped.
737 @retval EFI_DEVICE_ERROR Failed to stop the driver.
742 UsbBusControllerDriverStop (
743 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
744 IN EFI_HANDLE Controller
,
745 IN UINTN NumberOfChildren
,
746 IN EFI_HANDLE
*ChildHandleBuffer
749 extern EFI_USB_IO_PROTOCOL mUsbIoProtocol
;
750 extern EFI_DRIVER_BINDING_PROTOCOL mUsbBusDriverBinding
;
751 extern EFI_COMPONENT_NAME_PROTOCOL mUsbBusComponentName
;
752 extern EFI_COMPONENT_NAME2_PROTOCOL mUsbBusComponentName2
;