ir-rc5-decoder.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* ir-rc5-decoder.c - handle RC5(x) 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. /*
  15. * This code handles 14 bits RC5 protocols and 20 bits RC5x protocols.
  16. * There are other variants that use a different number of bits.
  17. * This is currently unsupported.
  18. * It considers a carrier of 36 kHz, with a total of 14/20 bits, where
  19. * the first two bits are start bits, and a third one is a filing bit
  20. */
  21. #include "rc-core-priv.h"
  22. #include <linux/module.h>
  23. #define RC5_NBITS 14
  24. #define RC5X_NBITS 20
  25. #define CHECK_RC5X_NBITS 8
  26. #define RC5_UNIT 888888 /* ns */
  27. #define RC5_BIT_START (1 * RC5_UNIT)
  28. #define RC5_BIT_END (1 * RC5_UNIT)
  29. #define RC5X_SPACE (4 * RC5_UNIT)
  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. if (!(dev->raw->enabled_protocols & RC_TYPE_RC5))
  50. return 0;
  51. if (!is_timing_event(ev)) {
  52. if (ev.reset)
  53. data->state = STATE_INACTIVE;
  54. return 0;
  55. }
  56. if (!geq_margin(ev.duration, RC5_UNIT, RC5_UNIT / 2))
  57. goto out;
  58. again:
  59. IR_dprintk(2, "RC5(x) decode started at state %i (%uus %s)\n",
  60. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  61. if (!geq_margin(ev.duration, RC5_UNIT, RC5_UNIT / 2))
  62. return 0;
  63. switch (data->state) {
  64. case STATE_INACTIVE:
  65. if (!ev.pulse)
  66. break;
  67. data->state = STATE_BIT_START;
  68. data->count = 1;
  69. /* We just need enough bits to get to STATE_CHECK_RC5X */
  70. data->wanted_bits = RC5X_NBITS;
  71. decrease_duration(&ev, RC5_BIT_START);
  72. goto again;
  73. case STATE_BIT_START:
  74. if (!eq_margin(ev.duration, RC5_BIT_START, RC5_UNIT / 2))
  75. break;
  76. data->bits <<= 1;
  77. if (!ev.pulse)
  78. data->bits |= 1;
  79. data->count++;
  80. data->state = STATE_BIT_END;
  81. return 0;
  82. case STATE_BIT_END:
  83. if (!is_transition(&ev, &dev->raw->prev_ev))
  84. break;
  85. if (data->count == data->wanted_bits)
  86. data->state = STATE_FINISHED;
  87. else if (data->count == CHECK_RC5X_NBITS)
  88. data->state = STATE_CHECK_RC5X;
  89. else
  90. data->state = STATE_BIT_START;
  91. decrease_duration(&ev, RC5_BIT_END);
  92. goto again;
  93. case STATE_CHECK_RC5X:
  94. if (!ev.pulse && geq_margin(ev.duration, RC5X_SPACE, RC5_UNIT / 2)) {
  95. /* RC5X */
  96. data->wanted_bits = RC5X_NBITS;
  97. decrease_duration(&ev, RC5X_SPACE);
  98. } else {
  99. /* RC5 */
  100. data->wanted_bits = RC5_NBITS;
  101. }
  102. data->state = STATE_BIT_START;
  103. goto again;
  104. case STATE_FINISHED:
  105. if (ev.pulse)
  106. break;
  107. if (data->wanted_bits == RC5X_NBITS) {
  108. /* RC5X */
  109. u8 xdata, command, system;
  110. xdata = (data->bits & 0x0003F) >> 0;
  111. command = (data->bits & 0x00FC0) >> 6;
  112. system = (data->bits & 0x1F000) >> 12;
  113. toggle = (data->bits & 0x20000) ? 1 : 0;
  114. command += (data->bits & 0x01000) ? 0 : 0x40;
  115. scancode = system << 16 | command << 8 | xdata;
  116. IR_dprintk(1, "RC5X scancode 0x%06x (toggle: %u)\n",
  117. scancode, toggle);
  118. } else {
  119. /* RC5 */
  120. u8 command, system;
  121. command = (data->bits & 0x0003F) >> 0;
  122. system = (data->bits & 0x007C0) >> 6;
  123. toggle = (data->bits & 0x00800) ? 1 : 0;
  124. command += (data->bits & 0x01000) ? 0 : 0x40;
  125. scancode = system << 8 | command;
  126. IR_dprintk(1, "RC5 scancode 0x%04x (toggle: %u)\n",
  127. scancode, toggle);
  128. }
  129. rc_keydown(dev, scancode, toggle);
  130. data->state = STATE_INACTIVE;
  131. return 0;
  132. }
  133. out:
  134. IR_dprintk(1, "RC5(x) decode failed at state %i (%uus %s)\n",
  135. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  136. data->state = STATE_INACTIVE;
  137. return -EINVAL;
  138. }
  139. static struct ir_raw_handler rc5_handler = {
  140. .protocols = RC_TYPE_RC5,
  141. .decode = ir_rc5_decode,
  142. };
  143. static int __init ir_rc5_decode_init(void)
  144. {
  145. ir_raw_handler_register(&rc5_handler);
  146. printk(KERN_INFO "IR RC5(x) protocol handler initialized\n");
  147. return 0;
  148. }
  149. static void __exit ir_rc5_decode_exit(void)
  150. {
  151. ir_raw_handler_unregister(&rc5_handler);
  152. }
  153. module_init(ir_rc5_decode_init);
  154. module_exit(ir_rc5_decode_exit);
  155. MODULE_LICENSE("GPL");
  156. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  157. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  158. MODULE_DESCRIPTION("RC5(x) IR protocol decoder");