概述
适用场景
使用***休眠-唤醒、POLL机制***时,都需要***休眠等待***某个事件发生时,差别在于后者可以指定休眠的时长。
如果不想APP休眠怎么办,那么需要驱动程序有数据时***主动通知*** App,App受到信号后执行信息处理函数。
使用流程
驱动程序通过发送信号给App,通知App处理数据。
- 驱动程序发送数据给App
- 驱动程序发送信号给App
- 发送SIGIO的信号
- 内核里面有相应的函数,来支持驱动发送信号给App
- App来接受信号,要让驱动发送信号给App。前提是App把自己告诉给驱动
- App收到信号后,执行信号处理函数。
- 信号处理函数和信号的联系,通过App注册信号处理函数绑定。
#define SIGHUP 1
#define SIGINT 2
#define SIGQUIT 3
#define SIGILL 4
#define SIGTRAP 5
#define SIGABRT 6
#define SIGIOT 6
#define SIGBUS 7
#define SIGFPE 8
#define SIGKILL 9
#define SIGUSR1 10
#define SIGSEGV 11
#define SIGUSR2 12
#define SIGPIPE 13
#define SIGALRM 14
#define SIGTERM 15
#define SIGSTKFLT 16
#define SIGCHLD 17
#define SIGCONT 18
#define SIGSTOP 19
#define SIGTSTP 20
#define SIGTTIN 21
#define SIGTTOU 22
#define SIGURG 23
#define SIGXCPU 24
#define SIGXFSZ 25
#define SIGVTALRM 26
#define SIGPROF 27
#define SIGWINCH 28
#define SIGIO 29 //驱动常用的信号
#define SIGPOLL SIGIO
/*
#define SIGLOST 29
*/
#define SIGPWR 30
#define SIGSYS 31
#define SIGUNUSED 31
/* These should not be considered constants from userland. */
#define SIGRTMIN 32
#ifndef SIGRTMAX
#define SIGRTMAX _NSIG
#endif
App内需要做的事
- App指定驱动程序的设备节点,告诉内核驱动,这个驱动发送SIGIO给App
- App需要把自己的进程ID号告诉指定的驱动,好让它找到App
- App需要指定可以接收的信号。
涉及的函数:
#include <signal.h>
//需要编写的函数类型
typedef void (*sighandler_t)(int);
//用来注册函数的函数,signum指定接受的新信号,handler指定处理的函数
sighandler_t signal(int signum, sighandler_t handler);
#include <unistd.h>
#include <fcntl.h>
//用来修改文件描述符
int fcntl(int fd, int cmd, ... /* arg */ );
//用来设置接受SIGIO和SIGURG信号事件的进程ID到文件描述符
F_SETOWN (int)
//返回文件访问模式和文件状态
F_GETFL (void)
//设置文件状态标志位,文件访问模式(O_RDONLY, O_WRONLY, O_RDWR)
F_SETFL (int)
驱动要做的事
- 记录App设置的进程ID号,并记录。
- 驱动需要能能异步通知功能,驱动中有对应的函数
- 当中断发生时,有数据时,驱动程序调用内核辅助函数(kill_fasync)发信号给App
涉及的函数:
//fasync_helper会分配、构造一个fasync_struct结构体,返回负数出错
int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
//当驱动文件flag被设置为FAYNC时,*fapp->fa_file = filp; //filp表示驱动程序文件,里面有设置的PID
//当驱动程序被设置为非FAYNC时,*fapp->fa_file = NULL;
//fp非空时,可以从中得到PID,得到发送给哪个APP
//sig表示发送什么信号,这里时SIGIO
//band表示为什么发信号,POLLIN有数据可读
void kill_fasync(struct fasync_struct **fp, int sig, int band)
编程梳理
源码
button_signal.c
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/platform_device.h>
#include <linux/device.h>
#include <linux/sched.h>
#include <linux/wait.h>
#include <linux/uaccess.h>
#include <linux/irqreturn.h>
#include <linux/gpio/consumer.h>
#include <linux/of_gpio.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/fs.h>
#include <linux/wait.h>
#include <linux/poll.h>
struct gpio_key {
int gpio;
struct gpio_desc *gpiod;
int flag;
int irq;
};
static struct gpio_key *myBtn_key;
static int button_major = 0;
static struct class *button_class;
static struct fasync_struct *btn_async;
static DECLARE_WAIT_QUEUE_HEAD(gpio_key_wait);
#define MaxSize 128
struct QNode {
int Data[MaxSize];
int rear;
int front;
};
typedef struct QNode *Queue;
int IsEmpty(Queue Q);
void AddQ(Queue PtrQ, int item);
int DeleteQ(Queue PtrQ);
int IsEmpty(Queue Q)
{
return (Q->rear == Q->front); //1:empty 0:not empty
}
void AddQ(Queue PtrQ, int item)
{
if((PtrQ->rear+1)%MaxSize == PtrQ->front) {
printk("%s,Queue fulln", __FUNCTION__);
return;
}
PtrQ->rear = (PtrQ->rear+1)%MaxSize;
PtrQ->Data[PtrQ->rear] = item;
}
int DeleteQ(Queue PtrQ)
{
if(PtrQ->front == PtrQ->rear) {
printk("%s,Queue emptyn", __FUNCTION__);
return -1;
} else {
PtrQ->front = (PtrQ->front+1)%MaxSize;
return PtrQ->Data[PtrQ->front];
}
}
static Queue irqBuff;
static ssize_t button_read(struct file *file, char __user *buf, size_t size, loff_t *offset)
{
int err;
int val;
wait_event_interruptible(gpio_key_wait, !IsEmpty(irqBuff));
val = DeleteQ(irqBuff);
err = copy_to_user(buf, &val, 4);
if(err != 4) {
return -1;
}
return 4;
}
static unsigned int button_poll(struct file *fp, poll_table * wait)
{
printk("%s,button polln", __FUNCTION__);
poll_wait(fp, &gpio_key_wait, wait);
return IsEmpty(irqBuff) ? 0 : POLLIN | POLLRDNORM;
}
int button_fasync(int fd, struct file *file, int on)
{
if(fasync_helper(fd, file, on, &btn_async) >= 0)
return 0;
else
return -EIO;
}
static struct file_operations button_ops = {
.owner = THIS_MODULE,
.read = button_read,
.poll = button_poll,
.fasync = button_fasync,
};
static irqreturn_t myBtn_irq_request(int irq, void *dev_id)
{
struct gpio_key *gpio_key = dev_id;
int val;
val = gpiod_get_value(gpio_key->gpiod);
printk(KERN_WARNING"key %d %dn", gpio_key->gpio, val);
val = (myBtn_key->gpio << 8)|val;
AddQ(irqBuff, val);
wake_up_interruptible(&gpio_key_wait);
kill_fasync(&btn_async, SIGIO, POLLIN);
return IRQ_HANDLED;
}
static int my_button_probe(struct platform_device *pdev)
{
struct device_node *node = pdev->dev.of_node;
int count;
enum of_gpio_flags flag;
int i, err;
count = of_gpio_count(node);
if(!count) {
printk("%s,there isn't any gpio availiablen", __FUNCTION__);
return -1;
}
myBtn_key = (struct gpio_key*)kzalloc(sizeof(struct gpio_key)*count, GFP_KERNEL);
if(!myBtn_key) {
printk("%s,kzalloc malloc failedn", __FUNCTION__);
return -1;
}
for(i=0;i<count;i++) {
myBtn_key[i].gpio = of_get_gpio_flags(node, i, &flag);
if(myBtn_key[i].gpio < 0) {
printk("%s, of_get_gpio_flags failedn", __FUNCTION__);
return -1;
}
myBtn_key[i].gpiod = gpio_to_desc(myBtn_key[i].gpio);
myBtn_key[i].flag = flag & OF_GPIO_ACTIVE_LOW;
myBtn_key[i].irq = gpio_to_irq(myBtn_key[i].gpio);
err = request_irq(myBtn_key[i].irq, myBtn_irq_request, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
"myBtn_key", &myBtn_key[i]);
}
button_major = register_chrdev(0, "mybutton", &button_ops);
if (button_major < 0) {
printk(KERN_ERR "button : couldn't get a major number.n");
return -1;
}
button_class = class_create(THIS_MODULE, "button_class");
if(IS_ERR(button_class)) {
printk(KERN_ERR "button class: create failedn");
unregister_chrdev(button_major, "mybutton");
return -1;
}
device_create(button_class, NULL, MKDEV(button_major, 0), NULL, "mybutton%d", 0);
return 0;
}
static int my_button_remove(struct platform_device *pdev)
{
struct device_node *node= pdev->dev.of_node;
int count;
int i;
device_destroy(button_class, MKDEV(button_major, 0));
class_destroy(button_class);
unregister_chrdev(button_major, "mybutton");
count = of_gpio_count(node);
for(i=0;i<count;i++) {
free_irq(myBtn_key[i].irq, &myBtn_key[i]);
}
kfree(myBtn_key);
return 0;
}
static struct of_device_id mybuttons[] = {
{ .compatible = "mybtn,btn_drv" },
{ },
};
static struct platform_driver my_button_driver = {
.probe = my_button_probe,
.remove = my_button_remove,
.driver = {
.name = "button_dirver",
.of_match_table = mybuttons,
},
};
static int gpio_button_init(void)
{
int err;
irqBuff = (Queue)kzalloc(sizeof(struct QNode), GFP_KERNEL);
err = platform_driver_register(&my_button_driver);
printk(KERN_WARNING"my button dirver initn");
return 0;
}
static void gpio_button_exit(void)
{
platform_driver_unregister(&my_button_driver);
kfree(irqBuff);
printk(KERN_WARNING"my button dirver exitn");
}
module_init(gpio_button_init);
module_exit(gpio_button_exit);
MODULE_LICENSE("GPL");
button_test.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
static int fd;
static void sig_func(int sig)
{
int val;
read(fd, &val, 4);
printf("get button: 0x%xn", val);
}
int main(int argc, char *argv[])
{
int val;
int flags;
if(argc != 2) {
printf("Usage: %d <dev>n", argv[0]);
return -1;
}
signal(SIGIO, sig_func);
fd = open(argv[1], O_RDWR);
if(fd<0) {
printf("can not open file %sn", argv[1]);
return -1;
}
fcntl(fd, F_SETOWN, getpid());
flags = fcntl(fd, F_GETFL);
fcntl(fd, F_SETFL, flags | FASYNC);
while(1) {
printf("hello worldn");
sleep(2);
}
close(fd);
return 0;
}
最后
以上就是慈祥野狼为你收集整理的rk3288 异步通知适用场景使用流程源码的全部内容,希望文章能够帮你解决rk3288 异步通知适用场景使用流程源码所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复