ir-rc5-decoder.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* ir-rc5-decoder.c - decoder for RC5(x) and StreamZap protocols
  2. *
  3. * Copyright (C) 2010 by Mauro Carvalho Chehab
  4. * Copyright (C) 2010 by Jarod Wilson <jarod@redhat.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation version 2 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. /*
  16. * This decoder handles the 14 bit RC5 protocol, 15 bit "StreamZap" protocol
  17. * and 20 bit RC5x protocol.
  18. */
  19. #include "rc-core-priv.h"
  20. #include <linux/module.h>
  21. #define RC5_NBITS 14
  22. #define RC5_SZ_NBITS 15
  23. #define RC5X_NBITS 20
  24. #define CHECK_RC5X_NBITS 8
  25. #define RC5_UNIT 888888 /* ns */
  26. #define RC5_BIT_START (1 * RC5_UNIT)
  27. #define RC5_BIT_END (1 * RC5_UNIT)
  28. #define RC5X_SPACE (4 * RC5_UNIT)
  29. #define RC5_TRAILER (6 * RC5_UNIT) /* In reality, approx 100 */
  30. enum rc5_state {
  31. STATE_INACTIVE,
  32. STATE_BIT_START,
  33. STATE_BIT_END,
  34. STATE_CHECK_RC5X,
  35. STATE_FINISHED,
  36. };
  37. /**
  38. * ir_rc5_decode() - Decode one RC-5 pulse or space
  39. * @dev: the struct rc_dev descriptor of the device
  40. * @ev: the struct ir_raw_event descriptor of the pulse/space
  41. *
  42. * This function returns -EINVAL if the pulse violates the state machine
  43. */
  44. static int ir_rc5_decode(struct rc_dev *dev, struct ir_raw_event ev)
  45. {
  46. struct rc5_dec *data = &dev->raw->rc5;
  47. u8 toggle;
  48. u32 scancode;
  49. enum rc_type protocol;
  50. if (!(dev->enabled_protocols & (RC_BIT_RC5 | RC_BIT_RC5X | RC_BIT_RC5_SZ)))
  51. return 0;
  52. if (!is_timing_event(ev)) {
  53. if (ev.reset)
  54. data->state = STATE_INACTIVE;
  55. return 0;
  56. }
  57. if (!geq_margin(ev.duration, RC5_UNIT, RC5_UNIT / 2))
  58. goto out;
  59. again:
  60. IR_dprintk(2, "RC5(x/sz) decode started at state %i (%uus %s)\n",
  61. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  62. if (!geq_margin(ev.duration, RC5_UNIT, RC5_UNIT / 2))
  63. return 0;
  64. switch (data->state) {
  65. case STATE_INACTIVE:
  66. if (!ev.pulse)
  67. break;
  68. data->state = STATE_BIT_START;
  69. data->count = 1;
  70. decrease_duration(&ev, RC5_BIT_START);
  71. goto again;
  72. case STATE_BIT_START:
  73. if (!ev.pulse && geq_margin(ev.duration, RC5_TRAILER, RC5_UNIT / 2)) {
  74. data->state = STATE_FINISHED;
  75. goto again;
  76. }
  77. if (!eq_margin(ev.duration, RC5_BIT_START, RC5_UNIT / 2))
  78. break;
  79. data->bits <<= 1;
  80. if (!ev.pulse)
  81. data->bits |= 1;
  82. data->count++;
  83. data->state = STATE_BIT_END;
  84. return 0;
  85. case STATE_BIT_END:
  86. if (!is_transition(&ev, &dev->raw->prev_ev))
  87. break;
  88. if (data->count == CHECK_RC5X_NBITS)
  89. data->state = STATE_CHECK_RC5X;
  90. else
  91. data->state = STATE_BIT_START;
  92. decrease_duration(&ev, RC5_BIT_END);
  93. goto again;
  94. case STATE_CHECK_RC5X:
  95. if (!ev.pulse && geq_margin(ev.duration, RC5X_SPACE, RC5_UNIT / 2)) {
  96. data->is_rc5x = true;
  97. decrease_duration(&ev, RC5X_SPACE);
  98. } else
  99. data->is_rc5x = false;
  100. data->state = STATE_BIT_START;
  101. goto again;
  102. case STATE_FINISHED:
  103. if (ev.pulse)
  104. break;
  105. if (data->is_rc5x && data->count == RC5X_NBITS) {
  106. /* RC5X */
  107. u8 xdata, command, system;
  108. if (!(dev->enabled_protocols & RC_BIT_RC5X)) {
  109. data->state = STATE_INACTIVE;
  110. return 0;
  111. }
  112. xdata = (data->bits & 0x0003F) >> 0;
  113. command = (data->bits & 0x00FC0) >> 6;
  114. system = (data->bits & 0x1F000) >> 12;
  115. toggle = (data->bits & 0x20000) ? 1 : 0;
  116. command += (data->bits & 0x01000) ? 0 : 0x40;
  117. scancode = system << 16 | command << 8 | xdata;
  118. protocol = RC_TYPE_RC5X;
  119. } else if (!data->is_rc5x && data->count == RC5_NBITS) {
  120. /* RC5 */
  121. u8 command, system;
  122. if (!(dev->enabled_protocols & RC_BIT_RC5)) {
  123. data->state = STATE_INACTIVE;
  124. return 0;
  125. }
  126. command = (data->bits & 0x0003F) >> 0;
  127. system = (data->bits & 0x007C0) >> 6;
  128. toggle = (data->bits & 0x00800) ? 1 : 0;
  129. command += (data->bits & 0x01000) ? 0 : 0x40;
  130. scancode = system << 8 | command;
  131. protocol = RC_TYPE_RC5;
  132. } else if (!data->is_rc5x && data->count == RC5_SZ_NBITS) {
  133. /* RC5 StreamZap */
  134. u8 command, system;
  135. if (!(dev->enabled_protocols & RC_BIT_RC5_SZ)) {
  136. data->state = STATE_INACTIVE;
  137. return 0;
  138. }
  139. command = (data->bits & 0x0003F) >> 0;
  140. system = (data->bits & 0x02FC0) >> 6;
  141. toggle = (data->bits & 0x01000) ? 1 : 0;
  142. scancode = system << 6 | command;
  143. protocol = RC_TYPE_RC5_SZ;
  144. } else
  145. break;
  146. IR_dprintk(1, "RC5(x/sz) scancode 0x%06x (p: %u, t: %u)\n",
  147. scancode, protocol, toggle);
  148. rc_keydown(dev, protocol, scancode, toggle);
  149. data->state = STATE_INACTIVE;
  150. return 0;
  151. }
  152. out:
  153. IR_dprintk(1, "RC5(x/sz) decode failed at state %i count %d (%uus %s)\n",
  154. data->state, data->count, TO_US(ev.duration), TO_STR(ev.pulse));
  155. data->state = STATE_INACTIVE;
  156. return -EINVAL;
  157. }
  158. static struct ir_raw_handler rc5_handler = {
  159. .protocols = RC_BIT_RC5 | RC_BIT_RC5X | RC_BIT_RC5_SZ,
  160. .decode = ir_rc5_decode,
  161. };
  162. static int __init ir_rc5_decode_init(void)
  163. {
  164. ir_raw_handler_register(&rc5_handler);
  165. printk(KERN_INFO "IR RC5(x/sz) protocol handler initialized\n");
  166. return 0;
  167. }
  168. static void __exit ir_rc5_decode_exit(void)
  169. {
  170. ir_raw_handler_unregister(&rc5_handler);
  171. }
  172. module_init(ir_rc5_decode_init);
  173. module_exit(ir_rc5_decode_exit);
  174. MODULE_LICENSE("GPL");
  175. MODULE_AUTHOR("Mauro Carvalho Chehab and Jarod Wilson");
  176. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  177. MODULE_DESCRIPTION("RC5(x/sz) IR protocol decoder");