Which drivers are used by usb mouse in linux kernel?

前端 未结 2 639
孤城傲影
孤城傲影 2020-12-08 11:42

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 ther

相关标签:
2条回答
  • 2020-12-08 12:26

    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.

    0 讨论(0)
  • 2020-12-08 12:35

    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.

    0 讨论(0)
提交回复
热议问题