Upgrade the Linux hardware driver structure

Source: Internet
Author: User
Upgrade the Linux hardware driver structure-general Linux technology-Linux programming and kernel information. For more information, see. A hardware driver is a software interface between hardware and Linux kernel. It is a low-level software component dedicated to a specific hardware. This software component allows hardware to interact with more common advanced application interfaces. Provides support for a specific subsystem or hardware port (such as SCSI, USB, or PCMCIA), unlike for all SCSI, USB, or PCMCIA hardware devices. Since new hardware is generated every day, it is impossible to test every hardware that may be used for a specific subsystem. The kernel only provides support for specific subsystems, and hardware drivers only provide support for specific hardware of these subsystems. The new kernel separates advanced application interfaces from low-level hardware functions. Users can easily add support for new hardware to existing systems by writing appropriate hardware drivers or modifying the kernel.

The Linux hardware driver can be integrated into the kernel in two ways: one is to directly compile the kernel so that it can be used once and for all; the other is to compile it into a target format, when you need to add a hardware, the kernel can call it in. When you set the Linux kernel, Each kernel setting compiler can display the description of each available kernel setting variable, so that you can decide which variable is to be eliminated and which one needs to be written to the kernel, which of the following can be compiled into a kernel module that can be loaded.

The advantage of writing a hardware driver into the kernel is that you can call it at any time without installing it. However, this greatly increases the space occupied by the kernel. Write the hardware driver into a load-able kernel module, although it will increase the occupation and running time of system resources because of the search for the driver module, however, compared with the resources consumed by a large kernel. Compiling a hardware driver into a load-able kernel module can also facilitate software development. When you need to develop or correct a hardware driver, you can dynamically uninstall the old version and load the new version. However, if your driver has been written to the kernel, then the kernel must be rewritten and the system must be restarted every time the modified program is tested. In addition, the hardware driver is considered as a loaded kernel module for development and configuration, so that users can upgrade the hardware driver as an independent system, instead of modifying the kernel.

All you need to do is compile and install the loadable kernel module. Other work is done by the module itself. When the system accesses a hardware device for the first time, as long as there is a module dependency tree created using the "depmod" command, the corresponding modules can be automatically loaded. The kernel module can be loaded and usually installed in a sub-directory of the system/lib/modules directory. The name of this subdirectory is determined by the value of the variables such as VERSION, PATCHLEVEL, SUBLEVEL, and EXTRAVERSION In The Makefile of the kernel.

The Linux 2.6 kernel provides a new and unified framework for hardware drivers. You can customize the hardware driver originally running in the earlier kernel version. The new driver framework provides full support for hardware plug-and-play and power management by defining various interfaces. Subsystems can communicate with various drivers through these interfaces. The new driver framework makes the boundary between the bus and the driver clearer. The Linux 2.6 kernel also introduces the sysfs File System for hierarchical processing of the hardware tree of each system. The Linux 2.6 kernel also specifies a new naming method for the loaded kernel module, using the. ko extension instead of the old standard. o (object) extension.

Here we will focus on the differences between the hardware driver in the 2.6 kernel and the hardware driver in the previous kernel in the main structure.

Upgrade the basic structure of the hardware driver

The standard hardware driver template for Linux 2.4 kernel is as follows:

# Define MODULE
# Include linux/module. h>
# Include linux/config. h>
# Include linux/init. h>
Static int _ init name_of_initialization_routine (void ){
/*
* Code here
*/
}
Static void _ exit name_of_cleanup_routine (void ){
/*
* Code here
*/
}
Module_init (name_of_initialization_routine );
Module_exit (name_of_cleanup_routine );
There is a common problem with hardware drivers in earlier versions of the kernel, that is, the name of the initialization module and clearing function is assumed. If the default names init_module () and cleanup_module () are used when developers write hardware drivers in earlier versions of the kernel (), you do not need to record the names of the initialization module and clearing function. This method is often prone to errors and has been gradually eliminated. In the 2.6 kernel, you must use the module_init () macro and module_exit () macro to record the names of initialization and exit procedures.

