rc-loopback.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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, int *txbuf, u32 n)
  88. {
  89. struct loopback_dev *lodev = dev->priv;
  90. u32 rxmask;
  91. unsigned count;
  92. unsigned total_duration = 0;
  93. unsigned i;
  94. DEFINE_IR_RAW_EVENT(rawir);
  95. if (n == 0 || n % sizeof(int)) {
  96. dprintk("invalid tx buffer size\n");
  97. return -EINVAL;
  98. }
  99. count = n / sizeof(int);
  100. for (i = 0; i < count; i++)
  101. total_duration += abs(txbuf[i]);
  102. if (total_duration == 0) {
  103. dprintk("invalid tx data, total duration zero\n");
  104. return -EINVAL;
  105. }
  106. if (lodev->txcarrier < lodev->rxcarriermin ||
  107. lodev->txcarrier > lodev->rxcarriermax) {
  108. dprintk("ignoring tx, carrier out of range\n");
  109. goto out;
  110. }
  111. if (lodev->learning)
  112. rxmask = RXMASK_LEARNING;
  113. else
  114. rxmask = RXMASK_REGULAR;
  115. if (!(rxmask & lodev->txmask)) {
  116. dprintk("ignoring tx, rx mask mismatch\n");
  117. goto out;
  118. }
  119. for (i = 0; i < count; i++) {
  120. rawir.pulse = i % 2 ? false : true;
  121. rawir.duration = abs(txbuf[i]) * 1000;
  122. if (rawir.duration)
  123. ir_raw_event_store_with_filter(dev, &rawir);
  124. }
  125. ir_raw_event_handle(dev);
  126. out:
  127. /* Lirc expects this function to take as long as the total duration */
  128. set_current_state(TASK_INTERRUPTIBLE);
  129. schedule_timeout(usecs_to_jiffies(total_duration));
  130. return n;
  131. }
  132. static void loop_set_idle(struct rc_dev *dev, bool enable)
  133. {
  134. struct loopback_dev *lodev = dev->priv;
  135. if (lodev->idle != enable) {
  136. dprintk("%sing idle mode\n", enable ? "enter" : "exit");
  137. lodev->idle = enable;
  138. }
  139. }
  140. static int loop_set_learning_mode(struct rc_dev *dev, int enable)
  141. {
  142. struct loopback_dev *lodev = dev->priv;
  143. if (lodev->learning != enable) {
  144. dprintk("%sing learning mode\n", enable ? "enter" : "exit");
  145. lodev->learning = !!enable;
  146. }
  147. return 0;
  148. }
  149. static int loop_set_carrier_report(struct rc_dev *dev, int enable)
  150. {
  151. struct loopback_dev *lodev = dev->priv;
  152. if (lodev->carrierreport != enable) {
  153. dprintk("%sabling carrier reports\n", enable ? "en" : "dis");
  154. lodev->carrierreport = !!enable;
  155. }
  156. return 0;
  157. }
  158. static int __init loop_init(void)
  159. {
  160. struct rc_dev *rc;
  161. int ret;
  162. rc = rc_allocate_device();
  163. if (!rc) {
  164. printk(KERN_ERR DRIVER_NAME ": rc_dev allocation failed\n");
  165. return -ENOMEM;
  166. }
  167. rc->input_name = "rc-core loopback device";
  168. rc->input_phys = "rc-core/virtual";
  169. rc->input_id.bustype = BUS_VIRTUAL;
  170. rc->input_id.version = 1;
  171. rc->driver_name = DRIVER_NAME;
  172. rc->map_name = RC_MAP_EMPTY;
  173. rc->priv = &loopdev;
  174. rc->driver_type = RC_DRIVER_IR_RAW;
  175. rc->allowed_protos = RC_TYPE_ALL;
  176. rc->timeout = 100 * 1000 * 1000; /* 100 ms */
  177. rc->min_timeout = 1;
  178. rc->max_timeout = UINT_MAX;
  179. rc->rx_resolution = 1000;
  180. rc->tx_resolution = 1000;
  181. rc->s_tx_mask = loop_set_tx_mask;
  182. rc->s_tx_carrier = loop_set_tx_carrier;
  183. rc->s_tx_duty_cycle = loop_set_tx_duty_cycle;
  184. rc->s_rx_carrier_range = loop_set_rx_carrier_range;
  185. rc->tx_ir = loop_tx_ir;
  186. rc->s_idle = loop_set_idle;
  187. rc->s_learning_mode = loop_set_learning_mode;
  188. rc->s_carrier_report = loop_set_carrier_report;
  189. rc->priv = &loopdev;
  190. loopdev.txmask = RXMASK_REGULAR;
  191. loopdev.txcarrier = 36000;
  192. loopdev.txduty = 50;
  193. loopdev.rxcarriermin = 1;
  194. loopdev.rxcarriermax = ~0;
  195. loopdev.idle = true;
  196. loopdev.learning = false;
  197. loopdev.carrierreport = false;
  198. ret = rc_register_device(rc);
  199. if (ret < 0) {
  200. printk(KERN_ERR DRIVER_NAME ": rc_dev registration failed\n");
  201. rc_free_device(rc);
  202. return ret;
  203. }
  204. loopdev.dev = rc;
  205. return 0;
  206. }
  207. static void __exit loop_exit(void)
  208. {
  209. rc_unregister_device(loopdev.dev);
  210. }
  211. module_init(loop_init);
  212. module_exit(loop_exit);
  213. module_param(debug, bool, S_IRUGO | S_IWUSR);
  214. MODULE_PARM_DESC(debug, "Enable debug messages");
  215. MODULE_DESCRIPTION("Loopback device for rc-core debugging");
  216. MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
  217. MODULE_LICENSE("GPL");