📄 cspi_io.cpp
字号:
//-----------------------------------------------------------------------------
//
// Function: SPI_Read
//
// This function reads data from the device identified by the open
// context.
//
// Parameters:
// hOpenContext
// [in] Handle to the open context of the device. The XXX_Open
// function creates and returns this identifier.
// pBuffer
// [out] Pointer to the buffer that stores the data read from the
// device. This buffer should be at least Count bytes long.
// Count
// [in] Number of bytes to read from the device into pBuffer.
//
// Returns:
// Returns zero to indicate end-of-file. Returns -1 to indicate an
// error. Returns the number of bytes read to indicate success.
//
//-----------------------------------------------------------------------------
DWORD SPI_Read(DWORD hOpenContext, LPVOID pBuffer, DWORD Count)
{
return 0;
}
//-----------------------------------------------------------------------------
//
// Function: SPI_Write
//
// This function writes data to the device.
//
// Parameters:
// hOpenContext
// [in] Handle to the open context of the device. The XXX_Open
// function creates and returns this identifier.
// pBuffer
// [out] Pointer to the buffer that contains the data to write.
// Count
// [in] Number of bytes to read from the device into pBuffer.
//
// Returns:
// The number of bytes written indicates success. A value of -1 indicates
// failure.
//
//-----------------------------------------------------------------------------
DWORD SPI_Write(DWORD hOpenContext, LPCVOID pBuffer, DWORD dwNumBytes)
{
return 0;
}
//-----------------------------------------------------------------------------
//
// Function: SPI_Seek
//
// This function moves the data pointer in the device.
//
// Parameters:
// hOpenContext
// [in] Handle to the open context of the device. The XXX_Open
// function creates and returns this identifier.
// Amount
// [in] Number of bytes to move the data pointer in the device.
// A positive value moves the data pointer toward the end of the
// file, and a negative value moves it toward the beginning.
// Type
// [in] Starting point for the data pointer.
//
// Returns:
// The new data pointer for the device indicates success. A value of -1
// indicates failure.
//
//-----------------------------------------------------------------------------
DWORD SPI_Seek(DWORD hOpenContext, long Amount, WORD Type)
{
return -1;
}
//-----------------------------------------------------------------------------
//
// Function: SPI_IOControl
//
// This function sends a command to a device.
//
// Parameters:
// hOpenContext
// [in] Handle to the open context of the device. The XXX_Open
// function creates and returns this identifier.
// dwCode
// [in] I/O control operation to perform. These codes are
// device-specific and are usually exposed to developers through
// a header file.
// pBufIn
// [in] Pointer to the buffer containing data to transfer to the
// device.
// dwLenIn
// [in] Number of bytes of data in the buffer specified for pBufIn.
// pBufOut
// [out] Pointer to the buffer used to transfer the output data
// from the device.
// dwLenOut
// [in] Maximum number of bytes in the buffer specified by pBufOut.
// pdwActualOut
// [out] Pointer to the DWORD buffer that this function uses to
// return the actual number of bytes received from the device.
//
// Returns:
// The new data pointer for the device indicates success. A value of -1
// indicates failure.
//
//-----------------------------------------------------------------------------
BOOL SPI_IOControl(DWORD hOpenContext, DWORD dwCode, PBYTE pBufIn,
DWORD dwLenIn, PBYTE pBufOut, DWORD dwLenOut,
PDWORD pdwActualOut)
{
BOOL bRet = FALSE;
cspiClass *pCspi = (cspiClass *)hOpenContext;
DEBUGMSG (ZONE_IOCTL|ZONE_FUNCTION, (TEXT("SPI_IOControl: hOpenContext=0x%x\r\n"),hOpenContext));
if (pCspi)
{
switch (dwCode)
{
case CSPI_IOCTL_EXCHANGE:
// enqueue the exchange packet
bRet = pCspi->CspiEnqueue(*((PCSPI_XCH_PKT_T *) pBufIn));
DEBUGMSG(!bRet, (TEXT("SPI_IOControl: CSPI_IOCTL_EXCHANGE failed\r\n")));
break;
case IOCTL_POWER_CAPABILITIES:
// Tell the power manager about ourselves.
if (pBufOut != NULL
&& dwLenOut >= sizeof(POWER_CAPABILITIES)
&& pdwActualOut != NULL)
{
__try
{
PPOWER_CAPABILITIES ppc = (PPOWER_CAPABILITIES) pBufOut;
memset(ppc, 0, sizeof(*ppc));
ppc->DeviceDx = DX_MASK(D0) | DX_MASK(D4);
*pdwActualOut = sizeof(*ppc);
bRet = TRUE;
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
ERRORMSG(TRUE, (_T("Exception in CSPI IOCTL_POWER_CAPABILITIES\r\n")));
}
}
break;
case IOCTL_POWER_SET:
if(pBufOut != NULL
&& dwLenOut == sizeof(CEDEVICE_POWER_STATE)
&& pdwActualOut != NULL)
{
__try
{
CEDEVICE_POWER_STATE dx = *(PCEDEVICE_POWER_STATE) pBufOut;
if (VALID_DX(dx))
{
// Any request that is not D0 becomes a D4 request
if (dx != D0)
{
dx = D4;
}
*(PCEDEVICE_POWER_STATE) pBufOut = dx;
*pdwActualOut = sizeof(CEDEVICE_POWER_STATE);
// change the current power state
pCspi->m_dxCurrent = dx;
// If we are turning off
if (dx == D4)
{
// polling mode
pCspi->m_bUsePolling = TRUE;
}
// Else we are powering on
else
{
// Interrupt mode
pCspi->m_bUsePolling = FALSE;
}
// Leave
bRet = TRUE;
}
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
ERRORMSG(TRUE, (_T("Exception in CSPI IOCTL_POWER_CAPABILITIES\r\n")));
}
}
break;
case IOCTL_POWER_GET:
if(pBufOut != NULL
&& dwLenOut == sizeof(CEDEVICE_POWER_STATE)
&& pdwActualOut != NULL)
{
// Just return our current Dx value
__try
{
*(PCEDEVICE_POWER_STATE) pBufOut = pCspi->m_dxCurrent; //getCurrentPowerState();
*pdwActualOut = sizeof(CEDEVICE_POWER_STATE);
bRet = TRUE;
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
ERRORMSG(TRUE, (_T("Exception in CSPI IOCTL_POWER_CAPABILITIES\r\n")));
}
}
break;
default:
break;
}
}
DEBUGMSG (ZONE_IOCTL|ZONE_FUNCTION, (TEXT("SPI_IOControl -\r\n")));
return bRet;
}
//-----------------------------------------------------------------------------
// The main dll entry point
//
// Parameters:
// hInstDll
// The instance that is attaching
// dwReason
// The reason for attaching
// lpvReserved
// Specifies further aspects of DLL initialization and cleanup
//
// Returns:
// The function returns TRUE if it succeeds
// or FALSE if initialization fails.
//
//-----------------------------------------------------------------------------
BOOL WINAPI CSPI_DllEntry(HANDLE hInstDll, DWORD dwReason, LPVOID lpvReserved)
{
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
DEBUGMSG(ZONE_FUNCTION, (TEXT("CSPI_DllEntry: DLL_PROCESS_ATTACH lpvReserved(0x%x)\r\n"),lpvReserved));
DisableThreadLibraryCalls((HMODULE) hInstDll);
break;
case DLL_PROCESS_DETACH:
DEBUGMSG(ZONE_FUNCTION, (TEXT("CSPI_DllEntry: DLL_PROCESS_DETACH lpvReserved(0x%x)\r\n"),lpvReserved));
break;
}
// return TRUE for success
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -