KMDF Mouse driver

Hello,

I am a beginner in kmdf driver development.
I would like to just try creating a simple driver to read data from my mouse ( I don’t want the mouse to work, just get the data sent on the USB ).

I used the “kmdf_enumswitches” kmdf template as it is supposed to read continous data on the USB (?) but the function “WdfUsbTargetDeviceCreateWithParameters” returns “STATUS_INVALID_DEVICE_REQUEST”

My guess is that the system knows it is a HID mouse and don’t let me open an USB handle to it.

Maybe what I am trying to do is not doable.

In this case, I thought about using an Arduino Due to send data to the driver, would that be an easier approach?

Thanks in advance

Do you want to see the raw usb data or the cooked mouse data? For cooked mouse data you can start with the moufiltr example. For the raw usb data you need to filter a lower part of the stack. Probably the easiest choice is to start with the toaster filter example and install it as a device lower filter below hidusb.sys. You will not send any requests on your own, rather you should filter the URBs sent by hidusb and in the completion routine you can see the data read from the device. BUT… what are you trying to learn? There may be easier paths to follow if you can state your goals.

Thanks for you answer,

I just want to demonstrate for a studies project how to retrieve data from a hardware using a windows driver.
I had in mind not to build a filter driver (as it would mean I just get the data from an already working windows driver that I didn’t code myself?), and rather be the first part of the stack: just the hardware and my driver, but maybe it is not possible?

I would like just the raw usb data (without even taking into account that it is a hid device).

Force install your driver and replace hidusb. You will have complete control over the io to the device.

I think you’re unclear about the architecture of USB. The only component that talks directly to the hardware is the Host Controller Driver. All other drivers in the USB stack pass around URBs (USB Request Blocks) as normal IRPs. Because of that, you might as well use a filter driver examine the URBs that get passed around during normal mouse operation.

It is possible for you to replace the HID driver with your own custom driver, or even with WinUSB to allow you to drive it from user mode. However, you will need to read and understand the USB Specification and the USB HID Specification in order to understand the protocols you need to follow.

Replacing the microsoft usb drivers with my own custom driver would definitely work, but I think it’s a tough task for a beginner.

I read some more microsoft docs and things are starting to get clearer.

So let’s say I have a hardware HID device, that has extra features that I need to handle. If I am right, what I should do is build a function driver that binds to the HID class Windows driver?

Outside of keyboard and mouse, you can directly access HID devices from user mode without an additional driver. You can write a function driver for the hid function, but it is not the simplest path.

So let’s say I have a hardware HID device, that has extra features that I need to handle. If I am right, what I should do is build a function driver that binds to the HID class Windows driver?

HID is not a particularly good example, because the operating system already has full driver support. No kernel code is required. Keyboards and mice don’t have “extra functions”; they follow the spec, and the system can handle the spec. For devices other than keyboards and mice, the user mode HID library can do anything you need to do.

Non-HID USB devices are fun to play with. There are dozens and dozens of “USB experimenter kits” that you can play with.