Android休眠流程总结

1. framework层

1.1 java层

休眠流程中上层有很多种,比如按power key进入休眠流程,还是就是settings下面的自动息屏流程。

1.1.1 power key休眠流程

当用户按下power key的时候,PhoneWindowManager类下的interceptBeforeQueueing,  在handle special key流程中,会命中KeyEvent.KEYCODE_POWER.

其业务逻辑里面就会调用到inteceptPowerKeyDown和interceptPowerKeyUp, 而休眠流程的起始条件,灭屏流程就是从这个interceptPowerKeyUp开始的。之后会就interceptPowerKeyUp展开,

但我们先来看下从key event发出后怎么走到interceptBeforeQueueing的,这里主要看下framework层的获得key event事件后响应流程,至于key event事件怎么分发的和怎么获得的 需要InuptDispatcher(inputFlinger)部分来分析,之后有时间再补充。

com_android_server_input_InputManagerService.cpp在收到InputDispatcher分发的出来PowerKey 事件之后,就会走到NativeInputManager::dispatchUnhandledKey方法,

这里方法里通过回调的机制回调到InputManagerService.java下面的native方法dispatchUnhandleKey,注意这个方法是回调方法。然后在调到InputManagerCallback下面的dispatchUnhandleKey,

然后再调到PhoneWindowManager下面的dispatchUnhandleKey方法, 然后调到interceptFallback方法,最终从这里面调到interceptBeforeQueueing这个方法。 

(类似的,ARN机制里面关于framework的一部分流程也是通过InputManagerService实现的,从native类com_android_server_input_InputManagerService.cpp的notifyANR通过反射开始回调java的方法InputManagerService类下的notifyANR方法,当然通信机制还是Binder了)。

 

interceptPowerKeyUp流程展开,这里方法里会调用powerPress这个方法,源码如下

 1 private void interceptPowerKeyUp(KeyEvent event, boolean interactive, boolean canceled) {
 2         ...
 3         if (!handled) {
 5        ...
 6             // Figure out how to handle the key now that it has been released.
 7             mPowerKeyPressCounter += 1;
 8 
 9             final int maxCount = getMaxMultiPressPowerCount();
10             final long eventTime = event.getDownTime();
11             if (mPowerKeyPressCounter < maxCount) {
12                 // This could be a multi-press.  Wait a little bit longer to confirm.
13                 // Continue holding the wake lock.
14                 Message msg = mHandler.obtainMessage(MSG_POWER_DELAYED_PRESS,
15                         interactive ? 1 : 0, mPowerKeyPressCounter, eventTime);
16                 msg.setAsynchronous(true);
17                 mHandler.sendMessageDelayed(msg, ViewConfiguration.getMultiPressTimeout());
18                 return;
19             }
20 
21             // No other actions.  Handle it immediately.
22             powerPress(eventTime, interactive, mPowerKeyPressCounter);/*这里开始灭屏流程*/
23         }
24 
25         // Done.  Reset our state.
26         finishPowerKeyPress();
27     }

powerPress主要实现的是根据power key按键次数来决定走什么流程,当按1次的时候走的是goToSleepFromPowerButton(), 其中SHORT_PRESS_POWER_GO_TO_SLEEP走doze流程,

