A small summary of the modular programming of Linux drivers

Source: Internet
Author: User
Tags dmesg

This article contains the basics of Linux driver modular programming, including the creation of multi-threading, latency, and makefile

With an example to illustrate

#include <linux/init.h>#include<linux/module.h>#include<linux/kernel.h>#include<linux/sched.h>#include<linux/init.h>#include<linux/timer.h>#include<linux/kthread.h>#include<linux/delay.h>#include<linux/sched.h>#include<linux/completion.h>#include<linux/signal.h>#include<asm/processor.h>#include<asm/system.h>#include<linux/param.h>#include<linux/gpio.h>#include"gpio-sama5d3-test.h"//if there is no corresponding. h file, please note//staticdeclare_completion (my_completion);Static structTask_struct *task;intFlag =0;intMy_function (void*Arg) {//PRINTK ("in%s () \ n", __function__);allow_signal (SIGKILL);//allows threads to receive Sigkill signalsMdelay ( -); PRINTK ("should stop:%d\n", Kthread_should_stop ());  while(!signal_pending (current) &&!kthread_should_stop ())//so that the thread can be killed or it can end in Rmmod.{PRINTK ("Jiffies is%lu\n", jiffies);             Set_current_state (task_interruptible); Schedule_timeout (HZ*5);        These two sentences are for the delay of 5s, within which the processor handles other processes. PRINTK ("should stop:%d\n", Kthread_should_stop ()); } PRINTK ("Leaving my_function\n"); Flag=1; return 0;}Static int__init Sama5d3_gpio_init (void) {PRINTK ("sama5d3 Gpio init!\n") ;Task= Kthread_run (My_function,null,"my_function");//PRINTK ("<1> init wait_for_completion () \ n"); return 0;}Static void__exit Sama5d3_gpio_exit (void){    intret; PRINTK ("sama5d3 Gpio exit!\n"); if(!flag) {        if(!Is_err (Task)) {ret=kthread_stop (Task); PRINTK (Kern_info"First thread function has stopped, return%d\n", ret); }    }//PRINTK ("task_struct:0x%x", Task);Printk"goodbye\n"); return;} Module_init (Sama5d3_gpio_init); Module_exit (Sama5d3_gpio_exit); Module_author ("Huangjjy"); Module_description ("GPIO driver for sama5d3"); Module_license ("GPL");

Makefile as follows:

ifeq ($ (kernelrelease),) #KERNEL_DIR=/home/user/project2/atmel/linux-at91///If cross-compiling, this is the kernel pathKernel_dir? =/lib/modules/$ (Shell uname-r)/buildpwd:=$ (shell pwd) #CROSS_COMPILE=/home/user/project2/atmel/toolchain/gcc-linaro-arm-linux-gnueabihf-4.7-2013.04-20130415_linux/bin/arm-linux-gnueabihf-#CC=$ (cross_compile) GCC//If it is cross-compiling, this is the cross-compilation tool chain name and path
#INCLUDE = $ (PWD)/gpio-sama5d3-test.h//if the. c file has a. h file, which is the path of. h Modules: $ (make)-C $ (kernel_dir) m=$ (PWD) modules #$ (make)-i$ (INCLUDE)-C $ (Kernel_dir) m=$ (PWD) modules//if the. C file has an. h file, use this compilation syntax. Phony:modules Cleanc Lean: $ (make)-C $ (kernel_dir) m=$ (PWD) cleanElseobj-M: = gpio-sama5d3-Test.oendif

After you have finished writing the above two files, save the exit.

# make

The. ko file is generated

# insmod ****.ko-------Mount Drive

#lsmod--------to view mounted drivers

#rmmod ***.ko------Unloading Drive

#dmesg or DMESG | TAIL-20 Viewing drive printing information

#dmesg-C-----Erase printing information

A small summary of the modular programming of Linux drivers

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.