目录
前言
前一篇虽然在驱动层面使用了中断的方式,但是应用层面还是需要不断在死循环里面执行读取函数,应用程序不能去做其它事情。poll机制解决了这个问题,当有事件发生时,才去执行读read函数,按键事件没有按下时,去执行其它的处理函数。并可通过设置超时时间,调整应用层等待的时间。先分析内核中Poll的运行机制,然后使用之。
Poll机制分析
应用层调用poll函数 -> 内核层sys_poll -> do_sys_poll -> poll_initwait 、do_poll -> do_pollfd -> 驱动层 s3c24xx_buttons_poll。
- 应用层poll,设置需要检测的设备文件句柄、需要监测的事件以及超时时间。
- sys_poll,对传入的超时时间进行判断转换,然后调用do_sys_poll。
- do_sys_poll,调用poll_initwait(初始化一个poll_wqueues变量table,即table->pt->qproc = __pollwait,__pollwait将在驱动的poll函数里用到),调用do_poll。
- do_poll,poll机制核心,在一个for循环当中 调用 do_pollfd(调用驱动程序中的s3c24xx_buttons_poll)、判断s3c24xx_buttons_poll的返回值、根据返回值或超时时间进行休眠或者返回超时。
- do_pollfd,调用驱动程序中的s3c24xx_buttons_poll。
- s3c24xx_buttons_poll
- 调用poll_wait,即 “3” 中__pollwait,将do_sys_poll进程加入到等待唤醒队列,即一旦等待唤醒队列中的button_waitq被唤醒,则唤醒 “4” 中提到的休眠状态,继续do_poll。
- 判断当前驱动设备状态,可读或可写或未就绪,返回对应状态值。
使用Poll的按键驱动程序
#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>
#define DEVICE_NAME "button" /* 加载模块后,执行”cat /proc/devices”命令看到的主设备名称 */
#define BUTTON_MAJOR 232 /* 主设备号 */
static struct class *buttons_class; //设备类 使用cd /sys/class 查看当前系统上的设备类
static struct class_device *buttons_class_dev; //类设备,一个设备类下可以有多个类设备 ls /dev/* 查看当前系统上的设备。或者 cd /sys/class/类名/ 查看当前设备类下的设备。
struct pin_desc{
unsigned int pin;
unsigned int pin_val;
};
struct pin_desc pins_desc[3] = {
{S3C2410_GPF0,0x1},
{S3C2410_GPF2,0x2},
{S3C2410_GPG3,0x3},
};
// 按下 0x1 0x2 0x3
// 松开 0x81 0x82 0x83
static unsigned char key_val;
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
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;
printk(" enter %d \n",irq);
pinval = s3c2410_gpio_getpin(pindesc->pin);
if(pinval)
{
// 松开
key_val = 0x80 | pindesc->pin_val;
}
else
{
// 按下
key_val = pindesc->pin_val;
}
ev_press = 1;
wake_up_interruptible(&button_waitq);
return IRQ_RETVAL(IRQ_HANDLED);
}
/* 应用程序对设备文件/dev/button 执行open(...)时,
* 就会调用s3c24xx_buttons_open函数
*/
static int s3c24xx_buttons_open(struct inode *inode, struct file *file)
{
// s3c2410_gpio_cfgpin(S3C2410_GPF0, S3C2410_GPF0_INP);
// s3c2410_gpio_cfgpin(S3C2410_GPF2, S3C2410_GPF2_INP);
// s3c2410_gpio_cfgpin(S3C2410_GPG3, S3C2410_GPG3_INP);
request_irq(IRQ_EINT0,button_irq,IRQT_BOTHEDGE,"S0",&pins_desc[0]);
request_irq(IRQ_EINT2,button_irq,IRQT_BOTHEDGE,"S1",&pins_desc[1]);
request_irq(IRQ_EINT11,button_irq,IRQT_BOTHEDGE,"S2",&pins_desc[2]);
return 0;
}
static int s3c24xx_buttons_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{
if (count != 1)
return -EINVAL;
wait_event_interruptible(button_waitq, ev_press);
copy_to_user(buff, &key_val, sizeof(key_val));
ev_press = 0;
return 0;
}
static ssize_t s3c24xx_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]);
return 0;
}
static unsigned int s3c24xx_buttons_poll(struct file *file, poll_table *wait)
{
unsigned int mask = 0;
poll_wait(file, &button_waitq, wait);
if (ev_press)
mask |= POLLIN | POLLWRNORM;
return mask;
}
/* 这个结构是字符设备驱动程序的核心
* 当应用程序操作设备文件时所调用的open、read、write等函数,
* 最终会调用这个结构中指定的对应函数
*/
static struct file_operations s3c24xx_buttons_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = s3c24xx_buttons_open,
.read = s3c24xx_buttons_read,
.release = s3c24xx_buttons_close,
.poll = s3c24xx_buttons_poll,
};
/*
* 执行insmod命令时就会调用这个函数
*/
static int __init s3c24xx_button_init(void)
{
int ret;
/* 注册字符设备
* 参数为主设备号、设备名字、file_operations结构;
* 这样,主设备号就和具体的file_operations结构联系起来了,
* 操作主设备为BUTTON_MAJOR的设备文件时,就会调用s3c24xx_buttons_fops中的相关成员函数
* BUTTON_MAJOR可以设为0,表示由内核自动分配主设备号,返回值为主设备号
*/
ret = register_chrdev(BUTTON_MAJOR, DEVICE_NAME, &s3c24xx_buttons_fops);
if (ret < 0) {
printk(DEVICE_NAME " can't register major number\n");
return ret;
}
// 创建设备类
buttons_class = class_create(THIS_MODULE, "buttons");
if (IS_ERR(buttons_class))
return PTR_ERR(buttons_class);
// 创建类设备节点
buttons_class_dev = class_device_create(buttons_class, NULL, MKDEV(BUTTON_MAJOR,0), NULL, "button");
if (unlikely(IS_ERR(buttons_class_dev)))
return PTR_ERR(buttons_class_dev);
printk(DEVICE_NAME " initialized\n");
return 0;
}
/*
* 执行rmmod命令时就会调用这个函数
*/
static void __exit s3c24xx_button_exit(void)
{
printk("to unregister_chrdev \n");
unregister_chrdev(BUTTON_MAJOR, DEVICE_NAME);
printk("to class_device_unregister \n");
class_device_unregister(buttons_class_dev);
printk("to class_destroy \n");
class_destroy(buttons_class);
}
/* 这两行指定驱动程序的初始化函数和卸载函数 */
module_init(s3c24xx_button_init);
module_exit(s3c24xx_button_exit);
/* 描述驱动程序的一些信息,不是必须的 */
MODULE_VERSION("0.1.0");
MODULE_DESCRIPTION("S3C2410/S3C2440 BUTTON Driver");
MODULE_LICENSE("GPL");
使用Poll的应用测试程序
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <poll.h>
int main(int argc, char **argv)
{
int i;
int ret;
int fd;
unsigned char press;
struct pollfd fds[1];
fd = open("/dev/button", 0); // 打开设备
if (fd < 0) {
printf("Can't open /dev/button\n");
return -1;
}
fds[0].fd = fd;
fds[0].events = POLLIN;
while (1) {
ret = poll(fds,1,1000);
if(0 == ret)
{
printf("\n time out \n");
}
else
{
ret = read(fd, &press, 1);
printf(" read buttons key : 0x%x \n",press);
}
}
close(fd);
return 0;
}
编译测试
查看本专栏前面两篇文章。
小结
三种按键驱动方式
- 查询方法:一直在查询,不断去查询是否有事件发生,整个过程都是占用CPU资源,非常消耗CPU资源。
- 中断方式:当有事件发生时,就去跳转到相应事件去处理,CPU占用时间少。
- poll方式:中断方式虽然占用CPU资源少,但是应用层面还是需要不断在死循环里面执行读取函数,应用程序不能去做其它事情。poll机制解决了这个问题,当有事件发生时,才去执行读read函数,按键事件没有按下时,去执行其它的处理函数。并可通过设置超时时间,调整应用层等待的时间。