About Me

My photo

Contract Software Engineer with over eighteen years experience in real-time embedded systems within the defence, aviation, medical, transportation and games sectors. Graduated from the University of Essex with a B.Sc. Computer Science Hons, Artificial Intelligence. Currently living in Horsham, West Sussex with my Brazilian wife and two young sons.

Specialised in the development of C/C++ real-time and embedded systems for Linux, Linux Kernel and Device Driver development, Android, Python, Java, SQLite, OpenGL ES and NFC (Near Field Communication) predominantly contactless cashless payment (Visa payWave, MasterCard and American Express), smart cards and Wi-Fi utilising agile and test driven development.

Developing technology to transform your life!

PayPal.co.uk

Wednesday, 27 May 2015

Kernel Magick: Creating a Dummy Driver - File Operations

The Linux Kernel is a monolithic operating system initially conceived and created by Linus Torvalds in 1991. Functionally it is one of the best operating systems ever created what makes it the best is the fact that it is open-source and released under the GNU General Public License version 2 (GPLv2).

This is the seventh part Linux tutorial for building and installing a device driver for the official Ubuntu 10.04 LTS kernel from the source code. This is a simple device driver with an entry and exit point with no functionality. I have included the macro and functions needed to set up the basic file operations.

1. To open a terminal:

CTRL + ALT + T

2. To create a working directory:

$ mkdir ex07

3. To change the directory into the working directory:

$ cd ex07

4. To launch our favourite text editor from the terminal:

$ vim ex07_module.c

5. To copy and paste the following source code:

#include <linux/init.h>
#include <linux/module.h>

MODULE_AUTHOR("Kyle Wickens");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("ex07 - kylewickens.blogspot.co.uk");
MODULE_VERSION("7.1");


int count = 1;
module_param(count, int, 0644);
MODULE_PARM_DESC(count, "maximum number of iterations");

int ex07_open(struct inode *pinode, struct file *pfile)
{
    printk(KERN_ALERT "Inside the %s function\n", __FUNCTION__);
    return 0;
}

ssize_t ex07_read(struct file *pfile, char __user *buffer, size_t length, loff_t *offset)
{
    printk(KERN_ALERT "Inside the %s function\n", __FUNCTION__);
    return 0;
}

ssize_t ex07_write(struct file *pfile, const char __user *buffer, size_t length, loff_t *offset)
{
    printk(KERN_ALERT "Inside the %s function\n", __FUNCTION__);
    return length;
}

int ex07_close(struct inode *pinode, struct file *pfile)
{
    printk(KERN_ALERT "Inside the %s function\n", __FUNCTION__);
    return 0;
}

struct file_operations ex07_file_operations = {
    .owner   = THIS_MODULE,
    .open    = ex07_open,
    .read    = ex07_read,
    .write   = ex07_write,
    .release = ex07_close,
};

__init int ex07_simple_module_init(void)
{
    printk(KERN_ALERT "Inside the %s function\n", __FUNCTION__);
/* register with the kernel and indicate that we are registering a character driver */
    register_chrdev(247 /* major number */,
        "Simple Character Driver" /* name of the driver */,
        &ex07_file_operations);

    return 0;
}

__exit void ex07_simple_module_exit(void)
{
    printk(KERN_ALERT "Inside the %s function\n", __FUNCTION__);
/* unregister the character device driver */
    unregister_chrdev(247, "Simple Character Driver");
}

module_init(ex07_simple_module_init);
module_exit(ex07_simple_module_exit);


6. To launch our favourite text editor from the terminal:

$ vim Makefile

7. To copy and paste the following source code:

obj-m := ex07_module.o

8. To build the kernel module from the source files:

$ make -C ../linux-source-code M=${PWD} modules

9. To load the kernel module:

$ sudo insmod ./ex07_module.ko

10. To check that the kernel module has been loaded:

$ lsmod | grep ex07_module

11. To test the functionality of the kernel module:

$ cat /proc/devices | grep 'Simple Character Driver'
$ sudo mknod -m 666 /dev/simple_char_device c 247 0
$ ls -l /dev/simple_char_device
$ cat /dev/simple_char_device
$ echo 'kylewickens.blogspot.co.uk' > /dev/simple_char_device


12. To remove the kernel module:

$ sudo rmmod ex07_module

