1、概述

(1)阻塞必要性
(2)内核等待队列
(3)阻塞驱动优化

2、内核等待队列

(1)定义等待队列:wait_queue_head_t my_queue
(2)初始化等待队列:init_waitqueue_head(&my_queue)
(3)定义+初始化等待队列DECLARE_WAIT_QUEUE_HEAD(&my_queue)
(4)进入等待队列,睡眠
  wait_even(queue,condition)
  当condition为真时,立即返回。否则让进程进入TASK_UNINTRRUPTIBLE模式的睡眠中,并挂在queue参数指定的等待队列中。
  wait_even_interruptible(queue, condition)
  当condition为真时,立即返回。否则让进程进入TASK_INTRRUPTIBLE模式的睡眠中,并挂在queue参数指定的等待队列中。
  int wait_even_killable(queue,condition)
  当condition为真时,立即返回。否则让进程进入TASK_KILLABLE模式的睡眠中,并挂在queue参数指定的等待队列中。
(5)从等待队列中唤醒:
  wake_up(wait_queue_t *q)
  从等待队列q中唤醒状态为TASK_UNINTERRUPTIBLE,TASK_INTERRUPTIBLE以及TASK_KILLABLE的所有进程。
  wake_up_interruptible(wait_queue_t *q)
  从等待队列中唤醒状态为TASK_INTERRUPTIBLE的进程

3、编码

#include <linux/module.h>
#include <linux/init.h>
#include <linux/miscdevice.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/fs.h>
#include <asm/uaccess.h>

#define GPFCON 0x56000050
#define GPFDAT 0x56000054
struct work_struct *work1;
struct timer_list buttons_timer;
unsigned int *gpio_data;
unsigned int key_num = 0;
wait_queue_head_t  key_q;//定义等待队列

void work1_func(struct work_struct *work)
{
    mod_timer(&buttons_timer, jiffies + (HZ /10));     
}
void buttons_timer_function(unsigned long data)  
{
    unsigned int key_val;
    
    key_val = readw(gpio_data)&0x1; 
    if (key_val == 0)
       key_num = 4;
    key_val = readw(gpio_data)&0x4;
    if (key_val == 0)
        key_num = 3;  
    wake_up(&key_q);//按键按下之后,有数据了,需要唤醒队列
} 
irqreturn_t key_int(int irq, void *dev_id)
{
    //1. 检测是否发生了按键中断
    //2. 清除已经发生的按键中断
    //3. 提交下半部
    schedule_work(work1);
    return IRQ_HANDLED;
}

void key_hw_init()
{ 
    unsigned int *gpio_config;
    unsigned short data;
    
    gpio_config = ioremap(GPFCON,4);
    data = readw(gpio_config);
    data &= ~0b110011;
    data |= 0b100010;

    writew(data,gpio_config);
    gpio_data = ioremap(GPFDAT,4);
}

int key_open(struct inode *node,struct file *filp)
{
    return 0;
}
ssize_t key_read(struct file *filp, char __user *buf, size_t size, loff_t *pos)
{ 
    wait_event(key_q,key_num);//进入到等待队列,如果条件为真就立即返回
    printk("in kernel :key num is %d\n",key_num);
    copy_to_user(buf, &key_num, 4);
    key_num = 0;//数据读了之后要把它清空,不清空的话下次进来条件就为真了
    return 4;
}
struct file_operations key_fops = 
{
    .open = key_open,
    .read = key_read,
};
struct miscdevice key_miscdev = {
    .minor = 200,
    .name = "key",
    .fops = &key_fops,
};
static int button_init()
{
    int ret;
    ret = misc_register(&key_miscdev);
    if (ret !=0)
        printk("register fail!\n");
    
    //注册中断处理程序
    request_irq(IRQ_EINT0,key_int,IRQF_TRIGGER_FALLING,"key",(void *)4);
    request_irq(IRQ_EINT2,key_int,IRQF_TRIGGER_FALLING,"key",(void *)3);
    
    key_hw_init();//按键初始化
    
    //. 创建工作
    work1 = kmalloc(sizeof(struct work_struct),GFP_KERNEL);
    INIT_WORK(work1, work1_func);
    
    /* 初始化定时器 */
    init_timer(&buttons_timer);
    buttons_timer.function  = buttons_timer_function;  
    add_timer(&buttons_timer);/* 向内核注册一个定时器 */    
    init_waitqueue_head(&key_q);/*初始化等待队列*/
    return 0;
}
static void button_exit()
{
    misc_deregister(&key_miscdev);
}
module_init(button_init);
module_exit(button_exit);

 

用户态:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

int main(int argc, char **argv)
{
    int fd;
    int key_num;
    fd = open("/dev/2440key", 0);
    if (fd<0)
        printf("open fail\n");
    read(fd, &key_num, 4);
    printf("key is %d\n",key_num);
    close(fd);
    return 0;
}