linux/drivers/media/rc/rc-loopback.c
<<
>>
Prefs
   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 <media/rc-core.h>
  30
  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
  35
  36static bool debug;
  37
  38struct loopback_dev {
  39        struct rc_dev *dev;
  40        u32 txmask;
  41        u32 txcarrier;
  42        u32 txduty;
  43        bool idle;
  44        bool learning;
  45        bool carrierreport;
  46        u32 rxcarriermin;
  47        u32 rxcarriermax;
  48};
  49
  50static struct loopback_dev loopdev;
  51
  52static int loop_set_tx_mask(struct rc_dev *dev, u32 mask)
  53{
  54        struct loopback_dev *lodev = dev->priv;
  55
  56        if ((mask & (RXMASK_REGULAR | RXMASK_LEARNING)) != mask) {
  57                dprintk("invalid tx mask: %u\n", mask);
  58                return -EINVAL;
  59        }
  60
  61        dprintk("setting tx mask: %u\n", mask);
  62        lodev->txmask = mask;
  63        return 0;
  64}
  65
  66static int loop_set_tx_carrier(struct rc_dev *dev, u32 carrier)
  67{
  68        struct loopback_dev *lodev = dev->priv;
  69
  70        dprintk("setting tx carrier: %u\n", carrier);
  71        lodev->txcarrier = carrier;
  72        return 0;
  73}
  74
  75static int loop_set_tx_duty_cycle(struct rc_dev *dev, u32 duty_cycle)
  76{
  77        struct loopback_dev *lodev = dev->priv;
  78
  79        if (duty_cycle < 1 || duty_cycle > 99) {
  80                dprintk("invalid duty cycle: %u\n", duty_cycle);
  81                return -EINVAL;
  82        }
  83
  84        dprintk("setting duty cycle: %u\n", duty_cycle);
  85        lodev->txduty = duty_cycle;
  86        return 0;
  87}
  88
  89static int loop_set_rx_carrier_range(struct rc_dev *dev, u32 min, u32 max)
  90{
  91        struct loopback_dev *lodev = dev->priv;
  92
  93        if (min < 1 || min > max) {
  94                dprintk("invalid rx carrier range %u to %u\n", min, max);
  95                return -EINVAL;
  96        }
  97
  98        dprintk("setting rx carrier range %u to %u\n", min, max);
  99        lodev->rxcarriermin = min;
 100        lodev->rxcarriermax = max;
 101        return 0;
 102}
 103
 104static int loop_tx_ir(struct rc_dev *dev, unsigned *txbuf, unsigned count)
 105{
 106        struct loopback_dev *lodev = dev->priv;
 107        u32 rxmask;
 108        unsigned total_duration = 0;
 109        unsigned i;
 110        DEFINE_IR_RAW_EVENT(rawir);
 111
 112        for (i = 0; i < count; i++)
 113                total_duration += abs(txbuf[i]);
 114
 115        if (total_duration == 0) {
 116                dprintk("invalid tx data, total duration zero\n");
 117                return -EINVAL;
 118        }
 119
 120        if (lodev->txcarrier < lodev->rxcarriermin ||
 121            lodev->txcarrier > lodev->rxcarriermax) {
 122                dprintk("ignoring tx, carrier out of range\n");
 123                goto out;
 124        }
 125
 126        if (lodev->learning)
 127                rxmask = RXMASK_LEARNING;
 128        else
 129                rxmask = RXMASK_REGULAR;
 130
 131        if (!(rxmask & lodev->txmask)) {
 132                dprintk("ignoring tx, rx mask mismatch\n");
 133                goto out;
 134        }
 135
 136        for (i = 0; i < count; i++) {
 137                rawir.pulse = i % 2 ? false : true;
 138                rawir.duration = txbuf[i] * 1000;
 139                if (rawir.duration)
 140                        ir_raw_event_store_with_filter(dev, &rawir);
 141        }
 142
 143        /* Fake a silence long enough to cause us to go idle */
 144        rawir.pulse = false;
 145        rawir.duration = dev->timeout;
 146        ir_raw_event_store_with_filter(dev, &rawir);
 147
 148        ir_raw_event_handle(dev);
 149
 150out:
 151        /* Lirc expects this function to take as long as the total duration */
 152        set_current_state(TASK_INTERRUPTIBLE);
 153        schedule_timeout(usecs_to_jiffies(total_duration));
 154        return count;
 155}
 156
 157static void loop_set_idle(struct rc_dev *dev, bool enable)
 158{
 159        struct loopback_dev *lodev = dev->priv;
 160
 161        if (lodev->idle != enable) {
 162                dprintk("%sing idle mode\n", enable ? "enter" : "exit");
 163                lodev->idle = enable;
 164        }
 165}
 166
 167static int loop_set_learning_mode(struct rc_dev *dev, int enable)
 168{
 169        struct loopback_dev *lodev = dev->priv;
 170
 171        if (lodev->learning != enable) {
 172                dprintk("%sing learning mode\n", enable ? "enter" : "exit");
 173                lodev->learning = !!enable;
 174        }
 175
 176        return 0;
 177}
 178
 179static int loop_set_carrier_report(struct rc_dev *dev, int enable)
 180{
 181        struct loopback_dev *lodev = dev->priv;
 182
 183        if (lodev->carrierreport != enable) {
 184                dprintk("%sabling carrier reports\n", enable ? "en" : "dis");
 185                lodev->carrierreport = !!enable;
 186        }
 187
 188        return 0;
 189}
 190
 191static int __init loop_init(void)
 192{
 193        struct rc_dev *rc;
 194        int ret;
 195
 196        rc = rc_allocate_device();
 197        if (!rc) {
 198                printk(KERN_ERR DRIVER_NAME ": rc_dev allocation failed\n");
 199                return -ENOMEM;
 200        }
 201
 202        rc->input_name          = "rc-core loopback device";
 203        rc->input_phys          = "rc-core/virtual";
 204        rc->input_id.bustype    = BUS_VIRTUAL;
 205        rc->input_id.version    = 1;
 206        rc->driver_name         = DRIVER_NAME;
 207        rc->map_name            = RC_MAP_EMPTY;
 208        rc->priv                = &loopdev;
 209        rc->driver_type         = RC_DRIVER_IR_RAW;
 210        rc->allowed_protos      = RC_TYPE_ALL;
 211        rc->timeout             = 100 * 1000 * 1000; /* 100 ms */
 212        rc->min_timeout         = 1;
 213        rc->max_timeout         = UINT_MAX;
 214        rc->rx_resolution       = 1000;
 215        rc->tx_resolution       = 1000;
 216        rc->s_tx_mask           = loop_set_tx_mask;
 217        rc->s_tx_carrier        = loop_set_tx_carrier;
 218        rc->s_tx_duty_cycle     = loop_set_tx_duty_cycle;
 219        rc->s_rx_carrier_range  = loop_set_rx_carrier_range;
 220        rc->tx_ir               = loop_tx_ir;
 221        rc->s_idle              = loop_set_idle;
 222        rc->s_learning_mode     = loop_set_learning_mode;
 223        rc->s_carrier_report    = loop_set_carrier_report;
 224        rc->priv                = &loopdev;
 225
 226        loopdev.txmask          = RXMASK_REGULAR;
 227        loopdev.txcarrier       = 36000;
 228        loopdev.txduty          = 50;
 229        loopdev.rxcarriermin    = 1;
 230        loopdev.rxcarriermax    = ~0;
 231        loopdev.idle            = true;
 232        loopdev.learning        = false;
 233        loopdev.carrierreport   = false;
 234
 235        ret = rc_register_device(rc);
 236        if (ret < 0) {
 237                printk(KERN_ERR DRIVER_NAME ": rc_dev registration failed\n");
 238                rc_free_device(rc);
 239                return ret;
 240        }
 241
 242        loopdev.dev = rc;
 243        return 0;
 244}
 245
 246static void __exit loop_exit(void)
 247{
 248        rc_unregister_device(loopdev.dev);
 249}
 250
 251module_init(loop_init);
 252module_exit(loop_exit);
 253
 254module_param(debug, bool, S_IRUGO | S_IWUSR);
 255MODULE_PARM_DESC(debug, "Enable debug messages");
 256
 257MODULE_DESCRIPTION("Loopback device for rc-core debugging");
 258MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
 259MODULE_LICENSE("GPL");
 260