V4L/DVB (3984): Fix CI interface on KNC1 DVBT and DVBC cards
authorAndrew de Quincey <adq_dvb@lidskialf.net>
Mon, 22 May 2006 13:32:02 +0000 (10:32 -0300)
committerMauro Carvalho Chehab <mchehab@infradead.org>
Sun, 25 Jun 2006 05:00:06 +0000 (02:00 -0300)
These cards need special handling for CI - reinitialising the frontend
device when the CI module is reset. Additionally the tda10021 needs to be set
into a different transport stream mode when a CI module is present.

Signed-off-by: Andrew de Quincey <adq_dvb@lidskialf.net>
Signed-off-by: Mauro Carvalho Chehab <mchehab@infradead.org>
drivers/media/dvb/dvb-core/dvb_ca_en50221.c
drivers/media/dvb/frontends/tda10021.c
drivers/media/dvb/frontends/tda10021.h
drivers/media/dvb/ttpci/budget-av.c

index 00347a750681942db7775769de7f7f2cf8f3eea3..2a03bf53cb2960b2c23ac1c387924f9f284b8f54 100644 (file)
@@ -1060,8 +1060,18 @@ static int dvb_ca_en50221_thread(void *data)
                                break;
 
                        case DVB_CA_SLOTSTATE_VALIDATE:
