ir-nec-decoder.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. #define NEC_UNIT 559979 /* ns */
  16. #define NEC_HEADER_MARK (16 * NEC_UNIT)
  17. #define NEC_HEADER_SPACE (8 * NEC_UNIT)
  18. #define NEC_REPEAT_SPACE (4 * NEC_UNIT)
  19. #define NEC_MARK (NEC_UNIT)
  20. #define NEC_0_SYMBOL (NEC_UNIT)
  21. #define NEC_1_SYMBOL (3 * NEC_UNIT)
  22. /* Start time: 4.5 ms + 560 us of the next pulse */
  23. #define MIN_START_TIME (3900000 + 560000)
  24. #define MAX_START_TIME (5100000 + 560000)
  25. /* Bit 1 time: 2.25ms us */
  26. #define MIN_BIT1_TIME 2050000
  27. #define MAX_BIT1_TIME 2450000
  28. /* Bit 0 time: 1.12ms us */
  29. #define MIN_BIT0_TIME 920000
  30. #define MAX_BIT0_TIME 1320000
  31. /* Total IR code is 110 ms, including the 9 ms for the start pulse */
  32. #define MAX_NEC_TIME 4000000
  33. /* Total IR code is 110 ms, including the 9 ms for the start pulse */
  34. #define MIN_REPEAT_TIME 99000000
  35. #define MAX_REPEAT_TIME 112000000
  36. /* Repeat time: 2.25ms us */
  37. #define MIN_REPEAT_START_TIME 2050000
  38. #define MAX_REPEAT_START_TIME 3000000
  39. #define REPEAT_TIME 240 /* ms */
  40. /* Used to register nec_decoder clients */
  41. static LIST_HEAD(decoder_list);
  42. static spinlock_t decoder_lock;
  43. enum nec_state {
  44. STATE_INACTIVE,
  45. STATE_HEADER_MARK,
  46. STATE_HEADER_SPACE,
  47. STATE_MARK,
  48. STATE_SPACE,
  49. STATE_TRAILER_MARK,
  50. STATE_TRAILER_SPACE,
  51. };
  52. struct nec_code {
  53. u8 address;
  54. u8 not_address;
  55. u8 command;
  56. u8 not_command;
  57. };
  58. struct decoder_data {
  59. struct list_head list;
  60. struct ir_input_dev *ir_dev;
  61. int enabled:1;
  62. /* State machine control */
  63. enum nec_state state;
  64. struct nec_code nec_code;
  65. unsigned count;
  66. };
  67. /**
  68. * get_decoder_data() - gets decoder data
  69. * @input_dev: input device
  70. *
  71. * Returns the struct decoder_data that corresponds to a device
  72. */
  73. static struct decoder_data *get_decoder_data(struct ir_input_dev *ir_dev)
  74. {
  75. struct decoder_data *data = NULL;
  76. spin_lock(&decoder_lock);
  77. list_for_each_entry(data, &decoder_list, list) {
  78. if (data->ir_dev == ir_dev)
  79. break;
  80. }
  81. spin_unlock(&decoder_lock);
  82. return data;
  83. }
  84. static ssize_t store_enabled(struct device *d,
  85. struct device_attribute *mattr,
  86. const char *buf,
  87. size_t len)
  88. {
  89. unsigned long value;
  90. struct ir_input_dev *ir_dev = dev_get_drvdata(d);
  91. struct decoder_data *data = get_decoder_data(ir_dev);
  92. if (!data)
  93. return -EINVAL;
  94. if (strict_strtoul(buf, 10, &value) || value > 1)
  95. return -EINVAL;
  96. data->enabled = value;
  97. return len;
  98. }
  99. static ssize_t show_enabled(struct device *d,
  100. struct device_attribute *mattr, char *buf)
  101. {
  102. struct ir_input_dev *ir_dev = dev_get_drvdata(d);
  103. struct decoder_data *data = get_decoder_data(ir_dev);
  104. if (!data)
  105. return -EINVAL;
  106. if (data->enabled)
  107. return sprintf(buf, "1\n");
  108. else
  109. return sprintf(buf, "0\n");
  110. }
  111. static DEVICE_ATTR(enabled, S_IRUGO | S_IWUSR, show_enabled, store_enabled);
  112. static struct attribute *decoder_attributes[] = {
  113. &dev_attr_enabled.attr,
  114. NULL
  115. };
  116. static struct attribute_group decoder_attribute_group = {
  117. .name = "nec_decoder",
  118. .attrs = decoder_attributes,
  119. };
  120. /**
  121. * handle_event() - Decode one NEC pulse or space
  122. * @input_dev: the struct input_dev descriptor of the device
  123. * @ev: event array with type/duration of pulse/space
  124. *
  125. * This function returns -EINVAL if the pulse violates the state machine
  126. */
  127. static int handle_event(struct input_dev *input_dev,
  128. struct ir_raw_event *ev)
  129. {
  130. struct decoder_data *data;
  131. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  132. int bit, last_bit;
  133. data = get_decoder_data(ir_dev);
  134. if (!data)
  135. return -EINVAL;
  136. /* Except for the initial event, what matters is the previous bit */
  137. bit = (ev->type & IR_PULSE) ? 1 : 0;
  138. last_bit = !bit;
  139. /* Discards spurious space last_bits when inactive */
  140. /* Very long delays are considered as start events */
  141. if (ev->delta.tv_nsec > NEC_HEADER_MARK + NEC_HEADER_SPACE - NEC_UNIT / 2)
  142. data->state = STATE_INACTIVE;
  143. if (ev->type & IR_START_EVENT)
  144. data->state = STATE_INACTIVE;
  145. switch (data->state) {
  146. case STATE_INACTIVE:
  147. if (!bit) /* PULSE marks the start event */
  148. return 0;
  149. data->count = 0;
  150. data->state = STATE_HEADER_MARK;
  151. memset (&data->nec_code, 0, sizeof(data->nec_code));
  152. return 0;
  153. case STATE_HEADER_MARK:
  154. if (!last_bit)
  155. goto err;
  156. if (ev->delta.tv_nsec < NEC_HEADER_MARK - 6 * NEC_UNIT)
  157. goto err;
  158. data->state = STATE_HEADER_SPACE;
  159. return 0;
  160. case STATE_HEADER_SPACE:
  161. if (last_bit)
  162. goto err;
  163. if (ev->delta.tv_nsec >= NEC_HEADER_SPACE - NEC_UNIT / 2) {
  164. data->state = STATE_MARK;
  165. return 0;
  166. }
  167. if (ev->delta.tv_nsec >= NEC_REPEAT_SPACE - NEC_UNIT / 2) {
  168. ir_repeat(input_dev);
  169. IR_dprintk(1, "Repeat last key\n");
  170. data->state = STATE_TRAILER_MARK;
  171. return 0;
  172. }
  173. goto err;
  174. case STATE_MARK:
  175. if (!last_bit)
  176. goto err;
  177. if ((ev->delta.tv_nsec > NEC_MARK + NEC_UNIT / 2) ||
  178. (ev->delta.tv_nsec < NEC_MARK - NEC_UNIT / 2))
  179. goto err;
  180. data->state = STATE_SPACE;
  181. return 0;
  182. case STATE_SPACE:
  183. if (last_bit)
  184. goto err;
  185. if ((ev->delta.tv_nsec >= NEC_0_SYMBOL - NEC_UNIT / 2) &&
  186. (ev->delta.tv_nsec < NEC_0_SYMBOL + NEC_UNIT / 2))
  187. bit = 0;
  188. else if ((ev->delta.tv_nsec >= NEC_1_SYMBOL - NEC_UNIT / 2) &&
  189. (ev->delta.tv_nsec < NEC_1_SYMBOL + NEC_UNIT / 2))
  190. bit = 1;
  191. else {
  192. IR_dprintk(1, "Decode failed at %d-th bit (%s) @%luus\n",
  193. data->count,
  194. last_bit ? "pulse" : "space",
  195. (ev->delta.tv_nsec + 500) / 1000);
  196. goto err2;
  197. }
  198. /* Ok, we've got a valid bit. proccess it */
  199. if (bit) {
  200. int shift = data->count;
  201. /*
  202. * NEC transmit bytes on this temporal order:
  203. * address | not address | command | not command
  204. */
  205. if (shift < 8) {
  206. data->nec_code.address |= 1 << shift;
  207. } else if (shift < 16) {
  208. data->nec_code.not_address |= 1 << (shift - 8);
  209. } else if (shift < 24) {
  210. data->nec_code.command |= 1 << (shift - 16);
  211. } else {
  212. data->nec_code.not_command |= 1 << (shift - 24);
  213. }
  214. }
  215. if (++data->count == 32) {
  216. u32 scancode;
  217. /*
  218. * Fixme: may need to accept Extended NEC protocol?
  219. */
  220. if ((data->nec_code.command ^ data->nec_code.not_command) != 0xff)
  221. goto checksum_err;
  222. if ((data->nec_code.address ^ data->nec_code.not_address) != 0xff) {
  223. /* Extended NEC */
  224. scancode = data->nec_code.address << 16 |
  225. data->nec_code.not_address << 8 |
  226. data->nec_code.command;
  227. IR_dprintk(1, "NEC scancode 0x%06x\n", scancode);
  228. } else {
  229. /* normal NEC */
  230. scancode = data->nec_code.address << 8 |
  231. data->nec_code.command;
  232. IR_dprintk(1, "NEC scancode 0x%04x\n", scancode);
  233. }
  234. ir_keydown(input_dev, scancode, 0);
  235. data->state = STATE_TRAILER_MARK;
  236. } else
  237. data->state = STATE_MARK;
  238. return 0;
  239. case STATE_TRAILER_MARK:
  240. if (!last_bit)
  241. goto err;
  242. data->state = STATE_TRAILER_SPACE;
  243. return 0;
  244. case STATE_TRAILER_SPACE:
  245. if (last_bit)
  246. goto err;
  247. data->state = STATE_INACTIVE;
  248. return 0;
  249. }
  250. err:
  251. IR_dprintk(1, "NEC decoded failed at state %d (%s) @ %luus\n",
  252. data->state,
  253. bit ? "pulse" : "space",
  254. (ev->delta.tv_nsec + 500) / 1000);
  255. err2:
  256. data->state = STATE_INACTIVE;
  257. return -EINVAL;
  258. checksum_err:
  259. data->state = STATE_INACTIVE;
  260. IR_dprintk(1, "NEC checksum error: received 0x%02x%02x%02x%02x\n",
  261. data->nec_code.address,
  262. data->nec_code.not_address,
  263. data->nec_code.command,
  264. data->nec_code.not_command);
  265. return -EINVAL;
  266. }
  267. /**
  268. * ir_nec_decode() - Decodes all NEC pulsecodes on a given array
  269. * @input_dev: the struct input_dev descriptor of the device
  270. * @evs: event array with type/duration of pulse/space
  271. * @len: length of the array
  272. * This function returns the number of decoded pulses
  273. */
  274. static int ir_nec_decode(struct input_dev *input_dev,
  275. struct ir_raw_event *evs,
  276. int len)
  277. {
  278. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  279. struct decoder_data *data;
  280. int pos = 0;
  281. int rc = 0;
  282. data = get_decoder_data(ir_dev);
  283. if (!data || !data->enabled)
  284. return 0;
  285. for (pos = 0; pos < len; pos++)
  286. handle_event(input_dev, &evs[pos]);
  287. return rc;
  288. }
  289. static int ir_nec_register(struct input_dev *input_dev)
  290. {
  291. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  292. struct decoder_data *data;
  293. int rc;
  294. rc = sysfs_create_group(&ir_dev->dev.kobj, &decoder_attribute_group);
  295. if (rc < 0)
  296. return rc;
  297. data = kzalloc(sizeof(*data), GFP_KERNEL);
  298. if (!data) {
  299. sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group);
  300. return -ENOMEM;
  301. }
  302. data->ir_dev = ir_dev;
  303. data->enabled = 1;
  304. spin_lock(&decoder_lock);
  305. list_add_tail(&data->list, &decoder_list);
  306. spin_unlock(&decoder_lock);
  307. return 0;
  308. }
  309. static int ir_nec_unregister(struct input_dev *input_dev)
  310. {
  311. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  312. static struct decoder_data *data;
  313. data = get_decoder_data(ir_dev);
  314. if (!data)
  315. return 0;
  316. sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group);
  317. spin_lock(&decoder_lock);
  318. list_del(&data->list);
  319. spin_unlock(&decoder_lock);
  320. return 0;
  321. }
  322. static struct ir_raw_handler nec_handler = {
  323. .decode = ir_nec_decode,
  324. .raw_register = ir_nec_register,
  325. .raw_unregister = ir_nec_unregister,
  326. };
  327. static int __init ir_nec_decode_init(void)
  328. {
  329. ir_raw_handler_register(&nec_handler);
  330. printk(KERN_INFO "IR NEC protocol handler initialized\n");
  331. return 0;
  332. }
  333. static void __exit ir_nec_decode_exit(void)
  334. {
  335. ir_raw_handler_unregister(&nec_handler);
  336. }
  337. module_init(ir_nec_decode_init);
  338. module_exit(ir_nec_decode_exit);
  339. MODULE_LICENSE("GPL");
  340. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  341. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  342. MODULE_DESCRIPTION("NEC IR protocol decoder");