ir-sanyo-decoder.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* ir-sanyo-decoder.c - handle SANYO IR Pulse/Space protocol
  2. *
  3. * Copyright (C) 2011 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. * This protocol uses the NEC protocol timings. However, data is formatted as:
  15. * 13 bits Custom Code
  16. * 13 bits NOT(Custom Code)
  17. * 8 bits Key data
  18. * 8 bits NOT(Key data)
  19. *
  20. * According with LIRC, this protocol is used on Sanyo, Aiwa and Chinon
  21. * Information for this protocol is available at the Sanyo LC7461 datasheet.
  22. */
  23. #include <linux/bitrev.h>
  24. #include "rc-core-priv.h"
  25. #define SANYO_NBITS (13+13+8+8)
  26. #define SANYO_UNIT 562500 /* ns */
  27. #define SANYO_HEADER_PULSE (16 * SANYO_UNIT)
  28. #define SANYO_HEADER_SPACE (8 * SANYO_UNIT)
  29. #define SANYO_BIT_PULSE (1 * SANYO_UNIT)
  30. #define SANYO_BIT_0_SPACE (1 * SANYO_UNIT)
  31. #define SANYO_BIT_1_SPACE (3 * SANYO_UNIT)
  32. #define SANYO_REPEAT_SPACE (150 * SANYO_UNIT)
  33. #define SANYO_TRAILER_PULSE (1 * SANYO_UNIT)
  34. #define SANYO_TRAILER_SPACE (10 * SANYO_UNIT) /* in fact, 42 */
  35. enum sanyo_state {
  36. STATE_INACTIVE,
  37. STATE_HEADER_SPACE,
  38. STATE_BIT_PULSE,
  39. STATE_BIT_SPACE,
  40. STATE_TRAILER_PULSE,
  41. STATE_TRAILER_SPACE,
  42. };
  43. /**
  44. * ir_sanyo_decode() - Decode one SANYO pulse or space
  45. * @dev: the struct rc_dev descriptor of the device
  46. * @duration: the struct ir_raw_event descriptor of the pulse/space
  47. *
  48. * This function returns -EINVAL if the pulse violates the state machine
  49. */
  50. static int ir_sanyo_decode(struct rc_dev *dev, struct ir_raw_event ev)
  51. {
  52. struct sanyo_dec *data = &dev->raw->sanyo;
  53. u32 scancode;
  54. u8 address, not_address, command, not_command;
  55. if (!(dev->raw->enabled_protocols & RC_TYPE_SANYO))
  56. return 0;
  57. if (!is_timing_event(ev)) {
  58. if (ev.reset) {
  59. IR_dprintk(1, "SANYO event reset received. reset to state 0\n");
  60. data->state = STATE_INACTIVE;
  61. }
  62. return 0;
  63. }
  64. IR_dprintk(2, "SANYO decode started at state %d (%uus %s)\n",
  65. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  66. switch (data->state) {
  67. case STATE_INACTIVE:
  68. if (!ev.pulse)
  69. break;
  70. if (eq_margin(ev.duration, SANYO_HEADER_PULSE, SANYO_UNIT / 2)) {
  71. data->count = 0;
  72. data->state = STATE_HEADER_SPACE;
  73. return 0;
  74. }
  75. break;
  76. case STATE_HEADER_SPACE:
  77. if (ev.pulse)
  78. break;
  79. if (eq_margin(ev.duration, SANYO_HEADER_SPACE, SANYO_UNIT / 2)) {
  80. data->state = STATE_BIT_PULSE;
  81. return 0;
  82. }
  83. break;
  84. case STATE_BIT_PULSE:
  85. if (!ev.pulse)
  86. break;
  87. if (!eq_margin(ev.duration, SANYO_BIT_PULSE, SANYO_UNIT / 2))
  88. break;
  89. data->state = STATE_BIT_SPACE;
  90. return 0;
  91. case STATE_BIT_SPACE:
  92. if (ev.pulse)
  93. break;
  94. if (!data->count && geq_margin(ev.duration, SANYO_REPEAT_SPACE, SANYO_UNIT / 2)) {
  95. if (!dev->keypressed) {
  96. IR_dprintk(1, "SANYO discarding last key repeat: event after key up\n");
  97. } else {
  98. rc_repeat(dev);
  99. IR_dprintk(1, "SANYO repeat last key\n");
  100. data->state = STATE_INACTIVE;
  101. }
  102. return 0;
  103. }
  104. data->bits <<= 1;
  105. if (eq_margin(ev.duration, SANYO_BIT_1_SPACE, SANYO_UNIT / 2))
  106. data->bits |= 1;
  107. else if (!eq_margin(ev.duration, SANYO_BIT_0_SPACE, SANYO_UNIT / 2))
  108. break;
  109. data->count++;
  110. if (data->count == SANYO_NBITS)
  111. data->state = STATE_TRAILER_PULSE;
  112. else
  113. data->state = STATE_BIT_PULSE;
  114. return 0;
  115. case STATE_TRAILER_PULSE:
  116. if (!ev.pulse)
  117. break;
  118. if (!eq_margin(ev.duration, SANYO_TRAILER_PULSE, SANYO_UNIT / 2))
  119. break;
  120. data->state = STATE_TRAILER_SPACE;
  121. return 0;
  122. case STATE_TRAILER_SPACE:
  123. if (ev.pulse)
  124. break;
  125. if (!geq_margin(ev.duration, SANYO_TRAILER_SPACE, SANYO_UNIT / 2))
  126. break;
  127. address = bitrev16((data->bits >> 29) & 0x1fff) >> 3;
  128. not_address = bitrev16((data->bits >> 16) & 0x1fff) >> 3;
  129. command = bitrev8((data->bits >> 8) & 0xff);
  130. not_command = bitrev8((data->bits >> 0) & 0xff);
  131. if ((command ^ not_command) != 0xff) {
  132. IR_dprintk(1, "SANYO checksum error: received 0x%08Lx\n",
  133. data->bits);
  134. data->state = STATE_INACTIVE;
  135. return 0;
  136. }
  137. scancode = address << 8 | command;
  138. IR_dprintk(1, "SANYO scancode: 0x%06x\n", scancode);
  139. rc_keydown(dev, scancode, 0);
  140. data->state = STATE_INACTIVE;
  141. return 0;
  142. }
  143. IR_dprintk(1, "SANYO decode failed at count %d state %d (%uus %s)\n",
  144. data->count, data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  145. data->state = STATE_INACTIVE;
  146. return -EINVAL;
  147. }
  148. static struct ir_raw_handler sanyo_handler = {
  149. .protocols = RC_TYPE_SANYO,
  150. .decode = ir_sanyo_decode,
  151. };
  152. static int __init ir_sanyo_decode_init(void)
  153. {
  154. ir_raw_handler_register(&sanyo_handler);
  155. printk(KERN_INFO "IR SANYO protocol handler initialized\n");
  156. return 0;
  157. }
  158. static void __exit ir_sanyo_decode_exit(void)
  159. {
  160. ir_raw_handler_unregister(&sanyo_handler);
  161. }
  162. module_init(ir_sanyo_decode_init);
  163. module_exit(ir_sanyo_decode_exit);
  164. MODULE_LICENSE("GPL");
  165. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  166. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  167. MODULE_DESCRIPTION("SANYO IR protocol decoder");