rc-loopback.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Loopback driver for rc-core,
  3. *
  4. * Copyright (c) 2010 David Härdeman <david@hardeman.nu>
  5. *
  6. * This driver receives TX data and passes it back as RX data,
  7. * which is useful for (scripted) debugging of rc-core without
  8. * having to use actual hardware.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. */
  25. #include <linux/device.h>
  26. #include <linux/module.h>
  27. #include <linux/sched.h>
  28. #include <media/rc-core.h>
  29. #define DRIVER_NAME "rc-loopback"
  30. #define dprintk(x...) if (debug) printk(KERN_INFO DRIVER_NAME ": " x)
  31. #define RXMASK_REGULAR 0x1
  32. #define RXMASK_LEARNING 0x2
  33. static bool debug;
  34. struct loopback_dev {
  35. struct rc_dev *dev;
  36. u32 txmask;
  37. u32 txcarrier;
  38. u32 txduty;
  39. bool idle;
  40. bool learning;
  41. bool carrierreport;
  42. u32 rxcarriermin;
  43. u32 rxcarriermax;
  44. };
  45. static struct loopback_dev loopdev;
  46. static int loop_set_tx_mask(struct rc_dev *dev, u32 mask)
  47. {
  48. struct loopback_dev *lodev = dev->priv;
  49. if ((mask & (RXMASK_REGULAR | RXMASK_LEARNING)) != mask) {
  50. dprintk("invalid tx mask: %u\n", mask);
  51. return -EINVAL;
  52. }
  53. dprintk("setting tx mask: %u\n", mask);
  54. lodev->txmask = mask;
  55. return 0;
  56. }
  57. static int loop_set_tx_carrier(struct rc_dev *dev, u32 carrier)
  58. {
  59. struct loopback_dev *lodev = dev->priv;
  60. dprintk("setting tx carrier: %u\n", carrier);
  61. lodev->txcarrier = carrier;
  62. return 0;
  63. }
  64. static int loop_set_tx_duty_cycle(struct rc_dev *dev, u32 duty_cycle)
  65. {
  66. struct loopback_dev *lodev = dev->priv;
  67. if (duty_cycle < 1 || duty_cycle > 99) {
  68. dprintk("invalid duty cycle: %u\n", duty_cycle);
  69. return -EINVAL;
  70. }
  71. dprintk("setting duty cycle: %u\n", duty_cycle);
  72. lodev->txduty = duty_cycle;
  73. return 0;
  74. }
  75. static int loop_set_rx_carrier_range(struct rc_dev *dev, u32 min, u32 max)
  76. {
  77. struct loopback_dev *lodev = dev->priv;
  78. if (min < 1 || min > max) {
  79. dprintk("invalid rx carrier range %u to %u\n", min, max);
  80. return -EINVAL;
  81. }
  82. dprintk("setting rx carrier range %u to %u\n", min, max);
  83. lodev->rxcarriermin = min;
  84. lodev->rxcarriermax = max;
  85. return 0;
  86. }
  87. static int loop_tx_ir(struct rc_dev *dev, unsigned *txbuf, unsigned count)
  88. {
  89. struct loopback_dev *lodev = dev->priv;
  90. u32 rxmask;
  91. unsigned i;
  92. DEFINE_IR_RAW_EVENT(rawir);
  93. if (lodev->txcarrier < lodev->rxcarriermin ||
  94. lodev->txcarrier > lodev->rxcarriermax) {
  95. dprintk("ignoring tx, carrier out of range\n");
  96. goto out;
  97. }
  98. if (lodev->learning)
  99. rxmask = RXMASK_LEARNING;
  100. else
  101. rxmask = RXMASK_REGULAR;
  102. if (!(rxmask & lodev->txmask)) {
  103. dprintk("ignoring tx, rx mask mismatch\n");
  104. goto out;
  105. }
  106. for (i = 0; i < count; i++) {
  107. rawir.pulse = i % 2 ? false : true;
  108. rawir.duration = txbuf[i] * 1000;
  109. if (rawir.duration)
  110. ir_raw_event_store_with_filter(dev, &rawir);
  111. }
  112. /* Fake a silence long enough to cause us to go idle */
  113. rawir.pulse = false;
  114. rawir.duration = dev->timeout;
  115. ir_raw_event_store_with_filter(dev, &rawir);
  116. ir_raw_event_handle(dev);
  117. out:
  118. return count;
  119. }
  120. static void loop_set_idle(struct rc_dev *dev, bool enable)
  121. {
  122. struct loopback_dev *lodev = dev->priv;
  123. if (lodev->idle != enable) {
  124. dprintk("%sing idle mode\n", enable ? "enter" : "exit");
  125. lodev->idle = enable;
  126. }
  127. }
  128. static int loop_set_learning_mode(struct rc_dev *dev, int enable)
  129. {
  130. struct loopback_dev *lodev = dev->priv;
  131. if (lodev->learning != enable) {
  132. dprintk("%sing learning mode\n", enable ? "enter" : "exit");
  133. lodev->learning = !!enable;
  134. }
  135. return 0;
  136. }
  137. static int loop_set_carrier_report(struct rc_dev *dev, int enable)
  138. {
  139. struct loopback_dev *lodev = dev->priv;
  140. if (lodev->carrierreport != enable) {
  141. dprintk("%sabling carrier reports\n", enable ? "en" : "dis");
  142. lodev->carrierreport = !!enable;
  143. }
  144. return 0;
  145. }
  146. static int __init loop_init(void)
  147. {
  148. struct rc_dev *rc;
  149. int ret;
  150. rc = rc_allocate_device();
  151. if (!rc) {
  152. printk(KERN_ERR DRIVER_NAME ": rc_dev allocation failed\n");
  153. return -ENOMEM;
  154. }
  155. rc->input_name = "rc-core loopback device";
  156. rc->input_phys = "rc-core/virtual";
  157. rc->input_id.bustype = BUS_VIRTUAL;
  158. rc->input_id.version = 1;
  159. rc->driver_name = DRIVER_NAME;
  160. rc->map_name = RC_MAP_EMPTY;
  161. rc->priv = &loopdev;
  162. rc->driver_type = RC_DRIVER_IR_RAW;
  163. rc->allowed_protocols = RC_BIT_ALL;
  164. rc->timeout = 100 * 1000 * 1000; /* 100 ms */
  165. rc->min_timeout = 1;
  166. rc->max_timeout = UINT_MAX;
  167. rc->rx_resolution = 1000;
  168. rc->tx_resolution = 1000;
  169. rc->s_tx_mask = loop_set_tx_mask;
  170. rc->s_tx_carrier = loop_set_tx_carrier;
  171. rc->s_tx_duty_cycle = loop_set_tx_duty_cycle;
  172. rc->s_rx_carrier_range = loop_set_rx_carrier_range;
  173. rc->tx_ir = loop_tx_ir;
  174. rc->s_idle = loop_set_idle;
  175. rc->s_learning_mode = loop_set_learning_mode;
  176. rc->s_carrier_report = loop_set_carrier_report;
  177. loopdev.txmask = RXMASK_REGULAR;
  178. loopdev.txcarrier = 36000;
  179. loopdev.txduty = 50;
  180. loopdev.rxcarriermin = 1;
  181. loopdev.rxcarriermax = ~0;
  182. loopdev.idle = true;
  183. loopdev.learning = false;
  184. loopdev.carrierreport = false;
  185. ret = rc_register_device(rc);
  186. if (ret < 0) {
  187. printk(KERN_ERR DRIVER_NAME ": rc_dev registration failed\n");
  188. rc_free_device(rc);
  189. return ret;
  190. }
  191. loopdev.dev = rc;
  192. return 0;
  193. }
  194. static void __exit loop_exit(void)
  195. {
  196. rc_unregister_device(loopdev.dev);
  197. }
  198. module_init(loop_init);
  199. module_exit(loop_exit);
  200. module_param(debug, bool, S_IRUGO | S_IWUSR);
  201. MODULE_PARM_DESC(debug, "Enable debug messages");
  202. MODULE_DESCRIPTION("Loopback device for rc-core debugging");
  203. MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
  204. MODULE_LICENSE("GPL");