⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 readwrite.cpp

📁 usb驱动及其测试程序(usb驱动中断处理演示)
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Read/Write request processors for usbint driver
// Copyright (C) 1999 by Walter Oney
// All rights reserved

#include "stddcls.h"
#include "driver.h"

#ifdef DBG
	#define MSGUSBSTRING(d,s,i) { \
		UNICODE_STRING sd; \
		if (i && NT_SUCCESS(GetStringDescriptor(d,i,&sd))) { \
			DbgPrint(s, sd.Buffer); \
			RtlFreeUnicodeString(&sd); \
		}}
#else
	#define MSGUSBSTRING(d,i,s)
#endif

NTSTATUS StartInterruptUrb(PDEVICE_EXTENSION pdx);
NTSTATUS OnInterrupt(PDEVICE_OBJECT junk, PIRP Irp, PDEVICE_EXTENSION pdx);
VOID StopInterruptUrb(PDEVICE_EXTENSION pdx);

///////////////////////////////////////////////////////////////////////////////

#pragma PAGEDCODE

VOID AbortPipe(PDEVICE_OBJECT fdo, USBD_PIPE_HANDLE hpipe)
	{							// AbortPipe
	PAGED_CODE();
	PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;
	URB urb;

	urb.UrbHeader.Length = (USHORT) sizeof(_URB_PIPE_REQUEST);
	urb.UrbHeader.Function = URB_FUNCTION_ABORT_PIPE;
	urb.UrbPipeRequest.PipeHandle = hpipe;

	NTSTATUS status = SendAwaitUrb(fdo, &urb);
	if (!NT_SUCCESS(status))
		KdPrint((DRIVERNAME " - Error %X in AbortPipe\n", status));
	}							// AbortPipe

///////////////////////////////////////////////////////////////////////////////

#pragma PAGEDCODE

NTSTATUS CreateInterruptUrb(PDEVICE_OBJECT fdo)
	{							// CreateInterruptUrb
	PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;
	ASSERT(pdx->PollingIrp == NULL);
	ASSERT(pdx->PollingUrb == NULL);

	PIRP Irp = IoAllocateIrp(pdx->LowerDeviceObject->StackSize, FALSE);
	if (!Irp)
		{
		KdPrint((DRIVERNAME " - Unable to create IRP for interrupt polling\n"));
		return STATUS_INSUFFICIENT_RESOURCES;
		}

	PURB urb = (PURB) ExAllocatePool(NonPagedPool, sizeof(_URB_BULK_OR_INTERRUPT_TRANSFER));
	if (!urb)
		{
		KdPrint((DRIVERNAME " - Unable to allocate interrupt polling URB\n"));
		IoFreeIrp(Irp);
		return STATUS_INSUFFICIENT_RESOURCES;
		}

	pdx->PollingIrp = Irp;
	pdx->PollingUrb = urb;

	return STATUS_SUCCESS;
	}							// CreateInterruptUrb

///////////////////////////////////////////////////////////////////////////////

#pragma PAGEDCODE

VOID DeleteInterruptUrb(PDEVICE_OBJECT fdo)
	{							// DeleteInterruptUrb
	PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;
	ASSERT(pdx->PollingIrp != NULL);
	ASSERT(pdx->PollingUrb != NULL);

	ExFreePool(pdx->PollingUrb);
	IoFreeIrp(pdx->PollingIrp);
	pdx->PollingIrp = NULL;
	pdx->PollingUrb = NULL;
	}							// DeleteInterruptUrb

///////////////////////////////////////////////////////////////////////////////

#pragma PAGEDCODE

NTSTATUS DispatchCleanup(PDEVICE_OBJECT fdo, PIRP Irp)
	{							// DispatchCleanup
	PAGED_CODE();
	KdPrint((DRIVERNAME " - IRP_MJ_CLEANUP\n"));
	PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;

	PIO_STACK_LOCATION stack = IoGetCurrentIrpStackLocation(Irp);

	GenericCleanupControlRequests(pdx->pgx, STATUS_CANCELLED, stack->FileObject);

	return CompleteRequest(Irp, STATUS_SUCCESS, 0);
	}							// DispatchCleanup

///////////////////////////////////////////////////////////////////////////////

#pragma PAGEDCODE

