/* * Copyright (C) 2011-2014 MediaTek Inc. * * This program is free software: you can redistribute it and/or modify it under the terms of the * GNU General Public License version 2 as published by the Free Software Foundation. * * 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. * * You should have received a copy of the GNU General Public License along with this program. * If not, see <http://www.gnu.org/licenses/>. */ #include <linux/module.h> #include <linux/list.h> #include <linux/spinlock.h> #include "SCP_power_monitor.h" #include "scp_helper.h" static LIST_HEAD(power_monitor_list); static DEFINE_SPINLOCK(pm_lock); static atomic_t power_status = ATOMIC_INIT(SENSOR_POWER_DOWN); void scp_power_monitor_notify(uint8_t action, void *data) { struct scp_power_monitor *c; unsigned long flags; spin_lock_irqsave(&pm_lock, flags); list_for_each_entry(c, &power_monitor_list, list) { WARN_ON(c->notifier_call == NULL); c->notifier_call(action, data); pr_debug("scp_power_monitor_notify, module name:%s notify\n", c->name); } switch (action) { case SENSOR_POWER_DOWN: atomic_set(&power_status, SENSOR_POWER_DOWN); break; case SENSOR_POWER_UP: atomic_set(&power_status, SENSOR_POWER_UP); break; } spin_unlock_irqrestore(&pm_lock, flags); } int scp_power_monitor_register(struct scp_power_monitor *monitor) { int err = 0; struct scp_power_monitor *c; WARN_ON(monitor->name == NULL || monitor->notifier_call == NULL); spin_lock_irq(&pm_lock); list_for_each_entry(c, &power_monitor_list, list) { if (!strcmp(c->name, monitor->name)) { err = -1; goto out; } } list_add(&monitor->list, &power_monitor_list); if (atomic_read(&power_status) == SENSOR_POWER_UP) { pr_debug("scp_power_monitor_notify, module name:%s notify\n", monitor->name); monitor->notifier_call(SENSOR_POWER_UP, NULL); } spin_unlock_irq(&pm_lock); return err; out: pr_err("%s scp_power_monitor_register fail\n", monitor->name); spin_unlock_irq(&pm_lock); return err; } int scp_power_monitor_deregister(struct scp_power_monitor *monitor) { if (WARN_ON(list_empty(&monitor->list))) return -1; spin_lock_irq(&pm_lock); list_del(&monitor->list); spin_unlock_irq(&pm_lock); return 0; }分析代码
时间: 2024-04-23 10:24:58 浏览: 96
N96.rar_*N96_mt63*_visual c_诺基亚
这段代码是一个基于 Linux 内核的电源监控模块,主要包含以下几个部分:
1. 头文件引入和宏定义:包括了一些必要的头文件和宏定义,如模块、链表、自旋锁等。
2. 结构体定义:定义了一个名为 scp_power_monitor 的结构体,包含了模块名称、通知回调函数等信息。
3. 全局变量定义:定义了一个名为 power_monitor_list 的链表和一个名为 power_status 的原子变量,分别用于存储已注册的电源监控模块和当前电源状态。
4. scp_power_monitor_notify 函数:用于通知所有已注册的电源监控模块有关电源状态的改变,通过遍历链表来调用每个模块的通知回调函数来实现。同时,根据 action 参数来更新 power_status 变量的值。
5. scp_power_monitor_register 函数:用于注册电源监控模块,将其添加到 power_monitor_list 链表中。如果已经存在同名模块,则返回错误;否则添加到链表中,并且如果当前电源状态为 SENSOR_POWER_UP,则调用该模块的通知回调函数来提醒其电源已经打开。
6. scp_power_monitor_deregister 函数:用于注销电源监控模块,从 power_monitor_list 链表中删除该模块的信息。
总体来说,这段代码实现了一个简单的电源监控模块,在需要监控电源状态的场景下可以作为一个可靠的工具使用。
阅读全文