First public contribution.
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 // ======== DWinsCPRMStack ========
92 DWinsCPRMStack::DWinsCPRMStack(TInt aBus, DMMCSocket* aSocket)
93 : DCPRMStack(aBus, aSocket)
95 iAddressedCard=KBroadcastToAllCards;
96 // iCMD42Failed=EFalse;
100 TInt DWinsCPRMStack::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=DCPRMStack::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 DWinsCPRMStack::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 DWinsCPRMStack::Panic(DWinsCPRMStack::EWinsMMCBadMachineInfo));
202 void DWinsCPRMStack::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 DWinsCPRMStack::GetBufferInfo(TUint8** aMDBuf, TInt* aMDBufLen)
226 *aMDBufLen = iMDBufLen;
229 void DWinsCPRMStack::Panic(TWinsMMCPanic aPanic)
231 _LIT(KPncNm,"PBUS-MMCSD-WINS");
232 Kern::PanicCurrentThread(KPncNm,aPanic);
235 TInt DWinsCPRMStack::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 DWinsCPRMStack::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 DWinsCPRMStack::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 DWinsCPRMStack::InitClockOff()
343 void DWinsCPRMStack::ASSPReset()
348 void DWinsCPRMStack::ASSPDisengage()
353 void DWinsCPRMStack::DoPowerDown()
358 LOCAL_C TInt SetMediaPasswordEnvironmentVar(TInt aSocketNum,TInt aCardNum,const TDesC8& aPasswd)
360 // Set the password for local drive 'aLocalDrive', card number 'aCardNum' to 'aPasswd' - as an
361 // environment variable. Note that the card number is only relevant where the emulated drive
362 // supports card hot-swapping (i.e. F4 whilst F5 is held down).
365 // Setup the appropriate environment variable string '_EPOC_LocDrv_<locDrvNum>_PWORD_<cardNum>'
366 TUint16 envVar[]=L"_EPOC_Socket_X_PWORD_Y";
368 envVar[13]=(TUint16)('0'+aSocketNum);
369 envVar[21]=(TUint16)('0'+aCardNum);
371 // Setup the new value of the environment variable
373 TInt len=aPasswd.Length();
375 // the password may be empty if a card's password is cleared
377 return(KErrArgument);
378 memcpy(&envVal[0],reinterpret_cast<const TUint16 *>(aPasswd.Ptr()),len);
381 // Now set the new value for the environment variable
382 if (SetEnvironmentVariable(envVar,&envVal[0]))
388 LOCAL_C TInt MediaPasswordEnvironmentVar(TInt aSocketNum,TInt aCardNum,TDes8& aPasswd)
390 // Get the password for local drive 'aLocalDrive', card number 'aCardNum' into 'aPasswd' - from
391 // an environment variable. Note that the card number is only relevant where the emulated drive
392 // supports card hot-swapping (i.e. F4 whilst F5 is held down).
395 TUint16 envVar[]=L"_EPOC_Socket_X_PWORD_Y";
397 envVar[13]=(TUint16)('0'+aSocketNum);
398 envVar[21]=(TUint16)('0'+aCardNum);
400 TUint16 envVal[100]; // To hold the value of the retreived environment variable
402 DWORD len=GetEnvironmentVariable(envVar,&envVal[0],100);
407 // Found the requested environment variable so there is a password for this local drive / card.
408 if ((len<<1)<=KMaxMediaPassword)
410 aPasswd.FillZ(KMaxMediaPassword);
412 aPasswd.Copy(reinterpret_cast<TUint8*>(&envVal[0]),len<<1);
419 return(KErrNotFound);
422 TMMCErr DWinsCPRMStack::DoPowerUpSM()
432 if(MMCSocket()->iVcc->SetState(EPsuOnCurLimit) != KErrNone)
433 return KMMCErrHardware;
435 for (TInt i = 0; i < KTotalWinsCardSlots; ++i)
437 // if card has a password, it will be locked on power up
438 TInt cardNum = (i==0) ? *Wins::CurrentPBusDevicePtr() : i + 1;
440 && MediaPasswordEnvironmentVar(
441 MMCSocket()->iSocketNumber, cardNum, *(iCardInfo[i]->iPWD))
444 iCardInfo[i]->iIsLocked = (iCardInfo[i]->iPWD->Length() > 0);
447 iCardInfo[i]->iIsLocked=EFalse;
449 iCardInfo[i]->iState = ECardStateIdle;
450 iCardInfo[i]->iRCA=0x0001; // Default RCA - spec 2.2, s4.2.1, 5.4
458 TMMCErr DWinsCPRMStack::InitClockOnSM()
470 void DWinsCPRMStack::AddressCard(TInt aCardNumber)
472 iAddressedCard = aCardNumber;
476 TInt DWinsCPRMStack::GetTargetSlotNumber(const TRCA& anRCA)
478 // when the controller is given a command with an embedded RCA, this function
479 // works out which physical card slot it corresponds to. If no card has been
480 // assigned the RCA then it returns -1.
485 for (TInt i = 0; i < KTotalWinsCardSlots; ++i)
487 if (iCardInfo[i]->iRCA==anRCA)
497 TMMCErr DWinsCPRMStack::IssueMMCCommandSM()
505 TMMCCommandDesc& cmd = Command();
507 // If the command contains an embedded RCA then extract it
510 if (/*cmd.iCommand == ECmdSetRelativeAddr || */cmd.iCommand == ECmdSelectCard
511 || cmd.iCommand == ECmdSendCSD || cmd.iCommand == ECmdSendCID
512 || cmd.iCommand == ECmdSendStatus || cmd.iCommand == ECmdGoInactiveState
513 || cmd.iCommand == ECmdFastIO || cmd.iCommand == ECmdAppCmd )
515 if ((cmd.iArgument >> 16) != 0)
518 tgtRCA=TUint16(cmd.iArgument >> 16);
522 // if the card contains an embedded RCA, work out which slot it corresponds to.
523 // At the end of the function, this card is used to generate the R1 response.
524 // Assume that if rca is supplied it either corresponds to the selected card or
525 // broadcast mode is on. (An exception is CMD7 with arg0 to deselect all cards.)
527 TInt targetCard = supRCA ? GetTargetSlotNumber(tgtRCA) : iAddressedCard;
528 TBool rto = EFalse; // response timeout
530 // if try to access card zero has been set to holding no card via F5 / F4 then timeout.
531 if ((targetCard == 0) && *Wins::CurrentPBusDevicePtr() < 0)
532 return KMMCErrResponseTimeOut;
536 // CMD42 is a data transfer command. That means the R1 response that it returns
537 // immediately is the state it is in on receiving the data block, and not after
538 // processing it. If the data block is invalid then LOCK_UNLOCK_FAILED will be
539 // set in the R1 response which is sent in reply to the next command.
541 TBool nextCMD42Failed = EFalse;
542 TBool lock_unlock_failed=EFalse;
544 // When the card is locked, it will only respond to basic command class (0) and
545 // lock card command class (7). An exception is CMD16. This is sent before CMD42,
546 // but is classified (MMC Spec 23.2, table 5) as belonging to classes 2 and 4.
547 // For data transfer commands, LOCK_UNLOCK_FAIL is set in response to the following
549 TMMCCommandEnum origCmd = cmd.iCommand;
551 // if targetting locked card...
552 if (targetCard != KBroadcastToAllCards && iCardInfo[targetCard]->iIsLocked)
554 // ...and not command used in init or CMD42 sequence...
555 if (!( ((cmd.iSpec.iCommandClass & (KMMCCmdClassApplication | KMMCCmdClassBasic | KMMCCmdClassLockCard)) != 0)
556 || (cmd.iCommand == ECmdSetBlockLen) || (cmd.iCommand == ECmdAppCmd) ))
558 lock_unlock_failed = ETrue;
559 cmd.iCommand = (TMMCCommandEnum) -1; // skip case processing
565 switch (cmd.iCommand)
567 case ECmdGoIdleState: // CMD0
568 if (iAddressedCard != KBroadcastToAllCards)
569 iCardInfo[iAddressedCard]->iState = ECardStateIdle;
572 for (TInt i = 0; i < KTotalWinsCardSlots; ++i)
573 iCardInfo[i]->iState = ECardStateIdle;
578 case ECmdSendOpCond: // CMD1
580 if (iAddressedCard != KBroadcastToAllCards)
581 iCardInfo[iAddressedCard]->iState = ECardStateReady;
584 for (TInt i = 0; i < KTotalWinsCardSlots; ++i)
585 iCardInfo[i]->iState = ECardStateReady;
588 // bit31 is set to indicate cards are not still powering up
589 TUint32 r3 = KMMCWinsCardOCRValue | KMMCOCRBusy;
590 TMMC::BigEndian4Bytes(cmd.iResponse, r3);
594 case ECmdAllSendCID: // CMD2
597 if (iAddressedCard != KBroadcastToAllCards)
599 idx = iAddressedCard;
601 iCardInfo[iAddressedCard]->iState == ECardStateReady,
602 DWinsCPRMStack::Panic(DWinsCPRMStack::EStkIMCBadStateCmd2));
605 idx = FindAnyCardInStack(ECardStateReady);
611 iCardInfo[idx]->iCID.Copy(cmd.iResponse);
612 iCardInfo[idx]->iState = ECardStateIdent;
617 case ECmdSetRelativeAddr: // CMD3
620 if (iAddressedCard != KBroadcastToAllCards)
623 iCardInfo[iAddressedCard]->iState == ECardStateIdent,
624 DWinsCPRMStack::Panic(DWinsCPRMStack::EStkIMCBadStateCmd3));
626 if (iCardInfo[iAddressedCard]->iIsSDCard)
628 static TUint16 RCACounter = 0x1234;
629 // SD Cards publish RCAs
631 iCardInfo[iAddressedCard]->iRCA = RCACounter;
632 iCardInfo[iAddressedCard]->iState = ECardStateStby;
633 TUint32 r6 = TUint32(RCACounter) << 16;
634 TMMC::BigEndian4Bytes(&cmd.iResponse[0],r6); // Ignore bits 47-40
638 iCardInfo[iAddressedCard]->iRCA = TUint16(cmd.iArgument >> 16);
639 iCardInfo[iAddressedCard]->iState=ECardStateStby;
644 // MultiMediaCards are assigned RCAs
645 idx = FindOneCardInStack(ECardStateIdent);
646 iCardInfo[iAddressedCard]->iRCA = TUint16(cmd.iArgument >> 16);
647 iCardInfo[iAddressedCard]->iState=ECardStateStby;
648 targetCard = iAddressedCard;
654 // if ACMD6 then change bus width
655 if (cmd.iSpec.iCommandClass == KMMCCmdClassApplication)
657 switch (cmd.iArgument)
660 iCardInfo[iAddressedCard]->iBusWidth = 1;
663 iCardInfo[iAddressedCard]->iBusWidth = 4;
666 DWinsCPRMStack::Panic(DWinsCPRMStack::EStkIMCCmd6InvalidWidth);
672 case ECmdSelectCard: // CMD7
674 // switch to broadcast mode so the currently selected and new cards
675 // receive the command simultaneously.
677 TInt idx = FindAnyCardInStack(ECardStateTran);
679 iCardInfo[idx]->iState = ECardStateStby;
680 if ((iAddressedCard=targetCard) == KBroadcastToAllCards)
684 iCardInfo[targetCard]->iState = ECardStateTran;
685 targetCard = targetCard;
691 // R1 response so status return as for any other R1 command.
692 if (cmd.iSpec.iCommandClass == KMMCCmdClassApplication)
695 iCardInfo[targetCard]->iIsSDCard,
696 DWinsCPRMStack::Panic(DWinsCPRMStack::EStkICMACMD13NotSD));
698 memset(cmd.iDataMemoryP, 0, KSDStatusBlockLength);
699 if (iCardInfo[targetCard]->iBusWidth == 1)
700 cmd.iDataMemoryP[0] = 0x00 << 6;
701 else // if (iCardInfo[targetCard]->iBusWidth == 4)
702 cmd.iDataMemoryP[0] = 0x02 << 6;
703 cmd.iDataMemoryP[7] = 0x28; // PROTECTED_AREA_SIZE
707 case ECmdReadSingleBlock:
708 case ECmdReadMultipleBlock:
710 winHandle=iCardInfo[targetCard]->iAreaHandles[KSDUserArea];
712 if ( cmd.iSpec.iUseStopTransmission && cmd.iBlockLength >= cmd.iTotalLength)
713 return( KMMCErrNotSupported );
716 TInt pos = cmd.iArgument;
717 if (SetFilePointer(winHandle,pos,NULL,FILE_BEGIN)==0xffffffffu)
718 err=MapLastErrorMmc();
722 TInt len = cmd.iTotalLength;
723 if (ReadFile(winHandle,(TAny*)cmd.iDataMemoryP,len,&res,NULL)==FALSE)
724 err=MapLastErrorMmc();
725 else if (res!=(DWORD)len)
730 if (err!=KMMCErrNone)
736 if (cmd.iSpec.iCommandClass == KMMCCmdClassApplication)
738 TMMC::BigEndian4Bytes(cmd.iResponse, iMBWOKBlocks);
741 // ------------------------------------------------------------------
743 case ECmdWriteMultipleBlock:
747 // periodically fail multi-block writes to test ACMD22 error recovery
748 if (cmd.iCommand != ECmdWriteMultipleBlock)
749 writeLen = cmd.iTotalLength;
752 const TInt KMaxFailCnt = 4;
753 static TInt failCnt = 0;
754 const TInt KMaxFailBlock = 4;
755 static TInt failBlocks = 0;
757 failCnt = (failCnt + 1) % KMaxFailCnt;
759 writeLen = cmd.iTotalLength;
762 failBlocks = (failBlocks + 1) % KMaxFailBlock;
764 // fail at least one block
765 TInt totalBlocks = cmd.iTotalLength / cmd.iBlockLength;
766 TInt blocksToFail = Min(failBlocks + 1, totalBlocks); // fail at least one block
767 iMBWOKBlocks = (totalBlocks - blocksToFail);
768 writeLen = iMBWOKBlocks * cmd.iBlockLength;
770 return KMMCErrDataTimeOut;
774 HANDLE h=iCardInfo[targetCard]->iAreaHandles[KSDUserArea];
777 TInt pos = cmd.iArgument;
778 if (SetFilePointer(h, pos, NULL, FILE_BEGIN)==0xffffffffu)
779 err = MapLastErrorMmc();
783 if (! WriteFile(h, (LPCVOID)cmd.iDataMemoryP,writeLen,&res,NULL))
784 err=MapLastErrorMmc();
785 else if (res!=(DWORD)writeLen)
791 if (err!=KMMCErrNone)
793 if (writeLen != cmd.iTotalLength)
794 return KMMCErrDataTimeOut;
799 // targetCard == -1 when ACMD41 being sent because not yet supplied
800 if (iAddressedCard != KBroadcastToAllCards)
802 // timeout if addressed card is not SD
803 if (! iCardInfo[iAddressedCard]->iIsSDCard)
808 // request sent to specific non-SD card
809 if (targetCard != -1 && ! iCardInfo[targetCard]->iIsSDCard)
816 iCardInfo[targetCard]->GetCSD(cmd.iResponse);
820 // ------------------------------------------------------------------
822 // in EPOC, Lock() does not actually lock the card. It just sets the
823 // password. This means that the card is still accessible to the user,
824 // but must be unlocked the next time it is powered up.
826 // a real card will transiently go into rcv and prg state while processing
827 // this command. When finished, it will fall back into tran state.
828 // The R1 response is sent immediately after CMD42. CIMReadWriteBlocksSM()
829 // sends CMD13 to find out whether or not LOCK_UNLOCK_FAIL was set.
831 // the asserts in this case protect against invalid data being sent from the
832 // media driver. A real card would fail these corrupt data blocks.
835 const TInt8 cmd_byte(*cmd.iDataMemoryP);
836 __ASSERT_DEBUG( // ensure not CLR_PWD && SET_PWD
837 !((cmd_byte & KMMCLockUnlockClrPwd) && (cmd_byte & KMMCLockUnlockSetPwd)),
838 DWinsCPRMStack::Panic(DWinsCPRMStack::EWinsMMCCorruptCommand) );
840 __ASSERT_DEBUG( // not actually lock a card
841 !(cmd_byte & KMMCLockUnlockLockUnlock),
842 DWinsCPRMStack::Panic(DWinsCPRMStack::EWinsMMCLockAttempt) );
844 if (cmd_byte & KMMCLockUnlockErase) // ERASE (not supported)
845 return KMMCErrNotSupported;
847 const TInt8 pwd_len = *(cmd.iDataMemoryP + 1);
848 const TPtrC8 pwd(cmd.iDataMemoryP + 2, pwd_len);
850 if ((cmd_byte & KMMCLockUnlockClrPwd) != 0) // CLR_PWD == 1
853 pwd_len >= 0 && pwd_len <= KMaxMediaPassword,
854 DWinsCPRMStack::Panic(DWinsCPRMStack::EWinsMMCCorruptCommand));
856 if (iCardInfo[targetCard]->iIsLocked) // clear when locked
857 nextCMD42Failed = ETrue;
858 else // clear when unlocked
860 if (iCardInfo[targetCard]->iPWD->Compare(pwd) != 0) // clear when unlocked with wrong password
861 nextCMD42Failed = ETrue;
862 else // clear when unlocked with right password
864 // Clear from password store
865 iCardInfo[targetCard]->iPWD->Zero();
866 iCardInfo[targetCard]->iIsLocked = EFalse;
867 nextCMD42Failed = EFalse;
869 // Clear from environment settings
870 TInt cardNum=(targetCard==0) ? *Wins::CurrentPBusDevicePtr() : 0; // Can't be -1 at this stage
871 SetMediaPasswordEnvironmentVar(MMCSocket()->iSocketNumber,cardNum,*(iCardInfo[targetCard]->iPWD));
875 else if ((cmd_byte & KMMCLockUnlockSetPwd) == 0) // SET_PWD == 0: unlock
878 pwd_len >= 0 && pwd_len <= KMaxMediaPassword,
879 DWinsCPRMStack::Panic(DWinsCPRMStack::EWinsMMCCorruptCommand) );
881 if (! iCardInfo[targetCard]->iIsLocked) // unlock when unlocked
882 nextCMD42Failed = ETrue;
885 if (iCardInfo[targetCard]->iPWD->Compare(pwd) != 0) // unlock when locked with wrong password
886 nextCMD42Failed = ETrue;
887 else // unlock when locked with right password
889 iCardInfo[targetCard]->iIsLocked = EFalse;
890 nextCMD42Failed = EFalse;
894 else /* ((cmd_byte & KMMCLockUnlockSetPwd) != 0) */ // SET_PWD == 1
897 cmd_byte & KMMCLockUnlockSetPwd,
898 DWinsCPRMStack::Panic(DWinsCPRMStack::EWinsMMCCorruptCommand) );
900 // if pwd_len < iCardInfo[targetCard]->iPWD->Length() then data block must be invalid.
901 // This can be caused by bad user input rather than inaccurate formation.
902 if (!( pwd_len >= iCardInfo[targetCard]->iPWD->Length()
903 && pwd_len <= iCardInfo[targetCard]->iPWD->Length() + KMaxMediaPassword ))
905 nextCMD42Failed = ETrue;
909 const TInt old_pwd_len = iCardInfo[targetCard]->iPWD->Length();
910 TPtrC8 old_pwd(cmd.iDataMemoryP + 2, old_pwd_len);
911 TPtrC8 new_pwd(cmd.iDataMemoryP + 2 + old_pwd_len, pwd_len - old_pwd_len);
913 // card must not be locked and supplied current password must be correct
914 if (iCardInfo[targetCard]->iIsLocked || iCardInfo[targetCard]->iPWD->Compare(old_pwd) != 0)
915 nextCMD42Failed = ETrue;
918 // Set in password store
919 iCardInfo[targetCard]->iPWD->Copy(new_pwd);
920 nextCMD42Failed = EFalse;
922 // Set in environment settings
923 TInt cardNum=(targetCard==0) ? *Wins::CurrentPBusDevicePtr() : 0; // Can't be -1 at this stage
924 SetMediaPasswordEnvironmentVar(MMCSocket()->iSocketNumber,cardNum,*(iCardInfo[targetCard]->iPWD));
927 } // else /* ((cmd_byte & KMMCLockUnlockSetPwd) != 0) */
928 } // case ECmdLockUnlock
929 // ------------------------------------------------------------------
935 return(KMMCErrResponseTimeOut);
937 cmd.iCommand = origCmd;
938 // If this is an R1 or R1b response type command then return card status as a response
939 if ( targetCard != -1
940 && (cmd.iSpec.iResponseType==ERespTypeR1 || cmd.iSpec.iResponseType==ERespTypeR1B) )
943 iCardInfo[targetCard]->iState
944 | ((iCardInfo[targetCard]->iIsLocked ? 1 : 0) << 25)
945 | ((lock_unlock_failed ? 1 : 0) << 24) );
947 if (iCMD42Failed) // previous CMD42
949 resp |= KMMCStatErrLockUnlock;
950 nextCMD42Failed = EFalse;
952 iCMD42Failed = nextCMD42Failed;
953 TMMC::BigEndian4Bytes(&cmd.iResponse[0],resp); // Ignore bits 47-40
958 TInt DWinsCPRMStack::FindAnyCardInStack(TMMCardStateEnum aState)
960 // first first active card in supplied state. Return -1 if
961 // no active card is in supplied state.
964 if (iAddressedCard != KBroadcastToAllCards)
965 return (iCardInfo[iAddressedCard]->iState == aState) ? iAddressedCard : -1;
968 for (TInt i = 0; i < KTotalWinsCardSlots; ++i)
970 if (iCardInfo[i]->iState == aState)
978 TInt DWinsCPRMStack::FindFirstCardInStack(TMMCardStateEnum aState)
980 // find card which is active on bus and in supplied state.
981 // There can be more than one active card in the the supplied state,
982 // but there should be at least one.
985 if (iAddressedCard != KBroadcastToAllCards)
987 __ASSERT_DEBUG(iCardInfo[iAddressedCard]->iState == aState, DWinsCPRMStack::Panic(DWinsCPRMStack::EStkFFCNotSelCard));
988 return iAddressedCard;
993 for (TInt i = 0; idx != -1 && i < KTotalWinsCardSlots; ++i)
995 if (iCardInfo[i]->iState == aState)
999 __ASSERT_DEBUG(idx != -1, DWinsCPRMStack::Panic(DWinsCPRMStack::EStkFFCNoneSel));
1004 TInt DWinsCPRMStack::FindOneCardInStack(TMMCardStateEnum aState)
1006 // find card which is active on bus and in supplied state.
1007 // There should be exactly one active card in the supplied state.
1010 if (iAddressedCard != KBroadcastToAllCards)
1012 __ASSERT_DEBUG(iCardInfo[iAddressedCard]->iState == aState, DWinsCPRMStack::Panic(DWinsCPRMStack::EStkFOCNotSelCard));
1013 return iAddressedCard;
1018 for (TInt i = 0; i < KTotalWinsCardSlots; ++i)
1020 if (iCardInfo[i]->iState == aState)
1022 __ASSERT_DEBUG(idx == -1, DWinsCPRMStack::Panic(DWinsCPRMStack::EStkFOCMultiSel));
1027 __ASSERT_DEBUG(idx != -1, DWinsCPRMStack::Panic(DWinsCPRMStack::EStkFOCNoneSel));
1032 TMMCErr DWinsCPRMStack::CIMCalculateMediaKeySM()
1034 // Calculate the Media Key record.
1045 return( KMMCErrNotSupported );
1050 TMMCErr DWinsCPRMStack::SetSecureCommandArgumentSM()
1052 // Auxilary state machine function called during a secure read/write blocks operation.
1053 // Perform random number generation and setting the secure command argument.
1064 return( KMMCErrNotSupported );
1070 // ======== DWinsMMCMediaChange ========
1072 #pragma warning( disable : 4355 ) // this used in initializer list
1073 DWinsMMCMediaChange::DWinsMMCMediaChange(TInt aMediaChangeNum)
1074 : DMMCMediaChange(aMediaChangeNum),
1075 iDoorClosedCount(0),
1076 iMediaChangeEnable(ETrue),
1079 iMediaDoorCloseReload=2; // Units: In theory-20ms, Actual-100ms
1081 #pragma warning( default : 4355 )
1083 TInt DWinsMMCMediaChange::Create()
1088 return(DMediaChangeBase::Create());
1091 void DWinsMMCMediaChange::DoorOpenService()
1093 // Handle the media change (this function, never postponed is called on media
1094 // change interrupt).
1097 Disable(); // Disable interrupt until door closes again.
1098 iDoorOpenDfc.Enque();
1101 void DWinsMMCMediaChange::DoDoorOpen()
1103 // Handle media door open (called on media door open interrupt).
1106 iDoorClosedCount=iMediaDoorCloseReload;
1107 // Just start a ticklink to poll for door closing
1108 iTickLink.Periodic(KMediaChangeTickInterval,DWinsMMCMediaChange::Tick,this);
1111 void DWinsMMCMediaChange::DoDoorClosed()
1113 // Handle media door closing (called on media door open interrupt).
1117 iTickLink.Cancel(); // Doesn't matter if wasn't enabled
1118 Enable(); // Re-enable door interrupts
1120 // While the door was open the user may have changed the card in slot 0
1121 if (iStackP && *Wins::CurrentPBusDevicePtr()>=0)
1122 iStackP->iCardInfo[0]=iStackP->iCardPool[*Wins::CurrentPBusDevicePtr()];
1125 void DWinsMMCMediaChange::ForceMediaChange()
1127 // Force media change
1133 TMediaState DWinsMMCMediaChange::MediaState()
1135 // Return status of media changed signal.
1139 if (iDoorClosedCount>0)
1141 return( (*Wins::MediaDoorOpenPtr())?EDoorOpen:EDoorClosed);
1144 void DWinsMMCMediaChange::Tick(TAny *aPtr)
1146 // Called on the tick to poll for door closing (called on DFC).
1150 ((DWinsMMCMediaChange*)aPtr)->TickService();
1153 void DWinsMMCMediaChange::TickService()
1155 // Called on the tick to poll for door closing (called on DFC).
1159 __ASSERT_DEBUG(iDoorClosedCount>=0,DWinsCPRMStack::Panic(DWinsCPRMStack::EWinsMMCMediaChangeTickFault));
1160 if (!(*Wins::MediaDoorOpenPtr()))
1162 if (iDoorClosedCount > 0)
1164 if (iDoorClosedCount == 0)
1165 DoorClosedService();
1168 iDoorClosedCount=iMediaDoorCloseReload; // Door open so start again.
1171 void DWinsMMCMediaChange::Enable()
1173 // Enable media change
1177 iMediaChangeEnable=ETrue;
1180 void DWinsMMCMediaChange::Disable()
1182 // Disable media change
1186 iMediaChangeEnable=EFalse;
1189 void DWinsMMCMediaChange::MediaChangeCallBack(TAny *aPtr)
1191 // Static called on media change
1195 DWinsMMCMediaChange* mc=(DWinsMMCMediaChange*)aPtr;
1196 if (mc!=NULL&&mc->iMediaChangeEnable)
1197 mc->DoorOpenService();
1201 // ======== TWinsCardInfo ========
1203 void TWinsCardInfo::GetCSD(TUint8* aResp) const
1206 TUint32 csd=(0x1<<30); /* CSD_STRUCTURE: CSD Version No 1.1 */
1207 csd|= (0x2<<26); /* SPEC_VERS: Version 2.1 */
1208 csd|= (0x0E<<16); /* TAAC: 1mS */
1209 csd|= (0x0A<<8); /* NSAC: 1000 */
1210 csd|= (0x59); /* TRAN_SPEED: 5.0Mbit/s */
1211 TMMC::BigEndian4Bytes(&aResp[0],csd);
1214 KMMCCmdClassBasic | KMMCCmdClassBlockRead
1215 | KMMCCmdClassBlockWrite | KMMCCmdClassLockCard;
1216 csd= (ccc<<20); /* CCC: classes 0, 2, 4, and 7 */
1217 csd|= (0x9<<16); /* READ_BL_LEN: 512 bytes */
1218 csd|= (0x0<<15); /* READ_BL_PARTIAL: No */
1219 csd|= (0x0<<14); /* WRITE_BLK_MISALIGN: No */
1220 csd|= (0x0<<13); /* READ_BLK_MISALIGN: No */
1221 csd|= (0x0<<12); /* DSR_IMP: No DSR */
1222 csd|= (0x0<<8); /* C_SIZE: 1Mb */
1223 csd|= (0x7F); /* C_SIZE: 1Mb (cont)*/
1224 TMMC::BigEndian4Bytes(&aResp[4],csd);
1226 csd= (3UL<<30); /* C_SIZE: 2Mb (cont) */
1227 csd|= (0x1<<27); /* VDD_R_CURR_MIN: 1mA */
1228 csd|= (0x1<<24); /* VDD_R_CURR_MAX: 5mA */
1229 csd|= (0x2<<21); /* VDD_W_CURR_MIN: 5mA */
1230 csd|= (0x3<<18); /* VDD_W_CURR_MAX: 25mA */
1231 csd|= (0x0<<15); /* C_SIZE_MULT: 0 */
1234 csd|= (0x0<<10); /* SECTOR_SIZE: 1 write block */
1235 csd|= (0x0<<5); /* ERASE_GRP_SIZE: 1 sector */
1236 csd|= (0x0); /* WP_GRP_SIZE: 1 erase group */
1240 csd |= (0x00 << (46 - 32)); // ERASE_BLK_EN
1241 csd |= (0x1f << (39 - 32)); // SECTOR_SIZE: 32 write blocks
1242 csd |= (0x00 << (32 - 32)); // WP_GRP_SIZE: 1 erase sector.
1244 TMMC::BigEndian4Bytes(&aResp[8],csd);
1246 csd= (0x0<<31); /* WP_GRP_ENABLE: No */
1247 csd|= (0x0<<29); /* DEFAULT_ECC: ? */
1248 csd|= (0x3<<26); /* R2W_FACTOR: 8 */
1249 csd|= (0x9<<22); /* WRITE_BL_LEN: 512 bytes */
1250 csd|= (0x0<<21); /* WRITE_BL_PARTIAL: No */
1251 csd|= (0x0<<15); /* FILE_FORMAT_GRP: Hard disk */
1252 csd|= (0x0<<14); /* COPY: original */
1253 csd|= (0x0<<13); /* PERM_WRITE_PROTECT: No */
1254 csd|= (0x0<<12); /* TMP_WRITE_PROTECT: No */
1255 csd|= (0x0<<10); /* FILE_FORMAT: Hard disk */
1256 csd|= (0x0<<8); /* ECC: None */
1257 csd|= (0x0<<1); /* CRC: ? */
1258 csd|= (0x1); /* not used */
1259 TMMC::BigEndian4Bytes(&aResp[12],csd);
1262 // ======== DWinsMMCPsu ========
1265 DWinsMMCPsu::DWinsMMCPsu(TInt aVccNum, TInt aMcId)
1266 : DMMCPsu(aVccNum, aMcId)
1269 void DWinsMMCPsu::Init()
1271 // Initialise the PSU
1277 void DWinsMMCPsu::DoSetState(TPBusPsuState aState)
1279 // Turn on/off the PSU. If it is possible to adjust the output voltage on this
1280 // PSU then retreive the required voltage level from TMMCPsu::iVoltageSetting
1281 // (which is in OCR register format).
1291 case EPsuOnCurLimit:
1296 TInt DWinsMMCPsu::VoltageInMilliVolts()
1298 // Return the level of the PSU (in mV) or -ve if error.
1305 void DWinsMMCPsu::DoCheckVoltage()
1307 // Check the voltage level of the PSU is as expected. Returns either KErrNone, KErrGeneral
1308 // to indicate the pass/fail state or KErrNotReady if the voltage check isn't complete.
1312 ReceiveVoltageCheckResult(KErrNone);
1315 void DWinsMMCPsu::PsuInfo(TPBusPsuInfo &anInfo)
1317 // Return machine info relating to the MMC PSU supply
1321 anInfo.iVoltageSupported=0x00040000; // 3.0V (OCR reg. format).
1322 anInfo.iMaxCurrentInMicroAmps=0;
1323 anInfo.iVoltCheckInterval=0;
1324 anInfo.iVoltCheckMethod=EPsuChkComparator;
1325 anInfo.iNotLockedTimeOut=0; // Not enabled
1326 anInfo.iInactivityTimeOut=5; // 5 Seconds