SUBMITTED TO: Mr. PRAVJOT SINGH LINUX DEVICE DRIVERS
v ARTICLE ON CHARACTER DRIVER:
THIS ARTICLE IS ALL ABOUT TO THE CONCEPTS THAT I HAVE LEARNED AND MAINLY CONCERNED TO THE WORK WHATEVER I HAVE PERFORMED IN THE PROJECT REGARDING ASSIGNMENT IN ORDER TO SUCCESSFUL COMPLETION OF TRAINING.
v INTRODUCTION OF CHARACTER DRIVER:
AS THE NAME SUGGESTS ,THE DEVICE -DRIVER PROVIDES THE MECHANISM TO DRIVE THE HARDWARE(DEVICE) ACCORDING TO THE APPLICTION.THE TYPE & CATEGORY OF THE DRIVER IS DEPENDENT ON THE TYPE & REQUIREMENT OF APPLICATION WHICH WEAREUSING. WHICH MEANS TOSAYTHAT TO ACCESS THE PENDRIVE WE NEED USB DRIVER ,FOR BTLUETOOTH WE WE NEED BT DRIVER & FOR MEDIA PLAYER WE NEED A SEPRATE ONE. DIFFERENT TYPES OF APPLICATION REQUIRES DIFFERENT TYPE OF DRIVERS.
CHARACTER DRIVER WORKS FOR CHARACTER DEVICES WHICH CAN BE ACCESSED AS STREAM OF BYTES. SUCH DRIVER USUALLY IMPLEMENTS OPEN, WRITE, READ & CLOSE SYSTEM CALLS.
I think One of the good features of Linux is the ability to extend at runtime the set of features offered by the kernel. This means that you can add modules to the kernel (and remove modules as well) while the system is up and running. The need of using device drivers is to increase the functionality of kernel . Each piece of code that can be added to the kernel at runtime is called a module. Each module is made up of piece of codes that can be dynamically linked to the running kernel by the insmod program and can be unlinked by the rmmod program.
Currently I am working on character driver & its practical implementation. In this article I am going to discuss what i know about character driver briefly.
Now as I have to make my driver capable of directly handshake-able with the kernel so it was essential to work in kernel space instead of user space ,so i have to use the kernel libraries(like kernel.h, init.h ,fs.h etc..) instead of glibc libraries(like stdio.h,stdlib.h,fcntl.h etc…).
v HOW TO IMPLEMENT?
- I AM GOING TO DESCRIBE THE BASIC STEPS WHICH I HAVE FOLLOWED WHILE IMPLEMENTING CHARACTER DRIVER:
– I started to write the program using two predefined macros named as module_init (entry_function) and module_exit(exit_function) as entrance point as main() in user space and exit point respectively.In which I have written my own start & end function. So after executing module_init execution will start from my function where I have to specify the major number and name of our driver, capacity to handle maximum number of device at a time and maximum memory of the driver. The major number identifies the driver associated with the device. I am firstly allocating major number to my driver which is basically a highest possible integer value between 0 to 255 by which our driver will be known in the kernel like other device drivers as stored in /proc/devices file .We can select major number for our driver from previously mentioned path but generally we privilege kernel to select major number because kernel will allocate it dynamically .This can be done with the register_chrdev_region() function call defined in fs.h which is in kernel library .In current scenario register_allok_chardev_region is in trend to do the errlier mentioned task .This will give the result of 32 bit(12 + 20 bit) at the address as give in first argument of call. After that I have used the first 12 bit i.e major number but i specify my own minor number generally 0 to upward in MKDEV() macro defined in kdev_t.h. After that I have allocated memory to sculldev structure(user defined structure that contain all information of the driver)) & separately initialized all its members as sculldev initialization function .Now used the cdev_init() and cdev_add() to initialize and add the cdev structure for our device which will contains the device specifications. Now I have written separate functions for each ( to create scullQsets structures, create q-sets structures, create quantums)& called these functions from write & read functions of driver modules to perform the desired operations in device.
In order to perform various operations like open, write, read, lseek and close file I have written an application in user space & mapped it similar kind if file operations (open, write read, llseek, release)that is in my driver program .So i have mapped these system calls in struct file_operations structure with my respective open,write,read,lseek and close functions according to prototype defined in <fs.h> kernel header file that will now handles the application calls instead of drivers of the OS.
But Before doing anything we have to map our allocated memory with the device memory this is done in the open function of driver with
container_of(ptr, type, member) in which first argument is pointer to i_cdev member of struct inode,2nd argument is user defined structure which contains pointer to struct cdev and third argument is pointer to struct cdev.Actually struct cdev is actual representation of the device. Now return of container_of will be starting address of mapped memory that we will use to read and write on device and i store this address in the private_data of the struct file structure defined in fs.h library.
After doing this now i have made my read, write and lseek function compatible with the almost all type of requests from application.In this write function will store the written data in own quantums when write request comes and give back that data according to the read request of application.I have also handled the single multithreaded application.
Now to test this driver we have to compile and insert our module in running kernel dynamically. This is done with command insmod ./modules/program name(without .c).ko & Compilation of the file should be by Makefile. After completion of work (successful insertion & execution of task) we have to remove our module this is done by command rmmod program name(without .c).
If every step is correct & written in proper manner then program will run & it will give desired result otherwise kernel will be crashed.
REMARKS: THERE MAY BE POSSIBILITY OF ERROR IN SYNTEX, SENTENCES,ARGUMENTS (& SOME-WHAT IN CONCEPTS ALSO ). SO ANY COMMENT, CORRECTION, SUGGESTION IS CORDIONLY INVITED.
REGARD’S
RANA BRIJENDRA SINGH