⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wdc_general.c

📁 WinDriver目录下的源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/****************************************************************
* File: wdc_general.c - Implementation of general WDC API -     *
*       init/uninit driver/WDC lib; open/close device           *
*                                                               *
*  Copyright (c) 2005 Jungo Ltd.  http://www.jungo.com          *
*****************************************************************/

#include "utils.h"
#include "wdc_lib.h"
#include "wdc_defs.h"
#include "wdc_err.h"

/*************************************************************
  General definitions
 *************************************************************/
#if defined(WINCE)
    #define strdup _strdup
#endif

/* Demo WinDriver license registration string */
#define WDC_DEMO_LICENSE_STR "12345abcde1234.license"

static HANDLE ghWD = INVALID_HANDLE_VALUE; 

/*************************************************************
  Static functions prototypes
 *************************************************************/
/* -----------------------------------------------    
    Open/close device
   ----------------------------------------------- */
#if !defined (__KERNEL__)
static DWORD PciSlotToId(const WD_PCI_SLOT *pSlot, WD_PCI_ID *pId);
static DWORD PcmciaSlotToId(const WD_PCMCIA_SLOT *pSlot, WD_PCMCIA_ID *pId);
static PWDC_DEVICE DeviceCreate(const PVOID pDeviceInfo, const PVOID pDevCtx,
    WD_BUS_TYPE bus);
static void DeviceDestroy(PWDC_DEVICE pDev);
static DWORD DeviceOpen(WDC_DEVICE_HANDLE *phDev, const PVOID pDeviceInfo,
    const PVOID pDevCtx, const CHAR *pcKPDriverName,
    PVOID pKPOpenData, WD_BUS_TYPE bus);
static DWORD DeviceClose(WDC_DEVICE_HANDLE hDev);
static DWORD KernelPlugInOpen(WDC_DEVICE_HANDLE hDev, 
    const CHAR *pcKPDriverName, PVOID pKPOpenData);
static DWORD SetDeviceInfo(PWDC_DEVICE pDev);
#endif

/*************************************************************
  Functions implementation
 *************************************************************/
/* -----------------------------------------------
    General functions
   ----------------------------------------------- */
/* Get handle to WinDriver (required for WD_XXX functions) */
HANDLE DLLCALLCONV WDC_GetWDHandle(void)
{
    return ghWD;
}

PVOID DLLCALLCONV WDC_GetDevContext(WDC_DEVICE_HANDLE hDev)
{
    return hDev ? ((PWDC_DEVICE)hDev)->pCtx : NULL;
}

/* Get device's bus type */
WD_BUS_TYPE DLLCALLCONV WDC_GetBusType(WDC_DEVICE_HANDLE hDev)
{
    if (!WdcIsValidDevHandle(hDev))
    {
        WDC_Err("WDC_GetBusType: %s", WdcGetLastErrStr());
        return WD_BUS_UNKNOWN;
    }

    return ((PWDC_DEVICE)hDev)->cardReg.Card.Item[0].I.Bus.dwBusType;
}

/* Sleep (default option - WDC_SLEEP_BUSY) */
DWORD DLLCALLCONV WDC_Sleep(DWORD dwMicroSecs, WDC_SLEEP_OPTIONS options)
{
    WD_SLEEP slp;
  
    BZERO(slp);
    slp.dwMicroSeconds = dwMicroSecs;
    slp.dwOptions = options;

    return WD_Sleep(ghWD, &slp);
}

/* -----------------------------------------------
    Open/close driver 
   ----------------------------------------------- */
DWORD DLLCALLCONV WDC_DriverOpen(WDC_DRV_OPEN_OPTIONS openOptions,
    const CHAR *sLicense)
{
#if !defined(__KERNEL__)
    DWORD dwStatus;
#endif
    
    if (ghWD != INVALID_HANDLE_VALUE)
        return WD_OPERATION_ALREADY_DONE;

    /* Open a handle to WinDriver */
    ghWD = WD_Open();
    if (ghWD == INVALID_HANDLE_VALUE)
        return WD_INVALID_HANDLE;  

#if defined(__KERNEL__)
    return WD_STATUS_SUCCESS;
#else
    /* Compare WinDriver files versions with running WinDriver kernel module version */
    if (openOptions & WDC_DRV_OPEN_CHECK_VER)
    {
        WD_VERSION ver;
        BZERO(ver);
        dwStatus = WD_Version(ghWD, &ver);
        if (WD_STATUS_SUCCESS != dwStatus)
        {
            WDC_Err("WDC_DriverOpen: Version check failed. Error 0x%lx - %s\n",
                dwStatus, Stat2Str(dwStatus));
            goto Error;
        }
    }
    
    /* Register WinDriver license registration string */
    if (openOptions & WDC_DRV_OPEN_REG_LIC)
    {
        WD_LICENSE lic;
        BZERO(lic);
        
        if (sLicense && strcmp(sLicense, ""))
            strcpy(lic.cLicense, sLicense);
        else
        {
            WDC_Trace("WDC_DriverOpen: No license to register -> attempting to register"
                "demo WinDriver license\n");
            strcpy(lic.cLicense, WDC_DEMO_LICENSE_STR);
        }

        dwStatus = WD_License(ghWD, &lic);
        if (WD_STATUS_SUCCESS != dwStatus)
        {
            WDC_Err("WDC_DriverOpen: Failed registering WinDriver license. "
                "Error 0x%lx - %s\n",   dwStatus, Stat2Str(dwStatus));
            goto Error;
        }
    }

    return WD_STATUS_SUCCESS;
    
Error:
    WDC_DriverClose();
    return dwStatus;
#endif
}

