"Go" 6.4.6 to compile the driver into the Linux kernel for testing

Source: Internet
Author: User

Original URL: http://www.apkbus.com/android-98520-1-1.html

In the previous sections, the Linux drivers were compiled into modules and then loaded dynamically for testing. The dynamic load driver module does not automatically mount with the Android system booting, so the Android system must be loaded with the Insmod or modprobe command every time the Linux driver module is booted.

For embedded systems (including embedded Android, embedded Linux, etc.) it is common to compile Linux drivers into the kernel. While this does not have dynamic loading flexibility, Linux drivers are automatically loaded as Android starts. Generally in the development process for testing and debugging convenience, the Linux driver will be loaded into the Linux kernel module. When the Linux driver passes the final test, the Linux driver is compiled into the Linux kernel and tested.

This section describes how to compile the Word_count driver into the Linux kernel and test the Word_count driver on the Android emulator and the s3c6410 Development Board, respectively.

The Linux kernel source code is designed as a detachable structure. This means that only the configuration file needs to be modified so that a Linux driver can be compiled into a module (. ko file), or compiled into a Linux kernel, or, of course, the Linux driver can be removed from the Linux kernel. The core configuration file is as follows.

Q. config: This file is located in the top-level directory of the Linux kernel source code, as a hidden file. This file is used to configure the modules in the Linux kernel. The Linux driver can be configured in a. config file in three ways: compiled into a driver module (. ko file), compiled into the kernel, and removed from the Linux kernel. You can manually modify the. config file, or you can use the make Menuconfig command to set the. config file in menu mode.

Q Kconfig: This file is available for each module directory that you want to connect to the Linux kernel. This file is primarily used to define the menu that the Make Menuconfig command displays (including the menu item name, Help information, option type, module dependency, and so on), and the Kconfig file can also import kconfig files located in other directories. The make command uses a recursive reference to the Kconfig file to find all the Kconfig files in the Linux kernel, creating a complete configuration menu.

Q Makefile: Common with kconfig files appear at the same time. For each kconfig file, it is necessary to have a makefile file. This file specifies how to compile the source code for the directory where the makefile file is located.

Now also use the Word_count driver example to detail how to add a Linux driver to the Linux kernel source tree. Since the Word_count driver is character driven, you can use the following steps to add the Word_count driver to the Linux kernel source tree.

1th step: Put the word_count.c file into the Linux kernel source code

Place the word_count.c file in the <linux kernel directory >/drivers/char directory.

2nd Step: Modify the Kconfig file

Open the/root/kernel/goldfish/drivers/char/kconfig file, locate Endmenu, and add the following code in front of Endmenu.

Config Word_count

BOOL "Word_count Driver"

Help

This is a word count driver. It can get a word count from/dev/wordcount

Where the string after config is used as the second half of the shell variable name, the first half is config_. In other words, each specific module will have a shell variable to hold the module's 3 compilation behavior (generate. ko files, compile into the Linux kernel, or remove from the Linux kernel). The variable for the Word_count driver module is config_word_count. The value of the variable is saved in the. config file.

BOOL means that the Word_count driver can only make two settings (compiled into the kernel and removed from the Linux kernel), followed by how to set the three settings for the menu item. The string following the bool is the text of the menu item. Help is used to set menu item information.

3rd Step: Modify the Makefile file

Open the/root/kernel/goldfish/drivers/char/makefile file. Most of the files are shown in 6-21, just find a place to insert the following content.
<ignore_js_op>
Figure 6-21 Adding the Word_count driver module to the makefile file

obj-$ (config_word_count) + = WORD_COUNT.O

A Config_word_count variable is created by setting the 2nd step, and the variable is used after obj-in step 3rd instead of using a fixed value (Y or M). The make command replaces this variable with the corresponding value when compiling the Linux kernel.

4th step: Set the. config file

The. config file can be configured manually or through the make Menuconfig command in the menu. Here we take the method of menu configuration. Now go to the top-level directory (/root/kernel/goldfish) of the Linux kernel. Then execute the make Menuconfig command to display the configuration menu and go to "Device Drivers" > "Character Devices" submenu, find "Word_count_driver" menu item, press SPACEBAR to "Word_ Count_driver "menu item before setting it to an asterisk (*), as shown in 6-22. Then exit the configuration interface and save the changes.

Press the "H" key to display the help information for the Word_count driver, as shown in 6-23.
<ignore_js_op>
Figure 6-22 Configuring the Word_count driver module
<ignore_js_op>
▲ Figure 6-23 Word_count driver Help information

After configuring the. config file, the reader can open the. config file and find Config_word_count, which will find that the value of the variable has been set to "Y".

5th step: Compiling the Linux kernel

Enter the/root/kernel/goldfish directory and execute the following command to compile the Linux kernel.

# make

If the reader has previously compiled the current Linux kernel, there is no need to worry that the compilation takes too long because make is smart enough to compile only the most recently modified modules and their dependent modules.

Once the Linux kernel has been successfully compiled, the reader can find the Zimage file in the/root/kernel/goldfish/arch/arm/boot directory and run the kernel using the Android emulator. The reader will find that there is a WordCount device file in the/dev directory, and we are not running the build.sh script file to install the Word_count driver. This is because the Android emulator has automatically loaded the Word_count driver when loading the Zimage kernel file. However, when testing the Word_count driver using the previous example, you still need to perform the following command to set the access rights of the/dev/wordcount device file.

# adb Shell chmod 777/dev/wordcount

If the reader does not want to copy word_count.c to the/root/kernel/goldfish/drivers/char directory, you can use the following command in/root/kernel/goldfish/drivers/ The char directory establishes a symbolic link.

# Ln-s/root/drivers/ch06/word_count/root/kernel/goldfish/drivers/char/word_ Count

The steps to add the Word_count directory to the Linux kernel source tree are as follows (you will need to comment out the settings of the above steps before you proceed with the following steps).

1th Step: Create a new Kconfig file

Create a Kconfig file in the Word_count directory and enter the following:

Config Word_count

TriState "Word_count Driver"

Default Y

Help

This is a word count driver. It can get a word count from/dev/wordcount

Where TriState represents a three-state type (compiled into the kernel, compiled into modules, removed from the Linux kernel). If you use tristate instead of bool, the menu item is preceded by an angle bracket. By pressing the "Y" key, an asterisk (*) is displayed in the angle brackets, indicating that the kernel is compiled. Press "M" key to display m in angle brackets, which means to compile the module. Pressing the "N" key, the angle brackets disappear in the symbol, indicating that the Word_count driver is ignored. If you keep pressing the "SPACEBAR" key, these 3 states are cycled.

Default is used to set defaults. If you use Tristate,default, you can set Y, M, and N three values, which are compiled into the kernel, compiled into modules, and removed from the Linux kernel, respectively. When the module is set for the first time, it is in the default state for the default setting.

Note If you use TriState, you must open the Enable loadable module support option as described in 6.4.2 section, otherwise the driver cannot be set to the module state (M state), and the menu item is still preceded by a pair of brackets.

2nd Step: Modify the Makefile file

The current contents of the makefile file in the Word_count directory are as follows:

Obj-m: = WORD_COUNT.O

The compilation type has been set to the Linux driver module in the makefile file (Obj-m is compiled into a. ko file). But now to add the Word_count driver to the Linux kernel source tree, you need to use the Config_word_count variable instead of M, so the contents of the makefile file need to be modified as follows.

obj-$ (config_word_count): = WORD_COUNT.O

After modifying the makefile file, if you also want to test the Word_count driver with the script files from the previous sections, you need to set the value of the Config_word_count variable in the. config file to M, and if the variable is not in the. config file, add a config The _word_count variable. Of course, you can also use the Make Menuconfig command settings.

In order to be able to compile the Word_count driver separately or compile it with the Linux kernel, we can rewrite the makefile file in the following form. When the Config_word_count variable is undefined, the description is not compiled with the Linux kernel.

# compile with the Linux kernel

Ifdef Config_word_count

obj-$ (config_word_count): = WORD_COUNT.O

Else

# compiled separately

Obj-m: = WORD_COUNT.O

endif

3rd Step: Modify the Kconfig file of the upper directory

In order to locate the Kconfig file in the Word_count directory, you need to refer to the Kconfig file in the Word_count directory in the Drivers/char/kconfig file. Now open the/root/kernel/goldfish/drivers/char/kconfig file and add the following line of code before "Endmenu".

SOURCE "Drivers/char/word_count/kconfig"

4th Step: Modify the Makefile file of the upper directory

Add the following line to the Drivers/char/makefile file so that the make command can find the makefile file in the Word_count directory.

obj-$ (config_word_count) + = word_count/

The next step is the same as the 4th and 5th steps in the five steps described earlier. When entering the Setup interface shown in 6-24, you can press "M" key to compile the Word_count driver module into a. ko file.
<ignore_js_op>
Figure 6-24 Setting the compilation type of the Word_count driver module

Note When you recompile the kernel after modifying the Linux kernel settings, the Linux driver modules that were previously compiled with the Linux kernel may not be installed due to a malformed format, so after recompiling the Linux kernel, you need to recompile the Linux driver module.

If you want to compile the Word_count driver module into another kernel, you can use a similar approach as above.

"Go" 6.4.6 to compile the driver into the Linux kernel for testing

Related Article

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.