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
196 // Stands for different functions of USB device
198 struct _USB_INTERFACE
{
201 USB_INTERFACE_DESC
*IfDesc
;
202 USB_INTERFACE_SETTING
*IfSetting
;
205 // Handles and protocols
208 EFI_USB_IO_PROTOCOL UsbIo
;
209 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
213 // Hub device special data
221 // Data used only by normal hub devices
223 USB_ENDPOINT_DESC
*HubEp
;
227 // Data used only by root hub to hand over device to
228 // companion UHCI driver if low/full speed devices are
229 // connected to EHCI.
235 // Stands for the current USB Bus
239 EFI_USB_BUS_PROTOCOL BusId
;
242 // Managed USB host controller
244 EFI_HANDLE HostHandle
;
245 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
246 EFI_USB2_HC_PROTOCOL
*Usb2Hc
;
247 EFI_USB_HC_PROTOCOL
*UsbHc
;
250 // Recorded the max supported usb devices.
251 // XHCI can support up to 255 devices.
252 // EHCI/UHCI/OHCI supports up to 127 devices.
256 // An array of device that is on the bus. Devices[0] is
257 // for root hub. Device with address i is at Devices[i].
259 USB_DEVICE
*Devices
[256];
262 // USB Bus driver need to control the recursive connect policy of the bus, only those wanted
263 // usb child device will be recursively connected.
265 // WantedUsbIoDPList tracks the Usb child devices which user want to recursivly fully connecte,
266 // every wanted child device is stored in a item of the WantedUsbIoDPList, whose structrure is
267 // DEVICE_PATH_LIST_ITEM
269 LIST_ENTRY WantedUsbIoDPList
;
278 USB_HUB_GET_PORT_STATUS GetPortStatus
;
279 USB_HUB_CLEAR_PORT_CHANGE ClearPortChange
;
280 USB_HUB_SET_PORT_FEATURE SetPortFeature
;
281 USB_HUB_CLEAR_PORT_FEATURE ClearPortFeature
;
282 USB_HUB_RESET_PORT ResetPort
;
283 USB_HUB_RELEASE Release
;
286 #define USB_US_LAND_ID 0x0409
288 #define DEVICE_PATH_LIST_ITEM_SIGNATURE SIGNATURE_32('d','p','l','i')
289 typedef struct _DEVICE_PATH_LIST_ITEM
{
292 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
293 } DEVICE_PATH_LIST_ITEM
;
296 USB_CLASS_DEVICE_PATH UsbClass
;
297 EFI_DEVICE_PATH_PROTOCOL End
;
298 } USB_CLASS_FORMAT_DEVICE_PATH
;
301 Free a DEVICE_PATH_LIST_ITEM list.
303 @param UsbIoDPList a DEVICE_PATH_LIST_ITEM list pointer.
305 @retval EFI_INVALID_PARAMETER If parameters are invalid, return this value.
306 @retval EFI_SUCCESS If free operation is successful, return this value.
311 UsbBusFreeUsbDPList (
312 IN LIST_ENTRY
*UsbIoDPList
316 Store a wanted usb child device info (its Usb part of device path) which is indicated by
317 RemainingDevicePath in a Usb bus which is indicated by UsbBusId.
319 @param UsbBusId Point to EFI_USB_BUS_PROTOCOL interface.
320 @param RemainingDevicePath The remaining device patch.
322 @retval EFI_SUCCESS Add operation is successful.
323 @retval EFI_INVALID_PARAMETER The parameters are invalid.
328 UsbBusAddWantedUsbIoDP (
329 IN EFI_USB_BUS_PROTOCOL
*UsbBusId
,
330 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
334 Check whether a usb child device is the wanted device in a bus.
336 @param Bus The Usb bus's private data pointer.
337 @param UsbIf The usb child device inferface.
339 @retval True If a usb child device is the wanted device in a bus.
340 @retval False If a usb child device is *NOT* the wanted device in a bus.
345 UsbBusIsWantedUsbIO (
347 IN USB_INTERFACE
*UsbIf
351 Recursively connnect every wanted usb child device to ensure they all fully connected.
352 Check all the child Usb IO handles in this bus, recursively connecte if it is wanted usb child device.
354 @param UsbBusId Point to EFI_USB_BUS_PROTOCOL interface.
356 @retval EFI_SUCCESS Connect is done successfully.
357 @retval EFI_INVALID_PARAMETER The parameter is invalid.
362 UsbBusRecursivelyConnectWantedUsbIo (
363 IN EFI_USB_BUS_PROTOCOL
*UsbBusId
367 USB_IO function to execute a control transfer. This
368 function will execute the USB transfer. If transfer
369 successes, it will sync the internal state of USB bus
372 @param This The USB_IO instance
373 @param Request The control transfer request
374 @param Direction Direction for data stage
375 @param Timeout The time to wait before timeout
376 @param Data The buffer holding the data
377 @param DataLength Then length of the data
378 @param UsbStatus USB result
380 @retval EFI_INVALID_PARAMETER The parameters are invalid
381 @retval EFI_SUCCESS The control transfer succeded.
382 @retval Others Failed to execute the transfer
387 UsbIoControlTransfer (
388 IN EFI_USB_IO_PROTOCOL
*This
,
389 IN EFI_USB_DEVICE_REQUEST
*Request
,
390 IN EFI_USB_DATA_DIRECTION Direction
,
392 IN OUT VOID
*Data
, OPTIONAL
393 IN UINTN DataLength
, OPTIONAL
394 OUT UINT32
*UsbStatus
398 Execute a bulk transfer to the device endpoint.
400 @param This The USB IO instance.
401 @param Endpoint The device endpoint.
402 @param Data The data to transfer.
403 @param DataLength The length of the data to transfer.
404 @param Timeout Time to wait before timeout.
405 @param UsbStatus The result of USB transfer.
407 @retval EFI_SUCCESS The bulk transfer is OK.
408 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
409 @retval Others Failed to execute transfer, reason returned in
416 IN EFI_USB_IO_PROTOCOL
*This
,
419 IN OUT UINTN
*DataLength
,
421 OUT UINT32
*UsbStatus
425 Execute a synchronous interrupt transfer.
427 @param This The USB IO instance.
428 @param Endpoint The device endpoint.
429 @param Data The data to transfer.
430 @param DataLength The length of the data to transfer.
431 @param Timeout Time to wait before timeout.
432 @param UsbStatus The result of USB transfer.
434 @retval EFI_SUCCESS The synchronous interrupt transfer is OK.
435 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
436 @retval Others Failed to execute transfer, reason returned in
442 UsbIoSyncInterruptTransfer (
443 IN EFI_USB_IO_PROTOCOL
*This
,
446 IN OUT UINTN
*DataLength
,
448 OUT UINT32
*UsbStatus
452 Queue a new asynchronous interrupt transfer, or remove the old
453 request if (IsNewTransfer == FALSE).
455 @param This The USB_IO instance.
456 @param Endpoint The device endpoint.
457 @param IsNewTransfer Whether this is a new request, if it's old, remove
459 @param PollInterval The interval to poll the transfer result, (in ms).
460 @param DataLength The length of perodic data transfer.
461 @param Callback The function to call periodicaly when transfer is
463 @param Context The context to the callback.
465 @retval EFI_SUCCESS New transfer is queued or old request is removed.
466 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
467 @retval Others Failed to queue the new request or remove the old
473 UsbIoAsyncInterruptTransfer (
474 IN EFI_USB_IO_PROTOCOL
*This
,
476 IN BOOLEAN IsNewTransfer
,
477 IN UINTN PollInterval
, OPTIONAL
478 IN UINTN DataLength
, OPTIONAL
479 IN EFI_ASYNC_USB_TRANSFER_CALLBACK Callback
, OPTIONAL
480 IN VOID
*Context OPTIONAL
484 Execute a synchronous isochronous transfer.
486 @param This The USB IO instance.
487 @param DeviceEndpoint The device endpoint.
488 @param Data The data to transfer.
489 @param DataLength The length of the data to transfer.
490 @param UsbStatus The result of USB transfer.
492 @retval EFI_UNSUPPORTED Currently isochronous transfer isn't supported.
497 UsbIoIsochronousTransfer (
498 IN EFI_USB_IO_PROTOCOL
*This
,
499 IN UINT8 DeviceEndpoint
,
506 Queue an asynchronous isochronous transfer.
508 @param This The USB_IO instance.
509 @param DeviceEndpoint The device endpoint.
510 @param Data The data to transfer.
511 @param DataLength The length of perodic data transfer.
512 @param IsochronousCallBack The function to call periodicaly when transfer is
514 @param Context The context to the callback.
516 @retval EFI_UNSUPPORTED Currently isochronous transfer isn't supported.
521 UsbIoAsyncIsochronousTransfer (
522 IN EFI_USB_IO_PROTOCOL
*This
,
523 IN UINT8 DeviceEndpoint
,
526 IN EFI_ASYNC_USB_TRANSFER_CALLBACK IsochronousCallBack
,
527 IN VOID
*Context OPTIONAL
531 Retrieve the device descriptor of the device.
533 @param This The USB IO instance.
534 @param Descriptor The variable to receive the device descriptor.
536 @retval EFI_SUCCESS The device descriptor is returned.
537 @retval EFI_INVALID_PARAMETER The parameter is invalid.
542 UsbIoGetDeviceDescriptor (
543 IN EFI_USB_IO_PROTOCOL
*This
,
544 OUT EFI_USB_DEVICE_DESCRIPTOR
*Descriptor
548 Return the configuration descriptor of the current active configuration.
550 @param This The USB IO instance.
551 @param Descriptor The USB configuration descriptor.
553 @retval EFI_SUCCESS The active configuration descriptor is returned.
554 @retval EFI_INVALID_PARAMETER Some parameter is invalid.
555 @retval EFI_NOT_FOUND Currently no active configuration is selected.
560 UsbIoGetActiveConfigDescriptor (
561 IN EFI_USB_IO_PROTOCOL
*This
,
562 OUT EFI_USB_CONFIG_DESCRIPTOR
*Descriptor
566 Retrieve the active interface setting descriptor for this USB IO instance.
568 @param This The USB IO instance.
569 @param Descriptor The variable to receive active interface setting.
571 @retval EFI_SUCCESS The active interface setting is returned.
572 @retval EFI_INVALID_PARAMETER Some parameter is invalid.
577 UsbIoGetInterfaceDescriptor (
578 IN EFI_USB_IO_PROTOCOL
*This
,
579 OUT EFI_USB_INTERFACE_DESCRIPTOR
*Descriptor
583 Retrieve the endpoint descriptor from this interface setting.
585 @param This The USB IO instance.
586 @param Index The index (start from zero) of the endpoint to
588 @param Descriptor The variable to receive the descriptor.
590 @retval EFI_SUCCESS The endpoint descriptor is returned.
591 @retval EFI_INVALID_PARAMETER Some parameter is invalid.
596 UsbIoGetEndpointDescriptor (
597 IN EFI_USB_IO_PROTOCOL
*This
,
599 OUT EFI_USB_ENDPOINT_DESCRIPTOR
*Descriptor
603 Retrieve the supported language ID table from the device.
605 @param This The USB IO instance.
606 @param LangIDTable The table to return the language IDs.
607 @param TableSize The size, in bytes, of the table LangIDTable.
609 @retval EFI_SUCCESS The language ID is return.
614 UsbIoGetSupportedLanguages (
615 IN EFI_USB_IO_PROTOCOL
*This
,
616 OUT UINT16
**LangIDTable
,
617 OUT UINT16
*TableSize
621 Retrieve an indexed string in the language of LangID.
623 @param This The USB IO instance.
624 @param LangID The language ID of the string to retrieve.
625 @param StringIndex The index of the string.
626 @param String The variable to receive the string.
628 @retval EFI_SUCCESS The string is returned.
629 @retval EFI_NOT_FOUND No such string existed.
634 UsbIoGetStringDescriptor (
635 IN EFI_USB_IO_PROTOCOL
*This
,
637 IN UINT8 StringIndex
,
642 Reset the device, then if that succeeds, reconfigure the
643 device with its address and current active configuration.
645 @param This The USB IO instance.
647 @retval EFI_SUCCESS The device is reset and configured.
648 @retval Others Failed to reset the device.
654 IN EFI_USB_IO_PROTOCOL
*This
658 Install Usb Bus Protocol on host controller, and start the Usb bus.
660 @param This The USB bus driver binding instance.
661 @param Controller The controller to check.
662 @param RemainingDevicePath The remaining device patch.
664 @retval EFI_SUCCESS The controller is controlled by the usb bus.
665 @retval EFI_ALREADY_STARTED The controller is already controlled by the usb bus.
666 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
671 UsbBusBuildProtocol (
672 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
673 IN EFI_HANDLE Controller
,
674 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
678 The USB bus driver entry pointer.
680 @param ImageHandle The driver image handle.
681 @param SystemTable The system table.
683 @return EFI_SUCCESS The component name protocol is installed.
684 @return Others Failed to init the usb driver.
689 UsbBusDriverEntryPoint (
690 IN EFI_HANDLE ImageHandle
,
691 IN EFI_SYSTEM_TABLE
*SystemTable
695 Check whether USB bus driver support this device.
697 @param This The USB bus driver binding protocol.
698 @param Controller The controller handle to check.
699 @param RemainingDevicePath The remaining device path.
701 @retval EFI_SUCCESS The bus supports this controller.
702 @retval EFI_UNSUPPORTED This device isn't supported.
707 UsbBusControllerDriverSupported (
708 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
709 IN EFI_HANDLE Controller
,
710 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
714 Start to process the controller.
716 @param This The USB bus driver binding instance.
717 @param Controller The controller to check.
718 @param RemainingDevicePath The remaining device patch.
720 @retval EFI_SUCCESS The controller is controlled by the usb bus.
721 @retval EFI_ALREADY_STARTED The controller is already controlled by the usb
723 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
728 UsbBusControllerDriverStart (
729 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
730 IN EFI_HANDLE Controller
,
731 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
735 Stop handle the controller by this USB bus driver.
737 @param This The USB bus driver binding protocol.
738 @param Controller The controller to release.
739 @param NumberOfChildren The child of USB bus that opened controller
741 @param ChildHandleBuffer The array of child handle.
743 @retval EFI_SUCCESS The controller or children are stopped.
744 @retval EFI_DEVICE_ERROR Failed to stop the driver.
749 UsbBusControllerDriverStop (
750 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
751 IN EFI_HANDLE Controller
,
752 IN UINTN NumberOfChildren
,
753 IN EFI_HANDLE
*ChildHandleBuffer
756 extern EFI_USB_IO_PROTOCOL mUsbIoProtocol
;
757 extern EFI_DRIVER_BINDING_PROTOCOL mUsbBusDriverBinding
;
758 extern EFI_COMPONENT_NAME_PROTOCOL mUsbBusComponentName
;
759 extern EFI_COMPONENT_NAME2_PROTOCOL mUsbBusComponentName2
;