ir-sony-decoder.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* ir-sony-decoder.c - handle Sony IR Pulse/Space protocol
  2. *
  3. * Copyright (C) 2010 by David Härdeman <david@hardeman.nu>
  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. #include <linux/bitrev.h>
  15. #include <linux/module.h>
  16. #include "rc-core-priv.h"
  17. #define SONY_UNIT 600000 /* ns */
  18. #define SONY_HEADER_PULSE (4 * SONY_UNIT)
  19. #define SONY_HEADER_SPACE (1 * SONY_UNIT)
  20. #define SONY_BIT_0_PULSE (1 * SONY_UNIT)
  21. #define SONY_BIT_1_PULSE (2 * SONY_UNIT)
  22. #define SONY_BIT_SPACE (1 * SONY_UNIT)
  23. #define SONY_TRAILER_SPACE (10 * SONY_UNIT) /* minimum */
  24. enum sony_state {
  25. STATE_INACTIVE,
  26. STATE_HEADER_SPACE,
  27. STATE_BIT_PULSE,
  28. STATE_BIT_SPACE,
  29. STATE_FINISHED,
  30. };
  31. /**
  32. * ir_sony_decode() - Decode one Sony pulse or space
  33. * @dev: the struct rc_dev descriptor of the device
  34. * @ev: the struct ir_raw_event descriptor of the pulse/space
  35. *
  36. * This function returns -EINVAL if the pulse violates the state machine
  37. */
  38. static int ir_sony_decode(struct rc_dev *dev, struct ir_raw_event ev)
  39. {
  40. struct sony_dec *data = &dev->raw->sony;
  41. enum rc_type protocol;
  42. u32 scancode;
  43. u8 device, subdevice, function;
  44. if (!(dev->enabled_protocols &
  45. (RC_BIT_SONY12 | RC_BIT_SONY15 | RC_BIT_SONY20)))
  46. return 0;
  47. if (!is_timing_event(ev)) {
  48. if (ev.reset)
  49. data->state = STATE_INACTIVE;
  50. return 0;
  51. }
  52. if (!geq_margin(ev.duration, SONY_UNIT, SONY_UNIT / 2))
  53. goto out;
  54. IR_dprintk(2, "Sony decode started at state %d (%uus %s)\n",
  55. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  56. switch (data->state) {
  57. case STATE_INACTIVE:
  58. if (!ev.pulse)
  59. break;
  60. if (!eq_margin(ev.duration, SONY_HEADER_PULSE, SONY_UNIT / 2))
  61. break;
  62. data->count = 0;
  63. data->state = STATE_HEADER_SPACE;
  64. return 0;
  65. case STATE_HEADER_SPACE:
  66. if (ev.pulse)
  67. break;
  68. if (!eq_margin(ev.duration, SONY_HEADER_SPACE, SONY_UNIT / 2))
  69. break;
  70. data->state = STATE_BIT_PULSE;
  71. return 0;
  72. case STATE_BIT_PULSE:
  73. if (!ev.pulse)
  74. break;
  75. data->bits <<= 1;
  76. if (eq_margin(ev.duration, SONY_BIT_1_PULSE, SONY_UNIT / 2))
  77. data->bits |= 1;
  78. else if (!eq_margin(ev.duration, SONY_BIT_0_PULSE, SONY_UNIT / 2))
  79. break;
  80. data->count++;
  81. data->state = STATE_BIT_SPACE;
  82. return 0;
  83. case STATE_BIT_SPACE:
  84. if (ev.pulse)
  85. break;
  86. if (!geq_margin(ev.duration, SONY_BIT_SPACE, SONY_UNIT / 2))
  87. break;
  88. decrease_duration(&ev, SONY_BIT_SPACE);
  89. if (!geq_margin(ev.duration, SONY_UNIT, SONY_UNIT / 2)) {
  90. data->state = STATE_BIT_PULSE;
  91. return 0;
  92. }
  93. data->state = STATE_FINISHED;
  94. /* Fall through */
  95. case STATE_FINISHED:
  96. if (ev.pulse)
  97. break;
  98. if (!geq_margin(ev.duration, SONY_TRAILER_SPACE, SONY_UNIT / 2))
  99. break;
  100. switch (data->count) {
  101. case 12:
  102. if (!(dev->enabled_protocols & RC_BIT_SONY12))
  103. goto finish_state_machine;
  104. device = bitrev8((data->bits << 3) & 0xF8);
  105. subdevice = 0;
  106. function = bitrev8((data->bits >> 4) & 0xFE);
  107. protocol = RC_TYPE_SONY12;
  108. break;
  109. case 15:
  110. if (!(dev->enabled_protocols & RC_BIT_SONY15))
  111. goto finish_state_machine;
  112. device = bitrev8((data->bits >> 0) & 0xFF);
  113. subdevice = 0;
  114. function = bitrev8((data->bits >> 7) & 0xFE);
  115. protocol = RC_TYPE_SONY15;
  116. break;
  117. case 20:
  118. if (!(dev->enabled_protocols & RC_BIT_SONY20))
  119. goto finish_state_machine;
  120. device = bitrev8((data->bits >> 5) & 0xF8);
  121. subdevice = bitrev8((data->bits >> 0) & 0xFF);
  122. function = bitrev8((data->bits >> 12) & 0xFE);
  123. protocol = RC_TYPE_SONY20;
  124. break;
  125. default:
  126. IR_dprintk(1, "Sony invalid bitcount %u\n", data->count);
  127. goto out;
  128. }
  129. scancode = device << 16 | subdevice << 8 | function;
  130. IR_dprintk(1, "Sony(%u) scancode 0x%05x\n", data->count, scancode);
  131. rc_keydown(dev, protocol, scancode, 0);
  132. goto finish_state_machine;
  133. }
  134. out:
  135. IR_dprintk(1, "Sony decode failed at state %d (%uus %s)\n",
  136. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  137. data->state = STATE_INACTIVE;
  138. return -EINVAL;
  139. finish_state_machine:
  140. data->state = STATE_INACTIVE;
  141. return 0;
  142. }
  143. static struct ir_raw_handler sony_handler = {
  144. .protocols = RC_BIT_SONY12 | RC_BIT_SONY15 | RC_BIT_SONY20,
  145. .decode = ir_sony_decode,
  146. };
  147. static int __init ir_sony_decode_init(void)
  148. {
  149. ir_raw_handler_register(&sony_handler);
  150. printk(KERN_INFO "IR Sony protocol handler initialized\n");
  151. return 0;
  152. }
  153. static void __exit ir_sony_decode_exit(void)
  154. {
  155. ir_raw_handler_unregister(&sony_handler);
  156. }
  157. module_init(ir_sony_decode_init);
  158. module_exit(ir_sony_decode_exit);
  159. MODULE_LICENSE("GPL");
  160. MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
  161. MODULE_DESCRIPTION("Sony IR protocol decoder");