ir-rc5-decoder.c 7.6 KB

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