kernel-moduleopenwrt

Struggling making built-in modules


I am designing two modules - one is class and second is device driver. My problem is both .ko modules at the end are not built-in and not called for probing the device in the device tree - when I insert class and then driver in using insmod, only then device file is created.

I put the files into the package/kernel/mypackage directory, with Makefile containing

define KernelPackage/mypackage
 ...
 FILES:= $(PKG_BUILD_DIR)/mydriver.ko $(PKG_BUILD_DIR)/myclass.ko
 AUTOLOAD:=$(call AutoLoad,30,mypackage,1)
 ...
endef

and then have src/ directory with source files and Makefile containing

obj-m += myclass.o
obj-m += mydriver.o

When I run make V=s package/kernel/mypackage/{clean,compile} I see "need-built-in=1" on the screen, when I just run make V=s I do not see it.

Next, if I change obj-m to obj-y things stop compiling at all, process is complaining about missing .ko files (which are really missing in the work directory).

What do I do wrong?


Solution

  • I put the files into the package/kernel/mypackage directory

    So your source code for your module is out-of-tree, i..e. separate from the kernel source code tree.

    Source code for a module that is in-tree, i..e. part of the kernel source code tree, can be built as a loadable module (i.e. a separate .ko file) or statically linked into the kernel image (aka "built-in").
    But out-of-tree code (such as your package for a module) can only be built as a loadable module. Your efforts to build it into the kernel will be futile.

    What do I do wrong?

    You've put your files in the wrong place.

    You could merge your code into an appropriate sudirectory in a copy of the kernel source tree, then define a new CONFIG_MYDRIVER build symbol for your module, and modify the appropriate Kconfig and Makefile files to support your new module.

    Or leave your code out-of-tree and build a loadable module. Then properly install the .ko file to automatically load your module during boot (i.e. after the rootfs is mounted and maybe before userspace is started); see https://lwn.net/Articles/740455/ and https://tldp.org/HOWTO/html_single/Module-HOWTO/#AUTOLOAD. If using an ARM SoC, then be aware of a properly written Device Tree that needs to specify the driver, e.g. see Driver code in kernel module doesn't execute?.