linux按键设备驱动
最近我在写字符设备驱动程序,前两天想自己动手写个按键的驱动程序,但是结果并不是我所期待的,希望您能帮我一下,谢谢啦!
程序源代码如下:
#include <linux/module.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <asm/io.h>
#include <asm/system.h>
#include <asm/uaccess.h>
#include <linux/semaphore.h>
#include <linux/poll.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#define count 1
#define KEY_NUM 4
#define KEY_UP 1
#define MYKEY_DOWN 0
typedef unsigned char key_ret;
static struct kbd_dev
{
unsigned int keystatus[KEY_NUM]; /* 按键状态,但是我还没有使用它 */
struct cdev *cdev;
};
static struct key_info
{
int irq_no; /* 中断号 */
char gpio_port[4]; /* 端口号 */
int key_no; /* 键值 */
};
static struct key_info key_info_tab[4] =
{
{IRQ_EINT0, "GPF1", 1},
{IRQ_EINT1, "GPF2", 2},
{IRQ_EINT2, "GPF3", 3},
{IRQ_EINT4, "GPF4", 4},
};
static int major = 280;
static int minor = 0;
static struct kbd_dev *kbd_devp = NULL;
static struct timer_list key_timer[KEY_NUM]; /* 四个内核定时器,用于消抖 */
volatile unsigned long *gpfcon;
static irqreturn_t key_irq_handler(int irq, void *dev_id)
{
printk("handle with irq : %d,irq_no:%d\n",irq,*(int *)dev_id);
/* 禁止该中断 */
disable_irq_nosync(irq);
printk("disbale_irq_nosync :%d\n",irq);
/* 设置1s后处理定时处理函数 */
key_timer[*(int *)dev_id - 1].expires = jiffies + HZ;
add_timer(&key_timer[*(int *)dev_id - 1]);
return IRQ_HANDLED;
}
static void timer_delay(unsigned long which)
{
printk("the irq_no:%d down\n",which);
/* 使能对应的中断 */
enable_irq(which);
}
static ssize_t kbd_read (struct file *filp, char __user *user, size_t size, loff_t *off)
{
return 0;
}
static ssize_t kbd_write (struct file *filp, const char __user *user, size_t size, loff_t *off)
{
return 0;
}
static int kbd_open(struct inode *inode, struct file *filp)
{
return 0;
}
static int kbd_release (struct inode *inode, struct file *filp)
{
return 0;
}
static struct file_operations kbd_fops =
{
.owner = THIS_MODULE,
.open = kbd_open,
.release = kbd_release,
.read = kbd_read,
.write = kbd_write,