Writing device drivers in Linux: A brief tutorial“Do you pine for the nice days of Minix- 1. Linus Torvalds. Pre- requisites. In order to develop Linux device drivers, it is necessary to have an understanding of the following: C programming. Some in- depth knowledge of C programming is needed, like pointer usage, bit manipulating functions, etc. If you currently have Windows running and you realize that you need some files for your work which you have stored on an Ext2 volume of your Linux installation, you. Microprocessor programming. It is necessary to know how microcomputers work internally: memory addressing, interrupts, etc. All of these concepts should be familiar to an assembler programmer. There are several different devices in Linux. For simplicity, this brief tutorial will only cover type char devices loaded as modules. Kernel 2. 6. x will be used (in particular, kernel 2. Debian Sarge, which is now Debian Stable). User space and kernel space. When you write device drivers, it’s important to make the distinction between “user space” and “kernel space”. Kernel space. Linux (which is a kernel) manages the machine's hardware in a simple and efficient manner, offering the user a simple and uniform programming interface. In the same way, the kernel, and in particular its device drivers, form a bridge or interface between the end- user/programmer and the hardware. Any subroutines or functions forming part of the kernel (modules and device drivers, for example) are considered to be part of kernel space. User space. End- user programs, like the UNIX shell or other GUI based applications (kpresenter for example), are part of the user space. Obviously, these applications need to interact with the system's hardware . However, they don’t do so directly, but through the kernel supported functions. All of this is shown in figure 1. Figure 1: User space where applications reside, and kernel space where modules or device drivers reside. Interfacing 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. Events. User functions. Kernel functions. Load module. Open device. Read device. Write device. Close device. Remove module. Table 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. Events. Kernel functions. Read data. Write 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. MODULE_LICENSE("Dual BSD/GPL"). Since the release of kernel version 2. 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. Next, you need to generate a makefile. The makefile for this example, which should be named Makefile, will be: =obj- m : = nothing. Unlike 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. 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. 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. Events. User functions. Kernel functions. Load moduleinsmod. Open device. Read device. Write device. Close device. Remove modulermmod. Table 3. Device driver events and their associated interfacing functions between kernel space and user space. The “Hello world” driver: loading and removing the driver in kernel space. When a module device driver is loaded into the kernel, some preliminary tasks are usually performed like resetting the device, reserving RAM, reserving interrupts, and reserving input/output ports, etc. These tasks are performed, in kernel space, by two functions which need to be present (and explicitly declared): module_init and module_exit; they correspond to the user space commands insmod and rmmod , which are used when installing or removing a module. To sum up, the user commands insmod and rmmod use the kernel space functions module_init and module_exit. Let’s see a practical example with the classic program Hello world: < hello. MODULE_LICENSE("Dual BSD/GPL"). Hello world!\n"). Bye, cruel world\n"). The actual functions hello_init and hello_exit can be given any name desired. However, in order for them to be identified as the corresponding loading and removing functions, they have to be passed as parameters to the functions module_init and module_exit. The printk function has also been introduced. It is very similar to the well known printf apart from the fact that it only works inside the kernel. The < 1> symbol shows the high priority of the message (low number). In this way, besides getting the message in the kernel system log files, you should also receive this message in the system console. This module can be compiled using the same command as before, after adding its name into the Makefile. In the rest of the article, I have left the Makefiles as an exercise for the reader. A complete Makefile that will compile all of the modules of this tutorial is shown in Appendix A. When the module is loaded or removed, the messages that were written in the printk statement will be displayed in the system console. If these messages do not appear in the console, you can view them by issuing the dmesg command or by looking at the system log file with cat /var/log/syslog. Table 4 shows these two new functions. Events. User functions. Kernel functions. Load moduleinsmodmodule_init()Open device. Read device. Write device. Close device. Remove modulermmodmodule_exit()Table 4. Device driver events and their associated interfacing functions between kernel space and user space. The complete driver “memory”: initial part of the driver. I’ll now show how to build a complete device driver: memory. This device will allow a character to be read from or written into it. This device, while normally not very useful, provides a very illustrative example since it is a complete driver; it's also easy to implement, since it doesn’t interface to a real hardware device (besides the computer itself). To develop this driver, several new #include statements which appear frequently in device drivers need to be added: =/* Necessary includes for device drivers */. O_ACCMODE */. #include < asm/system. MODULE_LICENSE("Dual BSD/GPL"). Declaration of memory. Structure that declares the usual file */. Declaration of the init and exit functions */. Global variables of the driver */. Major number */. int memory_major = 6. Buffer to store data */. After the #include files, the functions that will be defined later are declared.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
November 2017
Categories |