Mauro Carvalho Chehab | db1423a | 2010-04-04 10:27:20 -0300 | [diff] [blame] | 1 | /* ir-rc5-decoder.c - handle RC-5 IR Pulse/Space protocol |
| 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 | |
| 15 | /* |
| 16 | * This code only handles 14 bits RC-5 protocols. There are other variants |
| 17 | * that use a different number of bits. This is currently unsupported |
Mauro Carvalho Chehab | 9b09df5 | 2010-04-06 02:29:42 -0300 | [diff] [blame] | 18 | * It considers a carrier of 36 kHz, with a total of 14 bits, where |
| 19 | * 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] | 20 | */ |
| 21 | |
| 22 | #include <media/ir-core.h> |
| 23 | |
| 24 | #define RC5_NBITS 14 |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame^] | 25 | #define RC5_UNIT 888888 /* ns */ |
Mauro Carvalho Chehab | db1423a | 2010-04-04 10:27:20 -0300 | [diff] [blame] | 26 | |
| 27 | /* Used to register rc5_decoder clients */ |
| 28 | static LIST_HEAD(decoder_list); |
Mauro Carvalho Chehab | 6eb9435 | 2010-04-07 16:19:36 -0300 | [diff] [blame] | 29 | static DEFINE_SPINLOCK(decoder_lock); |
Mauro Carvalho Chehab | db1423a | 2010-04-04 10:27:20 -0300 | [diff] [blame] | 30 | |
| 31 | enum rc5_state { |
| 32 | STATE_INACTIVE, |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame^] | 33 | STATE_BIT_START, |
| 34 | STATE_BIT_END, |
| 35 | STATE_FINISHED, |
Mauro Carvalho Chehab | db1423a | 2010-04-04 10:27:20 -0300 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | struct decoder_data { |
| 39 | struct list_head list; |
| 40 | struct ir_input_dev *ir_dev; |
| 41 | int enabled:1; |
| 42 | |
| 43 | /* State machine control */ |
| 44 | enum rc5_state state; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame^] | 45 | u32 rc5_bits; |
| 46 | int last_unit; |
| 47 | unsigned count; |
Mauro Carvalho Chehab | db1423a | 2010-04-04 10:27:20 -0300 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | |
| 51 | /** |
| 52 | * get_decoder_data() - gets decoder data |
| 53 | * @input_dev: input device |
| 54 | * |
| 55 | * Returns the struct decoder_data that corresponds to a device |
| 56 | */ |
| 57 | |
| 58 | static struct decoder_data *get_decoder_data(struct ir_input_dev *ir_dev) |
| 59 | { |
| 60 | struct decoder_data *data = NULL; |
| 61 | |
| 62 | spin_lock(&decoder_lock); |
| 63 | list_for_each_entry(data, &decoder_list, list) { |
| 64 | if (data->ir_dev == ir_dev) |
| 65 | break; |
| 66 | } |
| 67 | spin_unlock(&decoder_lock); |
| 68 | return data; |
| 69 | } |
| 70 | |
| 71 | static ssize_t store_enabled(struct device *d, |
| 72 | struct device_attribute *mattr, |
| 73 | const char *buf, |
| 74 | size_t len) |
| 75 | { |
| 76 | unsigned long value; |
| 77 | struct ir_input_dev *ir_dev = dev_get_drvdata(d); |
| 78 | struct decoder_data *data = get_decoder_data(ir_dev); |
| 79 | |
| 80 | if (!data) |
| 81 | return -EINVAL; |
| 82 | |
| 83 | if (strict_strtoul(buf, 10, &value) || value > 1) |
| 84 | return -EINVAL; |
| 85 | |
| 86 | data->enabled = value; |
| 87 | |
| 88 | return len; |
| 89 | } |
| 90 | |
| 91 | static ssize_t show_enabled(struct device *d, |
| 92 | struct device_attribute *mattr, char *buf) |
| 93 | { |
| 94 | struct ir_input_dev *ir_dev = dev_get_drvdata(d); |
| 95 | struct decoder_data *data = get_decoder_data(ir_dev); |
| 96 | |
| 97 | if (!data) |
| 98 | return -EINVAL; |
| 99 | |
| 100 | if (data->enabled) |
| 101 | return sprintf(buf, "1\n"); |
| 102 | else |
| 103 | return sprintf(buf, "0\n"); |
| 104 | } |
| 105 | |
| 106 | static DEVICE_ATTR(enabled, S_IRUGO | S_IWUSR, show_enabled, store_enabled); |
| 107 | |
| 108 | static struct attribute *decoder_attributes[] = { |
| 109 | &dev_attr_enabled.attr, |
| 110 | NULL |
| 111 | }; |
| 112 | |
| 113 | static struct attribute_group decoder_attribute_group = { |
| 114 | .name = "rc5_decoder", |
| 115 | .attrs = decoder_attributes, |
| 116 | }; |
| 117 | |
| 118 | /** |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame^] | 119 | * ir_rc5_decode() - Decode one RC-5 pulse or space |
Mauro Carvalho Chehab | db1423a | 2010-04-04 10:27:20 -0300 | [diff] [blame] | 120 | * @input_dev: the struct input_dev descriptor of the device |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame^] | 121 | * @duration: duration of pulse/space in ns |
Mauro Carvalho Chehab | db1423a | 2010-04-04 10:27:20 -0300 | [diff] [blame] | 122 | * |
| 123 | * This function returns -EINVAL if the pulse violates the state machine |
| 124 | */ |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame^] | 125 | static int ir_rc5_decode(struct input_dev *input_dev, s64 duration) |
Mauro Carvalho Chehab | db1423a | 2010-04-04 10:27:20 -0300 | [diff] [blame] | 126 | { |
| 127 | struct decoder_data *data; |
| 128 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame^] | 129 | u8 command, system, toggle; |
| 130 | u32 scancode; |
| 131 | int u; |
Mauro Carvalho Chehab | db1423a | 2010-04-04 10:27:20 -0300 | [diff] [blame] | 132 | |
| 133 | data = get_decoder_data(ir_dev); |
| 134 | if (!data) |
| 135 | return -EINVAL; |
| 136 | |
Mauro Carvalho Chehab | 7f20d32 | 2010-04-04 14:45:04 -0300 | [diff] [blame] | 137 | if (!data->enabled) |
| 138 | return 0; |
| 139 | |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame^] | 140 | if (IS_RESET(duration)) { |
Mauro Carvalho Chehab | db1423a | 2010-04-04 10:27:20 -0300 | [diff] [blame] | 141 | data->state = STATE_INACTIVE; |
| 142 | return 0; |
| 143 | } |
| 144 | |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame^] | 145 | u = TO_UNITS(duration, RC5_UNIT); |
| 146 | if (DURATION(u) == 0) |
| 147 | goto out; |
| 148 | |
| 149 | again: |
| 150 | IR_dprintk(2, "RC5 decode started at state %i (%i units, %ius)\n", |
| 151 | data->state, u, TO_US(duration)); |
| 152 | |
| 153 | if (DURATION(u) == 0 && data->state != STATE_FINISHED) |
| 154 | return 0; |
| 155 | |
| 156 | switch (data->state) { |
| 157 | |
| 158 | case STATE_INACTIVE: |
| 159 | if (IS_PULSE(u)) { |
| 160 | data->state = STATE_BIT_START; |
| 161 | data->count = 1; |
| 162 | DECREASE_DURATION(u, 1); |
| 163 | goto again; |
| 164 | } |
| 165 | break; |
| 166 | |
| 167 | case STATE_BIT_START: |
| 168 | if (DURATION(u) == 1) { |
| 169 | data->rc5_bits <<= 1; |
| 170 | if (IS_SPACE(u)) |
| 171 | data->rc5_bits |= 1; |
| 172 | data->count++; |
| 173 | data->last_unit = u; |
| 174 | |
| 175 | /* |
| 176 | * If the last bit is zero, a space will merge |
| 177 | * with the silence after the command. |
| 178 | */ |
| 179 | if (IS_PULSE(u) && data->count == RC5_NBITS) { |
| 180 | data->state = STATE_FINISHED; |
| 181 | goto again; |
| 182 | } |
| 183 | |
| 184 | data->state = STATE_BIT_END; |
| 185 | return 0; |
| 186 | } |
| 187 | break; |
| 188 | |
| 189 | case STATE_BIT_END: |
| 190 | if (IS_TRANSITION(u, data->last_unit)) { |
| 191 | if (data->count == RC5_NBITS) |
| 192 | data->state = STATE_FINISHED; |
| 193 | else |
| 194 | data->state = STATE_BIT_START; |
| 195 | |
| 196 | DECREASE_DURATION(u, 1); |
| 197 | goto again; |
| 198 | } |
| 199 | break; |
| 200 | |
| 201 | case STATE_FINISHED: |
| 202 | command = (data->rc5_bits & 0x0003F) >> 0; |
| 203 | system = (data->rc5_bits & 0x007C0) >> 6; |
| 204 | toggle = (data->rc5_bits & 0x00800) ? 1 : 0; |
| 205 | command += (data->rc5_bits & 0x01000) ? 0 : 0x40; |
| 206 | scancode = system << 8 | command; |
| 207 | |
| 208 | IR_dprintk(1, "RC5 scancode 0x%04x (toggle: %u)\n", |
| 209 | scancode, toggle); |
| 210 | ir_keydown(input_dev, scancode, toggle); |
| 211 | data->state = STATE_INACTIVE; |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | out: |
| 216 | IR_dprintk(1, "RC5 decode failed at state %i (%i units, %ius)\n", |
| 217 | data->state, u, TO_US(duration)); |
Mauro Carvalho Chehab | db1423a | 2010-04-04 10:27:20 -0300 | [diff] [blame] | 218 | data->state = STATE_INACTIVE; |
| 219 | return -EINVAL; |
| 220 | } |
| 221 | |
Mauro Carvalho Chehab | db1423a | 2010-04-04 10:27:20 -0300 | [diff] [blame] | 222 | static int ir_rc5_register(struct input_dev *input_dev) |
| 223 | { |
| 224 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
| 225 | struct decoder_data *data; |
| 226 | int rc; |
| 227 | |
| 228 | rc = sysfs_create_group(&ir_dev->dev.kobj, &decoder_attribute_group); |
| 229 | if (rc < 0) |
| 230 | return rc; |
| 231 | |
| 232 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 233 | if (!data) { |
| 234 | sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group); |
| 235 | return -ENOMEM; |
| 236 | } |
| 237 | |
| 238 | data->ir_dev = ir_dev; |
| 239 | data->enabled = 1; |
| 240 | |
| 241 | spin_lock(&decoder_lock); |
| 242 | list_add_tail(&data->list, &decoder_list); |
| 243 | spin_unlock(&decoder_lock); |
| 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | static int ir_rc5_unregister(struct input_dev *input_dev) |
| 249 | { |
| 250 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
| 251 | static struct decoder_data *data; |
| 252 | |
| 253 | data = get_decoder_data(ir_dev); |
| 254 | if (!data) |
| 255 | return 0; |
| 256 | |
| 257 | sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group); |
| 258 | |
| 259 | spin_lock(&decoder_lock); |
| 260 | list_del(&data->list); |
| 261 | spin_unlock(&decoder_lock); |
| 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | static struct ir_raw_handler rc5_handler = { |
| 267 | .decode = ir_rc5_decode, |
| 268 | .raw_register = ir_rc5_register, |
| 269 | .raw_unregister = ir_rc5_unregister, |
| 270 | }; |
| 271 | |
| 272 | static int __init ir_rc5_decode_init(void) |
| 273 | { |
| 274 | ir_raw_handler_register(&rc5_handler); |
| 275 | |
| 276 | printk(KERN_INFO "IR RC-5 protocol handler initialized\n"); |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | static void __exit ir_rc5_decode_exit(void) |
| 281 | { |
| 282 | ir_raw_handler_unregister(&rc5_handler); |
| 283 | } |
| 284 | |
| 285 | module_init(ir_rc5_decode_init); |
| 286 | module_exit(ir_rc5_decode_exit); |
| 287 | |
| 288 | MODULE_LICENSE("GPL"); |
| 289 | MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>"); |
| 290 | MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)"); |
| 291 | MODULE_DESCRIPTION("RC-5 IR protocol decoder"); |