說明:
1. Based on linux 2.6.32 and android 2.2,only support SDR(mem).
2.參考文章:
一、新增特性介紹
實際上,android仍然是利用了標準linux的休眠喚醒系統,只不過添加了一些使用上的新特性,early suspend、late resume、wake lock。
Early suspend -這個機制定義了在suspend的早期,關閉顯示屏的時候,一些和顯示屏相關的設備,比如背光、重力感應器和触摸屏等設備都應該被關掉,但是此時系統可能還有持有wake lock的任務在運行,如音樂播放,電話,或者掃描sd卡上的文件等,這個時候整個系統還不能進入真正睡眠,直到所有的wake lock都被釋放。在嵌入式設備中,背光是一個很大的電源消耗,所有android加入了這種機制。
Late resume -這個機制定義了在resume的後期,也就是喚醒源已經將處理器喚醒,標準linux的喚醒流程已經走完了,在android上層系統識別出這個物理上的喚醒源是上層定義的,那麼上層將會發出late resume的命令給下層,這個時候將會調用相關設備註冊的late resume回調函數。
Wake lock - wakelock在android的電源管理系統中扮演一個核心的角色,wakelock是一種鎖的機制,只要有task拿著這個鎖,系統就無法進入休眠,可以被用戶態進程和內核線程獲得。這個鎖可以是有time-out的或者是沒有time-out, 具有time-out的wake_lock會在時間過去以後自動解鎖。如果沒有鎖了或者超時了,內核就會啟動標準linux的那套休眠機制機制來進入休眠。
二、 kernel層源碼解析- early suspend和late resume實現
相關源碼:
kernel/kernel/power/main.c
kernel/kernel/power/earlysuspend.c
kernel/kernel/power/wakelock.c
kernel/kernel/power/userwakelock.c
kernel/kernel/power/suspend.c
之前標準的linux的sysfs的接口只需要一個state就夠了,現在至少需要3個接口文件:state、wake_lock、wake_unlock。現在為了配合android為休眠喚醒添加的幾種新特性,可以填入文件state的模式又多了一種:on,標準android系統中只支持state的on和mem模式,其餘的暫不支持。wake_lock和wake_unlock接口對應的讀寫函數在文件userwakelock.c中,對wakelock.c中的create wakelock或者release wakelock進行了封裝,供用戶空間來使用。
如果上層用戶執行:echo xxx(on or mem) > sys/power/state的話,將會調用到如下函數:
static ssize_t state_store (struct kobject *kobj, struct kobj_attribute *attr,
const char *buf, size_t n)
{
#ifdef CONFIG_SUSPEND // set
#ifdef CONFIG_EARLYSUSPEND //set
suspend_state_t state = PM_SUSPEND_ON; // for early suspend and late resume
#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(); // 檢查是否要求進入disk省電模式,暫時不支持
goto Exit;
}
#ifdef CONFIG_SUSPEND // def
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) ) {
//需要經過平台pm.c文件定義的模式支持檢查函數,mtk只支持mem,同時如果是android發送出來的late resume命令(on),這裡也會放行,往下執行
error = 0;
request_suspend_state(state) ; // android休眠喚醒的路線
}
#else
error = enter_state(state) ; //標準linux休眠喚醒的路線
#endif
#endif
Exit:
return error ? error : n;
}
@ kernel/kernel/power/earlysuspend.c
enum {
DEBUG_USER_STATE = 1U << 0,
DEBUG_SUSPEND = 1U << 2,
};
int Earlysuspend_debug_mask = DEBUG_USER_STATE;
module_param_named(Earlysuspend_debug_mask, Earlysuspend_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP);
static DEFINE_MUTEX(early_suspend_lock);
static LIST_HEAD(early_suspend_handlers);
static void early_sys_sync(struct work_struct *work);
static void early_suspend(struct work_struct *work);
static void late_resume(struct work_struct *work);
static DECLARE_WORK(early_sys_sync_work, early_sys_sync);
static DECLARE_WORK(early_suspend_work, early_suspend);
static DECLARE_WORK(late_resume_work, late_resume);
static DEFINE_SPINLOCK(state_lock);
enum {
SUSPEND_REQUESTED = 0x1,
SUSPENDED = 0x2,
SUSPEND_REQUESTED_AND_SUSPENDED = SUSPEND_REQUESTED | SUSPENDED,
};
static int state; //初始化為0
static DECLARE_COMPLETION(fb_drv_ready);
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; // state = 1 or 3
// state的值會在0->1->3->2->0循環變化,後面分析代碼都可以看出這些值代表系統目前處於什麼階段,簡單得說就是:正常->準備進early suspend ->開始early suspend並且對名為main的wakelock解鎖,如果此時沒有其餘wakelock處於lock狀態,那麼系統就走linux的休眠喚醒路線讓整個系統真正休眠,直到喚醒源發生,然後將處理器和linux層喚醒。之後android層判斷本次底層醒來是由於我所定義的喚醒源引起的嗎?如果不是,android將不予理會,過段時間沒有wakelock鎖,系統會再次走linux的休眠路線進入休眠。如果是,那麼android上層就會寫一個on的指令到state接口中,同樣是會調用到函數request_suspend_state() -> 準備執行late resume ->開始執行late resume,之後整個系統就這樣被喚醒了。
if (Earlysuspend_debug_mask & DEBUG_USER_STATE) {
struct timespec ts; // 打印出debug信息
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);
}
// eg: [request_suspend_state]: sleep (0->3) at 97985478409 (2010-01-03 09:52:59.637902305 UTC), 這裡對時間的獲取和處理,在其他地方可以參考
// ready to enter earlysuspend
if ( !old_sleep && new_state != PM_SUSPEND_ON ) { // susepnd會進入這裡
state |= SUSPEND_REQUESTED; // state = 1
pr_info("[request_suspend_state]:
sys_sync_work_queue early_sys_sync_work/n");
queue_work(sys_sync_work_queue, &early_sys_sync_work);
pr_info("[request_suspend_state]: suspend_work_queue early_suspend_work/n");
queue_work(suspend_work_queue, &early_suspend_work);
//在wakelocks_init()函數(wakelock.c)中會創建這兩個工作隊列和工作者線程來專門負責處理sys_sync和early suspend的工作。關於工作隊列的詳情參考我工作隊列的文章
}
// ready to enter lateresume
else if ( old_sleep && new_state == PM_SUSPEND_ON ) {
state &= ~SUSPEND_REQUESTED; // state = 2
wake_lock(&main_wake_lock); //對main wakelock上鎖
pr_info("[request_suspend_state]: suspend_work_queue late_resume_work/n" );
if ( queue_work(suspend_work_queue, &late_resume_work) ) {
//提交late resume的工作項
//
// In order to synchronize the backlight turn on timing,
// block the thread and wait for fb driver late_resume()
// callback function is completed
//
wait_for_completion(&fb_drv_ready);
//等待完成量fb_drv_ready,他會在late resume結束之後完成
}
}
requested_suspend_state = new_state;
//存儲本次休眠或者是喚醒的狀態,供下次休眠或者喚醒使用
spin_unlock_irqrestore(&state_lock, irqflags);
}
在系統suspend的時候提交的兩個工作項會陸續被執行到,那麼下面就來看一下執行early suspend的關鍵函數。
static void early_sys_sync (struct work_struct *work)
{
wake_lock(&sys_sync_wake_lock);
printk("[sys_sync work] start/n");
sys_sync() ; //同步文件系統
printk("[sys_sync wrok] done/n");
wake_unlock(&sys_sync_wake_lock);
}
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; // state = 3
else
abort = 1;
spin_unlock_irqrestore(&state_lock, irqflags);
if (abort) { // suspend 中止退出
if (Earlysuspend_debug_mask & DEBUG_SUSPEND)
pr_info("[early_suspend]: abort, state %d/n", state);
mutex_unlock(&early_suspend_lock);
goto abort;
}
if (Earlysuspend_debug_mask & DEBUG_SUSPEND)
pr_info("[early_suspend]: call handlers/n");
list_for_each_entry(pos, &early_suspend_handlers, link) {
if (pos->suspend != NULL)
pos->suspend(pos);
}
//函數register_early_suspend()會將每一個early suspend項以優先級大小註冊到鍊錶early_suspend_handlers中,這裡就是一次取出,然後執行對應的early suspend回調函數
mutex_unlock(&early_suspend_lock);
// Remove sys_sync from early_suspend,
// and use work queue to complete sys_sync
abort:
spin_lock_irqsave(&state_lock, irqflags);
if (state == SUSPEND_REQUESTED_AND_SUSPENDED)
{
pr_info("[early_suspend]: wake_unlock(main)/n");
wake_unlock(&main_wake_lock);
// main wakelock解鎖。看到這裡,好像系統執行了early suspend之後就沒有往下執行標準linux的suspend流程了,其實不是,android的做法是,不是你執行完了early suspend 的回調就可以馬上走標準linux的suspend流程,而是會檢查還有沒有wakelock被持有,如果所有wakelock全是解鎖狀態,那麼就會執行標準linux的suspend步驟。
}
spin_unlock_irqrestore(&state_lock, irqflags);
}
static void late_resume (struct work_struct *work)
{
struct early_suspend *pos;
unsigned long irqflags;
int abort = 0;
int completed = 0;
mutex_lock(&early_suspend_lock);
spin_lock_irqsave(&state_lock, irqflags);
// return back from suspend
if ( state == SUSPENDED )
state &= ~SUSPENDED ; // state = 0
else
abort = 1;
spin_unlock_irqrestore(&state_lock, irqflags);
if (abort) {
if (Earlysuspend_debug_mask & DEBUG_SUSPEND)
pr_info("[late_resume]: abort, state %d/n", state);
goto abort;
}
if (Earlysuspend_debug_mask & DEBUG_SUSPEND)
pr_info("[late_resume]: call handlers/n");
list_for_each_entry_reverse(pos, &early_suspend_handlers, link)
{
if (!completed && pos->level < EARLY_SUSPEND_LEVEL_DISABLE_FB) {
complete(&fb_drv_ready);
completed = 1;
}
if (pos->resume != NULL)
pos->resume(pos);
}
//以和early suspend的逆序執行鍊錶early_suspend_handlers上的late resume回調函數
if (Earlysuspend_debug_mask & DEBUG_SUSPEND)
pr_info("[late_resume]: done/n");
abort:
if (!completed)
complete(&fb_drv_ready); // 設置完成量ok
mutex_unlock(&early_suspend_lock);
}
沒有留言:
張貼留言