实现:
1.让驱动有数据时,去主动告诉应用程序,让应用程序读。
驱动:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/ioport.h>
#include <linux/interrupt.h>
#include <linux/wait.h>
#include <linux/poll.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <mach/irqs.h>
#include <mach/gpio.h>
static int major_button = -1;
static struct class * class_button = NULL;
static struct device * device_button = NULL;
static int press_mark = 0;
static int key_val = 0;
static DECLARE_WAIT_QUEUE_HEAD(button_press_mark);
static struct timer_list buttons_timer; /* 定义一个定时器结构体 */
static struct fasync_struct * markbutton_fasync;
static void buttons_timer_function(unsigned long data)
{
key_val = gpio_get_value(S5PV210_GPH0(2));
printk(KERN_INFO “buttons_timer_function %d\n”, key_val);
if (key_val)
{
printk(KERN_INFO “gpio == 1 up\n”);
}
else
{
printk(KERN_INFO “gpio == 0 press\n”);
}
press_mark = 1;
wake_up_interruptible(&button_press_mark);
/*
kill_fasync函数告诉应用程序有数据可读了
markbutton_fasync 结构体里包含了发给谁 (应用层用PID指定)
SIGIO : 表示要发送的信号类型
POLL_IN: 表示发送的原因(有数据可读了)
*/
kill_fasync(&markbutton_fasync, SIGIO, POLL_IN);
}
static irqreturn_t button_interrupt_mark(int irq, void * data)
{
mod_timer(&buttons_timer, jiffies + (HZ /50));
return IRQ_HANDLED;
}
static int button_mark_open (struct inode * inode, struct file * file)
{
int err = -1;
if (gpio_request(S5PV210_GPH0(2), “gph0_2”))
{
printk(KERN_ERR “gpio_request S5PV210_GPH0(2) error\n”);
goto err_gpio_request;
}
s3c_gpio_cfgpin(S5PV210_GPH0(2), 0xf);
err = request_irq(IRQ_EINT2, button_interrupt_mark, IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
"irq-eint2", NULL);
if (err)
{
printk(KERN_ERR "request_irq IRQ_EINT2 error\n");
goto err_request_irq;
}
return 0;
err_request_irq:
gpio_free(S5PV210_GPH0(2));
err_gpio_request:
return -EBUSY;
}
static int button_mark_close (struct inode * inode, struct file * file)
{
disable_irq(IRQ_EINT2);
free_irq(IRQ_EINT2, NULL);
gpio_free(S5PV210_GPH0(2));
return 0;
}
static ssize_t button_mark_read (struct file * file,
char __user * out, size_t size, loff_t * offset)
{
int ret = 0;
//当press_mark = 0的时候, button读的时候应用进程将被阻塞,
printk(KERN_ERR "press_mark = %d\n", press_mark);
wait_event_interruptible(button_press_mark, press_mark);
ret = copy_to_user(out, &key_val, size);
if (ret)
{
printk(KERN_ERR "copy_to_user error\n");
return -EINVAL;
}
press_mark = 0;
return 0;
}
static unsigned int button_mark_poll (struct file * file,
struct poll_table_struct * wait)
{
unsigned int mask = 0;
poll_wait(file, &button_press_mark, wait);
if (press_mark)
mask |= POLLIN | POLLRDNORM;
return mask;
}
static int button_mark_fasync(int fd, struct file * file, int on)
{
return fasync_helper(fd, file, on, &markbutton_fasync);
}
static struct file_operations button_operations_mark =
{
.owner = THIS_MODULE,
.open = button_mark_open,
.release = button_mark_close,
.read = button_mark_read,
.poll = button_mark_poll,
.fasync = button_mark_fasync,
};
static int __init s5pv210_button_init(void)
{
init_timer(&buttons_timer);
buttons_timer.function = buttons_timer_function;
add_timer(&buttons_timer);
major_button = register_chrdev(0, "button", &button_operations_mark);
if (major_button < 0)
{
printk(KERN_ERR "register_chrdev error\n");
goto err_register_chrdev;
}
class_button = class_create(THIS_MODULE, "BUTTON_MARK");
if (!class_button)
{
printk(KERN_ERR "class_create error\n");
goto err_class_create;
}
device_button = device_create(class_button, NULL, MKDEV(major_button, 0),
NULL, "button-mark");
if (!device_button)
{
printk(KERN_ERR "device_create error\n");
goto err_device_create;
}
return 0;
err_device_create:
class_destroy(class_button);
err_class_create:
unregister_chrdev(major_button, “button”);
err_register_chrdev:
del_timer(&buttons_timer);
return -EBUSY;
}
static void __exit s5pv210_button_exit(void)
{
device_destroy(class_button, MKDEV(major_button, 0));
class_destroy(class_button);
unregister_chrdev(major_button, “button”);
del_timer(&buttons_timer);
}
module_init(s5pv210_button_init);
module_exit(s5pv210_button_exit);
// MODULE_xxx这种宏作用是用来添加模块描述信息
MODULE_LICENSE(“GPL”); // 描述模块的许可证
MODULE_AUTHOR(“Mark 867439374@qq.com”); // 描述模块的作者
MODULE_DESCRIPTION(“s5pv210 button driver”); // 描述模块的介绍信息
MODULE_ALIAS(“s5pv210_button”); // 描述模块的别名信息
应用:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <poll.h>
#include <signal.h>
#define PATHNAME “/dev/button-mark”
static int mark_fd = -1;
int val = -1;
void mark_button_sig(int sig)
{
read(mark_fd, &val, sizeof(int));
printf(“read is %d\n”, val);
}
int main(void)
{
int flag = -1;
signal(SIGIO,mark_button_sig);
mark_fd = open(PATHNAME, O_RDWR);
if (mark_fd < 0)
{
perror("");
return -1;
}
/* F_SETOWN: Set the process ID
* 告诉内核,发给谁 发给哪个进程
* 这里的意思就是发给当前进程
*/
fcntl(mark_fd, F_SETOWN, getpid());
/* F_GETFL :Read the file status flags
* 读出当前文件的状态
* 读 改 写 三部曲
*/
flag = fcntl(mark_fd,F_GETFL);
flag |= FASYNC;
/* F_SETFL: Set the file status flags to the value specified by arg
* int fcntl(int mark_fd, int cmd, long arg);
* 修改当前文件的状态,添加异步通知功能
*/
fcntl(mark_fd,F_SETFL,flag);
while(1)
{
//test
sleep(1000);
}
close(mark_fd);
return 0;
}
程序分析:
1.应用程序绑定信号和信号处理函数,signal(SIGIO,mark_button_sig);
2.应用使用fcntl(mark_fd, F_SETOWN, getpid());告诉内核要把信号发到哪个进程,这个的意思,是要内核把信号发到当前进程
3.flag |= FASYNC; fcntl(mark_fd,F_SETFL,flag);修改当前文件的状态,添加异步通知功能
4.驱动里面当按键按下的时候,会调用kill_fasync(&markbutton_fasync, SIGIO, POLL_IN);, 这个函数发一个SIGIO信号给当前的进程,也就是打开这个文件描述符的进程。 /*
kill_fasync函数告诉应用程序有数据可读了
markbutton_fasync 结构体里包含了发给谁 (应用层用PID指定)
SIGIO : 表示要发送的信号类型
POLL_IN: 表示发送的原因(有数据可读了)
*/
5.所以当按键按下的时候,驱动会向打开这个文件描述符的进程,发送一个SIGIO信号,然后应用进程收到信号的时候,就会去执行信号绑定的函数,然后read
6.button_mark_fasync的实现:
static int button_mark_fasync(int fd, struct file * file, int on)
{
return fasync_helper(fd, file, on, &markbutton_fasync);
}
这个file_opration里面的fasync不用实现,直接调用的是fasync_helper函数,这个由内核实现的。