There is a sample code to explain the process of making a device driver in linux system.
hello_printk.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
int init_module(void)
{
printk(KERN_INFO "Hello world 1.\n");
/*
* A non 0 return means init_module failed; module can't be loaded.
*/
/*
* return 0 means successfully loaded
*/
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Goodbye world 1.\n");
}
Makefile
obj-m += hello_printk.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
To run the files, you need to have a complete linux kernel installed in your system. You can find a list of kernel here: https://www.kernel.org/
For example, I installed linux 4.1.45
command:
$ wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.1.46.tar.xz
Now, you can go to the file folder and run make file.
$ make
You will find there is one .ko file. It is a kernel object file. Now, to add this module in your system, you need to attach it as a root user.
$ sudo insmod ./hello_printk.ko
To see your module in system,
$ cat /proc/modules
Another command to see or display your module with size and uses information:
$ lsmod
To check specific module information:
$ modinfo hello_printk.ko
To remove your module:
$ sudo rmmod hello_printk.ko
By the way, you can't see any text /output in your console. Because, the printk keeps messages in kernel. Hence, we can't see anything in user interface. To get the "Hello world" message, you need to look at kernel messages using 'dmesg' command or
$ dmesg
$ cat /var/log/syslog
Reference:
[1] http://www.tldp.org/LDP/lkmpg/2.6/lkmpg.pdf
[2] http://www.linuxdevcenter.com/pub/a/linux/2007/07/05/devhelloworld-a-simple-introduction-to-device-drivers-under-linux.html
[3] http://freesoftwaremagazine.com/articles/drivers_linux/
hello_printk.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
int init_module(void)
{
printk(KERN_INFO "Hello world 1.\n");
/*
* A non 0 return means init_module failed; module can't be loaded.
*/
/*
* return 0 means successfully loaded
*/
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Goodbye world 1.\n");
}
Makefile
obj-m += hello_printk.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
To run the files, you need to have a complete linux kernel installed in your system. You can find a list of kernel here: https://www.kernel.org/
For example, I installed linux 4.1.45
command:
$ wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.1.46.tar.xz
Now, you can go to the file folder and run make file.
$ make
You will find there is one .ko file. It is a kernel object file. Now, to add this module in your system, you need to attach it as a root user.
$ sudo insmod ./hello_printk.ko
To see your module in system,
$ cat /proc/modules
Another command to see or display your module with size and uses information:
$ lsmod
To check specific module information:
$ modinfo hello_printk.ko
To remove your module:
$ sudo rmmod hello_printk.ko
By the way, you can't see any text /output in your console. Because, the printk keeps messages in kernel. Hence, we can't see anything in user interface. To get the "Hello world" message, you need to look at kernel messages using 'dmesg' command or
cat /var/log/syslog
$ dmesg
$ cat /var/log/syslog
Reference:
[1] http://www.tldp.org/LDP/lkmpg/2.6/lkmpg.pdf
[2] http://www.linuxdevcenter.com/pub/a/linux/2007/07/05/devhelloworld-a-simple-introduction-to-device-drivers-under-linux.html
[3] http://freesoftwaremagazine.com/articles/drivers_linux/
মন্তব্যসমূহ
একটি মন্তব্য পোস্ট করুন