Linux高级字符设备之Poll操作

在用户程序中,select()和poll()也是与设备阻塞与非阻塞访问息息相关的,使用非阻塞I/O的应用程序通常会使用select和poll系统调用查询是否可对设备进行无阻塞的访问。select系统调用最终会引发设备驱动中的poll函数被执行。

一、select()系统调用:
用于多路监控,当没有一个文件满足要求时,select将阻塞调用进程。
1.select()原型:

int select(int maxfdp,fd_set *readfds,fd_set *writefds,fd_set *exceptfds,const struct timeval *timeout);
/*
*@maxfd : 需要检查的文件描述符个数,数值应该比是三组fd_set中最大数更大(即一般取所有文件描述符的最大值加1),而不是实际文件描述符的总数。
*@readfds: 用来检查可读性的一组文件描述符。
*@writesfds: 用来检查可写性的一组文件描述符。
*@exceptsfds:用来检查意外状态的文件描述符。(注:错误并不是意外状态)

*@timeout:NULL指针代表无限等待,否则是指向timeval结构的指针,代表最长等待时间。(如果其中tv_sec和tv_usec都等于0, 则文件描述符的状态不被影响,但函数并不挂起)

返回值:
(1)正常情况下返回满足要求的文件描述符个数;
(2)经过了timeout等待后仍无文件满足要求,返回0;
(3)如果select被某个信号中断,将返回-1并设置errno为EINTR;
(4)若出错,返回-1并设置相应的errno;

2.select的使用方法:
(1)将要监控的文件添加到文件描述符集;
(2)调用select开始监控;
(3)判断文件是否发生变化;

 

3.系统提供四个宏对描述符集进行操作:

void FD_SET(int fd, fd_set *fdset); //将文件描述符fd添加到文件描述符集fdset中;
void FD_CLR(int fd, fd_set *fdset); //从文件描述符集fdset中清除文件描述符fd;
void FD_ISSET(int fd, fd_set *fdset); //在调用select后使用FD_ISSET来检测文件描述符集中的文件fd发生了变化
void FD_ZERO(fd_set *fdset);//清空文件描述符集

二、Poll方法:

1.poll函数原型:

unsigned int(*poll)(struct file *filp, struct poll_table *wait);
//第一个参数为file结构体指针,第二个参数为轮询表指针。

这个函数应该进行以下两项工作:

(1)对可能引起设备文件状态变化的等待队列调用poll_wait()函数,将对应等待队列添加到poll_table中; 
(2)返回表示是否能对设备进行无阻塞可读或可写访问的掩码;
  位掩码:POLLRDNORM, POLLIN,POLLOUT,POLLWRNORM
  设备可读,通常返回:(POLLIN | POLLRDNORM)
  设备可写,通常返回:(POLLOUT | POLLWRNORM)


三、调用过程:

Linux下select调用的过程:

1、用户层应用程序调用select(),底层调用poll())
2、核心层调用sys_select() ------> do_select()
  最终调用文件描述符fd对应的struct file类型变量的struct file_operations *f_op的poll函数。
  poll指向的函数返回当前可否读写的信息。
  1)如果当前可读写,返回读写信息。
  2)如果当前不可读写,则阻塞进程,并等待驱动程序唤醒,重新调用poll函数,或超时返回。

3、驱动需要实现poll函数。
当驱动发现有数据可以读写时,通知核心层,核心层重新调用poll指向的函数查询信息。

poll_wait(filp,&wait_q,wait) // 此处将当前进程加入到等待队列中,但并不阻塞

  在中断中使用wake_up_interruptible(&wait_q)唤醒等待队列。

 

四、实例分析:

1.memdev.h

<div class="cnblogs_code" style="border: 1px solid rgb(204, 204, 204); padding: 5px; overflow: auto; margin: 5px 0px; font-family: 'Courier New' !important; background-color: rgb(245, 245, 245);"><img id="code_img_opened_38253450-1c6c-400f-a62a-602c08555f78" class="code_img_opened" src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="" style="border: 0px; vertical-align: middle; padding-right: 5px;" /><div id="cnblogs_code_open_38253450-1c6c-400f-a62a-602c08555f78" class="cnblogs_code_hide"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a target=_blank title="复制代码" style="color: rgb(119, 0, 0); font-size: 13px; border: none !important;"><img src="https://i-blog.csdnimg.cn/blog_migrate/69c5a8ac3fa60e0848d784a6dd461da6.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: 'Courier New' !important;">#ifndef _MEMDEV_H_
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">#define</span> _MEMDEV_H_

#ifndef MEMDEV_MAJOR
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">#define</span> MEMDEV_MAJOR 0   /*预设的mem的主设备号*/
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">#endif</span>

#ifndef MEMDEV_NR_DEVS
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">#define</span> MEMDEV_NR_DEVS 2    /*设备数*/
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">#endif</span>

#ifndef MEMDEV_SIZE
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">#define</span> MEMDEV_SIZE 4096
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">#endif</span>

复制代码
复制代码
/*mem设备描述结构体*/
struct mem_dev                                     
{                                                        
  char *data;                      
  unsigned long size; 
  wait_queue_head_t inq;  
};

#endif /* _MEMDEV_H_ */
复制代码

 
<span style="line-height: 23.3999996185303px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: rgb(238, 238, 221);">2.</span><span style="line-height: 23.3999996185303px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: rgb(238, 238, 221);">memdev.c</span>
<span style="line-height: 23.3999996185303px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: rgb(238, 238, 221);">
</span><div class="cnblogs_code_toolbar" style="margin-top: 5px; font-family: 'Courier New'; line-height: 21.6000003814697px; background-color: rgb(245, 245, 245);"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a target=_blank title="复制代码" style="color: rgb(119, 0, 0); font-size: 13px; border: none !important;"><img src="https://i-blog.csdnimg.cn/blog_migrate/69c5a8ac3fa60e0848d784a6dd461da6.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; line-height: 21.6000003814697px; font-family: 'Courier New' !important;">#include <linux/module.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <asm/io.h>
#include <asm/system.h>
#include <asm/uaccess.h>

#include <linux/poll.h>
#include <span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">memdev.h</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span>

<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">static</span> mem_major = MEMDEV_MAJOR;
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">bool</span> have_data = <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">false</span>; <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">表明设备有足够数据可供读</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>

module_param(mem_major, <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">int</span>, S_IRUGO);

<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">struct</span> mem_dev *mem_devp; <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">设备结构体指针</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>

<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">struct</span> cdev cdev; 

<span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">文件打开函数</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">int</span> mem_open(<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">struct</span> inode *inode, <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">struct</span> file *filp)
{
    <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">struct</span> mem_dev *dev;
    
    <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">获取次设备号</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
    <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">int</span> num = MINOR(inode->i_rdev);

    <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span> (num >= MEMDEV_NR_DEVS) 
            <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> -ENODEV;
    dev = &mem_devp[num];
    
    <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">将设备描述结构指针赋值给文件私有数据指针</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
    filp->private_data = dev;
    
    <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>; 
}

<span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">文件释放函数</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">int</span> mem_release(<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">struct</span> inode *inode, <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">struct</span> file *filp)
{
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>;
}

<span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">读函数</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">static</span> ssize_t mem_read(<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">struct</span> file *filp, <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">char</span> __user *buf, size_t size, loff_t *ppos)
{
  unsigned <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">long</span> p =  *ppos;
  unsigned <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">int</span> count = size;
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">int</span> ret = <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>;
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">struct</span> mem_dev *dev = filp->private_data; <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">获得设备结构体指针</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>

  <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">判断读位置是否有效</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span> (p >= MEMDEV_SIZE)
    <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>;
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span> (count > MEMDEV_SIZE - p)
    count = MEMDEV_SIZE - p;
    
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">while</span> (!have_data) <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> 没有数据可读,考虑为什么不用if,而用while </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
  {
        <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span> (filp->f_flags & O_NONBLOCK)
            <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> -EAGAIN;
    
    wait_event_interruptible(dev->inq,have_data);
  }

  <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">读数据到用户空间</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span> (copy_to_user(buf, (<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">void</span>*)(dev->data + p), count))
  {
    ret =  - EFAULT;
  }
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">else</span>
  {
    *ppos += count;
    ret = count;
   
    printk(KERN_INFO <span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">read %d bytes(s) from %d\n</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span>, count, p);
  }
  
  have_data = <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">false</span>; <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> 表明不再有数据可读 </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
  <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> 唤醒写进程 </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> ret;
}

<span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">写函数</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">static</span> ssize_t mem_write(<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">struct</span> file *filp, <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">const</span> <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">char</span> __user *buf, size_t size, loff_t *ppos)
{
  unsigned <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">long</span> p =  *ppos;
  unsigned <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">int</span> count = size;
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">int</span> ret = <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>;
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">struct</span> mem_dev *dev = filp->private_data; <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">获得设备结构体指针</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
  
  <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">分析和获取有效的写长度</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span> (p >= MEMDEV_SIZE)
    <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>;
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span> (count > MEMDEV_SIZE - p)
    count = MEMDEV_SIZE - p;

  <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">从用户空间写入数据</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span> (copy_from_user(dev->data + p, buf, count))
    ret =  - EFAULT;
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">else</span>
  {
    *ppos += count;
    ret = count;
    
    printk(KERN_INFO <span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">written %d bytes(s) from %d\n</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span>, count, p);
  }
  
  have_data = <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">true</span>; <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> 有新的数据可读 </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
    
    <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> 唤醒读进程 </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
    wake_up(&(dev->inq));

  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> ret;
}

<span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> seek文件定位函数 </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">static</span> loff_t mem_llseek(<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">struct</span> file *filp, loff_t offset, <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">int</span> whence)
{ 
    loff_t newpos;

    <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">switch</span>(whence) {
      <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">case</span> <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>: <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> SEEK_SET </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
        newpos = offset;
        <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">break</span>;

      <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">case</span> <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">1</span>: <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> SEEK_CUR </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
        newpos = filp->f_pos + offset;
        <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">break</span>;

      <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">case</span> <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">2</span>: <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> SEEK_END </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
        newpos = MEMDEV_SIZE -<span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">1</span> + offset;
        <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">break</span>;

      <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">default</span>: <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> can't happen </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
        <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> -EINVAL;
    }
    <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span> ((newpos<<span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>) || (newpos>MEMDEV_SIZE))
        <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> -EINVAL;
        
    filp->f_pos = newpos;
    <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> newpos;

}
unsigned <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">int</span> mem_poll(<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">struct</span> file *filp, poll_table *wait)
{
    <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">struct</span> mem_dev  *dev = filp->private_data; 
    unsigned <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">int</span> mask = <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>;
    
   <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">将等待队列添加到poll_table </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
    poll_wait(filp, &dev->inq,  wait);
 
    
    <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span> (have_data)         mask |= POLLIN | POLLRDNORM;  <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> readable </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>

    <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> mask;
}


<span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">文件操作结构体</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">static</span> <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">const</span> <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">struct</span> file_operations mem_fops =
{
  .owner = THIS_MODULE,
  .llseek = mem_llseek,
  .read = mem_read,
  .write = mem_write,
  .open = mem_open,
  .release = mem_release,
  .poll = mem_poll,
};

