]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/media/dvb/ttpci/budget-ci.c
V4L/DVB (5335): Budget-ci: Use the repeat handling of the input subsystem
[linux-2.6-omap-h63xx.git] / drivers / media / dvb / ttpci / budget-ci.c
index 2a2e9b400613ed6674c8ee69c349ec64f484be79..4ed4599ce816b78be6c5f3527ceb9f84028a2e8e 100644 (file)
  * the project's page is at http://www.linuxtv.org/dvb/
  */
 
-#include "budget.h"
-
 #include <linux/module.h>
 #include <linux/errno.h>
 #include <linux/slab.h>
 #include <linux/interrupt.h>
 #include <linux/input.h>
 #include <linux/spinlock.h>
+#include <media/ir-common.h>
+
+#include "budget.h"
 
 #include "dvb_ca_en50221.h"
 #include "stv0299.h"
 #include "bsbe1.h"
 #include "bsru6.h"
 
-#define DEBIADDR_IR            0x1234
+/*
+ * Regarding DEBIADDR_IR:
+ * Some CI modules hang if random addresses are read.
+ * Using address 0x4000 for the IR read means that we
+ * use the same address as for CI version, which should
+ * be a safe default.
+ */
+#define DEBIADDR_IR            0x4000
 #define DEBIADDR_CICONTROL     0x0000
 #define DEBIADDR_CIVERSION     0x4000
 #define DEBIADDR_IO            0x1000
 #define SLOTSTATUS_READY       8
 #define SLOTSTATUS_OCCUPIED    (SLOTSTATUS_PRESENT|SLOTSTATUS_RESET|SLOTSTATUS_READY)
 
+/*
+ * Milliseconds during which a key is regarded as pressed.
+ * If an identical command arrives within this time, the timer will start over.
+ */
+#define IR_KEYPRESS_TIMEOUT    250
+
+/* RC5 device wildcard */
+#define IR_DEVICE_ANY          255
+
+static int rc5_device = -1;
+module_param(rc5_device, int, 0644);
+MODULE_PARM_DESC(rc5_device, "only IR commands to given RC5 device (device = 0 - 31, any device = 255, default: autodetect)");
+
+static int ir_debug = 0;
+module_param(ir_debug, int, 0644);
+MODULE_PARM_DESC(ir_debug, "enable debugging information for IR decoding");
+
+struct budget_ci_ir {
+       struct input_dev *dev;
+       struct tasklet_struct msp430_irq_tasklet;
+       struct timer_list timer_keyup;
+       char name[72]; /* 40 + 32 for (struct saa7146_dev).name */
+       char phys[32];
+       struct ir_input_state state;
+       int rc5_device;
+       u32 last_raw;
+       u32 ir_key;
+       bool have_command;
+};
+
 struct budget_ci {
        struct budget budget;
-       struct input_dev *input_dev;
-       struct tasklet_struct msp430_irq_tasklet;
        struct tasklet_struct ciintf_irq_tasklet;
        int slot_status;
        int ci_irq;
        struct dvb_ca_en50221 ca;
-       char ir_dev_name[50];
+       struct budget_ci_ir ir;
        u8 tuner_pll_address; /* used for philips_tdm1316l configs */
 };
 
