编译OpenWrt内核驱动可以参考OpenWrt内部其它驱动的编写例程,来修改成自己需要的驱动
一、OpenWrt源代码获取与编译
1.1、搭建环境
下载OpenWrt的官方源码:
git clone https://github.com/openwrt/openwrt.git
1.2、安装编译依赖项
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
1.3、更新 feeds
进入openwrt目录后执行以下指令
./scripts/feeds update -a
./scripts/feeds install -a
1.4、配置编译选项
根据自己的平台来选择编译选项
make menuconfig
1.5、下载 dl 库
进入openwrt目录后执行以下指令
make download -j8
1.6、编译
make V=s -j1
二、OpenWrt驱动源代码分析
OpenWrt驱动存放目录为openwrt\package\kernel
以gpio-button-hotplug驱动为例,进行分析。在gpio-button-hotplug文件夹下面有一个Makefile文件和src文件夹,而src文件夹下有模块源码gpio-button-hotplug.c和源码编译Makefile
2.1、顶层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))
2.1.1、第一步
首先包含rules.mk、kernel.mk和package.mk文件。接着将驱动模块的名称定义为“gpio-button-hotplug”,并设置版本编号为“3”。
2.1.2、第二步
在软件包定义中的一些变量赋值:
SUBMENU:我们内核模块放置于“Other modules”。我们在make menuconfig时,可以在Kernel modules/other modules菜单下找到这个模块。
TITLE:标题,驱动模块的简短描述。
FILES:生成的驱动模块的存放位置。此处为设置为在编译目录下(就是编译过程中的临时目录build_dir)。
AUTOLOAD:代表是否在系统启动时自动装载到内核中,后面括号内有3个参数(参数1不变,参数2为驱动模块的装载顺序(可以省略这个参数,省略后系统自动分配状态顺序),参数3代表驱动模块名称)。
DEPENDS:如果驱动模块还需要依赖,则此变量设置为依赖文件名(此处没有依赖所以就未设置)。
2.1.3、第三步
配置menuconfig时的描述信息
2.1.4、第四步
编译源代码选项,在大多数情况下应该不用定义而使用默认值
2.1.5、第五步
最后KernelPackage,将驱动模块的名称作为参数传递给KernelPackage
2.2、源码makefile
obj-m += gpio-button-hotplug.o
2.3、源码gpio-button-hotplug.c
gpio-button-hotplug.c就是驱动的实际代码
三、编写自己的OpenWrt驱动代码
将gpio-button-hotplug文件夹复制为mykernel_test,放置到gpio-button-hotplug相同的目录下
3.1、顶层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:=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))
3.2、源码makefile
obj-m += mykernel_test.o
3.3、源码mykernel_test.c.c
#include <linux/init.h>
#include <linux/module.h>
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驱动代码
4.1、配置
在OpenWrt源码顶级目录下输入下面的命令配置内核
make menuconfig
依次按照如下顺序选择:
4.2、编译
在OpenWrt源码顶级目录下输入下面的命令编译驱动模块文章来源:https://www.toymoban.com/news/detail-702915.html
make package/kernel/mykernel_test/compile V=s
编译完成之后会在下面的目录中看到我们的内核模块
此ko模块和普通的linux内核的驱动模块的装载方法一样,不再赘述。文章来源地址https://www.toymoban.com/news/detail-702915.html
到了这里,关于编译OpenWrt内核驱动的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!