字符设备实验之按键异步通知

#include <linux/module.h>
#include <linux/kernel.h>


#include <linux/init.h>
#include <linux/fs.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/sched.h>
#include <linux/pm.h>
#include <linux/slab.h>
#include <linux/sysctl.h>
#include <linux/proc_fs.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/input.h>
#include <linux/gpio_keys.h>
#include <linux/workqueue.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/poll.h>




#include <asm/io.h>
#include <asm/irq.h>
#include <asm/uaccess.h>


#include <mach/map.h>
#include <mach/regs-clock.h>
#include <mach/regs-mem.h>
#include <mach/gpio.h>
#include <mach/gpio-smdkc110.h>
#include <mach/regs-gpio.h>


/********************************************************************************************
ioremap(),iounmap()
class_create(),class_destroy()
class_device_create(),class_device_unregister()
********************************************************************************************/
static dev_t devno;  
static struct cdev *pCdev;
static struct class *fifthdrv_class;
static struct device *fifthdrv_class_dev;




#define DEVICE_NAME "fifth_drv"
int major,minor;


volatile unsigned long *gpgcon;
volatile unsigned long *gpgdat;


static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
/* 中断事件标志, 中断服务程序将它置1,fifth_drv_read将它清0 */
static volatile int ev_press = 0;


struct pin_desc{
unsigned int pin;
unsigned int key_val;
};
/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
/* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
static unsigned char key_val;


struct pin_desc pins_desc[4] = {
{S5PV210_GPH0(0), 0x01},
{S5PV210_GPH0(1), 0x02},
{S5PV210_GPH0(2), 0x03},
{S5PV210_GPH0(3), 0x04},
};


/* 异步通知 :
为了使设备支持异步通知机制,驱动程序中涉及以下3项工作
1.支持F_SETOWN命令,能在这个控制命令处理中设置filp->f_owner为对应进程ID,不过此项工作已由内核完成,设备驱动无须处理
2.支持F_SETFL命令的处理,每当FASYNC标志改变时,驱动程序中的fasync()函数得以执行
    驱动中应该实现fasync()函数
3.在设备资源可以获得时,调用kill_fasync()函数激发相应的信号
*/
static struct fasync_struct *button_async;


/*
  * 确定按键值
  */
static irqreturn_t fifth_drv_irq(int irq, void *dev_id)
{
struct pin_desc * pindesc = (struct pin_desc *)dev_id;
unsigned int pinval;

pinval = gpio_get_value(pindesc->pin);


if (pinval)
{
/* 松开 */
key_val = 0x80 | pindesc->key_val;
}
else
{
/* 按下 */
key_val = pindesc->key_val;
}


    ev_press = 1;                  /* 表示中断发生了 */
    wake_up_interruptible(&button_waitq);   /* 唤醒休眠的进程, 在poll中用于唤醒等待的进程 */ 


kill_fasync (&button_async, SIGIO, POLL_IN);//kill_fasync通知应用程序

return IRQ_RETVAL(IRQ_HANDLED);
}


static int fifth_drv_open(struct inode *inode, struct file *file)
{
//printk("do fifth_drv_open \n");

/*
* K1,K2,K3,K4对应GPH0,GPH1,GPH2,GPH3
*/


/* 配置K1,K2,K3,K4为输入引脚 */
request_irq(IRQ_EINT(0),  fifth_drv_irq, IRQ_TYPE_EDGE_BOTH, "S2", &pins_desc[0]);
request_irq(IRQ_EINT(1),  fifth_drv_irq, IRQ_TYPE_EDGE_BOTH, "S3", &pins_desc[1]);
request_irq(IRQ_EINT(2),  fifth_drv_irq, IRQ_TYPE_EDGE_BOTH, "S4", &pins_desc[2]);
request_irq(IRQ_EINT(3),  fifth_drv_irq, IRQ_TYPE_EDGE_BOTH, "S5", &pins_desc[3]);

return 0;
}


static int fifth_drv_release(struct inode *inode, struct file *file)
{
//printk("do fifth_drv_release \n");
free_irq(IRQ_EINT(0), &pins_desc[0]);
free_irq(IRQ_EINT(1), &pins_desc[1]);
free_irq(IRQ_EINT(2), &pins_desc[2]);
free_irq(IRQ_EINT(3), &pins_desc[3]);
return 0;
}


static int fifth_drv_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{
if (count != 1)
return -EINVAL;


/* 如果没有按键动作, 休眠 */
wait_event_interruptible(button_waitq, ev_press);


/* 如果有按键动作, 返回键值 */
copy_to_user(buff, &key_val, 1);
ev_press = 0;


return 1;
}


static ssize_t fifth_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
//printk("do fifth_drv_write \n");
return 0;
}


static unsigned 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 *filp, int on)
{
printk("driver: fifth_drv_fasync, on=%d\n",on);
return fasync_helper (fd, filp, on, &button_async);//用于给字符设备建立异步通知队列
}


static struct file_operations fifth_drv_fops =
{
.owner   = THIS_MODULE, /* 这是一个宏,指向编译模块时自动创建的__this_module 变量*/
.open    = fifth_drv_open,
.release = fifth_drv_release,
.read    = fifth_drv_read,
.write   = fifth_drv_write,
.poll    = fifth_drv_poll,
.fasync =  fifth_drv_fasync,
};


static int __init fifth_drv_init(void)
{
//major = register_chrdev(0, DEVICE_NAME, &fifth_drv_fops);
int ret;
  
//使用udev自动生成设备文件,并分配设备号
pCdev = cdev_alloc();
if (!pCdev) {
printk(KERN_WARNING "cdev_alloc failed\n");
//goto out;
}
    pCdev->owner = THIS_MODULE;  
cdev_init(pCdev, &fifth_drv_fops);

ret = alloc_chrdev_region(&devno, 5, 1, "buttons");  
    if(ret){  
        printk(KERN_ERR "alloc char device region faild!\n");  
        //return ret;  
    }  

major = MAJOR(devno);
minor = MINOR(devno);

    ret = cdev_add(pCdev, devno, 1);  //cat /proc/devices可以查看到多了一个buttons设备
    if(ret){  
        printk(KERN_ERR "add char device faild!\n");  
        //goto add_error;  
    }

#if 1
fifthdrv_class = class_create(THIS_MODULE, "buttonsClass");//ls /sys/class/buttonsClass/可以查看到多了一个buttonsClass类
if(IS_ERR(fifthdrv_class)){  
        printk(KERN_ERR "create class error!\n");         
    } 

//ls /sys/class/buttonsClass/buttons5/可以查看到多了一个buttons5设备
fifthdrv_class_dev = device_create(fifthdrv_class, NULL, devno/*MKDEV(major, 0)*/, NULL, "buttons%d", MINOR(devno)); /* /dev/buttons */
if(IS_ERR(fifthdrv_class_dev)){  
        printk(KERN_ERR "create buttons device error!\n");          
    } 
#endif

printk("fifth_drv_init \n");
printk("major = %d,minor = %d \n", major,minor);


gpgcon = (volatile unsigned long *)ioremap(0xE0200C00, 16);
gpgdat = gpgcon + 1;

return 0;
}


static void __exit fifth_drv_exit(void)
{
//unregister_chrdev(major, DEVICE_NAME);
#if 1
device_destroy(fifthdrv_class,devno);
class_destroy(fifthdrv_class);
#endif


cdev_del(pCdev);
unregister_chrdev_region(devno, 1);  


iounmap(gpgcon);

printk("fifth_drv_exit \n");
}




module_init(fifth_drv_init);
module_exit(fifth_drv_exit);




MODULE_LICENSE("GPL");


测试程序为:



#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
#include <fcntl.h>
#include <signal.h>


int fd;


//异步通知的处理函数
void my_signal_fun(int signum)
{
unsigned char key_val;
read(fd, &key_val, 1);
printf("signum = %d, key_val: 0x%x\n", signum, key_val);
}


int main(int argc, char **argv)
{

int cnt = 0;
unsigned char key_vals;
int ret;

int Oflags;


signal(SIGIO, my_signal_fun);  //使用SIGIO(29)类型的信号量,并注册处理函数

fd = open("/dev/buttons5", O_RDWR);
if (fd < 0)
{
printf("can't open!\n");
return 0;
}

fcntl(fd, F_SETOWN, getpid());//告诉内核,发给谁

Oflags = fcntl(fd, F_GETFL); 

fcntl(fd, F_SETFL, Oflags | FASYNC); //改变fasync标记,最终会调用到驱动的fasync->fasync_helper(初始化/释放fasync_struct)

while (1)
{
sleep(1000);
}
return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值