NTSTATUS DispatchCreate(PDEVICE_OBJECT fdo, PIRP Irp)
	{							// DispatchCreate
	PAGED_CODE();
	KdPrint((DRIVERNAME " - IRP_MJ_CREATE\n"));
	PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;

	PIO_STACK_LOCATION stack = IoGetCurrentIrpStackLocation(Irp);

	// Only allow one handle at a time because we only have one place to
	// remember the pending IOCTL we use for notifying the application that
	// an interrupt has occurred. Setting the exclusive flag in our device
	// object is not likely to be effective in a production driver because
	// the named device object to which the open refers will usually be the PDO,
	// which won't necessarily have the exclusive attribute.

	if (InterlockedIncrement(&pdx->handles) > 1)
		{						// too many opens
		InterlockedDecrement(&pdx->handles);
		return CompleteRequest(Irp, STATUS_ACCESS_DENIED, 0);
		}						// too many opens

	// Claim the remove lock in Win2K so that removal waits until the
	// handle closes. Don't do this in Win98, however, because this
	// device might be removed by surprise with handles open, whereupon
	// we'll deadlock in HandleRemoveDevice waiting for a close that
	// can never happen because we can't run the user-mode code that
	// would do the close.

	NTSTATUS status;
	if (win98)
		status = STATUS_SUCCESS;
	else 
		status = IoAcquireRemoveLock(&pdx->RemoveLock, stack->FileObject);

	if (!NT_SUCCESS(status))
		return CompleteRequest(Irp, status, 0);

	// Repower the device.

	status = GenericWakeupFromIdle(pdx->pgx, TRUE);
	if (NT_SUCCESS(status))
		status = STATUS_SUCCESS;	// most espcially not STATUS_PENDING
	else
		{
		InterlockedDecrement(&pdx->handles);
		if (!win98)
			IoReleaseRemoveLock(&pdx->RemoveLock, stack->FileObject);
		}

	return CompleteRequest(Irp, status, 0);
	}							// DispatchCreate

///////////////////////////////////////////////////////////////////////////////

#pragma PAGEDCODE

NTSTATUS DispatchClose(PDEVICE_OBJECT fdo, PIRP Irp)
	{							// DispatchClose
	PAGED_CODE();
	KdPrint((DRIVERNAME " - IRP_MJ_CLOSE\n"));
	PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;
	PIO_STACK_LOCATION stack = IoGetCurrentIrpStackLocation(Irp);

	// The cleanup routine should have gotten rid of any pending
	// WAITINT operation

	ASSERT(pdx->InterruptIrp == NULL);

	// Depower the device

	InterlockedDecrement(&pdx->handles);

	GenericIdleDevice(pdx->pgx, PowerDeviceD3);
	
	// Release the remove lock to match the acquisition done in DispatchCreate

	if (!win98)
		IoReleaseRemoveLock(&pdx->RemoveLock, stack->FileObject);

	return CompleteRequest(Irp, STATUS_SUCCESS, 0);
	}							// DispatchClose

///////////////////////////////////////////////////////////////////////////////

#pragma PAGEDCODE

NTSTATUS GetStringDescriptor(PDEVICE_OBJECT fdo, UCHAR istring, PUNICODE_STRING s)
	{							// GetStringDescriptor
	NTSTATUS status;
	PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;
	URB urb;

	UCHAR data[256];			// maximum-length buffer

	// If this is the first time here, read string descriptor zero and arbitrarily select
	// the first language identifer as the one to use in subsequent get-descriptor calls.

	if (!pdx->langid)
		{						// determine default language id
		UsbBuildGetDescriptorRequest(&urb, sizeof(_URB_CONTROL_DESCRIPTOR_REQUEST), USB_STRING_DESCRIPTOR_TYPE,
			0, 0, data, NULL, sizeof(data), NULL);
		status = SendAwaitUrb(fdo, &urb);
		if (!NT_SUCCESS(status))
			return status;
		pdx->langid = *(LANGID*)(data + 2);
		}						// determine default language id

	// Fetch the designated string descriptor.

	UsbBuildGetDescriptorRequest(&urb, sizeof(_URB_CONTROL_DESCRIPTOR_REQUEST), USB_STRING_DESCRIPTOR_TYPE,
		istring, pdx->langid, data, NULL, sizeof(data), NULL);
	status = SendAwaitUrb(fdo, &urb);
	if (!NT_SUCCESS(status))
		return status;

	ULONG nchars = (data[0] - 2) / 2;
	PWSTR p = (PWSTR) ExAllocatePool(PagedPool, data[0]);
	if (!p)
		return STATUS_INSUFFICIENT_RESOURCES;

	memcpy(p, data + 2, nchars*2);
	p[nchars] = 0;

	s->Length = (USHORT) (2 * nchars);
	s->MaximumLength = (USHORT) ((2 * nchars) + 2);
	s->Buffer = p;

	return STATUS_SUCCESS;
	}							// GetStringDescriptor

