1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
| #include <linux/module.h> #include <linux/fs.h> #include <linux/uaccess.h> #include <linux/miscdevice.h> #include <linux/gpio.h> #include <linux/interrupt.h> #include <linux/timer.h> #include <linux/wait.h>
MODULE_LICENSE("Dual BSD/GPL"); MODULE_AUTHOR("Philon | https://ixx.life");
static struct { int gpio; int irq; wait_queue_head_t rwait; struct timer_list timer; u32 pulse; u32 space; size_t count; u32 data; } ir;
#define is_head(p, s) (p > 8900 && p < 9100 && s > 4400 && s < 4600) #define is_repeat(p, s) (p > 8900 && p < 9100 && s > 2150 && s < 2350) #define is_bfalse(p, s) (p > 500 && p < 650 && s > 500 && s < 650) #define is_btrue(p, s) (p > 500 && p < 650 && s > 1500 && s < 1750)
static irqreturn_t ir_rx(int irq, void* dev) { static ktime_t last = 0; u32 duration = (u32)ktime_to_us(ktime_get() - last);
if (!gpio_get_value(ir.gpio)) { ir.space = duration; } else { ir.pulse = duration; goto irq_out; }
if (is_head(ir.pulse, ir.space)) { ir.count = ir.data = 0; } else if (is_repeat(ir.pulse, ir.space)) { ir.count = 32; } else if (is_btrue(ir.pulse, ir.space)) { ir.data |= 1 << ir.count++; } else if (is_bfalse(ir.pulse, ir.space)) { ir.data |= 0 << ir.count++; } else { goto irq_out; }
if (ir.count >= 32) { wake_up(&ir.rwait); }
irq_out: mod_timer(&ir.timer, jiffies + (HZ / 10)); last = ktime_get(); return IRQ_HANDLED; }
static void clear_flag(struct timer_list *timer) { ir.pulse = 0; ir.space = 0; ir.count = 0; ir.data = 0; }
static ssize_t ir_read(struct file *filp, char __user *buf, size_t len, loff_t *off) { int rc = 0;
if ((filp->f_flags & O_NONBLOCK) && ir.count < 32) { return -EAGAIN; } else { DECLARE_WAITQUEUE(wq, current); add_wait_queue(&ir.rwait, &wq); wait_event(ir.rwait, ir.count == 32); remove_wait_queue(&ir.rwait, &wq); }
rc = copy_to_user(buf, &ir.data, sizeof(u32)); if (rc < 0) { return rc; }
ir.count = 0; *off += sizeof(u32); return sizeof(u32); }
static const struct file_operations fops = { .owner = THIS_MODULE, .read = ir_read, };
static struct miscdevice irdev = { .minor = MISC_DYNAMIC_MINOR, .name = "IR1838-NEC", .fops = &fops, .nodename = "ir0", .mode = 0744, };
static int __init ir_init(void) { int rc = 0;
init_waitqueue_head(&ir.rwait);
timer_setup(&ir.timer, clear_flag, 0); add_timer(&ir.timer);
ir.gpio = 18; if ((rc = gpio_request_one(ir.gpio, GPIOF_IN, "IR")) < 0) { printk(KERN_ERR "ERROR%d: can not request gpio%d\n", rc, ir.gpio); return rc; }
ir.irq = gpio_to_irq(ir.gpio); if ((rc = request_irq(ir.irq, ir_rx, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "IR", NULL)) < 0) { printk(KERN_ERR "ERROR%d: can not request irq\n", ir.irq); return rc; }
if ((rc = misc_register(&irdev)) < 0) { return rc; }
return 0; } module_init(ir_init);
static void __exit ir_exit(void) { misc_deregister(&irdev); free_irq(ir.irq, NULL); gpio_free(ir.gpio); del_timer(&ir.timer); } module_exit(ir_exit);
|