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 | |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 15 | #include <linux/bitrev.h> |
Mauro Carvalho Chehab | 3f113e3 | 2010-04-08 15:10:27 -0300 | [diff] [blame] | 16 | #include "ir-core-priv.h" |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 17 | |
Mauro Carvalho Chehab | 67780d6 | 2010-04-03 20:33:00 -0300 | [diff] [blame] | 18 | #define NEC_NBITS 32 |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 19 | #define NEC_UNIT 562500 /* ns */ |
| 20 | #define NEC_HEADER_PULSE PULSE(16) |
David Härdeman | 25bb10c | 2010-04-08 20:04:35 -0300 | [diff] [blame] | 21 | #define NECX_HEADER_PULSE PULSE(8) /* Less common NEC variant */ |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 22 | #define NEC_HEADER_SPACE SPACE(8) |
| 23 | #define NEC_REPEAT_SPACE SPACE(4) |
| 24 | #define NEC_BIT_PULSE PULSE(1) |
| 25 | #define NEC_BIT_0_SPACE SPACE(1) |
| 26 | #define NEC_BIT_1_SPACE SPACE(3) |
Mauro Carvalho Chehab | 9f15478 | 2010-03-21 13:00:55 -0300 | [diff] [blame] | 27 | |
Mauro Carvalho Chehab | 20d5f11 | 2010-03-25 23:49:46 -0300 | [diff] [blame] | 28 | /* Used to register nec_decoder clients */ |
| 29 | static LIST_HEAD(decoder_list); |
Mauro Carvalho Chehab | 6eb9435 | 2010-04-07 16:19:36 -0300 | [diff] [blame] | 30 | static DEFINE_SPINLOCK(decoder_lock); |
Mauro Carvalho Chehab | 20d5f11 | 2010-03-25 23:49:46 -0300 | [diff] [blame] | 31 | |
Mauro Carvalho Chehab | 2f16f63 | 2010-04-03 18:51:50 -0300 | [diff] [blame] | 32 | enum nec_state { |
| 33 | STATE_INACTIVE, |
Mauro Carvalho Chehab | 2f16f63 | 2010-04-03 18:51:50 -0300 | [diff] [blame] | 34 | STATE_HEADER_SPACE, |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 35 | STATE_BIT_PULSE, |
| 36 | STATE_BIT_SPACE, |
| 37 | STATE_TRAILER_PULSE, |
Mauro Carvalho Chehab | 2f16f63 | 2010-04-03 18:51:50 -0300 | [diff] [blame] | 38 | STATE_TRAILER_SPACE, |
| 39 | }; |
| 40 | |
Mauro Carvalho Chehab | 20d5f11 | 2010-03-25 23:49:46 -0300 | [diff] [blame] | 41 | struct decoder_data { |
| 42 | struct list_head list; |
| 43 | struct ir_input_dev *ir_dev; |
| 44 | int enabled:1; |
Mauro Carvalho Chehab | 2f16f63 | 2010-04-03 18:51:50 -0300 | [diff] [blame] | 45 | |
| 46 | /* State machine control */ |
| 47 | enum nec_state state; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 48 | u32 nec_bits; |
Mauro Carvalho Chehab | 2f16f63 | 2010-04-03 18:51:50 -0300 | [diff] [blame] | 49 | unsigned count; |
Mauro Carvalho Chehab | 20d5f11 | 2010-03-25 23:49:46 -0300 | [diff] [blame] | 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 | */ |
Mauro Carvalho Chehab | 20d5f11 | 2010-03-25 23:49:46 -0300 | [diff] [blame] | 59 | static struct decoder_data *get_decoder_data(struct ir_input_dev *ir_dev) |
| 60 | { |
| 61 | struct decoder_data *data = NULL; |
| 62 | |
| 63 | spin_lock(&decoder_lock); |
| 64 | list_for_each_entry(data, &decoder_list, list) { |
| 65 | if (data->ir_dev == ir_dev) |
| 66 | break; |
| 67 | } |
| 68 | spin_unlock(&decoder_lock); |
| 69 | return data; |
| 70 | } |
| 71 | |
| 72 | static ssize_t store_enabled(struct device *d, |
| 73 | struct device_attribute *mattr, |
| 74 | const char *buf, |
| 75 | size_t len) |
| 76 | { |
| 77 | unsigned long value; |
| 78 | struct ir_input_dev *ir_dev = dev_get_drvdata(d); |
| 79 | struct decoder_data *data = get_decoder_data(ir_dev); |
| 80 | |
| 81 | if (!data) |
| 82 | return -EINVAL; |
| 83 | |
| 84 | if (strict_strtoul(buf, 10, &value) || value > 1) |
| 85 | return -EINVAL; |
| 86 | |
| 87 | data->enabled = value; |
| 88 | |
| 89 | return len; |
| 90 | } |
| 91 | |
| 92 | static ssize_t show_enabled(struct device *d, |
| 93 | struct device_attribute *mattr, char *buf) |
| 94 | { |
| 95 | struct ir_input_dev *ir_dev = dev_get_drvdata(d); |
| 96 | struct decoder_data *data = get_decoder_data(ir_dev); |
| 97 | |
| 98 | if (!data) |
| 99 | return -EINVAL; |
| 100 | |
| 101 | if (data->enabled) |
| 102 | return sprintf(buf, "1\n"); |
| 103 | else |
| 104 | return sprintf(buf, "0\n"); |
| 105 | } |
| 106 | |
| 107 | static DEVICE_ATTR(enabled, S_IRUGO | S_IWUSR, show_enabled, store_enabled); |
| 108 | |
| 109 | static struct attribute *decoder_attributes[] = { |
| 110 | &dev_attr_enabled.attr, |
| 111 | NULL |
| 112 | }; |
| 113 | |
| 114 | static struct attribute_group decoder_attribute_group = { |
| 115 | .name = "nec_decoder", |
| 116 | .attrs = decoder_attributes, |
| 117 | }; |
| 118 | |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 119 | /** |
Mauro Carvalho Chehab | 587835a | 2010-04-04 10:44:51 -0300 | [diff] [blame] | 120 | * ir_nec_decode() - Decode one NEC pulse or space |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 121 | * @input_dev: the struct input_dev descriptor of the device |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 122 | * @duration: duration in ns of pulse/space |
Mauro Carvalho Chehab | 2f16f63 | 2010-04-03 18:51:50 -0300 | [diff] [blame] | 123 | * |
| 124 | * This function returns -EINVAL if the pulse violates the state machine |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 125 | */ |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 126 | static int ir_nec_decode(struct input_dev *input_dev, s64 duration) |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 127 | { |
Mauro Carvalho Chehab | 2f16f63 | 2010-04-03 18:51:50 -0300 | [diff] [blame] | 128 | struct decoder_data *data; |
| 129 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 130 | int u; |
| 131 | u32 scancode; |
| 132 | u8 address, not_address, command, not_command; |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 133 | |
Mauro Carvalho Chehab | 2f16f63 | 2010-04-03 18:51:50 -0300 | [diff] [blame] | 134 | data = get_decoder_data(ir_dev); |
| 135 | if (!data) |
| 136 | return -EINVAL; |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 137 | |
Mauro Carvalho Chehab | 7f20d32 | 2010-04-04 14:45:04 -0300 | [diff] [blame] | 138 | if (!data->enabled) |
| 139 | return 0; |
| 140 | |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 141 | if (IS_RESET(duration)) { |
Mauro Carvalho Chehab | 2f16f63 | 2010-04-03 18:51:50 -0300 | [diff] [blame] | 142 | data->state = STATE_INACTIVE; |
| 143 | return 0; |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 144 | } |
| 145 | |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 146 | u = TO_UNITS(duration, NEC_UNIT); |
| 147 | if (DURATION(u) == 0) |
| 148 | goto out; |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 149 | |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 150 | IR_dprintk(2, "NEC decode started at state %d (%i units, %ius)\n", |
| 151 | data->state, u, TO_US(duration)); |
| 152 | |
| 153 | switch (data->state) { |
| 154 | |
| 155 | case STATE_INACTIVE: |
David Härdeman | 25bb10c | 2010-04-08 20:04:35 -0300 | [diff] [blame] | 156 | if (u == NEC_HEADER_PULSE || u == NECX_HEADER_PULSE) { |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 157 | data->count = 0; |
| 158 | data->state = STATE_HEADER_SPACE; |
| 159 | } |
| 160 | return 0; |
| 161 | |
| 162 | case STATE_HEADER_SPACE: |
| 163 | if (u == NEC_HEADER_SPACE) { |
| 164 | data->state = STATE_BIT_PULSE; |
| 165 | return 0; |
| 166 | } else if (u == NEC_REPEAT_SPACE) { |
| 167 | ir_repeat(input_dev); |
| 168 | IR_dprintk(1, "Repeat last key\n"); |
| 169 | data->state = STATE_TRAILER_PULSE; |
| 170 | return 0; |
| 171 | } |
| 172 | break; |
| 173 | |
| 174 | case STATE_BIT_PULSE: |
| 175 | if (u == NEC_BIT_PULSE) { |
| 176 | data->state = STATE_BIT_SPACE; |
| 177 | return 0; |
| 178 | } |
| 179 | break; |
| 180 | |
| 181 | case STATE_BIT_SPACE: |
| 182 | if (u != NEC_BIT_0_SPACE && u != NEC_BIT_1_SPACE) |
| 183 | break; |
| 184 | |
| 185 | data->nec_bits <<= 1; |
| 186 | if (u == NEC_BIT_1_SPACE) |
| 187 | data->nec_bits |= 1; |
| 188 | data->count++; |
| 189 | |
| 190 | if (data->count != NEC_NBITS) { |
| 191 | data->state = STATE_BIT_PULSE; |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | address = bitrev8((data->nec_bits >> 24) & 0xff); |
| 196 | not_address = bitrev8((data->nec_bits >> 16) & 0xff); |
| 197 | command = bitrev8((data->nec_bits >> 8) & 0xff); |
| 198 | not_command = bitrev8((data->nec_bits >> 0) & 0xff); |
| 199 | |
| 200 | if ((command ^ not_command) != 0xff) { |
| 201 | IR_dprintk(1, "NEC checksum error: received 0x%08x\n", |
| 202 | data->nec_bits); |
| 203 | break; |
| 204 | } |
| 205 | |
| 206 | if ((address ^ not_address) != 0xff) { |
| 207 | /* Extended NEC */ |
| 208 | scancode = address << 16 | |
| 209 | not_address << 8 | |
| 210 | command; |
| 211 | IR_dprintk(1, "NEC (Ext) scancode 0x%06x\n", scancode); |
| 212 | } else { |
| 213 | /* normal NEC */ |
| 214 | scancode = address << 8 | command; |
| 215 | IR_dprintk(1, "NEC scancode 0x%04x\n", scancode); |
| 216 | } |
| 217 | |
| 218 | ir_keydown(input_dev, scancode, 0); |
| 219 | data->state = STATE_TRAILER_PULSE; |
| 220 | return 0; |
| 221 | |
| 222 | case STATE_TRAILER_PULSE: |
| 223 | if (u > 0) { |
| 224 | data->state = STATE_TRAILER_SPACE; |
| 225 | return 0; |
| 226 | } |
| 227 | break; |
| 228 | |
| 229 | case STATE_TRAILER_SPACE: |
| 230 | if (u < 0) { |
| 231 | data->state = STATE_INACTIVE; |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | break; |
| 236 | } |
| 237 | |
| 238 | out: |
| 239 | IR_dprintk(1, "NEC decode failed at state %d (%i units, %ius)\n", |
| 240 | data->state, u, TO_US(duration)); |
Mauro Carvalho Chehab | 2f16f63 | 2010-04-03 18:51:50 -0300 | [diff] [blame] | 241 | data->state = STATE_INACTIVE; |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 242 | return -EINVAL; |
| 243 | } |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 244 | |
Mauro Carvalho Chehab | 20d5f11 | 2010-03-25 23:49:46 -0300 | [diff] [blame] | 245 | static int ir_nec_register(struct input_dev *input_dev) |
| 246 | { |
| 247 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
| 248 | struct decoder_data *data; |
| 249 | int rc; |
| 250 | |
| 251 | rc = sysfs_create_group(&ir_dev->dev.kobj, &decoder_attribute_group); |
| 252 | if (rc < 0) |
| 253 | return rc; |
| 254 | |
| 255 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 256 | if (!data) { |
| 257 | sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group); |
| 258 | return -ENOMEM; |
| 259 | } |
| 260 | |
| 261 | data->ir_dev = ir_dev; |
| 262 | data->enabled = 1; |
| 263 | |
| 264 | spin_lock(&decoder_lock); |
| 265 | list_add_tail(&data->list, &decoder_list); |
| 266 | spin_unlock(&decoder_lock); |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | static int ir_nec_unregister(struct input_dev *input_dev) |
| 272 | { |
| 273 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
| 274 | static struct decoder_data *data; |
| 275 | |
| 276 | data = get_decoder_data(ir_dev); |
| 277 | if (!data) |
| 278 | return 0; |
| 279 | |
| 280 | sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group); |
| 281 | |
| 282 | spin_lock(&decoder_lock); |
| 283 | list_del(&data->list); |
| 284 | spin_unlock(&decoder_lock); |
| 285 | |
| 286 | return 0; |
| 287 | } |
| 288 | |
Mauro Carvalho Chehab | 995187b | 2010-03-24 20:47:53 -0300 | [diff] [blame] | 289 | static struct ir_raw_handler nec_handler = { |
Mauro Carvalho Chehab | 20d5f11 | 2010-03-25 23:49:46 -0300 | [diff] [blame] | 290 | .decode = ir_nec_decode, |
| 291 | .raw_register = ir_nec_register, |
| 292 | .raw_unregister = ir_nec_unregister, |
Mauro Carvalho Chehab | 995187b | 2010-03-24 20:47:53 -0300 | [diff] [blame] | 293 | }; |
| 294 | |
| 295 | static int __init ir_nec_decode_init(void) |
| 296 | { |
| 297 | ir_raw_handler_register(&nec_handler); |
| 298 | |
| 299 | printk(KERN_INFO "IR NEC protocol handler initialized\n"); |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | static void __exit ir_nec_decode_exit(void) |
| 304 | { |
| 305 | ir_raw_handler_unregister(&nec_handler); |
| 306 | } |
| 307 | |
| 308 | module_init(ir_nec_decode_init); |
| 309 | module_exit(ir_nec_decode_exit); |
| 310 | |
| 311 | MODULE_LICENSE("GPL"); |
| 312 | MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>"); |
| 313 | MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)"); |
| 314 | MODULE_DESCRIPTION("NEC IR protocol decoder"); |