代码拉取完成,页面将自动刷新
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <linux/semaphore.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/of_irq.h>
#include <linux/irq.h>
#include <linux/timer.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <linux/poll.h>
#include <linux/fcntl.h>
#define KEY_CNT 1 /* 设备号个数 */
#define KEY_NAME "asyncnoti" /* 名字 */
/* 定义按键值 */
#define KEY0VALUE 0X01 /* 按键值 */
#define INVAKEY 0XFF /* 无效的按键值 */
struct irq_keydesc {
int keyirq_gpio; /* gpio */
int irqnum; /* 中断号 */
unsigned char value; /* 按键对应的键值 */
char name[10]; /* 名字 */
irqreturn_t (*handler)(int, void *); /* 中断服务函数 */
};
/* keyirq设备结构体 */
struct keyirq_dev{
dev_t devid; /* 设备号 */
struct cdev cdev; /* cdev */
struct class *class; /* 类 */
struct device *device; /* 设备 */
int major; /* 主设备号 */
int minor; /* 次设备号 */
struct device_node *nd; /* 设备节点 */
atomic_t keyvalue; /* 按键值 */
struct timer_list timer; /* 定时器 (延时消抖)*/
struct irq_keydesc irq_keydesc;
atomic_t releasekey; /* 标记是否完成一次完成的按键,包括按下和释放 */
unsigned char curkeynum; /* 当前的按键号 */
wait_queue_head_t r_wait; /* 读等待队列头 */
struct fasync_struct *async_queue; /* 异步相关结构体 */
};
struct keyirq_dev keyirqdev; /* keyirq设备 */
static irqreturn_t key0_handler(int irq, void *dev_id)
{
struct keyirq_dev *dev = (struct keyirq_dev *)dev_id;
dev->timer.data = (volatile long)dev_id;
mod_timer(&dev->timer, jiffies + msecs_to_jiffies(10)); /* 10ms定时 */
return IRQ_RETVAL(IRQ_HANDLED);
}
void timer_function(unsigned long arg)
{
unsigned char value;
struct irq_keydesc *keydesc;
struct keyirq_dev *dev = (struct keyirq_dev *)arg;
keydesc = &dev->irq_keydesc;
value = gpio_get_value(keydesc->keyirq_gpio); /* 读取IO值 */
if(value == 0){ /* 按下按键 */
atomic_set(&dev->keyvalue, keydesc->value);
}
else{ /* 按键松开 */
atomic_set(&dev->keyvalue, 0x80 | keydesc->value);
atomic_set(&dev->releasekey, 1); /* 标记松开按键,即完成一次完整的按键过程 */
}
if(atomic_read(&dev->releasekey)) { /* 一次完整的按键过程 */
if(dev->async_queue)
kill_fasync(&dev->async_queue, SIGIO, POLL_IN); /* 释放SIGIO信号 */
}
#if 0
/* 唤醒进程 */
if (atomic_read(&dev->releasekey))
{ /* 完成一次按键过程 */
/* wake_up(&dev->r_wait); */
wake_up_interruptible(&dev->r_wait);
}
#endif
}
static int keyirqio_init(void)
{
int ret = 0;
keyirqdev.nd = of_find_node_by_path("/key");
if (keyirqdev.nd== NULL) {
return -EINVAL;
}
keyirqdev.irq_keydesc.keyirq_gpio = of_get_named_gpio(keyirqdev.nd ,"key-gpio", 0);
if (keyirqdev.irq_keydesc.keyirq_gpio < 0) {
printk("can't get keyirq0\r\n");
return -EINVAL;
}
printk("keyirq_gpio=%d\r\n",keyirqdev.irq_keydesc.keyirq_gpio);
/* 初始化keyirq所使用的IO */
gpio_request(keyirqdev.irq_keydesc.keyirq_gpio, "keyirq0"); /* 请求IO */
gpio_direction_input(keyirqdev.irq_keydesc.keyirq_gpio); /* 设置为输入 */
keyirqdev.irq_keydesc.irqnum = irq_of_parse_and_map(keyirqdev.nd, 0);
printk("irqnum=%d\r\n",keyirqdev.irq_keydesc.irqnum);
/* 申请中断 */
keyirqdev.irq_keydesc.handler = key0_handler;
keyirqdev.irq_keydesc.value = KEY0VALUE;
ret = request_irq(keyirqdev.irq_keydesc.irqnum, keyirqdev.irq_keydesc.handler,
IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, "KEYIRQ", &keyirqdev);
if(ret < 0){
printk("irq %d request failed!\r\n", keyirqdev.irq_keydesc.irqnum);
return -EFAULT;
}
/* 定时器初始化 */
init_timer(&keyirqdev.timer);
keyirqdev.timer.function = timer_function;
/* 初始化等待队列头 */
init_waitqueue_head(&keyirqdev.r_wait);
return 0;
}
static int keyirq_open(struct inode *inode, struct file *filp)
{
filp->private_data = &keyirqdev; /* 设置私有数据 */
return 0;
}
static ssize_t keyirq_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
{
int ret = 0;
unsigned char keyvalue = 0;
unsigned char releasekey = 0;
struct keyirq_dev *dev = (struct keyirq_dev *)filp->private_data;
// #if 0
// /* 加入等待队列,等待被唤醒,也就是有按键按下 */
// ret = wait_event_interruptible(dev->r_wait,
// atomic_read(&dev->releasekey));
// if (ret) {
// goto wait_error;
// }
// #endif
// DECLARE_WAITQUEUE(wait, current); /* 定义一个等待队列 */
// if (atomic_read(&dev->releasekey) == 0)
// { /* 没有按键按下 */
// add_wait_queue(&dev->r_wait, &wait); /* 添加到等待队列头 */
// __set_current_state(TASK_INTERRUPTIBLE); /* 设置任务状态 */
// schedule(); /* 进行一次任务切换 */
// if (signal_pending(current))
// { /* 判断是否为信号引起的唤醒 */
// ret = -ERESTARTSYS;
// goto wait_error;
// }
// }
// remove_wait_queue(&dev->r_wait, &wait); /* 唤醒以后将等待队列移除 */
if (filp->f_flags & O_NONBLOCK){ /* 非阻塞访问 */
if (atomic_read(&dev->releasekey) == 0) /* 没有按键按下 */
return -EAGAIN;
}else{
/* 阻塞访问 */
/* 加入等待队列,等待被唤醒,也就是有按键按下 */
ret = wait_event_interruptible(dev->r_wait,
atomic_read(&dev->releasekey));
if (ret)
{
goto wait_error;
}
}
keyvalue = atomic_read(&dev->keyvalue);
releasekey = atomic_read(&dev->releasekey);
if (releasekey) { /* 有按键按下 */
if (keyvalue & 0x80) {
keyvalue &= ~0x80;
ret = copy_to_user(buf, &keyvalue, sizeof(keyvalue));
} else {
goto data_error;
}
atomic_set(&dev->releasekey, 0);/* 按下标志清零 */
} else {
goto data_error;
}
return 0;
data_error:
return -EINVAL;
wait_error:
// set_current_state(TASK_RUNNING); /* 设置任务为运行态 */
// remove_wait_queue(&dev->r_wait, &wait); /* 将等待队列移除 */
return ret;
}
unsigned int keyirq_poll(struct file *filp,
struct poll_table_struct *wait)
{
unsigned int mask = 0;
struct keyirq_dev *dev = (struct keyirq_dev *)filp->private_data;
// poll_wait 函数不会引起阻塞,只是
// 将应用程序添加到 poll_table 中, wait就是 poll_table
poll_wait(filp, &dev->r_wait, wait); //轮询
if (atomic_read(&dev->releasekey))
{ /* 按键按下 */
mask = POLLIN | POLLRDNORM; /* 返回 PLLIN */
}
return mask;
// POLLIN 有数据可以读取。
// POLLPRI 有紧急的数据需要读取。
// POLLOUT 可以写数据。
// POLLERR 指定的文件描述符发生错误。
// POLLHUP 指定的文件描述符挂起。
// POLLNVAL 无效的请求。
// POLLRDNORM 等同于 POLLIN
}
static int keyirq_fasync(int fd, struct file *filp, int on)
{
struct keyirq_dev *dev = (struct keyirq_dev *)filp->private_data;
return fasync_helper(fd, filp, on, &dev->async_queue);
}
static int keyirq_release(struct inode *inode, struct file *filp)
{
return keyirq_fasync(-1, filp, 0); /* 删除异步通知 */
}
/* 设备操作函数 */
static struct file_operations keyirq_fops = {
.owner = THIS_MODULE,
.open = keyirq_open,
.read = keyirq_read,
.poll = keyirq_poll,
.release = keyirq_release,
.fasync = keyirq_fasync,
};
static int __init mykeyirq_init(void)
{
int ret = 0;
/* 初始化原子变量 */
atomic_set(&keyirqdev.keyvalue, INVAKEY);
if (ret < 0) {
return ret;
}
/* 注册字符设备驱动 */
/* 1、创建设备号 */
if (keyirqdev.major) { /* 定义了设备号 */
keyirqdev.devid = MKDEV(keyirqdev.major, 0);
register_chrdev_region(keyirqdev.devid, KEY_CNT, KEY_NAME);
} else { /* 没有定义设备号 */
alloc_chrdev_region(&keyirqdev.devid, 0, KEY_CNT, KEY_NAME); /* 申请设备号 */
keyirqdev.major = MAJOR(keyirqdev.devid); /* 获取分配号的主设备号 */
keyirqdev.minor = MINOR(keyirqdev.devid); /* 获取分配号的次设备号 */
}
/* 2、初始化cdev */
keyirqdev.cdev.owner = THIS_MODULE;
cdev_init(&keyirqdev.cdev, &keyirq_fops);
/* 3、添加一个cdev */
cdev_add(&keyirqdev.cdev, keyirqdev.devid, KEY_CNT);
/* 4、创建类 */
keyirqdev.class = class_create(THIS_MODULE, KEY_NAME);
if (IS_ERR(keyirqdev.class)) {
return PTR_ERR(keyirqdev.class);
}
/* 5、创建设备 */
keyirqdev.device = device_create(keyirqdev.class, NULL, keyirqdev.devid, NULL, KEY_NAME);
if (IS_ERR(keyirqdev.device)) {
return PTR_ERR(keyirqdev.device);
}
/* 6、始化按键 */
atomic_set(&keyirqdev.keyvalue, INVAKEY);
atomic_set(&keyirqdev.releasekey, 0);
keyirqio_init();
return 0;
}
static void __exit mykeyirq_exit(void)
{
del_timer_sync(&keyirqdev.timer); /* 删除定时器 */
free_irq(keyirqdev.irq_keydesc.irqnum, &keyirqdev);
/* 注销字符设备驱动 */
cdev_del(&keyirqdev.cdev);/* 删除cdev */
unregister_chrdev_region(keyirqdev.devid, KEY_CNT); /* 注销设备号 */
device_destroy(keyirqdev.class, keyirqdev.devid);
class_destroy(keyirqdev.class);
}
module_init(mykeyirq_init);
module_exit(mykeyirq_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("zhangkewei");
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。