Android Deep Explore (Vol. 1) Hal with Driver Development Learning notes (6)

Source: Internet
Author: User

Android Deep Explore ( vol. 1) HAL with Driver Development Learning notes (6)

Sixth. The first Linux driver counts the number of words

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 design concept makes interacting with Linux drivers as easy as interacting with ordinary files. Although there is no concept of events in C, there are concepts similar to events, which are callbacks (c a l b a C K) functions. Therefore, the most important step in writing the Lin u x driver is to write the Yin function, otherwise the data that interacts with the device file will not be processed.

6.1 steps to write Linux drivers

    1. Establish linux drive skeleton
    2. Registering and unregistering device files
    3. specifying driver-related information
    4. specifying callback functions
    5. writing business logic
    6. writing
    7. compile linux driver
    8. Install and uninstall linux driver

* The first 5 steps in the above 8 steps are about how to write the Lin u x drive J Word, after passing the 3 - step can make the L-n u x drive work normally.

6.2 Preparation of the L- n u driver before writing

The L- n U-x Drive Source code for this example is not put together with the li n u x kernel source code, but is placed separately in a directory. * First Use the following command to create a directory that holds the Li n u x driver.

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

# Cd/root/drivers/ch0 6/word_count

* Then, use the following command to build the driver source code file (word count.c)

# echo ' >word_count.c

Finally write a M a k E F i l e file,

# echo ' obj-m: = WORD_COUNT.O ' > Makefile

6.3 Writing The skeleton of the L-n u x driver (initialization and exit driver)

The code uses theP r i n t kfunction. This function is used to output log informationPRINTKfunction andP r i n t ffunctions are similar in usage. LinuxThe system divides the memory into user space and kernel space, and the program of the two spaces cannot be accessed directly. P r i n t ffunctions run in user space,P r i n t kThe function runs in the nuclear space. Therefore, the kernel program'sL i n u xDrive is not directly accessibleP r i n t fof the function. There are many ways to interact between programs running in these two pieces of memory. Among them, the device file is a main way of interaction, if the user space program to access the kernel space, as long as a driver can access the kernel space, and then the user space program through the device file and the driver to interact with. So, how toLinuxdynamically allocating memory space in the driver? It's also easy to solve similar problems. SinceL i n u xThe driver cannot directly invoke a function that is running in user space, so theL i n u xin the kernel, you have to provide alternatives. Can enter theLi n u xKernel source code >/i n c l u d edirectory, in each subdirectory of the directory ... Contains a large number ofCLanguage header File,The resources defined in these header files are substitutes for programs running in user space.

6.4. installing view and uninstall Linux Drivers

* Install Linux Drivers

# Ismod Word_count.ko

* See if word_count is installed successfully

# lsmod I grep word_count

* Uninstall Linux Drivers

# Remmod Word_count

* View output information for Linux drivers

# DMESG | grep Word_cound | Tail-n 2

6.5 specifying driver-related information

It is generally necessary for the L-n u x driver to specify the following 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.

The code is as follows (usually placed in word_count.c ):

Module_author ("lining");

Module_description ("Statistics of Word count.");

Module_alias ("Word Count module.");

Module_license ("GPL");

6.6 registering and unregistering device files

Create a device file for the w o r d_c o u n T driver, the name of the device file is w o r d C o unt, located in the/d e v directory. Device files are different from normal files and cannot be established using the i O function, I need to use m i s c_r egister function to build the device file, using m i s c_d e r e G I The S T e r function unregisters (removes) the device file.

* Considerations in writing

1. The provisioning file is described by the main device number and the secondary device number. Instead, use the m i s C _ r e G i s t e r function to set only the secondary device number. The master device number is set to ten. a device with a main device number of 1 0 is a simple character device with common characteristics in the L-nµx system. This type of device is called m i s C device.

2. The value of the Miscdevice.name variable is the name of the device file. In this example, the device file name is called W o r d c o u n T.

