Loading and probing of USB device drivers
The Linux USB driver expects a probe function which is called when a matching device is plugged in.
When a device is plugged into the USB bus that matches the device ID pattern that your driver registered with the USB core, the probe function is called.
Following are some questions that have appeared on the internet time and again6.
When will probe function be called ?⌗
When a matching entry registered in MODULE_DEVICE_TABLE is found for the device.
If there are two matches to MODULE_DEVICE_TABLE, would both probes be called ?⌗
No, only the one which binds to the device successfully.
Would all the modules always be loaded if they contain a matching entry in MODULE_DEVICE_TABLE ?⌗
By loading, call to module_init
is reffered.
Considering the case of udev, it’s the responsibility of udev to load kernel modules. The following confirms it:
Device drivers compiled as modules may have aliases built into them. Aliases are visible in the output of the modinfo program and are usually related to the bus-specific identifiers of devices supported by a module.
For example, the snd-fm801 driver supports PCI devices with vendor ID 0x1319 and device ID 0x0801, and has an alias of “pci:v00001319d00000801svsdbc04sc01i*”. For most devices, the bus driver exports the alias of the driver that would handle the device via sysfs. E.g., the /sys/bus/pci/devices/0000:00:0d.0/modalias file might contain the string “pci:v00001319d00000801sv00001319sd00001319bc04sc01i00”.
The default rules provided with udev will cause udevd to call out to /sbin/modprobe with the contents of the MODALIAS uevent environment variable (which should be the same as the contents of the modalias file in sysfs), thus loading all modules whose aliases match this string after wildcard expansion
Notice the last line, “thus loading all modules whose aliases match this string after wildcard expansion”. So yes! All modules will be loaded.
My module_init() was called by probe was not⌗
See above
Udev accesses same info in order to figure out the device, driver pair. Is that table accessible by any userland program ? Where?⌗
modinfo module_name
The alias
is matched against the device.
In what order will the kernel decide to give opportunities to device drivers to bind to a device ?⌗
Load order is no one’s responsibility. Without any priority, either driver may be probed first and consequently may end up binding to the device; the result is more or less random. It may even differ from one boot to the next.
Thanks to Alan Stern from linux-usb mailing list to clarify this to me. I highly recommend to read the thread and look at dd.c
, bus.c
in drivers/base
.
If you happen to find any discrepancies, do send me a note.