0% found this document useful (0 votes)
74 views10 pages

Driver AI

The document discusses creating a Linux kernel module that prints "Hello, world!" when loaded and "Goodbye, world!" when unloaded. It includes sections on module loading/unloading functions, the makefile, the C source code file containing the init and exit functions, example output when compiling, loading and unloading the module using insmod and rmmod, and references.

Uploaded by

Delhi On Road
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views10 pages

Driver AI

The document discusses creating a Linux kernel module that prints "Hello, world!" when loaded and "Goodbye, world!" when unloaded. It includes sections on module loading/unloading functions, the makefile, the C source code file containing the init and exit functions, example output when compiling, loading and unloading the module using insmod and rmmod, and references.

Uploaded by

Delhi On Road
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Device Driver

November, 2016
ACKNOWLEDGEMENT
I would like to acknowledge with thanks and appreciation all the people who played a role in the
successful completion of this project and to whom I shall remain grateful and indebted.
My sincere thanks go to my mentor, who accepted the challenge to guide me throughout the stages
of this project. Their proper vision of what they want along with their valuable corrections on my
work, significantly improved the quality of the final outcome.
Table of content
1) Introduction .................................................................................................................................... 4
1.1) Module .....................................................................................................................................4
2) Functions of Module Loading and Unloading.................................................................................4
3) Make file .........................................................................................................................................5
4) hello_printk.c ..................................................................................................................................5
5) output ...............................................................................................................................................7
6) Reference........................................................................................................................................10
1) Introduction
Linux is a monolithic kernel. And, the driver for it should be compiled together with the kernel
itself or should be implemented in the form of a kernel module to avoid the recompiling of the
kernel when driver adding is needed.

1.1) Module: A module is an object file prepared in a special way. The Linux kernel can load a
module to its address space and link the module with itself. The Linux kernel is written in 2
languages: C and assembler (the architecture dependent parts). The development of drivers for
Linux OS is possible only in C and assembler languages. The module code is executed in the kernel
context. It rests some additional responsibility in the developer: if there is an error in the user level
program, the results of this error will affect mainly the user program; if an error occurs in the kernel
module, it may affect the whole system. The kernel and its modules are built into a practically
single program module.

2) Functions of Module Loading and Unloading

The below written code that is required for the creation of module is very simple.
#include <linux/init.h>
#include <linux/module.h>
static int my_init(void)
{ return 0;
}
static void my_exit(void)
{ return;
}
module_init(my_init);
module_exit(my_exit);

This code does not do anything but allowing loading and unloading the module. When loading the
driver, the my_init function is called; when unloading the driver, the my_exit function is
called. We inform the kernel about it with the help of the module_init and module_exit
macros.

3) Make file

obj-m := hello_printk.o
# obj-m is a list of what kernel modules to build. The .o and other
# objects will be automatically built from the corresponding .c file -
# no need to list the source files explicitly.

KDIR := /lib/modules/$(shell uname -r)/build


# KDIR is the location of the kernel source. The current standard is
# to link to the associated source tree from the directory containing
# the compiled modules.

PWD := $(shell pwd)


# PWD is the current working directory and the location of our module
# source files.

Default:
$(MAKE) -C $(KDIR) M=$(PWD) modules
# default is the default make target. The rule here says to run make
# with a working directory of the directory containing the kernel
# source and compile only the modules in the PWD (local) directory.

4) hello_printk.c
/*
* The below are header files provided by the kernel which are
* required for all modules. They include things like the definition
* of the module_init() macro.
*/
#include <linux/init.h>
#include <linux/module.h>

/*
* This is the init function, which is run when the module is first
* loaded. The __init keyword tells the kernel that this code will
* only be run once, when the module is loaded.
*/

static int __init


hello_init(void)
{
printk("Hello, world!\n");
return 0;
}

/*
* The below macro informs the kernel as to which function to use as
* the init function.
*/

module_init(hello_init);

/*
* Similary, the exit function is run once, upon module unloading, and
* the module_exit() macro identifies which function is the exit
* function.
*/

static void __exit


hello_exit(void)
{
printk("Goodbye, world!\n");
}

module_exit(hello_exit);

5) Output

//amit@amit-HP-ProBook-445-G1:~/Documents/os project$ make


make -C /lib/modules/4.4.0-21-generic/build M=/home/amit/Documents/os project modules
make[1]: Entering directory '/usr/src/linux-headers-4.4.0-21-generic'
arch/x86/Makefile:148: CONFIG_X86_X32 enabled but no binutils support
Makefile:670: Cannot use CONFIG_CC_STACKPROTECTOR_STRONG: -fstack-protector-
strong not supported by compiler
make[1]: *** No rule to make target 'project'. Stop.
make[1]: Leaving directory '/usr/src/linux-headers-4.4.0-21-generic'
Makefile:12: recipe for target 'default' failed
make: *** [default] Error 2
amit@amit-HP-ProBook-445-G1:~/Documents/os project$ ls
hello_printk.c hello_printk.mod.c hello_printk.o modules.order
hello_printk.ko hello_printk.mod.o Makefile Module.symvers
amit@amit-HP-ProBook-445-G1:~/Documents/os project$ sudo insmod ./hello_printk.ko
[sudo] password for amit:
amit@amit-HP-ProBook-445-G1:~/Documents/os project$ dmesg|tail
[ 3857.486404] cfg80211: Regulatory domain changed to country: IN
[ 3857.486410] cfg80211: DFS Master region: JP
[ 3857.486412] cfg80211: (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp),
(dfs_cac_time)
[ 3857.486415] cfg80211: (2402000 KHz - 2482000 KHz @ 40000 KHz), (N/A, 2000 mBm),
(N/A)
[ 3857.486418] cfg80211: (5170000 KHz - 5250000 KHz @ 80000 KHz, 160000 KHz AUTO),
(N/A, 2000 mBm), (N/A)
[ 3857.486422] cfg80211: (5250000 KHz - 5330000 KHz @ 80000 KHz, 160000 KHz AUTO),
(N/A, 2000 mBm), (0 s)
[ 3857.486424] cfg80211: (5735000 KHz - 5835000 KHz @ 80000 KHz), (N/A, 2000 mBm),
(N/A)
[ 4562.853243] Hello, world!
[ 4580.736956] Goodbye, world!
[ 4679.325300] Hello, world!
amit@amit-HP-ProBook-445-G1:~/Documents/os project$ sudo rmmod hello_printk
amit@amit-HP-ProBook-445-G1:~/Documents/os project$ dmesg|tail
[ 3857.486410] cfg80211: DFS Master region: JP
[ 3857.486412] cfg80211: (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp),
(dfs_cac_time)
[ 3857.486415] cfg80211: (2402000 KHz - 2482000 KHz @ 40000 KHz), (N/A, 2000 mBm),
(N/A)
[ 3857.486422] cfg80211: (5250000 KHz - 5330000 KHz @ 80000 KHz, 160000 KHz AUTO),
(N/A, 2000 mBm), (0 s)
[ 3857.486424] cfg80211: (5735000 KHz - 5835000 KHz @ 80000 KHz), (N/A, 2000 mBm),
(N/A)
[ 4562.853243] Hello, world!
[ 4580.736956] Goodbye, world!
[ 4679.325300] Hello, world!
[ 4706.496191] Goodbye, world!
6) REFERENCES
 StackOverflow.com
 facebook.com/phpdevelopergroup
 youtube.com

You might also like