Writing Device Drivers, This is an easy tutorial for learning device driver programming |
![]() ![]() |
Writing Device Drivers, This is an easy tutorial for learning device driver programming |
Jul 16 2008, 04:12 PM
Post
#1
|
|
|
Administrator ![]() ![]() ![]() Group: Root Admin Posts: 49,777 Joined: 9-May 08 Member No.: 1 |
Do you pine for the nice days of Minix-1.1, when men were men and wrote their own device drivers?" Linus Torvalds
Pre-requisites In order to develop Linux device drivers, it is necessary to have an understanding of the following:
User space and kernel space When you write device drivers, it's important to make the distinction between "user space" and "kernel space".
Figure 1: User space where applications reside, and kernel space where modules or device drivers resideInterfacing functions between user space and kernel space The kernel offers several subroutines or functions in user space, which allow the end-user application programmer to interact with the hardware. Usually, in UNIX or Linux systems, this dialogue is performed through functions or subroutines in order to read and write files. The reason for this is that in Unix devices are seen, from the point of view of the user, as files. On the other hand, in kernel space Linux also offers several functions or subroutines to perform the low level interactions directly with the hardware, and allow the transfer of information from kernel to user space. Usually, for each function in user space (allowing the use of devices or files), there exists an equivalent in kernel space (allowing the transfer of information from the kernel to the user and vice-versa). This is shown in Table 1, which is, at this point, empty. It will be filled when the different device drivers concepts are introduced. EventsUser functionsKernel functionsLoad moduleOpen deviceRead deviceWrite deviceClose deviceRemove moduleTable 1. Device driver events and their associated interfacing functions in kernel space and user space. Interfacing functions between kernel space and the hardware device There are also functions in kernel space which control the device or exchange information between the kernel and the hardware. Table 2 illustrates these concepts. This table will also be filled as the concepts are introduced EventsKernel functionsRead dataWrite data Table 2. Device driver events and their associated functions between kernel space and the hardware device. The first driver: loading and removing the driver in user space I'll now show you how to develop your first Linux device driver, which will be introduced in the kernel as a module. For this purpose I'll write the following program in a file named nothing.c <nothing.c> = #include <linux/module.h> MODULE_LICENSE("Dual BSD/GPL");Since the release of kernel version 2.6.x, compiling modules has become slightly more complicated. First, you need to have a complete, compiled kernel source-code-tree. If you have a Debian Sarge system, you can follow the steps in Appendix B (towards the end of this article). In the following, I'll assume that a kernel version 2.6.8 is being used. Next, you need to generate a makefile. The makefile for this example, which should be named Makefile, will be: <Makefile1> = obj-m := nothing.oUnlike with previous versions of the kernel, it's now also necessary to compile the module using the same kernel that you're going to load and use the module with. To compile it, you can type: $ make -C /usr/src/kernel-source-2.6.8 M=pwd modules This extremely simple module belongs to kernel space and will form part of it once it's loaded. In user space, you can load the module as root by typing the following into the command line: # insmod nothing.ko The insmod command allows the installation of the module in the kernel. However, this particular module isn't of much use. It is possible to check that the module has been installed correctly by looking at all installed modules: # lsmod Finally, the module can be removed from the kernel using the command: # rmmod nothing By issuing the lsmod command again, you can verify that the module is no longer in the kernel. The summary of all this is shown in Table 3. EventsUser functionsKernel functionsLoad moduleinsmodOpen deviceRead deviceWrite deviceClose deviceRemove modulermmod Table 3. Device driver events and their associated interfacing functions between kernel space and user space. -------------------- -------------------------------------------------------------------------------------------
AdGuru.org is a current happenings discussions board for Information Technology, News and Fun visit: www.adguru.org |
|
|
|
![]() ![]() |
|
Lo-Fi Version | Time is now: 21st November 2009 - 04:48 PM |