ir-nec-decoder.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /* ir-nec-decoder.c - handle NEC 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. #include <linux/bitrev.h>
  15. #include "ir-core-priv.h"
  16. #define NEC_NBITS 32
  17. #define NEC_UNIT 562500 /* ns */
  18. #define NEC_HEADER_PULSE (16 * NEC_UNIT)
  19. #define NECX_HEADER_PULSE (8 * NEC_UNIT) /* Less common NEC variant */
  20. #define NEC_HEADER_SPACE (8 * NEC_UNIT)
  21. #define NEC_REPEAT_SPACE (8 * NEC_UNIT)
  22. #define NEC_BIT_PULSE (1 * NEC_UNIT)
  23. #define NEC_BIT_0_SPACE (1 * NEC_UNIT)
  24. #define NEC_BIT_1_SPACE (3 * NEC_UNIT)
  25. #define NEC_TRAILER_PULSE (1 * NEC_UNIT)
  26. #define NEC_TRAILER_SPACE (10 * NEC_UNIT) /* even longer in reality */
  27. /* Used to register nec_decoder clients */
  28. static LIST_HEAD(decoder_list);
  29. static DEFINE_SPINLOCK(decoder_lock);
  30. enum nec_state {
  31. STATE_INACTIVE,
  32. STATE_HEADER_SPACE,
  33. STATE_BIT_PULSE,
  34. STATE_BIT_SPACE,
  35. STATE_TRAILER_PULSE,
  36. STATE_TRAILER_SPACE,
  37. };
  38. struct decoder_data {
  39. struct list_head list;
  40. struct ir_input_dev *ir_dev;
  41. int enabled:1;
  42. /* State machine control */
  43. enum nec_state state;
  44. u32 nec_bits;
  45. unsigned count;
  46. };
  47. /**
  48. * get_decoder_data() - gets decoder data
  49. * @input_dev: input device
  50. *
  51. * Returns the struct decoder_data that corresponds to a device
  52. */
  53. static struct decoder_data *get_decoder_data(struct ir_input_dev *ir_dev)
  54. {
  55. struct decoder_data *data = NULL;
  56. spin_lock(&decoder_lock);
  57. list_for_each_entry(data, &decoder_list, list) {
  58. if (data->ir_dev == ir_dev)
  59. break;
  60. }
  61. spin_unlock(&decoder_lock);
  62. return data;
  63. }
  64. static ssize_t store_enabled(struct device *d,
  65. struct device_attribute *mattr,
  66. const char *buf,
  67. size_t len)
  68. {
  69. unsigned long value;
  70. struct ir_input_dev *ir_dev = dev_get_drvdata(d);
  71. struct decoder_data *data = get_decoder_data(ir_dev);
  72. if (!data)
  73. return -EINVAL;
  74. if (strict_strtoul(buf, 10, &value) || value > 1)
  75. return -EINVAL;
  76. data->enabled = value;
  77. return len;
  78. }
  79. static ssize_t show_enabled(struct device *d,
  80. struct device_attribute *mattr, char *buf)
  81. {
  82. struct ir_input_dev *ir_dev = dev_get_drvdata(d);
  83. struct decoder_data *data = get_decoder_data(ir_dev);
  84. if (!data)
  85. return -EINVAL;
  86. if (data->enabled)
  87. return sprintf(buf, "1\n");
  88. else
  89. return sprintf(buf, "0\n");
  90. }
  91. static DEVICE_ATTR(enabled, S_IRUGO | S_IWUSR, show_enabled, store_enabled);
  92. static struct attribute *decoder_attributes[] = {
  93. &dev_attr_enabled.attr,
  94. NULL
  95. };
  96. static struct attribute_group decoder_attribute_group = {
  97. .name = "nec_decoder",
  98. .attrs = decoder_attributes,
  99. };
  100. /**
  101. * ir_nec_decode() - Decode one NEC pulse or space
  102. * @input_dev: the struct input_dev descriptor of the device
  103. * @duration: the struct ir_raw_event descriptor of the pulse/space
  104. *
  105. * This function returns -EINVAL if the pulse violates the state machine
  106. */
  107. static int ir_nec_decode(struct input_dev *input_dev, struct ir_raw_event ev)
  108. {
  109. struct decoder_data *data;
  110. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  111. u32 scancode;
  112. u8 address, not_address, command, not_command;
  113. data = get_decoder_data(ir_dev);
  114. if (!data)
  115. return -EINVAL;
  116. if (!data->enabled)
  117. return 0;
  118. if (IS_RESET(ev)) {
  119. data->state = STATE_INACTIVE;
  120. return 0;
  121. }
  122. IR_dprintk(2, "NEC decode started at state %d (%uus %s)\n",
  123. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  124. switch (data->state) {
  125. case STATE_INACTIVE:
  126. if (!ev.pulse)
  127. break;
  128. if (!eq_margin(ev.duration, NEC_HEADER_PULSE, NEC_UNIT / 2) &&
  129. !eq_margin(ev.duration, NECX_HEADER_PULSE, NEC_UNIT / 2))
  130. break;
  131. data->count = 0;
  132. data->state = STATE_HEADER_SPACE;
  133. return 0;
  134. case STATE_HEADER_SPACE:
  135. if (ev.pulse)
  136. break;
  137. if (eq_margin(ev.duration, NEC_HEADER_SPACE, NEC_UNIT / 2)) {
  138. data->state = STATE_BIT_PULSE;
  139. return 0;
  140. } else if (eq_margin(ev.duration, NEC_REPEAT_SPACE, NEC_UNIT / 2)) {
  141. ir_repeat(input_dev);
  142. IR_dprintk(1, "Repeat last key\n");
  143. data->state = STATE_TRAILER_PULSE;
  144. return 0;
  145. }
  146. break;
  147. case STATE_BIT_PULSE:
  148. if (!ev.pulse)
  149. break;
  150. if (!eq_margin(ev.duration, NEC_BIT_PULSE, NEC_UNIT / 2))
  151. break;
  152. data->state = STATE_BIT_SPACE;
  153. return 0;
  154. case STATE_BIT_SPACE:
  155. if (ev.pulse)
  156. break;
  157. data->nec_bits <<= 1;
  158. if (eq_margin(ev.duration, NEC_BIT_1_SPACE, NEC_UNIT / 2))
  159. data->nec_bits |= 1;
  160. else if (!eq_margin(ev.duration, NEC_BIT_0_SPACE, NEC_UNIT / 2))
  161. break;
  162. data->count++;
  163. if (data->count == NEC_NBITS)
  164. data->state = STATE_TRAILER_PULSE;
  165. else
  166. data->state = STATE_BIT_PULSE;
  167. return 0;
  168. case STATE_TRAILER_PULSE:
  169. if (!ev.pulse)
  170. break;
  171. if (!eq_margin(ev.duration, NEC_TRAILER_PULSE, NEC_UNIT / 2))
  172. break;
  173. data->state = STATE_TRAILER_SPACE;
  174. return 0;
  175. case STATE_TRAILER_SPACE:
  176. if (ev.pulse)
  177. break;
  178. if (!geq_margin(ev.duration, NEC_TRAILER_SPACE, NEC_UNIT / 2))
  179. break;
  180. address = bitrev8((data->nec_bits >> 24) & 0xff);
  181. not_address = bitrev8((data->nec_bits >> 16) & 0xff);
  182. command = bitrev8((data->nec_bits >> 8) & 0xff);
  183. not_command = bitrev8((data->nec_bits >> 0) & 0xff);
  184. if ((command ^ not_command) != 0xff) {
  185. IR_dprintk(1, "NEC checksum error: received 0x%08x\n",
  186. data->nec_bits);
  187. break;
  188. }
  189. if ((address ^ not_address) != 0xff) {
  190. /* Extended NEC */
  191. scancode = address << 16 |
  192. not_address << 8 |
  193. command;
  194. IR_dprintk(1, "NEC (Ext) scancode 0x%06x\n", scancode);
  195. } else {
  196. /* Normal NEC */
  197. scancode = address << 8 | command;
  198. IR_dprintk(1, "NEC scancode 0x%04x\n", scancode);
  199. }
  200. ir_keydown(input_dev, scancode, 0);
  201. data->state = STATE_INACTIVE;
  202. return 0;
  203. }
  204. IR_dprintk(1, "NEC decode failed at state %d (%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_nec_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_nec_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 nec_handler = {
  243. .decode = ir_nec_decode,
  244. .raw_register = ir_nec_register,
  245. .raw_unregister = ir_nec_unregister,
  246. };
  247. static int __init ir_nec_decode_init(void)
  248. {
  249. ir_raw_handler_register(&nec_handler);
  250. printk(KERN_INFO "IR NEC protocol handler initialized\n");
  251. return 0;
  252. }
  253. static void __exit ir_nec_decode_exit(void)
  254. {
  255. ir_raw_handler_unregister(&nec_handler);
  256. }
  257. module_init(ir_nec_decode_init);
  258. module_exit(ir_nec_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("NEC IR protocol decoder");