The first routine that will be encountered when learning Linux device drivers:
de>#include <linux/init.h>#include <linux/module.h> MODULE_LICENSE ("Dual BSD/GPL");
static int hello_init(void){printk(KERN_ALERT "Hello, Tekkaman Ninja !\ ");return 0;}
static void Hello_exit(void){printk(KERN_ALERT "Goodbye, Tekkaman Ninja !\ Love Linux !Love ARM ! Love KeKe !de>de>\ de>de>"de>de>de>de>);}
module_init(hello_init);module_exit(hello_exit);de>
I copied it to my working directory and wrote a simple Makefile:
de>KERNELDIR = /home/tekkaman/working/SBC2440/linux-2.6.22.2# The current directory is passed to sub-makes as argumentPWD := $(shell pwd)INSTALLDIR = /home/tekkaman/working/rootfs/lib/modules
CROSS_COMPILE =/home/tekkaman/working/crosstool-gcc410-k26222/gcc-4.1.0-glibc-2.3.2/arm-9tdmi-linux-gnu/bin/arm-9tdmi-linux-gnu-CC = $(CROSS_COMPILE)gcc
obj-m := hello.o
modules:$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:cp hello.ko $( INSTALLDIR)
clean:rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
.PHONY: modules modules_install cleande>
To be honest, the above is my reference to the "Linux device driver (3rd Edition)" Makefile source code modified. I don't know much about Makefile, it is time to study and learn!
Then are make modules, make modules_install .
[root@Tekkaman-Ninja Helloworld]# make modulesmake -C /home/tekkaman/working/SBC2440/linux-2.6.22.2 M=/home/tekkaman/working/Linuxdriver/Helloworld modulesmake[1]: Entering directory `/home/tekkaman/working/SBC2440/linux-2.6.22.2'CC [M] /home/tekkaman/working/Linuxdriver/Helloworld/hello.oBuilding modules, stage 2.MODPOST 1 modulesCC /home/tekkaman/working /Linuxdriver/Helloworld/hello.mod.oLD [M] /home/tekkaman/working/Linuxdriver/Helloworld/hello.komake[1]: Leaving directory `/home/tekkaman/working/SBC2440/linux-2.6.22.2'[ ,null,null,3],Root@Tekkaman-Ninja Helloworld]# make modules_installcp hello.ko /home/tekkaman/working/rootfs/lib/modules[root@Tekkaman-Ninja Helloworld]#
Operation on my development board:[ ,null,null,3],Tekkaman2440@SBC2440V4]#cd /lib/modules/
[Tekkaman2440@SBC2440V4]#ls
cs89x0.ko hello.ko p80211.ko prism2_usb.ko
[Tekkaman2440 @SBC2440V4]#insmod hello.ko
Hello, Tekkaman Ninja !
[Tekkaman2440@SBC2440V4]#lsmod
Module Size Used by Not tainted
hello 1376 0
[Tekkaman2440@SBC2440V4]#rmmod hello
Goodbye, Tekkaman Ninja !
Love Linux !Love ARM ! Love KeKe !
[Tekkaman2440@SBC2440V4]#lsmod
Module Size Used by Not tainted
[Tekkaman2440@SBC2440V4 ]#
------------------------------------------ -------------------------------------- Learning experience: (1) the driver module runs in kernel space The runtime cannot depend on any function library and module connections, so the function called when writing the driver can only be a function that is part of the kernel. (2) An important difference between the driver module and the application program is that the application exits regardless of resource release or other cleanup work, but the module's exit function must carefully revoke everything the initialization function does, otherwise, before the system reboots Something will remain in the system. (3) The various working modes (levels) of the processor are actually designed for the user space and kernel space of the operating system
. Only two levels are used in the operating system
of the Unix class: the highest and lowest levels. (4) Pay great attention to the concurrent processing of the driver. (5) The function of the kernel API with double underscore (_ _), usually the underlying component of the interface, should be used with caution. (6) The kernel code cannot implement floating point book operations.
(7)Makefile analysis: de>obj-m := hello.o represents the module we are constructing is named hell.ko, make will automatically find the hell.c file in this directory to compile . If de>de>hello.o is generated by another source file (such as file1.c and de>de>file2.cde>de>), add it below (note the correspondence of the red font): de> De>de>de style="color: rgb(255, 1, 2);">hellode>de>-objs := file1.o file2.o ......de>
de> $(MAKE) -C $(KERNELDIR) M=$(PWD) modules where de>de>-C $(KERNELDIR) specifies the location of the kernel source code, which holds the kernel's top-level makefile. De>de>M=$(PWD) de>de> specifies the location of the module source code de>de>modules target points to the module set in the obj-m variable.
de>(8)insmod uses a common kernel symbol table to resolve undefined symbols in the module. The public kernel symbol table contains all the global kernel entries (the addresses of functions and variables), which are necessary to implement modular drivers. (9) Linux uses module stacking technology, we can divide the module into multiple layers, and shorten the development cycle by simplifying each layer. If a module needs to pass symbols to other modules, use the following macro:
de>EXPORT_SYMBOL(name);EXPORT_SYMBOL_GPL(name);de> symbols must be exported in the global variables section of the module file, as these two Macros will be extended to a declaration of a special variable, which must be global.
(10) All module code contains the following two header files:
de>#include <linux/init.h>#include <linux/module.h> de>
(11) All module codes should specify the license used:
Basic Concepts Virtualization allows multiple virtual machines with different operating systems to
Git as the worlds most advanced distributed version control system (no one), presumably many people
win7 There are a lot of tricks, if used well, can make the win7 system get no small optimization. No
Executing programs are called processes. When a program is stored in an executable file and is runni
Windows and Linux dual system uninstall Linux system
The problem of creating DBCA error problem under Linux operating system
Linux operating system firewall configuration modification instruction set
The uses and differences between Linux bashrc and profile
Linux Chinese dialog box garbled solution under Linux
Introduction to Linux operating system network configuration process under virtual machine
Win10 camera can not be opened?
Ghost 8.0 operation skills from entry to the master
Added WIMBoot support WinNTSetup 3.5 official release
How to properly remove the explorer.exe virus of Win7 system?
How to set up an application that can access calendars in Win10
Why w7 hide telnet command How to set
Windows7 Tips: Quickly copy the contents of the folder
6 tips for the Win10 system start menu do you want to know?
Windows8 system native Microsoft Pinyin input method detailed