📄 stcusbce.c
字号:
//
// 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.
//
/*++
Module Name:
stcusbce.c
Abstract:
Smart Card Reader Driver for Windows CE.
--*/
#include <stdio.h>
#include <smclib.h>
#include <devload.h>
#include <tchar.h>
#include "common.h"
#include "stcusbce.h"
#include "stccb.h"
#include "stccmd.h"
#include <bldver.h>
const TCHAR szDriverName[] = TEXT("STCUSB");
const TCHAR szStreamDriverKey[] = TEXT("Drivers\\USB\\StcUsb");
const TCHAR szDriverPrefix[] = TEXT("SCR");
// Used to synchronize access to global driver data structures
CRITICAL_SECTION g_DriverCritSect;
#ifdef DEBUG
DBGPARAM dpCurSettings = {
TEXT("STCUSB"), {
TEXT("Ioctl"), TEXT("ATR"), TEXT("Protocol"), TEXT("Driver"),
TEXT("Trace"),TEXT("Error"), TEXT("break"),TEXT("all"),
TEXT(" unused"),TEXT("unused"),TEXT("unused"),TEXT("unused"),
TEXT("unused"),TEXT("unused"),TEXT("unused"),TEXT("unused") },
//0xFF //DEBUG_ALL
0x00000020 // DEBUG_ERROR
};
#define DEBUG_BREAK_ON_INIT (0x40 & dpCurSettings.ulZoneMask)
#endif // DEBUG
// allow upto MAX_PSCR_DEVICES
PSMARTCARD_EXTENSION DeviceSlot[STCUSB_MAX_DEVICE];
DWORD UnitNo=0;
extern STC_REGISTER STCInitialize[];
extern STC_REGISTER STCClose[];
static const USB_DRIVER_SETTINGS g_DriverSettings
= { sizeof(USB_DRIVER_SETTINGS),
STCUSB_VENDOR_ID, STCUSB_PRODUCT_ID, USB_NO_INFO,
USB_NO_INFO, USB_NO_INFO, USB_NO_INFO,
USB_NO_INFO, USB_NO_INFO, USB_NO_INFO
};
BOOL WINAPI DeviceNotify(LPVOID lpvNotifyParameter,DWORD dwCode,LPDWORD *a,LPDWORD *b ,LPDWORD *c ,LPDWORD *d )
{
BOOL fRet;
PSMARTCARD_EXTENSION pSCE = (PSMARTCARD_EXTENSION)lpvNotifyParameter;
PREADER_EXTENSION readerExtension;
switch(dwCode) {
case USB_CLOSE_DEVICE:
SmartcardDebug(DEBUG_TRACE,(TEXT("STCUSB! USB_CLOSE_DEVICE ")));
readerExtension = pSCE->ReaderExtension;
fRet = DeactivateDevice(readerExtension->hStreamDevice);
ASSERT(fRet);
if (readerExtension->pPipeIn)
readerExtension->pUsbFuncs->lpClosePipe(readerExtension->pPipeIn);
if (readerExtension->pPipeOut)
readerExtension->pUsbFuncs->lpClosePipe(readerExtension->pPipeOut);
LocalFree(pSCE);
return TRUE;
}
return FALSE;
}
USBDeviceAttach(
USB_HANDLE hDevice, // @parm [IN] - USB device handle
LPCUSB_FUNCS lpUsbFuncs, // @parm [IN] - Pointer to USBDI function table
LPCUSB_INTERFACE lpInterface, // @parm [IN] - If client is being loaded as an interface
// driver, contains a pointer to the USB_INTERFACE
// structure that contains interface information.
// If client is not loaded for a specific interface,
// this parameter will be NULL, and the client
// may get interface information through <f FindInterface>
LPCWSTR szUniqueDriverId, // @parm [IN] - Contains client driver id string
LPBOOL fAcceptControl, // @parm [OUT]- Filled in with TRUE if we accept control
// of the device, or FALSE if USBD should continue
// to try to load client drivers.
LPCUSB_DRIVER_SETTINGS lpDriverSettings,// @parm [IN] - Contains pointer to USB_DRIVER_SETTINGS
// struct that indicates how we were loaded.
DWORD dwUnused) // @parm [IN] - Reserved for use with future versions of USBD
{
DWORD dwIndex;
PSMARTCARD_EXTENSION pSCE=NULL;
USB_PIPE pInPipe=NULL;
USB_PIPE pOutPipe=NULL;
SmartcardDebug(DEBUG_TRACE,(TEXT("STCUSB: Device Attach")));
*fAcceptControl = FALSE;
#ifdef DEBUG
if (DEBUG_BREAK_ON_INIT)
DebugBreak();
#endif
if (!lpInterface)
{
LPCUSB_DEVICE pUsbDevice;
pUsbDevice = lpUsbFuncs->lpGetDeviceInfo(hDevice);
if (!pUsbDevice || ! (lpInterface = lpUsbFuncs->lpFindInterface(pUsbDevice,0,0)))
{
return FALSE;
}
}
SmartcardDebug(DEBUG_TRACE,(TEXT("STCUSB: DeviceAttach, IF %u, #EP:%u, Class:%u, Sub:%u, Prot:%u\r\n"),
lpInterface->Descriptor.bInterfaceNumber,lpInterface->Descriptor.bNumEndpoints,
lpInterface->Descriptor.bInterfaceClass,lpInterface->Descriptor.bInterfaceSubClass,
lpInterface->Descriptor.bInterfaceProtocol));
for (dwIndex=0;dwIndex<2 && dwIndex < lpInterface->dwCount;dwIndex++)
{// search end point.
if (USB_ENDPOINT_DIRECTION_IN(lpInterface->lpEndpoints[dwIndex].Descriptor.bEndpointAddress))
{
if (pInPipe==NULL &&
(lpInterface->lpEndpoints[dwIndex].Descriptor.bmAttributes & USB_ENDPOINT_TYPE_MASK)
==USB_ENDPOINT_TYPE_BULK)
{
pInPipe=(*lpUsbFuncs->lpOpenPipe)(hDevice,&lpInterface->lpEndpoints[dwIndex].Descriptor);
ASSERT(pInPipe);
}
}
else
{
if (pOutPipe==NULL &&
(lpInterface->lpEndpoints[dwIndex].Descriptor.bmAttributes & USB_ENDPOINT_TYPE_MASK)
==USB_ENDPOINT_TYPE_BULK)
{
pOutPipe=(*lpUsbFuncs->lpOpenPipe)(hDevice,&lpInterface->lpEndpoints[dwIndex].Descriptor);
ASSERT(pOutPipe);
}
}
}
if (pInPipe && pOutPipe)
{
// allocate the device context (including smartcard and reader extension)
pSCE = LocalAlloc(
LPTR,
sizeof( SMARTCARD_EXTENSION )
+ sizeof(READER_EXTENSION)
);
ASSERT(pSCE);
if( pSCE)
{
PREADER_EXTENSION readerExtension;
//point to the reader extension
readerExtension = (PREADER_EXTENSION)(pSCE +1);
pSCE->ReaderExtension = readerExtension;
readerExtension->pPipeIn = pInPipe;
readerExtension->pPipeOut = pOutPipe;
readerExtension->pUsbFuncs = lpUsbFuncs;
readerExtension->hStreamDevice = ActivateDevice(szStreamDriverKey, (DWORD) pSCE);
readerExtension->hUsbDevice = hDevice;
if (readerExtension->hStreamDevice)
{
(*lpUsbFuncs->lpRegisterNotificationRoutine)(hDevice,DeviceNotify, pSCE);
*fAcceptControl = TRUE;
SmartcardDebug(DEBUG_TRACE,(TEXT("STCUSB!Device Attach Success")));
return TRUE;
}
}
}
if (pInPipe)
lpUsbFuncs->lpClosePipe(pInPipe);
if (pOutPipe)
lpUsbFuncs->lpClosePipe(pOutPipe);
if (pSCE)
LocalFree(pSCE);
return FALSE;
}
USBInstallDriver(
LPCWSTR szDriverLibFile) // @parm [IN] - Contains client driver DLL name
{
BOOL fRet = FALSE;
SmartcardDebug(DEBUG_TRACE,(TEXT("Start USBInstallDriver %s "),szDriverLibFile));
fRet=RegisterClientDriverID(szDriverName);
ASSERT(fRet);
if(fRet)
{
fRet = RegisterClientSettings(szDriverLibFile,
szDriverName, NULL, &g_DriverSettings);
ASSERT(fRet);
}
if (fRet)
{
HKEY hKey = NULL;
DWORD dwDisp;
DWORD dwStatus;
// Create registry key for Stream Driver. This will be used by ActivateDevice
// to instantiate a stream device. The key can be located anywhere as long as it has
// the dll name and prefix fields.
dwStatus = RegCreateKeyEx(
HKEY_LOCAL_MACHINE,
szStreamDriverKey,
0,
NULL,
0,
0,
NULL,
&hKey,
&dwDisp);
if (dwStatus == ERROR_SUCCESS && dwDisp == REG_CREATED_NEW_KEY) {
dwStatus = RegSetValueEx(
hKey,
DEVLOAD_DLLNAME_VALNAME,
0,
DEVLOAD_DLLNAME_VALTYPE,
(const BYTE *)szDriverLibFile,
(lstrlen(szDriverLibFile)+1)*sizeof(WCHAR)
);
if (dwStatus == ERROR_SUCCESS) {
dwStatus = RegSetValueEx(
hKey,
DEVLOAD_PREFIX_VALNAME,
0,
DEVLOAD_PREFIX_VALTYPE,
(const BYTE *) szDriverPrefix,
sizeof(szDriverPrefix)
);
}
#if (CE_MAJOR_VER < 0x0003)
// For WinCE 2.x and earlier, the driver name and clientinfo fields are not available until
// after SCR_Init, so we need an initialization ioctl.
if (dwStatus == ERROR_SUCCESS) {
dwStatus = IOCTL_INIT_COMPLETE;
dwStatus = RegSetValueEx(
hKey,
DEVLOAD_INITCODE_VALNAME,
0,
DEVLOAD_PREFIX_VALTYPE,
&dwStatus,
sizeof(DWORD)
);
}
#endif
}
if (hKey)
RegCloseKey(hKey);
if (dwStatus != ERROR_SUCCESS)
{
if (hKey)
RegDeleteKey(HKEY_LOCAL_MACHINE,szStreamDriverKey);
fRet = FALSE;
}
}
SmartcardDebug(DEBUG_TRACE,(TEXT("End USBInstallDriver return value = %d "),fRet));
ASSERT(fRet);
return fRet;
}
/*
* @func BOOL | USBUnInstallDriver | Uninstall USB client driver.
* @rdesc Return TRUE if install succeeds, or FALSE if there is some error.
* @comm This function can be called by a client driver to deregister itself
* with USBD.
* @xref <f USBInstallDriver>
*/
USBUnInstallDriver()
{
BOOL fRet = FALSE;
fRet = UnRegisterClientSettings(szDriverName, NULL, &g_DriverSettings);
fRet = fRet && UnRegisterClientDriverID(szDriverName);
RegDeleteKey(HKEY_LOCAL_MACHINE, szStreamDriverKey);
return fRet;
}
BOOL AddDevice(PSMARTCARD_EXTENSION pDevice)
{
int i;
EnterCriticalSection(&g_DriverCritSect);
for (i=0; i< STCUSB_MAX_DEVICE; i++)
{
if (DeviceSlot[i] == NULL)
{
DeviceSlot[i] = pDevice;
break;
}
}
LeaveCriticalSection(&g_DriverCritSect);
return (i < STCUSB_MAX_DEVICE);
}
BOOL RemoveDevice(PSMARTCARD_EXTENSION pDevice)
{
int i;
EnterCriticalSection(&g_DriverCritSect);
for (i=0; i< STCUSB_MAX_DEVICE; i++)
{
if (DeviceSlot[i] == pDevice)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -