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(&notifier->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(&notifier->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的更多相关文章

  1. Linux 内核通知链随笔【中】

    关于内核通知链不像Netlink那样,既可以用于内核与用户空间的通信,还能用于内核不同子系统之间的通信,通知链只能用于内核不同子系统之间的通信.那么内核通知链到底是怎么工作的?我们如何才能用好通知链? ...

  2. Linux 内核通知链随笔【中】【转】

    转自:http://blog.chinaunix.net/uid-23069658-id-4364171.html 关于内核通知链不像Netlink那样,既可以用于内核与用户空间的通信,还能用于内核不 ...

  3. Linux 内核通知链机制的原理及实现

    一.概念: 大多数内核子系统都是相互独立的,因此某个子系统可能对其它子系统产生的事件感兴趣.为了满足这个需求,也即是让某个子系统在发生某个事件时通知其它的子 系统,Linux内核提供了通知链的机制.通 ...

  4. Linux内核通知链模块

    通知链描写叙述 大多数内核子系统都是相互独立的,因此某个子系统可能对其他子系统产生的事件感兴趣. 为了满足这个需求,也即是让某个子系统在发生某个事件时通知其他的子系统.Linux内核提供了通知链的机制 ...

  5. Linux内核通知链机制的原理及实现【转】

    转自:http://www.cnblogs.com/armlinux/archive/2011/11/11/2396781.html 一.概念: 大多数内核子系统都是相互独立的,因此某个子系统可能对其 ...

  6. Linux内核调试方法总结之内核通知链

    Linux内核通知链notifier 1.内核通知链表简介(引用网络资料)    大多数内核子系统都是相互独立的,因此某个子系统可能对其它子系统产生的事件感兴趣.为了满足这个需求,也即是让某个子系统在 ...

  7. Linux内核基础--事件通知链(notifier chain)

    转载: http://blog.csdn.net/wuhzossibility/article/details/8079025 http://blog.chinaunix.net/uid-277176 ...

  8. Linux内核基础--事件通知链(notifier chain)good【转】

    转自:http://www.cnblogs.com/pengdonglin137/p/4075148.html 阅读目录(Content) 1.1. 概述 1.2.数据结构 1.3.  运行机理 1. ...

  9. Linux内核基础--事件通知链(notifier chain)【转】

    转自:http://blog.csdn.net/wuhzossibility/article/details/8079025 内核通知链 1.1. 概述 Linux内核中各个子系统相互依赖,当其中某个 ...

随机推荐

  1. SAP 2019 TechEd Key Note解读:云时代下SAP从业人员如何做二次开发?

    刚刚过去的在巴塞罗那举行的2019 SAP TechEd,SAP照例向全球广大的SAP生态圈从业者们传达了一些重要的信息,其中一条为:Building Extensions for the Intel ...

  2. Redis 学习-持久化与主从复制

    一.持久化 1. RDB rdb 是 redis 内存到硬盘的快照,用于持久化 ①. 通过执行命令,主动保存快照 save # 执行保存快照,执行时 redis 会处理阻塞状态直至执行完成. bgsa ...

  3. SQL进阶系列之0窗口函数

    窗口函数 What's 窗口函数? 窗口函数也称为OLAP(OnLine Analytical Processing)函数,目前MySQL还不支持. 窗口函数的语法 <窗口函数> OVER ...

  4. docer安装之pure-ftp

    https://hub.docker.com/r/stilliard/pure-ftpd Docker Pure-ftpd Server https://hub.docker.com/r/stilli ...

  5. 0029redis单机版环境搭建

    linux环境下安装单机版redis,主要分为如下几步: 1. 安装gcc 2.下载安装包 3.解压安装包 4.进入解压目录并执行make和make install命令 5.查看默认安装目录 6.更改 ...

  6. vue input只允许输入数字

    template: <input type="text" v-model="pageIndex" @keyup="inputChange&quo ...

  7. 十六.maven自动化构建protobuf代码依赖

    protobuf在序列化和反序列化中的优势: 1):序列化后体积相比Json和XML很小,适合网络传输2):支持跨平台多语言3):消息格式升级和兼容性还不错4):序列化反序列化速度很快,快于Json的 ...

  8. URL路径详解

    1.url http://localhost:8080/Test/1.html url表示浏览器访问服务器的网络路径   http:相当于人们交流时候的语言   :// 分隔符   localhost ...

  9. 多线程实现的方式二实现Rannable

    package thread; class Thread2 implements Runnable{ private String name; public Thread2(String name) ...

  10. c语言中,如果将无符号数转换为有符号数

    在使用ti的adc芯片ads1259时,芯片是24为数据格式保存的,其中最高位是符号位,因此可以理解为是有符号数据,但是在嵌入式系统中,没有直接24位的变量,因此使用32的无符号先保存24位的数据. ...