ir-sanyo-decoder.c 5.2 KB

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