Using HID device in driver and HID device corruption

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:

WCHAR pPattern[23] = {0,};  
UNICODE_STRING uniShortLink;  
  
if (!NT_SUCCESS(RtlStringCbPrintfW(pPattern, sizeof(pPattern), L"hid#vid_%4x&pid_%4x#", 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… :confused: 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

sry… i cannot edit this post to correct 2 small error in typings.Here is corrected code,but problem consists:
1-
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;
}
2-
while (pInterface[0])
{
if (wcsstr(_wcslwr(pInterface), pPattern))
break;
pInterface += (wcslen(pInterface) + 1);
}

Regards

You are not detecting your device correctly. The interface string is opaque, you should not be hunting for your vid/pid in there. Rather, you can open each instance and ask it for the vid/pid by sending IOCTL_HID_GET_COLLECTION_INFORMATION with an output buffer of HID_COLLECTION_INFORMATION.

Once the irp gets stuck, break in and run !irp and see who has pended it. What you really need to do is attach a kernel debugger and step through your code and see what is going wrong.

d

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@yahoo.com
Sent: Wednesday, February 13, 2008 11:43 PM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] Using HID device in driver and HID device corruption

sry… i cannot edit this post to correct 2 small error in typings.Here is corrected code,but problem consists:
1-
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;
}
2-
while (pInterface[0])
{
if (wcsstr(_wcslwr(pInterface), pPattern))
break;
pInterface += (wcslen(pInterface) + 1);
}

Regards


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

hi
First,I know it is not a correct way to do this way of detection, but for know it works well for my tests because I noticed that vid,pid seems to be correct always. but i will probably change it later(I don’t know if sending IOCTL_HID_GET_COLLECTION_INFORMATION do additional configurations or only get info,but I assume it is only for getting info and don’t do any configuration to HID device)
I using 2 sniffer to help me for finding problem:
1-WdmSniffer that logs IRP that reachs HID device.
2-A USB sniffer that logs commands that has reached to USB.
I should say when I see this logs when problem happens, every IRP_MJ_DEVICE_CONTROL and IRP_MJ_WRITE IRP packets and their responses are ok in WdmSniffer and I can see the coresponding requests on USB by USB sniffer.But when I recieve IRP_MJ_READ on WdmSniffer, i get no respose for it and when I check USB sniffer,the request has not reached to it.
I used your suggestion and checked this IRP_MJ_READ structure by kernel debugger,but it is strange because nobody has pended it(PendingReturened==false).
I assume it means that HID driver cannot forward this to lower device driver for USB device.

Regards

After more debugging,I noticed that this driver has 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 wana 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