Linux device driver learning (0)-Hello, world! Module

  

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:

Copyright © Windows knowledge All Rights Reserved