testdispatch.c

来自「usb开发」· C语言 代码 · 共 568 行 · 第 1/2 页

C
568
字号
#include "stdio.h"
#include "TestInc.h"

ULONG UsbVendorRequest(IN PDEVICE_OBJECT DeviceObject,
                          char * pVendorRequest);
ULONG UsbGetDeviceDescriptor(IN PDEVICE_OBJECT fdo,
							 PVOID pvOutputBuffer);
ULONG UsbGetStringDescriptor(IN PDEVICE_OBJECT fdo,
                             UCHAR             Index,
                             USHORT            LanguageId,
                             PVOID             pvOutputBuffer,
                             ULONG             ulLength);
ULONG UsbGetConfigDescriptor(IN PDEVICE_OBJECT fdo,
                             PVOID   pvOutputBuffer,
                             ULONG   ulLength);
							 
NTSTATUS UsbReceIn3(PDEVICE_EXTENSION pdx,
					 char * pRxdBuf,
					ULONG * pReadLen);							 
							 
NTSTATUS UsbSendOut1(PDEVICE_EXTENSION pdx,
					char * pTxdData,
					ULONG * pWriteLen);							 
//
NTSTATUS TestCreate(IN PDEVICE_OBJECT fdo, IN PIRP Irp)
{
	NTSTATUS ntStatus=STATUS_SUCCESS;
    PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION )fdo->DeviceExtension;

    pdx->OpenHandles++;
							
    Irp->IoStatus.Status = ntStatus;
    Irp->IoStatus.Information = 0;
    IoCompleteRequest (Irp, IO_NO_INCREMENT);
    return ntStatus;
}
//
NTSTATUS TestClose(IN PDEVICE_OBJECT fdo, IN PIRP Irp)
{
   NTSTATUS ntStatus=STATUS_SUCCESS;
   PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION )fdo->DeviceExtension;

   pdx->OpenHandles--;

   Irp->IoStatus.Status = STATUS_SUCCESS;
   Irp->IoStatus.Information = 0;
   IoCompleteRequest (Irp, IO_NO_INCREMENT);
   return ntStatus;
}



NTSTATUS TestRead(IN PDEVICE_OBJECT fdo,IN PIRP Irp)
{
	NTSTATUS NtStatus=STATUS_SUCCESS;
	
	PDEVICE_EXTENSION pdx;
	PIO_STACK_LOCATION IrpStack;
	
	LONGLONG FilePointer;
	char * pRxdBuf;
	ULONG ReadLen=16;
	
	pdx = (PDEVICE_EXTENSION )fdo->DeviceExtension;
    if (!LockDevice(fdo))
		return CompleteRequest(Irp, STATUS_DELETE_PENDING, 0);
		
	IrpStack=IoGetCurrentIrpStackLocation(Irp);
	FilePointer = IrpStack->Parameters.Read.ByteOffset.QuadPart;
	ReadLen = IrpStack->Parameters.Read.Length;
	pRxdBuf= (char *)(Irp->AssociatedIrp.SystemBuffer);
	
	if( FilePointer<0)
		return STATUS_INVALID_PARAMETER;
	else
	{
		NtStatus=UsbReceIn3(pdx,pRxdBuf,&ReadLen);
	}
	CompleteRequest(Irp, NtStatus, ReadLen);
    UnlockDevice(fdo);
	return NtStatus;	
}

NTSTATUS UsbReceIn1(PDEVICE_EXTENSION pdx, char * pRxdBuf, ULONG * pReadLen)
{
	ULONG RxdLen;
	PURB Urb;
	USHORT UrbSize;
	NTSTATUS NtStatus=STATUS_SUCCESS;
	char str[16];
	int counter=5;
	
	RxdLen=*pReadLen;
	*pReadLen=0;	
	//*check parameter valid
	DbgPrint("Read Started!");
	if(pRxdBuf==NULL)
	{
		return STATUS_INVALID_PARAMETER;
	}
	
	//Allocate memory for URB
	UrbSize=sizeof(struct _URB_BULK_OR_INTERRUPT_TRANSFER);
	Urb=(PURB)ExAllocatePool(NonPagedPool,UrbSize);
	if(Urb==NULL)
	{
		return STATUS_INSUFFICIENT_RESOURCES;
	}
	
		// Get start tick count and length of tick in 100ns units

	
	//*Read Loop exit if non-zero report
	while(counter--)
	{
		DbgPrint("1Read exeing!");
		sprintf(str,"rA=0x%x",pdx->Interface->Pipes[0].EndpointAddress);
		DbgPrint(str);
		UsbBuildInterruptOrBulkTransferRequest(
			Urb,UrbSize,
			pdx->Interface->Pipes[0].PipeHandle,
			pRxdBuf,NULL,RxdLen,
			USBD_TRANSFER_DIRECTION_IN|USBD_SHORT_TRANSFER_OK,
			NULL
		);
		DbgPrint("Urb built");
		NtStatus=Call_USBDI(pdx,Urb);
		DbgPrint("Called");
		if( !NT_SUCCESS(NtStatus) || !USBD_SUCCESS( Urb->UrbHeader.Status))
		{		
			NtStatus = STATUS_UNSUCCESSFUL;
			DbgPrint("read failed 1!");
			continue;
		}
		else
		{
			*pReadLen=Urb->UrbBulkOrInterruptTransfer.TransferBufferLength;
			break;
		}	
	}
	if(counter==-1)
	{
		DbgPrint("read failed 2!");
		NtStatus=STATUS_UNSUCCESSFUL;
	}
	else
	{
		DbgPrint("read successed!");
	}
	ExFreePool(Urb);
	return NtStatus;
}

