ir-sharp-decoder.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* ir-sharp-decoder.c - handle Sharp IR Pulse/Space protocol
  2. *
  3. * Copyright (C) 2013-2014 Imagination Technologies Ltd.
  4. *
  5. * Based on NEC decoder:
  6. * Copyright (C) 2010 by Mauro Carvalho Chehab
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation version 2 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/bitrev.h>
  18. #include <linux/module.h>
  19. #include "rc-core-priv.h"
  20. #define SHARP_NBITS 15
  21. #define SHARP_UNIT 40000 /* ns */
  22. #define SHARP_BIT_PULSE (8 * SHARP_UNIT) /* 320us */
  23. #define SHARP_BIT_0_PERIOD (25 * SHARP_UNIT) /* 1ms (680us space) */
  24. #define SHARP_BIT_1_PERIOD (50 * SHARP_UNIT) /* 2ms (1680ms space) */
  25. #define SHARP_ECHO_SPACE (1000 * SHARP_UNIT) /* 40 ms */
  26. #define SHARP_TRAILER_SPACE (125 * SHARP_UNIT) /* 5 ms (even longer) */
  27. enum sharp_state {
  28. STATE_INACTIVE,
  29. STATE_BIT_PULSE,
  30. STATE_BIT_SPACE,
  31. STATE_TRAILER_PULSE,
  32. STATE_ECHO_SPACE,
  33. STATE_TRAILER_SPACE,
  34. };
  35. /**
  36. * ir_sharp_decode() - Decode one Sharp pulse or space
  37. * @dev: the struct rc_dev descriptor of the device
  38. * @duration: the struct ir_raw_event descriptor of the pulse/space
  39. *
  40. * This function returns -EINVAL if the pulse violates the state machine
  41. */
  42. static int ir_sharp_decode(struct rc_dev *dev, struct ir_raw_event ev)
  43. {
  44. struct sharp_dec *data = &dev->raw->sharp;
  45. u32 msg, echo, address, command, scancode;
  46. if (!(dev->enabled_protocols & RC_BIT_SHARP))
  47. return 0;
  48. if (!is_timing_event(ev)) {
  49. if (ev.reset)
  50. data->state = STATE_INACTIVE;
  51. return 0;
  52. }
  53. IR_dprintk(2, "Sharp decode started at state %d (%uus %s)\n",
  54. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  55. switch (data->state) {
  56. case STATE_INACTIVE:
  57. if (!ev.pulse)
  58. break;
  59. if (!eq_margin(ev.duration, SHARP_BIT_PULSE,
  60. SHARP_BIT_PULSE / 2))
  61. break;
  62. data->count = 0;
  63. data->pulse_len = ev.duration;
  64. data->state = STATE_BIT_SPACE;
  65. return 0;
  66. case STATE_BIT_PULSE:
  67. if (!ev.pulse)
  68. break;
  69. if (!eq_margin(ev.duration, SHARP_BIT_PULSE,
  70. SHARP_BIT_PULSE / 2))
  71. break;
  72. data->pulse_len = ev.duration;
  73. data->state = STATE_BIT_SPACE;
  74. return 0;
  75. case STATE_BIT_SPACE:
  76. if (ev.pulse)
  77. break;
  78. data->bits <<= 1;
  79. if (eq_margin(data->pulse_len + ev.duration, SHARP_BIT_1_PERIOD,
  80. SHARP_BIT_PULSE * 2))
  81. data->bits |= 1;
  82. else if (!eq_margin(data->pulse_len + ev.duration,
  83. SHARP_BIT_0_PERIOD, SHARP_BIT_PULSE * 2))
  84. break;
  85. data->count++;
  86. if (data->count == SHARP_NBITS ||
  87. data->count == SHARP_NBITS * 2)
  88. data->state = STATE_TRAILER_PULSE;
  89. else
  90. data->state = STATE_BIT_PULSE;
  91. return 0;
  92. case STATE_TRAILER_PULSE:
  93. if (!ev.pulse)
  94. break;
  95. if (!eq_margin(ev.duration, SHARP_BIT_PULSE,
  96. SHARP_BIT_PULSE / 2))
  97. break;
  98. if (data->count == SHARP_NBITS) {
  99. /* exp,chk bits should be 1,0 */
  100. if ((data->bits & 0x3) != 0x2)
  101. break;
  102. data->state = STATE_ECHO_SPACE;
  103. } else {
  104. data->state = STATE_TRAILER_SPACE;
  105. }
  106. return 0;
  107. case STATE_ECHO_SPACE:
  108. if (ev.pulse)
  109. break;
  110. if (!eq_margin(ev.duration, SHARP_ECHO_SPACE,
  111. SHARP_ECHO_SPACE / 4))
  112. break;
  113. data->state = STATE_BIT_PULSE;
  114. return 0;
  115. case STATE_TRAILER_SPACE:
  116. if (ev.pulse)
  117. break;
  118. if (!geq_margin(ev.duration, SHARP_TRAILER_SPACE,
  119. SHARP_BIT_PULSE / 2))
  120. break;
  121. /* Validate - command, ext, chk should be inverted in 2nd */
  122. msg = (data->bits >> 15) & 0x7fff;
  123. echo = data->bits & 0x7fff;
  124. if ((msg ^ echo) != 0x3ff) {
  125. IR_dprintk(1,
  126. "Sharp checksum error: received 0x%04x, 0x%04x\n",
  127. msg, echo);
  128. break;
  129. }
  130. address = bitrev8((msg >> 7) & 0xf8);
  131. command = bitrev8((msg >> 2) & 0xff);
  132. scancode = address << 8 | command;
  133. IR_dprintk(1, "Sharp scancode 0x%04x\n", scancode);
  134. rc_keydown(dev, RC_TYPE_SHARP, scancode, 0);
  135. data->state = STATE_INACTIVE;
  136. return 0;
  137. }
  138. IR_dprintk(1, "Sharp decode failed at count %d state %d (%uus %s)\n",
  139. data->count, data->state, TO_US(ev.duration),
  140. TO_STR(ev.pulse));
  141. data->state = STATE_INACTIVE;
  142. return -EINVAL;
  143. }
  144. static struct ir_raw_handler sharp_handler = {
  145. .protocols = RC_BIT_SHARP,
  146. .decode = ir_sharp_decode,
  147. };
  148. static int __init ir_sharp_decode_init(void)
  149. {
  150. ir_raw_handler_register(&sharp_handler);
  151. pr_info("IR Sharp protocol handler initialized\n");
  152. return 0;
  153. }
  154. static void __exit ir_sharp_decode_exit(void)
  155. {
  156. ir_raw_handler_unregister(&sharp_handler);
  157. }
  158. module_init(ir_sharp_decode_init);
  159. module_exit(ir_sharp_decode_exit);
  160. MODULE_LICENSE("GPL");
  161. MODULE_AUTHOR("James Hogan <james.hogan@imgtec.com>");
  162. MODULE_DESCRIPTION("Sharp IR protocol decoder");