Linux内核异步数据处理--kfifo
引题:
在驱动编程中,经常会遇到异步数据处理的情况,比如采用中断或定时器处理数据输入输出等情况
此时数据的采集与处理往往不同步,于是驱动编程中数据采集方需要将采集的数据暂时放到一个缓冲区中,使用方在需要处理数据时从缓冲区中将数据读出
驱动编程中常采用队列这种数据结构来实现整个过程,我们可以选择自己编写一个队列,也可以利用内核中现有队列kfifo来实现
kfifo使用过程:
Kfifo接口函数介绍:
#include <linux/kfifo.h> //头文件包含
//kfifo结构体类型
struct kfifo {
unsigned char *buffer; //存放数据的缓存区
unsigned int size; //buffer空间大小
unsigned int in; //指向buffer队头
unsigned int out; //指向buffer队尾
};
Kfifo原理及结构示意图如下:
假设缓冲区大小size为8
缓冲区读写下标分别为:
in%size,out%size
kfifo的本质就是一个循环队列
1.申请与释放Kfifo
为fifo分配size大小的内存空间,返回0表示成功
int kfifo_alloc(struct kfifo *fifo,unsigned int size, gfp_t gfp_mask);
参数1:kfifo类型变量地址
参数2:分配内存空间的大小(单位字节)
参数3:GFP_KERNEL (申请内存标志位)
释放创建的FIFO
void kfifo_free(struct kfifo *fifo);
2.kfifo操作函数
将数据放入kfifo内,返回值为实际写入的数据长度
unsigned int kfifo_in(struct kfifo *fifo,const void *from,unsigned int len)
从kfifo读取数据,返回值为实际读出的数据长度
unsigned int kfifo_out(struct kfifo *fifo,void *to, unsigned int len)
参数1:用户定义的kfifo
参数2:读写数据的首地址
参数3:读写数据的大小
3.kfifo辅助检测函数:
获取fifo内的已用数据个数
unsigned int kfifo_len(struct kfifo *fifo)
获取fifo总大小
unsigned int kfifo_size(struct kfifo *fifo)
检查kfifo是否为空
int kfifo_is_empty(struct kfifo *fifo)
检查kfifo是否为满
int kfifo_is_full(struct kfifo *fifo)
实例代码:
驱动端:
#include <linux/device.h>
#include <linux/interrupt.h>
#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 <mach/gpio.h>
#include <mach/regs-gpio.h> /*S5PV210_GPH3_BASE*/
#include <linux/kfifo.h>
#define EINT_DEVICE_ID 1
#define DRIVER_NAME "key1_eint"
#define err(msg) printk(KERN_ERR "%s: " msg "\n", DRIVER_NAME)
#define __debug(fmt, arg...) printk(KERN_DEBUG fmt, ##arg)
#define GPH3CON (unsigned long)(S5PV210_GPH3_BASE+ 0x00)
#define GPH3DAT (unsigned long)(S5PV210_GPH3_BASE + 0x04)
#define GPH2UP (unsigned long)(S5PV210_GPH2_BASE + 0x08)
static int major = 0; /* Driver Major Number */
static int minor = 0; /* Driver Minor Number */
struct class *key_class;
static struct device *key_device;
/*定义kfifo变量,存储键值*/
static struct kfifo key_fifo;
/*信号量,read阻塞*/
static struct semaphore readable;
irqreturn_t buttons_interrupt(int irq, void *dev_id)
{
int retval;
unsigned char keynum;
keynum = (unsigned int)dev_id;
retval = kfifo_in(&key_fifo, &keynum, sizeof(keynum));//根据返回值判断是否成功
if(retval != sizeof(keynum)){
__debug("warning: the kfifo is full\n");
}else{
up(&readable);
}
return IRQ_HANDLED;
}
static void key_io_port_init(void)
{
unsigned long reg_val;
reg_val = readl(GPH3CON);
reg_val &= ~((0x0f<<0) | (0x0f<<4));
reg_val |= ((0x01<<0) | (0x01<<4));
writel(reg_val, GPH3CON);
reg_val = readl(GPH3DAT);
reg_val &= ~((0x01<<0) | (0x01<<1));
writel(reg_val, GPH3DAT);
reg_val = readl(GPH2UP);
reg_val &= ~(0x03<<8);
reg_val |= 0x02<<8;
writel(reg_val, GPH2UP);
}
static ssize_t key_read(struct file *filp, char *buf, size_t count, loff_t *f_pos)
{
unsigned char keynum = 0;
int cpy_len;
int retval;
retval = down_interruptible(&readable);
if(retval)
return retval;
retval = kfifo_out(&key_fifo, &keynum, sizeof(keynum)); //根据返回值判断是否成功
if(retval == sizeof(keynum)){
cpy_len = min(sizeof(keynum), count);
retval = copy_to_user(buf, &keynum, cpy_len);
return (cpy_len - retval);
}
return 0;
}
/* Driver Operation structure */
static struct file_operations key_fops = {
.owner = THIS_MODULE,
.read = key_read,
};
static int __init key_eint_init(void)
{
int retval;
key_io_port_init();
/*初始化信号量为0*/
sema_init(&readable, 0);
/*申请fifo空间*/
if(kfifo_alloc(&key_fifo, 128, GFP_KERNEL) != 0){
err("kfifo malloc failed!\n");
retval = -ENOMEM;
goto error;
}
retval = set_irq_type(IRQ_EINT(20),IRQ_TYPE_EDGE_FALLING);
if(retval){
err("IRQ_EINT20 set irq type failed");
goto error;
}
retval = request_irq(IRQ_EINT(20), buttons_interrupt, IRQF_DISABLED,
"KEY1", (void *)EINT_DEVICE_ID);
if(retval){
err("request eint20 failed");
goto error;
}
/* Driver register */
major = register_chrdev(major, DRIVER_NAME, &key_fops);
if(major < 0){
err("register char device fail");
retval = major;
goto error_register;
}
key_class=class_create(THIS_MODULE,DRIVER_NAME);
if(IS_ERR(key_class)){
err("class create failed!");
retval = PTR_ERR(key_class);
goto error_class;
}
key_device=device_create(key_class,NULL, MKDEV(major, minor), NULL,DRIVER_NAME);
if(IS_ERR(key_device)){
err("device create failed!");
retval = PTR_ERR(key_device);
goto error_device;
}
__debug("register myDriver OK! Major = %d\n", major);
return 0;
error_device:
class_destroy(key_class);
error_class:
unregister_chrdev(major, DRIVER_NAME);
error_register:
free_irq(IRQ_EINT(20), (void *)EINT_DEVICE_ID);
error:
return retval;
}
static void __exit key_eint_exit(void)
{
//__debug("in key_eint_exit\n");
kfifo_free(&key_fifo);
free_irq(IRQ_EINT(20), (void *)EINT_DEVICE_ID);
unregister_chrdev(major, DRIVER_NAME);
device_destroy(key_class,MKDEV(major, minor));
class_destroy(key_class);
return;
}
module_init(key_eint_init);
module_exit(key_eint_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Eric");
应用层:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
char *devname = "/dev/key1_eint";
int fd;
unsigned char key;
fd = open(devname, O_RDWR);
while(1)
{
read(fd, &key, sizeof(key));
//if(key)
printf("the key = %d\n",key);
}
close(fd);
}
上一篇: 读Linux内核kfifo