button_drv.c驱动文件:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <asm/io.h>                //含有iomap函数iounmap函数
#include <asm/uaccess.h>              //含有copy_from_user函数
#include <linux/device.h>            //含有类相关的处理函数
#include <asm/arch/regs-gpio.h>       //含有S3C2410_GPF0等相关的
#include <linux/irq.h>              //含有IRQ_HANDLED\IRQ_TYPE_EDGE_RISING
#include <asm-arm/irq.h>           //含有IRQT_BOTHEDGE触发类型
#include <linux/interrupt.h>        //含有request_irq、free_irq函数
#include <linux/delay.h>
#include <asm/hardware.h>
#include <linux/poll.h>

#define DRIVER_NAME "button_drv"
#define DEVICE_NAME "button_dev"

int major;

volatile unsigned long *gpfcon;
volatile unsigned long *gpfdat;
volatile unsigned long *gpgcon;
volatile unsigned long *gpgdat;

struct class *button_class;
struct class_device *button_class_device;

unsigned char ev_press;
DECLARE_WAIT_QUEUE_HEAD(button_waitq);     //注册等待队列

struct fasync_struct *button_fasync;       //定义fasync_struct结构体

unsigned char keyVal;

struct pin_desc{
  unsigned int pin;
  unsigned int key_val;
};
struct pin_desc pins_desc[4] = {
  {S3C2410_GPF0, 0x01},
  {S3C2410_GPF2, 0x02},
  {S3C2410_GPG3, 0x03},
  {S3C2410_GPG11, 0x04},
};

irqreturn_t buttons_irq(int irq, void *dev_id)
{
  unsigned int pin_val;
  struct pin_desc *pin_desc = (struct pin_desc *)dev_id;
  pin_val = s3c2410_gpio_getpin(pin_desc->pin);
  if(pin_val)
  {
    keyVal = 0x80 | pin_desc->key_val;
  }
  else
  {
    keyVal = pin_desc->key_val;
  }
  wake_up_interruptible(&button_waitq);
  ev_press = 1;
  kill_fasync(&button_fasync, SIGIO, POLL_IN); //发送信号给进程
  return IRQ_HANDLED;
}

int button_drv_open(struct inode *inode, struct file *file)
{
  int ret;
  ret = request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "S1", &pins_desc[0]);  //注册中断S1, 并将IRQ_EINT0这个引脚配

//置为中断模式, 双边沿触发
  if(ret<0)
  {
    printk("failed 1 button_drv_open");
  }
  ret = request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[1]);
  if(ret<0)
  {
    printk("failed 2 button_drv_open");
  }
  ret = request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "S3", &pins_desc[2]);
  if(ret<0)
  {
    printk("failed 3 button_drv_open");
  }
  ret = request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "S4", &pins_desc[3]);
  if(ret<0)
  {
    printk("failed 4 button_drv_open");
  }
  return 0;
  }

ssize_t button_drv_read(struct file *file, char __user *userbuf, size_t count, loff_t *off)
{
  int ret;

  ret = copy_to_user(userbuf, &keyVal, 1);
  if(ret<0)
  {
    printk("failed 1 button_drv_read \n");
    return -1;
  }
  ev_press = 0;
  return 1;
}

int button_drv_close(struct inode *inode, struct file *file)
{
  free_irq(IRQ_EINT0, &pins_desc[0]);
  free_irq(IRQ_EINT2, &pins_desc[1]);
  free_irq(IRQ_EINT11, &pins_desc[2]);
  free_irq(IRQ_EINT19, &pins_desc[3]);
  return 0;
}

unsigned int button_drv_poll(struct file *file, poll_table *wait)
{
  unsigned int mask = 0;
  poll_wait(file, &button_waitq, wait);
  if(ev_press)
  {
    mask |= POLLIN | POLLRDNORM;
  }
  return mask;
}

int button_drv_fasync(int fd, struct file *file, int on)
{
  int ret;
  ret = fasync_helper(fd, file, on, &button_fasync);
  if(ret<0)
  {
    printk("failed 1 button_drv_fasync \n");
    return ret;
  }
  return 0;
}

struct file_operations button_drv_fops = {
  .owner = THIS_MODULE,
  .open = button_drv_open,
  .read = button_drv_read,
  .release = button_drv_close,
  .poll = button_drv_poll,
  .fasync = button_drv_fasync,
};

int __init button_drv_init(void)
{
  major = register_chrdev(0, DRIVER_NAME, &button_drv_fops);
  if(major<0)
  {
    printk("failed 1 button_drv_init \n");
  }
  button_class = class_create(THIS_MODULE, DEVICE_NAME);
  if(button_class<0)
  {
    printk("failed 2 button_drv_init \n");
  }
  button_class_device = class_device_create(button_class, NULL, MKDEV(major, 0), NULL, DEVICE_NAME);
  if(button_class_device<0)
  {
    printk("failed 3 button_drv_init \n");
  }
  gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
  gpfdat = gpfcon + 1;
  gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
  gpgdat = gpgcon + 1;
  return 0;
}

void __exit button_drv_exit(void)
{
  unregister_chrdev(major, DEVICE_NAME);
  class_device_unregister(button_class_device);
  class_destroy(button_class);
  iounmap(gpfcon);
  iounmap(gpgcon);
}

module_init(button_drv_init);
module_exit(button_drv_exit);

MODULE_LICENSE("GPL");

Makefile文件:

obj-m += timer_drv.o

KERN_DIR = /work/system/linux-2.6.22.6

