]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/media/video/tuner-xc2028.c
V4L/DVB (6954): There isn't a MTS radio firmware
[linux-2.6-omap-h63xx.git] / drivers / media / video / tuner-xc2028.c
index b9135b7c43378374b584de7f16e7d3d7b9b7dd9f..f191f6a4807068869ef360d053325d08a7e3f14a 100644 (file)
@@ -11,7 +11,7 @@
 #include <linux/i2c.h>
 #include <asm/div64.h>
 #include <linux/firmware.h>
-#include <linux/videodev.h>
+#include <linux/videodev2.h>
 #include <linux/delay.h>
 #include <media/tuner.h>
 #include <linux/mutex.h>
 #include <linux/dvb/frontend.h>
 #include "dvb_frontend.h"
 
-#define PREFIX "xc2028 "
+
+#define PREFIX "xc2028"
+
+static int debug;
+module_param(debug, int, 0644);
+MODULE_PARM_DESC(debug, "enable verbose debug messages");
+
+static char audio_std[8];
+module_param_string(audio_std, audio_std, sizeof(audio_std), 0);
+MODULE_PARM_DESC(audio_std,
+       "Audio standard. XC3028 audio decoder explicitly "
+       "needs to know what audio\n"
+       "standard is needed for some video standards with audio A2 or NICAM.\n"
+       "The valid values are:\n"
+       "A2\n"
+       "A2/A\n"
+       "A2/B\n"
+       "NICAM\n"
+       "NICAM/A\n"
+       "NICAM/B\n");
 
 static LIST_HEAD(xc2028_list);
+static DEFINE_MUTEX(xc2028_list_mutex);
+
 /* struct for storing firmware table */
 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;
        int                     (*tuner_callback) (void *dev,
                                                   int command, int arg);
-       struct device           *dev;
        void                    *video_dev;
        int                     count;
        __u32                   frequency;
 
        struct firmware_description *firm;
        int                     firm_size;
+       __u16                   firm_version;
 
-       __u16                   version;
+       __u16                   hwmodel;
+       __u16                   hwvers;
 
        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? */
-
-       int                     max_len;        /* Max firmware chunk */
-
-       enum tuner_mode mode;
-       struct i2c_client       *i2c_client;
+       struct firmware_properties cur_fw;
 
        struct mutex lock;
 };
 
-#define i2c_send(rc, priv, buf, size)                                  \
-if (size != (rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size)))   \
-       tuner_info("i2c output error: rc = %d (should be %d)\n",        \
-       rc, (int)size);
-
-#define i2c_rcv(rc, priv, buf, size)                                   \
-if (size != (rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size)))   \
-       tuner_info("i2c input error: rc = %d (should be %d)\n",         \
-       rc, (int)size);
-
-#define send_seq(priv, data...)                                                \
-{      int rc;                                                         \
+#define i2c_send(priv, buf, size) ({                                   \
+       int _rc;                                                        \
+       _rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size);         \
+       if (size != _rc)                                                \
+               tuner_info("i2c output error: rc = %d (should be %d)\n",\
+                          _rc, (int)size);                             \
+       _rc;                                                            \
+})
+
+#define i2c_rcv(priv, buf, size) ({                                    \
+       int _rc;                                                        \
+       _rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size);         \
+       if (size != _rc)                                                \
+               tuner_err("i2c input error: rc = %d (should be %d)\n",  \
+                          _rc, (int)size);                             \
+       _rc;                                                            \
+})
+
+#define i2c_send_recv(priv, obuf, osize, ibuf, isize) ({               \
+       int _rc;                                                        \
+       _rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, obuf, osize,   \
+                                      ibuf, isize);                    \
+       if (isize != _rc)                                               \
+               tuner_err("i2c input error: rc = %d (should be %d)\n",  \
+                          _rc, (int)isize);                            \
+       _rc;                                                            \
+})
+
+#define send_seq(priv, data...)        ({                                      \
        static u8 _val[] = data;                                        \
+       int _rc;                                                        \
        if (sizeof(_val) !=                                             \
-                       (rc = tuner_i2c_xfer_send (&priv->i2c_props,    \
+                       (_rc = tuner_i2c_xfer_send(&priv->i2c_props,    \
                                                _val, sizeof(_val)))) { \
-               tuner_info("Error on line %d: %d\n",__LINE__,rc);       \
-               return -EINVAL;                                                 \
-       }                                                               \
-       msleep (10);                                                    \
-}
+               tuner_err("Error on line %d: %d\n", __LINE__, _rc);     \
+       } else                                                          \
+               msleep(10);                                             \
+       _rc;                                                            \
+})
 
-static int xc2028_get_reg(struct xc2028_data *priv, u16 reg)
+static unsigned int xc2028_get_reg(struct xc2028_data *priv, u16 reg, u16 *val)
 {
-       int rc;
-       unsigned char buf[1];
+       unsigned char buf[2];
+       unsigned char ibuf[2];
 
-       tuner_info("%s called\n", __FUNCTION__);
+       tuner_dbg("%s %04x called\n", __FUNCTION__, reg);
 
-       buf[0]= reg;
+       buf[0] = reg >> 8;
+       buf[1] = (unsigned char) reg;
 
-       i2c_send(rc, priv, buf, sizeof(buf));
-       if (rc<0)
-               return rc;
+       if (i2c_send_recv(priv, buf, 2, ibuf, 2) != 2)
+               return -EIO;
 
-       i2c_rcv(rc, priv, buf, 2);
-       if (rc<0)
-               return rc;
+       *val = (ibuf[1]) | (ibuf[0] << 8);
+       return 0;
+}
 
-       return (buf[1])|(buf[0]<<8);
+#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 ");
+        if (type & INIT1)
+               printk("INIT1 ");
+        if (type & F8MHZ)
+               printk("F8MHZ ");
+        if (type & MTS)
+               printk("MTS ");
+        if (type & D2620)
+               printk("D2620 ");
+        if (type & D2633)
+               printk("D2633 ");
+        if (type & DTV6)
+               printk("DTV6 ");
+        if (type & QAM)
+               printk("QAM ");
+        if (type & DTV7)
+               printk("DTV7 ");
+        if (type & DTV78)
+               printk("DTV78 ");
+        if (type & DTV8)
+               printk("DTV8 ");
+        if (type & FM)
+               printk("FM ");
+        if (type & INPUT1)
+               printk("INPUT1 ");
+        if (type & LCD)
+               printk("LCD ");
+        if (type & NOGD)
+               printk("NOGD ");
+        if (type & MONO)
+               printk("MONO ");
+        if (type & ATSC)
+               printk("ATSC ");
+        if (type & IF)
+               printk("IF ");
+        if (type & LG60)
+               printk("LG60 ");
+        if (type & ATI638)
+               printk("ATI638 ");
+        if (type & OREN538)
+               printk("OREN538 ");
+        if (type & OREN36)
+               printk("OREN36 ");
+        if (type & TOYOTA388)
+               printk("TOYOTA388 ");
+        if (type & TOYOTA794)
+               printk("TOYOTA794 ");
+        if (type & DIBCOM52)
+               printk("DIBCOM52 ");
+        if (type & ZARLINK456)
+               printk("ZARLINK456 ");
+        if (type & CHINA)
+               printk("CHINA ");
+        if (type & F6MHZ)
+               printk("F6MHZ ");
+        if (type & INPUT2)
+               printk("INPUT2 ");
+        if (type & SCODE)
+               printk("SCODE ");
+        if (type & HAS_IF)
+               printk("HAS_IF_%d ", int_freq);
 }
 
-static void free_firmware (struct xc2028_data *priv)
+static  v4l2_std_id parse_audio_std_option(void)
+{
+       if (strcasecmp(audio_std, "A2") == 0)
+               return V4L2_STD_A2;
+       if (strcasecmp(audio_std, "A2/A") == 0)
+               return V4L2_STD_A2_A;
+       if (strcasecmp(audio_std, "A2/B") == 0)
+               return V4L2_STD_A2_B;
+       if (strcasecmp(audio_std, "NICAM") == 0)
+               return V4L2_STD_NICAM;
+       if (strcasecmp(audio_std, "NICAM/A") == 0)
+               return V4L2_STD_NICAM_A;
+       if (strcasecmp(audio_std, "NICAM/B") == 0)
+               return V4L2_STD_NICAM_B;
+
+       return 0;
+}
+
+static void free_firmware(struct xc2028_data *priv)
 {
        int i;
 
        if (!priv->firm)
                return;
 
-       for (i=0;i<priv->firm_size;i++) {
-               if (priv->firm[i].ptr)
-                       kfree(priv->firm[i].ptr);
-       }
+       for (i = 0; i < priv->firm_size; i++)
+               kfree(priv->firm[i].ptr);
+
        kfree(priv->firm);
 
-       priv->firm=NULL;
-       priv->need_load_generic = 1;
+       priv->firm = NULL;
+       priv->firm_size = 0;
+
+       memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
 }
 
