📄 sdbusdriver.cpp
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
// Copyright (c) 2002 BSQUARE Corporation. All rights reserved.
// DO NOT REMOVE --- BEGIN EXTERNALLY DEVELOPED SOURCE CODE ID 40973--- DO NOT REMOVE
// Class implementation of the Bus Driver
#include "SDBusDriver.h"
#include <creg.hxx>
#include <devload.h>
#define GET_HC_NAME_FROM_SLOT(s) (s)->pHostController->HostControllerName
///////////////////////////////////////////////////////////////////////////////
// CSDBusDriver - Destructor
// Input:
// Output:
// Notes:
///////////////////////////////////////////////////////////////////////////////
CSDBusDriver::~CSDBusDriver()
{
m_Initialized = FALSE;
// Temporarily take the lock to be sure that no host controller is
// registering itself. Now that m_Initialized is FALSE, that call
// will no longer succeed.
AcquireLock();
ReleaseLock();
PLIST_ENTRY pListEntry = m_HostControllerListHead.Flink;
// Loop through the host controllers and remove them.
while (pListEntry != &m_HostControllerListHead) {
PSDBUS_HC_CONTEXT pHCContext = CONTAINING_RECORD(pListEntry, SDBUS_HC_CONTEXT, ListEntry);
DoDeregisterHostController(pHCContext, FALSE);
// Move to the next one
pListEntry = m_HostControllerListHead.Flink;
}
// kill the event dispatcher
if (NULL != m_pBusRequestCompleteDispatcher) {
delete m_pBusRequestCompleteDispatcher;
m_pBusRequestCompleteDispatcher = NULL;
}
if (NULL != m_SynchList) {
SDDeleteMemList(m_SynchList);
m_SynchList = NULL;
}
if (NULL != m_BusRequestList) {
SDDeleteMemList(m_BusRequestList);
m_BusRequestList = NULL;
}
DeleteCriticalSection(&m_BusDriverCriticalSection);
}
///////////////////////////////////////////////////////////////////////////////
// CSDBusDriver - constructor
// Input: pszActiveKey - context from XXX_Init
// Output:
// Notes:
///////////////////////////////////////////////////////////////////////////////
CSDBusDriver::CSDBusDriver(LPCTSTR pszActiveKey) :
m_sdBusEnum(pszActiveKey)
{
DEBUGCHK(pszActiveKey);
InitializeCriticalSection(&m_BusDriverCriticalSection);
m_Initialized = FALSE;
m_SynchList = NULL;
m_BusRequestList = NULL;
m_DispatchPriority = DEFAULT_THREAD_PRIORITY;
m_pBusRequestCompleteDispatcher = NULL;
InitializeListHead(&m_HostControllerListHead);
SDInitializeQueue(&m_CompletedRequestQueue);
m_BusRequestRetryCount = DEFAULT_BUS_REQUEST_RETRY_COUNT;
HKEY hkDevice = OpenDeviceKey(pszActiveKey);
if (hkDevice) {
m_regDevice.Open(hkDevice, _T(""));
RegCloseKey(hkDevice);
}
}
///////////////////////////////////////////////////////////////////////////////
// CSDBusDriver - Initialize Function for Bus driver object
// Input:
// Output:
// Notes:
// returns api status error
///////////////////////////////////////////////////////////////////////////////
SD_API_STATUS CSDBusDriver::Initialize()
{
DWORD requestListDepth; // request list depths
SD_API_STATUS status; // api status
if (!m_sdBusEnum.Init()) {
DEBUGMSG(SDCARD_ZONE_ERROR, (TEXT("SDBusDriver: Initialize: Failed to initialize the bus enum class\n")));
return SD_API_STATUS_INSUFFICIENT_RESOURCES;
}
if (!m_regDevice.IsOK()) {
DEBUGMSG(SDCARD_ZONE_ERROR, (TEXT("SDBusDriver: Initialize: Could not open device key\n")));
return SD_API_STATUS_UNSUCCESSFUL;
}
// get the request depth key if it exists
requestListDepth = m_regDevice.ValueDW(SDCARD_REQUEST_LIST_DEPTH_KEY,
SDCARD_DEFAULT_REQUEST_LIST_DEPTH);
if (requestListDepth != SDCARD_DEFAULT_REQUEST_LIST_DEPTH) {
DEBUGMSG(SDCARD_ZONE_INIT, (TEXT("SDBusDriver: Initialize: Using request list depth of %d entries \n"),requestListDepth));
}
// get the thread priority key if it exists
m_DispatchPriority = m_regDevice.ValueDW(SDCARD_THREAD_PRIORITY_KEY,
DEFAULT_THREAD_PRIORITY);
if (m_DispatchPriority != DEFAULT_THREAD_PRIORITY) {
DEBUGMSG(SDCARD_ZONE_INIT, (TEXT("SDBusDriver: Initialize: Dispatch thread priority set to; %d \n"),m_DispatchPriority));
}
// get the request retry key if it exists
m_BusRequestRetryCount = m_regDevice.ValueDW(SDCARD_REQUEST_RETRY_KEY,
DEFAULT_BUS_REQUEST_RETRY_COUNT);
if (m_BusRequestRetryCount != DEFAULT_BUS_REQUEST_RETRY_COUNT) {
DEBUGMSG(SDCARD_ZONE_INIT, (TEXT("SDBusDriver: Initialize: Bus request retry count set to; %d \n"),m_BusRequestRetryCount));
}
// create the bus request memory list
m_BusRequestList = SDCreateMemoryList('SDLT',
requestListDepth,
sizeof(SDBUS_BUS_REQUEST));
if (NULL == m_BusRequestList) {
DEBUGMSG(SDCARD_ZONE_ERROR, (TEXT("SDBusDriver: Initialize: Failed to allocate bus request list\n")));
return SD_API_STATUS_INSUFFICIENT_RESOURCES;
}
// create list of synchronous request stuctures
m_SynchList = SDCreateMemoryList(SD_BUS_DRIVER_TAG,
requestListDepth,
sizeof(SD_SYNCH_REQUEST_INFO));
if (NULL == m_SynchList) {
DEBUGMSG(SDCARD_ZONE_ERROR, (TEXT("SDBusDriver: Initialize: Failed to allocate synch list \n")));
return SD_API_STATUS_INSUFFICIENT_RESOURCES;
}
m_pBusRequestCompleteDispatcher = new CSDWorkItem(this,
(PSD_WORK_ITEM_FUNC)CSDBusDriver::BusRequestCompleteDispatch,
m_DispatchPriority,
0,
0,
FALSE); // create the work item without a message queue
if (NULL == m_pBusRequestCompleteDispatcher) {
DEBUGMSG(SDCARD_ZONE_ERROR, (TEXT("SDBusDriver: Initialize: Failed to allocate event dispatcher \n")));
return SD_API_STATUS_INSUFFICIENT_RESOURCES;
}
status = m_pBusRequestCompleteDispatcher->StartWorkItem();
if (!SD_API_SUCCESS(status)){
DEBUGMSG(SDCARD_ZONE_ERROR, (TEXT("SDBusDriver: Initialize: event dispatcher work item failed \n")));
return SD_API_STATUS_INSUFFICIENT_RESOURCES;
}
// set initialized flag
m_Initialized = TRUE;
return SD_API_STATUS_SUCCESS;
}
///////////////////////////////////////////////////////////////////////////////
// GetSlotInfo - get slot information for all slots in the system
// Input: pBuffer - buffer to hold the slot information
// Length - length of the buffer
// Output:
// Notes:
// returns the number of slots or zero on error
///////////////////////////////////////////////////////////////////////////////
DWORD CSDBusDriver::GetSlotInfo(PUCHAR pBuffer, DWORD Length)
{
LIST_ENTRY *pListEntry; // list entry
PSDBUS_HC_CONTEXT pHCContext; // host controller context
PSDBUS_HC_SLOT_CONTEXT pSlot; // slot context
PSDCARD_DEVICE_CONTEXT pDevice; // the device in the slot
ULONG slotIndex; // slot index
PBUS_DRIVER_SLOT_INFO slotInfoArray; // slot array
int slotArrayIndex; // index to slot array
ULONG slotCount; // running slot count
WCHAR description[MAX_SD_DESCRIPTION_STRING];
// get the head
pListEntry = m_HostControllerListHead.Flink;
// reset slot count
slotCount = 0;
// acquire the lock to protect the list
AcquireLock();
// loop through the host controllers and get the slot count
while (pListEntry != &m_HostControllerListHead) {
// get the context
pHCContext = CONTAINING_RECORD(pListEntry, SDBUS_HC_CONTEXT, ListEntry);
// increment the slot count
slotCount += pHCContext->NumberOfSlots;
// move to the next one
pListEntry = pListEntry->Flink;
}
// see if the caller just wants the count
if (pBuffer == NULL) {
ReleaseLock();
return slotCount;
}
// check the buffer
if (Length != slotCount * (sizeof(BUS_DRIVER_SLOT_INFO))) {
DEBUGMSG(SDCARD_ZONE_ERROR, (TEXT("SDBusDriver: GetSlotInfo : insufficient buffer \n")));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -