EmbLogic's Blog

E 30.01.30 : Kushal Chugh

?
ARTICLE:

CHARACTER DRIVER.

Heard so much about Character Driver when i came to Emblogic and now I am working on Character Driver on Linux from last couple of weeks.

Driver – As per my knowledge a Driver is basically a communication medium b/w the application layer and the hardware.
Device Drivers is classified into three basic types: (i) Character driver, (ii) Block driver and (iii)Network driver.

A Character device is one that can be accessed as a stream of bytes i.e. character by character and it sends the data byte by byte to the kernel buffer and user buffer when application give call of write and read respectively.

We use the driver as LKM(Loadable Kernel Module) as it is not essential to keep driver present in the kernel until system is power on. Each module is made up of object code that can be dynamically linked to the running kernel by the insmod program and can be unlinked by the rmmod program. Whenever we do insmod, the macro ‘module_init’ invokes our initialization routine and module is inserted to the kernel if it is valid. And when we do the rmmod, our module gets removed from the kernel through the macro ‘module_exit’. Our initialization routine registers the driver for the devices and obtains major no. from the registration call int register_chrdev_region(); or int alloc_chrdev_region()); which is defined in the library fs.h . After registering our device, we have to initialize all the resources that are going to be used in our device program. In module_exit, the exact opposite of initialization procedure is performed but in the reverse order. All the resources allocated to the driver will have to be freed . After our module gets inserted successfully onto the kernel,it can be seen in /proc/modules file.

We develop Charater Driver because it is most suitable for simple Hardware devices.
For implementing my character driver, we have used SCULL (Simple Character Utility for Loading Localities) because of its advantage that it does not depend upon hardwre. Scull is just some memory which is allocated from the kernel. We have defined the behaviour of the system calls for the device. We have used cdev_init and cdev_add functions to initialize the struct cdev. Struct cdev is the actual representation of the device.
Next step is to map the system calls with the routines written in my character driver. In our open routine,we are checking the mode and depending on that mode, we are performing some specific tasks . In the open routine we mappthe scull to the device by using the macro ‘container_of’. The return value of container_of will be starting address of mapped memory that we will use to read and write on device and we store this address in the private_data of the struct file structure defined in fs.h library. The difference between the kmalloc and container_of macro is that using kmalloc, the memory is allocated in the RAM but container_of macro maps the maps the scull into the device.

Write Data & Read Data.
In our write routine we write the data from application to the device via scull quantum by using copy_from_user() function. Write routine also checks for the offset value (file position) where the application wants to write.
In read routine we read the data from the device via scull quantum by using copy_to_user() function. Read routine also checks for the offset value from where application wants to read.
We have also implemented lseek routine that sets the offset value as provided by the application in lseek system call.

Now the time has come to test the driver.
(i) Compile and insert our module in running kernel dynamically. This is done byinsmod on shell here lkm is my name of c file of driver.Compilation of the file should be by Makefile.

(ii)To make communication possible between application application and device driver,we require node in (VFS).
Node is created by the command “mknod node_name c major no. minor no.”, where c specifies that it is for character driver.

(iii)After testing we should have to remove the module from kernel with command “rmmod” like rmmod lkm to free the allocated major number.

Character driver though while working was not an easy task to accomplish but with guidance of Sir and fellow friends it was a great experience.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>