DriverBinding.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /** @file
  2. Device driver for the Atmel ATSHA204A random number generator.
  3. Copyright (c) 2018, Linaro Ltd. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <Library/UefiDriverEntryPoint.h>
  7. #include "AtSha204aDriver.h"
  8. /**
  9. Tests to see if this driver supports a given controller.
  10. @param This[in] A pointer to the EFI_DRIVER_BINDING_PROTOCOL
  11. instance.
  12. @param ControllerHandle[in] The handle of the controller to test.
  13. @param RemainingDevicePath[in] The remaining device path.
  14. (Ignored - this is not a bus driver.)
  15. @retval EFI_SUCCESS The driver supports this controller.
  16. @retval EFI_ALREADY_STARTED The device specified by ControllerHandle is
  17. already being managed by the driver specified
  18. by This.
  19. @retval EFI_UNSUPPORTED The device specified by ControllerHandle is
  20. not supported by the driver specified by This.
  21. **/
  22. EFI_STATUS
  23. EFIAPI
  24. I2cHwrngDriverBindingSupported (
  25. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  26. IN EFI_HANDLE ControllerHandle,
  27. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
  28. )
  29. {
  30. EFI_I2C_IO_PROTOCOL *I2cIo;
  31. EFI_STATUS Status;
  32. //
  33. // Connect to the I2C stack
  34. //
  35. Status = gBS->OpenProtocol (ControllerHandle,
  36. &gEfiI2cIoProtocolGuid,
  37. (VOID **) &I2cIo,
  38. This->DriverBindingHandle,
  39. ControllerHandle,
  40. EFI_OPEN_PROTOCOL_BY_DRIVER);
  41. if (EFI_ERROR (Status)) {
  42. return Status;
  43. }
  44. if (CompareGuid (I2cIo->DeviceGuid, &gAtSha204aI2cDeviceGuid)) {
  45. DEBUG ((DEBUG_INIT | DEBUG_INFO, "Detected AtSha204a RNG device\n"));
  46. Status = EFI_SUCCESS;
  47. } else {
  48. Status = EFI_UNSUPPORTED;
  49. }
  50. //
  51. // Clean up.
  52. //
  53. gBS->CloseProtocol (ControllerHandle,
  54. &gEfiI2cIoProtocolGuid,
  55. This->DriverBindingHandle,
  56. ControllerHandle);
  57. return Status;
  58. }
  59. /**
  60. Starts a device controller or a bus controller.
  61. @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL
  62. instance.
  63. @param[in] ControllerHandle The handle of the device to start. This
  64. handle must support a protocol interface that
  65. supplies an I/O abstraction to the driver.
  66. @param[in] RemainingDevicePath The remaining portion of the device path.
  67. (Ignored - this is not a bus driver.)
  68. @retval EFI_SUCCESS The device was started.
  69. @retval EFI_DEVICE_ERROR The device could not be started due to a
  70. device error.
  71. @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a
  72. lack of resources.
  73. **/
  74. EFI_STATUS
  75. EFIAPI
  76. I2cHwrngDriverBindingStart (
  77. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  78. IN EFI_HANDLE ControllerHandle,
  79. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
  80. )
  81. {
  82. return AtSha204aInit (This->DriverBindingHandle, ControllerHandle);
  83. }
  84. /**
  85. Stops a device controller or a bus controller.
  86. @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL
  87. instance.
  88. @param[in] ControllerHandle A handle to the device being stopped. The handle
  89. must support a bus specific I/O protocol for the
  90. driver to use to stop the device.
  91. @param[in] NumberOfChildren The number of child device handles in
  92. ChildHandleBuffer.
  93. @param[in] ChildHandleBuffer An array of child handles to be freed. May be
  94. NULL if NumberOfChildren is 0.
  95. @retval EFI_SUCCESS The device was stopped.
  96. @retval EFI_DEVICE_ERROR The device could not be stopped due to a device
  97. error.
  98. **/
  99. EFI_STATUS
  100. EFIAPI
  101. I2cHwrngDriverBindingStop (
  102. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  103. IN EFI_HANDLE ControllerHandle,
  104. IN UINTN NumberOfChildren,
  105. IN EFI_HANDLE *ChildHandleBuffer OPTIONAL
  106. )
  107. {
  108. return AtSha204aRelease (This->DriverBindingHandle, ControllerHandle);
  109. }
  110. EFI_DRIVER_BINDING_PROTOCOL gI2cHwrngDriverBinding = {
  111. I2cHwrngDriverBindingSupported,
  112. I2cHwrngDriverBindingStart,
  113. I2cHwrngDriverBindingStop,
  114. 0xa,
  115. NULL,
  116. NULL
  117. };
  118. /**
  119. The entry point of AtSha204a UEFI Driver.
  120. @param ImageHandle The image handle of the UEFI Driver.
  121. @param SystemTable A pointer to the EFI System Table.
  122. @retval EFI_SUCCESS The Driver or UEFI Driver exited normally.
  123. @retval EFI_INCOMPATIBLE_VERSION _gUefiDriverRevision is greater than
  124. SystemTable->Hdr.Revision.
  125. **/
  126. EFI_STATUS
  127. EFIAPI
  128. EntryPoint (
  129. IN EFI_HANDLE ImageHandle,
  130. IN EFI_SYSTEM_TABLE *SystemTable
  131. )
  132. {
  133. EFI_STATUS Status;
  134. //
  135. // Add the driver to the list of drivers
  136. //
  137. Status = EfiLibInstallDriverBindingComponentName2 (
  138. ImageHandle, SystemTable, &gI2cHwrngDriverBinding, ImageHandle,
  139. NULL, &gAtSha204aDriverComponentName2);
  140. ASSERT_EFI_ERROR (Status);
  141. DEBUG ((DEBUG_INIT | DEBUG_INFO, "*** Installed AtSha204a driver! ***\n"));
  142. return EFI_SUCCESS;
  143. }
  144. /**
  145. Unload function for the AtSha204a Driver.
  146. @param ImageHandle[in] The allocated handle for the EFI image
  147. @retval EFI_SUCCESS The driver was unloaded successfully
  148. @retval EFI_INVALID_PARAMETER ImageHandle is not a valid image handle.
  149. **/
  150. EFI_STATUS
  151. EFIAPI
  152. UnloadImage (
  153. IN EFI_HANDLE ImageHandle
  154. )
  155. {
  156. EFI_STATUS Status;
  157. EFI_HANDLE *HandleBuffer;
  158. UINTN HandleCount;
  159. UINTN Index;
  160. //
  161. // Retrieve all I2C I/O handles in the handle database
  162. //
  163. Status = gBS->LocateHandleBuffer (ByProtocol,
  164. &gEfiI2cIoProtocolGuid,
  165. NULL,
  166. &HandleCount,
  167. &HandleBuffer);
  168. if (EFI_ERROR (Status)) {
  169. return Status;
  170. }
  171. //
  172. // Disconnect the driver from the handles in the handle database
  173. //
  174. for (Index = 0; Index < HandleCount; Index++) {
  175. Status = gBS->DisconnectController (HandleBuffer[Index],
  176. gImageHandle,
  177. NULL);
  178. }
  179. //
  180. // Free the handle array
  181. //
  182. gBS->FreePool (HandleBuffer);
  183. //
  184. // Uninstall protocols installed by the driver in its entrypoint
  185. //
  186. Status = gBS->UninstallMultipleProtocolInterfaces (ImageHandle,
  187. &gEfiDriverBindingProtocolGuid,
  188. &gI2cHwrngDriverBinding,
  189. NULL
  190. );
  191. return EFI_SUCCESS;
  192. }