#include "usbdriver.h"
NTSTATUS PnpHandleStartDevice(IN PDEVICE_OBJECT fdo,IN PIRP Irp);
NTSTATUS ForwardAndWait(IN PDEVICE_OBJECT fdo,IN PIRP Irp);
NTSTATUS ForwardIrpComplete(IN PDEVICE_OBJECT fdo,
IN PIRP Irp,
IN PKEVENT pev);
NTSTATUS UsbStartDevice(IN PDEVICE_OBJECT fdo);
NTSTATUS UsbConfigureDevice(IN PDEVICE_OBJECT fdo);
NTSTATUS UsbSelectInterfaces(IN PDEVICE_OBJECT fdo,
IN PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor,
IN PUSBD_INTERFACE_INFORMATION Interface);
NTSTATUS PnpHandleDefault(IN PDEVICE_OBJECT fdo,IN PIRP Irp);
NTSTATUS PnpHandleStopDevice(IN PDEVICE_OBJECT fdo);
NTSTATUS PnpHandleRemoveDevice(IN PDEVICE_OBJECT fdo,IN PIRP Irp);
NTSTATUS UsbRemoveDevice(IN PDEVICE_OBJECT fdo);
//
NTSTATUS TestAddDevice(IN PDRIVER_OBJECT DriverObject,
IN PDEVICE_OBJECT PhysicalDeviceObject)
{
NTSTATUS ntStatus = STATUS_SUCCESS;
WCHAR KernelDeviceNameBuffer[] = L"\\Device\\Cyusb-0";
UNICODE_STRING KernelDeviceNameUnicode;
WCHAR UserDeviceLinkBuffer[] = L"\\DosDevices\\Cyusb-0";
UNICODE_STRING UserDeviceLinkUnicode;
PDEVICE_OBJECT fdo = NULL;
PDEVICE_EXTENSION pdx;
RtlInitUnicodeString (&KernelDeviceNameUnicode,
KernelDeviceNameBuffer);
ntStatus = IoCreateDevice (DriverObject,
sizeof (DEVICE_EXTENSION),
&KernelDeviceNameUnicode,
FILE_DEVICE_UNKNOWN,
0,
FALSE,
&fdo);
if( !NT_SUCCESS(ntStatus))
return ntStatus;
RtlInitUnicodeString (&UserDeviceLinkUnicode,
UserDeviceLinkBuffer);
ntStatus = IoCreateSymbolicLink (&UserDeviceLinkUnicode,
&KernelDeviceNameUnicode);
pdx = (PDEVICE_EXTENSION) (fdo->DeviceExtension);
RtlCopyMemory(pdx->DeviceLinkName,
UserDeviceLinkBuffer,
sizeof(UserDeviceLinkBuffer));
pdx->OpenHandles = 0;
pdx->ConfigurationHandle = NULL;
pdx->DeviceDescriptor = NULL;
pdx->Interface = NULL;
fdo->Flags &= ~DO_DEVICE_INITIALIZING;
fdo->Flags |= DO_DIRECT_IO;
pdx->PhysicalDeviceObject=PhysicalDeviceObject;
pdx->LowerDeviceObject =
IoAttachDeviceToDeviceStack(fdo, PhysicalDeviceObject);
pdx->Usages = 1;
KeInitializeEvent(&pdx->evRemove,
NotificationEvent,
FALSE);
return ntStatus;
}
//
NTSTATUS TestPnpIrp(IN PDEVICE_OBJECT fdo, IN PIRP Irp)
{
NTSTATUS ntStatus=STATUS_SUCCESS;
PIO_STACK_LOCATION IrpStack;
PDEVICE_EXTENSION pdx = fdo->DeviceExtension;
ULONG MinorFunction;
if (!LockDevice(fdo))
return CompleteRequest(Irp, STATUS_DELETE_PENDING, 0);
IrpStack = IoGetCurrentIrpStackLocation (Irp);
MinorFunction = IrpStack->MinorFunction;
switch (MinorFunction)
{
case IRP_MN_START_DEVICE:
ntStatus = PnpHandleStartDevice(fdo,Irp);
break;
case IRP_MN_STOP_DEVICE:
PnpHandleDefault(fdo,Irp);
ntStatus = PnpHandleStopDevice(fdo);
break;
case IRP_MN_REMOVE_DEVICE:
ntStatus = PnpHandleRemoveDevice(fdo,Irp);
break;
case IRP_MN_QUERY_CAPABILITIES:
{
PDEVICE_CAPABILITIES pdc = IrpStack->Parameters.DeviceCapabilities.Capabilities;
if (pdc->Version < 1) {
ntStatus = PnpHandleDefault(fdo, Irp);
break;
}
ntStatus = ForwardAndWait(fdo, Irp);
if (NT_SUCCESS(ntStatus)) {
pdc = IrpStack->Parameters.DeviceCapabilities.Capabilities;
pdc->SurpriseRemovalOK = TRUE;
}
ntStatus = CompleteRequest(Irp, ntStatus, Irp->IoStatus.Information);
}
break;
default:
ntStatus = PnpHandleDefault(fdo, Irp);
}
if (MinorFunction != IRP_MN_REMOVE_DEVICE)
UnlockDevice(fdo);
return ntStatus;
}
BOOLEAN LockDevice(IN PDEVICE_OBJECT fdo)
{
PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;
LONG usage = InterlockedIncrement(&pdx->Usages);
if (pdx->Removing) {
if (InterlockedDecrement(&pdx->Usages) == 0)
KeSetEvent(&pdx->evRemove, 0, FALSE);
return FALSE;
}
return TRUE;
}
//
void UnlockDevice(PDEVICE_OBJECT fdo)
{
PDEVICE_EXTENSION pdx;
LONG usage;
pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;
usage = InterlockedDecrement(&pdx->Usages);
if (usage == 0) {
KeSetEvent(&pdx->evRemove, 0, FALSE);
}
}
//
NTSTATUS CompleteRequest(IN PIRP Irp,
IN NTSTATUS status,
IN ULONG info)
{
Irp->IoStatus.Status = status;
Irp->IoStatus.Information = info;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return status;
}
//
NTSTATUS PnpHandleStartDevice(IN PDEVICE_OBJECT fdo,IN PIRP Irp)
{
NTSTATUS ntStatus;
ntStatus = ForwardAndWait(fdo, Irp);
if (!NT_SUCCESS(ntStatus))
return CompleteRequest(Irp, ntStatus, Irp->IoStatus.Information);
ntStatus = UsbStartDevice(fdo);
return CompleteRequest(Irp, ntStatus, 0);
}
NTSTATUS ForwardAndWait(IN PDEVICE_OBJECT fdo,IN PIRP Irp)
{
NTSTATUS ntStatus;
PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;
KEVENT event;
KeInitializeEvent(&event, NotificationEvent, FALSE);
IoCopyCurrentIrpStackLocationToNext(Irp);
IoSetCompletionRoutine(Irp, (PIO_COMPLETION_ROUTINE)ForwardIrpComplete,
(PVOID) &event, TRUE, TRUE, TRUE);
ntStatus = IoCallDriver(pdx->LowerDeviceObject, Irp);
if (ntStatus == STATUS_PENDING) {
KeWaitForSingleObject(&event, Executive, KernelMode, FALSE, NULL);
ntStatus = Irp->IoStatus.Status;
}
return ntStatus;
}
//
NTSTATUS ForwardIrpComplete(IN PDEVICE_OBJECT fdo,
IN PIRP Irp,
IN PKEVENT pev)
{
KeSetEvent(pev, 0, FALSE);
return STATUS_MORE_PROCESSING_REQUIRED;
}
//
NTSTATUS UsbStartDevice(IN PDEVICE_OBJECT fdo)
{
NTSTATUS ntStatus;
PDEVICE_EXTENSION pdx;
PUSB_DEVICE_DESCRIPTOR deviceDescriptor = NULL;
PURB urb;
USHORT SizeUrb;
ULONG SizeDescriptor;
pdx = fdo->DeviceExtension;
SizeUrb=sizeof(struct _URB_CONTROL_DESCRIPTOR_REQUEST);
urb = ExAllocatePool( NonPagedPool,SizeUrb);
if (urb==NULL) return STATUS_NO_MEMORY;
SizeDescriptor = sizeof(USB_DEVICE_DESCRIPTOR);
deviceDescriptor = ExAllocatePool(NonPagedPool,SizeDescriptor);
if (deviceDescriptor==NULL) {
ExFreePool(urb);
return STATUS_NO_MEMORY;
}
UsbBuildGetDescriptorRequest(urb,
SizeUrb,
USB_DEVICE_DESCRIPTOR_TYPE,
0,
0,
deviceDescriptor,
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(