-/* from reading the following remotes:
-   Zenith Universal 7 / TV Mode 807 / VCR Mode 837
-   Hauppauge (from NOVA-CI-s box product)
-   i've taken a "middle of the road" approach and note the differences
-*/
-static u16 key_map[64] = {
-       /* 0x0X */
-       KEY_0, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7, KEY_8,
-       KEY_9,
-       KEY_ENTER,
-       KEY_RED,
-       KEY_POWER,              /* RADIO on Hauppauge */
-       KEY_MUTE,
-       0,
-       KEY_A,                  /* TV on Hauppauge */
-       /* 0x1X */
-       KEY_VOLUMEUP, KEY_VOLUMEDOWN,
-       0, 0,
-       KEY_B,
-       0, 0, 0, 0, 0, 0, 0,
-       KEY_UP, KEY_DOWN,
-       KEY_OPTION,             /* RESERVED on Hauppauge */
-       KEY_BREAK,
-       /* 0x2X */
-       KEY_CHANNELUP, KEY_CHANNELDOWN,
-       KEY_PREVIOUS,           /* Prev. Ch on Zenith, SOURCE on Hauppauge */
-       0, KEY_RESTART, KEY_OK,
-       KEY_CYCLEWINDOWS,       /* MINIMIZE on Hauppauge */
-       0,
-       KEY_ENTER,              /* VCR mode on Zenith */
-       KEY_PAUSE,
-       0,
-       KEY_RIGHT, KEY_LEFT,
-       0,
-       KEY_MENU,               /* FULL SCREEN on Hauppauge */
-       0,
-       /* 0x3X */
-       KEY_SLOW,
-       KEY_PREVIOUS,           /* VCR mode on Zenith */
-       KEY_REWIND,
-       0,
-       KEY_FASTFORWARD,
-       KEY_PLAY, KEY_STOP,
-       KEY_RECORD,
-       KEY_TUNER,              /* TV/VCR on Zenith */
-       0,
-       KEY_C,
-       0,
-       KEY_EXIT,
-       KEY_POWER2,
-       KEY_TUNER,              /* VCR mode on Zenith */
-       0,
-};
-
-static void msp430_ir_debounce(unsigned long data)
+static void msp430_ir_keyup(unsigned long data)
 {
-       struct input_dev *dev = (struct input_dev *) data;
-
-       if (dev->rep[0] == 0 || dev->rep[0] == ~0) {
-               input_event(dev, EV_KEY, key_map[dev->repeat_key], !!0);
-               return;
-       }
-
-       dev->rep[0] = 0;
-       dev->timer.expires = jiffies + HZ * 350 / 1000;
-       add_timer(&dev->timer);
-       input_event(dev, EV_KEY, key_map[dev->repeat_key], 2);  /* REPEAT */
+       struct budget_ci_ir *ir = (struct budget_ci_ir *) data;
+       ir_input_nokey(ir->dev, &ir->state);
 }
 
 static void msp430_ir_interrupt(unsigned long data)
 {
        struct budget_ci *budget_ci = (struct budget_ci *) data;
-       struct input_dev *dev = budget_ci->input_dev;
-       unsigned int code =
-               ttpci_budget_debiread(&budget_ci->budget, DEBINOSWAP, DEBIADDR_IR, 2, 1, 0) >> 8;
-
-       if (code & 0x40) {
-               code &= 0x3f;
+       struct input_dev *dev = budget_ci->ir.dev;
+       u32 command = ttpci_budget_debiread(&budget_ci->budget, DEBINOSWAP, DEBIADDR_IR, 2, 1, 0) >> 8;
+       u32 raw;
+
+       /*
+        * The msp430 chip can generate two different bytes, command and device
+        *
+        * type1: X1CCCCCC, C = command bits (0 - 63)
+        * type2: X0TDDDDD, D = device bits (0 - 31), T = RC5 toggle bit
+        *
+        * Each signal from the remote control can generate one or more command
+        * bytes and one or more device bytes. For the repeated bytes, the
+        * highest bit (X) is set. The first command byte is always generated
+        * before the first device byte. Other than that, no specific order
+        * seems to apply. To make life interesting, bytes can also be lost.
+        *
+        * Only when we have a command and device byte, a keypress is
+        * generated.
+        */
+
+       if (ir_debug)
+               printk("budget_ci: received byte 0x%02x\n", command);
+
+       /* Remove repeat bit, we use every command */
+       command = command & 0x7f;
+
+       /* Is this a RC5 command byte? */
+       if (command & 0x40) {
+               budget_ci->ir.have_command = true;
+               budget_ci->ir.ir_key = command & 0x3f;
+               return;
+       }
 
-               if (timer_pending(&dev->timer)) {
-                       if (code == dev->repeat_key) {
-                               ++dev->rep[0];
-                               return;
-                       }
-                       del_timer(&dev->timer);
-                       input_event(dev, EV_KEY, key_map[dev->repeat_key], !!0);
-               }
+       /* It's a RC5 device byte */
+       if (!budget_ci->ir.have_command)
+               return;
+       budget_ci->ir.have_command = false;
 
-               if (!key_map[code]) {
-                       printk("DVB (%s): no key for %02x!\n", __FUNCTION__, code);
-                       return;
-               }
+       if (budget_ci->ir.rc5_device != IR_DEVICE_ANY &&
+           budget_ci->ir.rc5_device != (command & 0x1f))
+               return;
 
-               /* initialize debounce and repeat */
-               dev->repeat_key = code;
-               /* Zenith remote _always_ sends 2 sequences */
-               dev->rep[0] = ~0;
-               /* 350 milliseconds */
-               dev->timer.expires = jiffies + HZ * 350 / 1000;
-               /* MAKE */
-               input_event(dev, EV_KEY, key_map[code], !0);
-               add_timer(&dev->timer);
+       /* Is this a repeated key sequence? (same device, command, toggle) */
+       raw = budget_ci->ir.ir_key | (command << 8);
+       if (budget_ci->ir.last_raw != raw || !timer_pending(&budget_ci->ir.timer_keyup)) {
+               ir_input_nokey(dev, &budget_ci->ir.state);
+               ir_input_keydown(dev, &budget_ci->ir.state,
+                                budget_ci->ir.ir_key, raw);
+               budget_ci->ir.last_raw = raw;
        }
+
+       mod_timer(&budget_ci->ir.timer_keyup, jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT));
 }
 
 static int msp430_ir_init(struct budget_ci *budget_ci)
 {
        struct saa7146_dev *saa = budget_ci->budget.dev;
-       struct input_dev *input_dev;
-       int i;
+       struct input_dev *input_dev = budget_ci->ir.dev;
+       int error;
+
+       budget_ci->ir.dev = input_dev = input_allocate_device();
+       if (!input_dev) {
+               printk(KERN_ERR "budget_ci: IR interface initialisation failed\n");
+               error = -ENOMEM;
+               goto out1;
+       }
 
-       budget_ci->input_dev = input_dev = input_allocate_device();
-       if (!input_dev)
-               return -ENOMEM;
+       snprintf(budget_ci->ir.name, sizeof(budget_ci->ir.name),
+                "Budget-CI dvb ir receiver %s", saa->name);
+       snprintf(budget_ci->ir.phys, sizeof(budget_ci->ir.phys),
+                "pci-%s/ir0", pci_name(saa->pci));
 
-       sprintf(budget_ci->ir_dev_name, "Budget-CI dvb ir receiver %s", saa->name);
+       input_dev->name = budget_ci->ir.name;
 
-       input_dev->name = budget_ci->ir_dev_name;
+       input_dev->phys = budget_ci->ir.phys;
+       input_dev->id.bustype = BUS_PCI;
+       input_dev->id.version = 1;
+       if (saa->pci->subsystem_vendor) {
+               input_dev->id.vendor = saa->pci->subsystem_vendor;
+               input_dev->id.product = saa->pci->subsystem_device;
+       } else {
+               input_dev->id.vendor = saa->pci->vendor;
+               input_dev->id.product = saa->pci->device;
+       }
+       input_dev->cdev.dev = &saa->pci->dev;
 
-       set_bit(EV_KEY, input_dev->evbit);
-       for (i = 0; i < ARRAY_SIZE(key_map); i++)
-               if (key_map[i])
-                       set_bit(key_map[i], input_dev->keybit);
+       /* Select keymap and address */
+       switch (budget_ci->budget.dev->pci->subsystem_device) {
+       case 0x100c:
+       case 0x100f:
+       case 0x1011:
+       case 0x1012:
+       case 0x1017:
+               /* The hauppauge keymap is a superset of these remotes */
+               ir_input_init(input_dev, &budget_ci->ir.state,
+                             IR_TYPE_RC5, ir_codes_hauppauge_new);
+
+               if (rc5_device < 0)
+                       budget_ci->ir.rc5_device = 0x1f;
+               else
+                       budget_ci->ir.rc5_device = rc5_device;
+               break;
+       case 0x1010:
+               /* for the Technotrend 1500 bundled remote */
+               ir_input_init(input_dev, &budget_ci->ir.state,
+                             IR_TYPE_RC5, ir_codes_tt_1500);
+
+               if (rc5_device < 0)
+                       budget_ci->ir.rc5_device = IR_DEVICE_ANY;
+               else
+                       budget_ci->ir.rc5_device = rc5_device;
+               break;
+       default:
+               /* unknown remote */
+               ir_input_init(input_dev, &budget_ci->ir.state,
+                             IR_TYPE_RC5, ir_codes_budget_ci_old);
+
+               if (rc5_device < 0)
+                       budget_ci->ir.rc5_device = IR_DEVICE_ANY;
+               else
+                       budget_ci->ir.rc5_device = rc5_device;
+               break;
+       }
+
+       /* initialise the key-up timeout handler */
+       init_timer(&budget_ci->ir.timer_keyup);
+       budget_ci->ir.timer_keyup.function = msp430_ir_keyup;
+       budget_ci->ir.timer_keyup.data = (unsigned long) &budget_ci->ir;
+       budget_ci->ir.last_raw = 0xffff; /* An impossible value */
+       error = input_register_device(input_dev);
+       if (error) {
+               printk(KERN_ERR "budget_ci: could not init driver for IR device (code %d)\n", error);
+               goto out2;
+       }
 
-       input_register_device(budget_ci->input_dev);
+       /* note: these must be after input_register_device */
+       input_dev->rep[REP_DELAY] = 400;
+       input_dev->rep[REP_PERIOD] = 250;
 
-       input_dev->timer.function = msp430_ir_debounce;
+       tasklet_init(&budget_ci->ir.msp430_irq_tasklet, msp430_ir_interrupt,
+                    (unsigned long) budget_ci);
 
-       saa7146_write(saa, IER, saa7146_read(saa, IER) | MASK_06);
+       SAA7146_IER_ENABLE(saa, MASK_06);
        saa7146_setgpio(saa, 3, SAA7146_GPIO_IRQHI);
 
        return 0;
+
+out2:
+       input_free_device(input_dev);
+out1:
+       return error;
 }
 
 static void msp430_ir_deinit(struct budget_ci *budget_ci)
 {
        struct saa7146_dev *saa = budget_ci->budget.dev;
-       struct input_dev *dev = budget_ci->input_dev;
+       struct input_dev *dev = budget_ci->ir.dev;
 
-       saa7146_write(saa, IER, saa7146_read(saa, IER) & ~MASK_06);
+       SAA7146_IER_DISABLE(saa, MASK_06);
        saa7146_setgpio(saa, 3, SAA7146_GPIO_INPUT);
+       tasklet_kill(&budget_ci->ir.msp430_irq_tasklet);
 
-       if (del_timer(&dev->timer))
-               input_event(dev, EV_KEY, key_map[dev->repeat_key], !!0);
+       del_timer_sync(&dev->timer);
+       ir_input_nokey(dev, &budget_ci->ir.state);
 
        input_unregister_device(dev);
 }
@@ -421,7 +486,7 @@ static int ciintf_init(struct budget_ci *budget_ci)
        memset(&budget_ci->ca, 0, sizeof(struct dvb_ca_en50221));
 
        // enable DEBI pins
-       saa7146_write(saa, MC1, saa7146_read(saa, MC1) | (0x800 << 16) | 0x800);
+       saa7146_write(saa, MC1, MASK_27 | MASK_11);
 
        // test if it is there
        ci_version = ttpci_budget_debiread(&budget_ci->budget, DEBICICTL, DEBIADDR_CIVERSION, 1, 1, 0);
@@ -473,7 +538,7 @@ static int ciintf_init(struct budget_ci *budget_ci)
                } else {
                        saa7146_setgpio(saa, 0, SAA7146_GPIO_IRQHI);
                }
-               saa7146_write(saa, IER, saa7146_read(saa, IER) | MASK_03);
+               SAA7146_IER_ENABLE(saa, MASK_03);
        }
 
        // enable interface
@@ -495,7 +560,7 @@ static int ciintf_init(struct budget_ci *budget_ci)
        return 0;
 
 error:
-       saa7146_write(saa, MC1, saa7146_read(saa, MC1) | (0x800 << 16));
+       saa7146_write(saa, MC1, MASK_27);
        return result;
 }
 
@@ -505,7 +570,7 @@ static void ciintf_deinit(struct budget_ci *budget_ci)
 
        // disable CI interrupts
        if (budget_ci->ci_irq) {
-               saa7146_write(saa, IER, saa7146_read(saa, IER) & ~MASK_03);
+               SAA7146_IER_DISABLE(saa, MASK_03);
                saa7146_setgpio(saa, 0, SAA7146_GPIO_INPUT);
                tasklet_kill(&budget_ci->ciintf_irq_tasklet);
        }
@@ -523,7 +588,7 @@ static void ciintf_deinit(struct budget_ci *budget_ci)
        dvb_ca_en50221_release(&budget_ci->ca);
 
        // disable DEBI pins
-       saa7146_write(saa, MC1, saa7146_read(saa, MC1) | (0x800 << 16));
+       saa7146_write(saa, MC1, MASK_27);
 }
 
 static void budget_ci_irq(struct saa7146_dev *dev, u32 * isr)
@@ -533,7 +598,7 @@ static void budget_ci_irq(struct saa7146_dev *dev, u32 * isr)
        dprintk(8, "dev: %p, budget_ci: %p\n", dev, budget_ci);
 
        if (*isr & MASK_06)
-               tasklet_schedule(&budget_ci->msp430_irq_tasklet);
+               tasklet_schedule(&budget_ci->ir.msp430_irq_tasklet);
 
        if (*isr & MASK_10)
                ttpci_budget_irq10_handler(dev, isr);
@@ -805,6 +870,17 @@ static struct tda1004x_config philips_tdm1316l_config = {
        .request_firmware = philips_tdm1316l_request_firmware,
 };
 
