📄 drvpnp.c
字号:
NULL,
SizeDescriptor,
NULL);
ntStatus = UsbCallUSBDI(fdo, urb);
if (NT_SUCCESS(ntStatus)) {
pdx->DeviceDescriptor = deviceDescriptor;
pdx->Stop = FALSE;
}
else {
ExFreePool(deviceDescriptor);
pdx->DeviceDescriptor = NULL;
}
ExFreePool(urb);
if (NT_SUCCESS(ntStatus)) {
ntStatus = UsbConfigureDevice(fdo);
}
return ntStatus;
}
NTSTATUS UsbCallUSBDI(IN PDEVICE_OBJECT fdo,IN PURB Urb)
{
NTSTATUS ntStatus, status = STATUS_SUCCESS;
PDEVICE_EXTENSION pdx;
PIRP irp;
KEVENT event;
IO_STATUS_BLOCK ioStatus;
PIO_STACK_LOCATION nextStack;
pdx = fdo->DeviceExtension;
KeInitializeEvent(&event, NotificationEvent, FALSE);
irp = IoBuildDeviceIoControlRequest(
IOCTL_INTERNAL_USB_SUBMIT_URB,
pdx->LowerDeviceObject,
NULL,
0,
NULL,
0,
TRUE,
&event,
&ioStatus);
nextStack = IoGetNextIrpStackLocation(irp);
nextStack->Parameters.Others.Argument1 = Urb;
ntStatus = IoCallDriver(pdx->LowerDeviceObject,irp);
if (ntStatus == STATUS_PENDING) {
KeWaitForSingleObject(&event,Suspended,KernelMode,FALSE,NULL);
ntStatus=ioStatus.Status;
}
if (NT_SUCCESS(ntStatus)) {
if (!(USBD_SUCCESS(Urb->UrbHeader.Status)))
ntStatus = STATUS_UNSUCCESSFUL;
}
return ntStatus;
}
//
NTSTATUS UsbConfigureDevice(IN PDEVICE_OBJECT fdo)
{
NTSTATUS ntStatus;
PDEVICE_EXTENSION pdx;
PURB urb = NULL;
USHORT SizeUrb;
ULONG SizeDescriptor;
PUSB_CONFIGURATION_DESCRIPTOR configurationDescriptor = NULL;
pdx = fdo->DeviceExtension;
SizeUrb = sizeof(struct _URB_CONTROL_DESCRIPTOR_REQUEST);
urb = ExAllocatePool(NonPagedPool,SizeUrb);
if (urb == NULL) return STATUS_NO_MEMORY;
SizeDescriptor=sizeof(USB_CONFIGURATION_DESCRIPTOR) + 16;
configurationDescriptor = ExAllocatePool(NonPagedPool, SizeDescriptor);
if (configurationDescriptor == NULL) {
ExFreePool(urb);
return STATUS_NO_MEMORY;
}
UsbBuildGetDescriptorRequest(urb,
SizeUrb,
USB_CONFIGURATION_DESCRIPTOR_TYPE,
0,
0,
configurationDescriptor,
NULL,
SizeDescriptor,
NULL);
ntStatus = UsbCallUSBDI(fdo, urb);
if (!NT_SUCCESS(ntStatus)) {
ExFreePool(urb);
ExFreePool(configurationDescriptor);
return ntStatus;
}
SizeDescriptor = configurationDescriptor->wTotalLength + 16;
ExFreePool(configurationDescriptor);
configurationDescriptor = NULL;
configurationDescriptor = ExAllocatePool(NonPagedPool, SizeDescriptor);
if (configurationDescriptor == NULL) {
ExFreePool(urb);
return STATUS_NO_MEMORY;
}
UsbBuildGetDescriptorRequest(urb,
SizeUrb,
USB_CONFIGURATION_DESCRIPTOR_TYPE,
0,
0,
configurationDescriptor,
NULL,
SizeDescriptor,
NULL);
ntStatus = UsbCallUSBDI(fdo, urb);
if (!NT_SUCCESS(ntStatus)) {
ExFreePool(urb);
ExFreePool(configurationDescriptor);
return ntStatus;
}
if (configurationDescriptor) {
ntStatus = UsbSelectInterfaces(fdo,
configurationDescriptor,
NULL
);
}
ExFreePool(urb);
ExFreePool(configurationDescriptor);
return ntStatus;
}
//
NTSTATUS UsbSelectInterfaces(IN PDEVICE_OBJECT fdo,
IN PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor,
IN PUSBD_INTERFACE_INFORMATION Interface)
{
NTSTATUS ntStatus;
PDEVICE_EXTENSION pdx;
PURB urb;
ULONG i;
UCHAR alternateSetting, MyInterfaceNumber;
PUSBD_INTERFACE_INFORMATION interfaceObject;
USBD_INTERFACE_LIST_ENTRY interfaceList[2];
pdx = fdo->DeviceExtension;
MyInterfaceNumber = 0;
alternateSetting = 0;
interfaceList[0].InterfaceDescriptor = USBD_ParseConfigurationDescriptor(ConfigurationDescriptor,
MyInterfaceNumber,
alternateSetting);
interfaceList[1].InterfaceDescriptor = NULL;
interfaceList[1].Interface = NULL;
urb = USBD_CreateConfigurationRequestEx(ConfigurationDescriptor,&interfaceList[0]);
interfaceObject = (PUSBD_INTERFACE_INFORMATION) (&(urb->UrbSelectConfiguration.Interface));
for (i=0; i<interfaceList[0].InterfaceDescriptor->bNumEndpoints; i++)
interfaceObject->Pipes[i].MaximumTransferSize = (64 * 1024) - 1;
ntStatus = UsbCallUSBDI(fdo, urb);
if (NT_SUCCESS(ntStatus) && USBD_SUCCESS(urb->UrbHeader.Status)) {
pdx->ConfigurationHandle =
urb->UrbSelectConfiguration.ConfigurationHandle;
pdx->Interface = ExAllocatePool(NonPagedPool,
interfaceObject->Length);
RtlCopyMemory(pdx->Interface, interfaceObject, interfaceObject->Length);
}
return ntStatus;
}
//
NTSTATUS PnpHandleDefault(IN PDEVICE_OBJECT fdo,IN PIRP Irp)
{
PDEVICE_EXTENSION pdx;
pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;
IoSkipCurrentIrpStackLocation(Irp);
return IoCallDriver(pdx->LowerDeviceObject, Irp);
}
//
NTSTATUS PnpHandleStopDevice(IN PDEVICE_OBJECT fdo)
{
NTSTATUS ntStatus;
PDEVICE_EXTENSION pdx;
PURB urb;
ULONG size;
pdx = fdo->DeviceExtension;
size = sizeof(struct _URB_SELECT_CONFIGURATION);
urb = ExAllocatePool(NonPagedPool,size);
if (urb==NULL) return STATUS_NO_MEMORY;
UsbBuildSelectConfigurationRequest(urb,
(USHORT)size,
NULL);
ntStatus = UsbCallUSBDI(fdo, urb);
ExFreePool(urb);
return ntStatus;
}
//
NTSTATUS PnpHandleRemoveDevice(IN PDEVICE_OBJECT fdo,IN PIRP Irp)
{
NTSTATUS ntStatus;
PDEVICE_EXTENSION pdx;
pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;
pdx->Removing = TRUE;
UnlockDevice(fdo);
UnlockDevice(fdo);
KeWaitForSingleObject(&pdx->evRemove, Executive, KernelMode, FALSE, NULL);
UsbRemoveDevice(fdo);
ntStatus = PnpHandleDefault(fdo, Irp);
return ntStatus;
}
//
NTSTATUS UsbRemoveDevice(IN PDEVICE_OBJECT fdo)
{
NTSTATUS ntStatus = STATUS_SUCCESS;
PDEVICE_EXTENSION pdx;
UNICODE_STRING deviceLinkUnicodeString;
pdx = fdo->DeviceExtension;
if (pdx->DeviceDescriptor)
ExFreePool(pdx->DeviceDescriptor);
if (pdx->Interface != NULL)
ExFreePool(pdx->Interface);
RtlInitUnicodeString (&deviceLinkUnicodeString,
pdx->DeviceLinkName);
IoDeleteSymbolicLink(&deviceLinkUnicodeString);
IoDetachDevice(pdx->LowerDeviceObject);
IoDeleteDevice (fdo);
return ntStatus;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -