/* kernel/power/earlysuspend.c
*
* Copyright (C) 2005-2008 Google, Inc.
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/ #include <linux/earlysuspend.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/rtc.h>
#include <linux/wakelock.h>
#include <linux/workqueue.h> #include "power.h" enum {
DEBUG_USER_STATE = 1U << ,
DEBUG_SUSPEND = 1U << ,
DEBUG_VERBOSE = 1U << ,
};
static int debug_mask = DEBUG_USER_STATE;
module_param_named(debug_mask, debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP); static DEFINE_MUTEX(early_suspend_lock);
static LIST_HEAD(early_suspend_handlers);
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);
static DEFINE_SPINLOCK(state_lock);
enum {
SUSPEND_REQUESTED = 0x1,
SUSPENDED = 0x2,
SUSPEND_REQUESTED_AND_SUSPENDED = SUSPEND_REQUESTED | SUSPENDED,
};
static int state; void register_early_suspend(struct early_suspend *handler)
{
struct list_head *pos; mutex_lock(&early_suspend_lock);
list_for_each(pos, &early_suspend_handlers) {
struct early_suspend *e;
e = list_entry(pos, struct early_suspend, link);
if (e->level > handler->level)
break;
}
list_add_tail(&handler->link, pos);
if ((state & SUSPENDED) && handler->suspend)
handler->suspend(handler);
mutex_unlock(&early_suspend_lock);
}
EXPORT_SYMBOL(register_early_suspend); void unregister_early_suspend(struct early_suspend *handler)
{
mutex_lock(&early_suspend_lock);
list_del(&handler->link);
mutex_unlock(&early_suspend_lock);
}
EXPORT_SYMBOL(unregister_early_suspend);
 
register_early_suspend(struct early_suspend *handler)会按照level从小到大的方式,将handler依次挂在early_suspend_handlers链表头上。
if (e->level > handler->level)判断链表节点e的level大于要插入的handler的level时就break当前遍历,用list_add_tail(&handler->link, pos);将handler->link插到pos节点的前面。

  static void early_suspend(struct work_struct *work)
{
struct early_suspend *pos;
unsigned long irqflags;
int abort = ; mutex_lock(&early_suspend_lock);
spin_lock_irqsave(&state_lock, irqflags);
if (state == SUSPEND_REQUESTED)
state |= SUSPENDED;
else
abort = ;
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) {
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); suspend_sys_sync_queue();
abort:
spin_lock_irqsave(&state_lock, irqflags);
if (state == SUSPEND_REQUESTED_AND_SUSPENDED)
wake_unlock(&main_wake_lock);
spin_unlock_irqrestore(&state_lock, irqflags);
} static void late_resume(struct work_struct *work)
{
struct early_suspend *pos;
unsigned long irqflags;
int abort = ; mutex_lock(&early_suspend_lock);
spin_lock_irqsave(&state_lock, irqflags);
if (state == SUSPENDED)
state &= ~SUSPENDED;
else
abort = ;
spin_unlock_irqrestore(&state_lock, irqflags); if (abort) {
if (debug_mask & DEBUG_SUSPEND)
pr_info("late_resume: abort, state %d\n", state);
goto abort;
}
if (debug_mask & DEBUG_SUSPEND)
pr_info("late_resume: call handlers\n");
list_for_each_entry_reverse(pos, &early_suspend_handlers, link) {
if (pos->resume != NULL) {
if (debug_mask & DEBUG_VERBOSE)
pr_info("late_resume: calling %pf\n", pos->resume); pos->resume(pos);
}
}
if (debug_mask & DEBUG_SUSPEND)
pr_info("late_resume: done\n");
abort:
mutex_unlock(&early_suspend_lock);
}
early_suspend(struct work_struct *work)和late_resume(struct work_struct *work)分别在第37、38行被声明为了early_suspend_work和late_resume_work,以便在
request_suspend_state(suspend_state_t new_state)中的suspend_work_queue工作队列中使用。

ealry_suspend(struct work_struct *work)中最关键的一句list_for_each_entry(pos, &early_suspend_handlers, link),表示按照level等级排列的顺序,依次调用pos所指向的各个
驱动中注册的ealrysuspend 指向的suspend handler。 late_resume(struct work_struct *work)中list_for_each_entry_reverse(pos, &early_suspend_handlers, link),表示按照level等级的逆序,依次调用pos所指向的各个
驱动中注册的earlysuspend 指向的resume handler。
 
这里有一个很有用的调试信息:pr_info("late_resume: calling %pf\n", pos->resume); %pf可以打印被调函数的函数名。
 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 + , tm.tm_mon + , 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);
} 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);
}
requested_suspend_state = new_state;
spin_unlock_irqrestore(&state_lock, irqflags);
} suspend_state_t get_suspend_state(void)
{
return requested_suspend_state;
}
request_suspend_state(suspend_state_t new_state)会被kernel/power/main.c中的state show调用,main.c将会在下一章详细说明。
request_suspend_state(suspend_state_t new_state)传入的参数new_state的类型suspend_state_t为一个typedef类型的变量:typedef int __bitwise suspend_state_t;

当new_state为0时,request_suspend_state调用queue_work(suspend_work_queue, &late_resume_work);分支进行late唤醒。
当new_state为3时,request_suspend_state调用queue_work(suspend_work_queue, &early_suspend_work);分支进行浅度休眠。 当系统休眠/唤醒时,串口上会出现如下打印:
request_suspend_state: sleep (0->3) at 6442598858519 (2012-02-05 01:31:08.172285684 UTC)
request_suspend_state: wakeup (3->0) at 6464854624023 (2012-02-05 01:31:30.428053438 UTC)
0代表唤醒,3代表休眠。
0->3 从唤醒到休眠(sleep);
3->0 从休眠到唤醒(wakeup);

Linux Power(一): kernel/power/earlysuspend.c的更多相关文章

  1. SQL Server 2014 BI新特性(三)Power Query和Power Map功能预览

    Power Query和Power Map是微软前不久在WPC上发布的Power BI中新的针对Excel的功能.借助这两样功能,自助式BI将更方便你发现和处理数据并且丰富数据的可视化功能. Powe ...

  2. jquery 现实多状态控件 (status & power(2,0)) = power(2,0)

    数据库表设计的时候,会有很些多状态的需求,比如招聘职位需要同时发布到武汉,广州,上海 实现方法有很多种,我选择了在职位表中建一个 int 型字段保存多种状态,这个涉及到一些算法,我要查询武汉和广州的职 ...

  3. Linux电源管理(4)-Power Manager Interface【转】

    本文转载自:http://www.wowotech.net/pm_subsystem/pm_interface.html 1. 前言 Linux电源管理中,相当多的部分是在处理Hibernate.Su ...

  4. 如何测量一个嵌入式Linux系统的功耗/power dissipation/power wastage/consumption

    参考: 1.Linux Circuit Software To Calculate Power Dissipation

  5. Power of Two & Power of Three & Power of Four

     Check Power of 2 Using O(1) time to check whether an integer n is a power of 2. Example For n=4, re ...

  6. Linux - 升级+编译kernel

    For upgrading present kernel to linux-next kernel, we need to follow below steps. 1. Check present k ...

  7. 【Linux】【Kernel】一个简单的内核模块例子

    1.本地主机的参数 zhangjun@zhangjun-virtual-machine:~$ uname -a Linux zhangjun-virtual-machine 4.4.0-31-gene ...

  8. 02.02.02 第2章 制作power bi图表(Power BI商业智能分析)

    ---恢复内容开始--- 02.02.02第2章 制作power bi图表 02.02.02.01 power pivot数据导入 00:08:43 02.02.02.02建立数据透视表 00:11: ...

  9. Linux内核线程kernel thread详解--Linux进程的管理与调度(十)

    内核线程 为什么需要内核线程 Linux内核可以看作一个服务进程(管理软硬件资源,响应用户进程的种种合理以及不合理的请求). 内核需要多个执行流并行,为了防止可能的阻塞,支持多线程是必要的. 内核线程 ...

随机推荐

  1. MySql存储引擎介绍

    MySQL5.5以后默认使用InnoDB存储引擎,其中InnoDB和BDB提供事务安全表,其它存储引擎都是非事务安全表.若要修改默认引擎,可以修改配置文件中的default-storage-engin ...

  2. python----抽象类

    #!/usr/local/python3.5/bin/python3.5 ####实现方法一 class Supper(object): def delegate(self): self.action ...

  3. js 创建对象

    1.工厂模式 function createPerson(name, age, job) { var o = new Object(); o.name = name; o.age = age; o.j ...

  4. 微控制器(MCU)破解秘笈--背景知识

    2.1 硅芯片安全措施的演变 工业控制器的硬件安全措施与嵌入式系统同时开始发展.三十年前的系统是由分离的部件如CPU,ROM,RAM,I/O缓冲器,串口和其他通信与控制接口组成的.如图2-1所示: 图 ...

  5. C 宏定义

    C/C++中宏使用总结 .C/C++中宏总结C程序的源代码中可包括各种编译指令,这些指令称为预处理命令.虽然它们实际上不是C语言的一部分,但却扩展了C程序设计的环境.本节将介绍如何应用预处理程序和注释 ...

  6. Keil C51汉字显示的bug问题(0xFD问题)

    一.缘起 这两天改进MCU的液晶显示方法,采用“即编即显”的思路,编写了一个可以直接显示字符串的程序.如程序调用disstr("我是你老爸");液晶屏上就会显示“我是你老爸”. 二 ...

  7. MVC之ActionResult

    一.所有的Controller都继承自System.Web.Mvc.Controller 目前ASP.NET MVC3默认提供了多种ActionResult的实现,在System.Web.Mvc命名空 ...

  8. SqlCommand类

    一.常用属性 CommandText 获取或设置要对数据源执行的 Transact-SQL 语句.表名或存储过程. CommandTimeout 获取或设置在终止执行命令的尝试并生成错误之前的等待时间 ...

  9. Java 舍入模式 数字的格式化

    舍入模式: UP向远离0的方向舍入 始终对非零舍弃部分前面的数字加 1.此舍入模式始终不会减少计算值的绝对值. 例如:1.6 → 2      -1.6 → -2      1.1 → 2      ...

  10. python爬虫系列之爬取多页gif图像

                   python爬取多页gif图像 作者:vpoet mail:vpoet_sir@163.com #coding:utf-8 import urllib import ur ...