SHORT_PRESS_POWER_REALLY_GO_TO_SLEEP走的是直接sleep的流程,还有按power key唤醒到到home的流程也走这里。

 1 private void powerPress(long eventTime, boolean interactive, int count) {
 2         ...
 3 
 4         if (count == 2) {
 5             powerMultiPressAction(eventTime, interactive, mDoublePressOnPowerBehavior);
 6         } else if (count == 3) {
 7             powerMultiPressAction(eventTime, interactive, mTriplePressOnPowerBehavior);
 8         } else if (interactive && !mBeganFromNonInteractive) {
 9             switch (mShortPressOnPowerBehavior) {
10                 case SHORT_PRESS_POWER_NOTHING:
11                     break;
12                 case SHORT_PRESS_POWER_GO_TO_SLEEP:
13                     goToSleepFromPowerButton(eventTime, 0);//走DOZE流程
14                     break;
15                 case SHORT_PRESS_POWER_REALLY_GO_TO_SLEEP:
16                     goToSleepFromPowerButton(eventTime, PowerManager.GO_TO_SLEEP_FLAG_NO_DOZE);//直接走sleep流程
17                     break;
18                 case SHORT_PRESS_POWER_REALLY_GO_TO_SLEEP_AND_GO_HOME:
19                     if (goToSleepFromPowerButton(eventTime,
20                             PowerManager.GO_TO_SLEEP_FLAG_NO_DOZE)) {
21                         launchHomeFromHotKey(DEFAULT_DISPLAY);
22                     }
23                     break;
24                 case SHORT_PRESS_POWER_GO_HOME:
25                     shortPressPowerGoHome();
26                     break;
27                 case SHORT_PRESS_POWER_CLOSE_IME_OR_GO_HOME: {
28                     if (mDismissImeOnBackKeyPressed) {
29                         if (mInputMethodManagerInternal == null) {
30                             mInputMethodManagerInternal =
31                                     LocalServices.getService(InputMethodManagerInternal.class);
32                         }
33                         if (mInputMethodManagerInternal != null) {
34                             mInputMethodManagerInternal.hideCurrentInputMethod();
35                         }
36                     } else {
37                         shortPressPowerGoHome();
38                     }
39                     break;
40                 }
41             }
42         }
43     }

接下来就通过2段代码调了PMS的goToSleep方法

 1 /**
 2      * Sends the device to sleep as a result of a power button press.
 3      *
 4      * @return True if the was device was sent to sleep, false if sleep was suppressed.
 5      */
 6     private boolean goToSleepFromPowerButton(long eventTime, int flags) {
 7         // Before we actually go to sleep, we check the last wakeup reason.
 8         // If the device very recently woke up from a gesture (like user lifting their device)
 9         // then ignore the sleep instruction. This is because users have developed
10         // a tendency to hit the power button immediately when they pick up their device, and we
11         // don't want to put the device back to sleep in those cases.
12         final PowerManager.WakeData lastWakeUp = mPowerManagerInternal.getLastWakeup();
13         if (lastWakeUp != null && lastWakeUp.wakeReason == PowerManager.WAKE_REASON_GESTURE) {
14             final int gestureDelayMillis = Settings.Global.getInt(mContext.getContentResolver(),
15                     Settings.Global.POWER_BUTTON_SUPPRESSION_DELAY_AFTER_GESTURE_WAKE,
16                     POWER_BUTTON_SUPPRESSION_DELAY_DEFAULT_MILLIS);
17             final long now = SystemClock.uptimeMillis();
18             if (mPowerButtonSuppressionDelayMillis > 0
19                     && (now < lastWakeUp.wakeTime + mPowerButtonSuppressionDelayMillis)) {
20                 Slog.i(TAG, "Sleep from power button suppressed. Time since gesture: "
21                         + (now - lastWakeUp.wakeTime) + "ms");
22                 return false;
23             }
24         }
25 
26         goToSleep(eventTime, PowerManager.GO_TO_SLEEP_REASON_POWER_BUTTON, flags);
27         return true;
28     }


private void goToSleep(long eventTime, int reason, int flags) {
mRequestedOrGoingToSleep = true;
mPowerManager.goToSleep(eventTime, reason, flags);
}
1 //PowerManager.java
2 public void goToSleep(long time, int reason, int flags) {
3         try {
4             mService.goToSleep(time, reason, flags);
5         } catch (RemoteException e) {
6             throw e.rethrowFromSystemServer();
7         }
8 }

@Override // Binder call
public void goToSleep(long eventTime, int reason, int flags) {
if (eventTime > SystemClock.uptimeMillis()) {
throw new IllegalArgumentException("event time must not be in the future");
}

mContext.enforceCallingOrSelfPermission(
android.Manifest.permission.DEVICE_POWER, null);

final int uid = Binder.getCallingUid();
final long ident = Binder.clearCallingIdentity();
try {
goToSleepInternal(eventTime, reason, flags, uid);
} finally {
Binder.restoreCallingIdentity(ident);
}
}

private void goToSleepInternal(long eventTime, int reason, int flags, int uid) {
synchronized (mLock) {
if (goToSleepNoUpdateLocked(eventTime, reason, flags, uid)) {
updatePowerStateLocked();
}
}
}

 

到此,可以看出PowerManager.java下的updatePowerStateLocked是一个统一的接口,也PMS的核心方法,也就是无论是按power key休眠还是自动息屏休眠,都会走到这里。

之后的流程本文后面统一分析,先看自动息屏流程。

 

1.1.2 自动息屏流程

为什么会自动息屏,简单的理解就是用户长时间内没有跟设备进行交互,然后触发了自动息屏流程。

自动休眠流程从PMS初始化开始就执行这个核心方法,这个方法又会去调updateUserActivitySummaryLocked,正是这个方法去计算用户没有响应的时间,当没有相应的时间

达到用户设置的息屏时间时,就设置mUserActivitySummary 为USER_ACTIVITY_SCREEN_DIM,再设置成USER_ACTIVITY_SCREEN_DREAM,最后设置成0。mUserActivitySummary为0时就会触发updatePowerStateLocked里的休眠流程了,也就是updateSuspendBlockerLocked。

额外提一下,与power key 的上报事件流程类似, userAcivity也是类似从底层开始接受用户跟设备的交互事件,这里直接从JAVA层开始讲,PMS有这样一个native方法userActivityFromNative。

 

1.1.3 updateSuspendBlockerLocked

 1 /**
 2      * Updates the suspend blocker that keeps the CPU alive.
 3      *
 4      * This function must have no other side-effects.
 5      */
 6     private void updateSuspendBlockerLocked() {11 
12         // Disable auto-suspend if needed.
13         // FIXME We should consider just leaving auto-suspend enabled forever since
14         // we already hold the necessary wakelocks.
15         if (!autoSuspend && mDecoupleHalAutoSuspendModeFromDisplayConfig) {
16             setHalAutoSuspendModeLocked(false);
17         }
18 
19         // First acquire suspend blockers if needed.
20         if (needWakeLockSuspendBlocker && !mHoldingWakeLockSuspendBlocker) {
21             mWakeLockSuspendBlocker.acquire();
22             mHoldingWakeLockSuspendBlocker = true;
23         }
24         if (needDisplaySuspendBlocker && !mHoldingDisplaySuspendBlocker) {
25             mDisplaySuspendBlocker.acquire();
26             mHoldingDisplaySuspendBlocker = true;
27         }
28 
29         // Inform the power HAL about interactive mode.
30         // Although we could set interactive strictly based on the wakefulness
31         // as reported by isInteractive(), it is actually more desirable to track
32         // the display policy state instead so that the interactive state observed
33         // by the HAL more accurately tracks transitions between AWAKE and DOZING.
34         // Refer to getDesiredScreenPolicyLocked() for details.
35         if (mDecoupleHalInteractiveModeFromDisplayConfig) {
36             // When becoming non-interactive, we want to defer sending this signal
37             // until the display is actually ready so that all transitions have
38             // completed.  This is probably a good sign that things have gotten
39             // too tangled over here...
40             if (interactive || mDisplayReady) {
41                 setHalInteractiveModeLocked(interactive);
42             }
43         }
44 
45         // Then release suspend blockers if needed.
46         if (!needWakeLockSuspendBlocker && mHoldingWakeLockSuspendBlocker) {
47             mWakeLockSuspendBlocker.release();
48             mHoldingWakeLockSuspendBlocker = false;
49         }
50         if (!needDisplaySuspendBlocker && mHoldingDisplaySuspendBlocker) {
51             mDisplaySuspendBlocker.release();
52             mHoldingDisplaySuspendBlocker = false;
53         }
54 
55         // Enable auto-suspend if needed.
56         if (autoSuspend && mDecoupleHalAutoSuspendModeFromDisplayConfig) {
57             setHalAutoSuspendModeLocked(true);
58         }
59     }
1 /** Wrapper for PowerManager.nativeSetAutoSuspend */
2         public void nativeSetAutoSuspend(boolean enable) {
3             PowerManagerService.nativeSetAutoSuspend(enable);
4         }
setHalAutoSuspendModeLocked 最终调用到了native方法nativeSetAutoSuspend。

1.2 native层
从native层开始,不管是power key键还是自动息屏休眠流程,都走的是nativeSetAutoSuspend, 别误以为autosuspend就是自动息屏休眠。autosuspend是android对linux源生suspend机制的补充。
从源码里可以看到从java传下来的enable参数是true的时候,它就去enblesuspend,如果是false,就去申请锁。同时这个锁会在enable为true的时候去释放,所以不影响休眠流程。

 1 static void nativeSetAutoSuspend(JNIEnv* /* env */, jclass /* clazz */, jboolean enable) {
 2     if (enable) {
 3         enableAutoSuspend();
 4         }
 5     } else {
 6         disableAutoSuspend();
 7         }
 8     }
 9 }
10 
11 void enableAutoSuspend() {
12     static bool enabled = false;
13     if (!enabled) {
14         sp<ISuspendControlService> suspendControl = getSuspendControl();
15         suspendControl->enableAutosuspend(&enabled);
16     }
17 
18     {
19         std::lock_guard<std::mutex> lock(gSuspendMutex);
20         if (gSuspendBlocker) {
21             gSuspendBlocker->release();
22             gSuspendBlocker.clear();
23         }
24     }
25 }
26 
27 void disableAutoSuspend() {
28     std::lock_guard<std::mutex> lock(gSuspendMutex);
29     if (!gSuspendBlocker) {
30         sp<ISystemSuspend> suspendHal = getSuspendHal();
31         gSuspendBlocker = suspendHal->acquireWakeLock(WakeLockType::PARTIAL,
32                 "PowerManager.SuspendLockout");
33     }
34 }

接下来再来看源码,这个suspendControl->enableAutosuspend(&enabled); 是调用哪里的方法

1 void SuspendControlService::setSuspendService(const wp<SystemSuspend>& suspend) {
2     mSuspend = suspend;
3 }
4 
5 binder::Status SuspendControlService::enableAutosuspend(bool* _aidl_return) {
6     const auto suspendService = mSuspend.promote();
7     return retOk(suspendService != nullptr && suspendService->enableAutosuspend(), _aidl_return);
8 }

bool SystemSuspend::enableAutosuspend() {
static bool initialized = false;
if (initialized) {
LOG(ERROR) << "Autosuspend already started.";
return false;
}

initAutosuspend();
initialized = true;
return true;
}

所以最终调到了SystemSuspend.cpp下的enableAutosuspend函数。

来详细看下initAutosuspend方法

 1 void SystemSuspend::initAutosuspend() {
 2     std::thread autosuspendThread([this] {
 3         while (true) {
 4             std::this_thread::sleep_for(mSleepTime);
 5             lseek(mWakeupCountFd, 0, SEEK_SET);
 6             const string wakeupCount = readFd(mWakeupCountFd);
 7 
 8             auto counterLock = std::unique_lock(mCounterLock);
 9             mCounterCondVar.wait(counterLock, [this] { return mSuspendCounter == 0; });
10             // The mutex is locked and *MUST* remain locked until we write to /sys/power/state.
11             // Otherwise, a WakeLock might be acquired after we check mSuspendCounter and before we
12             // write to /sys/power/state.
13 
14             if (!WriteStringToFd(wakeupCount, mWakeupCountFd)) {
15                 PLOG(VERBOSE) << "error writing from /sys/power/wakeup_count";
16                 continue;
17             }
18             bool success = WriteStringToFd(kSleepState, mStateFd);
19             counterLock.unlock();
20 
21             mControlService->notifyWakeup(success);
22 
23             updateSleepTime(success);
24         }
25     });
26     autosuspendThread.detach();
27     LOG(INFO) << "automatic system suspend enabled";
28 }

