Update contrib.
1 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
16 #include "plat_priv.h"
21 const TInt KDiskSectorSize=512;
23 const TInt KTotalMDiskSize=0x100000; // 1MB (if changing this then also change CSD response)
25 // ======== error code conversion ========
27 GLDEF_C TInt MapLastErrorEpoc()
29 // map an Win32 error code to Epoc32 value
33 switch (GetLastError())
35 case ERROR_SHARING_VIOLATION : res=KErrAccessDenied; break;
36 case ERROR_LOCK_VIOLATION : res=KErrLocked; break;
37 case ERROR_FILE_NOT_FOUND: res=KErrNotFound; break;
38 case ERROR_PATH_NOT_FOUND: res=KErrPathNotFound; break;
39 case ERROR_ALREADY_EXISTS:
40 case ERROR_FILE_EXISTS:
41 res=KErrAlreadyExists;
43 case ERROR_NOT_READY: res=KErrNotReady; break;
44 case ERROR_UNRECOGNIZED_VOLUME:
45 case ERROR_NOT_DOS_DISK:
48 case ERROR_UNRECOGNIZED_MEDIA: res=KErrCorrupt; break;
49 case ERROR_INVALID_NAME: res=KErrBadName; break;
50 case ERROR_NO_MORE_FILES: res=KErrEof; break;
55 GLDEF_C TMMCErr MapLastErrorMmc()
57 // map Win32 error to a TMMCErr error.
60 DWORD r=GetLastError();
64 case ERROR_SHARING_VIOLATION:
65 case ERROR_LOCK_VIOLATION:
66 res=KMMCErrLocked; // KErrLocked
68 case ERROR_FILE_NOT_FOUND:
69 case ERROR_PATH_NOT_FOUND:
70 res=KMMCErrNotFound; // KErrNotFound
72 case ERROR_ALREADY_EXISTS:
73 case ERROR_FILE_EXISTS:
74 res=KMMCErrAlreadyExists; // KErrAlreadyExists
76 case ERROR_NOT_READY: res=KMMCErrNoCard; break;
77 case ERROR_UNRECOGNIZED_VOLUME:
78 case ERROR_NOT_DOS_DISK:
79 res=KMMCErrGeneral; // KErrGeneral
81 case ERROR_UNRECOGNIZED_MEDIA:
82 case ERROR_INVALID_NAME:
83 case ERROR_NO_MORE_FILES:
84 res=KMMCErrResponseCRC; // KErrCorrupt
90 // ======== DWinsSDStack ========
92 DWinsSDStack::DWinsSDStack(TInt aBus, DMMCSocket* aSocket)
93 : DSDStack(aBus, aSocket)
95 iAddressedCard=KBroadcastToAllCards;
96 // iCMD42Failed=EFalse;
100 TInt DWinsSDStack::Init()
102 // Allocate any resources. Only created once on kernel initialization so dont
103 // worry about cleanup if it leaves.
106 if((iCardArray = new TSDCardArray(this)) == NULL)
109 TInt r=DMMCStack::Init();
113 DMediaChangeBase* pMCBase = MMCSocket()->iMediaChange;
114 static_cast<DWinsMMCMediaChange*>(pMCBase)->SetStackP(this);
115 Wins::SetMediaChangeCallBackPtr(DWinsMMCMediaChange::MediaChangeCallBack, (TAny*)pMCBase);
118 // Over time memory can become fragmented, and so it is not possible to
119 // allocate physically contiguous pages. Therefore, the buffers for IO
120 // are allocated at startup.
122 // block and erase sector size characteristics depend on the specific
123 // card model, and so the initial values are estimates based on a typical
124 // card. If these do not match the actual card's block size (or erase
125 // size, for SD,) then the media driver just gets a reduced or increased
126 // buffer area, and its efficiency varies accordingly.
128 // For the WINS implementation, fragmentation does not matter because
129 // DMA is not used. The memory must still be allocated here so MEDMMC is
132 // The constant calculations could be folded, but this illustrates how the
133 // values are derived.
136 // MMC - values from Hitachi 16Mb card, datasheet HB288016MM1
138 // minor buffer must contain enough space for MBR or block
139 const TUint mmcBlkSzLog2 = 9; // READ_BLK_LEN and WRITE_BLK_LEN
140 const TUint mmcBlkSz = 1 << mmcBlkSzLog2;
141 const TInt mmcMinorBufLen = Max(KDiskSectorSize, mmcBlkSz);
143 const TInt KMinMMCBlocksInBuffer = 8;
144 const TInt mmcCchBufLen = KMinMMCBlocksInBuffer << mmcBlkSzLog2;
146 const TInt mmcTotalBufLen = mmcMinorBufLen + mmcCchBufLen;
148 // SDCard - values from 64Mb Panasonic RP-SD064
150 const TUint sdBlkSzLog2 = 9; // READ_BL_LEN and WRITE_BLK_LEN
151 const TUint sdBlkSz = 1 << sdBlkSzLog2;
152 const TInt sdMinorBufLen = Max(KDiskSectorSize, sdBlkSz);
154 const TUint ss = 0x1f; // SECTOR_SIZE, add 1 for sector count
155 const TInt KMinSDBlocksInBuffer = 8;
156 const TInt sdCchBufLen = Max(KMinSDBlocksInBuffer, ss + 1) << sdBlkSzLog2;
158 const TInt sdTotalBufLen = sdMinorBufLen + sdCchBufLen;
160 const TInt totalBufLen = Max(mmcTotalBufLen, sdTotalBufLen);
162 iMDBuf = reinterpret_cast<TUint8*>(Kern::Alloc(totalBufLen));
163 iMDBufLen = totalBufLen;
165 // initialize each card on the stack
167 for (i = 0; i < KTotalWinsCards; ++i)
169 TInt r = SetupSimulatedCard(i);
174 // initialize pointers to currently present cards
176 // Slot zero can toggle between no card; card 0 and card 1. The current state is
177 // determined by *Wins::CurrentPBusDevicePtr() and toggled by pressing F4 when F5
178 // (door open) is held down. Because this function is only executed at startup,
179 // assume start with card zero.
180 iCardInfo[0] = iCardPool[0];
181 for (i = 1; i < KTotalWinsCardSlots; ++i)
183 iCardInfo[i]=iCardPool[i+1];
189 void DWinsSDStack::MachineInfo(TMMCMachineInfo& aMachineInfo)
191 aMachineInfo.iTotalSockets=KTotalWinsCardSlots;
192 aMachineInfo.iTotalMediaChanges=0; // Not used at present
193 aMachineInfo.iTotalPrimarySupplies=0; // Not used at present
195 aMachineInfo.iSPIMode=EFalse;
196 aMachineInfo.iBaseBusNumber=0;
198 __ASSERT_DEBUG(aMachineInfo.iTotalSockets<=KMaxMMCardsPerStack,
199 DWinsSDStack::Panic(DWinsSDStack::EWinsMMCBadMachineInfo));
202 void DWinsSDStack::AdjustPartialRead(
204 const TMMCard* aCard,
206 const TMMCard* /*aCard*/,
208 TUint32 aStart, TUint32 aEnd, TUint32* aPhysStart, TUint32* aPhysEnd) const
211 const TUint32 blkLen = aCard->CSD().ReadBlockLength();
212 const TUint32 blkMsk = blkLen - 1;
214 __ASSERT_DEBUG(aCard->CSD().ReadBlPartial(), Panic(EWinsMMCAPRNotSupp));
215 __ASSERT_DEBUG(aEnd - aStart <= blkLen, Panic(EWinsMMCAPRRange));
216 __ASSERT_DEBUG((aEnd & ~blkMsk) > (aStart & ~blkMsk), Panic(EWinsMMCAPRBoundary));
219 *aPhysStart = aStart & ~0x3;
220 *aPhysEnd = (aEnd + 0x3) & ~0x3;
223 void DWinsSDStack::GetBufferInfo(TUint8** aMDBuf, TInt* aMDBufLen)
226 *aMDBufLen = iMDBufLen;
229 void DWinsSDStack::Panic(TWinsMMCPanic aPanic)
231 _LIT(KPncNm,"PBUS-MMCSD-WINS");
232 Kern::PanicCurrentThread(KPncNm,aPanic);
235 TInt DWinsSDStack::SetupSimulatedCard(TInt aCardNum)
237 // allocate individual card with Win32 file. Only called at bootup, so no cleanup if fails.
240 TWinsCardInfo* cip = new TWinsCardInfo;
244 TUint8 cid[KMMCCIDLength];
248 cid[3] = TUint8('0' + aCardNum);
250 for (j = 4; j < KMMCCIDLength - 1; ++j)
252 cid[KMMCCIDLength - 1] = '#'; // '#' = 0x23, bit zero must be 1
255 cip->iPWD = new TMediaPassword;
262 // cards in slot zero are SD
266 cip->iIsSDCard = ETrue;
271 cip->iIsSDCard = EFalse;
275 cip->iState=ECardStateIdle;
277 for (TInt area = 0; area < mediaAreas; ++area)
279 TInt r = CreateBinFileForCard(aCardNum, area, &cip->iAreaHandles[area]);
283 iCardPool[aCardNum]=cip;
287 TInt DWinsSDStack::CreateBinFileForCard(TInt aCardNum, TInt aAreaNum, HANDLE* aHandle)
289 // create .bin file in temp directory to contain media area of card.
292 const char* emulatorPath = Property::GetString("EmulatorMediaPath");
293 if (!Emulator::CreateAllDirectories(emulatorPath))
294 return Emulator::LastError();
296 TBuf8<KMaxFileName> fn8(_L8(emulatorPath));
297 fn8.Append(_L8("MMCCRD"));
298 fn8.AppendNum(aCardNum);
299 fn8.Append('A'+aAreaNum);
300 fn8.Append(_L8(".BIN"));
303 *aHandle = CreateFileA(
304 (LPCSTR) fn8.Ptr(), // LPCSTR lpFileName,
305 GENERIC_READ | GENERIC_WRITE, // DWORD dwDesiredAccess
306 FILE_SHARE_READ | FILE_SHARE_WRITE, // DWORD dwShareMode
307 NULL, // LPSECURITY_ATTRIBUTES lpSecurityAttributes
308 OPEN_ALWAYS, // DWORD dwCreationDisposition
309 FILE_FLAG_RANDOM_ACCESS, // DWORD dwFlagsAndAttributes
310 NULL); // HANDLE hTemplateFile
312 if (*aHandle == INVALID_HANDLE_VALUE)
313 return MapLastErrorEpoc();
315 if ( SetFilePointer(*aHandle, KTotalMDiskSize, NULL, FILE_BEGIN) == 0xffffffffu
316 || ! SetEndOfFile(*aHandle) )
318 CloseHandle(*aHandle);
319 return MapLastErrorEpoc();
325 void DWinsSDStack::SetBusConfigDefaults(TMMCBusConfig& aConfig, TUint aClock)
327 const TUint KWinsMaxHwInterfaceClk=104000;
328 const TUint KWinsResponseTimeOut=6400;
329 const TUint KWinsDataTimeOut=40000;
330 const TUint KWinsBusyTimeOut=200000;
332 aConfig.iBusClock = (aClock > KWinsMaxHwInterfaceClk) ? KWinsMaxHwInterfaceClk : aClock;
333 aConfig.iResponseTimeOut=KWinsResponseTimeOut;
334 aConfig.iDataTimeOut=KWinsDataTimeOut;
335 aConfig.iBusyTimeOut=KWinsBusyTimeOut;
338 void DWinsSDStack::InitClockOff()
343 void DWinsSDStack::ASSPReset()
348 void DWinsSDStack::ASSPDisengage()
353 void DWinsSDStack::DoPowerDown()
359 LOCAL_C TInt SetMediaPasswordEnvironmentVar(TInt aSocketNum,TInt aCardNum,const TDesC8& aPasswd)
361 // Set the password for local drive 'aLocalDrive', card number 'aCardNum' to 'aPasswd' - as an
362 // environment variable. Note that the card number is only relevant where the emulated drive
363 // supports card hot-swapping (i.e. F4 whilst F5 is held down).
366 // Setup the appropriate environment variable string '_EPOC_LocDrv_<locDrvNum>_PWORD_<cardNum>'
367 TUint16 envVar[]=L"_EPOC_Socket_X_PWORD_Y";
369 envVar[13]=(TUint16)('0'+aSocketNum);
370 envVar[21]=(TUint16)('0'+aCardNum);
372 // Setup the new value of the environment variable
374 TInt len=aPasswd.Length();
376 // the password may be empty if a card's password is cleared
378 return(KErrArgument);
379 memcpy(&envVal[0],reinterpret_cast<const TUint16 *>(aPasswd.Ptr()),len);
382 // Now set the new value for the environment variable
383 if (SetEnvironmentVariable(envVar,&envVal[0]))
389 LOCAL_C TInt MediaPasswordEnvironmentVar(TInt aSocketNum,TInt aCardNum,TDes8& aPasswd)
391 // Get the password for local drive 'aLocalDrive', card number 'aCardNum' into 'aPasswd' - from
392 // an environment variable. Note that the card number is only relevant where the emulated drive
393 // supports card hot-swapping (i.e. F4 whilst F5 is held down).
396 TUint16 envVar[]=L"_EPOC_Socket_X_PWORD_Y";
398 envVar[13]=(TUint16)('0'+aSocketNum);
399 envVar[21]=(TUint16)('0'+aCardNum);
401 TUint16 envVal[100]; // To hold the value of the retreived environment variable
403 DWORD len=GetEnvironmentVariable(envVar,&envVal[0],100);
408 // Found the requested environment variable so there is a password for this local drive / card.
409 if ((len<<1)<=KMaxMediaPassword)
411 aPasswd.FillZ(KMaxMediaPassword);
413 aPasswd.Copy(reinterpret_cast<TUint8*>(&envVal[0]),len<<1);
420 return(KErrNotFound);
423 TMMCErr DWinsSDStack::DoPowerUpSM()
433 if(MMCSocket()->iVcc->SetState(EPsuOnCurLimit) != KErrNone)
434 return KMMCErrHardware;
436 for (TInt i = 0; i < KTotalWinsCardSlots; ++i)
438 // if card has a password, it will be locked on power up
439 TInt cardNum = (i==0) ? *Wins::CurrentPBusDevicePtr() : i;
441 && MediaPasswordEnvironmentVar(
442 MMCSocket()->iSocketNumber, cardNum, *(iCardInfo[i]->iPWD))
445 iCardInfo[i]->iIsLocked = (iCardInfo[i]->iPWD->Length() > 0);
448 iCardInfo[i]->iIsLocked=EFalse;
450 iCardInfo[i]->iState = ECardStateIdle;
451 iCardInfo[i]->iRCA=0x0001; // Default RCA - spec 2.2, s4.2.1, 5.4
459 TMMCErr DWinsSDStack::InitClockOnSM()
471 void DWinsSDStack::AddressCard(TInt aCardNumber)
473 iAddressedCard = aCardNumber;
477 TInt DWinsSDStack::GetTargetSlotNumber(const TRCA& anRCA)
479 // when the controller is given a command with an embedded RCA, this function
480 // works out which physical card slot it corresponds to. If no card has been
481 // assigned the RCA then it returns -1.
486 for (TInt i = 0; i < KTotalWinsCardSlots; ++i)
488 if (iCardInfo[i]->iRCA==anRCA)
498 TMMCErr DWinsSDStack::IssueMMCCommandSM()
506 TMMCCommandDesc& cmd = Command();
508 // If the command contains an embedded RCA then extract it
511 if (/*cmd.iCommand == ECmdSetRelativeAddr || */cmd.iCommand == ECmdSelectCard
512 || cmd.iCommand == ECmdSendCSD || cmd.iCommand == ECmdSendCID
513 || cmd.iCommand == ECmdSendStatus || cmd.iCommand == ECmdGoInactiveState
514 || cmd.iCommand == ECmdFastIO || cmd.iCommand == ECmdAppCmd )
516 if ((cmd.iArgument >> 16) != 0)
519 tgtRCA=TUint16(cmd.iArgument >> 16);
523 // if the card contains an embedded RCA, work out which slot it corresponds to.
524 // At the end of the function, this card is used to generate the R1 response.
525 // Assume that if rca is supplied it either corresponds to the selected card or
526 // broadcast mode is on. (An exception is CMD7 with arg0 to deselect all cards.)
528 TInt targetCard = supRCA ? GetTargetSlotNumber(tgtRCA) : iAddressedCard;
529 TBool rto = EFalse; // response timeout
531 // if try to access card zero has been set to holding no card via F5 / F4 then timeout.
532 if ((targetCard == 0) && *Wins::CurrentPBusDevicePtr() < 0)
533 return KMMCErrResponseTimeOut;
537 // CMD42 is a data transfer command. That means the R1 response that it returns
538 // immediately is the state it is in on receiving the data block, and not after
539 // processing it. If the data block is invalid then LOCK_UNLOCK_FAILED will be
540 // set in the R1 response which is sent in reply to the next command.
542 TBool nextCMD42Failed = EFalse;
543 TBool lock_unlock_failed=EFalse;
545 // When the card is locked, it will only respond to basic command class (0) and
546 // lock card command class (7). An exception is CMD16. This is sent before CMD42,
547 // but is classified (MMC Spec 23.2, table 5) as belonging to classes 2 and 4.
548 // For data transfer commands, LOCK_UNLOCK_FAIL is set in response to the following
550 TMMCCommandEnum origCmd = cmd.iCommand;
552 // if targetting locked card...
553 if (targetCard != KBroadcastToAllCards && iCardInfo[targetCard]->iIsLocked)
555 // ...and not command used in init or CMD42 sequence...
556 if (!( ((cmd.iSpec.iCommandClass & (KMMCCmdClassApplication | KMMCCmdClassBasic | KMMCCmdClassLockCard)) != 0)
557 || (cmd.iCommand == ECmdSetBlockLen) || (cmd.iCommand == ECmdAppCmd) ))
559 lock_unlock_failed = ETrue;
560 cmd.iCommand = (TMMCCommandEnum) -1; // skip case processing
566 switch (cmd.iCommand)
568 case ECmdGoIdleState: // CMD0
569 if (iAddressedCard != KBroadcastToAllCards)
570 iCardInfo[iAddressedCard]->iState = ECardStateIdle;
573 for (TInt i = 0; i < KTotalWinsCardSlots; ++i)
574 iCardInfo[i]->iState = ECardStateIdle;
579 case ECmdSendOpCond: // CMD1
581 if (iAddressedCard != KBroadcastToAllCards)
582 iCardInfo[iAddressedCard]->iState = ECardStateReady;
585 for (TInt i = 0; i < KTotalWinsCardSlots; ++i)
586 iCardInfo[i]->iState = ECardStateReady;
589 // bit31 is set to indicate cards are not still powering up
590 TUint32 r3 = KMMCWinsCardOCRValue | KMMCOCRBusy;
591 TMMC::BigEndian4Bytes(cmd.iResponse, r3);
595 case ECmdAllSendCID: // CMD2
598 if (iAddressedCard != KBroadcastToAllCards)
600 idx = iAddressedCard;
602 iCardInfo[iAddressedCard]->iState == ECardStateReady,
603 DWinsSDStack::Panic(DWinsSDStack::EStkIMCBadStateCmd2));
606 idx = FindAnyCardInStack(ECardStateReady);
612 iCardInfo[idx]->iCID.Copy(cmd.iResponse);
613 iCardInfo[idx]->iState = ECardStateIdent;
618 case ECmdSetRelativeAddr: // CMD3
621 if (iAddressedCard != KBroadcastToAllCards)
624 iCardInfo[iAddressedCard]->iState == ECardStateIdent,
625 DWinsSDStack::Panic(DWinsSDStack::EStkIMCBadStateCmd3));
627 if (iCardInfo[iAddressedCard]->iIsSDCard)
629 static TUint16 RCACounter = 0x1234;
630 // SD Cards publish RCAs
632 iCardInfo[iAddressedCard]->iRCA = RCACounter;
633 iCardInfo[iAddressedCard]->iState = ECardStateStby;
634 TUint32 r6 = TUint32(RCACounter) << 16;
635 TMMC::BigEndian4Bytes(&cmd.iResponse[0],r6); // Ignore bits 47-40
639 iCardInfo[iAddressedCard]->iRCA = TUint16(cmd.iArgument >> 16);
640 iCardInfo[iAddressedCard]->iState=ECardStateStby;
645 // MultiMediaCards are assigned RCAs
646 idx = FindOneCardInStack(ECardStateIdent);
647 iCardInfo[iAddressedCard]->iRCA = TUint16(cmd.iArgument >> 16);
648 iCardInfo[iAddressedCard]->iState=ECardStateStby;
649 targetCard = iAddressedCard;
655 // if ACMD6 then change bus width
656 if (cmd.iSpec.iCommandClass == KMMCCmdClassApplication)
658 switch (cmd.iArgument)
661 iCardInfo[iAddressedCard]->iBusWidth = 1;
664 iCardInfo[iAddressedCard]->iBusWidth = 4;
667 DWinsSDStack::Panic(DWinsSDStack::EStkIMCCmd6InvalidWidth);
673 case ECmdSelectCard: // CMD7
675 // switch to broadcast mode so the currently selected and new cards
676 // receive the command simultaneously.
678 TInt idx = FindAnyCardInStack(ECardStateTran);
680 iCardInfo[idx]->iState = ECardStateStby;
681 if ((iAddressedCard=targetCard) == KBroadcastToAllCards)
685 iCardInfo[targetCard]->iState = ECardStateTran;
686 targetCard = targetCard;
692 // R1 response so status return as for any other R1 command.
693 if (cmd.iSpec.iCommandClass == KMMCCmdClassApplication)
696 iCardInfo[targetCard]->iIsSDCard,
697 DWinsSDStack::Panic(DWinsSDStack::EStkICMACMD13NotSD));
699 memset(cmd.iDataMemoryP, 0, KSDStatusBlockLength);
700 if (iCardInfo[targetCard]->iBusWidth == 1)
701 cmd.iDataMemoryP[0] = 0x00 << 6;
702 else // if (iCardInfo[targetCard]->iBusWidth == 4)
703 cmd.iDataMemoryP[0] = 0x02 << 6;
704 cmd.iDataMemoryP[7] = 0x28; // PROTECTED_AREA_SIZE
708 case ECmdReadSingleBlock:
709 case ECmdReadMultipleBlock:
711 winHandle=iCardInfo[targetCard]->iAreaHandles[KSDUserArea];
713 if ( cmd.iSpec.iUseStopTransmission && cmd.iBlockLength >= cmd.iTotalLength)
714 return( KMMCErrNotSupported );
717 TInt pos = cmd.iArgument;
718 if (SetFilePointer(winHandle,pos,NULL,FILE_BEGIN)==0xffffffffu)
719 err=MapLastErrorMmc();
723 TInt len = cmd.iTotalLength;
724 if (ReadFile(winHandle,(TAny*)cmd.iDataMemoryP,len,&res,NULL)==FALSE)
725 err=MapLastErrorMmc();
726 else if (res!=(DWORD)len)
731 if (err!=KMMCErrNone)
737 if (cmd.iSpec.iCommandClass == KMMCCmdClassApplication)
739 TMMC::BigEndian4Bytes(cmd.iResponse, iMBWOKBlocks);
742 // ------------------------------------------------------------------
744 case ECmdWriteMultipleBlock:
748 // periodically fail multi-block writes to test ACMD22 error recovery
749 if (cmd.iCommand != ECmdWriteMultipleBlock)
750 writeLen = cmd.iTotalLength;
753 const TInt KMaxFailCnt = 4;
754 static TInt failCnt = 0;
755 const TInt KMaxFailBlock = 4;
756 static TInt failBlocks = 0;
758 failCnt = (failCnt + 1) % KMaxFailCnt;
760 writeLen = cmd.iTotalLength;
763 failBlocks = (failBlocks + 1) % KMaxFailBlock;
765 // fail at least one block
766 TInt totalBlocks = cmd.iTotalLength / cmd.iBlockLength;
767 TInt blocksToFail = Min(failBlocks + 1, totalBlocks); // fail at least one block
768 iMBWOKBlocks = (totalBlocks - blocksToFail);
769 writeLen = iMBWOKBlocks * cmd.iBlockLength;
771 return KMMCErrDataTimeOut;
775 HANDLE h=iCardInfo[targetCard]->iAreaHandles[KSDUserArea];
778 TInt pos = cmd.iArgument;
779 if (SetFilePointer(h, pos, NULL, FILE_BEGIN)==0xffffffffu)
780 err = MapLastErrorMmc();
784 if (! WriteFile(h, (LPCVOID)cmd.iDataMemoryP,writeLen,&res,NULL))
785 err=MapLastErrorMmc();
786 else if (res!=(DWORD)writeLen)
792 if (err!=KMMCErrNone)
794 if (writeLen != cmd.iTotalLength)
795 return KMMCErrDataTimeOut;
800 // targetCard == -1 when ACMD41 being sent because not yet supplied
801 if (iAddressedCard != KBroadcastToAllCards)
803 // timeout if addressed card is not SD
804 if (! iCardInfo[iAddressedCard]->iIsSDCard)
809 // request sent to specific non-SD card
810 if (targetCard != -1 && ! iCardInfo[targetCard]->iIsSDCard)
817 iCardInfo[targetCard]->GetCSD(cmd.iResponse);
821 // ------------------------------------------------------------------
823 // in EPOC, Lock() does not actually lock the card. It just sets the
824 // password. This means that the card is still accessible to the user,
825 // but must be unlocked the next time it is powered up.
827 // a real card will transiently go into rcv and prg state while processing
828 // this command. When finished, it will fall back into tran state.
829 // The R1 response is sent immediately after CMD42. CIMReadWriteBlocksSM()
830 // sends CMD13 to find out whether or not LOCK_UNLOCK_FAIL was set.
832 // the asserts in this case protect against invalid data being sent from the
833 // media driver. A real card would fail these corrupt data blocks.
836 const TInt8 cmd_byte(*cmd.iDataMemoryP);
837 __ASSERT_DEBUG( // ensure not CLR_PWD && SET_PWD
838 !((cmd_byte & KMMCLockUnlockClrPwd) && (cmd_byte & KMMCLockUnlockSetPwd)),
839 DWinsSDStack::Panic(DWinsSDStack::EWinsMMCCorruptCommand) );
841 __ASSERT_DEBUG( // not actually lock a card while setting the password
842 ((cmd_byte & (KMMCLockUnlockLockUnlock | KMMCLockUnlockSetPwd)) != (KMMCLockUnlockLockUnlock | KMMCLockUnlockSetPwd)),
843 DWinsSDStack::Panic(DWinsSDStack::EWinsMMCLockAttempt) );
845 if (cmd_byte & KMMCLockUnlockErase) // ERASE (not supported)
846 return KMMCErrNotSupported;
848 const TInt8 pwd_len = *(cmd.iDataMemoryP + 1);
849 const TPtrC8 pwd(cmd.iDataMemoryP + 2, pwd_len);
851 if ((cmd_byte & KMMCLockUnlockClrPwd) != 0) // CLR_PWD == 1
854 pwd_len >= 0 && pwd_len <= KMaxMediaPassword,
855 DWinsSDStack::Panic(DWinsSDStack::EWinsMMCCorruptCommand));
857 if (iCardInfo[targetCard]->iIsLocked) // clear when locked
858 nextCMD42Failed = ETrue;
859 else // clear when unlocked
861 if (iCardInfo[targetCard]->iPWD->Compare(pwd) != 0) // clear when unlocked with wrong password
863 nextCMD42Failed = ETrue;
864 lock_unlock_failed = ETrue;
866 else // clear when unlocked with right password
868 // Clear from password store
869 iCardInfo[targetCard]->iPWD->Zero();
870 iCardInfo[targetCard]->iIsLocked = EFalse;
871 nextCMD42Failed = EFalse;
873 // Clear from environment settings
874 TInt cardNum=(targetCard==0) ? *Wins::CurrentPBusDevicePtr() : targetCard; // Can't be -1 at this stage
875 SetMediaPasswordEnvironmentVar(MMCSocket()->iSocketNumber,cardNum,*(iCardInfo[targetCard]->iPWD));
879 else if ((cmd_byte & KMMCLockUnlockSetPwd) == 0) // SET_PWD == 0: unlock
882 pwd_len >= 0 && pwd_len <= KMaxMediaPassword,
883 DWinsSDStack::Panic(DWinsSDStack::EWinsMMCCorruptCommand) );
885 if (! iCardInfo[targetCard]->iIsLocked) // unlock when unlocked
886 nextCMD42Failed = ETrue;
889 if (iCardInfo[targetCard]->iPWD->Compare(pwd) != 0) // unlock when locked with wrong password
891 nextCMD42Failed = ETrue;
892 lock_unlock_failed = ETrue;
894 else // unlock when locked with right password
896 iCardInfo[targetCard]->iIsLocked = EFalse;
897 nextCMD42Failed = EFalse;
901 else if ((cmd_byte & KMMCLockUnlockSetPwd) == KMMCLockUnlockSetPwd) // SET_PWD == 1
904 cmd_byte & KMMCLockUnlockSetPwd,
905 DWinsSDStack::Panic(DWinsSDStack::EWinsMMCCorruptCommand) );
907 // if pwd_len < iCardInfo[targetCard]->iPWD->Length() then data block must be invalid.
908 // This can be caused by bad user input rather than inaccurate formation.
909 if (!( pwd_len >= iCardInfo[targetCard]->iPWD->Length()
910 && pwd_len <= iCardInfo[targetCard]->iPWD->Length() + KMaxMediaPassword ))
912 nextCMD42Failed = ETrue;
916 const TInt old_pwd_len = iCardInfo[targetCard]->iPWD->Length();
917 TPtrC8 old_pwd(cmd.iDataMemoryP + 2, old_pwd_len);
918 TPtrC8 new_pwd(cmd.iDataMemoryP + 2 + old_pwd_len, pwd_len - old_pwd_len);
920 // card must not be locked and supplied current password must be correct
921 if (iCardInfo[targetCard]->iIsLocked || iCardInfo[targetCard]->iPWD->Compare(old_pwd) != 0)
923 nextCMD42Failed = ETrue;
924 lock_unlock_failed = ETrue;
928 // Set in password store
929 iCardInfo[targetCard]->iPWD->Copy(new_pwd);
930 nextCMD42Failed = EFalse;
932 // Set in environment settings
933 TInt cardNum=(targetCard==0) ? *Wins::CurrentPBusDevicePtr() : targetCard; // Can't be -1 at this stage
934 SetMediaPasswordEnvironmentVar(MMCSocket()->iSocketNumber,cardNum,*(iCardInfo[targetCard]->iPWD));
938 else if ((cmd_byte & KMMCLockUnlockLockUnlock) == KMMCLockUnlockLockUnlock)
941 pwd_len >= 0 && pwd_len <= KMaxMediaPassword,
942 DWinsSDStack::Panic(DWinsSDStack::EWinsMMCCorruptCommand) );
944 if (iCardInfo[targetCard]->iIsLocked) // lock when locked
945 nextCMD42Failed = ETrue;
948 if (iCardInfo[targetCard]->iPWD->Compare(pwd) != 0) // lock with wrong password
950 nextCMD42Failed = ETrue;
951 lock_unlock_failed = ETrue;
953 else // lock with right password
955 iCardInfo[targetCard]->iIsLocked = ETrue;
956 nextCMD42Failed = EFalse;
962 __ASSERT_DEBUG(EFalse, DWinsSDStack::Panic(DWinsSDStack::EWinsMMCLockAttempt) );
964 } // case ECmdLockUnlock
965 // ------------------------------------------------------------------
971 return(KMMCErrResponseTimeOut);
973 cmd.iCommand = origCmd;
974 // If this is an R1 or R1b response type command then return card status as a response
975 if ( targetCard != -1
976 && (cmd.iSpec.iResponseType==ERespTypeR1 || cmd.iSpec.iResponseType==ERespTypeR1B) )
979 iCardInfo[targetCard]->iState
980 | ((iCardInfo[targetCard]->iIsLocked ? 1 : 0) << 25)
981 | ((lock_unlock_failed ? 1 : 0) << 24) );
983 if (iCMD42Failed) // previous CMD42
985 resp |= KMMCStatErrLockUnlock;
986 nextCMD42Failed = EFalse;
988 iCMD42Failed = nextCMD42Failed;
989 TMMC::BigEndian4Bytes(&cmd.iResponse[0],resp); // Ignore bits 47-40
994 TMMCErr DWinsSDStack::ModifyCardCapabilitySM()
996 // This function provides a chance to modify the capability of paticular cards.
997 // Licensee may overide this function to modify certain card's capability as needed.
998 // A state machine is needed in derived function and function of base class should be
999 // called in order to act more generic behaviour.
1011 SMF_INVOKES( DSDStack::BaseModifyCardCapabilitySMST, EStDone )
1018 TInt DWinsSDStack::FindAnyCardInStack(TMMCardStateEnum aState)
1020 // first first active card in supplied state. Return -1 if
1021 // no active card is in supplied state.
1024 if (iAddressedCard != KBroadcastToAllCards)
1025 return (iCardInfo[iAddressedCard]->iState == aState) ? iAddressedCard : -1;
1028 for (TInt i = 0; i < KTotalWinsCardSlots; ++i)
1030 if (iCardInfo[i]->iState == aState)
1038 TInt DWinsSDStack::FindFirstCardInStack(TMMCardStateEnum aState)
1040 // find card which is active on bus and in supplied state.
1041 // There can be more than one active card in the the supplied state,
1042 // but there should be at least one.
1045 if (iAddressedCard != KBroadcastToAllCards)
1047 __ASSERT_DEBUG(iCardInfo[iAddressedCard]->iState == aState, DWinsSDStack::Panic(DWinsSDStack::EStkFFCNotSelCard));
1048 return iAddressedCard;
1053 for (TInt i = 0; idx != -1 && i < KTotalWinsCardSlots; ++i)
1055 if (iCardInfo[i]->iState == aState)
1059 __ASSERT_DEBUG(idx != -1, DWinsSDStack::Panic(DWinsSDStack::EStkFFCNoneSel));
1064 TInt DWinsSDStack::FindOneCardInStack(TMMCardStateEnum aState)
1066 // find card which is active on bus and in supplied state.
1067 // There should be exactly one active card in the supplied state.
1070 if (iAddressedCard != KBroadcastToAllCards)
1072 __ASSERT_DEBUG(iCardInfo[iAddressedCard]->iState == aState, DWinsSDStack::Panic(DWinsSDStack::EStkFOCNotSelCard));
1073 return iAddressedCard;
1078 for (TInt i = 0; i < KTotalWinsCardSlots; ++i)
1080 if (iCardInfo[i]->iState == aState)
1082 __ASSERT_DEBUG(idx == -1, DWinsSDStack::Panic(DWinsSDStack::EStkFOCMultiSel));
1087 __ASSERT_DEBUG(idx != -1, DWinsSDStack::Panic(DWinsSDStack::EStkFOCNoneSel));
1093 // ======== DWinsMMCMediaChange ========
1095 #pragma warning( disable : 4355 ) // this used in initializer list
1096 DWinsMMCMediaChange::DWinsMMCMediaChange(TInt aMediaChangeNum)
1097 : DMMCMediaChange(aMediaChangeNum),
1098 iDoorClosedCount(0),
1099 iMediaChangeEnable(ETrue),
1102 iMediaDoorCloseReload=2; // Units: In theory-20ms, Actual-100ms
1104 #pragma warning( default : 4355 )
1106 TInt DWinsMMCMediaChange::Create()
1111 return(DMediaChangeBase::Create());
1114 void DWinsMMCMediaChange::DoorOpenService()
1116 // Handle the media change (this function, never postponed is called on media
1117 // change interrupt).
1120 Disable(); // Disable interrupt until door closes again.
1121 iDoorOpenDfc.Enque();
1124 void DWinsMMCMediaChange::DoDoorOpen()
1126 // Handle media door open (called on media door open interrupt).
1129 iDoorClosedCount=iMediaDoorCloseReload;
1130 // Just start a ticklink to poll for door closing
1131 iTickLink.Periodic(KMediaChangeTickInterval,DWinsMMCMediaChange::Tick,this);
1134 void DWinsMMCMediaChange::DoDoorClosed()
1136 // Handle media door closing (called on media door open interrupt).
1140 iTickLink.Cancel(); // Doesn't matter if wasn't enabled
1141 Enable(); // Re-enable door interrupts
1143 // While the door was open the user may have changed the card in slot 0
1144 if (iStackP && *Wins::CurrentPBusDevicePtr()>=0)
1145 iStackP->iCardInfo[0]=iStackP->iCardPool[*Wins::CurrentPBusDevicePtr()];
1148 void DWinsMMCMediaChange::ForceMediaChange()
1150 // Force media change
1156 TMediaState DWinsMMCMediaChange::MediaState()
1158 // Return status of media changed signal.
1162 if (iDoorClosedCount>0)
1164 return( (*Wins::MediaDoorOpenPtr())?EDoorOpen:EDoorClosed);
1167 void DWinsMMCMediaChange::Tick(TAny *aPtr)
1169 // Called on the tick to poll for door closing (called on DFC).
1173 ((DWinsMMCMediaChange*)aPtr)->TickService();
1176 void DWinsMMCMediaChange::TickService()
1178 // Called on the tick to poll for door closing (called on DFC).
1182 __ASSERT_DEBUG(iDoorClosedCount>=0,DWinsSDStack::Panic(DWinsSDStack::EWinsMMCMediaChangeTickFault));
1183 if (!(*Wins::MediaDoorOpenPtr()))
1185 if (iDoorClosedCount > 0)
1187 if (iDoorClosedCount == 0)
1188 DoorClosedService();
1191 iDoorClosedCount=iMediaDoorCloseReload; // Door open so start again.
1194 void DWinsMMCMediaChange::Enable()
1196 // Enable media change
1200 iMediaChangeEnable=ETrue;
1203 void DWinsMMCMediaChange::Disable()
1205 // Disable media change
1209 iMediaChangeEnable=EFalse;
1212 void DWinsMMCMediaChange::MediaChangeCallBack(TAny *aPtr)
1214 // Static called on media change
1218 DWinsMMCMediaChange* mc=(DWinsMMCMediaChange*)aPtr;
1219 if (mc!=NULL&&mc->iMediaChangeEnable)
1220 mc->DoorOpenService();
1224 // ======== TWinsCardInfo ========
1226 void TWinsCardInfo::GetCSD(TUint8* aResp) const
1229 TUint32 csd=(0x1<<30); /* CSD_STRUCTURE: CSD Version No 1.1 */
1230 csd|= (0x2<<26); /* SPEC_VERS: Version 2.1 */
1231 csd|= (0x0E<<16); /* TAAC: 1mS */
1232 csd|= (0x0A<<8); /* NSAC: 1000 */
1233 csd|= (0x59); /* TRAN_SPEED: 5.0Mbit/s */
1234 TMMC::BigEndian4Bytes(&aResp[0],csd);
1237 KMMCCmdClassBasic | KMMCCmdClassBlockRead
1238 | KMMCCmdClassBlockWrite | KMMCCmdClassLockCard;
1239 csd= (ccc<<20); /* CCC: classes 0, 2, 4, and 7 */
1240 csd|= (0x9<<16); /* READ_BL_LEN: 512 bytes */
1241 csd|= (0x0<<15); /* READ_BL_PARTIAL: No */
1242 csd|= (0x0<<14); /* WRITE_BLK_MISALIGN: No */
1243 csd|= (0x0<<13); /* READ_BLK_MISALIGN: No */
1244 csd|= (0x0<<12); /* DSR_IMP: No DSR */
1245 csd|= (0x0<<8); /* C_SIZE: 1Mb */
1246 csd|= (0x7F); /* C_SIZE: 1Mb (cont)*/
1247 TMMC::BigEndian4Bytes(&aResp[4],csd);
1249 csd= (3UL<<30); /* C_SIZE: 2Mb (cont) */
1250 csd|= (0x1<<27); /* VDD_R_CURR_MIN: 1mA */
1251 csd|= (0x1<<24); /* VDD_R_CURR_MAX: 5mA */
1252 csd|= (0x2<<21); /* VDD_W_CURR_MIN: 5mA */
1253 csd|= (0x3<<18); /* VDD_W_CURR_MAX: 25mA */
1254 csd|= (0x0<<15); /* C_SIZE_MULT: 0 */
1257 csd|= (0x0<<10); /* SECTOR_SIZE: 1 write block */
1258 csd|= (0x0<<5); /* ERASE_GRP_SIZE: 1 sector */
1259 csd|= (0x0); /* WP_GRP_SIZE: 1 erase group */
1263 csd |= (0x00 << (46 - 32)); // ERASE_BLK_EN
1264 csd |= (0x1f << (39 - 32)); // SECTOR_SIZE: 32 write blocks
1265 csd |= (0x00 << (32 - 32)); // WP_GRP_SIZE: 1 erase sector.
1267 TMMC::BigEndian4Bytes(&aResp[8],csd);
1269 csd= (0x0<<31); /* WP_GRP_ENABLE: No */
1270 csd|= (0x0<<29); /* DEFAULT_ECC: ? */
1271 csd|= (0x3<<26); /* R2W_FACTOR: 8 */
1272 csd|= (0x9<<22); /* WRITE_BL_LEN: 512 bytes */
1273 csd|= (0x0<<21); /* WRITE_BL_PARTIAL: No */
1274 csd|= (0x0<<15); /* FILE_FORMAT_GRP: Hard disk */
1275 csd|= (0x0<<14); /* COPY: original */
1276 csd|= (0x0<<13); /* PERM_WRITE_PROTECT: No */
1277 csd|= (0x0<<12); /* TMP_WRITE_PROTECT: No */
1278 csd|= (0x0<<10); /* FILE_FORMAT: Hard disk */
1279 csd|= (0x0<<8); /* ECC: None */
1280 csd|= (0x0<<1); /* CRC: ? */
1281 csd|= (0x1); /* not used */
1282 TMMC::BigEndian4Bytes(&aResp[12],csd);
1285 // ======== DWinsMMCPsu ========
1288 DWinsMMCPsu::DWinsMMCPsu(TInt aVccNum, TInt aMcId)
1289 : DMMCPsu(aVccNum, aMcId)
1292 void DWinsMMCPsu::Init()
1294 // Initialise the PSU
1300 void DWinsMMCPsu::DoSetState(TPBusPsuState aState)
1302 // Turn on/off the PSU. If it is possible to adjust the output voltage on this
1303 // PSU then retreive the required voltage level from TMMCPsu::iVoltageSetting
1304 // (which is in OCR register format).
1314 case EPsuOnCurLimit:
1319 TInt DWinsMMCPsu::VoltageInMilliVolts()
1321 // Return the level of the PSU (in mV) or -ve if error.
1328 void DWinsMMCPsu::DoCheckVoltage()
1330 // Check the voltage level of the PSU is as expected. Returns either KErrNone, KErrGeneral
1331 // to indicate the pass/fail state or KErrNotReady if the voltage check isn't complete.
1335 ReceiveVoltageCheckResult(KErrNone);
1338 void DWinsMMCPsu::PsuInfo(TPBusPsuInfo &anInfo)
1340 // Return machine info relating to the MMC PSU supply
1344 anInfo.iVoltageSupported=0x00040000; // 3.0V (OCR reg. format).
1345 anInfo.iMaxCurrentInMicroAmps=0;
1346 anInfo.iVoltCheckInterval=0;
1347 anInfo.iVoltCheckMethod=EPsuChkComparator;
1348 anInfo.iNotLockedTimeOut=0; // Not enabled
1349 anInfo.iInactivityTimeOut=5; // 5 Seconds