Sixth Chapter First Linux driver: Count the numbers of words
The Word_count driver for counting the number of words is the first complete Linux drive implemented in this book. Although the functionality of the Word_count drive is not complex, it is enough to allow developers who have never been in touch with Linux drivers to understand the complete development of Linux drivers. This chapter not only introduces an example, but also describes how to test Linux drivers using different methods. These methods are mainly differentiated by platform. Of course, more advanced functionality is required to write Linux drivers. When writing data to the printer driver, it is necessary for the printer driver to receive these written data and send them to the printer through the ports such as the wellhead USB on the PC. This process requires the Linui driver to respond to the data passed by the application. This is the Linux-driven event, although there is no concept of event in C, but there is a concept similar to the event, which is the callback (callback) function. Therefore, the most important step in writing a Linux drive is to write a function, otherwise data that interacts with the device file will not be processed.
Key steps:
Steps to write a Linux driver
1th Step: Build Linux driver skeleton (load and unload Linux drivers)
The skeleton part is mainly the initialization and exit function of Linux driver, the code is as follows:
#include <linux/module.h>
#include <linux/init .h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>
#include <asm/uaccess .h>
Initializing Linux drivers
static int word_count_init (void)
{
Output log Information
PRINTK ("work_count_init_success\n");
return 0;
}
Exiting Linux Drivers
static void Word_count_exit (void)
{
Output log Information
PRINTK ("word_count_init_exit_success\n");
}
Registering initialize Linux-driven functions
Module_init (Word_count_init);
Registering a function that exits the Linux driver
Module_exit (Word_count_exit);
2nd step: Registering and unregistering device files
# Insmod Word_count.ko//install Linux Driver
# Lsmmod | grep word_count//See if Word_count is installed successfully
# Rmmod Word_count//Uninstall Linux driver
# DMESG | grep Word_count | Tail–n 2//view log information that is output by Linux drivers.
You can also execute the following command to obtain log information from the Linux driver output.
# Cat/var/log/syslog | grep Word_count | Tail-n 2
3rd Step: Specify driver-related information
The module is specified using the Module_author macro.
Module Description: Specified using the Module_description macro.
Module aliases: Specified using the Module_alias macro.
Open Source protocol: Use the Module_license macro to specify.
4th step: Specify the callback function
Regardless of how complex or "cool" a Linux driver is, it makes sense to allow user-space applications to interact with kernel-space drivers. The most common way to interact is to read and write device files. Through File_operations.read and file_operations. The write member variable can specify the callback function pointer to be called by the read-write device file, respectively.
5th step: Writing Business logic
6th step: Writing the Makefile file
7th step: Compiling Linux drivers
8th step: Install and uninstall Linux drivers
Android Transplant and Driver Development sixth chapter experience