]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/media/video/tuner-xc2028.c
Merge branch 'upstream-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jgarzi...
[linux-2.6-omap-h63xx.git] / drivers / media / video / tuner-xc2028.c
index c921d6009a8b68bc4d59d73d4ebbf95425ce3815..50cf876f020f40659929821f5c560fb58b5b8cbf 100644 (file)
@@ -50,10 +50,20 @@ static DEFINE_MUTEX(xc2028_list_mutex);
 struct firmware_description {
        unsigned int  type;
        v4l2_std_id   id;
+       __u16         int_freq;
        unsigned char *ptr;
        unsigned int  size;
 };
 
+struct firmware_properties {
+       unsigned int    type;
+       v4l2_std_id     id;
+       v4l2_std_id     std_req;
+       __u16           int_freq;
+       unsigned int    scode_table;
+       int             scode_nr;
+};
+
 struct xc2028_data {
        struct list_head        xc2028_list;
        struct tuner_i2c_props  i2c_props;
@@ -67,19 +77,12 @@ struct xc2028_data {
        int                     firm_size;
        __u16                   firm_version;
 
-       struct xc2028_ctrl      ctrl;
-
-       v4l2_std_id             firm_type;         /* video stds supported
-                                                       by current firmware */
-       fe_bandwidth_t          bandwidth;         /* Firmware bandwidth:
-                                                             6M, 7M or 8M */
-       int                     need_load_generic; /* The generic firmware
-                                                             were loaded? */
+       __u16                   hwmodel;
+       __u16                   hwvers;
 
-       int                     max_len;        /* Max firmware chunk */
+       struct xc2028_ctrl      ctrl;
 
-       enum tuner_mode mode;
-       struct i2c_client       *i2c_client;
+       struct firmware_properties cur_fw;
 
        struct mutex lock;
 };
@@ -141,7 +144,8 @@ static unsigned int xc2028_get_reg(struct xc2028_data *priv, u16 reg, u16 *val)
        return 0;
 }
 
-void dump_firm_type(unsigned int type)
+#define dump_firm_type(t)      dump_firm_type_and_int_freq(t, 0)
+void dump_firm_type_and_int_freq(unsigned int type, u16 int_freq)
 {
         if (type & BASE)
                printk("BASE ");
@@ -203,6 +207,8 @@ void dump_firm_type(unsigned int type)
                printk("INPUT2 ");
         if (type & SCODE)
                printk("SCODE ");
+        if (type & HAS_IF)
+               printk("HAS_IF_%d ", int_freq);
 }
 
 static  v4l2_std_id parse_audio_std_option(void)
@@ -237,7 +243,8 @@ static void free_firmware(struct xc2028_data *priv)
 
        priv->firm = NULL;
        priv->firm_size = 0;
-       priv->need_load_generic = 1;
+
+       memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
 }
 
 static int load_all_firmwares(struct dvb_frontend *fe)
@@ -299,6 +306,7 @@ static int load_all_firmwares(struct dvb_frontend *fe)
        while (p < endp) {
                __u32 type, size;
                v4l2_std_id id;
+               __u16 int_freq = 0;
 
                n++;
                if (n >= n_array) {
@@ -319,6 +327,11 @@ static int load_all_firmwares(struct dvb_frontend *fe)
                id = le64_to_cpu(*(v4l2_std_id *) p);
                p += sizeof(id);
 
+               if (type & HAS_IF) {
+                       int_freq = le16_to_cpu(*(__u16 *) p);
+                       p += sizeof(int_freq);
+               }
+
                size = le32_to_cpu(*(__u32 *) p);
                p += sizeof(size);
 
@@ -340,15 +353,16 @@ static int load_all_firmwares(struct dvb_frontend *fe)
                }
                tuner_dbg("Reading firmware type ");
                if (debug) {
-                       dump_firm_type(type);
+                       dump_firm_type_and_int_freq(type, int_freq);
                        printk("(%x), id %llx, size=%d.\n",
-                                  type, (unsigned long long)id, size);
+                              type, (unsigned long long)id, size);
                }
 
                memcpy(priv->firm[n].ptr, p, size);
                priv->firm[n].type = type;
                priv->firm[n].id   = id;
                priv->firm[n].size = size;
