- Linux Device Driver Development Cookbook
- Rodolfo Giometti
- 296字
- 2021-06-24 13:54:14
Getting ready
To turn our dummy-code.c file into a kernel module, we only have to change our kernel settings, allowing the compilation of our example module (by replacing the * character with M in the kernel configuration menu). However, under some circumstances, it could be more useful having our driver released into a dedicated archive completely separated from kernel sources. Even in this case, no changes are to be done to the existing code, and we will be able to compile dummy-code.c inside the kernel source tree, or even outside it!
To build up our first kernel module as external code, we can safely take the preceding dummy-code.c file and then put it into a dedicated directory with the following Makefile:
ifndef KERNEL_DIR
$(error KERNEL_DIR must be set in the command line)
endif
PWD := $(shell pwd)
ARCH ?= arm64
CROSS_COMPILE ?= aarch64-linux-gnu-
# This specifies the kernel module to be compiled
obj-m += dummy-code.o
# The default action
all: modules
# The main tasks
modules clean:
make -C $(KERNEL_DIR) \
ARCH=$(ARCH) \
CROSS_COMPILE=$(CROSS_COMPILE) \
SUBDIRS=$(PWD) $@
Looking at the preceding code, we see that the KERNEL_DIR variable must be supplied on the command line pointing to the path to ESPRESSObin's previously compiled kernel sources, while the ARCH and CROSS_COMPILE variables are not mandatory since Makefile specifies them (however, supplying them on the command line will take precedence).
Also, we should verify that the insmod and rmmod commands are available in our ESPRESSObin, as follows:
# insmod -h
Usage:
insmod [options] filename [args]
Options:
-V, --version show version
-h, --help show this help
If they are not present, then they can be installed by adding the kmod package with the usual apt install kmod command.
- Linux設(shè)備驅(qū)動(dòng)開(kāi)發(fā)詳解:基于最新的Linux4.0內(nèi)核
- Linux設(shè)備驅(qū)動(dòng)開(kāi)發(fā)詳解(第2版)
- 零起點(diǎn)學(xué)Linux系統(tǒng)管理
- Linux網(wǎng)絡(luò)內(nèi)核分析與開(kāi)發(fā)
- 深入Linux內(nèi)核架構(gòu)與底層原理(第2版)
- 混沌工程:復(fù)雜系統(tǒng)韌性實(shí)現(xiàn)之道
- Windows Vista融會(huì)貫通
- 網(wǎng)絡(luò)操作系統(tǒng)管理與應(yīng)用(第三版)
- Social Data Visualization with HTML5 and JavaScript
- Microsoft Hyper-V Cluster Design
- Mastering Sass
- 辦公自動(dòng)化教程(Windows7+Office2010)
- 電腦辦公(Windows 7+Office 2016)入門(mén)與提高
- Serverless Architectures with Kubernetes
- 程序員必讀經(jīng)典(算法基礎(chǔ)+計(jì)算機(jī)系統(tǒng))