Android在标准linux基础上对休眠唤醒的实现(一)

说明:

1. Based on linux 2.6.32 and android 2.2only support SDR(mem).

2. 参考文章:

http://2695477.blog.51cto.com/blog/2685477/484751

http://www.docin.com/p-115475680.html

http://blogold.chinaunix.net/u3/113927/showart_2447111.html

http://www.cnmsdn.com/html/201003/1269407632ID2530.html

 

 

一、新增特性介绍

 

实际上,android仍然是利用了标准linux的休眠唤醒系统,只不过添加了一些使用上的新特性,early suspendlate resumewake lock

 

Early suspend - 这个机制定义了在suspend的早期,关闭显示屏的时候,一些和显示屏相关的设备,比如背光、重力感应器和触摸屏等设备都应该被关掉,但是此时系统可能还有持有wake lock的任务在运行,如音乐播放,电话,或者扫描sd卡上的文件等,这个时候整个系统还不能进入真正睡眠,直到所有的wake lock都没释放。在嵌入式设备中,悲观是一个很大的电源消耗,所有android加入了这种机制。

 

Late resume - 这个机制定义了在resume的后期,也就是唤醒源已经将处理器唤醒,标准linux的唤醒流程已经走完了,在android上层系统识别出这个物理上的唤醒源是上层定义的,那么上层将会发出late resume的命令给下层,这个时候将会调用相关设备注册的late resume回调函数。

 

Wake lock - wakelockandroid的电源管理系统中扮演一个核心的角色,wakelock是一种锁的机制, 只要有task拿着这个锁, 系统就无法进入休眠, 可以被用户态进程和内核线程获得。这个锁可以是有超时的或者是没有超时的, 超时的锁会在时间过去以后自动解锁。如果没有锁了或者超时了, 内核就会启动标准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

 

之前标准的linuxsysfs的接口只需要一个state就够了,现在至少需要3个接口文件:statewake_lockwake_unlock。现在为了配合android为休眠唤醒添加的几种新特性,可以填入文件state的模式又多了一种:on, 标准android系统中只支持stateonmem模式,其余的暂不支持。wake_lockwake_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并且对名为mianwakelock解锁,如果此时没有其余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_syncearly 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之后就没有往下执行标准linuxsuspend流程了,其实不是,android的做法是,不是你执行完了early suspend  的回调就可以马上走标准linuxsuspend流程,而是会检查还有没有wakelock被持有,如果所有wakelock全是解锁状态,那么就会执行标准linuxsuspend步骤。

}

       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);

}

 

你可能感兴趣的:(android,linux,Hibernate,工作,struct,list)