]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Bus/Sd/SdDxe/SdDxe.c
Vlv2TbltDevicePkg: fix ASSERT_EFI_ERROR() typos
[mirror_edk2.git] / MdeModulePkg / Bus / Sd / SdDxe / SdDxe.c
CommitLineData
48555339
FT
1/** @file\r
2 The SdDxe driver is used to manage the SD memory card device.\r
3\r
4 It produces BlockIo and BlockIo2 protocols to allow upper layer\r
5 access the SD memory card device.\r
6\r
7 Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>\r
8 This program and the accompanying materials\r
9 are licensed and made available under the terms and conditions of the BSD License\r
10 which accompanies this distribution. The full text of the license may be found at\r
11 http://opensource.org/licenses/bsd-license.php\r
12\r
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
15\r
16**/\r
17\r
18#include "SdDxe.h"\r
19\r
20//\r
21// SdDxe Driver Binding Protocol Instance\r
22//\r
23EFI_DRIVER_BINDING_PROTOCOL gSdDxeDriverBinding = {\r
24 SdDxeDriverBindingSupported,\r
25 SdDxeDriverBindingStart,\r
26 SdDxeDriverBindingStop,\r
27 0x10,\r
28 NULL,\r
29 NULL\r
30};\r
31\r
32//\r
33// Template for SD_DEVICE data structure.\r
34//\r
35SD_DEVICE mSdDeviceTemplate = {\r
36 SD_DEVICE_SIGNATURE, // Signature\r
37 NULL, // Handle\r
38 NULL, // DevicePath\r
39 0xFF, // Slot\r
40 FALSE, // SectorAddressing\r
41 { // BlockIo\r
42 EFI_BLOCK_IO_PROTOCOL_REVISION,\r
43 NULL,\r
44 SdReset,\r
45 SdReadBlocks,\r
46 SdWriteBlocks,\r
47 SdFlushBlocks\r
48 },\r
49 { // BlockIo2\r
50 NULL,\r
51 SdResetEx,\r
52 SdReadBlocksEx,\r
53 SdWriteBlocksEx,\r
54 SdFlushBlocksEx\r
55 },\r
56 { // BlockMedia\r
57 0, // MediaId\r
58 FALSE, // RemovableMedia\r
59 TRUE, // MediaPresent\r
60 FALSE, // LogicPartition\r
61 FALSE, // ReadOnly\r
62 FALSE, // WritingCache\r
63 0x200, // BlockSize\r
64 0, // IoAlign\r
65 0 // LastBlock\r
66 },\r
275d5136
FT
67 { // EraseBlock\r
68 EFI_ERASE_BLOCK_PROTOCOL_REVISION,\r
69 1,\r
70 SdEraseBlocks\r
71 },\r
48555339
FT
72 { // Queue\r
73 NULL,\r
74 NULL\r
75 },\r
76 { // Csd\r
77 0,\r
78 },\r
79 { // Cid\r
80 0,\r
81 },\r
82 NULL, // ControllerNameTable\r
83 { // ModelName\r
84 0,\r
85 },\r
86 NULL // Private\r
87};\r
88\r
89/**\r
90 Decode and print SD CSD Register content.\r
91\r
92 @param[in] Csd Pointer to SD_CSD data structure.\r
93\r
94 @retval EFI_SUCCESS The function completed successfully\r
95**/\r
96EFI_STATUS\r
97DumpCsd (\r
98 IN SD_CSD *Csd\r
99 )\r
100{\r
101 SD_CSD2 *Csd2;\r
102\r
103 DEBUG((DEBUG_INFO, "== Dump Sd Csd Register==\n"));\r
104 DEBUG((DEBUG_INFO, " CSD structure 0x%x\n", Csd->CsdStructure));\r
105 DEBUG((DEBUG_INFO, " Data read access-time 1 0x%x\n", Csd->Taac));\r
106 DEBUG((DEBUG_INFO, " Data read access-time 2 0x%x\n", Csd->Nsac));\r
107 DEBUG((DEBUG_INFO, " Max. bus clock frequency 0x%x\n", Csd->TranSpeed));\r
108 DEBUG((DEBUG_INFO, " Device command classes 0x%x\n", Csd->Ccc));\r
109 DEBUG((DEBUG_INFO, " Max. read data block length 0x%x\n", Csd->ReadBlLen));\r
110 DEBUG((DEBUG_INFO, " Partial blocks for read allowed 0x%x\n", Csd->ReadBlPartial));\r
111 DEBUG((DEBUG_INFO, " Write block misalignment 0x%x\n", Csd->WriteBlkMisalign));\r
112 DEBUG((DEBUG_INFO, " Read block misalignment 0x%x\n", Csd->ReadBlkMisalign));\r
113 DEBUG((DEBUG_INFO, " DSR implemented 0x%x\n", Csd->DsrImp));\r
114 if (Csd->CsdStructure == 0) {\r
115 DEBUG((DEBUG_INFO, " Device size 0x%x\n", Csd->CSizeLow | (Csd->CSizeHigh << 2)));\r
116 DEBUG((DEBUG_INFO, " Max. read current @ VDD min 0x%x\n", Csd->VddRCurrMin));\r
117 DEBUG((DEBUG_INFO, " Max. read current @ VDD max 0x%x\n", Csd->VddRCurrMax));\r
118 DEBUG((DEBUG_INFO, " Max. write current @ VDD min 0x%x\n", Csd->VddWCurrMin));\r
119 DEBUG((DEBUG_INFO, " Max. write current @ VDD max 0x%x\n", Csd->VddWCurrMax));\r
120 } else {\r
121 Csd2 = (SD_CSD2*)(VOID*)Csd;\r
122 DEBUG((DEBUG_INFO, " Device size 0x%x\n", Csd2->CSizeLow | (Csd->CSizeHigh << 16)));\r
123 }\r
124 DEBUG((DEBUG_INFO, " Erase sector size 0x%x\n", Csd->SectorSize));\r
125 DEBUG((DEBUG_INFO, " Erase single block enable 0x%x\n", Csd->EraseBlkEn));\r
126 DEBUG((DEBUG_INFO, " Write protect group size 0x%x\n", Csd->WpGrpSize));\r
127 DEBUG((DEBUG_INFO, " Write protect group enable 0x%x\n", Csd->WpGrpEnable));\r
128 DEBUG((DEBUG_INFO, " Write speed factor 0x%x\n", Csd->R2WFactor));\r
129 DEBUG((DEBUG_INFO, " Max. write data block length 0x%x\n", Csd->WriteBlLen));\r
130 DEBUG((DEBUG_INFO, " Partial blocks for write allowed 0x%x\n", Csd->WriteBlPartial));\r
131 DEBUG((DEBUG_INFO, " File format group 0x%x\n", Csd->FileFormatGrp));\r
132 DEBUG((DEBUG_INFO, " Copy flag (OTP) 0x%x\n", Csd->Copy));\r
133 DEBUG((DEBUG_INFO, " Permanent write protection 0x%x\n", Csd->PermWriteProtect));\r
134 DEBUG((DEBUG_INFO, " Temporary write protection 0x%x\n", Csd->TmpWriteProtect));\r
135 DEBUG((DEBUG_INFO, " File format 0x%x\n", Csd->FileFormat));\r
136\r
137 return EFI_SUCCESS;\r
138}\r
139\r
140/**\r
141 Get SD device model name.\r
142\r
143 @param[in, out] Device The pointer to the SD_DEVICE data structure.\r
144 @param[in] Cid Pointer to SD_CID data structure.\r
145\r
146 @retval EFI_SUCCESS The function completed successfully\r
147\r
148**/\r
149EFI_STATUS\r
150GetSdModelName (\r
151 IN OUT SD_DEVICE *Device,\r
152 IN SD_CID *Cid\r
153 )\r
154{\r
155 CHAR8 String[SD_MODEL_NAME_MAX_LEN];\r
156\r
157 ZeroMem (String, sizeof (String));\r
158 CopyMem (String, Cid->OemId, sizeof (Cid->OemId));\r
159 String[sizeof (Cid->OemId)] = ' ';\r
160 CopyMem (String + sizeof (Cid->OemId) + 1, Cid->ProductName, sizeof (Cid->ProductName));\r
161 String[sizeof (Cid->OemId) + sizeof (Cid->ProductName)] = ' ';\r
162 CopyMem (String + sizeof (Cid->OemId) + sizeof (Cid->ProductName) + 1, Cid->ProductSerialNumber, sizeof (Cid->ProductSerialNumber));\r
163\r
b68ccac1 164 AsciiStrToUnicodeStrS (String, Device->ModelName, sizeof (Device->ModelName) / sizeof (Device->ModelName[0]));\r
48555339
FT
165\r
166 return EFI_SUCCESS;\r
167}\r
168\r
169/**\r
170 Discover user area partition in the SD device.\r
171\r
172 @param[in] Device The pointer to the SD_DEVICE data structure.\r
173\r
174 @retval EFI_SUCCESS The user area partition in the SD device is successfully identified.\r
175 @return Others Some error occurs when identifying the user area.\r
176\r
177**/\r
178EFI_STATUS\r
179DiscoverUserArea (\r
180 IN SD_DEVICE *Device\r
181 )\r
182{\r
183 EFI_STATUS Status;\r
184 SD_CSD *Csd;\r
185 SD_CSD2 *Csd2;\r
186 SD_CID *Cid;\r
187 UINT64 Capacity;\r
188 UINT32 DevStatus;\r
189 UINT16 Rca;\r
190 UINT32 CSize;\r
191 UINT32 CSizeMul;\r
192 UINT32 ReadBlLen;\r
193\r
194 //\r
195 // Deselect the device to force it enter stby mode.\r
196 // Note here we don't judge return status as some SD devices return\r
197 // error but the state has been stby.\r
198 //\r
199 SdSelect (Device, 0);\r
200\r
201 Status = SdSetRca (Device, &Rca);\r
202 if (EFI_ERROR (Status)) {\r
203 DEBUG ((EFI_D_ERROR, "DiscoverUserArea(): Assign new Rca = 0x%x fails with %r\n", Rca, Status));\r
204 return Status;\r
205 }\r
206\r
207 Csd = &Device->Csd;\r
208 Status = SdGetCsd (Device, Rca, Csd);\r
209 if (EFI_ERROR (Status)) {\r
210 return Status;\r
211 }\r
212 DumpCsd (Csd);\r
213\r
214 Cid = &Device->Cid;\r
215 Status = SdGetCid (Device, Rca, Cid);\r
216 if (EFI_ERROR (Status)) {\r
217 return Status;\r
218 }\r
219 GetSdModelName (Device, Cid);\r
220\r
221 Status = SdSelect (Device, Rca);\r
222 if (EFI_ERROR (Status)) {\r
223 DEBUG ((EFI_D_ERROR, "DiscoverUserArea(): Reselect the device 0x%x fails with %r\n", Rca, Status));\r
224 return Status;\r
225 }\r
226\r
227 Status = SdSendStatus (Device, Rca, &DevStatus);\r
228 if (EFI_ERROR (Status)) {\r
229 return Status;\r
230 }\r
231\r
232 if (Csd->CsdStructure == 0) {\r
233 Device->SectorAddressing = FALSE;\r
234 CSize = (Csd->CSizeHigh << 2 | Csd->CSizeLow) + 1;\r
235 CSizeMul = (1 << (Csd->CSizeMul + 2));\r
236 ReadBlLen = (1 << (Csd->ReadBlLen));\r
237 Capacity = MultU64x32 (MultU64x32 ((UINT64)CSize, CSizeMul), ReadBlLen);\r
238 } else {\r
239 Device->SectorAddressing = TRUE;\r
240 Csd2 = (SD_CSD2*)(VOID*)Csd;\r
241 CSize = (Csd2->CSizeHigh << 16 | Csd2->CSizeLow) + 1;\r
242 Capacity = MultU64x32 ((UINT64)CSize, SIZE_512KB);\r
243 }\r
244\r
245 Device->BlockIo.Media = &Device->BlockMedia;\r
246 Device->BlockIo2.Media = &Device->BlockMedia;\r
247 Device->BlockMedia.IoAlign = Device->Private->PassThru->IoAlign;\r
248 Device->BlockMedia.BlockSize = 0x200;\r
249 Device->BlockMedia.LastBlock = 0x00;\r
250 Device->BlockMedia.RemovableMedia = TRUE;\r
251 Device->BlockMedia.MediaPresent = TRUE;\r
252 Device->BlockMedia.LogicalPartition = FALSE;\r
253 Device->BlockMedia.LastBlock = DivU64x32 (Capacity, Device->BlockMedia.BlockSize) - 1;\r
254\r
275d5136
FT
255 if (Csd->EraseBlkEn) {\r
256 Device->EraseBlock.EraseLengthGranularity = 1;\r
257 } else {\r
258 Device->EraseBlock.EraseLengthGranularity = (Csd->SectorSize + 1) * (1 << (Csd->WriteBlLen - 9));\r
259 }\r
260\r
48555339
FT
261 return Status;\r
262}\r
263\r
264/**\r
265 Scan SD Bus to discover the device.\r
266\r
267 @param[in] Private The SD driver private data structure.\r
268 @param[in] Slot The slot number to check device present.\r
269\r
270 @retval EFI_SUCCESS Successfully to discover the device and attach\r
271 SdMmcIoProtocol to it.\r
272 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack\r
273 of resources.\r
274 @retval EFI_ALREADY_STARTED The device was discovered before.\r
275 @retval Others Fail to discover the device.\r
276\r
277**/\r
278EFI_STATUS\r
279EFIAPI\r
280DiscoverSdDevice (\r
281 IN SD_DRIVER_PRIVATE_DATA *Private,\r
282 IN UINT8 Slot\r
283 )\r
284{\r
285 EFI_STATUS Status;\r
286 SD_DEVICE *Device;\r
287 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
288 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r
289 EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath;\r
290 EFI_HANDLE DeviceHandle;\r
291 EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru;\r
292\r
293 Device = NULL;\r
294 DevicePath = NULL;\r
295 NewDevicePath = NULL;\r
296 RemainingDevicePath = NULL;\r
297 PassThru = Private->PassThru;\r
298\r
299 //\r
300 // Build Device Path\r
301 //\r
302 Status = PassThru->BuildDevicePath (\r
303 PassThru,\r
304 Slot,\r
305 &DevicePath\r
306 );\r
307 if (EFI_ERROR(Status)) {\r
308 return Status;\r
309 }\r
310\r
311 if (DevicePath->SubType != MSG_SD_DP) {\r
312 Status = EFI_UNSUPPORTED;\r
313 goto Error;\r
314 }\r
315\r
316 NewDevicePath = AppendDevicePathNode (\r
317 Private->ParentDevicePath,\r
318 DevicePath\r
319 );\r
320\r
321 if (NewDevicePath == NULL) {\r
322 Status = EFI_OUT_OF_RESOURCES;\r
323 goto Error;\r
324 }\r
325\r
326 DeviceHandle = NULL;\r
327 RemainingDevicePath = NewDevicePath;\r
328 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &RemainingDevicePath, &DeviceHandle);\r
329 if (!EFI_ERROR (Status) && (DeviceHandle != NULL) && IsDevicePathEnd(RemainingDevicePath)) {\r
330 //\r
331 // The device has been started, directly return to fast boot.\r
332 //\r
333 Status = EFI_ALREADY_STARTED;\r
334 goto Error;\r
335 }\r
336\r
337 //\r
338 // Allocate buffer to store SD_DEVICE private data.\r
339 //\r
340 Device = AllocateCopyPool (sizeof (SD_DEVICE), &mSdDeviceTemplate);\r
341 if (Device == NULL) {\r
342 Status = EFI_OUT_OF_RESOURCES;\r
343 goto Error;\r
344 }\r
345\r
346 Device->DevicePath = NewDevicePath;\r
347 Device->Slot = Slot;\r
348 Device->Private = Private;\r
349 InitializeListHead (&Device->Queue);\r
350\r
351 //\r
352 // Expose user area in the Sd memory card to upper layer.\r
353 //\r
354 Status = DiscoverUserArea (Device);\r
355 if (EFI_ERROR(Status)) {\r
356 goto Error;\r
357 }\r
358\r
359 Device->ControllerNameTable = NULL;\r
360 AddUnicodeString2 (\r
361 "eng",\r
362 gSdDxeComponentName.SupportedLanguages,\r
363 &Device->ControllerNameTable,\r
364 Device->ModelName,\r
365 TRUE\r
366 );\r
367 AddUnicodeString2 (\r
368 "en",\r
369 gSdDxeComponentName.SupportedLanguages,\r
370 &Device->ControllerNameTable,\r
371 Device->ModelName,\r
372 FALSE\r
373 );\r
374\r
375 Status = gBS->InstallMultipleProtocolInterfaces (\r
376 &Device->Handle,\r
377 &gEfiDevicePathProtocolGuid,\r
378 Device->DevicePath,\r
379 &gEfiBlockIoProtocolGuid,\r
380 &Device->BlockIo,\r
381 &gEfiBlockIo2ProtocolGuid,\r
382 &Device->BlockIo2,\r
275d5136
FT
383 &gEfiEraseBlockProtocolGuid,\r
384 &Device->EraseBlock,\r
48555339
FT
385 NULL\r
386 );\r
387\r
388 if (!EFI_ERROR (Status)) {\r
389 gBS->OpenProtocol (\r
390 Private->Controller,\r
391 &gEfiSdMmcPassThruProtocolGuid,\r
392 (VOID **) &(Private->PassThru),\r
393 Private->DriverBindingHandle,\r
394 Device->Handle,\r
395 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
396 );\r
397 }\r
398\r
399Error:\r
400 FreePool (DevicePath);\r
401\r
402 if (EFI_ERROR (Status) && (NewDevicePath != NULL)) {\r
403 FreePool (NewDevicePath);\r
404 }\r
405\r
406 if (EFI_ERROR (Status) && (Device != NULL)) {\r
407 FreePool (Device);\r
408 }\r
409\r
410 return Status;\r
411}\r
412\r
413/**\r
414 Tests to see if this driver supports a given controller. If a child device is provided,\r
415 it further tests to see if this driver supports creating a handle for the specified child device.\r
416\r
417 This function checks to see if the driver specified by This supports the device specified by\r
418 ControllerHandle. Drivers will typically use the device path attached to\r
419 ControllerHandle and/or the services from the bus I/O abstraction attached to\r
420 ControllerHandle to determine if the driver supports ControllerHandle. This function\r
421 may be called many times during platform initialization. In order to reduce boot times, the tests\r
422 performed by this function must be very small, and take as little time as possible to execute. This\r
423 function must not change the state of any hardware devices, and this function must be aware that the\r
424 device specified by ControllerHandle may already be managed by the same driver or a\r
425 different driver. This function must match its calls to AllocatePages() with FreePages(),\r
426 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().\r
427 Since ControllerHandle may have been previously started by the same driver, if a protocol is\r
428 already in the opened state, then it must not be closed with CloseProtocol(). This is required\r
429 to guarantee the state of ControllerHandle is not modified by this function.\r
430\r
431 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
432 @param[in] ControllerHandle The handle of the controller to test. This handle\r
433 must support a protocol interface that supplies\r
434 an I/O abstraction to the driver.\r
435 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This\r
436 parameter is ignored by device drivers, and is optional for bus\r
437 drivers. For bus drivers, if this parameter is not NULL, then\r
438 the bus driver must determine if the bus controller specified\r
439 by ControllerHandle and the child controller specified\r
440 by RemainingDevicePath are both supported by this\r
441 bus driver.\r
442\r
443 @retval EFI_SUCCESS The device specified by ControllerHandle and\r
444 RemainingDevicePath is supported by the driver specified by This.\r
445 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and\r
446 RemainingDevicePath is already being managed by the driver\r
447 specified by This.\r
448 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and\r
449 RemainingDevicePath is already being managed by a different\r
450 driver or an application that requires exclusive access.\r
451 Currently not implemented.\r
452 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and\r
453 RemainingDevicePath is not supported by the driver specified by This.\r
454**/\r
455EFI_STATUS\r
456EFIAPI\r
457SdDxeDriverBindingSupported (\r
458 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
459 IN EFI_HANDLE Controller,\r
460 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
461 )\r
462{\r
463 EFI_STATUS Status;\r
464 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;\r
465 EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru;\r
466 UINT8 Slot;\r
467\r
468 //\r
469 // Test EFI_SD_MMC_PASS_THRU_PROTOCOL on the controller handle.\r
470 //\r
471 Status = gBS->OpenProtocol (\r
472 Controller,\r
473 &gEfiSdMmcPassThruProtocolGuid,\r
474 (VOID**) &PassThru,\r
475 This->DriverBindingHandle,\r
476 Controller,\r
477 EFI_OPEN_PROTOCOL_BY_DRIVER\r
478 );\r
479\r
480 if (Status == EFI_ALREADY_STARTED) {\r
481 return EFI_SUCCESS;\r
482 }\r
483\r
484 if (EFI_ERROR (Status)) {\r
485 return Status;\r
486 }\r
487\r
488 //\r
489 // Test RemainingDevicePath is valid or not.\r
490 //\r
491 if ((RemainingDevicePath != NULL) && !IsDevicePathEnd (RemainingDevicePath)) {\r
492 Status = PassThru->GetSlotNumber (PassThru, RemainingDevicePath, &Slot);\r
493 if (EFI_ERROR (Status)) {\r
494 //\r
495 // Close the I/O Abstraction(s) used to perform the supported test\r
496 //\r
497 gBS->CloseProtocol (\r
498 Controller,\r
499 &gEfiSdMmcPassThruProtocolGuid,\r
500 This->DriverBindingHandle,\r
501 Controller\r
502 );\r
503 return Status;\r
504 }\r
505 }\r
506\r
507 //\r
508 // Close the I/O Abstraction(s) used to perform the supported test\r
509 //\r
510 gBS->CloseProtocol (\r
511 Controller,\r
512 &gEfiSdMmcPassThruProtocolGuid,\r
513 This->DriverBindingHandle,\r
514 Controller\r
515 );\r
516\r
517 //\r
518 // Open the EFI Device Path protocol needed to perform the supported test\r
519 //\r
520 Status = gBS->OpenProtocol (\r
521 Controller,\r
522 &gEfiDevicePathProtocolGuid,\r
523 (VOID **) &ParentDevicePath,\r
524 This->DriverBindingHandle,\r
525 Controller,\r
526 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
527 );\r
528 return Status;\r
529}\r
530\r
531/**\r
532 Starts a device controller or a bus controller.\r
533\r
534 The Start() function is designed to be invoked from the EFI boot service ConnectController().\r
535 As a result, much of the error checking on the parameters to Start() has been moved into this\r
536 common boot service. It is legal to call Start() from other locations,\r
537 but the following calling restrictions must be followed or the system behavior will not be deterministic.\r
538 1. ControllerHandle must be a valid EFI_HANDLE.\r
539 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned\r
540 EFI_DEVICE_PATH_PROTOCOL.\r
541 3. Prior to calling Start(), the Supported() function for the driver specified by This must\r
542 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.\r
543\r
544 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
545 @param[in] ControllerHandle The handle of the controller to start. This handle\r
546 must support a protocol interface that supplies\r
547 an I/O abstraction to the driver.\r
548 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This\r
549 parameter is ignored by device drivers, and is optional for bus\r
550 drivers. For a bus driver, if this parameter is NULL, then handles\r
551 for all the children of Controller are created by this driver.\r
552 If this parameter is not NULL and the first Device Path Node is\r
553 not the End of Device Path Node, then only the handle for the\r
554 child device specified by the first Device Path Node of\r
555 RemainingDevicePath is created by this driver.\r
556 If the first Device Path Node of RemainingDevicePath is\r
557 the End of Device Path Node, no child handle is created by this\r
558 driver.\r
559\r
560 @retval EFI_SUCCESS The device was started.\r
561 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.\r
562 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
563 @retval Others The driver failded to start the device.\r
564\r
565**/\r
566EFI_STATUS\r
567EFIAPI\r
568SdDxeDriverBindingStart (\r
569 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
570 IN EFI_HANDLE Controller,\r
571 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
572 )\r
573{\r
574 EFI_STATUS Status;\r
575 EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru;\r
576 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;\r
577 SD_DRIVER_PRIVATE_DATA *Private;\r
578 UINT8 Slot;\r
579\r
580 Private = NULL;\r
581 PassThru = NULL;\r
582 Status = gBS->OpenProtocol (\r
583 Controller,\r
584 &gEfiSdMmcPassThruProtocolGuid,\r
585 (VOID **) &PassThru,\r
586 This->DriverBindingHandle,\r
587 Controller,\r
588 EFI_OPEN_PROTOCOL_BY_DRIVER\r
589 );\r
590 if ((EFI_ERROR (Status)) && (Status != EFI_ALREADY_STARTED)) {\r
591 return Status;\r
592 }\r
593\r
594 //\r
595 // Check EFI_ALREADY_STARTED to reuse the original SD_DRIVER_PRIVATE_DATA.\r
596 //\r
597 if (Status != EFI_ALREADY_STARTED) {\r
598 Private = AllocateZeroPool (sizeof (SD_DRIVER_PRIVATE_DATA));\r
599 if (Private == NULL) {\r
600 Status = EFI_OUT_OF_RESOURCES;\r
601 goto Error;\r
602 }\r
603\r
604 Status = gBS->OpenProtocol (\r
605 Controller,\r
606 &gEfiDevicePathProtocolGuid,\r
607 (VOID **) &ParentDevicePath,\r
608 This->DriverBindingHandle,\r
609 Controller,\r
610 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
611 );\r
612 ASSERT_EFI_ERROR (Status);\r
613 Private->PassThru = PassThru;\r
614 Private->Controller = Controller;\r
615 Private->ParentDevicePath = ParentDevicePath;\r
616 Private->DriverBindingHandle = This->DriverBindingHandle;\r
617\r
618 Status = gBS->InstallProtocolInterface (\r
619 &Controller,\r
620 &gEfiCallerIdGuid,\r
621 EFI_NATIVE_INTERFACE,\r
622 Private\r
623 );\r
624 if (EFI_ERROR (Status)) {\r
625 goto Error;\r
626 }\r
627 } else {\r
628 Status = gBS->OpenProtocol (\r
629 Controller,\r
630 &gEfiCallerIdGuid,\r
631 (VOID **) &Private,\r
632 This->DriverBindingHandle,\r
633 Controller,\r
634 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
635 );\r
636 if (EFI_ERROR (Status)) {\r
637 goto Error;\r
638 }\r
639 }\r
640\r
641 if (RemainingDevicePath == NULL) {\r
642 Slot = 0xFF;\r
643 while (TRUE) {\r
644 Status = PassThru->GetNextSlot (PassThru, &Slot);\r
645 if (EFI_ERROR (Status)) {\r
646 //\r
647 // Cannot find more legal slots.\r
648 //\r
649 Status = EFI_SUCCESS;\r
650 break;\r
651 }\r
652\r
653 Status = DiscoverSdDevice (Private, Slot);\r
654 if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) {\r
655 break;\r
656 }\r
657 }\r
658 } else if (!IsDevicePathEnd (RemainingDevicePath)) {\r
659 Status = PassThru->GetSlotNumber (PassThru, RemainingDevicePath, &Slot);\r
660 if (!EFI_ERROR (Status)) {\r
661 Status = DiscoverSdDevice (Private, Slot);\r
662 }\r
663 }\r
664\r
665Error:\r
666 if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) {\r
667 gBS->CloseProtocol (\r
668 Controller,\r
669 &gEfiSdMmcPassThruProtocolGuid,\r
670 This->DriverBindingHandle,\r
671 Controller\r
672 );\r
673\r
674 if (Private != NULL) {\r
675 gBS->UninstallMultipleProtocolInterfaces (\r
676 Controller,\r
677 &gEfiCallerIdGuid,\r
678 Private,\r
679 NULL\r
680 );\r
681 FreePool (Private);\r
682 }\r
683 }\r
684 return Status;\r
685}\r
686\r
687/**\r
688 Stops a device controller or a bus controller.\r
689\r
690 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().\r
691 As a result, much of the error checking on the parameters to Stop() has been moved\r
692 into this common boot service. It is legal to call Stop() from other locations,\r
693 but the following calling restrictions must be followed or the system behavior will not be deterministic.\r
694 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this\r
695 same driver's Start() function.\r
696 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid\r
697 EFI_HANDLE. In addition, all of these handles must have been created in this driver's\r
698 Start() function, and the Start() function must have called OpenProtocol() on\r
699 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.\r
700\r
701 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
702 @param[in] ControllerHandle A handle to the device being stopped. The handle must\r
703 support a bus specific I/O protocol for the driver\r
704 to use to stop the device.\r
705 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.\r
706 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL\r
707 if NumberOfChildren is 0.\r
708\r
709 @retval EFI_SUCCESS The device was stopped.\r
710 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.\r
711\r
712**/\r
713EFI_STATUS\r
714EFIAPI\r
715SdDxeDriverBindingStop (\r
716 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
717 IN EFI_HANDLE Controller,\r
718 IN UINTN NumberOfChildren,\r
719 IN EFI_HANDLE *ChildHandleBuffer\r
720 )\r
721{\r
722 EFI_STATUS Status;\r
723 BOOLEAN AllChildrenStopped;\r
724 UINTN Index;\r
725 SD_DRIVER_PRIVATE_DATA *Private;\r
726 SD_DEVICE *Device;\r
727 EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru;\r
728 EFI_BLOCK_IO2_PROTOCOL *BlockIo2;\r
729 EFI_BLOCK_IO_PROTOCOL *BlockIo;\r
730 LIST_ENTRY *Link;\r
731 LIST_ENTRY *NextLink;\r
732 SD_REQUEST *Request;\r
733 EFI_TPL OldTpl;\r
734\r
735 if (NumberOfChildren == 0) {\r
736 Status = gBS->OpenProtocol (\r
737 Controller,\r
738 &gEfiCallerIdGuid,\r
739 (VOID **) &Private,\r
740 This->DriverBindingHandle,\r
741 Controller,\r
742 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
743 );\r
744 if (EFI_ERROR (Status)) {\r
745 return EFI_DEVICE_ERROR;\r
746 }\r
747\r
748 gBS->UninstallProtocolInterface (\r
749 Controller,\r
750 &gEfiCallerIdGuid,\r
751 Private\r
752 );\r
753 gBS->CloseProtocol (\r
754 Controller,\r
755 &gEfiSdMmcPassThruProtocolGuid,\r
756 This->DriverBindingHandle,\r
757 Controller\r
758 );\r
759\r
760 FreePool (Private);\r
761\r
762 return EFI_SUCCESS;\r
763 }\r
764\r
765 AllChildrenStopped = TRUE;\r
766\r
767 for (Index = 0; Index < NumberOfChildren; Index++) {\r
768 BlockIo = NULL;\r
769 BlockIo2 = NULL;\r
770 Status = gBS->OpenProtocol (\r
771 ChildHandleBuffer[Index],\r
772 &gEfiBlockIoProtocolGuid,\r
773 (VOID **) &BlockIo,\r
774 This->DriverBindingHandle,\r
775 Controller,\r
776 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
777 );\r
778 if (EFI_ERROR (Status)) {\r
779 Status = gBS->OpenProtocol (\r
780 ChildHandleBuffer[Index],\r
781 &gEfiBlockIo2ProtocolGuid,\r
782 (VOID **) &BlockIo2,\r
783 This->DriverBindingHandle,\r
784 Controller,\r
785 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
786 );\r
787 if (EFI_ERROR (Status)) {\r
788 AllChildrenStopped = FALSE;\r
789 continue;\r
790 }\r
791 }\r
792\r
793 if (BlockIo != NULL) {\r
794 Device = SD_DEVICE_DATA_FROM_BLKIO (BlockIo);\r
795 } else {\r
796 ASSERT (BlockIo2 != NULL);\r
797 Device = SD_DEVICE_DATA_FROM_BLKIO2 (BlockIo2);\r
798 }\r
799\r
800 //\r
801 // Free all on-going async tasks.\r
802 //\r
803 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
804 for (Link = GetFirstNode (&Device->Queue);\r
805 !IsNull (&Device->Queue, Link);\r
806 Link = NextLink) {\r
807 NextLink = GetNextNode (&Device->Queue, Link);\r
808 RemoveEntryList (Link);\r
809\r
810 Request = SD_REQUEST_FROM_LINK (Link);\r
811\r
812 gBS->CloseEvent (Request->Event);\r
813 Request->Token->TransactionStatus = EFI_ABORTED;\r
814\r
815 if (Request->IsEnd) {\r
816 gBS->SignalEvent (Request->Token->Event);\r
817 }\r
818\r
819 FreePool (Request);\r
820 }\r
821 gBS->RestoreTPL (OldTpl);\r
822\r
823 //\r
824 // Close the child handle\r
825 //\r
826 Status = gBS->CloseProtocol (\r
827 Controller,\r
828 &gEfiSdMmcPassThruProtocolGuid,\r
829 This->DriverBindingHandle,\r
830 ChildHandleBuffer[Index]\r
831 );\r
832\r
833 Status = gBS->UninstallMultipleProtocolInterfaces (\r
834 ChildHandleBuffer[Index],\r
835 &gEfiDevicePathProtocolGuid,\r
836 Device->DevicePath,\r
837 &gEfiBlockIoProtocolGuid,\r
838 &Device->BlockIo,\r
839 &gEfiBlockIo2ProtocolGuid,\r
840 &Device->BlockIo2,\r
275d5136
FT
841 &gEfiEraseBlockProtocolGuid,\r
842 &Device->EraseBlock,\r
48555339
FT
843 NULL\r
844 );\r
845 if (EFI_ERROR (Status)) {\r
846 AllChildrenStopped = FALSE;\r
847 gBS->OpenProtocol (\r
848 Controller,\r
849 &gEfiSdMmcPassThruProtocolGuid,\r
850 (VOID **)&PassThru,\r
851 This->DriverBindingHandle,\r
852 ChildHandleBuffer[Index],\r
853 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
854 );\r
855 } else {\r
856 FreePool (Device->DevicePath);\r
857 FreeUnicodeStringTable (Device->ControllerNameTable);\r
858 FreePool (Device);\r
859 }\r
860 }\r
861\r
862 if (!AllChildrenStopped) {\r
863 return EFI_DEVICE_ERROR;\r
864 }\r
865\r
866 return EFI_SUCCESS;\r
867}\r
868\r
869/**\r
870 The user Entry Point for module SdDxe. The user code starts with this function.\r
871\r
872 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
873 @param[in] SystemTable A pointer to the EFI System Table.\r
874\r
875 @retval EFI_SUCCESS The entry point is executed successfully.\r
876 @retval other Some errors occur when executing this entry point.\r
877\r
878**/\r
879EFI_STATUS\r
880EFIAPI\r
881InitializeSdDxe (\r
882 IN EFI_HANDLE ImageHandle,\r
883 IN EFI_SYSTEM_TABLE *SystemTable\r
884 )\r
885{\r
886 EFI_STATUS Status;\r
887\r
888 //\r
889 // Install driver model protocol(s).\r
890 //\r
891 Status = EfiLibInstallDriverBindingComponentName2 (\r
892 ImageHandle,\r
893 SystemTable,\r
894 &gSdDxeDriverBinding,\r
895 ImageHandle,\r
896 &gSdDxeComponentName,\r
897 &gSdDxeComponentName2\r
898 );\r
899 ASSERT_EFI_ERROR (Status);\r
900\r
901 return Status;\r
902}\r
903\r