2 Implementation of driver entry point and driver binding protocol.
4 Copyright (c) 2005 - 2009, Intel Corporation. <BR>
5 All rights reserved. This program and the accompanying materials are licensed
6 and made available under the terms and conditions of the BSD License which
7 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
39 child device to start.
41 @retval EFI_SUCCESS This driver supports this device.
42 @retval EFI_ALREADY_STARTED This driver is already running on this device.
43 @retval Others 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 drivers as small
105 as possible, there are a few calling restrictions for this service.
106 ConnectController() must follow these calling restrictions. If any other
107 agent wishes to call Start() it must also follow these calling restrictions.
109 @param[in] This Protocol instance pointer.
110 @param[in] ControllerHandle Handle of device to bind driver to.
111 @param[in] RemainingDevicePath Optional parameter use to pick a specific
112 child device to start.
114 @retval EFI_SUCCESS This driver is added to ControllerHandle.
115 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle.
116 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for Mnp Service Data.
117 @retval Others 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 FreePool (MnpServiceData
);
181 Stop this driver on ControllerHandle. This service is called by the
182 EFI boot service DisconnectController(). In order to make drivers as
183 small as possible, there are a few calling restrictions for this service.
184 DisconnectController() must follow these calling restrictions. If any other
185 agent wishes to call Stop() it must also follow these calling restrictions.
187 @param[in] This Protocol instance pointer.
188 @param[in] ControllerHandle Handle of device to stop driver on.
189 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer. If
190 number of children is zero stop the entire
192 @param[in] ChildHandleBuffer List of Child Handles to Stop.
194 @retval EFI_SUCCESS This driver is removed ControllerHandle.
195 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
200 MnpDriverBindingStop (
201 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
202 IN EFI_HANDLE ControllerHandle
,
203 IN UINTN NumberOfChildren
,
204 IN EFI_HANDLE
*ChildHandleBuffer OPTIONAL
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 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 with a set of I/O services.
275 @param[in] This Protocol instance pointer.
276 @param[in, out] ChildHandle Pointer to the handle of the child to create. If
277 it is NULL, then a new handle is created. If
278 it is not NULL, then the I/O services are added
279 to the existing child handle.
281 @retval EFI_SUCCES The protocol was added to ChildHandle.
282 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
283 @retval EFI_OUT_OF_RESOURCES There are not enough resources availabe to
285 @retval Others The child handle was not created.
290 MnpServiceBindingCreateChild (
291 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
292 IN OUT EFI_HANDLE
*ChildHandle
296 MNP_SERVICE_DATA
*MnpServiceData
;
297 MNP_INSTANCE_DATA
*Instance
;
301 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
303 return EFI_INVALID_PARAMETER
;
306 MnpServiceData
= MNP_SERVICE_DATA_FROM_THIS (This
);
309 // Allocate buffer for the new instance.
311 Instance
= AllocateZeroPool (sizeof (MNP_INSTANCE_DATA
));
312 if (Instance
== NULL
) {
314 DEBUG ((EFI_D_ERROR
, "MnpServiceBindingCreateChild: Faild to allocate memory for the new instance.\n"));
315 return EFI_OUT_OF_RESOURCES
;
319 // Init the instance data.
321 MnpInitializeInstanceData (MnpServiceData
, Instance
);
323 Status
= gBS
->InstallMultipleProtocolInterfaces (
325 &gEfiManagedNetworkProtocolGuid
,
326 &Instance
->ManagedNetwork
,
329 if (EFI_ERROR (Status
)) {
333 "MnpServiceBindingCreateChild: Failed to install the MNP protocol, %r.\n",
340 // Save the instance's childhandle.
342 Instance
->Handle
= *ChildHandle
;
344 Status
= gBS
->OpenProtocol (
345 MnpServiceData
->ControllerHandle
,
346 &gEfiSimpleNetworkProtocolGuid
,
348 gMnpDriverBinding
.DriverBindingHandle
,
350 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
352 if (EFI_ERROR (Status
)) {
357 // Add the child instance into ChildrenList.
359 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
361 InsertTailList (&MnpServiceData
->ChildrenList
, &Instance
->InstEntry
);
362 MnpServiceData
->ChildrenNumber
++;
364 gBS
->RestoreTPL (OldTpl
);
368 if (EFI_ERROR (Status
)) {
370 if (Instance
->Handle
!= NULL
) {
372 gBS
->UninstallMultipleProtocolInterfaces (
373 &gEfiManagedNetworkProtocolGuid
,
374 &Instance
->ManagedNetwork
,
387 Destroys a child handle with a set of I/O services.
389 The DestroyChild() function does the opposite of CreateChild(). It removes a
390 protocol that was installed by CreateChild() from ChildHandle. If the removed
391 protocol is the last protocol on ChildHandle, then ChildHandle is destroyed.
393 @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL
395 @param[in] ChildHandle Handle of the child to destroy.
397 @retval EFI_SUCCES The protocol was removed from ChildHandle.
398 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that
400 @retval EFI_INVALID_PARAMETER ChildHandle is not a valid UEFI handle.
401 @retval EFI_ACCESS_DENIED The protocol could not be removed from the
402 ChildHandle because its services are being
404 @retval Others 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
);
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 Others Other errors as indicated.
533 MnpDriverEntryPoint (
534 IN EFI_HANDLE ImageHandle
,
535 IN EFI_SYSTEM_TABLE
*SystemTable
538 return EfiLibInstallDriverBindingComponentName2 (