ir-rc5-decoder.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. #define RC5_NBITS 14
  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. enum rc5_state {
  30. STATE_INACTIVE,
  31. STATE_BIT_START,
  32. STATE_BIT_END,
  33. STATE_CHECK_RC5X,
  34. STATE_FINISHED,
  35. };
  36. /**
  37. * ir_rc5_decode() - Decode one RC-5 pulse or space
  38. * @dev: the struct rc_dev descriptor of the device
  39. * @ev: the struct ir_raw_event descriptor of the pulse/space
  40. *
  41. * This function returns -EINVAL if the pulse violates the state machine
  42. */
  43. static int ir_rc5_decode(struct rc_dev *dev, struct ir_raw_event ev)
  44. {
  45. struct rc5_dec *data = &dev->raw->rc5;
  46. u8 toggle;
  47. u32 scancode;
  48. if (!(dev->raw->enabled_protocols & RC_TYPE_RC5))
  49. return 0;
  50. if (!is_timing_event(ev)) {
  51. if (ev.reset)
  52. data->state = STATE_INACTIVE;
  53. return 0;
  54. }
  55. if (!geq_margin(ev.duration, RC5_UNIT, RC5_UNIT / 2))
  56. goto out;
  57. again:
  58. IR_dprintk(2, "RC5(x) decode started at state %i (%uus %s)\n",
  59. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  60. if (!geq_margin(ev.duration, RC5_UNIT, RC5_UNIT / 2))
  61. return 0;
  62. switch (data->state) {
  63. case STATE_INACTIVE:
  64. if (!ev.pulse)
  65. break;
  66. data->state = STATE_BIT_START;
  67. data->count = 1;
  68. /* We just need enough bits to get to STATE_CHECK_RC5X */
  69. data->wanted_bits = RC5X_NBITS;
  70. decrease_duration(&ev, RC5_BIT_START);
  71. goto again;
  72. case STATE_BIT_START:
  73. if (!eq_margin(ev.duration, RC5_BIT_START, RC5_UNIT / 2))
  74. break;
  75. data->bits <<= 1;
  76. if (!ev.pulse)
  77. data->bits |= 1;
  78. data->count++;
  79. data->state = STATE_BIT_END;
  80. return 0;
  81. case STATE_BIT_END:
  82. if (!is_transition(&ev, &dev->raw->prev_ev))
  83. break;
  84. if (data->count == data->wanted_bits)
  85. data->state = STATE_FINISHED;
  86. else if (data->count == CHECK_RC5X_NBITS)
  87. data->state = STATE_CHECK_RC5X;
  88. else
  89. data->state = STATE_BIT_START;
  90. decrease_duration(&ev, RC5_BIT_END);
  91. goto again;
  92. case STATE_CHECK_RC5X:
  93. if (!ev.pulse && geq_margin(ev.duration, RC5X_SPACE, RC5_UNIT / 2)) {
  94. /* RC5X */
  95. data->wanted_bits = RC5X_NBITS;
  96. decrease_duration(&ev, RC5X_SPACE);
  97. } else {
  98. /* RC5 */
  99. data->wanted_bits = RC5_NBITS;
  100. }
  101. data->state = STATE_BIT_START;
  102. goto again;
  103. case STATE_FINISHED:
  104. if (ev.pulse)
  105. break;
  106. if (data->wanted_bits == RC5X_NBITS) {
  107. /* RC5X */
  108. u8 xdata, command, system;
  109. xdata = (data->bits & 0x0003F) >> 0;
  110. command = (data->bits & 0x00FC0) >> 6;
  111. system = (data->bits & 0x1F000) >> 12;
  112. toggle = (data->bits & 0x20000) ? 1 : 0;
  113. command += (data->bits & 0x01000) ? 0 : 0x40;
  114. scancode = system << 16 | command << 8 | xdata;
  115. IR_dprintk(1, "RC5X scancode 0x%06x (toggle: %u)\n",
  116. scancode, toggle);
  117. } else {
  118. /* RC5 */
  119. u8 command, system;
  120. command = (data->bits & 0x0003F) >> 0;
  121. system = (data->bits & 0x007C0) >> 6;
  122. toggle = (data->bits & 0x00800) ? 1 : 0;
  123. command += (data->bits & 0x01000) ? 0 : 0x40;
  124. scancode = system << 8 | command;
  125. IR_dprintk(1, "RC5 scancode 0x%04x (toggle: %u)\n",
  126. scancode, toggle);
  127. }
  128. rc_keydown(dev, scancode, toggle);
  129. data->state = STATE_INACTIVE;
  130. return 0;
  131. }
  132. out:
  133. IR_dprintk(1, "RC5(x) decode failed at state %i (%uus %s)\n",
  134. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  135. data->state = STATE_INACTIVE;
  136. return -EINVAL;
  137. }
  138. static struct ir_raw_handler rc5_handler = {
  139. .protocols = RC_TYPE_RC5,
  140. .decode = ir_rc5_decode,
  141. };
  142. static int __init ir_rc5_decode_init(void)
  143. {
  144. ir_raw_handler_register(&rc5_handler);
  145. printk(KERN_INFO "IR RC5(x) protocol handler initialized\n");
  146. return 0;
  147. }
  148. static void __exit ir_rc5_decode_exit(void)
  149. {
  150. ir_raw_handler_unregister(&rc5_handler);
  151. }
  152. module_init(ir_rc5_decode_init);
  153. module_exit(ir_rc5_decode_exit);
  154. MODULE_LICENSE("GPL");
  155. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  156. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  157. MODULE_DESCRIPTION("RC5(x) IR protocol decoder");