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. data->state = STATE_INACTIVE;
  104. return 0;
  105. }
  106. device = bitrev8((data->bits << 3) & 0xF8);
  107. subdevice = 0;
  108. function = bitrev8((data->bits >> 4) & 0xFE);
  109. protocol = RC_TYPE_SONY12;
  110. break;
  111. case 15:
  112. if (!(dev->enabled_protocols & RC_BIT_SONY15)) {
  113. data->state = STATE_INACTIVE;
  114. return 0;
  115. }
  116. device = bitrev8((data->bits >> 0) & 0xFF);
  117. subdevice = 0;
  118. function = bitrev8((data->bits >> 7) & 0xFE);
  119. protocol = RC_TYPE_SONY15;
  120. break;
  121. case 20:
  122. if (!(dev->enabled_protocols & RC_BIT_SONY20)) {
  123. data->state = STATE_INACTIVE;
  124. return 0;
  125. }
  126. device = bitrev8((data->bits >> 5) & 0xF8);
  127. subdevice = bitrev8((data->bits >> 0) & 0xFF);
  128. function = bitrev8((data->bits >> 12) & 0xFE);
  129. protocol = RC_TYPE_SONY20;
  130. break;
  131. default:
  132. IR_dprintk(1, "Sony invalid bitcount %u\n", data->count);
  133. goto out;
  134. }
  135. scancode = device << 16 | subdevice << 8 | function;
  136. IR_dprintk(1, "Sony(%u) scancode 0x%05x\n", data->count, scancode);
  137. rc_keydown(dev, protocol, scancode, 0);
  138. data->state = STATE_INACTIVE;
  139. return 0;
  140. }
  141. out:
  142. IR_dprintk(1, "Sony decode failed at state %d (%uus %s)\n",
  143. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  144. data->state = STATE_INACTIVE;
  145. return -EINVAL;
  146. }
  147. static struct ir_raw_handler sony_handler = {
  148. .protocols = RC_BIT_SONY12 | RC_BIT_SONY15 | RC_BIT_SONY20,
  149. .decode = ir_sony_decode,
  150. };
  151. static int __init ir_sony_decode_init(void)
  152. {
  153. ir_raw_handler_register(&sony_handler);
  154. printk(KERN_INFO "IR Sony protocol handler initialized\n");
  155. return 0;
  156. }
  157. static void __exit ir_sony_decode_exit(void)
  158. {
  159. ir_raw_handler_unregister(&sony_handler);
  160. }
  161. module_init(ir_sony_decode_init);
  162. module_exit(ir_sony_decode_exit);
  163. MODULE_LICENSE("GPL");
  164. MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
  165. MODULE_DESCRIPTION("Sony IR protocol decoder");