ir-rc5-decoder.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. * This code only handles 14 bits RC-5 protocols. There are other variants
  16. * that use a different number of bits. This is currently unsupported
  17. * It considers a carrier of 36 kHz, with a total of 14 bits, where
  18. * the first two bits are start bits, and a third one is a filing bit
  19. */
  20. #include <media/ir-core.h>
  21. static unsigned int ir_rc5_remote_gap = 888888;
  22. #define RC5_NBITS 14
  23. #define RC5_BIT (ir_rc5_remote_gap * 2)
  24. #define RC5_DURATION (ir_rc5_remote_gap * RC5_NBITS)
  25. /* Used to register rc5_decoder clients */
  26. static LIST_HEAD(decoder_list);
  27. static spinlock_t decoder_lock;
  28. enum rc5_state {
  29. STATE_INACTIVE,
  30. STATE_MARKSPACE,
  31. STATE_TRAILER,
  32. };
  33. struct rc5_code {
  34. u8 address;
  35. u8 command;
  36. };
  37. struct decoder_data {
  38. struct list_head list;
  39. struct ir_input_dev *ir_dev;
  40. int enabled:1;
  41. /* State machine control */
  42. enum rc5_state state;
  43. struct rc5_code rc5_code;
  44. unsigned code, elapsed, last_bit, last_code;
  45. };
  46. /**
  47. * get_decoder_data() - gets decoder data
  48. * @input_dev: input device
  49. *
  50. * Returns the struct decoder_data that corresponds to a device
  51. */
  52. static struct decoder_data *get_decoder_data(struct ir_input_dev *ir_dev)
  53. {
  54. struct decoder_data *data = NULL;
  55. spin_lock(&decoder_lock);
  56. list_for_each_entry(data, &decoder_list, list) {
  57. if (data->ir_dev == ir_dev)
  58. break;
  59. }
  60. spin_unlock(&decoder_lock);
  61. return data;
  62. }
  63. static ssize_t store_enabled(struct device *d,
  64. struct device_attribute *mattr,
  65. const char *buf,
  66. size_t len)
  67. {
  68. unsigned long value;
  69. struct ir_input_dev *ir_dev = dev_get_drvdata(d);
  70. struct decoder_data *data = get_decoder_data(ir_dev);
  71. if (!data)
  72. return -EINVAL;
  73. if (strict_strtoul(buf, 10, &value) || value > 1)
  74. return -EINVAL;
  75. data->enabled = value;
  76. return len;
  77. }
  78. static ssize_t show_enabled(struct device *d,
  79. struct device_attribute *mattr, char *buf)
  80. {
  81. struct ir_input_dev *ir_dev = dev_get_drvdata(d);
  82. struct decoder_data *data = get_decoder_data(ir_dev);
  83. if (!data)
  84. return -EINVAL;
  85. if (data->enabled)
  86. return sprintf(buf, "1\n");
  87. else
  88. return sprintf(buf, "0\n");
  89. }
  90. static DEVICE_ATTR(enabled, S_IRUGO | S_IWUSR, show_enabled, store_enabled);
  91. static struct attribute *decoder_attributes[] = {
  92. &dev_attr_enabled.attr,
  93. NULL
  94. };
  95. static struct attribute_group decoder_attribute_group = {
  96. .name = "rc5_decoder",
  97. .attrs = decoder_attributes,
  98. };
  99. /**
  100. * handle_event() - Decode one RC-5 pulse or space
  101. * @input_dev: the struct input_dev descriptor of the device
  102. * @ev: event array with type/duration of pulse/space
  103. *
  104. * This function returns -EINVAL if the pulse violates the state machine
  105. */
  106. static int ir_rc5_decode(struct input_dev *input_dev,
  107. struct ir_raw_event *ev)
  108. {
  109. struct decoder_data *data;
  110. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  111. int is_pulse, scancode, delta, toggle;
  112. data = get_decoder_data(ir_dev);
  113. if (!data)
  114. return -EINVAL;
  115. if (!data->enabled)
  116. return 0;
  117. delta = DIV_ROUND_CLOSEST(ev->delta.tv_nsec, ir_rc5_remote_gap);
  118. /* The duration time refers to the last bit time */
  119. is_pulse = (ev->type & IR_PULSE) ? 1 : 0;
  120. /* Very long delays are considered as start events */
  121. if (delta > RC5_DURATION || (ev->type & IR_START_EVENT))
  122. data->state = STATE_INACTIVE;
  123. switch (data->state) {
  124. case STATE_INACTIVE:
  125. IR_dprintk(2, "currently inative. Start bit (%s) @%uus\n",
  126. is_pulse ? "pulse" : "space",
  127. (unsigned)(ev->delta.tv_nsec + 500) / 1000);
  128. /* Discards the initial start space */
  129. if (!is_pulse)
  130. goto err;
  131. data->code = 1;
  132. data->last_bit = 1;
  133. data->elapsed = 0;
  134. memset(&data->rc5_code, 0, sizeof(data->rc5_code));
  135. data->state = STATE_MARKSPACE;
  136. return 0;
  137. case STATE_MARKSPACE:
  138. if (delta != 1)
  139. data->last_bit = data->last_bit ? 0 : 1;
  140. data->elapsed += delta;
  141. if ((data->elapsed % 2) == 1)
  142. return 0;
  143. data->code <<= 1;
  144. data->code |= data->last_bit;
  145. /* Fill the 2 unused bits at the command with 0 */
  146. if (data->elapsed / 2 == 6)
  147. data->code <<= 2;
  148. if (data->elapsed >= (RC5_NBITS - 1) * 2) {
  149. scancode = data->code;
  150. /* Check for the start bits */
  151. if ((scancode & 0xc000) != 0xc000) {
  152. IR_dprintk(1, "Code 0x%04x doesn't have two start bits. It is not RC-5\n", scancode);
  153. goto err;
  154. }
  155. toggle = (scancode & 0x2000) ? 1 : 0;
  156. if (scancode == data->last_code) {
  157. IR_dprintk(1, "RC-5 repeat\n");
  158. ir_repeat(input_dev);
  159. } else {
  160. data->last_code = scancode;
  161. scancode &= 0x1fff;
  162. IR_dprintk(1, "RC-5 scancode 0x%04x\n", scancode);
  163. ir_keydown(input_dev, scancode, 0);
  164. }
  165. data->state = STATE_TRAILER;
  166. }
  167. return 0;
  168. case STATE_TRAILER:
  169. data->state = STATE_INACTIVE;
  170. return 0;
  171. }
  172. err:
  173. IR_dprintk(1, "RC-5 decoded failed at %s @ %luus\n",
  174. is_pulse ? "pulse" : "space",
  175. (ev->delta.tv_nsec + 500) / 1000);
  176. data->state = STATE_INACTIVE;
  177. return -EINVAL;
  178. }
  179. static int ir_rc5_register(struct input_dev *input_dev)
  180. {
  181. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  182. struct decoder_data *data;
  183. int rc;
  184. rc = sysfs_create_group(&ir_dev->dev.kobj, &decoder_attribute_group);
  185. if (rc < 0)
  186. return rc;
  187. data = kzalloc(sizeof(*data), GFP_KERNEL);
  188. if (!data) {
  189. sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group);
  190. return -ENOMEM;
  191. }
  192. data->ir_dev = ir_dev;
  193. data->enabled = 1;
  194. spin_lock(&decoder_lock);
  195. list_add_tail(&data->list, &decoder_list);
  196. spin_unlock(&decoder_lock);
  197. return 0;
  198. }
  199. static int ir_rc5_unregister(struct input_dev *input_dev)
  200. {
  201. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  202. static struct decoder_data *data;
  203. data = get_decoder_data(ir_dev);
  204. if (!data)
  205. return 0;
  206. sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group);
  207. spin_lock(&decoder_lock);
  208. list_del(&data->list);
  209. spin_unlock(&decoder_lock);
  210. return 0;
  211. }
  212. static struct ir_raw_handler rc5_handler = {
  213. .decode = ir_rc5_decode,
  214. .raw_register = ir_rc5_register,
  215. .raw_unregister = ir_rc5_unregister,
  216. };
  217. static int __init ir_rc5_decode_init(void)
  218. {
  219. ir_raw_handler_register(&rc5_handler);
  220. printk(KERN_INFO "IR RC-5 protocol handler initialized\n");
  221. return 0;
  222. }
  223. static void __exit ir_rc5_decode_exit(void)
  224. {
  225. ir_raw_handler_unregister(&rc5_handler);
  226. }
  227. module_init(ir_rc5_decode_init);
  228. module_exit(ir_rc5_decode_exit);
  229. MODULE_LICENSE("GPL");
  230. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  231. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  232. MODULE_DESCRIPTION("RC-5 IR protocol decoder");