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

register_chrdev (major_number,device,file_operation)

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

                                             unregister_chrdev(major_number, device)

 

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

=====================================================================================

#include<linux/init.h>

#include<linux/module.h>

#include<linux/fs.h>

#include<linux/string.h>

#include<asm/uaccess.h>

MODULE_LICENSE(“GPL”);

 

static char ker_buf[100];   //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

{

int t=register_chrdev(90,”mydev”,&fops);

if(t<0)

printk(KERN_ALERT “device registration failed.”);

else

printk(KERN_ALERT “device registred\n”);

return 0;

}

static void hello_exit(void) //exit function to be called at the time of rmmod

{

unregister_chrdev(90,”mydev”);

printk(KERN_ALERT “exit”);

}

 

static int dev_open(struct inode *inod, struct file *fil)

{

printk(“KERN_ALERT device opened”);

return 0;

}

 

static ssize_t dev_read(struct file *filep,char *buf,size_t len,loff_t *off)

{

copy_to_user(buf,ker_buf,len);

return len;

}

 

static ssize_t dev_write(struct file *flip,const char *buf,size_t len,loff_t *off)

{

copy_from_user(ker_buf,buf,len);

ker_buf[len]=0;

return len;

}

 

static int dev_release(struct inode *inod,struct file *fil)

{

printk(“KERN_ALERT device closed\n”);

return 0;

}

 

module_init(hello_init);

module_exit(hello_exit);

 

=====================================================================================

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.

makefile

save this file as Makefile. Please note that name of file should has M capital.

now give make to build the module.

ncccccc

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.

t

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.

pg

after creating node you can see your node by giving following command

                                                                             cat proc/devices

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

pppp

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.

sh

 

adana elektrikci

', 'auto'); ga('send', 'pageview');