ir-nec-decoder.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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_NBITS 32
  16. #define NEC_UNIT 559979 /* ns */
  17. #define NEC_HEADER_MARK (16 * NEC_UNIT)
  18. #define NEC_HEADER_SPACE (8 * NEC_UNIT)
  19. #define NEC_REPEAT_SPACE (4 * NEC_UNIT)
  20. #define NEC_MARK (NEC_UNIT)
  21. #define NEC_0_SPACE (NEC_UNIT)
  22. #define NEC_1_SPACE (3 * NEC_UNIT)
  23. /* Used to register nec_decoder clients */
  24. static LIST_HEAD(decoder_list);
  25. static spinlock_t decoder_lock;
  26. enum nec_state {
  27. STATE_INACTIVE,
  28. STATE_HEADER_MARK,
  29. STATE_HEADER_SPACE,
  30. STATE_MARK,
  31. STATE_SPACE,
  32. STATE_TRAILER_MARK,
  33. STATE_TRAILER_SPACE,
  34. };
  35. struct nec_code {
  36. u8 address;
  37. u8 not_address;
  38. u8 command;
  39. u8 not_command;
  40. };
  41. struct decoder_data {
  42. struct list_head list;
  43. struct ir_input_dev *ir_dev;
  44. int enabled:1;
  45. /* State machine control */
  46. enum nec_state state;
  47. struct nec_code nec_code;
  48. unsigned count;
  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 = "nec_decoder",
  101. .attrs = decoder_attributes,
  102. };
  103. /**
  104. * handle_event() - Decode one NEC pulse or space
  105. * @input_dev: the struct input_dev descriptor of the device
  106. * @ev: event array with type/duration of pulse/space
  107. *
  108. * This function returns -EINVAL if the pulse violates the state machine
  109. */
  110. static int handle_event(struct input_dev *input_dev,
  111. struct ir_raw_event *ev)
  112. {
  113. struct decoder_data *data;
  114. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  115. int bit, last_bit;
  116. data = get_decoder_data(ir_dev);
  117. if (!data)
  118. return -EINVAL;
  119. /* Except for the initial event, what matters is the previous bit */
  120. bit = (ev->type & IR_PULSE) ? 1 : 0;
  121. last_bit = !bit;
  122. /* Discards spurious space last_bits when inactive */
  123. /* Very long delays are considered as start events */
  124. if (ev->delta.tv_nsec > NEC_HEADER_MARK + NEC_HEADER_SPACE - NEC_UNIT / 2)
  125. data->state = STATE_INACTIVE;
  126. if (ev->type & IR_START_EVENT)
  127. data->state = STATE_INACTIVE;
  128. switch (data->state) {
  129. case STATE_INACTIVE:
  130. if (!bit) /* PULSE marks the start event */
  131. return 0;
  132. data->count = 0;
  133. data->state = STATE_HEADER_MARK;
  134. memset (&data->nec_code, 0, sizeof(data->nec_code));
  135. return 0;
  136. case STATE_HEADER_MARK:
  137. if (!last_bit)
  138. goto err;
  139. if (ev->delta.tv_nsec < NEC_HEADER_MARK - 6 * NEC_UNIT)
  140. goto err;
  141. data->state = STATE_HEADER_SPACE;
  142. return 0;
  143. case STATE_HEADER_SPACE:
  144. if (last_bit)
  145. goto err;
  146. if (ev->delta.tv_nsec >= NEC_HEADER_SPACE - NEC_UNIT / 2) {
  147. data->state = STATE_MARK;
  148. return 0;
  149. }
  150. if (ev->delta.tv_nsec >= NEC_REPEAT_SPACE - NEC_UNIT / 2) {
  151. ir_repeat(input_dev);
  152. IR_dprintk(1, "Repeat last key\n");
  153. data->state = STATE_TRAILER_MARK;
  154. return 0;
  155. }
  156. goto err;
  157. case STATE_MARK:
  158. if (!last_bit)
  159. goto err;
  160. if ((ev->delta.tv_nsec > NEC_MARK + NEC_UNIT / 2) ||
  161. (ev->delta.tv_nsec < NEC_MARK - NEC_UNIT / 2))
  162. goto err;
  163. data->state = STATE_SPACE;
  164. return 0;
  165. case STATE_SPACE:
  166. if (last_bit)
  167. goto err;
  168. if ((ev->delta.tv_nsec >= NEC_0_SPACE - NEC_UNIT / 2) &&
  169. (ev->delta.tv_nsec < NEC_0_SPACE + NEC_UNIT / 2))
  170. bit = 0;
  171. else if ((ev->delta.tv_nsec >= NEC_1_SPACE - NEC_UNIT / 2) &&
  172. (ev->delta.tv_nsec < NEC_1_SPACE + NEC_UNIT / 2))
  173. bit = 1;
  174. else {
  175. IR_dprintk(1, "Decode failed at %d-th bit (%s) @%luus\n",
  176. data->count,
  177. last_bit ? "pulse" : "space",
  178. (ev->delta.tv_nsec + 500) / 1000);
  179. goto err2;
  180. }
  181. /* Ok, we've got a valid bit. proccess it */
  182. if (bit) {
  183. int shift = data->count;
  184. /*
  185. * NEC transmit bytes on this temporal order:
  186. * address | not address | command | not command
  187. */
  188. if (shift < 8) {
  189. data->nec_code.address |= 1 << shift;
  190. } else if (shift < 16) {
  191. data->nec_code.not_address |= 1 << (shift - 8);
  192. } else if (shift < 24) {
  193. data->nec_code.command |= 1 << (shift - 16);
  194. } else {
  195. data->nec_code.not_command |= 1 << (shift - 24);
  196. }
  197. }
  198. if (++data->count == NEC_NBITS) {
  199. u32 scancode;
  200. /*
  201. * Fixme: may need to accept Extended NEC protocol?
  202. */
  203. if ((data->nec_code.command ^ data->nec_code.not_command) != 0xff)
  204. goto checksum_err;
  205. if ((data->nec_code.address ^ data->nec_code.not_address) != 0xff) {
  206. /* Extended NEC */
  207. scancode = data->nec_code.address << 16 |
  208. data->nec_code.not_address << 8 |
  209. data->nec_code.command;
  210. IR_dprintk(1, "NEC scancode 0x%06x\n", scancode);
  211. } else {
  212. /* normal NEC */
  213. scancode = data->nec_code.address << 8 |
  214. data->nec_code.command;
  215. IR_dprintk(1, "NEC scancode 0x%04x\n", scancode);
  216. }
  217. ir_keydown(input_dev, scancode, 0);
  218. data->state = STATE_TRAILER_MARK;
  219. } else
  220. data->state = STATE_MARK;
  221. return 0;
  222. case STATE_TRAILER_MARK:
  223. if (!last_bit)
  224. goto err;
  225. data->state = STATE_TRAILER_SPACE;
  226. return 0;
  227. case STATE_TRAILER_SPACE:
  228. if (last_bit)
  229. goto err;
  230. data->state = STATE_INACTIVE;
  231. return 0;
  232. }
  233. err:
  234. IR_dprintk(1, "NEC decoded failed at state %d (%s) @ %luus\n",
  235. data->state,
  236. bit ? "pulse" : "space",
  237. (ev->delta.tv_nsec + 500) / 1000);
  238. err2:
  239. data->state = STATE_INACTIVE;
  240. return -EINVAL;
  241. checksum_err:
  242. data->state = STATE_INACTIVE;
  243. IR_dprintk(1, "NEC checksum error: received 0x%02x%02x%02x%02x\n",
  244. data->nec_code.address,
  245. data->nec_code.not_address,
  246. data->nec_code.command,
  247. data->nec_code.not_command);
  248. return -EINVAL;
  249. }
  250. /**
  251. * ir_nec_decode() - Decodes all NEC pulsecodes on a given array
  252. * @input_dev: the struct input_dev descriptor of the device
  253. * @evs: event array with type/duration of pulse/space
  254. * @len: length of the array
  255. * This function returns the number of decoded pulses
  256. */
  257. static int ir_nec_decode(struct input_dev *input_dev,
  258. struct ir_raw_event *evs,
  259. int len)
  260. {
  261. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  262. struct decoder_data *data;
  263. int pos = 0;
  264. int rc = 0;
  265. data = get_decoder_data(ir_dev);
  266. if (!data || !data->enabled)
  267. return 0;
  268. for (pos = 0; pos < len; pos++)
  269. handle_event(input_dev, &evs[pos]);
  270. return rc;
  271. }
  272. static int ir_nec_register(struct input_dev *input_dev)
  273. {
  274. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  275. struct decoder_data *data;
  276. int rc;
  277. rc = sysfs_create_group(&ir_dev->dev.kobj, &decoder_attribute_group);
  278. if (rc < 0)
  279. return rc;
  280. data = kzalloc(sizeof(*data), GFP_KERNEL);
  281. if (!data) {
  282. sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group);
  283. return -ENOMEM;
  284. }
  285. data->ir_dev = ir_dev;
  286. data->enabled = 1;
  287. spin_lock(&decoder_lock);
  288. list_add_tail(&data->list, &decoder_list);
  289. spin_unlock(&decoder_lock);
  290. return 0;
  291. }
  292. static int ir_nec_unregister(struct input_dev *input_dev)
  293. {
  294. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  295. static struct decoder_data *data;
  296. data = get_decoder_data(ir_dev);
  297. if (!data)
  298. return 0;
  299. sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group);
  300. spin_lock(&decoder_lock);
  301. list_del(&data->list);
  302. spin_unlock(&decoder_lock);
  303. return 0;
  304. }
  305. static struct ir_raw_handler nec_handler = {
  306. .decode = ir_nec_decode,
  307. .raw_register = ir_nec_register,
  308. .raw_unregister = ir_nec_unregister,
  309. };
  310. static int __init ir_nec_decode_init(void)
  311. {
  312. ir_raw_handler_register(&nec_handler);
  313. printk(KERN_INFO "IR NEC protocol handler initialized\n");
  314. return 0;
  315. }
  316. static void __exit ir_nec_decode_exit(void)
  317. {
  318. ir_raw_handler_unregister(&nec_handler);
  319. }
  320. module_init(ir_nec_decode_init);
  321. module_exit(ir_nec_decode_exit);
  322. MODULE_LICENSE("GPL");
  323. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  324. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  325. MODULE_DESCRIPTION("NEC IR protocol decoder");