David Härdeman | 733419b | 2010-04-08 20:04:30 -0300 | [diff] [blame] | 1 | /* ir-rc5-decoder.c - handle RC5(x) IR Pulse/Space protocol |
Mauro Carvalho Chehab | db1423a | 2010-04-04 10:27:20 -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 | db1423a | 2010-04-04 10:27:20 -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 | |
| 15 | /* |
David Härdeman | 733419b | 2010-04-08 20:04:30 -0300 | [diff] [blame] | 16 | * This code handles 14 bits RC5 protocols and 20 bits RC5x protocols. |
| 17 | * There are other variants that use a different number of bits. |
| 18 | * This is currently unsupported. |
| 19 | * It considers a carrier of 36 kHz, with a total of 14/20 bits, where |
Mauro Carvalho Chehab | 9b09df5 | 2010-04-06 02:29:42 -0300 | [diff] [blame] | 20 | * the first two bits are start bits, and a third one is a filing bit |
Mauro Carvalho Chehab | db1423a | 2010-04-04 10:27:20 -0300 | [diff] [blame] | 21 | */ |
| 22 | |
Mauro Carvalho Chehab | f62de67 | 2010-11-09 23:09:57 -0300 | [diff] [blame] | 23 | #include "rc-core-priv.h" |
Paul Gortmaker | 7a707b8 | 2011-07-03 14:03:12 -0400 | [diff] [blame] | 24 | #include <linux/module.h> |
Mauro Carvalho Chehab | db1423a | 2010-04-04 10:27:20 -0300 | [diff] [blame] | 25 | |
| 26 | #define RC5_NBITS 14 |
David Härdeman | 733419b | 2010-04-08 20:04:30 -0300 | [diff] [blame] | 27 | #define RC5X_NBITS 20 |
| 28 | #define CHECK_RC5X_NBITS 8 |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 29 | #define RC5_UNIT 888888 /* ns */ |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 30 | #define RC5_BIT_START (1 * RC5_UNIT) |
| 31 | #define RC5_BIT_END (1 * RC5_UNIT) |
| 32 | #define RC5X_SPACE (4 * RC5_UNIT) |
Mauro Carvalho Chehab | db1423a | 2010-04-04 10:27:20 -0300 | [diff] [blame] | 33 | |
Mauro Carvalho Chehab | db1423a | 2010-04-04 10:27:20 -0300 | [diff] [blame] | 34 | enum rc5_state { |
| 35 | STATE_INACTIVE, |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 36 | STATE_BIT_START, |
| 37 | STATE_BIT_END, |
David Härdeman | 733419b | 2010-04-08 20:04:30 -0300 | [diff] [blame] | 38 | STATE_CHECK_RC5X, |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 39 | STATE_FINISHED, |
Mauro Carvalho Chehab | db1423a | 2010-04-04 10:27:20 -0300 | [diff] [blame] | 40 | }; |
| 41 | |
Mauro Carvalho Chehab | db1423a | 2010-04-04 10:27:20 -0300 | [diff] [blame] | 42 | /** |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 43 | * ir_rc5_decode() - Decode one RC-5 pulse or space |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 44 | * @dev: the struct rc_dev descriptor of the device |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 45 | * @ev: the struct ir_raw_event descriptor of the pulse/space |
Mauro Carvalho Chehab | db1423a | 2010-04-04 10:27:20 -0300 | [diff] [blame] | 46 | * |
| 47 | * This function returns -EINVAL if the pulse violates the state machine |
| 48 | */ |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 49 | static int ir_rc5_decode(struct rc_dev *dev, struct ir_raw_event ev) |
Mauro Carvalho Chehab | db1423a | 2010-04-04 10:27:20 -0300 | [diff] [blame] | 50 | { |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 51 | struct rc5_dec *data = &dev->raw->rc5; |
David Härdeman | 733419b | 2010-04-08 20:04:30 -0300 | [diff] [blame] | 52 | u8 toggle; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 53 | u32 scancode; |
Mauro Carvalho Chehab | db1423a | 2010-04-04 10:27:20 -0300 | [diff] [blame] | 54 | |
James Hogan | 1a1934f | 2014-02-28 20:17:03 -0300 | [diff] [blame] | 55 | if (!rc_protocols_enabled(dev, RC_BIT_RC5 | RC_BIT_RC5X)) |
David Härdeman | c003ab1 | 2012-10-11 19:11:54 -0300 | [diff] [blame] | 56 | return 0; |
Mauro Carvalho Chehab | 7f20d32 | 2010-04-04 14:45:04 -0300 | [diff] [blame] | 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 | db1423a | 2010-04-04 10:27:20 -0300 | [diff] [blame] | 61 | return 0; |
| 62 | } |
| 63 | |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 64 | if (!geq_margin(ev.duration, RC5_UNIT, RC5_UNIT / 2)) |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 65 | goto out; |
| 66 | |
| 67 | again: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 68 | IR_dprintk(2, "RC5(x) decode started at state %i (%uus %s)\n", |
| 69 | data->state, TO_US(ev.duration), TO_STR(ev.pulse)); |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 70 | |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 71 | if (!geq_margin(ev.duration, RC5_UNIT, RC5_UNIT / 2)) |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 72 | return 0; |
| 73 | |
| 74 | switch (data->state) { |
| 75 | |
| 76 | case STATE_INACTIVE: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 77 | if (!ev.pulse) |
| 78 | break; |
| 79 | |
| 80 | data->state = STATE_BIT_START; |
| 81 | data->count = 1; |
| 82 | /* We just need enough bits to get to STATE_CHECK_RC5X */ |
| 83 | data->wanted_bits = RC5X_NBITS; |
| 84 | decrease_duration(&ev, RC5_BIT_START); |
| 85 | goto again; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 86 | |
| 87 | case STATE_BIT_START: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 88 | if (!eq_margin(ev.duration, RC5_BIT_START, RC5_UNIT / 2)) |
| 89 | break; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 90 | |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 91 | data->bits <<= 1; |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 92 | if (!ev.pulse) |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 93 | data->bits |= 1; |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 94 | data->count++; |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 95 | data->state = STATE_BIT_END; |
| 96 | return 0; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 97 | |
| 98 | case STATE_BIT_END: |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 99 | if (!is_transition(&ev, &dev->raw->prev_ev)) |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 100 | break; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 101 | |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 102 | if (data->count == data->wanted_bits) |
| 103 | data->state = STATE_FINISHED; |
| 104 | else if (data->count == CHECK_RC5X_NBITS) |
| 105 | data->state = STATE_CHECK_RC5X; |
| 106 | else |
| 107 | data->state = STATE_BIT_START; |
| 108 | |
| 109 | decrease_duration(&ev, RC5_BIT_END); |
| 110 | goto again; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 111 | |
David Härdeman | 733419b | 2010-04-08 20:04:30 -0300 | [diff] [blame] | 112 | case STATE_CHECK_RC5X: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 113 | if (!ev.pulse && geq_margin(ev.duration, RC5X_SPACE, RC5_UNIT / 2)) { |
David Härdeman | 733419b | 2010-04-08 20:04:30 -0300 | [diff] [blame] | 114 | /* RC5X */ |
| 115 | data->wanted_bits = RC5X_NBITS; |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 116 | decrease_duration(&ev, RC5X_SPACE); |
David Härdeman | 733419b | 2010-04-08 20:04:30 -0300 | [diff] [blame] | 117 | } else { |
| 118 | /* RC5 */ |
| 119 | data->wanted_bits = RC5_NBITS; |
| 120 | } |
| 121 | data->state = STATE_BIT_START; |
| 122 | goto again; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 123 | |
David Härdeman | 733419b | 2010-04-08 20:04:30 -0300 | [diff] [blame] | 124 | case STATE_FINISHED: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 125 | if (ev.pulse) |
| 126 | break; |
| 127 | |
David Härdeman | 733419b | 2010-04-08 20:04:30 -0300 | [diff] [blame] | 128 | if (data->wanted_bits == RC5X_NBITS) { |
| 129 | /* RC5X */ |
| 130 | u8 xdata, command, system; |
James Hogan | 1a1934f | 2014-02-28 20:17:03 -0300 | [diff] [blame] | 131 | if (!rc_protocols_enabled(dev, RC_BIT_RC5X)) { |
David Härdeman | c003ab1 | 2012-10-11 19:11:54 -0300 | [diff] [blame] | 132 | data->state = STATE_INACTIVE; |
| 133 | return 0; |
| 134 | } |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 135 | xdata = (data->bits & 0x0003F) >> 0; |
| 136 | command = (data->bits & 0x00FC0) >> 6; |
| 137 | system = (data->bits & 0x1F000) >> 12; |
| 138 | toggle = (data->bits & 0x20000) ? 1 : 0; |
| 139 | command += (data->bits & 0x01000) ? 0 : 0x40; |
David Härdeman | 733419b | 2010-04-08 20:04:30 -0300 | [diff] [blame] | 140 | scancode = system << 16 | command << 8 | xdata; |
| 141 | |
| 142 | IR_dprintk(1, "RC5X scancode 0x%06x (toggle: %u)\n", |
| 143 | scancode, toggle); |
| 144 | |
| 145 | } else { |
| 146 | /* RC5 */ |
| 147 | u8 command, system; |
James Hogan | 1a1934f | 2014-02-28 20:17:03 -0300 | [diff] [blame] | 148 | if (!rc_protocols_enabled(dev, RC_BIT_RC5)) { |
David Härdeman | c003ab1 | 2012-10-11 19:11:54 -0300 | [diff] [blame] | 149 | data->state = STATE_INACTIVE; |
| 150 | return 0; |
| 151 | } |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 152 | command = (data->bits & 0x0003F) >> 0; |
| 153 | system = (data->bits & 0x007C0) >> 6; |
| 154 | toggle = (data->bits & 0x00800) ? 1 : 0; |
| 155 | command += (data->bits & 0x01000) ? 0 : 0x40; |
David Härdeman | 733419b | 2010-04-08 20:04:30 -0300 | [diff] [blame] | 156 | scancode = system << 8 | command; |
| 157 | |
| 158 | IR_dprintk(1, "RC5 scancode 0x%04x (toggle: %u)\n", |
| 159 | scancode, toggle); |
| 160 | } |
| 161 | |
Mauro Carvalho Chehab | ca86674 | 2010-11-17 13:53:11 -0300 | [diff] [blame] | 162 | rc_keydown(dev, scancode, toggle); |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 163 | data->state = STATE_INACTIVE; |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | out: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 168 | IR_dprintk(1, "RC5(x) decode failed at state %i (%uus %s)\n", |
| 169 | data->state, TO_US(ev.duration), TO_STR(ev.pulse)); |
Mauro Carvalho Chehab | db1423a | 2010-04-04 10:27:20 -0300 | [diff] [blame] | 170 | data->state = STATE_INACTIVE; |
| 171 | return -EINVAL; |
| 172 | } |
| 173 | |
Mauro Carvalho Chehab | db1423a | 2010-04-04 10:27:20 -0300 | [diff] [blame] | 174 | static struct ir_raw_handler rc5_handler = { |
David Härdeman | c003ab1 | 2012-10-11 19:11:54 -0300 | [diff] [blame] | 175 | .protocols = RC_BIT_RC5 | RC_BIT_RC5X, |
Mauro Carvalho Chehab | db1423a | 2010-04-04 10:27:20 -0300 | [diff] [blame] | 176 | .decode = ir_rc5_decode, |
Mauro Carvalho Chehab | db1423a | 2010-04-04 10:27:20 -0300 | [diff] [blame] | 177 | }; |
| 178 | |
| 179 | static int __init ir_rc5_decode_init(void) |
| 180 | { |
| 181 | ir_raw_handler_register(&rc5_handler); |
| 182 | |
David Härdeman | 733419b | 2010-04-08 20:04:30 -0300 | [diff] [blame] | 183 | printk(KERN_INFO "IR RC5(x) protocol handler initialized\n"); |
Mauro Carvalho Chehab | db1423a | 2010-04-04 10:27:20 -0300 | [diff] [blame] | 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | static void __exit ir_rc5_decode_exit(void) |
| 188 | { |
| 189 | ir_raw_handler_unregister(&rc5_handler); |
| 190 | } |
| 191 | |
| 192 | module_init(ir_rc5_decode_init); |
| 193 | module_exit(ir_rc5_decode_exit); |
| 194 | |
| 195 | MODULE_LICENSE("GPL"); |
Mauro Carvalho Chehab | 37e59f8 | 2014-02-07 08:03:07 -0200 | [diff] [blame] | 196 | MODULE_AUTHOR("Mauro Carvalho Chehab"); |
Mauro Carvalho Chehab | db1423a | 2010-04-04 10:27:20 -0300 | [diff] [blame] | 197 | MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)"); |
David Härdeman | 733419b | 2010-04-08 20:04:30 -0300 | [diff] [blame] | 198 | MODULE_DESCRIPTION("RC5(x) IR protocol decoder"); |