lovealamo 发表于 2012-04-26 20:59

请教,如何实现linux驱动里响应系统poweroff信息。

我现在有个驱动程序,例如叫example.c,编译后是ex.ko。现在我想在这个驱动程序里处理当系统发生关机(poweroff)的事件,我该如何设计,这个关机事件,有可能是用户敲的命令。

我该如何在我这个驱动程序里捕获这个poweroff事件,然后进行相应的处理,然后关机呢????


谢谢。请大家提供个思路或者方法,有小例子更好。

lovealamo 发表于 2012-04-27 14:55

我顶。

冬天的落阳 发表于 2012-04-27 22:31

帮顶。
会不会和电源管理部分有关,我瞎猜的。

WZXZLY 发表于 2012-04-28 09:29

看看内核通知链关机部分,关机部分应该有“关机通知链”,有的话只要在你自己的模块中用链注册函数注册到这个链中就OK了

WZXZLY 发表于 2012-04-28 09:37

刚才看了看源码,在power.c中power_exit()时那个通知链就是你需要注册进去的

chengxuetao 发表于 2012-05-03 15:09

注册通知事件,linux支持的通知事件有以下几种(include/linux/notifier.h):
#define SYS_DOWN        0x0001        /* Notify of system down */
#define SYS_RESTART        SYS_DOWN
#define SYS_HALT        0x0002        /* Notify of system halt */
#define SYS_POWER_OFF        0x0003        /* Notify of system power off */

下面给你个参考代码:
static int example_notifier_call(struct notifier_block *this, unsigned long code, void *_cmd)
{
        int mode = 0;

        if (code == SYS_RESTART)
               {
                  ... ...
               }

        else if (code == SYS_POWER_OFF)
                {
                  ... ...
               }

        return NOTIFY_DONE;
}

static struct notifier_block example_reboot_notifier = {
        .notifier_call = example_notifier_call,
};

chengxuetao 发表于 2012-05-03 15:10

下面的函数用于注册通知
/* register rnotifier*/
register_reboot_notifier(&example_reboot_notifier);
页: [1]
查看完整版本: 请教,如何实现linux驱动里响应系统poweroff信息。