Prevent Usbhid from Autoloading When Usb Hid Device Is Plugged In

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.

automatically bind a device to a specific driver

I found two ways to automatically bind a device to my driver

  • Adding the device to the hid_have_special_driver struct in hid-core.c.

    The struct looks somthing like that:

    static const struct hid_device_id hid_have_special_driver[] = {
    #if IS_ENABLED(CONFIG_HID_A4TECH)
    { HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_WCP32PU) },
    { HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_X5_005D) },
    { HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_RP_649) },
    #endif
    //...
    {}
    }

    This is how it is normally done, I haven't tried it yet. You have to recompile hid-core.c (hid.ko).

  • Using the bind and unbind functionality inside an udev-rule.

    Add a new rule to /etc/udev/rules.d/ (e.g. 99-mydriver.rules) which automatically unbinds the device from hid-generic and binds it to your driver

    Under Arch Linux x86_64:

    ACTION=="bind", KERNELS=="0005:<VENDOR_ID>:<PRODUCT_ID>.*", SUBSYSTEMS=="hid", DRIVERS=="hid-generic", \
    RUN+="/bin/bash -c 'echo $kernel > /sys/bus/hid/drivers/$driver/unbind'", \
    RUN+="/bin/bash -c 'echo $kernel > /sys/bus/hid/drivers/<MY_DRIVER>/bind'"

    Under Raspbian Stretch the following works for me

    ACTION=="add", KERNEL=="0005:<VENDOR_ID>:<PRODUCT_ID>.*", SUBSYSTEM=="hid", DRIVER=="hid-generic", \
    RUN+="/bin/bash -c 'echo $kernel > /sys/bus/hid/drivers/hid-generic/unbind'", \
    RUN+="/bin/bash -c 'echo $kernel > /sys/bus/hid/drivers/<MY_DRIVER>/bind'"

    Replace <VENDOR_ID>, <PRODUCT_ID> and <MY_DRIVER> as needed


Further Information: http://0x0001.de/linux-driver-loading-registration-and-binding/

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.

Django - queryset and general SQL questions

The ommitted joins are important. If you look at the full query, you will see that Django does two Left Outer Joins from the foo table to the joining table foo_bar and to the bar table.

Consider two foos and two bars. Let foo_1 be related to bar_1 and bar_2, and foo_2 not be related to any bars.

The query below with two left outer joins with include every foo at least once, and NULL will appear in the bar column for foo_2 which is not related to any bars.

SELECT foo.id as foo_id, bar.id as bar_id 
FROM foo LEFT OUTER JOIN foo_bar
ON foo_id = foo_bar.foo_id
LEFT OUTER JOIN bar
ON foo_bar.bar_id = bar.id;
+--------+--------+
| foo_id | bar_id |
+--------+--------+
| 1 | 1 |
| 1 | 2 |
| 2 | NULL |
+--------+--------+

The query for Foo.objects.get(bar__isnull=True) is similar to this, but it doesn't select anything from the bar table, and it filter on bar.id is NULL, because we only want the foos which are not related to any bars.

SELECT foo.id as foo_id 
FROM foo LEFT OUTER JOIN foo_bar
ON foo_id = foo_bar.foo_id
LEFT OUTER JOIN bar
ON foo_bar.bar_id = bar.id
where bar_id is NULL;
+--------+
| foo_id |
+--------+
| 2 |
+--------+

How can I prevent linux from initializing a USB HID device

you have right, you have to play with the udev rules.

First of all you have to identify your device. Find the idProduct and the idVendor of your device. You can use:

lsusb

Then in the rules.d folder (/etc/udev/rules.d) create a new file with the name:

10-my-usb.rules

In this file add this line

SUBSYSTEM=="usb",ATTRS{idVendor}=="XXXX", ATTRS{idProduct}=="XXXX", MODE="666", GROUP+="plugdev"

Replace the XXXX with the value you get before

Then restart your udev rules:

sudo udevadm trigger

Then unplug and replug normally you can use it



Related Topics



Leave a reply



Submit