Develop an android driver that can count words (2) Develop an android driver that can count words (3)

Source: Internet
Author: User
Develop an android driver that can count words (1)

5. Specify Driver-related information

Although it is not necessary to specify this information, a complete Linux driverProgramThe driver-related information is specified. You must specify the following information for the Linux driver.

1. Module Author: specified using module_author macro.

2. Module Description: Use the module_description macro to specify.

3. Module alias: Use the module_alias macro to specify the module alias.

4. Open-source Protocol: Use module_license macro to specify.

In addition to this information, the Linux driver module also contains some information. You can run the following command to view the word_count.ko information.

# Modinfo word_count.ko

After the preceding command is executed, the information shown in 6-6 is output. Depends indicates the dependency of the current driver module, and word_count does not depend on anything. Therefore, this item is empty. Vermagic indicates that the current Linux driver is compiled in that Linux kernel version.

Use the followingCodeSpecify the preceding four types of information. These codes are usually placed at the end of the word_count.c file.

Module_author ("lining ");

Module_description ("Statistics of wordcount .");

Module_alias ("Word Count module .");

Module_license ("GPL ");

Now use the method in the previous section to re-compile the word_count.c file. Then run The modinfo command to display the information shown in 6-7. As shown in Figure 6-7, the above Code settings are included in the word_count.ko file.

Vi. Open-source protocols

Although many individual developers or small companies do not consider the restrictions of open-source protocols too much, if large companies violate open-source protocols, they may be sued. Therefore, when using open-source software for companies with a certain scale and influence, pay attention to the open-source protocols used by these software.

To reduce the difficulty of releasing the Linux driver and the installation package size, many Linux drivers are openSource code. Use module_license macro to specify the Open Source protocol in the Linux driver source code. For example, the word_count driver uses the GPL protocol. So what protocol should we use to write the Linux driver? Currently, there are many open-source protocols. You can view all open source protocols on the following page.

Http://www.opensource.org/licenses/alphabetical

The following describes the basic information about the five most commonly used open source protocols. For details about the five open source protocols and other open source protocols, see the relevant pages of the open sourceinitiative organization.

GPL Protocol

For programmers who like to study technology, they should like the GPL protocol. Because the GPL protocol forces open source software to use the open source protocol. For example, the Linux kernel uses the GPL protocol. GPL is free/open-source. However, unlike other open-source protocols (such as BSD and Apache licence), the Open-Source GPL protocol is more thorough. Not only do you require open-source/free software using the GPL protocol, but you also require its derivative code to be open-source/free. For example, if software a adopts GPL agreement and software B uses software a, software B must be free/open-source. The B software must also adopt the GPL protocol. C software uses B software, and C software must also be open-source/free. Of course, C software must also adopt the GPL protocol. This is the so-called "contagious ". This is also the reason why many Linux releases and other software using the GPL protocol are currently open-source,

Because the GPL protocol strictly requires that software products that use the GPL protocol must use the GPL protocol and must be open-source/free. For commercial software or departments that require code confidentiality, it is not suitable to use the GPL protocol to publish software, or to reference A Class Library Based on the GPL protocol. To meet the needs of commercial companies and confidentiality, the lgpl Protocol emerged on the basis of the GPL protocol.

 

Lgpl Protocol

Lgpl is an open source protocol designed for class libraries. Unlike GPL, any software that uses/Modifies/derives the GPL class library must use the GPL protocol. Lgpl allows commercial software to use the lgpl class library through class library reference (Link) without the need for open source commercial software code. This allows open source code using the lgpl protocol to be referenced and published and sold by commercial software as class libraries.

However, if you modify or derive the lgpl protocol code, all the modified Code, the additional code involved in the modification part, and the derived code must adopt the lgpl protocol. Therefore, the open source code of the lgpl protocol is suitable for being referenced by commercial software as a third-party class library, but it is not suitable for the lgpl protocol code, secondary Development of commercial software is implemented through modification and derivative methods.

BSD Protocol

BSD open source protocol is a protocol that gives users great freedom. Basically, users can "do whatever they want", use them freely, modify the source code, or release the modified Code as open source or proprietary software. But the premise of "do whatever you want" is that when you publish code that uses the BSD Protocol, or perform secondary development of your own product based on the BSD Protocol code, the following three conditions must be met.

1. If the released product contains the source code, the BSD Protocol in the original code must be included in the source code.

2. If you only release binary class libraries/software, you must include the BSD Protocol in the original code in the Library/software documentation and copyright statement.

3. Open-source author/organization name and original product name cannot be used for marketing.

The BSD Protocol encourages code sharing, but it must respect the copyright of source code authors. BSD is a friendly protocol for business integration because it allows users to modify and re-release code, and also allows users to use or develop commercial software release and sales on BSD code. Many companies prefer the BSD Protocol when selecting open-source products, because they have full control over these third-party code and can modify or launch the code again when necessary.

Apache licence 2.0 protocol

Apache licence is a well-known non-profit open-source Apache protocol. Similar to BSD, this Protocol also encourages code sharing and respect for the copyright of the original author. It also allows code modification and re-release (as an open source or commercial software ). The conditions to be met are similar to those of BSD.

1. A copy of Apache licence is required for the code user.

