免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1027 | 回复: 0
打印 上一主题 下一主题

一个简单按键驱动for mini2440 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-08-16 11:10 |只看该作者 |倒序浏览
最近移植了linux内核, 制作了文件系统,于是就写了一个简单key驱动检验一下.

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define DEVICE_NAME "mini2440_key"
#define KEY_MAJOR 232
static DECLARE_WAIT_QUEUE_HEAD(key_waitq);
static volatile int ev_press = 0;
static volatile int press_cnt[] = { 0, 0, 0, 0, 0, 0};
struct key_irq_desc {
unsigned int irq;
unsigned long flags;
const char *name;
};
static struct key_irq_desc key_irqs[] = {
{IRQ_EINT8, IRQT_FALLING, "key1"},
{IRQ_EINT11, IRQT_FALLING, "key2" },
{IRQ_EINT13, IRQT_FALLING, "key3"},
{IRQ_EINT14, IRQT_FALLING, "key4"},
{IRQ_EINT15, IRQT_FALLING, "key5"},
{IRQ_EINT19, IRQT_FALLING, "key6"}
};
static irqreturn_t key_interrupt(int irq, void *dev_id)
{
volatile int *press_cnt = (volatile int *)dev_id;

*press_cnt = *press_cnt + 1;
ev_press = 1;
wake_up_interruptible(&key_waitq);

return IRQ_RETVAL(IRQ_HANDLED);
}

static int mini2440_key_open(struct inode *inode, struct file *file)
{
int i;
int err;

for (i = 0; i = 0; i--)
  {
   free_irq(key_irqs.irq, (void *)&press_cnt);
  }
}
return 0;
}
static int mini2440_key_release(struct inode *inode, struct file *file)
{
int i;

for (i = 0; i
static int mini2440_key_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{
unsigned long err;

wait_event_interruptible(key_waitq, ev_press);

ev_press = 0;

err = copy_to_user(buff, (const void *)press_cnt, min(sizeof(press_cnt), count));

memset((void *)press_cnt, 0, sizeof(press_cnt));

return err ? -EFAULT : 0;
}
static struct file_operations mini2440_key_fops = {
.owner = THIS_MODULE,
.open = mini2440_key_open,
.release = mini2440_key_release,
.read = mini2440_key_read,
};
static int __init mini2440_key_init(void)
{
int ret;
ret = register_chrdev(KEY_MAJOR, DEVICE_NAME, &mini2440_key_fops);
if (ret
static int __exit mini2440_key_exit(void)
{
unregister_chrdev(KEY_MAJOR, DEVICE_NAME);
return 0;
}
module_init(mini2440_key_init);
module_exit(mini2440_key_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("more&&high");
insmod mini2440_key.ko
mknod /dev/mini2440_key c 232 0

测试程序
./test_key
/home # ./test_key
key 3 has been pressed 1 times!
/home # ./test_key
key 4 has been pressed 1 times!
/home # ./test_key
key 6 has been pressed 1 times!
/home #

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/94311/showart_2029152.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP