📄 readwrite.cpp
字号:
return; // defer notification until IRP finishes
} // cancel poll
KeReleaseSpinLock(&pdx->polllock, oldirql);
} // losing power
// No need to cancel the poll, so notify power management code immediately
GenericSaveRestoreComplete(context);
} // SaveContext
///////////////////////////////////////////////////////////////////////////////
#pragma PAGEDCODE
NTSTATUS SendAwaitUrb(PDEVICE_OBJECT fdo, PURB urb)
{ // SendAwaitUrb
PAGED_CODE();
ASSERT(KeGetCurrentIrql() == PASSIVE_LEVEL);
PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;
KEVENT event;
KeInitializeEvent(&event, NotificationEvent, FALSE);
IO_STATUS_BLOCK iostatus;
PIRP Irp = IoBuildDeviceIoControlRequest(IOCTL_INTERNAL_USB_SUBMIT_URB,
pdx->LowerDeviceObject, NULL, 0, NULL, 0, TRUE, &event, &iostatus);
if (!Irp)
{
KdPrint((DRIVERNAME " - Unable to allocate IRP for sending URB\n"));
return STATUS_INSUFFICIENT_RESOURCES;
}
PIO_STACK_LOCATION stack = IoGetNextIrpStackLocation(Irp);
stack->Parameters.Others.Argument1 = (PVOID) urb;
NTSTATUS status = IoCallDriver(pdx->LowerDeviceObject, Irp);
if (status == STATUS_PENDING)
{
KeWaitForSingleObject(&event, Executive, KernelMode, FALSE, NULL);
status = iostatus.Status;
}
return status;
} // SendAwaitUrb
///////////////////////////////////////////////////////////////////////////////
#pragma PAGEDCODE
NTSTATUS StartDevice(PDEVICE_OBJECT fdo, PCM_PARTIAL_RESOURCE_LIST raw, PCM_PARTIAL_RESOURCE_LIST translated)
{ // StartDevice
PAGED_CODE();
NTSTATUS status;
PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;
URB urb; // URB for use in this subroutine
// Read our device descriptor. The only real purpose to this would be to find out how many
// configurations there are so we can read their descriptors. In this simplest of examples,
// there's only one configuration.
UsbBuildGetDescriptorRequest(&urb, sizeof(_URB_CONTROL_DESCRIPTOR_REQUEST), USB_DEVICE_DESCRIPTOR_TYPE,
0, 0, &pdx->dd, NULL, sizeof(pdx->dd), NULL);
status = SendAwaitUrb(fdo, &urb);
if (!NT_SUCCESS(status))
{
KdPrint((DRIVERNAME " - Error %X trying to read device descriptor\n", status));
return status;
}
ASSERT(pdx->dd.bNumConfigurations == 1); // only expect one configuration
MSGUSBSTRING(fdo, DRIVERNAME " - Configuring device from %ws\n", pdx->dd.iManufacturer);
MSGUSBSTRING(fdo, DRIVERNAME " - Product is %ws\n", pdx->dd.iProduct);
MSGUSBSTRING(fdo, DRIVERNAME " - Serial number is %ws\n", pdx->dd.iSerialNumber);
// Read the descriptor of the first configuration. This requires two steps. The first step
// reads the fixed-size configuration descriptor alone. The second step reads the
// configuration descriptor plus all imbedded interface and endpoint descriptors.
USB_CONFIGURATION_DESCRIPTOR tcd;
UsbBuildGetDescriptorRequest(&urb, sizeof(_URB_CONTROL_DESCRIPTOR_REQUEST), USB_CONFIGURATION_DESCRIPTOR_TYPE,
0, 0, &tcd, NULL, sizeof(tcd), NULL);
status = SendAwaitUrb(fdo, &urb);
if (!NT_SUCCESS(status))
{
KdPrint((DRIVERNAME " - Error %X trying to read configuration descriptor 1\n", status));
return status;
}
ULONG size = tcd.wTotalLength;
PUSB_CONFIGURATION_DESCRIPTOR pcd = (PUSB_CONFIGURATION_DESCRIPTOR) ExAllocatePool(NonPagedPool, size);
if (!pcd)
{
KdPrint((DRIVERNAME " - Unable to allocate %X bytes for configuration descriptor\n", size));
return STATUS_INSUFFICIENT_RESOURCES;
}
__try
{
UsbBuildGetDescriptorRequest(&urb, sizeof(_URB_CONTROL_DESCRIPTOR_REQUEST), USB_CONFIGURATION_DESCRIPTOR_TYPE,
0, 0, pcd, NULL, size, NULL);
status = SendAwaitUrb(fdo, &urb);
if (!NT_SUCCESS(status))
{
KdPrint((DRIVERNAME " - Error %X trying to read configuration descriptor 1\n", status));
return status;
}
MSGUSBSTRING(fdo, DRIVERNAME " - Selecting configuration named %ws\n", pcd->iConfiguration);
// Locate the descriptor for the one and only interface we expect to find
PUSB_INTERFACE_DESCRIPTOR pid = USBD_ParseConfigurationDescriptorEx(pcd, pcd,
-1, -1, -1, -1, -1);
ASSERT(pid);
MSGUSBSTRING(fdo, DRIVERNAME " - Selecting interface named %ws\n", pid->iInterface);
// Create a URB to use in selecting a configuration.
USBD_INTERFACE_LIST_ENTRY interfaces[2] = {
{pid, NULL},
{NULL, NULL}, // fence to terminate the array
};
PURB selurb = USBD_CreateConfigurationRequestEx(pcd, interfaces);
if (!selurb)
{
KdPrint((DRIVERNAME " - Unable to create configuration request\n"));
return STATUS_INSUFFICIENT_RESOURCES;
}
__try
{
// Verify that the interface describes exactly the endpoints we expect
if (pid->bNumEndpoints != 1)
{
KdPrint((DRIVERNAME " - %d is the wrong number of endpoints\n", pid->bNumEndpoints));
return STATUS_DEVICE_CONFIGURATION_ERROR;
}
PUSB_ENDPOINT_DESCRIPTOR ped = (PUSB_ENDPOINT_DESCRIPTOR) pid;
ped = (PUSB_ENDPOINT_DESCRIPTOR) USBD_ParseDescriptors(pcd, tcd.wTotalLength, ped, USB_ENDPOINT_DESCRIPTOR_TYPE);
if (!ped || ped->bEndpointAddress != 0x82 || ped->bmAttributes != USB_ENDPOINT_TYPE_INTERRUPT || ped->wMaxPacketSize != 4)
{
KdPrint((DRIVERNAME " - Endpoint has wrong attributes\n"));
return STATUS_DEVICE_CONFIGURATION_ERROR;
}
++ped;
PUSBD_INTERFACE_INFORMATION pii = interfaces[0].Interface;
ASSERT(pii->NumberOfPipes == pid->bNumEndpoints);
// Submit the set-configuration request
status = SendAwaitUrb(fdo, selurb);
if (!NT_SUCCESS(status))
{
KdPrint((DRIVERNAME " - Error %X trying to select configuration\n", status));
return status;
}
// Save the configuration and pipe handles
pdx->hconfig = selurb->UrbSelectConfiguration.ConfigurationHandle;
pdx->hintpipe = pii->Pipes[0].PipeHandle;
// Transfer ownership of the configuration descriptor to the device extension
pdx->pcd = pcd;
pcd = NULL;
// Begin polling the interrupt endpoint
StartInterruptUrb(pdx);
}
__finally
{
ExFreePool(selurb);
}
}
__finally
{
if (pcd)
ExFreePool(pcd);
}
return STATUS_SUCCESS;
} // StartDevice
///////////////////////////////////////////////////////////////////////////////
// This function issues a read to the interrupt pipe.
#pragma LOCKEDCODE
NTSTATUS StartInterruptUrb(PDEVICE_EXTENSION pdx)
{ // StartInterruptUrb
// If the interrupt polling IRP is currently running, don't try to start
// it again.
BOOLEAN startirp;
KIRQL oldirql;
KeAcquireSpinLock(&pdx->polllock, &oldirql);
if (pdx->pollpending)
startirp = FALSE;
else
startirp = TRUE, pdx->pollpending = TRUE;
KeReleaseSpinLock(&pdx->polllock, oldirql);
if (!startirp)
return STATUS_DEVICE_BUSY; // already pending
PIRP Irp = pdx->PollingIrp;
PURB urb = pdx->PollingUrb;
ASSERT(Irp && urb);
// Acquire the remove lock so we can't remove the lower device while the IRP
// is still active.
NTSTATUS status = IoAcquireRemoveLock(&pdx->RemoveLock, Irp);
if (!NT_SUCCESS(status))
{
pdx->pollpending = 0;
return status;
}
// Initialize the URB we use for reading the interrupt pipe
UsbBuildInterruptOrBulkTransferRequest(urb, sizeof(_URB_BULK_OR_INTERRUPT_TRANSFER),
pdx->hintpipe, &pdx->intdata, NULL, 4, USBD_TRANSFER_DIRECTION_IN | USBD_SHORT_TRANSFER_OK, NULL);
// Install "OnInterrupt" as the completion routine for the polling IRP.
IoSetCompletionRoutine(Irp, (PIO_COMPLETION_ROUTINE) OnInterrupt, pdx, TRUE, TRUE, TRUE);
// Initialize the IRP for an internal control request
PIO_STACK_LOCATION stack = IoGetNextIrpStackLocation(Irp);
stack->MajorFunction = IRP_MJ_INTERNAL_DEVICE_CONTROL;
stack->Parameters.DeviceIoControl.IoControlCode = IOCTL_INTERNAL_USB_SUBMIT_URB;
stack->Parameters.Others.Argument1 = urb;
// This IRP might have been cancelled the last time it was used, in which case
// the cancel flag will still be on. Clear it to prevent USBD from thinking that it's
// been cancelled again! A better way to do this would be to call IoReuseIrp,
// but that function is not available in Win98/Me, and I thought it better to
// avoid requiring WDMSTUB for just that one function.
Irp->Cancel = FALSE;
return IoCallDriver(pdx->LowerDeviceObject, Irp);
} // StartInterruptUrb
///////////////////////////////////////////////////////////////////////////////
// This function cancels our outstanding interrupt read
#pragma LOCKEDCODE
VOID StopInterruptUrb(PDEVICE_EXTENSION pdx)
{ // StopInterruptUrb
if (pdx->pollpending)
IoCancelIrp(pdx->PollingIrp);
} // StopInterruptUrb
///////////////////////////////////////////////////////////////////////////////
#pragma PAGEDCODE
VOID StopDevice(IN PDEVICE_OBJECT fdo, BOOLEAN oktouch /* = FALSE */)
{ // StopDevice
PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;
// Cancel the interrupt polling URB in case it's currently active
StopInterruptUrb(pdx);
// If it's okay to touch our hardware (i.e., we're processing an IRP_MN_STOP_DEVICE),
// deconfigure the device.
if (oktouch)
{ // deconfigure device
URB urb;
UsbBuildSelectConfigurationRequest(&urb, sizeof(_URB_SELECT_CONFIGURATION), NULL);
NTSTATUS status = SendAwaitUrb(fdo, &urb);
if (!NT_SUCCESS(status))
KdPrint((DRIVERNAME " - Error %X trying to deconfigure device\n", status));
} // deconfigure device
if (pdx->pcd)
ExFreePool(pdx->pcd);
pdx->pcd = NULL;
} // StopDevice
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -