ir-rc5-sz-decoder.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* ir-rc5-sz-decoder.c - handle RC5 Streamzap IR Pulse/Space protocol
  2. *
  3. * Copyright (C) 2010 by Mauro Carvalho Chehab <mchehab@redhat.com>
  4. * Copyright (C) 2010 by Jarod Wilson <jarod@redhat.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation version 2 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. /*
  16. * This code handles the 15 bit RC5-ish protocol used by the Streamzap
  17. * PC Remote.
  18. * It considers a carrier of 36 kHz, with a total of 15 bits, where
  19. * the first two bits are start bits, and a third one is a filing bit
  20. */
  21. #include "rc-core-priv.h"
  22. #include <linux/module.h>
  23. #define RC5_SZ_NBITS 15
  24. #define RC5_UNIT 888888 /* ns */
  25. #define RC5_BIT_START (1 * RC5_UNIT)
  26. #define RC5_BIT_END (1 * RC5_UNIT)
  27. enum rc5_sz_state {
  28. STATE_INACTIVE,
  29. STATE_BIT_START,
  30. STATE_BIT_END,
  31. STATE_FINISHED,
  32. };
  33. /**
  34. * ir_rc5_sz_decode() - Decode one RC-5 Streamzap pulse or space
  35. * @dev: the struct rc_dev descriptor of the device
  36. * @ev: the struct ir_raw_event descriptor of the pulse/space
  37. *
  38. * This function returns -EINVAL if the pulse violates the state machine
  39. */
  40. static int ir_rc5_sz_decode(struct rc_dev *dev, struct ir_raw_event ev)
  41. {
  42. struct rc5_sz_dec *data = &dev->raw->rc5_sz;
  43. u8 toggle, command, system;
  44. u32 scancode;
  45. if (!(dev->raw->enabled_protocols & RC_TYPE_RC5_SZ))
  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, RC5_UNIT, RC5_UNIT / 2))
  53. goto out;
  54. again:
  55. IR_dprintk(2, "RC5-sz decode started at state %i (%uus %s)\n",
  56. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  57. if (!geq_margin(ev.duration, RC5_UNIT, RC5_UNIT / 2))
  58. return 0;
  59. switch (data->state) {
  60. case STATE_INACTIVE:
  61. if (!ev.pulse)
  62. break;
  63. data->state = STATE_BIT_START;
  64. data->count = 1;
  65. data->wanted_bits = RC5_SZ_NBITS;
  66. decrease_duration(&ev, RC5_BIT_START);
  67. goto again;
  68. case STATE_BIT_START:
  69. if (!eq_margin(ev.duration, RC5_BIT_START, RC5_UNIT / 2))
  70. break;
  71. data->bits <<= 1;
  72. if (!ev.pulse)
  73. data->bits |= 1;
  74. data->count++;
  75. data->state = STATE_BIT_END;
  76. return 0;
  77. case STATE_BIT_END:
  78. if (!is_transition(&ev, &dev->raw->prev_ev))
  79. break;
  80. if (data->count == data->wanted_bits)
  81. data->state = STATE_FINISHED;
  82. else
  83. data->state = STATE_BIT_START;
  84. decrease_duration(&ev, RC5_BIT_END);
  85. goto again;
  86. case STATE_FINISHED:
  87. if (ev.pulse)
  88. break;
  89. /* RC5-sz */
  90. command = (data->bits & 0x0003F) >> 0;
  91. system = (data->bits & 0x02FC0) >> 6;
  92. toggle = (data->bits & 0x01000) ? 1 : 0;
  93. scancode = system << 6 | command;
  94. IR_dprintk(1, "RC5-sz scancode 0x%04x (toggle: %u)\n",
  95. scancode, toggle);
  96. rc_keydown(dev, scancode, toggle);
  97. data->state = STATE_INACTIVE;
  98. return 0;
  99. }
  100. out:
  101. IR_dprintk(1, "RC5-sz decode failed at state %i (%uus %s)\n",
  102. data->state, TO_US(ev.duration), TO_STR(ev.pulse));
  103. data->state = STATE_INACTIVE;
  104. return -EINVAL;
  105. }
  106. static struct ir_raw_handler rc5_sz_handler = {
  107. .protocols = RC_TYPE_RC5_SZ,
  108. .decode = ir_rc5_sz_decode,
  109. };
  110. static int __init ir_rc5_sz_decode_init(void)
  111. {
  112. ir_raw_handler_register(&rc5_sz_handler);
  113. printk(KERN_INFO "IR RC5 (streamzap) protocol handler initialized\n");
  114. return 0;
  115. }
  116. static void __exit ir_rc5_sz_decode_exit(void)
  117. {
  118. ir_raw_handler_unregister(&rc5_sz_handler);
  119. }
  120. module_init(ir_rc5_sz_decode_init);
  121. module_exit(ir_rc5_sz_decode_exit);
  122. MODULE_LICENSE("GPL");
  123. MODULE_AUTHOR("Jarod Wilson <jarod@redhat.com>");
  124. MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
  125. MODULE_DESCRIPTION("RC5 (streamzap) IR protocol decoder");