NTSTATUS UsbReceIn3(PDEVICE_EXTENSION pdx, char * pRxdBuf, ULONG * pReadLen)
{
	ULONG RxdLen;
	PURB Urb;
	USHORT UrbSize;
	NTSTATUS NtStatus=STATUS_SUCCESS;
	char str[16];
	int counter=5;
	
	RxdLen=*pReadLen;
	*pReadLen=0;	
	//check parameter valid
	DbgPrint("3Read Started!");
	if(pRxdBuf==NULL)
	{
		return STATUS_INVALID_PARAMETER;
	}
	
	//*Allocate memory for URB*
	UrbSize=sizeof(struct _URB_BULK_OR_INTERRUPT_TRANSFER);
	Urb=(PURB)ExAllocatePool(NonPagedPool,UrbSize);
	if(Urb==NULL)
	{
		return STATUS_INSUFFICIENT_RESOURCES;
	}
	
		// Get start tick count and length of tick in 100ns units

	
	//*Read Loop exit if non-zero report*
	while(counter--)
	{
		DbgPrint("3Read exeing!");
		sprintf(str,"rA=0x%x",pdx->Interface->Pipes[2].EndpointAddress);
		DbgPrint(str);
		UsbBuildInterruptOrBulkTransferRequest(
			Urb,UrbSize,
			pdx->Interface->Pipes[2].PipeHandle,
			pRxdBuf,NULL,RxdLen,
			USBD_TRANSFER_DIRECTION_IN|USBD_SHORT_TRANSFER_OK,
			NULL
		);
		DbgPrint("Urb built");
		NtStatus=Call_USBDI(pdx,Urb);
		DbgPrint("Called");
		if( !NT_SUCCESS(NtStatus) || !USBD_SUCCESS( Urb->UrbHeader.Status))
		{		
			NtStatus = STATUS_UNSUCCESSFUL;
			DbgPrint("read failed 1!");
			continue;
		}
		else
		{
			*pReadLen=Urb->UrbBulkOrInterruptTransfer.TransferBufferLength;
			break;
		}	
	}
	if(counter==-1)
	{
		DbgPrint("read failed 2!");
		NtStatus=STATUS_UNSUCCESSFUL;
	}
	else
	{
		DbgPrint("read successed!");
	}
	ExFreePool(Urb);
	return NtStatus;
}

NTSTATUS TestWrite(IN PDEVICE_OBJECT fdo,IN PIRP Irp)
{
	NTSTATUS NtStatus=STATUS_SUCCESS;
	
	PDEVICE_EXTENSION pdx;
	PIO_STACK_LOCATION IrpStack;
	
	LONGLONG FilePointer;
	char * pTxdData;
	ULONG WriteLen;
	
	pdx = (PDEVICE_EXTENSION )fdo->DeviceExtension;
    if (!LockDevice(fdo))
		return CompleteRequest(Irp, STATUS_DELETE_PENDING, 0);	
		
	IrpStack = IoGetCurrentIrpStackLocation(Irp);
	FilePointer=IrpStack->Parameters.Write.ByteOffset.QuadPart;
	pTxdData= (char *)(Irp->AssociatedIrp.SystemBuffer);
	WriteLen   =IrpStack->Parameters.Write.Length;
	
	if(FilePointer<0)
		NtStatus=STATUS_INVALID_PARAMETER;
	else
	{
		NtStatus=UsbSendOut1(pdx,pTxdData,&WriteLen);
	}
	
	CompleteRequest(Irp, NtStatus, WriteLen);
    UnlockDevice(fdo);
	return NtStatus;
}
//
NTSTATUS UsbSendOut1(PDEVICE_EXTENSION pdx,char * pTxdData,ULONG * pWriteLen)
{
	ULONG TxdLen;
	PURB Urb;
	USHORT UrbSize;
	NTSTATUS NtStatus=STATUS_SUCCESS;
	char str[16];
	TxdLen=*pWriteLen;
	*pWriteLen=0;
	
	DbgPrint("UsbSendOut1 started!");
	/*check parameter valid*/
	if(pTxdData==NULL)
	{
		return STATUS_INVALID_PARAMETER;
	}
	
	/*Allocate memory for URB*/
	UrbSize=sizeof(struct _URB_BULK_OR_INTERRUPT_TRANSFER);
	Urb=(PURB)ExAllocatePool(NonPagedPool,UrbSize);
	if(Urb==NULL)
	{
		return STATUS_INSUFFICIENT_RESOURCES;
	}
	sprintf(str,"wA=0x%x",pdx->Interface->Pipes[1].EndpointAddress);
	DbgPrint(str);
	UsbBuildInterruptOrBulkTransferRequest(
		Urb,UrbSize,
		pdx->Interface->Pipes[1].PipeHandle,

⌨️ 快捷键说明

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