📄 xbcd_driver.c
字号:
{
KdPrint(("XBCDStartDevice - %d is the wrong number of endpoints", pid->bNumEndpoints));
return STATUS_DEVICE_CONFIGURATION_ERROR;
}*/
pii = interfaces[0].Interface;
if (pii->NumberOfPipes != pid->bNumEndpoints)
{
KdPrint(("XBCDStartDevice - NumberOfPipes %d does not match bNumEndpoints %d",pii->NumberOfPipes, pid->bNumEndpoints));
return STATUS_DEVICE_CONFIGURATION_ERROR;
}
KdPrint(("Pipe 0 : MaxTransfer %d, MaxPckSize %d, PipeType %d, Interval %d, Handle %d Address %d", pii->Pipes[0].MaximumTransferSize, pii->Pipes[0].MaximumPacketSize, pii->Pipes[0].PipeType, pii->Pipes[0].Interval, pii->Pipes[0].PipeHandle, pii->Pipes[0].EndpointAddress));
//pii->Pipes[0].MaximumTransferSize = 0x0020;
//pii->Pipes[0].MaximumPacketSize = 0x0020;
//pii->Pipes[0].PipeType = UsbdPipeTypeInterrupt;
//pii->Pipes[0].Interval = 0x04;
KdPrint(("Pipe 1 : MaxTransfer %d, MaxPckSize %d, PipeType %d, Interval %d, Handle %d Address %d", pii->Pipes[1].MaximumTransferSize, pii->Pipes[1].MaximumPacketSize, pii->Pipes[1].PipeType, pii->Pipes[1].Interval, pii->Pipes[1].PipeHandle, pii->Pipes[1].EndpointAddress));
//pii->Pipes[1].MaximumTransferSize = 0x0006;
//pii->Pipes[1].MaximumPacketSize = 0x0020;
//pii->Pipes[1].PipeType = UsbdPipeTypeInterrupt;
//pii->Pipes[1].Interval = 0x04;
// Submit the set-configuration request
status = SendAwaitUrb(pFdo, selurb);
if (!NT_SUCCESS(status))
{
KdPrint(("XBCDStartDevice - Error %X trying to select configuration", status));
return status;
}
// Save the configuration and pipe handles
pDevExt->hconfig = selurb->UrbSelectConfiguration.ConfigurationHandle;
pDevExt->hintpipe = pii->Pipes[0].PipeHandle;
pDevExt->hintoutpipe = pii->Pipes[1].PipeHandle;
//ResetPipe(pFdo,pii->Pipes[0].PipeHandle);
//ResetPipe(pFdo,pii->Pipes[1].PipeHandle);
// Transfer ownership of the configuration descriptor to the device extension
pDevExt->pcd = pcd;
pcd = NULL;
// Initialize the variable that will contain the data read from device
RtlZeroMemory(&pDevExt->intdata, sizeof(pDevExt->intdata));
RtlZeroMemory(&pDevExt->intoutdata, sizeof(pDevExt->intoutdata));
RtlZeroMemory(&pDevExt->buttons, sizeof(pDevExt->buttons));
ExFreePool(selurb);
if (pcd)
{
ExFreePool(pcd);
}
KeInitializeDpc(&pDevExt->timeDPC, timerDPCProc, pDevExt);
KeInitializeTimer(&pDevExt->timer);
XBCDReadButtonConfig(pFdo);
pDevExt->DeviceStarted = TRUE;
return STATUS_SUCCESS;
}
#pragma PAGEDCODE
VOID XBCDRemoveDevice(PDEVICE_OBJECT pFdo, PIRP pIrp)
{
PDEVICE_EXTENSION pDevExt = GET_MINIDRIVER_DEVICE_EXTENSION(pFdo);
NTSTATUS status;
pDevExt->PowerDown = TRUE;
if(pDevExt->pIrp)
{
KdPrint(("XBCDRemoveDevice - Trying to delete the interrupt urb"));
DeleteInterruptUrb(pFdo);
}
return;
}
VOID XBCDStopDevice(PDEVICE_OBJECT pFdo, PIRP pIrp)
{
NTSTATUS status;
PDEVICE_EXTENSION pDevExt = GET_MINIDRIVER_DEVICE_EXTENSION(pFdo);
URB urb;
// Cancel the URB in case it's currently active
KdPrint(("XBCDStopDevice - About to stop the interrupt urb"));
StopInterruptUrb(pDevExt);
KdPrint(("XBCDStopDevice - Stopped the interrupt urb"));
KeCancelTimer(&pDevExt->timer);
if(pDevExt->DeviceStarted)
{
pDevExt->DeviceStarted = FALSE;
KdPrint(("XBCDStopDevice - Starting to deconfigure device"));
UsbBuildSelectConfigurationRequest(&urb, sizeof(struct _URB_SELECT_CONFIGURATION), NULL);
status = SendAwaitUrb(pFdo, &urb);
KdPrint(("XBCDStopDevice - Deconfiguring device"));
if(!NT_SUCCESS(status))
{
KdPrint(("XBCDStopDevice - Error %X trying to deconfigure device", status));
}
}
KdPrint(("XBCDStopDevice - freeing pcd"));
if(pDevExt->pcd)
ExFreePool(pDevExt->pcd);
pDevExt->pcd = NULL;
KdPrint(("XBCDStopDevice - passing irp down"));
KdPrint(("XBCDStopDevice - returning"));
return;
}
#pragma PAGEDCODE
NTSTATUS XBCDDispatchPower(IN PDEVICE_OBJECT pFdo, IN PIRP pIrp)
{
PIO_STACK_LOCATION stack = IoGetCurrentIrpStackLocation(pIrp);
PDEVICE_EXTENSION pDevExt = GET_MINIDRIVER_DEVICE_EXTENSION(pFdo);
/*switch (stack->MinorFunction)
{
case IRP_MN_WAIT_WAKE:
KdPrint(("XBCDDispatchPower - IRP_MN_WAIT_WAKE"));
break;
case IRP_MN_POWER_SEQUENCE:
KdPrint(("XBCDDispatchPower - IRP_MN_POWER_SEQUENCE"));
break;
case IRP_MN_SET_POWER:
KdPrint(("XBCDDispatchPower - IRP_MN_SET_POWER"));
break;
case IRP_MN_QUERY_POWER:
KdPrint(("XBCDDispatchPower - IRP_MN_QUERY_POWER"));
break;
}*/
IoSkipCurrentIrpStackLocation(pIrp);
PoStartNextPowerIrp(pIrp);
KdPrint(("XBCDDispatchPower"));
return PoCallDriver(pDevExt->pLowerPdo, pIrp);
}
#pragma PAGEDCODE
int ReadRegistry(HANDLE hKey, PCWSTR entry, int defValue)
{
NTSTATUS ntstatus;
PKEY_VALUE_PARTIAL_INFORMATION vpi;
ULONG size = 0;
int iReceived = 0;
UNICODE_STRING valname;
RtlInitUnicodeString(&valname, entry);
size = sizeof(KEY_VALUE_PARTIAL_INFORMATION) + sizeof(valname) + sizeof(int);
vpi = (PKEY_VALUE_PARTIAL_INFORMATION) ExAllocatePool(PagedPool, size);
ntstatus = ZwQueryValueKey(hKey, &valname, KeyValuePartialInformation, vpi, size, &size);
if(NT_SUCCESS(ntstatus))
{
RtlCopyMemory(&iReceived, &vpi->Data, sizeof(iReceived));
}
else
{
iReceived = defValue;
}
ExFreePool(vpi);
return iReceived;
}
void XBCDReadButtonConfig(IN PDEVICE_OBJECT pFdo)
{
PDEVICE_EXTENSION pDevExt = GET_MINIDRIVER_DEVICE_EXTENSION(pFdo);
HANDLE hKey;
NTSTATUS ntstatus;
int iReceived;
pDevExt->LStickDZ = 0;
pDevExt->RStickDZ = 0;
ntstatus = IoOpenDeviceRegistryKey(GET_PHYSICAL_DEVICE_OBJECT(pFdo), PLUGPLAY_REGKEY_DEVICE, KEY_ALL_ACCESS, &hKey);
if (NT_SUCCESS(ntstatus))
{
int count;
KdPrint(("IoOpenDeviceRegistryKey - success"));
pDevExt->btnset = TRUE;
for(count=1; count<=12; count++)
{
if(count == 1)
iReceived = ReadRegistry(hKey, L"1", 0);
if(count == 2)
iReceived = ReadRegistry(hKey, L"2", 0);
if(count == 3)
iReceived = ReadRegistry(hKey, L"3", 0);
if(count == 4)
iReceived = ReadRegistry(hKey, L"4", 0);
if(count == 5)
iReceived = ReadRegistry(hKey, L"5", 0);
if(count == 6)
iReceived = ReadRegistry(hKey, L"6", 0);
if(count == 7)
iReceived = ReadRegistry(hKey, L"7", 0);
if(count == 8)
iReceived = ReadRegistry(hKey, L"8", 0);
if(count == 9)
iReceived = ReadRegistry(hKey, L"9", 0);
if(count == 10)
iReceived = ReadRegistry(hKey, L"10", 0);
if(count == 11)
iReceived = ReadRegistry(hKey, L"11", 0);
if(count == 12)
iReceived = ReadRegistry(hKey, L"12", 0);
KdPrint(("Button %d - %d",count, iReceived));
if((iReceived < 1) || (iReceived > 12))
{
pDevExt->btnset = FALSE;
}
else
{
pDevExt->buttons[count - 1] = power2(iReceived - 1);
}
}
iReceived = ReadRegistry(hKey, L"LStickDZ", 0);
if(iReceived < 0)
{
pDevExt->LStickDZ = 0;
}
else
{
if(iReceived >= 100)
{
pDevExt->LStickDZ = 32766;
}
else
{
pDevExt->LStickDZ = 32767 * iReceived/100;
}
}
KdPrint(("%d", iReceived));
iReceived = ReadRegistry(hKey, L"RStickDZ", 0);
if(iReceived < 0)
{
pDevExt->RStickDZ = 0;
}
else
{
if(iReceived >= 100)
{
pDevExt->RStickDZ = 32766;
}
else
{
pDevExt->RStickDZ = 32767 * iReceived/100;
}
}
KdPrint(("%d", iReceived));
iReceived = ReadRegistry(hKey, L"LAFactor", 100);
if((iReceived < 0) || (iReceived > 100))
{
pDevExt->LAFactor = 100;
}
else
{
pDevExt->LAFactor = iReceived;
}
KdPrint(("LAFactor = %d", pDevExt->LAFactor));
iReceived = ReadRegistry(hKey, L"RAFactor", 100);
if((iReceived < 0) || (iReceived > 100))
{
pDevExt->RAFactor = 100;
}
else
{
pDevExt->RAFactor = iReceived;
}
KdPrint(("RAFactor = %d", pDevExt->RAFactor));
pDevExt->iNumConf = 0;
pDevExt->iCurrentConf = 0;
for(count=1; count<=6; count++)
{
if(count == 1)
iReceived = ReadRegistry(hKey, L"Conf1", 0);
if(count == 2)
iReceived = ReadRegistry(hKey, L"Conf2", 0);
if(count == 3)
iReceived = ReadRegistry(hKey, L"Conf3", 0);
if(count == 4)
iReceived = ReadRegistry(hKey, L"Conf4", 0);
if(count == 5)
iReceived = ReadRegistry(hKey, L"Conf5", 0);
if(count == 6)
iReceived = ReadRegistry(hKey, L"Conf6", 0);
switch(iReceived)
{
case 1:
{
pDevExt->iConf[count - 1] = 1;
pDevExt->iNumConf += 1;
if(count == 1)
{
pDevExt->iXYMov = 12;
pDevExt->iPOV = 2;
pDevExt->iSlider = 16;
}
break;
}
case 2:
{
pDevExt->iConf[count - 1] = 2;
pDevExt->iNumConf += 1;
if(count == 1)
{
pDevExt->iXYMov = 2;
pDevExt->iPOV = 16;
pDevExt->iSlider = 12;
}
break;
}
case 3:
{
pDevExt->iConf[count - 1] = 3;
pDevExt->iNumConf += 1;
if(count == 1)
{
pDevExt->iXYMov = 16;
pDevExt->iPOV = 12;
pDevExt->iSlider = 2;
}
break;
}
case 4:
{
pDevExt->iConf[count - 1] = 4;
pDevExt->iNumConf += 1;
if(count == 1)
{
pDevExt->iXYMov = 12;
pDevExt->iPOV = 16;
pDevExt->iSlider = 2;
}
break;
}
case 5:
{
pDevExt->iConf[count - 1] = 5;
pDevExt->iNumConf += 1;
if(count == 1)
{
pDevExt->iXYMov = 16;
pDevExt->iPOV = 2;
pDevExt->iSlider = 12;
}
break;
}
case 6:
{
pDevExt->iConf[count - 1] = 6;
pDevExt->iNumConf += 1;
if(count == 1)
{
pDevExt->iXYMov = 2;
pDevExt->iPOV = 12;
pDevExt->iSlider = 16;
}
break;
}
default:
{
pDevExt->iConf[count - 1] = 0;
break;
}
}
}
if (pDevExt->iNumConf == 0)
{
pDevExt->iNumConf = 1;
pDevExt->iConf[0] = 1;
pDevExt->iXYMov = 12;
pDevExt->iPOV = 2;
pDevExt->iSlider = 16;
}
iReceived = ReadRegistry(hKey, L"BThreshold", 0);
if((iReceived < 1) || (iReceived > 255))
{
pDevExt->BThreshold = 10;
}
else
{
pDevExt->BThreshold = iReceived;
}
iReceived = ReadRegistry(hKey, L"TThreshold", 0);
if((iReceived < 1) || (iReceived > 255))
{
pDevExt->TThreshold = 10;
}
else
{
pDevExt->TThreshold = iReceived;
}
iReceived = ReadRegistry(hKey, L"TThrottle", 0);
if((iReceived < 0) || (iReceived > 1))
{
pDevExt->bTThrottle = 0;
}
else
{
pDevExt->bTThrottle = iReceived;
}
iReceived = ReadRegistry(hKey, L"FPCalc", 0);
if((iReceived < 0) || (iReceived > 1))
{
pDevExt->bFPCalc = 0;
}
else
{
pDevExt->bFPCalc = iReceived;
}
}
ZwClose(hKey);
}
int power2(int n)
{
int vTotal;
int Count;
for(Count=0; !(Count > n); Count++)
{
if(Count==0)
{
vTotal = 1;
}
else
{
vTotal= vTotal * 2;
}
}
return vTotal;
}
#pragma LOCKEDCODE
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -