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 |
| 18 | */ |
| 19 | |
| 20 | #include <media/ir-core.h> |
| 21 | |
| 22 | #define RC5_NBITS 14 |
| 23 | #define RC5_HALFBIT 888888 /* ns */ |
| 24 | #define RC5_BIT (RC5_HALFBIT * 2) |
| 25 | #define RC5_DURATION (RC5_BIT * RC5_NBITS) |
| 26 | |
| 27 | #define is_rc5_halfbit(nsec) ((ev->delta.tv_nsec >= RC5_HALFBIT / 2) && \ |
| 28 | (ev->delta.tv_nsec < RC5_HALFBIT + RC5_HALFBIT / 2)) |
| 29 | |
| 30 | #define n_half(nsec) ((ev->delta.tv_nsec + RC5_HALFBIT / 2) / RC5_HALFBIT) |
| 31 | |
| 32 | /* Used to register rc5_decoder clients */ |
| 33 | static LIST_HEAD(decoder_list); |
| 34 | static spinlock_t decoder_lock; |
| 35 | |
| 36 | enum rc5_state { |
| 37 | STATE_INACTIVE, |
| 38 | STATE_START2_SPACE, |
| 39 | STATE_START2_MARK, |
| 40 | STATE_MARKSPACE, |
| 41 | STATE_TRAILER_MARK, |
| 42 | }; |
| 43 | |
| 44 | static char *st_name[] = { |
| 45 | "Inactive", |
| 46 | "start2 sapce", |
| 47 | "start2 mark", |
| 48 | "mark", |
| 49 | "space", |
| 50 | "trailer" |
| 51 | }; |
| 52 | |
| 53 | struct rc5_code { |
| 54 | u8 address; |
| 55 | u8 command; |
| 56 | }; |
| 57 | |
| 58 | struct decoder_data { |
| 59 | struct list_head list; |
| 60 | struct ir_input_dev *ir_dev; |
| 61 | int enabled:1; |
| 62 | |
| 63 | /* State machine control */ |
| 64 | enum rc5_state state; |
| 65 | struct rc5_code rc5_code; |
| 66 | unsigned n_half; |
| 67 | unsigned count; |
| 68 | }; |
| 69 | |
| 70 | |
| 71 | /** |
| 72 | * get_decoder_data() - gets decoder data |
| 73 | * @input_dev: input device |
| 74 | * |
| 75 | * Returns the struct decoder_data that corresponds to a device |
| 76 | */ |
| 77 | |
| 78 | static struct decoder_data *get_decoder_data(struct ir_input_dev *ir_dev) |
| 79 | { |
| 80 | struct decoder_data *data = NULL; |
| 81 | |
| 82 | spin_lock(&decoder_lock); |
| 83 | list_for_each_entry(data, &decoder_list, list) { |
| 84 | if (data->ir_dev == ir_dev) |
| 85 | break; |
| 86 | } |
| 87 | spin_unlock(&decoder_lock); |
| 88 | return data; |
| 89 | } |
| 90 | |
| 91 | static ssize_t store_enabled(struct device *d, |
| 92 | struct device_attribute *mattr, |
| 93 | const char *buf, |
| 94 | size_t len) |
| 95 | { |
| 96 | unsigned long value; |
| 97 | struct ir_input_dev *ir_dev = dev_get_drvdata(d); |
| 98 | struct decoder_data *data = get_decoder_data(ir_dev); |
| 99 | |
| 100 | if (!data) |
| 101 | return -EINVAL; |
| 102 | |
| 103 | if (strict_strtoul(buf, 10, &value) || value > 1) |
| 104 | return -EINVAL; |
| 105 | |
| 106 | data->enabled = value; |
| 107 | |
| 108 | return len; |
| 109 | } |
| 110 | |
| 111 | static ssize_t show_enabled(struct device *d, |
| 112 | struct device_attribute *mattr, char *buf) |
| 113 | { |
| 114 | struct ir_input_dev *ir_dev = dev_get_drvdata(d); |
| 115 | struct decoder_data *data = get_decoder_data(ir_dev); |
| 116 | |
| 117 | if (!data) |
| 118 | return -EINVAL; |
| 119 | |
| 120 | if (data->enabled) |
| 121 | return sprintf(buf, "1\n"); |
| 122 | else |
| 123 | return sprintf(buf, "0\n"); |
| 124 | } |
| 125 | |
| 126 | static DEVICE_ATTR(enabled, S_IRUGO | S_IWUSR, show_enabled, store_enabled); |
| 127 | |
| 128 | static struct attribute *decoder_attributes[] = { |
| 129 | &dev_attr_enabled.attr, |
| 130 | NULL |
| 131 | }; |
| 132 | |
| 133 | static struct attribute_group decoder_attribute_group = { |
| 134 | .name = "rc5_decoder", |
| 135 | .attrs = decoder_attributes, |
| 136 | }; |
| 137 | |
| 138 | /** |
| 139 | * handle_event() - Decode one RC-5 pulse or space |
| 140 | * @input_dev: the struct input_dev descriptor of the device |
| 141 | * @ev: event array with type/duration of pulse/space |
| 142 | * |
| 143 | * This function returns -EINVAL if the pulse violates the state machine |
| 144 | */ |
Mauro Carvalho Chehab | 587835a | 2010-04-04 10:44:51 -0300 | [diff] [blame] | 145 | static int ir_rc5_decode(struct input_dev *input_dev, |
Mauro Carvalho Chehab | db1423a | 2010-04-04 10:27:20 -0300 | [diff] [blame] | 146 | struct ir_raw_event *ev) |
| 147 | { |
| 148 | struct decoder_data *data; |
| 149 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
| 150 | int bit, last_bit, n_half; |
| 151 | |
| 152 | data = get_decoder_data(ir_dev); |
| 153 | if (!data) |
| 154 | return -EINVAL; |
| 155 | |
Mauro Carvalho Chehab | 7f20d32 | 2010-04-04 14:45:04 -0300 | [diff] [blame^] | 156 | if (!data->enabled) |
| 157 | return 0; |
| 158 | |
Mauro Carvalho Chehab | db1423a | 2010-04-04 10:27:20 -0300 | [diff] [blame] | 159 | /* Except for the initial event, what matters is the previous bit */ |
| 160 | bit = (ev->type & IR_PULSE) ? 1 : 0; |
| 161 | |
| 162 | last_bit = !bit; |
| 163 | |
| 164 | /* Discards spurious space last_bits when inactive */ |
| 165 | |
| 166 | /* Very long delays are considered as start events */ |
| 167 | if (ev->delta.tv_nsec > RC5_DURATION + RC5_HALFBIT / 2) |
| 168 | data->state = STATE_INACTIVE; |
| 169 | |
| 170 | if (ev->type & IR_START_EVENT) |
| 171 | data->state = STATE_INACTIVE; |
| 172 | |
| 173 | switch (data->state) { |
| 174 | case STATE_INACTIVE: |
| 175 | IR_dprintk(1, "currently inative. Received bit (%s) @%luus\n", |
| 176 | last_bit ? "pulse" : "space", |
| 177 | (ev->delta.tv_nsec + 500) / 1000); |
| 178 | |
| 179 | /* Discards the initial start space */ |
| 180 | if (bit) |
| 181 | return 0; |
| 182 | data->count = 0; |
| 183 | data->n_half = 0; |
| 184 | memset (&data->rc5_code, 0, sizeof(data->rc5_code)); |
| 185 | |
| 186 | data->state = STATE_START2_SPACE; |
| 187 | return 0; |
| 188 | case STATE_START2_SPACE: |
| 189 | if (last_bit) |
| 190 | goto err; |
| 191 | if (!is_rc5_halfbit(ev->delta.tv_nsec)) |
| 192 | goto err; |
| 193 | data->state = STATE_START2_MARK; |
| 194 | return 0; |
| 195 | case STATE_START2_MARK: |
| 196 | if (!last_bit) |
| 197 | goto err; |
| 198 | |
| 199 | if (!is_rc5_halfbit(ev->delta.tv_nsec)) |
| 200 | goto err; |
| 201 | |
| 202 | data->state = STATE_MARKSPACE; |
| 203 | return 0; |
| 204 | case STATE_MARKSPACE: |
| 205 | n_half = n_half(ev->delta.tv_nsec); |
| 206 | if (n_half < 1 || n_half > 3) { |
| 207 | IR_dprintk(1, "Decode failed at %d-th bit (%s) @%luus\n", |
| 208 | data->count, |
| 209 | last_bit ? "pulse" : "space", |
| 210 | (ev->delta.tv_nsec + 500) / 1000); |
| 211 | printk("%d halves\n", n_half); |
| 212 | goto err2; |
| 213 | } |
| 214 | data->n_half += n_half; |
| 215 | |
| 216 | if (!last_bit) |
| 217 | return 0; |
| 218 | |
| 219 | /* Got one complete mark/space cycle */ |
| 220 | |
| 221 | bit = ((data->count + 1) * 2)/ data->n_half; |
| 222 | |
| 223 | printk("%d halves, %d bits\n", n_half, bit); |
| 224 | |
| 225 | #if 1 /* SANITY check - while testing the decoder */ |
| 226 | if (bit > 1) { |
| 227 | IR_dprintk(1, "Decoder HAS failed at %d-th bit (%s) @%luus\n", |
| 228 | data->count, |
| 229 | last_bit ? "pulse" : "space", |
| 230 | (ev->delta.tv_nsec + 500) / 1000); |
| 231 | |
| 232 | goto err2; |
| 233 | } |
| 234 | #endif |
| 235 | /* Ok, we've got a valid bit. proccess it */ |
| 236 | if (bit) { |
| 237 | int shift = data->count; |
| 238 | |
| 239 | /* |
| 240 | * RC-5 transmit bytes on this temporal order: |
| 241 | * address | not address | command | not command |
| 242 | */ |
| 243 | if (shift < 8) { |
| 244 | data->rc5_code.address |= 1 << shift; |
| 245 | } else { |
| 246 | data->rc5_code.command |= 1 << (shift - 8); |
| 247 | } |
| 248 | } |
| 249 | IR_dprintk(1, "RC-5: bit #%d: %d (%d)\n", |
| 250 | data->count, bit, data->n_half); |
| 251 | if (++data->count >= RC5_NBITS) { |
| 252 | u32 scancode; |
| 253 | scancode = data->rc5_code.address << 8 | |
| 254 | data->rc5_code.command; |
| 255 | IR_dprintk(1, "RC-5 scancode 0x%04x\n", scancode); |
| 256 | |
| 257 | ir_keydown(input_dev, scancode, 0); |
| 258 | |
| 259 | data->state = STATE_TRAILER_MARK; |
| 260 | } |
| 261 | return 0; |
| 262 | case STATE_TRAILER_MARK: |
| 263 | if (!last_bit) |
| 264 | goto err; |
| 265 | data->state = STATE_INACTIVE; |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | err: |
| 270 | IR_dprintk(1, "RC-5 decoded failed at state %s (%s) @ %luus\n", |
| 271 | st_name[data->state], |
| 272 | bit ? "pulse" : "space", |
| 273 | (ev->delta.tv_nsec + 500) / 1000); |
| 274 | err2: |
| 275 | data->state = STATE_INACTIVE; |
| 276 | return -EINVAL; |
| 277 | } |
| 278 | |
Mauro Carvalho Chehab | db1423a | 2010-04-04 10:27:20 -0300 | [diff] [blame] | 279 | static int ir_rc5_register(struct input_dev *input_dev) |
| 280 | { |
| 281 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
| 282 | struct decoder_data *data; |
| 283 | int rc; |
| 284 | |
| 285 | rc = sysfs_create_group(&ir_dev->dev.kobj, &decoder_attribute_group); |
| 286 | if (rc < 0) |
| 287 | return rc; |
| 288 | |
| 289 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 290 | if (!data) { |
| 291 | sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group); |
| 292 | return -ENOMEM; |
| 293 | } |
| 294 | |
| 295 | data->ir_dev = ir_dev; |
| 296 | data->enabled = 1; |
| 297 | |
| 298 | spin_lock(&decoder_lock); |
| 299 | list_add_tail(&data->list, &decoder_list); |
| 300 | spin_unlock(&decoder_lock); |
| 301 | |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | static int ir_rc5_unregister(struct input_dev *input_dev) |
| 306 | { |
| 307 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
| 308 | static struct decoder_data *data; |
| 309 | |
| 310 | data = get_decoder_data(ir_dev); |
| 311 | if (!data) |
| 312 | return 0; |
| 313 | |
| 314 | sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group); |
| 315 | |
| 316 | spin_lock(&decoder_lock); |
| 317 | list_del(&data->list); |
| 318 | spin_unlock(&decoder_lock); |
| 319 | |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | static struct ir_raw_handler rc5_handler = { |
| 324 | .decode = ir_rc5_decode, |
| 325 | .raw_register = ir_rc5_register, |
| 326 | .raw_unregister = ir_rc5_unregister, |
| 327 | }; |
| 328 | |
| 329 | static int __init ir_rc5_decode_init(void) |
| 330 | { |
| 331 | ir_raw_handler_register(&rc5_handler); |
| 332 | |
| 333 | printk(KERN_INFO "IR RC-5 protocol handler initialized\n"); |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | static void __exit ir_rc5_decode_exit(void) |
| 338 | { |
| 339 | ir_raw_handler_unregister(&rc5_handler); |
| 340 | } |
| 341 | |
| 342 | module_init(ir_rc5_decode_init); |
| 343 | module_exit(ir_rc5_decode_exit); |
| 344 | |
| 345 | MODULE_LICENSE("GPL"); |
| 346 | MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>"); |
| 347 | MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)"); |
| 348 | MODULE_DESCRIPTION("RC-5 IR protocol decoder"); |