Overlapped I/O in WDF

Hi,

What are the necessary steps to make overlapped I/O work in WDF?
I tried to implement overlapped I/O, but to no success.

At the user side, I use the following code:

OVERLAPPED overlapped;
HANDLE hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
overlapped.Offset = 0;
overlapped.OffsetHigh = 0;
overlapped.hEvent = hEvent;
ret = DeviceIoControl(hDevice,dwIoControlCode,lpInBuffer,inBufSize,
lpOutBuffer,outBufSize,(LPDWORD)&bytesReturned,(LPOVERLAPPED)&overlapped);
if (!ret) {
DWORD dwError = GetLastError();
switch (dwError) {
case ERROR_IO_PENDING:
switch (WaitForSingleObject(hEvent, (timeout == 0) ? INFINITE : timeout))
{
case WAIT_TIMEOUT:
/* time out occurred */
CancelIo(hDevice);
break;
case WAIT_OBJECT_0:
//success
break;
case WAIT_ABANDONED:
break;

default:
{
// UNSPECIFIED ERROR
break;
}
}
}
}
CloseHandle(hEvent);

At the driver side, in the IoDeviceControl, I just return (no WdfRequestComplete - this to simulate a never interrupting device).
I don’t mark requests as cancellable, I don’t requeue the request.
There is one device synchronized queue.

The above code, hangs while calling CancelIo.

I am sure I am missing something essential, like pending the request, requeueing the request, …
Can anyone point me in the right direction?

Thanks

If your driver does not requeue the request it is not going to get
cancelled for you, you have to register a cancel handler by calling
WdfRequestMarkCancelable.

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of
xxxxx@barco.com
Sent: Tuesday, September 18, 2007 7:35 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] Overlapped I/O in WDF

Hi,

What are the necessary steps to make overlapped I/O work in WDF?
I tried to implement overlapped I/O, but to no success.

At the user side, I use the following code:

OVERLAPPED overlapped;
HANDLE hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
overlapped.Offset = 0;
overlapped.OffsetHigh = 0;
overlapped.hEvent = hEvent;
ret = DeviceIoControl(hDevice,dwIoControlCode,lpInBuffer,inBufSize,
lpOutBuffer,outBufSize,(LPDWORD)&bytesReturned,(LPOVERLAPPED)&overlapped
);
if (!ret) {
DWORD dwError = GetLastError();
switch (dwError) {
case ERROR_IO_PENDING:
switch (WaitForSingleObject(hEvent, (timeout == 0) ? INFINITE :
timeout))
{
case WAIT_TIMEOUT:
/* time out occurred */
CancelIo(hDevice);
break;
case WAIT_OBJECT_0:
//success
break;
case WAIT_ABANDONED:
break;

default:
{
// UNSPECIFIED ERROR
break;
}
}
}
}
CloseHandle(hEvent);

At the driver side, in the IoDeviceControl, I just return (no
WdfRequestComplete - this to simulate a never interrupting device).
I don’t mark requests as cancellable, I don’t requeue the request.
There is one device synchronized queue.

The above code, hangs while calling CancelIo.

I am sure I am missing something essential, like pending the request,
requeueing the request, …
Can anyone point me in the right direction?

Thanks


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

Kurt Pattyn wrote:

At the driver side, in the IoDeviceControl, I just return (no
WdfRequestComplete - this to simulate a never interrupting
device). I don’t mark requests as cancellable, I don’t requeue
the request.

Mark said to add cancel support in your driver, but I think it would be easier for you to move the request that you’re not completing in your dispatch routine into another (manual) queue. Then you get cancellation support for free.

xxxxx@gmail.com wrote:

Kurt Pattyn wrote:

> At the driver side, in the IoDeviceControl, I just return (no
> WdfRequestComplete - this to simulate a never interrupting
> device). I don’t mark requests as cancellable, I don’t requeue
> the request.
>

Mark said to add cancel support in your driver, but I think it would be easier for you to move the request that you’re not completing in your dispatch routine into another (manual) queue. Then you get cancellation support for free.

This is excellent advice. One of the key lessons that I’ve taken away
from my KMDF experience is “don’t be afraid to use queues”. Queues are
lightweight and low impact, and there’s nothing wrong with creating a
manual queue that never holds more than one request.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

He could do that as well. The OP seemed to want a simulation of a single
threaded and very slow hardware device. A limited to one element queue
seems a bit pointless, and canceling an operation ‘on the hardware’
usually requires driver intervention to correctly process the cancel
operation.

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of
xxxxx@gmail.com
Sent: Tuesday, September 18, 2007 12:31 PM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] Overlapped I/O in WDF

Kurt Pattyn wrote:

At the driver side, in the IoDeviceControl, I just return (no
WdfRequestComplete - this to simulate a never interrupting
device). I don’t mark requests as cancellable, I don’t requeue
the request.

Mark said to add cancel support in your driver, but I think it would be
easier for you to move the request that you’re not completing in your
dispatch routine into another (manual) queue. Then you get cancellation
support for free.


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

Thanks Mark,

WdfRequestMarkCancelable solved the thing. And indeed, I wanted to simulate a single threaded, very slow hardware device.
I know that for Vista, all drivers must implement cancellation logic.
So my next question is:
Which is the best approach: a cancel routine or a manual queue?
It is so, that the operation has to be canceled on the hardware as well, so driver intervention is required as well.

Kurt

So you need an explicit notification of cancel - and
WdfRequestMarkCancelable would be the appropriate way to do this.

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of
xxxxx@barco.com
Sent: Wednesday, September 19, 2007 3:57 AM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] Overlapped I/O in WDF

Thanks Mark,

WdfRequestMarkCancelable solved the thing. And indeed, I wanted to
simulate a single threaded, very slow hardware device.
I know that for Vista, all drivers must implement cancellation logic.
So my next question is:
Which is the best approach: a cancel routine or a manual queue?
It is so, that the operation has to be canceled on the hardware as well,
so driver intervention is required as well.

Kurt


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

Not to disagree, but a manual queue provides the same function via EvtIoCanceledOnQueue, and has the added benefits of (a) providing a place you can find requests when your hardware actually does complete the operation, (b) management of the races between cancellation and your completion processing, and (c) the queue’s context can be handy place to keep state for your own purposes (although you could also add an object to the request).

These may not be significant in this particular case, but they are worth considering in general solution design terms. This is one of several cases where the framework provides multiple ways to solve a problem.

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of Roddy, Mark
Sent: Wednesday, September 19, 2007 6:29 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] Overlapped I/O in WDF

So you need an explicit notification of cancel - and
WdfRequestMarkCancelable would be the appropriate way to do this.

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of
xxxxx@barco.com
Sent: Wednesday, September 19, 2007 3:57 AM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] Overlapped I/O in WDF

Thanks Mark,

WdfRequestMarkCancelable solved the thing. And indeed, I wanted to
simulate a single threaded, very slow hardware device.
I know that for Vista, all drivers must implement cancellation logic.
So my next question is:
Which is the best approach: a cancel routine or a manual queue?
It is so, that the operation has to be canceled on the hardware as well,
so driver intervention is required as well.

Kurt