ir-nec-decoder.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 <media/ir-core.h>
  15. /* Start time: 4.5 ms + 560 us of the next pulse */
  16. #define MIN_START_TIME (3900000 + 560000)
  17. #define MAX_START_TIME (5100000 + 560000)
  18. /* Bit 1 time: 2.25ms us */
  19. #define MIN_BIT1_TIME 2050000
  20. #define MAX_BIT1_TIME 2450000
  21. /* Bit 0 time: 1.12ms us */
  22. #define MIN_BIT0_TIME 920000
  23. #define MAX_BIT0_TIME 1320000
  24. /* Total IR code is 110 ms, including the 9 ms for the start pulse */
  25. #define MAX_NEC_TIME 4000000
  26. /* Total IR code is 110 ms, including the 9 ms for the start pulse */
  27. #define MIN_REPEAT_TIME 99000000
  28. #define MAX_REPEAT_TIME 112000000
  29. /* Repeat time: 2.25ms us */
  30. #define MIN_REPEAT_START_TIME 2050000
  31. #define MAX_REPEAT_START_TIME 3000000
  32. #define REPEAT_TIME 240 /* ms */
  33. /* Used to register nec_decoder clients */
  34. static LIST_HEAD(decoder_list);
  35. static spinlock_t decoder_lock;
  36. struct decoder_data {
  37. struct list_head list;
  38. struct ir_input_dev *ir_dev;
  39. int enabled:1;
  40. };
  41. /**
  42. * get_decoder_data() - gets decoder data
  43. * @input_dev: input device
  44. *
  45. * Returns the struct decoder_data that corresponds to a device
  46. */
  47. static struct decoder_data *get_decoder_data(struct ir_input_dev *ir_dev)
  48. {
  49. struct decoder_data *data = NULL;
  50. spin_lock(&decoder_lock);
  51. list_for_each_entry(data, &decoder_list, list) {
  52. if (data->ir_dev == ir_dev)
  53. break;
  54. }
  55. spin_unlock(&decoder_lock);
  56. return data;
  57. }
  58. static ssize_t store_enabled(struct device *d,
  59. struct device_attribute *mattr,
  60. const char *buf,
  61. size_t len)
  62. {
  63. unsigned long value;
  64. struct ir_input_dev *ir_dev = dev_get_drvdata(d);
  65. struct decoder_data *data = get_decoder_data(ir_dev);
  66. if (!data)
  67. return -EINVAL;
  68. if (strict_strtoul(buf, 10, &value) || value > 1)
  69. return -EINVAL;
  70. data->enabled = value;
  71. return len;
  72. }
  73. static ssize_t show_enabled(struct device *d,
  74. struct device_attribute *mattr, char *buf)
  75. {
  76. struct ir_input_dev *ir_dev = dev_get_drvdata(d);
  77. struct decoder_data *data = get_decoder_data(ir_dev);
  78. if (!data)
  79. return -EINVAL;
  80. if (data->enabled)
  81. return sprintf(buf, "1\n");
  82. else
  83. return sprintf(buf, "0\n");
  84. }
  85. static DEVICE_ATTR(enabled, S_IRUGO | S_IWUSR, show_enabled, store_enabled);
  86. static struct attribute *decoder_attributes[] = {
  87. &dev_attr_enabled.attr,
  88. NULL
  89. };
  90. static struct attribute_group decoder_attribute_group = {
  91. .name = "nec_decoder",
  92. .attrs = decoder_attributes,
  93. };
  94. /** is_repeat - Check if it is a NEC repeat event
  95. * @input_dev: the struct input_dev descriptor of the device
  96. * @pos: the position of the first event
  97. * @len: the length of the buffer
  98. */
  99. static int is_repeat(struct ir_raw_event *evs, int len, int pos)
  100. {
  101. if ((evs[pos].delta.tv_nsec < MIN_REPEAT_START_TIME) ||
  102. (evs[pos].delta.tv_nsec > MAX_REPEAT_START_TIME))
  103. return 0;
  104. if (++pos >= len)
  105. return 0;
  106. if ((evs[pos].delta.tv_nsec < MIN_REPEAT_TIME) ||
  107. (evs[pos].delta.tv_nsec > MAX_REPEAT_TIME))
  108. return 0;
  109. return 1;
  110. }
  111. /**
  112. * __ir_nec_decode() - Decode one NEC pulsecode
  113. * @input_dev: the struct input_dev descriptor of the device
  114. * @evs: event array with type/duration of pulse/space
  115. * @len: length of the array
  116. * @pos: position to start seeking for a code
  117. * This function returns -EINVAL if no pulse got decoded,
  118. * 0 if buffer is empty and 1 if one keycode were handled.
  119. */
  120. static int __ir_nec_decode(struct input_dev *input_dev,
  121. struct ir_raw_event *evs,
  122. int len, int *pos)
  123. {
  124. struct ir_input_dev *ir = input_get_drvdata(input_dev);
  125. int count = -1;
  126. int ircode = 0, not_code = 0;
  127. /* Be sure that the first event is an start one and is a pulse */
  128. for (; *pos < len; (*pos)++) {
  129. /* Very long delays are considered as start events */
  130. if (evs[*pos].delta.tv_nsec > MAX_NEC_TIME)
  131. break;
  132. if (evs[*pos].type & IR_START_EVENT)
  133. break;
  134. IR_dprintk(1, "%luus: Spurious NEC %s\n",
  135. (evs[*pos].delta.tv_nsec + 500) / 1000,
  136. (evs[*pos].type & IR_SPACE) ? "space" : "pulse");
  137. }
  138. if (*pos >= len)
  139. return 0;
  140. (*pos)++; /* First event doesn't contain data */
  141. if (evs[*pos].type != IR_PULSE)
  142. goto err;
  143. /* Check if it is a NEC repeat event */
  144. if (is_repeat(evs, len, *pos)) {
  145. *pos += 2;
  146. if (ir->keypressed) {
  147. ir_repeat(input_dev);
  148. IR_dprintk(1, "NEC repeat event\n");
  149. return 1;
  150. } else {
  151. IR_dprintk(1, "missing NEC repeat event\n");
  152. return 0;
  153. }
  154. }
  155. /* First space should have 4.5 ms otherwise is not NEC protocol */
  156. if ((evs[*pos].delta.tv_nsec < MIN_START_TIME) ||
  157. (evs[*pos].delta.tv_nsec > MAX_START_TIME))
  158. goto err;
  159. count = 0;
  160. for ((*pos)++; *pos < len; (*pos)++) {
  161. int bit;
  162. if ((evs[*pos].delta.tv_nsec > MIN_BIT1_TIME) &&
  163. (evs[*pos].delta.tv_nsec < MAX_BIT1_TIME))
  164. bit = 1;
  165. else if ((evs[*pos].delta.tv_nsec > MIN_BIT0_TIME) &&
  166. (evs[*pos].delta.tv_nsec < MAX_BIT0_TIME))
  167. bit = 0;
  168. else
  169. goto err;
  170. if (bit) {
  171. int shift = count;
  172. /* Address first, then command */
  173. if (shift < 8) {
  174. shift += 8;
  175. ircode |= 1 << shift;
  176. } else if (shift < 16) {
  177. not_code |= 1 << shift;
  178. } else if (shift < 24) {
  179. shift -= 16;
  180. ircode |= 1 << shift;
  181. } else {
  182. shift -= 24;
  183. not_code |= 1 << shift;
  184. }
  185. }
  186. if (++count == 32)
  187. break;
  188. }
  189. (*pos)++;
  190. /*
  191. * Fixme: may need to accept Extended NEC protocol?
  192. */
  193. if ((ircode & ~not_code) != ircode) {
  194. IR_dprintk(1, "NEC checksum error: code 0x%04x, not-code 0x%04x\n",
  195. ircode, not_code);
  196. return -EINVAL;
  197. }
  198. IR_dprintk(1, "NEC scancode 0x%04x\n", ircode);
  199. ir_keydown(input_dev, ircode, 0);
  200. return 1;
  201. err:
  202. IR_dprintk(1, "NEC decoded failed at bit %d (%s) while decoding %luus time\n",
  203. count,
  204. (evs[*pos].type & IR_SPACE) ? "space" : "pulse",
  205. (evs[*pos].delta.tv_nsec + 500) / 1000);
  206. return -EINVAL;
  207. }
  208. /**
  209. * __ir_nec_decode() - Decodes all NEC pulsecodes on a given array
  210. * @input_dev: the struct input_dev descriptor of the device
  211. * @evs: event array with type/duration of pulse/space
  212. * @len: length of the array
  213. * This function returns the number of decoded pulses
  214. */
  215. static int ir_nec_decode(struct input_dev *input_dev,
  216. struct ir_raw_event *evs,
  217. int len)
  218. {
  219. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  220. struct decoder_data *data;
  221. int pos = 0;
  222. int rc = 0;
  223. data = get_decoder_data(ir_dev);
  224. if (!data || !data->enabled)
  225. return 0;
  226. while (pos < len) {
  227. if (__ir_nec_decode(input_dev, evs, len, &pos) > 0)
  228. rc++;
  229. }
  230. return rc;
  231. }
  232. static int ir_nec_register(struct input_dev *input_dev)
  233. {
  234. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  235. struct decoder_data *data;
  236. int rc;
  237. rc = sysfs_create_group(&ir_dev->dev.kobj, &decoder_attribute_group);
  238. if (rc < 0)
  239. return rc;
  240. data = kzalloc(sizeof(*data), GFP_KERNEL);
  241. if (!data) {
  242. sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group);
  243. return -ENOMEM;
  244. }
  245. data->ir_dev = ir_dev;
  246. data->enabled = 1;
  247. spin_lock(&decoder_lock);
  248. list_add_tail(&data->list, &decoder_list);
  249. spin_unlock(&decoder_lock);
  250. return 0;
  251. }
  252. static int ir_nec_unregister(struct input_dev *input_dev)
  253. {
  254. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  255. static struct decoder_data *data;
  256. data = get_decoder_data(ir_dev);
  257. if (!data)
  258. return 0;
  259. sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group);
  260. spin_lock(&decoder_lock);
  261. list_del(&data->list);
  262. spin_unlock(&decoder_lock);
  263. return 0;
  264. }
  265. static struct ir_raw_handler nec_handler = {
  266. .decode = ir_nec_decode,
  267. .raw_register = ir_nec_register,
  268. .raw_unregister = ir_nec_unregister,
  269. };
  270. static int __init ir_nec_decode_init(void)
  271. {
  272. ir_raw_handler_register(&nec_handler);
  273. printk(KERN_INFO "IR NEC protocol handler initialized\n");
  274. return 0;
  275. }
  276. static void __exit ir_nec_decode_exit(void)
  277. {
  278. ir_raw_handler_unregister(&nec_handler);
  279. }
  280. module_init(ir_nec_decode_init);
  281. module_exit(ir_nec_decode_exit);
  282. MODULE_LICENSE("GPL");
  283. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  284. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  285. MODULE_DESCRIPTION("NEC IR protocol decoder");