• “休眠-唤醒”机制:

① APP 调用 read 等函数试图读取数据,比如读取按键;
② APP 进入内核态,也就是调用驱动中的对应函数,发现有数据则复制到用户空间并马上返回;
③ 如果 APP 在内核态,也就是在驱动程序中发现没有数据,则 APP 休眠;
④ 当有数据时,比如当按下按键时,驱动程序的中断服务程序被调用,它会记录数据、唤醒 APP;
⑤ APP 继续运行它的内核态代码,也就是驱动程序中的函数,复制数据到用户空间并马上返回。

  • image.png
  • image.png
  • 所谓休眠就是把自己的状态改为非RUNNING,这样内核的调度器就不会让它运行
  • 唤醒就是把程序的状态改为 RUNNING,这样内核的调度器有合适的时间就会让它运行
  • image.png
  • 图中红线部分都属于 APP1 的“上下文”,或者这样说:红线所涉及的代码,都是 APP1 调用的
  • 在 APP1 的“上下文”,也就是在 APP1 的执行过程中,它是可以休眠的。
  • 在中断的处理过程中,也就是 gpio_key_irq 的执行过程中,它不能休眠:“中断”怎么能休眠?“中断”休眠了,谁来调度其他 APP 啊?
  • 在中断处理函数中,不能休眠,也就不能调用会导致休眠的函数
  • 休眠函数
    • image.png
    • 比较重要的参数就是:

① wq:waitqueue,等待队列
休眠时除了把程序状态改为非 RUNNING 之外,还要把进程/进程放入 wq 中,以后中断服务程序要从 wq 中把它取出来唤醒。
没有 wq 的话,茫茫人海中,中断服务程序去哪里找到你?
② condition
这可以是一个变量,也可以是任何表达式。表示“一直等待,直到 condition 为真”。

  • 唤醒函数
    • image.png
  • 要写出具备休眠-唤醒的驱动程序,wait_event_xxx(wq,event)和wake_up_xxx(wq)两个函数是关键
    • 要休眠的线程,放在 wq 队列里,中断处理函数从 wq 队列里把它取出来唤醒

① 初始化 wq 队列
② 在驱动的 read 函数中,调用 wait_event_interruptible:
③ 在中断服务程序里:
设置 event 为 TRUE,并调用 wake_up_interruptible 唤醒线程。

  • 使用环形缓冲区避免按键数据被丢失 ```c struct gpio_key{ int gpio; struct gpio_desc *gpiod; int flag; int irq; } ;

static struct gpio_key *gpio_keys_100ask;

/ 主设备号 / static int major = 0; static struct class *gpio_key_class;

/ 环形缓冲区 /

define BUF_LEN 128

static int g_keys[BUF_LEN]; static int r, w;

define NEXT_POS(x) ((x+1) % BUF_LEN)

static int is_key_buf_empty(void) { return (r == w); }

static int is_key_buf_full(void) { return (r == NEXT_POS(w)); }

static void put_key(int key) { if (!is_key_buf_full()) { g_keys[w] = key; w = NEXT_POS(w); } }

static int get_key(void) { int key = 0; if (!is_key_buf_empty()) { key = g_keys[r]; r = NEXT_POS(r); } return key; }

static DECLARE_WAIT_QUEUE_HEAD(gpio_key_wait);

/ 实现对应的open/read/write等函数,填入file_operations结构体 / static ssizet gpiokeydrvread (struct file file, char __user buf, sizet size, lofft *offset) { //printk(“%s %s line %d\n”, __FILE, __FUNCTION, __LINE); int err; int key;

  1. wait_event_interruptible(gpio_key_wait, !is_key_buf_empty());
  2. key = get_key();
  3. err = copy_to_user(buf, &key, 4);
  4. return 4;

}

/ 定义自己的file_operations结构体 / static struct file_operations gpio_key_drv = { .owner = THIS_MODULE, .read = gpio_key_drv_read, };

static irqreturn_t gpio_key_isr(int irq, void dev_id) { struct gpio_key gpio_key = dev_id; int val; int key;

  1. val = gpiod_get_value(gpio_key->gpiod);
  2. printk("key %d %d\n", gpio_key->gpio, val);
  3. key = (gpio_key->gpio << 8) | val;
  4. put_key(key);
  5. wake_up_interruptible(&gpio_key_wait);
  6. return IRQ_HANDLED;

}

/* 1. 从platform_device获得GPIO

    1. gpio=>irq
    1. request_irq / static int gpio_key_probe(struct platform_device pdev) { int err; struct device_node *node = pdev->dev.of_node; int count; int i; enum of_gpio_flags flag;

      printk(“%s %s line %d\n”, FILE, FUNCTION, LINE);

      count = ofgpiocount(node); if (!count) { printk(“%s %s line %d, there isn’t any gpio available\n”, FILE, FUNCTION, __LINE); return -1; }

      gpio_keys_100ask = kzalloc(sizeof(struct gpio_key) * count, GFP_KERNEL); for (i = 0; i < count; i++) { gpio_keys_100ask[i].gpio = of_get_gpio_flags(node, i, &flag); if (gpio_keys_100ask[i].gpio < 0) {

      1. printk("%s %s line %d, of_get_gpio_flags fail\n", __FILE__, __FUNCTION__, __LINE__);
      2. return -1;

      } gpio_keys_100ask[i].gpiod = gpio_to_desc(gpio_keys_100ask[i].gpio); gpio_keys_100ask[i].flag = flag & OF_GPIO_ACTIVE_LOW; gpio_keys_100ask[i].irq = gpio_to_irq(gpio_keys_100ask[i].gpio); }

      for (i = 0; i < count; i++) { err = request_irq(gpio_keys_100ask[i].irq, gpio_key_isr, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, “100ask_gpio_key”, &gpio_keys_100ask[i]); }

      / 注册file_operations / major = register_chrdev(0, “100ask_gpio_key”, &gpio_key_drv); / /dev/gpio_key /

      gpiokeyclass = classcreate(THISMODULE, “100askgpiokey_class”); if (IS_ERR(gpio_key_class)) { printk(“%s %s line %d\n”, __FILE, __FUNCTION, __LINE); unregister_chrdev(major, “100ask_gpio_key”); return PTR_ERR(gpio_key_class); }

      device_create(gpio_key_class, NULL, MKDEV(major, 0), NULL, “100ask_gpio_key”); / /dev/100ask_gpio_key /

      return 0;

}

static int gpio_key_remove(struct platform_device pdev) { //int err; struct device_node node = pdev->dev.of_node; int count; int i;

  1. device_destroy(gpio_key_class, MKDEV(major, 0));
  2. class_destroy(gpio_key_class);
  3. unregister_chrdev(major, "100ask_gpio_key");
  4. count = of_gpio_count(node);
  5. for (i = 0; i < count; i++)
  6. {
  7. free_irq(gpio_keys_100ask[i].irq, &gpio_keys_100ask[i]);
  8. }
  9. kfree(gpio_keys_100ask);
  10. return 0;

}

static const struct of_device_id ask100_keys[] = { { .compatible = “100ask,gpio_key” }, { }, };

/ 1. 定义platform_driver / static struct platform_driver gpio_keys_driver = { .probe = gpio_key_probe, .remove = gpio_key_remove, .driver = { .name = “100ask_gpio_key”, .of_match_table = ask100_keys, }, };

/ 2. 在入口函数注册platform_driver / static int __init gpio_key_init(void) { int err;

  1. printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
  2. err = platform_driver_register(&gpio_keys_driver);
  3. return err;

}

/* 3. 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数

  • 卸载platformdriver */ static void exit gpio_key_exit(void) { printk(“%s %s line %d\n”, FILE, FUNCTION, LINE_);

    platform_driver_unregister(&gpio_keys_driver); }

/ 7. 其他完善:提供设备信息,自动创建设备节点 /

module_init(gpio_key_init); module_exit(gpio_key_exit); MODULE_LICENSE(“GPL”); ```