一、驱动程序的实现
1、定义static struct fasync_struct *newdrv_async;
2、加入
static struct file_operations new_drv_fops = {
.fasync =new_drv_fasync,
};
static int new_drv_fasync (int fd, struct file *filp, int on)
{
return fasync_helper (fd, filp, on, &newdrv_async);//初始化newdrv_async
}
3、发送信号
kill_fasync(&newdrv_async,SIGIO,POLL_IN);//发送信号,给newdrv_async
二、应用程序实现
设置好目标设备的SIGIO信号处理函数;等待内核kill_fasync()释放 SIGIO 信号
signal(SIGIO,signal_fun);
让设备中的信号发到当前进程;告诉内核(驱动程序)发给谁--本程序的pid
fcntl(fd,F_SETOWN,getpid());
获得文件fd的flags,即open函数的第二个参数O_RDWR
oflags=fcntl(fd,F_GETFL);
给设备文件fd添加异步通知模式,最终调用驱动的.fasync->fansync_help函数,初始化/释放,将fd加入异步IO通知队列
fcntl(fd,F_SETFL,oflags|FASYNC);
驱动程序
#include <linux/module.h>
#include <linux/sched.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 <linux/device.h>
#include <mach/gpio.h>
#include <linux/interrupt.h>
#include <linux/poll.h>
static struct class *newdrv_class;
volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpfdat = NULL;
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
static unsigned char key;
static struct fasync_struct *newdrv_async;
struct pin_desc{
unsigned int pin;//引脚
unsigned int key_val;//键值,按下01 02 03 04;松开81 82 83 84
};
struct pin_desc pins_desc[4]={
{S3C2410_GPF(0), 0x01},
{S3C2410_GPF(2), 0x02},
};
static int 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)//1 松开
{
key=0x80|pindesc->key_val;
}
else//按下
key=pindesc->key_val;
kill_fasync(&newdrv_async,SIGIO,POLL_IN);//发送信号,给newdrv_async
return IRQ_RETVAL(IRQ_HANDLED);//接收到了中断信号,并处理,返回1
}
static int new_drv_open(struct inode *inode, struct file *file)
{
/* 中断引脚 中断函数 触发方式 名称 传入dev—id */
request_irq(IRQ_EINT0, buttons_irq, (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING), "S2", &pins_desc[0]);
request_irq(IRQ_EINT2, buttons_irq, (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING), "S3", &pins_desc[1]);
return 0;
}
static ssize_t new_drv_read(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
if(count!=1)
return -EINVA;
copy_to_user(buf, &key, 1);
return 1;
}
static int new_drv_close(struct inode *inode, struct file *file)
{
free_irq(IRQ_EINT0, &pins_desc[0]);
free_irq(IRQ_EINT2, &pins_desc[1]);
return 0;
}
static int new_drv_fasync (int fd, struct file *filp, int on)
{
return fasync_helper (fd, filp, on, &newdrv_async);//初始化newdrv_async
}
static struct file_operations new_drv_fops = {
.owner =THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open =new_drv_open,
.read =new_drv_read,
.release=new_drv_close,
.fasync =new_drv_fasync,
};
int major;
static int new_drv_init(void)
{
major = register_chrdev(0, "newdrv", &new_drv_fops); // 注册, 自动分配并返回主设备号
/*在/sys/class/下创建类目录*/
newdrv_class = class_create(THIS_MODULE, "newdrv");
/*根据类目录,在驱动模块初始化函数中实现设备节点的自动创建*/
device_create(newdrv_class, NULL, MKDEV(major, 0), NULL, "newdrv");
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;
return 0;
}
static void new_drv_exit(void)
{
unregister_chrdev(major, "newdrv");
device_destroy(newdrv_class, MKDEV(major, 0));
class_destroy(newdrv_class);
iounmap(gpfcon);
}
module_init(new_drv_init);
module_exit(new_drv_exit);
MODULE_LICENSE("GPL");
应用程序
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
int fd;
void signal_fun(int signum) //信号值-USR1
{
unsigned char key_val,cnt;
read(fd,&key_val,1);
printf("signal=%d, %d times\n",signum,++cnt);
}
int main(int argc, char **argv)
{
unsigned char key_val;
int oflags;
/*设置好目标设备的SIGIO信号处理函数;等待内核kill_fasync()释放 SIGIO 信号*/
**signal(SIGIO,signal_fun);**
fd = open("/dev/newdrv", O_RDWR);
if(fd<0)
printf("can't open\n");
/*F_SETOWN:设置文件描述词fd上接收SIGIO
或 SIGURG事件信号的进程或进程组标识;
F_SETFL :设置文件状态标志;
F_GETFL:读取文件状态标识
*/
/*让设备中的信号发到当前进程;告诉内核(驱动程序)发给谁--本程序的pid*/
fcntl(fd,F_SETOWN,getpid());
/*获得文件fd的flags,即open函数的第二个参数O_RDWR*/
oflags=fcntl(fd,F_GETFL);
/*给设备文件fd添加异步通知模式,最终调用驱动的.fasync->fansync_help函数,初始化/释放,将fd加入异步IO通知队列*/
fcntl(fd,F_SETFL,oflags|FASYNC);
while (1)
{
sleep(1000);
}
return 0;
}