You are on page 1of 6

LINUX DEVICE DRIVERS

To write linux device drivers we have to know little knowledge about linux kernel programming
A kernel module has to have at least two functions: init module which is called when the module is
inserted into the kernel, and cleanup module which is called just before it is removed.Typically, init
module either registers a handler for something with the kernel, or it replaces one of the kernel
function with its own code (usually code to do something and then call the original function). The
cleanup module function is supposed to undo whatever init module did, so the module can be
unloaded safely.
mod1.c
/* hello.c
*
* "Hello, world" - the kernel module version.
*/
/* The necessary header files */
/* Standard in kernel modules */
#include <linux/kernel.h>
/* Were doing kernel work */
#include <linux/module.h>
/* Specifically, a module */
/* Initialize the module */
int init_module()
{
printk("Hello, world - this is the kernel speaking\n");
/* If we return a non zero value, it means that
* init_module failed and the kernel module
* cant be loaded */
return 0;
}
/* Cleanup - undid whatever init_module did */
void cleanup_module()
{
printk("Short is the life of a kernel module\n");
}

Makefile
obj-m =hello.o
KDIR= /lib/modules/$(shell uname -r)/build
all:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
rm -rf $(wildcard *.o *.ko *.mod.* .c* .t* test Module.symvers *.order *.marker

mod2.c

/*Module exporting choosen symbols


Author:Emfact
Version:1.0
Tested with: Linux kernel version 2.6.29 also
*/
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/init.h>
/*Kernel module Comments*/
MODULE_AUTHOR("TEAM EMFACT");
MODULE_DESCRIPTION("FIRST MODULE");
MODULE_LICENSE("GPL");
void hello_func(void);
EXPORT_SYMBOL_GPL(hello_func); //func exported
static int val=300;
void hello_func(void)
{
printk("func invoked\n ");
printk(" val = %d",val);
}
int myinit(void)
{
printk(" module inserted\n ");
return 0;
}
void myexit(void)
{
printk(" module removed\n ");
}
module_init(myinit);
module_exit(myexit);
/*Kernel module Comments*/
MODULE_AUTHOR("TEAM EMFACT");
MODULE_DESCRIPTION("FIRST MODULE");
MODULE_LICENSE("GPL and additional rights");
//Note: All the log levels are defined as part of /linux/kernel.h

depmod.c
/*Dep module
Author:Team-c
Version:1.0
Tested with: Linux kernel version 2.6.29 also
*/
#ifndef MODULE
#define MODULE
#endif
#ifndef __KERNEL__
#define __KERNEL__
#endif
#include<linux/module.h>
#include<linux/version.h>
#include<linux/kernel.h>
#include<linux/init.h>
extern void hello_func(void);
static __init int myinit(void)
{
printk(" Calling Kernel symbol \n ");
hello_func();
return 0;
}
static __exit void myexit(void)
{
printk(" cleanup invoked \n");
}
module_init(myinit);
module_exit(myexit);
/*Kernel module Comments*/
MODULE_AUTHOR("TEAM EMFACT");
MODULE_DESCRIPTION("DEP MODULE");
MODULE_LICENSE("GPL");

mod3.c
/*Module exporting choosen symbols
Author:Team-c
Version:1.0
Tested with: Linux kernel version 2.6.29 also
*/
#include<linux/module.h>
#include<linux/kernel.h>

#include<linux/init.h>
#include<linux/stat.h>
#include<linux/moduleparam.h>
static int val=0;
module_param(val, int, S_IRUGO);//read only
//module_param(val,int,S_IRUGO|S_IWUSR);
MODULE_PARM_DESC (val,"INTIALISE ME AT INSERTION TIME");
MODULE_SUPPORTED_DEVICE(NULL);
void func(void);
void func()
{
printk("func invoked\n ");
printk(" val = %d",val);
}
int myinit(void)
{
printk(" module inserted\n ");
func();
return 0;
}
void myexit(void)
{
printk(" module removed\n ");
}
module_init(myinit);
module_exit(myexit);
/*Kernel module Comments*/
MODULE_AUTHOR("TEAM Emfact");
MODULE_DESCRIPTION("FIRST MODULE");
MODULE_LICENSE("DUAL BSD/GPL");
//All module param types are defined in <linux/moduleparam.h>

1.2

Multiple File Kernel Modules

Sometimes it makes sense to divide a kernel module between several source files. In this case, you
need to do the following:
start.c
/* start.c
* Copyright (C) 1999 by Ori Pomerantz
*
* "Hello, world" - the kernel module version.
* This file includes just the start routine
*/
/* The necessary header files */
/* Standard in kernel modules */
#include <linux/kernel.h>
/* Were doing kernel work */
#include <linux/module.h>
/* Specifically, a module */
/* Initialize the module */
int init_module()
{
printk("Hello, world - this is the kernel speaking\n");
/* If we return a non zero value, it means that
* init_module failed and the kernel module
* cant be loaded */
return 0;
}

stop.c
/* stop.c
* Copyright (C) 1999 by Ori Pomerantz
*
* "Hello, world" - the kernel module version. This
* file includes just the stop routine.
*/
/* The necessary header files */
/* Standard in kernel modules */
#include <linux/kernel.h>
/* Were doing kernel work */
#define __NO_VERSION__
#include <linux/module.h>
#include <linux/version.h>
/* This isnt "the" file
* of the kernel module */
/* Specifically, a module */

/* Not included by
* module.h because
* of the __NO_VERSION__ */
/* Cleanup - undid whatever init_module did */
void cleanup_module()
{
printk("Short is the life of a kernel module\n");
}

Makefile
MODULE_NAME = mymodule
SRC

:= start.c stop.c

# Path to target Linux Kernel


KDIR
:= /lib/modules/$(shell uname -r)/build # <--- Fill in with path to kernel you're
compiling against
$(MODULE_NAME)-objs = $(SRC:.c=.o)
obj-m
PWD

:= $(MODULE_NAME).o
:= $(shell pwd)

EXTRA_CFLAGS := -I$(PWD)/src -I$(PWD)/include


all:
$(MAKE) -C $(KDIR) M=$(PWD) modules

You might also like