Basic concepts about character device driver

The first step in writing drivers: the ability for users to program custom drivers supplied (mechanism)!

 

a, module runs in kernel space

Applications run in user space

   

B, the runtime module, stack allocated small note stack overflow;

   

C function, having two underscore prefix (__), typically underlying the interface components, need to be careful to use;

   

d, the kernel symbol table

Public kernel symbol table for the undefined symbols in the interpretation module, which contains all of the global kernel items (functions and variables)

Address; export kernel symbol table to be used, you can establish the dependencies between modules (modules laminate technology);

If a module needs to export symbols to other modules:

. 1 EXPORT_SYMBOL (name);                 // (name must be a global variable) derived symbols
2 EXPORT_SYMBOL_GPL (name);         // symbols are for use under GPL license module

   

e, all modules are contained in headers

. 1 #include <Linux / the module.h>         // contains a large number of defined functions and symbols
2 #include <Linux / init.h>                 // function specified entrance
. 3 #include <Linux / moduleparam.h>         // Optional: with function parameters passed to the module in the definition of

   

f, specify a code license

1 MODULE_LICENSE("GPL");

   

g, using #include <linux / errno.h> error codes defined in the return values of function.

   

h, only the system call name with "sys_" prefix ago

 

Makefile

a, a source file

obj-m += src.o

 

b, a plurality of source files

obj-m + = src.o
module-objs : = src1.o src1.o

 

c, Example

1 KERN_DIR = /work/system/linux-2.6.22.6
2
3 all:
4         make -C $(KERN_DIR) M=`pwd` modules
5

6 clean:
7         make -C $(KERN_DIR) M=`pwd` modules clean
8
        rm -rf modules.ord
er
9
10 obj-m += src.o 

   

Loading and unloading module

Loading module: insmod

modprobe

Difference between the two: modprobe to load the module will consider whether the reference number of the current kernel symbol does not exist; if there is such a reference, modprobe will look for other module defines symbols in the current module search path, and load these modules to kernel.

 

Uninstall module: rmmod

   

Lists the currently loaded modules: lsmod

 

insmod * .i

module_init(fun_init)

1 static int __init fun_init(void)
2 {
3         ......
4 }
5 module_init(fun_init);

 

rmmod * .i

module_exit(fun_exit);

1 static int __exit fun_exit(void)
2 {
3         ......
4 }
5 module_exit(fun_exit);

 

During initialization error handling:

① check the return value of the function, the function to ensure the smooth implementation;

② After the error, timely revocation operation has been successfully performed (release the memory occupied);

③ can goto error handling;

   

   

Passing parameters to the module

A well-designed module can be configured when loading.

 

insmod hellop howmany=10 whom="Mom"

   

The method of definition module may pass parameters:

driver.c

1 ......
2 static char *whom = "world";
3 static int howmany = 1;

4 ......
5 module_param(howmany, int, S_IRUGO);
6 module_param(whom, charp, S_IRUGO);
7 ......

   

Supported parameter types:

(bool, invbool, charp, int, long, short, uint, ulong, ushort)

   

   

An array of transfer parameters in a manner

1 module_param_array(name, type, num, perm);

Guess you like

Origin www.cnblogs.com/lilto/p/11876797.html