一.了解poll机制
poll机制:在规定时间内按下按键时立刻返回,没有的话在时间到后返回
参考1:
参考2:
《linux设备驱动程序的p163页》
在file_operations结构体中,open,read,write。对应的也提供了poll,在poll中加入poll函数
驱动中poll函数格式为:unsigned int (*poll) (struct file *, struct poll_table_struct *);
在source_insight搜索.poll,随便找一个poll函数例子,拷贝函数格式为:
linux调用open时会调用sys_open,调用read时会调用sys_read,同样调用poll时,会调用sys_poll,我们来分析sys_poll怎么实现(参考上面的文件:poll机制分析以及在 source_insight搜索sys_poll)
当app执行poll函数时,系统进入sys_poll——>do_sys_poll——>poll_initwait和do_poll。在 do_poll中进行for循环,执行驱动函数的poll函数,把当前进程挂到等待队列中,当有中断时,让do_pollfd即驱动函数的返回值非零,否则进入等待,休眠_timeout,直到超时或者非零,或者有信号在等待处理时break循环。
poll机制决定是否休眠的是返回值是否为0。所以我们只要设置当按键出发中断时让poll驱动函数的返回值为非0就可以不休眠。
为此,之前的read函数里的休眠可以去掉,因为poll已经帮我们实现了,而且实现了再规定时间没响应时自动响应。
二.poll驱动函数如下:
这里一开始就设置返回值mask为0,意味着一开始就休眠,直到ev_press(中断标志位)=1时才不休眠,去执行read函数,即打印。
三.编写测试程序
在驱动函数我们只提供了poll的接口,没有使用poll,因此在测试程序要编写poll函数。在虚拟机上搜索poll(),即man poll(poll()执行与select(2)类似的任务:它等待一个集合中的一个
准备执行I/O的文件描述符。)
(1)poll函数详解
poll函数用于监测多个等待事件,若事件未发生,进程睡眠,放弃CPU控制权,若监测的任何一个事件发生,poll将唤醒睡眠的进程,并判断是什么等待事件发生,执行相应的操作。poll函数退出后,struct pollfd变量的所有值被清零,需要重新设置。
头文件:#include <poll.h>
int poll(struct pollfd *fds, nfds_t nfds, int timeout);
返回值:
>0:数组fds中准备好读、写或出错状态的那些socket描述符的总数量;
==0:数组fds中没有任何socket描述符准备好读、写,或出错;此时poll超时,超时时间是timeout毫秒;换句话说,如果所检测的socket描述符上没有任何事件发生的话,那么poll()函数会阻塞timeout所指定的毫秒时间长度之后返回,如果timeout==0,那么poll() 函数立即返回而不阻塞,如果timeout==INFTIM,那么poll() 函数会一直阻塞下去,直到所检测的socket描述符上的感兴趣的事件发生是才返回,如果感兴趣的事件永远不发生,那么poll()就会永远阻塞下去;
-1: poll函数调用失败,同时会自动设置全局变量errno;
参数一:struct pollfd *fds
要监视的文件描述符集在fds参数中指定,该参数是以下结构的数组形式:
struct pollfd{
int fd; // 文件描述符
short event;// 请求的事件
short revent;// 返回的事件
}
fd: open后返回的文件描述符,如果为负,然后忽略相应的events字段 revents字段返回零
一个指定
events:应用程序对文件描述符fd感兴趣的事件。此字段可以指定为零,在这种情况下,只有事件可以在revents中返回POLLHUP、POLLERR和POLLNVAL(参见下文)。
revents: 字段revents是一个输出参数,由内核用实际发生的事件。在revents中返回的位可以包括事件中指定的任何值,POLLERR值之一,POLLHUP或POLLNVAL。(这三个位在事件中是没有意义的字段,并将在revents字段中设置相应的条件是正确的。)
如果没有为任何事件请求事件(也没有错误)文件描述符,然后poll()阻塞,直到其中一个事件发生
参数二:nfds:
调用者应该在nfds中指定fds数组中的项数。
参数三:timeout:
timeout参数指定poll()的毫秒数应该阻止等待文件描述符就绪。调用将阻塞,直到:*
文件描述符就绪;
*调用被信号处理程序中断;或
超时超时。
指定零原因超时 即使没有准备好文件描述符,也要立即返回poll()。
(2)写poll函数
根据man poll 可知,poll函数可以检测多个文件描述符,定义在结构体pollfd数组中,因此写测试程序时,我们要写定义结构体变量数组
struct pollfd fds[1]; //表示检测一个文件描述符
然后设置fd的参数:
fds[0].fd = fd; //文件描述符
fds[0].events = POLLIN; //表示有数据要读
设置poll函数
ret = poll(fds, 1, 5000); //1为只有一个文件描述符检测,5000 = 5s ,ret是返回值,ret==0表示没有事件发生,休眠直到time_out后,返回;ret>1时,表示有时间发生。ret==-1,poll出错
所以我们可以设置在while里判断ret的值,来判断是否有事件(中断发生)
当有中断时,ev_press=1,并唤醒等待队列里被poll休眠的进程
驱动函数secondddrv_butttons_poll会返回mask |= POLLIN | POLLRDNORM,在sys_poll中的do_poll会跳出循环,不再休眠,执行read函数,返回键值。
四.具体代码为:
驱动代码:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.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/irq.h>
#include <linux/poll.h>
static struct class *seconddrv_buttons_class;
static struct class_device *seconddrv_buttons_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,third_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 */
struct pin_desc pins_desc[4] = {
{S3C2410_GPF0, 0x01},
{S3C2410_GPF2, 0x02},
{S3C2410_GPG3, 0x03},
{S3C2410_GPG11, 0x04},
};
static unsigned char key_val;
static irqreturn_t mybuttons_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) // 1为松开
{
key_val =0x80 | pindesc->key_val;
}
else
{
key_val =pindesc->key_val;
}
ev_press = 1; /* 中断标志位=1,表示中断发生了 */
wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */
return IRQ_RETVAL(IRQ_HANDLED);
}
static int seconddrv_buttons_open(struct inode *inode, struct file *file)
{
/*配置GPF0,2为输入引脚,中断号参考arch\arm\plat-s3c24xx\Irq.c的s3c24xx_init_irq*/
/*配置GPG3,11为输入引脚*/
request_irq(IRQ_EINT0, mybuttons_irq, IRQT_BOTHEDGE, "S1", &pins_desc[0]);
request_irq(IRQ_EINT2, mybuttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[1]);
request_irq(IRQ_EINT11, mybuttons_irq, IRQT_BOTHEDGE, "S3", &pins_desc[2]);
request_irq(IRQ_EINT19, mybuttons_irq, IRQT_BOTHEDGE, "S4", &pins_desc[3]);
//printk("seconddrv_open\n");
return 0;
}
ssize_t seconddrv_buttons_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;
}
static int seconddrv_buttons_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 int secondddrv_butttons_poll(struct file *file, struct poll_table_struct *wait)
{
unsigned int mask = 0;
poll_wait(file, &button_waitq, wait); // 不会立即休眠
if (ev_press) //ev_press 不为 0 时,poll 休眠直到time_out后break循环体 ,为1时,break
mask |= POLLIN | POLLRDNORM;
return mask;
}
static struct file_operations seconddrv_buttons_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = seconddrv_buttons_open,
.read = seconddrv_buttons_read,
.release= seconddrv_buttons_close,
.poll = secondddrv_butttons_poll,
};
int major;
int seconddrv_buttons_init(void)
{
major = register_chrdev(0,"seconddrv",&seconddrv_buttons_fops);
seconddrv_buttons_class = class_create(THIS_MODULE,"buttons");
seconddrv_buttons_class_dev = class_device_create(seconddrv_buttons_class,NULL,MKDEV(major,0),NULL,"mybuttons");
gpfcon = (volatile unsigned long *)ioremap(0x56000050,16);
gpfdat = gpfcon + 1;
gpgcon = (volatile unsigned long *)ioremap(0x56000060,16);
gpgdat = gpgcon +1;
return 0;
}
void seconddrv_buttons_exit(void)
{
unregister_chrdev(major,"seconddrv");
class_destroy(seconddrv_buttons_class);
class_device_unregister(seconddrv_buttons_class_dev);
iounmap(gpfcon);
iounmap(gpgcon);
return 0;
}
module_init(seconddrv_buttons_init);
module_exit(seconddrv_buttons_exit);
MODULE_LICENSE("GPL");
测试代码:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <poll.h>
/*
*mybuttons_test
*/
int main(int argc, char **argv)
{
int fd;
unsigned char key_val;
struct pollfd fds[1];
int ret;
fd = open("/dev/mybuttons", O_RDWR);
if (fd < 0)
{
printf("can't open!\n");
}
fds[0].fd =fd;
fds[0].events = POLLIN;
while (1)
{
ret = poll(fds,1,5000);
if(ret == 0)
{
printf("time out!\n");
}
else
{
read(fd, &key_val, 1);
printf("key_val = 0x%x\n", key_val);
}
}
return 0;
}