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 <linux/slab.h>
30 #include <media/rc-core.h>
32 #define DRIVER_NAME "rc-loopback"
33 #define dprintk(x...) if (debug) printk(KERN_INFO DRIVER_NAME ": " x)
34 #define RXMASK_REGULAR 0x1
35 #define RXMASK_LEARNING 0x2
51 static struct loopback_dev loopdev;
53 static int loop_set_tx_mask(struct rc_dev *dev, u32 mask)
55 struct loopback_dev *lodev = dev->priv;
57 if ((mask & (RXMASK_REGULAR | RXMASK_LEARNING)) != mask) {
58 dprintk("invalid tx mask: %u\n", mask);
62 dprintk("setting tx mask: %u\n", mask);
67 static int loop_set_tx_carrier(struct rc_dev *dev, u32 carrier)
69 struct loopback_dev *lodev = dev->priv;
71 dprintk("setting tx carrier: %u\n", carrier);
72 lodev->txcarrier = carrier;
76 static int loop_set_tx_duty_cycle(struct rc_dev *dev, u32 duty_cycle)
78 struct loopback_dev *lodev = dev->priv;
80 if (duty_cycle < 1 || duty_cycle > 99) {
81 dprintk("invalid duty cycle: %u\n", duty_cycle);
85 dprintk("setting duty cycle: %u\n", duty_cycle);
86 lodev->txduty = duty_cycle;
90 static int loop_set_rx_carrier_range(struct rc_dev *dev, u32 min, u32 max)
92 struct loopback_dev *lodev = dev->priv;
94 if (min < 1 || min > max) {
95 dprintk("invalid rx carrier range %u to %u\n", min, max);
99 dprintk("setting rx carrier range %u to %u\n", min, max);
100 lodev->rxcarriermin = min;
101 lodev->rxcarriermax = max;
105 static int loop_tx_ir(struct rc_dev *dev, unsigned *txbuf, unsigned count)
107 struct loopback_dev *lodev = dev->priv;
110 DEFINE_IR_RAW_EVENT(rawir);
112 if (lodev->txcarrier < lodev->rxcarriermin ||
113 lodev->txcarrier > lodev->rxcarriermax) {
114 dprintk("ignoring tx, carrier out of range\n");
119 rxmask = RXMASK_LEARNING;
121 rxmask = RXMASK_REGULAR;
123 if (!(rxmask & lodev->txmask)) {
124 dprintk("ignoring tx, rx mask mismatch\n");
128 for (i = 0; i < count; i++) {
129 rawir.pulse = i % 2 ? false : true;
130 rawir.duration = txbuf[i] * 1000;
132 ir_raw_event_store_with_filter(dev, &rawir);
135 /* Fake a silence long enough to cause us to go idle */
137 rawir.duration = dev->timeout;
138 ir_raw_event_store_with_filter(dev, &rawir);
140 ir_raw_event_handle(dev);
146 static void loop_set_idle(struct rc_dev *dev, bool enable)
148 struct loopback_dev *lodev = dev->priv;
150 if (lodev->idle != enable) {
151 dprintk("%sing idle mode\n", enable ? "enter" : "exit");
152 lodev->idle = enable;
156 static int loop_set_learning_mode(struct rc_dev *dev, int enable)
158 struct loopback_dev *lodev = dev->priv;
160 if (lodev->learning != enable) {
161 dprintk("%sing learning mode\n", enable ? "enter" : "exit");
162 lodev->learning = !!enable;
168 static int loop_set_carrier_report(struct rc_dev *dev, int enable)
170 struct loopback_dev *lodev = dev->priv;
172 if (lodev->carrierreport != enable) {
173 dprintk("%sabling carrier reports\n", enable ? "en" : "dis");
174 lodev->carrierreport = !!enable;
180 static int loop_set_wakeup_filter(struct rc_dev *dev,
181 struct rc_scancode_filter *sc_filter)
183 static const unsigned int max = 512;
184 struct ir_raw_event *raw;
188 /* fine to disable filter */
189 if (!sc_filter->mask)
192 /* encode the specified filter and loop it back */
193 raw = kmalloc_array(max, sizeof(*raw), GFP_KERNEL);
194 ret = ir_raw_encode_scancode(dev->enabled_wakeup_protocols, sc_filter,
196 /* still loop back the partial raw IR even if it's incomplete */
200 /* do the loopback */
201 for (i = 0; i < ret; ++i)
202 ir_raw_event_store(dev, &raw[i]);
203 ir_raw_event_handle(dev);
213 static int __init loop_init(void)
218 rc = rc_allocate_device();
220 printk(KERN_ERR DRIVER_NAME ": rc_dev allocation failed\n");
224 rc->input_name = "rc-core loopback device";
225 rc->input_phys = "rc-core/virtual";
226 rc->input_id.bustype = BUS_VIRTUAL;
227 rc->input_id.version = 1;
228 rc->driver_name = DRIVER_NAME;
229 rc->map_name = RC_MAP_EMPTY;
231 rc->driver_type = RC_DRIVER_IR_RAW;
232 rc->encode_wakeup = true;
233 rc->allowed_protocols = RC_BIT_ALL;
234 rc->timeout = 100 * 1000 * 1000; /* 100 ms */
236 rc->max_timeout = UINT_MAX;
237 rc->rx_resolution = 1000;
238 rc->tx_resolution = 1000;
239 rc->s_tx_mask = loop_set_tx_mask;
240 rc->s_tx_carrier = loop_set_tx_carrier;
241 rc->s_tx_duty_cycle = loop_set_tx_duty_cycle;
242 rc->s_rx_carrier_range = loop_set_rx_carrier_range;
243 rc->tx_ir = loop_tx_ir;
244 rc->s_idle = loop_set_idle;
245 rc->s_learning_mode = loop_set_learning_mode;
246 rc->s_carrier_report = loop_set_carrier_report;
247 rc->s_wakeup_filter = loop_set_wakeup_filter;
249 loopdev.txmask = RXMASK_REGULAR;
250 loopdev.txcarrier = 36000;
252 loopdev.rxcarriermin = 1;
253 loopdev.rxcarriermax = ~0;
255 loopdev.learning = false;
256 loopdev.carrierreport = false;
258 ret = rc_register_device(rc);
260 printk(KERN_ERR DRIVER_NAME ": rc_dev registration failed\n");
269 static void __exit loop_exit(void)
271 rc_unregister_device(loopdev.dev);
274 module_init(loop_init);
275 module_exit(loop_exit);
277 module_param(debug, bool, S_IRUGO | S_IWUSR);
278 MODULE_PARM_DESC(debug, "Enable debug messages");
280 MODULE_DESCRIPTION("Loopback device for rc-core debugging");
281 MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
282 MODULE_LICENSE("GPL");