Linux kernel device drivers for Linux kernel base notes collation

  • 2020-12-18 02:02:21
  • OfStack

1. Linux kernel driver module mechanism

Statically, the driver module is programmed into the kernel and loaded when the kernel starts
Dynamic loading, the driver module is coded as ko, after the kernel starts, it is loaded when needed

2. Write kernel drivers


#include <linux/module.h>
#include <linux/init.h>
static int __init test_init(void) 
{
return 0; // return 0 Indicates success ,  Add a negative number back to exit the load module 
}
//__init  After the kernel has initialized the driver,   Frees the code instruction space for this function 

static void __exit test_exit(void)
{
....
}
//__exit  Specifies that this function is used only when the driver is unloaded,   Release after use 
module_init(test_init); // The specified test_init Initializes the function for the module 
module_exit(test_exit); // The specified test_exit Offload function for module exit 
MODULE_LICENSE("GPL"); // Specify which protocols are supported 
MODULE_AUTHOR(" The author ");
MODULE_DESCRIPTION(" describe ");
MODULE_VERSION(" version ");
#define __init __section(.init.text)
#define __initdata __section(.init.data)
char __initdata buf[] = "hello world";
#define __exitdata __section(.exit.data)
#define __exit __section(.exit.text)
/////////////

modinfo test.ko View the information for the module

cat /proc/modules View the current system dynamic load module equivalent lsmod

test 1768 0 - Live 0xbf03c000

Module name, size of memory used, number of calls, valid, memory address of module

ls /sys/module View all modules

3. Makefile of the driver module

obj-m += test. o // Source file is test. c modules: ES36en-ES37en kernel source directory M= driver code directory modules modules install: ES42en-ES43en kernel source directory M= directory modules_install INSTALL_MOD_PATH=/ file system path clean: ES51en-ES52en kernel source directory M= driver code directory modules clean

4. View messages from the driver output


cat /var/log/messages
tail /var/log/messages

5. Level control of printk

/usr/src/kernels/2.6.18-194.el5-i686/include/linux/kernel.h


<linux/kernel.h>
#define KERN_EMERG "<0>" /* system is unusable */
#define KERN_ALERT "<1>" /* action must be taken immediately */
#define KERN_CRIT "<2>" /* critical conditions */
#define KERN_ERR "<3>" /* error conditions */
#define KERN_WARNING "<4>" /* warning conditions */
#define KERN_NOTICE "<5>" /* normal but significant condition */
#define KERN_INFO "<6>" /* informational */
#define KERN_DEBUG "<7>" /* debug-level messages */

The default level is KERN_WARNING "<4>"

Use: printk(KERN_INFO"内容");

[

View the current kernel output level cat proc/sys/kernel/printk
7 4 1 7
7:console_loglevel
4:default_message_loglevel
1:minimum_console_loglevel
7:default_console_loglevel

]

When the printk function is used at a lower level than the current level console_loglevel Level, can output, otherwise not output

Modify level output echo 8 > /proc/sys/kernel/printk

conclusion


Related articles: