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 | |
Mauro Carvalho Chehab | 20d5f11 | 2010-03-25 23:49:46 -0300 | [diff] [blame^] | 42 | /* Used to register nec_decoder clients */ |
| 43 | static LIST_HEAD(decoder_list); |
| 44 | static spinlock_t decoder_lock; |
| 45 | |
| 46 | struct decoder_data { |
| 47 | struct list_head list; |
| 48 | struct ir_input_dev *ir_dev; |
| 49 | int enabled:1; |
| 50 | }; |
| 51 | |
| 52 | |
| 53 | /** |
| 54 | * get_decoder_data() - gets decoder data |
| 55 | * @input_dev: input device |
| 56 | * |
| 57 | * Returns the struct decoder_data that corresponds to a device |
| 58 | */ |
| 59 | |
| 60 | static struct decoder_data *get_decoder_data(struct ir_input_dev *ir_dev) |
| 61 | { |
| 62 | struct decoder_data *data = NULL; |
| 63 | |
| 64 | spin_lock(&decoder_lock); |
| 65 | list_for_each_entry(data, &decoder_list, list) { |
| 66 | if (data->ir_dev == ir_dev) |
| 67 | break; |
| 68 | } |
| 69 | spin_unlock(&decoder_lock); |
| 70 | return data; |
| 71 | } |
| 72 | |
| 73 | static ssize_t store_enabled(struct device *d, |
| 74 | struct device_attribute *mattr, |
| 75 | const char *buf, |
| 76 | size_t len) |
| 77 | { |
| 78 | unsigned long value; |
| 79 | struct ir_input_dev *ir_dev = dev_get_drvdata(d); |
| 80 | struct decoder_data *data = get_decoder_data(ir_dev); |
| 81 | |
| 82 | if (!data) |
| 83 | return -EINVAL; |
| 84 | |
| 85 | if (strict_strtoul(buf, 10, &value) || value > 1) |
| 86 | return -EINVAL; |
| 87 | |
| 88 | data->enabled = value; |
| 89 | |
| 90 | return len; |
| 91 | } |
| 92 | |
| 93 | static ssize_t show_enabled(struct device *d, |
| 94 | struct device_attribute *mattr, char *buf) |
| 95 | { |
| 96 | struct ir_input_dev *ir_dev = dev_get_drvdata(d); |
| 97 | struct decoder_data *data = get_decoder_data(ir_dev); |
| 98 | |
| 99 | if (!data) |
| 100 | return -EINVAL; |
| 101 | |
| 102 | if (data->enabled) |
| 103 | return sprintf(buf, "1\n"); |
| 104 | else |
| 105 | return sprintf(buf, "0\n"); |
| 106 | } |
| 107 | |
| 108 | static DEVICE_ATTR(enabled, S_IRUGO | S_IWUSR, show_enabled, store_enabled); |
| 109 | |
| 110 | static struct attribute *decoder_attributes[] = { |
| 111 | &dev_attr_enabled.attr, |
| 112 | NULL |
| 113 | }; |
| 114 | |
| 115 | static struct attribute_group decoder_attribute_group = { |
| 116 | .name = "nec_decoder", |
| 117 | .attrs = decoder_attributes, |
| 118 | }; |
| 119 | |
| 120 | |
Mauro Carvalho Chehab | 9f15478 | 2010-03-21 13:00:55 -0300 | [diff] [blame] | 121 | /** is_repeat - Check if it is a NEC repeat event |
| 122 | * @input_dev: the struct input_dev descriptor of the device |
| 123 | * @pos: the position of the first event |
| 124 | * @len: the length of the buffer |
| 125 | */ |
| 126 | static int is_repeat(struct ir_raw_event *evs, int len, int pos) |
| 127 | { |
| 128 | if ((evs[pos].delta.tv_nsec < MIN_REPEAT_START_TIME) || |
| 129 | (evs[pos].delta.tv_nsec > MAX_REPEAT_START_TIME)) |
| 130 | return 0; |
| 131 | |
| 132 | if (++pos >= len) |
| 133 | return 0; |
| 134 | |
| 135 | if ((evs[pos].delta.tv_nsec < MIN_REPEAT_TIME) || |
| 136 | (evs[pos].delta.tv_nsec > MAX_REPEAT_TIME)) |
| 137 | return 0; |
| 138 | |
| 139 | return 1; |
| 140 | } |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 141 | |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 142 | /** |
| 143 | * __ir_nec_decode() - Decode one NEC pulsecode |
| 144 | * @input_dev: the struct input_dev descriptor of the device |
| 145 | * @evs: event array with type/duration of pulse/space |
| 146 | * @len: length of the array |
| 147 | * @pos: position to start seeking for a code |
Mauro Carvalho Chehab | 9f15478 | 2010-03-21 13:00:55 -0300 | [diff] [blame] | 148 | * This function returns -EINVAL if no pulse got decoded, |
| 149 | * 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] | 150 | */ |
| 151 | static int __ir_nec_decode(struct input_dev *input_dev, |
| 152 | struct ir_raw_event *evs, |
| 153 | int len, int *pos) |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 154 | { |
Mauro Carvalho Chehab | 9f15478 | 2010-03-21 13:00:55 -0300 | [diff] [blame] | 155 | struct ir_input_dev *ir = input_get_drvdata(input_dev); |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 156 | int count = -1; |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 157 | int ircode = 0, not_code = 0; |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 158 | |
| 159 | /* 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] | 160 | for (; *pos < len; (*pos)++) { |
Mauro Carvalho Chehab | 9f15478 | 2010-03-21 13:00:55 -0300 | [diff] [blame] | 161 | /* Very long delays are considered as start events */ |
| 162 | if (evs[*pos].delta.tv_nsec > MAX_NEC_TIME) |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 163 | break; |
Mauro Carvalho Chehab | 9f15478 | 2010-03-21 13:00:55 -0300 | [diff] [blame] | 164 | if (evs[*pos].type & IR_START_EVENT) |
| 165 | break; |
| 166 | IR_dprintk(1, "%luus: Spurious NEC %s\n", |
| 167 | (evs[*pos].delta.tv_nsec + 500) / 1000, |
| 168 | (evs[*pos].type & IR_SPACE) ? "space" : "pulse"); |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 169 | |
Mauro Carvalho Chehab | 9f15478 | 2010-03-21 13:00:55 -0300 | [diff] [blame] | 170 | } |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 171 | if (*pos >= len) |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 172 | return 0; |
| 173 | |
Mauro Carvalho Chehab | 9f15478 | 2010-03-21 13:00:55 -0300 | [diff] [blame] | 174 | (*pos)++; /* First event doesn't contain data */ |
| 175 | |
| 176 | if (evs[*pos].type != IR_PULSE) |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 177 | goto err; |
| 178 | |
Mauro Carvalho Chehab | 9f15478 | 2010-03-21 13:00:55 -0300 | [diff] [blame] | 179 | /* Check if it is a NEC repeat event */ |
| 180 | if (is_repeat(evs, len, *pos)) { |
| 181 | *pos += 2; |
| 182 | if (ir->keypressed) { |
| 183 | mod_timer(&ir->raw->timer_keyup, |
| 184 | jiffies + msecs_to_jiffies(REPEAT_TIME)); |
| 185 | IR_dprintk(1, "NEC repeat event\n"); |
| 186 | return 1; |
| 187 | } else { |
| 188 | IR_dprintk(1, "missing NEC repeat event\n"); |
| 189 | return 0; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /* First space should have 4.5 ms otherwise is not NEC protocol */ |
| 194 | if ((evs[*pos].delta.tv_nsec < MIN_START_TIME) || |
| 195 | (evs[*pos].delta.tv_nsec > MAX_START_TIME)) |
| 196 | goto err; |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 197 | |
| 198 | count = 0; |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 199 | for ((*pos)++; *pos < len; (*pos)++) { |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 200 | int bit; |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 201 | if ((evs[*pos].delta.tv_nsec > MIN_BIT1_TIME) && |
| 202 | (evs[*pos].delta.tv_nsec < MAX_BIT1_TIME)) |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 203 | bit = 1; |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 204 | else if ((evs[*pos].delta.tv_nsec > MIN_BIT0_TIME) && |
| 205 | (evs[*pos].delta.tv_nsec < MAX_BIT0_TIME)) |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 206 | bit = 0; |
| 207 | else |
| 208 | goto err; |
| 209 | |
| 210 | if (bit) { |
| 211 | int shift = count; |
| 212 | /* Address first, then command */ |
| 213 | if (shift < 8) { |
| 214 | shift += 8; |
| 215 | ircode |= 1 << shift; |
| 216 | } else if (shift < 16) { |
| 217 | not_code |= 1 << shift; |
| 218 | } else if (shift < 24) { |
| 219 | shift -= 16; |
| 220 | ircode |= 1 << shift; |
| 221 | } else { |
| 222 | shift -= 24; |
| 223 | not_code |= 1 << shift; |
| 224 | } |
| 225 | } |
| 226 | if (++count == 32) |
| 227 | break; |
| 228 | } |
Mauro Carvalho Chehab | 995187b | 2010-03-24 20:47:53 -0300 | [diff] [blame] | 229 | (*pos)++; |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 230 | |
| 231 | /* |
| 232 | * Fixme: may need to accept Extended NEC protocol? |
| 233 | */ |
| 234 | if ((ircode & ~not_code) != ircode) { |
| 235 | IR_dprintk(1, "NEC checksum error: code 0x%04x, not-code 0x%04x\n", |
| 236 | ircode, not_code); |
| 237 | return -EINVAL; |
| 238 | } |
| 239 | |
| 240 | IR_dprintk(1, "NEC scancode 0x%04x\n", ircode); |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 241 | ir_keydown(input_dev, ircode); |
Mauro Carvalho Chehab | 9f15478 | 2010-03-21 13:00:55 -0300 | [diff] [blame] | 242 | mod_timer(&ir->raw->timer_keyup, |
| 243 | jiffies + msecs_to_jiffies(REPEAT_TIME)); |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 244 | |
Mauro Carvalho Chehab | 9f15478 | 2010-03-21 13:00:55 -0300 | [diff] [blame] | 245 | return 1; |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 246 | err: |
Mauro Carvalho Chehab | 9f15478 | 2010-03-21 13:00:55 -0300 | [diff] [blame] | 247 | IR_dprintk(1, "NEC decoded failed at bit %d (%s) while decoding %luus time\n", |
| 248 | count, |
| 249 | (evs[*pos].type & IR_SPACE) ? "space" : "pulse", |
| 250 | (evs[*pos].delta.tv_nsec + 500) / 1000); |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 251 | |
| 252 | return -EINVAL; |
| 253 | } |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 254 | |
| 255 | /** |
| 256 | * __ir_nec_decode() - Decodes all NEC pulsecodes on a given array |
| 257 | * @input_dev: the struct input_dev descriptor of the device |
| 258 | * @evs: event array with type/duration of pulse/space |
| 259 | * @len: length of the array |
Mauro Carvalho Chehab | 93c312f | 2010-03-25 21:13:43 -0300 | [diff] [blame] | 260 | * This function returns the number of decoded pulses |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 261 | */ |
Mauro Carvalho Chehab | 995187b | 2010-03-24 20:47:53 -0300 | [diff] [blame] | 262 | static int ir_nec_decode(struct input_dev *input_dev, |
| 263 | struct ir_raw_event *evs, |
| 264 | int len) |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 265 | { |
Mauro Carvalho Chehab | 20d5f11 | 2010-03-25 23:49:46 -0300 | [diff] [blame^] | 266 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
| 267 | struct decoder_data *data; |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 268 | int pos = 0; |
| 269 | int rc = 0; |
| 270 | |
Mauro Carvalho Chehab | 20d5f11 | 2010-03-25 23:49:46 -0300 | [diff] [blame^] | 271 | data = get_decoder_data(ir_dev); |
| 272 | if (!data || !data->enabled) |
| 273 | return 0; |
| 274 | |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 275 | while (pos < len) { |
Mauro Carvalho Chehab | 9f15478 | 2010-03-21 13:00:55 -0300 | [diff] [blame] | 276 | if (__ir_nec_decode(input_dev, evs, len, &pos) > 0) |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 277 | rc++; |
| 278 | } |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 279 | return rc; |
| 280 | } |
| 281 | |
Mauro Carvalho Chehab | 20d5f11 | 2010-03-25 23:49:46 -0300 | [diff] [blame^] | 282 | static int ir_nec_register(struct input_dev *input_dev) |
| 283 | { |
| 284 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
| 285 | struct decoder_data *data; |
| 286 | int rc; |
| 287 | |
| 288 | rc = sysfs_create_group(&ir_dev->dev.kobj, &decoder_attribute_group); |
| 289 | if (rc < 0) |
| 290 | return rc; |
| 291 | |
| 292 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 293 | if (!data) { |
| 294 | sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group); |
| 295 | return -ENOMEM; |
| 296 | } |
| 297 | |
| 298 | data->ir_dev = ir_dev; |
| 299 | data->enabled = 1; |
| 300 | |
| 301 | spin_lock(&decoder_lock); |
| 302 | list_add_tail(&data->list, &decoder_list); |
| 303 | spin_unlock(&decoder_lock); |
| 304 | |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | static int ir_nec_unregister(struct input_dev *input_dev) |
| 309 | { |
| 310 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
| 311 | static struct decoder_data *data; |
| 312 | |
| 313 | data = get_decoder_data(ir_dev); |
| 314 | if (!data) |
| 315 | return 0; |
| 316 | |
| 317 | sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group); |
| 318 | |
| 319 | spin_lock(&decoder_lock); |
| 320 | list_del(&data->list); |
| 321 | spin_unlock(&decoder_lock); |
| 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
Mauro Carvalho Chehab | 995187b | 2010-03-24 20:47:53 -0300 | [diff] [blame] | 326 | static struct ir_raw_handler nec_handler = { |
Mauro Carvalho Chehab | 20d5f11 | 2010-03-25 23:49:46 -0300 | [diff] [blame^] | 327 | .decode = ir_nec_decode, |
| 328 | .raw_register = ir_nec_register, |
| 329 | .raw_unregister = ir_nec_unregister, |
Mauro Carvalho Chehab | 995187b | 2010-03-24 20:47:53 -0300 | [diff] [blame] | 330 | }; |
| 331 | |
| 332 | static int __init ir_nec_decode_init(void) |
| 333 | { |
| 334 | ir_raw_handler_register(&nec_handler); |
| 335 | |
| 336 | printk(KERN_INFO "IR NEC protocol handler initialized\n"); |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | static void __exit ir_nec_decode_exit(void) |
| 341 | { |
| 342 | ir_raw_handler_unregister(&nec_handler); |
| 343 | } |
| 344 | |
| 345 | module_init(ir_nec_decode_init); |
| 346 | module_exit(ir_nec_decode_exit); |
| 347 | |
| 348 | MODULE_LICENSE("GPL"); |
| 349 | MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>"); |
| 350 | MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)"); |
| 351 | MODULE_DESCRIPTION("NEC IR protocol decoder"); |