13. To check that the kernel module has been removed:

$ lsmod | grep ex07_module

14. To clean the kernel module source files:

$ make -C ../linux-source-code M=${PWD} clean

15. Have fun!

16. Please consider a contribution if this article helped you.

#include <linux/init.h> – first of must have include files for kernel module development.

#include <linux/module.h> – second of must have include files for kernel module development.

MODULE_AUTHOR - is a macro that is used to declare the modules author.

MODULE_LICENSE - is a macro that is used to declare how the code is licensed.

MODULE_VERSION - is a macro that is used to declare the modules version.

MODULE_DESCRIPTION - is a macro that is used to describe what the module actually does.

MODULE_PARM_DESC - is a macro that is used to describe what the arguments that the module can take.

printk – is the kernel formatted print function and behaves almost identically to the printf function. It can be called from just about anywhere in the kernel at any time: interrupt or process context, while a lock is being held. The messages are written to the kernel log buffer which is circular buffer. The default size is 16KB on uniprocessor machines.

KERN_ALERT – is a macro used to define the loglevels for printk. The loglevels are used to decide whether to print the message to the kernel log buffer. KERN_ALERT specifies a problem that requires immediate attention.

__FUNCTION__ – is a macro used to hold the name of the current function as a literal string.

THIS_MODULE - is a macro used to define the ownership of the module.

module_init – is a macro that calls the specified function either called during do_initcalls or at the module insertion using the insmod command.

module_exit – is a macro that calls the specified function during the driver clean-up when the rmmod command used. If the module is statically linked to the kernel then it has no effect and isn't built into the module.

module_param - is a macro that specifies the variable that takes the command line parameter, its type and permissions.

0644 - is the permission for the specified command line parameter (-rw-r--r--).

__init - is a macro declares that the function as only used during the initialization process, thus giving the kernel the possibility to free the resource after initialization.

__exit - is a macro declares that the function as only used during the clean-up process. If the module is statically linked to the kernel then it has no effect and isn't built into the module.

register_chrdev - is a function that registers the major number and name of the module under the special device driver list (/dev).

unregister_chrdev - is a function that is used to unregistered the module from the special device driver list (/dev).

struct file_operations - is a structure that is used to declare and holds pointers to functions defined by the driver that perform various operations on the device.

Sunday, 24 May 2015

Kernel Magick: Creating a Dummy Driver - Parameters

The Linux Kernel is a monolithic operating system initially conceived and created by Linus Torvalds in 1991. Functionally it is one of the best operating systems ever created what makes it the best is the fact that it is open-source and released under the GNU General Public License version 2 (GPLv2).

This is the sixth part Linux tutorial for building and installing a device driver for the official Ubuntu 10.04 LTS kernel from the source code. This is a simple device driver with an entry and exit point with no functionality. I have included the macro to pass command line parameters into a kernel module.

1. To open a terminal:

CTRL + ALT + T

2. To create a working directory:

$ mkdir ex06

3. To change the directory into the working directory:

$ cd ex06

4. To launch our favourite text editor from the terminal:

$ vim ex06_module.c

5. To copy and paste the following source code:

#include <linux/init.h>
#include <linux/module.h>

MODULE_AUTHOR("Kyle Wickens");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("ex06 - kylewickens.blogspot.co.uk");
MODULE_VERSION("6.1");


int count = 1;
module_param(count, int, 0644);
MODULE_PARM_DESC(count, "maximum number of iterations");

__init int ex06_module_init(void)
{
    int i = 0;

    printk(KERN_ALERT "Inside the %s function\n", __FUNCTION__);

    for (i = 0; i < count; i ++) {
        printk(KERN_ALERT "Index = %d\n", i);
    }

    return 0;
}

__exit void ex06_module_exit(void)
{
    int i = 0;
    printk(KERN_ALERT "Inside the %s function\n", __FUNCTION__);

    for (i = 0; i < count; i ++) {
        printk(KERN_ALERT "Index = %d\n", i);
    }
}

module_init(ex06_module_init);
module_exit(ex06_module_exit);


6. To launch our favourite text editor from the terminal:

$ vim Makefile

7. To copy and paste the following source code:

obj-m := ex06_module.o

8. To build the kernel module from the source files:

$ make -C ../linux-source-code M=${PWD} modules

9. To check that the kernel module has been loaded:

$ sudo insmod ./ex06_module.ko
$ lsmod | grep ex06_module

$ cat /sys/module/ex06_module/parameters/count
$ ls -al /sys/module/ex06_module/parameters/count

$ sudo rmmod ex06_module
$ lsmod | grep ex06_module
$ sudo modinfo ex06_module.ko

$ sudo insmod ./ex06_module.ko count=5
$ lsmod | grep ex06_module

$ cat
/sys/module/ex06_module/parameters/count
$ ls -al $
/sys/module/ex06_module/parameters/count
$ sudo modinfo
ex06_module.ko

$ sudo rmmod
ex06_module
$ lsmod | grep
ex06_module

$ sudo insmod ./
ex06_module.ko
$ lsmod | grep
ex06_module

$ echo 6 | sudo tee /sys/module/
ex06_module/parameters/count
$ cat /sys/module/
ex06_module/parameters/count
$ ls -al /sys/module/
ex06_module/parameters/count
$ sudo modinfo
ex06_module.ko

$ sudo rmmod
ex06_module
$ lsmod | grep
ex06_module

10. To clean the kernel module source files:

$ make -C ../linux-source-code M=${PWD} clean

11. Have fun!

12. Please consider a contribution if this article helped you.

#include <linux/init.h> – first of must have include files for kernel module development.

#include <linux/module.h> – second of must have include files for kernel module development.

MODULE_AUTHOR - is a macro that is used to declare the modules author.

MODULE_LICENSE - is a macro that is used to declare how the code is licensed.

MODULE_VERSION - is a macro that is used to declare the modules version.

MODULE_DESCRIPTION - is a macro that is used to describe what the module actually does.

MODULE_PARM_DESC - is a macro that is used to describe what the arguments that the module can take.

printk – is the kernel formatted print function and behaves almost identically to the printf function. It can be called from just about anywhere in the kernel at any time: interrupt or process context, while a lock is being held. The messages are written to the kernel log buffer which is circular buffer. The default size is 16KB on uniprocessor machines.

KERN_ALERT – is a macro used to define the loglevels for printk. The loglevels are used to decide whether to print the message to the kernel log buffer. KERN_ALERT specifies a problem that requires immediate attention.

__FUNCTION__ –  is a macro used to hold the name of the current function as a literal  string.

module_init – is a macro that calls the specified function either called during do_initcalls or at the module insertion using the insmod command.

module_exit – is a macro that calls the specified function during the driver clean-up when the rmmod command used. If the module is statically linked to the kernel then it has no effect and isn't built into the module.

module_param - is a macro that specifies the variable that takes the command line parameter, its type and permissions.

0644 - is the permission for the specified command line parameter (-rw-r--r--).

__init - is a macro declares that the function as only used during the initialization process, thus giving the kernel the possibility to free the resource after initialization.

__exit - is a macro declares that the function as only used during the clean-up process. If the module is statically linked to the kernel then it has no effect and isn't built into the module.

Wednesday, 20 May 2015

Kernel Magick: Creating a Dummy Driver - Exporting Functions

The Linux Kernel is a monolithic operating system initially conceived and created by Linus Torvalds in 1991. Functionally it is one of the best operating systems ever created what makes it the best is the fact that it is open-source and released under the GNU General Public License version 2 (GPLv2).

This is the fifth part Linux tutorial for building and installing a device driver for the official Ubuntu 10.04 LTS kernel from the source code. This is a simple device driver with an entry and exit point with no functionality. I have included the macro to export functions to external modules.

1. To open a terminal:

CTRL + ALT + T

2. To create a working directory:

$ mkdir ex05

3. To change the directory into the working directory:

$ cd ex05

4. To launch our favourite text editor from the terminal:

$ vim ex05_using_module.c

5. To copy and paste the following source code:

#include <linux/init.h>
#include <linux/module.h>

MODULE_AUTHOR("Kyle Wickens");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("ex05 - using - kylewickens.blogspot.co.uk");
MODULE_VERSION("5.1");


int ex05_module_function(void);

__init int ex05_module_init(void)
{
    printk(KERN_ALERT "Inside the %s function\n", __FUNCTION__);
    ex05_simple_module_function();

    return 0;
}

void ex05_simple_exit(void)
{
    printk(KERN_ALERT "Inside the %s function\n", __FUNCTION__);
}

module_init(ex05_module_init);
module_exit(ex05_module_exit);


6. To launch our favourite text editor from the terminal:

$ vim ex05_export_module.c

7. To copy and paste the following source code:

#include <linux/init.h>
#include <linux/module.h>

MODULE_AUTHOR("Kyle Wickens");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("ex05 - export - kylewickens.blogspot.co.uk");
MODULE_VERSION("5.1");

int ex05_module_function(void)
{
    printk(KERN_ALERT "Inside the %s function\n", __FUNCTION__);

    return 0;
}


__init int ex05_module_init(void)
{
    printk(KERN_ALERT "Inside the %s function\n", __FUNCTION__);

    return 0;
}

__exit void ex05_module_exit(void)
{
    printk(KERN_ALERT "Inside the %s function\n", __FUNCTION__);
}

module_init(ex05_module_init);
module_exit(ex05_module_exit);


EXPORT_SYMBOL(ex05_module_function);

8. To launch our favourite text editor from the terminal:

$ vim Makefile

9. To copy and paste the following source code:

obj-m := ex05_export_module.o
obj-m += ex05_using_module.o


10. To build the kernel module from the source files:

$ make -C ../linux-source-code M=${PWD} modules

11. To check that the kernel module has been loaded:

$ sudo insmod ex05_using_module.ko
$ lsmod | grep ex05_using_module

$ sudo insmod ex05_export_module.ko
$ lsmod | grep ex05_export_module

$ sudo insmod ex05_using_module.ko
$ lsmod | grep ex05_using_module


12. To check that the kernel module has been removed:

$ sudo rmmod ex05_using_module.ko
$ lsmod | grep ex05_using_module

$ sudo rmmod ex05_export_module.ko
$ lsmod | grep ex05_export_module


13. To clean the kernel module source files:

$ make -C ../linux-source-code M=${PWD} clean

14. Have fun!

15. Please consider a contribution if this article helped you.

#include <linux/init.h> – first of must have include files for kernel module development.

#include <linux/module.h> – second of must have include files for kernel module development.

MODULE_AUTHOR - is a macro that is used to declare the modules author.

MODULE_LICENSE - is a macro that is used to declare how the code is licensed.

MODULE_VERSION - is a macro that is used to declare the modules version.

MODULE_DESCRIPTION - is a macro that is used to describe what the module actually does.

EXPORT_SYMBOL - is a macro that is used to export a function so that it can be used by different modules.

EXPORT_SYMBOL_GPL - is a macro that is used to export a function so that it can be used by different GPL compliant modules.

printk – is the kernel formatted print function and behaves almost identically to the printf function. It can be called from just about anywhere in the kernel at any time: interrupt or process context, while a lock is being held. The messages are written to the kernel log buffer which is circular buffer. The default size is 16KB on uniprocessor machines.

KERN_ALERT – is a macro used to define the loglevels for printk. The loglevels are used to decide whether to print the message to the kernel log buffer. KERN_ALERT specifies a problem that requires immediate attention.

__FUNCTION__ –  is a macro used to hold the name of the current function as a literal  string.

module_init – is a macro that calls the specified function either called during do_initcalls or at the module insertion using the insmod command.

module_exit – is a macro that calls the specified function during the driver clean-up when the rmmod command used. If the module is statically linked to the kernel then it has no effect and isn't built into the module.

__initdata - is a macro that declares the the initializated data as only used during the initialization process, thus giving the kernel the possibility to free the resource after initialization.

__init - is a macro declares that the function as only used during the initialization process, thus giving the kernel the possibility to free the resource after initialization.

__exit - is a macro declares that the function as only used during the clean-up process. If the module is statically linked to the kernel then it has no effect and isn't built into the module.

Monday, 18 May 2015

Kernel Magick: Creating a Dummy Driver - Discarded Functions and Variables

The Linux Kernel is a monolithic operating system initially conceived and created by Linus Torvalds in 1991. Functionally it is one of the best operating systems ever created what makes it the best is the fact that it is open-source and released under the GNU General Public License version 2 (GPLv2).

This is the fourth part Linux tutorial for building and installing a device driver for the official Ubuntu 10.04 LTS kernel from the source code. This is a simple device driver with an entry and exit point with no functionality. I have included macros to discard functions and/or variables after initialization or not to include the function if the kernel modules are statically linked.

1. To open a terminal:

CTRL + ALT + T

2. To create a working directory:

$ mkdir ex04

3. To change the directory into the working directory:

$ cd ex04

4. To launch our favourite text editor from the terminal:

$ vim ex04_module.c

5. To copy and paste the following source code:

#include <linux/init.h>
#include <linux/module.h>

MODULE_AUTHOR("Kyle Wickens");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("ex04 - kylewickens.blogspot.co.uk");
MODULE_VERSION("4.1");

__initdata int count = 10;

__init int ex04_module_function1(void)
{
    printk(KERN_ALERT "Inside the %s function\n", __FUNCTION__);
}

int ex04_module_function2(void)
{
    printk(KERN_ALERT "Inside the %s function\n", __FUNCTION__);
}

__init int ex04_module_init(void)
{
    int i = 0;

    printk(KERN_ALERT "Inside the %s function\n", __FUNCTION__);
    for (i = 0; i < count; i++) {
        printk(KERN_ALERT "Index = %d\n", i);
    }

    ex04_module_function1();
    ex04_module_function2();

    return 0;
}

__exit void ex04_module_exit(void)
{
    printk(KERN_ALERT "Inside the %s function\n", __FUNCTION__);


    ex04_module_function2();
}

module_init(ex04_module_init);
module_exit(ex04_module_exit);


6. To launch our favourite text editor from the terminal:

$ vim Makefile

7. To copy and paste the following source code:

obj-m := ex04_module.o

8. To build the kernel module from the source files:

$ make -C ../linux-source-code M=${PWD} modules

9. To load the kernel module:

$ sudo insmod ./ex04_module.ko

10. To check that the kernel module has been loaded:

$ lsmod | grep ex04_module

11. To remove the kernel module:

$ sudo rmmod ex04_module

12. To check that the kernel module has been removed:

$ lsmod | grep ex04_module

13. To clean the kernel module source files:

$ make -C ../linux-source-code M=${PWD} clean

14. Have fun!

15. Please consider a contribution if this article helped you.

#include <linux/init.h> – first of must have include files for kernel module development.

#include <linux/module.h> – second of must have include files for kernel module development.

MODULE_AUTHOR - is a macro that is used to declare the modules author.

MODULE_LICENSE - is a macro that is used to declare how the code is licensed.

MODULE_VERSION - is a macro that is used to declare the modules version.

MODULE_DESCRIPTION - is a macro that is used to describe what the module actually does.

printk – is the kernel formatted print function and behaves almost identically to the printf function. It can be called from just about anywhere in the kernel at any time: interrupt or process context, while a lock is being held. The messages are written to the kernel log buffer which is circular buffer. The default size is 16KB on uniprocessor machines.

KERN_ALERT – is a macro used to define the loglevels for printk. The loglevels are used to decide whether to print the message to the kernel log buffer. KERN_ALERT specifies a problem that requires immediate attention.

__FUNCTION__ –  is a macro used to hold the name of the current function as a literal  string.

module_init – is a macro that calls the specified function either called during do_initcalls or at the module insertion using the insmod command.

module_exit – is a macro that calls the specified function during the driver clean-up when the rmmod command used. If the module is statically linked to the kernel then it has no effect and isn't built into the module.

__initdata - is a macro that declares the the initializated data as only used during the initialization process, thus giving the kernel the possiblity to free the resource after initialization.

__init - is a macro declares that the function as only used during the initialization process, thus giving the kernel the possibility to free the resource after initialization.

__exit - is a macro declares that the function as only used during the clean-up process. If the module is statically linked to the kernel then it has no effect and isn't built into the module.

Thursday, 14 May 2015

Kernel Magick: Creating a Dummy Driver - Module License

The Linux Kernel is a monolithic operating system initially conceived and created by Linus Torvalds in 1991. Functionally it is one of the best operating systems ever created what makes it the best is the fact that it is open-source and released under the GNU General Public License version 2 (GPLv2).

This is the second part Linux tutorial for building and installing a device driver for the official Ubuntu 10.04 LTS kernel from the source code. This is a simple device driver with an entry and exit point with no functionality. I have included macros to declare the author, software license, version and description.

1. To open a terminal:

CTRL + ALT + T

2. To create a working directory:

$ mkdir ex03

3. To change the directory into the working directory:

$ cd ex03

4. To launch our favourite text editor from the terminal:

$ vim ex03_module.c

5. To copy and paste the following source code:

#include <linux/init.h>
#include <linux/module.h>

MODULE_AUTHOR("Kyle Wickens");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("ex03 - kylewickens.blogspot.co.uk");
MODULE_VERSION("3.1");

int ex03_module_init(void)
{
    printk(KERN_ALERT "Inside the %s function\n", __FUNCTION__);
    return 0;
}

void ex03_module_exit(void)
{
    printk(KERN_ALERT "Inside the %s function\n", __FUNCTION__);
}

module_init(ex03_module_init);
module_exit(ex03_module_exit);


6. To launch our favourite text editor from the terminal:

$ vim Makefile

7. To copy and paste the following source code:

obj-m := ex03_module.o

8. To build the kernel module from the source files:

$ make -C ../linux-source-code M=${PWD} modules

9. To load the kernel module:

$ sudo insmod ./ex03_module.ko

10. To check that the kernel module has been loaded:

$ lsmod | grep ex03_module

11. To remove the kernel module:

$ sudo rmmod ex03_module

12. To check that the kernel module has been removed:

$ lsmod | grep ex03_module

13. To clean the kernel module source files:

$ make -C ../linux-source-code M=${PWD} clean

14. Have fun!

15. Please consider a contribution if this article helped you.

#include <linux/init.h> – first of must have include files for kernel module development.

#include <linux/module.h> – second of must have include files for kernel module development.

MODULE_AUTHOR - is a macro that is used to declare the modules author.

MODULE_LICENSE - is a macro that is used to declare how the code is licensed.

MODULE_VERSION - is a macro that is used to declare the modules version.

MODULE_DESCRIPTION - is a macro that is used to describe what the module actually does.

printk – is the kernel formatted print function and behaves almost identically to the printf function. It can be called from just about anywhere in the kernel at any time: interrupt or process context, while a lock is being held. The messages are written to the kernel log buffer which is circular buffer. The default size is 16KB on uniprocessor machines.

KERN_ALERT – is a macro used to define the loglevels for printk. The loglevels are used to decide whether to print the message to the kernel log buffer. KERN_ALERT specifies a problem that requires immediate attention.

__FUNCTION__ –  is a macro used to hold the name of the current function as a literal  string.

module_init – is a macro that calls the specified function either called during do_initcalls or at the module insertion using the insmod command.

module_exit – is a macro that calls the specified function during the driver clean-up when the rmmod command used. If the module is statically linked to the kernel then it has no effect.

Tuesday, 12 May 2015

Kernel Magick: Creating a Dummy Driver - Separate Files

The Linux Kernel is a monolithic operating system initially conceived and created by Linus Torvalds in 1991. Functionally it is one of the best operating systems ever created what makes it the best is the fact that it is open-source and released under the GNU General Public License version 2 (GPLv2).

This is the third part Linux tutorial for building and installing a device driver for the official Ubuntu 10.04 LTS kernel from the source code. This is a simple device driver with an entry and exit point with no functionality. I have included changes to the makefile to demonstrate how to compile the driver from multiple source files.

1. To open a terminal:

CTRL + ALT + T

2. To create a working directory:

$ mkdir ex02

3. To change the directory into the working directory:

$ cd ex02

4. To launch our favourite text editor from the terminal:

$ vim ex02_init_module.c

5. To copy and paste the following source code:

#include <linux/init.h>
#include <linux/module.h>

int ex01_module_init(void)
{
    printk(KERN_ALERT "Inside the %s function\n", __FUNCTION__);
    return 0;
}

module_init(ex01_module_init);


6. To launch our favourite text editor from the terminal:

#include <linux/init.h>
#include <linux/module.h>

void ex02_module_exit(void)
{
    printk(KERN_ALERT "Inside the %s function\n", __FUNCTION__);
}

module_exit(ex02_module_exit);


7. To launch our favourite text editor from the terminal:

$ vim Makefile

8. To copy and paste the following source code:

obj-m := ex02_module.o