2. If you have modified the code, you must describe it in the modified file.

3. In the extended code (Modification and code derived from the source code), the Protocol, trademark, patent statement in the original code and other instructions required by the original author shall be included.

4. If a new product contains a notice file, Apache licence must be included in the notice file. You can add your own license in notice, but it cannot be expressed as Apache licence.

Apache licence is also a friendly license for commercial applications. Users can also modify the code as needed to meet their needs and release/sell as open-source or commercial products.

MIT Protocol

MIT is a loose license agreement like BSD. The author only wants to retain the copyright without any additional restrictions. that is to say, you must include the original license agreement statement in your release, whether published in binary or source code.

VII. Register and deregister Device Files

This section creates a device file for the word_count driver, which is named wordcount and located in the/dev directory. A device file is different from a common file and cannot be created using the I/O function. You must use the misc_register function to create a device file and use the misc_deregister function to unregister (remove) the device file. The two functions are defined as follows:

Extern int misc_register (struct miscdevice * MISC); extern int misc_deregister (struct miscdevice * MISC );

Generally, you need to create a device file when initializing the Linux driver and delete the device file when uninstalling the Linux driver. In addition, a structure (miscdevice) is required to describe the information related to the device file. The miscdevice struct contains an important member variable fops, which is used to describe the function pointer of a device file in various events that can be triggered. The data type of the member variable is also a knot file_operations.

In this section, you need to modify the word_count_init and word_count_exit functions in the word_count.c file and define some macros and variables. The modified code is as follows:

// Define the device file name # define device_name "wordcount" // callback function pointer corresponding to the event triggered by the device file // owner: Which driver modules the device Event Callback Function applies, this_module indicates that it is applied to the current drive module static struct file_operations dev_fops = {. owner = this_module}; // information about the device file // minor: the second device number misc_dynamic_minor. The device number is dynamically generated. Name: The device file name/FOPS: file_operations struct variable pointer static struct miscdevice MISC = {. minor = misc_dynamic_minor ,. name = device_name ,. foPs = & dev_fops}; // initialize the Linux driver static int word_count_init (void) {int ret; // create the device file ret = misc_register (& MISC ); // output log information printk ("word_count_init_success \ n"); return ret;} // uninstall the Linux driver static void word_count_exit (void) {// cancel (remove) device File misc_deregister (& MISC); // output log information printk ("word_init_exit_success \ n ");}

Note the following points when writing the above Code:

1. The device file is described by the primary device number and secondary device number. The misc_register function can be used to set only the next device number. The master device number is set to 10. The device with the master device Number 10 is a simple character device with common features in Linux systems. Such devices are called MISC devices. If the driver function implemented by the reader is not complex, you can consider using 10 as the main device number, And the next device number can be customized or dynamically generated (the misc_dynamic_minor constant must be specified ). In this way, you can use the misc_register and misc_deregister functions to simplify the procedure of registering and deregistering a device file. The following sections describe how to use the register_chrdev_region and alloc_chrdev_region functions to register and deregister device files by specifying both the primary device number and secondary device number.

2. The value of miscdevice. Name is the name of the device file. In this example, the device file name is wordcount.

3. Although the file_operations struct defines multiple callback function pointer variables, this section does not initialize any callback function pointer variables. Only the file_operations.owner variable is initialized. If the value of this variable is a module struct, file_operations can be applied to these driver modules specified by the module. If the value of the owner variable is this_module, file_operations is only applied to the current driver module.

4. If the device file is successfully registered, the misc_register function returns an integer other than 0. If the device file fails to be registered, the system returns 0.

5. Some readers may have noticed this. All functions and variables in word_count.c are declared as static. This is because the C language uses static statements to declare functions, variables, and other resources. The system will separately place these functions and variables in a certain area of the memory until the program completely exits, otherwise these resources will not be released. Once the Linux driver is loaded, the driver will remain in the memory unless it is manually uninstalled or shut down, so these functions and variable resources will remain in the memory. That is to say, multiple calls to these resources do not need to be performed on the stack or the stack. It is conducive to improving the driving efficiency.

Recompile the word_count.c file and run the following command to install the word_count driver.

# Insmod word_count.ko

If the word_count driver has been installed, use the following command to download the word_count driver, and then use the preceding command to install the word_count driver.

# Rmmod word_count

After the word_count driver is installed, run the following command to view the devices in the/dev directory.

# Ls-A/dev

After the preceding command is executed, the information shown in 6-8 is output, and a wordcount file is added (in the white box ).

To view the master and secondary device numbers of the wordcount device file, run the following command.

# Ls-L/dev

Run the preceding command to output the information shown in 6-9. the first digit in the white box is the master device number and the second digit is the slave device number.

Use the following command to display the main devices and device numbers in the current system.

# Cat/proc/devices

After the preceding command is executed, the information shown in 6-10 is output, from which you can find the MISC device and the master device number 10.

Develop an android driver that can count words (3)

This article is excerpted to Android Deep Exploration (Volume 1): Hal and driver development , the following articles describe how to develop the Linux driver of the ARM architecture, and use the android program, ndk, and executable files to test the Linux driver. Can be in Ubuntu Linux, Android simulator and cloud6410 Development Board (you can buy OK6410-A Development Board, need to brush Android)

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.