📄 usbddrv.cpp
字号:
if(RegOpenKeyEx(hkUsb, gcszLoadClients, 0, 0, &hkLoadOrder)
== ERROR_SUCCESS)
{
HKEY hkGroup1;
GetSettingString(szTemp, lpDriverSettings->dwVendorId,
lpDriverSettings->dwProductId,
lpDriverSettings->dwReleaseNumber);
if(RegOpenKeyEx(hkLoadOrder, szTemp, 0, 0, &hkGroup1)
== ERROR_SUCCESS)
{
HKEY hkGroup2;
GetSettingString(szTemp, lpDriverSettings->dwDeviceClass,
lpDriverSettings->dwDeviceSubClass,
lpDriverSettings->dwDeviceProtocol);
if(RegOpenKeyEx(hkGroup1, szTemp, 0, 0, &hkGroup2)
== ERROR_SUCCESS)
{
HKEY hkGroup3;
GetSettingString(szTemp, lpDriverSettings->dwInterfaceClass,
lpDriverSettings->dwInterfaceSubClass,
lpDriverSettings->dwInterfaceProtocol);
if(RegOpenKeyEx(hkGroup2, szTemp, 0, 0, &hkGroup3)
== ERROR_SUCCESS)
{
if(RegDeleteKey(hkGroup3, szUniqueDriverId)
== ERROR_SUCCESS)
{
fRet = TRUE;
}
cchEnum = sizeof(szTemp) / sizeof(szTemp[0]);
lEnum = RegEnumKeyEx(hkGroup3, 0, szTemp,
&cchEnum, NULL, NULL, NULL, NULL);
RegCloseKey(hkGroup3);
if(lEnum == ERROR_NO_MORE_ITEMS)
{
GetSettingString(szTemp,
lpDriverSettings->dwInterfaceClass,
lpDriverSettings->dwInterfaceSubClass,
lpDriverSettings->dwInterfaceProtocol);
RegDeleteKey(hkGroup2, szTemp);
}
}
cchEnum = sizeof(szTemp) / sizeof(szTemp[0]);
lEnum = RegEnumKeyEx(hkGroup2, 0, szTemp,
&cchEnum, NULL, NULL, NULL, NULL);
RegCloseKey(hkGroup2);
if(lEnum == ERROR_NO_MORE_ITEMS)
{
GetSettingString(szTemp,
lpDriverSettings->dwDeviceClass,
lpDriverSettings->dwDeviceSubClass,
lpDriverSettings->dwDeviceProtocol);
RegDeleteKey(hkGroup1, szTemp);
}
}
cchEnum = sizeof(szTemp) / sizeof(szTemp[0]);
lEnum = RegEnumKeyEx(hkGroup1, 0, szTemp,
&cchEnum, NULL, NULL, NULL, NULL);
RegCloseKey(hkGroup1);
if(lEnum == ERROR_NO_MORE_ITEMS)
{
GetSettingString(szTemp, lpDriverSettings->dwVendorId,
lpDriverSettings->dwProductId,
lpDriverSettings->dwReleaseNumber);
RegDeleteKey(hkLoadOrder, szTemp);
}
}
RegCloseKey(hkLoadOrder);
}
RegCloseKey(hkUsb);
}
return fRet;
}
DWORD WINAPI SignalEventFunc(LPVOID lpvNotifyParameter)
{
HANDLE * pEvent = (HANDLE *)lpvNotifyParameter;
SetEvent(*pEvent);
return 0;
}
/*
* @func LPCUSB_INTERFACE | FindInterface | Search for a specific interface on a device.
* @rdesc Returns pointer to USB_INTERFACE structure if interface is found,
* otherwise NULL.
* @comm Alternate settings are used to supply alternate interfaces that
* may be selected by client software. Not all devices will have
* alternate settings, a value of 0 should be used in this case.
* @xref <f GetDeviceInfo>
*/
extern "C" LPCUSB_INTERFACE
FindInterface(
LPCUSB_DEVICE lpDeviceInfo, // @parm [IN] - Pointer to device info struct (returned
// from <f GetDeviceInfo>)
UCHAR bInterfaceNumber, // @parm [IN] - Interface number
UCHAR bAlternateSetting) // @parm [IN] - Alternate interface setting
{
UINT cInterfaces =
lpDeviceInfo->lpActiveConfig->dwNumInterfaces;
PCUSB_INTERFACE pInterface =
lpDeviceInfo->lpActiveConfig->lpInterfaces;
DEBUGMSG(ZONE_API,(TEXT("+USBD:FindInterface, if:%u, Alt:%u\r\n"),
bInterfaceNumber,bAlternateSetting));
for(UINT iInterface = 0 ; iInterface < cInterfaces ; ++iInterface)
{
if(pInterface->Descriptor.bInterfaceNumber == bInterfaceNumber &&
pInterface->Descriptor.bAlternateSetting == bAlternateSetting)
{
DEBUGMSG(ZONE_API,(TEXT("-USBD:FindInterface\r\n")));
return pInterface;
}
++pInterface;
}
DEBUGMSG(ZONE_API,(TEXT("-USBD:FindInterface, interface not found\r\n")));
return NULL;
}
/*
* @func BOOL | TakeFrameLengthControl | Register for exclusive access to control the USB frame length.
* @rdesc Return TRUE if frame length control is given, FALSE if another driver
* has already obtained control of USB frame length.
* @comm Use with care - changing the USB frame length may affect other devices on the bus.
* @xref <f SetFrameLength> <f ReleaseFrameLengthControl>
*/
extern "C" BOOL
TakeFrameLengthControl(
USB_HANDLE hDevice) // @parm [IN] - USB device handle
{
SDevice * pDev = (SDevice *)hDevice;
if (!ValidateDeviceHandle(pDev))
{
DEBUGMSG(ZONE_WARNING,(TEXT("!USBD:TakeFrameLengthControl - Invalid device handle\r\n")));
return FALSE;
}
SHcd * pHcd = pDev->pHcd;
BOOL fRet = FALSE;
DEBUGMSG(ZONE_API,(TEXT("+USBD:TakeFrameLengthControl\r\n")));
EnterCriticalSection(&pHcd->csFrameLengthControl);
if(!pHcd->pFrameControlOwner)
{
pHcd->pFrameControlOwner = pDev;
fRet = TRUE;
}
LeaveCriticalSection(&pHcd->csFrameLengthControl);
DEBUGMSG(ZONE_API,(TEXT("-USBD:TakeFrameLengthControl\r\n")));
return fRet;
}
/*
* @func BOOL | ReleaseFrameLengthControl | Release exclusive access to USB frame length control
* @rdesc Return TRUE if successful, FALSE if client has not previously obtained frame length control
* @xref <f SetFrameLength> <f TakeFrameLengthControl>
*/
extern "C" BOOL
ReleaseFrameLengthControl(
USB_HANDLE hDevice) // @parm [IN] - USB device handle
{
SDevice * pDev = (SDevice *)hDevice;
if (!ValidateDeviceHandle(pDev))
{
DEBUGMSG(ZONE_WARNING,(TEXT("!USBD:ReleaseFrameLengthControl - Invalid device handle\r\n")));
return FALSE;
}
SHcd * pHcd = pDev->pHcd;
BOOL fRet = FALSE;
DEBUGMSG(ZONE_API,(TEXT("+USBD:ReleaseFrameLengthControl\r\n")));
// once we have somebody own frame length control then only he
// can release it... and only once at that!
if(pHcd->pFrameControlOwner == pDev)
{
fRet=(*pHcd->pHcdFuncs->lpStopAdjustingFrame)(pHcd->pvHcd);
pHcd->pFrameControlOwner = NULL;
}
DEBUGMSG(ZONE_API,(TEXT("-USBD:ReleaseFrameLengthControl\r\n")));
return fRet;
}
/*
* @func BOOL | SetFrameLength | Change USB frame length
* @rdesc Return TRUE if successful, FALSE if error.
* @comm This function should be used infrequently by client drivers, as
* changing the USB frame length may affect other devices on the bus.
* Must obtain frame control access by calling <f TakeFrameLengthControl>
* before attempting to change the frame length. To enable devices to
* adjust to the new frame length, USB requires that frame length changes
* be done gradually, over a period of frames, instead of just jumping to
* the target frame length. The client driver passes in an event handle,
* which will be signalled once the frame length has reached the target.
* @xref <f TakeFrameLengthControl> <f ReleaseFrameLengthControl>
*/
extern "C" BOOL
SetFrameLength(
USB_HANDLE hDevice, // @parm [IN] - Handle to USB device
HANDLE hEvent, // @parm [IN] - Event to signal when frame length reaches target value
USHORT uFrameLength)// @parm [IN] - Desired frame length
{
SDevice * pDev = (SDevice *)hDevice;
if (!ValidateDeviceHandle(pDev))
{
DEBUGMSG(ZONE_WARNING,(TEXT("!USBD:SetFrameLength - Invalid device handle\r\n")));
return FALSE;
}
SHcd * pHcd = pDev->pHcd;
BOOL fRet = FALSE;
DEBUGMSG(ZONE_API,(TEXT("+USBD:SetFrameLength\r\n")));
// it is illegal to release and adjust at the same time, so this
// device will have frame length control now and until we leave this
// function
if(pHcd->pFrameControlOwner == pDev)
{
fRet = (*pHcd->pHcdFuncs->lpSetFrameLength)(pHcd->pvHcd,
hEvent, uFrameLength);
}
DEBUGMSG(ZONE_API,(TEXT("-USBD:SetFrameLength\r\n")));
return fRet;
}
/*
* @func BOOL | GetFrameNumber | Get current USB frame number
* @rdesc Return TRUE if successful, FALSE if error
*/
extern "C" BOOL
GetFrameNumber(
USB_HANDLE hDevice, // @parm [IN] - USB device handle
LPDWORD lpdwFrameNumber) // @parm [OUT]- Filled in with frame number
{
SDevice * pDev = (SDevice *)hDevice;
if (!ValidateDeviceHandle(pDev))
{
DEBUGMSG(ZONE_WARNING,(TEXT("!USBD:GetFrameNumber - Invalid device handle\r\n")));
return FALSE;
}
SHcd * pHcd = pDev->pHcd;
DEBUGMSG(ZONE_API,(TEXT("+USBD:GetFrameNumber\r\n")));
return (*pHcd->pHcdFuncs->lpGetFrameNumber)(pHcd->pvHcd, lpdwFrameNumber);
}
/*
* @func BOOL | GetFrameLength | Get current USB frame length
* @rdesc Return TRUE if successful, FALSE if error.
*/
extern "C" BOOL
GetFrameLength(
USB_HANDLE hDevice, // @parm [IN] - USB device handle
LPUSHORT lpuFrameLength) // @parm [OUT]- Filled in with current frame length
{
SDevice * pDev = (SDevice *)hDevice;
if (!ValidateDeviceHandle(pDev))
{
DEBUGMSG(ZONE_WARNING,(TEXT("!USBD:GetFrameLength - Invalid device handle\r\n")));
return FALSE;
}
SHcd * pHcd = pDev->pHcd;
DEBUGMSG(ZONE_API,(TEXT("+USBD:GetFrameLength\r\n")));
return (*pHcd->pHcdFuncs->lpGetFrameLength)(pHcd->pvHcd, lpuFrameLength);
}
/*
* @func USB_PIPE | OpenPipe | Open a pipe for communication with USB device.
* @rdesc Return USB pipe handle, or NULL if error. If an error occurs, call
* <f GetLastError> for extended error information. The following Win32
* error codes are applicable: <nl>
* ERROR_INVALID_HANDLE -- The device handle is invalid <nl>
* ERROR_OUTOFMEMORY -- Could not obtain required memory for operation <nl>
* ERROR_BUSY -- Could not allocate sufficient bandwidth for pipe <nl>
*
* @comm Only 16 pipes may be open simultaneously on a device (including
* default endpoint 0 pipe).
* @xref <f ClosePipe>
*/
extern "C" USB_PIPE
OpenPipe(
USB_HANDLE hDevice, // @parm [IN] - USB device handle
LPCUSB_ENDPOINT_DESCRIPTOR lpEndpointDescriptor) // @parm [IN] - Pointer to endpoint descriptor
{
BOOL fRet;
SDevice * pDev = (SDevice *)hDevice;
if (!ValidateDeviceHandle(pDev))
{
DEBUGMSG(ZONE_WARNING,(TEXT("!USBD:OpenPipe - Invalid device handle\r\n")));
SetLastError(ERROR_INVALID_HANDLE);
return NULL;
}
SHcd * pHcd = pDev->pHcd;
LPHCD_OPEN_PIPE pFunc = pHcd->pHcdFuncs->lpOpenPipe;
SPipe * pPipe = GetPipeObject(pDev);
if (pPipe == NULL)
{
SetLastError(ERROR_OUTOFMEMORY);
return NULL;
}
pPipe->wMaxPktSize = lpEndpointDescriptor->wMaxPacketSize;
DEBUGMSG(ZONE_API|ZONE_PIPE,
(TEXT("+USBD:OpenPipe, EP: %u, MaxPkt: %u, Type: %s\r\n"),
lpEndpointDescriptor->bEndpointAddress,lpEndpointDescriptor->wMaxPacketSize,
aszTypeStrings[lpEndpointDescriptor->bmAttributes&3]));
fRet = (*pFunc)(pHcd->pvHcd, pDev->iDevice, lpEndpointDescriptor,
&pPipe->iEndpointIndex);
if (fRet) {
UINT iFreePipe = gcMaxPipes; // this value is deliberately out-of-bounds
EnterCriticalSection(&pDev->csPipeLock);
for (UINT iPipe = 0 ; iPipe < gcMaxPipes ; ++iPipe) {
SPipe *pTmp = pDev->apPipes[iPipe];
if (pTmp) {
if (pTmp->iEndpointIndex == pPipe->iEndpointIndex) {
DEBUGMSG(ZONE_WARNING|ZONE_PIPE, (TEXT("-USBD:OpenPipe - pipe is already open\n")));
SetLastError(ERROR_BUSY);
iFreePipe = gcMaxPipes;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -