Concepts related to Linux device driver (1) modules

Source: Internet
Author: User

The simplest kernel module
#include <linux/init.h>#include <linux/module.h>Static int__init Hello_init (void)//kernel initialization function{PRINTK ("Hello world!\n");//Print function, similar to Prinft        return 0; }Static void__exit Hello_exit (void)//kernel cleanup function{PRINTK ("Good bye!\n"); } module_init (Hello_init);//Specify initialization functionModule_exit (Hello_exit);//Specify clear functionModule_license ("GPL");//Specify the license used by the codeModule_author ("Laowu");//Designated authorModule_version ("1.0");//Specify code revision number
Makefile of kernel modules
obj-m += hello.o             KDIR:=~/linux-2.6make -C$(KDIRM=`pwd` modules  
    1. Obj-m + = hello.o shows that there is a kernel module that needs to be constructed from hello.o, and the generated module name is Hello.ko.
      If Hello.ko is generated by two source files, such as file1.c file2.c, it should be written as follows
      Obj-m: = Hello.ko
      HELLO-OBJS: = FILE1.O file2.0
    2. Make-c $ (kdir) m= pwd modules
      First change the directory to the location specified by-C (that is, the kernel source code directory), which holds the top-level makefile file of the kernel, the m= option, let the makefile return to the module source code directory before constructing the modules target
Insertion of the kernel

Insmod Hello.ko

Kernel parameters

How to use the Module_param function:
Module_param (name, type, perm)
Name: Names of kernel parameters, defined by themselves;
Type: Types of kernel parameters, common types Byte, short, int, long, ULONG, BOOL, charp (character pointer);
Perm: The kernel parameter has permission S_irugo (Read access to module parameters). In fact, permissions and file permissions are almost
Methods for passing parameters when the kernel loads the module:
Use command: Insmod Xxx.ko a=1
After the module is loaded, and his permissions are not 0, the corresponding module parameters can be found in the/sys/module/xxx/parameter directory.

Export symbol table

Use Export_symbol to export functions for use by other modules, and the symbol table in the "/proc/kallsyms" file, which records the symbols of functions and the addresses of functions in memory.

Concepts related to Linux device driver (1) modules

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.