Linux Driver Development-module driver

Source: Internet
Author: User

The Linux kernel is highly customizable and can be customized by configuring the compilation options.

When configuring the kernel compilation option, there are generally three compiler options for the driver, not compiling, compiling kernel-driven, and compiling to module-driven. So Linux drivers are generally divided into two categories, kernel-driven and module-driven, of course, there is no absolute difference between the two; when compiled into kernel-driven, the driver will run with the kernel, which is the same as the intrinsic part of the kernel, and when compiled into a module driver, each driver is a standalone individual and is installed when needed , and then uninstall, you can save hardware resources. The following describes the module-driven development process.

1, to obtain the kernel source code, according to the target device (PC or development Board, etc.) parameters (Arch and cross_compile) compiled source code, the general target device has a corresponding cross-compiler tool chain, so before compiling the kernel source code to build a good compilation environment.

2, write the module driver code, here with the simplest driving framework to explain:

①, driver source files: tsetdriver.c

1#include <linux/init.h>2#include <linux/module.h>3 4 int StaticHello_init (void)//Entry function5 {6PRINTK (Kern_alert"Install tsetdriver.ko\n");7     return 0;8 }9 Ten void StaticHello_exit (void)//Exit Function One { APRINTK (Kern_alert"Exit tsetdriver.ko\n"); - } -  the Module_init (Hello_init);  -Module_exit (Hello_exit);

②, Makefile (the Makefile initial letter of the Tips kernel program must be capitalized):

1 obj-m: = tsetdriver.o23 kdir: = ~/opt/linux-2.6   //compiled kernel source path 4 5 All : 6     Make-c $ (kdir) m=$ (PWD)  cross_compile=linux-fsl-linux-gnueabi- modules  //cross-compile prefix, based on your own modifications  78clean:9     rm-rf. *.cmd *.o *.mod.c *.ko. tmp_versions

3, compile, install, test:

①, compile driver: (Generate *.ko file)

Make

②, install driver, uninstall drive:

Copy the *.ko to the target board and test the on-load operation

[Email protected]/home# insmod Tsetdriver.ko install tsetdriver.ko[email protected]/home# rmmod Tsetdriver.ko exit tsetd River.ko

4. Conclusion:

Driver development is different from application development, the driver's entry function is specified by the macro Module_init (), the parameter is the entry parameter, in the entry function completes the resource request, the hardware device initializes the operation, the export function is specified by the macro Module_exit (), completes the resource release and so on, The above content is the most basic framework of the driver.

      

Linux Driver Development-module driver

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.