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 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> 31 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 36 37 static bool debug; 38 39 struct loopback_dev { 40 struct rc_dev *dev; 41 u32 txmask; 42 u32 txcarrier; 43 u32 txduty; 44 bool idle; 45 bool learning; 46 bool carrierreport; 47 u32 rxcarriermin; 48 u32 rxcarriermax; 49 }; 50 51 static struct loopback_dev loopdev; 52 53 static int loop_set_tx_mask(struct rc_dev *dev, u32 mask) 54 { 55 struct loopback_dev *lodev = dev->priv; 56 57 if ((mask & (RXMASK_REGULAR | RXMASK_LEARNING)) != mask) { 58 dprintk("invalid tx mask: %u\n", mask); 59 return -EINVAL; 60 } 61 62 dprintk("setting tx mask: %u\n", mask); 63 lodev->txmask = mask; 64 return 0; 65 } 66 67 static int loop_set_tx_carrier(struct rc_dev *dev, u32 carrier) 68 { 69 struct loopback_dev *lodev = dev->priv; 70 71 dprintk("setting tx carrier: %u\n", carrier); 72 lodev->txcarrier = carrier; 73 return 0; 74 } 75 76 static int loop_set_tx_duty_cycle(struct rc_dev *dev, u32 duty_cycle) 77 { 78 struct loopback_dev *lodev = dev->priv; 79 80 if (duty_cycle < 1 || duty_cycle > 99) { 81 dprintk("invalid duty cycle: %u\n", duty_cycle); 82 return -EINVAL; 83 } 84 85 dprintk("setting duty cycle: %u\n", duty_cycle); 86 lodev->txduty = duty_cycle; 87 return 0; 88 } 89 90 static int loop_set_rx_carrier_range(struct rc_dev *dev, u32 min, u32 max) 91 { 92 struct loopback_dev *lodev = dev->priv; 93 94 if (min < 1 || min > max) { 95 dprintk("invalid rx carrier range %u to %u\n", min, max); 96 return -EINVAL; 97 } 98 99 dprintk("setting rx carrier range %u to %u\n", min, max); 100 lodev->rxcarriermin = min; 101 lodev->rxcarriermax = max; 102 return 0; 103 } 104 105 static int loop_tx_ir(struct rc_dev *dev, unsigned *txbuf, unsigned count) 106 { 107 struct loopback_dev *lodev = dev->priv; 108 u32 rxmask; 109 unsigned i; 110 DEFINE_IR_RAW_EVENT(rawir); 111 112 if (lodev->txcarrier < lodev->rxcarriermin || 113 lodev->txcarrier > lodev->rxcarriermax) { 114 dprintk("ignoring tx, carrier out of range\n"); 115 goto out; 116 } 117 118 if (lodev->learning) 119 rxmask = RXMASK_LEARNING; 120 else 121 rxmask = RXMASK_REGULAR; 122 123 if (!(rxmask & lodev->txmask)) { 124 dprintk("ignoring tx, rx mask mismatch\n"); 125 goto out; 126 } 127 128 for (i = 0; i < count; i++) { 129 rawir.pulse = i % 2 ? false : true; 130 rawir.duration = txbuf[i] * 1000; 131 if (rawir.duration) 132 ir_raw_event_store_with_filter(dev, &rawir); 133 } 134 135 /* Fake a silence long enough to cause us to go idle */ 136 rawir.pulse = false; 137 rawir.duration = dev->timeout; 138 ir_raw_event_store_with_filter(dev, &rawir); 139 140 ir_raw_event_handle(dev); 141 142 out: 143 return count; 144 } 145 146 static void loop_set_idle(struct rc_dev *dev, bool enable) 147 { 148 struct loopback_dev *lodev = dev->priv; 149 150 if (lodev->idle != enable) { 151 dprintk("%sing idle mode\n", enable ? "enter" : "exit"); 152 lodev->idle = enable; 153 } 154 } 155 156 static int loop_set_learning_mode(struct rc_dev *dev, int enable) 157 { 158 struct loopback_dev *lodev = dev->priv; 159 160 if (lodev->learning != enable) { 161 dprintk("%sing learning mode\n", enable ? "enter" : "exit"); 162 lodev->learning = !!enable; 163 } 164 165 return 0; 166 } 167 168 static int loop_set_carrier_report(struct rc_dev *dev, int enable) 169 { 170 struct loopback_dev *lodev = dev->priv; 171 172 if (lodev->carrierreport != enable) { 173 dprintk("%sabling carrier reports\n", enable ? "en" : "dis"); 174 lodev->carrierreport = !!enable; 175 } 176 177 return 0; 178 } 179 180 static int loop_set_wakeup_filter(struct rc_dev *dev, 181 struct rc_scancode_filter *sc_filter) 182 { 183 static const unsigned int max = 512; 184 struct ir_raw_event *raw; 185 int ret; 186 int i; 187 188 /* fine to disable filter */ 189 if (!sc_filter->mask) 190 return 0; 191 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, 195 raw, max); 196 /* still loop back the partial raw IR even if it's incomplete */ 197 if (ret == -ENOBUFS) 198 ret = max; 199 if (ret >= 0) { 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); 204 205 ret = 0; 206 } 207 208 kfree(raw); 209 210 return ret; 211 } 212 213 static int __init loop_init(void) 214 { 215 struct rc_dev *rc; 216 int ret; 217 218 rc = rc_allocate_device(); 219 if (!rc) { 220 printk(KERN_ERR DRIVER_NAME ": rc_dev allocation failed\n"); 221 return -ENOMEM; 222 } 223 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; 230 rc->priv = &loopdev; 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 */ 235 rc->min_timeout = 1; 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; 248 249 loopdev.txmask = RXMASK_REGULAR; 250 loopdev.txcarrier = 36000; 251 loopdev.txduty = 50; 252 loopdev.rxcarriermin = 1; 253 loopdev.rxcarriermax = ~0; 254 loopdev.idle = true; 255 loopdev.learning = false; 256 loopdev.carrierreport = false; 257 258 ret = rc_register_device(rc); 259 if (ret < 0) { 260 printk(KERN_ERR DRIVER_NAME ": rc_dev registration failed\n"); 261 rc_free_device(rc); 262 return ret; 263 } 264 265 loopdev.dev = rc; 266 return 0; 267 } 268 269 static void __exit loop_exit(void) 270 { 271 rc_unregister_device(loopdev.dev); 272 } 273 274 module_init(loop_init); 275 module_exit(loop_exit); 276 277 module_param(debug, bool, S_IRUGO | S_IWUSR); 278 MODULE_PARM_DESC(debug, "Enable debug messages"); 279 280 MODULE_DESCRIPTION("Loopback device for rc-core debugging"); 281 MODULE_AUTHOR("David Härdeman <david@hardeman.nu>"); 282 MODULE_LICENSE("GPL"); 283