OpenWrt Forum Archive

Topic: Develop Openwrt kernel module

The content of this topic has been archived on 1 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

I'm learning how to develop a kernel module in Openwrt. I make a hello world try. The package directory tree is:

khelloworld/

    Makefile

    src/

        khelloworld.c

        Makefile

The Openwrt Makefile:

include $(TOPDIR)/rules.mk
include $(INCLUDE_DIR)/kernel.mk

PKG_NAME:=khelloworld
PKG_RELEASE:=1
PKG_VERSION:=1.0

include $(INCLUDE_DIR)/package.mk

define KernelPackage/khelloworld
  SUBMENU:=HELLO WORLD MODULES
  TITLE:=khelloworld
  MAINTAINER:=Nobody
  MENU:=1
  FILES:=$(PKG_BUILD_DIR)/$(PKG_NAME).$(LINUX_KMOD_SUFFIX)
endef

EXTRA_KCONFIG:= \
    CONFIG_HELLO_MOD=m

EXTRA_CFLAGS:= \
    $(patsubst CONFIG_%, -DCONFIG_%=1, $(patsubst %=m,%,$(filter %=m,$(EXTRA_KCONFIG)))) \
    $(patsubst CONFIG_%, -DCONFIG_%=1, $(patsubst %=y,%,$(filter %=y,$(EXTRA_KCONFIG)))) \

#MAKE_OPTS:= \
#   EXTRA_CFLAGS="$(EXTRA_CFLAGS)" \
#   $(EXTRA_KCONFIG)

define Build/Prepare
    # Copy sources
    mkdir -p $(PKG_BUILD_DIR)
    cp -R ./src/* $(PKG_BUILD_DIR)/
endef

define Build/Compile
    $(MAKE) -C "$(LINUX_DIR)" \
        CROSS_COMPILE="$(TARGET_CROSS)" \
        ARCH="$(LINUX_KARCH)" \
        SUBDIRS="$(PKG_BUILD_DIR)" \
        EXTRA_CFLAGS="$(EXTRA_CFLAGS)" \
        $(EXTRA_KCONFIG) \
        modules
endef
$(eval $(call KernelPackage,khelloworld))

The source Makefile:

obj-m += khelloworld.o

all:
    $(MAKE) -C "$(LINUX_DIR)" \
    $(MAKE_OPTS) \
    modules

The C helloworld source:

#include <linux/module.h>
#include <linux/kernel.h>

int init_module( void ) {
    printk( KERN_INFO "Hello World KERNEL!!!\n" );
    return 0;
}

void cleanup_module( void ) {
    printk( KERN_INFO "Goodbye World KERNEL!!!\n" );
}

This module compiles well with this command: make package/khelloworld/compile

Then I installed it in my openwrt using this command:  opkg install kmod-khelloworld-xxxxxx.ipk

the install is done without errors. but the problem dmesg doesn't display the expected message of the init function.

I thought that insmod is missing. So I added the following install macro to the Makefile just before "$(eval $(call KernelPackage,khelloworld))"

define Build/install
    insmod $(PKG_BUILD_DIR)/khelloworld.ko
endef

but that didn't solve the problem. Has anyone an idea about this problem and how I can correct it?

(Last edited by OmarKallel on 8 Oct 2017, 04:43)

You should rather use this example, and update the AUTOLOAD section.

define KernelPackage/avila-wdt
  SUBMENU:=Other modules
  TITLE:=GPIO hardware watchdog driver for modified Avila boards
  DEPENDS:=@GPIO_SUPPORT @TARGET_ixp4xx
  FILES:=$(PKG_BUILD_DIR)/avila-wdt.ko
  AUTOLOAD:=$(call AutoLoad,10,avila-wdt)
endef

Does insmod-ding the .ko manually work?

Regards,
-w-

The discussion might have continued from here.