📄 sdbusdriver.h
字号:
//
// 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-2004 BSQUARE Corporation. All rights reserved.
// DO NOT REMOVE --- BEGIN EXTERNALLY DEVELOPED SOURCE CODE ID 40973--- DO NOT REMOVE
// Bus driver definitions
#include <SDCardDDK.h>
#include <SDHCD.h>
#include "SDCardApi.h"
#include "SDWorkItem.h"
#include "SDEnum.h"
#include "SDBusRequest.h"
#include <CReg.hxx>
/*************************************************************************/
/****** Date : 07.05.04 ******/
/****** Developer : HS.JANG ******/
/****** Description : This is a flag that means that it supports ******/
/****** SDCard that supports spec2.0 *****/
/*************************************************************************/
#define SD_SPEC_20
/*************************************************************************/
#ifndef _SDCARD_BUSDRIVER_DEFINED
#define _SDCARD_BUSDRIVER_DEFINED
#define ENABLE_SDIO_V1_1_SUPPORT 1
// base registry key for the SDCARD driver
#define SDCARD_SDMEMORY_CLASS_REG_PATH TEXT("\\Drivers\\SDCARD\\ClientDrivers\\Class\\SDMemory_Class")
#define SDCARD_MMC_CLASS_REG_PATH TEXT("\\Drivers\\SDCARD\\ClientDrivers\\Class\\MMC_Class")
#define SDCARD_CUSTOM_DEVICE_REG_PATH TEXT("\\Drivers\\SDCARD\\ClientDrivers\\Custom")
#define SDCARD_SDIO_CLASS_REG_PATH TEXT("\\Drivers\\SDCARD\\ClientDrivers\\Class\\SDIO_Class")
#define SDCARD_THREAD_PRIORITY_KEY TEXT("ThreadPriority")
#define SDCARD_REQUEST_LIST_DEPTH_KEY TEXT("RequestListDepth")
#define SDCARD_REQUEST_RETRY_KEY TEXT("RequestRetryCount")
#define SDCARD_CLOCK_RATE_OVERRIDE TEXT("SDClockRateOverride")
#define SDCARD_INTERFACE_MODE_OVERRIDE TEXT("SDInterfaceOverride")
#define SDCARD_INTERFACE_OVERRIDE_1BIT 0
#define SDCARD_INTERFACE_OVERRIDE_4BIT 1
#define SDCARD_DEFAULT_REQUEST_LIST_DEPTH 32
#define DEFAULT_THREAD_PRIORITY 100
#define DEFAULT_MEMORY_TAGS 6
#define DEFAULT_MESSAGE_ENTRIES 7
#define DEFAULT_REFCOUNT_WAIT 100
#define DEFAULT_MAX_WAIT_COUNT 20
#define DEFAULT_BUS_REQUEST_RETRY_COUNT 3
#define DEVICE_CLEANUP_POLLING_INTERVAL 100
#define POWER_RESUME_POLLING_INTERVAL 1000
#define POWER_RESUME_DELAY_INTERVAL 2000
// typdef for the current slot state
typedef enum SD_SLOT_STATE {
SlotInactive = 0, // slot is inactive
SlotIdle, // slot is idle (after power up)
Ready, // the slot is ready, the client driver now has control
SlotDeviceEjected, // the device is being ejected
SlotInitFailed, // slot initialization failed
SlotDeselected, // card in slot is deselected
SlotResetting // slot is resetting
} *PSD_SLOT_STATE;
typedef struct SDBUS_HC_CONTEXT *PSDBUS_HC_CONTEXT;
// slot context , one per slot
typedef struct SDBUS_HC_SLOT_CONTEXT : SDCARD_HC_SLOT_INFO {
PSDBUS_HC_CONTEXT pHostController; // the host controller this slot belongs to
SD_DEVICE_HANDLE hDevice; // handle to the device occupying this slot
SD_SLOT_STATE SlotState; // slot state
DWORD SlotIndex; // the slot index
SD_REQUEST_QUEUE RequestQueue; // request queue for this slot
PVOID pWorkItem; // work item
DWORD Flags; // flags
SD_CARD_INTERFACE SlotInterface; // Slot interface
BOOL fEnablePowerControl; // enable or disable power control for the slot
USHORT AllocatedPower; // power allocated
} *PSDBUS_HC_SLOT_CONTEXT;
// the following bits are for flags field in the slot context
#define SD_SLOT_FLAG_SDIO_INTERRUPTS_ENABLED 0x00000001
#define IS_SLOT_SDIO_INTERRUPT_ON(pSlot) ((pSlot)->Flags & SD_SLOT_FLAG_SDIO_INTERRUPTS_ENABLED)
#define FLAG_SD_SLOT_INTERRUPTS_ON(pSlot) ((pSlot)->Flags |= SD_SLOT_FLAG_SDIO_INTERRUPTS_ENABLED)
#define FLAG_SD_SLOT_INTERRUPTS_OFF(pSlot) ((pSlot)->Flags &= ~SD_SLOT_FLAG_SDIO_INTERRUPTS_ENABLED)
// host controller context
typedef struct SDBUS_HC_CONTEXT : SDCARD_HC_CONTEXT {
DWORD dwSig; // signature of structure
LIST_ENTRY ListEntry; // hc list pointers
DWORD dwHCNumber; // hc number
PVOID pSystemContext; // system context
DWORD NumberOfSlots; // number of slots
SDBUS_HC_SLOT_CONTEXT SlotList[1]; // beginning of slot list array
} *PSDBUS_HC_CONTEXT;
#define SDHCGetSlotContext(pHc, Index) &((pHc)->SlotList[Index])
#define SDHCDSetSlotPower(pHc,slot,d) ((pHc)->SlotList[slot].AllocatedPower = d)
#define SDHCDGetSlotPower(pHc,slot) ((pHc)->SlotList[slot].AllocatedPower)
#define SDHCDSetSlotEnablePowerControl(pHc,slot,d) ((pHc)->SlotList[slot].fEnablePowerControl = d)
#define SDHCDGetSlotEnablePowerControl(pHc,slot) ((pHc)->SlotList[slot].fEnablePowerControl)
// the card registers
typedef struct SDCARD_CARD_REGISTERS {
UCHAR OCR[SD_OCR_REGISTER_SIZE]; // SD OCR
UCHAR IO_OCR[SD_IO_OCR_REGISTER_SIZE]; // IO OCR
UCHAR CID[SD_CID_REGISTER_SIZE]; // CID
UCHAR CSD[SD_CSD_REGISTER_SIZE]; // CSD
UCHAR SCR[SD_SCR_REGISTER_SIZE]; // SCR
#ifdef __MMC_SPEC_4_2_SUPPORT__
// buffer for Extended CSD register
UCHAR EXT_CSD[MMC_EXTCSD_REGISTER_SIZE];
#endif
} *PSDCARD_CARD_REGISTERS;
// typedef for work item callback
typedef VOID (*PSD_BUS_WORK_ITEM_FUNC) (PVOID);
// SDIO common information (shared by all I/O functions)
typedef struct SDIO_COMMON_INFORMATION {
UCHAR CCCRRev; // CCCE/SDIO Revision (common for all functions)
UCHAR SDSpec; // SD Spec version register
UCHAR CardCapability; // card capability (common for all functions)
DWORD CommonCISPointer; // common CIS pointer for this function
USHORT ManufacturerID; // 16 bit manufacturer ID (common)
USHORT CardID; // 16 bit cardID (common)
PWCHAR pProductInformation; // storage for the product information string (common)
UCHAR CCCRShadowIntEnable; // shadowed interrupt enable register (Parent Only)
UCHAR CCCRShadowIOEnable; // shadowed I/O enable register (Parent Only)
BOOL fCardSupportsPowerControl; // Does the card support Power Control
BOOL fPowerControlEnabled; // Is Power Control Enabled for the Card
} *PSDIO_COMMON_INFORMATION;
// SDIO information for each I/O function
typedef struct SDIO_INFORMATION {
UCHAR Function; // function number
UCHAR DeviceCode; // device interface code for this function
DWORD CISPointer; // CIS pointer for this function
DWORD CSAPointer; // CSA pointer for this function
PWCHAR pFunctionInformation; // storage for the function information string
PSD_INTERRUPT_CALLBACK pInterruptCallBack; // for SDIO devices the interrupt callback
PSDIO_COMMON_INFORMATION pCommonInformation; // common information (parent only)
BOOL fWUS; // Wake up supported?
ULONG SoftBlockCount; // number of blocks
ULONG SoftBlockSize; // size of each block
ULONG SoftBlockLengthInBytes; // Size of the Soft-Blocks
PUCHAR pSoftBlockBuffer; // Data buffer starting address
PUCHAR pSoftBlockEndOfBuffer;// Data buffer ending address
PUCHAR pSoftBlockData; // Current segment data buffer address
DWORD SoftBlockArgument; // Command argument for the request.
UCHAR SoftBlockCommand; // Command for the request.
UCHAR Flags; // Soft-Block control flags.
SD_FUNCTION_POWER_DRAW PowerDrawData; // current draw of function.
#define SFTBLK_USE_FOR_CMD18 1 // Use Soft-Block for read operations.
#define SFTBLK_USE_FOR_CMD25 2 // Use Soft-Block for write operations.
#define SFTBLK_USE_FOR_CMD53_READ 4 // Use Soft-Block for read operations.
#define SFTBLK_USE_FOR_CMD53_WRITE 8 // Use Soft-Block for write operations.
#define SFTBLK_USE_ALWAYS 0x10 // Use Soft-Block always.
#define FSTPTH_DISABLE 0x20 // Disable the use of Fast-Path.
} *PSDIO_INFORMATION;
// SD/SDIO/MMC specific information
typedef struct SDMMC_INFORMATION {
ULONG DataAccessWriteClocks; // total clocks required for the write to finish
ULONG DataAccessReadClocks; // total access delay in clocks for the first byte transferred in a read
BOOL CardIsLocked; // card is locked
} *PSDMMC_INFORMATION;
// SD Card information structure
typedef struct SDCARD_INFORMATION {
SDIO_INFORMATION SDIOInformation; // SDIO information
SDMMC_INFORMATION SDMMCInformation; // SD/SDIO/MMC information
} *PSDCARD_INFORMATION;
// synchronous request information
typedef struct __SD_SYNCH_REQUEST_INFO {
HANDLE hWaitEvent; // wait object
PSD_COMMAND_RESPONSE pResponse; // response buffer pointer from caller
SD_COMMAND_RESPONSE TempResponse; // temp response buffer
SD_API_STATUS Status; // completion status
} SD_SYNCH_REQUEST_INFO, *PSD_SYNCH_REQUEST_INFO;
// the device context definition returned as the SD_DEVICE_HANDLE
typedef struct SDCARD_DEVICE_CONTEXT {
// pGetDeviceFunctions is deprecated, but needs to stay as a
// pointer type for legacy reasons.
#ifdef SD_SPEC_20
/*************************************************************************/
/****** Date : 07.05.04 ******/
/****** Developer : HS.JANG ******/
/****** Description : Each SDCard device context has to have ******/
/****** information about the card type becuase of ******/
/****** SDHC ( high capacity ) , HSMMC card ******/
/*************************************************************************/
DWORD dwWhatIsCardType;
/*************************************************************************/
#endif
PVOID pvReserved; // reserved (DO NOT CHANGE THIS LOCATION)
HINSTANCE hDriver; // handle to the device driver DLL (DO NOT CHANGE THIS LOCATION)
DWORD dwSig; // signature of structure
PSDBUS_HC_SLOT_CONTEXT pSlot; // the slot this device is on
LONG ReferenceCount; // this object's reference count
TCHAR ClientName[MAX_SDCARD_CLIENT_NAME]; // client name
DWORD ClientFlags; // flags set by the client driver
SDCARD_DEVICE_TYPE DeviceType; // device type
CSdDeviceFolder *pDeviceFolder; // device folder
PVOID pDeviceContext; // device specific context
PSD_SLOT_EVENT_CALLBACK pSlotEventCallBack; // slot event callback
SD_CARD_RCA RelativeAddress; // card's relative address
SDCARD_CARD_REGISTERS CachedRegisters; // cached registers
DWORD OperatingVoltage; // current operating voltage
PVOID pSystemContext; // system context
SDCARD_INFORMATION SDCardInfo; // information for SD Card (based on type)
CRITICAL_SECTION DeviceCritSection; // device instance critical section for interlocking between functions
SD_CARD_INTERFACE CardInterface; // card's current interface
BOOL bCardSelectRequest; // request the card to be selected
BOOL bCardDeselectRequest; // request the card to be deselected
SDCARD_DEVICE_CONTEXT *pNext; // next device in chain for multifunction devices
SDCARD_DEVICE_CONTEXT *pParentDevice; // parent device (for synchronization)
//
SD_SYNCH_REQUEST_INFO synchInfo;
CRITICAL_SECTION syncCritSection;
DWORD syncCount;
} *PSDCARD_DEVICE_CONTEXT;
// Signature of SDCARD_DEVICE_CONTEXT structure
#define VALID_DEVICE_CONTEXT_SIG 'cDdS' // SdDc
BOOL ValidateClientHandle(PSDCARD_DEVICE_CONTEXT pDevice);
// helpful macros
#define CLIENT_HANDLES_RETRY(pDevice) ((pDevice)->ClientFlags & SD_CLIENT_HANDLES_RETRY)
#define SDDCGetClientDeviceFromHandle(handle) ((PSDCARD_DEVICE_CONTEXT)(handle))
#define SDDCGetClientName(pDevice) (pDevice)->ClientName
#define SDDCAcquireDeviceLock(pDevice) EnterCriticalSection(&((pDevice)->DeviceCritSection));
#define SDDCReleaseDeviceLock(pDevice) LeaveCriticalSection(&((pDevice)->DeviceCritSection));
#define SDDCIncrementRefCount(pDevice) InterlockedIncrement(&((pDevice)->ReferenceCount));
#define SDDCDecrementRefCount(pDevice) \
{ \
DEBUG_CHECK(((pDevice)->ReferenceCount != 0),(TEXT("Reference Count decremented past zero!"))); \
InterlockedDecrement(&((pDevice)->ReferenceCount)); \
}
#define IS_DEVICE_MULTIFUNCTION(pDevice) \
(((pDevice)->pParentDevice != (pDevice)) || (NULL != (pDevice)->pNext))
#define SDDCGetRefCount(pDevice) (pDevice)->ReferenceCount
// bus request System Flags
// Bits 31-28 are defined in SDCardDDK.h
#define SD_BUS_REQUEST_BUSY 0x00000100 // bus request is in the HC
#define SD_BUS_REQUEST_NON_CANCELABLE 0x00000200 // bus request is non-cancelable
#define SD_BUS_REQUEST_COMPLETING 0x00000400 // bus request is being processed in dispatcher
#define SD_BUS_REQUEST_USING_SOFT_BLOCK 0x00000800 // Soft-block processing in use for this request
#define SD_BUS_REQUEST_SLOT_RESET 0x00001000 // bus request for resetting slot
#define SYSTEM_FLAGS_RETRY_COUNT_MASK 0x000000FF
#define SD_REQUEST_MARK_BUSY(p) ((p)->SystemFlags |= SD_BUS_REQUEST_BUSY)
#define IS_SD_REQUEST_BUSY(p) ((p)->SystemFlags & SD_BUS_REQUEST_BUSY)
#define SD_REQUEST_MARK_NON_CANCELABLE(p) ((p)->SystemFlags |= SD_BUS_REQUEST_NON_CANCELABLE)
#define SD_REQUEST_MARK_CANCELABLE(p) ((p)->SystemFlags &= ~SD_BUS_REQUEST_NON_CANCELABLE)
#define IS_SD_REQUEST_CANCELABLE(p) (!((p)->SystemFlags & SD_BUS_REQUEST_NON_CANCELABLE))
#define SD_REQUEST_MARK_COMPLETING(p) ((p)->SystemFlags |= SD_BUS_REQUEST_COMPLETING)
#define IS_REQUEST_COMPLETING(p) ((p)->SystemFlags & SD_BUS_REQUEST_COMPLETING)
#define SD_REQUEST_USING_SOFT_BLOCK(p) ((p)->SystemFlags |= SD_BUS_REQUEST_USING_SOFT_BLOCK )
#define SD_REQUEST_NO_SOFT_BLOCK(p) ((p)->SystemFlags &= ~ SD_BUS_REQUEST_USING_SOFT_BLOCK)
#define IS_REQUEST_USING_SOFT_BLOCK(p) ((p)->SystemFlags & SD_BUS_REQUEST_USING_SOFT_BLOCK)
#define IS_SLOTRESET_REQUEST(p) ((p)->SystemFlags & SD_BUS_REQUEST_SLOT_RESET)
// macros for manipulating the retry count
#define SDRequestGetRetryCount(pRequest) \
((pRequest)->SystemFlags & SYSTEM_FLAGS_RETRY_COUNT_MASK)
#define SDRequestSetRetryCount(pRequest, Value) \
{ \
(pRequest)->SystemFlags &= ~SYSTEM_FLAGS_RETRY_COUNT_MASK; \
(pRequest)->SystemFlags |= (Value & SYSTEM_FLAGS_RETRY_COUNT_MASK); \
}
#define SDDecrementRetryCount(pRequest) \
{ \
ULONG retryCount; \
retryCount = SDRequestGetRetryCount(pRequest); \
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -