To learn about the driver, I found an instance helloworld and wrote my first driver.
1. Establish the environment
Install the Ubuntu system, open the terminal, and enter with the root permission. The command is as follows:
bory@borya:~$ sudo -s
View your Linux kernel package
root@borya:~# apt-cache search linux-sourcelinux-source - Linux kernel source with Ubuntu patcheslinux-source-3.0.0 - Linux kernel source for version 3.0.0 with Ubuntu patches
The kernel package for the local machine is a linux-source-3.0.0
Next, download the kernel source code package.
root@borya:~# apt-get install linux-source-3.0.0
After the download is complete, first extract the CS file to the/usr/srcdirectory and then decompress linux-source-3.0.0.tar.bz2.
root@borya:~# cd /usr/src/root@borya:/usr/src# lslinux-headers-3.0.0-12 linux-source-3.0.0 vboxguest-4.1.16linux-headers-3.0.0-12-generic linux-source-3.0.0.tar.bz2root@borya:/usr/src# tar jxvf linux-source-3.0.0.tar.bz2
Compile oldconfig
root@borya:/usr/src/linux-source-3.0.0# make oldconfig
Compile bzimage again. This takes more than one hour.
root@borya:/usr/src/linux-source-3.0.0# make bzImage
It may take more than one hour to continue compiling modules.
root@borya:/usr/src/linux-source-3.0.0# make modules
Finally, install modules.
root@borya:/usr/src/linux-source-3.0.0# make modules_install
So far, the environment has been set up. The following is our first driver helloworld.
2. Run the driver helloworld.
Create a directory named test under any working directory of the user.
Write hello. c
#include "linux/init.h"#include "linux/module.h"static int hello_init(void){ printk(KERN_ALERT "Hello World linux_driver_module\n"); return 0;}static void hello_exit(void){ printk(KERN_ALERT "This is first step linux_driver_module\n");}module_init(hello_init);module_exit(hello_exit);
Write the MAKEFILE file below
ifneq ($(KERNELRELEASE),)obj-m := hello.oelseKERNELDIR ?= /lib/modules/$(shell uname -r)/buildPWD := $(shell pwd)default: $(MAKE) -C $(KERNELDIR) M=$(PWD) modulesendif
To explain, $ (shell uname-R) refers to the content output by you input the shell command uname-R on the terminal. It is actually a directory name, and $ (shell PWD) refers to the current path.
The last step is make.
bory@borya:~/driver/hello$ make
You can use ls to check which files are added after make.
Of course it is not enough. We have not seen the helloworld attribute. Run the insmod command to load hello. Ko to the kernel.
bory@borya:~/driver/hello$ insmod ./hello.ko
It seems that there is no helloworld. Well, let's go to the log file and check whether/var/log/syslog is enabled.
Jul 9 21:30:36 Borya kernel: [20377.621046] Hello World linux_driver_module
Uninstall hello
root@borya:~/driver/hello$rmmod ./hello.ko
Open/var/log/syslog again and you will see the last line.
Jul 9 22:06:51 Borya kernel: [22551.983955] This is first step linux_driver_module
At this point, the first driver in Linux ended perfectly!