2 Implementation of driver entry point and driver binding protocol.
4 Copyright (c) 2005 - 2008, Intel Corporation.<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "MnpDriver.h"
19 EFI_DRIVER_BINDING_PROTOCOL gMnpDriverBinding
= {
20 MnpDriverBindingSupported
,
21 MnpDriverBindingStart
,
29 Test to see if this driver supports ControllerHandle. This service
30 is called by the EFI boot service ConnectController(). In
31 order to make drivers as small as possible, there are a few calling
32 restrictions for this service. ConnectController() must
33 follow these calling restrictions. If any other agent wishes to call
34 Supported() it must also follow these calling restrictions.
36 @param[in] This Protocol instance pointer.
37 @param[in] ControllerHandle Handle of device to test
38 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
41 @retval EFI_SUCCESS This driver supports this device
42 @retval EFI_ALREADY_STARTED This driver is already running on this device
43 @retval other This driver does not support this device
48 MnpDriverBindingSupported (
49 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
50 IN EFI_HANDLE ControllerHandle
,
51 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
55 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
;
58 // Test to see if MNP is already installed.
60 Status
= gBS
->OpenProtocol (
62 &gEfiManagedNetworkServiceBindingProtocolGuid
,
64 This
->DriverBindingHandle
,
66 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
68 if (!EFI_ERROR (Status
)) {
69 return EFI_ALREADY_STARTED
;
73 // Test to open the Simple Network protocol BY_DRIVER.
75 Status
= gBS
->OpenProtocol (
77 &gEfiSimpleNetworkProtocolGuid
,
79 This
->DriverBindingHandle
,
81 EFI_OPEN_PROTOCOL_BY_DRIVER
84 if (EFI_ERROR (Status
)) {
89 // Close the openned SNP protocol.
93 &gEfiSimpleNetworkProtocolGuid
,
94 This
->DriverBindingHandle
,
103 Start this driver on ControllerHandle. This service is called by the
104 EFI boot service ConnectController(). In order to make
105 drivers as small as possible, there are a few calling restrictions for
106 this service. ConnectController() must follow these
107 calling restrictions. If any other agent wishes to call Start() it
108 must also follow these calling restrictions.
110 @param[in] This Protocol instance pointer.
111 @param[in] ControllerHandle Handle of device to bind driver to.
112 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
115 @retval EFI_SUCCESS This driver is added to ControllerHandle
116 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
117 @retval other This driver does not support this device
122 MnpDriverBindingStart (
123 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
124 IN EFI_HANDLE ControllerHandle
,
125 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
129 MNP_SERVICE_DATA
*MnpServiceData
;
130 BOOLEAN MnpInitialized
;
132 MnpInitialized
= FALSE
;
134 MnpServiceData
= AllocateZeroPool (sizeof (MNP_SERVICE_DATA
));
135 if (MnpServiceData
== NULL
) {
136 DEBUG ((EFI_D_ERROR
, "MnpDriverBindingStart(): Failed to allocate the Mnp Service Data.\n"));
138 return EFI_OUT_OF_RESOURCES
;
142 // Initialize the Mnp Service Data.
144 Status
= MnpInitializeServiceData (MnpServiceData
, This
->DriverBindingHandle
, ControllerHandle
);
145 if (EFI_ERROR (Status
)) {
147 DEBUG ((EFI_D_ERROR
, "MnpDriverBindingStart: MnpInitializeServiceData failed, %r.\n",Status
));
151 MnpInitialized
= TRUE
;
154 // Install the MNP Service Binding Protocol.
156 Status
= gBS
->InstallMultipleProtocolInterfaces (
158 &gEfiManagedNetworkServiceBindingProtocolGuid
,
159 &MnpServiceData
->ServiceBinding
,
165 if (EFI_ERROR (Status
)) {
167 if (MnpInitialized
) {
169 // Flush the Mnp Service Data.
171 MnpFlushServiceData (MnpServiceData
, This
->DriverBindingHandle
);
174 gBS
->FreePool (MnpServiceData
);
181 Stop this driver on ControllerHandle. This service is called by the
182 EFI boot service DisconnectController(). In order to
183 make drivers as small as possible, there are a few calling
184 restrictions for this service. DisconnectController()
185 must follow these calling restrictions. If any other agent wishes
186 to call Stop() it must also follow these calling restrictions.
188 @param[in] This Protocol instance pointer.
189 @param[in] ControllerHandle Handle of device to stop driver on
190 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
191 children is zero stop the entire bus driver.
192 @param[in] ChildHandleBuffer List of Child Handles to Stop.
194 @retval EFI_SUCCESS This driver is removed ControllerHandle
195 @retval other This driver was not removed from this device
200 MnpDriverBindingStop (
201 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
202 IN EFI_HANDLE ControllerHandle
,
203 IN UINTN NumberOfChildren
,
204 IN EFI_HANDLE
*ChildHandleBuffer
208 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
209 MNP_SERVICE_DATA
*MnpServiceData
;
210 MNP_INSTANCE_DATA
*Instance
;
213 // Retrieve the MNP service binding protocol from the ControllerHandle.
215 Status
= gBS
->OpenProtocol (
217 &gEfiManagedNetworkServiceBindingProtocolGuid
,
218 (VOID
**) &ServiceBinding
,
219 This
->DriverBindingHandle
,
221 EFI_OPEN_PROTOCOL_GET_PROTOCOL
223 if (EFI_ERROR (Status
)) {
227 "MnpDriverBindingStop: Locate MNP Service Binding Protocol failed, %r.\n",
230 return EFI_DEVICE_ERROR
;
233 MnpServiceData
= MNP_SERVICE_DATA_FROM_THIS (ServiceBinding
);
235 if (NumberOfChildren
== 0) {
237 // Uninstall the MNP Service Binding Protocol.
239 gBS
->UninstallMultipleProtocolInterfaces (
241 &gEfiManagedNetworkServiceBindingProtocolGuid
,
247 // Flush the Mnp service data.
249 MnpFlushServiceData (MnpServiceData
, This
->DriverBindingHandle
);
251 gBS
->FreePool (MnpServiceData
);
253 while (!IsListEmpty (&MnpServiceData
->ChildrenList
)) {
255 // Don't use NetListRemoveHead here, the remove opreration will be done
256 // in ServiceBindingDestroyChild.
258 Instance
= NET_LIST_HEAD (
259 &MnpServiceData
->ChildrenList
,
264 ServiceBinding
->DestroyChild (ServiceBinding
, Instance
->Handle
);
273 Creates a child handle and installs a protocol.
275 The CreateChild() function installs a protocol on ChildHandle.
276 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
277 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
279 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
280 @param ChildHandle Pointer to the handle of the child to create. If it is NULL,
281 then a new handle is created. If it is a pointer to an existing UEFI handle,
282 then the protocol is added to the existing UEFI handle.
284 @retval EFI_SUCCES The protocol was added to ChildHandle.
285 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
286 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to create
288 @retval other The child handle was not created
293 MnpServiceBindingCreateChild (
294 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
295 IN OUT EFI_HANDLE
*ChildHandle
299 MNP_SERVICE_DATA
*MnpServiceData
;
300 MNP_INSTANCE_DATA
*Instance
;
304 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
306 return EFI_INVALID_PARAMETER
;
309 MnpServiceData
= MNP_SERVICE_DATA_FROM_THIS (This
);
312 // Allocate buffer for the new instance.
314 Instance
= AllocateZeroPool (sizeof (MNP_INSTANCE_DATA
));
315 if (Instance
== NULL
) {
317 DEBUG ((EFI_D_ERROR
, "MnpServiceBindingCreateChild: Faild to allocate memory for the new instance.\n"));
318 return EFI_OUT_OF_RESOURCES
;
322 // Init the instance data.
324 MnpInitializeInstanceData (MnpServiceData
, Instance
);
326 Status
= gBS
->InstallMultipleProtocolInterfaces (
328 &gEfiManagedNetworkProtocolGuid
,
329 &Instance
->ManagedNetwork
,
332 if (EFI_ERROR (Status
)) {
336 "MnpServiceBindingCreateChild: Failed to install the MNP protocol, %r.\n",
343 // Save the instance's childhandle.
345 Instance
->Handle
= *ChildHandle
;
347 Status
= gBS
->OpenProtocol (
348 MnpServiceData
->ControllerHandle
,
349 &gEfiSimpleNetworkProtocolGuid
,
351 gMnpDriverBinding
.DriverBindingHandle
,
353 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
355 if (EFI_ERROR (Status
)) {
360 // Add the child instance into ChildrenList.
362 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
364 InsertTailList (&MnpServiceData
->ChildrenList
, &Instance
->InstEntry
);
365 MnpServiceData
->ChildrenNumber
++;
367 gBS
->RestoreTPL (OldTpl
);
371 if (EFI_ERROR (Status
)) {
373 if (Instance
->Handle
!= NULL
) {
375 gBS
->UninstallMultipleProtocolInterfaces (
376 &gEfiManagedNetworkProtocolGuid
,
377 &Instance
->ManagedNetwork
,
382 gBS
->FreePool (Instance
);
390 Destroys a child handle with a protocol installed on it.
392 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
393 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
394 last protocol on ChildHandle, then ChildHandle is destroyed.
396 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
397 @param ChildHandle Handle of the child to destroy
399 @retval EFI_SUCCES The protocol was removed from ChildHandle.
400 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
401 @retval EFI_INVALID_PARAMETER Child handle is not a valid UEFI Handle.
402 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
403 because its services are being used.
404 @retval other The child handle was not destroyed
409 MnpServiceBindingDestroyChild (
410 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
411 IN EFI_HANDLE ChildHandle
415 MNP_SERVICE_DATA
*MnpServiceData
;
416 EFI_MANAGED_NETWORK_PROTOCOL
*ManagedNetwork
;
417 MNP_INSTANCE_DATA
*Instance
;
420 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
422 return EFI_INVALID_PARAMETER
;
425 MnpServiceData
= MNP_SERVICE_DATA_FROM_THIS (This
);
428 // Try to retrieve ManagedNetwork Protocol from ChildHandle.
430 Status
= gBS
->OpenProtocol (
432 &gEfiManagedNetworkProtocolGuid
,
433 (VOID
**) &ManagedNetwork
,
434 gMnpDriverBinding
.DriverBindingHandle
,
436 EFI_OPEN_PROTOCOL_GET_PROTOCOL
438 if (EFI_ERROR (Status
)) {
440 return EFI_UNSUPPORTED
;
443 Instance
= MNP_INSTANCE_DATA_FROM_THIS (ManagedNetwork
);
446 // MnpServiceBindingDestroyChild may be called twice: first called by
447 // MnpServiceBindingStop, second called by uninstalling the MNP protocol
448 // in this ChildHandle. Use destroyed to make sure the resource clean code
449 // will only excecute once.
451 if (Instance
->Destroyed
) {
456 Instance
->Destroyed
= TRUE
;
459 // Close the Simple Network protocol.
462 MnpServiceData
->ControllerHandle
,
463 &gEfiSimpleNetworkProtocolGuid
,
464 gMnpDriverBinding
.DriverBindingHandle
,
469 // Uninstall the ManagedNetwork protocol.
471 Status
= gBS
->UninstallMultipleProtocolInterfaces (
473 &gEfiManagedNetworkProtocolGuid
,
474 &Instance
->ManagedNetwork
,
477 if (EFI_ERROR (Status
)) {
481 "MnpServiceBindingDestroyChild: Failed to uninstall the ManagedNetwork protocol, %r.\n",
485 Instance
->Destroyed
= FALSE
;
489 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
492 // Reset the configuration.
494 ManagedNetwork
->Configure (ManagedNetwork
, NULL
);
497 // Try to flush the RcvdPacketQueue.
499 MnpFlushRcvdDataQueue (Instance
);
502 // Clean the RxTokenMap.
504 NetMapClean (&Instance
->RxTokenMap
);
507 // Remove this instance from the ChildrenList.
509 RemoveEntryList (&Instance
->InstEntry
);
510 MnpServiceData
->ChildrenNumber
--;
512 gBS
->RestoreTPL (OldTpl
);
514 gBS
->FreePool (Instance
);
520 The entry point for Mnp driver which installs the driver binding and component
521 name protocol on its ImageHandle.
523 @param[in] ImageHandle The image handle of the driver.
524 @param[in] SystemTable The system table.
526 @retval EFI_SUCCES The driver binding and component name protocols are
527 successfully installed.
528 @retval other failed.
533 MnpDriverEntryPoint (
534 IN EFI_HANDLE ImageHandle
,
535 IN EFI_SYSTEM_TABLE
*SystemTable
538 return EfiLibInstallDriverBindingComponentName2 (