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) |
Maxim Levitsky | e31f412 | 2010-07-31 11:59:19 -0300 | [diff] [blame] | 23 | #define NEC_REPEAT_SPACE (4 * NEC_UNIT) |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 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 */ |
Maxim Levitsky | 86ff071 | 2010-07-31 11:59:20 -0300 | [diff] [blame] | 29 | #define NECX_REPEAT_BITS 1 |
Mauro Carvalho Chehab | 9f15478 | 2010-03-21 13:00:55 -0300 | [diff] [blame] | 30 | |
Mauro Carvalho Chehab | 2f16f63 | 2010-04-03 18:51:50 -0300 | [diff] [blame] | 31 | enum nec_state { |
| 32 | STATE_INACTIVE, |
Mauro Carvalho Chehab | 2f16f63 | 2010-04-03 18:51:50 -0300 | [diff] [blame] | 33 | STATE_HEADER_SPACE, |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 34 | STATE_BIT_PULSE, |
| 35 | STATE_BIT_SPACE, |
| 36 | STATE_TRAILER_PULSE, |
Mauro Carvalho Chehab | 2f16f63 | 2010-04-03 18:51:50 -0300 | [diff] [blame] | 37 | STATE_TRAILER_SPACE, |
| 38 | }; |
| 39 | |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 40 | /** |
Mauro Carvalho Chehab | 587835a | 2010-04-04 10:44:51 -0300 | [diff] [blame] | 41 | * ir_nec_decode() - Decode one NEC pulse or space |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 42 | * @input_dev: the struct input_dev descriptor of the device |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 43 | * @duration: the struct ir_raw_event descriptor of the pulse/space |
Mauro Carvalho Chehab | 2f16f63 | 2010-04-03 18:51:50 -0300 | [diff] [blame] | 44 | * |
| 45 | * This function returns -EINVAL if the pulse violates the state machine |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 46 | */ |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 47 | 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] | 48 | { |
Mauro Carvalho Chehab | 2f16f63 | 2010-04-03 18:51:50 -0300 | [diff] [blame] | 49 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 50 | struct nec_dec *data = &ir_dev->raw->nec; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 51 | u32 scancode; |
| 52 | u8 address, not_address, command, not_command; |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 53 | |
David Härdeman | 667c9eb | 2010-06-13 17:29:31 -0300 | [diff] [blame] | 54 | if (!(ir_dev->raw->enabled_protocols & IR_TYPE_NEC)) |
Mauro Carvalho Chehab | 7f20d32 | 2010-04-04 14:45:04 -0300 | [diff] [blame] | 55 | return 0; |
| 56 | |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame^] | 57 | if (!is_timing_event(ev)) { |
| 58 | if (ev.reset) |
| 59 | data->state = STATE_INACTIVE; |
Mauro Carvalho Chehab | 2f16f63 | 2010-04-03 18:51:50 -0300 | [diff] [blame] | 60 | return 0; |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 61 | } |
| 62 | |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 63 | IR_dprintk(2, "NEC decode started at state %d (%uus %s)\n", |
| 64 | data->state, TO_US(ev.duration), TO_STR(ev.pulse)); |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 65 | |
| 66 | switch (data->state) { |
| 67 | |
| 68 | case STATE_INACTIVE: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 69 | if (!ev.pulse) |
| 70 | break; |
| 71 | |
Maxim Levitsky | 86ff071 | 2010-07-31 11:59:20 -0300 | [diff] [blame] | 72 | if (eq_margin(ev.duration, NEC_HEADER_PULSE, NEC_UNIT / 2)) { |
| 73 | data->is_nec_x = false; |
| 74 | data->necx_repeat = false; |
| 75 | } else if (eq_margin(ev.duration, NECX_HEADER_PULSE, NEC_UNIT / 2)) |
| 76 | data->is_nec_x = true; |
| 77 | else |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 78 | break; |
| 79 | |
| 80 | data->count = 0; |
| 81 | data->state = STATE_HEADER_SPACE; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 82 | return 0; |
| 83 | |
| 84 | case STATE_HEADER_SPACE: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 85 | if (ev.pulse) |
| 86 | break; |
| 87 | |
| 88 | if (eq_margin(ev.duration, NEC_HEADER_SPACE, NEC_UNIT / 2)) { |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 89 | data->state = STATE_BIT_PULSE; |
| 90 | return 0; |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 91 | } 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] | 92 | ir_repeat(input_dev); |
| 93 | IR_dprintk(1, "Repeat last key\n"); |
| 94 | data->state = STATE_TRAILER_PULSE; |
| 95 | return 0; |
| 96 | } |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 97 | |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 98 | break; |
| 99 | |
| 100 | case STATE_BIT_PULSE: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 101 | if (!ev.pulse) |
| 102 | break; |
| 103 | |
| 104 | if (!eq_margin(ev.duration, NEC_BIT_PULSE, NEC_UNIT / 2)) |
| 105 | break; |
| 106 | |
| 107 | data->state = STATE_BIT_SPACE; |
| 108 | return 0; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 109 | |
| 110 | case STATE_BIT_SPACE: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 111 | if (ev.pulse) |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 112 | break; |
| 113 | |
Maxim Levitsky | 86ff071 | 2010-07-31 11:59:20 -0300 | [diff] [blame] | 114 | if (data->necx_repeat && data->count == NECX_REPEAT_BITS && |
| 115 | geq_margin(ev.duration, |
| 116 | NEC_TRAILER_SPACE, NEC_UNIT / 2)) { |
| 117 | IR_dprintk(1, "Repeat last key\n"); |
| 118 | ir_repeat(input_dev); |
| 119 | data->state = STATE_INACTIVE; |
| 120 | return 0; |
| 121 | |
| 122 | } else if (data->count > NECX_REPEAT_BITS) |
| 123 | data->necx_repeat = false; |
| 124 | |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 125 | data->bits <<= 1; |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 126 | if (eq_margin(ev.duration, NEC_BIT_1_SPACE, NEC_UNIT / 2)) |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 127 | data->bits |= 1; |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 128 | else if (!eq_margin(ev.duration, NEC_BIT_0_SPACE, NEC_UNIT / 2)) |
| 129 | break; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 130 | data->count++; |
| 131 | |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 132 | if (data->count == NEC_NBITS) |
| 133 | data->state = STATE_TRAILER_PULSE; |
| 134 | else |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 135 | data->state = STATE_BIT_PULSE; |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 136 | |
| 137 | return 0; |
| 138 | |
| 139 | case STATE_TRAILER_PULSE: |
| 140 | if (!ev.pulse) |
| 141 | break; |
| 142 | |
| 143 | if (!eq_margin(ev.duration, NEC_TRAILER_PULSE, NEC_UNIT / 2)) |
| 144 | break; |
| 145 | |
| 146 | data->state = STATE_TRAILER_SPACE; |
| 147 | return 0; |
| 148 | |
| 149 | case STATE_TRAILER_SPACE: |
| 150 | if (ev.pulse) |
| 151 | break; |
| 152 | |
| 153 | if (!geq_margin(ev.duration, NEC_TRAILER_SPACE, NEC_UNIT / 2)) |
| 154 | break; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 155 | |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 156 | address = bitrev8((data->bits >> 24) & 0xff); |
| 157 | not_address = bitrev8((data->bits >> 16) & 0xff); |
| 158 | command = bitrev8((data->bits >> 8) & 0xff); |
| 159 | not_command = bitrev8((data->bits >> 0) & 0xff); |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 160 | |
| 161 | if ((command ^ not_command) != 0xff) { |
| 162 | IR_dprintk(1, "NEC checksum error: received 0x%08x\n", |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 163 | data->bits); |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 164 | break; |
| 165 | } |
| 166 | |
| 167 | if ((address ^ not_address) != 0xff) { |
| 168 | /* Extended NEC */ |
| 169 | scancode = address << 16 | |
| 170 | not_address << 8 | |
| 171 | command; |
| 172 | IR_dprintk(1, "NEC (Ext) scancode 0x%06x\n", scancode); |
| 173 | } else { |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 174 | /* Normal NEC */ |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 175 | scancode = address << 8 | command; |
| 176 | IR_dprintk(1, "NEC scancode 0x%04x\n", scancode); |
| 177 | } |
| 178 | |
Maxim Levitsky | 86ff071 | 2010-07-31 11:59:20 -0300 | [diff] [blame] | 179 | if (data->is_nec_x) |
| 180 | data->necx_repeat = true; |
| 181 | |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 182 | ir_keydown(input_dev, scancode, 0); |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 183 | data->state = STATE_INACTIVE; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 184 | return 0; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 185 | } |
| 186 | |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 187 | IR_dprintk(1, "NEC decode failed at state %d (%uus %s)\n", |
| 188 | data->state, TO_US(ev.duration), TO_STR(ev.pulse)); |
Mauro Carvalho Chehab | 2f16f63 | 2010-04-03 18:51:50 -0300 | [diff] [blame] | 189 | data->state = STATE_INACTIVE; |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 190 | return -EINVAL; |
| 191 | } |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 192 | |
Mauro Carvalho Chehab | 995187b | 2010-03-24 20:47:53 -0300 | [diff] [blame] | 193 | static struct ir_raw_handler nec_handler = { |
David Härdeman | 667c9eb | 2010-06-13 17:29:31 -0300 | [diff] [blame] | 194 | .protocols = IR_TYPE_NEC, |
Mauro Carvalho Chehab | 20d5f11 | 2010-03-25 23:49:46 -0300 | [diff] [blame] | 195 | .decode = ir_nec_decode, |
Mauro Carvalho Chehab | 995187b | 2010-03-24 20:47:53 -0300 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | static int __init ir_nec_decode_init(void) |
| 199 | { |
| 200 | ir_raw_handler_register(&nec_handler); |
| 201 | |
| 202 | printk(KERN_INFO "IR NEC protocol handler initialized\n"); |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | static void __exit ir_nec_decode_exit(void) |
| 207 | { |
| 208 | ir_raw_handler_unregister(&nec_handler); |
| 209 | } |
| 210 | |
| 211 | module_init(ir_nec_decode_init); |
| 212 | module_exit(ir_nec_decode_exit); |
| 213 | |
| 214 | MODULE_LICENSE("GPL"); |
| 215 | MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>"); |
| 216 | MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)"); |
| 217 | MODULE_DESCRIPTION("NEC IR protocol decoder"); |