一 what
在我们前面的三个帖子中,我们介绍了linux字符设备驱动的查询,中断,以及poll机制方式来获取一个gpio引脚电平,这三种方式的共同点,都需要我们应用程序主动通过调用read函数去读取,那么有没有别的方式呢?比如说由驱动来告知应用程序,因为发生了某件事情,你可以来读取了呢?
答案是肯定的,linux内核帮我们封装了一个异步通知signal的方式,我们可以在驱动程序中,发生某件事情(比如说发生了gpio中断)时,通过signal告知应用程序,你可以读取某个字符设备的状态了。
二 why
同样地,为了降低cpu占用率,以及优化应用程序的工作方式,我们可以采样异步通知的方式来获取字符设备的状态。
三 how
在开始写驱动之前,我们先通过一个简单的例子来熟悉一下signal的流程,我们写一个应用程序,如下
#include <stdio.h>
#include <signal.h>
void my_signal_fun(int signum)
{
static int cnt = 0;
printf("signal = %d, %d times\n", signum, ++cnt);
}
int main(int argc, char *argv[])
{
signal(SIGUSR1, my_signal_fun);
while (1)
{
sleep(1000);
}
return 0;
}
我们主要关心函数 signal(SIGUSR1, my_signal_fun),它就是一种和内核驱动异步通知的一种方式,关于这个函数的具体实现,请阅读linux源码,这里不做详细分析,我们按如下步骤测试这个应用程序
- 编译 arm-linux-gcc -o signal_my signal_my.c
- 上传到服务器 cp signal_my /work/nfs_root/first_fs
- 后台运行可执行程序 ./signal_my &
- 查看进程状态 ps
查看进程./signal_my的pid号 - 内核驱动发送信号给进程 kill -USR1 772 (772是通过ps查询到的./signal_my进程号)
我们每次在串口下面输入kill -USR1 772命令,串口下面都会打印my_signal_fun函数中的部分
- 退出signal_my进程 kill -9 772
sinal的处理函数的主要几个要点如下
a. 注册信号处理函数,也就是my_signal_fun函数实现,以及signal(SIGUSR1, my_signal_fun)注册
b. 谁来发,很明显是内核驱动发出来,告知应用程序
c. 发给谁,内核驱动要发给应用程序
d. 怎么发,内核驱动要怎么才能发出一个信号给到应用程序呢
带着上面的几个问题,我们先来看内核驱动要实现的地方
a. 谁来发,内核驱动来发
在这里字符设备驱动的open、read函数还是需要的,因为和之前的帖子中一模一样,这里就不在贴出这部分代码。
既然支持这种signal机制,首先应用程序调用signal()函数时,字符设备驱动要能找到对应的signal函数,所以我们先定义一个fifth_drv_fasync函数,如下
static struct fasync_struct *button_async;
static int fifth_drv_fasync(int fd, struct file *filp, int on)
{
printk("driver: app use it\n");
return fasync_helper(fd, filp, on, &button_async);
}
static struct file_operations fifth_drv_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = fifth_drv_open,
.read = fifth_drv_read,
.release = fifth_drv_close,
.poll = fifth_drv_poll,
.fasync = fifth_drv_fasync,
};
每次应用程序调用signal()函数时,驱动中就会调用fifth_drv_fasync函数。
b. 怎么发
内核驱动中知道了发给谁之后,那么怎么实现发送的过程呢?一般地,可以在中断中调用linux内核函数kill_fasync来实现发送过程
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
struct pin_desc *pindesc = (struct pin_desc *)dev_id;
unsigned int pinval;
//printk("irq=%d\n", irq);
pinval = s3c2410_gpio_getpin(pindesc->pin);
if (pinval)
{
/* 松开 */
key_val = 0x80 | pindesc->key_val;
}
else
{
/* 按下 */
key_val = pindesc->key_val;
}
ev_press = 1; /* 表示中断发生了 */
wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */
kill_fasync(&button_async, SIGIO, POLL_IN);
return IRQ_RETVAL(IRQ_HANDLED);
}
看完字符设备驱动需要做的工作之后,我们来看看应用程序需需要做的工作
a. 注册信号处理函数
在信号处理函数中,调用read函数读取按键状态
void my_signal_fun(int signum)
{
unsigned int key_val;
read(fd, &key_val, 1);
printf("key_val=0x%x\n", key_val);
}
b. 如何告诉设备驱动,应该发给我,调用内核提供的函数fcntl来实现
/* 注册信号处理函数 */
signal(SIGIO, my_signal_fun);
/* 告诉驱动程序 它的pid号,解决发给谁的问题 */
fcntl(fd, F_SETOWN, getpid());
/* 改变fasync标志,最终会调用fifth_drv_fasync驱动 */
oflags = fcntl(fd, F_GETFL)
fcntl(fd, F_SETFL, oflags | 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>
static struct class *fifthdrv_class;
static struct class_device *fifthdrv_class_dev;
volatile unsigned long *gpfcon;
volatile unsigned long *gpgcon;
volatile unsigned long *gpfdat;
volatile unsigned long *gpgdat;
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
/* 中断事件标志, 中断服务程序将它置1,fifth_drv_read将它清0 */
static volatile int ev_press = 0;
static unsigned char key_val;
struct pin_desc
{
unsigned int pin;
unsigned int key_val;
};
/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
/* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
struct pin_desc pins_desc[4] =
{
{S3C2410_GPF0, 0x01},
{S3C2410_GPF2, 0x02},
{S3C2410_GPG3, 0x03},
{S3C2410_GPG11, 0x04}
};
static struct fasync_struct *button_async;
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
struct pin_desc *pindesc = (struct pin_desc *)dev_id;
unsigned int pinval;
//printk("irq=%d\n", irq);
pinval = s3c2410_gpio_getpin(pindesc->pin);
if (pinval)
{
/* 松开 */
key_val = 0x80 | pindesc->key_val;
}
else
{
/* 按下 */
key_val = pindesc->key_val;
}
ev_press = 1; /* 表示中断发生了 */
wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */
kill_fasync(&button_async, SIGIO, POLL_IN);
return IRQ_RETVAL(IRQ_HANDLED);
}
static int fifth_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;
}
static int fifth_drv_read(struct file *filp, char __user *buf,
size_t size, loff_t *ppos)
{
/* 返回4个引脚电平状态 */
if (size != 1)
return -EINVAL;
/* 如果没有按键动作, 休眠 */
wait_event_interruptible(button_waitq, ev_press);
/* 如果有按键动作, 返回键值 */
copy_to_user(buf, &key_val, 1);
ev_press = 0;
return (sizeof(key_val));
}
int fifth_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 unsigned int fifth_drv_poll(struct file *file, struct poll_table_struct *wait)
{
unsigned int mask = 0;
poll_wait(file, &button_waitq, wait); // 不会立即休眠
if (ev_press)
mask |= POLLIN | POLLRDNORM;
return mask;
}
static int fifth_drv_fasync(int fd, struct file *filp, int on)
{
printk("driver: app use it\n");
return fasync_helper(fd, filp, on, &button_async);
}
static struct file_operations fifth_drv_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = fifth_drv_open,
.read = fifth_drv_read,
.release = fifth_drv_close,
.poll = fifth_drv_poll,
.fasync = fifth_drv_fasync,
};
static int major;
static int fifth_drv_init(void)
{
major = register_chrdev(0, "my_fifth_drv", &fifth_drv_fops);
fifthdrv_class = class_create(THIS_MODULE, "myfifthdrv");
fifthdrv_class_dev = class_device_create(fifthdrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/xyz */
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;
gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
gpgdat = gpgcon + 1;
return 0;
}
static void fifth_drv_exit(void)
{
unregister_chrdev(major, "my_fifth_drv");
class_device_unregister(fifthdrv_class_dev);
class_destroy(fifthdrv_class);
iounmap(gpfcon);
iounmap(gpgcon);
return 0;
}
module_init(fifth_drv_init);
module_exit(fifth_drv_exit);
MODULE_LICENSE("GPL");
应用程序代码如下
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <poll.h>
static int fd;
void my_signal_fun(int signum)
{
unsigned int key_val;
read(fd, &key_val, 1);
printf("key_val=0x%x\n", key_val);
}
/* forthdrvtest
*/
int main(int argc, char **argv)
{
int oflags;
fd = open("/dev/buttons", O_RDWR);
if (fd < 0)
{
printf("can't open!\n");
return 0;
}
signal(SIGIO, my_signal_fun);
fcntl(fd, F_SETOWN, getpid());
oflags = fcntl(fd, F_GETFL);
fcntl(fd, F_SETFL, oflags | FASYNC);
while (1)
{
sleep(1000);
}
return 0;
}
四 test
测试方式和《linux字符设备驱动之poll篇(三)》中的过程完全一样,发现我们按键按下之后,串口会有提示打印