字符设备驱动(七)按键异步通知

按键驱动方式对比

  1. 查询:耗资源
  2. 中断: 没有超时机制,当没有中断作为生产者,read函数一直休眠
  3. poll机制,加入超时机制
  • 上述三种都是app主动去获取按键,使用异步通知的形式可以使按键发生后,通知app去读取

进程间发信号

以前使用kill -9 pid实际也就是发送信号给进程,信号9为关闭进程.我们使用 man signal查看需要头文件 #include <signal.h>测试程序如下如果用在gcc下编译需要为sleep函数添加头文件#include <unistd.h>

NAME
       signal - ANSI C signal handling

SYNOPSIS
       #include <signal.h>

       typedef void (*sighandler_t)(int);

       sighandler_t signal(int signum, sighandler_t handler);

实例函数如下arm-linux-gcc -o test test.c

#include <stdio.h>
#include <signal.h>
#include <unistd.h>

 //typedef void (*sighandler_t)(int); 
void my_signal_fun(int signum)
{
	static int cnt=0;
	printf("signum=%d ,%d times\n",signum,++cnt );

}

int main(int argc, char const *argv[])
{
	signal(SIGUSR1,my_signal_fun);
	while(1)
	{

		sleep(100);
	}
	return 0;
}

测试使用kill -USR1 pid后会打印信号

book@100ask:~/stu/dri/code/10th$ ./test &
[3] 4356
book@100ask:~/stu/dri/code/10th$ kill -USR1 4356
signum=10 ,1 times
book@100ask:~/stu/dri/code/10th$ kill -10 4356
signum=10 ,2 times

目标

驱动程序使用信号通知应用程序去读取按键

如何让驱动通知应用

  1. 应用程序注册信号处理函数
  2. 驱动程序发信号
  3. 信号被发给应用程序,应用程序需要告诉自己的pid给驱动程序
  4. 驱动程序 调用void kill_fasync(struct fasync_struct **fp, int sig, int band)发信号

程序编写

驱动程序

搜索下发送信号的函数kill_fasync,寻找一个字符设备是怎么使用的,在drivers/char/rtc.c中有如下调用

kill_fasync (&rtc_async_queue, SIGIO, POLL_IN);

//结构体定义如下:
static struct fasync_struct *rtc_async_queue;
struct fasync_struct {
	int	magic;
	int	fa_fd;
	struct	fasync_struct	*fa_next; /* singly linked list */
	struct	file 		*fa_file;
};

信号的接受者被定义在fasync_struct中,搜索下其初始化函数,发现函数rtc_fasync被调用如下形式,也就是类似于open、close的形式了

static const struct file_operations rtc_fops = {
	.owner		= THIS_MODULE,
	.llseek		= no_llseek,
	.read		= rtc_read,
#ifdef RTC_IRQ
	.poll		= rtc_poll,
#endif
	.ioctl		= rtc_ioctl,
	.open		= rtc_open,
	.release	= rtc_release,
	.fasync		= rtc_fasync,
};

函数的原型如下,这个函数用来初始化结构体,应用程序会最终调用他告知驱动应该信号给哪个pid

static int rtc_fasync (int fd, struct file *filp, int on)
{
	return fasync_helper (fd, filp, on, &rtc_async_queue);
}

也就是是说应用程序通过fasync来调用驱动具体的rtc_fasync,这个函数会设置fasync_struct这个结构体,这个结构体会被当做驱动发送信号函数的参数,也就是说应用程序告诉驱动程序其目标

步骤如下

  1. 定义一个fasync_struct结构体,在中断中使用发送信号

  2. 定义一个供app函数调用的.fasync

    static int drv_fasync (int fd, struct file *filp, int on)
    {
        printk("driver: drv_fasync\n");
    	return fasync_helper (fd, filp, on, &drv_async_queue);
    }
    

应用程序

  1. 设置哪个进程将接受到来自fdSIGIO信号,如何获取自身程序的pid

    man pid
    
    SYNOPSIS
           #include <sys/types.h>
           #include <unistd.h>
    
           pid_t getpid(void);
           pid_t getppid(void);
    
  2. 获取当前的标志,添加FASYNC机制

• 复制一个现存的描述符(cmd=F_DUPFD)    。
• 获得/设置文件描述符标记(cmd=F_GETFD或F_SETFD)   。
• 获得/设置文件状态标志(cmd=F_GETFL或F_SETFL) 。
• 获得/设置异步I/O有权(cmd=F_GETOWN或F_SETOWN) 。
• 获得/设置记录锁(cmd=F_GETLK,F_SETLK或F_SETLKW)。

程序

fcntl(fd, F_SETOWN, getpid());			//告知信号发送给哪个进程,
Oflags = fcntl(fd, F_GETFL);			//获取当前的文件状态   
fcntl(fd, F_SETFL, Oflags | FASYNC);  	// 改变fasync标记,
										//最终会调用到驱动的faync > fasync_helper
										//初始化/释放fasync_struct
										//这个会触发驱动函数的fasync

完整代码如下

驱动

#include <linux/module.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 <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <linux/poll.h>
//#include <linux/interrupt.h>
volatile unsigned long *gpfcon;
volatile unsigned long *gpfdat;
volatile unsigned long *gpgcon;
volatile unsigned long *gpgdat;

static struct class *drv_class;
static struct class_device	*drv_class_dev;

// 定义一个名为`button_waitq`的队列
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
// flag=1 means irq happened and need to update
int flag=0;

static struct fasync_struct *drv_async_queue; 

struct pin_desc{
	unsigned int pin;
	unsigned int key_val;
};


/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
/* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
static unsigned char key_val;

struct pin_desc pins_desc[4] = {
	{S3C2410_GPF0, 0x01},
	{S3C2410_GPF2, 0x02},
	{S3C2410_GPG3, 0x03},
	{S3C2410_GPG11, 0x04},
};


static irqreturn_t buttons_irq(int irq, void *dev_id)
{
	printk("irq%d\r\n",irq);

	struct pin_desc * pindesc = (struct pin_desc *)dev_id;
	unsigned int pinval;
	
	pinval = s3c2410_gpio_getpin(pindesc->pin);

	if (pinval)
	{
		/* 松开 */
		key_val = 0x80 | pindesc->key_val;
	}
	else
	{
		/* 按下 */
		key_val = pindesc->key_val;
	}

	//wake_up_interruptible(&button_waitq);   /* 唤醒休眠的进程 */
	flag=1;

    kill_fasync (&drv_async_queue, SIGIO, POLL_IN);
	return IRQ_RETVAL(IRQ_HANDLED);
}

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

	if (flag)
		mask |= POLLIN | POLLRDNORM;

	return mask;
}

static int drv_open(struct inode *inode, struct file *file)
{
	/* 配置GPF0,2为输入引脚 */
	/* 配置GPG3,11为输入引脚 */
	request_irq(IRQ_EINT0,  buttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[0]);
	request_irq(IRQ_EINT2,  buttons_irq, IRQT_BOTHEDGE, "S3", &pins_desc[1]);
	request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "S4", &pins_desc[2]);
	request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "S5", &pins_desc[3]);	
	return 0;
}

int 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_EINT11,&pins_desc[2]);
	free_irq(IRQ_EINT19,&pins_desc[3]);
	return 0;
}


static ssize_t drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
	//int minor =  MINOR(file->f_dentry->d_inode->i_rdev);
	//printk("drv_write=%d\n",minor);
	return 0;
}

static ssize_t drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
	if (size != 1)
	return -EINVAL;

	/* 如果没有按键动作, 休眠 */
	//wait_event_interruptible(button_waitq, flag);

	/* 如果有按键动作, 返回键值 */
	copy_to_user(buf, &key_val, 1);
	flag = 0;
	
	return 1;
}


static int drv_fasync (int fd, struct file *filp, int on)
{
    printk("driver: drv_fasync\n");
	return fasync_helper (fd, filp, on, &drv_async_queue);
}

static struct file_operations drv_fops = {
	.owner  =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .open   =   drv_open,     
	.write	=	drv_write,
	.read	=	drv_read,	 
	.release =  drv_close, 
	.poll    =  drv_poll, 
    .fasync  =  drv_fasync,
};

static int major;
static int drv_init(void)
{
	int minor=0;
	major=register_chrdev(0, "drv", &drv_fops); // 注册, 告诉内核
	drv_class = class_create(THIS_MODULE, "drv");
	drv_class_dev = class_device_create(drv_class, NULL, MKDEV(major, 0), NULL, "xyz%d", minor);

	gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
	gpfdat = gpfcon + 1;
	gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
	gpgdat = gpgcon + 1;
	return 0;
}

static void drv_exit(void)
{
	unregister_chrdev(major, "drv"); // 卸载
	class_device_unregister(drv_class_dev);
	class_destroy(drv_class);
	iounmap(gpfcon);
	iounmap(gpgcon);
}

module_init(drv_init);
module_exit(drv_exit);

MODULE_AUTHOR("xxx");
MODULE_VERSION("0.1.0");
MODULE_DESCRIPTION("S3C2410/S3C2440 LED Driver");
MODULE_LICENSE("GPL");


App


#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 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/xyz0", O_RDWR);
	if (fd < 0)
	{
		printf("can't open!\n");
	}

	fcntl(fd, F_SETOWN, getpid());	
	Oflags = fcntl(fd, F_GETFL); 
	fcntl(fd, F_SETFL, Oflags | FASYNC);


	while (1)
	{
		sleep(1000);
	}
	
	return 0;
}

测试

可以发现打开文件的时候会有提示driver: drv_fasync,说明App会主动调用.fasync

# insmod dri.ko
# 后台运行
# ./test &
# driver: drv_fasync
#实际休眠状态的

# ps
  PID  Uid        VSZ Stat Command
  798 0          1308 S   ./test
....

# 按键按下正常打印
# irq55
key_val: 0x3
irq55
key_val: 0x83
irq18
posted @ 2018-11-25 17:24  zongzi10010  阅读(387)  评论(0编辑  收藏  举报