Log in

View Full Version : using HID device in Driver and strange device corruption


Hero
February 14th, 2008, 01:16
Hi all
I don't know that you ever forced to use an another HID device in your own device(your device need to work with another device in HID structure),but i forced to do so.
At first I show you a small part of driver code for this situation:
Code:
PWSTR pInterfaceList = NULL, pInterface = NULL;
WCHAR pPattern[23] = {0,};
UNICODE_STRING uniShortLink;

if (!NT_SUCCESS(RtlStringCbPrintfW(pPattern, sizeof(pPattern), L"hid#vid_%04x&pid_%04x#", HID_VENDORID, HID_PRODUCTID)))
{
status = STATUS_INTERNAL_ERROR;
CompleteIrp(pIrp, status, 0);
return status;
}

status = IoGetDeviceInterfaces(&pDeviceExt->HidGuid, NULL, 0, &pInterfaceList);
if (!NT_SUCCESS(status))
{
CompleteIrp(pIrp, status, 0);
return status;
}

pInterface = pInterfaceList;
while (pInterface[0])
{
if (wcsstr(_wcslwr(pInterface), pPattern))
break;
pInterface += (wcslen(pInterface) + 1);
}

if (!pInterface[0])
{
status = STATUS_DEVICE_NOT_CONNECTED;
CompleteIrp(pIrp, status, 0);
return status;
}

RtlInitUnicodeString(&uniShortLink, pInterface);

status = IoGetDeviceObjectPointer(&uniShortLink, FILE_READ_DATA | FILE_WRITE_DATA,
&pDeviceExt->pDeviceFileObj, &pDeviceExt->pDeviceObj);

ExFreePool(pInterfaceList);

if (!NT_SUCCESS(status))
{
CompleteIrp(pIrp, status, 0);
return status;
}

-------
IoBuildDeviceIoControlRequest(for flushing device synchronously)
IoBuildSynchronousFsdRequest(write to device synchronously)
IoBuildSynchronousFsdRequest(read from device synchronously)
-------

if (pDeviceExt->pDeviceFileObj)
{
ObDereferenceObject(pDeviceExt->pDeviceFileObj);
pDeviceExt->pDeviceFileObj = NULL;
}
pDeviceExt->pDeviceObj = NULL;

As you see,the first part of code,finds proper HID device and gets its DEVICE_OBJECT and FILE_OBJECT by IoGetDeviceObjectPointer.
After that I use IoBuildDeviceIoControlRequest to create a request for flushing HID device buffers and two IoBuildSynchronousFsdRequest calls in order to create write and read requests to/from HID device.
Finnaly i use ObDereferenceObject for releasing objects acquired.

Now this is my problem:
Assume an application that using this code.If that application creates a service of my driver and runs codes that I have shown you,and then stops driver close service handle,there is no problem. I can run program several times without any problem.
But if after running program, I restart computer, After boot up HID device will be corrupted!!!!It means in codes that I had shown you, all operations for opening device,sending device IO control and write to HID is OK,But operation hangs on reading from HID device(or in order hand, synchronous read event never signals and if you use port sniffers,read request never reachs usb device but all other has been reached).It seems read request has been disappeared after eraching system HID device... if you check system's 'Device Manger' in a computer(not VMWare) you will see a warning sign on your HID device that means device is not working properly.

What do you think about this problem?What has been caused it?

Regards

Maximus
February 16th, 2008, 09:05
Try to replace the syncrhonous version of the call with the fsd asynchronous and see if problem persists. Same for IoBuildDeviceIoControlRequest -remove its event usage and use it in async.
Maybe you suffer of a multiple call that 'locks' your device somewhere, somehow.

Well, at least worth a try.

Hero
February 17th, 2008, 00:30
I'm not sure about locks,it because deferent behaviour on deferent OSs.
When I tried several ways on Windows XP SP2 and nothing solved problem,I tried my code on Windows 2003 Server and I get a deferent result.
On this windows,after reboot,I cannot open HID device using IoGetDeviceObjectPointer and when I wanna check device in device manager,I get BSOD.
I think debugging in Windows 2003 Server will give me better results.
In addition,Can I use Driver Studio in Windows 2003 Server, or Windows XP SP2 is the last compatible one?

Regards