📄 sdenum.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.
//
#include "SDEnum.h"
#include "SDBusDriver.h"
static
SD_API_STATUS
CallSlotOptionHandler(
SDCARD_DEVICE_CONTEXT *pDevice,
SD_SLOT_OPTION_CODE sdOption,
PVOID pData,
ULONG ulOptionSize
)
{
PREFAST_DEBUGCHK(pDevice);
PSDBUS_HC_SLOT_CONTEXT pSlot = pDevice->pSlot;
PREFAST_DEBUGCHK(pSlot);
PSDBUS_HC_CONTEXT pHostController = pSlot->pHostController;
PREFAST_DEBUGCHK(pHostController);
PREFAST_DEBUGCHK(pHostController->pSlotOptionHandler);
return pHostController->pSlotOptionHandler(pHostController,
pSlot->SlotIndex, sdOption, pData, ulOptionSize);
}
CSdDeviceFolder::CSdDeviceFolder(
LPCTSTR pszBusName,
LPCTSTR pszTemplateRegPath,
SDCARD_DEVICE_CONTEXT *pDevice,
DWORD dwBusType,
DWORD dwBusNumber,
PCI_SLOT_NUMBER SlotNumber,
HANDLE hParent,
DWORD dwInitReg
)
: DeviceFolder(pszBusName, pszTemplateRegPath, dwBusType, dwBusNumber,
SlotNumber.u.bits.DeviceNumber,SlotNumber.u.bits.FunctionNumber, hParent, dwInitReg)
{
DEBUGCHK(pszBusName);
DEBUGCHK(pszTemplateRegPath);
DEBUGCHK(pDevice);
DEBUGCHK(hParent);
m_pDevice = pDevice;
}
CEDEVICE_POWER_STATE
CSdDeviceFolder::GetPowerState(
)
{
CEDEVICE_POWER_STATE cps = D0;
CallSlotOptionHandler(m_pDevice, SDHCDGetSlotPowerState,
&cps, sizeof(cps));
return cps;
}
BOOL
CSdDeviceFolder::SetPowerState(
CEDEVICE_POWER_STATE cpsNew
)
{
DEBUGCHK(VALID_DX(cpsNew));
SD_API_STATUS status = CallSlotOptionHandler(m_pDevice,
SDHCDSetSlotPowerState, &cpsNew, sizeof(cpsNew));
return SD_API_SUCCESS(status);
}
CSdBusEnum::CSdBusEnum(
LPCTSTR pszActiveRegPath
)
: DefaultBusDriver(pszActiveRegPath)
{
m_pszBusName = NULL;
}
CSdBusEnum::~CSdBusEnum(
)
{
if (m_pszBusName) free(m_pszBusName);
}
BOOL
CSdBusEnum::Init(
)
{
return DefaultBusDriver::Init();
}
BOOL
CSdBusEnum::SetBusName(
LPCTSTR pszBusName
)
{
DEBUGCHK(pszBusName);
if (m_pszBusName) {
free(m_pszBusName);
}
m_pszBusName = _tcsdup(pszBusName);
return (m_pszBusName != NULL);
}
LPCTSTR
CSdBusEnum::GetBusName(
)
{
LPCTSTR pszRet;
if (m_pszBusName) {
pszRet = m_pszBusName;
}
else {
pszRet = DEFAULT_SD_BUS_NAME;
}
return pszRet;
}
DWORD
CSdBusEnum::GetBusNamePrefix(
LPTSTR pszReturnBusName,
DWORD cchReturnBusName
)
{
DWORD dwRet = 0;
LPCTSTR pszBusName = GetBusName();
DWORD cchToCopy = min(_tcslen(pszBusName) + 1, cchReturnBusName);
if (pszReturnBusName && cchReturnBusName) {
_tcsncpy(pszReturnBusName, pszBusName, cchToCopy);
pszReturnBusName[cchToCopy - 1] = 0;
dwRet = cchToCopy;
}
return dwRet;
}
SD_API_STATUS
CSdBusEnum::ActivateChild(
LPCTSTR pszChildDeviceKey,
DWORD dwHCNumber,
DWORD dwSlotNumber,
DWORD dwFunctionNumber,
SDCARD_DEVICE_CONTEXT *pDevice,
CSdDeviceFolder **ppDeviceFolder
)
{
DEBUGCHK(pDevice);
PREFAST_DEBUGCHK(ppDeviceFolder);
SD_API_STATUS status = SD_API_STATUS_UNSUCCESSFUL;
PCI_SLOT_NUMBER slotNumber = { dwSlotNumber, dwFunctionNumber };
CSdDeviceFolder *pNewChildFolder =
new CSdDeviceFolder(GetBusName(), pszChildDeviceKey, pDevice,
Internal, dwHCNumber, slotNumber, GetDeviceHandle(), 1);
if (pNewChildFolder) {
pNewChildFolder->AddRef();
// the reason we pass a regini structure is to make client drivers
// source code compatible with CE 3.0 where the card handle has to be
// retrieved from the registry. ActivateDeviceEx does not create the
// context key.
// this little task sets up the context key in the active path
REGINI riDevice = {
DEVLOAD_CLIENTINFO_VALNAME,
(PBYTE) &pDevice,
sizeof(pDevice),
DEVLOAD_CLIENTINFO_VALTYPE
};
if (pNewChildFolder->AddInitReg(1, &riDevice)) {
if (InsertChild(pNewChildFolder)) {
LPCTSTR pszChildBusName(pNewChildFolder->GetDeviceBusName());
DEBUGCHK(pszChildBusName);
if (DefaultBusDriver::ActivateChild(pszChildBusName)) {
status = SD_API_STATUS_SUCCESS;
*ppDeviceFolder = pNewChildFolder;
pNewChildFolder->AddRef(); // Add a reference
}
else {
BOOL fSuccess = RemoveChildByFolder(pNewChildFolder);
DEBUGCHK(fSuccess);
}
}
}
pNewChildFolder->DeRef(); // If failure, pNewChildFolder will be deleted here
}
return status;
}
SD_API_STATUS
CSdBusEnum::DeactivateChild(
CSdDeviceFolder *pDeviceFolder
)
{
PREFAST_DEBUGCHK(pDeviceFolder);
SD_API_STATUS status = SD_API_STATUS_UNSUCCESSFUL;
if (DefaultBusDriver::DeactivateChild(pDeviceFolder->GetDeviceBusName())) {
if (RemoveChildByFolder(pDeviceFolder)) {
pDeviceFolder->DeRef(); // This will delete the object
status = SD_API_STATUS_SUCCESS;
}
}
return status;
}
BOOL
CSdBusEnum::IOControl(DWORD dwCode, PBYTE pBufIn, DWORD dwLenIn, PBYTE pBufOut, DWORD dwLenOut,
PDWORD pdwActualOut)
{
BOOL fRet;
switch (dwCode) {
case IOCTL_BUS_TRANSLATE_BUS_ADDRESS:
case IOCTL_BUS_TRANSLATE_SYSTEM_ADDRESS:
case IOCTL_BUS_ACTIVATE_CHILD:
case IOCTL_BUS_DEACTIVATE_CHILD:
case IOCTL_BUS_IS_CHILD_REMOVED:
case IOCTL_BUS_GET_CONFIGURE_DATA:
case IOCTL_BUS_SET_CONFIGURE_DATA:
SetLastError(ERROR_NOT_SUPPORTED);
fRet = FALSE;
break;
default:
fRet = DefaultBusDriver::IOControl(dwCode, pBufIn, dwLenIn, pBufOut,
dwLenOut, pdwActualOut);
break;
};
return fRet;
}
BOOL
CSdBusEnum::SetChildDevicePowerState(
PCE_BUS_POWER_STATE pcbps
)
{
BOOL fRet = FALSE;
if ( (pcbps != NULL) && (pcbps->lpDeviceBusName != NULL) && (pcbps->lpceDevicePowerState!=NULL) ) {
CSdDeviceFolder * pDevice =
(CSdDeviceFolder *) GetChildByName(pcbps->lpDeviceBusName);
if (pDevice) {
fRet = pDevice->SetPowerState(*(pcbps->lpceDevicePowerState));
pDevice->DeRef();
}
}
return fRet;
}
BOOL
CSdBusEnum::GetChildDevicePowerState(
PCE_BUS_POWER_STATE pcbps
)
{
BOOL fRet = FALSE;
if ( (pcbps != NULL) && (pcbps->lpDeviceBusName != NULL) && (pcbps->lpceDevicePowerState!=NULL) ) {
CSdDeviceFolder * pDevice =
(CSdDeviceFolder *) GetChildByName(pcbps->lpDeviceBusName);
if (pDevice) {
fRet = TRUE;
*(pcbps->lpceDevicePowerState) = pDevice->GetPowerState();
pDevice->DeRef();
}
}
return fRet;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -