Hello World
| |
| |
| #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> |
| |
| #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"); |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」