6th. First Linux driver: Count the number of words

Source: Internet
Author: User

6th. First Linux driver: Count the number of words

What the hell is a 6.1Linux drive?


There is no essential difference between Linux drivers and normal Linux APIs, except that they are used in a different way than Linux.
Linux-driven work and access is one of the highlights of Linux. The Linux system maps each driver into a single file. These files become device files or drive files that are stored in the/dev directory.
Since most Linux drivers have their corresponding device files, exchanging data with the Linux driver becomes the exchange of data with the device files.
The most important step in writing a Linux drive is to write a callback function otherwise the data that interacts with the device file will not be processed.

6.2 Writing Linux driver steps


The first step is to build the Linux driver skeleton (load and unload Linux drivers)

Step two registering and unregistering device files

The third step is to specify driver-related information

Fourth step to make a callback function

Fifth Step writing business logic

Sixth step to write the makefile file

Seventh Step compiling Linux drivers

Eighth step install and uninstall Linux drivers

The first five steps are about how to write a Linux driver, and the next three steps can make the Linux driver work correctly.
Note: These eight steps do not have to be in order, according to their own needs to choose the first to complete the steps.

6.3 First Linux driver: Count the numbers of words


6.3.1 preparation of Linux drivers before writing
Enter the command first to establish the directory where the Linux driver is stored.
6.3.2 writing the skeleton of the Linux driver (Initialize and exit drivers)
The skeleton part of the Linux driver, the skeleton part is the initialization and exit function of the Linux driver. Code: Book P72
Install Linux driver: # insmod Word_count.ko
To see if Word_count installed successfully: # Lsmod | grep word_count
Uninstall Linux driver: # rmmod Word_count

6.3.3 Specifying related program information
1. The module is specified using the Module_author macro.
2. Module Description: Use module_description macro to specify.
3. Module alias: Use Module_alias macro to specify.
4. Open source protocol: Use MODULE_LICENSN macro to specify.

Open source Agreement, GPL agreement, LGPL agreement, BSD protocol, Apache Licence 2.0 protocol, MIT Protocol

6.3.4 registering and unregistering device files
To unregister a device file using the Misc_deregister function:
extern int Misc_register (struct miscdevice * misc);
extern int Misc_deregister (struct miscdevice * misc);


6.3.5 Developing callback functions
The most common way to interact is to read and write device files. The File_operations.read and File_operations.write member variables allow you to specify the callback function pointer to be called by the read-write device file, respectively.

The algorithm of 6.3.6 to realize statistic word number
The algorithm implemented by this section counts as a single word a string separated by a space, tab, carriage return, newline character, which takes into account the number of delimiters.
Word_count Driver Complete code: Book P84

6.3.7 compiling, installing, and uninstalling Linux drivers
Execute command:
# DMESG |tail-n 1
#modinfo Word_count.ko
View Log output information and Word_count driver module information

6.4 Testing Linux drivers using multiple methods

6.4.1 using Ubuntu Linux to test Linux drivers
Test Linux Driver code: Book P88

6.4.2 testing Linux drivers on the Android emulator via native (Native) C programs
Note: It is recommended to upload files to the Android simulator or development Board, put the files into the/data/local directory, many other directories of the system, such as/system/bin, are read-only, unless the Word_ Count.ko files are packaged into the system.img, otherwise the data cannot be written to these directories, even with root privileges.

6.4.3 using the Android NDK to test Linux drivers
Ndk_
TEST_WORD_COUNT.C file's full code: Book P94

6.4.4 using Java code to directly manipulate device files to test Linux drivers
If Android has root privileges, you can fully manipulate/dev/wordcount device files directly using Java code.

6.4.5 testing Linux drivers with the s3c6410 Development Board
1. Use executable program to test Linux driver on s3c6410 Development Board
2. Test Linux drivers on the s3c6410 Development Board using the Android NDK
3. Test Linux drivers with Java code on the s3c6410 Development Board

6.4.6 to compile the driver into the Linux kernel driver
The core configuration files are:. config, Kconfig, Makefile
To add Word_count drivers to the Linux kernel source tree:
First step: Put the word_count.c file into the kernel source code
Step Two: Modify the Kconfig file
Step three: Modify the makefile file
Fourth step: Set the. config file
Fifth step: Compiling the. config file

6.5 Developing and testing Linux drivers with eclipse
6.5.1 developing Linux drivers in eclipse
To develop a Linux driver using eclipse:
The first step: Build C Engineering
Step Two: Establish C source code file link
Step three: Set the Include path
Fourth step: Compiling Linux drivers

6.5.2 testing Linux drivers in Eclipes
Test Linux drivers:
First step: Import test_word_count.c
Step two: Set the Include path
Step three: Set up target
Fourth Step: Build Project
Fifth step: Run the test program

6th. First Linux driver: Count the number of words

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.