实际上就是去写了2个节点文件,"/sys/power/wakeup_count", "/sys/power/state", 当echo "mem" > /sys/power/state 且 "/sys/power/wakeup_count" 等于0的时候,系统就会进入suspend。

所以android本身就做了一些wrap, 只要上层能够实现上面这2个条件,系统就会进入suspend状态。而这些wrap就是kernel层的休眠机制,第2章接着讲。

在讲kernel休眠机制之前,先来总结下native层的框架,native层也采用RPC通信机制,也用到aidl接口。

实际上,com_android_server_power_PowerManagerService这个是这个机制中的客户端,而SuspendControlService是这个机制的服务端,他是对ISuspendControlService.aidl接口的具体实现。

这个服务端中又调用到了一个HAL的具体实现类SystemSuspend.cpp,它可以理解为是硬件的一个具体模块类。大致调用关系图如下

 

2. kernel层

2.1 kernel休眠机制简介

Android在Linux内核原有的睡眠唤醒模块上基础上,主要增加了下面三个机制:

Wake _Lock 唤醒锁机制;

Early _Suspend 预挂起机制;

Late _Resume 迟唤醒机制;

其基本原理如下:
当启动一个应用程序的时候,它都可以申请一个wake_lock唤醒锁,每当申请成功之后都会在内核中注册一下(通知系统内核,现在已经有锁被申请),当应用程序在某种情况下释放wake_lock的时候,会注销之前所申请的wake_lock。特别要注意的是:只要是系统中有一个wake_lock的时候,系统此时都不能进行睡眠。但此时各个模块可以进行early_suspend。当系统中所有的wake_lock都被释放之后,系统就会进入真正的kernel的睡眠状态。在系统启动的时候会创建一个主唤醒锁main_wake_lock,该锁是内核初始化并持有的一个WAKE_LOCK_SUSPEND属性的非限时唤醒锁。因此,系统正常工作时,将始终因为该锁被内核持有而无法进入睡眠状态。也就是说在不添加新锁的情况下,只需将main_wake_lock 解锁,系统即可进入睡眠状态。
结构图如下: 

 

 

在标准Linux中,休眠主要分三个主要的步骤:(1)冻结用户态进程和内核态任务;(2)调用注册的设备的suspend的回调函数,其调用顺序是按照驱动加载时的注册顺序。(3)休眠核心设备和使CPU进入休眠态冻结进程是内核把进程列表中所有的进程的状态都设置为停止,并且保存下所有进程的上下文。这个动作在android中也是有的。

备注:在andoird 早些时间的之前这些写节点操作是libsuspend库中完成的,具体目录如下:

system/core/libsuspend/autosuspend.c

system/core/libsuspend/autosuspend_wakeup_count.c

2.2 休眠流程分析

讲流程之前,先将kernel节点文件的创建。

2.2.1 /sys/power/state的创建

节点文件的创建在kernel/kernel/power/main.c中实现,

1)sys/power/state节点生成

 1 #define power_attr(_name) \
 2 static struct kobj_attribute _name##_attr = {    \
 3     .attr    = {                \
 4         .name = __stringify(_name),    \
 5         .mode = 0644,            \
 6     },                    \
 7     .show    = _name##_show,            \
 8     .store    = _name##_store,        \
 9 }
10  
11 power_attr(state);
12  
13 static struct attribute * g[] = {
14     &state_attr.attr,
15     NULL,
16 };
17  
18 static struct attribute_group attr_group = {
19     .attrs = g,
20 };
21 static int __init pm_init(void)
22 {
23     power_kobj = kobject_create_and_add("power", NULL);
24     if (!power_kobj)
25         return -ENOMEM;
26     error = sysfs_create_group(power_kobj, &attr_group);
27  
28 }
29  
30 core_initcall(pm_init);

 

2.2.2 /sys/power/wakeup_count的读写

这部分内容是用来记录和读取wakeup_count的次数,也在kernel/kernel/power/main.c中实现,后续详细补充。

 

2.2.3 kernel流程

