Tech Support > Microsoft Windows > Drivers > Newbie: need to develop a filter driver for a USB device ?
Newbie: need to develop a filter driver for a USB device ?
Posted by Nicola Marinelli on June 14th, 2006


Hi,

I'm an experienced Windows programmer, but I'm absolutely new to Windows
driver development.
My problem is that I have a USB device and an application (not coded by me)
that communicate by means of a specific driver (not coded by me) that maps
the device (or the usb port the device is attached to) on a virtual COM
port: I need to intercept the data that the device sends to the application
and change it slightly (leaving the device, the original driver and the
application untouched).
I started to read the DDK's documentation, and now I have the following
questions ...

Do I really need a driver ? [I would answer YES]
Do I really need a kernel mode driver ? [I would answer YES]
Do I need a filer driver ? [I would answer YES]
Upper or lower ? [I really don't know]
On which 'stack': USB or serial ? [I would say USB, because the COM port is
a 'virtual' one, but I'm absolutely not sure].

Thanks, Nicola Marinelli.


Posted by Maxim S. Shatskih on June 15th, 2006


Yes.

Yes.

Yes.

Upper. Upper filter will see the serial IOCTLs and the serial data, and the
lower filter will only see the URBs.

Virtual serial stack on USB, with USBHUB's PDO in the bottom.

Serial stack is only for real standard PC serial UART hardware.

--
Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
maxim@storagecraft.com
http://www.storagecraft.com


Posted by Doron Holan [MS] on June 15th, 2006


furthermore, you should write your filter driver using KMDF, otherwise you
will spend alot of time writing code that is boilerplate

see http://www.microsoft.com/whdc/driver/wdf/KMDF_pkg.mspx

d

--
Please do not send e-mail directly to this alias. this alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.


"Maxim S. Shatskih" <maxim@storagecraft.com> wrote in message
news:uS2wp2AkGHA.1272@TK2MSFTNGP03.phx.gbl...


Posted by Nicola Marinelli on July 16th, 2006


So at the end I started to try to develop my first driver, using KMDF: I
found a sample filter driver 's code, I built it, I managed to install it
(even if I don't know well how) as an upper filter of a virtual COM port
(that actually is an USB device) and I tested that it works, that is to say,
does nothing. So far so good.
This sample driver has a single queue, for which it defines callbacks for
read, write, device control and internal device control requests. I'm only
interested in filtering read data, so the default behaviour is ok for the
other request types.
Since I need to modify the read data I changed only the read request
completion callback in order to retrieve the output buffer address and do
the job before calling 'WdfRequestComplete':

[...]

NTSTATUS status;
PVOID Buffer;
size_t Length;

status = WdfRequestRetrieveOutputBuffer(Request,
0,
&Buffer,
&Length);

if (NT_SUCCESS(status))
{
// do the job on Buffer
}

WdfRequestComplete(Request, Params->IoStatus.Status);


Now, 'WdfRequestRetrieveOutputBuffer' returns without errors and gives me a
non NULL Buffer pointer, but if I try to access the buffer I ger a blue
scrren that says PAGE_FAULT_IN_NONPAGED_AREA !
The sample driver I'm upgrading doesn't make any call to
'WdfDeviceInitSetIoType', so I'm assuming that it's using the
'WdfDeviceIoBuffered' access method, so I think I should be able to directly
use the address returned by 'WdfRequestRetrieveOutputBuffer'.
Is my buffer access method being overwritten with an other one ? Am I
missing something ?

Ciao, Nicola.


Posted by Doron Holan [MS] on July 16th, 2006


please post the code which operates on Buffer

d
--
Please do not send e-mail directly to this alias. this alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.


"Nicola Marinelli" <nicola.marinelli@libero.TOGLIMI.it> wrote in message
news:nxxug.42685$zy5.862898@twister1.libero.it...


Posted by Nicola Marinelli on July 16th, 2006


Ehm ... ... ... there was a bug in that piece of code and now that I fixed
it the driver works !
Sorry, but since it's my very first driver I was convinced I made something
wrong with the 'driver stuff' ... :-)

If you are curious, this is the bugged code (quite shameful, but it is just
a test):

NTSTATUS status;

PVOID Buffer;
size_t Length;

status = WdfRequestRetrieveOutputBuffer(Request, 0, &Buffer, &Length);

if (NT_SUCCESS(status))

{
unsigned int n;

char * p = (char *) Buffer;

for (n = 0; n < Length - 9; n++) // BUG: if Length < 9 ...

{
if ((p[0] == '1') &&
(p[1] == '2') &&
(p[2] == '3') &&
(p[3] == '4') &&
(p[4] == '5') &&
(p[5] == '6') &&
(p[6] == '7) &&
(p[7] == '8') &&
(p[8] == '9'))

{
p[0] = 'a';
p[1] = 'b';
p[2] = 'c';
p[3] = 'd';
p[4] = 'e';
p[5] = 'f';
p[6] = 'g';
p[7] = 'h';
p[8] = 'i';
}

p += 1;
}
}


Thanks a lot !

Ciao, Nicola.