字符设备驱动-异步通知
来源:互联网 发布:java工程师转正申请 编辑:程序博客网 时间:2024/05/21 00:15
我们前面的三种按键操作中,都需要通过应用程序不断地主动通过read()来读驱动程序或者通过poll机制利用返回的信息做出决定。
我们想要当按下按键时利用驱动程序来通知应用程序则需要引入异步通知
异步通知:意思就是,一旦设备就绪,则主动通知应用程序,这样应用程序根本不需要查询设备状态,非常类似于硬件上”中断的概念”
我们先来看一个例子:
Signal.c
#include <stdio.h>#include <signal.h>void czg_signal_handler(int signum){ static int cnt = 0; printf("signal = %d, %d times\n",signum,++cnt);}int main(int argc,char **argv){ signal(SIGUSR1,czg_signal_handler); while(1) { sleep(1000); } return 0;}
编译放到nfs服务器上运行:
arm-linux-gcc -o Signal Signal.c
cp Signal /work/nfs_root/czg
测试: ./Signal &
ps
kill -SIGUSR1 776
//kill -9 776 杀死进程
从上面的代码我们可以总结出异步通知的四个要点:
① 注册信号处理函数
② 谁来发?
③ 发给谁 ?
④ 怎么发?
进而我们引入到驱动中实现:
① 注册信号处理函数:在应用程序中注册
② 谁来发:驱动来发
驱动的faync > fasync_helper:初始化/释放fasync_struct
③ 发给谁: 发给应用程序(应用程序先告诉驱动所调用进程的PID)
④ 怎么发:驱动调用kill_fasyn()
为了使设备支持异步通知机制,驱动程序中涉及以下3项工作:
- Ⅰ. 支持F_SETOWN命令,能在这个控制命令处理中设置filp->f_owner为对应进程ID。
不过此项工作已由内核完成,设备驱动无须处理。 Ⅱ. 支持F_SETFL命令的处理,每当FASYNC标志改变时,驱动程序中的fasync()函数将得以执行。驱动中实现fasync()函数。
Ⅲ. 在设备资源可获得时,调用kill_fasync()函数激发相应的信号
测试:
驱动程序:fifth_drv.c
#include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> #include <linux/init.h> #include <linux/delay.h> #include <linux/irq.h>#include <asm/uaccess.h> #include <asm/irq.h> #include <asm/io.h> #include <asm/arch/regs-gpio.h> #include <asm/hardware.h>#include <linux/poll.h>static struct fasync_struct *button_async;static struct class *fifthdrv_class;static struct class_device *fifthdrv_class_dev;static DECLARE_WAIT_QUEUE_HEAD(button_waitq);/* 中断事件标志, 中断服务程序将它置1,s3c24xx_fifth_read将它清0 */static volatile int ev_press = 0;volatile unsigned long *gpfcon;volatile unsigned long *gpfdat;volatile unsigned long *gpgcon;volatile unsigned long *gpgdat;struct pin_desc{ unsigned int pin; unsigned int key_val;};/* 键值: 按下时,0x01、0x02、0x03 *//* 键值: 松开时,0x81、0x82、0x83 */static unsigned char key_val;struct pin_desc pin_desc[3] = { {S3C2410_GPF0,0X01}, {S3C2410_GPF2,0X02}, {S3C2410_GPG3,0X03},};static irqreturn_t buttons_irq(int irq, void *dev_id){ struct pin_desc *pindesc = (struct pin_desc *)dev_id; unsigned int pinval; pinval = s3c2410_gpio_getpin(pindesc->pin); if(pinval) { /* 松开 */ key_val = 0x80 | (pindesc->key_val); *gpfdat |= ((1<<4) | (1<<5) | (1<<6)); } else { /* 按下 */ key_val = pindesc->key_val; *gpfdat &= ~((1<<4) | (1<<5) | (1<<6)); } ev_press = 1; /* 表示中断发生了 */ wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */ kill_fasync (&button_async, SIGIO, POLL_IN); return IRQ_RETVAL(IRQ_HANDLED);}static int fifth_drv_open(struct inode *inode,struct file *file){ /* 配置GPF0,2、GPG3为中断引脚 */ request_irq(IRQ_EINT0, buttons_irq,IRQT_BOTHEDGE,"s2",&pin_desc[0]); request_irq(IRQ_EINT2, buttons_irq,IRQT_BOTHEDGE,"s3",&pin_desc[1]); request_irq(IRQ_EINT11,buttons_irq,IRQT_BOTHEDGE,"s4",&pin_desc[2]); /* 配置GPF4、5、6为输入引脚 */ *gpfcon &= ~((0x3<<4*2) | (0x3<<5*2) | (0x3<<6*2)); *gpfcon |= ((1<<4*2) | (1<<5*2) | (1<<6*2)); 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; /* 如果无按键动作发生,则进行休眠状态 */ /* 如果ev_press等于0,休眠 */ wait_event_interruptible(button_waitq,ev_press); /* 如果有按键动作发生,则返回按键的值 */ copy_to_user(buf,&key_val,1); ev_press = 0; return 1;}static int fifth_drv_close (struct inode *inode, struct file *file){ free_irq(IRQ_EINT0, &pin_desc[0]); free_irq(IRQ_EINT2, &pin_desc[1]); free_irq(IRQ_EINT11, &pin_desc[2]); return 0;}static unsigned int fifth_drv_poll(struct file *file, struct poll_table_struct *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 *filp, int on){ printk("driver: fifth_drv_fasync\n"); return fasync_helper (fd, filp, on, &button_async);}static struct file_operations fifth_drv_fops = { .owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */ .open = fifth_drv_open, .read = fifth_drv_read, .release = fifth_drv_close, .poll = fifth_drv_poll, .fasync = fifth_drv_fasync,};int major; static int fifth_drv_init(void){ major = register_chrdev(0,"fifth_drv",&fifth_drv_fops); fifthdrv_class = class_create(THIS_MODULE,"fifthdrv"); fifthdrv_class_dev = class_device_create(fifthdrv_class,NULL,MKDEV(major,0),NULL,"buttons"); gpfcon = (volatile unsigned long *)ioremap(0x56000050,16); gpfdat = gpfcon + 1; gpgcon = (volatile unsigned long *)ioremap(0x56000060,16); gpgdat = gpgcon + 1; return 0;}static int fifth_drv_exit(void){ unregister_chrdev(major,"fifth_drv"); class_device_unregister(fifthdrv_class_dev); class_destroy(fifthdrv_class); iounmap(gpfcon); iounmap(gpgcon); return 0;}module_init(fifth_drv_init);module_exit(fifth_drv_exit);MODULE_LICENSE("GPL");
驱动测试程序:fifthdrvtest.c
#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>/* * fifthdrvtest */int fd;void czg_signal_handler(int signum){ unsigned char key_val = 0; read(fd,&key_val,1); printf("key_val: 0x%x\n",key_val);}int main(int argc, char **argv){ int ret; int oflags; signal(SIGIO,czg_signal_handler); 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); // 改变fasync标记, //最终会调用到驱动的faync > fasync_helper:初始化/释放fasync_struct //然后当按键按下时候,在irqreturn_t buttons_irq中断处理中调用kill_fasync while(1) { sleep(1000); } return 0;}
Mafefile
KERN_DIR = /work/system/linux-2.6.22.6all: make -C $(KERN_DIR) M=`pwd` modules clean: make -C $(KERN_DIR) M=`pwd` modules clean rm -rf modules.orderobj-m += fifth_drv.o
测试命令
makearm-linux-gcc -o fifthdrvtest fifthdrvtest.c cp fifthdrvtest fifth_drv.ko /work/nfs_root/czginsmod fifth_drv.ko./fifthdrvtest &ps
阅读全文
1 0
- 字符设备驱动-异步通知
- Linux字符设备驱动之异步通知
- linux字符设备驱动之异步通知
- Linux的异步通知字符设备驱动
- linux字符设备驱动-异步通知笔记
- Linux字符设备驱动之异步通知
- linux字符设备驱动-异步通知
- 字符设备异步通知
- 字符设备驱动之Buttons-异步通知(fasync)
- 字符设备驱动之笔记-异步通知(fasync)
- 字符设备驱动学习笔记----异步通知机制
- 字符设备驱动之笔记-异步通知(fasync)
- 字符设备驱动--异步通知、同步互斥阻塞
- linux设备驱动--异步通知
- linux设备驱动异步通知
- 字符设备驱动第八课----异步通知(信号驱动IO)
- Linux设备驱动编程之异步通知
- 深入浅出 Linux设备驱动异步通知介绍
- HBase知识点
- SPIE模板
- 【滴滴】餐馆
- Sql链接数据库的用法
- 打印Hello World
- 字符设备驱动-异步通知
- 随笔(各种知识点)
- 独家版权模式纷争不断,国外的经验值得借鉴
- 滴滴内推--大数据开发工程师
- C++string类的实现
- LeetCode: Median of Two Sorted Arrays
- 图像滤波及去噪初步
- Java并发包多线程总结
- Spring,为内部方法新起一个事务,此处应有坑。