all:
make -C $(KERN_DIR) M=`pwd` modules

clean:
rm -rf *.o *.ko *.order *.symvers *.mod.c

button_app.c文件:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
#include <signal.h>

static int fd;

void button_signal(int signum)
{
  unsigned char keyVal;
  printf("signal = %d \n", signum);
  read(fd, &keyVal, 1);
  printf("keyVal = 0x%x \n", keyVal);
}

int main(int argc, char **argv)
{
  int oflags;
  char *filename;

  filename = argv[1];
  fd = open(filename, O_RDWR);
  if(fd<0)
  {
    printf("can not open \n");
  }
  signal(SIGIO, button_signal);        //注册一个信号, 启动信号驱动机制
  fcntl(fd, F_SETOWN, getpid());       //将本应用程序的进程号告诉给内核,最终使得驱动程序可以成功发送信号给应用程序
  oflags = fcntl(fd, F_GETFL);         //取得当前的状态
  fcntl(fd, F_SETFL, oflags|FASYNC);      //改变fasync标记, 最终会调用到驱动的fasync->fasync_helper
  while(1)
  {
    sleep(1000);
  }
  return 0;
}

编译生成button_drv.ko和button_app文件,运行./button_app /dev/button_dev

Linux 驱动——Button驱动4(fasync)异步通知的更多相关文章

  1. 入门级的按键驱动——按键驱动笔记之poll机制-异步通知-同步互斥阻塞-定时器防抖

    文章对应视频的第12课,第5.6.7.8节. 在这之前还有查询方式的驱动编写,中断方式的驱动编写,这篇文章中暂时没有这些类容.但这篇文章是以这些为基础写的,前面的内容有空补上. 按键驱动——按下按键, ...

  2. Linux学习 :按键信号 之 异步通知

    一.异步通知概念: 异步通知是指:一旦设备就绪,则主动通知应用程序,应用程序根本就不需要查询设备状态,类似于中断的概念,一个进程收到一个信号与处理器收到一个中断请求可以说是一样的.信号是异步的,一个进 ...

  3. Linux 驱动——Button驱动7(Timer)消抖

    button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/f ...

  4. Linux 驱动——Button驱动6(mutex、NBLOCK、O_NONBLOCK)互斥信号量、阻塞、非阻塞

    button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/f ...

  5. Linux 驱动——Button驱动5(atomic)原子量

    button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/f ...

  6. Linux 驱动——Button驱动3(poll机制)

    button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/f ...

  7. Linux 驱动——Button驱动2

    button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/f ...

  8. Linux 驱动——Button驱动1

    button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/i ...

  9. linux设备驱动归纳总结(三):7.异步通知fasync【转】

    本文转载自:http://blog.chinaunix.net/uid-25014876-id-62725.html linux设备驱动归纳总结(三):7.异步通知fasync xxxxxxxxxxx ...

随机推荐

  1. python内置函数详细描述与实例演示

    python有许多内置函数,列出表格如下 内置函数 abs() delattr() hash() memoryview() set() all() dict() help() min() setatt ...

  2. C#数组--(Array类的属性和方法)

    Array 类是 C# 中所有数组的基类,它是在 System 命名空间中定义.Array 类提供了各种用于数组的属性和方法,可看作扩充了功能的数组(但不等同数组),可以使用Array类的属性来对数组 ...

  3. XV Open Cup named after E.V. Pankratiev. GP of Central Europe (AMPPZ-2014)--J.Cave

    给你一棵树,现在有m个专家,每个专家计划从$a_i$走到$b_i$, 经过的距离不超过$d_i$,现在让你找一个点,使得所有专家的路途都能经过这个点 令$S_i$表示满足第i个专家的所有点,先检查1可 ...

  4. Mac OS X 操作系统下JDK安装与环境变量配置

    1. 下载JDK. 去oracle官网的Java SE Downloads页面(如图 1),下载Mac os版本JDK(如图 2): 图 1 图 2   2. 安装JDK. 下载完成后,双击.dmg文 ...

  5. spring reference

    Spring框架概述 Spring可以轻松创建Java企业应用程序.它提供了在企业环境中使用Java语言所需的一切,支持Groovy和Kotlin作为JVM上的替代语言,并可根据应用程序的需要灵活地创 ...

  6. 微信跳转外部浏览器下载app

    很多朋友问我怎么解决微信内点击链接或扫描二维码可以直接跳出微信在外部浏览器打开网页链接和下载APP,其实这并不难,只要我们实现微信跳转功能即可.下面给大家介绍这个功能 方案实现教程:http://sk ...

  7. [luogu P3391] 文艺平衡树

    [luogu P3391] 文艺平衡树 题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区 ...

  8. Vue的学习

    1.Vue是什么 参考Vue官网,一套用于构建用户界面的渐进式框架. 2.什么是渐进式框架 引用大神的话:“它给你提供足够的optional,但并不主张很多required,也不多做职责之外的事!这就 ...

  9. vue-calendar 基于 vue 2.0 开发的轻量,高性能日历组件

    vue-calendar-component 基于 vue 2.0 开发的轻量,高性能日历组件 占用内存小,性能好,样式好看,可扩展性强 原生 js 开发,没引入第三方库 Why Github 上很多 ...

  10. arrow function

    简介 JavaScript 中,函数可以用箭头语法(”=>”)定义,有时候也叫“lambda表达式”.这种语法主要意图是定义轻量级的内联回调函数.例如: // Arrow function: [ ...