在framework层已经执行到写"/sys/power/state"这个动作,那就会触发kernel里面的逻辑,也就是节点文件store逻辑。

1 static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,const char *buf, size_t n)
2 {
3     state = decode_state(buf, n);//将"mem"转换成对应的数字
4     error = pm_suspend(state);//这里才是真正开启kernel的休眠流程
5 }
 1 int pm_suspend(suspend_state_t state)
 2 {
 3     pm_suspend_marker("entry");
 4     error = enter_state(state);
 5     pm_suspend_marker("exit");
 6 }
 7  
 8 static void pm_suspend_marker(char *annotation)
 9 {
10     struct timespec ts;
11     struct rtc_time tm;
12  
13     getnstimeofday(&ts);
14     rtc_time_to_tm(ts.tv_sec, &tm);
15     pr_info("PM: suspend %s %d-%02d-%02d %02d:%02d:%02d.%09lu UTC\n",
16         annotation, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
17         tm.tm_hour, tm.tm_min, tm.tm_sec, ts.tv_nsec);
18 }
19 
20 static int enter_state(suspend_state_t state)
21 {
22     printk(KERN_INFO "PM: Syncing filesystems ... ");
23     sys_sync();
24     printk("done.\n");
25  
26     pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
27     error = suspend_prepare(state);
28  
29     pr_debug("PM: Entering %s sleep\n", pm_states[state]);
30     pm_restrict_gfp_mask();
31     error = suspend_devices_and_enter(state);
32     pm_restore_gfp_mask();
33  
34  Finish:
35     pr_debug("PM: Finishing wakeup.\n");
36     suspend_finish();
37 }
//在suspend_prepare的时候去 冻结cpu的中的任务
static
int suspend_prepare(suspend_state_t state) { if (need_suspend_ops(state) && (!suspend_ops || !suspend_ops->enter)) //kernel/drivers/cpuidle/lpm-levels.c suspend_set_ops(&lpm_suspend_ops); return -EPERM; error = pm_notifier_call_chain(PM_SUSPEND_PREPARE); error = suspend_freeze_processes(); pm_notifier_call_chain(PM_POST_SUSPEND); return error; } static inline int suspend_freeze_processes(void) { error = freeze_processes(); error = freeze_kernel_threads(); }
//然后去suspend一些外设,最后走suspend_enter流程。 
int
suspend_devices_and_enter(suspend_state_t state) { int error; bool wakeup = false; if (need_suspend_ops(state) && !suspend_ops) return -ENOSYS; if (need_suspend_ops(state) && suspend_ops->begin) { error = suspend_ops->begin(state);//lpm相关的 if (error) goto Close; } suspend_console(); error = dpm_suspend_start(PMSG_SUSPEND);//dpm相关的 if (error) { printk(KERN_ERR "PM: Some devices failed to suspend\n"); goto Recover_platform; } suspend_test_finish("suspend devices"); if (suspend_test(TEST_DEVICES)) goto Recover_platform; do { error = suspend_enter(state, &wakeup); //正常休眠会停在该函数,如果唤醒了,则走下面的流程 } while (!error && !wakeup && need_suspend_ops(state) && suspend_ops->suspend_again && suspend_ops->suspend_again()); Resume_devices: dpm_resume_end(PMSG_RESUME);//dpm相关的 resume_console(); Close: if (need_suspend_ops(state) && suspend_ops->end) suspend_ops->end();//lpm相关的 return error; Recover_platform: if (need_suspend_ops(state) && suspend_ops->recover) suspend_ops->recover();//lpm相关的 goto Resume_devices; }
 1 static int suspend_enter(suspend_state_t state, bool *wakeup)
 2 {
 3     error = dpm_suspend_end(PMSG_SUSPEND);//dpm相关的
 4     error = disable_nonboot_cpus();
 5     arch_suspend_disable_irqs();
 6     error = syscore_suspend();
 7     if (!error) {
 8         *wakeup = pm_wakeup_pending();
 9         if (!(suspend_test(TEST_CORE) || *wakeup)) {
10             error = suspend_ops->enter(state);//停在这..........
11             events_check_enabled = false;
12         }
13         syscore_resume();
14     }
15     arch_suspend_enable_irqs();
16  Enable_cpus:
17     enable_nonboot_cpus();
18  
19  Platform_wake:
20     if (need_suspend_ops(state) && suspend_ops->wake)
21         suspend_ops->wake();//lpm相关的
22  
23     dpm_resume_start(PMSG_RESUME);//dpm相关的
24  
25  Platform_finish:
26     if (need_suspend_ops(state) && suspend_ops->finish)
27         suspend_ops->finish(); //lpm相关的
28  
29     return error;
30 
31 }

//这2个函数都会走到dpm里面的一些电源管理函数的逻辑和drive的里面的lpm相关的逻辑, 

 1 int dpm_suspend_start(pm_message_t state)
 2 {
 3     dpm_prepare(state);    //prepare  dpm_list
 4     dpm_suspend(state);    //suspend  dpm_prepared_list
 5 }
 6 
 7 int dpm_suspend_end(pm_message_t state)
 8 {
 9     dpm_suspend_late(state);   //suspend_late      dpm_suspended_list
10     dpm_suspend_noirq(state);  //suspend_noirq    dpm_late_early_list
11 }
12 
13 void dpm_resume_start(pm_message_t state)  
14 {
15     dpm_resume_noirq(state);  //resume_noirq
16     dpm_resume_early(state);  //resume_early
17 }
18 
19 void dpm_resume_end(pm_message_t state)
20 {
21     dpm_resume(state);  //resume
22     dpm_complete(state);//complete
23 }
 1 struct dev_pm_ops {
 2     int (*prepare)(struct device *dev);
 3     void (*complete)(struct device *dev);
 4     int (*suspend)(struct device *dev);
 5     int (*resume)(struct device *dev);
 6     int (*freeze)(struct device *dev);
 7     int (*thaw)(struct device *dev);
 8     int (*poweroff)(struct device *dev);
 9     int (*restore)(struct device *dev);
10     int (*suspend_late)(struct device *dev);
11     int (*resume_early)(struct device *dev);
12     int (*freeze_late)(struct device *dev);
13     int (*thaw_early)(struct device *dev);
14     int (*poweroff_late)(struct device *dev);
15     int (*restore_early)(struct device *dev);
16     int (*suspend_noirq)(struct device *dev);
17     int (*resume_noirq)(struct device *dev);
18     int (*freeze_noirq)(struct device *dev);
19     int (*thaw_noirq)(struct device *dev);
20     int (*poweroff_noirq)(struct device *dev);
21     int (*restore_noirq)(struct device *dev);
22     int (*runtime_suspend)(struct device *dev);
23     int (*runtime_resume)(struct device *dev);
24     int (*runtime_idle)(struct device *dev);
25 
26 };
1 2 
3 static const struct dev_pm_ops goodix_ts_dev_pm_ops = {
4     .suspend = goodix_ts_suspend,
5     .resume = goodix_ts_resume,
6 };

类dpm函数的作用是按顺序执行相关的电源函数.

 

 

休眠prepare--->suspend--->suspend_late--->suspend_noirq

唤醒resume_noirq--->resume_early--->resume--->complete

 

//而且让cpu休眠的流程在cpuidle的驱动中实现,也就是lpm, kernel/drivers/cpuidle/lpm-levels.c。

 1 static int lpm_probe(struct platform_device *pdev)
 2 {
 3     suspend_set_ops(&lpm_suspend_ops);
 4 }
 5 
 6 static const struct platform_suspend_ops lpm_suspend_ops = {
 7     .enter = lpm_suspend_enter,
 8     .valid = suspend_valid_only_mem,
 9     .prepare_late = lpm_suspend_prepare,
10     .wake = lpm_suspend_wake,
11 };

kernel/kernel/power/suspend.c

1 void suspend_set_ops(const struct platform_suspend_ops *ops)
2 {
3     suspend_ops = ops;    
4 }

 

posted on 2020-07-13 13:17  找工作中  阅读(6517)  评论(0编辑  收藏  举报