+static struct tda1004x_config philips_tdm1316l_config_invert = {
+
+       .demod_address = 0x8,
+       .invert = 1,
+       .invert_oclk = 0,
+       .xtal_freq = TDA10046_XTAL_4M,
+       .agc_config = TDA10046_AGC_DEFAULT,
+       .if_freq = TDA10046_FREQ_3617,
+       .request_firmware = philips_tdm1316l_request_firmware,
+};
+
 static int dvbc_philips_tdm1316l_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
 {
        struct budget_ci *budget_ci = (struct budget_ci *) fe->dvb->priv;
@@ -828,7 +904,7 @@ static int dvbc_philips_tdm1316l_tuner_set_params(struct dvb_frontend *fe, struc
                band = 1;
        } else if (tuner_frequency < 200000000) {
                cp = 6;
-               band = 1;
+               band = 2;
        } else if (tuner_frequency < 290000000) {
                cp = 3;
                band = 2;
@@ -1029,7 +1105,7 @@ static void frontend_init(struct budget_ci *budget_ci)
        case 0x1012:            // TT DVB-T CI budget (tda10046/Philips tdm1316l(tda6651tt))
                budget_ci->tuner_pll_address = 0x60;
                budget_ci->budget.dvb_frontend =
-                       dvb_attach(tda10046_attach, &philips_tdm1316l_config, &budget_ci->budget.i2c_adap);
+                       dvb_attach(tda10046_attach, &philips_tdm1316l_config_invert, &budget_ci->budget.i2c_adap);
                if (budget_ci->budget.dvb_frontend) {
                        budget_ci->budget.dvb_frontend->ops.tuner_ops.init = philips_tdm1316l_tuner_init;
                        budget_ci->budget.dvb_frontend->ops.tuner_ops.set_params = philips_tdm1316l_tuner_set_params;
@@ -1075,24 +1151,23 @@ static int budget_ci_attach(struct saa7146_dev *dev, struct saa7146_pci_extensio
        struct budget_ci *budget_ci;
        int err;
 
-       if (!(budget_ci = kmalloc(sizeof(struct budget_ci), GFP_KERNEL)))
-               return -ENOMEM;
+       budget_ci = kzalloc(sizeof(struct budget_ci), GFP_KERNEL);
+       if (!budget_ci) {
+               err = -ENOMEM;
+               goto out1;
+       }
 
        dprintk(2, "budget_ci: %p\n", budget_ci);
 
-       budget_ci->budget.ci_present = 0;
-
        dev->ext_priv = budget_ci;
 
-       if ((err = ttpci_budget_init(&budget_ci->budget, dev, info, THIS_MODULE))) {
-               kfree(budget_ci);
-               return err;
-       }
-
-       tasklet_init(&budget_ci->msp430_irq_tasklet, msp430_ir_interrupt,
-                    (unsigned long) budget_ci);
+       err = ttpci_budget_init(&budget_ci->budget, dev, info, THIS_MODULE);
+       if (err)
+               goto out2;
 
-       msp430_ir_init(budget_ci);
+       err = msp430_ir_init(budget_ci);
+       if (err)
+               goto out3;
 
        ciintf_init(budget_ci);
 
@@ -1102,6 +1177,13 @@ static int budget_ci_attach(struct saa7146_dev *dev, struct saa7146_pci_extensio
        ttpci_budget_init_hooks(&budget_ci->budget);
 
        return 0;
+
+out3:
+       ttpci_budget_deinit(&budget_ci->budget);
+out2:
+       kfree(budget_ci);
+out1:
+       return err;
 }
 
 static int budget_ci_detach(struct saa7146_dev *dev)
@@ -1112,16 +1194,13 @@ static int budget_ci_detach(struct saa7146_dev *dev)
 
        if (budget_ci->budget.ci_present)
                ciintf_deinit(budget_ci);
+       msp430_ir_deinit(budget_ci);
        if (budget_ci->budget.dvb_frontend) {
                dvb_unregister_frontend(budget_ci->budget.dvb_frontend);
                dvb_frontend_detach(budget_ci->budget.dvb_frontend);
        }
        err = ttpci_budget_deinit(&budget_ci->budget);
 
-       tasklet_kill(&budget_ci->msp430_irq_tasklet);
-
-       msp430_ir_deinit(budget_ci);
-
        // disable frontend and CI interface
        saa7146_setgpio(saa, 2, SAA7146_GPIO_INPUT);
 
@@ -1154,7 +1233,7 @@ MODULE_DEVICE_TABLE(pci, pci_tbl);
 
 static struct saa7146_extension budget_extension = {
        .name = "budget_ci dvb",
-       .flags = SAA7146_I2C_SHORT_DELAY,
+       .flags = SAA7146_USE_I2C_IRQ,
 
        .module = THIS_MODULE,
        .pci_tbl = &pci_tbl[0],