阅读:1176回复:2
在系统线程里直接向USBD发请求来和设备读取数据。如何做呢。给个思路吧
那位大侠能讲解USB驱动中构建一个URB并传到USBD所要的主要函数和流程呢。中间是要通过IRP来传的。
我现在想在系统线程里直接向USBD发请求来和设备读取数据。如何做呢。给个思路吧 |
|
沙发#
发布于:2003-10-10 16:48
我的源码想这样:
我在系统线程中调用下面的函数; void ThreadProc(PDEVICE_EXTENSION pdx ) { while(true) { Ezusb_Read_Write(usbfdo,usbIrp);// 就在这句,我不知道如何把上面这个函数所要的环境传进去,我就用了全局变量usbfdo,usbIrp.在我DeviceIoControl的时候就把fdo,irp 赋值给usbfdo,usbIrp.但在函数里面语句 PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation (Irp); PBULK_TRANSFER_CONTROL bulkControl = (PBULK_TRANSFER_CONTROL)Irp->AssociatedIrp.SystemBuffer;这里bulkControl的值传不进来。我想是IoGetCurrentIrpStackLocation 的关系吧。 ???? IoGetCurrentIrpStackLocation这个函数要如何使用,在什么环境下用呢。 ???? // } } NTSTATUS Ezusb_Read_Write( IN PDEVICE_OBJECT fdo, IN PIRP Irp ) /*++ Routine Description: Arguments: Return Value: NT status code STATUS_SUCCESS: Read was done successfully STATUS_INVALID_PARAMETER_3: The Endpoint Index does not specify an IN pipe STATUS_NO_MEMORY: Insufficient data memory was supplied to perform the READ This routine fills the status code into the Irp --*/ { PDEVICE_EXTENSION pdx = fdo->DeviceExtension; NTSTATUS ntStatus; PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation (Irp); PBULK_TRANSFER_CONTROL bulkControl = (PBULK_TRANSFER_CONTROL)Irp->AssociatedIrp.SystemBuffer; ULONG bufferLength = irpStack->Parameters.DeviceIoControl.OutputBufferLength; PURB urb = NULL; ULONG urbSize = 0; ULONG transferFlags = 0; PUSBD_INTERFACE_INFORMATION interfaceInfo = NULL; PUSBD_PIPE_INFORMATION pipeInfo = NULL; USBD_PIPE_HANDLE pipeHandle = NULL; Ezusb_KdPrint((\"enter Ezusb_Read_Write()n\")); // // 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 - Exitingn\")); return STATUS_UNSUCCESSFUL; } if (bulkControl->pipeNum > interfaceInfo->NumberOfPipes) { Ezusb_KdPrint((\"Ezusb_Read_Write() invalid pipe - Exitingn\")); return STATUS_INVALID_PARAMETER; } pipeInfo = &(interfaceInfo->Pipes[bulkControl->pipeNum]); if (!((pipeInfo->PipeType == UsbdPipeTypeBulk) || (pipeInfo->PipeType == UsbdPipeTypeInterrupt))) { Ezusb_KdPrint((\"Ezusb_Read_Write() invalid pipe - Exitingn\")); return STATUS_INVALID_PARAMETER; } pipeHandle = pipeInfo->PipeHandle; if (!pipeHandle) { Ezusb_KdPrint((\"Ezusb_Read_Write() invalid pipe - Exitingn\")); return STATUS_UNSUCCESSFUL; } if (bufferLength > pipeInfo->MaximumTransferSize) { Ezusb_KdPrint((\"Ezusb_Read_Write() invalid transfer size - Exitingn\")); return STATUS_INVALID_PARAMETER; } // // allocate and fill in the Usb request (URB) // urbSize = sizeof(struct _URB_BULK_OR_INTERRUPT_TRANSFER); urb = ExAllocatePool(NonPagedPool,urbSize); if (!urb) { Ezusb_KdPrint((\"Ezusb_Read_Write() unable to alloc URB - Exitingn\")); return STATUS_NO_MEMORY; } transferFlags = USBD_SHORT_TRANSFER_OK; // // get direction info from the endpoint address // 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 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 bytesn\",Irp->IoStatus.Information)); } // // free the URB // ExFreePool(urb); return ntStatus; } |
|
板凳#
发布于:2003-10-10 21:33
怎么没人给点意见呢。是不是我问的问题太菜,或不解其意啊。
不在郁闷中成长,就在郁闷中毁灭。 |
|