ex02_module-objs := \
    ex02_init_module.o \
    ex02_exit_module.o


9. To build the kernel module from the source files:

$ make -C ../linux-source-code M=${PWD} modules

10. To load the kernel module:

$ sudo insmod ./ex02_module.ko

11. To check that the kernel module has been loaded:

$ lsmod | grep ex02_module

12. To remove the kernel module:

$ sudo rmmod ex02_module

13. To check that the kernel module has been loaded:

$ lsmod | grep ex02_module

14. To clean the kernel module source files:

$ make -C ../linux-source-code M=${PWD} clean

15. Have fun!

16. Please consider a contribution if this article helped you.

#include <linux/init.h> – first of must have include files for kernel module development.

#include <linux/module.h> – second of must have include files for kernel module development.

printk – is the kernel formatted print function and behaves almost identically to the printf function. It can be called from just about anywhere in the kernel at any time: interrupt or process context, while a lock is being held. The messages are written to the kernel log buffer which is circular buffer. The default size is 16KB on uniprocessor machines.

KERN_ALERT – is a macro used to define the loglevels for printk. The loglevels are used to decide whether to print the message to the kernel log buffer. KERN_ALERT specifies a problem that requires immediate attention.

__FUNCTION__ –  is a macro used to hold the name of the current function as a literal  string.

module_init – is a macro that calls the specified function either called during do_initcalls or at the module insertion using the insmod command.

module_exit – is a macro that calls the specified function during the driver clean-up when the rmmod command used. If the module is statically linked to the kernel then it has no effect.

Monday, 11 May 2015

Kernel Magick: Creating a Dummy Driver - Hello World

The Linux Kernel is a monolithic operating system initially conceived and created by Linus Torvalds in 1991. Functionally it is one of the best operating systems ever created what makes it the best is the fact that it is open-source and released under the GNU General Public License version 2 (GPLv2).

This is the second part Linux tutorial for building and installing a device driver for the official Ubuntu 10.04 LTS kernel from the source code. This is a simple device driver with an entry and exit point with no functionality.

1. To open a terminal:

CTRL + ALT + T

2. To create a working directory:

$ mkdir ex01

3. To change the directory into the working directory:

$ cd ex01

4. To launch our favourite text editor from the terminal:

$ vim ex01_module.c

5. To copy and paste the following source code:

#include <linux/init.h>
#include <linux/module.h>

int ex01_module_init(void)
{
    printk(KERN_ALERT "Inside the %s function\n", __FUNCTION__);
    return 0;
}

void ex01_module_exit(void)
{
    printk(KERN_ALERT "Inside the %s function\n", __FUNCTION__);
}

module_init(ex01_module_init);
module_exit(ex01_module_exit);


6. To launch our favourite text editor from the terminal:

$ vim Makefile

7. To copy and paste the following source code:

obj-m := ex01_module.o

8. To build the kernel module from the source files:

$ make -C ../linux-source-code M=${PWD} modules

9. To load the kernel module:

$ sudo insmod ./ex01_module.ko

10. To check that the kernel module has been loaded:

$ lsmod | grep ex01_module

11. To remove the kernel module:

$ sudo rmmod ex01_module

12. To check that the kernel module has been removed:

$ lsmod | grep ex01_module

13. To clean the kernel module source files:

$ make -C ../linux-source-code M=${PWD} clean

14. Have fun!

15. Please consider a contribution if this article helped you.

#include <linux/init.h> – first of must have include files for kernel module development.

#include <linux/module.h> – second of must have include files for kernel module development.

printk – is the kernel formatted print function and behaves almost identically to the printf function. It can be called from just about anywhere in the kernel at any time: interrupt or process context, while a lock is being held. The messages are written to the kernel log buffer which is circular buffer. The default size is 16KB on uniprocessor machines.

KERN_ALERT – is a macro used to define the loglevels for printk. The loglevels are used to decide whether to print the message to the kernel log buffer. KERN_ALERT specifies a problem that requires immediate attention.

__FUNCTION__ –  is a macro used to hold the name of the current function as a literal  string.

module_init – is a macro that calls the specified function either called during do_initcalls or at the module insertion using the insmod command.

module_exit – is a macro that calls the specified function during the driver clean-up when the rmmod command used. If the module is statically linked to the kernel then it has no effect.