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 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
13 // EFI_DRIVER_BINDING_PROTOCOL instance
15 EFI_DRIVER_BINDING_PROTOCOL gI2cBusDriverBinding
= {
16 I2cBusDriverSupported
,
25 // Template for I2C Bus Child Device.
27 I2C_DEVICE_CONTEXT gI2cDeviceContextTemplate
= {
31 I2cBusQueueRequest
, // QueueRequest
34 0, // HardwareRevision
35 NULL
// I2cControllerCapabilities
39 NULL
, // I2cBusContext
43 // Template for controller device path node.
45 CONTROLLER_DEVICE_PATH gControllerDevicePathTemplate
= {
50 (UINT8
) (sizeof (CONTROLLER_DEVICE_PATH
)),
51 (UINT8
) ((sizeof (CONTROLLER_DEVICE_PATH
)) >> 8)
58 // Template for vendor device path node.
60 VENDOR_DEVICE_PATH gVendorDevicePathTemplate
= {
65 (UINT8
) (sizeof (VENDOR_DEVICE_PATH
)),
66 (UINT8
) ((sizeof (VENDOR_DEVICE_PATH
)) >> 8)
69 { 0x0, 0x0, 0x0, { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }}
75 GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE mI2cBusDriverNameTable
[] = {
76 { "eng;en", (CHAR16
*) L
"I2C Bus Driver" },
81 // EFI Component Name Protocol
83 GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL gI2cBusComponentName
= {
84 (EFI_COMPONENT_NAME_GET_DRIVER_NAME
) I2cBusComponentNameGetDriverName
,
85 (EFI_COMPONENT_NAME_GET_CONTROLLER_NAME
) I2cBusComponentNameGetControllerName
,
90 // EFI Component Name 2 Protocol
92 GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL gI2cBusComponentName2
= {
93 I2cBusComponentNameGetDriverName
,
94 I2cBusComponentNameGetControllerName
,
99 Retrieves a Unicode string that is the user readable name of the driver.
101 This function retrieves the user readable name of a driver in the form of a
102 Unicode string. If the driver specified by This has a user readable name in
103 the language specified by Language, then a pointer to the driver name is
104 returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
105 by This does not support the language specified by Language,
106 then EFI_UNSUPPORTED is returned.
108 @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
109 EFI_COMPONENT_NAME_PROTOCOL instance.
111 @param Language[in] A pointer to a Null-terminated ASCII string
112 array indicating the language. This is the
113 language of the driver name that the caller is
114 requesting, and it must match one of the
115 languages specified in SupportedLanguages. The
116 number of languages supported by a driver is up
117 to the driver writer. Language is specified
118 in RFC 4646 or ISO 639-2 language code format.
120 @param DriverName[out] A pointer to the Unicode string to return.
121 This Unicode string is the name of the
122 driver specified by This in the language
123 specified by Language.
125 @retval EFI_SUCCESS The Unicode string for the Driver specified by
126 This and the language specified by Language was
127 returned in DriverName.
129 @retval EFI_INVALID_PARAMETER Language is NULL.
131 @retval EFI_INVALID_PARAMETER DriverName is NULL.
133 @retval EFI_UNSUPPORTED The driver specified by This does not support
134 the language specified by Language.
139 I2cBusComponentNameGetDriverName (
140 IN EFI_COMPONENT_NAME2_PROTOCOL
*This
,
142 OUT CHAR16
**DriverName
145 return LookupUnicodeString2 (
147 This
->SupportedLanguages
,
148 mI2cBusDriverNameTable
,
150 (BOOLEAN
)(This
!= &gI2cBusComponentName2
)
155 Retrieves a Unicode string that is the user readable name of the controller
156 that is being managed by a driver.
158 This function retrieves the user readable name of the controller specified by
159 ControllerHandle and ChildHandle in the form of a Unicode string. If the
160 driver specified by This has a user readable name in the language specified by
161 Language, then a pointer to the controller name is returned in ControllerName,
162 and EFI_SUCCESS is returned. If the driver specified by This is not currently
163 managing the controller specified by ControllerHandle and ChildHandle,
164 then EFI_UNSUPPORTED is returned. If the driver specified by This does not
165 support the language specified by Language, then EFI_UNSUPPORTED is returned.
167 @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
168 EFI_COMPONENT_NAME_PROTOCOL instance.
170 @param ControllerHandle[in] The handle of a controller that the driver
171 specified by This is managing. This handle
172 specifies the controller whose name is to be
175 @param ChildHandle[in] The handle of the child controller to retrieve
176 the name of. This is an optional parameter that
177 may be NULL. It will be NULL for device
178 drivers. It will also be NULL for a bus drivers
179 that wish to retrieve the name of the bus
180 controller. It will not be NULL for a bus
181 driver that wishes to retrieve the name of a
184 @param Language[in] A pointer to a Null-terminated ASCII string
185 array indicating the language. This is the
186 language of the driver name that the caller is
187 requesting, and it must match one of the
188 languages specified in SupportedLanguages. The
189 number of languages supported by a driver is up
190 to the driver writer. Language is specified in
191 RFC 4646 or ISO 639-2 language code format.
193 @param ControllerName[out] A pointer to the Unicode string to return.
194 This Unicode string is the name of the
195 controller specified by ControllerHandle and
196 ChildHandle in the language specified by
197 Language from the point of view of the driver
200 @retval EFI_SUCCESS The Unicode string for the user readable name in
201 the language specified by Language for the
202 driver specified by This was returned in
205 @retval EFI_INVALID_PARAMETER ControllerHandle is NULL.
207 @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid
210 @retval EFI_INVALID_PARAMETER Language is NULL.
212 @retval EFI_INVALID_PARAMETER ControllerName is NULL.
214 @retval EFI_UNSUPPORTED The driver specified by This is not currently
215 managing the controller specified by
216 ControllerHandle and ChildHandle.
218 @retval EFI_UNSUPPORTED The driver specified by This does not support
219 the language specified by Language.
224 I2cBusComponentNameGetControllerName (
225 IN EFI_COMPONENT_NAME2_PROTOCOL
*This
,
226 IN EFI_HANDLE ControllerHandle
,
227 IN EFI_HANDLE ChildHandle OPTIONAL
,
229 OUT CHAR16
**ControllerName
232 return EFI_UNSUPPORTED
;
236 Check if the child of I2C controller has been created.
238 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
239 @param[in] Controller I2C controller handle.
240 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path.
241 @param[in] RemainingHasControllerNode Indicate if RemainingDevicePath contains CONTROLLER_DEVICE_PATH.
242 @param[in] RemainingControllerNumber Controller number in CONTROLLER_DEVICE_PATH.
244 @retval EFI_SUCCESS The child of I2C controller is not created.
245 @retval Others The child of I2C controller has been created or other errors happen.
249 CheckRemainingDevicePath (
250 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
251 IN EFI_HANDLE Controller
,
252 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
,
253 IN BOOLEAN RemainingHasControllerNode
,
254 IN UINT32 RemainingControllerNumber
258 EFI_DEVICE_PATH_PROTOCOL
*SystemDevicePath
;
259 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenInfoBuffer
;
262 BOOLEAN SystemHasControllerNode
;
263 UINT32 SystemControllerNumber
;
265 SystemHasControllerNode
= FALSE
;
266 SystemControllerNumber
= 0;
268 Status
= gBS
->OpenProtocolInformation (
270 &gEfiI2cHostProtocolGuid
,
274 if (EFI_ERROR (Status
)) {
278 for (Index
= 0; Index
< EntryCount
; Index
++) {
279 if ((OpenInfoBuffer
[Index
].Attributes
& EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
) != 0) {
280 Status
= gBS
->OpenProtocol (
281 OpenInfoBuffer
[Index
].ControllerHandle
,
282 &gEfiDevicePathProtocolGuid
,
283 (VOID
**) &SystemDevicePath
,
284 This
->DriverBindingHandle
,
286 EFI_OPEN_PROTOCOL_GET_PROTOCOL
288 if (!EFI_ERROR (Status
)) {
290 // Find vendor device path node and compare
292 while (!IsDevicePathEnd (SystemDevicePath
)) {
293 if ((DevicePathType (SystemDevicePath
) == HARDWARE_DEVICE_PATH
) &&
294 (DevicePathSubType (SystemDevicePath
) == HW_VENDOR_DP
)) {
296 // Check if vendor device path is same between system device path and remaining device path
298 if (CompareMem (SystemDevicePath
, RemainingDevicePath
, sizeof (VENDOR_DEVICE_PATH
)) == 0) {
300 // Get controller node appended after vendor node
302 SystemDevicePath
= NextDevicePathNode (SystemDevicePath
);
303 if ((DevicePathType (SystemDevicePath
) == HARDWARE_DEVICE_PATH
) &&
304 (DevicePathSubType (SystemDevicePath
) == HW_CONTROLLER_DP
)) {
305 SystemHasControllerNode
= TRUE
;
306 SystemControllerNumber
= ((CONTROLLER_DEVICE_PATH
*) SystemDevicePath
)->ControllerNumber
;
308 SystemHasControllerNode
= FALSE
;
309 SystemControllerNumber
= 0;
311 if (((SystemHasControllerNode
) && (!RemainingHasControllerNode
) && (SystemControllerNumber
== 0)) ||
312 ((!SystemHasControllerNode
) && (RemainingHasControllerNode
) && (RemainingControllerNumber
== 0)) ||
313 ((SystemHasControllerNode
) && (RemainingHasControllerNode
) && (SystemControllerNumber
== RemainingControllerNumber
)) ||
314 ((!SystemHasControllerNode
) && (!RemainingHasControllerNode
))) {
315 DEBUG ((EFI_D_ERROR
, "This I2C device has been already started.\n"));
316 Status
= EFI_UNSUPPORTED
;
321 SystemDevicePath
= NextDevicePathNode (SystemDevicePath
);
323 if (EFI_ERROR (Status
)) {
329 FreePool (OpenInfoBuffer
);
334 Tests to see if this driver supports a given controller. If a child device is provided,
335 it further tests to see if this driver supports creating a handle for the specified child device.
337 This function checks to see if the driver specified by This supports the device specified by
338 ControllerHandle. Drivers will typically use the device path attached to
339 ControllerHandle and/or the services from the bus I/O abstraction attached to
340 ControllerHandle to determine if the driver supports ControllerHandle. This function
341 may be called many times during platform initialization. In order to reduce boot times, the tests
342 performed by this function must be very small, and take as little time as possible to execute. This
343 function must not change the state of any hardware devices, and this function must be aware that the
344 device specified by ControllerHandle may already be managed by the same driver or a
345 different driver. This function must match its calls to AllocatePages() with FreePages(),
346 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
347 Since ControllerHandle may have been previously started by the same driver, if a protocol is
348 already in the opened state, then it must not be closed with CloseProtocol(). This is required
349 to guarantee the state of ControllerHandle is not modified by this function.
351 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
352 @param[in] ControllerHandle The handle of the controller to test. This handle
353 must support a protocol interface that supplies
354 an I/O abstraction to the driver.
355 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
356 parameter is ignored by device drivers, and is optional for bus
357 drivers. For bus drivers, if this parameter is not NULL, then
358 the bus driver must determine if the bus controller specified
359 by ControllerHandle and the child controller specified
360 by RemainingDevicePath are both supported by this
363 @retval EFI_SUCCESS The device specified by ControllerHandle and
364 RemainingDevicePath is supported by the driver specified by This.
365 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
366 RemainingDevicePath is already being managed by the driver
368 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
369 RemainingDevicePath is already being managed by a different
370 driver or an application that requires exclusive access.
371 Currently not implemented.
372 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
373 RemainingDevicePath is not supported by the driver specified by This.
377 I2cBusDriverSupported (
378 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
379 IN EFI_HANDLE Controller
,
380 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
384 EFI_I2C_ENUMERATE_PROTOCOL
*I2cEnumerate
;
385 EFI_I2C_HOST_PROTOCOL
*I2cHost
;
386 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
387 EFI_DEVICE_PATH_PROTOCOL
*DevPathNode
;
388 BOOLEAN RemainingHasControllerNode
;
389 UINT32 RemainingControllerNumber
;
391 RemainingHasControllerNode
= FALSE
;
392 RemainingControllerNumber
= 0;
395 // Determine if the I2c Enumerate Protocol is available
397 Status
= gBS
->OpenProtocol (
399 &gEfiI2cEnumerateProtocolGuid
,
400 (VOID
**) &I2cEnumerate
,
401 This
->DriverBindingHandle
,
403 EFI_OPEN_PROTOCOL_BY_DRIVER
405 if ((EFI_ERROR (Status
)) && (Status
!= EFI_ALREADY_STARTED
)) {
409 if (!EFI_ERROR (Status
)) {
412 &gEfiI2cEnumerateProtocolGuid
,
413 This
->DriverBindingHandle
,
418 Status
= gBS
->OpenProtocol (
420 &gEfiDevicePathProtocolGuid
,
421 (VOID
**) &ParentDevicePath
,
422 This
->DriverBindingHandle
,
424 EFI_OPEN_PROTOCOL_BY_DRIVER
427 if ((EFI_ERROR (Status
)) && (Status
!= EFI_ALREADY_STARTED
)) {
431 if (!EFI_ERROR (Status
)) {
434 &gEfiDevicePathProtocolGuid
,
435 This
->DriverBindingHandle
,
440 if ((RemainingDevicePath
!= NULL
) && !IsDevicePathEnd (RemainingDevicePath
)) {
442 // Check if the first node of RemainingDevicePath is a hardware vendor device path
444 if ((DevicePathType (RemainingDevicePath
) != HARDWARE_DEVICE_PATH
) ||
445 (DevicePathSubType (RemainingDevicePath
) != HW_VENDOR_DP
)) {
446 return EFI_UNSUPPORTED
;
449 // Check if the second node of RemainingDevicePath is a controller node
451 DevPathNode
= NextDevicePathNode (RemainingDevicePath
);
452 if (!IsDevicePathEnd (DevPathNode
)) {
453 if ((DevicePathType (DevPathNode
) != HARDWARE_DEVICE_PATH
) ||
454 (DevicePathSubType (DevPathNode
) != HW_CONTROLLER_DP
)) {
455 return EFI_UNSUPPORTED
;
457 RemainingHasControllerNode
= TRUE
;
458 RemainingControllerNumber
= ((CONTROLLER_DEVICE_PATH
*) DevPathNode
)->ControllerNumber
;
464 // Determine if the I2C Host Protocol is available
466 Status
= gBS
->OpenProtocol (
468 &gEfiI2cHostProtocolGuid
,
470 This
->DriverBindingHandle
,
472 EFI_OPEN_PROTOCOL_BY_DRIVER
475 if (!EFI_ERROR (Status
)) {
478 &gEfiI2cHostProtocolGuid
,
479 This
->DriverBindingHandle
,
485 if (Status
== EFI_ALREADY_STARTED
) {
486 if ((RemainingDevicePath
== NULL
) ||
487 ((RemainingDevicePath
!= NULL
) && IsDevicePathEnd (RemainingDevicePath
))) {
489 // If RemainingDevicePath is NULL or is the End of Device Path Node, return EFI_SUCCESS.
491 Status
= EFI_SUCCESS
;
494 // Test if the child with the RemainingDevicePath has already been created.
496 Status
= CheckRemainingDevicePath (
500 RemainingHasControllerNode
,
501 RemainingControllerNumber
510 Starts a device controller or a bus controller.
512 The Start() function is designed to be invoked from the EFI boot service ConnectController().
513 As a result, much of the error checking on the parameters to Start() has been moved into this
514 common boot service. It is legal to call Start() from other locations,
515 but the following calling restrictions must be followed or the system behavior will not be deterministic.
516 1. ControllerHandle must be a valid EFI_HANDLE.
517 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
518 EFI_DEVICE_PATH_PROTOCOL.
519 3. Prior to calling Start(), the Supported() function for the driver specified by This must
520 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
522 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
523 @param[in] ControllerHandle The handle of the controller to start. This handle
524 must support a protocol interface that supplies
525 an I/O abstraction to the driver.
526 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
527 parameter is ignored by device drivers, and is optional for bus
528 drivers. For a bus driver, if this parameter is NULL, then handles
529 for all the children of Controller are created by this driver.
530 If this parameter is not NULL and the first Device Path Node is
531 not the End of Device Path Node, then only the handle for the
532 child device specified by the first Device Path Node of
533 RemainingDevicePath is created by this driver.
534 If the first Device Path Node of RemainingDevicePath is
535 the End of Device Path Node, no child handle is created by this
538 @retval EFI_SUCCESS The device was started.
539 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
540 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
541 @retval Others The driver failed to start the device.
547 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
548 IN EFI_HANDLE Controller
,
549 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
552 EFI_I2C_ENUMERATE_PROTOCOL
*I2cEnumerate
;
553 EFI_I2C_HOST_PROTOCOL
*I2cHost
;
554 I2C_BUS_CONTEXT
*I2cBusContext
;
556 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
558 I2cBusContext
= NULL
;
559 ParentDevicePath
= NULL
;
564 // Determine if the I2C controller is available
566 Status
= gBS
->OpenProtocol (
568 &gEfiI2cHostProtocolGuid
,
570 This
->DriverBindingHandle
,
572 EFI_OPEN_PROTOCOL_BY_DRIVER
574 if (EFI_ERROR (Status
) && (Status
!= EFI_ALREADY_STARTED
)) {
575 DEBUG ((EFI_D_ERROR
, "I2cBus: open I2C host error, Status = %r\n", Status
));
579 if (Status
== EFI_ALREADY_STARTED
) {
580 Status
= gBS
->OpenProtocol (
583 (VOID
**) &I2cBusContext
,
584 This
->DriverBindingHandle
,
586 EFI_OPEN_PROTOCOL_GET_PROTOCOL
588 if (EFI_ERROR (Status
)) {
589 DEBUG ((EFI_D_ERROR
, "I2cBus: open private protocol error, Status = %r.\n", Status
));
595 // Get the I2C bus enumeration API
597 Status
= gBS
->OpenProtocol (
599 &gEfiI2cEnumerateProtocolGuid
,
600 (VOID
**)&I2cEnumerate
,
601 This
->DriverBindingHandle
,
603 EFI_OPEN_PROTOCOL_BY_DRIVER
605 if (EFI_ERROR (Status
) && (Status
!= EFI_ALREADY_STARTED
)) {
606 DEBUG ((EFI_D_ERROR
, "I2cBus: open I2C enumerate error, Status = %r\n", Status
));
610 Status
= gBS
->OpenProtocol (
612 &gEfiDevicePathProtocolGuid
,
613 (VOID
**) &ParentDevicePath
,
614 This
->DriverBindingHandle
,
616 EFI_OPEN_PROTOCOL_BY_DRIVER
618 if (EFI_ERROR (Status
) && (Status
!= EFI_ALREADY_STARTED
)) {
619 DEBUG ((EFI_D_ERROR
, "I2cBus: open device path error, Status = %r\n", Status
));
623 if ((RemainingDevicePath
!= NULL
) && IsDevicePathEnd (RemainingDevicePath
)) {
625 // If RemainingDevicePath is the End of Device Path Node,
626 // don't create any child device and return EFI_SUCCESS
632 // Allocate the buffer for I2C_BUS_CONTEXT if it is not allocated before.
634 if (I2cBusContext
== NULL
) {
636 // Allocate the I2C context structure for the current I2C controller
638 I2cBusContext
= AllocateZeroPool (sizeof (I2C_BUS_CONTEXT
));
639 if (I2cBusContext
== NULL
) {
640 DEBUG ((EFI_D_ERROR
, "I2cBus: there is no enough memory to allocate.\n"));
641 Status
= EFI_OUT_OF_RESOURCES
;
647 .->| I2C_BUS_CONTEXT|<----- This file Protocol (gEfiCallerIdGuid) installed on I2C Controller handle
650 | +----------------------------+
651 | | I2C_DEVICE_CONTEXT |
654 | I2C IO Protocol Structure | <----- I2C IO Protocol
656 +----------------------------+
659 I2cBusContext
->I2cHost
= I2cHost
;
660 I2cBusContext
->I2cEnumerate
= I2cEnumerate
;
662 // Parent controller used to create children
664 I2cBusContext
->Controller
= Controller
;
666 // Parent controller device path used to create children device path
668 I2cBusContext
->ParentDevicePath
= ParentDevicePath
;
670 I2cBusContext
->DriverBindingHandle
= This
->DriverBindingHandle
;
672 Status
= gBS
->InstallMultipleProtocolInterfaces (
678 if (EFI_ERROR (Status
)) {
679 DEBUG ((EFI_D_ERROR
, "I2cBus: install private protocol error, Status = %r.\n", Status
));
687 Status
= RegisterI2cDevice (I2cBusContext
, Controller
, RemainingDevicePath
);
692 if (EFI_ERROR (Status
)) {
693 DEBUG ((EFI_D_ERROR
, "I2cBus: Start() function failed, Status = %r\n", Status
));
694 if (ParentDevicePath
!= NULL
) {
697 &gEfiDevicePathProtocolGuid
,
698 This
->DriverBindingHandle
,
703 if (I2cHost
!= NULL
) {
706 &gEfiI2cHostProtocolGuid
,
707 This
->DriverBindingHandle
,
712 if (I2cEnumerate
!= NULL
) {
715 &gEfiI2cEnumerateProtocolGuid
,
716 This
->DriverBindingHandle
,
721 if (I2cBusContext
!= NULL
) {
722 Status
= gBS
->UninstallMultipleProtocolInterfaces (
728 FreePool (I2cBusContext
);
733 // Return the operation status.
740 Stops a device controller or a bus controller.
742 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
743 As a result, much of the error checking on the parameters to Stop() has been moved
744 into this common boot service. It is legal to call Stop() from other locations,
745 but the following calling restrictions must be followed or the system behavior will not be deterministic.
746 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
747 same driver's Start() function.
748 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
749 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
750 Start() function, and the Start() function must have called OpenProtocol() on
751 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
753 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
754 @param[in] ControllerHandle A handle to the device being stopped. The handle must
755 support a bus specific I/O protocol for the driver
756 to use to stop the device.
757 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
758 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
759 if NumberOfChildren is 0.
761 @retval EFI_SUCCESS The device was stopped.
762 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
768 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
769 IN EFI_HANDLE Controller
,
770 IN UINTN NumberOfChildren
,
771 IN EFI_HANDLE
*ChildHandleBuffer
774 I2C_BUS_CONTEXT
*I2cBusContext
;
776 BOOLEAN AllChildrenStopped
;
779 if (NumberOfChildren
== 0) {
782 &gEfiDevicePathProtocolGuid
,
783 This
->DriverBindingHandle
,
789 &gEfiI2cHostProtocolGuid
,
790 This
->DriverBindingHandle
,
796 &gEfiI2cEnumerateProtocolGuid
,
797 This
->DriverBindingHandle
,
801 Status
= gBS
->OpenProtocol (
804 (VOID
**) &I2cBusContext
,
805 This
->DriverBindingHandle
,
807 EFI_OPEN_PROTOCOL_GET_PROTOCOL
809 if (!EFI_ERROR (Status
)) {
810 gBS
->UninstallMultipleProtocolInterfaces (
817 // No more child now, free bus context data.
819 FreePool (I2cBusContext
);
824 AllChildrenStopped
= TRUE
;
826 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
828 Status
= UnRegisterI2cDevice (This
, Controller
, ChildHandleBuffer
[Index
]);
829 if (EFI_ERROR (Status
)) {
830 AllChildrenStopped
= FALSE
;
834 if (!AllChildrenStopped
) {
835 return EFI_DEVICE_ERROR
;
841 Enumerate the I2C bus
843 This routine walks the platform specific data describing the
844 I2C bus to create the I2C devices where driver GUIDs were
847 @param[in] I2cBusContext Address of an I2C_BUS_CONTEXT structure
848 @param[in] Controller Handle to the controller
849 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path.
851 @retval EFI_SUCCESS The bus is successfully configured
856 IN I2C_BUS_CONTEXT
*I2cBusContext
,
857 IN EFI_HANDLE Controller
,
858 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
861 I2C_DEVICE_CONTEXT
*I2cDeviceContext
;
863 CONST EFI_I2C_DEVICE
*Device
;
864 CONST EFI_I2C_DEVICE
*TempDevice
;
865 UINT32 RemainingPathDeviceIndex
;
866 EFI_DEVICE_PATH_PROTOCOL
*DevPathNode
;
867 BOOLEAN BuildControllerNode
;
870 Status
= EFI_SUCCESS
;
871 BuildControllerNode
= TRUE
;
874 // Default DeviceIndex
876 RemainingPathDeviceIndex
= 0;
879 // Determine the controller number in Controller Node Device Path when RemainingDevicePath is not NULL.
881 if (RemainingDevicePath
!= NULL
) {
883 // Check if there is a controller node appended after vendor node
885 DevPathNode
= NextDevicePathNode (RemainingDevicePath
);
886 if ((DevicePathType (DevPathNode
) == HARDWARE_DEVICE_PATH
) &&
887 (DevicePathSubType(DevPathNode
) == HW_CONTROLLER_DP
)) {
889 // RemainingDevicePath != NULL and RemainingDevicePath contains Controller Node,
890 // add Controller Node to Device Path on child handle.
892 RemainingPathDeviceIndex
= ((CONTROLLER_DEVICE_PATH
*) DevPathNode
)->ControllerNumber
;
895 // RemainingDevicePath != NULL and RemainingDevicePath does not contain Controller Node,
896 // do not add controller node to Device Path on child handle.
898 BuildControllerNode
= FALSE
;
903 // Walk the list of I2C devices on this bus
908 // Get the next I2C device
910 Status
= I2cBusContext
->I2cEnumerate
->Enumerate (I2cBusContext
->I2cEnumerate
, &Device
);
911 if (EFI_ERROR (Status
) || Device
== NULL
) {
912 if (RemainingDevicePath
!= NULL
) {
913 Status
= EFI_NOT_FOUND
;
915 Status
= EFI_SUCCESS
;
921 // Determine if the device info is valid
923 if ((Device
->DeviceGuid
== NULL
) || (Device
->SlaveAddressCount
== 0) || (Device
->SlaveAddressArray
== NULL
)) {
924 DEBUG ((EFI_D_ERROR
, "Invalid EFI_I2C_DEVICE reported by I2c Enumerate protocol.\n"));
928 if (RemainingDevicePath
== NULL
) {
929 if (Device
->DeviceIndex
== 0) {
931 // Determine if the controller node is necessary when controller number is zero in I2C device
937 // Get the next I2C device
939 Status
= I2cBusContext
->I2cEnumerate
->Enumerate (I2cBusContext
->I2cEnumerate
, &TempDevice
);
940 if (EFI_ERROR (Status
) || TempDevice
== NULL
) {
941 Status
= EFI_SUCCESS
;
944 if (CompareGuid (Device
->DeviceGuid
, TempDevice
->DeviceGuid
)) {
950 // RemainingDevicePath == NULL and only DeviceIndex 0 is present on the I2C bus,
951 // do not add Controller Node to Device Path on child handle.
953 BuildControllerNode
= FALSE
;
958 // Find I2C device reported in Remaining Device Path
960 if ((!CompareGuid (&((VENDOR_DEVICE_PATH
*)RemainingDevicePath
)->Guid
, Device
->DeviceGuid
)) ||
961 (RemainingPathDeviceIndex
!= Device
->DeviceIndex
)) {
967 // Build the device context for current I2C device.
969 I2cDeviceContext
= NULL
;
970 I2cDeviceContext
= AllocateCopyPool (sizeof (I2C_DEVICE_CONTEXT
), &gI2cDeviceContextTemplate
);
971 ASSERT (I2cDeviceContext
!= NULL
);
972 if (I2cDeviceContext
== NULL
) {
977 // Initialize the specific device context
979 I2cDeviceContext
->I2cBusContext
= I2cBusContext
;
980 I2cDeviceContext
->I2cDevice
= Device
;
981 I2cDeviceContext
->I2cIo
.DeviceGuid
= Device
->DeviceGuid
;
982 I2cDeviceContext
->I2cIo
.DeviceIndex
= Device
->DeviceIndex
;
983 I2cDeviceContext
->I2cIo
.HardwareRevision
= Device
->HardwareRevision
;
984 I2cDeviceContext
->I2cIo
.I2cControllerCapabilities
= I2cBusContext
->I2cHost
->I2cControllerCapabilities
;
987 // Build the device path
989 Status
= I2cBusDevicePathAppend (I2cDeviceContext
, BuildControllerNode
);
990 ASSERT_EFI_ERROR (Status
);
991 if (EFI_ERROR (Status
)) {
996 // Install the protocol
998 Status
= gBS
->InstallMultipleProtocolInterfaces (
999 &I2cDeviceContext
->Handle
,
1000 &gEfiI2cIoProtocolGuid
,
1001 &I2cDeviceContext
->I2cIo
,
1002 &gEfiDevicePathProtocolGuid
,
1003 I2cDeviceContext
->DevicePath
,
1005 if (EFI_ERROR (Status
)) {
1007 // Free resources for this I2C device
1009 ReleaseI2cDeviceContext (I2cDeviceContext
);
1014 // Create the child handle
1016 Status
= gBS
->OpenProtocol (
1018 &gEfiI2cHostProtocolGuid
,
1019 (VOID
**) &I2cBusContext
->I2cHost
,
1020 I2cBusContext
->DriverBindingHandle
,
1021 I2cDeviceContext
->Handle
,
1022 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
1024 if (EFI_ERROR (Status
)) {
1025 Status
= gBS
->UninstallMultipleProtocolInterfaces (
1026 I2cDeviceContext
->Handle
,
1027 &gEfiDevicePathProtocolGuid
,
1028 I2cDeviceContext
->DevicePath
,
1029 &gEfiI2cIoProtocolGuid
,
1030 &I2cDeviceContext
->I2cIo
,
1034 // Free resources for this I2C device
1036 ReleaseI2cDeviceContext (I2cDeviceContext
);
1040 if (RemainingDevicePath
!= NULL
) {
1042 // Child has been created successfully
1053 Queue an I2C transaction for execution on the I2C device.
1055 This routine must be called at or below TPL_NOTIFY. For synchronous
1056 requests this routine must be called at or below TPL_CALLBACK.
1058 This routine queues an I2C transaction to the I2C controller for
1059 execution on the I2C bus.
1061 When Event is NULL, QueueRequest() operates synchronously and returns
1062 the I2C completion status as its return value.
1064 When Event is not NULL, QueueRequest() synchronously returns EFI_SUCCESS
1065 indicating that the asynchronous I2C transaction was queued. The values
1066 above are returned in the buffer pointed to by I2cStatus upon the
1067 completion of the I2C transaction when I2cStatus is not NULL.
1069 The upper layer driver writer provides the following to the platform
1072 1. Vendor specific GUID for the I2C part
1073 2. Guidance on proper construction of the slave address array when the
1074 I2C device uses more than one slave address. The I2C bus protocol
1075 uses the SlaveAddressIndex to perform relative to physical address
1076 translation to access the blocks of hardware within the I2C device.
1078 @param[in] This Pointer to an EFI_I2C_IO_PROTOCOL structure.
1079 @param[in] SlaveAddressIndex Index value into an array of slave addresses
1080 for the I2C device. The values in the array
1081 are specified by the board designer, with the
1082 third party I2C device driver writer providing
1083 the slave address order.
1085 For devices that have a single slave address,
1086 this value must be zero. If the I2C device
1087 uses more than one slave address then the
1088 third party (upper level) I2C driver writer
1089 needs to specify the order of entries in the
1090 slave address array.
1092 \ref ThirdPartyI2cDrivers "Third Party I2C
1093 Drivers" section in I2cMaster.h.
1094 @param[in] Event Event to signal for asynchronous transactions,
1095 NULL for synchronous transactions
1096 @param[in] RequestPacket Pointer to an EFI_I2C_REQUEST_PACKET structure
1097 describing the I2C transaction
1098 @param[out] I2cStatus Optional buffer to receive the I2C transaction
1101 @retval EFI_SUCCESS The asynchronous transaction was successfully
1102 queued when Event is not NULL.
1103 @retval EFI_SUCCESS The transaction completed successfully when
1105 @retval EFI_BAD_BUFFER_SIZE The RequestPacket->LengthInBytes value is too
1107 @retval EFI_DEVICE_ERROR There was an I2C error (NACK) during the
1109 @retval EFI_INVALID_PARAMETER RequestPacket is NULL
1110 @retval EFI_NO_MAPPING The EFI_I2C_HOST_PROTOCOL could not set the
1111 bus configuration required to access this I2C
1113 @retval EFI_NO_RESPONSE The I2C device is not responding to the slave
1114 address selected by SlaveAddressIndex.
1115 EFI_DEVICE_ERROR will be returned if the
1116 controller cannot distinguish when the NACK
1118 @retval EFI_OUT_OF_RESOURCES Insufficient memory for I2C transaction
1119 @retval EFI_UNSUPPORTED The controller does not support the requested
1125 I2cBusQueueRequest (
1126 IN CONST EFI_I2C_IO_PROTOCOL
*This
,
1127 IN UINTN SlaveAddressIndex
,
1128 IN EFI_EVENT Event OPTIONAL
,
1129 IN EFI_I2C_REQUEST_PACKET
*RequestPacket
,
1130 OUT EFI_STATUS
*I2cStatus OPTIONAL
1133 CONST EFI_I2C_DEVICE
*I2cDevice
;
1134 I2C_BUS_CONTEXT
*I2cBusContext
;
1135 CONST EFI_I2C_HOST_PROTOCOL
*I2cHost
;
1136 I2C_DEVICE_CONTEXT
*I2cDeviceContext
;
1139 if (RequestPacket
== NULL
) {
1140 return EFI_INVALID_PARAMETER
;
1144 // Validate the I2C slave index
1146 I2cDeviceContext
= I2C_DEVICE_CONTEXT_FROM_PROTOCOL (This
);
1147 I2cDevice
= I2cDeviceContext
->I2cDevice
;
1148 if ( SlaveAddressIndex
>= I2cDevice
->SlaveAddressCount
) {
1149 return EFI_INVALID_PARAMETER
;
1153 // Locate the I2c Host Protocol to queue request
1155 I2cBusContext
= I2cDeviceContext
->I2cBusContext
;
1156 I2cHost
= I2cBusContext
->I2cHost
;
1159 // Start the I2C operation
1161 Status
= I2cHost
->QueueRequest (
1163 I2cDevice
->I2cBusConfiguration
,
1164 I2cDevice
->SlaveAddressArray
[SlaveAddressIndex
],
1174 Release all the resources allocated for the I2C device.
1176 This function releases all the resources allocated for the I2C device.
1178 @param I2cDeviceContext The I2C child device involved for the operation.
1182 ReleaseI2cDeviceContext (
1183 IN I2C_DEVICE_CONTEXT
*I2cDeviceContext
1186 if (I2cDeviceContext
== NULL
) {
1190 if (I2cDeviceContext
->DevicePath
!= NULL
) {
1191 FreePool (I2cDeviceContext
->DevicePath
);
1194 FreePool (I2cDeviceContext
);
1198 Unregister an I2C device.
1200 This function removes the protocols installed on the controller handle and
1201 frees the resources allocated for the I2C device.
1203 @param This The pointer to EFI_DRIVER_BINDING_PROTOCOL instance.
1204 @param Controller The controller handle of the I2C device.
1205 @param Handle The child handle.
1207 @retval EFI_SUCCESS The I2C device is successfully unregistered.
1208 @return Others Some error occurs when unregistering the I2C device.
1212 UnRegisterI2cDevice (
1213 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1214 IN EFI_HANDLE Controller
,
1215 IN EFI_HANDLE Handle
1219 I2C_DEVICE_CONTEXT
*I2cDeviceContext
;
1220 EFI_I2C_IO_PROTOCOL
*I2cIo
;
1221 EFI_I2C_HOST_PROTOCOL
*I2cHost
;
1225 Status
= gBS
->OpenProtocol (
1227 &gEfiI2cIoProtocolGuid
,
1229 This
->DriverBindingHandle
,
1231 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1233 if (EFI_ERROR (Status
)) {
1238 // Get I2c device context data.
1240 I2cDeviceContext
= I2C_DEVICE_CONTEXT_FROM_PROTOCOL (I2cIo
);
1243 // Close the child handle
1245 gBS
->CloseProtocol (
1247 &gEfiI2cHostProtocolGuid
,
1248 This
->DriverBindingHandle
,
1253 // The I2C Bus driver installs the I2C Io and Device Path Protocol in the DriverBindingStart().
1254 // Here should uninstall them.
1256 Status
= gBS
->UninstallMultipleProtocolInterfaces (
1258 &gEfiDevicePathProtocolGuid
,
1259 I2cDeviceContext
->DevicePath
,
1260 &gEfiI2cIoProtocolGuid
,
1261 &I2cDeviceContext
->I2cIo
,
1265 if (EFI_ERROR (Status
)) {
1267 // Keep parent and child relationship
1271 &gEfiI2cHostProtocolGuid
,
1273 This
->DriverBindingHandle
,
1275 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
1281 // Free resources for this I2C device
1283 ReleaseI2cDeviceContext (I2cDeviceContext
);
1289 Create a path for the I2C device
1291 Append the I2C slave path to the I2C master controller path.
1293 @param[in] I2cDeviceContext Address of an I2C_DEVICE_CONTEXT structure.
1294 @param[in] BuildControllerNode Flag to build controller node in device path.
1296 @retval EFI_SUCCESS The I2C device path is built successfully.
1297 @return Others It is failed to built device path.
1301 I2cBusDevicePathAppend (
1302 IN I2C_DEVICE_CONTEXT
*I2cDeviceContext
,
1303 IN BOOLEAN BuildControllerNode
1306 EFI_DEVICE_PATH_PROTOCOL
*PreviousDevicePath
;
1308 PreviousDevicePath
= NULL
;
1311 // Build vendor device path
1313 CopyMem (&gVendorDevicePathTemplate
.Guid
, I2cDeviceContext
->I2cDevice
->DeviceGuid
, sizeof (EFI_GUID
));
1314 I2cDeviceContext
->DevicePath
= AppendDevicePathNode (
1315 I2cDeviceContext
->I2cBusContext
->ParentDevicePath
,
1316 (EFI_DEVICE_PATH_PROTOCOL
*) &gVendorDevicePathTemplate
1318 ASSERT (I2cDeviceContext
->DevicePath
!= NULL
);
1319 if (I2cDeviceContext
->DevicePath
== NULL
) {
1320 return EFI_OUT_OF_RESOURCES
;
1323 if ((BuildControllerNode
) && (I2cDeviceContext
->DevicePath
!= NULL
)) {
1325 // Build the final I2C device path with controller node
1327 PreviousDevicePath
= I2cDeviceContext
->DevicePath
;
1328 gControllerDevicePathTemplate
.ControllerNumber
= I2cDeviceContext
->I2cDevice
->DeviceIndex
;
1329 I2cDeviceContext
->DevicePath
= AppendDevicePathNode (
1330 I2cDeviceContext
->DevicePath
,
1331 (EFI_DEVICE_PATH_PROTOCOL
*) &gControllerDevicePathTemplate
1333 gBS
->FreePool (PreviousDevicePath
);
1334 ASSERT (I2cDeviceContext
->DevicePath
!= NULL
);
1335 if (I2cDeviceContext
->DevicePath
== NULL
) {
1336 return EFI_OUT_OF_RESOURCES
;
1344 The user entry point for the I2C bus module. The user code starts with
1347 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1348 @param[in] SystemTable A pointer to the EFI System Table.
1350 @retval EFI_SUCCESS The entry point is executed successfully.
1351 @retval other Some error occurs when executing this entry point.
1357 IN EFI_HANDLE ImageHandle
,
1358 IN EFI_SYSTEM_TABLE
*SystemTable
1364 // Install driver model protocol(s).
1366 Status
= EfiLibInstallDriverBindingComponentName2 (
1369 &gI2cBusDriverBinding
,
1371 &gI2cBusComponentName
,
1372 &gI2cBusComponentName2
1374 ASSERT_EFI_ERROR (Status
);
1381 This is the unload handle for I2C bus module.
1383 Disconnect the driver specified by ImageHandle from all the devices in the handle database.
1384 Uninstall all the protocols installed in the driver entry point.
1386 @param[in] ImageHandle The drivers' driver image.
1388 @retval EFI_SUCCESS The image is unloaded.
1389 @retval Others Failed to unload the image.
1395 IN EFI_HANDLE ImageHandle
1399 EFI_HANDLE
*DeviceHandleBuffer
;
1400 UINTN DeviceHandleCount
;
1402 EFI_COMPONENT_NAME_PROTOCOL
*ComponentName
;
1403 EFI_COMPONENT_NAME2_PROTOCOL
*ComponentName2
;
1406 // Get the list of all I2C Controller handles in the handle database.
1407 // If there is an error getting the list, then the unload
1410 Status
= gBS
->LocateHandleBuffer (
1412 &gEfiI2cHostProtocolGuid
,
1418 if (!EFI_ERROR (Status
)) {
1420 // Disconnect the driver specified by Driver BindingHandle from all
1421 // the devices in the handle database.
1423 for (Index
= 0; Index
< DeviceHandleCount
; Index
++) {
1424 Status
= gBS
->DisconnectController (
1425 DeviceHandleBuffer
[Index
],
1426 gI2cBusDriverBinding
.DriverBindingHandle
,
1429 if (EFI_ERROR (Status
)) {
1436 // Uninstall all the protocols installed in the driver entry point
1438 Status
= gBS
->UninstallMultipleProtocolInterfaces (
1439 gI2cBusDriverBinding
.DriverBindingHandle
,
1440 &gEfiDriverBindingProtocolGuid
,
1441 &gI2cBusDriverBinding
,
1444 ASSERT_EFI_ERROR (Status
);
1447 // Note we have to one by one uninstall the following protocols.
1448 // It's because some of them are optionally installed based on
1449 // the following PCD settings.
1450 // gEfiMdePkgTokenSpaceGuid.PcdDriverDiagnosticsDisable
1451 // gEfiMdePkgTokenSpaceGuid.PcdComponentNameDisable
1452 // gEfiMdePkgTokenSpaceGuid.PcdDriverDiagnostics2Disable
1453 // gEfiMdePkgTokenSpaceGuid.PcdComponentName2Disable
1455 Status
= gBS
->HandleProtocol (
1456 gI2cBusDriverBinding
.DriverBindingHandle
,
1457 &gEfiComponentNameProtocolGuid
,
1458 (VOID
**) &ComponentName
1460 if (!EFI_ERROR (Status
)) {
1461 gBS
->UninstallProtocolInterface (
1462 gI2cBusDriverBinding
.DriverBindingHandle
,
1463 &gEfiComponentNameProtocolGuid
,
1468 Status
= gBS
->HandleProtocol (
1469 gI2cBusDriverBinding
.DriverBindingHandle
,
1470 &gEfiComponentName2ProtocolGuid
,
1471 (VOID
**) &ComponentName2
1473 if (!EFI_ERROR (Status
)) {
1474 gBS
->UninstallProtocolInterface (
1475 gI2cBusDriverBinding
.DriverBindingHandle
,
1476 &gEfiComponentName2ProtocolGuid
,
1481 Status
= EFI_SUCCESS
;
1485 // Free the buffer containing the list of handles from the handle database
1487 if (DeviceHandleBuffer
!= NULL
) {
1488 gBS
->FreePool (DeviceHandleBuffer
);