最近研究如何讓Android不休眠。聽組裏人說,機器在充電的時候不休眠。我試了一下,確實是,串口可以使用(CONFIG_PM_DEBUG並沒有打開)。
這個時候,LCD顯示屏是休眠了,觸摸屏也休眠了,其他的比如重力傳感器等就沒有看了,但是標準的Linux系統並沒有進入休眠。看了網上好多關於Android系統的休眠與喚醒
例子,感覺有些懵懵懂懂的。於是,還是看內核代碼吧。
Android在標準的Linux休眠與喚醒機制上又加了一層,就是early_suspend / late_resume。顧名思意,使用early_suspend()進行休眠的設備,它休眠的時刻早於其他設備,使用late_resume()喚醒的設備,它被喚醒的時刻要晚於其他設備。這對函數通常成對出現,當內核打開了CONFIG_EARLY_SUSPEND(Android默認打開)後,就可以使
用這組函數來代替驅動中標準的 suspend / resume接口。
好了,講到early_suspend和late_resume,似乎必須要扯到一種叫做wake_lock的鎖定機制了。其實,單純從某個設備的驅動程序上來講,未必需要用到wake_lock機制,
比如我們的觸摸屏驅動中使用了early_suspend,就沒有使用wake_lock.
目前,我瞭解到的,wake_lock的用途只有一個,那就是防止系統進入休眠(這裏的休眠,指的是標準的Linux的休眠,不包含使用early_suspend()進行休眠的設備,
使用early_suspend()的設備,在系統還有wake_lock鎖的時候,也是要休眠的)。
好吧,現在是時候分析下Android/Linux的休眠與喚醒了,雖然好多先人 都已經講了這些,而且講的還不錯,這裏我還是要提一下。
root@android:/ # ls /sys/power/
pm_async
state
wait_for_fb_sleep
wait_for_fb_wake
wake_lock
wake_unlock
wakeup_count
這裏,我只關注state,當state 的值變化時,內核會調用
static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
const char *buf, size_t n)
{
#ifdef CONFIG_SUSPEND
#ifdef CONFIG_EARLYSUSPEND
suspend_state_t state = PM_SUSPEND_ON;
#else
suspend_state_t state = PM_SUSPEND_STANDBY;
#endif
const char * const *s;
#endif
char *p;
int len;
int error = -EINVAL;
p = memchr(buf, '\n', n);
len = p ? p - buf : n;
/* First, check if we are requested to hibernate */
if (len == 4 && !strncmp(buf, "disk", len)) {
error = hibernate();
goto Exit;
}
#ifdef CONFIG_SUSPEND
for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
if (*s && len == strlen(*s) && !strncmp(buf, *s, len))
break;
}
if (state < PM_SUSPEND_MAX && *s)
#ifdef CONFIG_EARLYSUSPEND
if (state == PM_SUSPEND_ON || valid_state(state)) {
error = 0;
request_suspend_state(state);//這裏,進入了Android的休眠與喚醒的處理函數
}
#else
error = enter_state(state);
#endif
#endif
Exit:
return error ? error : n;
}
power_attr(state);
request_suspend_state()都幹了些什麼事情
void request_suspend_state(suspend_state_t new_state)
{
unsigned long irqflags;
int old_sleep;
spin_lock_irqsave(&state_lock, irqflags);
old_sleep = state & SUSPEND_REQUESTED;
if (debug_mask & DEBUG_USER_STATE) {
struct timespec ts;
struct rtc_time tm;
getnstimeofday(&ts);
rtc_time_to_tm(ts.tv_sec, &tm);
pr_info("request_suspend_state: %s (%d->%d) at %lld "
"(%d-%02d-%02d %02d:%02d:%02d.%09lu UTC)\n",
new_state != PM_SUSPEND_ON ? "sleep" : "wakeup",
requested_suspend_state, new_state,
ktime_to_ns(ktime_get()),
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec, ts.tv_nsec);
}
if (!old_sleep && new_state != PM_SUSPEND_ON) {
state |= SUSPEND_REQUESTED;
queue_work(suspend_work_queue, &early_suspend_work);//在休眠的時候,去遍歷執行early_suspend_work這個隊列
} else if (old_sleep && new_state == PM_SUSPEND_ON) {
state &= ~SUSPEND_REQUESTED;
wake_lock(&main_wake_lock);
queue_work(suspend_work_queue, &late_resume_work);//在喚醒的時候,去遍歷執行late_resume_work這個隊列
}
requested_suspend_state = new_state;
spin_unlock_irqrestore(&state_lock, irqflags);
}
怎麼樣,是不是很簡單,根據用戶/系統所請求的狀態,去做相應的動作(休眠/喚醒)
能用到的一些變量的聲明在這裏
static void early_suspend(struct work_struct *work);
static void late_resume(struct work_struct *work);
static DECLARE_WORK(early_suspend_work, early_suspend);
static DECLARE_WORK(late_resume_work, late_resume);
看名字也知道了,early_suspend這個函數指針來處理early_suspend_work這條隊列,late_resume 這個函數指針來處理late_resume_work這條隊列。
雖然函數early_suspend()和late_resume()的實現都非常易懂,這裏還是要貼出來,因爲還有些東西要分析一下。
static void early_suspend(struct work_struct *work)
{
struct early_suspend *pos;
unsigned long irqflags;
int abort = 0;
mutex_lock(&early_suspend_lock);
spin_lock_irqsave(&state_lock, irqflags);
if (state == SUSPEND_REQUESTED)
state |= SUSPENDED;
else
abort = 1;
spin_unlock_irqrestore(&state_lock, irqflags);
if (abort) {
if (debug_mask & DEBUG_SUSPEND)
pr_info("early_suspend: abort, state %d\n", state);
mutex_unlock(&early_suspend_lock);
goto abort;
}
if (debug_mask & DEBUG_SUSPEND)
pr_info("early_suspend: call handlers\n");
list_for_each_entry(pos, &early_suspend_handlers, link) {//這裏就是關鍵了,遍歷early_suspend_handler這條鏈表(在驅動中註冊early_suspend的時候,都註冊到這條鏈表上了)
if (pos->suspend != NULL) {
if (debug_mask & DEBUG_VERBOSE)
pr_info("early_suspend: calling %pf\n", pos->suspend);
pos->suspend(pos);//調用各個實現進行各設備的休眠
}
}
mutex_unlock(&early_suspend_lock);
if (debug_mask & DEBUG_SUSPEND)
pr_info("early_suspend: sync\n");
sys_sync();
abort:
spin_lock_irqsave(&state_lock, irqflags);
if (state == SUSPEND_REQUESTED_AND_SUSPENDED)
wake_unlock(&main_wake_lock);//這裏很重要,別小看這個一個wake_unlock,起初我也以爲這僅僅是一個釋放main鎖,其實裏面有玄機呢。還記得wake_lock主要用來幹嘛麼,用來防止系統休眠,也就是說,只要系統中其他地方還擁有wake_lock鎖(類型WAKE_LOCK_SUSPEND),系統就沒法進入休眠,如果沒有鎖了,那就要接着走標準Linux的那一套休眠機制了
spin_unlock_irqrestore(&state_lock, irqflags);
}
先跳過late_resume()。來看下wake_unlock()的實現吧void wake_unlock(struct wake_lock *lock) { int type; unsigned long irqflags; spin_lock_irqsave(&list_lock, irqflags); type = lock->flags & WAKE_LOCK_TYPE_MASK; #ifdef CONFIG_WAKELOCK_STAT wake_unlock_stat_locked(lock, 0); #endif if (debug_mask & DEBUG_WAKE_LOCK) pr_info("wake_unlock: %s\n", lock->name); lock->flags &= ~(WAKE_LOCK_ACTIVE | WAKE_LOCK_AUTO_EXPIRE); list_del(&lock->link); list_add(&lock->link, &inactive_locks); if (type == WAKE_LOCK_SUSPEND) {//類型,驅動中一般只有這一種類型 long has_lock = has_wake_lock_locked(type); if (has_lock > 0) { if (debug_mask & DEBUG_EXPIRE) pr_info("wake_unlock: %s, start expire timer, " "%ld\n", lock->name, has_lock); mod_timer(&expire_timer, jiffies + has_lock); } else { if (del_timer(&expire_timer)) if (debug_mask & DEBUG_EXPIRE) pr_info("wake_unlock: %s, stop expire " "timer\n", lock->name); if (has_lock == 0)//如果沒有鎖了,要進入標準Linux的休眠機制了,咱們接着往下跟 queue_work(suspend_work_queue, &suspend_work); } if (lock == &main_wake_lock) { if (debug_mask & DEBUG_SUSPEND) print_active_locks(WAKE_LOCK_SUSPEND); #ifdef CONFIG_WAKELOCK_STAT update_sleep_wait_stats_locked(0); #endif } } spin_unlock_irqrestore(&list_lock, irqflags); } EXPORT_SYMBOL(wake_unlock);
static void suspend(struct work_struct *work)
{
int ret;
int entry_event_num;
struct timespec ts_entry, ts_exit;
if (has_wake_lock(WAKE_LOCK_SUSPEND)) {
if (debug_mask & DEBUG_SUSPEND)
pr_info("suspend: abort suspend\n");
return;
}
entry_event_num = current_event_num;
sys_sync();
if (debug_mask & DEBUG_SUSPEND)
pr_info("suspend: enter suspend\n");
getnstimeofday(&ts_entry);
ret = pm_suspend(requested_suspend_state);//這裏是關鍵點
getnstimeofday(&ts_exit);
if (debug_mask & DEBUG_EXIT_SUSPEND) {
struct rtc_time tm;
rtc_time_to_tm(ts_exit.tv_sec, &tm);
pr_info("suspend: exit suspend, ret = %d "
"(%d-%02d-%02d %02d:%02d:%02d.%09lu UTC)\n", ret,
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec, ts_exit.tv_nsec);
}
if (ts_exit.tv_sec - ts_entry.tv_sec <= 1) {
++suspend_short_count;
if (suspend_short_count == SUSPEND_BACKOFF_THRESHOLD) {
suspend_backoff();
suspend_short_count = 0;
}
} else {
suspend_short_count = 0;
}
if (current_event_num == entry_event_num) {
if (debug_mask & DEBUG_SUSPEND)
pr_info("suspend: pm_suspend returned with no event\n");
wake_lock_timeout(&unknown_wakeup, HZ / 2);
}
}
static DECLARE_WORK(suspend_work, suspend);
int pm_suspend(suspend_state_t state)
{
if (state > PM_SUSPEND_ON && state < PM_SUSPEND_MAX)
return enter_state(state);//正如你所料,開始走Linux那套休眠的流程了
return -EINVAL;
}
EXPORT_SYMBOL(pm_suspend);
喚醒相關的代碼就不貼 了,跟休眠類似的。
下面講下驅動中如何使用wake_lock和early_suspend,總的來說,還是挺簡單的
比如在設備probe的時候做如下操作
struct early_suspend early_suspend;
early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1; //等級,等級大小和suspend順序一致,和resume順序相反
early_suspend.suspend = xxx_early_suspend;//指定函數指針,需自己實現
early_suspend.resume = xxx_late_resume;
register_early_suspend(&early_suspend);//註冊進核心,也就是加入剛纔early_suspend_handlers那個鏈表
struct wake_lock chrg_lock;
wake_lock_init(&chrg_lock, WAKE_LOCK_SUSPEND, "xxx_wake_lock");//初始化類型爲WAKE_LOCK_SUSPEND的wake_lock鎖
#ifdef CONFIG_HAS_EARLYSUSPEND
static void xxx_early_suspend(struct early_suspend *h)
{
....
wake_lock(&chrg_lock);
....
}
static void xxx_late_resume(struct early_suspend *h)
{
.....
wake_unlock(&chrg_lock);
....
}
#endif