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