The Linux Kernel can be built in many different ways. Most drivers can be included unconditionally, compiled as modules to be loaded at runtime or be left out completely.
The kernel configuration is stored in the file .config which can be created by calling make menuconfig. In order to create the menu structure that is presented by make menuconfig there a several Kconfig files in the source tree.
To add a new config option, edit the corresponding Kconfig file (for example driver/char/Kconfig) and add your entry:
config EXAMPLE tristate "Example driver" depends on EXPERIMENTAL default n help Say yes if you want to compile this example driver...
Full documentation for Kconfig files is located in Documentation/kbuild/kconfig-language.txt.
Another step is needed to actually get the driver built into the kernel. Every directory contains a file called Makefile which gets included by the top level make file. There are two important variables that get initialized by all those make file snippets: obj-y and obj-m. obj-y holds all object files that get compiled into the kernel, obj-m holds all object files that get build as modules. For each configure option from the Kconfig file there is a variable that can be used in the make files. It evaluates to y if the option is to be include, m if the driver has to be build as module and n if it should not be compiled. The following construct is used very often to automatically do the right thing at compile time:
obj-$(CONFIG_EXAMPLE) += example.o
Full documentation of Makefile syntax is located in Documentation/kbuild/makefiles.txt.
Sometimes it is too complicated to modify the kernel sources in order to include a new driver. The Linux build infrastructure has special support for out-of-tree modules. Everything that is needed to build a module for some kernel is installed to /lib/modules/$version/build, just next to all the kernel modules. You can use the following Makefile to have your module build by the regular kernel build infrastructure:
# build modules obj-m = example.o
# use the kernel build system KERNEL_VERSION := $(uname -r) KERNEL_SOURCE := /lib/modules/$(KERNEL_VERSION)/build all: make -C $(KERNEL_SOURCE) M=`pwd` modules clean: make -C $(KERNEL_SOURCE) M=`pwd` clean
#include <linux/module.h>
Kernel modules contain code that can be loaded into the running kernel, much like shared libraries in user space programs. They are located in .ko files in /lib/modules/$version/kernel. There are several utilities to work with them (included in the module-init-tools package):
Print the list of currently loaded kernel modules.
Display informations about the module.
Load a kernel module by specifying the full path name.
Load a kernel module by module name. It is not neccessary to specify the full file name, it will be searched for in /lib/modules. If this module depends on other modules to work, then those will be loaded automatically, too.
Remove a module from the running kernel.
module_{init,exit,param} MODULE_{LICENSE,AUTHOR,DESCRIPTION}
Ein Modul darf erst wieder aus dem Kern entfernt werden wenn es garantiert nicht mehr verwendet wird. Viele Datenstrukturen enthalten ein .owner Feld, das mit dem Makro THIS_MODULE initialisiert werden muss. Solange eine solche Datenstruktur in Verwendung ist, kann das dazugehoerige Modul nicht entfernt werden.
Funktionen die als __init gekennzeichnet sind werden nach der Initialisierung wieder aus dem Speicher entfernt. Funktionen die als __exit gekennzeichnet sind werden nur bei Modulen mitkompiliert.