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. 安卓开发之常见Handler API和 定时器的使用

    package com.lidaochen.test; import android.os.Bundle; import android.os.Handler; import android.supp ...

  2. JS去除字符串左右两端的空格(转载)

    来源:https://www.cnblogs.com/fanyf/p/3785387.html var str='     测试     '; 一.函数 <script type="t ...

  3. sqlserver 将一个表中的某些字段更新到另一个表中(转载)

    来源:https://blog.csdn.net/qq_23888451/article/details/86615555 https://blog.csdn.net/cyxinda/article/ ...

  4. mysql基础指令知识

    桌面指令(cmd)进入mysql客户端 第一步:安装mysql,配置环境变量 第二步:手动开启服务 第三步:输入如下指令: mysql [-h localhost -P 3306] -u  用户名 - ...

  5. c# 比较字符串

  6. sql语句,数据库中,同表添加,主键不同,数据相同。

    insert into tablename(各个字段名) select #新主键值,其他字段名 from tablename where No = #表中主键值

  7. [LeetCode]1221. Split a String in Balanced Strings

    Balanced strings are those who have equal quantity of 'L' and 'R' characters. Given a balanced strin ...

  8. xtarbackup 简单恢复

    xtrbackup Xtrabackup安装 #下载epel源 wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/ep ...

  9. 1216 Vue基础

    目录 前端框架 Vue 1.简介 1.1 优点 2 使用 2.1 基础 2.2 文本指令 2.3 事件指令 2.4 属性指令 JS面向对象补充 前端框架 angular ---更新程度太快,且不向下兼 ...

  10. LINQ查询表达式(5) - LINQ Null值处理&异常处理

    查询表达式中处理Null值 此示例演示如何处理源集合中可能的 null 值. 诸如 IEnumerable<T> 等对象集合可能包含值为 null 的元素. 如果源集合为 null 或包含 ...