[Linux] 内核通知链 notifier
Linux 内核中每个模块之间都是独立的,如果模块需要感知其他模块的事件,就需要用到内核通知链。
最典型的通知链应用就是 LCD 和 TP 之间,TP 需要根据 LCD 的亮灭来控制是否打开关闭触摸功能。
通俗的讲,LCD 会创建一个函数链表,TP 会将 suspend 和 resume 函数添加到链表中,当 LCD 发生亮灭变化时,会根据情况执行链表上所有对应的函数,函数会根据不同的动作执行 TP 的 suspend 和 resume 函数。
下面参考 TP 写一个内核通知链 demo。
#include <linux/module.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/fb.h>
#include <linux/notifier.h> // 构造dmeo的结构体,包括需要执行的函数
struct demo_device {
struct notifier_block fb_notif;
void (*demo_suspend)(struct demo_device *dev);
void (*demo_resume)(struct demo_device *dev);
struct mutex ops_lock;
}; struct demo_device demo; // 当LCD状态变化时执行的函数
static inline int fb_notifier_callback(struct notifier_block *self,
unsigned long action, void *data)
{
struct demo_device *notifier;
struct fb_event *event = data;
int blank_mode;
int ret = ; // 根据参数fb_notif查找结构体demo_device的首地址
notifier = container_of(self, struct demo_device, fb_notif); mutex_lock(¬ifier->ops_lock); switch (action) {
// LCD灭屏
case FB_EARLY_EVENT_BLANK:
blank_mode = *((int *)event->data);
if (blank_mode != FB_BLANK_UNBLANK)
notifier->demo_suspend(notifier);
break; // LCD亮屏
case FB_EVENT_BLANK:
blank_mode = *((int *)event->data);
if (blank_mode == FB_BLANK_UNBLANK)
notifier->demo_resume(notifier);
break; default:
break;
} mutex_unlock(¬ifier->ops_lock); if (ret < )
{
printk("demo_notifier_callback error action = %x, blank_mode = %x\n", (int)action, blank_mode);
return ret;
} return NOTIFY_OK;
} static inline int demo_register_fb(struct demo_device *dev)
{
memset(&dev->fb_notif, , sizeof(dev->fb_notif));
// 给回调函数赋值,也就是LCD状态变化时执行的函数
dev->fb_notif.notifier_call = fb_notifier_callback;
mutex_init(&dev->ops_lock); // 将demo加入LCD的内核通知链
return fb_register_client(&dev->fb_notif);
} static inline void demo_unregister_fb(struct demo_device *dev)
{
// 将demo从LCD的内核通知链删除
fb_unregister_client(&dev->fb_notif);
} static void demo_early_suspend(struct demo_device *dev)
{
printk("%s\n", __func__);
} static void demo_early_resume(struct demo_device *dev)
{
printk("%s\n", __func__);
} static int __init demo_init(void)
{
printk("%s\n", __func__); demo.demo_suspend = demo_early_suspend;
demo.demo_resume = demo_early_resume; demo_register_fb(&demo); return ;
} static void __exit demo_exit(void)
{
printk("%s\n", __func__); demo_unregister_fb(&demo);
} module_init(demo_init);
module_exit(demo_exit); MODULE_DESCRIPTION("Notifier Demo Driver");
MODULE_AUTHOR("AaronLee");
MODULE_LICENSE("GPL");
[Linux] 内核通知链 notifier的更多相关文章
- Linux 内核通知链随笔【中】
关于内核通知链不像Netlink那样,既可以用于内核与用户空间的通信,还能用于内核不同子系统之间的通信,通知链只能用于内核不同子系统之间的通信.那么内核通知链到底是怎么工作的?我们如何才能用好通知链? ...
- Linux 内核通知链随笔【中】【转】
转自:http://blog.chinaunix.net/uid-23069658-id-4364171.html 关于内核通知链不像Netlink那样,既可以用于内核与用户空间的通信,还能用于内核不 ...
- Linux 内核通知链机制的原理及实现
一.概念: 大多数内核子系统都是相互独立的,因此某个子系统可能对其它子系统产生的事件感兴趣.为了满足这个需求,也即是让某个子系统在发生某个事件时通知其它的子 系统,Linux内核提供了通知链的机制.通 ...
- Linux内核通知链模块
通知链描写叙述 大多数内核子系统都是相互独立的,因此某个子系统可能对其他子系统产生的事件感兴趣. 为了满足这个需求,也即是让某个子系统在发生某个事件时通知其他的子系统.Linux内核提供了通知链的机制 ...
- Linux内核通知链机制的原理及实现【转】
转自:http://www.cnblogs.com/armlinux/archive/2011/11/11/2396781.html 一.概念: 大多数内核子系统都是相互独立的,因此某个子系统可能对其 ...
- Linux内核调试方法总结之内核通知链
Linux内核通知链notifier 1.内核通知链表简介(引用网络资料) 大多数内核子系统都是相互独立的,因此某个子系统可能对其它子系统产生的事件感兴趣.为了满足这个需求,也即是让某个子系统在 ...
- Linux内核基础--事件通知链(notifier chain)
转载: http://blog.csdn.net/wuhzossibility/article/details/8079025 http://blog.chinaunix.net/uid-277176 ...
- Linux内核基础--事件通知链(notifier chain)good【转】
转自:http://www.cnblogs.com/pengdonglin137/p/4075148.html 阅读目录(Content) 1.1. 概述 1.2.数据结构 1.3. 运行机理 1. ...
- Linux内核基础--事件通知链(notifier chain)【转】
转自:http://blog.csdn.net/wuhzossibility/article/details/8079025 内核通知链 1.1. 概述 Linux内核中各个子系统相互依赖,当其中某个 ...
随机推荐
- python实战项目
没有一个完整的项目开发过程,是不会对整个开发流程以及理论知识有牢固的认知的,对于怎样将所学的理论知识应用到实际开发中更是不得而知了! 以上就是我们在学习过程中必须要有项目实战开发经验的原因,其实无论项 ...
- SetCurrentCellAddressCore 函数的可重入调用
绑定数据在线程中 private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { if (Di ...
- IDEA中导入Maven模块
IDEA中导入Maven模块方式有二种: 1)批量添加,不可添加文件夹 2)单个添加,可添加任意文件
- CentOS 7.x 配置静态 IP 并启用
centos 7.x 配置静态 IP 并启用 0 问题由来 通过查询本机的IP,发现本机并没有有效IP: [pan@localhost ~]$ ip addr 所以,我们需要设置本机的静态IP,并启用 ...
- 适合公司和个人的目标管理方法:OKR!
1.定义 OKR就是Objectives and Key Results的简称,包括目标(Objectives)和关键结果(Key Results)两个要素. 2.目的 就公司和团队而言 ...
- python3 xml_to_dict、dict_to_xml等互相转换的方法
from basic_config_vars.config_vars import moviesAllFilePath,moviesFilePath #自定义的一些文件变量 import xmltod ...
- djangCrm
---恢复内容开始--- 一> 在数据库进行循环取多对多 def get_classlist(self): l=[] for cls in self.class_list.all(): l.ap ...
- 《你说对就队》第七次作业:团队项目设计完善&编码
<你说对就队>第七次作业:团队项目设计完善&编码 项目 内容 这个作业属于哪个课程 [教师博客主页链接] 这个作业的要求在哪里 [作业链接地址] 团队名称 <你说对就队> ...
- 配置jdk和环境变量
1.官网下载jdk1.8,默认安装即可 2.JAVE_HOME:jdk安装目录 path:C:;%JAVA_HOME%\bin; C:;%JAVA_HONE%\jre\bin;(当dos界面输入命令 ...
- tcp的三次握手和四次挥手转自https://www.jianshu.com/p/d3725391af59
三次握手(three-way handshaking) 1.背景:TCP位于传输层,作用是提供可靠的字节流服务,为了准确无误地将数据送达目的地,TCP协议采纳三次握手策略. 2.原理: 1)发送端首先 ...