编写按键中断驱动——poll机制
- 硬件平台:韦东山嵌入式Linxu开发板(S3C2440.v3)
- 软件平台:运行于VMware Workstation 12 Player下UbuntuLTS16.04_x64 系统
- 参考资料:《嵌入式Linux应用开发手册》、《嵌入式Linux应用开发手册第2版》、https://blog.csdn.net/juS3Ve/article/details/81437432
- 开发环境:Linux 2.6.22.6 内核、arm-linux-gcc-3.4.5-glibc-2.3.6工具链
目录
一、前言
问题:在我们的应用程序中的,存在如下状况,在执行read()
函数时,假如按键一直没有按下,则该进程会一直处于阻塞状态,此时应用不可以执行其他进程,导致程序卡死。
假设:那么我们是否可以像上一个程序一样,让进程在处于休眠,在一定的条件下才可以唤醒。
答案:是可以的,这个时候就要引用poll机制,使阻塞型函数超时返回,避免一直等待造成的阻塞。
二、简述Linux中的poll机制
1、sys_poll函数
源码位于fs/select.c
asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds,
long timeout_msecs)
{
s64 timeout_jiffies;
if (timeout_msecs > 0) {
#if HZ > 1000
/* We can only overflow if HZ > 1000 */
if (timeout_msecs / 1000 > (s64)0x7fffffffffffffffULL / (s64)HZ)
timeout_jiffies = -1;
else
#endif
timeout_jiffies = msecs_to_jiffies(timeout_msecs);
} else {
/* Infinite (< 0) or no (0) timeout */
timeout_jiffies = timeout_msecs;
}
return do_sys_poll(ufds, nfds, &timeout_jiffies);
}
可以看到,在对超时参数进行一定的处理后,执行do_sys_poll()
函数
2、do_sys_poll()
函数
源码位于fs/select.c
int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, s64 *timeout)
{
/*……*/
poll_initwait(&table);
/*……*/
fdcount = do_poll(nfds, head, &table, timeout);
/*……*/
}
比较关键的函数有两个:
2.1 poll_initwait()
函数
void poll_initwait(struct poll_wqueues *pwq)
{
init_poll_funcptr(&pwq->pt, __pollwait);
pwq->error = 0;
pwq->table = NULL;
pwq->inline_index = 0;
}
static inline void init_poll_funcptr(poll_table *pt, poll_queue_proc qproc)
{
pt->qproc = qproc;
}
poll_initwait()
---------> init_poll_funcptr(&pwq->pt, __pollwait)
--------->pt->qproc = qproc
。
最终这个qproc
就是init_poll_funcptr(&pwq->pt, __pollwait);
中的__pollwait
2.1.1__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);
}
2.2 do_poll()
函数大致框架
static int do_poll(unsigned int nfds, struct poll_list *list,
struct poll_wqueues *wait, s64 *timeout)
{
/*........*/
for (;;) {
/*........*/
for (; pfd != pfd_end; pfd++) {
if (do_pollfd(pfd, pt)) {
count++;
pt = NULL;
}
}
}
/*
* All waiters have already been registered, so don't provide
* a poll_table to them on the next loop iteration.
*/
pt = NULL;
if (count || !*timeout || signal_pending(current))
break;
count = wait->error;
if (count)
break;
/*........*/
__timeout = schedule_timeout(__timeout);
/*........*/
__set_current_state(TASK_RUNNING);
return count;
}
分析其中的代码,可以发现,它的作用如下:
① for(; ; )
这是个循环,它退出的条件为:
- ①.a.
if (count || !*timeout || signal_pending(current))
——(count非0,超时、有信号等待处理)
而count非0
表示do_pollfd()
至少有一个成功。
do_pollfd()
函数大致框架:
static inline unsigned int do_pollfd(struct pollfd *pollfd, poll_table *pwait)
{
mask = 0;
fd = pollfd->fd;
if (fd >= 0) {
/*........*/
if (file != NULL) {
mask = DEFAULT_POLLMASK;
if (file->f_op && file->f_op->poll)
mask = file->f_op->poll(file, pwait);
mask &= pollfd->events | POLLERR | POLLHUP;
fput_light(file, fput_needed);
}
}
pollfd->revents = mask;
return mask;
}
可以看到:
-
mask = file->f_op->poll(file, pwait);
:当在file_operation中定义了poll,则执行我们在驱动中编写的poll,驱动中poll的返回值给mask -
mask &= pollfd->events | POLLERR | POLLHUP
:mask和应用程序中poll()
传入的events| POLLERR | POLLHUP
进行与运算并返回。 -
①.b.
count = wait->error; if (count) break;
:发生错误
②、 __timeout = schedule_timeout(__timeout);
:让本进程休眠一段时间。
注意:应用程序执行poll调用后,如果①的条件不满足,进程就会进入休眠。那么,谁唤醒呢?除了休眠到指定时间被系统唤醒外,还可以被驱动程序唤醒──记住这点,这就是为什么驱动的poll里要调用poll_wait的原因
3、总结
-
poll
----->sys_poll
----->do_sys_poll
----->poll_initwait
poll_initwait
函数注册
一下回调函数__pollwait
,它就是我们的驱动程序执行poll_wait时,真正被调用的函数。 -
接下来执行
file->f_op->poll
,即我们驱动程序里自己实现的poll函数
它会调用poll_wait
把自己挂入某个队列,这个队列也是我们的驱动自己定义的; -
如果设备未就绪,
do_sys_poll
里会让进程休眠一定时间。 -
进程被唤醒的条件有:一是上面说的“一定时间”到了,二是被驱动程序唤醒。驱动程序发现条件就绪时,就把“某个队列”上挂着的进程唤醒,这个队列,就是前面通过poll_wait把本进程挂过去的队列。
-
如果驱动程序没有去唤醒进程,那么
chedule_timeout(__timeou)
超时后,会重复2、3动作
,直到
应用程序的poll调用传入的时间到达
。
三、代码编写
1、驱动程序修改
在file_operations
结构体中需要添加.poll
项
static struct file_operations button_drv_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = button_drv_open,
.read = button_drv_read,
.release = button_drv_close,
.poll = buton_drv_poll,
};
编写buton_drv_poll()
函数
unsigned int buton_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; //返回给do_poll函数中的mask
}
2、完整驱动程序
#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>
int major;
static struct class *buttondrv_class;
static struct class_device *buttondrv_class_dev;
volatile unsigned long *gpfcon;
volatile unsigned long *gpfdat;
volatile unsigned long *gpgcon;
volatile unsigned long *gpgdat;
/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04
* 键值: 松开时, 0x81, 0x82, 0x83, 0x84
*/
static unsigned char key_val;
/* 按键信息 */
struct pin_desc{
unsigned int pin;
unsigned int key_val;
};
/* 存储4个按键的信息 */
struct pin_desc pins_desc[4] = {
{S3C2410_GPF0, 0x01},
{S3C2410_GPF2, 0x02},
{S3C2410_GPG3, 0x03},
{S3C2410_GPG11, 0x04},
};
/* 中断注册信息 */
struct buttonirq_decs{
unsigned int irq;
unsigned long flags;
const char *devname;
void *dev_id;
};
struct buttonirq_decs buttonirqs_decs[4] = {
{IRQ_EINT0, IRQT_BOTHEDGE, "S2", &pins_desc[0]},
{IRQ_EINT2, IRQT_BOTHEDGE, "S3", &pins_desc[1]},
{IRQ_EINT11, IRQT_BOTHEDGE, "S4", &pins_desc[2]},
{IRQ_EINT19, IRQT_BOTHEDGE, "S5", &pins_desc[3]},
};
/* 生成一个等待队列的队头,名字为button_waitq */
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
/* 中断事件标志, 中断服务程序将它置1,button_drv_read将它清0 */
static volatile int ev_press = 0;
/* 中断服务函数 */
static irqreturn_t button_irq(int irq, void *dev_id)
{
struct pin_desc *pindesc = (struct pin_desc *)dev_id;
unsigned int pinval;
pinval = s3c2410_gpio_getpin(pindesc->pin); //读取IO口电平
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);
}
/* open驱动函数 */
static int button_drv_open(struct inode *inode, struct file *file)
{
unsigned int i;
int err;
/* 注册中断 */
for(i = 0; i < (sizeof(buttonirqs_decs)/sizeof(buttonirqs_decs[0])); i++){
err = request_irq(buttonirqs_decs[i].irq, button_irq, buttonirqs_decs[i].flags,
buttonirqs_decs[i].devname, buttonirqs_decs[i].dev_id);
if(err){
printk("func button_drv_open err: request_irq num:%d\n", i);
break;
}
}
/* 出现错误,释放已经注册的中断 */
if(err){
i--;
for (; i >= 0; i--)
free_irq(buttonirqs_decs[i].irq, buttonirqs_decs[i].dev_id);
return -EBUSY;
}
return 0;
}
/* read驱动函数 */
static ssize_t button_drv_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
{
unsigned long ret = 0;
int err;
/* 如果没有按键动作, 休眠 */
err = wait_event_interruptible(button_waitq, ev_press);
if (err){
printk("func button_drv_read() err: wait_event_interruptible\n");
return err;
}
/* 如果有按键动作, 返回键值 */
ret = copy_to_user(buf, &key_val, 1);
ev_press = 0; //清中断标志
if(ret < 0){
printk("func button_drv_read() err: copy_to_user\n");
return -EFAULT;
}
return sizeof(key_val);
}
/* close驱动函数 */
int button_drv_close (struct inode *inode, struct file *file)
{
int i;
/* 取消中断 */
for(i = 0; i < (sizeof(buttonirqs_decs)/sizeof(buttonirqs_decs[0])); i++)
free_irq(buttonirqs_decs[i].irq, buttonirqs_decs[i].dev_id);
return 0;
}
unsigned int buton_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; //返回给do_poll函数中的mask
}
static struct file_operations button_drv_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = button_drv_open,
.read = button_drv_read,
.release = button_drv_close,
.poll = buton_drv_poll,
};
/* 入口函数
* 执行”insmod button_drv.ko”命令时就会调用这个函数
*/
static int button_drv_init(void)
{
//注册一个字符设备,名字为button_drv
major = register_chrdev(0, "button_drv", &button_drv_fops);
//创建一个类,名字为buttond_rv(/class/button_drv)
buttondrv_class = class_create(THIS_MODULE, "button_drv");
//创建一个设备节点,名为button(/dev/button)
buttondrv_class_dev = class_device_create(buttondrv_class, NULL, MKDEV(major, 0), NULL, "button");
/* 映射物理地址 */
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;
gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
gpgdat = gpgcon + 1;
return 0;
}
/* 出口函数
* 执行”rmmod button_drv.ko”命令时就会调用这个函数
*/
static void button_drv_exit(void)
{
unregister_chrdev(major, "button_drv"); // 卸载字符
class_device_unregister(buttondrv_class_dev); //删除设备节点
class_destroy(buttondrv_class); //销毁类
/* 取消映射 */
iounmap(gpfcon);
iounmap(gpgcon);
}
module_init(button_drv_init);
module_exit(button_drv_exit);
MODULE_LICENSE("GPL");
3、测试程序修改
要使用poll机制,需要在测试程序调用poll()
函数,函数原型如下:
int poll(struct pollfd fd[], nfds_t nfds, int timeout);
/* 第一个参数fd:一个结构数组,struct pollfd结构如下:
struct pollfd{
int fd; //文件描述符
short events; //请求的事件
short revents; //返回的事件
};
第二个参数nfds:要监视的描述符的数目
第三个参数timeout:是一个用毫秒表示的时间,是指定poll在返回前没有接收事件时应该等待的时间
*/
4、完整测试程序代码
#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;
int ret = 0;
unsigned char key_val;
int count = 0;
struct pollfd fds[1];
fd = open("/dev/button", O_RDWR);
if (fd < 0){
printf("can't open!\n");
return -1;
}
fds[0].fd = fd;
fds[0].events = POLLIN;
while (1)
{
ret = poll(fds, 1, 5000);
if (ret == 0)
{
printf("time out\n");
}else{
ret = read(fd, &key_val, 1);
if(ret < 0)
printf(" func read() err\n");
else
printf("key_val = 0x%x\n", key_val);
}
}
return ret;
}
有关于测试程序如何与内核合作,完成poll机制的调用,可以参考这个篇博客https://www.cnblogs.com/andyfly/p/9480434.html
四、实际运行
可以看到,当我们运行测试程序时,不按下按键,测试测试程序不会一直等待read()
函数有返回值,而是休眠5秒,在这5秒内,你可以采用中断唤醒进程,或称5秒过后,唤醒进程。