8.1.3. Minimal Device Driver Example
Because Linux supports loadable device drivers, it is relatively easy to demonstrate a simple device driver skeleton. Listing 8-1 illustrates a loadable device driver module that contains the bare minimum structure to be loaded and unloaded by a running kernel.
Listing 8-1. Minimal Device Driver
/* Example Minimal Character Device Driver */
#include <linux/module.h>
static int __init hello_init(void) {
printk('Hello Example Init
');
return 0;
}
static void __exit hello_exit(void) {
printk('Hello Example Exit
');
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_AUTHOR('Chris Hallinan');
MODULE_DESCRIPTION('Hello World Example');
MODULE_LICENSE('GPL');
The skeletal driver in Listing 8-1 contains enough structure for the kernel to load and unload the driver, and to invoke the initialization and exit routines. Let's look at how this is done because it illustrates some important high-level concepts that are useful for device driver development.
A device driver is a special kind of binary module. Unlike a stand-alone binary executable application, a device driver cannot be simply executed from a command prompt. The 2.6 kernel series requires that the binary be in a special 'kernel object' format. When properly built, the device driver binary module contains a .ko suffix. The build steps and compiler options required to create the .ko module object can be quite complex. Here we outline a set of steps to harness the power of the Linux kernel build system without requiring you to become an expert in it, which is beyond the scope of this book.
8.1.4. Module Build Infrastructure
A device driver must be compiled against the kernel on which it will execute. Although it is possible to load and execute kernel modules built against a different kernel version, it is risky to do so unless you are certain that the module does not rely on any features of your new kernel. The easiest way to do this is to build the module within the kernel's own source tree. This ensures that as the developer changes the kernel configuration, his custom driver is automatically rebuilt with the correct kernel configuration. It is certainly possible to build your drivers outside of the kernel source tree. However, in this case, you are responsible for making sure that your device driver build configuration stays in sync with the kernel you want to run your driver on. This typically includes compiler switches, location of kernel header files, and kernel configuration options.
For the example driver introduced in Listing 8-1, the following changes were made to the stock Linux kernel source tree to enable building this example driver. We explain each step in detail.
1. Starting from the top-level Linux source directory, create a directory under .../drivers/char called examples.
2. Add a menu item to the kernel configuration to enable building examples and to specify built-in or loadable kernel module.
3. Add the new examples subdirectory to the .../drivers/char/Makefile conditional on the menu item created in step 2.
4. Create a makefile for the new examples directory, and add the hello1.o module object to be compiled conditional on the menu item created in step 2.
5. Finally, create the driver hello1.c source file from Listing 8.1.
Adding the examples directory under the .../drivers/char subdirectory is self-explanatory. After this directory is created, two files are created in this directory: the module source file itself from Listing 8-1 and the makefile for the examples directory. The makefile for examples is quite trivial. It will contain this single line:
obj-$(CONFIG_EXAMPLES) += hello1.o
Adding the menu item to the kernel configuration utility is a little more involved. Listing 8-2 contains a patch that, when applied to the .../drivers/char/Kconfig file from a recent Linux release, adds the configuration menu item to enable our examples configuration option. For those readers not familiar with the diff / patch format, each line in Listing 8-1 preceded by a single plus (+) character is inserted in the file between the indicated lines (those without the leading + character).
Listing 8-2. Kconfig Patch for Examples
diff -u ~/base/linux-2.6.14/drivers/char/Kconfig ./drivers/char/Kconfig
--- ~/base/linux-2.6.14/drivers/char/Kconfig
+++ ./drivers/char/Kconfig
@@ -4,6 +4,12 @@
menu 'Character devices'
+config EXAMPLES
+ tristate 'Enable Examples'
+ default M
+ ---help---
+ Enable compilation option for driver examples
+
config VT
bool 'Virtual terminal' if EMBEDDED
select INPUT
When applied to Kconfig in the .../drivers/char subdirectory of a recent Linux kernel, this patch results in a new kernel configuration option called CONFIG_EXAMPLES. As a reminder from our discussion on building the Linux kernel in Chapter 4, 'The Linux KernelA Different Perspective,' the configuration utility is invoked as follows (this example assumes the ARM architecture):
$ make ARCH=ARM CROSS_COMPILE=xscale_be- gconfig
After the configuration utility is invoked using a command similar to the previous one, our new Enable Examples configuration option appears under the Character devices menu, as indicated in the patch. Because it is defined as type tristate, the kernel developer can choose from three choices:
(N) No. Do not compile examples.
(Y) Yes. Compile examples and link with final kernel image.
(M) Module. Compile examples as dynamically loadable module.