Windows驱动_USB驱动之七

时间:2021-06-07 17:42:58

              偶然的机会知道了一个同事的工资,竟然比自己多1K多,刚开始,自己心里有点不平衡,一般来说,经验差不多,工资的水平应该不会低多少,后来,这跟自己的环境,以及以前的工资都是有很大的关系的,后来想到,自己之前的工资,真实很低,这边给这么多,马上就答应了,如果换做另外一个人,因为他目前的工资水平已经很高,所以他可能对这个工资会考虑一下,如果公司的项目急需要人,就会跟他涨工资,这就促成了,他的工资比一般人高,但是这种高,千万不要以为,是公司认为他的能力够,而是项目的原因,所以,当公司一下没项目的时候,就立马会从这样的人下手。所以,我们要从长远来看,比如,你在公司的发展,公司是不是认可你,承认过你等等,这些都可以看出公司对你的看重,当然,公司不能一下,将你的工资,加得很高,这个必须符合整个公司的工资水平等等。所以,我们必须从长远来看这个问题。

 

             今天,我们来看关于Windows驱动中USB设备的同步传输是如何进行的,昨天,我们说到了,这个函数是ReadWriteIsochEndPoints,首先看下这个函数的源码:

VOID
ReadWriteIsochEndPoints(
_In_ WDFQUEUE Queue,
_In_ WDFREQUEST Request,
_In_ ULONG Length,
_In_ WDF_REQUEST_TYPE RequestType
)
/*++

Routine Description:

This routine does some validation and invokes appropriate function to perform Isoch transfer

--*/
{
NTSTATUS status;
WDF_USB_PIPE_INFORMATION pipeInfo;
PFILE_CONTEXT fileContext;
WDFUSBPIPE pipe;
PDEVICE_CONTEXT deviceContext;
PREQUEST_CONTEXT rwContext;

UNREFERENCED_PARAMETER(Length);

UsbSamp_DbgPrint(3, ("ReadWriteIsochEndPoints - begins\n"));

//
// Get the pipe associate with this request.
//
fileContext = GetFileContext(WdfRequestGetFileObject(Request));
pipe = fileContext->Pipe;

WDF_USB_PIPE_INFORMATION_INIT(&pipeInfo);
WdfUsbTargetPipeGetInformation(pipe, &pipeInfo);

if ((WdfUsbPipeTypeIsochronous != pipeInfo.PipeType)) {
UsbSamp_DbgPrint(1, ("Pipe type is not Isochronous\n"));
status = STATUS_INVALID_DEVICE_REQUEST;
goto Exit;

}

if (RequestType == WdfRequestTypeRead && WdfUsbTargetPipeIsInEndpoint(pipe) == FALSE) {
UsbSamp_DbgPrint(1, ("Invalid pipe - not an input pipe\n"));
status = STATUS_INVALID_PARAMETER;
goto Exit;
}

if (RequestType == WdfRequestTypeWrite && WdfUsbTargetPipeIsOutEndpoint(pipe) == FALSE) {
UsbSamp_DbgPrint(1, ("Invalid pipe - not an output pipe\n"));
status = STATUS_INVALID_PARAMETER;
goto Exit;
}

deviceContext = GetDeviceContext(WdfIoQueueGetDevice(Queue));
rwContext = GetRequestContext(Request);

if (RequestType == WdfRequestTypeRead) {
rwContext->Read = TRUE;
status = WdfRequestForwardToIoQueue(Request, deviceContext->IsochReadQueue);
}
else {
rwContext->Read = FALSE;
status = WdfRequestForwardToIoQueue(Request, deviceContext->IsochWriteQueue);
}

if (!NT_SUCCESS(status)){
UsbSamp_DbgPrint(1, ("WdfRequestForwardToIoQueue failed with status 0x%x\n", status));
goto Exit;
}

return;

Exit:
WdfRequestCompleteWithInformation(Request, status, 0);
return;
}


这个函数的上半部分,好理解,得到pipeinfo,我们知道同步读,所以对应的PIPE输出端点OUT ,同步写,对应的PIPE的输入端点IN,下半部分很简单,直接调用WdfRequestForwardToIoQueue将请求送至相应的同步读和写队列。这个时候,我们可以回忆一下,这两个队列,是如何注册的,我们这两个队列都有注册队列事件回调函数,当队列中有请求进入的时候,将调用这个回调函数,我们看前面的代码:

    status = WdfIoQueueReadyNotify(pDevContext->IsochReadQueue,
                                   UsbSamp_EvtIoQueueReadyNotification,
                                   (WDFCONTEXT)pDevContext);

    status = WdfIoQueueReadyNotify(pDevContext->IsochWriteQueue,
                                   UsbSamp_EvtIoQueueReadyNotification,
                                   (WDFCONTEXT)pDevContext);