3. If the device file is successfully registered,m i s C _ _ R e G i s t e r function returns The integer number of non 0, if registering the device file fails to return 0

4. W o r d_c o u n T. all functions and variable in C are declared as s t a t i c. the C language uses s t a t i C to declare functions, variables and other resources, the system will put these functions and variables in one area of memory, until the program exits completely, otherwise these resources will not be released , L i n u x Drive once loaded, the drive will always reside in memory, unless manually uninstalled or shut down, so these functions and variable resources will remain in memory. That is, multiple calls to these resources do not have to stack, out of the stack operation, is conducive to improve the efficiency of the drive operation.

6.7 specifying a callback function

  1. W o r d _ c o u n t_r e a Dand theWOrd_c o u n t_w r i t Ethe parameters of the function are basically the same, only the first2a parameterb u Fslightly different. Word_count _r e a D.of the functionb u Fparameter type isc h a R*, whileW o r d_count _w r i t Eof the functionb u Fparameter type isCo n s Tchar*; that meansW o r d_count _w r i t Ein the functionb u Fparameter values cannot be modified. In the Word_count_read functionb u Fparameter represents the data read from the device file, which meansb u Fdata from the device file may beread out, as to how much data can be read, depends onW o r d_count _r e a D.The return value of the function.
  2. Because kernel-space programs do not have direct access to data in the user space, they need to be used in . W o r d_c o u n t-r e a D and word _c o unt_w r i t e functions respectively using Copy_to_user and c o p y _from _user function copy data from the kernel space to the user space The ring is copied from the user space to the kernel space.

6.8 Many ways to test Linux drivers

1. testing Linux drivers with Ubuntu Linux

2.A n d r o i d simulator test L-n u x drive via native (Native) C Program

* The g o l d F I s H core for android o i d simulation does not allow the dynamic loading of the L-N U-X driver module, so it needs to be compiled L i n u x kernel perform the following command to configure the L-n u x core.

# CD Kernel Goldfish

# Make Menuconfig

* after the command above, press SPACEBAR will be the second top "Enable loadable module s U P p ort" check the front is (*), and then press ENTER to select the submenu, select the first 3 items, otherwise L i n u x drive die fast still cannot install and uninstall. Stays set when you exit the Settings menu. Finally, after compiling the kernel successfully, the A n d r o i d Simulator can dynamically load the L-n u x driver Module using the newly generated zimage kernel file .

* Perform the compile, upload, and install of the w o r ld_c o u n T driver in the build.h script file, then enter the terminal of the A n d R o d simulator, using e c h o and the The D-MESG command can perform a w o r d _ c o u n T Drive and view the test results.

* to make the compilation step as simple as possible using Android to set compilation parameters and compile with the make command. First create a n d r o i d. m k file in the Root/driver/ch06/word_count directory , and enter the following content.

LOCAL path:= $ (call My-dir)

Include $ (CLEAR VARS)

# Specify the source code file to compile

LOCAL _src _files: = Test _word_count. C

# Specifies the module name, which is also the executable file name generated after compilation

Local_module:= Test_word_cunt

LOCAL _modu LE _t AGS:= optional

Include $ (build_executable)

A R i dro i d. The M k file has the following two places to explain.

LO C AL _module_tags.

Represents the directory in which the current project (a n-d ROI D . mk file resides) is compiled in what mode. If set to optional, the pattern is not considered, that is, it will be compiled under any film. The variable can be set with a value of us er,use R d e b u g, e ng, o p t i o n a l where eng is the default value.

Include $ (build_exec UT ABLE).

Build _executable represents the creation of an executable file. Executable path is"a n d r o i d source code directory "

3. setting up Linux drivers using Android ADK

4. Use JAVA code to directly manipulate device files to test Linux Drivers

5. Test the Linux driver with the s3c6410 Development Board

6. Compile the driver into the Linux kernel for testing

Android Deep Exploration (Volume 1) HAL and Driver Development Study notes (6)

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.