Sixth. First Linux driver: Count the number of words experience notes

Source: Internet
Author: User

First, the Linux system maps each driver into a single file. These files are called device files or drive files and are saved in the/dev directory. This makes interacting with Linux drivers as easy as interacting with normal files. Most Linux drivers have their corresponding device files, so exchanging data with the Linux driver becomes the exchange of data with the drive device.

Ii. Writing Linux Drivers

1. Build Linux driver skeleton

Linux kernel requires load and unload drivers when using drivers

Load driver: Set up device file, allocate memory address space, etc. module_init function processing driver initialization

Uninstall drive: Delete device files, free memory address space, etc. module_exit function processing exit

A C program file containing the two macros of these two functions can also be seen as a Linux-powered skeleton

2. Registering and unregistering device files

Any Linux driver requires a device file, or the application will not be able to interact with the driver.

Set up the device file: Completed in the first step of the function to process the Linux initialization work. Misc_register function

Delete device files: Completed in the first step of the function to process the Linux exit. Misc_deregister function

3. Specify driver-related information

The driver is self-describing, the driver's author name, the open source protocol used, the alias, the driver description, and so on. This information needs to be specified in the driver source code.

Module_author, Module_license, Module_alls, Module_descripion, and other macros can specify driver-related information

4. Specifying a callback function

A driver does not have to specify all the callback functions, and the callback function is registered by the relevant mechanism.

5. Writing business logic

Specific business logic is related to the functionality of the drive, and the business logic may consist of multiple functions, multiple files, or even multiple Linux driver modules

6. Writing the makefile file

The compilation rules for the Linux kernel source code are defined by the makefile file. So writing a new Linux driver must have a makefile file

7. Compiling Linux Drivers

Can be compiled directly into the kernel or can be compiled separately as a module

8. Install and uninstall Linux drivers

If the Linux driver is compiled into the kernel, as long as Linux uses the kernel, the driver will be automatically reproduced

If the Linux driver is in a separate module, you need to load the Linux driver module with the Insmod or Modprode command, and the Rmmod command uninstalls the Linux driver module.

Three, the number of statistical words

Using device files as media to interact with the application, the application passes a space-delimited string to the device file, and then reads from the device file the number of words that the string contains.

Preparatory work:

1. Create the directory where the Linux drivers are stored:

"Mkdir-p/root/drivers/ch06/word_count"

"Cd/root/drivers/ch06/word_count"

2. Create the driver source code file: "echo" > word_count.c "

3. Write the Makefile file: "Echo ' obj-m: = WORD_COUNT.O ' > Makefile"

Obj-m indicates that the Linux driver is compiled as a module (. ko file) and obj-y means that the Linux driver is compiled into the Linux kernel. Both need to be assigned with a ": =" value. Using the OBJ-Y,.O file will be connected to the BUILT-IN.O file into the kernel. BUILT-IN.O files are intermediate target files that are generated by the. o file that connects the same class of programs. "Make Menuconfig" configures whether each driver and other kernel programs are allowed to be compiled into the kernel. When the driver relies on other programs, the dependent files are specified using Module-y or MODULE-OBJS (module represents the block name).

Writing skeletons

The skeleton section is primarily the initialization and exit functions of the Linux driver.

The PRINTK function is used to output log information. The printf function runs in user space, the PRINTK function runs in kernel space, and Linux drivers belonging to kernel programs do not have direct access to the printf function. Use the DMESG command to view log information that is output by Linux drivers.

specifying driver-related information

The Modinfo command looks at the information, depends represents the current driver module dependency, and Vermagic indicates which Linux kernel version the current Linux drive module is compiled under.

Module Module_author

Module Description: Module_description

Module alias: Module_alias

Open Source Agreement: Module_license (GPL Agreement, LGPL protocol, BSD protocol, Apache Licence 2.0 Protocol, MIT Protocol)

Registering and unregistering device files

A device file requires a struct (miscdevice) to describe its associated information, and its member variable fops is used to describe the function pointer of the device file in various triggering events, and its data type is also a struct file_operations. Use the Misc_register function to set only the secondary device number. A device with a master number of 10 is a simple character device with common features in a Linux system, called a misc device.

The "cat/proc/devices" command shows which primary devices and the main device number are in the current system.

Specifies the callback function and an algorithm for implementing statistical word numbers

User-space applications have the longest interaction with kernel-space drivers by reading and writing device files, and the File_operations.read and File_operations.write member variables allow you to specify the callback function pointers to be called by the read-write device file, respectively.

compiling, installing, and uninstalling Linux drivers

The difference between Insmod and modprobe is that the modprobe command can check the dependency of the driver module. Use the Depmod command to detect the dependencies of the Linux driver module before you use the modprobe command to load the driver module. The Depmod command must use the absolute path of the Linux driver module (. ko file), which writes the dependency information of the kernel module to the MODULES.DEP file of the kernel that is currently in use. The modprobe command simply uses the driver name and does not need to be followed by. Ko.

Iv. developing Linux drivers in eclipse

The first step: Build C Engineering

Step Two: Establish C source code file link New>source folder folder name Input src, import word_count.c file

Step three: Set include path click menu item properties,c/c++general > Paths and Symbols, select include GNU C, add two Way Diameter:/root/kernel/goldfish/include and/root/kernel/goldfish/arch/arm/include

Fourth step: Compiling Linux drivers

V. Testing Linux Drivers

First step: Import the Test_word_count.c file

Step two: Set the Include path

Step three: Set Target make target > Create, enter Word_count_eclipse_test in the Target Name text box, click OK

Fourth step: Build Project Make Target > Build, select the file created in the third step, then click Build

Fifth step: Running the test program run as > Local C + + application

Sixth. First Linux driver: Count the number of words experience notes

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.