Search code examples
linuxlinux-kernellinux-device-driverkernel-module

How are some Linux device drivers automatically loaded/unloaded?


With regards to Linux device drivers, my understanding (obtained from reading this excellent DIY article) is that there are essentially six events/parts of a device driver "lifecycle":

  • Load - the compiled driver is loaded as a Linux kernel module via insmod
  • Open - the device is opened/readied for usage
  • Read/Write - the device is used
  • Close - the device is closed/disconnected, currently no longer useable (unless re-opened)
  • Release - the driver (now a kernel module) is unloaded from the kernel via rmmod

Thanks to that article and countless others, I can now write a whole bunch of C code to implement hooks/callbacks for what should happen when the kernel issues Open, Read, Write and Close commands. But, it seems that the driver must be loaded/released manually by issuing an insmod (load) and rmmod (release) at the shell.

However, I know this can't be the case because certain devices, like USB, allow you to connect/disconnect them dynamically/on-the-fly, and their respective drivers must be automatically loaded/released on-the-fly as well.

So this stirs up the following question: How do certain technologies, like USB, automate the execution of insmod and rmmod (hence the dynamic loading/releasing of USB device drivers)?


Solution

  • Its based on linux hotplug. The below link may useful for you.

    https://web.archive.org/web/20150316211806/http://www.makelinux.net/ldd3/chp-14-sect-7