ir-sony-decoder.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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_proto protocol;
  42. u32 scancode;
  43. u8 device, subdevice, function;
  44. if (!is_timing_event(ev)) {
  45. if (ev.reset)
  46. data->state = STATE_INACTIVE;
  47. return 0;
  48. }
  49. if (!geq_margin(ev.duration, SONY_UNIT, SONY_UNIT / 2))
  50. goto out;
  51. dev_dbg(&dev->dev, "Sony decode started at state %d (%uus %s)\n",
  52. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  53. switch (data->state) {
  54. case STATE_INACTIVE:
  55. if (!ev.pulse)
  56. break;
  57. if (!eq_margin(ev.duration, SONY_HEADER_PULSE, SONY_UNIT / 2))
  58. break;
  59. data->count = 0;
  60. data->state = STATE_HEADER_SPACE;
  61. return 0;
  62. case STATE_HEADER_SPACE:
  63. if (ev.pulse)
  64. break;
  65. if (!eq_margin(ev.duration, SONY_HEADER_SPACE, SONY_UNIT / 2))
  66. break;
  67. data->state = STATE_BIT_PULSE;
  68. return 0;
  69. case STATE_BIT_PULSE:
  70. if (!ev.pulse)
  71. break;
  72. data->bits <<= 1;
  73. if (eq_margin(ev.duration, SONY_BIT_1_PULSE, SONY_UNIT / 2))
  74. data->bits |= 1;
  75. else if (!eq_margin(ev.duration, SONY_BIT_0_PULSE, SONY_UNIT / 2))
  76. break;
  77. data->count++;
  78. data->state = STATE_BIT_SPACE;
  79. return 0;
  80. case STATE_BIT_SPACE:
  81. if (ev.pulse)
  82. break;
  83. if (!geq_margin(ev.duration, SONY_BIT_SPACE, SONY_UNIT / 2))
  84. break;
  85. decrease_duration(&ev, SONY_BIT_SPACE);
  86. if (!geq_margin(ev.duration, SONY_UNIT, SONY_UNIT / 2)) {
  87. data->state = STATE_BIT_PULSE;
  88. return 0;
  89. }
  90. data->state = STATE_FINISHED;
  91. /* Fall through */
  92. case STATE_FINISHED:
  93. if (ev.pulse)
  94. break;
  95. if (!geq_margin(ev.duration, SONY_TRAILER_SPACE, SONY_UNIT / 2))
  96. break;
  97. switch (data->count) {
  98. case 12:
  99. if (!(dev->enabled_protocols & RC_PROTO_BIT_SONY12))
  100. goto finish_state_machine;
  101. device = bitrev8((data->bits << 3) & 0xF8);
  102. subdevice = 0;
  103. function = bitrev8((data->bits >> 4) & 0xFE);
  104. protocol = RC_PROTO_SONY12;
  105. break;
  106. case 15:
  107. if (!(dev->enabled_protocols & RC_PROTO_BIT_SONY15))
  108. goto finish_state_machine;
  109. device = bitrev8((data->bits >> 0) & 0xFF);
  110. subdevice = 0;
  111. function = bitrev8((data->bits >> 7) & 0xFE);
  112. protocol = RC_PROTO_SONY15;
  113. break;
  114. case 20:
  115. if (!(dev->enabled_protocols & RC_PROTO_BIT_SONY20))
  116. goto finish_state_machine;
  117. device = bitrev8((data->bits >> 5) & 0xF8);
  118. subdevice = bitrev8((data->bits >> 0) & 0xFF);
  119. function = bitrev8((data->bits >> 12) & 0xFE);
  120. protocol = RC_PROTO_SONY20;
  121. break;
  122. default:
  123. dev_dbg(&dev->dev, "Sony invalid bitcount %u\n",
  124. data->count);
  125. goto out;
  126. }
  127. scancode = device << 16 | subdevice << 8 | function;
  128. dev_dbg(&dev->dev, "Sony(%u) scancode 0x%05x\n", data->count,
  129. scancode);
  130. rc_keydown(dev, protocol, scancode, 0);
  131. goto finish_state_machine;
  132. }
  133. out:
  134. dev_dbg(&dev->dev, "Sony decode failed at state %d (%uus %s)\n",
  135. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  136. data->state = STATE_INACTIVE;
  137. return -EINVAL;
  138. finish_state_machine:
  139. data->state = STATE_INACTIVE;
  140. return 0;
  141. }
  142. static const struct ir_raw_timings_pl ir_sony_timings = {
  143. .header_pulse = SONY_HEADER_PULSE,
  144. .bit_space = SONY_BIT_SPACE,
  145. .bit_pulse[0] = SONY_BIT_0_PULSE,
  146. .bit_pulse[1] = SONY_BIT_1_PULSE,
  147. .trailer_space = SONY_TRAILER_SPACE + SONY_BIT_SPACE,
  148. .msb_first = 0,
  149. };
  150. /**
  151. * ir_sony_encode() - Encode a scancode as a stream of raw events
  152. *
  153. * @protocol: protocol to encode
  154. * @scancode: scancode to encode
  155. * @events: array of raw ir events to write into
  156. * @max: maximum size of @events
  157. *
  158. * Returns: The number of events written.
  159. * -ENOBUFS if there isn't enough space in the array to fit the
  160. * encoding. In this case all @max events will have been written.
  161. */
  162. static int ir_sony_encode(enum rc_proto protocol, u32 scancode,
  163. struct ir_raw_event *events, unsigned int max)
  164. {
  165. struct ir_raw_event *e = events;
  166. u32 raw, len;
  167. int ret;
  168. if (protocol == RC_PROTO_SONY12) {
  169. raw = (scancode & 0x7f) | ((scancode & 0x1f0000) >> 9);
  170. len = 12;
  171. } else if (protocol == RC_PROTO_SONY15) {
  172. raw = (scancode & 0x7f) | ((scancode & 0xff0000) >> 9);
  173. len = 15;
  174. } else {
  175. raw = (scancode & 0x7f) | ((scancode & 0x1f0000) >> 9) |
  176. ((scancode & 0xff00) << 4);
  177. len = 20;
  178. }
  179. ret = ir_raw_gen_pl(&e, max, &ir_sony_timings, len, raw);
  180. if (ret < 0)
  181. return ret;
  182. return e - events;
  183. }
  184. static struct ir_raw_handler sony_handler = {
  185. .protocols = RC_PROTO_BIT_SONY12 | RC_PROTO_BIT_SONY15 |
  186. RC_PROTO_BIT_SONY20,
  187. .decode = ir_sony_decode,
  188. .encode = ir_sony_encode,
  189. .carrier = 40000,
  190. .min_timeout = SONY_TRAILER_SPACE,
  191. };
  192. static int __init ir_sony_decode_init(void)
  193. {
  194. ir_raw_handler_register(&sony_handler);
  195. printk(KERN_INFO "IR Sony protocol handler initialized\n");
  196. return 0;
  197. }
  198. static void __exit ir_sony_decode_exit(void)
  199. {
  200. ir_raw_handler_unregister(&sony_handler);
  201. }
  202. module_init(ir_sony_decode_init);
  203. module_exit(ir_sony_decode_exit);
  204. MODULE_LICENSE("GPL");
  205. MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
  206. MODULE_DESCRIPTION("Sony IR protocol decoder");