ir-nec-decoder.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 DEFINE_SPINLOCK(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. * ir_nec_decode() - 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 ir_nec_decode(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. if (!data->enabled)
  120. return 0;
  121. /* Except for the initial event, what matters is the previous bit */
  122. bit = (ev->type & IR_PULSE) ? 1 : 0;
  123. last_bit = !bit;
  124. /* Discards spurious space last_bits when inactive */
  125. /* Very long delays are considered as start events */
  126. if (ev->delta.tv_nsec > NEC_HEADER_MARK + NEC_HEADER_SPACE - NEC_UNIT / 2)
  127. data->state = STATE_INACTIVE;
  128. if (ev->type & IR_START_EVENT)
  129. data->state = STATE_INACTIVE;
  130. switch (data->state) {
  131. case STATE_INACTIVE:
  132. if (!bit) /* PULSE marks the start event */
  133. return 0;
  134. data->count = 0;
  135. data->state = STATE_HEADER_MARK;
  136. memset (&data->nec_code, 0, sizeof(data->nec_code));
  137. return 0;
  138. case STATE_HEADER_MARK:
  139. if (!last_bit)
  140. goto err;
  141. if (ev->delta.tv_nsec < NEC_HEADER_MARK - 6 * NEC_UNIT)
  142. goto err;
  143. data->state = STATE_HEADER_SPACE;
  144. return 0;
  145. case STATE_HEADER_SPACE:
  146. if (last_bit)
  147. goto err;
  148. if (ev->delta.tv_nsec >= NEC_HEADER_SPACE - NEC_UNIT / 2) {
  149. data->state = STATE_MARK;
  150. return 0;
  151. }
  152. if (ev->delta.tv_nsec >= NEC_REPEAT_SPACE - NEC_UNIT / 2) {
  153. ir_repeat(input_dev);
  154. IR_dprintk(1, "Repeat last key\n");
  155. data->state = STATE_TRAILER_MARK;
  156. return 0;
  157. }
  158. goto err;
  159. case STATE_MARK:
  160. if (!last_bit)
  161. goto err;
  162. if ((ev->delta.tv_nsec > NEC_MARK + NEC_UNIT / 2) ||
  163. (ev->delta.tv_nsec < NEC_MARK - NEC_UNIT / 2))
  164. goto err;
  165. data->state = STATE_SPACE;
  166. return 0;
  167. case STATE_SPACE:
  168. if (last_bit)
  169. goto err;
  170. if ((ev->delta.tv_nsec >= NEC_0_SPACE - NEC_UNIT / 2) &&
  171. (ev->delta.tv_nsec < NEC_0_SPACE + NEC_UNIT / 2))
  172. bit = 0;
  173. else if ((ev->delta.tv_nsec >= NEC_1_SPACE - NEC_UNIT / 2) &&
  174. (ev->delta.tv_nsec < NEC_1_SPACE + NEC_UNIT / 2))
  175. bit = 1;
  176. else {
  177. IR_dprintk(1, "Decode failed at %d-th bit (%s) @%luus\n",
  178. data->count,
  179. last_bit ? "pulse" : "space",
  180. (ev->delta.tv_nsec + 500) / 1000);
  181. goto err2;
  182. }
  183. /* Ok, we've got a valid bit. proccess it */
  184. if (bit) {
  185. int shift = data->count;
  186. /*
  187. * NEC transmit bytes on this temporal order:
  188. * address | not address | command | not command
  189. */
  190. if (shift < 8) {
  191. data->nec_code.address |= 1 << shift;
  192. } else if (shift < 16) {
  193. data->nec_code.not_address |= 1 << (shift - 8);
  194. } else if (shift < 24) {
  195. data->nec_code.command |= 1 << (shift - 16);
  196. } else {
  197. data->nec_code.not_command |= 1 << (shift - 24);
  198. }
  199. }
  200. if (++data->count == NEC_NBITS) {
  201. u32 scancode;
  202. /*
  203. * Fixme: may need to accept Extended NEC protocol?
  204. */
  205. if ((data->nec_code.command ^ data->nec_code.not_command) != 0xff)
  206. goto checksum_err;
  207. if ((data->nec_code.address ^ data->nec_code.not_address) != 0xff) {
  208. /* Extended NEC */
  209. scancode = data->nec_code.address << 16 |
  210. data->nec_code.not_address << 8 |
  211. data->nec_code.command;
  212. IR_dprintk(1, "NEC scancode 0x%06x\n", scancode);
  213. } else {
  214. /* normal NEC */
  215. scancode = data->nec_code.address << 8 |
  216. data->nec_code.command;
  217. IR_dprintk(1, "NEC scancode 0x%04x\n", scancode);
  218. }
  219. ir_keydown(input_dev, scancode, 0);
  220. data->state = STATE_TRAILER_MARK;
  221. } else
  222. data->state = STATE_MARK;
  223. return 0;
  224. case STATE_TRAILER_MARK:
  225. if (!last_bit)
  226. goto err;
  227. data->state = STATE_TRAILER_SPACE;
  228. return 0;
  229. case STATE_TRAILER_SPACE:
  230. if (last_bit)
  231. goto err;
  232. data->state = STATE_INACTIVE;
  233. return 0;
  234. }
  235. err:
  236. IR_dprintk(1, "NEC decoded failed at state %d (%s) @ %luus\n",
  237. data->state,
  238. bit ? "pulse" : "space",
  239. (ev->delta.tv_nsec + 500) / 1000);
  240. err2:
  241. data->state = STATE_INACTIVE;
  242. return -EINVAL;
  243. checksum_err:
  244. data->state = STATE_INACTIVE;
  245. IR_dprintk(1, "NEC checksum error: received 0x%02x%02x%02x%02x\n",
  246. data->nec_code.address,
  247. data->nec_code.not_address,
  248. data->nec_code.command,
  249. data->nec_code.not_command);
  250. return -EINVAL;
  251. }
  252. static int ir_nec_register(struct input_dev *input_dev)
  253. {
  254. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  255. struct decoder_data *data;
  256. int rc;
  257. rc = sysfs_create_group(&ir_dev->dev.kobj, &decoder_attribute_group);
  258. if (rc < 0)
  259. return rc;
  260. data = kzalloc(sizeof(*data), GFP_KERNEL);
  261. if (!data) {
  262. sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group);
  263. return -ENOMEM;
  264. }
  265. data->ir_dev = ir_dev;
  266. data->enabled = 1;
  267. spin_lock(&decoder_lock);
  268. list_add_tail(&data->list, &decoder_list);
  269. spin_unlock(&decoder_lock);
  270. return 0;
  271. }
  272. static int ir_nec_unregister(struct input_dev *input_dev)
  273. {
  274. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  275. static struct decoder_data *data;
  276. data = get_decoder_data(ir_dev);
  277. if (!data)
  278. return 0;
  279. sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group);
  280. spin_lock(&decoder_lock);
  281. list_del(&data->list);
  282. spin_unlock(&decoder_lock);
  283. return 0;
  284. }
  285. static struct ir_raw_handler nec_handler = {
  286. .decode = ir_nec_decode,
  287. .raw_register = ir_nec_register,
  288. .raw_unregister = ir_nec_unregister,
  289. };
  290. static int __init ir_nec_decode_init(void)
  291. {
  292. ir_raw_handler_register(&nec_handler);
  293. printk(KERN_INFO "IR NEC protocol handler initialized\n");
  294. return 0;
  295. }
  296. static void __exit ir_nec_decode_exit(void)
  297. {
  298. ir_raw_handler_unregister(&nec_handler);
  299. }
  300. module_init(ir_nec_decode_init);
  301. module_exit(ir_nec_decode_exit);
  302. MODULE_LICENSE("GPL");
  303. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  304. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  305. MODULE_DESCRIPTION("NEC IR protocol decoder");