阅读:963回复:1
请教高手 ezusb.sys 驱动实时处理如何写呢。给我的源码给点意见吧
高手们好:
我的应用程序如下: ULONG ulLength = 8*1024*1024; unsigned char *tempbuffer=(unsigned char *)malloc(ulLength); bResult = DeviceIoControl (hDevice, ioctl_val, &bulkControl, sizeof (BULK_TRANSFER_CONTROL), tempbuffer, ulLength ,//length (unsigned long *)&nBytes, NULL); 驱动里的程序如下: NTSTATUS Ezusb_Read_Write( IN PDEVICE_OBJECT fdo, IN PIRP Irp ) { PDEVICE_EXTENSION pdx = fdo->DeviceExtension; NTSTATUS ntStatus; PIO_STACK_LOCATION irp=StackIoGetCurrentIrpStackLocation (Irp); PBULK_TRANSFER_CONTROL bulkControl = (PBULK_TRANSFER_CONTROL)Irp->AssociatedIrp.SystemBuffer; int i; ULONG bufferLength = irpStack->Parameters.DeviceIoControl.OutputBufferLength; PURB urb = NULL; ULONG urbSize = 0; ULONG transferFlags = 0; ULONG Length = MmGetMdlByteCount(Irp->MdlAddress); //addin PUSBD_INTERFACE_INFORMATION interfaceInfo = NULL; BYTE* pMdlAddress = MmGetSystemAddressForMdlSafe(Irp->MdlAddress, NormalPagePriority);//addin ULONG singleTransferLength = 64*1024;//addin PUSBD_PIPE_INFORMATION pipeInfo = NULL; USBD_PIPE_HANDLE pipeHandle = NULL; Ezusb_KdPrint((\"OutputBufferLength==%d\\n\"),irpStack->Parameters.DeviceIoControl.OutputBufferLength); Ezusb_KdPrint((\"enter Ezusb_Read_Write()\\n\")); Ezusb_KdPrint((\"bulkControl->pipeNum=%d\\n\",bulkControl->pipeNum)); Ezusb_KdPrint((\"Length=%d\\n\",Length)); // // verify that the selected pipe is valid, and get a handle to it. If anything // is wrong, return an error // interfaceInfo = pdx->Interface; if (!interfaceInfo) { Ezusb_KdPrint((\"Ezusb_Read_Write() no interface info - Exiting\\n\")); return STATUS_UNSUCCESSFUL; } Ezusb_KdPrint((\"interfaceInfo->NumberOfPipes=%d\\n\",interfaceInfo->NumberOfPipes)); if (bulkControl->pipeNum > interfaceInfo->NumberOfPipes) { Ezusb_KdPrint((\"pipeNum Ezusb_Read_Write() invalid pipe - Exiting\\n\")); return STATUS_INVALID_PARAMETER; } pipeInfo = &(interfaceInfo->Pipes[bulkControl->pipeNum]); if (!((pipeInfo->PipeType == UsbdPipeTypeBulk) || (pipeInfo->PipeType == UsbdPipeTypeInterrupt))) { Ezusb_KdPrint((\" PipeType Ezusb_Read_Write() invalid pipe - Exiting\\n\")); return STATUS_INVALID_PARAMETER; } pipeHandle = pipeInfo->PipeHandle; if (!pipeHandle) { Ezusb_KdPrint((\"pipeHandle Ezusb_Read_Write() invalid pipe - Exiting\\n\")); return STATUS_UNSUCCESSFUL; } Ezusb_KdPrint((\"pipe\'s MaximumTransferSize==%d\\n\"),pipeInfo->MaximumTransferSize); if (bufferLength > pipeInfo->MaximumTransferSize) { Ezusb_KdPrint((\"Ezusb_Read_Write() invalid transfer size - Exiting\\n\")); return STATUS_INVALID_PARAMETER; } // // allocate and fill in the Usb request (URB) // RtlCopyMemory // urbSize = sizeof(struct _URB_BULK_OR_INTERRUPT_TRANSFER); urb = ExAllocatePool(NonPagedPool,urbSize); if (!urb) { Ezusb_KdPrint((\"Ezusb_Read_Write() unable to alloc URB - Exiting\\n\")); return STATUS_NO_MEMORY; } // PVOID ioBuffer = Irp->AssociatedIrp.SystemBuffer; Ezusb_KdPrint((\"Irp->MdlAddress bufferLength==%d\\n\"),bufferLength); transferFlags = USBD_SHORT_TRANSFER_OK; // transferFlags = USBD_TRANSFER_DIRECTION_IN; // // get direction info from the endpoint address // for( i=0; i< 128; i++) { if (USB_ENDPOINT_DIRECTION_IN(pipeInfo->EndpointAddress)) transferFlags |= USBD_TRANSFER_DIRECTION_IN; UsbBuildInterruptOrBulkTransferRequest(urb, //ptr to urb (USHORT) urbSize, //size of urb pipeHandle, //usbd pipe handle NULL, //TransferBuffer Irp->MdlAddress, //mdl singleTransferLength,//bufferLength, //bufferlength transferFlags, //flags NULL); //link // // Call the USB Stack. // ntStatus = Ezusb_CallUSBD(fdo, urb); // // If the transfer was successful, report the length of the transfer to the // caller by setting IoStatus.Information // if (NT_SUCCESS(ntStatus)) { Irp->IoStatus.Information = urb->UrbBulkOrInterruptTransfer.TransferBufferLength; Ezusb_KdPrint((\"Successfully transfered 0x%x bytes\\n\",Irp->IoStatus.Information)); } //然后用UsbBuildInterruptOrBulkTransferRequest()循环读取数据放到ioBuffer中,每次成功返回后都拷贝数据如下: RtlCopyMemory((pMdlAddress + i*singleTransferLength), bulkControl, singleTransferLength); } // // free the URB // ExFreePool(urb); return ntStatus; } 我是依葫芦画瓢,拿别人的程序改的。我想实现的功能是在应用中开大缓存,把地址传给驱动,然驱动不断循环从设备那读取我要的数据,后拷贝到我的应用缓存中去。这样行吗。 我在调试中发现outputbufferlength的值不是我传来的值,甚至是负数。驱动在if (bufferLength > pipeInfo->MaximumTransferSize)这里就退出了。所以下面的我就无法调。我想也会有很多错的。清高手们看看。。 |
|
沙发#
发布于:2003-09-25 12:18
有哪位高手指导啊。
|
|