linux内核版本:linux-2.6.30.4
目的:我们在中断方式的按键应用程序中,如果没有按键按下,read就会永远在那等待,所以如果在这个程序里还想做其他事就不可能了。因此我们这次改进它,让它在等待5秒钟,如果5秒钟内没有按键按下就返回,可以在read后面做其他事,只需要在驱动程序里加入poll机制就可以完成目的。
#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 <linux/device.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <mach/regs-gpio.h>
#include <linux/poll.h> static struct class *fourth_drv_class;
static struct class_device *fourth_drv_class_dev; static volatile unsigned long *gpfcon;
static volatile unsigned long *gpfdat;
static int major; struct pin_desc
{
unsigned int pin;
unsigned int key_val;
}; static unsigned char key_val; static struct pin_desc pins_desc[] =
{
{S3C2410_GPF1, 0x01},
{S3C2410_GPF4, 0x02},
{S3C2410_GPF2, 0x03},
{S3C2410_GPF0, 0x04},
}; static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
static volatile int ev_press = ; static irqreturn_t buttons_irq(int irq, void *dev_id)
{
struct pin_desc *pin = (struct pin_desc *)dev_id;
unsigned int pinval;
pinval = s3c2410_gpio_getpin(pin->pin); if (pinval)
{
//松开
key_val = 0x80 | pin->key_val;
}
else
{
//按下
key_val = pin->key_val;
} wake_up_interruptible(&button_waitq);
ev_press = ; return IRQ_HANDLED;
} static int fourth_drv_open(struct inode *inode, struct file *file)
{
request_irq(IRQ_EINT1, buttons_irq, IRQ_TYPE_EDGE_BOTH, "S1", &pins_desc[]);
request_irq(IRQ_EINT4, buttons_irq, IRQ_TYPE_EDGE_BOTH, "S2", &pins_desc[]);
request_irq(IRQ_EINT2, buttons_irq, IRQ_TYPE_EDGE_BOTH, "S3", &pins_desc[]);
request_irq(IRQ_EINT0, buttons_irq, IRQ_TYPE_EDGE_BOTH, "S4", &pins_desc[]);
return ;
} static ssize_t fourth_drv_read(struct file *file, char __user *buf,
size_t count, loff_t *ppos)
{
if (count != )
return -EINVAL;
/*如果没有按键动作,休眠*/
wait_event_interruptible(button_waitq, ev_press); /*如果有按键动作发生,返回键值*/
copy_to_user(buf, &key_val, );
ev_press = ;
return ;
} static int fourth_drv_release(struct inode *inode, struct file *file)
{
free_irq(IRQ_EINT1, &pins_desc[]);
free_irq(IRQ_EINT4, &pins_desc[]);
free_irq(IRQ_EINT2, &pins_desc[]);
free_irq(IRQ_EINT0, &pins_desc[]);
return ;
} static unsigned int fourth_drv_poll(struct file *file, poll_table *wait) {
unsigned int mask = ; /*这并不会休眠,只是把当前进程挂到队列里*/
poll_wait(file, &button_waitq, wait); if (ev_press)
{
mask |= (POLLIN | POLLRDNORM);
}
return mask;
} static struct file_operations fourth_fops = {
.owner = THIS_MODULE,
.open = fourth_drv_open,
.read = fourth_drv_read,
.poll = fourth_drv_poll,
.release = fourth_drv_release,
}; int fourth_init()
{
int ret;
major = register_chrdev(, "fourth_drv", &fourth_fops);
fourth_drv_class = class_create(THIS_MODULE, "fourth_drv");
device_create(fourth_drv_class, NULL, MKDEV(major, ), NULL, "buttons"); gpfcon = (volatile unsigned long *)ioremap(0x56000050, );
gpfdat = gpfcon + ; return ;
}
static void fourth_exit()
{
unregister_chrdev(major, "fourth_drv");
device_destroy(fourth_drv_class, MKDEV(major, ));
class_destroy(fourth_drv_class);
iounmap(gpfcon);
} module_init(fourth_init);
module_exit(fourth_exit); MODULE_LICENSE("GPL");
测试程序:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h> /* fourthdrvtest
*/
int main(int argc, char **argv)
{
int fd;
unsigned char key_val;
int ret;
struct pollfd fds[]; fd = open("/dev/buttons", O_RDWR);
if (fd < )
{
printf("can't open!\n");
} fds[].fd = fd;
fds[].events = POLLIN;
while ()
{
ret = poll(fds, , );
if (ret == )
{
printf("time out!\n");
}
else
{
read(fd, &key_val, );
printf("key_val === 0x%x\n", key_val);
}
} return ;
}
运行效果: