- Linux Device Driver Development Cookbook
- Rodolfo Giometti
- 505字
- 2021-06-24 13:54:13
How to do it...
Let's see how to do it by following these steps:
- Since this book talks about device drivers, let's start by adding our code under the drivers directory of the Linux sources, and specifically in drivers/misc, where miscellaneous drivers lie. We should place a file named dummy-code.c in drivers/misc with the following contents:
/*
* Dummy code
*/
#include <linux/module.h>
static int __init dummy_code_init(void)
{
printk(KERN_INFO "dummy-code loaded\n");
return 0;
}
static void __exit dummy_code_exit(void)
{
printk(KERN_INFO "dummy-code unloaded\n");
}
module_init(dummy_code_init);
module_exit(dummy_code_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Rodolfo Giometti");
MODULE_DESCRIPTION("Dummy code");
- Our new file, drivers/misc/dummy-code.c, will have no effect if we don't properly insert it into the kernel configuration and building system. In order to do so, we have to modify the drivers/misc/Kconfig and drivers/misc/Makefile files as follows. The former file must be changed, as follows:
--- a/drivers/misc/Kconfig
+++ b/drivers/misc/Kconfig
@@ -527,4 +527,10 @@ source "drivers/misc/echo/Kconfig"
source "drivers/misc/cxl/Kconfig"
source "drivers/misc/ocxl/Kconfig"
source "drivers/misc/cardreader/Kconfig"
+
+config DUMMY_CODE
+ tristate "Dummy code"
+ default n
+ ---help---
+ This module is just for demonstration purposes.
endmenu
The modifications for the latter are as follows:
--- a/drivers/misc/Makefile
+++ b/drivers/misc/Makefile
@@ -58,3 +58,4 @@ obj-$(CONFIG_ASPEED_LPC_SNOOP) += aspeed-lpc-snoop.o
obj-$(CONFIG_PCI_ENDPOINT_TEST) += pci_endpoint_test.o
obj-$(CONFIG_OCXL) += ocxl/
obj-$(CONFIG_MISC_RTSX) += cardreader/
+obj-$(CONFIG_DUMMY_CODE) += dummy-code.o
Note that you can easily add the preceding code and whatever is needed to compile it by just using the patch command, as follows, in your main directory of Linux sources:
$ patch -p1 < add_custom_code.patch
$ patch -p1 < add_custom_code.patch
- Well, if we now use the make menuconfig command and we navigate through Device Drivers to the bottom of the Misc devices menu entries, we should get something as shown in the following screenshot:

In the preceding screenshot, I've already selected the Dummy code entry so that we can see what the final settings should look like.
Note that the Dummy code entry must be selected as built-in ( the * character) and not as module (the M character).
Note also that, if we do not execute the make menuconfig command and we execute directly the make Image command to compile the kernel, then the building system will ask us what to do with the DUMMY_CODE setting, as shown in the following. Obviously, we have to answer yes by using the y character:
$ make Image
scripts/kconfig/conf --syncconfig Kconfig
*
* Restart config...
*
*
* Misc devices
*
Analog Devices Digital Potentiometers (AD525X_DPOT) [N/m/y/?] n
...
Dummy code (DUMMY_CODE) [N/m/y/?] (NEW) y
Note also that, if we do not execute the make menuconfig command and we execute directly the make Image command to compile the kernel, then the building system will ask us what to do with the DUMMY_CODE setting, as shown in the following. Obviously, we have to answer yes by using the y character:
$ make Image
scripts/kconfig/conf --syncconfig Kconfig
*
* Restart config...
*
*
* Misc devices
*
Analog Devices Digital Potentiometers (AD525X_DPOT) [N/m/y/?] n
...
Dummy code (DUMMY_CODE) [N/m/y/?] (NEW) y
- If everything is correctly in place, then we execute the make Image command to recompile the kernel. We should see that our new file is compiled and then added to the kernel Image file, as follows:
$ make Image
scripts/kconfig/conf --syncconfig Kconfig
...
CC drivers/misc/dummy-code.o
AR drivers/misc/built-in.a
AR drivers/built-in.a
...
LD vmlinux
SORTEX vmlinux
SYSMAP System.map
OBJCOPY arch/arm64/boot/Image
- OK, now what we have to do is just replace the Image file on the microSD with the one that has just been rebuilt and then restart the system (see the How to add the kernel recipe in Chapter 1, Installing the Development System).
推薦閱讀
- Linux實戰(zhàn)
- Red Hat Enterprise Linux 8系統(tǒng)管理實戰(zhàn)
- vSphere Virtual Machine Management
- 嵌入式應(yīng)用程序設(shè)計綜合教程(微課版)
- Windows Server 2019 Administration Fundamentals
- 8051軟核處理器設(shè)計實戰(zhàn)
- Android物聯(lián)網(wǎng)開發(fā)細(xì)致入門與最佳實踐
- INSTANT Migration from Windows Server 2008 and 2008 R2 to 2012 How-to
- Advanced TypeScript Programming Projects
- ElasticSearch Cookbook
- Cassandra 3.x High Availability(Second Edition)
- 寫給架構(gòu)師的Linux實踐:設(shè)計并實現(xiàn)基于Linux的IT解決方案
- μC/OS-III內(nèi)核實現(xiàn)與應(yīng)用開發(fā)實戰(zhàn)指南:基于STM32
- Learning Continuous Integration with Jenkins(Second Edition)
- 鴻蒙HarmonyOS手機(jī)應(yīng)用開發(fā)實戰(zhàn)