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 */ |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 20 | #define NEC_HEADER_PULSE (16 * NEC_UNIT) |
| 21 | #define NECX_HEADER_PULSE (8 * NEC_UNIT) /* Less common NEC variant */ |
| 22 | #define NEC_HEADER_SPACE (8 * NEC_UNIT) |
| 23 | #define NEC_REPEAT_SPACE (8 * NEC_UNIT) |
| 24 | #define NEC_BIT_PULSE (1 * NEC_UNIT) |
| 25 | #define NEC_BIT_0_SPACE (1 * NEC_UNIT) |
| 26 | #define NEC_BIT_1_SPACE (3 * NEC_UNIT) |
| 27 | #define NEC_TRAILER_PULSE (1 * NEC_UNIT) |
| 28 | #define NEC_TRAILER_SPACE (10 * NEC_UNIT) /* even longer in reality */ |
Mauro Carvalho Chehab | 9f15478 | 2010-03-21 13:00:55 -0300 | [diff] [blame] | 29 | |
Mauro Carvalho Chehab | 20d5f11 | 2010-03-25 23:49:46 -0300 | [diff] [blame] | 30 | /* Used to register nec_decoder clients */ |
| 31 | static LIST_HEAD(decoder_list); |
Mauro Carvalho Chehab | 6eb9435 | 2010-04-07 16:19:36 -0300 | [diff] [blame] | 32 | static DEFINE_SPINLOCK(decoder_lock); |
Mauro Carvalho Chehab | 20d5f11 | 2010-03-25 23:49:46 -0300 | [diff] [blame] | 33 | |
Mauro Carvalho Chehab | 2f16f63 | 2010-04-03 18:51:50 -0300 | [diff] [blame] | 34 | enum nec_state { |
| 35 | STATE_INACTIVE, |
Mauro Carvalho Chehab | 2f16f63 | 2010-04-03 18:51:50 -0300 | [diff] [blame] | 36 | STATE_HEADER_SPACE, |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 37 | STATE_BIT_PULSE, |
| 38 | STATE_BIT_SPACE, |
| 39 | STATE_TRAILER_PULSE, |
Mauro Carvalho Chehab | 2f16f63 | 2010-04-03 18:51:50 -0300 | [diff] [blame] | 40 | STATE_TRAILER_SPACE, |
| 41 | }; |
| 42 | |
Mauro Carvalho Chehab | 20d5f11 | 2010-03-25 23:49:46 -0300 | [diff] [blame] | 43 | struct decoder_data { |
| 44 | struct list_head list; |
| 45 | struct ir_input_dev *ir_dev; |
Mauro Carvalho Chehab | 2f16f63 | 2010-04-03 18:51:50 -0300 | [diff] [blame] | 46 | |
| 47 | /* State machine control */ |
| 48 | enum nec_state state; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 49 | u32 nec_bits; |
Mauro Carvalho Chehab | 2f16f63 | 2010-04-03 18:51:50 -0300 | [diff] [blame] | 50 | unsigned count; |
Mauro Carvalho Chehab | 20d5f11 | 2010-03-25 23:49:46 -0300 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | |
| 54 | /** |
| 55 | * get_decoder_data() - gets decoder data |
| 56 | * @input_dev: input device |
| 57 | * |
| 58 | * Returns the struct decoder_data that corresponds to a device |
| 59 | */ |
Mauro Carvalho Chehab | 20d5f11 | 2010-03-25 23:49:46 -0300 | [diff] [blame] | 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 | |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 73 | /** |
Mauro Carvalho Chehab | 587835a | 2010-04-04 10:44:51 -0300 | [diff] [blame] | 74 | * ir_nec_decode() - Decode one NEC pulse or space |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 75 | * @input_dev: the struct input_dev descriptor of the device |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 76 | * @duration: the struct ir_raw_event descriptor of the pulse/space |
Mauro Carvalho Chehab | 2f16f63 | 2010-04-03 18:51:50 -0300 | [diff] [blame] | 77 | * |
| 78 | * This function returns -EINVAL if the pulse violates the state machine |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 79 | */ |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 80 | static int ir_nec_decode(struct input_dev *input_dev, struct ir_raw_event ev) |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 81 | { |
Mauro Carvalho Chehab | 2f16f63 | 2010-04-03 18:51:50 -0300 | [diff] [blame] | 82 | struct decoder_data *data; |
| 83 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 84 | u32 scancode; |
| 85 | u8 address, not_address, command, not_command; |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 86 | |
Mauro Carvalho Chehab | 2f16f63 | 2010-04-03 18:51:50 -0300 | [diff] [blame] | 87 | data = get_decoder_data(ir_dev); |
| 88 | if (!data) |
| 89 | return -EINVAL; |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 90 | |
David Härdeman | 667c9eb | 2010-06-13 17:29:31 -0300 | [diff] [blame^] | 91 | if (!(ir_dev->raw->enabled_protocols & IR_TYPE_NEC)) |
Mauro Carvalho Chehab | 7f20d32 | 2010-04-04 14:45:04 -0300 | [diff] [blame] | 92 | return 0; |
| 93 | |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 94 | if (IS_RESET(ev)) { |
Mauro Carvalho Chehab | 2f16f63 | 2010-04-03 18:51:50 -0300 | [diff] [blame] | 95 | data->state = STATE_INACTIVE; |
| 96 | return 0; |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 97 | } |
| 98 | |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 99 | IR_dprintk(2, "NEC decode started at state %d (%uus %s)\n", |
| 100 | data->state, TO_US(ev.duration), TO_STR(ev.pulse)); |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 101 | |
| 102 | switch (data->state) { |
| 103 | |
| 104 | case STATE_INACTIVE: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 105 | if (!ev.pulse) |
| 106 | break; |
| 107 | |
| 108 | if (!eq_margin(ev.duration, NEC_HEADER_PULSE, NEC_UNIT / 2) && |
| 109 | !eq_margin(ev.duration, NECX_HEADER_PULSE, NEC_UNIT / 2)) |
| 110 | break; |
| 111 | |
| 112 | data->count = 0; |
| 113 | data->state = STATE_HEADER_SPACE; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 114 | return 0; |
| 115 | |
| 116 | case STATE_HEADER_SPACE: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 117 | if (ev.pulse) |
| 118 | break; |
| 119 | |
| 120 | if (eq_margin(ev.duration, NEC_HEADER_SPACE, NEC_UNIT / 2)) { |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 121 | data->state = STATE_BIT_PULSE; |
| 122 | return 0; |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 123 | } else if (eq_margin(ev.duration, NEC_REPEAT_SPACE, NEC_UNIT / 2)) { |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 124 | ir_repeat(input_dev); |
| 125 | IR_dprintk(1, "Repeat last key\n"); |
| 126 | data->state = STATE_TRAILER_PULSE; |
| 127 | return 0; |
| 128 | } |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 129 | |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 130 | break; |
| 131 | |
| 132 | case STATE_BIT_PULSE: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 133 | if (!ev.pulse) |
| 134 | break; |
| 135 | |
| 136 | if (!eq_margin(ev.duration, NEC_BIT_PULSE, NEC_UNIT / 2)) |
| 137 | break; |
| 138 | |
| 139 | data->state = STATE_BIT_SPACE; |
| 140 | return 0; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 141 | |
| 142 | case STATE_BIT_SPACE: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 143 | if (ev.pulse) |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 144 | break; |
| 145 | |
| 146 | data->nec_bits <<= 1; |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 147 | if (eq_margin(ev.duration, NEC_BIT_1_SPACE, NEC_UNIT / 2)) |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 148 | data->nec_bits |= 1; |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 149 | else if (!eq_margin(ev.duration, NEC_BIT_0_SPACE, NEC_UNIT / 2)) |
| 150 | break; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 151 | data->count++; |
| 152 | |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 153 | if (data->count == NEC_NBITS) |
| 154 | data->state = STATE_TRAILER_PULSE; |
| 155 | else |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 156 | data->state = STATE_BIT_PULSE; |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 157 | |
| 158 | return 0; |
| 159 | |
| 160 | case STATE_TRAILER_PULSE: |
| 161 | if (!ev.pulse) |
| 162 | break; |
| 163 | |
| 164 | if (!eq_margin(ev.duration, NEC_TRAILER_PULSE, NEC_UNIT / 2)) |
| 165 | break; |
| 166 | |
| 167 | data->state = STATE_TRAILER_SPACE; |
| 168 | return 0; |
| 169 | |
| 170 | case STATE_TRAILER_SPACE: |
| 171 | if (ev.pulse) |
| 172 | break; |
| 173 | |
| 174 | if (!geq_margin(ev.duration, NEC_TRAILER_SPACE, NEC_UNIT / 2)) |
| 175 | break; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 176 | |
| 177 | address = bitrev8((data->nec_bits >> 24) & 0xff); |
| 178 | not_address = bitrev8((data->nec_bits >> 16) & 0xff); |
| 179 | command = bitrev8((data->nec_bits >> 8) & 0xff); |
| 180 | not_command = bitrev8((data->nec_bits >> 0) & 0xff); |
| 181 | |
| 182 | if ((command ^ not_command) != 0xff) { |
| 183 | IR_dprintk(1, "NEC checksum error: received 0x%08x\n", |
| 184 | data->nec_bits); |
| 185 | break; |
| 186 | } |
| 187 | |
| 188 | if ((address ^ not_address) != 0xff) { |
| 189 | /* Extended NEC */ |
| 190 | scancode = address << 16 | |
| 191 | not_address << 8 | |
| 192 | command; |
| 193 | IR_dprintk(1, "NEC (Ext) scancode 0x%06x\n", scancode); |
| 194 | } else { |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 195 | /* Normal NEC */ |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 196 | scancode = address << 8 | command; |
| 197 | IR_dprintk(1, "NEC scancode 0x%04x\n", scancode); |
| 198 | } |
| 199 | |
| 200 | ir_keydown(input_dev, scancode, 0); |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 201 | data->state = STATE_INACTIVE; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 202 | return 0; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 203 | } |
| 204 | |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 205 | IR_dprintk(1, "NEC decode failed at state %d (%uus %s)\n", |
| 206 | data->state, TO_US(ev.duration), TO_STR(ev.pulse)); |
Mauro Carvalho Chehab | 2f16f63 | 2010-04-03 18:51:50 -0300 | [diff] [blame] | 207 | data->state = STATE_INACTIVE; |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 208 | return -EINVAL; |
| 209 | } |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 210 | |
Mauro Carvalho Chehab | 20d5f11 | 2010-03-25 23:49:46 -0300 | [diff] [blame] | 211 | static int ir_nec_register(struct input_dev *input_dev) |
| 212 | { |
| 213 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
| 214 | struct decoder_data *data; |
Mauro Carvalho Chehab | 20d5f11 | 2010-03-25 23:49:46 -0300 | [diff] [blame] | 215 | |
| 216 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
David Härdeman | 667c9eb | 2010-06-13 17:29:31 -0300 | [diff] [blame^] | 217 | if (!data) |
Mauro Carvalho Chehab | 20d5f11 | 2010-03-25 23:49:46 -0300 | [diff] [blame] | 218 | return -ENOMEM; |
Mauro Carvalho Chehab | 20d5f11 | 2010-03-25 23:49:46 -0300 | [diff] [blame] | 219 | |
| 220 | data->ir_dev = ir_dev; |
Mauro Carvalho Chehab | 20d5f11 | 2010-03-25 23:49:46 -0300 | [diff] [blame] | 221 | |
| 222 | spin_lock(&decoder_lock); |
| 223 | list_add_tail(&data->list, &decoder_list); |
| 224 | spin_unlock(&decoder_lock); |
| 225 | |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | static int ir_nec_unregister(struct input_dev *input_dev) |
| 230 | { |
| 231 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
| 232 | static struct decoder_data *data; |
| 233 | |
| 234 | data = get_decoder_data(ir_dev); |
| 235 | if (!data) |
| 236 | return 0; |
| 237 | |
Mauro Carvalho Chehab | 20d5f11 | 2010-03-25 23:49:46 -0300 | [diff] [blame] | 238 | spin_lock(&decoder_lock); |
| 239 | list_del(&data->list); |
| 240 | spin_unlock(&decoder_lock); |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
Mauro Carvalho Chehab | 995187b | 2010-03-24 20:47:53 -0300 | [diff] [blame] | 245 | static struct ir_raw_handler nec_handler = { |
David Härdeman | 667c9eb | 2010-06-13 17:29:31 -0300 | [diff] [blame^] | 246 | .protocols = IR_TYPE_NEC, |
Mauro Carvalho Chehab | 20d5f11 | 2010-03-25 23:49:46 -0300 | [diff] [blame] | 247 | .decode = ir_nec_decode, |
| 248 | .raw_register = ir_nec_register, |
| 249 | .raw_unregister = ir_nec_unregister, |
Mauro Carvalho Chehab | 995187b | 2010-03-24 20:47:53 -0300 | [diff] [blame] | 250 | }; |
| 251 | |
| 252 | static int __init ir_nec_decode_init(void) |
| 253 | { |
| 254 | ir_raw_handler_register(&nec_handler); |
| 255 | |
| 256 | printk(KERN_INFO "IR NEC protocol handler initialized\n"); |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | static void __exit ir_nec_decode_exit(void) |
| 261 | { |
| 262 | ir_raw_handler_unregister(&nec_handler); |
| 263 | } |
| 264 | |
| 265 | module_init(ir_nec_decode_init); |
| 266 | module_exit(ir_nec_decode_exit); |
| 267 | |
| 268 | MODULE_LICENSE("GPL"); |
| 269 | MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>"); |
| 270 | MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)"); |
| 271 | MODULE_DESCRIPTION("NEC IR protocol decoder"); |