我们直接来看下这个函数UsbSamp_EvtIoQueueReadyNotification:

VOID
UsbSamp_EvtIoQueueReadyNotification(
WDFQUEUE Queue,
WDFCONTEXT Context
)
/*++

Routine Description:

This function is called when the WDF queue transitions from 0 to 1 requests in the
queue. Because we are using queue level synchronization, the framework will not
call this routine concurrently if another request is received while this routine is
handling the previously retrieved request. That way we can compute the StartFrame
and dispatch requests without being concerned about two requests racing through
this routine and using StartFrame numbers that overlap each other.

This is common routine for both read and write requests.

--*/
{
NTSTATUS status;
WDF_REQUEST_PARAMETERS requestParams;
WDFREQUEST request;
PREQUEST_CONTEXT rwContext;

do {

status = WdfIoQueueRetrieveNextRequest(Queue, &request);

if (!NT_SUCCESS(status)) {
return;
}

rwContext = GetRequestContext(request);

WDF_REQUEST_PARAMETERS_INIT(&requestParams);
WdfRequestGetParameters(request, &requestParams);

if (rwContext->Read) {
PerformIsochTransfer((PDEVICE_CONTEXT)Context,
request,
(ULONG)requestParams.Parameters.Read.Length);
}
else {
PerformIsochTransfer((PDEVICE_CONTEXT)Context,
request,
(ULONG)requestParams.Parameters.Write.Length);

}

} while (status == STATUS_SUCCESS);

return;
}


这个函数很简单,直接得到请求的配置参数,然后调用PerformIsochTransfer,我们再来看这个函数:

VOID
PerformIsochTransfer(
_In_ PDEVICE_CONTEXT DeviceContext,
_In_ WDFREQUEST Request,
_In_ ULONG TotalLength
)
/*++

Routine Description:

Common routine to perform isoch transfer to fullspeed and highspeed device.

Arguments:

Device - Device handle
Queue - Queue the request is delivered from
Request - Read/Write Request received from the user app.
TotalLength - Length of the user buffer.
Request - Read or Write request

Return Value:

VOID
--*/
{
ULONG numberOfPackets;
NTSTATUS status;
PREQUEST_CONTEXT rwContext;
WDFUSBPIPE pipe;
PFILE_CONTEXT fileContext;
WDF_OBJECT_ATTRIBUTES attributes;
ULONG j;
USBD_PIPE_HANDLE usbdPipeHandle;
PMDL requestMdl;
WDFMEMORY urbMemory;
PURB urb;
size_t urbSize;
ULONG offset;
ULONG frameNumber, numberOfFrames;
PPIPE_CONTEXT pipeContext;

rwContext = GetRequestContext(Request);

UsbSamp_DbgPrint(3, ("PerformIsochTransfer %s for Length %d - begins\n",
rwContext->Read ? "Read":"Write", TotalLength));
//
// Get the pipe associate with this request.
//
fileContext = GetFileContext(WdfRequestGetFileObject(Request));
pipe = fileContext->Pipe;
pipeContext = GetPipeContext(pipe);

if ((TotalLength % pipeContext->TransferSizePerFrame) != 0) {
UsbSamp_DbgPrint(1, ("The transfer must evenly start and end on whole frame boundaries.\n"));
UsbSamp_DbgPrint(1, ("Transfer length should be multiples of %d\n", pipeContext->TransferSizePerFrame));
status = STATUS_INVALID_PARAMETER;
goto Exit;
}

if (DeviceContext->IsDeviceSuperSpeed) {

numberOfFrames = TotalLength / pipeContext->TransferSizePerFrame;
numberOfPackets = TotalLength / pipeContext->TransferSizePerMicroframe;

//
// Then make sure the buffer doesn't exceed maximum allowed packets per transfer
//

if (numberOfPackets > MAX_SUPPORTED_PACKETS_FOR_SUPER_SPEED) {
UsbSamp_DbgPrint(1, ("NumberOfPackets %d required to transfer exceeds the limit %d\n",
numberOfPackets, MAX_SUPPORTED_PACKETS_FOR_SUPER_SPEED));
status = STATUS_INVALID_PARAMETER;
goto Exit;
}

UsbSamp_DbgPrint(3, ("Will send %d packets of %d bytes in %d frames\n",
numberOfPackets, pipeContext->TransferSizePerMicroframe, numberOfFrames));

} else if (DeviceContext->IsDeviceHighSpeed) {


numberOfFrames = TotalLength / pipeContext->TransferSizePerFrame;
numberOfPackets = TotalLength / pipeContext->TransferSizePerMicroframe;

//
// Then make sure the buffer doesn't exceed maximum allowed packets per transfer
//

if (numberOfPackets > MAX_SUPPORTED_PACKETS_FOR_HIGH_SPEED) {
UsbSamp_DbgPrint(1, ("NumberOfPackets %d required to transfer exceeds the limit %d\n",
numberOfPackets, MAX_SUPPORTED_PACKETS_FOR_HIGH_SPEED));
status = STATUS_INVALID_PARAMETER;
goto Exit;
}

UsbSamp_DbgPrint(3, ("Will send %d packets of %d bytes in %d frames\n",
numberOfPackets, pipeContext->TransferSizePerMicroframe, numberOfFrames));

}
else {

numberOfPackets = TotalLength / pipeContext->TransferSizePerFrame;
numberOfFrames = numberOfPackets;

//
// Then make sure the buffer doesn't exceed maximum allowed packets per transfer
//

if (numberOfPackets > MAX_SUPPORTED_PACKETS_FOR_FULL_SPEED) {
UsbSamp_DbgPrint(1, ("NumberOfPackets %d required to transfer exceeds the limit %d\n",
numberOfPackets, MAX_SUPPORTED_PACKETS_FOR_FULL_SPEED));
status = STATUS_INVALID_PARAMETER;
goto Exit;
}

UsbSamp_DbgPrint(3, ("Will send %d packets of %d bytes in %d frames\n",
numberOfPackets, pipeContext->TransferSizePerFrame, numberOfFrames));
}

if (rwContext->Read == TRUE) {
status = WdfRequestRetrieveOutputWdmMdl(Request, &requestMdl);
if (!NT_SUCCESS(status)){
UsbSamp_DbgPrint(1, ("WdfRequestRetrieveOutputWdmMdl failed %x\n", status));
goto Exit;
}
}
else {
status = WdfRequestRetrieveInputWdmMdl(Request, &requestMdl);
if (!NT_SUCCESS(status)){
UsbSamp_DbgPrint(1, ("WdfRequestRetrieveInputWdmMdl failed %x\n", status));
goto Exit;
}
}

urbSize = GET_ISO_URB_SIZE(numberOfPackets);

//
// Allocate memory for URB.
//
WDF_OBJECT_ATTRIBUTES_INIT(&attributes);
attributes.ParentObject = Request;

status = WdfUsbTargetDeviceCreateIsochUrb(DeviceContext->WdfUsbTargetDevice,
&attributes,
numberOfPackets,
&urbMemory,
NULL);

if (!NT_SUCCESS(status)) {
UsbSamp_DbgPrint(1, ("WdfUsbTargetDeviceCreateIsochUrb failed 0x%x\n", status));
goto Exit;
}

urb = WdfMemoryGetBuffer(urbMemory, NULL);

usbdPipeHandle = WdfUsbTargetPipeWdmGetPipeHandle(pipe);
urb->UrbIsochronousTransfer.Hdr.Length = (USHORT) urbSize;
urb->UrbIsochronousTransfer.Hdr.Function = URB_FUNCTION_ISOCH_TRANSFER;
urb->UrbIsochronousTransfer.PipeHandle = usbdPipeHandle;

if (rwContext->Read) {
urb->UrbIsochronousTransfer.TransferFlags = USBD_TRANSFER_DIRECTION_IN;
}
else {
urb->UrbIsochronousTransfer.TransferFlags = USBD_TRANSFER_DIRECTION_OUT;
}

urb->UrbIsochronousTransfer.TransferBufferLength = TotalLength;
urb->UrbIsochronousTransfer.TransferBufferMDL = requestMdl;
urb->UrbIsochronousTransfer.NumberOfPackets = numberOfPackets;
urb->UrbIsochronousTransfer.UrbLink = NULL;

//
// Set the offsets for every packet for reads/writes
//
offset = 0;

for (j = 0; j < numberOfPackets; j++) {

if (DeviceContext->IsDeviceHighSpeed ||
DeviceContext->IsDeviceSuperSpeed) {
urb->UrbIsochronousTransfer.IsoPacket[j].Offset = j * pipeContext->TransferSizePerMicroframe;
}
else {
urb->UrbIsochronousTransfer.IsoPacket[j].Offset = j * pipeContext->TransferSizePerFrame;
}

//
// Length is a return value on Isoch IN. It is ignored on Isoch OUT.
//
urb->UrbIsochronousTransfer.IsoPacket[j].Length = 0;
urb->UrbIsochronousTransfer.IsoPacket[j].Status = 0;

UsbSamp_DbgPrint(3, ("IsoPacket[%d].Offset = %X IsoPacket[%d].Length = %X\n",
j, urb->UrbIsochronousTransfer.IsoPacket[j].Offset,
j, urb->UrbIsochronousTransfer.IsoPacket[j].Length));
}

//
// Calculate the StartFrame number:
// When the client driver sets the ASAP flag, it basically guarantees that
// it will make data available to the host controller (HC) and that the
// HC should transfer it in the next transfer frame for the endpoint.
// (The HC maintains a next transfer frame state variable for each endpoint).
// If the data does not get to the HC fast enough, the USBD_ISO_PACKET_DESCRIPTOR -
// Status is USBD_STATUS_BAD_START_FRAME on uhci. On ohci it is 0xC000000E.
//

//urb->UrbIsochronousTransfer.TransferFlags |= USBD_START_ISO_TRANSFER_ASAP;

//
// Instead of using ASAP, we will explicitly set start frame since we cannot control the
// response time of application that's sending request to us.
//
status = WdfUsbTargetDeviceRetrieveCurrentFrameNumber(DeviceContext->WdfUsbTargetDevice,
&frameNumber);
if (!NT_SUCCESS(status)) {
UsbSamp_DbgPrint(1, ("Failed to get frame number urb\n"));
goto Exit;
}

if (frameNumber < pipeContext->NextFrameNumber) {
//
// Controller hasn't finished sending perivously scheduled request. So we will use
// the NextFrameNumber that we calculated for this one.
//
urb->UrbIsochronousTransfer.StartFrame = pipeContext->NextFrameNumber;
}
else {
//
// Controller frame number has advanced beyond the NextFrameNumber we calculated.
//
urb->UrbIsochronousTransfer.StartFrame = frameNumber;
}

//
// Let us add a small latency to account for the time delay in reaching the controller
// from here.
//
urb->UrbIsochronousTransfer.StartFrame += DISPATCH_LATENCY_IN_MS;

//
// Calculate the NextFrameNumber.
//
pipeContext->NextFrameNumber = urb->UrbIsochronousTransfer.StartFrame + numberOfFrames;

//
// Associate the URB with the request.
//
status = WdfUsbTargetPipeFormatRequestForUrb(pipe,
Request,
urbMemory,
NULL );
if (!NT_SUCCESS(status)) {
UsbSamp_DbgPrint(1, ("Failed to format requset for urb\n"));
goto Exit;
}

WdfRequestSetCompletionRoutine(Request,
UsbSamp_EvtIsoRequestCompletionRoutine,
rwContext);

rwContext->UrbMemory = urbMemory;
rwContext->Mdl = requestMdl;
rwContext->Length = TotalLength;
rwContext->Numxfer = 0;
rwContext->VirtualAddress = (ULONG_PTR)MmGetMdlVirtualAddress(requestMdl);

if (WdfRequestSend(Request, WdfUsbTargetPipeGetIoTarget(pipe), WDF_NO_SEND_OPTIONS) == FALSE) {
status = WdfRequestGetStatus(Request);
UsbSamp_DbgPrint(1, ("WdfRequestSend failed with status code 0x%x\n", status));
goto Exit;
}

Exit:

if (!NT_SUCCESS(status)) {
WdfRequestCompleteWithInformation(Request, status, 0);
}

UsbSamp_DbgPrint(3, ("PerformHighSpeedIsochTransfer -- ends status 0x%x\n", status));
return;
}

