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 | * |
Mauro Carvalho Chehab | 37e59f8 | 2014-02-07 08:03:07 -0200 | [diff] [blame^] | 3 | * Copyright (C) 2010 by Mauro Carvalho Chehab |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 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> |
Paul Gortmaker | 7a707b8 | 2011-07-03 14:03:12 -0400 | [diff] [blame] | 16 | #include <linux/module.h> |
Mauro Carvalho Chehab | f62de67 | 2010-11-09 23:09:57 -0300 | [diff] [blame] | 17 | #include "rc-core-priv.h" |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 18 | |
Mauro Carvalho Chehab | 67780d6 | 2010-04-03 20:33:00 -0300 | [diff] [blame] | 19 | #define NEC_NBITS 32 |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 20 | #define NEC_UNIT 562500 /* ns */ |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 21 | #define NEC_HEADER_PULSE (16 * NEC_UNIT) |
| 22 | #define NECX_HEADER_PULSE (8 * NEC_UNIT) /* Less common NEC variant */ |
| 23 | #define NEC_HEADER_SPACE (8 * NEC_UNIT) |
Maxim Levitsky | e31f412 | 2010-07-31 11:59:19 -0300 | [diff] [blame] | 24 | #define NEC_REPEAT_SPACE (4 * NEC_UNIT) |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 25 | #define NEC_BIT_PULSE (1 * NEC_UNIT) |
| 26 | #define NEC_BIT_0_SPACE (1 * NEC_UNIT) |
| 27 | #define NEC_BIT_1_SPACE (3 * NEC_UNIT) |
| 28 | #define NEC_TRAILER_PULSE (1 * NEC_UNIT) |
| 29 | #define NEC_TRAILER_SPACE (10 * NEC_UNIT) /* even longer in reality */ |
Maxim Levitsky | 86ff071 | 2010-07-31 11:59:20 -0300 | [diff] [blame] | 30 | #define NECX_REPEAT_BITS 1 |
Mauro Carvalho Chehab | 9f15478 | 2010-03-21 13:00:55 -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 | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 41 | /** |
Mauro Carvalho Chehab | 587835a | 2010-04-04 10:44:51 -0300 | [diff] [blame] | 42 | * ir_nec_decode() - Decode one NEC pulse or space |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 43 | * @dev: the struct rc_dev descriptor of the device |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 44 | * @duration: the struct ir_raw_event descriptor of the pulse/space |
Mauro Carvalho Chehab | 2f16f63 | 2010-04-03 18:51:50 -0300 | [diff] [blame] | 45 | * |
| 46 | * This function returns -EINVAL if the pulse violates the state machine |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 47 | */ |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 48 | static int ir_nec_decode(struct rc_dev *dev, struct ir_raw_event ev) |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 49 | { |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 50 | struct nec_dec *data = &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; |
Jarod Wilson | 4be22b6 | 2011-03-22 17:23:15 -0300 | [diff] [blame] | 53 | bool send_32bits = false; |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 54 | |
David Härdeman | 9719afa | 2013-03-06 16:52:05 -0300 | [diff] [blame] | 55 | if (!(dev->enabled_protocols & RC_BIT_NEC)) |
Mauro Carvalho Chehab | 7f20d32 | 2010-04-04 14:45:04 -0300 | [diff] [blame] | 56 | return 0; |
| 57 | |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 58 | if (!is_timing_event(ev)) { |
| 59 | if (ev.reset) |
| 60 | data->state = STATE_INACTIVE; |
Mauro Carvalho Chehab | 2f16f63 | 2010-04-03 18:51:50 -0300 | [diff] [blame] | 61 | return 0; |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 62 | } |
| 63 | |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 64 | IR_dprintk(2, "NEC decode started at state %d (%uus %s)\n", |
| 65 | data->state, TO_US(ev.duration), TO_STR(ev.pulse)); |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 66 | |
| 67 | switch (data->state) { |
| 68 | |
| 69 | case STATE_INACTIVE: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 70 | if (!ev.pulse) |
| 71 | break; |
| 72 | |
Sean Young | 743135e | 2012-07-31 06:37:29 -0300 | [diff] [blame] | 73 | if (eq_margin(ev.duration, NEC_HEADER_PULSE, NEC_UNIT * 2)) { |
Maxim Levitsky | 86ff071 | 2010-07-31 11:59:20 -0300 | [diff] [blame] | 74 | data->is_nec_x = false; |
| 75 | data->necx_repeat = false; |
| 76 | } else if (eq_margin(ev.duration, NECX_HEADER_PULSE, NEC_UNIT / 2)) |
| 77 | data->is_nec_x = true; |
| 78 | else |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 79 | break; |
| 80 | |
| 81 | data->count = 0; |
| 82 | data->state = STATE_HEADER_SPACE; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 83 | return 0; |
| 84 | |
| 85 | case STATE_HEADER_SPACE: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 86 | if (ev.pulse) |
| 87 | break; |
| 88 | |
Sean Young | 743135e | 2012-07-31 06:37:29 -0300 | [diff] [blame] | 89 | if (eq_margin(ev.duration, NEC_HEADER_SPACE, NEC_UNIT)) { |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 90 | data->state = STATE_BIT_PULSE; |
| 91 | return 0; |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 92 | } else if (eq_margin(ev.duration, NEC_REPEAT_SPACE, NEC_UNIT / 2)) { |
Mariusz Białończyk | 21d3301 | 2010-12-29 19:48:43 -0300 | [diff] [blame] | 93 | if (!dev->keypressed) { |
| 94 | IR_dprintk(1, "Discarding last key repeat: event after key up\n"); |
| 95 | } else { |
| 96 | rc_repeat(dev); |
| 97 | IR_dprintk(1, "Repeat last key\n"); |
| 98 | data->state = STATE_TRAILER_PULSE; |
| 99 | } |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 100 | return 0; |
| 101 | } |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 102 | |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 103 | break; |
| 104 | |
| 105 | case STATE_BIT_PULSE: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 106 | if (!ev.pulse) |
| 107 | break; |
| 108 | |
| 109 | if (!eq_margin(ev.duration, NEC_BIT_PULSE, NEC_UNIT / 2)) |
| 110 | break; |
| 111 | |
| 112 | data->state = STATE_BIT_SPACE; |
| 113 | return 0; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 114 | |
| 115 | case STATE_BIT_SPACE: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 116 | if (ev.pulse) |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 117 | break; |
| 118 | |
Maxim Levitsky | 86ff071 | 2010-07-31 11:59:20 -0300 | [diff] [blame] | 119 | if (data->necx_repeat && data->count == NECX_REPEAT_BITS && |
| 120 | geq_margin(ev.duration, |
| 121 | NEC_TRAILER_SPACE, NEC_UNIT / 2)) { |
| 122 | IR_dprintk(1, "Repeat last key\n"); |
Mauro Carvalho Chehab | ca86674 | 2010-11-17 13:53:11 -0300 | [diff] [blame] | 123 | rc_repeat(dev); |
Maxim Levitsky | 86ff071 | 2010-07-31 11:59:20 -0300 | [diff] [blame] | 124 | data->state = STATE_INACTIVE; |
| 125 | return 0; |
| 126 | |
| 127 | } else if (data->count > NECX_REPEAT_BITS) |
| 128 | data->necx_repeat = false; |
| 129 | |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 130 | data->bits <<= 1; |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 131 | 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] | 132 | data->bits |= 1; |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 133 | else if (!eq_margin(ev.duration, NEC_BIT_0_SPACE, NEC_UNIT / 2)) |
| 134 | break; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 135 | data->count++; |
| 136 | |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 137 | if (data->count == NEC_NBITS) |
| 138 | data->state = STATE_TRAILER_PULSE; |
| 139 | else |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 140 | data->state = STATE_BIT_PULSE; |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 141 | |
| 142 | return 0; |
| 143 | |
| 144 | case STATE_TRAILER_PULSE: |
| 145 | if (!ev.pulse) |
| 146 | break; |
| 147 | |
| 148 | if (!eq_margin(ev.duration, NEC_TRAILER_PULSE, NEC_UNIT / 2)) |
| 149 | break; |
| 150 | |
| 151 | data->state = STATE_TRAILER_SPACE; |
| 152 | return 0; |
| 153 | |
| 154 | case STATE_TRAILER_SPACE: |
| 155 | if (ev.pulse) |
| 156 | break; |
| 157 | |
| 158 | if (!geq_margin(ev.duration, NEC_TRAILER_SPACE, NEC_UNIT / 2)) |
| 159 | break; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 160 | |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 161 | address = bitrev8((data->bits >> 24) & 0xff); |
| 162 | not_address = bitrev8((data->bits >> 16) & 0xff); |
| 163 | command = bitrev8((data->bits >> 8) & 0xff); |
| 164 | not_command = bitrev8((data->bits >> 0) & 0xff); |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 165 | |
| 166 | if ((command ^ not_command) != 0xff) { |
| 167 | IR_dprintk(1, "NEC checksum error: received 0x%08x\n", |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 168 | data->bits); |
Jarod Wilson | 4be22b6 | 2011-03-22 17:23:15 -0300 | [diff] [blame] | 169 | send_32bits = true; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 170 | } |
| 171 | |
Jarod Wilson | 4be22b6 | 2011-03-22 17:23:15 -0300 | [diff] [blame] | 172 | if (send_32bits) { |
| 173 | /* NEC transport, but modified protocol, used by at |
| 174 | * least Apple and TiVo remotes */ |
James Hogan | 18bc174 | 2014-01-17 10:58:50 -0300 | [diff] [blame] | 175 | scancode = not_address << 24 | |
| 176 | address << 16 | |
| 177 | not_command << 8 | |
| 178 | command; |
Jarod Wilson | 4be22b6 | 2011-03-22 17:23:15 -0300 | [diff] [blame] | 179 | IR_dprintk(1, "NEC (modified) scancode 0x%08x\n", scancode); |
| 180 | } else if ((address ^ not_address) != 0xff) { |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 181 | /* Extended NEC */ |
| 182 | scancode = address << 16 | |
| 183 | not_address << 8 | |
| 184 | command; |
| 185 | IR_dprintk(1, "NEC (Ext) scancode 0x%06x\n", scancode); |
| 186 | } else { |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 187 | /* Normal NEC */ |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 188 | scancode = address << 8 | command; |
| 189 | IR_dprintk(1, "NEC scancode 0x%04x\n", scancode); |
| 190 | } |
| 191 | |
Maxim Levitsky | 86ff071 | 2010-07-31 11:59:20 -0300 | [diff] [blame] | 192 | if (data->is_nec_x) |
| 193 | data->necx_repeat = true; |
| 194 | |
Mauro Carvalho Chehab | ca86674 | 2010-11-17 13:53:11 -0300 | [diff] [blame] | 195 | rc_keydown(dev, scancode, 0); |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 196 | data->state = STATE_INACTIVE; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 197 | return 0; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 198 | } |
| 199 | |
Mauro Carvalho Chehab | 39cac37 | 2011-11-23 12:03:32 -0300 | [diff] [blame] | 200 | IR_dprintk(1, "NEC decode failed at count %d state %d (%uus %s)\n", |
| 201 | data->count, data->state, TO_US(ev.duration), TO_STR(ev.pulse)); |
Mauro Carvalho Chehab | 2f16f63 | 2010-04-03 18:51:50 -0300 | [diff] [blame] | 202 | data->state = STATE_INACTIVE; |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 203 | return -EINVAL; |
| 204 | } |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 205 | |
Mauro Carvalho Chehab | 995187b | 2010-03-24 20:47:53 -0300 | [diff] [blame] | 206 | static struct ir_raw_handler nec_handler = { |
David Härdeman | c003ab1 | 2012-10-11 19:11:54 -0300 | [diff] [blame] | 207 | .protocols = RC_BIT_NEC, |
Mauro Carvalho Chehab | 20d5f11 | 2010-03-25 23:49:46 -0300 | [diff] [blame] | 208 | .decode = ir_nec_decode, |
Mauro Carvalho Chehab | 995187b | 2010-03-24 20:47:53 -0300 | [diff] [blame] | 209 | }; |
| 210 | |
| 211 | static int __init ir_nec_decode_init(void) |
| 212 | { |
| 213 | ir_raw_handler_register(&nec_handler); |
| 214 | |
| 215 | printk(KERN_INFO "IR NEC protocol handler initialized\n"); |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | static void __exit ir_nec_decode_exit(void) |
| 220 | { |
| 221 | ir_raw_handler_unregister(&nec_handler); |
| 222 | } |
| 223 | |
| 224 | module_init(ir_nec_decode_init); |
| 225 | module_exit(ir_nec_decode_exit); |
| 226 | |
| 227 | MODULE_LICENSE("GPL"); |
Mauro Carvalho Chehab | 37e59f8 | 2014-02-07 08:03:07 -0200 | [diff] [blame^] | 228 | MODULE_AUTHOR("Mauro Carvalho Chehab"); |
Mauro Carvalho Chehab | 995187b | 2010-03-24 20:47:53 -0300 | [diff] [blame] | 229 | MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)"); |
| 230 | MODULE_DESCRIPTION("NEC IR protocol decoder"); |