Write kernel module for openwrt15.05
Table of Contents
1 kernel module code structure
package/hello ├── Makefile └── src ├── hello.c ├── Kconfig └── Makefile
2 source code
2.1 hello/Makefile
# # Copyright (C) 2008 OpenWrt.org # # This is free software, licensed under the GNU General Public License v2. # See /LICENSE for more information. # include $(TOPDIR)/rules.mk include $(INCLUDE_DIR)/kernel.mk PKG_NAME:=hello PKG_RELEASE:=1 include $(INCLUDE_DIR)/package.mk define KernelPackage/$(PKG_NAME) SUBMENU:=Test Modules TITLE:=hello driver FILES:=$(PKG_BUILD_DIR)/*.$(LINUX_KMOD_SUFFIX) KCONFIG:= endef define KernelPackage/$(PKG_NAME)/description Kernel module hello endef EXTRA_KCONFIG:= \ CONFIG_HELLO=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:= \ ARCH="$(LINUX_KARCH)" \ CROSS_COMPILE="$(TARGET_CROSS)" \ SUBDIRS="$(PKG_BUILD_DIR)" \ EXTRA_CFLAGS="$(EXTRA_CFLAGS)" \ $(EXTRA_KCONFIG) define Build/Prepare mkdir -p $(PKG_BUILD_DIR) $(CP) ./src/* $(PKG_BUILD_DIR)/ endef define Build/Compile $(MAKE) -C "$(LINUX_DIR)" \ $(MAKE_OPTS) \ modules endef $(eval $(call KernelPackage,$(PKG_NAME)))
2.2 hello/src/hello.c
#include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h> static int __init hello_init(void) { printk("%s\n", __func__); return 0; } static void __exit hello_exit(void) { printk("%s\n", __func__); } module_init(hello_init); module_exit(hello_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Kevin");
2.3 hello/src/Kconfig
config HELLO tristate "Just a example" help This is a example, for debugging kernel model. If unsure, say N.
2.4 hello/src/Makefile
obj-$(CONFIG_HELLO) += hello.o
3 module select and compile
3.1 module select
$ make menuconfig Kernel modules ---> Test Modules ---> <M> kmod-hello
3.2 module compile
make package/hello/{clean,prepare,compile,install} V=s
the compiled module locates in:
build_dir/target-arm_cortex-a7_uClibc-1.0.14_eabi/linux-ipq806x/hello/