In addition, in the 2.6 kernel, you no longer need to describe the # define MODULE in the source code or Makefile. The kernel build system automatically defines and verifies such symbols. When users write hardware drivers for the 2.6 kernel, such building systems will inevitably be used.

To compile an existing module and load it to the 2.6 kernel, you must first complete some basic structural changes. However, when using this structure to load a module, you will notice that an error message of a bad module is displayed in the standard output device and system log. To eliminate this information, you need to add an example for the MODULE_LICENSE () Macro, for example, MODULE_LICENSE ("GPL "). This type of macro introduced after kernel 2.4 can be defined as a module for obtaining GPL Version 2 or updating the Version license. Other valid values include "GPL v2", "GPL and additional rights", and "Dual BSD/GPL" (select BSD or GPL license), "Dual MPL/GPL" (select Mozilla or GPL license), and "Proprietary ".

2.6 The simplest template of the hardware driver in the kernel is as follows:

# Include img src = "/files/misc/lt.gif"> linux/module. h>
# Include img src = "/files/misc/lt.gif"> linux/config. h>
# Include img src = "/files/misc/lt.gif"> linux/init. h>
MODULE_LICENSE ("GPL ");
Static int _ init name_of_initialization_routine (void ){
/* Code goes here */
Return 0;
}
Static void _ exit name_of_cleanup_routine (void ){
/* Code goes here */
}
Module_init (name_of_initialization_routine );
Module_exit (name_of_cleanup_routine );



In addition to the changes required by the hardware driver, the most important changes in the Linux 2.6 kernel are completed during the kernel building process.

Changes During module setup

For all developers who develop hardware drivers, a basic change that has a major impact on them is not due to the kernel source code, but to integrate the compilation process of external modules into a standard kernel building mechanism. If you are not using an integrated development environment (for example, TimeSys TimeStorm, which can detect the kernel version and automatically create Makefile), you need to manually create Makefile for the hardware driver.

In kernels of version 2.4 and earlier versions, the development and compilation positions of the module are not limited. You only need to move the appropriate compilation mark to the command line or the Makefile of the module. These tags include two symbolic definitions and a pointer required for compiling modules. This pointer points to a directory containing the files contained in the kernel. The following statement is used as an example to create a loadable kernel module named testmod. o:
# Gcc-d1_kernel _-DMODULE-I/usr/src/linux-2.4.21/include-O2-c testmod. c

The process of building a module for the 2.6 kernel is relatively simple, but it is not easy to meet all the conditions required for successful compilation. You do not need to manually specify MODULE-oriented instructions (such as MODULE, _ KERNEL _) or new symbols (such as KBUILD_BASENAME and KBUILD_MODNAME ), you only need to integrate the process of building a system with the standard kernel of the external module. You do not need to specify options such as-O2, because the process automatically calls all the mandatory flag, just like other modules that can be loaded with the kernel. Makefile compilation is much simpler. For example, Makefile compiled for the testmod. ko module that is compatible with the 2.6 kernel is as follows:
Obj-m: = testmod. o.

However, to create an external module, you must write the kernel source code tree interface first. In this way, some temporary directories can be created for compilation. Below is a command line for the 2.6 kernel build module. It can be executed from the source code directory of the Inclusion module:
# Make-C/usr/src/linux-2.6.1 SUBDIRS = $ PWD modules

This example command assumes that the directory where the user's module source code and Makefile are located is the same as that in the command that the user is running. If you do not use the POSIX command (such as BASH), you can use the "SUBDIRS = 'pwd'" command to replace the SUBDIRS parameter with the $ pwd variable. In this way, you can use the "pwd" command to identify the working directory. The command for creating an exit is as follows:

