13
votes

I read from LDD3 chapter 14 about hotplug drivers.I need to write a usb mouse driver which load when I plug the hardware. Now, doing some experiment I come to know that there is a driver named "hid-generic" which is called when plug-unplug.

[ 6654.232046] usb 3-1: new low-speed USB device number 3 using uhci_hcd
[ 6654.462061] usb 3-1: New USB device found, idVendor=093a, idProduct=2510
[ 6654.462067] usb 3-1: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[ 6654.462071] usb 3-1: Product: USB OPTICAL MOUSE
[ 6654.462074] usb 3-1: Manufacturer: PIXART
[ 6654.489316] input: PIXART USB OPTICAL MOUSE as /devices/pci0000:00/0000:00:1d.1/usb3/3-1/3-1:1.0/input/input12
[ 6654.489445] hid-generic 0003:093A:2510.0004: input,hidraw0: USB HID v1.10 Mouse [PIXART USB OPTICAL MOUSE] on usb-0000:00:1d.1-1/input0

Also lsmod shows,

Module                  Size  Used by
hid_generic            12541  0 
usbhid                 47259  0 
hid                   105241  2 hid_generic,usbhid
psmouse               102541  0 

My doubts are as follows,

1) To make my module load (hotplug) when this mouse plugs in, I have to disable these 3 drivers in kernel and build whole kernel with my driver with vendor and device ID in id_table. Right?

2) I also read about USB core drivers and USB device drivers. So these HID drivers are core drivers or device drivers?

3) Which are core drivers and device driver in case of USB mouse? And where can I find them in kernel source?

Thanks, Sunil.

2
psmouse is the device driver responsible for mouse events detection on Linux, if you disable it that is $ sudo rmmod psmouse, then you will not be able to detect mouse events on you PCSantosh A
@SantoshA psmouse handles PS/2 mice. The output above shows that hid-generic is used.CL.
@CL, Remove hid-generic module using rmmod and test whether the mouse is working or not for confirmationSantosh A
@SantoshA - removing only hid_generic disables mouse from working.Sunil Shahu

2 Answers

20
votes

I'll try to answer your questions one by one :

1) To make my module load (hotplug) when this mouse plugs in, I have to disable these 3 drivers in kernel and build whole kernel with my driver with vendor and device ID in id_table. Right?

Yes, but there are some additional things you need to take care of. First understand how a particular module(driver) gets loaded. The key to this is MODULE_DEVICE_TABLE(usb, &my_id_table); Whenever a particular module is "installed" (using make modules_install), an entry, according to the id table passed in MODULE_DEVICE_TABLE gets created in /lib/modules/<your_kernel>/modules.usbmap and /lib/modules/<your_kernel>/modules.dep file(search for the string "usbhid" in the files). Whenever a new usb device is detected, the kernel reads these files to find the matching parameters. If it is found, the following module is loaded from the corresponding path found in /lib/modules/<your_kernel>/modules.dep which holds the info. about the path where the driver is located and also its dependencies.

So, now even if you unload(rmmod) usbhid from the kernel, it will be loaded again when you re-insert your mouse. To avoid this from happening you need to modify those files, i.e. remove the entries from the files. To do so, "move" the usbhid driver from its original path(generally located at /lib/modules/<your_kernel>/kernel/drivers/hid/usbhid/usbhid.ko to a safe place. Now rebuild the dependencies such that the entries would be removed from the dependency files.

Now you need to create entries of your driver. Just install your driver and you are good to go!

So, to summarize :

$ sudo rmmod usbhid                                      # Unload the usb mouse driver
$ cd /lib/modules/$(uname -r)/                           # Move to your current kernel
$ vim modules.usbmap                                     # Check for the "usbhid" string
$ vim modules.dep                                        # Check for "usbhid.ko:" string
$ sudo mv kernel/drivers/hid/usbhid/usbhid.ko ~/Desktop  # Take backup of your current 
                                                           usb mouse driver
$ sudo depmod -a                                         # Rebuild the dependency files

Now check the dependency files for the string "usbhid" again. It shouldn't be there!

$ cd /path/to/your/driver
$ sudo make modules_install  # Install your driver into /lib/modules/$(uname -r)/extra
$ sudo depmod -a             # Rebuild the dependency files

After this step, search for the string corresponding to your module in the dependency files, and it should be there! From this moment on, whenever you insert the mouse(or from boot itself) your driver will be loaded, instead of the original.

Once your are done playing with your driver, you may copy back the original usbhid file to its original destination and rebuild the dependency files (sudo depmod -a)

Now I also see that you are trying to use vendor and device id to match your device, in which case, the driver would work only for your mouse. The recommended way is to use class ids, which makes your driver work for any usb mouse.


2) I also read about USB core drivers and USB device drivers. So these HID drivers are core drivers or device drivers?

usbhid is basically a "device driver". The classification of drivers could be briefed out as : core drivers, host controller drivers and device drivers :

Device Drivers : This is the software used to control the devices. For example usb mouse, pci based ethernet card, usb pendrive, i2c based accelerometer.

Host Controller Drivers : This is the software written to control the bus controller. For example USB Host Controllers(EHCI, UHCI, OHCI, etc.), PCI Host Controller, I2C Masters, etc.

Core Drivers : These actually glues up the above discussed drivers. Examples are USB core, PCI core, etc. Core drivers provides helper routines(APIs) such that the device and host-controller driver could make use of them(concept of module stacking!). These are the ones, which bind the correct device to its driver. There are many other services provided by the core drivers.

Example code for USB Device Driver :

http://lxr.free-electrons.com/source/drivers/hid/usbhid/usbmouse.c

You may find the USB Host Controller Drivers under :

http://lxr.free-electrons.com/source/drivers/usb/host/

USB Core resides here : http://lxr.free-electrons.com/source/drivers/usb/core/

I think this also answers your third question!

Hope this helped.

1
votes

The device driver is usbhid.

To prevent it from attaching to your device, add a HID_QUIRK_IGNORE entry to drivers/hid/usbhid/hid-quirks.c, or use the quirks parameter of the usbhid module.