linux Driven Learning 2-Character Device Driver


Preliminary learning of character device-driven notes.

1. Driver module commands

In linux, when a driver is compiled in a modular mode, it is not directly active as the system starts, but lying quietly in a corner of the kernel, which is not available. Applications that want to invoke the driver must load it before they can use it. There is also an uninstallation process after use.
If you've played with drivers, you know that after compiling the drivers, there will be a KO (kernel object) file. The meaning of this file is to move some of the functions of the kernel outside the kernel, insert the kernel when needed, and uninstall when not needed.
So there are some commands that make it easy for us to work with kernel modules
(1) lsmod(list module, display module list) function is to print out the list of modules installed in the kernel
(2) insmod(install module), install a module, use insmod xxx.ko
(3) modinfo(module infomation, display module information) Prints out the information that comes with the kernel module, including license, version number, kernel directory, etc. Usage modinfo xxx.ko
(4) rmmod(remove module, uninstall module) uninstall the installed module, using rmmod xxxx (no suffix, no ko)

2. Simple driver modules

Let's write a simple driver to show some of the commands we just made
hello.c

#include <linux/module.h>
#include <linux/init.h>

MODULE_LICENSE     ("GPL");
MODULE_AUTHOR      ("XHH");
MODULE_DESCRIPTION ("A test module");

static int hello_init(void) {
	printk(KERN_DEBUG "hello world\n");
	return 0;
}

static void hello_exit(void) {
	printk(KERN_DEBUG"goodbye my world\n");
}

module_init(hello_init);
module_exit(hello_exit);

Makefile

KEVN := $(shell uname -r)
PWD  := $(shell pwd)
KERN_DIR := /lib/modules/$(KEVN)/build

obj-m := hello.o

all:
	make	-C $(KERN_DIR) M=$(PWD) modules
clean:
	make	-C $(KERN_DIR) M=$(PWD) clean

Writing Makefile here

make Here is TAB	-C $(KERN_DIR)  M=$(PWD) modules  //M=$(PWD) cannot have spaces in the middle

At the beginning

make after compilation

We can use lsmod to see which of our current driver modules are, and note that these are rewindings, the latest on top

It is obvious that hello.ko is not installed
Let's load insmod hello.ko and see

There's a hint, no operation rights, just switch to root rights

It's already installed here
Unloaded when not in use

Loaded drivers see information through modinfo

Since we don't see our printk information in ubuntu, we need to use dmesg to view the information

The latest drivers for loading and unloading are at the bottom, which we can see directly

3. Code Interpretation

#include <linux/module.h>
#include <linux/init.h>

MODULE_LICENSE     ("GPL");
MODULE_AUTHOR      ("XHH");
MODULE_DESCRIPTION ("A test module");

static int hello_init(void) {
	printk(KERN_DEBUG "hello world\n");
	return 0;
}

static void hello_exit(void) {
	printk(KERN_DEBUG"goodbye my world\n");
}

module_init(hello_init);
module_exit(hello_exit);

First, explain the header file:
linux/module.h refers to the module.h file in the next directory of linux, mainly the functions related to the modules MODULE_LICENSE in the code
linux/init.h refers to the file init.h in the next directory under linux. The main function of this file is to underline _init and _exit in the code, which is equivalent to writing directly into the document.init.exit.
Header files in multiple directories can also change linux/xxx/yyy/ooo.h

MODUE_LICENSE 
MODULE_AUTHOR
MODULE_DESCRIPTION
 That's all the information. module Agreements, Authors, Simple Descriptions, etc.
printk(KERN_DEBUG "hello world\n");
printk Is a function in the kernel, and we printf yes C Library, so here's the printf It doesn't work because we drive inside the kernel.
KERN_DEBUG There should be a total of 8 levels. The smaller the number, the bigger the proof error. See the following figure
 We can even write like this
//printk("<7>""hello world\n")

module_init(hello_init);
module_exit(hello_exit);
Last one starts with us MODULE_LICENSE Same as standard, the kernel has given me a defined number interface, we just need to use it

Static is also required because there are so many drivers in the kernel that you can't guarantee your name is different from others, so it's better to add static.

Keywords: Linux

Added by daveeboi on Sun, 26 Sep 2021 19:19:34 +0300