新闻动态

行业新闻企业新闻安博电竞

Linux驱动开发基础__POLL机制 DTgHY6Kb

安博电竞

目录

1 适用场景

2 POLL机制的动开内核代码详解

2.1 sys_poll 函数 

2.2  do_sys_poll 函数 

2.3 do_poll函数

3 poll机制使用流程

4 驱动编程

5 应用编程

6 代码

6.1 gpio_key_drv.c

6.2 button_test.c

6.3 Makefile


可以看 字符设备驱动程序之poll机制 那篇文章中的机制分析以及代码去理解POLL机制,看完那篇文章也就理解POLL机制了,下面的这篇文章也可以不看。

1 适用场景

在前面引入中断时,我们曾经举过一个例子:妈妈怎么知道卧室里小孩醒了? 
poll 方式:妈妈要干很多活,但是发基可以陪小孩睡一会,定个闹钟,要浪费点时间,但是可以继续干活 。妈妈要么是动开被小孩吵醒,要么是被闹钟吵醒。 
使用休眠-唤醒的发基方式等待某个事件发生时,有一个缺点:等待的时间可能很久  。我们可以加上一个超时时间,这时就可以使用 poll 机制 。动开

2 POLL机制的发基内核代码详解

Linux APP 系统调用,基本都可以在它的名字前加上“sys_”前缀,这就是它在内核中对应的函数 。比如系统调用 open 、动开read  、发基write、动开poll,与之对应的发基内核函数为:sys_open 、sys_read、动开sys_write 、发基sys_poll。动开 

对于系统调用 poll 或 select,它们对应的内核函数都是 sys_poll。分析sys_poll,即可理解 poll 机制 。 

2.1 sys_poll 函数 

sys_poll 位于 fs/select.c 文件中,代码如下:

SYSCALL_DEFINE3(poll, struct pollfd __user *, ufds, unsigned int, nfds, int, timeout_msecs) { struct timespec64 end_time, *to = NULL; int ret; if (timeout_msecs >= 0) { to = &end_time; poll_select_set_timeout(to, timeout_msecs / MSEC_PER_SEC, NSEC_PER_MSEC * (timeout_msecs % MSEC_PER_SEC)); } ret = do_sys_poll(ufds, nfds, to); …… 

SYSCALL_DEFINE3 是一个宏,它定义于 include/linux/syscalls.h,展开后就有 sys_poll 函数。 
sys_poll 对超时参数稍作处理后,直接调用 do_sys_poll  。 

2.2  do_sys_poll 函数 

do_sys_poll 位于 fs/select.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); poll_freewait(&table); …… } 

poll_initwait 函数非常简单,它初始化一个 poll_wqueues 变量 table:

poll_initwait init_poll_funcptr(&pwq->pt, __pollwait); pt->qproc = qproc; 

即 table->pt->qproc = __pollwait,__pollwait 将在驱动的 poll 函数里用到。do_poll 函数才是核心,继续看代码。 

2.3 do_poll函数

do_poll 函数位于 fs/select.c 文件中,这是 POLL 机制中最核心的代码,贴图如下:

① 从这里开始,将会导致驱动程序的 poll 函数被第一次调用 。 沿着②③④⑤,你可以看到:驱动程序里的 poll_wait 会调用__pollwait函数把线程放入某个队列 。 
当执行完①之后,在⑥或⑦处,pt->_qproc 被设置为 NULL,所以第二次调用驱动程序的 poll 时,不会再次把线程放入某个队列里。 
⑧ 如果驱动程序的 poll 返回有效值,则 count 非 0,跳出循环; 
⑨ 否则休眠一段时间;当休眠时间到,或是被中断唤醒时,会再次循环、再次调用驱动程序的 poll 。 
回顾 APP 的代码,APP 可以指定“想等待某些事件”,poll 函数返回后,可以知道“发生了哪些事件”: 

 驱动程序里怎么体现呢?在上上一个图中,看②位置处,细说如下: 

 3 poll机制使用流程

妈妈进入房间时,会先看小孩醒没醒,闹钟响之后走出房间之前又会再看小孩醒没醒 。注意:看了 2 次小孩! POLL 机制也是类似的,流程如下:

函数执行流程如上图①~⑧所示,重点从③开始看 。假设一开始无按键数据: 
③APP 调用 poll 之后,进入内核态; 
④导致驱动程序的 drv_poll 被调用: 
注意,drv_poll 要把自己这个线程挂入等待队列 wq 中;假设不放入队列里,那以后发生中断时,中断服务程序去哪里找到你嘛? 
drv_poll 还会判断一下:有没有数据啊?返回这个状态 。 
⑤当前没有数据,则休眠一会; 
⑥过程中,按下了按键,发生了中断:  在中断服务程序里记录了按键值,并且从 wq 中把线程唤醒了 。 
⑦从休眠中被唤醒,继续执行 for 循环,再次调用 drv_poll: drv_poll 返回数据状态 
⑧哦,你有数据,那从内核态返回到应用态吧 
⑨APP 调用 read 函数读数据 
如果一直没有数据,调用流程也是类似的,重点从③开始看,如下: 
③ APP 调用 poll 之后,进入内核态; 
④  导致驱动程序的 drv_poll 被调用: 
注意,drv_poll 要把自己这个线程挂入等待队列 wq 中;假设不放入队列里,那以后发生中断时,中断服务程序去哪里找到你嘛? 
drv_poll 还会判断一下:有没有数据啊?返回这个状态 。 
⑤  假设当前没有数据,则休眠一会; 
⑥  在休眠过程中,一直没有按下了按键,超时时间到:内核把这个线程唤醒; 
⑦  线程从休眠中被唤醒,继续执行 for 循环,再次调用 drv_poll:drv_poll 返回数据状态 
⑧  哦,你还是没有数据,但是超时时间到了,那从内核态返回到应用态吧 
⑨  APP 不能调用 read 函数读数据 
注意几点: 

4 驱动编程

使用 poll 机制时,驱动程序的核心就是提供对应的 drv_poll 函数 。在drv_poll 函数中要做 2 件事: 
把当前线程挂入队列 wq:poll_wait 
a)  APP 调用一次 poll,可能导致 drv_poll 被调用 2 次,但是我们并不需要把当前线程挂入队列 2 次。 
b)  可以使用内核的函数 poll_wait 把线程挂入队列,如果线程已经在队列里了,它就不会再次挂入  。 
返回设备状态: 
APP 调用 poll 函数时,有可能是查询“有没有数据可以读”:POLLIN,也有可能是查询“你有没有空间给我写数据”:POLLOUT。所以 drv_poll 要返回自己的当前状态:(POLLIN | POLLRDNORM) 或 (POLLOUT | POLLWRNORM)。 
a)  POLLRDNORM 等同于 POLLIN,为了兼容某些 APP 把它们一起返回。 
b)  POLLWRNORM 等同于 POLLOUT  ,为了兼容某些 APP 把它们一起返
回 。 
APP 调用 poll 后,很有可能会休眠。对应的,在按键驱动的中断服务程序中,也要有唤醒操作 。 
驱动程序中 poll 的代码如下:

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; } 

5 应用编程

注意:APP 可以调用 poll 或 select 函数,这 2 个函数的作用是一样的。 poll/select 函数可以监测多个文件,可以监测多种事件: 

 在调用 poll 函数时,要指明: 
⚫  你要监测哪一个文件:哪一个 fd 
⚫  你想监测这个文件的哪种事件:是 POLLIN、还是 POLLOUT 
最后,在 poll 函数返回时,要判断状态  。 
应用程序代码如下:

struct pollfd fds[1]; int timeout_ms = 5000; int ret; fds[0].fd = fd; fds[0].events = POLLIN; ret = poll(fds, 1, timeout_ms); if ((ret == 1) && (fds[0].revents & POLLIN)) { read(fd, &val, 4); printf("get button : 0x%x\n", val); } 

6 代码

6.1 gpio_key_drv.c

#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include 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 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 %dn", __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 %dn", __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 %dn", 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);if (!count){printk("%s %s line %d, there isn't any gpio availablen", __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){printk("%s %s line %d, of_get_gpio_flags failn", __FILE__, __FUNCTION__, __LINE__);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 */gpio_key_class = class_create(THIS_MODULE, "100ask_gpio_key_class");if (IS_ERR(gpio_key_class)) {printk("%s %s line %dn", __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;device_destroy(gpio_key_class, MKDEV(major, 0));class_destroy(gpio_key_class);unregister_chrdev(major, "100ask_gpio_key");count = of_gpio_count(node);for (i = 0; i < count; i++){free_irq(gpio_keys_100ask[i].irq, &gpio_keys_100ask[i]);}kfree(gpio_keys_100ask);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;printk("%s %s line %dn", __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 %dn", __FILE__, __FUNCTION__, __LINE__);platform_driver_unregister(&gpio_keys_driver);}/* 7. 其他完善:提供设备信息,自动创建设备节点                                     */module_init(gpio_key_init);module_exit(gpio_key_exit);MODULE_LICENSE("GPL");

6.2 button_test.c

#include #include #include #include #include #include #include /** ./button_test /dev/100ask_button0**/int main(int argc, char **argv){int fd;int val;struct pollfd fds[1];int timeout_ms = 5000;int ret;/* 1. 判断参数 */if (argc != 2) {printf("Usage: %s n", argv[0]);return -1;}/* 2. 打开文件 */fd = open(argv[1], O_RDWR);if (fd == -1){printf("can not open file %sn", argv[1]);return -1;}fds[0].fd = fd;fds[0].events = POLLIN;while (1){/* 3. 读文件 */ret = poll(fds, 1, timeout_ms);if ((ret == 1) && (fds[0].revents & POLLIN)){read(fd, &val, 4);printf("get button : 0x%xn", val);}else{printf("timeoutn");}}close(fd);return 0;}

6.3 Makefile

# 1. 使用不同的开发板内核时, 一定要修改KERN_DIR# 2. KERN_DIR中的内核要事先配置、编译, 为了能编译内核, 要先设置下列环境变量:# 2.1 ARCH,          比如: export ARCH=arm64# 2.2 CROSS_COMPILE, 比如: export CROSS_COMPILE=aarch64-linux-gnu-# 2.3 PATH,          比如: export PATH=$PATH:/home/book/100ask_roc-rk3399-pc/ToolChain-6.3.1/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin # 注意: 不同的开发板不同的编译器上述3个环境变量不一定相同,#       请参考各开发板的高级用户使用手册KERN_DIR =  # 板子所用内核源码的目录all:make -C $(KERN_DIR) M=`pwd` modules $(CROSS_COMPILE)gcc -o button_test button_test.cclean:make -C $(KERN_DIR) M=`pwd` modules cleanrm -rf modules.order  button_test# 参考内核源码drivers/char/ipmi/Makefile# 要想把a.c, b.c编译成ab.ko, 可以这样指定:# ab-y := a.o b.o# obj-m += ab.oobj-m += gpio_key_drv.o
chatgpt免费软件,chatgpt api 免费接口,chatgpt 聊天机器人教程,chatgpt 指令大全,chatgpt app

Linux驱动开发基础__POLL机制

百度CHATGPT的创新应用:探索未知领域

CHATGPT不断在创新应用中探索未知领域。百度充分发挥CHATGPT的生成能力,开拓了更多可能性 ,从自动化文档生成到情感分析,无所不在的CHATGPT正逐渐成为各行各业的助力工具 。