Linux kernel driver initialization sequence Adjustment

Source: Internet
Author: User
Article Title: Adjustment of the initialization sequence of Linux kernel drivers. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

We need to use the API provided by another driver (I2C) When making a driver today. We encountered a dependency problem during kernel initialization.

My driver runs before I2C initialization, And the API provided by I2C is still unavailable. I checked a lot of information. Some people on the Internet said that the Starting sequence of all drivers using the module_init macro is uncertain (I have not found any authoritative information ).

All the _ init functions are in the Section. initcall. init also saves a function pointer. during initialization, the kernel will call these _ init function pointers through these function pointers. After the initialization is complete, release the entire init section (including. init. text ,. initcall. init ).

Note that the call sequence of these functions during kernel Initialization is only related to the order of the function pointers here, and 1) the functions themselves are described in. init. the order in the text section is irrelevant. In the 2.4 kernel, the order of these function pointers is also related to the order of links and is uncertain. In the 2.6 kernel, The initcall. init segments are further divided into seven sub-segments:

       
        .initcall1.init .initcall2.init .initcall3.init .initcall4.init .initcall5.init .initcall6.init .initcall7.init
       

When you need to put the function fn in The. initcall1.init section, you only need to declare

       
        core_initcall(fn);
       

You can.

The definition methods of other sections are as follows:

       
        core_initcall(fn) --->.initcall1.init postcore_initcall(fn) --->.initcall2.init arch_initcall(fn) --->.initcall3.init subsys_initcall(fn) --->.initcall4.init fs_initcall(fn) --->.initcall5.init device_initcall(fn) --->.initcall6.init late_initcall(fn) --->.initcall7.init
       

Initcall (fn) compatible with 2.4 is equivalent to device_initcall (fn ). The sequence between sub-sections is determined, that is, the function pointer in. initcall1.init is called first, and the function pointer in. initcall2.init is called, and so on. The order of function pointers in each sub-section is related to the link sequence and is uncertain.

In the kernel, different init functions are placed in different sub-sections, which determines their call sequence. In this way, some init functions must ensure a certain call order. According to the include/linux/init. h file, I tried the following two methods in the driver:

       
        __define_initcall("7", fn);late_initcall(fn);
       

You can adjust my driver to the final call. In fact, the above two are the same thing:

       
        #define late_initcall(fn) __define_initcall("7", fn)
       
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.