android的休眠和唤醒流程

时间:2021-12-04 12:57:35

android休眠唤醒流程:

power按键事件上报给android系统,最终由windownmanager接收到,当有按键事件时判断是否需要休眠后唤醒系统,然后调用powermanager系统服务去写/sys/power/state节点.
            此节点的写函数里判断收到的内容,来执行android的休眠early_suspend/唤醒late_resume流程.

android层:

private int setScreenStateLocked(boolean on)                                         电源管理服务:    frameworks/base/services/java/com/android/server/PowerManagerService.java
         int err = Power.setScreenState(on);                                           
                                                                                        在文件frameworks/base/core/java/android/os/Power.java定义
                                                                                                79     public static native int setScreenState(boolean on);
                                                                                        具体实现在:frameworks/base/core/jni/android_os_Power.cpp
                                                                                                static int setScreenState(JNIEnv *env, jobject clazz, jboolean on);
                set_screen_state(on);                                                    在文件hardware/libhardware_legacy/power/power.c中定义并实现
                    write(g_fds[REQUEST_STATE], buf, len);                                写/sys/power/state节点

内核层:

向节点写内容会调用kernel/power/main.c的写函数:
        static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,const char *buf, size_t n)
            request_suspend_state(state);                                                调用此函数:    具体实现在./kernel/power/earlysuspend.c中
                state:    为休眠则调用early_suspend_work                                    android early_suspen工作队列
                        为唤醒则调用late_resume_work                                    android late_resume_work工作队列
                       
                                                                                        drivers/video/samsung/s3cfb_main.c中早已经注册屏幕开关
                                                                                            register_early_suspend(&fbdev[i]->early_suspend);
                                                                                        所以
                    为休眠则调用early_suspend_work中的屏幕休眠函数:
                            s3cfb_early_suspend()
                                backlight_on()---->s3cfb_backlight_on()                    在文件arch/arm/plat-s5p/dev-fimd-s5p.c中定义    npd->backlight_on = s3cfb_backlight_on;
                                        s3cfb_backlight_on中直接操作用来控制lcd的gpio关闭屏幕                                        arch/arm/mach-exynos/setup-fb-s5p.c

为唤醒时调用late_resume_work中的屏幕唤醒函数:
                            s3cfb_late_resume()
                                backlight_off()---->s3cfb_backlight_off()                在文件arch/arm/plat-s5p/dev-fimd-s5p.c中定义    npd->backlight_off = s3cfb_backlight_off;
                                        s3cfb_backlight_off中直接操作用来控制lcd的gpio使能屏幕
               
                执行完所有的early_suspend后执行解锁main_wake_lock,以便休眠.
                wake_unlock(&main_wake_lock);
                    mod_timer(&expire_timer, jiffies + has_lock);                        在文件/kernel/power/wakelock.c中           
                                                                                                    此函数将启用expire_timer定时器,定时器内容即expire_wake_locks
                                                                                                    340 static DEFINE_TIMER(expire_timer, expire_wake_locks, 0, 0);

有四种方式可以引起休眠
        ①在wake_unlock()中, 如果发现解锁以后没有任何其他的wake lock了, 就开始休眠
        ②在定时器到时间以后, 定时器的回调函数会查看是否有其他的wake lock, 如果没有, 就在这里让系统进入睡眠
        ③在wake_lock() 中, 对一个wake lock加锁以后, 会再次检查一下有没有锁,             刚加上锁,为什么要检查,有用吗????
        ④按power键,调用earlysuspend.使系统或应用程序释放锁.从而调用上述三个函数进入休眠
    if(has_lock ==0)
        queue_work(suspend_work_queue,&suspend_work);    由DECLARE_WORK(suspend_work, suspend);知道,队列中的内容即suspend函数.
            suspend();                                    kernel/power/wakelock.c
                pm_suspend(requested_suspend_state);
                    enter_state(state);                    kernel/power/suspend.c
                        suspend_devices_and_enter(state);
                            suspend_enter(state);
                                suspend_ops->enter(state);            -->调用平台相关的休眠函数,定义在中arch/arm/plat-samsung/pm.c:379:static const struct platform_suspend_ops s3c_pm_ops

                          即s3c_pm_enter

            s3c_pm_arch_stop_clocks()  -->               休眠时执行的的最后一个函数.系统停在此处,等待中断或rtc等唤醒源唤醒.

                               
                                从此处开始唤醒流程
                                enable_nonboot_cpus();
                            suspend_test_start();                    ---------->kernel已经被唤醒,当按键或中断来临后可以执行中断函数,上报唤醒事件.对于外部中断来说,上报power按键事件.
                        pm_restore_gfp_mask();            kernel/power/suspend.c
                    enter_state(state);
                 pm_suspend(suspend_state_t state);                                           
            suspend();                                    kernel/power/wakelock.c