这个函数很长,但是逻辑并不复杂,前面就是根据不同速度的设备来设置帧,numberOfFrames,numberOfPackets,关于这两个值,跟USB的协议相关,后续在补充,我们接着往下看,对于读请求,我们首先得到输出的MDL,对于写请求,首先得到输入的MDL,分别调用WdfRequestRetrieveOutputWdmMdl和WdfRequestRetrieveInputWdmMdl得到,然后后面需要通过numberOfPackets,创建URB的内存,然后设置URB的参数,最后调用WdfUsbTargetPipeFormatRequestForUrb异步发送URB请求,并设置完成例程:

VOID
UsbSamp_EvtIsoRequestCompletionRoutine(
_In_ WDFREQUEST Request,
_In_ WDFIOTARGET Target,
PWDF_REQUEST_COMPLETION_PARAMS CompletionParams,
_In_ WDFCONTEXT Context
)
/*++

Routine Description:

Completion Routine

Arguments:

Context - Driver supplied context
Target - Target handle
Request - Request handle
Params - request completion params


Return Value:

VOID

--*/
{
PURB urb;
NTSTATUS status;
ULONG length, i, totalPacketLenght;
PREQUEST_CONTEXT rwContext;

UNREFERENCED_PARAMETER(Target);

UsbSamp_DbgPrint(3, ("UsbSampEvtIsoRequestCompletionRoutine - begins\n"));

rwContext = (PREQUEST_CONTEXT)Context;

urb = (PURB) WdfMemoryGetBuffer(rwContext->UrbMemory, NULL);

length = urb->UrbIsochronousTransfer.TransferBufferLength;

status = CompletionParams->IoStatus.Status;

totalPacketLenght = 0;

for (i = 0; i < urb->UrbIsochronousTransfer.NumberOfPackets; i++) {

UsbSamp_DbgPrint(3, ("IsoPacket[%d].Length = %X IsoPacket[%d].Status = %X\n",
i,
urb->UrbIsochronousTransfer.IsoPacket[i].Length,
i,
urb->UrbIsochronousTransfer.IsoPacket[i].Status));

totalPacketLenght += urb->UrbIsochronousTransfer.IsoPacket[i].Length;
}

if (NT_SUCCESS(status) && USBD_SUCCESS(urb->UrbHeader.Status)) {
//
// For iosch out, IsoPacket[].Length field is not updated by the USB stack.
//
if (rwContext->Read == TRUE) {
NT_ASSERT(totalPacketLenght == length);
}

WdfRequestCompleteWithInformation(Request, STATUS_SUCCESS, length);

UsbSamp_DbgPrint(3, ("Request completed with success, TransferBufferLength = %d, TotalPacketLength = %d\n",
length, totalPacketLenght));
}
else {

UsbSamp_DbgPrint(1, ("Read or write irp failed with NTSTATUS 0x%x, USBD_STATUS 0x%x\n",
status, urb->UrbHeader.Status));

WdfRequestCompleteWithInformation(Request, status, 0);
}

UsbSamp_DbgPrint(3, ("UsbSampEvtIsoRequestCompletionRoutine - ends\n"));

return;
}


