明确为什么要引用poll机制?

while(1)

{

  read(fd,&key_val,1);//如果没有按键按下,它会一直在等待。现在想做这么一件事情:如果5s后,没有按键按下,它就会返回。此时就要用到poll机制

}

当应用程序调用poll时,会相应的调用内核空间的sys_poll
sys_poll
  do_sys_poll
    poll_initwait(&table)
    void poll_initwait(struct poll_wqueues *pwq)
      init_poll_funcptr(&pwq->pt, __pollwait);//下面这个函数为该函数的具体实现
      static inline void init_poll_funcptr(poll_table *pt, poll_queue_proc qproc)
      {
        pt->qproc = qproc;//此时就相当于:pt->qproc = __pollwait,所以指针
        //qproc就指向了函数__pollwait
      }
      //接下来看一下__pollwait的具体实现:
      static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,poll_table *p)
      {
        struct poll_table_entry *entry = poll_get_entry(p);
        if (!entry)
        return;
        get_file(filp);
        entry->filp = filp;
        entry->wait_address = wait_address;
        init_waitqueue_entry(&entry->wait, current);
        add_wait_queue(wait_address, &entry->wait);
      }
  从上面的分析可知,poll_initwait(&table)函数只实现了将当前进程加入到wait_address队列中
  do_poll(nfds, head, &table, timeout),会进入一个死循环,在死循环里调用了do_pollfd(pfd, pt)
    do_pollfd(pfd, pt)
      mask = file->f_op->poll(file, pwait);//unsigned int (*poll) (struct file *, struct poll_table_struct *);
      在驱动程序中的file_operations结构体中,定义了.poll=forth_drv_poll, 从而使得poll指针指向了函数forth_drv_poll,
      所以在执行poll函数时,实际上是调用了驱动程序中的forth_drv_poll这个函数。在forth_drv_poll这个函数中,会
      调用poll_wait(file, &button_waitq, wait),看一下该函数的具体实现:
      void poll_wait(struct file * filp, wait_queue_head_t * wait_address, poll_table *p)
      {
        if (p && wait_address)
        p->qproc(filp, wait_address, p);//在上面已经分析过,p->qproc指向函数__pollwait。
        因此这条语句相当于执行__pollwait(filp, wait_address, p),从上面的分析可以看出,将当前进程加入到
        wait_address等待队列中。
      }
  注意:在我们的驱动程序中会有一个返回值,假设为mask1.那么此时mask=mask1,而mask又将返回给do_pollfd(pfd, pt)这个函数。内核中有这样一段程序:
          for(;;)
            {
              if (do_pollfd(pfd, pt)) {
                count++;
                pt = NULL;
            }如果mask非零,count++,表示按键按下。

            /*若count不为0或者超时或者有事件发生则会跳出死循环*/
            if (count || !*timeout || signal_pending(current))
            break;
            /*若count为0且未超时且无事件发生则会休眠__timeout时间*/
            schedule_timeout(__timeout);当休眠到timeout这段时间之后,还是没有任何事件发生,会再次进入循环中
            ,当到达if (count || !*timeout || signal_pending(current))时,会跳出循环。
}

应用程序:

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

int main(int argc, char **argv)
{
  int fd;
  unsigned char key_val;
  int ret;

  struct pollfd fds[1];   //poll机制可以同时查看多个文件,在这里我们只查询一个驱动文件。

  fd = open("/dev/buttons", O_RDWR);
  if (fd < 0)
  {
    printf("can't open!\n");
  }

  fds[0].fd = fd;  //所要查询的驱动文件
  fds[0].events = POLLIN; //POLLIN表示有数据等待读取
  while (1)
 {
    ret = poll(fds, 1, 5000);//1表示只有一个文件
    if (ret == 0) //返回值为0,表示超时
  {
    printf("time out\n");
  }
    else
  {
    read(fd, &key_val, 1);
    printf("key_val = 0x%x\n", key_val);
  }
 }

  return 0;
}

/*注意:与本节的程序无关,只是更好的熟悉poll函数

poll函数原型:

#include <poll.h>

int poll(struct pollfd fd[], nfds_t nfds, int timeout);

struct pollfd的结构如下:

struct pollfd{

int fd; // 文件描述符
short event;// 请求的事件
short revent;// 返回的事件

}

*/

驱动程序:

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

static struct class *forthdrv_class;
static struct class_device *forthdrv_class_dev;

volatile unsigned long *gpfcon;
volatile unsigned long *gpfdat;

volatile unsigned long *gpgcon;
volatile unsigned long *gpgdat;

static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

/* 中断事件标志, 中断服务程序将它置1,forth_drv_read将它清0 */
static volatile int ev_press = 0;

struct pin_desc{
unsigned int pin;
unsigned int key_val;
};

/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
/* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
static unsigned char key_val;

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

/*
* 确定按键值
*/
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
  struct pin_desc * pindesc = (struct pin_desc *)dev_id;
  unsigned int pinval;

  pinval = s3c2410_gpio_getpin(pindesc->pin);

  if (pinval)
  {
    /* 松开 */
    key_val = 0x80 | pindesc->key_val;
  }
  else
  {
    /* 按下 */
    key_val = pindesc->key_val;
  }

  ev_press = 1; /* 表示中断发生了 */
  wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */

  return IRQ_RETVAL(IRQ_HANDLED);
}

static int forth_drv_open(struct inode *inode, struct file *file)
{
  /* 配置GPF0,2为输入引脚 */
  /* 配置GPG3,11为输入引脚 */
  request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[0]);
  request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "S3", &pins_desc[1]);
  request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "S4", &pins_desc[2]);
  request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "S5", &pins_desc[3]);

  return 0;
}

ssize_t forth_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
  if (size != 1)
  return -EINVAL;

  /* 如果没有按键动作, 休眠 */
  wait_event_interruptible(button_waitq, ev_press);

  /* 如果有按键动作, 返回键值 */
  copy_to_user(buf, &key_val, 1);
  ev_press = 0;

  return 1;
}

int forth_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;
}

static unsigned forth_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;
}

static struct file_operations sencod_drv_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = forth_drv_open,
.read = forth_drv_read,
.release = forth_drv_close,
.poll = forth_drv_poll,
};

int major;
static int forth_drv_init(void)
{
  major = register_chrdev(0, "forth_drv", &sencod_drv_fops);

  forthdrv_class = class_create(THIS_MODULE, "forth_drv");

  forthdrv_class_dev = class_device_create(forthdrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/buttons */

  gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
  gpfdat = gpfcon + 1;

  gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
  gpgdat = gpgcon + 1;

  return 0;
}

static void forth_drv_exit(void)
{
  unregister_chrdev(major, "forth_drv");
  class_device_unregister(forthdrv_class_dev);
  class_destroy(forthdrv_class);
  iounmap(gpfcon);
  iounmap(gpgcon);
  return 0;
}

module_init(forth_drv_init);

module_exit(forth_drv_exit);

MODULE_LICENSE("GPL");

转载于:https://www.cnblogs.com/-glb/p/7077019.html

字符设备驱动程序之poll机制(韦大仙)相关推荐

  1. 韦东山驱动视频笔记——3.字符设备驱动程序之poll机制

    linux内核版本:linux-2.6.30.4 目的:我们在中断方式的按键应用程序中,如果没有按键按下,read就会永远在那等待,所以如果在这个程序里还想做其他事就不可能了.因此我们这次改进它,让它 ...

  2. 字符设备驱动程序之poll机制

    前面的按键中断驱动,只能实现当有按键按下的时候,输出,平时cpu处于休眠状态.如果我想实现,休眠一段时间执行一些指令,当有中断发生时,cpu又可以立即响应.那就得用poll机制. poll机制分析 韦 ...

  3. 字符设备驱动程序——点亮、熄灭LED操作

    2019独角兽企业重金招聘Python工程师标准>>> 字符设备:是指只能一个字节一个字节读写的设备,不能随机读取设备内存中的某一数据,读取数据需要按照先后数据.字符设备是面向流的设 ...

  4. 字符设备驱动程序框架

    1, 设备号的内部表示形式 类型:dev_t 32=12(主设备号) + 20(次设备号) 相关宏:<linux/kdev_t.h> MAJOR(dev_t dev) MINOR(dev_ ...

  5. 字符设备驱动程序的传统写法

    以led驱动程序为例,介绍字符设备驱动程序的传统写法. 驱动程序: 程序代码来源于韦老大视频代码 1 #include <linux/module.h> 2 #include <li ...

  6. i.MX6ULL学习笔记--字符设备驱动程序

    i.MX6ULL学习笔记--字符设备驱动程序 简介 1.驱动的配置过程 1.1设备号 1.2哈希表-chrdevs 1.3哈希表-obj_map->probes 1.4文件操作接口 1.5简单了 ...

  7. 第12课第3节 字符设备驱动程序之查询方式的按键驱动程序

    第12课第3节 字符设备驱动程序之查询方式的按键驱动程序 cat /proc/devices //查询主设备号 insmod ./second_drv.ko ls /dev/button -l pos ...

  8. 字符设备驱动0:一个简单但完整的字符设备驱动程序

    参考: linux设备驱动程序之简单字符设备驱动 [很详细,必看]http://www.cnblogs.com/geneil/archive/2011/12/03/2272869.html //在驱动 ...

  9. 第12课第2.2节 字符设备驱动程序之LED驱动程序_测试改进

    第12课第2.2节 字符设备驱动程序之LED驱动程序_测试改进 //仅用flash上的根文件系统启动后,手工MOUNT NFS mount -t nfs -o nolock,vers=2 192.16 ...

最新文章

  1. 安全产品研发与落地的一些方法与思考
  2. oracle中 游标实例
  3. 关于JAVA编译时找不到自定义包的问题
  4. oracle第一次使用语句创建作业失败记
  5. 数学建模学习笔记——图论最短路径
  6. [error] error while loading Consumer, class file '/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.25-5.b18.fc2
  7. SQL 单一用户(解决)
  8. linux正向连接shell_[经验] Linux 怎么连接 Xshell?
  9. Java并发——线程安全
  10. Packet Tracer 5.0实验(二) 交换机的Telnet远程登录设置
  11. [置顶] C#中给Label控件设置BackgroundImage属性
  12. 敏感词库快速添加到mysql数据库,并在页面使用方法过滤敏感词
  13. 开拓者队医当选年度最佳 球迷:罗伊情何以堪
  14. Serverless Job—— 传统任务新变革
  15. php国际象棋棋盘,php趣味编程 - php输出国际象棋棋盘 - 小徐
  16. 计算机查找全部文件,一键找到你电脑中的所有文件
  17. 网站丨这四个网站好像有点好玩
  18. EAS中的EAS License与Apusic的License区别
  19. C++20中的协程(Coroutine)
  20. 2021.10月自考

热门文章

  1. pytorch nn.Linear
  2. java Spring beans
  3. C语言 底层IO 输入输出
  4. flask 自定义错误页面
  5. date_range
  6. python 控制语句
  7. 微投抖的1080_1080P不到三千元 微投价格战竟如此激烈
  8. windows安装HTK3.4.1
  9. 宏碁(acer)被攻击:黑客索要 3.25 亿元赎金
  10. linux7 vi 末行 快捷键,vi 常用操作快捷键