Mauro Carvalho Chehab | 995187b | 2010-03-24 20:47:53 -0300 | [diff] [blame^] | 1 | /* ir-nec-decoder.c - handle NEC IR Pulse/Space protocol |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 2 | * |
| 3 | * Copyright (C) 2010 by Mauro Carvalho Chehab <mchehab@redhat.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation version 2 of the License. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | */ |
| 14 | |
| 15 | #include <media/ir-core.h> |
| 16 | |
Mauro Carvalho Chehab | 9f15478 | 2010-03-21 13:00:55 -0300 | [diff] [blame] | 17 | /* Start time: 4.5 ms + 560 us of the next pulse */ |
| 18 | #define MIN_START_TIME (3900000 + 560000) |
| 19 | #define MAX_START_TIME (5100000 + 560000) |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 20 | |
Mauro Carvalho Chehab | 9f15478 | 2010-03-21 13:00:55 -0300 | [diff] [blame] | 21 | /* Bit 1 time: 2.25ms us */ |
| 22 | #define MIN_BIT1_TIME 2050000 |
| 23 | #define MAX_BIT1_TIME 2450000 |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 24 | |
Mauro Carvalho Chehab | 9f15478 | 2010-03-21 13:00:55 -0300 | [diff] [blame] | 25 | /* Bit 0 time: 1.12ms us */ |
| 26 | #define MIN_BIT0_TIME 920000 |
| 27 | #define MAX_BIT0_TIME 1320000 |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 28 | |
Mauro Carvalho Chehab | 9f15478 | 2010-03-21 13:00:55 -0300 | [diff] [blame] | 29 | /* Total IR code is 110 ms, including the 9 ms for the start pulse */ |
| 30 | #define MAX_NEC_TIME 4000000 |
| 31 | |
| 32 | /* Total IR code is 110 ms, including the 9 ms for the start pulse */ |
| 33 | #define MIN_REPEAT_TIME 99000000 |
| 34 | #define MAX_REPEAT_TIME 112000000 |
| 35 | |
| 36 | /* Repeat time: 2.25ms us */ |
| 37 | #define MIN_REPEAT_START_TIME 2050000 |
| 38 | #define MAX_REPEAT_START_TIME 3000000 |
| 39 | |
| 40 | #define REPEAT_TIME 240 /* ms */ |
| 41 | |
| 42 | /** is_repeat - Check if it is a NEC repeat event |
| 43 | * @input_dev: the struct input_dev descriptor of the device |
| 44 | * @pos: the position of the first event |
| 45 | * @len: the length of the buffer |
| 46 | */ |
| 47 | static int is_repeat(struct ir_raw_event *evs, int len, int pos) |
| 48 | { |
| 49 | if ((evs[pos].delta.tv_nsec < MIN_REPEAT_START_TIME) || |
| 50 | (evs[pos].delta.tv_nsec > MAX_REPEAT_START_TIME)) |
| 51 | return 0; |
| 52 | |
| 53 | if (++pos >= len) |
| 54 | return 0; |
| 55 | |
| 56 | if ((evs[pos].delta.tv_nsec < MIN_REPEAT_TIME) || |
| 57 | (evs[pos].delta.tv_nsec > MAX_REPEAT_TIME)) |
| 58 | return 0; |
| 59 | |
| 60 | return 1; |
| 61 | } |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 62 | |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 63 | /** |
| 64 | * __ir_nec_decode() - Decode one NEC pulsecode |
| 65 | * @input_dev: the struct input_dev descriptor of the device |
| 66 | * @evs: event array with type/duration of pulse/space |
| 67 | * @len: length of the array |
| 68 | * @pos: position to start seeking for a code |
Mauro Carvalho Chehab | 9f15478 | 2010-03-21 13:00:55 -0300 | [diff] [blame] | 69 | * This function returns -EINVAL if no pulse got decoded, |
| 70 | * 0 if buffer is empty and 1 if one keycode were handled. |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 71 | */ |
| 72 | static int __ir_nec_decode(struct input_dev *input_dev, |
| 73 | struct ir_raw_event *evs, |
| 74 | int len, int *pos) |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 75 | { |
Mauro Carvalho Chehab | 9f15478 | 2010-03-21 13:00:55 -0300 | [diff] [blame] | 76 | struct ir_input_dev *ir = input_get_drvdata(input_dev); |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 77 | int count = -1; |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 78 | int ircode = 0, not_code = 0; |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 79 | |
| 80 | /* Be sure that the first event is an start one and is a pulse */ |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 81 | for (; *pos < len; (*pos)++) { |
Mauro Carvalho Chehab | 9f15478 | 2010-03-21 13:00:55 -0300 | [diff] [blame] | 82 | /* Very long delays are considered as start events */ |
| 83 | if (evs[*pos].delta.tv_nsec > MAX_NEC_TIME) |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 84 | break; |
Mauro Carvalho Chehab | 9f15478 | 2010-03-21 13:00:55 -0300 | [diff] [blame] | 85 | if (evs[*pos].type & IR_START_EVENT) |
| 86 | break; |
| 87 | IR_dprintk(1, "%luus: Spurious NEC %s\n", |
| 88 | (evs[*pos].delta.tv_nsec + 500) / 1000, |
| 89 | (evs[*pos].type & IR_SPACE) ? "space" : "pulse"); |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 90 | |
Mauro Carvalho Chehab | 9f15478 | 2010-03-21 13:00:55 -0300 | [diff] [blame] | 91 | } |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 92 | if (*pos >= len) |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 93 | return 0; |
| 94 | |
Mauro Carvalho Chehab | 9f15478 | 2010-03-21 13:00:55 -0300 | [diff] [blame] | 95 | (*pos)++; /* First event doesn't contain data */ |
| 96 | |
| 97 | if (evs[*pos].type != IR_PULSE) |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 98 | goto err; |
| 99 | |
Mauro Carvalho Chehab | 9f15478 | 2010-03-21 13:00:55 -0300 | [diff] [blame] | 100 | /* Check if it is a NEC repeat event */ |
| 101 | if (is_repeat(evs, len, *pos)) { |
| 102 | *pos += 2; |
| 103 | if (ir->keypressed) { |
| 104 | mod_timer(&ir->raw->timer_keyup, |
| 105 | jiffies + msecs_to_jiffies(REPEAT_TIME)); |
| 106 | IR_dprintk(1, "NEC repeat event\n"); |
| 107 | return 1; |
| 108 | } else { |
| 109 | IR_dprintk(1, "missing NEC repeat event\n"); |
| 110 | return 0; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /* First space should have 4.5 ms otherwise is not NEC protocol */ |
| 115 | if ((evs[*pos].delta.tv_nsec < MIN_START_TIME) || |
| 116 | (evs[*pos].delta.tv_nsec > MAX_START_TIME)) |
| 117 | goto err; |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 118 | |
| 119 | count = 0; |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 120 | for ((*pos)++; *pos < len; (*pos)++) { |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 121 | int bit; |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 122 | if ((evs[*pos].delta.tv_nsec > MIN_BIT1_TIME) && |
| 123 | (evs[*pos].delta.tv_nsec < MAX_BIT1_TIME)) |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 124 | bit = 1; |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 125 | else if ((evs[*pos].delta.tv_nsec > MIN_BIT0_TIME) && |
| 126 | (evs[*pos].delta.tv_nsec < MAX_BIT0_TIME)) |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 127 | bit = 0; |
| 128 | else |
| 129 | goto err; |
| 130 | |
| 131 | if (bit) { |
| 132 | int shift = count; |
| 133 | /* Address first, then command */ |
| 134 | if (shift < 8) { |
| 135 | shift += 8; |
| 136 | ircode |= 1 << shift; |
| 137 | } else if (shift < 16) { |
| 138 | not_code |= 1 << shift; |
| 139 | } else if (shift < 24) { |
| 140 | shift -= 16; |
| 141 | ircode |= 1 << shift; |
| 142 | } else { |
| 143 | shift -= 24; |
| 144 | not_code |= 1 << shift; |
| 145 | } |
| 146 | } |
| 147 | if (++count == 32) |
| 148 | break; |
| 149 | } |
Mauro Carvalho Chehab | 995187b | 2010-03-24 20:47:53 -0300 | [diff] [blame^] | 150 | (*pos)++; |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 151 | |
| 152 | /* |
| 153 | * Fixme: may need to accept Extended NEC protocol? |
| 154 | */ |
| 155 | if ((ircode & ~not_code) != ircode) { |
| 156 | IR_dprintk(1, "NEC checksum error: code 0x%04x, not-code 0x%04x\n", |
| 157 | ircode, not_code); |
| 158 | return -EINVAL; |
| 159 | } |
| 160 | |
| 161 | IR_dprintk(1, "NEC scancode 0x%04x\n", ircode); |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 162 | ir_keydown(input_dev, ircode); |
Mauro Carvalho Chehab | 9f15478 | 2010-03-21 13:00:55 -0300 | [diff] [blame] | 163 | mod_timer(&ir->raw->timer_keyup, |
| 164 | jiffies + msecs_to_jiffies(REPEAT_TIME)); |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 165 | |
Mauro Carvalho Chehab | 9f15478 | 2010-03-21 13:00:55 -0300 | [diff] [blame] | 166 | return 1; |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 167 | err: |
Mauro Carvalho Chehab | 9f15478 | 2010-03-21 13:00:55 -0300 | [diff] [blame] | 168 | IR_dprintk(1, "NEC decoded failed at bit %d (%s) while decoding %luus time\n", |
| 169 | count, |
| 170 | (evs[*pos].type & IR_SPACE) ? "space" : "pulse", |
| 171 | (evs[*pos].delta.tv_nsec + 500) / 1000); |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 172 | |
| 173 | return -EINVAL; |
| 174 | } |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 175 | |
| 176 | /** |
| 177 | * __ir_nec_decode() - Decodes all NEC pulsecodes on a given array |
| 178 | * @input_dev: the struct input_dev descriptor of the device |
| 179 | * @evs: event array with type/duration of pulse/space |
| 180 | * @len: length of the array |
| 181 | * This function returns the number of decoded pulses or -EINVAL if no |
| 182 | * pulse got decoded |
| 183 | */ |
Mauro Carvalho Chehab | 995187b | 2010-03-24 20:47:53 -0300 | [diff] [blame^] | 184 | static int ir_nec_decode(struct input_dev *input_dev, |
| 185 | struct ir_raw_event *evs, |
| 186 | int len) |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 187 | { |
| 188 | int pos = 0; |
| 189 | int rc = 0; |
| 190 | |
| 191 | while (pos < len) { |
Mauro Carvalho Chehab | 9f15478 | 2010-03-21 13:00:55 -0300 | [diff] [blame] | 192 | if (__ir_nec_decode(input_dev, evs, len, &pos) > 0) |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 193 | rc++; |
| 194 | } |
| 195 | |
| 196 | if (!rc) |
| 197 | return -EINVAL; |
| 198 | return rc; |
| 199 | } |
| 200 | |
Mauro Carvalho Chehab | 995187b | 2010-03-24 20:47:53 -0300 | [diff] [blame^] | 201 | static struct ir_raw_handler nec_handler = { |
| 202 | .decode = ir_nec_decode, |
| 203 | }; |
| 204 | |
| 205 | static int __init ir_nec_decode_init(void) |
| 206 | { |
| 207 | ir_raw_handler_register(&nec_handler); |
| 208 | |
| 209 | printk(KERN_INFO "IR NEC protocol handler initialized\n"); |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | static void __exit ir_nec_decode_exit(void) |
| 214 | { |
| 215 | ir_raw_handler_unregister(&nec_handler); |
| 216 | } |
| 217 | |
| 218 | module_init(ir_nec_decode_init); |
| 219 | module_exit(ir_nec_decode_exit); |
| 220 | |
| 221 | MODULE_LICENSE("GPL"); |
| 222 | MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>"); |
| 223 | MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)"); |
| 224 | MODULE_DESCRIPTION("NEC IR protocol decoder"); |