-static int load_all_firmwares (struct dvb_frontend *fe)
+static int load_all_firmwares(struct dvb_frontend *fe)
 {
        struct xc2028_data    *priv = fe->tuner_priv;
-       const struct firmware *fw=NULL;
+       const struct firmware *fw   = NULL;
        unsigned char         *p, *endp;
-       int                   rc=0, n, n_array;
+       int                   rc = 0;
+       int                   n, n_array;
        char                  name[33];
 
-       tuner_info("%s called\n", __FUNCTION__);
+       tuner_dbg("%s called\n", __FUNCTION__);
 
-       tuner_info("Loading firmware %s\n", priv->ctrl.fname);
-       rc = request_firmware(&fw, priv->ctrl.fname, priv->dev);
+       tuner_dbg("Reading firmware %s\n", priv->ctrl.fname);
+       rc = request_firmware(&fw, priv->ctrl.fname,
+                             &priv->i2c_props.adap->dev);
        if (rc < 0) {
-               if (rc==-ENOENT)
-                       tuner_info("Error: firmware %s not found.\n",
+               if (rc == -ENOENT)
+                       tuner_err("Error: firmware %s not found.\n",
                                   priv->ctrl.fname);
                else
-                       tuner_info("Error %d while requesting firmware %s \n",
+                       tuner_err("Error %d while requesting firmware %s \n",
                                   rc, priv->ctrl.fname);
 
                return rc;
        }
-       p=fw->data;
-       endp=p+fw->size;
+       p = fw->data;
+       endp = p + fw->size;
 
-       if(fw->size<sizeof(name)-1+2) {
-               tuner_info("Error: firmware size is zero!\n");
-               rc=-EINVAL;
-               goto done;
+       if (fw->size < sizeof(name) - 1 + 2 + 2) {
+               tuner_err("Error: firmware file %s has invalid size!\n",
+                         priv->ctrl.fname);
+               goto corrupt;
        }
 
-       memcpy(name,p,sizeof(name)-1);
-       name[sizeof(name)-1]=0;
-       p+=sizeof(name)-1;
+       memcpy(name, p, sizeof(name) - 1);
+       name[sizeof(name) - 1] = 0;
+       p += sizeof(name) - 1;
 
-       priv->version = le16_to_cpu(*(__u16 *)p);
+       priv->firm_version = le16_to_cpu(*(__u16 *) p);
        p += 2;
 
-       tuner_info("firmware: %s, ver %d.%d\n", name,
-                                       priv->version>>8, priv->version&0xff);
-
-       if (p+2>endp)
-               goto corrupt;
-
-       n_array = le16_to_cpu(*(__u16 *)p);
+       n_array = le16_to_cpu(*(__u16 *) p);
        p += 2;
 
-       tuner_info("there are %d firmwares at %s\n", n_array, priv->ctrl.fname);
-
-       priv->firm=kzalloc(sizeof(*priv->firm)*n_array,GFP_KERNEL);
+       tuner_info("Loading %d firmware images from %s, type: %s, ver %d.%d\n",
+                  n_array, priv->ctrl.fname, name,
+                  priv->firm_version >> 8, priv->firm_version & 0xff);
 
-       if (!fw) {
-               tuner_info("Not enough memory for loading firmware.\n");
-               rc=-ENOMEM;
-               goto done;
+       priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
+       if (priv->firm == NULL) {
+               tuner_err("Not enough memory to load firmware file.\n");
+               rc = -ENOMEM;
+               goto err;
        }
-
        priv->firm_size = n_array;
-       n=-1;
-       while (p<endp) {
+
+       n = -1;
+       while (p < endp) {
                __u32 type, size;
                v4l2_std_id id;
+               __u16 int_freq = 0;
 
                n++;
                if (n >= n_array) {
-                       tuner_info("Too much firmwares at the file\n");
+                       tuner_err("More firmware images in file than "
+                                 "were expected!\n");
                        goto corrupt;
                }
 
                /* Checks if there's enough bytes to read */
-               if (p+sizeof(type)+sizeof(id)+sizeof(size)>endp) {
-                       tuner_info("Lost firmware!\n");
+               if (p + sizeof(type) + sizeof(id) + sizeof(size) > endp) {
+                       tuner_err("Firmware header is incomplete!\n");
                        goto corrupt;
                }
 
-               type = le32_to_cpu(*(__u32 *)p);
+               type = le32_to_cpu(*(__u32 *) p);
                p += sizeof(type);
 
-               id = le64_to_cpu(*(v4l2_std_id *)p);
+               id = le64_to_cpu(*(v4l2_std_id *) p);
                p += sizeof(id);
 
-               size = le32_to_cpu(*(v4l2_std_id *)p);
+               if (type & HAS_IF) {
+                       int_freq = le16_to_cpu(*(__u16 *) p);
+                       p += sizeof(int_freq);
+               }
+
+               size = le32_to_cpu(*(__u32 *) p);
                p += sizeof(size);
 
-               if ((!size)||(size+p>endp)) {
-                       tuner_info("Firmware type %x, id %lx corrupt\n",
-                                  type, (unsigned long) id);
+               if ((!size) || (size + p > endp)) {
+                       tuner_err("Firmware type ");
+                       dump_firm_type(type);
+                       printk("(%x), id %llx is corrupted "
+                              "(size=%d, expected %d)\n",
+                              type, (unsigned long long)id,
+                              (unsigned)(endp - p), size);
                        goto corrupt;
                }
 
-               priv->firm[n].ptr=kzalloc(size,GFP_KERNEL);
-               if (!priv->firm[n].ptr) {
-                       tuner_info("Not enough memory.\n");
-                       rc=-ENOMEM;
+               priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
+               if (priv->firm[n].ptr == NULL) {
+                       tuner_err("Not enough memory to load firmware file.\n");
+                       rc = -ENOMEM;
                        goto err;
                }
-               tuner_info("Loading firmware type %x, id %lx, size=%d.\n",
-                                  type, (unsigned long) id, size);
+               tuner_dbg("Reading firmware type ");
+               if (debug) {
+                       dump_firm_type_and_int_freq(type, int_freq);
+                       printk("(%x), id %llx, size=%d.\n",
+                              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;
        }
 
-       if (n+1 != priv->firm_size) {
-               tuner_info("Firmware file is incomplete!\n");
+       if (n + 1 != priv->firm_size) {
+               tuner_err("Firmware file is incomplete!\n");
                goto corrupt;
        }
 
        goto done;
 
 corrupt:
-       rc=-EINVAL;
-       tuner_info("Error: firmware file is corrupted!\n");
+       rc = -EINVAL;
+       tuner_err("Error: firmware file is corrupted!\n");
 
 err:
-       tuner_info("Releasing loaded firmware file.\n");
-
+       tuner_info("Releasing partially loaded firmware file.\n");
        free_firmware(priv);
 
 done:
        release_firmware(fw);
-       tuner_info("Firmware files loaded.\n");
+       if (rc == 0)
+               tuner_dbg("Firmware files loaded.\n");
 
        return rc;
 }
 
-static int load_firmware (struct dvb_frontend *fe, unsigned int type,
-                         v4l2_std_id *id)
+static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
+                        v4l2_std_id *id)
 {
        struct xc2028_data *priv = fe->tuner_priv;
-       int i, rc;
-       unsigned char *p, *endp, buf[priv->max_len];
+       int                 i, best_i = -1, best_nr_matches = 0;
+       unsigned int        ign_firm_type_mask = 0;
 
-       tuner_info("%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) {
-               printk (KERN_ERR PREFIX "Error! firmware not loaded\n");
+               tuner_err("Error! firmware not loaded\n");
                return -EINVAL;
        }
 
-       if ((type == 0) && (*id == 0))
-               *id=V4L2_STD_PAL;
+       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))
+       for (i = 0; i < priv->firm_size; i++) {
+               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;
+       for (i = 0; i < priv->firm_size; i++) {
+               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 ? */
 
-       tuner_info ("Can't find firmware for type=%x, id=%lx\n", type,
-                   (long int)*id);
-       return -EINVAL;
+       i = -ENOENT;
+       goto ret;
 
 found:
        *id = priv->firm[i].id;
-       tuner_info ("Found firmware for type=%x, id=%lx\n", type,
-                   (long int)*id);
-
-       p = priv->firm[i].ptr;
 
-       if (!p) {
-               printk(KERN_ERR PREFIX "Firmware pointer were freed!");
-               return -EINVAL;
+ret:
+       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);
        }
-       endp = p+priv->firm[i].size;
+       return i;
+}
+
+static int load_firmware(struct dvb_frontend *fe, unsigned int type,
+                        v4l2_std_id *id)
+{
+       struct xc2028_data *priv = fe->tuner_priv;
+       int                pos, rc;
+       unsigned char      *p, *endp, buf[priv->ctrl.max_len];
+
+       tuner_dbg("%s called\n", __FUNCTION__);
+
+       pos = seek_firmware(fe, type, id);
+       if (pos < 0)
+               return pos;
 
-       while (p<endp) {
+       tuner_info("Loading firmware for type=");
+       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;
+
+       while (p < endp) {
                __u16 size;
 
                /* Checks if there's enough bytes to read */
-               if (p+sizeof(size)>endp) {
-                       tuner_info("missing bytes\n");
+               if (p + sizeof(size) > endp) {
+                       tuner_err("Firmware chunk size is wrong\n");
                        return -EINVAL;
                }
 
-
-               size = le16_to_cpu(*(__u16 *)p);
+               size = le16_to_cpu(*(__u16 *) p);
                p += sizeof(size);
 
                if (size == 0xffff)
@@ -322,24 +515,43 @@ found:
                if (!size) {
                        /* Special callback command received */
                        rc = priv->tuner_callback(priv->video_dev,
-                                            XC2028_TUNER_RESET, 0);
-                       if (rc<0) {
-                               tuner_info("Error at RESET code %d\n",
-                                                               (*p)&0x7f);
+                                                 XC2028_TUNER_RESET, 0);
+                       if (rc < 0) {
+                               tuner_err("Error at RESET code %d\n",
+                                          (*p) & 0x7f);
                                return -EINVAL;
                        }
                        continue;
                }
+               if (size >= 0xff00) {
+                       switch (size) {
+                       case 0xff00:
+                               rc = priv->tuner_callback(priv->video_dev,
+                                                       XC2028_RESET_CLK, 0);
+                               if (rc < 0) {
+                                       tuner_err("Error at RESET code %d\n",
+                                                 (*p) & 0x7f);
+                                       return -EINVAL;
+                               }
+                               break;
+                       default:
+                               tuner_info("Invalid RESET code %d\n",
+                                          size & 0x7f);
+                               return -EINVAL;
+
+                       }
+                       continue;
+               }
 
                /* Checks for a sleep command */
                if (size & 0x8000) {
-                       msleep (size & 0x7fff);
+                       msleep(size & 0x7fff);
                        continue;
                }
 
                if ((size + p > endp)) {
-                       tuner_info("missing bytes: need %d, have %d\n",
-                                       size, (int)(endp-p));
+                       tuner_err("missing bytes: need %d, have %d\n",
+                                  size, (int)(endp - p));
                        return -EINVAL;
                }
 
@@ -348,14 +560,15 @@ found:
                size--;
 
                /* Sends message chunks */
-               while (size>0) {
-                       int len = (size<priv->max_len-1)?size:priv->max_len-1;
+               while (size > 0) {
+                       int len = (size < priv->ctrl.max_len - 1) ?
+                                  size : priv->ctrl.max_len - 1;
 
-                       memcpy(buf+1, p, len);
+                       memcpy(buf + 1, p, len);
 
-                       i2c_send(rc, priv, buf, len+1);
-                       if (rc<0) {
-                               tuner_info("%d returned from send\n",rc);
+                       rc = i2c_send(priv, buf, len + 1);
+                       if (rc < 0) {
+                               tuner_err("%d returned from send\n", rc);
                                return -EINVAL;
                        }
 
@@ -363,237 +576,356 @@ found:
                        size -= len;
                }
        }
-       return -EINVAL;
+       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 load_scode(struct dvb_frontend *fe, unsigned int type,
+                        v4l2_std_id *id, __u16 int_freq, int scode)
 {
-       struct xc2028_data      *priv = fe->tuner_priv;
-       int                     rc, version;
-       v4l2_std_id             std0=0;
-       unsigned int            type0=0,type=0;
-       int                     change_digital_bandwidth;
-
-       tuner_info("%s called\n", __FUNCTION__);
+       struct xc2028_data *priv = fe->tuner_priv;
+       int                pos, rc;
+       unsigned char      *p;
 
-       if (!priv->firm) {
-               if (!priv->ctrl.fname)
-                       return -EINVAL;
+       tuner_dbg("%s called\n", __FUNCTION__);
 
-               rc=load_all_firmwares(fe);
-               if (rc<0)
-                       return rc;
+       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;
        }
 
-       tuner_info( "I am in mode %u and I should switch to mode %i\n",
-                                                   priv->mode, new_mode);
+       p = priv->firm[pos].ptr;
 
-       /* first of all, determine whether we have switched the mode */
-       if(new_mode != priv->mode) {
-               priv->mode = new_mode;
-               priv->need_load_generic = 1;
+       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;
        }
 
-       change_digital_bandwidth = (priv->mode == T_DIGITAL_TV
-                                && bandwidth != priv->bandwidth) ? 1 : 0;
-       tuner_info("old bandwidth %u, new bandwidth %u\n", priv->bandwidth,
-                   bandwidth);
+       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->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;
+       if (priv->firm_version < 0x0202)
+               rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
+       else
+               rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
+       if (rc < 0)
+               return -EIO;
 
-               type0=BASE;
+       rc = i2c_send(priv, p, 12);
+       if (rc < 0)
+               return -EIO;
 
-               if (priv->ctrl.type == XC2028_FIRM_MTS)
-                       type0 |= MTS;
+       rc = send_seq(priv, {0x00, 0x8c});
+       if (rc < 0)
+               return -EIO;
+
+       return 0;
+}
 
-               if (priv->bandwidth==8)
-                       type0 |= F8MHZ;
+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;
+       struct firmware_properties new_fw;
+       int                        rc = 0, is_retry = 0;
+       u16                        version, hwmodel;
+       v4l2_std_id                std0;
 
-               /* FIXME: How to load FM and FM|INPUT1 firmwares? */
+       tuner_dbg("%s called\n", __FUNCTION__);
 
-               rc = load_firmware(fe, type0, &std0);
-               if (rc<0) {
-                       tuner_info("Error %d while loading generic firmware\n",
-                                  rc);
-                       return rc;
+       if (!priv->firm) {
+               if (!priv->ctrl.fname) {
+                       tuner_info("xc2028/3028 firmware name not set!\n");
+                       return -EINVAL;
                }
 
-               priv->need_load_generic=0;
-               priv->firm_type=0;
-               if(priv->mode == T_DIGITAL_TV) {
-                       change_digital_bandwidth=1;
-               }
+               rc = load_all_firmwares(fe);
+               if (rc < 0)
+                       return rc;
        }
 
-       tuner_info("I should change bandwidth %u\n",
-                                                  change_digital_bandwidth);
+       if (priv->ctrl.mts && !(type & FM))
+               type |= MTS;
 
-       if (change_digital_bandwidth) {
+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;
+
+       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);
+       }
 
-               /*FIXME: Should allow selecting between D2620 and D2633 */
-               type |= D2620;
+       /* 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: 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;
+       /* Updating BASE - forget about all currently loaded firmware */
+       memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
+
+       /* Reset is needed before loading firmware */
+       rc = priv->tuner_callback(priv->video_dev,
+                                 XC2028_TUNER_RESET, 0);
+       if (rc < 0)
+               goto fail;
 
-               default:
-                       tuner_info("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;
        }
 
        /* Load INIT1, if needed */
-       tuner_info("Trying to load init1 firmware\n");
-       type0 = BASE | INIT1 | priv->ctrl.type;
-       if (priv->ctrl.type == XC2028_FIRM_MTS)
-               type0 |= MTS;
-
-       /* FIXME: Should handle errors - if INIT1 found */
-       rc = load_firmware(fe, type0, &std0);
+       tuner_dbg("Load init1 firmware, if exists\n");
+
+       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->cur_fw.type == (BASE | new_fw.type) &&
+           priv->cur_fw.std_req == std) {
+               tuner_dbg("Std-specific firmware already loaded.\n");
+               goto skip_std_specific;
+       }
 
-       if (priv->ctrl.type == XC2028_FIRM_MTS)
-               type |= MTS;
+       /* 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);
 
-       tuner_info("firmware standard to load: %08lx\n",(unsigned long) std);
-       if (priv->firm_type & std) {
-               tuner_info("no need to load a std-specific firmware.\n");
-               return 0;
+       if (rc < 0)
+               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;
        }
 
-       rc = load_firmware(fe, type, &std);
-       if (rc<0)
-               return rc;
+       /* Load SCODE firmware, if exists */
+       tuner_dbg("Trying to load scode %d\n", new_fw.scode_nr);
+
+       rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
+                       new_fw.int_freq, new_fw.scode_nr);
 
-       version = xc2028_get_reg(priv, 0x4);
-       tuner_info("Firmware version is %d.%d\n",
-                                       (version>>4)&0x0f,(version)&0x0f);
+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)
 {
        struct xc2028_data *priv = fe->tuner_priv;
-       int                frq_lock, signal=0;
+       u16                 frq_lock, signal = 0;
+       int                 rc;
 
-       tuner_info("%s called\n", __FUNCTION__);
+       tuner_dbg("%s called\n", __FUNCTION__);
 
        mutex_lock(&priv->lock);
 
-       *strength = 0;
-
-       frq_lock = xc2028_get_reg(priv, 0x2);
-       if (frq_lock<=0)
+       /* Sync Lock Indicator */
+       rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
+       if (rc < 0 || frq_lock == 0)
                goto ret;
 
        /* Frequency is locked. Return signal quality */
 
-       signal = xc2028_get_reg(priv, 0x40);
-
-       if(signal<=0) {
-               signal=frq_lock;
-       }
+       /* Get SNR of the video signal */
+       rc = xc2028_get_reg(priv, 0x0040, &signal);
+       if (rc < 0)
+               signal = -frq_lock;
 
 ret:
        mutex_unlock(&priv->lock);
 
        *strength = signal;
 
-       return 0;
+       return rc;
 }
 
 #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;
-       unsigned char buf[5];
-       u32 div, offset = 0;
+       int                rc = -EINVAL;
+       unsigned char      buf[4];
+       u32                div, offset = 0;
 
-       tuner_info("%s called\n", __FUNCTION__);
+       tuner_dbg("%s called\n", __FUNCTION__);
 
        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_info("should set frequency %d kHz)\n", freq / 1000);
+       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;
+               /*
+                * 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;
+       }
 
-       div = (freq - offset + DIV/2)/DIV;
+       div = (freq - offset + DIV / 2) / DIV;
 
        /* CMD= Set frequency */
-
-       if (priv->version<0x0202) {
-               send_seq(priv, {0x00, 0x02, 0x00, 0x00});
-       } else {
-               send_seq(priv, {0x80, 0x02, 0x00, 0x00});
-       }
+       if (priv->firm_version < 0x0202)
+               rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
+       else
+               rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
+       if (rc < 0)
+               goto ret;
 
        rc = priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
-       if (rc<0)
+       if (rc < 0)
                goto ret;
 
        msleep(10);
 
-       buf[0]= 0xff & (div>>24);
-       buf[1]= 0xff & (div>>16);
-       buf[2]= 0xff & (div>>8);
-       buf[3]= 0xff & (div);
-       buf[4]= 0;
+       buf[0] = 0xff & (div >> 24);
+       buf[1] = 0xff & (div >> 16);
+       buf[2] = 0xff & (div >> 8);
+       buf[3] = 0xff & (div);
 
-       i2c_send(rc, priv, buf, sizeof(buf));
-       if (rc<0)
+       rc = i2c_send(priv, buf, sizeof(buf));
+       if (rc < 0)
                goto ret;
        msleep(100);
 
-       priv->frequency=freq;
+       priv->frequency = freq;
 
-       printk("divider= %02x %02x %02x %02x (freq=%d.%02d)\n",
-                buf[1],buf[2],buf[3],buf[4],
-                freq / 1000000, (freq%1000000)/10000);
+       tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n",
+              buf[0], buf[1], buf[2], buf[3],
+              freq / 1000000, (freq % 1000000) / 1000);
 
-       rc=0;
+       rc = 0;
 
 ret:
        mutex_unlock(&priv->lock);
@@ -601,55 +933,154 @@ ret:
        return rc;
 }
 
-static int xc2028_set_tv_freq(struct dvb_frontend *fe,
-                       struct analog_parameters *p)
+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_info("%s called\n", __FUNCTION__);
+       tuner_dbg("%s called\n", __FUNCTION__);
 
-       return generic_set_tv_freq(fe, 62500l*p->frequency, T_ANALOG_TV,
-                                             p->std,
-                                             BANDWIDTH_8_MHZ /* NOT USED */);
+       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_info("%s called\n", __FUNCTION__);
+       tuner_dbg("%s called\n", __FUNCTION__);
 
-       /* FIXME: Only OFDM implemented */
-       if (fe->ops.info.type != FE_OFDM) {
-               tuner_info ("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;
 
-       tuner_info("%s called\n", __FUNCTION__);
+       tuner_dbg("%s called\n", __FUNCTION__);
+
+       mutex_lock(&xc2028_list_mutex);
 
        priv->count--;
 
        if (!priv->count) {
                list_del(&priv->xc2028_list);
 
-               if (priv->ctrl.fname)
-                       kfree(priv->ctrl.fname);
+               kfree(priv->ctrl.fname);
 
                free_firmware(priv);
-               kfree (priv);
+               kfree(priv);
+               fe->tuner_priv = NULL;
        }
 
+       mutex_unlock(&xc2028_list_mutex);
+
        return 0;
 }
 
@@ -657,117 +1088,121 @@ static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
 {
        struct xc2028_data *priv = fe->tuner_priv;
 
-       tuner_info("%s called\n", __FUNCTION__);
+       tuner_dbg("%s called\n", __FUNCTION__);
 
        *frequency = priv->frequency;
 
        return 0;
 }
 
-static int xc2028_set_config (struct dvb_frontend *fe, void *priv_cfg)
+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_info("%s called\n", __FUNCTION__);
+       tuner_dbg("%s called\n", __FUNCTION__);
 
-       priv->ctrl.type = p->type;
+       mutex_lock(&priv->lock);
 
-       if (p->fname) {
-               if (priv->ctrl.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)
-                       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;
 
-       tuner_info("%s OK\n", __FUNCTION__);
+       mutex_unlock(&priv->lock);
 
-       return 0;
+       return rc;
 }
 
 static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
        .info = {
-                       .name           = "Xceive XC3028",
-                       .frequency_min  =  42000000,
-                       .frequency_max  = 864000000,
-                       .frequency_step =     50000,
-               },
+                .name = "Xceive XC3028",
+                .frequency_min = 42000000,
+                .frequency_max = 864000000,
+                .frequency_step = 50000,
+                },
 
        .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,
 
-//     int (*sleep)(struct dvb_frontend *fe);
-//     int (*get_bandwidth)(struct dvb_frontend *fe, u32 *bandwidth);
-//     int (*get_status)(struct dvb_frontend *fe, u32 *status);
 };
 
-int xc2028_attach(struct dvb_frontend *fe, struct i2c_adapter* i2c_adap,
-                 u8 i2c_addr, struct device *dev, void *video_dev,
-                 int (*tuner_callback) (void *dev, int command,int arg))
+struct dvb_frontend *xc2028_attach(struct dvb_frontend *fe,
+                                  struct xc2028_config *cfg)
 {
        struct xc2028_data *priv;
+       void               *video_dev;
 
-       printk( KERN_INFO PREFIX "Xcv2028/3028 init called!\n");
-
-       if (NULL == dev)
-               return -ENODEV;
+       if (debug)
+               printk(KERN_DEBUG PREFIX ": Xcv2028/3028 init called!\n");
 
-       if (NULL == video_dev)
-               return -ENODEV;
+       if (NULL == cfg || NULL == cfg->video_dev)
+               return NULL;
 
-       if (!tuner_callback) {
-               printk( KERN_ERR PREFIX "No tuner callback!\n");
-               return -EINVAL;
+       if (!fe) {
+               printk(KERN_ERR PREFIX ": No frontend!\n");
+               return NULL;
        }
 
+       video_dev = cfg->video_dev;
+
+       mutex_lock(&xc2028_list_mutex);
+
        list_for_each_entry(priv, &xc2028_list, xc2028_list) {
-               if (priv->dev == dev) {
-                       dev = NULL;
+               if (priv->video_dev == cfg->video_dev) {
+                       video_dev = NULL;
+                       break;
                }
        }
 
-       if (dev) {
+       if (video_dev) {
                priv = kzalloc(sizeof(*priv), GFP_KERNEL);
-               if (priv == NULL)
-                       return -ENOMEM;
-
-               fe->tuner_priv = priv;
+               if (priv == NULL) {
+                       mutex_unlock(&xc2028_list_mutex);
+                       return NULL;
+               }
 
-               priv->bandwidth=BANDWIDTH_6_MHZ;
-               priv->need_load_generic=1;
-               priv->mode = T_UNINITIALIZED;
-               priv->i2c_props.addr = i2c_addr;
-               priv->i2c_props.adap = i2c_adap;
-               priv->dev = dev;
+               priv->i2c_props.addr = cfg->i2c_addr;
+               priv->i2c_props.adap = cfg->i2c_adap;
                priv->video_dev = video_dev;
-               priv->tuner_callback = tuner_callback;
-               priv->max_len = 13;
-
+               priv->tuner_callback = cfg->callback;
+               priv->ctrl.max_len = 13;
 
                mutex_init(&priv->lock);
 
-               list_add_tail(&priv->xc2028_list,&xc2028_list);
+               list_add_tail(&priv->xc2028_list, &xc2028_list);
        }
+
+       fe->tuner_priv = priv;
        priv->count++;
 
        memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
-                                              sizeof(xc2028_dvb_tuner_ops));
+              sizeof(xc2028_dvb_tuner_ops));
 
        tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
 
-       return 0;
+       if (cfg->ctrl)
+               xc2028_set_config(fe, cfg->ctrl);
+
+       mutex_unlock(&xc2028_list_mutex);
+
+       return fe;
 }
 
 EXPORT_SYMBOL(xc2028_attach);