DWORD DLLCALLCONV WDC_DriverClose(void)
{
    WDC_SetDebugOptions(WDC_DBG_NONE, NULL);

    if (INVALID_HANDLE_VALUE != ghWD)
    {
        WD_Close(ghWD);
        ghWD = INVALID_HANDLE_VALUE;
    }

    return WD_STATUS_SUCCESS;
}

/* -----------------------------------------------
    Scan bus (PCI/PCMCIA)
   ----------------------------------------------- */
DWORD DLLCALLCONV WDC_PciScanDevices(DWORD dwVendorId, DWORD dwDeviceId,
    WDC_PCI_SCAN_RESULT *pPciScanResult)
{
    DWORD dwStatus, i;
    WD_PCI_SCAN_CARDS scanDevices;
    
    WDC_Trace("WDC_PciScanDevices entered\n");

    if (!WdcIsValidPtr(pPciScanResult,
        "NULL pointer to device scan results struct"))
    {
        return WD_INVALID_PARAMETER;
    }
    
    BZERO(scanDevices);
    scanDevices.searchId.dwVendorId = dwVendorId;
    scanDevices.searchId.dwDeviceId = dwDeviceId;
 
    dwStatus = WD_PciScanCards(ghWD, &scanDevices);
    if (WD_STATUS_SUCCESS != dwStatus)
    {
        WDC_Err("WDC_PciScanDevices: Failed scanning PCI bus. "
            "Error 0x%lx - %s\n",
            dwStatus, Stat2Str(dwStatus));
        return dwStatus;
    }

    BZERO(*pPciScanResult);
    pPciScanResult->dwNumDevices = scanDevices.dwCards;
    
    for (i = 0; i < scanDevices.dwCards; i++)
    {
        pPciScanResult->deviceId[i] = scanDevices.cardId[i];
        pPciScanResult->deviceSlot[i] = scanDevices.cardSlot[i];
    }

    WDC_Trace("WDC_PciScanDevices: PCI bus scanned successfully.\n"
        "Found %ld matching cards (vendor ID 0x%lx, device ID 0x%lx)\n",
        pPciScanResult->dwNumDevices, dwVendorId, dwDeviceId);

    return WD_STATUS_SUCCESS;
}

DWORD DLLCALLCONV WDC_PcmciaScanDevices(WORD wManufacturerId, WORD wDeviceId,
    WDC_PCMCIA_SCAN_RESULT *pPcmciaScanResult)
{
    DWORD dwStatus, i;
    WD_PCMCIA_SCAN_CARDS scanDevices;
    
    WDC_Trace("WDC_PcmciaScanDevices entered\n");

    if (!WdcIsValidPtr(pPcmciaScanResult,
        "NULL pointer to device scan results struct"))
    {
        return WD_INVALID_PARAMETER;
    }
    
    BZERO(scanDevices);
    scanDevices.searchId.wManufacturerId = wManufacturerId;
    scanDevices.searchId.wCardId = wDeviceId;
 
    dwStatus = WD_PcmciaScanCards(ghWD, &scanDevices);
    if (WD_STATUS_SUCCESS != dwStatus)
    {
        WDC_Err("WDC_PcmciaScanDevices: Failed scanning PCMCIA bus. "
            "Error 0x%lx - %s\n",
            dwStatus, Stat2Str(dwStatus));
        return dwStatus;
    }

    BZERO(*pPcmciaScanResult);
    
    pPcmciaScanResult->dwNumDevices = scanDevices.dwCards;
    
    for (i = 0; i < scanDevices.dwCards; i++)
    {
        pPcmciaScanResult->deviceId[i] = scanDevices.cardId[i];
        pPcmciaScanResult->deviceSlot[i] = scanDevices.cardSlot[i];
    }

    WDC_Trace("WDC_PcmciaScanDevices: PCMCIA bus scanned successfully.\n"
        "Found %ld matching cards (manufacturer ID 0x%lx, device ID 0x%lx)\n",
        pPcmciaScanResult->dwNumDevices, wManufacturerId, wDeviceId);

    return WD_STATUS_SUCCESS;
}

/* -----------------------------------------------
    Get device's resources information (PCI/PCMCIA)
   ----------------------------------------------- */
DWORD DLLCALLCONV WDC_PciGetDeviceInfo(WD_PCI_CARD_INFO *pDeviceInfo)
{
    if (!WdcIsValidPtr(pDeviceInfo, "NULL pointer to card information struct"))
    {
        WDC_Err("WDC_PciGetDeviceInfo: %s", WdcGetLastErrStr());
        return WD_INVALID_PARAMETER;
    }
    
    return WD_PciGetCardInfo(ghWD, pDeviceInfo);
}
        