///////////////////////////////////////////////////////////////////////////////

#pragma LOCKEDCODE

NTSTATUS OnInterrupt(PDEVICE_OBJECT junk, PIRP Irp, PDEVICE_EXTENSION pdx)
	{							// OnInterrupt

	KIRQL oldirql;
	KeAcquireSpinLock(&pdx->polllock, &oldirql);
	pdx->pollpending = FALSE;		// allow another poll to be started
	PVOID powercontext = pdx->powercontext;
	pdx->powercontext = NULL;
	KeReleaseSpinLock(&pdx->polllock, oldirql);

	// If the poll completed successfully, do whatever it is we do when we
	// get an interrupt (in this sample, that's answering an IOCTL) and
	// reissue the read. We're trying to have a read outstanding on the
	// interrupt pipe all the time except when power is off.

	if (NT_SUCCESS(Irp->IoStatus.Status))
		{						// device signalled an interrupt
		KdPrint((DRIVERNAME " - Interrupt!\n"));

		PIRP intirp = GenericUncacheControlRequest(pdx->pgx, &pdx->InterruptIrp);
		if (intirp)
			CompleteRequest(intirp, STATUS_SUCCESS, 0);
		else
			InterlockedIncrement(&pdx->numints);

		// Unless we're in the middle of a power-off sequence, reissue the
		// polling IRP. Normally, SaveContext would have tried to cancel the
		// IRP, and we won't get to this statement because STATUS_CANCELLED
		// will fail the NT_SUCCESS test. We don't have any guarantee that the
		// IRP will actually complete with STATUS_CANCELLED, though. Hence this test.
		
		if (!powercontext)
			StartInterruptUrb(pdx); // issue next polling request
		}						// device signalled an interrupt
#if DBG	
	else
		{
		KdPrint((DRIVERNAME " - Interrupt polling IRP %X failed - %X (USBD status %X)\n",
			Irp, Irp->IoStatus.Status, URB_STATUS(pdx->PollingUrb)));
		}
#endif

	// If we cancelled the poll during a power-down sequence, notify our
	// power management code that it can continue.

	if (powercontext)
		GenericSaveRestoreComplete(powercontext);

	IoReleaseRemoveLock(&pdx->RemoveLock, Irp); // balances acquisition in StartInterruptUrb

	return STATUS_MORE_PROCESSING_REQUIRED;
	}							// OnInterrupt

///////////////////////////////////////////////////////////////////////////////

#pragma PAGEDCODE

VOID ResetDevice(PDEVICE_OBJECT fdo)
	{							// ResetDevice
	PAGED_CODE();
	PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;

	KEVENT event;
	KeInitializeEvent(&event, NotificationEvent, FALSE);
	IO_STATUS_BLOCK iostatus;

	PIRP Irp = IoBuildDeviceIoControlRequest(IOCTL_INTERNAL_USB_RESET_PORT,
		pdx->LowerDeviceObject, NULL, 0, NULL, 0, TRUE, &event, &iostatus);
	if (!Irp)
		return;

	NTSTATUS status = IoCallDriver(pdx->LowerDeviceObject, Irp);
	if (status == STATUS_PENDING)
		{
		KeWaitForSingleObject(&event, Executive, KernelMode, FALSE, NULL);
		status = iostatus.Status;
		}

	if (!NT_SUCCESS(status))
		KdPrint((DRIVERNAME " - Error %X trying to reset device\n", status));
	}							// ResetDevice

///////////////////////////////////////////////////////////////////////////////

#pragma PAGEDCODE

NTSTATUS ResetPipe(PDEVICE_OBJECT fdo, USBD_PIPE_HANDLE hpipe)
	{							// ResetPipe
	PAGED_CODE();
	PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;
	URB urb;

	urb.UrbHeader.Length = (USHORT) sizeof(_URB_PIPE_REQUEST);
	urb.UrbHeader.Function = URB_FUNCTION_RESET_PIPE;
	urb.UrbPipeRequest.PipeHandle = hpipe;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -