Hello World
/*按键设备中断触发,poll机制非阻塞,定时器使用方式*/
//keys 按键设备异步通知方式驱动文件 keys_async_drv.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/mutex.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <mach/hardware.h>
#include <mach/regs-gpio.h>
#include <linux/device.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/poll.h>
//使用三星的 GPIO 库 API
#include <plat/gpio-fns.h>
#include <mach/regs-gpio.h>
#define DEVICE_NAME "key_dev"
static struct class *key_irq_class;
static struct device *key_irq_device;
static DECLARE_WAIT_QUEUE_HEAD(key_waitq);
static struct timer_list key_timer;
static volatile int ev_press = 0;
struct pin_desc {
unsigned int pin;
unsigned int key_val;
};
struct pin_desc pin_desc[] = {
{S3C2410_GPF(0), 0x01},
{S3C2410_GPF(2), 0x02},
{S3C2410_GPF(3), 0x03},
{S3C2410_GPF(11), 0x04},
};
struct pin_desc *pindesc = NULL;
static unsigned char key_val = 0;
/*按键中断服务函数*/
static irqreturn_t key_irq_handler(int irq, void *dev_id){
pindesc = (struct pin_desc *)dev_id;
/*启动定时器*/
mod_timer(&key_timer, jiffies + msecs_to_jiffies(100));
return IRQ_RETVAL(IRQ_HANDLED);
}
static int key_irq_open(struct inode *inode, struct file *file){
int ret;
/*配置引脚为中断功能*/
s3c2410_gpio_cfgpin(S3C2410_GPF(0), S3C2410_GPF0_EINT0);
s3c2410_gpio_cfgpin(S3C2410_GPF(2), S3C2410_GPF2_EINT2);
s3c2410_gpio_cfgpin(S3C2410_GPF(3), S3C2410_GPG3_EINT11);
s3c2410_gpio_cfgpin(S3C2410_GPF(11), S3C2410_GPG11_EINT19);
/*请求中断*/
ret = request_irq(IRQ_EINT0, key_irq_handler, IRQ_TYPE_EDGE_BOTH, "key_irq_s2", (void *)&pin_desc[0]);
if(ret < 0){
printk("key_irq_s2 request_irq failed\n");
return ret;
}
ret = request_irq(IRQ_EINT2, key_irq_handler, IRQ_TYPE_EDGE_BOTH, "key_irq_s3", (void *)&pin_desc[1]);
if(ret < 0){
printk("key_irq_s3 request_irq failed\n");
return ret;
}
ret = request_irq(IRQ_EINT11, key_irq_handler, IRQ_TYPE_EDGE_BOTH, "key_irq_s4", (void *)&pin_desc[2]);
if(ret < 0){
printk("key_irq_s4 request_irq failed\n");
return ret;
}
ret = request_irq(IRQ_EINT19, key_irq_handler, IRQ_TYPE_EDGE_BOTH, "key_irq_s5", (void *)&pin_desc[3]);
if(ret < 0){
printk("key_irq_s5 request_irq failed\n");
return ret;
}
return 0;
return 0;
}
static int key_irq_release(struct inode *inode, struct file *file){
free_irq(IRQ_EINT0, (void *)&pin_desc[0]);
free_irq(IRQ_EINT2, (void *)&pin_desc[1]);
free_irq(IRQ_EINT11, (void *)&pin_desc[2]);
free_irq(IRQ_EINT19, (void *)&pin_desc[3]);
return 0;
}
static int key_irq_read(struct file *file, char __user *buf, size_t size, loff_t *offset){
if(size != 1){
return -EINVAL;
}
/*若是没有按键按下,则休眠*/
wait_event_interruptible(key_waitq,ev_press);
/*若有,则返回按键值*/
if(copy_to_user(buf, (const void *)&key_val, 1)){
printk("key_irq_read copy_to_user failed\n");
return -EFAULT;
}
ev_press = 0;
return 1;
}
/*非阻塞方式轮询*/
static unsigned key_irq_poll(struct file *file, poll_table *wait){
unsigned int mask = 0;
poll_wait(file, &key_waitq, wait);
if(ev_press){
mask |= POLLIN | POLLRDNORM;
}
return mask;
}
static struct file_operations key_irq_fops = {
.owner = THIS_MODULE,
.open = key_irq_open,
.release = key_irq_release,
.read = key_irq_read,
.poll = key_irq_poll,
};
/*定时器超时处理函数*/
static void timer_handle(unsigned long data){
unsigned int pinval;
if(!pindesc) return;
pinval = s3c2410_gpio_getpin(pindesc->pin);
if(pinval){
key_val = 0x08 | pindesc->key_val;
}else{
key_val = pindesc->key_val;
}
ev_press = 1;
wake_up_interruptible(&key_waitq);
}
int major;
static int __init key_irq_init(void){
major = register_chrdev(0, DEVICE_NAME, &key_irq_fops);
if(major < 0){
printk("key_irq_init register_chrdev failed\n");
return major;
}
key_irq_class = class_create(THIS_MODULE, "keys_irq_drv");
if(IS_ERR(key_irq_class)){
unregister_chrdev(major, DEVICE_NAME);
printk("key_irq_init class_create failed\n");
return PTR_ERR(key_irq_class);
}
key_irq_device = device_create(key_irq_class, NULL, MKDEV(major, 0), NULL, DEVICE_NAME);
if(IS_ERR(key_irq_device)){
class_destroy(key_irq_class);
unregister_chrdev(major, DEVICE_NAME);
printk("key_irq_init device_create failed\n");
return PTR_ERR(key_irq_device);
}
init_timer(&key_timer);
key_timer.function = timer_handle;
add_timer(&key_timer);
printk("key_irq_init success\n");
return 0;
}
static void __exit key_irq_exit(void){
unregister_chrdev(major, DEVICE_NAME);
device_destroy(key_irq_class, MKDEV(major, 0));
class_destroy(key_irq_class);
del_timer(&key_timer);
printk("key_irq_exit success\n");
}
module_init(key_irq_init);
module_exit(key_irq_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("key irq driver");
标签:return,pin,keys,irq,drv,static,key,async,include
From: https://www.cnblogs.com/zxz-FINE/p/17912396.html