DWORD DLLCALLCONV WDC_PcmciaGetDeviceInfo(WD_PCMCIA_CARD_INFO *pDeviceInfo)
{
    if (!WdcIsValidPtr(pDeviceInfo, "NULL pointer to card information struct"))
    {
        WDC_Err("WDC_PcmciaDeviceInfo: %s", WdcGetLastErrStr());
        return WD_INVALID_PARAMETER;
    }
    
    return WD_PcmciaGetCardInfo(ghWD, pDeviceInfo);
}

/* -----------------------------------------------
    Open/close device
   ----------------------------------------------- */
#if !defined (__KERNEL__)
DWORD DLLCALLCONV WDC_PciDeviceOpen(WDC_DEVICE_HANDLE *phDev,
    const WD_PCI_CARD_INFO *pDeviceInfo, const PVOID pDevCtx,
    PVOID reserved, const CHAR *pcKPDriverName, PVOID pKPOpenData)
{
    DWORD dwStatus;
    
    dwStatus = DeviceOpen(phDev, (const PVOID)pDeviceInfo, pDevCtx,
        pcKPDriverName, pKPOpenData, WD_BUS_PCI);
    if (WD_STATUS_SUCCESS != dwStatus)
        WDC_Err("WDC_PciDeviceOpen failed: %s", WdcGetLastErrStr());

    return dwStatus;
}

DWORD DLLCALLCONV WDC_PcmciaDeviceOpen(WDC_DEVICE_HANDLE *phDev,
    const WD_PCMCIA_CARD_INFO *pDeviceInfo, const PVOID pDevCtx,
    PVOID reserved, const CHAR *pcKPDriverName, PVOID pKPOpenData)
{
    DWORD dwStatus;
    
    dwStatus = DeviceOpen(phDev, (const PVOID)pDeviceInfo, pDevCtx,
        pcKPDriverName, pKPOpenData, WD_BUS_PCMCIA);
    if (WD_STATUS_SUCCESS != dwStatus)
        WDC_Err("WDC_PcmciaDeviceOpen failed: %s", WdcGetLastErrStr());

    return dwStatus;
}

DWORD DLLCALLCONV WDC_IsaDeviceOpen(WDC_DEVICE_HANDLE *phDev,
    const WD_CARD *pDeviceInfo, const PVOID pDevCtx,
    PVOID reserved, const CHAR *pcKPDriverName, PVOID pKPOpenData)
{
    DWORD dwStatus;
    
    dwStatus = DeviceOpen(phDev, (const PVOID)pDeviceInfo, pDevCtx,
        pcKPDriverName, pKPOpenData, WD_BUS_ISA);
    if (WD_STATUS_SUCCESS != dwStatus)
        WDC_Err("WDC_IsaDeviceOpen failed: %s", WdcGetLastErrStr());

    return dwStatus;
}

DWORD DLLCALLCONV WDC_PciDeviceClose(WDC_DEVICE_HANDLE hDev)
{
    DWORD dwStatus;

    dwStatus = DeviceClose(hDev);
    if (WD_STATUS_SUCCESS != dwStatus)
        WDC_Err("WDC_PciDeviceClose failed: %s", WdcGetLastErrStr());

    return dwStatus;
}

DWORD DLLCALLCONV WDC_PcmciaDeviceClose(WDC_DEVICE_HANDLE hDev)
{
    DWORD dwStatus;

    dwStatus = DeviceClose(hDev);
    if (WD_STATUS_SUCCESS != dwStatus)
        WDC_Err("WDC_PcmciaDeviceClose failed: %s", WdcGetLastErrStr());

    return dwStatus;
}

DWORD DLLCALLCONV WDC_IsaDeviceClose(WDC_DEVICE_HANDLE hDev)
{    
    DWORD dwStatus;

    dwStatus = DeviceClose(hDev);
    if (WD_STATUS_SUCCESS != dwStatus)
        WDC_Err("WDC_IsaDeviceClose failed: %s", WdcGetLastErrStr());

    return dwStatus;
}

/* -----------------------------------------------
    Open Kernel PlugIn Driver
   ----------------------------------------------- */
DWORD DLLCALLCONV WDC_KernelPlugInOpen(WDC_DEVICE_HANDLE hDev, 
    const CHAR *pcKPDriverName, PVOID pKPOpenData)
{
    if (!WdcIsValidDevHandle(hDev))
    {
        WDC_Err("WDC_KernelPlugInOpen: %s", WdcGetLastErrStr());
        return WD_INVALID_HANDLE;
    }
        
    if (!pcKPDriverName || !strcmp(pcKPDriverName, ""))
    {
        WdcSetLastErrStr("Error - empty kernel-plugin driver name\n");
        return WD_INVALID_PARAMETER;
    }

    return KernelPlugInOpen(hDev, pcKPDriverName, pKPOpenData);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -