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