SJain SJain - 2 months ago 4
C Question

What is MODULE_ALIAS in Linux device driver code?

Explanation for MODULE_ALIAS in the code says

/* work with hotplug and coldplug */

But, what exactly is MODULE_ALIAS?

Is there a significance for : (colon) in the argument?


MODULE_ALIAS is macro, added in 2002 with update of linux kernel module loaders, and used since 2003. This macro allows module creator to define additional names of the module (aliases), for example to make autoloading of the module easier.

The aliases are used to give some special name, e.g. "block-major-100" directly in the module source, instead of using /etc/modules.conf for defining aliases. When user program accesses the block device with major number 100, kernel will try to load "block-major-100". Without MODULE_ALIAS kernel should go to userspace and read /etc/modules.conf with helper. And with MODULE_ALIAS("block-major-100") kernel will solve the search by itself.

You can read more about this macro in "MODULE_ALIAS" article by corbet, 2003-09-03.

There are several more special versions of the MODULE_ALIAS, listed by corbet:

The actual variants used depend on the subsystem; block drivers use MODULE_ALIAS_BLOCKDEV, for example, while char devices use MODULE_ALIAS_CHARDEV or MODULE_ALIAS_MISCDEV and network protocols use MODULE_ALIAS_NETPROTO.

According to 2011 patch from Mans Rullgard (linaro), or to commit by Kay Sievers (vrfy), MODULE_ALIAS with argument like "platform:... is used to enable module auto-loading "when platform devices are scanned.". In SPI drivers, it is used for "hotpluggable SPI platform drivers, to allow module auto loading.", since 43cc71eed1250755986da4c0f9898f9a635cb3bf by Kay Sievers - "platform: prefix MODALIAS with "platform:"":

Prefix platform modalias strings with "platform:", which modprobe config to blacklist alias resolving if userspace configures it.

Driver aliases with "platform:" are used in drivers/base/platform.c file, function modalias_show(...) (snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);) and in platform_uevent(...) add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX, pdev->name); where PLATFORM_MODULE_PREFIX macro is defined as "platform:" (so, colon mark is significant).