Getting started with Linux drivers

Source: Internet
Author: User

 

It is one of the main purposes of drivers to prevent users' programs from directly accessing the key data structures and hardware devices in the kernel. Therefore, a well-designed driver usually hides the complexity and variability of hardware. For example, when a program writes data to a disk, it only needs to open a file and execute the write command, without having to know that the size of each sector on the disk is 512 bytes or 1024 bytes, the detailed problem is handled by the driver. In addition, drivers can also hide the variability of hardware (may come from different manufacturers, or even different models of the same manufacturer) and provide users with a unified access interface. This is also the basis for the existence of "Everything is a file" in the Unix/Linux creed. 1. loadable module)Linux adopts the "macro kernel" structure and comes with a well-designed interface to dynamically load or remove the driver module when the system is running. This flexible design brings great convenience to the end users and even the program development process. Based on this function, developers do not need to restart the system to test the driver every time it is modified. Of course, the driver can also be compiled to the kernel in static mode, and many key drivers also need to be compiled in this way. For example, for a diskless workstation, since the system needs to load the required resources from other shared file systems through the NIC at first, the NIC driver must be statically compiled into the kernel at this time, the reason is that the loaded modules are dynamically loaded only after the system is started. Generally, you can use the startup script to load the dynamic driver module. Of course, you can also use the relevant commands to load the driver as needed. In addition, the kernel can automatically request to load the required modules when a service requires a special module. Although the driver is called a load module in the previous article, there is no definite term in the kernel module, such as device drivers and lkm and loadable kernel modules) kernel Modules, loadable modules, driver modules, and modules) and so on are commonly used to indicate the hardware drivers that can be dynamically loaded into the kernel. In this article, they will not be used separately. 2. hardware driver structureAlthough the development of Linux/Unix driver modules has been constantly evolving, its basic structure has not changed much. Hardware devices can be divided into two categories: character devices and Block devices.
  • A character device is a device that accesses data in a serial stream data sequence. The character device driver is responsible for this behavior. Generally, the character device driver must at least implement system calls such as open, close, read, and write. Common Character devices such as the console (/dev/console) and serial port (/dev/ttys0 ).
  • Block devices are usually addressable, and their data access is usually carried out in a fixed size of data blocks, but the data block storage location may be random. In most Unix systems, a block device transmits one or more whole pieces of data that is often 512 bytes (or twice its power, however, Linux allows any bytes of data to be transmitted at a time. The difference between Linux and character devices is that the data management method in the kernel is different from the driver interface implementation. A disk is a common block device.
3. Example of a driverBecause Linux supports load-type hardware drivers, it is easy to build a simple driver framework to describe the driver structure. The following is an example of the drivers for devices in the kernel model:

/* Example Minimal Character Device Driver */
#include
<linux/module.h>
static int __init hello_init(void)
{
printk("Hello Device Driver World!\n");
return 0;
}
static void __exit hello_exit(void)
{
printk("Goodbye, Cruel World!\n");
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_AUTHOR("Marion");
MODULE_DESCRIPTION("An example of device driver!");
MODULE_LICENSE("GPL");
/* End the hello*/

Although the above example is short, it fully possesses the program structure for Kernel loading, uninstallation, initialization, and exit. Unlike the standard binary executable program, the device driver is a special binary module, which cannot be simply executed through shell. Modules of the 2.6 series kernel are usually in the kernel object format. After compilation, these modules are usually suffixed with. Ko. The steps for creating a kernel object and the compilation options are complex. This article only describes the general process of creating a kernel object to help you understand the building steps of the kernel object. 4. module construction BasicsDriver compilation must be performed on specific kernels. Although the driver module compiled on another different kernel can also run, if you are not sure whether this module depends on certain features of the kernel during compilation, it brings great risks to the later running process. Therefore, the safest way is to build kernel objects based on the source tree of a certain kernel. This can also ensure that after developers change the Kernel configuration, the driver will also be rebuilt based on the newly configured kernel. If you need to build kernel objects on different kernels, make sure that the compilation options used for the driver configuration during build, the location where the kernel header file is referenced, and the Kernel configuration options are configured in the kernel where the driver actually runs. To build a Driver Based on the preceding example, follow these steps: (1) create a subdirectory named examples in the ../driver/Char directory of the kernel source code directory;
(2) Add a menu item in the Kernel configuration file to allow you to select examples for compiling during kernel compilation (compile into kernel or compile into module );
(3) add a sub-directory item examples in the file ../driver/Char/makefile to correspond to the menu item in step 2;
(4) is... create a MAKEFILE file in the/driver/Char/examples directory, and add hello to the menu item created in the second step. O module object for compilation; (5) Add the driver source code. The following describes the previous steps in detail: first, in the kernel source code directory... create the examples sub-directory in the/driver/Char directory, and then create two files: one is the driver source code in the previous example and the other is the MAKEFILE file created for it. The MAKEFILE file is very simple, and its content is only one line as follows:
OBJ-$ (config_examples) + = Hello. o the process of adding menu items to the Kernel configuration tool may be slightly bypassed. First, you must add a "Config" item in the file ../driver/Char/kconfig to enable the examples configuration item in the previous article. Add the following content after a line of menu "character devices": config examples
Tristate "enable Examples"
Default m
--- Help ---
Enable compilation option for driver examples and then return to the kernel directory. After running the kernel compilation command make gconfig (which requires XWindow support, you can choose Device Drivers-> character drivers to find the "enable Examples" item we added. The default value is "-" (specified by the default item), which is compiled into the kernel module. If it is set to "right", it indicates that the program is compiled into the kernel statically. If it is null, it indicates that this item is not compiled. As shown in: Next, you need... add an option to the/Drivers/Char/MAKEFILE file to indicate that the kernel compiler will compile the hello1 module in the examples subdirectory when we select config_examples. Add the following line near "obj-$ (config_ipmi_handler) + = IPMI/": obj-$ (config_examples) + = examples/to this point, in this example, the driver build infrastructure has been completed and will be automatically selected during the kernel compilation process for compilation. After executing the "make gconfig" command, execute the following command to compile the driver module. # Make modules

Chk include/Linux/version. h
Chk include/Linux/utsrelease. h
CC [m] Drivers/Char/examples/Hello. o
Building modules, stage 2.
Modpost
CC Drivers/Char/examples/Hello. Mod. o
LD [m] Drivers/Char/examples/Hello. Ko. Next, you can run the following command to install the compiled kernel module:
# Make modules_install when using this method, the installation process will re-install all the compiled kernel modules, including other previously compiled modules, which is not required. In a standard-installed Linux system, the kernel module is usually located in/lib/modules/<kernel-version> /... <Kernel-version> indicates the version number of the kernel running in the current system. The structure of this directory is similar to that of the kernel source code tree. The modules installed using the "make modules_install" command are usually located in this directory. Therefore, when you install one or more kernel modules separately, you can create a directory related to the kernel module driver that is the same as that in the kernel source code tree during compilation in this directory, and compile the completed *. copy the Ko file to the new directory.5. Load/uninstall the kernel moduleAfter the installation is complete, you can manually load or uninstall these modules, which can be implemented using modprobe. First, load the hello module. # Modprobe hello
# Tail-1/var/log/messages
SEP 14 22:06:23 localhost kernel: Hello Device Driver world! This module calls the module initialization function during loading. The program uses the module_init () macro (macro) to specify the module initialization function, such as module_init (hello_init ). In this module, the initialization function is only used to print a line of information to the system log. The information is defined in hello_init. In actual driver programming, initialization functions are often used to perform resource allocation and hardware device initialization. Next, you can use the lsmod command to display all modules loaded in the system in a formatted list. If the hello module appears, the previous loading is successful. Example: # lsmod
Module size used
Hello 5632 0
IPv6 274208 18
Autofs4 25092 2
I2c_core 25344 1 i2c_piix4
............ The used by column indicates the information that is being used by the current module and other modules that depend on the current module. The last line indicates that the i2c_piix4 module depends on the i2c_core module. You can use the-r option of modprobe to uninstall the kernel module. # Modprobe-r hello
# Tail-1/var/log/messages
SEP 14 22:14:33 localhost kernel: Goodbye, cruel world! When the hello module exits, it calls the exit routine function, which is implemented using the module_exit () Macro. The working method is similar to the previous loading process.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.