# Make: Entering directory '/usr/src/linux-2.6.1'
* ** Warning: Overriding SUBDIRS on the command line can cause
* ** Inconsistencies
Make [1]: 'arch/i386/kernel/asm-offsets.s' is up to date.
Building modules, stage 2.
MODPOST
CC/home/wvl/timesys/2.6/testmod. mod. o
LD [M]/home/wvl/timesys/2.6/testmod. ko
# Make: Leaving directory '/usr/src/linux-2.6.1'



The testmod. ko module is generated when the "make" command is successfully completed. The new kernel module naming rule is used for naming this module. If you have modified the system boot program so that the module can be loaded with a clear name, you must confirm that after the kernel is upgraded to 2.6, whether the naming rules of these modules follow the new naming rules.

Adapt to 2.6 kernel internal changes

The Linux 2.6 kernel also brings about many internal changes. You need to change the existing driver to adapt to this change. These changes include the asynchronous I/O mechanism of the kernel, the DMA support layer, the memory and page allocation mechanism, the hardware driver of the data block, and the new category hard disk interface. For example, the function for allocating and managing memory and pages changes. In the 2.6 kernel, the system uses a standard interface named mempool. The usage and management of the module reference count has also changed. The module reference count is mainly used to determine whether a module is in use and to safely uninstall unused modules. In the 2.6 kernel, the command sequence has been replaced by the working sequence. A major change that affects a large number of different drivers is the new interface of the parameter module. The MODULE_PARM () macro is replaced by detailed parameter descriptions. This description comes from the new module_param () Macro.

Linux 2.6 kernel priority and SMP recognition bring new problems to drivers. In a single-processor system, in a Linux kernel with no priority, some drivers can assume that there is no need to provide a re-import interface between the two processors because they cannot run the driver at the same time. The driver can use the "spinlock" or "mutex" command to protect data that can be accessed from multiple processes. These considerations are especially important for those who write high-performance and real-time hardware drivers for Embedded environments (such as TimeSys Linux.

Other considerations

If you are more dependent on the Linux 2.6 kernel, you need to make other changes to the driver. For example, although the devfs file system has been written to the kernel since the birth of the 2.3 kernel, and is marked as a Discard command in the 2.6 kernel settings, it is often used in some special fields. For example, in Embedded Computing, devfs provides strong flexibility and a compressed/dev file name. The devfs file system is an intermediate step between hardcoded hardware nodes. These nodes are mainly used in earlier Linux and Unix systems. At the same time, it is also a combination of udev, hotplug and sysfs file systems. Udev support technology is currently being written into the Linux 2.6 kernel. TimeSys has developed commercial Linux systems with such technologies. If you are using another Linux release, you may find that devfs support and integration technology is very important for drivers.

If you want to use the devfs file system, you must first activate the support for it when building the kernel. This step can be completed in File systems → Pseudo filesystems of the kernel setting and editing program. To use devfs, you also need to change the hardware node identification method of the hardware driver. When you use the traditional/dev directory as the location of the Linux hardware descriptor file, the hardware driver registers the new hardware by starting the register_blkdev () or register_chrdev () function. Which one to use depends on whether the driver registers a data block hardware or a character hardware, and must know the hardware master number and secondary number in advance. In addition, because udev is a hot swappable program, it can automatically create and delete logon items under the/dev directory, so this method is also applicable to the new udev hardware mechanism.

When using the devfs hardware file system, the hardware driver must use the devfs_register () System Call to register their hardware. The driver can continue to use the previously specified Primary and Secondary encoding, or specify the DEVFS_FL_AUTO_DEVNUM flag for the devfs_register () call, which is automatically specified by devfs.

Summary

Users often modify the kernel for reasons such as improving system performance, adding system functions, simplifying and standardizing the system. Each new version of the Linux kernel will bring about many new changes, which have a great impact on developers at different levels. This article summarizes the changes in the hardware driver program and the module building process under the 2.6 kernel. Tools such as TimeStorm can provide users with templates to upgrade drivers, and automatically create and manage makefiles for loaded kernel modules. However, if you are manually maintaining existing hardware drivers or developing new hardware drivers, you will need to carefully consider the 2.6 kernel changes and make the right choices.

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.