简介
4.19.19
最近正在学习按键的驱动。除了
- 轮询
- 定时
- poll机制
- 这个机制其实和selet函数有关,关于select以前使用过,在IO多路复用或者检测字符设备文件有数据输入的时候。
- select 最终也是调用的poll函数
- 定时机制
- 异步通知的方式
IO多路复用-select - IO多路复用的代码。我们使用slect输入了一串的fd。文件描述符。select会根据这些描述符调用其对应的poll函数。
# include/linux/fs.hstruct file_operations {__poll_t (*poll) (struct file *, struct poll_table_struct *);}typedef struct poll_table_struct {poll_queue_proc _qproc;__poll_t _key;} poll_table;
代码
#include <linux/module.h>#include <linux/poll.h>#include <linux/fs.h>#include <linux/errno.h>#include <linux/miscdevice.h>#include <linux/kernel.h>#include <linux/major.h>#include <linux/mutex.h>#include <linux/proc_fs.h>#include <linux/seq_file.h>#include <linux/stat.h>#include <linux/init.h>#include <linux/device.h>#include <linux/tty.h>#include <linux/kmod.h>#include <linux/gfp.h>#include <linux/gpio/consumer.h>#include <linux/platform_device.h>#include <linux/of_gpio.h>#include <linux/of_irq.h>#include <linux/interrupt.h>#include <linux/irq.h>#include <linux/slab.h>struct gpio_key{int gpio;struct gpio_desc *gpiod;int flag;int irq;} ;static struct gpio_key *gpio_keys_array;/* 主设备号 */static int major = 0;static struct class *gpio_key_class;/* 环形缓冲区 */#define BUF_LEN 128static 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 ssize_t gpio_key_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset){//printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);int err;int key;wait_event_interruptible(gpio_key_wait, !is_key_buf_empty());key = get_key();err = copy_to_user(buf, &key, 4);return 4;}static unsigned int gpio_key_drv_poll(struct file *fp, poll_table * wait){printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);poll_wait(fp, &gpio_key_wait, wait);return is_key_buf_empty() ? 0 : POLLIN | POLLRDNORM;}/* 定义自己的file_operations结构体 */static struct file_operations gpio_key_drv = {.owner = THIS_MODULE,.read = gpio_key_drv_read,.poll = gpio_key_drv_poll,};static irqreturn_t gpio_key_isr(int irq, void *dev_id){struct gpio_key *gpio_key = dev_id;int val;int key;val = gpiod_get_value(gpio_key->gpiod);printk("key %d %d\n", gpio_key->gpio, val);key = (gpio_key->gpio << 8) | val;put_key(key);wake_up_interruptible(&gpio_key_wait);return IRQ_HANDLED;}/* 1. 从platform_device获得GPIO* 2. gpio=>irq* 3. 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 = of_gpio_count(node); # 根据平台数据得到对应的GPIO数量if (!count){printk("%s %s line %d, there isn't any gpio available\n", __FILE__, __FUNCTION__, __LINE__);return -1;}// 分配数据gpio_keys_array = kzalloc(sizeof(struct gpio_key) * count, GFP_KERNEL);for (i = 0; i < count; i++){gpio_keys_array[i].gpio = of_get_gpio_flags(node, i, &flag);if (gpio_keys_array[i].gpio < 0){printk("%s %s line %d, of_get_gpio_flags fail\n", __FILE__, __FUNCTION__, __LINE__);return -1;}# 这个需要去深入分析gpio子系统就能明白了,这里无需多讲gpio_keys_array[i].gpiod = gpio_to_desc(gpio_keys_array[i].gpio);gpio_keys_array[i].flag = flag & OF_GPIO_ACTIVE_LOW;gpio_keys_array[i].irq = gpio_to_irq(gpio_keys_array[i].gpio);}for (i = 0; i < count; i++){# 申请中断err = request_irq(gpio_keys_array[i].irq, gpio_key_isr, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "gpio_key", &gpio_keys_array[i]);}/* 注册file_operations */major = register_chrdev(0, "gpio_key", &gpio_key_drv); /* /dev/gpio_key */// 新创建一个类// 光宇gpio_key_class = class_create(THIS_MODULE, "gpio_key_class");if (IS_ERR(gpio_key_class)) {printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);unregister_chrdev(major, "gpio_key");return PTR_ERR(gpio_key_class);}/*创建设备文件*/device_create(gpio_key_class, NULL, MKDEV(major, 0), NULL, "gpio_key"); /* /dev/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;device_destroy(gpio_key_class, MKDEV(major, 0));class_destroy(gpio_key_class);unregister_chrdev(major, "gpio_key");count = of_gpio_count(node);for (i = 0; i < count; i++){free_irq(gpio_keys_array[i].irq, &gpio_keys_array[i]);}kfree(gpio_keys_array);return 0;}static const struct of_device_id ask100_keys[] = {{ .compatible = "tset,gpio_key" },{ },};/* 1. 定义platform_driver */static struct platform_driver gpio_keys_driver = {.probe = gpio_key_probe,.remove = gpio_key_remove,.driver = {.name = "gpio_key", #.of_match_table = ask100_keys, # 匹配设备树里面的 compatible},};/* 2. 在入口函数注册platform_driver */static int __init gpio_key_init(void){int err;printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);err = platform_driver_register(&gpio_keys_driver); # 注册一个平台设备return err;}/* 3. 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数* 卸载platform_driver*/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");MODULE_AUTHOR("zhouchengzhu <1073355312@qq.com>");MODULE_DESCRIPTION("A Poll test demo");MODULE_VERSION("0.1.0");
- 关于平台设备的匹配可以参考我以前写的文章 内核平台设备的匹配过程.pdf
调用过程
app:pollkernel:sys_polldo_sys_poll# do_sys_pollroot@zhou 00:33:50 ~/1/1/Linux-4.9.88 # grep -nrw "do_sys_poll" --include="*.c"fs/select.c:884:int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,
- 分析do_sys_poll
```c
int do_sys_poll(struct pollfd __user ufds, unsigned int nfds,struct timespec64 end_time)
poll_initwait(&table);
fdcount = do_poll(head, &table, end_time);# 关于 __pollwait 这个 poll_queue_proc 数据以后再分析init_poll_funcptr(&pwq->pt, __pollwait); // pwq->pt->_qproc = __pollwait # 惠帝#
static int do_poll(struct poll_list list, struct poll_wqueues wait, struct timespec64 *end_time) for (;;)
# 执行对应文件的poll函数do_pollfd(pfd, pt, &can_busy_loop,busy_flag)# 超时标志被设置好以后或者count非0,退出if (count || timed_out)break;# 在这里让出控制权。if (!poll_schedule_timeout(wait, TASK_INTERRUPTIBLE, to, slack))timed_out = 1;
<a name="7teKu"></a>### 分析poll_initwait 初始化 __pollwait ,这个属于回调函数,然后 do_poll 调用文件f_op的poll函数。<a name="1WkLO"></a>#### __pollwait 回调函数```cstatic void __pollwait(struct file *filp, wait_queue_head_t *wait_address,poll_table *p)entry->wait_address = wait_address;
关于等待队列的操作,参考我以前写的等待队列深入理解。
小结
poll 函数想要理解还是需要深入理解等待队列。但是和等待队列的使用有一点不同的是,当poll_wait将当前进程加入等待队列以后,并且没有立即释放控制权,
