button_drv.c驱动文件:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <asm/io.h> 
#include <asm/uaccess.h> 
#include <linux/device.h> 
#include <asm/arch/regs-gpio.h> 
#include <linux/irq.h> 
#include <asm-arm/irq.h> 
#include <linux/interrupt.h> 
#include <linux/delay.h>
#include <asm/hardware.h>
#include <linux/poll.h>

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

int major;

static DECLARE_MUTEX(button_lock); //定义互斥锁

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;

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;
  if(file->f_flags&O_NONBLOCK)                               //非阻塞
  {
    if(down_trylock(&button_lock))                //获取信号量, 失败返回非0
    {
      printk("failed 1 button_drv_open \n");
      return -EBUSY;
    }
  }
  else                                 //阻塞
  {
    down(&button_lock);                       //获取信号量, 如果无法获取则休眠
  }

  ret = request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "S1", &pins_desc[0]);
  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;

  if(file->f_flags&O_NONBLOCK)
  {
    if(ev_press!=1) //没有按键按下直接返回
    {
      printk("failed 1 button_drv_read \n");
      return -EAGAIN;
    }
  }
  else //阻塞
  {
    wait_event_interruptible(button_waitq, ev_press);           //如果按键没有动作则进入休眠
  }

  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]);
  up(&button_lock);                                 //释放互斥信号量
  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_1.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); 
  while(1)
  {
    sleep(1000);
  }
  return 0;
}

button_app_2.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|O_NONBLOCK); //非阻塞
  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);
  while(1)
  {
    sleep(1000);
  }
  return 0;
}

编译生成button_drv.ko和button_app_1、button_app_2文件,运行./button_app_1 /dev/button_dev或./button_app_2 /dev/button_dev

Linux 驱动——Button驱动6(mutex、NBLOCK、O_NONBLOCK)互斥信号量、阻塞、非阻塞的更多相关文章

  1. [arm驱动]Linux内核开发之阻塞非阻塞IO----轮询操作【转】

    本文转载自:http://7071976.blog.51cto.com/7061976/1392082 <[arm驱动]Linux内核开发之阻塞非阻塞IO----轮询操作>涉及内核驱动函数 ...

  2. 🍛 餐厅吃饭版理解 IO 模型:阻塞 / 非阻塞 / IO 复用 / 信号驱动 / 异步

    IO 概念 一个基本的 IO,它会涉及到两个系统对象,一个是调用这个 IO 的进程对象,另一个就是系统内核 (kernel).当一个 read 操作发生时,它会经历两个阶段: 通过 read 系统调用 ...

  3. linux基础编程:IO模型:阻塞/非阻塞/IO复用 同步/异步 Select/Epoll/AIO(转载)

      IO概念 Linux的内核将所有外部设备都可以看做一个文件来操作.那么我们对与外部设备的操作都可以看做对文件进行操作.我们对一个文件的读写,都通过调用内核提供的系统调用:内核给我们返回一个file ...

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

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

  5. Linux驱动技术(五) _设备阻塞/非阻塞读写

    等待队列是内核中实现进程调度的一个十分重要的数据结构,其任务是维护一个链表,链表中每一个节点都是一个PCB(进程控制块),内核会将PCB挂在等待队列中的所有进程都调度为睡眠状态,直到某个唤醒的条件发生 ...

  6. Linux驱动技术(五) _设备阻塞/非阻塞读写【转】

    转自:http://www.cnblogs.com/xiaojiang1025/p/6377925.html 等待队列是内核中实现进程调度的一个十分重要的数据结构,其任务是维护一个链表,链表中每一个节 ...

  7. Linux IO 同步/异步 阻塞/非阻塞

    同步IO:导致请求进程阻塞,直到IO操作完成: 是内核通知我们何时进行启动IO操作,而实际的IO操作需要当前进程本身阻塞完成: 包括:阻塞式IO模型,非阻塞式IO模型,IO复用模型,信号驱动式IO模型 ...

  8. Linux IO模型(同步异步阻塞非阻塞等)的几篇好文章

    聊聊同步.异步.阻塞与非阻塞聊聊Linux 五种IO模型聊聊IO多路复用之select.poll.epoll详解 ​

  9. [uart]linux串口的阻塞非阻塞切换

    比如写的时候设置为阻塞,读的时候设置为非阻塞,就需要下面的切换方式 1.获取文件的flags,即open函数的第二个参数: flags = fcntl(fd,F_GETFL,0); 2.设置文件的fl ...

随机推荐

  1. useragent大全

    分享几个常见的User-Agent,复制粘贴过来的,谢谢原创. window.navigator.userAgent 1) Chrome Win7: Mozilla/5.0 (Windows NT 6 ...

  2. Word中如何删除目录页的页码

    ---恢复内容开始--- word中插入目录之后想要为每页添加页码,如果我们直接添加页码的话会出现目录是第一页,正文部分的页码是从2开始而不是1,用下面的方法就可以解决 首先让文档中的所有符号可见 第 ...

  3. 一个axios的简单教程

    转载于:https://www.jianshu.com/p/13cf01cdb81f 转载仅供个人学习 首先要明白的是axios是什么:axios是基于promise(诺言)用于浏览器和node.js ...

  4. HTTP Basic和Digest认证介绍与计算

    一.说明 web用户认证,最开始是get提交+把用户名密码存放在客户端的cookie中的形式:在意识到这样不安全之后逐渐演变成了post提交+把用户凭证放到了服务端的session中的形式(当然ses ...

  5. c# .net core 设置缓存

    1.开启ResponseCaching的缓存(ResponseCaching相当于老版本的OutPutCache): 在Startup.cs文件中设置: public void ConfigureSe ...

  6. Tensorflow常用的函数:tf.cast

    1.tf.cast(x,dtype,name) 此函数的目的是为了将x数据,准换为dtype所表示的类型,例如tf.float32,tf.bool,tf.uint8等 example:  import ...

  7. unity中的UGUI一些组件的使用

    一.Toggle Group(Script) LeftButtons上添加Toggle Group组件,属性Allow Switch Off打对勾,代表它的所有子物体上带有Toggle组件的属性Is ...

  8. day43-python消息队列二-queue模块

    Python提供了Queue模块来专门实现消息队列Queue对象 Queue对象实现一个fifo队列(其他的还有lifo.priority队列,这里不再介绍).queue只有maxsize一个构造参数 ...

  9. mvc route .html 后缀 404

    <system.webServer>    <validation validateIntegratedModeConfiguration="false" /&g ...

  10. STL空间配置器、vector、list、deque、map复习

    本文写于2017-03-03,从老账号迁移到本账号,原文地址:https://www.cnblogs.com/huangweiyang/p/6440830.html STL的六大组件:容器.算法.迭代 ...