Writing an Example Driver From Scratch: Chapter-1 Header files

Chapter-1 Header files 

The series of posts explains writing a small example driver right from the scratch. There is no physical device, but we will take an array and consider it as our device for which the driver will be written.
The code is inspired by the example codes provided with the book "The Device Drivers" by Alessandro Rubini
Basic knowledge of kernel programming and c Programming.

The driver being described is for 2.6.x kernels.

We will call the device  char_dev , which will be a structure as follows

struct device {
        char array[100];
        struct semaphore sem;

So our device can hold a maximum of 100 characters and its concurrent access can be controlled using the semaphore "sem".

In this driver we will implement the following 4 operations for the device

As this is a basic driver we will not be implementing any control over the data being accepted from the user.

To begin with let us look at the possible header files we might need to write the driver.
As we are going to write a kernel program and it is going to be a module we would need the following two header files


Next to do the read and write we will make use of the functions copy__to_user and copy_from_user, for these operations we will need the header file


To make use of the semaphores as and when needed, we will need


And as we are going to write a character driver, we will need the following file


So we can begin writing our module by including the required above files

#include < linux/module.h >
#include < linux/kernel.h >
#include < linux/fs.h>
#include < asm/uaccess.h >
#include < linux/semaphore.h >
#include < linux/cdev.h >

The next step is to figure out how do we write the init function. Init function that gets called the moment we insert the module into kernel.

Next Chapter
The init function

Reference books

Linux Kernel Programming (3rd Edition)

Linux Device Drivers, 3rd Edition

Follow by Email