EmbLogic's Blog

Character Driver

CHARACTER DRIVER

As we have seen always, there is a Driver which drives every machine automatically or manually. In the same way Driver plays most important role in LINUX. Before we move on to Character Driver lets talk a bit about Device Driver. Device driver is a computer program that simply controls the external devices like Mouse, Keyboard, Pen-drive, USB, Hard-disk, Printer, Scanner, Web-cam etc, Drivers basically depends on hardware and also the operating what you are using.

There are three types of Device Drivers in LINUX.
1.Character Driver.
2.Block Driver.
3.Network Driver.

Character Driver: Character Driver simply means it Read or Write Character by Character only.
There are some set of rules to write a Character Driver. Follow the rules and you can write a best Character Driver.

Initialization: Firstly we have to initialize the kernel module, for which we have to use macro init_module(), this macro is use to initialize our kernel module. This function is called when the module is inserted into the kernel with help of command insmod. Now we have to end our module if we start it and for this we use at the end cleanup_module(). This function has called when module is remove from the kernel and the command is rmmod. These two are the most important part of kernel module. We are using some modules here which means we have to use header file #includethis header file needed by all kernel module. One more header file we use for the initialization of kernel loadable module is #includeand also we need to define two macros at the end of initialization and cleanup function are module_init(init_function); & module_exit(cleanup_function); these are the macros that designates a module is initialization and clean up functions.

After written your first code now is the time to compile it, here in kernel module we need a Makefile for the compilation of our kernel module. After this write make and if you are lucky then you will see a successfully compiled module. After the compilation you can check the ko(Kernel object) file with the help of ls on the command line. Now enter into the modules with the help of command, cd modules.
In the modules insert your kernel object file, write insmod filename.ko. To Check whether the file is insert successfully or not, give command dmesg by which you can see your inserted module. Once the module insert, now is the time to remove it with the help of command rmmod, and again enter dmesg to check whether the module removed successfully or not.

Make sure that there are no errors in your program, because when you work on the character driver is just like you are going to hack the kernel and one single mistake in your program may lead to reboot but reboot means you are lucky, because sometimes due to errors your system may crash. So its better to make your self good in system C.

The above process may have one alternate. Rather than go for that long you can do it by scripting as well. All you need to do just write a script and than change the mode of your scripting file into the executable file by giving command chmod called change mode.

Here the registration of your loadable kernel module is complete. We are making this module for a device, but now we are not using any physical hardware. In the place of this we will assign some memory inside our system which will act as device for our character driver and further testing we will do with the help of that created memory which will create with the help of scull(Simple Character Utility for Loading Localities). We are using scull because it isn’t hardware dependent. Scull using the space which is allocated by the kernel. And also the portability of scull is so easy across the computer architecture on which the linux runs. We allocate memory to to scull with the help of kmalloc.

Now once you allocate memory for scull you should check the major and minor number of your device. Major and Minor number are the most important part for any device access, to check the major and minor write ls -l. Here you can see the major & minor number. Actually why these major and minor number concept we use, because the major number shows that which driver we have to use to acces particular hardware, because system already assign a unique major number for every device, all device files with same major number are controlled by same driver. Now the driver use minor number to distinguish between the various driver it controls. Driver may have same major number but have different minor number.

The command to check the major and minor number is-:
printk(KERN_ALERT “Major no.:%d\n”, MAJOR(dev));
printk(KERN_ALERT “Minor no.:%d\n”, MINOR(dev));

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>