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
Prerequisites
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;
}char_dev;
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
Open.
Read.
Write.
Close.
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
linux/module.h
linux/kernel.h
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
asm/uaccess.h
To make use of the semaphores as and when needed, we will need
linux/semaphore.h
And as we are going to write a character driver, we will need the following file
linux/cdev.h
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
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
Prerequisites
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;
}char_dev;
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
Open.
Read.
Write.
Close.
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
linux/module.h
linux/kernel.h
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
asm/uaccess.h
To make use of the semaphores as and when needed, we will need
linux/semaphore.h
And as we are going to write a character driver, we will need the following file
linux/cdev.h
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.
Linux Kernel Programming (3rd Edition)
Linux Device Drivers, 3rd Edition
0 comments:
Post a Comment