27

I have a hard time understanding the exact usage of MODULE_DEVICE_TABLE(usb, id_table)

AFAIK this will generate the map files that will be used later by modprobe whenever a new device is inserted, it will match it against those map files and load the module if it matches.

But my misunderstanding is "isn't the module loaded anyway?"

I mean I already loaded it when I did insmod module-name. or am I missing something?

Sagar Jain
  • 7,475
  • 12
  • 47
  • 83
silentnights
  • 813
  • 3
  • 11
  • 21

3 Answers3

28

It is usually used to support hot-plugging, by loading/inserting the driver for a device if not already loaded.

There is a similar question here: Detect the presence of a device when it's hot plugged in Linux

(From my ans)

It works as follows:

  1. Each driver in the code exposes its vendor/device id using:

      MODULE_DEVICE_TABLE(of, omap_mcspi_of_match);
    
  2. At compilation time the build process extracts this infomation from all the drivers and prepares a device table.

  3. When you insert the device, the device table is referred by the kernel and if an entry is found matching the device/vendor id of the added device, then its module is loaded and initialized.

brokenfoot
  • 11,083
  • 10
  • 59
  • 80
  • so the function that is passed to module_init, will it be called after insmod? or when a matching device is found? – silentnights Apr 06 '14 at 23:35
  • 2
    If the driver has already been `insmod`d then the `module_init` would not be called when you insert the device, the kernel will insmod driver if the driver isn't already `insmod`d. In other words, the `kernel` also does a call to `insmod driver.ko` when the device is hot-plugged. `Insmod`ing an already `insmod` driver doesn't cause any problem, it just indicates a warning saying driver already inserted. – brokenfoot Apr 06 '14 at 23:41
  • 1
    So just to make sure I understand you correctly, there is a difference between inserting a module and loading it... When I insert the module it means the kernel is aware of it's existence and the module registers what functionality it can handle. When a kernel finds a matching device in my case, it will load the module to handle the device. – silentnights Apr 07 '14 at 00:01
  • 4
    No there isn't. Loading=inserting. There are two ways to insert the module: 1. Manually using (insmod/modprobe), 2. Automatically by kernel- "hot-plugging" (kernel after finding the device-driver from the device table does an insmod/modprobe). – brokenfoot Apr 07 '14 at 00:07
  • So, in my first insmod, the module is loaded and linked to any matching device. If another device that matches my driver is inserted, the kernel will load it again to handle the new device as well. Sorry for my many question, but I want to make sure I understand this correctly. – silentnights Apr 07 '14 at 08:24
  • I think this is how it works: as @brokenfoot says, points 1 and 2 occur. Pt 3: (however the module is loaded up) if there's a match, the kernel module is either already in memory or just loaded in; now the kernel merely invokes it's 'probe' method, which continues driver init.. Where's the probe method defined? The 2nd param to MODULE_DEVICE_TABLE is a structure of type [dev]_driver (eg., usb_driver): one of it's members is probe. The driver author sets it to the method required..and the kernel invokes it! – kaiwan Apr 07 '14 at 10:23
  • Kernel doesn't load any modules when hot-plugging, it doesn't also checks device table. Though, your answer is not correct. – Alexey Shmalko Aug 10 '14 at 11:56
  • Where does the mentioned 'device table' located, which saves MODULE_DEVICE_TABLE registrations? What happen to point 2,3 if the module is compiled separately [not built with kernel]? – Chand Priyankara May 23 '15 at 14:50
  • When we compile a module to a running kernel, how would point 3 effects? 'At compilation time the build process extracts this information' Where does this information stored? – Chand Priyankara Jun 17 '15 at 06:15
  • So does `At compilation time the build process` refer to the kernel compilation? – vkats Jun 11 '17 at 14:03
  • @betontalpfa, why did you add unrelated link to the ans? ` (from here)` ? – brokenfoot Oct 09 '20 at 14:44
  • Hi, [that page](https://www.elinux.org/USB) exactly matches your answer. It doesn't seem *unrelated*. It is good to add the official source reference to the solution. (Documentation, link to official page, etc) – betontalpfa Oct 09 '20 at 15:19
  • The link to other SO question already has references. I applaud your transparency, it doesn't apply here. The page you pointed to was created in 2017, while my ans predates it (2014). – brokenfoot Oct 09 '20 at 15:44
14

According to Linux Device Drivers:

  1. MODULE_DEVICE_TABLE is used to generate map files by depmod program;
  2. When device is hot-plugged, bus driver generates hotplug event. Kernel calls /sbin/hotplug with appropriate environmental variables set;
  3. Given map files and information from environment, /sbin/hotplug decides which module to load and actually loads it. If the module is already loaded, it's OK.

I should mention again that this mechanism just ensures that needed module is in-place when device is plugged. That doesn't link module with that device or anything else. Just loads module.

To check if driver is OK for specific device, match() function from bus_type is used.

Alexey Shmalko
  • 3,678
  • 1
  • 19
  • 35
14

Here is how I understands the things [Xbuntu 14.04 compatible].

Once we wrote a module, we can either load it manually, or automatically.

  • Manually -> insmod modulename.ko or modprob modulename.ko
  • Automatically-> There are multiple ways.

    1. copy to /lib/modules/`uname -r`/kernel/modulename.ko and update /etc/modules. System will load the module while booting.

    2. Write a script/command to load the module.ko for an specific harware add/change/remove event in a udev rule /etc/udev/rules.d/10-local.rules. You can do both load/unload using this method.

    3. Code your module with MODULE_DEVICE_TABLE registration. Then load your modulename.ko once and run depmod command [sudo depmod -a] to add the new module to /lib/modules/3.16.0-34-generic/modules.alias /lib/modules/3.16.0-34-generic/modules.dep files. As I know, system will load only if the module is not loaded.

You can monitor module loading/unloading using udev events using :

udevadm monitor

command.

Chand Priyankara
  • 6,739
  • 2
  • 40
  • 63
  • 2
    A slight correction: To add the new module to modules.alias, you do not need to load the module instead it just needs to be placed in `/lib/module/\`uname -r\`/kernel` before running `depmod -a` – microMolvi Jul 08 '15 at 15:47
  • 3
    Better to place it in `/lib/module/`uname -r`/extra`. – Alexis May 05 '20 at 13:13