Chinaunix首页 | 论坛 | 博客
  • 博客访问: 929041
  • 博文数量: 376
  • 博客积分: 154
  • 博客等级: 入伍新兵
  • 技术积分: 1558
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-13 08:42
文章分类

全部博文(376)

文章存档

2014年(11)

2013年(88)

2012年(260)

2011年(17)

分类: LINUX

2013-09-24 17:11:25

原文地址:内核通知机制 作者:linux_hope

实现功能:GPIO键值及其状态在另一个模块响应
gpio_keys.c
#include
#ifdef DEBUG
#define dbg_printk(fmt, args...) \
    printk(TAG " %s;%d " fmt , __FUNCTION__, __LINE__, ## args);
#else
#define dbg_printk(fmt, args...)
#endif

static RAW_NOTIFIER_HEAD(KEY_NOTIFIER);

int register_key_notifier(struct notifier_block *nb)
{
    return raw_notifier_chain_register(&KEY_NOTIFIER, nb);
}
EXPORT_SYMBOL(register_key_notifier);

int unregister_key_notifier(struct notifier_block *nb)
{
    return raw_notifier_chain_unregister(&KEY_NOTIFIER, nb);
}
EXPORT_SYMBOL(unregister_key_notifier);

static int key_notifier_call_chain(unsigned long val, void *v)
{
    return raw_notifier_call_chain(&KEY_NOTIFIER, val, v);
}

gpio key_down:
        key_notifier_call_chain(button->code,"down");
gpio_key_up:
        key_notifier_call_chain(0,"up");


xxx.c
#include
extern int register_key_notifier(struct notifier_block *nb);
extern int unregister_key_notifier(struct notifier_block *nb);
static void key_state_response(struct notifier_block *, unsigned long , void *);

static struct notifier_block key_detect_notifier =
{
    .notifier_call = key_state_response,
};

void key_state_response(struct notifier_block *key_block, unsigned long keyval, void *v)
{
    if(keyval == KEY_POWER)
    {
        printk("key power %s\n",v);
    }
    else if(keyval == KEY_VOLUMEUP)
    {
        printk("key volumeup %s\n",v);
    }
    else if(keyval == KEY_VOLUMEDOWN)
    {
        printk("key volumedown %s\n",v);
    }
    else
    {
        printk("key %s\n",v);
    }
}

register_key_notifier(&key_detect_notifier);
unregister_key_notifier(&key_detect_notifier);

阅读(735) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~