Loading Module on Keyboard Hotplug

Loading module on keyboard hotplug

I had the same problem. In my case it was caused by the usbhid module being already loaded, since I was using a USB mouse.

If I understand it correctly, in Ubuntu 14.04, the udev rule that loads the proper module(s) when a new device is attached is the following (located in /lib/udev/rules.d/80-drivers.rules):

DRIVER!="?*", ENV{MODALIAS}=="?*", RUN{builtin}="kmod load $env{MODALIAS}"

As you can see, kmod load is executed only if the new device has no driver. However, if usbhid is already loaded, the just-attached keyboard already has a driver. Therefore the "hello world" module is not loaded.

A possible solution is to modify/override the udev rule by removing the DRIVER!="?*" condition, thus turning it into:

ENV{MODALIAS}=="?*", RUN{builtin}="kmod load $env{MODALIAS}"`.

Another possible workaround is to unload the usbhid module before attaching the keyboard. Of course, that will cause all USB keyboards, mice and other HID class devices to stop working until you attach the new keyboard.

How to load a module (not a driver) when a USB device is plugged in

In modern Linux the functionality for loading drivers/modules (or invoking any other commands) whenever new hardware is detected is handled by udev. You will have to write a udev rule for your device that will instruct the kernel to load your module when your device has been detected and the corresponding event has occurred. Read more about it here.

Load state of a module in /proc/modules

There are 3 possible states for this field:

  • Live
  • Loading
  • Unloading

You can see this in kernel sources, in kernel/module.c file:

/* Informative for users. */
seq_printf(m, " %s",
mod->state == MODULE_STATE_GOING ? "Unloading" :
mod->state == MODULE_STATE_COMING ? "Loading" :
"Live");

The description of mod->state can be found in enum module_state, in include/linux/module.h:

enum module_state {
MODULE_STATE_LIVE, /* Normal state. */
MODULE_STATE_COMING, /* Full formed, running module_init. */
MODULE_STATE_GOING, /* Going away. */
MODULE_STATE_UNFORMED, /* Still setting it up. */
};

Which drivers are used by usb mouse in linux kernel?

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.

Prevent usbhid from autoloading when USB HID device is plugged in

udev is the best and easiest way doing that, add a new rule in e.g.:
/etc/udev/rules.d/99-disable-usb-hid.rules:

SUBSYSTEMS=="usb", DRIVERS=="usbhid", ACTION=="add", ATTR{authorized}="0"

and restart udev.
I've just tested it in Debian Jessie ARM 4.4.16.



Related Topics



Leave a reply



Submit