异步通知的意思是:一旦设备就绪,则主动通知应用程序,这样应用程序根本就不需要查询设备状态,这一点非常类似于硬件上"中断"地概念,比较准确的称谓是"信号驱动(SIGIO)的异步I/O"。
异步通知的功能需要应用和驱动完成一些步骤。
1. 应用程序应做的工作
(1)注册信号处理函数。
通过signal 或sigaction()实现。
(2)使进程成为该文件的的属主进程。
通过fcntl 的F_SETOWN命令来实现。如fcntl(fd, F_SETOWN, getpid());
(3)启用异步通知功能。
通过fcntl 的F_SETFL命令设置FASYNC标记。
应用程序代码如下
-
#include <sys/types.h>
-
#include <sys/stat.h>
-
#include <fcntl.h>
-
#include <stdio.h>
-
#include <poll.h>
-
#include <signal.h>
-
#include <unistd.h>
-
-
int fd;
-
-
void my_signal_fun(int signum)
-
{
-
unsigned char key_val;
-
read(fd, &key_val, 1);
-
printf("key_val: 0x%x\n", key_val);
-
}
-
-
int main(int argc, char **argv)
-
{
-
unsigned char key_val;
-
int ret;
-
int Oflags;
-
-
signal(SIGIO, my_signal_fun);
-
-
fd = open("/dev/button_signal", O_RDWR);
-
if (fd < 0)
-
{
-
printf("can't open!\n");
-
return 0;
-
}
-
-
fcntl(fd, F_SETOWN, getpid());//告诉内核发给谁
-
-
Oflags = fcntl(fd, F_GETFL); // 读出Oflags
-
-
fcntl(fd, F_SETFL, Oflags | FASYNC);//改变fasync标记,最终会调用到驱动的fasync里的fasync_helper:初始化/释放fasync_struct
-
-
while (1)
-
{
-
sleep(1000);
-
}
-
-
return 0;
-
}
2. 驱动应做的工作
在内核里异步通知是通过,异步通知队列struct fasync_struct来实现的。在这里,我们只需要用到内核提供的两个有关异步通知队列的函数就可以了。fasync_helper 和 kill_fasync。
fasync_helper 用于把文件指针加到(或移除,当参数on = 0)异步通知队列
kill_fasync用于发送信号给应用程序。
具体的字符设备驱动通过下面这几个步骤来实现:
(1) 实现struct file_operations的.fasync函数。
这个函数,会在应用程序里,设置/删除FASYNC文件标记时会调用。该函数指针的原型为:
int (*fasync)(int fd, struct file *filp, int mode) ; 在函数实现时,我们只需要调用:
int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp) ;
(2)在数据就绪时,调用kill_fasync通知应用程序
驱动程序代码如下
-
//moudle.h 包含了大量加载模块需要的函数和符号的定义
-
#include <linux/module.h>
-
//kernel.h以便使用printk()等函数
-
#include <linux/kernel.h>
-
//fs.h包含常用的数据结构,如struct file等
-
#include <linux/fs.h>
-
//uaccess.h 包含copy_to_user(),copy_from_user()等函数
-
#include <linux/uaccess.h>
-
//io.h 包含inl(),outl(),readl(),writel()等IO口操作函数
-
#include <linux/io.h>
-
#include <linux/miscdevice.h>
-
#include <linux/pci.h>
-
//init.h来指定你的初始化和清理函数,例如:module_init(init_function)、module_exit(cleanup_function)
-
#include <linux/init.h>
-
#include <linux/delay.h>
-
#include <linux/device.h>
-
#include <linux/cdev.h>
-
#include <linux/gpio.h>
-
#include <linux/irq.h>
-
#include <linux/sched.h>
-
#include <linux/interrupt.h>
-
#include <linux/poll.h>
-
//irq.h中断与并发请求事件
-
#include <asm/irq.h>
-
//下面这些头文件是IO口在内核的虚拟映射地址,涉及IO口的操作所必须包含
-
//#include <mach/gpio.h>
-
#include <mach/regs-gpio.h>
-
#include <plat/gpio-cfg.h>
-
#include <mach/hardware.h>
-
#include <mach/map.h>
-
-
-
static struct class *button_signal_class;
-
static int major;
-
-
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
-
/* 中断事件标志, 中断服务程序将它置1,button_signal_read将它清0 */
-
static volatile int ev_press = 0;
-
-
static struct fasync_struct *button_async;
-
-
struct pin_desc {
-
unsigned int pin;
-
unsigned int button_val;
-
};
-
-
static unsigned char button_val;
-
-
static struct pin_desc pins_desc[6] = {
-
{S3C64XX_GPN(0), 0x01},
-
{S3C64XX_GPN(1), 0x02},
-
{S3C64XX_GPN(2), 0x03},
-
{S3C64XX_GPN(3), 0x04},
-
{S3C64XX_GPN(4), 0x05},
-
{S3C64XX_GPN(5), 0x06},
-
};
-
-
/*中断服务函数*/
-
/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 ...*/
-
/* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 ...*/
-
static irqreturn_t button_signal_irq(int irq, void *dev_id)
-
{
-
struct pin_desc *pindesc = (struct pin_desc *)dev_id;
-
unsigned char pinval;
-
-
pinval = gpio_get_value(pindesc->pin);
-
-
if(pinval == 1)
-
button_val = 0x80 | pindesc->button_val;
-
else
-
button_val = pindesc->button_val;
-
-
ev_press = 1; /* 表示中断发生了 */
-
wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */
-
-
kill_fasync (&button_async, SIGIO, POLL_IN);//通过这个函数去发送信号给应用程序
-
-
return IRQ_HANDLED;
-
}
-
-
static int button_signal_open(struct inode *inode, struct file *file)
-
{
-
int ret;
-
-
/*request_irq注册中断
-
*第一个参数是中断号,也是中断处理函数button_signal的第一个参数
-
*第二个参数是中断处理函数
-
*第三个参数是中断触发方式
-
*第四个参数是设备名称
-
*第五个参数是设备ID,也是中断处理函数button_signal的第二个参数
-
*/
-
ret = request_irq(IRQ_EINT(0), button_signal_irq, IRQ_TYPE_EDGE_BOTH, "k1", &pins_desc[0]);
-
if (ret){
-
printk("request IRQ_EINT(0) error!\n");
-
free_irq(IRQ_EINT(0), &pins_desc[0]);
-
}
-
ret = request_irq(IRQ_EINT(1), button_signal_irq, IRQ_TYPE_EDGE_BOTH, "k2", &pins_desc[1]);
-
if (ret){
-
printk("request IRQ_EINT(1) error!\n");
-
free_irq(IRQ_EINT(1), &pins_desc[1]);
-
}
-
-
ret = request_irq(IRQ_EINT(2), button_signal_irq, IRQ_TYPE_EDGE_BOTH, "k3", &pins_desc[2]);
-
if (ret){
-
printk("request IRQ_EINT(2) error!\n");
-
free_irq(IRQ_EINT(2), &pins_desc[2]);
-
}
-
-
ret = request_irq(IRQ_EINT(3), button_signal_irq, IRQ_TYPE_EDGE_BOTH, "k4", &pins_desc[3]);
-
if (ret){
-
printk("request IRQ_EINT(3) error!\n");
-
free_irq(IRQ_EINT(3), &pins_desc[3]);
-
}
-
-
ret = request_irq(IRQ_EINT(4), button_signal_irq, IRQ_TYPE_EDGE_BOTH, "k5", &pins_desc[4]);
-
if (ret){
-
printk("request IRQ_EINT(4) error!\n");
-
free_irq(IRQ_EINT(4), &pins_desc[4]);
-
}
-
-
ret = request_irq(IRQ_EINT(5), button_signal_irq, IRQ_TYPE_EDGE_BOTH, "k6", &pins_desc[5]);
-
if (ret){
-
printk("request IRQ_EINT(5) error!\n");
-
free_irq(IRQ_EINT(5), &pins_desc[5]);
-
}
-
return 0;
-
}
-
-
static ssize_t button_signal_read(struct file *file, char __user *buf, size_t size, loff_t * ppos)
-
{
-
if (size != 1)
-
return -EINVAL;
-
-
/* 如果没有按键动作, 休眠 */
-
/*ev_press=0时休眠,ev_press=1时继续向下执行程序*/
-
wait_event_interruptible(button_waitq, ev_press);
-
-
/* 如果有按键动作, 返回键值 */
-
if( copy_to_user(buf, &button_val, 1) ){
-
printk("error in function ‘copy_to_user’ !\n");
-
return -EFAULT;
-
}
-
-
ev_press = 0;
-
return 0;
-
}
-
-
static ssize_t button_signal_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
-
{
-
return 0;
-
}
-
-
static int button_signal_release(struct inode *inode, struct file *file)
-
{
-
free_irq(IRQ_EINT(0), &pins_desc[0]);
-
free_irq(IRQ_EINT(1), &pins_desc[1]);
-
free_irq(IRQ_EINT(2), &pins_desc[2]);
-
free_irq(IRQ_EINT(3), &pins_desc[3]);
-
free_irq(IRQ_EINT(4), &pins_desc[4]);
-
free_irq(IRQ_EINT(5), &pins_desc[5]);
-
-
return 0;
-
}
-
-
static unsigned button_signal_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;
-
}
-
-
static int button_signal_fasync (int fd, struct file *filp, int on)
-
{
-
printk("driver: button_signal_fasync\n");
-
return fasync_helper (fd, filp, on, &button_async);
-
}
-
-
static struct file_operations button_signal_fops = {
-
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
-
.open = button_signal_open,
-
.read = button_signal_read,
-
.write = button_signal_write,
-
.release = button_signal_release,
-
.poll = button_signal_poll,
-
.fasync = button_signal_fasync,
-
};
-
-
static int button_signal_init(void)
-
{
-
major = register_chrdev(0, "button_signal_dev", &button_signal_fops);//注册,告诉内核
-
button_signal_class = class_create(THIS_MODULE, "button_signal_cls");
-
device_create(button_signal_class, NULL, MKDEV(major, 0), NULL, "button_signal");/* /dev/button_signal */
-
-
return 0;
-
}
-
-
static void button_signal_exit(void)
-
{
-
unregister_chrdev(major, "button_signal_cls");
-
device_destroy(button_signal_class, MKDEV(major, 0));
-
class_destroy(button_signal_class);
-
}
-
-
module_init(button_signal_init);
-
module_exit(button_signal_exit);
-
-
MODULE_LICENSE("GPL");
阅读(863) | 评论(0) | 转发(0) |