ir-rc6-decoder.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /* ir-rc6-decoder.c - A decoder for the RC6 IR protocol
  2. *
  3. * Copyright (C) 2010 by David Härdeman <david@hardeman.nu>
  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 "ir-core-priv.h"
  15. /*
  16. * This decoder currently supports:
  17. * RC6-0-16 (standard toggle bit in header)
  18. * RC6-6A-24 (no toggle bit)
  19. * RC6-6A-32 (MCE version with toggle bit in body)
  20. */
  21. #define RC6_UNIT 444444 /* us */
  22. #define RC6_HEADER_NBITS 4 /* not including toggle bit */
  23. #define RC6_0_NBITS 16
  24. #define RC6_6A_SMALL_NBITS 24
  25. #define RC6_6A_LARGE_NBITS 32
  26. #define RC6_PREFIX_PULSE (6 * RC6_UNIT)
  27. #define RC6_PREFIX_SPACE (2 * RC6_UNIT)
  28. #define RC6_BIT_START (1 * RC6_UNIT)
  29. #define RC6_BIT_END (1 * RC6_UNIT)
  30. #define RC6_TOGGLE_START (2 * RC6_UNIT)
  31. #define RC6_TOGGLE_END (2 * RC6_UNIT)
  32. #define RC6_MODE_MASK 0x07 /* for the header bits */
  33. #define RC6_STARTBIT_MASK 0x08 /* for the header bits */
  34. #define RC6_6A_MCE_TOGGLE_MASK 0x8000 /* for the body bits */
  35. /* Used to register rc6_decoder clients */
  36. static LIST_HEAD(decoder_list);
  37. static DEFINE_SPINLOCK(decoder_lock);
  38. enum rc6_mode {
  39. RC6_MODE_0,
  40. RC6_MODE_6A,
  41. RC6_MODE_UNKNOWN,
  42. };
  43. enum rc6_state {
  44. STATE_INACTIVE,
  45. STATE_PREFIX_SPACE,
  46. STATE_HEADER_BIT_START,
  47. STATE_HEADER_BIT_END,
  48. STATE_TOGGLE_START,
  49. STATE_TOGGLE_END,
  50. STATE_BODY_BIT_START,
  51. STATE_BODY_BIT_END,
  52. STATE_FINISHED,
  53. };
  54. struct decoder_data {
  55. struct list_head list;
  56. struct ir_input_dev *ir_dev;
  57. int enabled:1;
  58. /* State machine control */
  59. enum rc6_state state;
  60. u8 header;
  61. u32 body;
  62. struct ir_raw_event prev_ev;
  63. bool toggle;
  64. unsigned count;
  65. unsigned wanted_bits;
  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 = "rc6_decoder",
  118. .attrs = decoder_attributes,
  119. };
  120. static enum rc6_mode rc6_mode(struct decoder_data *data) {
  121. switch (data->header & RC6_MODE_MASK) {
  122. case 0:
  123. return RC6_MODE_0;
  124. case 6:
  125. if (!data->toggle)
  126. return RC6_MODE_6A;
  127. /* fall through */
  128. default:
  129. return RC6_MODE_UNKNOWN;
  130. }
  131. }
  132. /**
  133. * ir_rc6_decode() - Decode one RC6 pulse or space
  134. * @input_dev: the struct input_dev descriptor of the device
  135. * @ev: the struct ir_raw_event descriptor of the pulse/space
  136. *
  137. * This function returns -EINVAL if the pulse violates the state machine
  138. */
  139. static int ir_rc6_decode(struct input_dev *input_dev, struct ir_raw_event ev)
  140. {
  141. struct decoder_data *data;
  142. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  143. u32 scancode;
  144. u8 toggle;
  145. data = get_decoder_data(ir_dev);
  146. if (!data)
  147. return -EINVAL;
  148. if (!data->enabled)
  149. return 0;
  150. if (IS_RESET(ev)) {
  151. data->state = STATE_INACTIVE;
  152. return 0;
  153. }
  154. if (!geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2))
  155. goto out;
  156. again:
  157. IR_dprintk(2, "RC6 decode started at state %i (%uus %s)\n",
  158. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  159. if (!geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2))
  160. return 0;
  161. switch (data->state) {
  162. case STATE_INACTIVE:
  163. if (!ev.pulse)
  164. break;
  165. /* Note: larger margin on first pulse since each RC6_UNIT
  166. is quite short and some hardware takes some time to
  167. adjust to the signal */
  168. if (!eq_margin(ev.duration, RC6_PREFIX_PULSE, RC6_UNIT))
  169. break;
  170. data->state = STATE_PREFIX_SPACE;
  171. data->count = 0;
  172. return 0;
  173. case STATE_PREFIX_SPACE:
  174. if (ev.pulse)
  175. break;
  176. if (!eq_margin(ev.duration, RC6_PREFIX_SPACE, RC6_UNIT / 2))
  177. break;
  178. data->state = STATE_HEADER_BIT_START;
  179. return 0;
  180. case STATE_HEADER_BIT_START:
  181. if (!eq_margin(ev.duration, RC6_BIT_START, RC6_UNIT / 2))
  182. break;
  183. data->header <<= 1;
  184. if (ev.pulse)
  185. data->header |= 1;
  186. data->count++;
  187. data->prev_ev = ev;
  188. data->state = STATE_HEADER_BIT_END;
  189. return 0;
  190. case STATE_HEADER_BIT_END:
  191. if (!is_transition(&ev, &data->prev_ev))
  192. break;
  193. if (data->count == RC6_HEADER_NBITS)
  194. data->state = STATE_TOGGLE_START;
  195. else
  196. data->state = STATE_HEADER_BIT_START;
  197. decrease_duration(&ev, RC6_BIT_END);
  198. goto again;
  199. case STATE_TOGGLE_START:
  200. if (!eq_margin(ev.duration, RC6_TOGGLE_START, RC6_UNIT / 2))
  201. break;
  202. data->toggle = ev.pulse;
  203. data->prev_ev = ev;
  204. data->state = STATE_TOGGLE_END;
  205. return 0;
  206. case STATE_TOGGLE_END:
  207. if (!is_transition(&ev, &data->prev_ev) ||
  208. !geq_margin(ev.duration, RC6_TOGGLE_END, RC6_UNIT / 2))
  209. break;
  210. if (!(data->header & RC6_STARTBIT_MASK)) {
  211. IR_dprintk(1, "RC6 invalid start bit\n");
  212. break;
  213. }
  214. data->state = STATE_BODY_BIT_START;
  215. data->prev_ev = ev;
  216. decrease_duration(&ev, RC6_TOGGLE_END);
  217. data->count = 0;
  218. switch (rc6_mode(data)) {
  219. case RC6_MODE_0:
  220. data->wanted_bits = RC6_0_NBITS;
  221. break;
  222. case RC6_MODE_6A:
  223. /* This might look weird, but we basically
  224. check the value of the first body bit to
  225. determine the number of bits in mode 6A */
  226. if ((!ev.pulse && !geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2)) ||
  227. geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2))
  228. data->wanted_bits = RC6_6A_LARGE_NBITS;
  229. else
  230. data->wanted_bits = RC6_6A_SMALL_NBITS;
  231. break;
  232. default:
  233. IR_dprintk(1, "RC6 unknown mode\n");
  234. goto out;
  235. }
  236. goto again;
  237. case STATE_BODY_BIT_START:
  238. if (!eq_margin(ev.duration, RC6_BIT_START, RC6_UNIT / 2))
  239. break;
  240. data->body <<= 1;
  241. if (ev.pulse)
  242. data->body |= 1;
  243. data->count++;
  244. data->prev_ev = ev;
  245. data->state = STATE_BODY_BIT_END;
  246. return 0;
  247. case STATE_BODY_BIT_END:
  248. if (!is_transition(&ev, &data->prev_ev))
  249. break;
  250. if (data->count == data->wanted_bits)
  251. data->state = STATE_FINISHED;
  252. else
  253. data->state = STATE_BODY_BIT_START;
  254. decrease_duration(&ev, RC6_BIT_END);
  255. goto again;
  256. case STATE_FINISHED:
  257. if (ev.pulse)
  258. break;
  259. switch (rc6_mode(data)) {
  260. case RC6_MODE_0:
  261. scancode = data->body & 0xffff;
  262. toggle = data->toggle;
  263. IR_dprintk(1, "RC6(0) scancode 0x%04x (toggle: %u)\n",
  264. scancode, toggle);
  265. break;
  266. case RC6_MODE_6A:
  267. if (data->wanted_bits == RC6_6A_LARGE_NBITS) {
  268. toggle = data->body & RC6_6A_MCE_TOGGLE_MASK ? 1 : 0;
  269. scancode = data->body & ~RC6_6A_MCE_TOGGLE_MASK;
  270. } else {
  271. toggle = 0;
  272. scancode = data->body & 0xffffff;
  273. }
  274. IR_dprintk(1, "RC6(6A) scancode 0x%08x (toggle: %u)\n",
  275. scancode, toggle);
  276. break;
  277. default:
  278. IR_dprintk(1, "RC6 unknown mode\n");
  279. goto out;
  280. }
  281. ir_keydown(input_dev, scancode, toggle);
  282. data->state = STATE_INACTIVE;
  283. return 0;
  284. }
  285. out:
  286. IR_dprintk(1, "RC6 decode failed at state %i (%uus %s)\n",
  287. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  288. data->state = STATE_INACTIVE;
  289. return -EINVAL;
  290. }
  291. static int ir_rc6_register(struct input_dev *input_dev)
  292. {
  293. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  294. struct decoder_data *data;
  295. int rc;
  296. rc = sysfs_create_group(&ir_dev->dev.kobj, &decoder_attribute_group);
  297. if (rc < 0)
  298. return rc;
  299. data = kzalloc(sizeof(*data), GFP_KERNEL);
  300. if (!data) {
  301. sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group);
  302. return -ENOMEM;
  303. }
  304. data->ir_dev = ir_dev;
  305. data->enabled = 1;
  306. spin_lock(&decoder_lock);
  307. list_add_tail(&data->list, &decoder_list);
  308. spin_unlock(&decoder_lock);
  309. return 0;
  310. }
  311. static int ir_rc6_unregister(struct input_dev *input_dev)
  312. {
  313. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  314. static struct decoder_data *data;
  315. data = get_decoder_data(ir_dev);
  316. if (!data)
  317. return 0;
  318. sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group);
  319. spin_lock(&decoder_lock);
  320. list_del(&data->list);
  321. spin_unlock(&decoder_lock);
  322. return 0;
  323. }
  324. static struct ir_raw_handler rc6_handler = {
  325. .decode = ir_rc6_decode,
  326. .raw_register = ir_rc6_register,
  327. .raw_unregister = ir_rc6_unregister,
  328. };
  329. static int __init ir_rc6_decode_init(void)
  330. {
  331. ir_raw_handler_register(&rc6_handler);
  332. printk(KERN_INFO "IR RC6 protocol handler initialized\n");
  333. return 0;
  334. }
  335. static void __exit ir_rc6_decode_exit(void)
  336. {
  337. ir_raw_handler_unregister(&rc6_handler);
  338. }
  339. module_init(ir_rc6_decode_init);
  340. module_exit(ir_rc6_decode_exit);
  341. MODULE_LICENSE("GPL");
  342. MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
  343. MODULE_DESCRIPTION("RC6 IR protocol decoder");