Writing device driver? - windows

I wonder if I understand correctly...
Say, if I want to control how my mouse work, i.e Left Button open window, Right Button send keystroke 'A' etc.
But I am not talking about writting something like follows in an application:
void MouseDown(xxxxEventArgs e, sender object)
{
}
I want to completely controls how the device work, then I will need to write a driver for it? From what I learn in assembly before, controlling a device I should need to know their port to communicate with the device. But say if I buy a Logitech mouse, is it possible to write a mouse driver myself to use it?
Because I saw some project that they buy a usb web cam from store, and they could able to control the web came to rotate, recevie the image from the web cam, I wonder if that's because the web cam has API provided them?
Thanks in advance.

If you want to control the device in it's entirety, then you need to write a device driver indeed. This is a non-trivial task and you should read up on it. There is a tutorial on it here and there a book for windows driver development here.
If you want to write device drivers, you should be very well versed with C and/or C++.

You do not need to write a device driver for what you are trying to do.The device driver has nothing but as per the data sheet of the device address of registers where it can read,write,do IOMMU etc or some other stuff.What you will need is some kind of hacking the application programming part of the thing which you are trying to achieve.
Because device driver code just reads the data from device and writes back it is the application which is concerned for it.Though in some case device driver programmer provide a method (function) to application programmer so that they can write their application and invoke those methods.In your case you need to just understand how the application code is talking to device driver.
In case you want to write a device driver check this
http://www.freesoftwaremagazine.com/articles/drivers_linux?page=0%2C0

In such kind of cases you can proceed with writing your own device driver by C++ and assemb
lyem

Related

Can I use WinUSB alongside the built-in HID drivers?

I am prototyping a keyboard using a Pi Zero, and I plan to set the Pi Zero up so that it emulates an HID-compliant USB keyboard (for Windows). There are many guides on how to do this, so I do not think this will be an issue. The additional functionality of this keyboard is going to require the ability to configure certain aspects keyboard on the host machine and send those configurations back up to the device.
My understanding is that once I have the pi zero emulating an HID keyboard, I will not have to do any extra work with Windows to get the host to accept the new keyboard device - it will automatically recognize the HID device and use the correct built-in driver. The configuration bit, however, I will have to work on myself. I was planning on writing a Win32 application that calls WinUSB as the other driver that handles transfer for the configurations.
On the MSDN page for selecting a USB driver model, it states that WinUSB is a good option if:
Your device is accessed by a single application.
The question stands thusly:
Does having my device configured as an HID keyboard prohibit me from being able to use WinUSB as a configuration driver? More specifically, does having my keyboard constantly open in an HID filter driver (I believe the HID host is a filter driver) count as the device being used in one application already, where the configuration application would be a second?
I believe this answers my question.
https://social.msdn.microsoft.com/Forums/en-US/9687e8ba-9eb0-4d41-a8ac-973a029e05b2/winusb-sample-to-read-inputs-from-keyboard
only one driver can be installed on the device, either HID or winusb. you could force winusb onto the keyboard but then it would stop looking like a keyboard to the OS.
Ignoring the irking lack of capitalization in this post, this seems to state pretty conclusively that a separate driver needs to be written. How unfortunate.

How to use gamepad by STM32?

I'm a very beginner of using STM32 and now I'm trying to move a robot by using STM32F4.
Now I can move a robot by controlling a device such as motor, encoder and other devices.
However, I don't have any idea about reading gamepad (HID device of USB).
I'm trying this because I want to control a robot by gamepad.
Is it possible to read gamepad which use USB by STM32F4?
If there are a page which write about this problem, I'm afraid you tell me the URL.
Thank you.
It is possible if your uC has the USB interface which can be configured as a host device. Check with your micro DS. but you have to remember - it is not an easy task, you will have to learn USB deeply, and implement the host stack.

Gamepad and joystick support on Mac OS X in user space

I have been searching through how to do gamepad and joystick support on Mac for some days and all resources that I found seems to suggest a pre-installed driver along with using Apple's HID API, which works.
The drawback about this approach is that each joystick and gamepad will require another kernel extension to be loaded, so it can be recognized by HID manager, or at least a code less Info.plist saying it conforms to the earlier installed driver. For instance, when I have an 360 Xbox driver KEXT in house, the Xbox controller from Microsoft will work, but not the Logitech one (I tried F710).
As Apple suggests the application that uses a gamepad or joystick should be able to do themselves at user space without introducing any KEXT stuff. Is there a way to do it?
The thing I had in mind was something like using IORegistry or IOUSB API to get the device when they get plugged in (USB Prober shows it at least). Then somehow get the description of the device, then use that description to register the device as a HID one. Then the whole HID manager can be used.
Am I on the right track? Or is there any other way to do this?
Since IOKit API actually provided keywords like kHIDUsage_GD_Joystick, and there's an ForceFeedback.h library, I suppose Apple designed their HID API with joystick and force feedback in mind. That's the slim hope I had that this might work.
Some reference documentation and open source project:
Colin Munro's 360 driver
HID API Documents
DDHID Project
After revisiting this, I found out the solution to be operating directly on the file descriptors of the device. libusb is an excellent library which greatly simplify your life on this and they have Mac supported.
xboxdrv link is a great example on how to operate on file socket using libusb.
In pseudo code, it should look like this:
enumerate device
detect kernel driver and detach it if possible
open device
file off a initial transfer
wait on the callback function to handle msg and error properly
start run loop or select on fd to call libusb_event_handle
Check libusb for more info link.