-                               if (dvb_ca_en50221_parse_attributes(ca, slot)
-                                   != 0) {
+                               if (dvb_ca_en50221_parse_attributes(ca, slot) != 0) {
+                                       /* we need this extra check for annoying interfaces like the budget-av */
+                                       if ((!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) &&
+                                           (ca->pub->poll_slot_status)) {
+                                               int status = ca->pub->poll_slot_status(ca->pub, slot, 0);
+                                               if (!(status & DVB_CA_EN50221_POLL_CAM_PRESENT)) {
+                                                       ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
+                                                       dvb_ca_en50221_thread_update_delay(ca);
+                                                       break;
+                                               }
+                                       }
+
                                        printk("dvb_ca adapter %d: Invalid PC card inserted :(\n",
                                               ca->dvbdev->adapter->num);
                                        ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
@@ -1108,6 +1118,17 @@ static int dvb_ca_en50221_thread(void *data)
 
                        case DVB_CA_SLOTSTATE_LINKINIT:
                                if (dvb_ca_en50221_link_init(ca, slot) != 0) {
+                                       /* we need this extra check for annoying interfaces like the budget-av */
+                                       if ((!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) &&
+                                           (ca->pub->poll_slot_status)) {
+                                               int status = ca->pub->poll_slot_status(ca->pub, slot, 0);
+                                               if (!(status & DVB_CA_EN50221_POLL_CAM_PRESENT)) {
+                                                       ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
+                                                       dvb_ca_en50221_thread_update_delay(ca);
+                                                       break;
+                                               }
+                                       }
+
                                        printk("dvb_ca adapter %d: DVB CAM link initialisation failed :(\n", ca->dvbdev->adapter->num);
                                        ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
                                        dvb_ca_en50221_thread_update_delay(ca);
index 12e0ec27cb3ca91da8391195d258fd02c67bfd70..c429979171912d35cba08d6283a0637aa44ebf2e 100644 (file)
@@ -90,6 +90,14 @@ static int tda10021_writereg (struct tda10021_state* state, u8 reg, u8 data)
        return (ret != 1) ? -EREMOTEIO : 0;
 }
 
+int tda10021_write_byte(struct dvb_frontend* fe, int reg, int data)
+{
+       struct tda10021_state* state = fe->demodulator_priv;
+
+       return tda10021_writereg(state, reg, data);
+}
+EXPORT_SYMBOL(tda10021_write_byte);
+
 static u8 tda10021_readreg (struct tda10021_state* state, u8 reg)
 {
        u8 b0 [] = { reg };
index 3fc338e0ec4fb71b301e0d56f32ad6f42f8bd8c9..b1df4259bee9f4d7c16a0030d34f04e062517dcf 100644 (file)
@@ -35,4 +35,6 @@ struct tda10021_config
 extern struct dvb_frontend* tda10021_attach(const struct tda10021_config* config,
                                            struct i2c_adapter* i2c, u8 pwm);
 
+extern int tda10021_write_byte(struct dvb_frontend* fe, int reg, int data);
+
 #endif // TDA10021_H
index 68004187860f433fb13f9f0df8bed36c69e6f894..3ff67523cce2dbd505310c011cfd993f789cbaf6 100644 (file)
 
 #define DEBICICAM              0x02420000
 
+#define SLOTSTATUS_NONE         1
+#define SLOTSTATUS_PRESENT      2
+#define SLOTSTATUS_RESET        4
+#define SLOTSTATUS_READY        8
+#define SLOTSTATUS_OCCUPIED     (SLOTSTATUS_PRESENT|SLOTSTATUS_RESET|SLOTSTATUS_READY)
+
 struct budget_av {
        struct budget budget;
        struct video_device *vd;
@@ -58,8 +64,15 @@ struct budget_av {
        struct tasklet_struct ciintf_irq_tasklet;
        int slot_status;
        struct dvb_ca_en50221 ca;
+       u8 reinitialise_demod:1;
+       u8 tda10021_poclkp:1;
+       u8 tda10021_ts_enabled;
+       int (*tda10021_set_frontend)(struct dvb_frontend *fe, struct dvb_frontend_parameters *p);
 };
 
+static int ciintf_slot_shutdown(struct dvb_ca_en50221 *ca, int slot);
+
+
 /* GPIO Connections:
  * 0 - Vcc/Reset (Reset is controlled by capacitor). Resets the frontend *AS WELL*!
  * 1 - CI memory select 0=>IO memory, 1=>Attribute Memory
@@ -129,9 +142,10 @@ static int ciintf_read_attribute_mem(struct dvb_ca_en50221 *ca, int slot, int ad
        udelay(1);
 
        result = ttpci_budget_debiread(&budget_av->budget, DEBICICAM, address & 0xfff, 1, 0, 1);
-
-       if (result == -ETIMEDOUT)
-               budget_av->slot_status = 0;
+       if (result == -ETIMEDOUT) {
+               ciintf_slot_shutdown(ca, slot);
+               printk(KERN_INFO "budget-av: cam ejected 1\n");
+       }
        return result;
 }
 
@@ -147,9 +161,10 @@ static int ciintf_write_attribute_mem(struct dvb_ca_en50221 *ca, int slot, int a
        udelay(1);
 
        result = ttpci_budget_debiwrite(&budget_av->budget, DEBICICAM, address & 0xfff, 1, value, 0, 1);
-
-       if (result == -ETIMEDOUT)
-               budget_av->slot_status = 0;
+       if (result == -ETIMEDOUT) {
+               ciintf_slot_shutdown(ca, slot);
+               printk(KERN_INFO "budget-av: cam ejected 2\n");
+       }
        return result;
 }
 
@@ -165,9 +180,11 @@ static int ciintf_read_cam_control(struct dvb_ca_en50221 *ca, int slot, u8 addre
        udelay(1);
 
        result = ttpci_budget_debiread(&budget_av->budget, DEBICICAM, address & 3, 1, 0, 0);
-
-       if (result == -ETIMEDOUT)
-               budget_av->slot_status = 0;
+       if ((result == -ETIMEDOUT) || ((result == 0xff) && ((address & 3) < 2))) {
+               ciintf_slot_shutdown(ca, slot);
+               printk(KERN_INFO "budget-av: cam ejected 3\n");
+               return -ETIMEDOUT;
+       }
        return result;
 }
 
@@ -183,9 +200,10 @@ static int ciintf_write_cam_control(struct dvb_ca_en50221 *ca, int slot, u8 addr
        udelay(1);
 
        result = ttpci_budget_debiwrite(&budget_av->budget, DEBICICAM, address & 3, 1, value, 0, 0);
-
-       if (result == -ETIMEDOUT)
-               budget_av->slot_status = 0;
+       if (result == -ETIMEDOUT) {
+               ciintf_slot_shutdown(ca, slot);
+               printk(KERN_INFO "budget-av: cam ejected 5\n");
+       }
        return result;
 }
 
@@ -193,12 +211,12 @@ static int ciintf_slot_reset(struct dvb_ca_en50221 *ca, int slot)
 {
        struct budget_av *budget_av = (struct budget_av *) ca->data;
        struct saa7146_dev *saa = budget_av->budget.dev;
-       int timeout = 50; // 5 seconds (4.4.6 Ready)
 
        if (slot != 0)
                return -EINVAL;
 
        dprintk(1, "ciintf_slot_reset\n");
+       budget_av->slot_status = SLOTSTATUS_RESET;
 
        saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTHI); /* disable card */
 
@@ -208,20 +226,17 @@ static int ciintf_slot_reset(struct dvb_ca_en50221 *ca, int slot)
        msleep(20); /* 20 ms Vcc settling time */
 
        saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTLO); /* enable card */
+       ttpci_budget_set_video_port(saa, BUDGET_VIDEO_PORTB);
+       msleep(20);
 
-       /* This should have been based on pin 16 READY of the pcmcia port,
-        * but AFAICS it is not routed to the saa7146 */
-       while (--timeout > 0 && ciintf_read_attribute_mem(ca, slot, 0) != 0x1d)
-               msleep(100);
-
-       /* reinitialise the frontend */
-       dvb_frontend_reinitialise(budget_av->budget.dvb_frontend);
+       /* reinitialise the frontend if necessary */
+       if (budget_av->reinitialise_demod)
+               dvb_frontend_reinitialise(budget_av->budget.dvb_frontend);
 
-       if (timeout <= 0)
-       {
-               printk(KERN_ERR "budget-av: cam reset failed (timeout).\n");
-               saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTHI); /* disable card */
-               return -ETIMEDOUT;
+       /* set tda10021 back to original clock configuration on reset */
+       if (budget_av->tda10021_poclkp) {
+               tda10021_write_byte(budget_av->budget.dvb_frontend, 0x12, 0xa0);
+               budget_av->tda10021_ts_enabled = 0;
        }
 
        return 0;
@@ -238,7 +253,13 @@ static int ciintf_slot_shutdown(struct dvb_ca_en50221 *ca, int slot)
        dprintk(1, "ciintf_slot_shutdown\n");
 
        ttpci_budget_set_video_port(saa, BUDGET_VIDEO_PORTB);
-       budget_av->slot_status = 0;
+       budget_av->slot_status = SLOTSTATUS_NONE;
+
+       /* set tda10021 back to original clock configuration when cam removed */
+       if (budget_av->tda10021_poclkp) {
+               tda10021_write_byte(budget_av->budget.dvb_frontend, 0x12, 0xa0);
+               budget_av->tda10021_ts_enabled = 0;
+       }
        return 0;
 }
 
@@ -253,6 +274,13 @@ static int ciintf_slot_ts_enable(struct dvb_ca_en50221 *ca, int slot)
        dprintk(1, "ciintf_slot_ts_enable: %d\n", budget_av->slot_status);
 
        ttpci_budget_set_video_port(saa, BUDGET_VIDEO_PORTA);
+
+       /* tda10021 seems to need a different TS clock config when data is routed to the CAM */
+       if (budget_av->tda10021_poclkp) {
+               tda10021_write_byte(budget_av->budget.dvb_frontend, 0x12, 0xa1);
+               budget_av->tda10021_ts_enabled = 1;
+       }
+
        return 0;
 }
 
@@ -260,50 +288,61 @@ static int ciintf_poll_slot_status(struct dvb_ca_en50221 *ca, int slot, int open
 {
        struct budget_av *budget_av = (struct budget_av *) ca->data;
        struct saa7146_dev *saa = budget_av->budget.dev;
-       int cam_present = 0;
+       int result;
 
        if (slot != 0)
                return -EINVAL;
 
-       if (!budget_av->slot_status)
-       {
-               // first of all test the card detect line
+       /* test the card detect line - needs to be done carefully
+        * since it never goes high for some CAMs on this interface (e.g. topuptv) */
+       if (budget_av->slot_status == SLOTSTATUS_NONE) {
                saa7146_setgpio(saa, 3, SAA7146_GPIO_INPUT);
                udelay(1);
-               if (saa7146_read(saa, PSR) & MASK_06)
-               {
-                       cam_present = 1;
+               if (saa7146_read(saa, PSR) & MASK_06) {
+                       if (budget_av->slot_status == SLOTSTATUS_NONE) {
+                               budget_av->slot_status = SLOTSTATUS_PRESENT;
+                               printk(KERN_INFO "budget-av: cam inserted A\n");
+                       }
                }
                saa7146_setgpio(saa, 3, SAA7146_GPIO_OUTLO);
+       }
 
-               // that is unreliable however, so try and read from IO memory
-               if (!cam_present)
-               {
-                       saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTLO);
-                       if (ttpci_budget_debiread(&budget_av->budget, DEBICICAM, 0, 1, 0, 1) != -ETIMEDOUT)
-                       {
-                               cam_present = 1;
+       /* We also try and read from IO memory to work round the above detection bug. If
+        * there is no CAM, we will get a timeout. Only done if there is no cam
+        * present, since this test actually breaks some cams :(
+        *
+        * if the CI interface is not open, we also do the above test since we
+        * don't care if the cam has problems - we'll be resetting it on open() anyway */
+       if ((budget_av->slot_status == SLOTSTATUS_NONE) || (!open)) {
+               saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTLO);
+               result = ttpci_budget_debiread(&budget_av->budget, DEBICICAM, 0, 1, 0, 1);
+               if ((result >= 0) && (budget_av->slot_status == SLOTSTATUS_NONE)) {
+                       budget_av->slot_status = SLOTSTATUS_PRESENT;
+                       printk(KERN_INFO "budget-av: cam inserted B\n");
+               } else if (result < 0) {
+                       if (budget_av->slot_status != SLOTSTATUS_NONE) {
+                               ciintf_slot_shutdown(ca, slot);
+                               printk(KERN_INFO "budget-av: cam ejected 5\n");
+                               return 0;
                        }
                }
+       }
 
-               // did we find something?
-               if (cam_present) {
-                       printk(KERN_INFO "budget-av: cam inserted\n");
-                       budget_av->slot_status = 1;
-               }
-       } else if (!open) {
-               saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTLO);
-               if (ttpci_budget_debiread(&budget_av->budget, DEBICICAM, 0, 1, 0, 1) == -ETIMEDOUT)
-               {
-                       printk(KERN_INFO "budget-av: cam ejected\n");
-                       saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTHI); /* disable card */
-                       budget_av->slot_status = 0;
+       /* read from attribute memory in reset/ready state to know when the CAM is ready */
+       if (budget_av->slot_status == SLOTSTATUS_RESET) {
+               result = ciintf_read_attribute_mem(ca, slot, 0);
+               if (result == 0x1d) {
+                       budget_av->slot_status = SLOTSTATUS_READY;
                }
        }
 
-       if (budget_av->slot_status == 1)
-               return DVB_CA_EN50221_POLL_CAM_PRESENT | DVB_CA_EN50221_POLL_CAM_READY;
-
+       /* work out correct return code */
+       if (budget_av->slot_status != SLOTSTATUS_NONE) {
+               if (budget_av->slot_status & SLOTSTATUS_READY) {
+                       return DVB_CA_EN50221_POLL_CAM_PRESENT | DVB_CA_EN50221_POLL_CAM_READY;
+               }
+               return DVB_CA_EN50221_POLL_CAM_PRESENT;
+       }
        return 0;
 }
 
@@ -333,6 +372,8 @@ static int ciintf_init(struct budget_av *budget_av)
        budget_av->ca.slot_ts_enable = ciintf_slot_ts_enable;
        budget_av->ca.poll_slot_status = ciintf_poll_slot_status;
        budget_av->ca.data = budget_av;
+       budget_av->budget.ci_present = 1;
+       budget_av->slot_status = SLOTSTATUS_NONE;
 
        if ((result = dvb_ca_en50221_init(&budget_av->budget.dvb_adapter,
                                          &budget_av->ca, 0, 1)) != 0) {
@@ -341,7 +382,6 @@ static int ciintf_init(struct budget_av *budget_av)
        }
 
        printk(KERN_INFO "budget-av: ci interface initialised.\n");
-       budget_av->budget.ci_present = 1;
        return 0;
 
 error:
@@ -976,7 +1016,7 @@ static int philips_sd1878_ci_set_symbol_rate(struct dvb_frontend *fe,
 
 static struct stv0299_config philips_sd1878_config = {
        .demod_address = 0x68,
-       .inittab = philips_sd1878_inittab,
+     .inittab = philips_sd1878_inittab,
        .mclk = 88000000UL,
        .invert = 0,
        .skip_reinit = 0,
@@ -1018,6 +1058,23 @@ static u8 read_pwm(struct budget_av *budget_av)
 #define SUBID_DVBT_KNC1                0x0030
 #define SUBID_DVBT_CINERGY1200 0x1157
 
+
+static int tda10021_set_frontend(struct dvb_frontend *fe,
+                                struct dvb_frontend_parameters *p)
+{
+       struct budget_av* budget_av = fe->dvb->priv;
+       int result;
+
+       result = budget_av->tda10021_set_frontend(fe, p);
+       if (budget_av->tda10021_ts_enabled) {
+               tda10021_write_byte(budget_av->budget.dvb_frontend, 0x12, 0xa1);
+       } else {
+               tda10021_write_byte(budget_av->budget.dvb_frontend, 0x12, 0xa0);
+       }
+
+       return result;
+}
+
 static void frontend_init(struct budget_av *budget_av)
 {
        struct saa7146_dev * saa = budget_av->budget.dev;
@@ -1083,34 +1140,23 @@ static void frontend_init(struct budget_av *budget_av)
 
        case SUBID_DVBC_KNC1:
        case SUBID_DVBC_KNC1_PLUS:
+       case SUBID_DVBC_CINERGY1200:
+               budget_av->reinitialise_demod = 1;
                fe = tda10021_attach(&philips_cu1216_config,
                                     &budget_av->budget.i2c_adap,
                                     read_pwm(budget_av));
                if (fe) {
+                       budget_av->tda10021_poclkp = 1;
+                       budget_av->tda10021_set_frontend = fe->ops->set_frontend;
+                       fe->ops->set_frontend = tda10021_set_frontend;
                        fe->ops->tuner_ops.set_params = philips_cu1216_tuner_set_params;
                }
                break;
 
        case SUBID_DVBT_KNC1:
        case SUBID_DVBT_KNC1_PLUS:
-               fe = tda10046_attach(&philips_tu1216_config,
-                                    &budget_av->budget.i2c_adap);
-               if (fe) {
-                       fe->ops->tuner_ops.init = philips_tu1216_tuner_init;
-                       fe->ops->tuner_ops.set_params = philips_tu1216_tuner_set_params;
-               }
-               break;
-
-       case SUBID_DVBC_CINERGY1200:
-               fe = tda10021_attach(&philips_cu1216_config,
-                                    &budget_av->budget.i2c_adap,
-                                    read_pwm(budget_av));
-               if (fe) {
-                       fe->ops->tuner_ops.set_params = philips_cu1216_tuner_set_params;
-               }
-               break;
-
        case SUBID_DVBT_CINERGY1200:
+               budget_av->reinitialise_demod = 1;
                fe = tda10046_attach(&philips_tu1216_config,
                                     &budget_av->budget.i2c_adap);
                if (fe) {