Linux Device Drivers Tutorials
Writing a simple Character Device Driver
In this article we will write a character driver which will support various function like open, close , read and write. In basic character driver structure as we have discussed the components of a driver. following are the steps to write a character driver.
1) write character driver code:- character driver code contains following component
1) Register driver
At the time of init , driver need to register with kernel. We can register driver by following function
major_ number :- In our driver code we will assume major number is 90 .
device:- device is the name of device for which you are writing the driver. In our character driver we are keeping name of our device is “mydev” .
file_operation:- File operation is the structure which contains the functions supported by your device.
2) unregister driver
At the time of exit our driver needs to be unregister with kernel. We can unregister it by using following function
3) device functions
Our driver has to implement all the functions supported by our driver. In our character driver we are implementing open, read , write and release function.
Below is the code of our driver . Save this file as test.c
static char ker_buf; //driver local buffer
static int dev_open(struct inode *inod, struct file *fil);
static ssize_t dev_read(struct file *filep,char *buf,size_t len,loff_t *off);
static ssize_t dev_write(struct file *flip,const char *buff,size_t len,loff_t *off);
static int dev_release(struct inode *inod,struct file *fil);
//structure containing device operation
static struct file_operations fops=
.read=dev_read, //pointer to device read funtion
.write=dev_write, //pointer to device write function
.open=dev_open, //pointer to device open function
.release=dev_release, //pointer to device realese function
static int hello_init(void) //init function to be called at the time of insmod
printk(KERN_ALERT “device registration failed.”);
printk(KERN_ALERT “device registred\n”);
static void hello_exit(void) //exit function to be called at the time of rmmod
static int dev_open(struct inode *inod, struct file *fil)
printk(“KERN_ALERT device opened”);
static ssize_t dev_read(struct file *filep,char *buf,size_t len,loff_t *off)
static ssize_t dev_write(struct file *flip,const char *buf,size_t len,loff_t *off)
static int dev_release(struct inode *inod,struct file *fil)
printk(“KERN_ALERT device closed\n”);
2) Build driver
To build driver we need to write, make file and save it in the same directory where our driver code is save. Below is the example of make file.
save this file as Makefile. Please note that name of file should has M capital.
now give make to build the module.
it will generate test.ko file as an output.
3)inserting driver module
We can insert our driver module in kernel by using insmod command.
4) make device node
As we know that for user our device is just a file. so we need to create a device node in kernel device tree. Following is the command to create the device node
mknod path type major minor
path:- path is the place where device file needs to be created . Mostly we create our device file under /dev directory. we give same name of device file which we registered at the time registering our driver. As in above code wehad given device name is “mydev”. So we will give path as /dev/mydev.
type:- type will be “c” or “b” . Weather our device will be registered as character device or block device.
major :- Major number of driver. In our case we assumed device major number as 90.
minor:- minor number is defined as minor number of device. we are assuming this value to 1.
after creating node you can see your node by giving following command
it will list out all the devices registered in your system.
6) change permission of device
To enables processes to read and write our device we need to change the permission of our device by following command
chmod a+r+w /dev/mydev
our driver is ready now , device node is created . we can test our driver by writing a small user space application which will open our device and will try to write and read to device. Following is the code of our test application
this application is writing hello to device and reading same from device.
save this file as test_app.c and compile this file as we compile other c file.
cc -o test_app test_app.c
execute this file to test the driver by following command.
- Lectures 12
- Quizzes 0
- Duration 50 hours
- Skill level All levels
- Language English
- Students 0
- Assessments Self
Introduction To Linux
Learn About LDD