📄 driver.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.
//
/*++
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
Module Name:
usbhid.cpp
Abstract:
USB Client Driver for Human Interface Device (HID) Class.
Functions:
Notes:
--*/
#include "usbhid.h"
extern CRITICAL_SECTION g_csHidLock;
/*
* DLL entry point
*/
extern "C"
BOOL
DllEntry(
HANDLE hDllHandle,
DWORD dwReason,
LPVOID lpReserved
)
{
UNREFERENCED_PARAMETER(lpReserved);
switch (dwReason) {
case DLL_PROCESS_ATTACH:
DEBUGREGISTER((HINSTANCE)hDllHandle);
DEBUGMSG(ZONE_INIT, (_T("Hid DllEntry Attach\r\n")));
NKDbgPrintfW(_T("Driver Hid DllEntry Attach\r\n"));
DisableThreadLibraryCalls((HMODULE) hDllHandle);
InitializeCriticalSection(&g_csHidLock);
break;
case DLL_PROCESS_DETACH:
DeleteCriticalSection(&g_csHidLock);
DEBUGMSG(ZONE_INIT, (_T("Hid DllEntry Detach\r\n")));
NKDbgPrintfW(_T("Driver Hid DllEntry Detach\r\n"));
break;
default:
break;
}
return TRUE ;
}
/*
* USBInstallDriver
*
* USB driver install routine - set up client registry settings so we will be loaded
* by USBD for HID devices. This function should not be called for systems in which the
* OEM ships the HID driver.
*
* Return value:
* Return TRUE if install succeeds, or FALSE if there is some error.
*/
extern "C"
BOOL
USBInstallDriver(
LPCWSTR szDriverLibFile // Contains client driver DLL name
)
{
SETFNAME(_T("USBInstallDriver"));
BOOL fRet = FALSE;
const WCHAR szUsbDeviceID[] = CLASS_NAME_SZ;
WCHAR szSubClassRegKey[sizeof(CLIENT_REGKEY_SZ)+16] = CLIENT_REGKEY_SZ;
USB_DRIVER_SETTINGS usbDriverSettings = { DRIVER_SETTINGS };
REG_VALUE_DESCR usbPrnKeyValues[] = {
(_T("Dll")), REG_SZ, 0, (PBYTE)(DRIVER_NAME),
(_T("Prefix")), REG_SZ, 0, (PBYTE)(DEVICE_PREFIX),
NULL, 0, 0, NULL
};
DEBUGCHK(szDriverLibFile != NULL);
DEBUGMSG(ZONE_INIT, (_T("%s: Install function called (driver: %s)\r\n"), pszFname, szDriverLibFile));
// register with USBD
fRet = RegisterClientDriverID(szUsbDeviceID);
if (fRet == FALSE) {
DEBUGMSG(ZONE_ERROR, (_T("%s: RegisterClientDriverID error:%d\r\n"), pszFname, GetLastError()));
// NKDbgPrintfW((_T("Driver USBInstallDriver RegisterClientDriverID error:%d\r\n"), GetLastError()));
goto EXIT;
}
fRet = RegisterClientSettings( szDriverLibFile,
szUsbDeviceID,
NULL,
&usbDriverSettings );
if (fRet == FALSE) {
UnRegisterClientDriverID(szUsbDeviceID);
DEBUGMSG(ZONE_ERROR, (_T("%s: RegisterClientSettings error:%d\r\n"), pszFname, GetLastError()));
// NKDbgPrintfW((_T("Driver USBInstallDriver RegisterClientSetting error:%d\r\n"), GetLastError()));
goto EXIT;
}
// Add our default values to the reg
if ( !GetSetKeyValues( szSubClassRegKey,
&usbPrnKeyValues[0],
SET,
TRUE ) ) {
DEBUGMSG(ZONE_ERROR, (_T("%s: GetSetKeyValues failed!\r\n"), pszFname));
// NKDbgPrintfW((_T("Driver USBInstallDriver GetSetKeyValues error:%d\r\n"), GetLastError()));
goto EXIT;
}
fRet = TRUE;
EXIT:
return fRet;
}
/*
* USBUnInstallDriver
*
* This function can be called by a client driver to deregister itself
* with USBD.
*/
extern "C"
BOOL
USBUnInstallDriver(
VOID
)
{
SETFNAME(_T("USBUnInstallDriver"));
BOOL fRet = FALSE;
const WCHAR szUsbDeviceID[] = CLASS_NAME_SZ;
USB_DRIVER_SETTINGS usbDriverSettings = { DRIVER_SETTINGS };
DEBUGMSG(ZONE_INIT, (_T("%s: Uninstall function called\r\n"), pszFname));
fRet = UnRegisterClientSettings( szUsbDeviceID,
NULL,
&usbDriverSettings );
if (fRet == TRUE) {
fRet = UnRegisterClientDriverID(szUsbDeviceID);
NKDbgPrintfW(_T("Driver Uninstall function called\r\n"));
}
return fRet;
}
/*
* USBDeviceNotifications
*
* Process notifications from USBD. Currently, the only notification
* supported is a device removal. Unused parameters are reserved for
* future use.
*/
extern "C"
BOOL
USBDeviceNotifications(LPVOID lpvNotifyParameter, DWORD dwCode,
LPDWORD * dwInfo1, LPDWORD * dwInfo2, LPDWORD * dwInfo3,
LPDWORD * dwInfo4)
{
SETFNAME(_T("USBDeviceNotifications"));
BOOL fRet = FALSE;
PUSBHID_CONTEXT pUsbHid = (PUSBHID_CONTEXT) lpvNotifyParameter;
UNREFERENCED_PARAMETER(dwInfo1);
UNREFERENCED_PARAMETER(dwInfo2);
UNREFERENCED_PARAMETER(dwInfo3);
UNREFERENCED_PARAMETER(dwInfo4);
DEBUGCHK(pUsbHid != NULL);
ValidateHidContext(pUsbHid);
ValidateHidGlobals();
switch(dwCode)
{
case USB_CLOSE_DEVICE:
DEBUGMSG(ZONE_INIT, (TEXT("%s: USB_CLOSE_DEVICE\r\n"), pszFname));
// NKDbgPrintfW(TEXT("%s:Driver USB_CLOSE_DEVICE\r\n"), pszFname);
LockHidData();
// Remove and free the device context;
RemoveDeviceContext(pUsbHid);
// Free the global HID data if this was the last HID device.
FreeGlobalHidData();
ReleaseHidData();
fRet = TRUE;
break;
default:
DEBUGMSG(ZONE_ERROR, (TEXT("%s: Unhandled code:%d\n"), pszFname, dwCode));
// NKDbgPrintfW(TEXT("%s:Driver Unhandled code:%d\r\n"), pszFname,dwCode);
break;
}
return fRet;
}
/*
* USBDeviceAttach
*
* USB device attach routine. This function is called by USBD when a device is attached
* to the USB, and a matching registry key is found off the LoadClients registry key.
* We must determine whether the device may be controlled by this driver, and load
* drivers for any uncontrolled interfaces.
*
* Return Value:
* Return TRUE upon success, or FALSE if an error occurs.
*/
extern "C"
BOOL
USBDeviceAttach(
USB_HANDLE hDevice, // USB device handle
LPCUSB_FUNCS UsbFuncs, // Pointer to USBDI function table.
LPCUSB_INTERFACE UsbInterface,// 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.
LPCWSTR UniqueDriverId, // Contains client driver id string.
LPBOOL AcceptControl, // 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 UsbDriverSettings,// Contains pointer to USB_DRIVER_SETTINGS struct that indicates how we were loaded.
DWORD Unused // Reserved for use with future versions of USBD
)
{
SETFNAME(_T("USBDeviceAttach"));
BOOL fRet = FALSE;
LPCUSB_INTERFACE pUsbInterface = NULL;
PUSBHID_CONTEXT pUsbHid = NULL;
DEBUGMSG(ZONE_INIT, (_T("+%s: 0x%x, %s\r\n"), pszFname, hDevice, UniqueDriverId));
UNREFERENCED_PARAMETER(UniqueDriverId);
UNREFERENCED_PARAMETER(UsbDriverSettings);
UNREFERENCED_PARAMETER(Unused);
DEBUGCHK(UsbFuncs != NULL);
DEBUGCHK(UniqueDriverId != NULL);
DEBUGCHK(AcceptControl != NULL);
DEBUGCHK(UsbDriverSettings != NULL);
// Determine if we control this USB peripheral...
*AcceptControl = FALSE;
pUsbInterface = ParseUsbDescriptors(hDevice, UsbFuncs, UsbInterface);
LockHidData();
if (pUsbInterface == NULL) {
goto EXIT;
}
// Tell USBD to stop looking for drivers
*AcceptControl = TRUE;
// Initialize the global data (the function verifies that it only
// occurs once).
if (InitializeGlobalHidData() == FALSE) {
goto EXIT;
}
// We found a device and interface we control, so create our device context.
pUsbHid = CreateHidDevice(hDevice, UsbFuncs, pUsbInterface);
if (pUsbHid == NULL) {
goto EXIT;
}
// Add this device to our global device list.
if (AddDeviceToList(pUsbHid) == FALSE) {
goto EXIT;
}
// Register to receive notifications regarding this device from USB.
(*UsbFuncs->lpRegisterNotificationRoutine)(hDevice, USBDeviceNotifications, pUsbHid);
fRet = TRUE;
EXIT:
if (fRet == FALSE)
{
// Clean up. We must free all global data if this was the only
// HID device.
FreeGlobalHidData(); // Only frees if no more devices in list.
if (pUsbHid != NULL) {
RemoveDeviceContext(pUsbHid);
}
}
ReleaseHidData();
NKDbgPrintfW(TEXT("Driver USBDeveiceAttach Fail:%d\r\n"),GetLastError());
DEBUGMSG(ZONE_INIT, (_T("-%s\r\n"), pszFname));
return fRet;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -