2 This file implements I2C IO Protocol which enables the user to manipulate a single
3 I2C device independent of the host controller and I2C design.
5 Copyright (c) 2013 - 2015, 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.
19 // EFI_DRIVER_BINDING_PROTOCOL instance
21 EFI_DRIVER_BINDING_PROTOCOL gI2cBusDriverBinding
= {
22 I2cBusDriverSupported
,
31 // Template for I2C Bus Child Device.
33 I2C_DEVICE_CONTEXT gI2cDeviceContextTemplate
= {
37 I2cBusQueueRequest
, // QueueRequest
40 0, // HardwareRevision
41 NULL
// I2cControllerCapabilities
45 NULL
, // I2cBusContext
49 // Template for controller device path node.
51 CONTROLLER_DEVICE_PATH gControllerDevicePathTemplate
= {
56 (UINT8
) (sizeof (CONTROLLER_DEVICE_PATH
)),
57 (UINT8
) ((sizeof (CONTROLLER_DEVICE_PATH
)) >> 8)
64 // Template for vendor device path node.
66 VENDOR_DEVICE_PATH gVendorDevicePathTemplate
= {
71 (UINT8
) (sizeof (VENDOR_DEVICE_PATH
)),
72 (UINT8
) ((sizeof (VENDOR_DEVICE_PATH
)) >> 8)
75 { 0x0, 0x0, 0x0, { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }}
81 GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE mI2cBusDriverNameTable
[] = {
82 { "eng;en", (CHAR16
*) L
"I2C Bus Driver" },
87 // EFI Component Name Protocol
89 GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL gI2cBusComponentName
= {
90 (EFI_COMPONENT_NAME_GET_DRIVER_NAME
) I2cBusComponentNameGetDriverName
,
91 (EFI_COMPONENT_NAME_GET_CONTROLLER_NAME
) I2cBusComponentNameGetControllerName
,
96 // EFI Component Name 2 Protocol
98 GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL gI2cBusComponentName2
= {
99 I2cBusComponentNameGetDriverName
,
100 I2cBusComponentNameGetControllerName
,
105 Retrieves a Unicode string that is the user readable name of the driver.
107 This function retrieves the user readable name of a driver in the form of a
108 Unicode string. If the driver specified by This has a user readable name in
109 the language specified by Language, then a pointer to the driver name is
110 returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
111 by This does not support the language specified by Language,
112 then EFI_UNSUPPORTED is returned.
114 @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
115 EFI_COMPONENT_NAME_PROTOCOL instance.
117 @param Language[in] A pointer to a Null-terminated ASCII string
118 array indicating the language. This is the
119 language of the driver name that the caller is
120 requesting, and it must match one of the
121 languages specified in SupportedLanguages. The
122 number of languages supported by a driver is up
123 to the driver writer. Language is specified
124 in RFC 4646 or ISO 639-2 language code format.
126 @param DriverName[out] A pointer to the Unicode string to return.
127 This Unicode string is the name of the
128 driver specified by This in the language
129 specified by Language.
131 @retval EFI_SUCCESS The Unicode string for the Driver specified by
132 This and the language specified by Language was
133 returned in DriverName.
135 @retval EFI_INVALID_PARAMETER Language is NULL.
137 @retval EFI_INVALID_PARAMETER DriverName is NULL.
139 @retval EFI_UNSUPPORTED The driver specified by This does not support
140 the language specified by Language.
145 I2cBusComponentNameGetDriverName (
146 IN EFI_COMPONENT_NAME2_PROTOCOL
*This
,
148 OUT CHAR16
**DriverName
151 return LookupUnicodeString2 (
153 This
->SupportedLanguages
,
154 mI2cBusDriverNameTable
,
156 (BOOLEAN
)(This
!= &gI2cBusComponentName2
)
161 Retrieves a Unicode string that is the user readable name of the controller
162 that is being managed by a driver.
164 This function retrieves the user readable name of the controller specified by
165 ControllerHandle and ChildHandle in the form of a Unicode string. If the
166 driver specified by This has a user readable name in the language specified by
167 Language, then a pointer to the controller name is returned in ControllerName,
168 and EFI_SUCCESS is returned. If the driver specified by This is not currently
169 managing the controller specified by ControllerHandle and ChildHandle,
170 then EFI_UNSUPPORTED is returned. If the driver specified by This does not
171 support the language specified by Language, then EFI_UNSUPPORTED is returned.
173 @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
174 EFI_COMPONENT_NAME_PROTOCOL instance.
176 @param ControllerHandle[in] The handle of a controller that the driver
177 specified by This is managing. This handle
178 specifies the controller whose name is to be
181 @param ChildHandle[in] The handle of the child controller to retrieve
182 the name of. This is an optional parameter that
183 may be NULL. It will be NULL for device
184 drivers. It will also be NULL for a bus drivers
185 that wish to retrieve the name of the bus
186 controller. It will not be NULL for a bus
187 driver that wishes to retrieve the name of a
190 @param Language[in] A pointer to a Null-terminated ASCII string
191 array indicating the language. This is the
192 language of the driver name that the caller is
193 requesting, and it must match one of the
194 languages specified in SupportedLanguages. The
195 number of languages supported by a driver is up
196 to the driver writer. Language is specified in
197 RFC 4646 or ISO 639-2 language code format.
199 @param ControllerName[out] A pointer to the Unicode string to return.
200 This Unicode string is the name of the
201 controller specified by ControllerHandle and
202 ChildHandle in the language specified by
203 Language from the point of view of the driver
206 @retval EFI_SUCCESS The Unicode string for the user readable name in
207 the language specified by Language for the
208 driver specified by This was returned in
211 @retval EFI_INVALID_PARAMETER ControllerHandle is NULL.
213 @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid
216 @retval EFI_INVALID_PARAMETER Language is NULL.
218 @retval EFI_INVALID_PARAMETER ControllerName is NULL.
220 @retval EFI_UNSUPPORTED The driver specified by This is not currently
221 managing the controller specified by
222 ControllerHandle and ChildHandle.
224 @retval EFI_UNSUPPORTED The driver specified by This does not support
225 the language specified by Language.
230 I2cBusComponentNameGetControllerName (
231 IN EFI_COMPONENT_NAME2_PROTOCOL
*This
,
232 IN EFI_HANDLE ControllerHandle
,
233 IN EFI_HANDLE ChildHandle OPTIONAL
,
235 OUT CHAR16
**ControllerName
238 return EFI_UNSUPPORTED
;
242 Check if the child of I2C controller has been created.
244 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
245 @param[in] Controller I2C controller handle.
246 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path.
247 @param[in] RemainingHasControllerNode Indicate if RemainingDevicePath contains CONTROLLER_DEVICE_PATH.
248 @param[in] RemainingControllerNumber Controller number in CONTROLLER_DEVICE_PATH.
250 @retval EFI_SUCCESS The child of I2C controller is not created.
251 @retval Others The child of I2C controller has been created or other errors happen.
255 CheckRemainingDevicePath (
256 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
257 IN EFI_HANDLE Controller
,
258 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
,
259 IN BOOLEAN RemainingHasControllerNode
,
260 IN UINT32 RemainingControllerNumber
264 EFI_DEVICE_PATH_PROTOCOL
*SystemDevicePath
;
265 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenInfoBuffer
;
268 BOOLEAN SystemHasControllerNode
;
269 UINT32 SystemControllerNumber
;
271 SystemHasControllerNode
= FALSE
;
272 SystemControllerNumber
= 0;
274 Status
= gBS
->OpenProtocolInformation (
276 &gEfiI2cHostProtocolGuid
,
280 if (EFI_ERROR (Status
)) {
284 for (Index
= 0; Index
< EntryCount
; Index
++) {
285 if ((OpenInfoBuffer
[Index
].Attributes
& EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
) != 0) {
286 Status
= gBS
->OpenProtocol (
287 OpenInfoBuffer
[Index
].ControllerHandle
,
288 &gEfiDevicePathProtocolGuid
,
289 (VOID
**) &SystemDevicePath
,
290 This
->DriverBindingHandle
,
292 EFI_OPEN_PROTOCOL_GET_PROTOCOL
294 if (!EFI_ERROR (Status
)) {
296 // Find vendor device path node and compare
298 while (!IsDevicePathEnd (SystemDevicePath
)) {
299 if ((DevicePathType (SystemDevicePath
) == HARDWARE_DEVICE_PATH
) &&
300 (DevicePathSubType (SystemDevicePath
) == HW_VENDOR_DP
)) {
302 // Check if vendor device path is same between system device path and remaining device path
304 if (CompareMem (SystemDevicePath
, RemainingDevicePath
, sizeof (VENDOR_DEVICE_PATH
)) == 0) {
306 // Get controller node appended after vendor node
308 SystemDevicePath
= NextDevicePathNode (SystemDevicePath
);
309 if ((DevicePathType (SystemDevicePath
) == HARDWARE_DEVICE_PATH
) &&
310 (DevicePathSubType (SystemDevicePath
) == HW_CONTROLLER_DP
)) {
311 SystemHasControllerNode
= TRUE
;
312 SystemControllerNumber
= ((CONTROLLER_DEVICE_PATH
*) SystemDevicePath
)->ControllerNumber
;
314 SystemHasControllerNode
= FALSE
;
315 SystemControllerNumber
= 0;
317 if (((SystemHasControllerNode
) && (!RemainingHasControllerNode
) && (SystemControllerNumber
== 0)) ||
318 ((!SystemHasControllerNode
) && (RemainingHasControllerNode
) && (RemainingControllerNumber
== 0)) ||
319 ((SystemHasControllerNode
) && (RemainingHasControllerNode
) && (SystemControllerNumber
== RemainingControllerNumber
)) ||
320 ((!SystemHasControllerNode
) && (!RemainingHasControllerNode
))) {
321 DEBUG ((EFI_D_ERROR
, "This I2C device has been already started.\n"));
322 Status
= EFI_UNSUPPORTED
;
327 SystemDevicePath
= NextDevicePathNode (SystemDevicePath
);
329 if (EFI_ERROR (Status
)) {
335 FreePool (OpenInfoBuffer
);
340 Tests to see if this driver supports a given controller. If a child device is provided,
341 it further tests to see if this driver supports creating a handle for the specified child device.
343 This function checks to see if the driver specified by This supports the device specified by
344 ControllerHandle. Drivers will typically use the device path attached to
345 ControllerHandle and/or the services from the bus I/O abstraction attached to
346 ControllerHandle to determine if the driver supports ControllerHandle. This function
347 may be called many times during platform initialization. In order to reduce boot times, the tests
348 performed by this function must be very small, and take as little time as possible to execute. This
349 function must not change the state of any hardware devices, and this function must be aware that the
350 device specified by ControllerHandle may already be managed by the same driver or a
351 different driver. This function must match its calls to AllocatePages() with FreePages(),
352 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
353 Since ControllerHandle may have been previously started by the same driver, if a protocol is
354 already in the opened state, then it must not be closed with CloseProtocol(). This is required
355 to guarantee the state of ControllerHandle is not modified by this function.
357 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
358 @param[in] ControllerHandle The handle of the controller to test. This handle
359 must support a protocol interface that supplies
360 an I/O abstraction to the driver.
361 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
362 parameter is ignored by device drivers, and is optional for bus
363 drivers. For bus drivers, if this parameter is not NULL, then
364 the bus driver must determine if the bus controller specified
365 by ControllerHandle and the child controller specified
366 by RemainingDevicePath are both supported by this
369 @retval EFI_SUCCESS The device specified by ControllerHandle and
370 RemainingDevicePath is supported by the driver specified by This.
371 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
372 RemainingDevicePath is already being managed by the driver
374 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
375 RemainingDevicePath is already being managed by a different
376 driver or an application that requires exclusive access.
377 Currently not implemented.
378 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
379 RemainingDevicePath is not supported by the driver specified by This.
383 I2cBusDriverSupported (
384 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
385 IN EFI_HANDLE Controller
,
386 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
390 EFI_I2C_ENUMERATE_PROTOCOL
*I2cEnumerate
;
391 EFI_I2C_HOST_PROTOCOL
*I2cHost
;
392 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
393 EFI_DEVICE_PATH_PROTOCOL
*DevPathNode
;
394 BOOLEAN RemainingHasControllerNode
;
395 UINT32 RemainingControllerNumber
;
397 RemainingHasControllerNode
= FALSE
;
398 RemainingControllerNumber
= 0;
401 // Determine if the I2c Enumerate Protocol is available
403 Status
= gBS
->OpenProtocol (
405 &gEfiI2cEnumerateProtocolGuid
,
406 (VOID
**) &I2cEnumerate
,
407 This
->DriverBindingHandle
,
409 EFI_OPEN_PROTOCOL_BY_DRIVER
411 if ((EFI_ERROR (Status
)) && (Status
!= EFI_ALREADY_STARTED
)) {
415 if (!EFI_ERROR (Status
)) {
418 &gEfiI2cEnumerateProtocolGuid
,
419 This
->DriverBindingHandle
,
424 Status
= gBS
->OpenProtocol (
426 &gEfiDevicePathProtocolGuid
,
427 (VOID
**) &ParentDevicePath
,
428 This
->DriverBindingHandle
,
430 EFI_OPEN_PROTOCOL_BY_DRIVER
433 if ((EFI_ERROR (Status
)) && (Status
!= EFI_ALREADY_STARTED
)) {
437 if (!EFI_ERROR (Status
)) {
440 &gEfiDevicePathProtocolGuid
,
441 This
->DriverBindingHandle
,
446 if ((RemainingDevicePath
!= NULL
) && !IsDevicePathEnd (RemainingDevicePath
)) {
448 // Check if the first node of RemainingDevicePath is a hardware vendor device path
450 if ((DevicePathType (RemainingDevicePath
) != HARDWARE_DEVICE_PATH
) ||
451 (DevicePathSubType (RemainingDevicePath
) != HW_VENDOR_DP
)) {
452 return EFI_UNSUPPORTED
;
455 // Check if the second node of RemainingDevicePath is a controller node
457 DevPathNode
= NextDevicePathNode (RemainingDevicePath
);
458 if (!IsDevicePathEnd (DevPathNode
)) {
459 if ((DevicePathType (DevPathNode
) != HARDWARE_DEVICE_PATH
) ||
460 (DevicePathSubType (DevPathNode
) != HW_CONTROLLER_DP
)) {
461 return EFI_UNSUPPORTED
;
463 RemainingHasControllerNode
= TRUE
;
464 RemainingControllerNumber
= ((CONTROLLER_DEVICE_PATH
*) DevPathNode
)->ControllerNumber
;
470 // Determine if the I2C Host Protocol is available
472 Status
= gBS
->OpenProtocol (
474 &gEfiI2cHostProtocolGuid
,
476 This
->DriverBindingHandle
,
478 EFI_OPEN_PROTOCOL_BY_DRIVER
481 if (!EFI_ERROR (Status
)) {
484 &gEfiI2cHostProtocolGuid
,
485 This
->DriverBindingHandle
,
491 if (Status
== EFI_ALREADY_STARTED
) {
492 if ((RemainingDevicePath
== NULL
) ||
493 ((RemainingDevicePath
!= NULL
) && IsDevicePathEnd (RemainingDevicePath
))) {
495 // If RemainingDevicePath is NULL or is the End of Device Path Node, return EFI_SUCCESS.
497 Status
= EFI_SUCCESS
;
500 // Test if the child with the RemainingDevicePath has already been created.
502 Status
= CheckRemainingDevicePath (
506 RemainingHasControllerNode
,
507 RemainingControllerNumber
516 Starts a device controller or a bus controller.
518 The Start() function is designed to be invoked from the EFI boot service ConnectController().
519 As a result, much of the error checking on the parameters to Start() has been moved into this
520 common boot service. It is legal to call Start() from other locations,
521 but the following calling restrictions must be followed or the system behavior will not be deterministic.
522 1. ControllerHandle must be a valid EFI_HANDLE.
523 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
524 EFI_DEVICE_PATH_PROTOCOL.
525 3. Prior to calling Start(), the Supported() function for the driver specified by This must
526 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
528 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
529 @param[in] ControllerHandle The handle of the controller to start. This handle
530 must support a protocol interface that supplies
531 an I/O abstraction to the driver.
532 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
533 parameter is ignored by device drivers, and is optional for bus
534 drivers. For a bus driver, if this parameter is NULL, then handles
535 for all the children of Controller are created by this driver.
536 If this parameter is not NULL and the first Device Path Node is
537 not the End of Device Path Node, then only the handle for the
538 child device specified by the first Device Path Node of
539 RemainingDevicePath is created by this driver.
540 If the first Device Path Node of RemainingDevicePath is
541 the End of Device Path Node, no child handle is created by this
544 @retval EFI_SUCCESS The device was started.
545 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
546 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
547 @retval Others The driver failded to start the device.
553 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
554 IN EFI_HANDLE Controller
,
555 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
558 EFI_I2C_ENUMERATE_PROTOCOL
*I2cEnumerate
;
559 EFI_I2C_HOST_PROTOCOL
*I2cHost
;
560 I2C_BUS_CONTEXT
*I2cBusContext
;
562 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
564 I2cBusContext
= NULL
;
565 ParentDevicePath
= NULL
;
570 // Determine if the I2C controller is available
572 Status
= gBS
->OpenProtocol (
574 &gEfiI2cHostProtocolGuid
,
576 This
->DriverBindingHandle
,
578 EFI_OPEN_PROTOCOL_BY_DRIVER
580 if (EFI_ERROR (Status
) && (Status
!= EFI_ALREADY_STARTED
)) {
581 DEBUG ((EFI_D_ERROR
, "I2cBus: open I2C host error, Status = %r\n", Status
));
585 if (Status
== EFI_ALREADY_STARTED
) {
586 Status
= gBS
->OpenProtocol (
589 (VOID
**) &I2cBusContext
,
590 This
->DriverBindingHandle
,
592 EFI_OPEN_PROTOCOL_GET_PROTOCOL
594 if (EFI_ERROR (Status
)) {
595 DEBUG ((EFI_D_ERROR
, "I2cBus: open private protocol error, Status = %r.\n", Status
));
601 // Get the I2C bus enumeration API
603 Status
= gBS
->OpenProtocol (
605 &gEfiI2cEnumerateProtocolGuid
,
606 (VOID
**)&I2cEnumerate
,
607 This
->DriverBindingHandle
,
609 EFI_OPEN_PROTOCOL_BY_DRIVER
611 if (EFI_ERROR (Status
) && (Status
!= EFI_ALREADY_STARTED
)) {
612 DEBUG ((EFI_D_ERROR
, "I2cBus: open I2C enumerate error, Status = %r\n", Status
));
616 Status
= gBS
->OpenProtocol (
618 &gEfiDevicePathProtocolGuid
,
619 (VOID
**) &ParentDevicePath
,
620 This
->DriverBindingHandle
,
622 EFI_OPEN_PROTOCOL_BY_DRIVER
624 if (EFI_ERROR (Status
) && (Status
!= EFI_ALREADY_STARTED
)) {
625 DEBUG ((EFI_D_ERROR
, "I2cBus: open device path error, Status = %r\n", Status
));
629 if ((RemainingDevicePath
!= NULL
) && IsDevicePathEnd (RemainingDevicePath
)) {
631 // If RemainingDevicePath is the End of Device Path Node,
632 // don't create any child device and return EFI_SUCESS
638 // Allocate the buffer for I2C_BUS_CONTEXT if it is not allocated before.
640 if (I2cBusContext
== NULL
) {
642 // Allocate the I2C context structure for the current I2C controller
644 I2cBusContext
= AllocateZeroPool (sizeof (I2C_BUS_CONTEXT
));
645 if (I2cBusContext
== NULL
) {
646 DEBUG ((EFI_D_ERROR
, "I2cBus: there is no enough memory to allocate.\n"));
647 Status
= EFI_OUT_OF_RESOURCES
;
653 .->| I2C_BUS_CONTEXT|<----- This file Protocol (gEfiCallerIdGuid) installed on I2C Controller handle
656 | +----------------------------+
657 | | I2C_DEVICE_CONTEXT |
660 | I2C IO Protocol Structure | <----- I2C IO Protocol
662 +----------------------------+
665 I2cBusContext
->I2cHost
= I2cHost
;
666 I2cBusContext
->I2cEnumerate
= I2cEnumerate
;
668 // Parent controller used to create children
670 I2cBusContext
->Controller
= Controller
;
672 // Parent controller device path used to create children device path
674 I2cBusContext
->ParentDevicePath
= ParentDevicePath
;
676 I2cBusContext
->DriverBindingHandle
= This
->DriverBindingHandle
;
678 Status
= gBS
->InstallMultipleProtocolInterfaces (
684 if (EFI_ERROR (Status
)) {
685 DEBUG ((EFI_D_ERROR
, "I2cBus: install private protocol error, Status = %r.\n", Status
));
693 Status
= RegisterI2cDevice (I2cBusContext
, Controller
, RemainingDevicePath
);
698 if (EFI_ERROR (Status
)) {
699 DEBUG ((EFI_D_ERROR
, "I2cBus: Start() function failed, Status = %r\n", Status
));
700 if (ParentDevicePath
!= NULL
) {
703 &gEfiDevicePathProtocolGuid
,
704 This
->DriverBindingHandle
,
709 if (I2cHost
!= NULL
) {
712 &gEfiI2cHostProtocolGuid
,
713 This
->DriverBindingHandle
,
718 if (I2cEnumerate
!= NULL
) {
721 &gEfiI2cEnumerateProtocolGuid
,
722 This
->DriverBindingHandle
,
727 if (I2cBusContext
!= NULL
) {
728 Status
= gBS
->UninstallMultipleProtocolInterfaces (
734 FreePool (I2cBusContext
);
739 // Return the operation status.
746 Stops a device controller or a bus controller.
748 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
749 As a result, much of the error checking on the parameters to Stop() has been moved
750 into this common boot service. It is legal to call Stop() from other locations,
751 but the following calling restrictions must be followed or the system behavior will not be deterministic.
752 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
753 same driver's Start() function.
754 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
755 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
756 Start() function, and the Start() function must have called OpenProtocol() on
757 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
759 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
760 @param[in] ControllerHandle A handle to the device being stopped. The handle must
761 support a bus specific I/O protocol for the driver
762 to use to stop the device.
763 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
764 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
765 if NumberOfChildren is 0.
767 @retval EFI_SUCCESS The device was stopped.
768 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
774 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
775 IN EFI_HANDLE Controller
,
776 IN UINTN NumberOfChildren
,
777 IN EFI_HANDLE
*ChildHandleBuffer
780 I2C_BUS_CONTEXT
*I2cBusContext
;
782 BOOLEAN AllChildrenStopped
;
785 if (NumberOfChildren
== 0) {
788 &gEfiDevicePathProtocolGuid
,
789 This
->DriverBindingHandle
,
795 &gEfiI2cHostProtocolGuid
,
796 This
->DriverBindingHandle
,
802 &gEfiI2cEnumerateProtocolGuid
,
803 This
->DriverBindingHandle
,
807 Status
= gBS
->OpenProtocol (
810 (VOID
**) &I2cBusContext
,
811 This
->DriverBindingHandle
,
813 EFI_OPEN_PROTOCOL_GET_PROTOCOL
815 if (!EFI_ERROR (Status
)) {
816 gBS
->UninstallMultipleProtocolInterfaces (
823 // No more child now, free bus context data.
825 FreePool (I2cBusContext
);
830 AllChildrenStopped
= TRUE
;
832 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
834 Status
= UnRegisterI2cDevice (This
, Controller
, ChildHandleBuffer
[Index
]);
835 if (EFI_ERROR (Status
)) {
836 AllChildrenStopped
= FALSE
;
840 if (!AllChildrenStopped
) {
841 return EFI_DEVICE_ERROR
;
847 Enumerate the I2C bus
849 This routine walks the platform specific data describing the
850 I2C bus to create the I2C devices where driver GUIDs were
853 @param[in] I2cBusContext Address of an I2C_BUS_CONTEXT structure
854 @param[in] Controller Handle to the controller
855 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path.
857 @retval EFI_SUCCESS The bus is successfully configured
862 IN I2C_BUS_CONTEXT
*I2cBusContext
,
863 IN EFI_HANDLE Controller
,
864 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
867 I2C_DEVICE_CONTEXT
*I2cDeviceContext
;
869 CONST EFI_I2C_DEVICE
*Device
;
870 CONST EFI_I2C_DEVICE
*TempDevice
;
871 UINT32 RemainingPathDeviceIndex
;
872 EFI_DEVICE_PATH_PROTOCOL
*DevPathNode
;
873 BOOLEAN BuildControllerNode
;
876 Status
= EFI_SUCCESS
;
877 BuildControllerNode
= TRUE
;
880 // Default DeviceIndex
882 RemainingPathDeviceIndex
= 0;
885 // Determine the controller number in Controller Node Device Path when RemainingDevicePath is not NULL.
887 if (RemainingDevicePath
!= NULL
) {
889 // Check if there is a controller node appended after vendor node
891 DevPathNode
= NextDevicePathNode (RemainingDevicePath
);
892 if ((DevicePathType (DevPathNode
) == HARDWARE_DEVICE_PATH
) &&
893 (DevicePathSubType(DevPathNode
) == HW_CONTROLLER_DP
)) {
895 // RemainingDevicePath != NULL and RemainingDevicePath contains Controller Node,
896 // add Controller Node to Device Path on child handle.
898 RemainingPathDeviceIndex
= ((CONTROLLER_DEVICE_PATH
*) DevPathNode
)->ControllerNumber
;
901 // RemainingDevicePath != NULL and RemainingDevicePath does not contain Controller Node,
902 // do not add controller node to Device Path on child handle.
904 BuildControllerNode
= FALSE
;
909 // Walk the list of I2C devices on this bus
914 // Get the next I2C device
916 Status
= I2cBusContext
->I2cEnumerate
->Enumerate (I2cBusContext
->I2cEnumerate
, &Device
);
917 if (EFI_ERROR (Status
) || Device
== NULL
) {
918 if (RemainingDevicePath
!= NULL
) {
919 Status
= EFI_NOT_FOUND
;
921 Status
= EFI_SUCCESS
;
927 // Determine if the device info is valid
929 if ((Device
->DeviceGuid
== NULL
) || (Device
->SlaveAddressCount
== 0) || (Device
->SlaveAddressArray
== NULL
)) {
930 DEBUG ((EFI_D_ERROR
, "Invalid EFI_I2C_DEVICE reported by I2c Enumerate protocol.\n"));
934 if (RemainingDevicePath
== NULL
) {
935 if (Device
->DeviceIndex
== 0) {
937 // Determine if the controller node is necessary when controller number is zero in I2C device
943 // Get the next I2C device
945 Status
= I2cBusContext
->I2cEnumerate
->Enumerate (I2cBusContext
->I2cEnumerate
, &TempDevice
);
946 if (EFI_ERROR (Status
) || TempDevice
== NULL
) {
947 Status
= EFI_SUCCESS
;
950 if (CompareGuid (Device
->DeviceGuid
, TempDevice
->DeviceGuid
)) {
956 // RemainingDevicePath == NULL and only DeviceIndex 0 is present on the I2C bus,
957 // do not add Controller Node to Device Path on child handle.
959 BuildControllerNode
= FALSE
;
964 // Find I2C device reported in Remaining Device Path
966 if ((!CompareGuid (&((VENDOR_DEVICE_PATH
*)RemainingDevicePath
)->Guid
, Device
->DeviceGuid
)) ||
967 (RemainingPathDeviceIndex
!= Device
->DeviceIndex
)) {
973 // Build the device context for current I2C device.
975 I2cDeviceContext
= NULL
;
976 I2cDeviceContext
= AllocateCopyPool (sizeof (I2C_DEVICE_CONTEXT
), &gI2cDeviceContextTemplate
);
977 ASSERT (I2cDeviceContext
!= NULL
);
978 if (I2cDeviceContext
== NULL
) {
983 // Initialize the specific device context
985 I2cDeviceContext
->I2cBusContext
= I2cBusContext
;
986 I2cDeviceContext
->I2cDevice
= Device
;
987 I2cDeviceContext
->I2cIo
.DeviceGuid
= Device
->DeviceGuid
;
988 I2cDeviceContext
->I2cIo
.DeviceIndex
= Device
->DeviceIndex
;
989 I2cDeviceContext
->I2cIo
.HardwareRevision
= Device
->HardwareRevision
;
990 I2cDeviceContext
->I2cIo
.I2cControllerCapabilities
= I2cBusContext
->I2cHost
->I2cControllerCapabilities
;
993 // Build the device path
995 Status
= I2cBusDevicePathAppend (I2cDeviceContext
, BuildControllerNode
);
996 ASSERT_EFI_ERROR (Status
);
997 if (EFI_ERROR (Status
)) {
1002 // Install the protocol
1004 Status
= gBS
->InstallMultipleProtocolInterfaces (
1005 &I2cDeviceContext
->Handle
,
1006 &gEfiI2cIoProtocolGuid
,
1007 &I2cDeviceContext
->I2cIo
,
1008 &gEfiDevicePathProtocolGuid
,
1009 I2cDeviceContext
->DevicePath
,
1011 if (EFI_ERROR (Status
)) {
1013 // Free resources for this I2C device
1015 ReleaseI2cDeviceContext (I2cDeviceContext
);
1020 // Create the child handle
1022 Status
= gBS
->OpenProtocol (
1024 &gEfiI2cHostProtocolGuid
,
1025 (VOID
**) &I2cBusContext
->I2cHost
,
1026 I2cBusContext
->DriverBindingHandle
,
1027 I2cDeviceContext
->Handle
,
1028 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
1030 if (EFI_ERROR (Status
)) {
1031 Status
= gBS
->UninstallMultipleProtocolInterfaces (
1032 I2cDeviceContext
->Handle
,
1033 &gEfiDevicePathProtocolGuid
,
1034 I2cDeviceContext
->DevicePath
,
1035 &gEfiI2cIoProtocolGuid
,
1036 &I2cDeviceContext
->I2cIo
,
1040 // Free resources for this I2C device
1042 ReleaseI2cDeviceContext (I2cDeviceContext
);
1046 if (RemainingDevicePath
!= NULL
) {
1048 // Child has been created successfully
1059 Queue an I2C transaction for execution on the I2C device.
1061 This routine must be called at or below TPL_NOTIFY. For synchronous
1062 requests this routine must be called at or below TPL_CALLBACK.
1064 This routine queues an I2C transaction to the I2C controller for
1065 execution on the I2C bus.
1067 When Event is NULL, QueueRequest() operates synchronously and returns
1068 the I2C completion status as its return value.
1070 When Event is not NULL, QueueRequest() synchronously returns EFI_SUCCESS
1071 indicating that the asynchronous I2C transaction was queued. The values
1072 above are returned in the buffer pointed to by I2cStatus upon the
1073 completion of the I2C transaction when I2cStatus is not NULL.
1075 The upper layer driver writer provides the following to the platform
1078 1. Vendor specific GUID for the I2C part
1079 2. Guidance on proper construction of the slave address array when the
1080 I2C device uses more than one slave address. The I2C bus protocol
1081 uses the SlaveAddressIndex to perform relative to physical address
1082 translation to access the blocks of hardware within the I2C device.
1084 @param[in] This Pointer to an EFI_I2C_IO_PROTOCOL structure.
1085 @param[in] SlaveAddressIndex Index value into an array of slave addresses
1086 for the I2C device. The values in the array
1087 are specified by the board designer, with the
1088 third party I2C device driver writer providing
1089 the slave address order.
1091 For devices that have a single slave address,
1092 this value must be zero. If the I2C device
1093 uses more than one slave address then the
1094 third party (upper level) I2C driver writer
1095 needs to specify the order of entries in the
1096 slave address array.
1098 \ref ThirdPartyI2cDrivers "Third Party I2C
1099 Drivers" section in I2cMaster.h.
1100 @param[in] Event Event to signal for asynchronous transactions,
1101 NULL for synchronous transactions
1102 @param[in] RequestPacket Pointer to an EFI_I2C_REQUEST_PACKET structure
1103 describing the I2C transaction
1104 @param[out] I2cStatus Optional buffer to receive the I2C transaction
1107 @retval EFI_SUCCESS The asynchronous transaction was successfully
1108 queued when Event is not NULL.
1109 @retval EFI_SUCCESS The transaction completed successfully when
1111 @retval EFI_BAD_BUFFER_SIZE The RequestPacket->LengthInBytes value is too
1113 @retval EFI_DEVICE_ERROR There was an I2C error (NACK) during the
1115 @retval EFI_INVALID_PARAMETER RequestPacket is NULL
1116 @retval EFI_NO_MAPPING The EFI_I2C_HOST_PROTOCOL could not set the
1117 bus configuration required to access this I2C
1119 @retval EFI_NO_RESPONSE The I2C device is not responding to the slave
1120 address selected by SlaveAddressIndex.
1121 EFI_DEVICE_ERROR will be returned if the
1122 controller cannot distinguish when the NACK
1124 @retval EFI_OUT_OF_RESOURCES Insufficient memory for I2C transaction
1125 @retval EFI_UNSUPPORTED The controller does not support the requested
1131 I2cBusQueueRequest (
1132 IN CONST EFI_I2C_IO_PROTOCOL
*This
,
1133 IN UINTN SlaveAddressIndex
,
1134 IN EFI_EVENT Event OPTIONAL
,
1135 IN EFI_I2C_REQUEST_PACKET
*RequestPacket
,
1136 OUT EFI_STATUS
*I2cStatus OPTIONAL
1139 CONST EFI_I2C_DEVICE
*I2cDevice
;
1140 I2C_BUS_CONTEXT
*I2cBusContext
;
1141 CONST EFI_I2C_HOST_PROTOCOL
*I2cHost
;
1142 I2C_DEVICE_CONTEXT
*I2cDeviceContext
;
1145 if (RequestPacket
== NULL
) {
1146 return EFI_INVALID_PARAMETER
;
1150 // Validate the I2C slave index
1152 I2cDeviceContext
= I2C_DEVICE_CONTEXT_FROM_PROTOCOL (This
);
1153 I2cDevice
= I2cDeviceContext
->I2cDevice
;
1154 if ( SlaveAddressIndex
>= I2cDevice
->SlaveAddressCount
) {
1155 return EFI_INVALID_PARAMETER
;
1159 // Locate the I2c Host Protocol to queue request
1161 I2cBusContext
= I2cDeviceContext
->I2cBusContext
;
1162 I2cHost
= I2cBusContext
->I2cHost
;
1165 // Start the I2C operation
1167 Status
= I2cHost
->QueueRequest (
1169 I2cDevice
->I2cBusConfiguration
,
1170 I2cDevice
->SlaveAddressArray
[SlaveAddressIndex
],
1180 Release all the resources allocated for the I2C device.
1182 This function releases all the resources allocated for the I2C device.
1184 @param I2cDeviceContext The I2C child device involved for the operation.
1188 ReleaseI2cDeviceContext (
1189 IN I2C_DEVICE_CONTEXT
*I2cDeviceContext
1192 if (I2cDeviceContext
== NULL
) {
1196 if (I2cDeviceContext
->DevicePath
!= NULL
) {
1197 FreePool (I2cDeviceContext
->DevicePath
);
1200 FreePool (I2cDeviceContext
);
1204 Unregister an I2C device.
1206 This function removes the protocols installed on the controller handle and
1207 frees the resources allocated for the I2C device.
1209 @param This The pointer to EFI_DRIVER_BINDING_PROTOCOL instance.
1210 @param Controller The controller handle of the I2C device.
1211 @param Handle The child handle.
1213 @retval EFI_SUCCESS The I2C device is successfully unregistered.
1214 @return Others Some error occurs when unregistering the I2C device.
1218 UnRegisterI2cDevice (
1219 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1220 IN EFI_HANDLE Controller
,
1221 IN EFI_HANDLE Handle
1225 I2C_DEVICE_CONTEXT
*I2cDeviceContext
;
1226 EFI_I2C_IO_PROTOCOL
*I2cIo
;
1227 EFI_I2C_HOST_PROTOCOL
*I2cHost
;
1231 Status
= gBS
->OpenProtocol (
1233 &gEfiI2cIoProtocolGuid
,
1235 This
->DriverBindingHandle
,
1237 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1239 if (EFI_ERROR (Status
)) {
1244 // Get I2c device context data.
1246 I2cDeviceContext
= I2C_DEVICE_CONTEXT_FROM_PROTOCOL (I2cIo
);
1249 // Close the child handle
1251 gBS
->CloseProtocol (
1253 &gEfiI2cHostProtocolGuid
,
1254 This
->DriverBindingHandle
,
1259 // The I2C Bus driver installs the I2C Io and Device Path Protocol in the DriverBindingStart().
1260 // Here should uninstall them.
1262 Status
= gBS
->UninstallMultipleProtocolInterfaces (
1264 &gEfiDevicePathProtocolGuid
,
1265 I2cDeviceContext
->DevicePath
,
1266 &gEfiI2cIoProtocolGuid
,
1267 &I2cDeviceContext
->I2cIo
,
1271 if (EFI_ERROR (Status
)) {
1273 // Keep parent and child relationship
1277 &gEfiI2cHostProtocolGuid
,
1279 This
->DriverBindingHandle
,
1281 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
1287 // Free resources for this I2C device
1289 ReleaseI2cDeviceContext (I2cDeviceContext
);
1295 Create a path for the I2C device
1297 Append the I2C slave path to the I2C master controller path.
1299 @param[in] I2cDeviceContext Address of an I2C_DEVICE_CONTEXT structure.
1300 @param[in] BuildControllerNode Flag to build controller node in device path.
1302 @retval EFI_SUCCESS The I2C device path is built successfully.
1303 @return Others It is failed to built device path.
1307 I2cBusDevicePathAppend (
1308 IN I2C_DEVICE_CONTEXT
*I2cDeviceContext
,
1309 IN BOOLEAN BuildControllerNode
1312 EFI_DEVICE_PATH_PROTOCOL
*PreviousDevicePath
;
1314 PreviousDevicePath
= NULL
;
1317 // Build vendor device path
1319 CopyMem (&gVendorDevicePathTemplate
.Guid
, I2cDeviceContext
->I2cDevice
->DeviceGuid
, sizeof (EFI_GUID
));
1320 I2cDeviceContext
->DevicePath
= AppendDevicePathNode (
1321 I2cDeviceContext
->I2cBusContext
->ParentDevicePath
,
1322 (EFI_DEVICE_PATH_PROTOCOL
*) &gVendorDevicePathTemplate
1324 ASSERT (I2cDeviceContext
->DevicePath
!= NULL
);
1325 if (I2cDeviceContext
->DevicePath
== NULL
) {
1326 return EFI_OUT_OF_RESOURCES
;
1329 if ((BuildControllerNode
) && (I2cDeviceContext
->DevicePath
!= NULL
)) {
1331 // Build the final I2C device path with controller node
1333 PreviousDevicePath
= I2cDeviceContext
->DevicePath
;
1334 gControllerDevicePathTemplate
.ControllerNumber
= I2cDeviceContext
->I2cDevice
->DeviceIndex
;
1335 I2cDeviceContext
->DevicePath
= AppendDevicePathNode (
1336 I2cDeviceContext
->DevicePath
,
1337 (EFI_DEVICE_PATH_PROTOCOL
*) &gControllerDevicePathTemplate
1339 gBS
->FreePool (PreviousDevicePath
);
1340 ASSERT (I2cDeviceContext
->DevicePath
!= NULL
);
1341 if (I2cDeviceContext
->DevicePath
== NULL
) {
1342 return EFI_OUT_OF_RESOURCES
;
1350 The user entry point for the I2C bus module. The user code starts with
1353 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1354 @param[in] SystemTable A pointer to the EFI System Table.
1356 @retval EFI_SUCCESS The entry point is executed successfully.
1357 @retval other Some error occurs when executing this entry point.
1363 IN EFI_HANDLE ImageHandle
,
1364 IN EFI_SYSTEM_TABLE
*SystemTable
1370 // Install driver model protocol(s).
1372 Status
= EfiLibInstallDriverBindingComponentName2 (
1375 &gI2cBusDriverBinding
,
1377 &gI2cBusComponentName
,
1378 &gI2cBusComponentName2
1380 ASSERT_EFI_ERROR (Status
);
1387 This is the unload handle for I2C bus module.
1389 Disconnect the driver specified by ImageHandle from all the devices in the handle database.
1390 Uninstall all the protocols installed in the driver entry point.
1392 @param[in] ImageHandle The drivers' driver image.
1394 @retval EFI_SUCCESS The image is unloaded.
1395 @retval Others Failed to unload the image.
1401 IN EFI_HANDLE ImageHandle
1405 EFI_HANDLE
*DeviceHandleBuffer
;
1406 UINTN DeviceHandleCount
;
1408 EFI_COMPONENT_NAME_PROTOCOL
*ComponentName
;
1409 EFI_COMPONENT_NAME2_PROTOCOL
*ComponentName2
;
1412 // Get the list of all I2C Controller handles in the handle database.
1413 // If there is an error getting the list, then the unload
1416 Status
= gBS
->LocateHandleBuffer (
1418 &gEfiI2cHostProtocolGuid
,
1424 if (!EFI_ERROR (Status
)) {
1426 // Disconnect the driver specified by Driver BindingHandle from all
1427 // the devices in the handle database.
1429 for (Index
= 0; Index
< DeviceHandleCount
; Index
++) {
1430 Status
= gBS
->DisconnectController (
1431 DeviceHandleBuffer
[Index
],
1432 gI2cBusDriverBinding
.DriverBindingHandle
,
1435 if (EFI_ERROR (Status
)) {
1442 // Uninstall all the protocols installed in the driver entry point
1444 Status
= gBS
->UninstallMultipleProtocolInterfaces (
1445 gI2cBusDriverBinding
.DriverBindingHandle
,
1446 &gEfiDriverBindingProtocolGuid
,
1447 &gI2cBusDriverBinding
,
1450 ASSERT_EFI_ERROR (Status
);
1453 // Note we have to one by one uninstall the following protocols.
1454 // It's because some of them are optionally installed based on
1455 // the following PCD settings.
1456 // gEfiMdePkgTokenSpaceGuid.PcdDriverDiagnosticsDisable
1457 // gEfiMdePkgTokenSpaceGuid.PcdComponentNameDisable
1458 // gEfiMdePkgTokenSpaceGuid.PcdDriverDiagnostics2Disable
1459 // gEfiMdePkgTokenSpaceGuid.PcdComponentName2Disable
1461 Status
= gBS
->HandleProtocol (
1462 gI2cBusDriverBinding
.DriverBindingHandle
,
1463 &gEfiComponentNameProtocolGuid
,
1464 (VOID
**) &ComponentName
1466 if (!EFI_ERROR (Status
)) {
1467 gBS
->UninstallProtocolInterface (
1468 gI2cBusDriverBinding
.DriverBindingHandle
,
1469 &gEfiComponentNameProtocolGuid
,
1474 Status
= gBS
->HandleProtocol (
1475 gI2cBusDriverBinding
.DriverBindingHandle
,
1476 &gEfiComponentName2ProtocolGuid
,
1477 (VOID
**) &ComponentName2
1479 if (!EFI_ERROR (Status
)) {
1480 gBS
->UninstallProtocolInterface (
1481 gI2cBusDriverBinding
.DriverBindingHandle
,
1482 &gEfiComponentName2ProtocolGuid
,
1487 Status
= EFI_SUCCESS
;
1491 // Free the buffer containing the list of handles from the handle database
1493 if (DeviceHandleBuffer
!= NULL
) {
1494 gBS
->FreePool (DeviceHandleBuffer
);