您的位置:首页 > 运维架构 > Linux

Linux驱动之poll机制

2015-09-13 16:52 531 查看
上一篇文章中我们采用了中断方式来查询按键,当你仔细观察的时候,虽然我们已经睡眠了,但是read的函数一直在那里等待,一直休眠下去,有没有一种机制设置个时间,来自动唤醒呢?也可以被中断函数唤醒呢?答案是有的,就是这篇文章讲得poll机制。

上篇文章的链接:http://blog.csdn.net/qq_21792169/article/details/48415987

驱动程序:

#include <linux/module.h> /*模块有关的*/

#include <linux/kernel.h>/*内核有关的*/

#include <linux/fs.h>/*文件系统有关的*/

#include <linux/init.h>

#include <linux/device.h>

#include <linux/miscdevice.h>

#include <linux/delay.h>

#include <linux/irq.h>

#include <linux/interrupt.h>/*linux中断*/

#include <linux/poll.h>

#include <asm/io.h>

#include <asm/irq.h>

#include <asm/uaccess.h>  //copy_to_user

#include <mach/regs-gpio.h>/*寄存器设置S3C2410_GPF0等的定义*/

#include <mach/hardware.h>//s3c2410_gpio_getpin等的定义

#include <mach/irqs.h> //IRQ_EINT0等的定义

#include <asm/system.h>

#include <asm/signal.h>

/*

*  休眠用的队列的一个宏

*/

static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

/* 中断事件标志, 中断服务程序将它置1,fifth_drv_read将它清0 */

static volatile int ev_press = 0;

#define DEVICE_NAME  "poll"

static struct class *poll_class;

struct pin_desc{
unsigned int pin;
unsigned int key_val;

};

static unsigned char keyval;//jianzhi

/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */

/* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */

struct pin_desc pins_desc[4] = {
{S3C2410_GPF0, 0x01},
{S3C2410_GPF2, 0x02},
{S3C2410_GPF3, 0x03},
{S3C2410_GPF4, 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)
{
/* 松开 */
keyval = 0x80 | pindesc->key_val;
}
else
{
/* 按下 */
keyval = pindesc->key_val;
}
ev_press = 1;                  /* 表示中断发生了 */
wake_up_interruptible(&button_waitq);   /* 唤醒休眠的进程 */
return IRQ_HANDLED;

}

static int fifth_drv_open(struct inode *inode, struct file *file)      /* 应用程序调用open时候就调用这里,这时候在注册中断函数 */

{
request_irq(IRQ_EINT0,  buttons_irq, IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, "S2", &pins_desc[0]);
request_irq(IRQ_EINT2,  buttons_irq, IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, "S3", &pins_desc[1]);
request_irq(IRQ_EINT3,  buttons_irq, IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, "S4", &pins_desc[2]);
request_irq(IRQ_EINT4,  buttons_irq, IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, "S5", &pins_desc[3]);

    return 0;

}

ssize_t fifth_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, &keyval, 1);
ev_press = 0;//继续休眠
return 0;

}

static unsigned fifth_drv_poll(struct file *file, poll_table *wait)

{
unsigned int mask = 0;
poll_wait(file, &button_waitq, wait); // 不会立即休眠

//当中段发生时候要用wake_up_interruptible(&button_waitq);才会退出
if (ev_press)
mask |= POLLIN | POLLRDNORM;

return mask;//

}

int fifth_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_EINT3, &pins_desc[2]);
free_irq(IRQ_EINT4, &pins_desc[3]);
return 0;

}

static struct file_operations fifth_drv_fops = {

    .owner   =  THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */

    .open    =  fifth_drv_open,     
.read =fifth_drv_read,  
.release =  fifth_drv_close,
.poll    =  fifth_drv_poll,

};

static  int major;             /* 自动分配主设备号 */

static int fifth_drv_init(void)

{
major = register_chrdev(0, DEVICE_NAME, &fifth_drv_fops);  /* 注册这个fifth_drv_fops结构体 */
poll_class = class_create(THIS_MODULE, DEVICE_NAME);    /* 创建类 */
device_create(poll_class, NULL, MKDEV(major, 0), NULL, "poll"); /* 类下面创建设备节点,这里利用了mdev机制自动创建设备节点 */
return 0;

}

static void fifth_drv_exit(void)

{
unregister_chrdev(major, "poll");   /* 卸载 */
device_destroy(poll_class,MKDEV(major, 0));   /* 删除类下面的设备节点 */
class_destroy(poll_class);                  /* 删除类 */

}

module_init(fifth_drv_init);   /* 入口函数 */

module_exit(fifth_drv_exit);

MODULE_LICENSE("GPL");   /* 协议 */

测试程序:

#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];

fd = open("/dev/poll", 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;

}

Makefile其他的部分是不变的,只需要修改文件名字就可以了。
obj-m :=pollt.o

KERNELDIR ?= /home/work/Linux/linux-2.6.28.7

PWD := $(shell pwd)

default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

clean:
rm -f  *o  *.mod.o  *mod.c  *.symvers *.order
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: