[Linux driver] Chapter 2 structure and operation module

Source: Internet
Author: User

Set the test system development environment and hello World entry module as described in the previous blog, please refer to the http://blog.csdn.net/tianshuai11/article/details/7442168

1. Comparison between core modules and Applications

Applications: small-and medium-scale programs that run a single task from start to end.

Core Module: Register yourself in advance to serve a future request. Then his initialization function ends immediately.

When you exit, the application does not release the resources you have applied for, and the module must carefully cancel all the initialization functions before exiting.

2. user space and kernel space

The module runs in the kernel space, and the application runs inKernel spaceApplication Space.

Every time an application executes a system call or is suspended due to hardware interruption, Unix switches the execution mode from user space to kernel space.

Applications are deployed in the virtual memory and have a large stack space (saving the function call history and the automatic variables in the current active function ). The kernel has a very small stack, so our own functions must share this stack with the call chain of the entire kernel space.

[Note] the kernel API shows the function name with two underscores (_): The underlying component of the interface.

Iii. Initialization and Shutdown

Static int _ init initialization_function (void)

{

/* Initialization Code */

Return <int>;

}

Module_init (initialization_function); // specifies the kernel initialization location. If this function is not available, the initialization function cannot be called.

[Note] _ init _ initdata indicates that the function is only used during initialization and is not used after the module is loaded.


Static void _ init cleanup_function (void) // No Return Value


The cleanup function is similar to module_exit (cleanup_function)

4. handle errors during initialization

1) always remember that registration may fail, so the module code should always check the return value.

2) When registering, some modules fail to be registered, you need to cancel the registered facilities on your own. Otherwise, the kernel is in an unstable state. The only effective solution: reboot the system

3) Use GOTO

Int _ my_init_function (void)

{

Int err;

Err = regeister_this (ptr1, "Skull ");

If (ERR) goto fail_this;

Err = regeister_that (ptr2, "Skull ");

If (ERR) goto fail_that;

Err = regeister_those (ptr3, "Skull ");

If (ERR) goto fail_those;


Return 0; // success


Fail_this: Return err;

Fail_those: unregeister_those (ptr3, "Skull ");

Fail_that: unregeister_that (ptr3, "Skull ");

}

4) when the initialization function is still running, the kernel is likely to call our module. Therefore, we should not register any facility before it is used to support all internal initialization of a facility.

5. Many types of module parameters are supported;
1) basic types:
Bool: Boolean Type
Invbool: the bool type of the value is reversed;
CHARP: character pointer type. The memory is allocated to strings provided by the user;
INT: integer
Long: Long Integer
Short: Short integer
Uint: unsigned integer
Ulong: Unsigned long integer
Ushort: Unsigned short integer

How to define module parameters:
Module_param (name, type, Perm );
Name: indicates the parameter name;
Type: indicates the parameter type;
Perm: indicates the parameter access permission;

2) array type: values provided by the list separated by commas;
Declare an array parameter:
Module_param_array (name, type, num, Perm );
Name: indicates the name of the array;
Type: indicates the parameter type;
Num: number of elements in the array;
Perm: indicates the parameter access permission;
3) parameter Access Permissions
Perm in modlue_param and module_param_array is used to set the access permission for this parameter;
Perm indicates the attributes of the parameter in the file node corresponding to the sysfs file system. You can use <Linux/STAT. h> permission value defined in. This value controls who can access the representation of these module parameters in the sysfs file system. When the perm is 0, this parameter indicates that the file node corresponding to the sysfs file system does not exist. Otherwise, after the module is loaded, a directory named after this module appears under the/sys/module/directory, with the given permissions;
For example:
# Deprecision s_irwxu 00700
# Define s_irusr 00400
# Define s_iwusr 00200
# Define s_ixusr 00100
# Define s_irwxg 00070
# Define s_irgrp 00040
# Define s_iwgrp 00020
# Define s_ixgrp 00010
# Define s_irwxo 00007
# Define s_iroth 00004
# Define s_iwoth 00002
# Define s_ixoth 00001
Note: If a parameter is modified by sysfs, the parameter value that your module sees is also modified, but your module will not receive any notifications. You should not make the module parameters writable, unless you are ready to detect this change and respond accordingly;

6. Compile the driver in the user space

Advantages of the user space DRIVER:
1) link to the entire C library
2) the driver can complete many unconventional tasks without using external programs (for complex peripherals, it is often necessary to issue user-provided policy applications together with the driver.
3) floating point numbers can be used in the driver. In some special hardware, floating point numbers may be used, but the Linux kernel does not support floating point numbers. This problem can be easily solved if it can be implemented in user mode.
4) The driver problem will not cause the entire system to be suspended. People with experience in driver development will be deeply touched by debugging. Some errors often cause the entire system to be suspended. User-mode drivers are much easier to debug.
5) User memory can be swapped out
6) well-designed drivers can still support concurrent access to devices
7) provide drivers with closed source code without using GPL, which is more flexible.


The most common example of a user space driver is X-server. Many USB drivers can also be placed in the user space. At present, many users try to provide drivers for PCI devices in user mode.

Disadvantages of user space drive:
1) the interruption is unavailable in the user space. The latest UIO interface has solved this problem.
2) slow response time
3) Only character devices are supported. Block devices and network devices are not supported.
4) low reliability, many drivers are closed-source, and we cannot solve the problem by reading the code.
5) some hardware vendors only provide user space drivers that match certain Linux development versions (often outdated ).














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.