[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 数据类型 常用法方
python 数据类型 常用法方 upper() 大写 str lower() 小写 str strip() rstrip() lstrip() 去除字符两边的空格 去右边 左边空白 str repl ...
- Js网站开发学习第一天
1.登录时,记住密码单选框,鼠标移上去显示div里的内容,移开则消失: <head> <meta http-equiv="Content-Type" conten ...
- Python JSON的简单使用
1 json简介 1.1 json是什么? JSON(JavaScript Object Notation)是一种轻量级的数据交换格式. “在JSON出现之前,大家一 ...
- http状态码记录
一些常见的状态码为: 200 - 服务器成功返回网页404 - 请求的网页不存在503 - 服务不可用详细分解: 1xx(临时响应)表示临时响应并需要请求者继续执行操作的状态代码. 代码 说明100 ...
- Centos部署项目
nginx + virtualenv + uwsgi + django + mysql + supervisor 部署项目 一.安装Python3 二.安装MariaDB,并授权远程 grant al ...
- IDEA实用教程(七)—— IDEA的断点调试
IDEA实用教程(七)-- IDEA的断点调试 23/100 发布文章 qq_41684621 六. IDEA的断点调试 打断点 在行号的右侧点击鼠标左键,出现红色圆形图标,说明已经被打上断点 Deb ...
- 让 Python 代码更易维护的七种武器——代码风格(pylint、Flake8、Isort、Autopep8、Yapf、Black)测试覆盖率(Coverage)CI(JK)
让 Python 代码更易维护的七种武器 2018/09/29 · 基础知识 · 武器 原文出处: Jeff Triplett 译文出处:linux中国-Hank Chow 检查你的代码的质 ...
- MySQL延迟 查询主表
在主外键表存在关系的时候如果加上"lazy=true"的话,则表明延迟,即只查询主表中的内容,而不查询外键表中的内容. <hibernate-mapping> < ...
- mysql导出PDM文件步骤
打开mysql把表导出成sql文件(如:service.sql) powerdesigner:选择 File--->Reverse Engineer--->Database 重命名(mod ...
- oracle 查询表重复数据 并 删除保留一条
语法:select count(字段名),字段名 from 表名 group by 字段名 having count(字段名)>1 实例: select count(name),name ...