在《》我们所说的读写操作在本篇实现。
在WDF中实现此功能主要为:EvtIoRead和EvtIoWrite。
首先,在EvtDeviceAdd设置以上两个回调事件。
ioQueueConfig.EvtIoRead = EvtIoRead;
ioQueueConfig.EvtIoWrite = EvtIoWrite;
然后,在EvtDevicePrepareHardware中获取WDFUSBPIPE并测试他。
pDeviceContext->BulkReadPipe = WdfUsbInterfaceGetConfiguredPipe(
pDeviceContext->UsbInterface,
BULK_IN_ENDPOINT_INDEX,
NULL);// pipeInfo
WdfUsbTargetPipeSetNoMaximumPacketSizeCheck(pDeviceContext->BulkReadPipe);
pDeviceContext->BulkWritePipe = WdfUsbInterfaceGetConfiguredPipe(
pDeviceContext->UsbInterface,
BULK_OUT_ENDPOINT_INDEX,
NULL);// pipeInfo
WdfUsbTargetPipeSetNoMaximumPacketSizeCheck(pDeviceContext->BulkWritePipe);
(1)获取WDFUSBPIPE
使用[](https://msdn.microsoft.com/en-us/library/windows/hardware/ff550057%28v=vs.85%29.aspx "WdfUsbInterfaceGetConfiguredPipe")方法.
WDFUSBPIPE WdfUsbInterfaceGetConfiguredPipe(
[in] WDFUSBINTERFACE UsbInterface,
[in] UCHAR PipeIndex,
[out, optional] PWDF_USB_PIPE_INFORMATION PipeInfo
);
(2) 测试WDFUSBPIPE
使用[](https://msdn.microsoft.com/en-us/library/windows/hardware/ff550057%28v=vs.85%29.aspx "WdfUsbTargetPipeSetNoMaximumPacketSizeCheck")方法
VOID WdfUsbTargetPipeSetNoMaximumPacketSizeCheck(
[in] WDFUSBPIPE Pipe
);
最后,实现以上两个回调事件和他们的完成例程。
1. 获得传输缓存
读:[](https://msdn.microsoft.com/en-us/library/windows/hardware/ff550019%28v=vs.85%29.aspx "WdfRequestRetrieveOutputMemory")
NTSTATUS WdfRequestRetrieveOutputMemory(
[in] WDFREQUEST Request,
[out] WDFMEMORY *Memory
);
写:[](https://msdn.microsoft.com/en-us/library/windows/hardware/ff550015%28v=vs.85%29.aspx "WdfRequestRetrieveInputMemory")
NTSTATUS WdfRequestRetrieveInputMemory(
[in] WDFREQUEST Request,
[out] WDFMEMORY *Memory
);
2. 格式化并发送一个请求对象到USB驱动程序堆栈
读:[](https://msdn.microsoft.com/en-us/library/windows/hardware/ff551136%28v=vs.85%29.aspx "WdfUsbTargetPipeFormatRequestForRead")
NTSTATUS WdfUsbTargetPipeFormatRequestForRead(
[in] WDFUSBPIPE Pipe,
[in] WDFREQUEST Request,
[in, optional] WDFMEMORY ReadMemory,
[in, optional] PWDFMEMORY_OFFSET ReadOffset
);
写:[](https://msdn.microsoft.com/en-us/library/windows/hardware/ff551141%28v=vs.85%29.aspx "WdfUsbTargetPipeFormatRequestForWrite")
NTSTATUS WdfUsbTargetPipeFormatRequestForWrite(
[in] WDFUSBPIPE Pipe,
[in] WDFREQUEST Request,
[in, optional] WDFMEMORY WriteMemory,
[in, optional] PWDFMEMORY_OFFSET WriteOffset
);
3. 对请求实现一个完成例程
(1) 设置完成例程
使用[](https://msdn.microsoft.com/en-us/library/windows/hardware/ff550030%28v=vs.85%29.aspx "WdfRequestSetCompletionRoutine")方法。
VOID WdfRequestSetCompletionRoutine(
[in] WDFREQUEST Request,
[in, optional] PFN_WDF_REQUEST_COMPLETION_ROUTINE CompletionRoutine,
[in, optional] WDFCONTEXT CompletionContext
);
(2) 完成例程
1). 检查请求状态
status = CompletionParams->IoStatus.Status;
2). 检查传输的字节数
读完:
bytesRead = usbCompletionParams->Parameters.PipeRead.Length;
写完:
bytesWritten = usbCompletionParams->Parameters.PipeWrite.Length;
3). 检查USBD状态
首先,赋值
usbCompletionParams = CompletionParams->Parameters.Usb.Completion;
最后,获取
usbCompletionParams->UsbdStatus;
参考文章:
1. 《Working with USB Pipes》
2. 《How to send USB bulk transfer requests》
原文地址:http://blog.csdn.net/xiaobin_hlj80/article/details/46664815