此驱动程序之前的按键驱动程序(中断方式)上加以优化。用到异步通知。对于内核来讲,既然用户想得到的是按键后的状态,那么自然不必时时都要read状态。当它检测到中断发生变主动通知用户,用户再来读。这样,用户空间、内核就可以着手干点其它的事情,而不必忙等按键按下或释放。那么就先从应用程序上面看。
怎么设置相关联到“异步通知”呢?
flag = fcntl(fd, F_GETFL);
fcntl(fd, F_SETFL,
flag|FASYNC);
这样的两句就可以将文件描述符fd与异步通知关联,到时候将会接到相关通知。
怎么知道通知道本程序呢?
fcntl(fd,
F_SETOWN,
getpid());
将其与进程号关联,这样内核就知道要将通知发往本进程。
得到通知后该怎么做呢?
sighandler_t
signal(int signum, sighandler_t
handler);
后面的handler是接到信号后的处理函数,而对于一般的IO操作,signum通常为SIGIO,这点在内核驱动中要保持一致。handler的定义为
typedef
void (*sighandler_t)(int); 在这个函数体内就可以去read数据了。
驱动程序上面又是如何实现的呢?
需要在file_operations结构体里设置.fasync =
xxx_fasync,并定义xxx_fasync函数.
static int xxx_fasync(int fd, struct file *filp,
int on)
当fcntl(fd, F_SETFL,
flag|FASYNC);设置异步通知标志后,就会调用驱动里的xxx_fasync函数,我们需要在驱动程序里先定义一个fasync_struct结构体指针,然后通过xxx_fasync函数再调用int
fasync_helper(int fd, struct file * filp, int on, struct fasync_struct
**fapp)去初始化这个结构体。以便在中断服务程序里发送信号。当产生按键中断时,再不是像poll机制那样唤醒等待队列,而是用kill_fasync(struct
fasync_struct **fp, int sig, int
band)发送信号。应用程序收到信号后会调用自定义的handler做相关处理(本程序是read内核的按键状态)。
详细的驱动程序为:
- #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 <linux/cdev.h>
- #include <linux/interrupt.h>
- #include <linux/device.h>
- #include <linux/sched.h>
- #include <linux/gpio.h>
- #include <linux/poll.h>
- #define FIFTHDEV MKDEV(250, 0)
- struct cdev fifthdrv_cdev;
- static struct class *fifthdrv_class;
- struct button {
- int irq;
- char *name;
- int pin;
- int val;
- };
- //static volatile int pressed = 0;
- static unsigned char key_val;
- struct fasync_struct *button_async;
- //DECLARE_WAIT_QUEUE_HEAD(button_wqh);
- /* 六个按键的相关定义整合到结构体 */
- static struct button buttons[6] = {
- {IRQ_EINT8, "K1", S3C2410_GPG(0), 0x1},
- {IRQ_EINT11, "K2", S3C2410_GPG(3), 0x2},
- {IRQ_EINT13, "K3", S3C2410_GPG(5), 0x3},
- {IRQ_EINT14, "K4", S3C2410_GPG(6), 0x4},
- {IRQ_EINT15, "K5", S3C2410_GPG(7), 0x5},
- {IRQ_EINT19, "K6", S3C2410_GPG(11),0x6},
- };
- /* 中断处理函数 */
- static irqreturn_t fifthdrv_intr(int irq, void *data)
- {
- struct button *buttonp;
- int val;
-
- buttonp = (struct button*)data;
- val = s3c2410_gpio_getpin(buttonp->pin);
-
- if (!val) {/* 按下按键*/
- key_val = buttonp->val;
- } else { /* 释放按键*/
- key_val = buttonp->val | 0x10; //将第4位置1,做标记
- }
- //pressed = 1; //此处改变按下标志,以使队列不继续睡眠
- //wake_up_interruptible(&button_wqh);
- kill_fasync(&button_async, SIGIO, POLL_IN);
-
- return IRQ_RETVAL(IRQ_HANDLED);
- }
- static int fifthdrv_open(struct inode * inode, struct file * file)
- {
- int i=6;
- while(i--){
- request_irq(buttons[i].irq, &fifthdrv_intr, IRQ_TYPE_EDGE_BOTH,
- buttons[i].name, &buttons[i]);
- }
- return 0;
-
- }
- static ssize_t fifthdrv_read(struct file *file, char __user *user, size_t size,loff_t*o)
- {
- int sz = sizeof(key_val) ;
-
- if (sz != size) {
- return -EINVAL;
- }
-
- /* 使用异步通知,此处不必休眠 */
- //wait_event_interruptible(button_wqh, pressed);
-
- copy_to_user(user, &key_val, sz);
- /* 重新清除按下标志 */
- //pressed = 0;
- return sz;
- }
- static int fifthdrv_close(struct inode *inode, struct file *file)
- {
- int i=6;
-
- while(i--) {
- free_irq(buttons[i].irq, &buttons[i]);
- }
- return 0;
- }
- /*
- static unsigned int fifthdrv_poll(struct file *file, poll_table *wait)
- {
- unsigned int res=0;
-
- poll_wait(file, &button_wqh, wait);
- if (pressed) {
- res |= POLLIN | POLLRDNORM;
- }
- return res;
- }
- */
- static int fifthdrv_fasync(int fd, struct file *filp, int on)
- {
- return fasync_helper(fd, filp, on, &button_async);
- }
- static struct file_operations fifthdrv_ops = {
- .owner = THIS_MODULE,
- .open = fifthdrv_open,
- .read = fifthdrv_read,
- //.poll = fifthdrv_poll,
- .release = fifthdrv_close,
- .fasync = fifthdrv_fasync,
- };
- static int fifthdrv_init(void)
- {
- int ret;
- int devt = FIFTHDEV;
-
- ret = register_chrdev_region(devt, 1, "fifthdrv");
- if (ret) {
- printk(KERN_ERR "Unable to register minors for fifthdrv\n");
- goto fail;
- }
- fifthdrv_class = class_create(THIS_MODULE, "fifthdrv_class");
- if (IS_ERR(fifthdrv_class)) {
- printk(KERN_ERR "can't register device class\n");
- return PTR_ERR(fifthdrv_class);
- }
- device_create(fifthdrv_class, NULL, devt, NULL, "buttons");
- cdev_init(&fifthdrv_cdev, &fifthdrv_ops);
- ret = cdev_add(&fifthdrv_cdev, devt, 1);
- if (ret < 0)
- goto fail_cdev;
- return 0;
-
- fail_cdev:
- class_unregister(fifthdrv_class);
- device_destroy(fifthdrv_class, devt);
- cdev_del(&fifthdrv_cdev);
- fail:
- unregister_chrdev_region(devt, 1);
-
- return 0;
- }
- static void fifthdrv_exit(void)
- {
- class_unregister(fifthdrv_class);
- device_destroy(fifthdrv_class, FIFTHDEV);
- cdev_del(&fifthdrv_cdev);
- unregister_chrdev_region(FIFTHDEV, 1);
- }
- module_init(fifthdrv_init);
- module_exit(fifthdrv_exit);
- MODULE_LICENSE("GPL");
应用程序为:
- #include <stdio.h>
- #include <signal.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/types.h>
-
- int fd;
- void signal_f(int signum)
- {
- unsigned char val;
- static unsigned int cnt = 1;
- read(fd, &val, sizeof(val));
- printf("cnt:%d, val:0x%x\n", cnt++, val);
- }
- int main(int argc, char *argv[])
- {
- int flag;
-
- fd = open("/dev/buttons", O_RDWR);
- if (fd < 0)
- {
- printf("can't open!\n");
- }
- signal(SIGIO, signal_f);
- fcntl(fd, F_SETOWN, getpid());
- flag = fcntl(fd, F_GETFL);
- fcntl(fd, F_SETFL, flag|FASYNC);
- while (1)
- {
- sleep(1000);
- }
- return 0;
- }
运行状态:
文章说明:
此博文只为总结分享学习经验,许多内容不能也并未详细描述,如每个函数的参数、返回值,何为内核空间、用户空间,怎么操作寄存器, Makefile的编写,编译应用程序等等,这些知识点都在相关资料中能得到更专业、详细的解答。
阅读(4251) | 评论(0) | 转发(1) |