这个完成例程,没有做什么事情,只是简单的检查下状态值,读到的长度,是否跟需要的长度一致,从而判断返回失败还是成功,从而USB的读写传输基本分析完毕,后续对于停止请求,这个很简单:

VOID
UsbSamp_EvtIoStop(
_In_ WDFQUEUE Queue,
_In_ WDFREQUEST Request,
_In_ ULONG ActionFlags
)
/*++

Routine Description:

This callback is invoked on every inflight request when the device
is suspended or removed. Since our inflight read and write requests
are actually pending in the target device, we will just acknowledge
its presence. Until we acknowledge, complete, or requeue the requests
framework will wait before allowing the device suspend or remove to
proceeed. When the underlying USB stack gets the request to suspend or
remove, it will fail all the pending requests.

Arguments:

Return Value:
None

--*/
{
UNREFERENCED_PARAMETER(Queue);

if (ActionFlags & WdfRequestStopActionSuspend ) {
WdfRequestStopAcknowledge(Request, FALSE); // Don't requeue
}
else if (ActionFlags & WdfRequestStopActionPurge) {
WdfRequestCancelSentRequest(Request);
}

return;
}


至此,我们的这个Windows驱动,USB设备传输的例子分析结束了,因为USB协议的复杂性,后续还要时间来钻研,这样粗略的看一下,是希望自己对于Windows下的USB设备驱动的整个流程有个基本概念,流程,出现问题,从哪里入手等等。目的基本是达到了,后续,我会结合USBVIEW.EXE,重新写一个USB鼠标的驱动,来替代微软的USB鼠标的驱动。