ir-nec-decoder.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. /* State machine control */
  42. enum nec_state state;
  43. u32 nec_bits;
  44. unsigned count;
  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. /**
  64. * ir_nec_decode() - Decode one NEC pulse or space
  65. * @input_dev: the struct input_dev descriptor of the device
  66. * @duration: the struct ir_raw_event descriptor of the pulse/space
  67. *
  68. * This function returns -EINVAL if the pulse violates the state machine
  69. */
  70. static int ir_nec_decode(struct input_dev *input_dev, struct ir_raw_event ev)
  71. {
  72. struct decoder_data *data;
  73. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  74. u32 scancode;
  75. u8 address, not_address, command, not_command;
  76. data = get_decoder_data(ir_dev);
  77. if (!data)
  78. return -EINVAL;
  79. if (!(ir_dev->raw->enabled_protocols & IR_TYPE_NEC))
  80. return 0;
  81. if (IS_RESET(ev)) {
  82. data->state = STATE_INACTIVE;
  83. return 0;
  84. }
  85. IR_dprintk(2, "NEC decode started at state %d (%uus %s)\n",
  86. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  87. switch (data->state) {
  88. case STATE_INACTIVE:
  89. if (!ev.pulse)
  90. break;
  91. if (!eq_margin(ev.duration, NEC_HEADER_PULSE, NEC_UNIT / 2) &&
  92. !eq_margin(ev.duration, NECX_HEADER_PULSE, NEC_UNIT / 2))
  93. break;
  94. data->count = 0;
  95. data->state = STATE_HEADER_SPACE;
  96. return 0;
  97. case STATE_HEADER_SPACE:
  98. if (ev.pulse)
  99. break;
  100. if (eq_margin(ev.duration, NEC_HEADER_SPACE, NEC_UNIT / 2)) {
  101. data->state = STATE_BIT_PULSE;
  102. return 0;
  103. } else if (eq_margin(ev.duration, NEC_REPEAT_SPACE, NEC_UNIT / 2)) {
  104. ir_repeat(input_dev);
  105. IR_dprintk(1, "Repeat last key\n");
  106. data->state = STATE_TRAILER_PULSE;
  107. return 0;
  108. }
  109. break;
  110. case STATE_BIT_PULSE:
  111. if (!ev.pulse)
  112. break;
  113. if (!eq_margin(ev.duration, NEC_BIT_PULSE, NEC_UNIT / 2))
  114. break;
  115. data->state = STATE_BIT_SPACE;
  116. return 0;
  117. case STATE_BIT_SPACE:
  118. if (ev.pulse)
  119. break;
  120. data->nec_bits <<= 1;
  121. if (eq_margin(ev.duration, NEC_BIT_1_SPACE, NEC_UNIT / 2))
  122. data->nec_bits |= 1;
  123. else if (!eq_margin(ev.duration, NEC_BIT_0_SPACE, NEC_UNIT / 2))
  124. break;
  125. data->count++;
  126. if (data->count == NEC_NBITS)
  127. data->state = STATE_TRAILER_PULSE;
  128. else
  129. data->state = STATE_BIT_PULSE;
  130. return 0;
  131. case STATE_TRAILER_PULSE:
  132. if (!ev.pulse)
  133. break;
  134. if (!eq_margin(ev.duration, NEC_TRAILER_PULSE, NEC_UNIT / 2))
  135. break;
  136. data->state = STATE_TRAILER_SPACE;
  137. return 0;
  138. case STATE_TRAILER_SPACE:
  139. if (ev.pulse)
  140. break;
  141. if (!geq_margin(ev.duration, NEC_TRAILER_SPACE, NEC_UNIT / 2))
  142. break;
  143. address = bitrev8((data->nec_bits >> 24) & 0xff);
  144. not_address = bitrev8((data->nec_bits >> 16) & 0xff);
  145. command = bitrev8((data->nec_bits >> 8) & 0xff);
  146. not_command = bitrev8((data->nec_bits >> 0) & 0xff);
  147. if ((command ^ not_command) != 0xff) {
  148. IR_dprintk(1, "NEC checksum error: received 0x%08x\n",
  149. data->nec_bits);
  150. break;
  151. }
  152. if ((address ^ not_address) != 0xff) {
  153. /* Extended NEC */
  154. scancode = address << 16 |
  155. not_address << 8 |
  156. command;
  157. IR_dprintk(1, "NEC (Ext) scancode 0x%06x\n", scancode);
  158. } else {
  159. /* Normal NEC */
  160. scancode = address << 8 | command;
  161. IR_dprintk(1, "NEC scancode 0x%04x\n", scancode);
  162. }
  163. ir_keydown(input_dev, scancode, 0);
  164. data->state = STATE_INACTIVE;
  165. return 0;
  166. }
  167. IR_dprintk(1, "NEC decode failed at state %d (%uus %s)\n",
  168. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  169. data->state = STATE_INACTIVE;
  170. return -EINVAL;
  171. }
  172. static int ir_nec_register(struct input_dev *input_dev)
  173. {
  174. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  175. struct decoder_data *data;
  176. data = kzalloc(sizeof(*data), GFP_KERNEL);
  177. if (!data)
  178. return -ENOMEM;
  179. data->ir_dev = ir_dev;
  180. spin_lock(&decoder_lock);
  181. list_add_tail(&data->list, &decoder_list);
  182. spin_unlock(&decoder_lock);
  183. return 0;
  184. }
  185. static int ir_nec_unregister(struct input_dev *input_dev)
  186. {
  187. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  188. static struct decoder_data *data;
  189. data = get_decoder_data(ir_dev);
  190. if (!data)
  191. return 0;
  192. spin_lock(&decoder_lock);
  193. list_del(&data->list);
  194. spin_unlock(&decoder_lock);
  195. return 0;
  196. }
  197. static struct ir_raw_handler nec_handler = {
  198. .protocols = IR_TYPE_NEC,
  199. .decode = ir_nec_decode,
  200. .raw_register = ir_nec_register,
  201. .raw_unregister = ir_nec_unregister,
  202. };
  203. static int __init ir_nec_decode_init(void)
  204. {
  205. ir_raw_handler_register(&nec_handler);
  206. printk(KERN_INFO "IR NEC protocol handler initialized\n");
  207. return 0;
  208. }
  209. static void __exit ir_nec_decode_exit(void)
  210. {
  211. ir_raw_handler_unregister(&nec_handler);
  212. }
  213. module_init(ir_nec_decode_init);
  214. module_exit(ir_nec_decode_exit);
  215. MODULE_LICENSE("GPL");
  216. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  217. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  218. MODULE_DESCRIPTION("NEC IR protocol decoder");