2 * Loopback driver for rc-core,
4 * Copyright (c) 2010 David Härdeman <david@hardeman.nu>
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.
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.
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.
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
26 #include <linux/device.h>
27 #include <linux/module.h>
28 #include <linux/sched.h>
29 #include <media/rc-core.h>
31 #define DRIVER_NAME "rc-loopback"
32 #define dprintk(x...) if (debug) printk(KERN_INFO DRIVER_NAME ": " x)
33 #define RXMASK_REGULAR 0x1
34 #define RXMASK_LEARNING 0x2
50 static struct loopback_dev loopdev;
52 static int loop_set_tx_mask(struct rc_dev *dev, u32 mask)
54 struct loopback_dev *lodev = dev->priv;
56 if ((mask & (RXMASK_REGULAR | RXMASK_LEARNING)) != mask) {
57 dprintk("invalid tx mask: %u\n", mask);
61 dprintk("setting tx mask: %u\n", mask);
66 static int loop_set_tx_carrier(struct rc_dev *dev, u32 carrier)
68 struct loopback_dev *lodev = dev->priv;
70 dprintk("setting tx carrier: %u\n", carrier);
71 lodev->txcarrier = carrier;
75 static int loop_set_tx_duty_cycle(struct rc_dev *dev, u32 duty_cycle)
77 struct loopback_dev *lodev = dev->priv;
79 if (duty_cycle < 1 || duty_cycle > 99) {
80 dprintk("invalid duty cycle: %u\n", duty_cycle);
84 dprintk("setting duty cycle: %u\n", duty_cycle);
85 lodev->txduty = duty_cycle;
89 static int loop_set_rx_carrier_range(struct rc_dev *dev, u32 min, u32 max)
91 struct loopback_dev *lodev = dev->priv;
93 if (min < 1 || min > max) {
94 dprintk("invalid rx carrier range %u to %u\n", min, max);
98 dprintk("setting rx carrier range %u to %u\n", min, max);
99 lodev->rxcarriermin = min;
100 lodev->rxcarriermax = max;
104 static int loop_tx_ir(struct rc_dev *dev, unsigned *txbuf, unsigned count)
106 struct loopback_dev *lodev = dev->priv;
109 DEFINE_IR_RAW_EVENT(rawir);
111 if (lodev->txcarrier < lodev->rxcarriermin ||
112 lodev->txcarrier > lodev->rxcarriermax) {
113 dprintk("ignoring tx, carrier out of range\n");
118 rxmask = RXMASK_LEARNING;
120 rxmask = RXMASK_REGULAR;
122 if (!(rxmask & lodev->txmask)) {
123 dprintk("ignoring tx, rx mask mismatch\n");
127 for (i = 0; i < count; i++) {
128 rawir.pulse = i % 2 ? false : true;
129 rawir.duration = txbuf[i] * 1000;
131 ir_raw_event_store_with_filter(dev, &rawir);
134 /* Fake a silence long enough to cause us to go idle */
136 rawir.duration = dev->timeout;
137 ir_raw_event_store_with_filter(dev, &rawir);
139 ir_raw_event_handle(dev);
145 static void loop_set_idle(struct rc_dev *dev, bool enable)
147 struct loopback_dev *lodev = dev->priv;
149 if (lodev->idle != enable) {
150 dprintk("%sing idle mode\n", enable ? "enter" : "exit");
151 lodev->idle = enable;
155 static int loop_set_learning_mode(struct rc_dev *dev, int enable)
157 struct loopback_dev *lodev = dev->priv;
159 if (lodev->learning != enable) {
160 dprintk("%sing learning mode\n", enable ? "enter" : "exit");
161 lodev->learning = !!enable;
167 static int loop_set_carrier_report(struct rc_dev *dev, int enable)
169 struct loopback_dev *lodev = dev->priv;
171 if (lodev->carrierreport != enable) {
172 dprintk("%sabling carrier reports\n", enable ? "en" : "dis");
173 lodev->carrierreport = !!enable;
179 static int __init loop_init(void)
184 rc = rc_allocate_device();
186 printk(KERN_ERR DRIVER_NAME ": rc_dev allocation failed\n");
190 rc->input_name = "rc-core loopback device";
191 rc->input_phys = "rc-core/virtual";
192 rc->input_id.bustype = BUS_VIRTUAL;
193 rc->input_id.version = 1;
194 rc->driver_name = DRIVER_NAME;
195 rc->map_name = RC_MAP_EMPTY;
197 rc->driver_type = RC_DRIVER_IR_RAW;
198 rc->allowed_protos = RC_BIT_ALL;
199 rc->timeout = 100 * 1000 * 1000; /* 100 ms */
201 rc->max_timeout = UINT_MAX;
202 rc->rx_resolution = 1000;
203 rc->tx_resolution = 1000;
204 rc->s_tx_mask = loop_set_tx_mask;
205 rc->s_tx_carrier = loop_set_tx_carrier;
206 rc->s_tx_duty_cycle = loop_set_tx_duty_cycle;
207 rc->s_rx_carrier_range = loop_set_rx_carrier_range;
208 rc->tx_ir = loop_tx_ir;
209 rc->s_idle = loop_set_idle;
210 rc->s_learning_mode = loop_set_learning_mode;
211 rc->s_carrier_report = loop_set_carrier_report;
213 loopdev.txmask = RXMASK_REGULAR;
214 loopdev.txcarrier = 36000;
216 loopdev.rxcarriermin = 1;
217 loopdev.rxcarriermax = ~0;
219 loopdev.learning = false;
220 loopdev.carrierreport = false;
222 ret = rc_register_device(rc);
224 printk(KERN_ERR DRIVER_NAME ": rc_dev registration failed\n");
233 static void __exit loop_exit(void)
235 rc_unregister_device(loopdev.dev);
238 module_init(loop_init);
239 module_exit(loop_exit);
241 module_param(debug, bool, S_IRUGO | S_IWUSR);
242 MODULE_PARM_DESC(debug, "Enable debug messages");
244 MODULE_DESCRIPTION("Loopback device for rc-core debugging");
245 MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
246 MODULE_LICENSE("GPL");