韦东山驱动视频笔记——4.字符设备驱动程序之异步通知
2013-08-08 11:20
357 查看
linux内核版本:linux-2.6.30.4
目的:我们前面写的几个驱动程序不管是查询方式、中断方式还是poll方式,都要求我们应用程序去主动的查询,看有没有按键发生,要想实现不需要让应用程序主动去查询,而是按键产生后,由驱动主动告诉应用,就要实现异步通知方式。
为了使设备支持异步通知机制,驱动程序中涉及以下3项工作:
1.支持F_SETOWN命令,能在这个控制命令处理中设置file->f_owner为对应进程ID,不过此项工作已由内核完成,设备驱动无须处理
2.支持F_SETFL命令的处理,每当FASYNC标志改变时,驱动程序中的fasync()函数将得以执行,驱动中应该实现fasync()函数
3.在设备资源可获得时,调用kill_fasync()函数激发相应的信号
测试程序:
运行效果:
目的:我们前面写的几个驱动程序不管是查询方式、中断方式还是poll方式,都要求我们应用程序去主动的查询,看有没有按键发生,要想实现不需要让应用程序主动去查询,而是按键产生后,由驱动主动告诉应用,就要实现异步通知方式。
为了使设备支持异步通知机制,驱动程序中涉及以下3项工作:
1.支持F_SETOWN命令,能在这个控制命令处理中设置file->f_owner为对应进程ID,不过此项工作已由内核完成,设备驱动无须处理
2.支持F_SETFL命令的处理,每当FASYNC标志改变时,驱动程序中的fasync()函数将得以执行,驱动中应该实现fasync()函数
3.在设备资源可获得时,调用kill_fasync()函数激发相应的信号
#include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> #include <linux/init.h> #include <linux/delay.h> #include <asm/uaccess.h> #include <asm/irq.h> #include <asm/io.h> #include <linux/device.h> #include <linux/irq.h> #include <linux/interrupt.h> #include <mach/regs-gpio.h> #include <linux/poll.h> static struct class *fifth_drv_class; static struct class_device *fifth_drv_class_dev; static volatile unsigned long *gpfcon; static volatile unsigned long *gpfdat; static int major; struct pin_desc { unsigned int pin; unsigned int key_val; }; static unsigned char key_val; static struct pin_desc pins_desc[4] = { {S3C2410_GPF1, 0x01}, {S3C2410_GPF4, 0x02}, {S3C2410_GPF2, 0x03}, {S3C2410_GPF0, 0x04}, }; static DECLARE_WAIT_QUEUE_HEAD(button_waitq); static volatile int ev_press = 0; static struct fasync_struct *button_async; static irqreturn_t buttons_irq(int irq, void *dev_id) { struct pin_desc *pin = (struct pin_desc *)dev_id; unsigned int pinval; pinval = s3c2410_gpio_getpin(pin->pin); if (pinval) { //松开 key_val = 0x80 | pin->key_val; } else { //按下 key_val = pin->key_val; } wake_up_interruptible(&button_waitq); ev_press = 1; kill_fasync(&button_async, SIGIO, POLL_IN); return IRQ_HANDLED; } static int fifth_drv_open(struct inode *inode, struct file *file) { request_irq(IRQ_EINT1, buttons_irq, IRQ_TYPE_EDGE_BOTH, "S1", &pins_desc[0]); request_irq(IRQ_EINT4, buttons_irq, IRQ_TYPE_EDGE_BOTH, "S2", &pins_desc[1]); request_irq(IRQ_EINT2, buttons_irq, IRQ_TYPE_EDGE_BOTH, "S3", &pins_desc[2]); request_irq(IRQ_EINT0, buttons_irq, IRQ_TYPE_EDGE_BOTH, "S4", &pins_desc[3]); return 0; } static ssize_t fifth_drv_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) { if (count != 1) return -EINVAL; /*如果没有按键动作,休眠*/ wait_event_interruptible(button_waitq, ev_press); /*如果有按键动作发生,返回键值*/ copy_to_user(buf, &key_val, 1); ev_press = 0; return 1; } static int fifth_drv_release(struct inode *inode, struct file *file) { free_irq(IRQ_EINT1, &pins_desc[0]); free_irq(IRQ_EINT4, &pins_desc[1]); free_irq(IRQ_EINT2, &pins_desc[2]); free_irq(IRQ_EINT0, &pins_desc[3]); return 0; } static unsigned int fifth_drv_poll(struct file *file, poll_table *wait) { unsigned int mask = 0; /*这并不会休眠,只是把当前进程挂到队列里*/ poll_wait(file, &button_waitq, wait); if (ev_press) { mask |= (POLLIN | POLLRDNORM); } return mask; } static int fifth_drv_fasync(int fd, struct file *file, int on) { printk("driver:fifth_drv_fasync\n"); return fasync_helper(fd, file, on, &button_async); } static struct file_operations fifth_fops = { .owner = THIS_MODULE, .open = fifth_drv_open, .read = fifth_drv_read, .poll = fifth_drv_poll, .fasync = fifth_drv_fasync, .release = fifth_drv_release, }; int fifth_init() { int ret; major = register_chrdev(0, "fifth_drv", &fifth_fops); fifth_drv_class = class_create(THIS_MODULE, "fifth_drv"); device_create(fifth_drv_class, NULL, MKDEV(major, 0), NULL, "buttons"); gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16); gpfdat = gpfcon + 1; return 0; } static void fifth_exit() { unregister_chrdev(major, "fifth_drv"); device_destroy(fifth_drv_class, MKDEV(major, 0)); class_destroy(fifth_drv_class); iounmap(gpfcon); } module_init(fifth_init); module_exit(fifth_exit); MODULE_LICENSE("GPL");
测试程序:
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <poll.h> #include <signal.h> #include <sys/types.h> #include <unistd.h> #include <fcntl.h> /* fifthdrvtest */ int fd; void my_signal_func(int signum) { unsigned char key_val; read(fd, &key_val, 1); printf("key_val ===0x%x\n", key_val); } int main(int argc, char **argv) { unsigned char key_val; int ret, oflags; signal(SIGIO, my_signal_func); fd = open("/dev/buttons", O_RDWR); if (fd < 0) { printf("can't open!\n"); } fcntl(fd, F_SETOWN, getpid()); oflags = fcntl(fd, F_GETFL); fcntl(fd, F_SETFL, oflags|FASYNC); while (1) { sleep(1000); } return 0; }
运行效果:
相关文章推荐
- 韦东山驱动视频笔记——1.字符设备驱动程序之查询方式的按键驱动程序
- 韦东山驱动视频笔记——2.字符设备驱动程序之中断方式的按键驱动程序
- 韦东山驱动视频笔记——3.字符设备驱动程序之poll机制
- 字符设备驱动学习笔记----异步通知机制
- 字符设备驱动笔记——异步通知(八)
- 字符设备驱动之笔记-异步通知(fasync)
- 07-S3C2440驱动学习(一)嵌入式linux字符设备驱动-按键驱动程序之异步通知机制+原子操作+互斥信号量+阻塞与非阻塞+定时器去抖
- linux字符设备驱动-异步通知笔记
- 字符设备驱动之笔记-异步通知(fasync)
- 字符设备驱动第八课----异步通知(信号驱动IO)
- Linux字符设备驱动之异步通知
- 跟着韦东山老师学字符设备驱动之查询方式的按键驱动程序分析
- 【Linux 驱动】第六章 高级字符驱动程序操作----异步通知
- 韦东山Linux视频驱动第2期》学习总结之第17课(USB设备驱动程序)
- 字符设备驱动--异步通知、同步互斥阻塞
- Linux设备驱动程序学习笔记 高级字符驱动学习--阻塞型I/0
- Linux的异步通知字符设备驱动
- 字符设备驱动程序——异步通知
- Linux字符设备驱动之异步通知
- 韦东山第12.6节-字符驱动之异步通知