驱动开发 —— 文件IO模型(阻塞/非阻塞)

我们使用中断的目的,就是为了在中断发生时,才去读操作,避免像查询一样一直read,从而占据大量的CPU。

一、阻塞: 当进程在读取外部设备的资源(数据),资源没有准备好,进程就会休眠

  linux应用中,大部分的函数接口都是阻塞 scanf(); read(); write(); accept();

  休眠读取:

程序设计目的:App去读取按键值,如果有按键中断触发(键值有改变)则打印,否则休眠.

驱动开发 —— 文件IO模型(阻塞/非阻塞)

如上框图所示:

在main函数中,进入while(1)死循环之后,执行read操作,

若按键值更新,则读取键值

若未更新,则进入休眠并等待更新,更新后,唤醒进程。

如何设置休眠机制?

1,将当前进程加入到等待队列头中
        add_wait_queue(wait_queue_head_t * q, wait_queue_t * wait)
2,将当前进程状态设置成(可接受中断信号)TASK_INTERRUPTIBLE
        set_current_state(TASK_INTERRUPTIBLE)
3,让出调度--休眠
        schedule(void)更加智能方便的接口,可以实现以上功能:  wait_event_interruptible(wq, condition);

在驱动中如何写阻塞代码:

1,等待队列头
      wait_queue_head_t
    
      init_waitqueue_head(wait_queue_head_t *q);//初始化队列头
    
2,在需要等待(没有数据)的时候,进行休眠
        wait_event_interruptible(wait_queue_head_t wq, condition) // 内部会构建一个等待队列项/节点wait_queue_t
        参数1: 等待队列头
        参数2: 条件,如果是为假,就会等待,如果为真,就不会等待
                可以用一标志位,来表示是否有数据


3,在一个合适的时候(有数据),会将进程唤醒
        wake_up_interruptible(wait_queue_head_t *q)

        用法:
            wake_up_interruptible(&key_dev->wq_head);
            //同时设置标志位
            key_dev->key_state  = 1;

代码示例:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/kdev_t.h>
#include <linux/err.h>
#include <linux/device.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include <linux/wait.h>
#include <linux/sched.h>



#define GPXCON_REG 0X11000C20   //不可以从数据寄存器开始映射,要配置寄存器
#define KEY_ENTER  28

//0、设计一个描述按键的数据的对象
struct key_event{
    int code;    //按键类型:home,esc,enter
    int value;   //表状态,按下,松开
};

//1、设计一个全局对象——— 描述key的信息
struct key_desc{
    unsigned int dev_major;
    int irqno;  //中断号
    struct class  *cls;
    struct device *dev;
    void *reg_base;
    struct key_event event;
    wait_queue_head_t wq_head;
    int key_state;   //表示是否有数据
};

struct key_desc *key_dev;


irqreturn_t key_irq_handler(int irqno, void *devid)
{
    printk("----------%s---------",__FUNCTION__);

    int value;
    //读取按键状态
    value = readl(key_dev->reg_base + 4) & (0x01<<2);
    
    if(value){
        printk("key3 up\n");
        key_dev->event.code  = KEY_ENTER;
        key_dev->event.value = 0;
    }else{
        printk("key3 down\n");
        key_dev->event.code  = KEY_ENTER;
        key_dev->event.value = 1;
    }

    //表示有数据,唤醒等待队列中的等待项
    wake_up_interruptible(&key_dev->wq_head);
    
    //同时设置标志位,表示有数据
    key_dev->key_state = 1;
    
    return IRQ_HANDLED;
}


//获取中断号
int get_irqno_from_node(void)
{
    int irqno;
    //获取设备树中的节点
    struct device_node *np = of_find_node_by_path("/key_int_node");
    if(np){
        printk("find node success\n");
    }else{
        printk("find node failed\n");
    }

    //通过节点去获取中断号
    irqno = irq_of_parse_and_map(np, 0);
    printk("iqrno = %d",key_dev->irqno);

    return irqno;
}

ssize_t key_drv_read (struct file * filp, char __user * buf, size_t count, loff_t * fops)
{
    //printk("----------%s---------",__FUNCTION__);
    int ret;

    //在没有数据时,进行休眠
        //key_state在zalloc初始化空间后,为0,则阻塞
    wait_event_interruptible(key_dev->wq_head, key_dev->key_state);
    
    ret = copy_to_user(buf, &key_dev->event, count);
    if(ret > 0)
    {
        printk("copy_to_user error\n");
        return -EFAULT;
    }

    
    //传递给用户数据后,将数据清除,否则APP每次读都是第一次的数据
    memset(&key_dev->event, 0, sizeof(key_dev->event));
    key_dev->key_state = 0;
    
    return count;
}

ssize_t key_drv_write (struct file *filp, const char __user * buf, size_t count, loff_t * fops)
{
    printk("----------%s---------",__FUNCTION__);
    return 0;
}

int key_drv_open (struct inode * inode, struct file *filp)
{
    printk("----------%s---------",__FUNCTION__);
    return 0;
}

int key_drv_close (struct inode *inode, struct file *filp)
{
    printk("----------%s---------",__FUNCTION__);
    return 0;
}


const struct file_operations key_fops = {
    .open    = key_drv_open,
    .read    = key_drv_read,
    .write   = key_drv_write,
    .release = key_drv_close,

};



static int __init key_drv_init(void)
{
    //演示如何获取到中断号
    int ret;
    
    //1、设定全局设备对象并分配空间
    key_dev = kzalloc(sizeof(struct key_desc), GFP_KERNEL);  //GFP_KERNEL表正常分配内存
                          //kzalloc相比于kmalloc,不仅分配连续空间,还会将内存初始化清零

    //2、动态申请设备号
    key_dev->dev_major = register_chrdev(0, "key_drv", &key_fops);

    //3、创建设备节点文件
    key_dev->cls = class_create(THIS_MODULE, "key_cls");
    key_dev->dev = device_create(key_dev->cls, NULL, MKDEV(key_dev->dev_major, 0), NULL, "key0");

    //4、硬件初始化 -- 地址映射或中断申请    
    
    key_dev->reg_base = ioremap(GPXCON_REG,8);

    key_dev->irqno = get_irqno_from_node();
    
    ret = request_irq(key_dev->irqno, key_irq_handler, IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, 
        "key3_eint10", NULL);
    if(ret != 0)
    {
        printk("request_irq error\n");
        return ret;
    }

    //初始化等待队列头
    init_waitqueue_head(&key_dev->wq_head);  //wait_queue_head_t *q
    
    
    
    return 0;
}

static void __exit key_drv_exit(void)
{
    iounmap(GPXCON_REG);
    free_irq(key_dev->irqno, NULL);  //free_irq与request_irq的最后一个参数一致
    device_destroy(key_dev->cls, MKDEV(key_dev->dev_major, 0));
    class_destroy(key_dev->cls);
    unregister_chrdev(key_dev->dev_major, "key_drv");
    kfree(key_dev);
}



module_init(key_drv_init);
module_exit(key_drv_exit);

MODULE_LICENSE("GPL");

key_drv.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


#define KEY_ENTER  28

//0、设计一个描述按键的数据的对象
struct key_event{
    int code;    //按键类型:home,esc,enter
    int value;   //表状态,按下,松开
};


int main(int argc, char *argv[])
{
    struct key_event event;
    int fd;
    fd = open("/dev/key0", O_RDWR);
    if(fd < 0)
    {
        perror("open");
        exit(1);
    }

    while(1)
    {
        read(fd, &event, sizeof(struct key_event));

        if(event.code == KEY_ENTER)
        {
            if(event.value)
            {
                printf("APP__ key enter down\n");
            }else{

                printf("APP__ key enter up\n");
            }
        }
    }

    close(fd);

    return 0;
}

key_test.c

ROOTFS_DIR = /home/linux/source/rootfs#根文件系统路径

APP_NAME = key_test
MODULE_NAME = key_drv

CROSS_COMPILE = /home/linux/toolchains/gcc-4.6.4/bin/arm-none-linux-gnueabi-
CC = $(CROSS_COMPILE)gcc

ifeq ($(KERNELRELEASE),)

KERNEL_DIR = /home/linux/kernel/linux-3.14-fs4412          #编译过的内核源码的路径
CUR_DIR = $(shell pwd)     #当前路径

all:
    make -C $(KERNEL_DIR) M=$(CUR_DIR) modules  #把当前路径编成modules
    $(CC) $(APP_NAME).c -o $(APP_NAME)
    @#make -C 进入到内核路径
    @#M 指定当前路径(模块位置)

clean:
    make -C $(KERNEL_DIR) M=$(CUR_DIR) clean

install:
    sudo cp -raf *.ko $(APP_NAME) $(ROOTFS_DIR)/drv_module     #把当前的所有.ko文件考到根文件系统的drv_module目录

else

obj-m += $(MODULE_NAME).o    #指定内核要把哪个文件编译成ko

endif

Makefile

测试:

./key_test  &  后台运行,,top查看后台进程

驱动开发 —— 文件IO模型(阻塞/非阻塞)

 在没有按键中断时,进程休眠阻塞

二、非阻塞

在读写的时候,若没有数据,立刻返回,并且返回一个出错码

(设计上,非阻塞方式在linux中用的比较少,因为会比较消耗资源)

驱动开发 —— 文件IO模型(阻塞/非阻塞)

 在while循环中,如果没有数据,非阻塞的方式机会一直在内核与用户空间返回出错码,消耗资源,和没有休眠的方式差不多。

用户空间:
open("/dev/key0", O_RDWR|O_NONBLOCK);
//将fd设置为非阻塞方式,后续的IO操作都是基于非阻塞的fd
------------------------------------
内核空间:
驱动中需要去区分,当前模式是阻塞还是非阻塞
//如果当前是非阻塞模式,并且没有数据,立马返回一个出错码
ssize_t key_drv_read (struct file *filep, char __user *buf, size_t count, loff_t *foops)
{
    if(filp->f_flags & O_NONBLOCK && !key_dev->key_state)
        return -EAGAIN;
}
    //如果为阻塞方式 或者 非阻塞下有数据,都不满足判断,会执行以下代码,兼容非阻塞模式
      ....
    wait_event_interruptible    
      ....