+               priv->firm[n].int_freq = int_freq;
 
                p += size;
        }
@@ -380,9 +394,14 @@ static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
                         v4l2_std_id *id)
 {
        struct xc2028_data *priv = fe->tuner_priv;
-       int                i;
+       int                 i, best_i = -1, best_nr_matches = 0;
+       unsigned int        ign_firm_type_mask = 0;
 
-       tuner_dbg("%s called\n", __FUNCTION__);
+       tuner_dbg("%s called, want type=", __FUNCTION__);
+       if (debug) {
+               dump_firm_type(type);
+               printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
+       }
 
        if (!priv->firm) {
                tuner_err("Error! firmware not loaded\n");
@@ -392,28 +411,64 @@ static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
        if (((type & ~SCODE) == 0) && (*id == 0))
                *id = V4L2_STD_PAL;
 
+       if (type & BASE)
+               type &= BASE_TYPES;
+       else if (type & SCODE) {
+               type &= SCODE_TYPES;
+               ign_firm_type_mask = HAS_IF;
+       } else if (type & DTV_TYPES)
+               type &= DTV_TYPES;
+       else if (type & STD_SPECIFIC_TYPES)
+               type &= STD_SPECIFIC_TYPES;
+
        /* Seek for exact match */
        for (i = 0; i < priv->firm_size; i++) {
-               if ((type == priv->firm[i].type) && (*id == priv->firm[i].id))
+               if ((type == (priv->firm[i].type & ~ign_firm_type_mask)) &&
+                   (*id == priv->firm[i].id))
                        goto found;
        }
 
        /* Seek for generic video standard match */
        for (i = 0; i < priv->firm_size; i++) {
-               if ((type == priv->firm[i].type) && (*id & priv->firm[i].id))
-                       goto found;
+               v4l2_std_id match_mask;
+               int nr_matches;
+
+               if (type != (priv->firm[i].type & ~ign_firm_type_mask))
+                       continue;
+
+               match_mask = *id & priv->firm[i].id;
+               if (!match_mask)
+                       continue;
+
+               if ((*id & match_mask) == *id)
+                       goto found; /* Supports all the requested standards */
+
+               nr_matches = hweight64(match_mask);
+               if (nr_matches > best_nr_matches) {
+                       best_nr_matches = nr_matches;
+                       best_i = i;
+               }
+       }
+
+       if (best_nr_matches > 0) {
+               tuner_dbg("Selecting best matching firmware (%d bits) for "
+                         "type=", best_nr_matches);
+               dump_firm_type(type);
+               printk("(%x), id %016llx:\n", type, (unsigned long long)*id);
+               i = best_i;
+               goto found;
        }
 
        /*FIXME: Would make sense to seek for type "hint" match ? */
 
-       i = -EINVAL;
+       i = -ENOENT;
        goto ret;
 
 found:
        *id = priv->firm[i].id;
 
 ret:
-       tuner_dbg("%s firmware for type=", (i < 0)? "Can't find": "Found");
+       tuner_dbg("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
        if (debug) {
                dump_firm_type(type);
                printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
@@ -426,7 +481,7 @@ static int load_firmware(struct dvb_frontend *fe, unsigned int type,
 {
        struct xc2028_data *priv = fe->tuner_priv;
        int                pos, rc;
-       unsigned char      *p, *endp, buf[priv->max_len];
+       unsigned char      *p, *endp, buf[priv->ctrl.max_len];
 
        tuner_dbg("%s called\n", __FUNCTION__);
 
@@ -435,8 +490,9 @@ static int load_firmware(struct dvb_frontend *fe, unsigned int type,
                return pos;
 
        tuner_info("Loading firmware for type=");
-       dump_firm_type(type);
-       printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
+       dump_firm_type(priv->firm[pos].type);
+       printk("(%x), id %016llx.\n", priv->firm[pos].type,
+              (unsigned long long)*id);
 
        p = priv->firm[pos].ptr;
        endp = p + priv->firm[pos].size;
@@ -505,8 +561,8 @@ static int load_firmware(struct dvb_frontend *fe, unsigned int type,
 
                /* Sends message chunks */
                while (size > 0) {
-                       int len = (size < priv->max_len - 1) ?
-                                  size : priv->max_len - 1;
+                       int len = (size < priv->ctrl.max_len - 1) ?
+                                  size : priv->ctrl.max_len - 1;
 
                        memcpy(buf + 1, p, len);
 
@@ -524,7 +580,7 @@ static int load_firmware(struct dvb_frontend *fe, unsigned int type,
 }
 
 static int load_scode(struct dvb_frontend *fe, unsigned int type,
-                        v4l2_std_id *id, int scode)
+                        v4l2_std_id *id, __u16 int_freq, int scode)
 {
        struct xc2028_data *priv = fe->tuner_priv;
        int                pos, rc;
@@ -532,14 +588,40 @@ static int load_scode(struct dvb_frontend *fe, unsigned int type,
 
        tuner_dbg("%s called\n", __FUNCTION__);
 
-       pos = seek_firmware(fe, type, id);
-       if (pos < 0)
-               return pos;
+       if (!int_freq) {
+               pos = seek_firmware(fe, type, id);
+               if (pos < 0)
+                       return pos;
+       } else {
+               for (pos = 0; pos < priv->firm_size; pos++) {
+                       if ((priv->firm[pos].int_freq == int_freq) &&
+                           (priv->firm[pos].type & HAS_IF))
+                               break;
+               }
+               if (pos == priv->firm_size)
+                       return -ENOENT;
+       }
 
        p = priv->firm[pos].ptr;
 
-       if ((priv->firm[pos].size != 12 * 16) || (scode >= 16))
-               return -EINVAL;
+       if (priv->firm[pos].type & HAS_IF) {
+               if (priv->firm[pos].size != 12 * 16 || scode >= 16)
+                       return -EINVAL;
+               p += 12 * scode;
+       } else {
+               /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
+                * has a 2-byte size header in the firmware format. */
+               if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
+                   le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12)
+                       return -EINVAL;
+               p += 14 * scode + 2;
+       }
+
+       tuner_info("Loading SCODE for type=");
+       dump_firm_type_and_int_freq(priv->firm[pos].type,
+                                   priv->firm[pos].int_freq);
+       printk("(%x), id %016llx.\n", priv->firm[pos].type,
+              (unsigned long long)*id);
 
        if (priv->firm_version < 0x0202)
                rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
@@ -548,7 +630,7 @@ static int load_scode(struct dvb_frontend *fe, unsigned int type,
        if (rc < 0)
                return -EIO;
 
-       rc = i2c_send(priv, p + 12 * scode, 12);
+       rc = i2c_send(priv, p, 12);
        if (rc < 0)
                return -EIO;
 
@@ -559,15 +641,14 @@ static int load_scode(struct dvb_frontend *fe, unsigned int type,
        return 0;
 }
 
-static int check_firmware(struct dvb_frontend *fe, enum tuner_mode new_mode,
-                         v4l2_std_id std, fe_bandwidth_t bandwidth)
+static int check_firmware(struct dvb_frontend *fe, unsigned int type,
+                         v4l2_std_id std, __u16 int_freq)
 {
-       struct xc2028_data      *priv = fe->tuner_priv;
-       int                     rc;
-       u16                     version, hwmodel;
-       v4l2_std_id             std0 = 0;
-       unsigned int            type0 = 0, type = 0;
-       int                     change_digital_bandwidth;
+       struct xc2028_data         *priv = fe->tuner_priv;
+       struct firmware_properties new_fw;
+       int                        rc = 0, is_retry = 0;
+       u16                        version, hwmodel;
+       v4l2_std_id                std0;
 
        tuner_dbg("%s called\n", __FUNCTION__);
 
@@ -582,125 +663,159 @@ static int check_firmware(struct dvb_frontend *fe, enum tuner_mode new_mode,
                        return rc;
        }
 
-       tuner_dbg("I am in mode %u and I should switch to mode %i\n",
-                  priv->mode, new_mode);
-
-       /* first of all, determine whether we have switched the mode */
-       if (new_mode != priv->mode) {
-               priv->mode = new_mode;
-               priv->need_load_generic = 1;
-       }
-
-       change_digital_bandwidth = (priv->mode == T_DIGITAL_TV
-                                   && bandwidth != priv->bandwidth) ? 1 : 0;
-       tuner_dbg("old bandwidth %u, new bandwidth %u\n", priv->bandwidth,
-                  bandwidth);
-
-       if (priv->need_load_generic) {
-               /* Reset is needed before loading firmware */
-               rc = priv->tuner_callback(priv->video_dev,
-                                         XC2028_TUNER_RESET, 0);
-               if (rc < 0)
-                       return rc;
-
-               type0 = BASE;
-
-               if (priv->ctrl.type == XC2028_FIRM_MTS)
-                       type0 |= MTS;
-
-               if (bandwidth == BANDWIDTH_7_MHZ ||
-                   bandwidth == BANDWIDTH_8_MHZ)
-                       type0 |= F8MHZ;
-
-               /* FIXME: How to load FM and FM|INPUT1 firmwares? */
+       if (priv->ctrl.mts && !(type & FM))
+               type |= MTS;
 
-               rc = load_firmware(fe, type0, &std0);
-               if (rc < 0) {
-                       tuner_err("Error %d while loading generic firmware\n",
-                                 rc);
-                       return rc;
-               }
+retry:
+       new_fw.type = type;
+       new_fw.id = std;
+       new_fw.std_req = std;
+       new_fw.scode_table = SCODE | priv->ctrl.scode_table;
+       new_fw.scode_nr = 0;
+       new_fw.int_freq = int_freq;
 
-               priv->need_load_generic = 0;
-               priv->firm_type = 0;
-               if (priv->mode == T_DIGITAL_TV)
-                       change_digital_bandwidth = 1;
+       tuner_dbg("checking firmware, user requested type=");
+       if (debug) {
+               dump_firm_type(new_fw.type);
+               printk("(%x), id %016llx, ", new_fw.type,
+                      (unsigned long long)new_fw.std_req);
+               if (!int_freq) {
+                       printk("scode_tbl ");
+                       dump_firm_type(priv->ctrl.scode_table);
+                       printk("(%x), ", priv->ctrl.scode_table);
+               } else
+                       printk("int_freq %d, ", new_fw.int_freq);
+               printk("scode_nr %d\n", new_fw.scode_nr);
        }
 
-       tuner_dbg("I should change bandwidth %u\n", change_digital_bandwidth);
-
-       if (change_digital_bandwidth) {
+       /* No need to reload base firmware if it matches */
+       if (((BASE | new_fw.type) & BASE_TYPES) ==
+           (priv->cur_fw.type & BASE_TYPES)) {
+               tuner_dbg("BASE firmware not changed.\n");
+               goto skip_base;
+       }
 
-               /*FIXME: Should allow selecting between D2620 and D2633 */
-               type |= D2620;
+       /* Updating BASE - forget about all currently loaded firmware */
+       memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
 
-               /* FIXME: When should select a DTV78 firmware?
-                */
-               switch (bandwidth) {
-               case BANDWIDTH_8_MHZ:
-                       type |= DTV8;
-                       break;
-               case BANDWIDTH_7_MHZ:
-                       type |= DTV7;
-                       break;
-               case BANDWIDTH_6_MHZ:
-                       /* FIXME: Should allow select also ATSC */
-                       type |= DTV6 | QAM;
-                       break;
+       /* Reset is needed before loading firmware */
+       rc = priv->tuner_callback(priv->video_dev,
+                                 XC2028_TUNER_RESET, 0);
+       if (rc < 0)
+               goto fail;
 
-               default:
-                       tuner_err("error: bandwidth not supported.\n");
-               };
-               priv->bandwidth = bandwidth;
+       /* BASE firmwares are all std0 */
+       std0 = 0;
+       rc = load_firmware(fe, BASE | new_fw.type, &std0);
+       if (rc < 0) {
+               tuner_err("Error %d while loading base firmware\n",
+                         rc);
+               goto fail;
        }
 
-       if (!change_digital_bandwidth && priv->mode == T_DIGITAL_TV)
-               return 0;
-
        /* Load INIT1, if needed */
        tuner_dbg("Load init1 firmware, if exists\n");
-       type0 = BASE | INIT1;
-       if (priv->ctrl.type == XC2028_FIRM_MTS)
-               type0 |= MTS;
 
-       /* FIXME: Should handle errors - if INIT1 found */
-       rc = load_firmware(fe, type0, &std0);
+       rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
+       if (rc == -ENOENT)
+               rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
+                                  &std0);
+       if (rc < 0 && rc != -ENOENT) {
+               tuner_err("Error %d while loading init1 firmware\n",
+                         rc);
+               goto fail;
+       }
 
-       /* FIXME: Should add support for FM radio
+skip_base:
+       /*
+        * No need to reload standard specific firmware if base firmware
+        * was not reloaded and requested video standards have not changed.
         */
-
-       if (priv->ctrl.type == XC2028_FIRM_MTS)
-               type |= MTS;
-
-       if (priv->firm_type & std) {
+       if (priv->cur_fw.type == (BASE | new_fw.type) &&
+           priv->cur_fw.std_req == std) {
                tuner_dbg("Std-specific firmware already loaded.\n");
-               return 0;
+               goto skip_std_specific;
        }
 
-       /* Add audio hack to std mask */
-       std |= parse_audio_std_option();
+       /* Reloading std-specific firmware forces a SCODE update */
+       priv->cur_fw.scode_table = 0;
+
+       rc = load_firmware(fe, new_fw.type, &new_fw.id);
+       if (rc == -ENOENT)
+               rc = load_firmware(fe, new_fw.type & ~F8MHZ, &new_fw.id);
 
-       rc = load_firmware(fe, type, &std);
        if (rc < 0)
-               return rc;
+               goto fail;
+
+skip_std_specific:
+       if (priv->cur_fw.scode_table == new_fw.scode_table &&
+           priv->cur_fw.scode_nr == new_fw.scode_nr) {
+               tuner_dbg("SCODE firmware already loaded.\n");
+               goto check_device;
+       }
+
+       if (new_fw.type & FM)
+               goto check_device;
 
        /* Load SCODE firmware, if exists */
-       tuner_dbg("Trying to load scode 0\n");
-       type |= SCODE;
+       tuner_dbg("Trying to load scode %d\n", new_fw.scode_nr);
 
-       rc = load_scode(fe, type, &std, 0);
+       rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
+                       new_fw.int_freq, new_fw.scode_nr);
 
-       xc2028_get_reg(priv, 0x0004, &version);
-       xc2028_get_reg(priv, 0x0008, &hwmodel);
+check_device:
+       if (xc2028_get_reg(priv, 0x0004, &version) < 0 ||
+           xc2028_get_reg(priv, 0x0008, &hwmodel) < 0) {
+               tuner_err("Unable to read tuner registers.\n");
+               goto fail;
+       }
 
        tuner_info("Device is Xceive %d version %d.%d, "
                   "firmware version %d.%d\n",
                   hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
                   (version & 0xf0) >> 4, version & 0xf);
 
-       priv->firm_type = std;
+       /* Check firmware version against what we downloaded. */
+       if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
+               tuner_err("Incorrect readback of firmware version.\n");
+               goto fail;
+       }
+
+       /* Check that the tuner hardware model remains consistent over time. */
+       if (priv->hwmodel == 0 && (hwmodel == 2028 || hwmodel == 3028)) {
+               priv->hwmodel = hwmodel;
+               priv->hwvers  = version & 0xff00;
+       } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
+                  priv->hwvers != (version & 0xff00)) {
+               tuner_err("Read invalid device hardware information - tuner "
+                         "hung?\n");
+               goto fail;
+       }
+
+       memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
+
+       /*
+        * By setting BASE in cur_fw.type only after successfully loading all
+        * firmwares, we can:
+        * 1. Identify that BASE firmware with type=0 has been loaded;
+        * 2. Tell whether BASE firmware was just changed the next time through.
+        */
+       priv->cur_fw.type |= BASE;
 
        return 0;
+
+fail:
+       memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
+       if (!is_retry) {
+               msleep(50);
+               is_retry = 1;
+               tuner_dbg("Retrying firmware load\n");
+               goto retry;
+       }
+
+       if (rc == -ENOENT)
+               rc = -EINVAL;
+       return rc;
 }
 
 static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
@@ -735,9 +850,11 @@ ret:
 
 #define DIV 15625
 
-static int generic_set_tv_freq(struct dvb_frontend *fe, u32 freq /* in Hz */ ,
-                              enum tuner_mode new_mode,
-                              v4l2_std_id std, fe_bandwidth_t bandwidth)
+static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */,
+                           enum tuner_mode new_mode,
+                           unsigned int type,
+                           v4l2_std_id std,
+                           u16 int_freq)
 {
        struct xc2028_data *priv = fe->tuner_priv;
        int                rc = -EINVAL;
@@ -748,25 +865,34 @@ static int generic_set_tv_freq(struct dvb_frontend *fe, u32 freq /* in Hz */ ,
 
        mutex_lock(&priv->lock);
 
-       /* HACK: It seems that specific firmware need to be reloaded
-          when freq is changed */
-
-       priv->firm_type = 0;
-
-       /* Reset GPIO 1 */
-       rc = priv->tuner_callback(priv->video_dev, XC2028_TUNER_RESET, 0);
-       if (rc < 0)
-               goto ret;
-
-       msleep(10);
        tuner_dbg("should set frequency %d kHz\n", freq / 1000);
 
-       if (check_firmware(fe, new_mode, std, bandwidth) < 0)
+       if (check_firmware(fe, type, std, int_freq) < 0)
                goto ret;
 
-       if (new_mode == T_DIGITAL_TV) {
+       /* On some cases xc2028 can disable video output, if
+        * very weak signals are received. By sending a soft
+        * reset, this is re-enabled. So, it is better to always
+        * send a soft reset before changing channels, to be sure
+        * that xc2028 will be in a safe state.
+        * Maybe this might also be needed for DTV.
+        */
+       if (new_mode == T_ANALOG_TV) {
+               rc = send_seq(priv, {0x00, 0x00});
+       } else if (priv->cur_fw.type & ATSC) {
+               offset = 1750000;
+       } else {
                offset = 2750000;
-               if (priv->bandwidth == BANDWIDTH_7_MHZ)
+               /*
+                * We must adjust the offset by 500kHz in two cases in order
+                * to correctly center the IF output:
+                * 1) When the ZARLINK456 or DIBCOM52 tables were explicitly
+                *    selected and a 7MHz channel is tuned;
+                * 2) When tuning a VHF channel with DTV78 firmware.
+                */
+               if (((priv->cur_fw.type & DTV7) &&
+                    (priv->cur_fw.scode_table & (ZARLINK456 | DIBCOM52))) ||
+                   ((priv->cur_fw.type & DTV78) && freq < 470000000))
                        offset -= 500000;
        }
 
@@ -810,37 +936,132 @@ ret:
        return rc;
 }
 
-static int xc2028_set_tv_freq(struct dvb_frontend *fe,
+static int xc2028_set_analog_freq(struct dvb_frontend *fe,
                              struct analog_parameters *p)
 {
        struct xc2028_data *priv = fe->tuner_priv;
+       unsigned int       type=0;
 
        tuner_dbg("%s called\n", __FUNCTION__);
 
-       return generic_set_tv_freq(fe, 62500l * p->frequency, T_ANALOG_TV,
-                                  p->std, BANDWIDTH_8_MHZ);
-                                  /* XXX Are some analog standards 6MHz? */
+       if (p->mode == V4L2_TUNER_RADIO) {
+               type |= FM;
+               if (priv->ctrl.input1)
+                       type |= INPUT1;
+               return generic_set_freq(fe, (625l * p->frequency) / 10,
+                               T_ANALOG_TV, type, 0, 0);
+       }
+
+       /* if std is not defined, choose one */
+       if (!p->std)
+               p->std = V4L2_STD_MN;
+
+       /* PAL/M, PAL/N, PAL/Nc and NTSC variants should use 6MHz firmware */
+       if (!(p->std & V4L2_STD_MN))
+               type |= F8MHZ;
+
+       /* Add audio hack to std mask */
+       p->std |= parse_audio_std_option();
+
+       return generic_set_freq(fe, 62500l * p->frequency,
+                               T_ANALOG_TV, type, p->std, 0);
 }
 
 static int xc2028_set_params(struct dvb_frontend *fe,
                             struct dvb_frontend_parameters *p)
 {
        struct xc2028_data *priv = fe->tuner_priv;
+       unsigned int       type=0;
+       fe_bandwidth_t     bw = BANDWIDTH_8_MHZ;
+       u16                demod = 0;
 
        tuner_dbg("%s called\n", __FUNCTION__);
 
-       /* FIXME: Only OFDM implemented */
-       if (fe->ops.info.type != FE_OFDM) {
-               tuner_err("DTV type not implemented.\n");
+       if (priv->ctrl.d2633)
+               type |= D2633;
+       else
+               type |= D2620;
+
+       switch(fe->ops.info.type) {
+       case FE_OFDM:
+               bw = p->u.ofdm.bandwidth;
+               break;
+       case FE_QAM:
+               tuner_info("WARN: There are some reports that "
+                          "QAM 6 MHz doesn't work.\n"
+                          "If this works for you, please report by "
+                          "e-mail to: v4l-dvb-maintainer@linuxtv.org\n");
+               bw = BANDWIDTH_6_MHZ;
+               type |= QAM;
+               break;
+       case FE_ATSC:
+               bw = BANDWIDTH_6_MHZ;
+               /* The only ATSC firmware (at least on v2.7) is D2633,
+                  so overrides ctrl->d2633 */
+               type |= ATSC| D2633;
+               type &= ~D2620;
+               break;
+       /* DVB-S is not supported */
+       default:
                return -EINVAL;
        }
 
-       return generic_set_tv_freq(fe, p->frequency, T_DIGITAL_TV,
-                                  0 /* NOT USED */,
-                                  p->u.ofdm.bandwidth);
+       switch (bw) {
+       case BANDWIDTH_8_MHZ:
+               if (p->frequency < 470000000)
+                       priv->ctrl.vhfbw7 = 0;
+               else
+                       priv->ctrl.uhfbw8 = 1;
+               type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV8;
+               type |= F8MHZ;
+               break;
+       case BANDWIDTH_7_MHZ:
+               if (p->frequency < 470000000)
+                       priv->ctrl.vhfbw7 = 1;
+               else
+                       priv->ctrl.uhfbw8 = 0;
+               type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV7;
+               type |= F8MHZ;
+               break;
+       case BANDWIDTH_6_MHZ:
+               type |= DTV6;
+               priv->ctrl.vhfbw7 = 0;
+               priv->ctrl.uhfbw8 = 0;
+               break;
+       default:
+               tuner_err("error: bandwidth not supported.\n");
+       };
+
+       /* All S-code tables need a 200kHz shift */
+       if (priv->ctrl.demod)
+               demod = priv->ctrl.demod + 200;
+
+       return generic_set_freq(fe, p->frequency,
+                               T_DIGITAL_TV, type, 0, demod);
+}
+
+static int xc2028_sleep(struct dvb_frontend *fe)
+{
+       struct xc2028_data *priv = fe->tuner_priv;
+       int rc = 0;
+
+       tuner_dbg("%s called\n", __FUNCTION__);
+
+       mutex_lock(&priv->lock);
+
+       if (priv->firm_version < 0x0202)
+               rc = send_seq(priv, {0x00, 0x08, 0x00, 0x00});
+       else
+               rc = send_seq(priv, {0x80, 0x08, 0x00, 0x00});
+
+       priv->cur_fw.type = 0;  /* need firmware reload */
+
+       mutex_unlock(&priv->lock);
 
+       return rc;
 }
 
+
 static int xc2028_dvb_release(struct dvb_frontend *fe)
 {
        struct xc2028_data *priv = fe->tuner_priv;
@@ -881,32 +1102,30 @@ static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
 {
        struct xc2028_data *priv = fe->tuner_priv;
        struct xc2028_ctrl *p    = priv_cfg;
+       int                 rc   = 0;
 
        tuner_dbg("%s called\n", __FUNCTION__);
 
        mutex_lock(&priv->lock);
 
-       priv->ctrl.type = p->type;
-
-       if (p->fname) {
-               kfree(priv->ctrl.fname);
+       kfree(priv->ctrl.fname);
+       free_firmware(priv);
 
-               priv->ctrl.fname = kmalloc(strlen(p->fname) + 1, GFP_KERNEL);
-               if (priv->ctrl.fname == NULL) {
-                       mutex_unlock(&priv->lock);
-                       return -ENOMEM;
-               }
+       memcpy(&priv->ctrl, p, sizeof(priv->ctrl));
+       priv->ctrl.fname = NULL;
 
-               free_firmware(priv);
-               strcpy(priv->ctrl.fname, p->fname);
+       if (p->fname) {
+               priv->ctrl.fname = kstrdup(p->fname, GFP_KERNEL);
+               if (priv->ctrl.fname == NULL)
+                       rc = -ENOMEM;
        }
 
-       if (p->max_len > 0)
-               priv->max_len = p->max_len;
+       if (priv->ctrl.max_len < 9)
+               priv->ctrl.max_len = 13;
 
        mutex_unlock(&priv->lock);
 
-       return 0;
+       return rc;
 }
 
 static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
@@ -918,15 +1137,17 @@ static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
                 },
 
        .set_config        = xc2028_set_config,
-       .set_analog_params = xc2028_set_tv_freq,
+       .set_analog_params = xc2028_set_analog_freq,
        .release           = xc2028_dvb_release,
        .get_frequency     = xc2028_get_frequency,
        .get_rf_strength   = xc2028_signal,
        .set_params        = xc2028_set_params,
+       .sleep             = xc2028_sleep,
 
 };
 
-void *xc2028_attach(struct dvb_frontend *fe, struct xc2028_config *cfg)
+struct dvb_frontend *xc2028_attach(struct dvb_frontend *fe,
+                                  struct xc2028_config *cfg)
 {
        struct xc2028_data *priv;
        void               *video_dev;
@@ -934,7 +1155,7 @@ void *xc2028_attach(struct dvb_frontend *fe, struct xc2028_config *cfg)
        if (debug)
                printk(KERN_DEBUG PREFIX ": Xcv2028/3028 init called!\n");
 
-       if (NULL == cfg->video_dev)
+       if (NULL == cfg || NULL == cfg->video_dev)
                return NULL;
 
        if (!fe) {
@@ -960,14 +1181,11 @@ void *xc2028_attach(struct dvb_frontend *fe, struct xc2028_config *cfg)
                        return NULL;
                }
 
-               priv->bandwidth = BANDWIDTH_6_MHZ;
-               priv->need_load_generic = 1;
-               priv->mode = T_UNINITIALIZED;
                priv->i2c_props.addr = cfg->i2c_addr;
                priv->i2c_props.adap = cfg->i2c_adap;
                priv->video_dev = video_dev;
                priv->tuner_callback = cfg->callback;
-               priv->max_len = 13;
+               priv->ctrl.max_len = 13;
 
                mutex_init(&priv->lock);
 
@@ -982,6 +1200,9 @@ void *xc2028_attach(struct dvb_frontend *fe, struct xc2028_config *cfg)
 
        tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
 
+       if (cfg->ctrl)
+               xc2028_set_config(fe, cfg->ctrl);
+
        mutex_unlock(&xc2028_list_mutex);
 
        return fe;