📄 usbddrv.cpp
字号:
DEBUGMSG(ZONE_LOADER,(TEXT("USBD:HcdDeviceDetached: Closing all pipes\r\n")));
for (UINT iPipe = gcEndpointZero; iPipe < gcMaxPipes ; ++iPipe)
{
if (pDev->apPipes[iPipe])
ClosePipe(pDev->apPipes[iPipe]);
}
// Notify client drivers of device removal
fRet = CloseUSBDevice(pDev);
// Free client Dlls
while(pDev->pDriverLibs)
{
SDriverLibs * pNext;
if(pDev->pDriverLibs->hDriver) {
DEBUGMSG(ZONE_LOADER,(TEXT("USBD: Unloading client DLL...\r\n")));
FreeLibrary(pDev->pDriverLibs->hDriver);
DEBUGMSG(ZONE_LOADER,(TEXT("USBD: Client DLL unloaded\r\n")));
}
pNext = pDev->pDriverLibs->pNext;
delete pDev->pDriverLibs;
pDev->pDriverLibs = pNext;
}
// all client drivers are removed so we don't need to lock the notfication
while (pDev->pNotifyList)
{
SNotifyList * pNext;
pNext = pDev->pNotifyList->pNext;
delete pDev->pNotifyList;
pDev->pNotifyList = pNext;
}
// Free all pipe structures in our free list
while (pDev->pFreePipeList) {
pPipe = pDev->pFreePipeList;
pDev->pFreePipeList = pPipe->pNext;
FreePipeObjectMem(pPipe);
}
// if we hold frame length control then we release it here!
ReleaseFrameLengthControl(pDev);
DeleteCriticalSection(&pDev->csPipeLock);
DeleteCriticalSection(&pDev->csSerializeNotifyRoutine);
DeleteCriticalSection(&pDev->csLibList);
pDev->dwSig = 0;
delete pDev;
DEBUGMSG(ZONE_LOADER,(TEXT("-USBD:HcdDeviceDetached, pDev:0x%X\r\n"),pDev));
return fRet;
}
/*
* @func BOOL | TranslateStringDescr | Translate a USB string descriptor into a NULL terminated string.
* @rdesc Returns TRUE if lpStringDescr points to a valid USB string descriptor.
* @comm If the string to be translated would overflow the supplied buffer,
* it is truncated to fit.
*/
extern "C" BOOL
TranslateStringDescr(
LPCUSB_STRING_DESCRIPTOR lpStringDescr, // @parm [IN] - Pointer to USB string descriptor
LPWSTR szString, // @parm [OUT]- Buffer to receive string data
DWORD cchStringLength) // @parm [IN] - Size of string buffer (characters)
{
ASSERT(sizeof(szString[0]) == sizeof(lpStringDescr->bString[0]));
szString[0] = 0;
if(lpStringDescr->bDescriptorType != USB_STRING_DESCRIPTOR_TYPE)
return FALSE;
UINT cchString = (lpStringDescr->bLength - sizeof(USB_COMMON_DESCRIPTOR)) /
sizeof(lpStringDescr->bString[0]);
--cchStringLength; // normalize to actual # of characters we can copy
if(cchString > cchStringLength)
cchString = cchStringLength;
memcpy(szString, &lpStringDescr->bString, cchString * sizeof(szString[0]));
szString[cchString] = 0;
return TRUE;
}
/*
* @func BOOL | LoadGenericInterfaceDriver | Called by clients to load driver for an interface
* @rdesc Returns TRUE if a driver was successfully loaded for interface, otherwise FALSE.
* @comm If a client accepts control of a device, this function must be called
* to load a driver for any uncontrolled interfaces.
*/
extern "C" BOOL
LoadGenericInterfaceDriver(
USB_HANDLE hDevice, // @parm [IN] - Handle to USB device (passed in <f USBDeviceAttach>)
LPCUSB_INTERFACE lpInterface) // @parm [IN] - Pointer to USB_INTERFACE struct for interface
{
SDevice * pDev = (SDevice *)hDevice;
BOOL fRet = TRUE;
BOOL fLoaded = FALSE;
DEBUGMSG(ZONE_LOADER,(TEXT("+USBD:LoadGenericInterfaceDriver\r\n")));
fRet = LoadUSBClient(pDev, &fLoaded, lpInterface);
if(fLoaded == FALSE)
fRet = FALSE;
DEBUGMSG(ZONE_LOADER,(TEXT("-USBD:LoadGenericInterfaceDriver\r\n")));
return fRet;
}
/*
* @func BOOL | RegisterClientDriverID | Called to register a unique string to identify a client driver.
* @rdesc Return TRUE if client id was successfully registered, FALSE if an error occurred.
*/
extern "C" BOOL
RegisterClientDriverID(
LPCWSTR szUniqueDriverId)
{
BOOL fRet = FALSE;
DWORD dwGarbage;
HKEY hkUsb;
if(RegCreateKeyEx(HKEY_LOCAL_MACHINE, gcszUsbRegKey, 0, NULL, 0, 0, NULL,
&hkUsb, &dwGarbage) == ERROR_SUCCESS)
{
HKEY hkDrivers;
if(RegCreateKeyEx(hkUsb, gcszDriverIDs, 0, NULL, 0, 0, NULL,
&hkDrivers, &dwGarbage) == ERROR_SUCCESS)
{
HKEY hkThisDriver;
if(RegCreateKeyEx(hkDrivers, szUniqueDriverId, 0, NULL, 0, 0,
NULL, &hkThisDriver, &dwGarbage) == ERROR_SUCCESS)
{
fRet = TRUE;
RegCloseKey(hkThisDriver);
}
RegCloseKey(hkDrivers);
}
RegCloseKey(hkUsb);
}
return fRet;
}
extern "C" BOOL UnRegisterClientDriverID(LPCWSTR szUniqueDriverId)
{
BOOL fRet = FALSE;
HKEY hkUsb;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, gcszUsbRegKey, 0, 0, &hkUsb)
== ERROR_SUCCESS)
{
HKEY hkDrivers;
if(RegOpenKeyEx(hkUsb, gcszDriverIDs, 0, 0, &hkDrivers)
== ERROR_SUCCESS)
{
if(RegDeleteKey(hkDrivers, szUniqueDriverId) == ERROR_SUCCESS)
{
fRet = TRUE;
}
RegCloseKey(hkDrivers);
}
RegCloseKey(hkUsb);
}
return fRet;
}
/*
* @func HKEY | OpenClientRegistryKey | Opens registry key associated with client driver.
* @rdesc Return handle to open key, or NULL if key doesn't exist, or other error occurs.
* @comm Client key is created in <f RegisterClientDriverId>.
* @xref <f RegisterClientDriverId>
*/
extern "C" HKEY
OpenClientRegistryKey(
LPCWSTR szUniqueDriverId) // @parm [IN] - Unique driver Id string
{
HKEY hkUsb;
HKEY hkDriverKey = NULL;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, gcszUsbRegKey, 0, 0, &hkUsb)
== ERROR_SUCCESS)
{
HKEY hkDrivers;
if(RegOpenKeyEx(hkUsb, gcszDriverIDs, 0, 0, &hkDrivers)
== ERROR_SUCCESS)
{
if(RegOpenKeyEx(hkDrivers, szUniqueDriverId, 0, 0, &hkDriverKey)
!= ERROR_SUCCESS)
{
hkDriverKey = NULL;
}
RegCloseKey(hkDrivers);
}
RegCloseKey(hkUsb);
}
return hkDriverKey;
}
/*
* @func BOOL | RegisterClientSettings | Called to set up client driver load settings.
* @rdesc Returns TRUE if settings were successfully configured, otherwise FALSE.
* @comm This function should be called by client drivers in their installation routine.
* It sets up appropriate registry keys based on lpDriverSettings so that the
* driver will be loaded whenever the device is attached.
* @xref <t USB_DRIVER_SETTINGS> <f UnRegisterClientSettings>
*/
extern "C" BOOL
RegisterClientSettings(
LPCWSTR szDriverLibFile, // @parm [IN] - Client driver DLL name
LPCWSTR szUniqueDriverId, // @parm [IN] - Unique client driver id string
LPCWSTR szReserved, // @parm [IN] - Should be set to 0 to ensure compatibility
// with future versions of Windows CE
LPCUSB_DRIVER_SETTINGS lpDriverSettings)// @parm [IN] - Specifies how driver is to be loaded
{
UnusedParameter(szReserved);
BOOL fRet = FALSE;
WCHAR szTemp[gcMaxDriverString];
DWORD dwGarbage;
HKEY hkUsb;
if(RegCreateKeyEx(HKEY_LOCAL_MACHINE, gcszUsbRegKey, 0, NULL, 0, 0, NULL,
&hkUsb, &dwGarbage) == ERROR_SUCCESS)
{
HKEY hkLoadOrder;
if(RegCreateKeyEx(hkUsb, gcszLoadClients, 0, NULL, 0, 0, NULL,
&hkLoadOrder, &dwGarbage) == ERROR_SUCCESS)
{
HKEY hkGroup1;
GetSettingString(szTemp, lpDriverSettings->dwVendorId,
lpDriverSettings->dwProductId,
lpDriverSettings->dwReleaseNumber);
if(RegCreateKeyEx(hkLoadOrder, szTemp, 0, NULL, 0, 0, NULL,
&hkGroup1, &dwGarbage) == ERROR_SUCCESS)
{
HKEY hkGroup2;
GetSettingString(szTemp, lpDriverSettings->dwDeviceClass,
lpDriverSettings->dwDeviceSubClass,
lpDriverSettings->dwDeviceProtocol);
if(RegCreateKeyEx(hkGroup1, szTemp, 0, NULL, 0, 0, NULL,
&hkGroup2, &dwGarbage) == ERROR_SUCCESS)
{
HKEY hkGroup3;
GetSettingString(szTemp, lpDriverSettings->dwInterfaceClass,
lpDriverSettings->dwInterfaceSubClass,
lpDriverSettings->dwInterfaceProtocol);
if(RegCreateKeyEx(hkGroup2, szTemp, 0, NULL, 0, 0, NULL,
&hkGroup3, &dwGarbage) == ERROR_SUCCESS)
{
HKEY hkSettings;
if(RegCreateKeyEx(hkGroup3, szUniqueDriverId, 0, NULL,
0, 0, NULL, &hkSettings, &dwGarbage)
== ERROR_SUCCESS)
{
if(RegSetValueEx(hkSettings, gcszDllName, 0,
REG_SZ, (LPBYTE)szDriverLibFile,
(lstrlen(szDriverLibFile) + 1) *
sizeof(szDriverLibFile[0]))
== ERROR_SUCCESS)
{
fRet = TRUE;
}
RegCloseKey(hkSettings);
}
RegCloseKey(hkGroup3);
}
RegCloseKey(hkGroup2);
}
RegCloseKey(hkGroup1);
}
RegCloseKey(hkLoadOrder);
}
RegCloseKey(hkUsb);
}
return fRet;
}
/*
* @func BOOL | UnRegisterClientSettings | Deregister client driver settings.
* @rdesc Returns TRUE if settings were successfully removed, otherwise FALSE.
* @comm This function should be called by client drivers in their installation routine.
* It sets up appropriate registry keys based on lpDriverSettings param so that the
* driver will be loaded whenever the device is attached.
* @xref <t USB_DRIVER_SETTINGS> <f RegisterClientSettings>
*/
extern "C" BOOL
UnRegisterClientSettings(
LPCWSTR szUniqueDriverId, // @parm [IN] - Client driver DLL name
LPCWSTR szReserved, // @parm [IN] - Should be set to 0 to ensure compatibility
// with future versions of Windows CE
LPCUSB_DRIVER_SETTINGS lpDriverSettings) // @parm [IN] - Must match the settings the client was registered with
{
UnusedParameter(szReserved);
// REVIEW this procedure has not been tested!
BOOL fRet = FALSE;
WCHAR szTemp[gcMaxDriverString];
LONG lEnum;
DWORD cchEnum;
HKEY hkUsb;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, gcszUsbRegKey, 0, 0, &hkUsb)
== ERROR_SUCCESS)
{
HKEY hkLoadOrder;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -