locked
HID Keyboard Driver RRS feed

  • Question

  • Hello,

    I'm trying to write USB HID Keyboard driver for a USB controller (it is recognized in system as normal HID Keyboard in Windows). The driver will change keystrokes depending on selected user profile.

    I have read a lot of topics on this forum, and read a lot of documentations... and unfortunately I not sure how to do it.

    So far I was able to do this with kbfiltr sample, that is:
    - I have removed all unnecessary code (left only kbfiltr.c with methods: DriverEntry, KbFilter_EvtDeviceAdd, KbFilter_EvtIoInternalDeviceControl, KbFilter_ServiceCallback, KbFilterRequestCompletionRoutine)
    - I have implemented my code in KbFilter Service Callback routine.

    And everything works (keystrokes are replaced) with PS/2 type keyboard on virtual machine.

    Now I'm trying to make this work with USB HID device but no luck. Device manager shows "This device cannot start. (Code 10)" under my device driver.
    I don't know what to do, to make this sample (kbfiltr) work as a USB HID device.

    I would be very grateful for the help.

    -- 
    Greetings,
    Rysiek

    Sunday, April 2, 2017 6:05 PM

All replies

  • What hardware id and inf did you use to install it on a hid stack? And wrt all the code you removed, I assume it was the raw pdo code. As soon as you need to communicate with user mode for your custom user profile you will need to add that code back

    d -- This posting is provided "AS IS" with no warranties, and confers no rights.

    Sunday, April 2, 2017 6:51 PM
  • Thank you for answer.

    For testing purposes I used normal USB Keyboard (Vendor ID: 0x1C4F, ProductId: 0x0026) and in driver INF I have tryied with:
    %kbfiltr.DeviceDesc% = kbfiltr, USB\VID_1C4F&PID_0026
    %kbfiltr.DeviceDesc% = kbfiltr, HID\Vid_1C4F&Pid_0026
    %kbfiltr.DeviceDesc% = kbfiltr, HID\Vid_1C4F&Pid_0026&... (I don't remeber it but I copied values from Hardware Ids from HID Keyboard driver details) 
    And I used those values with devcon.exe. It was only working (replacing HID Keyboard driver) with USB values and HID with full parameters from driver details, but with error message: "This device cannot start. (Code 10)".

    I removed all the code, because I didn't know what was doing the problem, and I wanted to begin with minimal code.
    BTW. I wanted to use WMI (like in firefly example) to change driver profiles.

    -- 
    Greetings,
    Rysiek

    Monday, April 3, 2017 7:15 AM
  • you should use the hardware ID  HID\Vid_1C4F&Pid_0026, not the USB\.... value. to debug why you are getting a code 10, attach a kernel debugger, set a bp on your EvtDevicePrepareHardware and EvtDeviceD0Entry and see if the breakpoints hit. if they do, it is a failure in your code. if they don't, the INF has not fully configured the device properly. post your full INF

    d -- This posting is provided "AS IS" with no warranties, and confers no rights.

    Monday, April 3, 2017 5:34 PM
  • I have tested with both ID-s (USB and HID).

    I have problem with running driver using Visual Studio. It runs commands on remove (Virutal Box) device, but driver is not installed (it is not running) - look: https://rtprog.pl/~rysiek/winddk/vs_debug.png

    To debugging I'm using DevCon.exe  (https://rtprog.pl/~rysiek/winddk/dev_con_install.png) + DbgView and plugging in USB device after system is started and DbgView is running.
    https://rtprog.pl/~rysiek/winddk/usb_plugin.png

    As you can see in the screenshot. Right driver is loaded (build date is valid) after USB device plugin. 
    "DriverEntry" is executed without error (no error DebugPrint). "KbFilter_EvtDeviceAdd" is executed without error (no error DebugPrint).
    KbFilter_EvtIoInternalDeviceControl method is called with IOCTL_INTERNAL_KEYBOARD_CONNECT IoControlCode, but device manager displays error code and no keystore is logged in KbFilter_ServiceCallback method.

    Every error code is logged  - https://rtprog.pl/~rysiek/winddk/kbfiltr.c, but DbgView doesn't show any error log.

    My inf file is: https://rtprog.pl/~rysiek/winddk/kbfiltr.inx

    -- 
    Greetings,
    Rysiek

    Monday, April 3, 2017 7:05 PM
  • sorry, but I am not going to follow unknown links in a post.

    1) make sure that whatever install you did on the USB\... ID is undone and no longer configured on the system. that will seriously impair any future work you want to do

    2) if you are getting the keyboard connect internal IOCTL, things are progressing pretty far as starting the device.  kbdclass (the driver that is on top of your filter) used to be a sample in the wdk, so you can see what it is doing AFTER sending the keyboard connect IOCTL to see what is failing


    d -- This posting is provided "AS IS" with no warranties, and confers no rights.

    Monday, April 3, 2017 9:17 PM
  • Sorry for unknow links (I cannot attach images because I have unverified account).

    Ad. 1. I have made the test again with new clean Virtual Device - the same result.

    Ad. 2. I have checked IOCTL_INTERNAL_KEYBOARD_CONNECT there is no error code there.

    I don't know what is the second IoControlCode = 0xb3fc3.

    I have added few more debug logs...

    Below is output after plugging in USB device with installed driver using devcon.exe from DbgView.exe:

    UsbSleepStudy_RegisterPdo: New pCsContext DeviceDescription USB Input Device 
    UsbSleepStudy_RegisterPdo: New pCsContext HardwareId USB\VID_09DA&PID_057F&REV_0268&MI_00 
    UsbSleepStudy_RegisterPdo: Exit 0000000000 
    UsbSleepStudy_DeviceD0Entry: Enter 
    UsbSleepStudy_DeviceD0Entry: Handle 0XFFFF8C0B70067690 
    UsbSleepStudy_DeviceD0Entry: Exit STATUS_SUCCESS Context 0XFFFF8C0B70067690 
    UsbSleepStudy_DeviceD0Entry: Exit Handle 0XFFFF8C0B70067690 
    UsbSleepStudy_RegisterPdo: Enter 
    UsbSleepStudy_RegisterPdo: New pCsContext 0XFFFF8C0B6F18BBD0 
    UsbSleepStudy_RegisterPdo: New pCsContext DevicePdo 0XFFFF8C0B6E626060 
    UsbSleepStudy_RegisterPdo: New pCsContext ParentPdo 0XFFFF8C0B6E481060 
    UsbSleepStudy_GetDeviceStringProperty: Enter 
    UsbSleepStudy_GetDeviceStringProperty: Device 0XFFFF8C0B6E626060 
    UsbSleepStudy_GetDeviceStringProperty: DeviceProperty 0000000000 
    UsbSleepStudy_GetDeviceStringProperty: PropertyString USB Input Device 
    UsbSleepStudy_GetDeviceStringProperty: Exit 
    UsbSleepStudy_GetDeviceStringProperty: Enter 
    UsbSleepStudy_GetDeviceStringProperty: Device 0XFFFF8C0B6E626060 
    UsbSleepStudy_GetDeviceStringProperty: DeviceProperty 0x00000001 
    UsbSleepStudy_GetDeviceStringProperty: PropertyString USB\VID_09DA&PID_057F&REV_0268&MI_01 
    UsbSleepStudy_GetDeviceStringProperty: Exit 
    UsbSleepStudy_RegisterPdo: New pCsContext DeviceDescription USB Input Device 
    UsbSleepStudy_RegisterPdo: New pCsContext HardwareId USB\VID_09DA&PID_057F&REV_0268&MI_01 
    UsbSleepStudy_RegisterPdo: Exit 0000000000 
    UsbSleepStudy_DeviceD0Entry: Enter 
    UsbSleepStudy_DeviceD0Entry: Handle 0XFFFF8C0B6F18BBD0 
    UsbSleepStudy_DeviceD0Entry: Exit STATUS_SUCCESS Context 0XFFFF8C0B6F18BBD0 
    UsbSleepStudy_DeviceD0Entry: Exit Handle 0XFFFF8C0B6F18BBD0 
    Keyboard Filter Driver Sample - Driver Framework Edition.
    Built Apr  3 2017 21:49:42
    Enter FilterEvtDeviceAdd 
    Entered KbFilter_EvtIoInternalDeviceControl: 0xb0203
    KbFilter_EvtIoInternalDeviceControl: IOCTL_INTERNAL_KEYBOARD_CONNECT
    KbFilter_EvtIoInternalDeviceControl: IOCTL_INTERNAL_KEYBOARD_CONNECT Complete
    KbFilter_EvtIoInternalDeviceControl: forwardWithCompletionRoutine = FALSE
    Entered KbFilter_EvtIoInternalDeviceControl: 0xb3fc3
    KbFilter_EvtIoInternalDeviceControl: forwardWithCompletionRoutine = FALSE
    

    IoControlCode IOCTL_INTERNAL_KEYBOARD_CONNECT is completed without errors.

    And command are processed with

    WDF_REQUEST_SEND_OPTIONS_INIT(&options, WDF_REQUEST_SEND_OPTION_SEND_AND_FORGET);
    
            ret = WdfRequestSend(Request, WdfDeviceGetIoTarget(hDevice), &options);

    Without errors.

    My inf file has only changed (from the original sample file) section with device ID:

    [Standard.NT$ARCH$]
    %kbfiltr.DeviceDesc% = kbfiltr, HID\VID_09DA&PID_057F&MI_00&Col01
    

    -- 
    Greetins,
    Rysiek

    Monday, April 3, 2017 10:09 PM
  • Thanks for help. I managed to write it with a different approach.

    Wednesday, April 5, 2017 8:50 PM
  • and to help others, what other approach did you use?

    d -- This posting is provided "AS IS" with no warranties, and confers no rights.

    Thursday, April 6, 2017 4:40 AM
  • I started from Firefly sample, which is a USB HID Mouse driver. Changed INX file:
    - device ID to my Vendor ID and Product ID,
    - changed Mouse to Keyboard settings.
    This give me working HID Keyboard Upper Filter as wanted. Then I added to code from Kbfiltr to attach to KbFilter_ServiceCallback

    -- 
    Greetings,
    Rysiek

    Thursday, April 6, 2017 6:05 AM