📄 mld_anwi.c
字号:
/* mld_anwi.c - access hardware for manlib */
/* Copyright (c) 2001 Atheros Communications, Inc., All Rights Reserved */
/*
DESCRIPTION
This module contains the functions to access the hardware for the dk_client
*/
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <malloc.h>
#include <assert.h>
#include <time.h>
#include <process.h>
#include <winioctl.h>
#ifndef LEGACY
#include <initguid.h>
#include <setupapi.h>
#endif
#include "wlantype.h"
#include "mld_anwi.h"
#include "anwiioctl.h"
#include "ar5210reg.h"
#include "athreg.h"
#include "manlib.h"
#ifndef LEGACY
#include "anwi_guid.h"
#endif
static FILE *logFile; /* file handle of logfile */
static A_BOOL logging; /* set to 1 if a log file open */
static A_BOOL enablePrint = 1;
static A_UINT16 quietMode; /* set to 1 for quiet mode, 0 for not */
static A_BOOL driverOpened;
/////////////////////////////////////////////////////////////////////////////
//GLOBAL VARIABLES
#ifdef LEGACY
HANDLE hDriver;
#endif
MDK_WLAN_DRV_INFO globDrvInfo; /* Global driver info */
/////////////////////////////////////////////////////////////////////////////
// FORWARD DECLARATIONS
A_UINT16 driverOpen();
void envCleanup(A_BOOL closeDriver);
void deviceCleanup(A_UINT16 devIndex);
BOOL WINAPI consoleCtrlHandler(DWORD dwCtrlType);
/**************************************************************************
* envInit - performs any intialization needed by the environment
*
* For the windows NT hardware environment, need to open the driver and
* perform any other initialization required by it
*
* RETURNS: A_OK if it works, A_ERROR if not
*/
A_STATUS envInit
(
A_BOOL debugMode,
A_BOOL openDriver
)
{
A_UINT16 i;
// quiet our optional uiPrintfs...
dk_quiet((A_UINT16)(debugMode ? 0 : 1));
// open a handle to the driver
// need not open the driver if art is controlling a remote client
driverOpened = 0;
if (openDriver) {
if (!driverOpen()) {
return A_ERROR;
}
driverOpened = 1;
}
globDrvInfo.devCount = 0;
// set all the devInfo pointers to null
for(i = 0; i < WLAN_MAX_DEV; i++)
globDrvInfo.pDevInfoArray[i] = NULL;
if ( !SetConsoleCtrlHandler(consoleCtrlHandler, TRUE) ) {
uiPrintf("Error: Unable to register CTRL-C handler, got %d from GetLastError()\n", GetLastError());
return A_ERROR;
}
return A_OK;
}
BOOL WINAPI consoleCtrlHandler
(
DWORD dwCtrlType
)
{
char* sType;
switch ( dwCtrlType ) {
case CTRL_C_EVENT:
sType = "CTRL_C_EVENT";
break;
case CTRL_BREAK_EVENT:
sType = "CTRL_BREAK_EVENT";
break;
case CTRL_CLOSE_EVENT:
sType = "CTRL_CLOSE_EVENT";
break;
case CTRL_LOGOFF_EVENT:
sType = "CTRL_LOGOFF_EVENT";
break;
default:
sType = "UNKNOWN event";
}
uiPrintf("!!! Received %s.... Cleaning up and closing down !!!\n", sType);
// MDS: should possibly worry about reentrancy into envCleanup(), but
// there's only a very small probability of that happening, so we won't
// worry about it right now....
envCleanup(TRUE);
ExitProcess(1);
return TRUE; // we handled the situation, so we return TRUE
}
void envCleanup
(
A_BOOL closeDriver
)
{
A_UINT16 i;
// cleanup all the devInfo structures
for ( i = 0; i < WLAN_MAX_DEV; i++ ) {
if ( globDrvInfo.pDevInfoArray[i] ) {
deviceCleanup(i);
}
}
globDrvInfo.devCount = 0;
// close the handle to the driver
if ((closeDriver) && (driverOpened)) {
#ifdef LEGACY
CloseHandle (hDriver);
#endif
driverOpened = 0;
}
}
#ifdef LEGACY
// This function tries to register ourself with the wdm.
BOOL RegisterClient(int index,pAnwiOutClientInfo pRetCliInfo)
{
A_BOOL status;
A_UINT32 bR = 0;
anwiReturnContext returnContext;
pAnwiVersionInfo pAnwiVer;
anwiVersionInfo anwiVer;
anwiInClientInfo cliInInfo;
pAnwiOutClientInfo pCliOutInfo;
ULONG extraLen;
status = DeviceIoControl (hDriver,
IOCTL_ANWI_GET_VERSION,
NULL,
0,
&returnContext,
sizeof(anwiReturnContext),
&bR,
NULL);
if ((!status) || (returnContext.returnCode != ANWI_OK)) {
printf("Error returned by get_version DeviceIoControl call \n");
return FALSE;
}
if (returnContext.contextLen != sizeof(anwiVersionInfo)) {
printf("Return size (%d) from get version DeviceIoControl call doesnt match the expected size (%d) \n",returnContext.contextLen,sizeof(anwiVersionInfo));
return FALSE;
}
pAnwiVer = (pAnwiVersionInfo) returnContext.context;
if (pAnwiVer->majorVersion != ANWI_MAJOR_VERSION) {
printf("Driver Version doesnt match with the client version \n");
return FALSE;
}
printf("ANWI %d.%d \n",pAnwiVer->majorVersion,pAnwiVer->minorVersion);
memcpy(&anwiVer, pAnwiVer, sizeof(anwiVersionInfo));
cliInInfo.index = index;
cliInInfo.baseAddress = 0;
cliInInfo.irqLevel = 0;
status = DeviceIoControl (hDriver,
IOCTL_ANWI_DEVICE_OPEN,
&cliInInfo,
sizeof(anwiInClientInfo),
&returnContext,
sizeof(anwiReturnContext),
&bR,
NULL);
if ((!status) || (returnContext.returnCode != ANWI_OK)) {
printf("Error returned by device open DeviceIoControl call \n");
return FALSE;
}
// backward compatible with 1.0 driver
if (anwiVer.minorVersion == 0) {
extraLen = sizeof(pCliOutInfo->regRange) + sizeof(pCliOutInfo->memSize);
} else {
extraLen = 0;
}
if (returnContext.contextLen != (sizeof(anwiOutClientInfo) - extraLen)) {
printf("Return size (%d) from device open DeviceIoControl call doesnt match the expected size (%d) \n",returnContext.contextLen,sizeof(anwiOutClientInfo));
return -1;
}
pCliOutInfo = (pAnwiOutClientInfo) returnContext.context;
memcpy(pRetCliInfo, pCliOutInfo, (sizeof(anwiOutClientInfo) - extraLen));
if (anwiVer.minorVersion == 0) {
pRetCliInfo->regRange = 65536;
pRetCliInfo->memSize = 1024 * 1024; // 1 MB
}
return TRUE;
}
A_BOOL unRegisterClient(int cliId)
{
A_BOOL status;
A_UINT32 bR = 0;
anwiReturnContext returnContext;
anwiUnRegisterClientInfo unRegInfo;
unRegInfo.cliId = cliId;
status = DeviceIoControl (hDriver,
IOCTL_ANWI_DEVICE_CLOSE,
&unRegInfo,
sizeof(anwiUnRegisterClientInfo),
&returnContext,
sizeof(anwiReturnContext),
&bR,
NULL);
if ((!status) || (returnContext.returnCode != ANWI_OK)) {
printf("Error returned by device close DeviceIoControl call \n");
return FALSE;
}
return TRUE;
}
#else
HANDLE getDeviceViaInterface(GUID *pGuid, DWORD instance)
{
HDEVINFO info;
SP_INTERFACE_DEVICE_DATA ifdata;
DWORD ReqLen;
PSP_INTERFACE_DEVICE_DETAIL_DATA ifDetail;
HANDLE handle;
info = SetupDiGetClassDevs(pGuid,NULL,NULL,DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);
if (info == INVALID_HANDLE_VALUE) {
printf("No HDEVINFO available for this GUID \n");
return INVALID_HANDLE_VALUE;
}
ifdata.cbSize = sizeof(ifdata);
if (!SetupDiEnumDeviceInterfaces(info,NULL,pGuid, instance, &ifdata)) {
printf("No SP_INTERFACE_DEVICE_DATA availabe for this GUID instance \n");
SetupDiDestroyDeviceInfoList(info);
return INVALID_HANDLE_VALUE;
}
SetupDiGetDeviceInterfaceDetail(info,&ifdata,NULL,0,&ReqLen,NULL);
ifDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc(ReqLen*sizeof(char));
if (ifDetail == NULL) {
printf("cannot allocate memory\n");
SetupDiDestroyDeviceInfoList(info);
return INVALID_HANDLE_VALUE;
}
// Get symbolic link name
ifDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
if (!SetupDiGetDeviceInterfaceDetail(info,&ifdata,ifDetail,ReqLen,NULL,NULL)) {
printf("No PSP_INTERFACE_DEVICE_DETAIL_DATA availabe for this GUID instance \n");
free(ifDetail);
SetupDiDestroyDeviceInfoList(info);
return INVALID_HANDLE_VALUE;
}
// printf("Symbolic name is %s \n",ifDetail->DevicePath);
handle = CreateFile(ifDetail->DevicePath,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
free(ifDetail);
SetupDiDestroyDeviceInfoList(info);
return handle;
}
int getClientInfo(HANDLE hDevice,pAnwiOutClientInfo pRetCliInfo)
{
BOOL status;
ULONG bR = 0;
anwiReturnContext returnContext;
pAnwiVersionInfo pAnwiVer;
anwiVersionInfo anwiVer;
anwiInClientInfo cliInInfo;
pAnwiOutClientInfo pCliOutInfo;
ULONG extraLen;
status = DeviceIoControl (hDevice,
IOCTL_ANWI_GET_VERSION,
NULL,
0,
&returnContext,
sizeof(anwiReturnContext),
&bR,
NULL);
if ((!status) || (returnContext.returnCode != ANWI_OK)) {
printf("Error returned by get_version DeviceIoControl call \n");
return -1;
}
if (returnContext.contextLen != sizeof(anwiVersionInfo)) {
printf("Return size (%d) from get version DeviceIoControl call doesnt match the expected size (%d) \n",returnContext.contextLen,sizeof(anwiVersionInfo));
return -1;
}
pAnwiVer = (pAnwiVersionInfo) returnContext.context;
if (pAnwiVer->majorVersion != ANWI_MAJOR_VERSION) {
printf("Driver Version doesnt match with the client version \n");
return -1;
}
printf("ANWI %d.%d \n",pAnwiVer->majorVersion,pAnwiVer->minorVersion);
memcpy(&anwiVer, pAnwiVer, sizeof(anwiVersionInfo));
cliInInfo.baseAddress = 0;
cliInInfo.irqLevel = 0;
status = DeviceIoControl (hDevice,
IOCTL_ANWI_GET_CLIENT_INFO,
&cliInInfo,
sizeof(anwiInClientInfo),
&returnContext,
sizeof(anwiReturnContext),
&bR,
NULL);
if ((!status) || (returnContext.returnCode != ANWI_OK)) {
printf("Error returned by device open DeviceIoControl call \n");
return -1;
}
// backward compatible with 1.0 driver
if (anwiVer.minorVersion == 0) {
extraLen = sizeof(pCliOutInfo->regRange) + sizeof(pCliOutInfo->memSize);
} else {
extraLen = 0;
}
if (returnContext.contextLen != (sizeof(anwiOutClientInfo) - extraLen)) {
printf("Return size (%d) from device open DeviceIoControl call doesnt match the expected size (%d) \n",returnContext.contextLen,sizeof(anwiOutClientInfo));
return -1;
}
pCliOutInfo = (pAnwiOutClientInfo) returnContext.context;
memcpy(pRetCliInfo, pCliOutInfo, (sizeof(anwiOutClientInfo) - extraLen));
if (anwiVer.minorVersion == 0) {
pRetCliInfo->regRange = 65536;
pRetCliInfo->memSize = 1024 * 1024; // 1 MB
}
#ifndef CUSTOMER_REL
printf("Mem phy addr = %x vir addr = %x \n",pCliOutInfo->memPhyAddr,pCliOutInfo->memVirAddr);
printf("Reg phy addr = %x vir addr = %x \n",pCliOutInfo->regPhyAddr,pCliOutInfo->regVirAddr);
#endif
return 0;
}
#endif // LEGACY
/**************************************************************************
* deviceInit - performs any initialization needed for a device
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -