编译OpenWrt内核驱动可以参考OpenWrt内部其它驱动的编写例程,来修改成自己需要的驱动
下载OpenWrt的官方源码:
git clone https://github.com/openwrt/openwrt.git
sudo apt update -y
sudo apt full-upgrade -y
sudo apt install -y ack antlr3 asciidoc autoconf automake autopoint binutils bison build-essential \
bzip2 ccache cmake cpio curl device-tree-compiler fastjar flex gawk gettext gcc-multilib g++-multilib \
git gperf haveged help2man intltool libc6-dev-i386 libelf-dev libglib2.0-dev libgmp3-dev libltdl-dev \
libmpc-dev libmpfr-dev libncurses5-dev libncursesw5-dev libreadline-dev libssl-dev libtool lrzsz \
mkisofs msmtp nano ninja-build p7zip p7zip-full patch pkgconf python2.7 python3 python3-pyelftools \
libpython3-dev qemu-utils rsync scons squashfs-tools subversion swig texinfo uglifyjs upx-ucl unzip \
vim wget xmlto xxd zlib1g-dev python3-setuptools
进入openwrt目录后执行以下指令
./scripts/feeds update -a
./scripts/feeds install -a
根据自己的平台来选择编译选项
make menuconfig
进入openwrt目录后执行以下指令
make download -j8
make V=s -j1
OpenWrt驱动存放目录为openwrt\package\kernel
以gpio-button-hotplug驱动为例,进行分析。在gpio-button-hotplug文件夹下面有一个Makefile文件和src文件夹,而src文件夹下有模块源码gpio-button-hotplug.c和源码编译Makefile
顶层makefile如下:
#
# Copyright (C) 2008-2012 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:=gpio-button-hotplug
PKG_RELEASE:=3
PKG_LICENSE:=GPL-2.0
include $(INCLUDE_DIR)/package.mk
define KernelPackage/gpio-button-hotplug
SUBMENU:=Other modules
TITLE:=Simple GPIO Button Hotplug driver
FILES:=$(PKG_BUILD_DIR)/gpio-button-hotplug.ko
AUTOLOAD:=$(call AutoLoad,30,gpio-button-hotplug,1)
KCONFIG:=
endef
define KernelPackage/gpio-button-hotplug/description
This is a replacement for the following in-kernel drivers:
1) gpio_keys (KEYBOARD_GPIO)
2) gpio_keys_polled (KEYBOARD_GPIO_POLLED)
Instead of generating input events (like in-kernel drivers do) it generates
uevent-s and broadcasts them. This allows disabling input subsystem which is
an overkill for OpenWrt simple needs.
endef
define Build/Compile
$(KERNEL_MAKE) M="$(PKG_BUILD_DIR)" modules
endef
$(eval $(call KernelPackage,gpio-button-hotplug))
首先包含rules.mk、kernel.mk和package.mk文件。接着将驱动模块的名称定义为“gpio-button-hotplug”,并设置版本编号为“3”。
在软件包定义中的一些变量赋值:
SUBMENU:我们内核模块放置于“Other modules”。我们在make menuconfig时,可以在Kernel modules/other modules菜单下找到这个模块。
TITLE:标题,驱动模块的简短描述。
FILES:生成的驱动模块的存放位置。此处为设置为在编译目录下(就是编译过程中的临时目录build_dir)。
AUTOLOAD:代表是否在系统启动时自动装载到内核中,后面括号内有3个参数(参数1不变,参数2为驱动模块的装载顺序(可以省略这个参数,省略后系统自动分配状态顺序),参数3代表驱动模块名称)。
DEPENDS:如果驱动模块还需要依赖,则此变量设置为依赖文件名(此处没有依赖所以就未设置)。
最后KernelPackage,将驱动模块的名称作为参数传递给KernelPackage
obj-m += gpio-button-hotplug.o
gpio-button-hotplug.c就是驱动的实际代码
将gpio-button-hotplug文件夹复制为mykernel_test,放置到gpio-button-hotplug相同的目录下
修改顶层makefile如下:
#
# Copyright (C) 2008-2012 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:=mykernel_test
PKG_RELEASE:=3
PKG_LICENSE:=GPL-2.0
include $(INCLUDE_DIR)/package.mk
define KernelPackage/mykernel_test
SUBMENU:=Other modules
TITLE:=Simple GPIO Button Hotplug driver
FILES:=$(PKG_BUILD_DIR)/mykernel_test.ko
AUTOLOAD:=$(call AutoLoad,30,mykernel_test,1)
KCONFIG:=
endef
define KernelPackage/mykernel_test/description
This is a replacement for the following in-kernel drivers:
1) gpio_keys (KEYBOARD_GPIO)
2) gpio_keys_polled (KEYBOARD_GPIO_POLLED)
Instead of generating input events (like in-kernel drivers do) it generates
uevent-s and broadcasts them. This allows disabling input subsystem which is
an overkill for OpenWrt simple needs.
endef
define Build/Compile
$(KERNEL_MAKE) M="$(PKG_BUILD_DIR)" modules
endef
$(eval $(call KernelPackage,mykernel_test))
obj-m += mykernel_test.o
#include
#include
static int __init mykernel_test_init(void)
{
printk(KERN_INFO "mykernel_test enter\n");
return 0;
}
static void __exit mykernel_test_exit(void)
{
printk(KERN_INFO "mykernel_test exit\n ");
}
module_init(mykernel_test_init);
module_exit(mykernel_test_exit);
MODULE_LICENSE("GPL v2");
在OpenWrt源码顶级目录下输入下面的命令配置内核
make menuconfig
在OpenWrt源码顶级目录下输入下面的命令编译驱动模块
make package/kernel/mykernel_test/compile V=s