Replace Windows USB Class Driver with a custom driver?

I wonder if anyone can help at all, a bit of a specialist problem this.
I have an application that needs to read and analyse a number of USB devices (not simultaneously, they are each run in seperate tests and could in theory be run on different machines).
Each of the USB devices is based on the USB HID class, and are manufactured by different companies, none of these USB devices are designed to be run on PC, but are meant for a different platform, however for the purposes of testing the devices the client has requested that the test application is run from a PC.
Some of the devices will start up, be recognised by windows which will initialise and start them correctly using the generic HID class driver built into windows, the devices will then start sending correct data packets of the data to be tested.
Some of the devices will start up, be recognised by windows which will try to start them but fail to fully to initialise them leaving them in a half initialised state. This is fine, as I can use my beagle protocol analyser to capture the initialisation packets from the genuine platform and then use the LibUSBDotNet library to replicate the remaining packets in the initialisation sequence and get them to start sending the packets correctly.
The problem I have is with one particular device (though there are some more I haven't tested yet so it's quite possible one of those may also exhibit the same problem). The issue is the the Windows HID class driver recognises the device and trys to initialise and start it, this works after a fashion and the device starts sending data.
The problem is that the data being sent is different to that which is sent to the genuine platform (containing only a subset of the full data). It's as though windows has initialised the device into a different mode.
When I capture the initialisation packets from both the PC and the genuine platform using my USB protocol analyser I see that Windows is sending some slightly different initialisation packets. Using LibUSBDotNet to resend the correct packets once Windows has already started the device seems to have no effect.
My problem is that I need to stop windows from trying to initialise the device using the standard HID class driver, I've tried removing the driver in Device Manager but it still initialises it (and the driver is magically reassigned in device manager). I've done some investigation and there are possible alternatives:
Create a specific driver which windows will assign to the particular VID/PID of the device but that does nothing, then I can use LibUSBDotNet to send the correct initialisation sequence to the device from within my own code.
Use something like WinUSB to create a proper driver for the device (or possibly to create a "dead" driver like 1.
Will a driver with a specific VID/PID defined be used by windows in preference to it's inbuilt USB HID class driver? If not then I would be wasting my time going down this route?
Note, my mac initialises the problem device correctly, and I've asked the question of the client whether the application can be developed for Mac and their answer was frustrating Windows only.
I've no experience in writing proper Windows drivers, though I have experience in talking to USB at a relatively low level (so that part doesn't worry too much). Can anyone suggest a good course of action (before I potentially waste weeks investigating how to write drivers for the PC only to find my selected course of action can't deliver what I required).
Any help or suggest much appreciated.
Thanks,
Rich
Added after trying suggestions below:
I tried using the LibUsbDotNet inf wizard to create the necessary files and install them and this appeared to work - certainly the device was now appearing in Device Manager as a libusb-win32 device - not HID device and the associated driver was libusb driver. Even after doing this the device still seems to become initialised and start sending the wrong type of data packets although now those packets are no longer handled by the class driver and are just lost.
I also came across Zadig which has a similar inf creation wizard for WinUSB and this had exactly the same result.
A colleague has suggested that it might not be windows itself that is switching the device into this mode, rather the device identifying that it is connected to a windows machine and switching itself into this mode. I suspect this is the case, in which case I am stuck - time to have another conversation with the client.
Many thanks for the help.
You're using libusb-win32 as a filter driver; that is, the HidUsb device driver is assigned and loaded for your device, but then the libusb-win32 driver is loaded on top and gives you unobstructed access to the hardware.
If you don't want a HidUsb (or any other class driver) to perform any communication "on your behalf", simply associate libusb-win32 as a device driver with your hardware. For this, you'd have to create an .INF file associating it with the VID/PID/Revision of each USB device. If I recall correctly, libusb-win32 even comes with a utility to generate such .INF files.
If you install this .INF file e.g. with PnpUtil.exe (available on Vista or higher), you might still run into issues where, although you're a better match than the generic HID driver, the HID driver is still selected.
The generic HID driver matches devices by their Compatible IDs (i.e. by a USB interface class) while you'd be matching by Hardware IDs (which have higher priority). However, Windows might give priority to other aspects, such as your driver being unsigned. Read: How Windows Selects Drivers
Luckily, even in that scenario, signing drivers with a self-generated certificate (use CertUtil.exe, MakeCat.exe and SignTool.exe) is not too difficult.

Windows driver creation

I would like to try creating a custom driver for a trackpad of mine. Could you please recommend an opensource driver to start with? My search turned out nothing, so I hope there actually any skeleton code to begin building from.
Is it a USB HID device? If so, it should be pretty easy to read data from the device.
Check out this code (C#) for example.
There is also the UMDF (User Mode Driver Framework) that supposedly allows writing drivers for USB-like devices in user mode. I have never used it though. There are a couple of sample drivers to download.

Resources