?? sdmemmain.cpp
字號:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES OR INDEMNITIES.
//
// Copyright (c) 2002 BSQUARE Corporation. All rights reserved.
// DO NOT REMOVE --- BEGIN EXTERNALLY DEVELOPED SOURCE CODE ID 40973--- DO NOT REMOVE
// Driver entry points for SD Memory Card client driver
#include <windows.h>
#include "SDMemory.h"
#include <ceddk.h>
// initialize debug zones
SD_DEBUG_INSTANTIATE_ZONES(
TEXT("SDMemory"), // module name
ZONE_ENABLE_INIT | ZONE_ENABLE_ERROR | ZONE_ENABLE_WARN, // initial settings
TEXT("Disk I/O"),
TEXT("Card I/O"),
TEXT("Bus Requests"),
TEXT("Power"),
TEXT(""),
TEXT(""),
TEXT(""),
TEXT(""),
TEXT(""),
TEXT(""),
TEXT(""));
DWORD SetDiskInfo( PSD_MEMCARD_INFO, PDISK_INFO );
DWORD GetDiskInfo( PSD_MEMCARD_INFO, PDISK_INFO );
DWORD GetStorageID( PSD_MEMCARD_INFO, PSTORAGE_IDENTIFICATION, DWORD, DWORD* );
BOOL GetDeviceInfo(PSD_MEMCARD_INFO pMemCard, PSTORAGEDEVICEINFO pStorageInfo);
#define DEFAULT_MEMORY_TAGS 4
///////////////////////////////////////////////////////////////////////////////
// DllEntry - the main dll entry point
// Input: hInstance - the instance that is attaching
// Reason - the reason for attaching
// pReserved - not much
// Output:
// Return: always returns TRUE
// Notes: this is only used to initialize the zones
///////////////////////////////////////////////////////////////////////////////
extern "C" BOOL WINAPI DllEntry(HINSTANCE hInstance, ULONG Reason, LPVOID pReserved)
{
BOOL fRet = TRUE;
if ( Reason == DLL_PROCESS_ATTACH ) {
DisableThreadLibraryCalls((HMODULE) hInstance);
if (!SDInitializeCardLib()) {
fRet = FALSE;
}
}
else if ( Reason == DLL_PROCESS_DETACH ) {
SDDeinitializeCardLib();
}
return fRet;
}
///////////////////////////////////////////////////////////////////////////////
// SMC_Close - the close entry point for the memory driver
// Input: hOpenContext - the context returned from SMC_Open
// Output:
// Return: always returns TRUE
// Notes:
///////////////////////////////////////////////////////////////////////////////
extern "C" BOOL WINAPI SMC_Close(DWORD hOpenContext)
{
DEBUGMSG(SDCARD_ZONE_FUNC, (TEXT("SDMemory: +-SMC_Close\n")));
return TRUE;
}
///////////////////////////////////////////////////////////////////////////////
// CleanUpDevice - cleanup the device instance
// Input: pDevice - device instance
// Output:
// Return:
// Notes:
///////////////////////////////////////////////////////////////////////////////
VOID CleanUpDevice(PSD_MEMCARD_INFO pDevice)
{
DeinitializePowerManagement(pDevice);
// acquire removal lock
AcquireRemovalLock(pDevice);
if (NULL != pDevice->pRegPath) {
// free the reg path
SDFreeMemory(pDevice->pRegPath);
}
if (NULL != pDevice->hBufferList) {
// delete the buffer memory list
SDDeleteMemList(pDevice->hBufferList);
}
ReleaseRemovalLock(pDevice);
DeleteCriticalSection(&pDevice->RemovalLock);
DeleteCriticalSection(&pDevice->CriticalSection);
// free the sterile I/O request
if (NULL != pDevice->pSterileIoRequest) {
LocalFree(pDevice->pSterileIoRequest);
pDevice->pSterileIoRequest = NULL;
}
// free the device memory
SDFreeMemory(pDevice);
}
///////////////////////////////////////////////////////////////////////////////
// SMC_Deinit - the deinit entry point for the memory driver
// Input: hDeviceContext - the context returned from SMC_Init
// Output:
// Return: always returns TRUE
// Notes:
///////////////////////////////////////////////////////////////////////////////
extern "C" BOOL WINAPI SMC_Deinit(DWORD hDeviceContext)
{
PSD_MEMCARD_INFO pDevice;
DEBUGMSG(SDCARD_ZONE_INIT, (TEXT("SDMemory: +SMC_Deinit\n")));
pDevice = (PSD_MEMCARD_INFO)hDeviceContext;
// now it is safe to clean up
CleanUpDevice(pDevice);
DEBUGMSG(SDCARD_ZONE_INIT, (TEXT("SDMemory: -SMC_Deinit\n")));
return TRUE;
}
///////////////////////////////////////////////////////////////////////////////
// SlotEventCallBack - slot event callback for fast-path events
// Input: hDevice - device handle
// pContext - device specific context that was registered
// SlotEventType - slot event type
// pData - Slot event data (can be NULL)
// DataLength - length of slot event data (can be 0)
// Output:
// Return:
// Notes:
//
// If this callback is registered the client driver can be notified of
// slot events (such as device removal) using a fast path mechanism. This
// is useful if a driver must be notified of device removal
// before its XXX_Deinit is called.
//
// This callback can be called at a high thread priority and should only
// set flags or set events. This callback must not perform any
// bus requests or call any apis that can perform bus requests.
///////////////////////////////////////////////////////////////////////////////
VOID SlotEventCallBack(SD_DEVICE_HANDLE hDevice,
PVOID pContext,
SD_SLOT_EVENT_TYPE SlotEventType,
PVOID pData,
DWORD DataLength)
{
PSD_MEMCARD_INFO pDevice;
DEBUGMSG(SDCARD_ZONE_INIT, (TEXT("SDMemory: +SlotEventCallBack - %d \n"),SlotEventType));
switch (SlotEventType) {
case SDCardEjected :
pDevice = (PSD_MEMCARD_INFO)pContext;
// mark that the card is being ejected
pDevice->CardEjected = TRUE;
// acquire the removal lock to block this callback
// in case an ioctl is in progress
AcquireRemovalLock(pDevice);
ReleaseRemovalLock(pDevice);
break;
}
DEBUGMSG(SDCARD_ZONE_INIT, (TEXT("SDMemory: -SlotEventCallBack \n")));
}
///////////////////////////////////////////////////////////////////////////////
// SMC_Init - the init entry point for the memory driver
// Input: dwContext - the context for this init
// Output:
// Return: non-zero context
// Notes:
///////////////////////////////////////////////////////////////////////////////
extern "C" DWORD WINAPI SMC_Init(DWORD dwContext)
{
SD_DEVICE_HANDLE hClientHandle; // client handle
PSD_MEMCARD_INFO pDevice; // this instance of the device
SDCARD_CLIENT_REGISTRATION_INFO ClientInfo; // client into
ULONG BufferSize; // size of buffer
HKEY hSubKey; // registry key
SD_API_STATUS Status; // intermediate status
DWORD data; // registry data
DWORD dataLength; // registry data length
DEBUGMSG(SDCARD_ZONE_INIT, (TEXT("SDMemory: +SMC_Init\r\n")));
pDevice = (PSD_MEMCARD_INFO)SDAllocateMemoryWithTag(
sizeof(SD_MEMCARD_INFO),
SD_MEMORY_TAG);
if (pDevice == NULL) {
DEBUGMSG(SDCARD_ZONE_ERROR, (TEXT("SDMemory: Failed to allocate device info\r\n")));
DEBUGMSG(SDCARD_ZONE_INIT, (TEXT("SDMemory: -SMC_Init\r\n")));
return 0;
}
// initialize sterile I/O request to NULL
pDevice->pSterileIoRequest = NULL;
InitializeCriticalSection(&pDevice->CriticalSection);
InitializeCriticalSection(&pDevice->RemovalLock);
// get the device handle from the bus driver
hClientHandle = SDGetDeviceHandle(dwContext, &pDevice->pRegPath);
// store device handle in local context
pDevice->hDevice = hClientHandle;
if (NULL == hClientHandle) {
DEBUGMSG(SDCARD_ZONE_ERROR, (TEXT("SDMemory: Failed to get client handle\r\n")));
CleanUpDevice(pDevice);
DEBUGMSG(SDCARD_ZONE_INIT, (TEXT("SDMemory: -SMC_Init\r\n")));
return 0;
}
// allocate sterile I/O request
pDevice->pSterileIoRequest = (PSG_REQ)LocalAlloc(
LPTR,
(sizeof(SG_REQ) + ((MAX_SG_BUF - 1) * sizeof(SG_BUF)))
);
if (NULL == pDevice->pSterileIoRequest) {
DEBUGMSG(SDCARD_ZONE_ERROR, (TEXT("SDMemory: Failed to allocate sterile I/O request\r\n")));
CleanUpDevice(pDevice);
DEBUGMSG(SDCARD_ZONE_INIT, (TEXT("SDMemory: -SMC_Init\r\n")));
return 0;
}
// register our debug zones
SDRegisterDebugZones(hClientHandle, pDevice->pRegPath);
memset(&ClientInfo, 0, sizeof(ClientInfo));
// set client options and register as a client device
_tcscpy(ClientInfo.ClientName, TEXT("Memory Card"));
// set the callback
ClientInfo.pSlotEventCallBack = SlotEventCallBack;
Status = SDRegisterClient(hClientHandle, pDevice, &ClientInfo);
if (!SD_API_SUCCESS(Status)) {
DEBUGMSG(SDCARD_ZONE_ERROR, (TEXT("SDMemory: Failed to register client : 0x%08X\r\n"),
Status));
CleanUpDevice(pDevice);
DEBUGMSG(SDCARD_ZONE_INIT, (TEXT("SDMemory: -SMC_Init\r\n")));
return 0;
}
#ifdef _FOR_MOVI_NAND_
/**
* Description : There is no way to distinguish between HSMMC and moviNAND.
* So, We assume A HSMMC card is a moviNAND. Default value is false;
*/
pDevice->IsHSMMC = FALSE;
#endif
// configure card and retrieve disk size/format information
if( SDMemCardConfig( pDevice ) != ERROR_SUCCESS ) {
CleanUpDevice(pDevice);
DEBUGMSG( SDCARD_ZONE_ERROR, (TEXT("SDMemory: Error initializing MemCard structure and card\r\n")));
return 0;
}
// aet a default block transfer size
pDevice->BlockTransferSize = DEFAULT_BLOCK_TRANSFER_SIZE;
// read configuration from registry
// open the reg path
if (RegOpenKeyEx(
HKEY_LOCAL_MACHINE,
pDevice->pRegPath,
0,
KEY_ALL_ACCESS,
&hSubKey) == ERROR_SUCCESS
) {
// read "BlockTransferSize"
dataLength = sizeof(pDevice->BlockTransferSize);
RegQueryValueEx(
hSubKey,
BLOCK_TRANSFER_SIZE_KEY,
NULL,
NULL,
(PUCHAR)&(pDevice->BlockTransferSize),
&dataLength);
if (pDevice->BlockTransferSize != DEFAULT_BLOCK_TRANSFER_SIZE) {
DEBUGMSG(SDCARD_ZONE_INIT, (TEXT("SDMemory: Initialize: Using block transfer size of %d blocks\r\n"),
pDevice->BlockTransferSize));
}
// read "SingleBlockWrites"
// default to using mulitple block writes
pDevice->SingleBlockWrites = FALSE;
dataLength = sizeof(DWORD);
data = 0;
if (RegQueryValueEx(
hSubKey,
SINGLE_BLOCK_WRITES_KEY,
NULL,
NULL,
(PUCHAR)&data,
&dataLength) == ERROR_SUCCESS
) {
// key is present
pDevice->SingleBlockWrites = TRUE;
DEBUGMSG(SDCARD_ZONE_INIT, (TEXT("SDMemory: Initialize: Using single block write commands only\r\n")));
}
// read "DisablePowerManagement"
// on by default unless key is present
pDevice->EnablePowerManagement = TRUE;
dataLength = sizeof(DWORD);
data = 0;
if (RegQueryValueEx(
hSubKey,
DISABLE_POWER_MANAGEMENT,
NULL,
NULL,
(PUCHAR)&data,
&dataLength) == ERROR_SUCCESS
) {
// key is present
DEBUGMSG(SDCARD_ZONE_INIT, (TEXT("SDMemory: Initialize: Disabled power management\r\n")));
pDevice->EnablePowerManagement = FALSE;
}
// read "IdleTimeout"
pDevice->IdleTimeout = DEFAULT_IDLE_TIMEOUT;
dataLength = sizeof(pDevice->IdleTimeout);
if (RegQueryValueEx(
hSubKey,
IDLE_TIMEOUT,
NULL,
NULL,
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -