Linux kernel module introduction, the advantages of using Linux modules

1.1 Introduction to Linux Kernel Modules 1.1.1 Overview of Linux Kernel Modules

In the embedded device driver development, the driver is released in the form of modules, which greatly improves the flexibility of the device. Users only need to get the relevant driver module and insert it into the user's kernel to use it flexibly. Your device.

1.1.2 Advantages of using Linux modules

1. Users can extend the functionality of the Linux system at any time.

2. When you want to modify the driver of the current Linux system, you only need to uninstall the old module, compile the target driver module, and reinstall and insert.

3. If you need to use the new module in the system, you don't have to recompile the kernel, just insert the corresponding module.

4. Reduce the size of the Linux kernel and save flash.

1.2 Getting Started with Linux Modules 1.2.1 Module Related Commands 1.2.1.1 Details of Linux Module Commands

1. Module installation command: insmod

Insmod xxxx.ko

2. View the currently installed module: lsmod

Lsmod does not require parameters

3. Module uninstall command: rmmod

Rmmod xxxxx.ko

4. View module information: modinfo

Operate on X86:

[root@zhifachen linux-3.5]# modinfo/root/work/rootfs/home/mod/tiny4412_hello_module.ko

Filename: /root/work/rootfs/home/mod/tiny4412_hello_module.ko

License: GPL

Depends:

Intree: Y

Vermagic: 3.5.0-FriendlyARM SMP preempt mod_unload ARMv7 p2v8

[root@zhifachen linux-3.5]#

1.2.1.2 Linux Module Command Test Example

Note: The compilation of the module is dependent on a specific source code, and this is compiled, and does not use mrproper, distclean to clear the source project.

1.2.2 Compiling modules in the kernel source tree using makemodules

1. Add kernel menu

2. make menuconfig is configured as M

3. Enter makemodules in the top level directory terminal

Direct make can also generate module files (.ko) and also generate zImage. It is faster to use the make modules command when you only want to compile modules without compiling zImage.

1.2.3 Compiling the module source files in the kernel source tree into the kernel image

If you don't need a configuration menu and just want to compile the code into the kernel zImage, the Makefile can write obj-y += xxxx.o

If you don't need a configuration menu and just want to compile the code as an external module, the Makefile can write obj-m+= xxxx.o

1.2.4 Add a custom module to the kernel source and compile 1.2.5 external stand-alone Makefile compilation module

In the driver development phase, the chances of accessing the new driver are very high. If you want to modify the kernel source Makefile, it is too complicated and not conducive to porting. In actual development, another method will be used to compile the module file.

1.2.5.1 Linux kernel module code file template

You can directly copy the sample template that comes with the kernel source code to test: drivers/char/tiny4412_hello_module.c

1.2.5.2 Linux kernel module compiling Makefile template

Compile the module Makefile:

Obj-m += xyd_hello_module.o
All:
@make -C /root/work/linux-3.5/ M=$(PWD) modules
@rm -rf *.o *.mod.c modules.order Module.symvers *~ *.bak
Clean:
@rm -rf *.o *.mod.c modules.order Module.symvers *.ko *~ *.bak

Make -C $(KDIR)M=$(PWD) modules

The M= option causes the Makefile to return to the module source directory before constructing the modules target, and then the modules target points to the module set in the obj-m variable; in the above example, we set the variable to module.o.

The above clearing rules are written by themselves. You can also use the kernel Makefile cleanup rules to clean up the generated target files. The improved code:

Obj-m += xyd_hello_module.o
# x86 Compile time: uname -r get the current kernel version number
#KDIR := /lib/modules/`uname -r`/build
#arm Use when compiling
KDIR :=/root/work/linux-3.5
All:
@make -C $(KDIR) M=$(PWD) modules
Clean:
@make -C $(KDIR) M=$(PWD) modules clean
@rm -f *.ko.unsigned *~

1.1.1 Linux kernel printk function

The printf function cannot be used to output information in the kernel. To use the printk function, this function has output level control.

The information output by the kernel through printk() has a log level. The log level is controlled by adding an integer with angle brackets before the string output from printk(), such as printk("<6>Hello, world!/n" );. There are eight different log levels available in the kernel, and there are corresponding macros in linux/kernel.h.

#defineKERN_EMERG "<0>" /* system is unusable System not available*/

#defineKERN_ALERT "<1>" /* action must be takenimmediately alert, action must be taken immediately*/

#define KERN_CRIT "<2>" /*critical conditions critical state */

#defineKERN_ERR "<3>" /* error conditions Error status*/

#defineKERN_WARNING "<4>" /* warningconditions warning status*/

#defineKERN_NOTICE "<5>" /* normal but significant normal, but compelling */

#define KERN_INFO "<6>" /*informational Information*/

#defineKERN_DEBUG "<7>" /* debug-level messages debug level information */

You can find the setup information by looking at the contents of the /proc/sys/kernel/printk file.

[root@ChenZhiFa/]# cat /proc/sys/kernel/printk

7 4 1 7

You can modify the levels by the echo command:

[root@ChenZhiFa/]# echo 4 4 1 7 > /proc/sys/kernel/printk

Note: Direct use of printk ("kkkk"); This does not indicate the level, this is not explicitly specified.

Only the level is higher than the current terminal level will be displayed on the terminal.

The following tests are performed on the X86 system (tested in the pure character interface, invalid in the graphical interface)

In the graphical interface system, press Ctl + Alt + F2 (one of the F2~F6 keys, different computers may be different), you can enter the pure character interface, you need to return to press Ctl +Alt + F1 (different computers may be different)

Printk --- does not support floating point numbers, in fact the driver does not support floating point operations. All floating point operations should be placed in user space.

Headphones

We've been around for over 16+ years. We make sure our sound is The Best Sound.
Our products include gaming headset, Bluetooth Earphone, Headphones Noise Cancelling, Best Wireless Earbuds, Bluetooth Mask, Headphones For Sleeping, Headphones in Headband, Bluetooth Beanie Hat, bluetooth for motorcycle helmet, etc
Manufacturing high-quality products for customers according to international standards, such as CE ROHS FCC REACH UL SGS BQB etc.
We help 200+ customers create custom Bluetooth headphones, earbuds, earphones, etc audio products design for various industries.

Customized Headphones, personalized gifts, promotional products custom , Bluetooth Earphones,Best Headphones

TOPNOTCH INTERNATIONAL GROUP LIMITED , https://www.itopnoobluetoothes.com