<span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">设备驱动模块加载函数</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">static</span> <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">int</span> memdev_init(<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">void</span>)
{
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">int</span> result;
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">int</span> i;

  dev_t devno = MKDEV(mem_major, <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>);

  <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> 静态申请设备号</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span> (mem_major)
    result = register_chrdev_region(devno, <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">2</span>, <span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">memdev</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span>);
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">else</span>  <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> 动态分配设备号 </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
  {
    result = alloc_chrdev_region(&devno, <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>, <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">2</span>, <span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">memdev</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span>);
    mem_major = MAJOR(devno);
  }  
  
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span> (result < <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>)
    <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> result;

  <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">初始化cdev结构</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
  cdev_init(&cdev, &mem_fops);
  cdev.owner = THIS_MODULE;
  cdev.ops = &mem_fops;
  
  <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> 注册字符设备 </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
  cdev_add(&cdev, MKDEV(mem_major, <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>), MEMDEV_NR_DEVS);
   
  <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> 为设备描述结构分配内存</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
  mem_devp = kmalloc(MEMDEV_NR_DEVS * <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">sizeof</span>(<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">struct</span> mem_dev), GFP_KERNEL);
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span> (!mem_devp)    <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">申请失败</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
  {
    result =  - ENOMEM;
    <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">goto</span> fail_malloc;
  }
  memset(mem_devp, <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>, <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">sizeof</span>(<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">struct</span> mem_dev));
  
  <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">为设备分配内存</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">for</span> (i=<span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>; i < MEMDEV_NR_DEVS; i++) 
  {
        mem_devp[i].size = MEMDEV_SIZE;
        mem_devp[i].data = kmalloc(MEMDEV_SIZE, GFP_KERNEL);
        memset(mem_devp[i].data, <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>, MEMDEV_SIZE);
  
      <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">初始化等待队列</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
     init_waitqueue_head(&(mem_devp[i].inq));
     <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">init_waitqueue_head(&(mem_devp[i].outq));</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">
</span>  }
   
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>;

  fail_malloc: 
  unregister_chrdev_region(devno, <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">1</span>);
  
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> result;
}

<span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">模块卸载函数</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">static</span> <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">void</span> memdev_exit(<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">void</span>)
{
  cdev_del(&cdev);   <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">注销设备</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
  kfree(mem_devp);     <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">释放设备结构体内存</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
  unregister_chrdev_region(MKDEV(mem_major, <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>), <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">2</span>); <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">释放设备号</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
}

MODULE_AUTHOR(<span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">David Xie</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span>);
MODULE_LICENSE(<span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">GPL</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span>);

module_init(memdev_init);
module_exit(memdev_exit);
 

3. app-write.c

复制代码
#include <stdio.h>

int main()
{
    FILE *fp = NULL;
    char Buf[128];
    
    
    /*打开设备文件*/
    fp = fopen("/dev/memdev0","r+");
    if (fp == NULL)
    {
        printf("Open Dev memdev Error!\n");
        return -1;
    }
    
    /*写入设备*/
    strcpy(Buf,"memdev is char dev!");
    printf("Write BUF: %s\n",Buf);
    fwrite(Buf, sizeof(Buf), 1, fp);
    
    sleep(5);
    fclose(fp);
    
    return 0;    

}

4. app-read.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <errno.h>

int main()
{
    int fd;
    fd_set rds;
    int ret;
    char Buf[128];
    
    /*初始化Buf*/
    strcpy(Buf,"memdev is char dev!");
    printf("BUF: %s\n",Buf);
    
    /*打开设备文件*/
    fd = open("/dev/memdev0",O_RDWR);
    
    FD_ZERO(&rds);
    FD_SET(fd, &rds);

    /*清除Buf*/
    strcpy(Buf,"Buf is NULL!");
    printf("Read BUF1: %s\n",Buf);

    ret = select(fd + 1, &rds, NULL, NULL, NULL);
    if (ret < 0) 
    {
        printf("select error!\n");
        exit(1);
    }
    if (FD_ISSET(fd, &rds)) 
        read(fd, Buf, sizeof(Buf));            
    
    /*检测结果*/
    printf("Read BUF2: %s\n",Buf);
    
    close(fd);
    
    return 0;    
}
复制代码


void FD_SET(int fd, fd_set *fdset); //将文件描述符fd添加到文件描述符集fdset中;
void FD_CLR(int fd, fd_set *fdset); //从文件描述符集fdset中清除文件描述符fd;
void FD_ISSET(int fd, fd_set *fdset); //在调用select后使用FD_ISSET来检测文件描述符集中的文件fd发生了变化
void FD_ZERO(fd_set *fdset);//清空文件描述符集

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值