OSDN Git Service

[media] af9015: switch to new DVB-USB
authorAntti Palosaari <crope@iki.fi>
Thu, 7 Jun 2012 23:36:35 +0000 (20:36 -0300)
committerMauro Carvalho Chehab <mchehab@redhat.com>
Sat, 4 Aug 2012 10:56:28 +0000 (07:56 -0300)
Almost all hacks are now removed and much less code.
Reducing af9015 driver code from 2084 to 1609, it is almost
500 LOC less!

Signed-off-by: Antti Palosaari <crope@iki.fi>
Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
drivers/media/dvb/dvb-usb/Kconfig
drivers/media/dvb/dvb-usb/af9015.c
drivers/media/dvb/dvb-usb/af9015.h

index a6ffdb3..2f6973a 100644 (file)
@@ -341,7 +341,7 @@ config DVB_USB_DTV5100
 
 config DVB_USB_AF9015
        tristate "Afatech AF9015 DVB-T USB2.0 support"
-       depends on DVB_USB
+       depends on DVB_USB_V2
        select DVB_AF9013
        select DVB_PLL              if !DVB_FE_CUSTOMISE
        select MEDIA_TUNER_MT2060   if !MEDIA_TUNER_CUSTOMISE
index 677fed7..fe5ef3a 100644 (file)
  *
  */
 
-#include <linux/hash.h>
-#include <linux/slab.h>
-
 #include "af9015.h"
-#include "af9013.h"
-#include "mt2060.h"
-#include "qt1010.h"
-#include "tda18271.h"
-#include "mxl5005s.h"
-#include "mc44s803.h"
-#include "tda18218.h"
-#include "mxl5007t.h"
 
 static int dvb_usb_af9015_debug;
 module_param_named(debug, dvb_usb_af9015_debug, int, 0644);
@@ -42,44 +31,18 @@ module_param_named(remote, dvb_usb_af9015_remote, int, 0644);
 MODULE_PARM_DESC(remote, "select remote");
 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
 
-static DEFINE_MUTEX(af9015_usb_mutex);
-
-static struct af9015_config af9015_config;
-static struct dvb_usb_device_properties af9015_properties[3];
-static int af9015_properties_count = ARRAY_SIZE(af9015_properties);
-
-static struct af9013_config af9015_af9013_config[] = {
-       {
-               .i2c_addr = AF9015_I2C_DEMOD,
-               .ts_mode = AF9013_TS_USB,
-               .api_version = { 0, 1, 9, 0 },
-               .gpio[0] = AF9013_GPIO_HI,
-               .gpio[3] = AF9013_GPIO_TUNER_ON,
-
-       }, {
-               .ts_mode = AF9013_TS_SERIAL,
-               .api_version = { 0, 1, 9, 0 },
-               .gpio[0] = AF9013_GPIO_TUNER_ON,
-               .gpio[1] = AF9013_GPIO_LO,
-       }
-};
-
-static int af9015_rw_udev(struct usb_device *udev, struct req_t *req)
+static int af9015_ctrl_msg(struct dvb_usb_device *d, struct req_t *req)
 {
 #define BUF_LEN 63
 #define REQ_HDR_LEN 8 /* send header size */
 #define ACK_HDR_LEN 2 /* rece header size */
-       int act_len, ret;
+       struct af9015_state *state = d->priv;
+       int ret, wlen, rlen;
        u8 buf[BUF_LEN];
        u8 write = 1;
-       u8 msg_len = REQ_HDR_LEN;
-       static u8 seq; /* packet sequence number */
-
-       if (mutex_lock_interruptible(&af9015_usb_mutex) < 0)
-               return -EAGAIN;
 
        buf[0] = req->cmd;
-       buf[1] = seq++;
+       buf[1] = state->seq++;
        buf[2] = req->i2c_addr;
        buf[3] = req->addr >> 8;
        buf[4] = req->addr & 0xff;
@@ -111,7 +74,7 @@ static int af9015_rw_udev(struct usb_device *udev, struct req_t *req)
        default:
                err("unknown command:%d", req->cmd);
                ret = -1;
-               goto error_unlock;
+               goto error;
        }
 
        /* buffer overflow check */
@@ -119,73 +82,42 @@ static int af9015_rw_udev(struct usb_device *udev, struct req_t *req)
                (!write && (req->data_len > BUF_LEN - ACK_HDR_LEN))) {
                err("too much data; cmd:%d len:%d", req->cmd, req->data_len);
                ret = -EINVAL;
-               goto error_unlock;
+               goto error;
        }
 
-       /* write requested */
+       /* write receives seq + status = 2 bytes
+          read receives seq + status + data = 2 + N bytes */
+       wlen = REQ_HDR_LEN;
+       rlen = ACK_HDR_LEN;
        if (write) {
+               wlen += req->data_len;
                memcpy(&buf[REQ_HDR_LEN], req->data, req->data_len);
-               msg_len += req->data_len;
+       } else {
+               rlen += req->data_len;
        }
 
-       deb_xfer(">>> ");
-       debug_dump(buf, msg_len, deb_xfer);
-
-       /* send req */
-       ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 0x02), buf, msg_len,
-               &act_len, AF9015_USB_TIMEOUT);
-       if (ret)
-               err("bulk message failed:%d (%d/%d)", ret, msg_len, act_len);
-       else
-               if (act_len != msg_len)
-                       ret = -1; /* all data is not send */
-       if (ret)
-               goto error_unlock;
-
-       /* no ack for those packets */
+       /* no ack for these packets */
        if (req->cmd == DOWNLOAD_FIRMWARE || req->cmd == RECONNECT_USB)
-               goto exit_unlock;
-
-       /* write receives seq + status = 2 bytes
-          read receives seq + status + data = 2 + N bytes */
-       msg_len = ACK_HDR_LEN;
-       if (!write)
-               msg_len += req->data_len;
-
-       ret = usb_bulk_msg(udev, usb_rcvbulkpipe(udev, 0x81), buf, msg_len,
-               &act_len, AF9015_USB_TIMEOUT);
-       if (ret) {
-               err("recv bulk message failed:%d", ret);
-               ret = -1;
-               goto error_unlock;
-       }
+               rlen = 0;
 
-       deb_xfer("<<< ");
-       debug_dump(buf, act_len, deb_xfer);
+       ret = dvb_usbv2_generic_rw(d, buf, wlen, buf, rlen, 0);
+       if (ret)
+               goto error;
 
        /* check status */
-       if (buf[1]) {
+       if (rlen && buf[1]) {
                err("command failed:%d", buf[1]);
                ret = -1;
-               goto error_unlock;
+               goto error;
        }
 
        /* read request, copy returned data to return buf */
        if (!write)
                memcpy(req->data, &buf[ACK_HDR_LEN], req->data_len);
-
-error_unlock:
-exit_unlock:
-       mutex_unlock(&af9015_usb_mutex);
-
+error:
        return ret;
 }
 
-static int af9015_ctrl_msg(struct dvb_usb_device *d, struct req_t *req)
-{
-       return af9015_rw_udev(d->udev, req);
-}
-
 static int af9015_write_regs(struct dvb_usb_device *d, u16 addr, u8 *val,
        u8 len)
 {
@@ -194,11 +126,6 @@ static int af9015_write_regs(struct dvb_usb_device *d, u16 addr, u8 *val,
        return af9015_ctrl_msg(d, &req);
 }
 
-static int af9015_write_reg(struct dvb_usb_device *d, u16 addr, u8 val)
-{
-       return af9015_write_regs(d, addr, &val, 1);
-}
-
 static int af9015_read_regs(struct dvb_usb_device *d, u16 addr, u8 *val, u8 len)
 {
        struct req_t req = {READ_MEMORY, AF9015_I2C_DEMOD, addr, 0, 0, len,
@@ -206,6 +133,11 @@ static int af9015_read_regs(struct dvb_usb_device *d, u16 addr, u8 *val, u8 len)
        return af9015_ctrl_msg(d, &req);
 }
 
+static int af9015_write_reg(struct dvb_usb_device *d, u16 addr, u8 val)
+{
+       return af9015_write_regs(d, addr, &val, 1);
+}
+
 static int af9015_read_reg(struct dvb_usb_device *d, u16 addr, u8 *val)
 {
        return af9015_read_regs(d, addr, val, 1);
@@ -214,10 +146,11 @@ static int af9015_read_reg(struct dvb_usb_device *d, u16 addr, u8 *val)
 static int af9015_write_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
        u8 val)
 {
+       struct af9015_state *state = d->priv;
        struct req_t req = {WRITE_I2C, addr, reg, 1, 1, 1, &val};
 
-       if (addr == af9015_af9013_config[0].i2c_addr ||
-           addr == af9015_af9013_config[1].i2c_addr)
+       if (addr == state->af9013_config[0].i2c_addr ||
+           addr == state->af9013_config[1].i2c_addr)
                req.addr_len = 3;
 
        return af9015_ctrl_msg(d, &req);
@@ -226,19 +159,53 @@ static int af9015_write_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
 static int af9015_read_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
        u8 *val)
 {
+       struct af9015_state *state = d->priv;
        struct req_t req = {READ_I2C, addr, reg, 0, 1, 1, val};
 
-       if (addr == af9015_af9013_config[0].i2c_addr ||
-           addr == af9015_af9013_config[1].i2c_addr)
+       if (addr == state->af9013_config[0].i2c_addr ||
+           addr == state->af9013_config[1].i2c_addr)
                req.addr_len = 3;
 
        return af9015_ctrl_msg(d, &req);
 }
 
+static int af9015_do_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit, u8 op)
+{
+       int ret;
+       u8 val, mask = 0x01;
+
+       ret = af9015_read_reg(d, addr, &val);
+       if (ret)
+               return ret;
+
+       mask <<= bit;
+       if (op) {
+               /* set bit */
+               val |= mask;
+       } else {
+               /* clear bit */
+               mask ^= 0xff;
+               val &= mask;
+       }
+
+       return af9015_write_reg(d, addr, val);
+}
+
+static int af9015_set_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit)
+{
+       return af9015_do_reg_bit(d, addr, bit, 1);
+}
+
+static int af9015_clear_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit)
+{
+       return af9015_do_reg_bit(d, addr, bit, 0);
+}
+
 static int af9015_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
        int num)
 {
        struct dvb_usb_device *d = i2c_get_adapdata(adap);
+       struct af9015_state *state = d->priv;
        int ret = 0, i = 0;
        u16 addr;
        u8 uninitialized_var(mbox), addr_len;
@@ -270,8 +237,8 @@ Due to that the only way to select correct tuner is use demodulator I2C-gate.
                return -EAGAIN;
 
        while (i < num) {
-               if (msg[i].addr == af9015_af9013_config[0].i2c_addr ||
-                   msg[i].addr == af9015_af9013_config[1].i2c_addr) {
+               if (msg[i].addr == state->af9013_config[0].i2c_addr ||
+                   msg[i].addr == state->af9013_config[1].i2c_addr) {
                        addr = msg[i].buf[0] << 8;
                        addr += msg[i].buf[1];
                        mbox = msg[i].buf[2];
@@ -287,7 +254,7 @@ Due to that the only way to select correct tuner is use demodulator I2C-gate.
                                ret = -EOPNOTSUPP;
                                goto error;
                        }
-                       if (msg[i].addr == af9015_af9013_config[0].i2c_addr)
+                       if (msg[i].addr == state->af9013_config[0].i2c_addr)
                                req.cmd = READ_MEMORY;
                        else
                                req.cmd = READ_I2C;
@@ -304,8 +271,7 @@ Due to that the only way to select correct tuner is use demodulator I2C-gate.
                                ret = -EOPNOTSUPP;
                                goto error;
                        }
-                       if (msg[i].addr ==
-                               af9015_af9013_config[0].i2c_addr) {
+                       if (msg[i].addr == state->af9013_config[0].i2c_addr) {
                                ret = -EINVAL;
                                goto error;
                        }
@@ -323,7 +289,7 @@ Due to that the only way to select correct tuner is use demodulator I2C-gate.
                                ret = -EOPNOTSUPP;
                                goto error;
                        }
-                       if (msg[i].addr == af9015_af9013_config[0].i2c_addr)
+                       if (msg[i].addr == state->af9013_config[0].i2c_addr)
                                req.cmd = WRITE_MEMORY;
                        else
                                req.cmd = WRITE_I2C;
@@ -358,595 +324,226 @@ static struct i2c_algorithm af9015_i2c_algo = {
        .functionality = af9015_i2c_func,
 };
 
-static int af9015_do_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit, u8 op)
+static int af9015_identify_state(struct dvb_usb_device *d)
 {
        int ret;
-       u8 val, mask = 0x01;
+       u8 reply;
+       struct req_t req = {GET_CONFIG, 0, 0, 0, 0, 1, &reply};
 
-       ret = af9015_read_reg(d, addr, &val);
+       ret = af9015_ctrl_msg(d, &req);
        if (ret)
                return ret;
 
-       mask <<= bit;
-       if (op) {
-               /* set bit */
-               val |= mask;
-       } else {
-               /* clear bit */
-               mask ^= 0xff;
-               val &= mask;
-       }
-
-       return af9015_write_reg(d, addr, val);
-}
+       deb_info("%s: reply:%02x\n", __func__, reply);
+       if (reply == 0x02)
+               ret = WARM;
+       else
+               ret = COLD;
 
-static int af9015_set_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit)
-{
-       return af9015_do_reg_bit(d, addr, bit, 1);
+       return ret;
 }
 
-static int af9015_clear_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit)
+static int af9015_download_firmware(struct dvb_usb_device *d,
+       const struct firmware *fw)
 {
-       return af9015_do_reg_bit(d, addr, bit, 0);
-}
+       struct af9015_state *state = d->priv;
+       int i, len, remaining, ret;
+       struct req_t req = {DOWNLOAD_FIRMWARE, 0, 0, 0, 0, 0, NULL};
+       u16 checksum = 0;
 
-static int af9015_init_endpoint(struct dvb_usb_device *d)
-{
-       int ret;
-       u16 frame_size;
-       u8  packet_size;
-       deb_info("%s: USB speed:%d\n", __func__, d->udev->speed);
+       deb_info("%s:\n", __func__);
 
-       /* Windows driver uses packet count 21 for USB1.1 and 348 for USB2.0.
-          We use smaller - about 1/4 from the original, 5 and 87. */
-#define TS_PACKET_SIZE            188
+       /* calc checksum */
+       for (i = 0; i < fw->size; i++)
+               checksum += fw->data[i];
 
-#define TS_USB20_PACKET_COUNT      87
-#define TS_USB20_FRAME_SIZE       (TS_PACKET_SIZE*TS_USB20_PACKET_COUNT)
+       state->firmware_size = fw->size;
+       state->firmware_checksum = checksum;
 
-#define TS_USB11_PACKET_COUNT       5
-#define TS_USB11_FRAME_SIZE       (TS_PACKET_SIZE*TS_USB11_PACKET_COUNT)
+       #define FW_ADDR 0x5100 /* firmware start address */
+       #define LEN_MAX 55 /* max packet size */
+       for (remaining = fw->size; remaining > 0; remaining -= LEN_MAX) {
+               len = remaining;
+               if (len > LEN_MAX)
+                       len = LEN_MAX;
 
-#define TS_USB20_MAX_PACKET_SIZE  512
-#define TS_USB11_MAX_PACKET_SIZE   64
+               req.data_len = len;
+               req.data = (u8 *) &fw->data[fw->size - remaining];
+               req.addr = FW_ADDR + fw->size - remaining;
 
-       if (d->udev->speed == USB_SPEED_FULL) {
-               frame_size = TS_USB11_FRAME_SIZE/4;
-               packet_size = TS_USB11_MAX_PACKET_SIZE/4;
-       } else {
-               frame_size = TS_USB20_FRAME_SIZE/4;
-               packet_size = TS_USB20_MAX_PACKET_SIZE/4;
+               ret = af9015_ctrl_msg(d, &req);
+               if (ret) {
+                       err("firmware download failed:%d", ret);
+                       goto error;
+               }
        }
 
-       ret = af9015_set_reg_bit(d, 0xd507, 2); /* assert EP4 reset */
-       if (ret)
-               goto error;
-       ret = af9015_set_reg_bit(d, 0xd50b, 1); /* assert EP5 reset */
-       if (ret)
-               goto error;
-       ret = af9015_clear_reg_bit(d, 0xdd11, 5); /* disable EP4 */
-       if (ret)
-               goto error;
-       ret = af9015_clear_reg_bit(d, 0xdd11, 6); /* disable EP5 */
-       if (ret)
-               goto error;
-       ret = af9015_set_reg_bit(d, 0xdd11, 5); /* enable EP4 */
-       if (ret)
+       /* firmware loaded, request boot */
+       req.cmd = BOOT;
+       req.data_len = 0;
+       ret = af9015_ctrl_msg(d, &req);
+       if (ret) {
+               err("firmware boot failed:%d", ret);
                goto error;
-       if (af9015_config.dual_mode) {
-               ret = af9015_set_reg_bit(d, 0xdd11, 6); /* enable EP5 */
-               if (ret)
-                       goto error;
        }
-       ret = af9015_clear_reg_bit(d, 0xdd13, 5); /* disable EP4 NAK */
-       if (ret)
-               goto error;
-       if (af9015_config.dual_mode) {
-               ret = af9015_clear_reg_bit(d, 0xdd13, 6); /* disable EP5 NAK */
+
+error:
+       return ret;
+}
+
+/* hash (and dump) eeprom */
+static int af9015_eeprom_hash(struct dvb_usb_device *d)
+{
+       struct af9015_state *state = d->priv;
+       int ret;
+       static const unsigned int eeprom_size = 256;
+       unsigned int reg;
+       u8 val, *eeprom;
+       struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, &val};
+
+       eeprom = kmalloc(eeprom_size, GFP_KERNEL);
+       if (eeprom == NULL)
+               return -ENOMEM;
+
+       for (reg = 0; reg < eeprom_size; reg++) {
+               req.addr = reg;
+               ret = af9015_ctrl_msg(d, &req);
                if (ret)
-                       goto error;
+                       goto free;
+
+               eeprom[reg] = val;
        }
-       /* EP4 xfer length */
-       ret = af9015_write_reg(d, 0xdd88, frame_size & 0xff);
-       if (ret)
-               goto error;
-       ret = af9015_write_reg(d, 0xdd89, frame_size >> 8);
-       if (ret)
-               goto error;
-       /* EP5 xfer length */
-       ret = af9015_write_reg(d, 0xdd8a, frame_size & 0xff);
-       if (ret)
-               goto error;
-       ret = af9015_write_reg(d, 0xdd8b, frame_size >> 8);
-       if (ret)
-               goto error;
-       ret = af9015_write_reg(d, 0xdd0c, packet_size); /* EP4 packet size */
-       if (ret)
-               goto error;
-       ret = af9015_write_reg(d, 0xdd0d, packet_size); /* EP5 packet size */
-       if (ret)
-               goto error;
-       ret = af9015_clear_reg_bit(d, 0xd507, 2); /* negate EP4 reset */
-       if (ret)
-               goto error;
-       if (af9015_config.dual_mode) {
-               ret = af9015_clear_reg_bit(d, 0xd50b, 1); /* negate EP5 reset */
-               if (ret)
-                       goto error;
+
+       if (dvb_usb_af9015_debug & 0x01)
+               print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, eeprom,
+                               eeprom_size);
+
+       BUG_ON(eeprom_size % 4);
+
+       state->eeprom_sum = 0;
+       for (reg = 0; reg < eeprom_size / sizeof(u32); reg++) {
+               state->eeprom_sum *= GOLDEN_RATIO_PRIME_32;
+               state->eeprom_sum += le32_to_cpu(((u32 *)eeprom)[reg]);
        }
 
-       /* enable / disable mp2if2 */
-       if (af9015_config.dual_mode)
-               ret = af9015_set_reg_bit(d, 0xd50b, 0);
-       else
-               ret = af9015_clear_reg_bit(d, 0xd50b, 0);
+       deb_info("%s: eeprom sum=%.8x\n", __func__, state->eeprom_sum);
 
-error:
-       if (ret)
-               err("endpoint init failed:%d", ret);
+       ret = 0;
+free:
+       kfree(eeprom);
        return ret;
 }
 
-static int af9015_copy_firmware(struct dvb_usb_device *d)
+static int af9015_read_config(struct dvb_usb_device *d)
 {
+       struct af9015_state *state = d->priv;
        int ret;
-       u8 fw_params[4];
-       u8 val, i;
-       struct req_t req = {COPY_FIRMWARE, 0, 0x5100, 0, 0, sizeof(fw_params),
-               fw_params };
-       deb_info("%s:\n", __func__);
+       u8 val, i, offset = 0;
+       struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, &val};
 
-       fw_params[0] = af9015_config.firmware_size >> 8;
-       fw_params[1] = af9015_config.firmware_size & 0xff;
-       fw_params[2] = af9015_config.firmware_checksum >> 8;
-       fw_params[3] = af9015_config.firmware_checksum & 0xff;
+       deb_info("%s:\n", __func__);
 
-       /* wait 2nd demodulator ready */
-       msleep(100);
+       /* IR remote controller */
+       req.addr = AF9015_EEPROM_IR_MODE;
+       /* first message will timeout often due to possible hw bug */
+       for (i = 0; i < 4; i++) {
+               ret = af9015_ctrl_msg(d, &req);
+               if (!ret)
+                       break;
+       }
+       if (ret)
+               goto error;
 
-       ret = af9015_read_reg_i2c(d,
-               af9015_af9013_config[1].i2c_addr, 0x98be, &val);
+       ret = af9015_eeprom_hash(d);
        if (ret)
                goto error;
-       else
-               deb_info("%s: firmware status:%02x\n", __func__, val);
 
-       if (val == 0x0c) /* fw is running, no need for download */
-               goto exit;
+       deb_info("%s: IR mode=%d\n", __func__, val);
+       state->ir_mode = val;
 
-       /* set I2C master clock to fast (to speed up firmware copy) */
-       ret = af9015_write_reg(d, 0xd416, 0x04); /* 0x04 * 400ns */
-       if (ret)
-               goto error;
-
-       msleep(50);
-
-       /* copy firmware */
+       /* TS mode - one or two receivers */
+       req.addr = AF9015_EEPROM_TS_MODE;
        ret = af9015_ctrl_msg(d, &req);
        if (ret)
-               err("firmware copy cmd failed:%d", ret);
-       deb_info("%s: firmware copy done\n", __func__);
-
-       /* set I2C master clock back to normal */
-       ret = af9015_write_reg(d, 0xd416, 0x14); /* 0x14 * 400ns */
-       if (ret)
-               goto error;
-
-       /* request boot firmware */
-       ret = af9015_write_reg_i2c(d, af9015_af9013_config[1].i2c_addr,
-               0xe205, 1);
-       deb_info("%s: firmware boot cmd status:%d\n", __func__, ret);
-       if (ret)
-               goto error;
-
-       for (i = 0; i < 15; i++) {
-               msleep(100);
-
-               /* check firmware status */
-               ret = af9015_read_reg_i2c(d,
-                       af9015_af9013_config[1].i2c_addr, 0x98be, &val);
-               deb_info("%s: firmware status cmd status:%d fw status:%02x\n",
-                       __func__, ret, val);
-               if (ret)
-                       goto error;
-
-               if (val == 0x0c || val == 0x04) /* success or fail */
-                       break;
-       }
-
-       if (val == 0x04) {
-               err("firmware did not run");
-               ret = -1;
-       } else if (val != 0x0c) {
-               err("firmware boot timeout");
-               ret = -1;
-       }
-
-error:
-exit:
-       return ret;
-}
-
-/* hash (and dump) eeprom */
-static int af9015_eeprom_hash(struct usb_device *udev)
-{
-       static const unsigned int eeprom_size = 256;
-       unsigned int reg;
-       int ret;
-       u8 val, *eeprom;
-       struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, &val};
-
-       eeprom = kmalloc(eeprom_size, GFP_KERNEL);
-       if (eeprom == NULL)
-               return -ENOMEM;
-
-       for (reg = 0; reg < eeprom_size; reg++) {
-               req.addr = reg;
-               ret = af9015_rw_udev(udev, &req);
-               if (ret)
-                       goto free;
-               eeprom[reg] = val;
-       }
-
-       if (dvb_usb_af9015_debug & 0x01)
-               print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, eeprom,
-                               eeprom_size);
-
-       BUG_ON(eeprom_size % 4);
-
-       af9015_config.eeprom_sum = 0;
-       for (reg = 0; reg < eeprom_size / sizeof(u32); reg++) {
-               af9015_config.eeprom_sum *= GOLDEN_RATIO_PRIME_32;
-               af9015_config.eeprom_sum += le32_to_cpu(((u32 *)eeprom)[reg]);
-       }
-
-       deb_info("%s: eeprom sum=%.8x\n", __func__, af9015_config.eeprom_sum);
-
-       ret = 0;
-free:
-       kfree(eeprom);
-       return ret;
-}
-
-static int af9015_init(struct dvb_usb_device *d)
-{
-       int ret;
-       deb_info("%s:\n", __func__);
-
-       /* init RC canary */
-       ret = af9015_write_reg(d, 0x98e9, 0xff);
-       if (ret)
-               goto error;
-
-       ret = af9015_init_endpoint(d);
-       if (ret)
-               goto error;
-
-error:
-       return ret;
-}
-
-static int af9015_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
-{
-       int ret;
-       deb_info("%s: onoff:%d\n", __func__, onoff);
-
-       if (onoff)
-               ret = af9015_set_reg_bit(adap->dev, 0xd503, 0);
-       else
-               ret = af9015_clear_reg_bit(adap->dev, 0xd503, 0);
-
-       return ret;
-}
-
-static int af9015_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
-       int onoff)
-{
-       int ret;
-       u8 idx;
-
-       deb_info("%s: set pid filter, index %d, pid %x, onoff %d\n",
-               __func__, index, pid, onoff);
-
-       ret = af9015_write_reg(adap->dev, 0xd505, (pid & 0xff));
-       if (ret)
-               goto error;
-
-       ret = af9015_write_reg(adap->dev, 0xd506, (pid >> 8));
-       if (ret)
-               goto error;
-
-       idx = ((index & 0x1f) | (1 << 5));
-       ret = af9015_write_reg(adap->dev, 0xd504, idx);
-
-error:
-       return ret;
-}
-
-static int af9015_download_firmware(struct usb_device *udev,
-       const struct firmware *fw)
-{
-       int i, len, remaining, ret;
-       struct req_t req = {DOWNLOAD_FIRMWARE, 0, 0, 0, 0, 0, NULL};
-       u16 checksum = 0;
-
-       deb_info("%s:\n", __func__);
-
-       /* calc checksum */
-       for (i = 0; i < fw->size; i++)
-               checksum += fw->data[i];
-
-       af9015_config.firmware_size = fw->size;
-       af9015_config.firmware_checksum = checksum;
-
-       #define FW_ADDR 0x5100 /* firmware start address */
-       #define LEN_MAX 55 /* max packet size */
-       for (remaining = fw->size; remaining > 0; remaining -= LEN_MAX) {
-               len = remaining;
-               if (len > LEN_MAX)
-                       len = LEN_MAX;
-
-               req.data_len = len;
-               req.data = (u8 *) &fw->data[fw->size - remaining];
-               req.addr = FW_ADDR + fw->size - remaining;
-
-               ret = af9015_rw_udev(udev, &req);
-               if (ret) {
-                       err("firmware download failed:%d", ret);
-                       goto error;
-               }
-       }
-
-       /* firmware loaded, request boot */
-       req.cmd = BOOT;
-       ret = af9015_rw_udev(udev, &req);
-       if (ret) {
-               err("firmware boot failed:%d", ret);
-               goto error;
-       }
-
-error:
-       return ret;
-}
-
-struct af9015_rc_setup {
-       unsigned int id;
-       char *rc_codes;
-};
-
-static char *af9015_rc_setup_match(unsigned int id,
-       const struct af9015_rc_setup *table)
-{
-       for (; table->rc_codes; table++)
-               if (table->id == id)
-                       return table->rc_codes;
-       return NULL;
-}
-
-static const struct af9015_rc_setup af9015_rc_setup_modparam[] = {
-       { AF9015_REMOTE_A_LINK_DTU_M, RC_MAP_ALINK_DTU_M },
-       { AF9015_REMOTE_MSI_DIGIVOX_MINI_II_V3, RC_MAP_MSI_DIGIVOX_II },
-       { AF9015_REMOTE_MYGICTV_U718, RC_MAP_TOTAL_MEDIA_IN_HAND },
-       { AF9015_REMOTE_DIGITTRADE_DVB_T, RC_MAP_DIGITTRADE },
-       { AF9015_REMOTE_AVERMEDIA_KS, RC_MAP_AVERMEDIA_RM_KS },
-       { }
-};
-
-static const struct af9015_rc_setup af9015_rc_setup_hashes[] = {
-       { 0xb8feb708, RC_MAP_MSI_DIGIVOX_II },
-       { 0xa3703d00, RC_MAP_ALINK_DTU_M },
-       { 0x9b7dc64e, RC_MAP_TOTAL_MEDIA_IN_HAND }, /* MYGICTV U718 */
-       { 0x5d49e3db, RC_MAP_DIGITTRADE }, /* LC-Power LC-USB-DVBT */
-       { }
-};
-
-static const struct af9015_rc_setup af9015_rc_setup_usbids[] = {
-       { (USB_VID_TERRATEC << 16) | USB_PID_TERRATEC_CINERGY_T_STICK_RC,
-               RC_MAP_TERRATEC_SLIM_2 },
-       { (USB_VID_TERRATEC << 16) | USB_PID_TERRATEC_CINERGY_T_STICK_DUAL_RC,
-               RC_MAP_TERRATEC_SLIM },
-       { (USB_VID_VISIONPLUS << 16) | USB_PID_AZUREWAVE_AD_TU700,
-               RC_MAP_AZUREWAVE_AD_TU700 },
-       { (USB_VID_VISIONPLUS << 16) | USB_PID_TINYTWIN,
-               RC_MAP_AZUREWAVE_AD_TU700 },
-       { (USB_VID_MSI_2 << 16) | USB_PID_MSI_DIGI_VOX_MINI_III,
-               RC_MAP_MSI_DIGIVOX_III },
-       { (USB_VID_MSI_2 << 16) | USB_PID_MSI_DIGIVOX_DUO,
-               RC_MAP_MSI_DIGIVOX_III },
-       { (USB_VID_LEADTEK << 16) | USB_PID_WINFAST_DTV_DONGLE_GOLD,
-               RC_MAP_LEADTEK_Y04G0051 },
-       { (USB_VID_LEADTEK << 16) | USB_PID_WINFAST_DTV2000DS,
-               RC_MAP_LEADTEK_Y04G0051 },
-       { (USB_VID_AVERMEDIA << 16) | USB_PID_AVERMEDIA_VOLAR_X,
-               RC_MAP_AVERMEDIA_M135A },
-       { (USB_VID_AFATECH << 16) | USB_PID_TREKSTOR_DVBT,
-               RC_MAP_TREKSTOR },
-       { (USB_VID_KWORLD_2 << 16) | USB_PID_TINYTWIN_2,
-               RC_MAP_DIGITALNOW_TINYTWIN },
-       { (USB_VID_GTEK << 16) | USB_PID_TINYTWIN_3,
-               RC_MAP_DIGITALNOW_TINYTWIN },
-       { (USB_VID_KWORLD_2 << 16) | USB_PID_SVEON_STV22,
-               RC_MAP_MSI_DIGIVOX_III },
-       { }
-};
-
-static void af9015_set_remote_config(struct usb_device *udev,
-               struct dvb_usb_device_properties *props)
-{
-       u16 vid = le16_to_cpu(udev->descriptor.idVendor);
-       u16 pid = le16_to_cpu(udev->descriptor.idProduct);
-
-       /* try to load remote based module param */
-       props->rc.core.rc_codes = af9015_rc_setup_match(
-               dvb_usb_af9015_remote, af9015_rc_setup_modparam);
-
-       /* try to load remote based eeprom hash */
-       if (!props->rc.core.rc_codes)
-               props->rc.core.rc_codes = af9015_rc_setup_match(
-                       af9015_config.eeprom_sum, af9015_rc_setup_hashes);
-
-       /* try to load remote based USB ID */
-       if (!props->rc.core.rc_codes)
-               props->rc.core.rc_codes = af9015_rc_setup_match(
-                       (vid << 16) | pid, af9015_rc_setup_usbids);
-
-       /* try to load remote based USB iManufacturer string */
-       if (!props->rc.core.rc_codes && vid == USB_VID_AFATECH) {
-               /* Check USB manufacturer and product strings and try
-                  to determine correct remote in case of chip vendor
-                  reference IDs are used.
-                  DO NOT ADD ANYTHING NEW HERE. Use hashes instead. */
-               char manufacturer[10];
-               memset(manufacturer, 0, sizeof(manufacturer));
-               usb_string(udev, udev->descriptor.iManufacturer,
-                       manufacturer, sizeof(manufacturer));
-               if (!strcmp("MSI", manufacturer)) {
-                       /* iManufacturer 1 MSI
-                          iProduct      2 MSI K-VOX */
-                       props->rc.core.rc_codes = af9015_rc_setup_match(
-                               AF9015_REMOTE_MSI_DIGIVOX_MINI_II_V3,
-                               af9015_rc_setup_modparam);
-               }
-       }
-
-       /* finally load "empty" just for leaving IR receiver enabled */
-       if (!props->rc.core.rc_codes)
-               props->rc.core.rc_codes = RC_MAP_EMPTY;
-
-       return;
-}
-
-static int af9015_read_config(struct usb_device *udev)
-{
-       int ret;
-       u8 val, i, offset = 0;
-       struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, &val};
-
-       /* IR remote controller */
-       req.addr = AF9015_EEPROM_IR_MODE;
-       /* first message will timeout often due to possible hw bug */
-       for (i = 0; i < 4; i++) {
-               ret = af9015_rw_udev(udev, &req);
-               if (!ret)
-                       break;
-       }
-       if (ret)
                goto error;
 
-       ret = af9015_eeprom_hash(udev);
-       if (ret)
-               goto error;
+       state->dual_mode = val;
+       deb_info("%s: TS mode=%d\n", __func__, state->dual_mode);
 
-       deb_info("%s: IR mode=%d\n", __func__, val);
-       for (i = 0; i < af9015_properties_count; i++) {
-               if (val == AF9015_IR_MODE_DISABLED)
-                       af9015_properties[i].rc.core.rc_codes = NULL;
-               else
-                       af9015_set_remote_config(udev, &af9015_properties[i]);
-       }
+       /* disable 2nd adapter because we don't have PID-filters */
+       if (d->udev->speed == USB_SPEED_FULL)
+               state->dual_mode = 0;
 
-       /* TS mode - one or two receivers */
-       req.addr = AF9015_EEPROM_TS_MODE;
-       ret = af9015_rw_udev(udev, &req);
-       if (ret)
-               goto error;
-       af9015_config.dual_mode = val;
-       deb_info("%s: TS mode=%d\n", __func__, af9015_config.dual_mode);
-
-       /* Set adapter0 buffer size according to USB port speed, adapter1 buffer
-          size can be static because it is enabled only USB2.0 */
-       for (i = 0; i < af9015_properties_count; i++) {
-               /* USB1.1 set smaller buffersize and disable 2nd adapter */
-               if (udev->speed == USB_SPEED_FULL) {
-                       af9015_properties[i].adapter[0].fe[0].stream.u.bulk.buffersize
-                               = TS_USB11_FRAME_SIZE;
-                       /* disable 2nd adapter because we don't have
-                          PID-filters */
-                       af9015_config.dual_mode = 0;
-               } else {
-                       af9015_properties[i].adapter[0].fe[0].stream.u.bulk.buffersize
-                               = TS_USB20_FRAME_SIZE;
-               }
-       }
-
-       if (af9015_config.dual_mode) {
+       if (state->dual_mode) {
                /* read 2nd demodulator I2C address */
                req.addr = AF9015_EEPROM_DEMOD2_I2C;
-               ret = af9015_rw_udev(udev, &req);
+               ret = af9015_ctrl_msg(d, &req);
                if (ret)
                        goto error;
-               af9015_af9013_config[1].i2c_addr = val;
-
-               /* enable 2nd adapter */
-               for (i = 0; i < af9015_properties_count; i++)
-                       af9015_properties[i].num_adapters = 2;
 
-       } else {
-                /* disable 2nd adapter */
-               for (i = 0; i < af9015_properties_count; i++)
-                       af9015_properties[i].num_adapters = 1;
+               state->af9013_config[1].i2c_addr = val;
        }
 
-       for (i = 0; i < af9015_properties[0].num_adapters; i++) {
+       for (i = 0; i < state->dual_mode + 1; i++) {
                if (i == 1)
                        offset = AF9015_EEPROM_OFFSET;
                /* xtal */
                req.addr = AF9015_EEPROM_XTAL_TYPE1 + offset;
-               ret = af9015_rw_udev(udev, &req);
+               ret = af9015_ctrl_msg(d, &req);
                if (ret)
                        goto error;
                switch (val) {
                case 0:
-                       af9015_af9013_config[i].clock = 28800000;
+                       state->af9013_config[i].clock = 28800000;
                        break;
                case 1:
-                       af9015_af9013_config[i].clock = 20480000;
+                       state->af9013_config[i].clock = 20480000;
                        break;
                case 2:
-                       af9015_af9013_config[i].clock = 28000000;
+                       state->af9013_config[i].clock = 28000000;
                        break;
                case 3:
-                       af9015_af9013_config[i].clock = 25000000;
+                       state->af9013_config[i].clock = 25000000;
                        break;
                };
                deb_info("%s: [%d] xtal=%d set clock=%d\n", __func__, i,
-                       val, af9015_af9013_config[i].clock);
+                               val, state->af9013_config[i].clock);
 
                /* IF frequency */
                req.addr = AF9015_EEPROM_IF1H + offset;
-               ret = af9015_rw_udev(udev, &req);
+               ret = af9015_ctrl_msg(d, &req);
                if (ret)
                        goto error;
 
-               af9015_af9013_config[i].if_frequency = val << 8;
+               state->af9013_config[i].if_frequency = val << 8;
 
                req.addr = AF9015_EEPROM_IF1L + offset;
-               ret = af9015_rw_udev(udev, &req);
+               ret = af9015_ctrl_msg(d, &req);
                if (ret)
                        goto error;
 
-               af9015_af9013_config[i].if_frequency += val;
-               af9015_af9013_config[i].if_frequency *= 1000;
+               state->af9013_config[i].if_frequency += val;
+               state->af9013_config[i].if_frequency *= 1000;
                deb_info("%s: [%d] IF frequency=%d\n", __func__, i,
-                       af9015_af9013_config[0].if_frequency);
+                               state->af9013_config[i].if_frequency);
 
                /* MT2060 IF1 */
                req.addr = AF9015_EEPROM_MT2060_IF1H  + offset;
-               ret = af9015_rw_udev(udev, &req);
+               ret = af9015_ctrl_msg(d, &req);
                if (ret)
                        goto error;
-               af9015_config.mt2060_if1[i] = val << 8;
+               state->mt2060_if1[i] = val << 8;
                req.addr = AF9015_EEPROM_MT2060_IF1L + offset;
-               ret = af9015_rw_udev(udev, &req);
+               ret = af9015_ctrl_msg(d, &req);
                if (ret)
                        goto error;
-               af9015_config.mt2060_if1[i] += val;
+               state->mt2060_if1[i] += val;
                deb_info("%s: [%d] MT2060 IF1=%d\n", __func__, i,
-                       af9015_config.mt2060_if1[i]);
+                               state->mt2060_if1[i]);
 
                /* tuner */
                req.addr =  AF9015_EEPROM_TUNER_ID1 + offset;
-               ret = af9015_rw_udev(udev, &req);
+               ret = af9015_ctrl_msg(d, &req);
                if (ret)
                        goto error;
                switch (val) {
@@ -958,24 +555,24 @@ static int af9015_read_config(struct usb_device *udev)
                case AF9013_TUNER_TDA18271:
                case AF9013_TUNER_QT1010A:
                case AF9013_TUNER_TDA18218:
-                       af9015_af9013_config[i].spec_inv = 1;
+                       state->af9013_config[i].spec_inv = 1;
                        break;
                case AF9013_TUNER_MXL5003D:
                case AF9013_TUNER_MXL5005D:
                case AF9013_TUNER_MXL5005R:
                case AF9013_TUNER_MXL5007T:
-                       af9015_af9013_config[i].spec_inv = 0;
+                       state->af9013_config[i].spec_inv = 0;
                        break;
                case AF9013_TUNER_MC44S803:
-                       af9015_af9013_config[i].gpio[1] = AF9013_GPIO_LO;
-                       af9015_af9013_config[i].spec_inv = 1;
+                       state->af9013_config[i].gpio[1] = AF9013_GPIO_LO;
+                       state->af9013_config[i].spec_inv = 1;
                        break;
                default:
                        warn("tuner id=%d not supported, please report!", val);
                        return -ENODEV;
                };
 
-               af9015_af9013_config[i].tuner = val;
+               state->af9013_config[i].tuner = val;
                deb_info("%s: [%d] tuner id=%d\n", __func__, i, val);
        }
 
@@ -986,112 +583,52 @@ error:
        /* AverMedia AVerTV Volar Black HD (A850) device have bad EEPROM
           content :-( Override some wrong values here. Ditto for the
           AVerTV Red HD+ (A850T) device. */
-       if (le16_to_cpu(udev->descriptor.idVendor) == USB_VID_AVERMEDIA &&
-               ((le16_to_cpu(udev->descriptor.idProduct) ==
+       if (le16_to_cpu(d->udev->descriptor.idVendor) == USB_VID_AVERMEDIA &&
+               ((le16_to_cpu(d->udev->descriptor.idProduct) ==
                        USB_PID_AVERMEDIA_A850) ||
-               (le16_to_cpu(udev->descriptor.idProduct) ==
+               (le16_to_cpu(d->udev->descriptor.idProduct) ==
                        USB_PID_AVERMEDIA_A850T))) {
                deb_info("%s: AverMedia A850: overriding config\n", __func__);
                /* disable dual mode */
-               af9015_config.dual_mode = 0;
-                /* disable 2nd adapter */
-               for (i = 0; i < af9015_properties_count; i++)
-                       af9015_properties[i].num_adapters = 1;
+               state->dual_mode = 0;
 
                /* set correct IF */
-               af9015_af9013_config[0].if_frequency = 4570000;
+               state->af9013_config[0].if_frequency = 4570000;
        }
 
        return ret;
 }
 
-static int af9015_identify_state(struct usb_device *udev,
-                                struct dvb_usb_device_properties *props,
-                                struct dvb_usb_device_description **desc,
-                                int *cold)
+static int af9015_get_usb_stream_config(struct dvb_frontend *fe,
+               struct usb_data_stream_properties *stream)
 {
-       int ret;
-       u8 reply;
-       struct req_t req = {GET_CONFIG, 0, 0, 0, 0, 1, &reply};
-
-       ret = af9015_rw_udev(udev, &req);
-       if (ret)
-               return ret;
-
-       deb_info("%s: reply:%02x\n", __func__, reply);
-       if (reply == 0x02)
-               *cold = 0;
-       else
-               *cold = 1;
-
-       return ret;
-}
+       struct dvb_usb_adapter *adap;
 
-static int af9015_rc_query(struct dvb_usb_device *d)
-{
-       struct af9015_state *priv = d->priv;
-       int ret;
-       u8 buf[17];
+       deb_info("%s: fe=%p\n", __func__, fe);
 
-       /* read registers needed to detect remote controller code */
-       ret = af9015_read_regs(d, 0x98d9, buf, sizeof(buf));
-       if (ret)
-               goto error;
+       stream->type = USB_BULK;
+       stream->count = 8;
+       stream->endpoint = 0x84;
+       stream->u.bulk.buffersize = TS_USB20_FRAME_SIZE;
 
-       /* If any of these are non-zero, assume invalid data */
-       if (buf[1] || buf[2] || buf[3])
-               return ret;
+       if (fe == NULL)
+               return 0;
 
-       /* Check for repeat of previous code */
-       if ((priv->rc_repeat != buf[6] || buf[0]) &&
-                                       !memcmp(&buf[12], priv->rc_last, 4)) {
-               deb_rc("%s: key repeated\n", __func__);
-               rc_keydown(d->rc_dev, priv->rc_keycode, 0);
-               priv->rc_repeat = buf[6];
-               return ret;
-       }
+        adap = fe->dvb->priv;
 
-       /* Only process key if canary killed */
-       if (buf[16] != 0xff && buf[0] != 0x01) {
-               deb_rc("%s: key pressed %02x %02x %02x %02x\n", __func__,
-                       buf[12], buf[13], buf[14], buf[15]);
+       if (adap->id == 1)
+               stream->endpoint = 0x85;
 
-               /* Reset the canary */
-               ret = af9015_write_reg(d, 0x98e9, 0xff);
-               if (ret)
-                       goto error;
+       if (adap->dev->udev->speed == USB_SPEED_FULL)
+               stream->u.bulk.buffersize = TS_USB11_FRAME_SIZE;
 
-               /* Remember this key */
-               memcpy(priv->rc_last, &buf[12], 4);
-               if (buf[14] == (u8) ~buf[15]) {
-                       if (buf[12] == (u8) ~buf[13]) {
-                               /* NEC */
-                               priv->rc_keycode = buf[12] << 8 | buf[14];
-                       } else {
-                               /* NEC extended*/
-                               priv->rc_keycode = buf[12] << 16 |
-                                       buf[13] << 8 | buf[14];
-                       }
-               } else {
-                       /* 32 bit NEC */
-                       priv->rc_keycode = buf[12] << 24 | buf[13] << 16 |
-                                       buf[14] << 8 | buf[15];
-               }
-               rc_keydown(d->rc_dev, priv->rc_keycode, 0);
-       } else {
-               deb_rc("%s: no key press\n", __func__);
-               /* Invalidate last keypress */
-               /* Not really needed, but helps with debug */
-               priv->rc_last[2] = priv->rc_last[3];
-       }
-
-       priv->rc_repeat = buf[6];
-
-error:
-       if (ret)
-               err("%s: failed:%d", __func__, ret);
+       return 0;
+}
 
-       return ret;
+static int af9015_get_adapter_count(struct dvb_usb_device *d)
+{
+       struct af9015_state *state = d->priv;
+       return state->dual_mode + 1;
 }
 
 /* override demod callbacks for resource locking */
@@ -1099,14 +636,14 @@ static int af9015_af9013_set_frontend(struct dvb_frontend *fe)
 {
        int ret;
        struct dvb_usb_adapter *adap = fe->dvb->priv;
-       struct af9015_state *priv = adap->dev->priv;
+       struct af9015_state *state = adap->dev->priv;
 
-       if (mutex_lock_interruptible(&adap->dev->usb_mutex))
+       if (mutex_lock_interruptible(&state->fe_mutex))
                return -EAGAIN;
 
-       ret = priv->set_frontend[adap->id](fe);
+       ret = state->set_frontend[adap->id](fe);
 
-       mutex_unlock(&adap->dev->usb_mutex);
+       mutex_unlock(&state->fe_mutex);
 
        return ret;
 }
@@ -1117,14 +654,14 @@ static int af9015_af9013_read_status(struct dvb_frontend *fe,
 {
        int ret;
        struct dvb_usb_adapter *adap = fe->dvb->priv;
-       struct af9015_state *priv = adap->dev->priv;
+       struct af9015_state *state = adap->dev->priv;
 
-       if (mutex_lock_interruptible(&adap->dev->usb_mutex))
+       if (mutex_lock_interruptible(&state->fe_mutex))
                return -EAGAIN;
 
-       ret = priv->read_status[adap->id](fe, status);
+       ret = state->read_status[adap->id](fe, status);
 
-       mutex_unlock(&adap->dev->usb_mutex);
+       mutex_unlock(&state->fe_mutex);
 
        return ret;
 }
@@ -1134,14 +671,14 @@ static int af9015_af9013_init(struct dvb_frontend *fe)
 {
        int ret;
        struct dvb_usb_adapter *adap = fe->dvb->priv;
-       struct af9015_state *priv = adap->dev->priv;
+       struct af9015_state *state = adap->dev->priv;
 
-       if (mutex_lock_interruptible(&adap->dev->usb_mutex))
+       if (mutex_lock_interruptible(&state->fe_mutex))
                return -EAGAIN;
 
-       ret = priv->init[adap->id](fe);
+       ret = state->init[adap->id](fe);
 
-       mutex_unlock(&adap->dev->usb_mutex);
+       mutex_unlock(&state->fe_mutex);
 
        return ret;
 }
@@ -1151,14 +688,14 @@ static int af9015_af9013_sleep(struct dvb_frontend *fe)
 {
        int ret;
        struct dvb_usb_adapter *adap = fe->dvb->priv;
-       struct af9015_state *priv = adap->dev->priv;
+       struct af9015_state *state = adap->dev->priv;
 
-       if (mutex_lock_interruptible(&adap->dev->usb_mutex))
+       if (mutex_lock_interruptible(&state->fe_mutex))
                return -EAGAIN;
 
-       ret = priv->sleep[adap->id](fe);
+       ret = state->sleep[adap->id](fe);
 
-       mutex_unlock(&adap->dev->usb_mutex);
+       mutex_unlock(&state->fe_mutex);
 
        return ret;
 }
@@ -1168,14 +705,14 @@ static int af9015_tuner_init(struct dvb_frontend *fe)
 {
        int ret;
        struct dvb_usb_adapter *adap = fe->dvb->priv;
-       struct af9015_state *priv = adap->dev->priv;
+       struct af9015_state *state = adap->dev->priv;
 
-       if (mutex_lock_interruptible(&adap->dev->usb_mutex))
+       if (mutex_lock_interruptible(&state->fe_mutex))
                return -EAGAIN;
 
-       ret = priv->tuner_init[adap->id](fe);
+       ret = state->tuner_init[adap->id](fe);
 
-       mutex_unlock(&adap->dev->usb_mutex);
+       mutex_unlock(&state->fe_mutex);
 
        return ret;
 }
@@ -1185,32 +722,122 @@ static int af9015_tuner_sleep(struct dvb_frontend *fe)
 {
        int ret;
        struct dvb_usb_adapter *adap = fe->dvb->priv;
-       struct af9015_state *priv = adap->dev->priv;
+       struct af9015_state *state = adap->dev->priv;
 
-       if (mutex_lock_interruptible(&adap->dev->usb_mutex))
+       if (mutex_lock_interruptible(&state->fe_mutex))
                return -EAGAIN;
 
-       ret = priv->tuner_sleep[adap->id](fe);
+       ret = state->tuner_sleep[adap->id](fe);
+
+       mutex_unlock(&state->fe_mutex);
+
+       return ret;
+}
+
+static int af9015_copy_firmware(struct dvb_usb_device *d)
+{
+       struct af9015_state *state = d->priv;
+       int ret;
+       u8 fw_params[4];
+       u8 val, i;
+       struct req_t req = {COPY_FIRMWARE, 0, 0x5100, 0, 0, sizeof(fw_params),
+               fw_params };
+       deb_info("%s:\n", __func__);
+
+       fw_params[0] = state->firmware_size >> 8;
+       fw_params[1] = state->firmware_size & 0xff;
+       fw_params[2] = state->firmware_checksum >> 8;
+       fw_params[3] = state->firmware_checksum & 0xff;
+
+       /* wait 2nd demodulator ready */
+       msleep(100);
+
+       ret = af9015_read_reg_i2c(d, state->af9013_config[1].i2c_addr,
+                       0x98be, &val);
+       if (ret)
+               goto error;
+       else
+               deb_info("%s: firmware status:%02x\n", __func__, val);
+
+       if (val == 0x0c) /* fw is running, no need for download */
+               goto exit;
+
+       /* set I2C master clock to fast (to speed up firmware copy) */
+       ret = af9015_write_reg(d, 0xd416, 0x04); /* 0x04 * 400ns */
+       if (ret)
+               goto error;
+
+       msleep(50);
+
+       /* copy firmware */
+       ret = af9015_ctrl_msg(d, &req);
+       if (ret)
+               err("firmware copy cmd failed:%d", ret);
+       deb_info("%s: firmware copy done\n", __func__);
+
+       /* set I2C master clock back to normal */
+       ret = af9015_write_reg(d, 0xd416, 0x14); /* 0x14 * 400ns */
+       if (ret)
+               goto error;
+
+       /* request boot firmware */
+       ret = af9015_write_reg_i2c(d, state->af9013_config[1].i2c_addr,
+                       0xe205, 1);
+       deb_info("%s: firmware boot cmd status:%d\n", __func__, ret);
+       if (ret)
+               goto error;
+
+       for (i = 0; i < 15; i++) {
+               msleep(100);
 
-       mutex_unlock(&adap->dev->usb_mutex);
+               /* check firmware status */
+               ret = af9015_read_reg_i2c(d, state->af9013_config[1].i2c_addr,
+                               0x98be, &val);
+               deb_info("%s: firmware status cmd status:%d fw status:%02x\n",
+                       __func__, ret, val);
+               if (ret)
+                       goto error;
 
+               if (val == 0x0c || val == 0x04) /* success or fail */
+                       break;
+       }
+
+       if (val == 0x04) {
+               err("firmware did not run");
+               ret = -1;
+       } else if (val != 0x0c) {
+               err("firmware boot timeout");
+               ret = -1;
+       }
+
+error:
+exit:
        return ret;
 }
 
-
 static int af9015_af9013_frontend_attach(struct dvb_usb_adapter *adap)
 {
        int ret;
        struct af9015_state *state = adap->dev->priv;
 
-       if (adap->id == 1) {
+       if (adap->id == 0) {
+               state->af9013_config[0].ts_mode = AF9013_TS_USB;
+               memcpy(state->af9013_config[0].api_version, "\x0\x1\x9\x0", 4);
+               state->af9013_config[0].gpio[0] = AF9013_GPIO_HI;
+               state->af9013_config[0].gpio[3] = AF9013_GPIO_TUNER_ON;
+       } else if (adap->id == 1) {
+               state->af9013_config[1].ts_mode = AF9013_TS_SERIAL;
+               memcpy(state->af9013_config[1].api_version, "\x0\x1\x9\x0", 4);
+               state->af9013_config[1].gpio[0] = AF9013_GPIO_TUNER_ON;
+               state->af9013_config[1].gpio[1] = AF9013_GPIO_LO;
+
                /* copy firmware to 2nd demodulator */
-               if (af9015_config.dual_mode) {
+               if (state->dual_mode) {
                        ret = af9015_copy_firmware(adap->dev);
                        if (ret) {
                                err("firmware copy to 2nd frontend " \
                                        "failed, will disable it");
-                               af9015_config.dual_mode = 0;
+                               state->dual_mode = 0;
                                return -ENODEV;
                        }
                } else {
@@ -1219,8 +846,8 @@ static int af9015_af9013_frontend_attach(struct dvb_usb_adapter *adap)
        }
 
        /* attach demodulator */
-       adap->fe_adap[0].fe = dvb_attach(af9013_attach,
-               &af9015_af9013_config[adap->id], &adap->dev->i2c_adap);
+       adap->fe[0] = dvb_attach(af9013_attach,
+               &state->af9013_config[adap->id], &adap->dev->i2c_adap);
 
        /*
         * AF9015 firmware does not like if it gets interrupted by I2C adapter
@@ -1228,27 +855,26 @@ static int af9015_af9013_frontend_attach(struct dvb_usb_adapter *adap)
         * is used only 2nd demodulator and tuner on dual tuner devices.
         * Override demodulator callbacks and use mutex for limit access to
         * those "critical" paths to keep AF9015 happy.
-        * Note: we abuse unused usb_mutex here.
         */
-       if (adap->fe_adap[0].fe) {
+       if (adap->fe[0]) {
                state->set_frontend[adap->id] =
-                       adap->fe_adap[0].fe->ops.set_frontend;
-               adap->fe_adap[0].fe->ops.set_frontend =
+                       adap->fe[0]->ops.set_frontend;
+               adap->fe[0]->ops.set_frontend =
                        af9015_af9013_set_frontend;
 
                state->read_status[adap->id] =
-                       adap->fe_adap[0].fe->ops.read_status;
-               adap->fe_adap[0].fe->ops.read_status =
+                       adap->fe[0]->ops.read_status;
+               adap->fe[0]->ops.read_status =
                        af9015_af9013_read_status;
 
-               state->init[adap->id] = adap->fe_adap[0].fe->ops.init;
-               adap->fe_adap[0].fe->ops.init = af9015_af9013_init;
+               state->init[adap->id] = adap->fe[0]->ops.init;
+               adap->fe[0]->ops.init = af9015_af9013_init;
 
-               state->sleep[adap->id] = adap->fe_adap[0].fe->ops.sleep;
-               adap->fe_adap[0].fe->ops.sleep = af9015_af9013_sleep;
+               state->sleep[adap->id] = adap->fe[0]->ops.sleep;
+               adap->fe[0]->ops.sleep = af9015_af9013_sleep;
        }
 
-       return adap->fe_adap[0].fe == NULL ? -ENODEV : 0;
+       return adap->fe[0] == NULL ? -ENODEV : 0;
 }
 
 static struct mt2060_config af9015_mt2060_config = {
@@ -1316,57 +942,57 @@ static struct mxl5007t_config af9015_mxl5007t_config = {
 
 static int af9015_tuner_attach(struct dvb_usb_adapter *adap)
 {
-       int ret;
        struct af9015_state *state = adap->dev->priv;
+       int ret;
        deb_info("%s:\n", __func__);
 
-       switch (af9015_af9013_config[adap->id].tuner) {
+       switch (state->af9013_config[adap->id].tuner) {
        case AF9013_TUNER_MT2060:
        case AF9013_TUNER_MT2060_2:
-               ret = dvb_attach(mt2060_attach, adap->fe_adap[0].fe,
+               ret = dvb_attach(mt2060_attach, adap->fe[0],
                        &adap->dev->i2c_adap, &af9015_mt2060_config,
-                       af9015_config.mt2060_if1[adap->id])
+                       state->mt2060_if1[adap->id])
                        == NULL ? -ENODEV : 0;
                break;
        case AF9013_TUNER_QT1010:
        case AF9013_TUNER_QT1010A:
-               ret = dvb_attach(qt1010_attach, adap->fe_adap[0].fe,
+               ret = dvb_attach(qt1010_attach, adap->fe[0],
                        &adap->dev->i2c_adap,
                        &af9015_qt1010_config) == NULL ? -ENODEV : 0;
                break;
        case AF9013_TUNER_TDA18271:
-               ret = dvb_attach(tda18271_attach, adap->fe_adap[0].fe, 0xc0,
+               ret = dvb_attach(tda18271_attach, adap->fe[0], 0xc0,
                        &adap->dev->i2c_adap,
                        &af9015_tda18271_config) == NULL ? -ENODEV : 0;
                break;
        case AF9013_TUNER_TDA18218:
-               ret = dvb_attach(tda18218_attach, adap->fe_adap[0].fe,
+               ret = dvb_attach(tda18218_attach, adap->fe[0],
                        &adap->dev->i2c_adap,
                        &af9015_tda18218_config) == NULL ? -ENODEV : 0;
                break;
        case AF9013_TUNER_MXL5003D:
-               ret = dvb_attach(mxl5005s_attach, adap->fe_adap[0].fe,
+               ret = dvb_attach(mxl5005s_attach, adap->fe[0],
                        &adap->dev->i2c_adap,
                        &af9015_mxl5003_config) == NULL ? -ENODEV : 0;
                break;
        case AF9013_TUNER_MXL5005D:
        case AF9013_TUNER_MXL5005R:
-               ret = dvb_attach(mxl5005s_attach, adap->fe_adap[0].fe,
+               ret = dvb_attach(mxl5005s_attach, adap->fe[0],
                        &adap->dev->i2c_adap,
                        &af9015_mxl5005_config) == NULL ? -ENODEV : 0;
                break;
        case AF9013_TUNER_ENV77H11D5:
-               ret = dvb_attach(dvb_pll_attach, adap->fe_adap[0].fe, 0xc0,
+               ret = dvb_attach(dvb_pll_attach, adap->fe[0], 0xc0,
                        &adap->dev->i2c_adap,
                        DVB_PLL_TDA665X) == NULL ? -ENODEV : 0;
                break;
        case AF9013_TUNER_MC44S803:
-               ret = dvb_attach(mc44s803_attach, adap->fe_adap[0].fe,
+               ret = dvb_attach(mc44s803_attach, adap->fe[0],
                        &adap->dev->i2c_adap,
                        &af9015_mc44s803_config) == NULL ? -ENODEV : 0;
                break;
        case AF9013_TUNER_MXL5007T:
-               ret = dvb_attach(mxl5007t_attach, adap->fe_adap[0].fe,
+               ret = dvb_attach(mxl5007t_attach, adap->fe[0],
                        &adap->dev->i2c_adap,
                        0xc0, &af9015_mxl5007t_config) == NULL ? -ENODEV : 0;
                break;
@@ -1374,575 +1000,437 @@ static int af9015_tuner_attach(struct dvb_usb_adapter *adap)
        default:
                ret = -ENODEV;
                err("Unknown tuner id:%d",
-                       af9015_af9013_config[adap->id].tuner);
+                       state->af9013_config[adap->id].tuner);
        }
 
-       if (adap->fe_adap[0].fe->ops.tuner_ops.init) {
+       if (adap->fe[0]->ops.tuner_ops.init) {
                state->tuner_init[adap->id] =
-                       adap->fe_adap[0].fe->ops.tuner_ops.init;
-               adap->fe_adap[0].fe->ops.tuner_ops.init = af9015_tuner_init;
+                       adap->fe[0]->ops.tuner_ops.init;
+               adap->fe[0]->ops.tuner_ops.init = af9015_tuner_init;
        }
 
-       if (adap->fe_adap[0].fe->ops.tuner_ops.sleep) {
+       if (adap->fe[0]->ops.tuner_ops.sleep) {
                state->tuner_sleep[adap->id] =
-                       adap->fe_adap[0].fe->ops.tuner_ops.sleep;
-               adap->fe_adap[0].fe->ops.tuner_ops.sleep = af9015_tuner_sleep;
+                       adap->fe[0]->ops.tuner_ops.sleep;
+               adap->fe[0]->ops.tuner_ops.sleep = af9015_tuner_sleep;
+       }
+
+       return ret;
+}
+
+static int af9015_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
+{
+       int ret;
+       deb_info("%s: onoff:%d\n", __func__, onoff);
+
+       if (onoff)
+               ret = af9015_set_reg_bit(adap->dev, 0xd503, 0);
+       else
+               ret = af9015_clear_reg_bit(adap->dev, 0xd503, 0);
+
+       return ret;
+}
+
+static int af9015_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
+       int onoff)
+{
+       int ret;
+       u8 idx;
+
+       deb_info("%s: set pid filter, index %d, pid %x, onoff %d\n",
+               __func__, index, pid, onoff);
+
+       ret = af9015_write_reg(adap->dev, 0xd505, (pid & 0xff));
+       if (ret)
+               goto error;
+
+       ret = af9015_write_reg(adap->dev, 0xd506, (pid >> 8));
+       if (ret)
+               goto error;
+
+       idx = ((index & 0x1f) | (1 << 5));
+       ret = af9015_write_reg(adap->dev, 0xd504, idx);
+
+error:
+       return ret;
+}
+
+static int af9015_init_endpoint(struct dvb_usb_device *d)
+{
+       struct af9015_state *state = d->priv;
+       int ret;
+       u16 frame_size;
+       u8  packet_size;
+       deb_info("%s: USB speed:%d\n", __func__, d->udev->speed);
+
+       if (d->udev->speed == USB_SPEED_FULL) {
+               frame_size = TS_USB11_FRAME_SIZE/4;
+               packet_size = TS_USB11_MAX_PACKET_SIZE/4;
+       } else {
+               frame_size = TS_USB20_FRAME_SIZE/4;
+               packet_size = TS_USB20_MAX_PACKET_SIZE/4;
+       }
+
+       ret = af9015_set_reg_bit(d, 0xd507, 2); /* assert EP4 reset */
+       if (ret)
+               goto error;
+       ret = af9015_set_reg_bit(d, 0xd50b, 1); /* assert EP5 reset */
+       if (ret)
+               goto error;
+       ret = af9015_clear_reg_bit(d, 0xdd11, 5); /* disable EP4 */
+       if (ret)
+               goto error;
+       ret = af9015_clear_reg_bit(d, 0xdd11, 6); /* disable EP5 */
+       if (ret)
+               goto error;
+       ret = af9015_set_reg_bit(d, 0xdd11, 5); /* enable EP4 */
+       if (ret)
+               goto error;
+       if (state->dual_mode) {
+               ret = af9015_set_reg_bit(d, 0xdd11, 6); /* enable EP5 */
+               if (ret)
+                       goto error;
+       }
+       ret = af9015_clear_reg_bit(d, 0xdd13, 5); /* disable EP4 NAK */
+       if (ret)
+               goto error;
+       if (state->dual_mode) {
+               ret = af9015_clear_reg_bit(d, 0xdd13, 6); /* disable EP5 NAK */
+               if (ret)
+                       goto error;
+       }
+       /* EP4 xfer length */
+       ret = af9015_write_reg(d, 0xdd88, frame_size & 0xff);
+       if (ret)
+               goto error;
+       ret = af9015_write_reg(d, 0xdd89, frame_size >> 8);
+       if (ret)
+               goto error;
+       /* EP5 xfer length */
+       ret = af9015_write_reg(d, 0xdd8a, frame_size & 0xff);
+       if (ret)
+               goto error;
+       ret = af9015_write_reg(d, 0xdd8b, frame_size >> 8);
+       if (ret)
+               goto error;
+       ret = af9015_write_reg(d, 0xdd0c, packet_size); /* EP4 packet size */
+       if (ret)
+               goto error;
+       ret = af9015_write_reg(d, 0xdd0d, packet_size); /* EP5 packet size */
+       if (ret)
+               goto error;
+       ret = af9015_clear_reg_bit(d, 0xd507, 2); /* negate EP4 reset */
+       if (ret)
+               goto error;
+       if (state->dual_mode) {
+               ret = af9015_clear_reg_bit(d, 0xd50b, 1); /* negate EP5 reset */
+               if (ret)
+                       goto error;
        }
 
+       /* enable / disable mp2if2 */
+       if (state->dual_mode)
+               ret = af9015_set_reg_bit(d, 0xd50b, 0);
+       else
+               ret = af9015_clear_reg_bit(d, 0xd50b, 0);
+
+error:
+       if (ret)
+               err("endpoint init failed:%d", ret);
+       return ret;
+}
+
+static int af9015_init(struct dvb_usb_device *d)
+{
+       struct af9015_state *state = d->priv;
+       int ret;
+       deb_info("%s:\n", __func__);
+
+       mutex_init(&state->fe_mutex);
+
+       /* init RC canary */
+       ret = af9015_write_reg(d, 0x98e9, 0xff);
+       if (ret)
+               goto error;
+
+       ret = af9015_init_endpoint(d);
+       if (ret)
+               goto error;
+
+error:
        return ret;
 }
 
-enum af9015_usb_table_entry {
-       AFATECH_9015,
-       AFATECH_9016,
-       WINFAST_DTV_GOLD,
-       PINNACLE_PCTV_71E,
-       KWORLD_PLUSTV_399U,
-       TINYTWIN,
-       AZUREWAVE_TU700,
-       TERRATEC_AF9015,
-       KWORLD_PLUSTV_PC160,
-       AVERTV_VOLAR_X,
-       XTENSIONS_380U,
-       MSI_DIGIVOX_DUO,
-       AVERTV_VOLAR_X_REV2,
-       TELESTAR_STARSTICK_2,
-       AVERMEDIA_A309_USB,
-       MSI_DIGIVOX_MINI_III,
-       KWORLD_E396,
-       KWORLD_E39B,
-       KWORLD_E395,
-       TREKSTOR_DVBT,
-       AVERTV_A850,
-       AVERTV_A805,
-       CONCEPTRONIC_CTVDIGRCU,
-       KWORLD_MC810,
-       GENIUS_TVGO_DVB_T03,
-       KWORLD_399U_2,
-       KWORLD_PC160_T,
-       SVEON_STV20,
-       TINYTWIN_2,
-       WINFAST_DTV2000DS,
-       KWORLD_UB383_T,
-       KWORLD_E39A,
-       AVERMEDIA_A815M,
-       CINERGY_T_STICK_RC,
-       CINERGY_T_DUAL_RC,
-       AVERTV_A850T,
-       TINYTWIN_3,
-       SVEON_STV22,
+struct af9015_rc_setup {
+       unsigned int id;
+       char *rc_codes;
 };
 
-static struct usb_device_id af9015_usb_table[] = {
-       [AFATECH_9015] = {
-               USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9015)},
-       [AFATECH_9016] = {
-               USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9016)},
-       [WINFAST_DTV_GOLD] = {
-               USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV_DONGLE_GOLD)},
-       [PINNACLE_PCTV_71E] = {
-               USB_DEVICE(USB_VID_PINNACLE, USB_PID_PINNACLE_PCTV71E)},
-       [KWORLD_PLUSTV_399U] = {
-               USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U)},
-       [TINYTWIN] = {
-               USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TINYTWIN)},
-       [AZUREWAVE_TU700] = {
-               USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_AZUREWAVE_AD_TU700)},
-       [TERRATEC_AF9015] = {
-               USB_DEVICE(USB_VID_TERRATEC,
-                               USB_PID_TERRATEC_CINERGY_T_USB_XE_REV2)},
-       [KWORLD_PLUSTV_PC160] = {
-               USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_2T)},
-       [AVERTV_VOLAR_X] = {
-               USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X)},
-       [XTENSIONS_380U] = {
-               USB_DEVICE(USB_VID_XTENSIONS, USB_PID_XTENSIONS_XD_380)},
-       [MSI_DIGIVOX_DUO] = {
-               USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGIVOX_DUO)},
-       [AVERTV_VOLAR_X_REV2] = {
-               USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X_2)},
-       [TELESTAR_STARSTICK_2] = {
-               USB_DEVICE(USB_VID_TELESTAR,  USB_PID_TELESTAR_STARSTICK_2)},
-       [AVERMEDIA_A309_USB] = {
-               USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A309)},
-       [MSI_DIGIVOX_MINI_III] = {
-               USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGI_VOX_MINI_III)},
-       [KWORLD_E396] = {
-               USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U)},
-       [KWORLD_E39B] = {
-               USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_2)},
-       [KWORLD_E395] = {
-               USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_3)},
-       [TREKSTOR_DVBT] = {
-               USB_DEVICE(USB_VID_AFATECH, USB_PID_TREKSTOR_DVBT)},
-       [AVERTV_A850] = {
-               USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A850)},
-       [AVERTV_A805] = {
-               USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A805)},
-       [CONCEPTRONIC_CTVDIGRCU] = {
-               USB_DEVICE(USB_VID_KWORLD_2, USB_PID_CONCEPTRONIC_CTVDIGRCU)},
-       [KWORLD_MC810] = {
-               USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_MC810)},
-       [GENIUS_TVGO_DVB_T03] = {
-               USB_DEVICE(USB_VID_KYE, USB_PID_GENIUS_TVGO_DVB_T03)},
-       [KWORLD_399U_2] = {
-               USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U_2)},
-       [KWORLD_PC160_T] = {
-               USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_T)},
-       [SVEON_STV20] = {
-               USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV20)},
-       [TINYTWIN_2] = {
-               USB_DEVICE(USB_VID_KWORLD_2, USB_PID_TINYTWIN_2)},
-       [WINFAST_DTV2000DS] = {
-               USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV2000DS)},
-       [KWORLD_UB383_T] = {
-               USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_UB383_T)},
-       [KWORLD_E39A] = {
-               USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_4)},
-       [AVERMEDIA_A815M] = {
-               USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A815M)},
-       [CINERGY_T_STICK_RC] = {
-               USB_DEVICE(USB_VID_TERRATEC,
-                               USB_PID_TERRATEC_CINERGY_T_STICK_RC)},
-       [CINERGY_T_DUAL_RC] = {
-               USB_DEVICE(USB_VID_TERRATEC,
-                               USB_PID_TERRATEC_CINERGY_T_STICK_DUAL_RC)},
-       [AVERTV_A850T] = {
-               USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A850T)},
-       [TINYTWIN_3] = {
-               USB_DEVICE(USB_VID_GTEK, USB_PID_TINYTWIN_3)},
-       [SVEON_STV22] = {
-               USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV22)},
+static char *af9015_rc_setup_match(unsigned int id,
+       const struct af9015_rc_setup *table)
+{
+       for (; table->rc_codes; table++)
+               if (table->id == id)
+                       return table->rc_codes;
+       return NULL;
+}
+
+static const struct af9015_rc_setup af9015_rc_setup_modparam[] = {
+       { AF9015_REMOTE_A_LINK_DTU_M, RC_MAP_ALINK_DTU_M },
+       { AF9015_REMOTE_MSI_DIGIVOX_MINI_II_V3, RC_MAP_MSI_DIGIVOX_II },
+       { AF9015_REMOTE_MYGICTV_U718, RC_MAP_TOTAL_MEDIA_IN_HAND },
+       { AF9015_REMOTE_DIGITTRADE_DVB_T, RC_MAP_DIGITTRADE },
+       { AF9015_REMOTE_AVERMEDIA_KS, RC_MAP_AVERMEDIA_RM_KS },
        { }
 };
-MODULE_DEVICE_TABLE(usb, af9015_usb_table);
-
-#define AF9015_RC_INTERVAL 500
-static struct dvb_usb_device_properties af9015_properties[] = {
-       {
-               .caps = DVB_USB_IS_AN_I2C_ADAPTER,
-
-               .usb_ctrl = DEVICE_SPECIFIC,
-               .download_firmware = af9015_download_firmware,
-               .firmware = "dvb-usb-af9015.fw",
-               .no_reconnect = 1,
-
-               .size_of_priv = sizeof(struct af9015_state),
-
-               .num_adapters = 2,
-               .adapter = {
-                       {
-                               .num_frontends = 1,
-                               .fe = {
-                                       {
-                                               .caps = DVB_USB_ADAP_HAS_PID_FILTER |
-                                                       DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
-
-                                               .pid_filter_count = 32,
-                                               .pid_filter       = af9015_pid_filter,
-                                               .pid_filter_ctrl  = af9015_pid_filter_ctrl,
-
-                                               .frontend_attach = af9015_af9013_frontend_attach,
-                                               .tuner_attach    = af9015_tuner_attach,
-                                               .stream = {
-                                                       .type = USB_BULK,
-                                                       .count = 6,
-                                                       .endpoint = 0x84,
-                                               },
-                                       }
-                               },
-                       },
-                       {
-                               .num_frontends = 1,
-                               .fe = {
-                                       {
-                                               .frontend_attach = af9015_af9013_frontend_attach,
-                                               .tuner_attach    = af9015_tuner_attach,
-                                               .stream = {
-                                                       .type = USB_BULK,
-                                                       .count = 6,
-                                                       .endpoint = 0x85,
-                                                       .u = {
-                                                               .bulk = {
-                                                                       .buffersize = TS_USB20_FRAME_SIZE,
-                                                               }
-                                                       }
-                                               },
-                                       }
-                               },
-                       }
-               },
 
-               .identify_state = af9015_identify_state,
+static const struct af9015_rc_setup af9015_rc_setup_hashes[] = {
+       { 0xb8feb708, RC_MAP_MSI_DIGIVOX_II },
+       { 0xa3703d00, RC_MAP_ALINK_DTU_M },
+       { 0x9b7dc64e, RC_MAP_TOTAL_MEDIA_IN_HAND }, /* MYGICTV U718 */
+       { 0x5d49e3db, RC_MAP_DIGITTRADE }, /* LC-Power LC-USB-DVBT */
+       { }
+};
 
-               .rc.core = {
-                       .protocol         = RC_TYPE_NEC,
-                       .module_name      = "af9015",
-                       .rc_query         = af9015_rc_query,
-                       .rc_interval      = AF9015_RC_INTERVAL,
-                       .allowed_protos   = RC_TYPE_NEC,
-               },
+static int af9015_rc_query(struct dvb_usb_device *d)
+{
+       struct af9015_state *state = d->priv;
+       int ret;
+       u8 buf[17];
 
-               .i2c_algo = &af9015_i2c_algo,
-
-               .num_device_descs = 12, /* check max from dvb-usb.h */
-               .devices = {
-                       {
-                               .name = "Afatech AF9015 DVB-T USB2.0 stick",
-                               .cold_ids = {
-                                       &af9015_usb_table[AFATECH_9015],
-                                       &af9015_usb_table[AFATECH_9016],
-                               },
-                       }, {
-                               .name = "Leadtek WinFast DTV Dongle Gold",
-                               .cold_ids = {
-                                       &af9015_usb_table[WINFAST_DTV_GOLD],
-                               },
-                       }, {
-                               .name = "Pinnacle PCTV 71e",
-                               .cold_ids = {
-                                       &af9015_usb_table[PINNACLE_PCTV_71E],
-                               },
-                       }, {
-                               .name = "KWorld PlusTV Dual DVB-T Stick " \
-                                       "(DVB-T 399U)",
-                               .cold_ids = {
-                                       &af9015_usb_table[KWORLD_PLUSTV_399U],
-                                       &af9015_usb_table[KWORLD_399U_2],
-                               },
-                       }, {
-                               .name = "DigitalNow TinyTwin DVB-T Receiver",
-                               .cold_ids = {
-                                       &af9015_usb_table[TINYTWIN],
-                                       &af9015_usb_table[TINYTWIN_2],
-                                       &af9015_usb_table[TINYTWIN_3],
-                               },
-                       }, {
-                               .name = "TwinHan AzureWave AD-TU700(704J)",
-                               .cold_ids = {
-                                       &af9015_usb_table[AZUREWAVE_TU700],
-                               },
-                       }, {
-                               .name = "TerraTec Cinergy T USB XE",
-                               .cold_ids = {
-                                       &af9015_usb_table[TERRATEC_AF9015],
-                               },
-                       }, {
-                               .name = "KWorld PlusTV Dual DVB-T PCI " \
-                                       "(DVB-T PC160-2T)",
-                               .cold_ids = {
-                                       &af9015_usb_table[KWORLD_PLUSTV_PC160],
-                               },
-                       }, {
-                               .name = "AVerMedia AVerTV DVB-T Volar X",
-                               .cold_ids = {
-                                       &af9015_usb_table[AVERTV_VOLAR_X],
-                               },
-                       }, {
-                               .name = "TerraTec Cinergy T Stick RC",
-                               .cold_ids = {
-                                       &af9015_usb_table[CINERGY_T_STICK_RC],
-                               },
-                       }, {
-                               .name = "TerraTec Cinergy T Stick Dual RC",
-                               .cold_ids = {
-                                       &af9015_usb_table[CINERGY_T_DUAL_RC],
-                               },
-                       }, {
-                               .name = "AverMedia AVerTV Red HD+ (A850T)",
-                               .cold_ids = {
-                                       &af9015_usb_table[AVERTV_A850T],
-                               },
-                       },
-               }
-       }, {
-               .caps = DVB_USB_IS_AN_I2C_ADAPTER,
-
-               .usb_ctrl = DEVICE_SPECIFIC,
-               .download_firmware = af9015_download_firmware,
-               .firmware = "dvb-usb-af9015.fw",
-               .no_reconnect = 1,
-
-               .size_of_priv = sizeof(struct af9015_state),
-
-               .num_adapters = 2,
-               .adapter = {
-                       {
-                               .num_frontends = 1,
-                               .fe = {
-                                       {
-                                               .caps = DVB_USB_ADAP_HAS_PID_FILTER |
-                                                       DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
-
-                                               .pid_filter_count = 32,
-                                               .pid_filter       = af9015_pid_filter,
-                                               .pid_filter_ctrl  = af9015_pid_filter_ctrl,
-
-                                               .frontend_attach = af9015_af9013_frontend_attach,
-                                               .tuner_attach    = af9015_tuner_attach,
-                                               .stream = {
-                                                       .type = USB_BULK,
-                                                       .count = 6,
-                                                       .endpoint = 0x84,
-                                               },
-                                       }
-                               },
-                       },
-                       {
-                               .num_frontends = 1,
-                               .fe = {
-                                       {
-                                               .frontend_attach = af9015_af9013_frontend_attach,
-                                               .tuner_attach    = af9015_tuner_attach,
-                                               .stream = {
-                                                       .type = USB_BULK,
-                                                       .count = 6,
-                                                       .endpoint = 0x85,
-                                                       .u = {
-                                                               .bulk = {
-                                                                       .buffersize = TS_USB20_FRAME_SIZE,
-                                                               }
-                                                       }
-                                               },
-                                       }
-                               },
-                       }
-               },
+       deb_info("%s:\n", __func__);
 
-               .identify_state = af9015_identify_state,
+       /* read registers needed to detect remote controller code */
+       ret = af9015_read_regs(d, 0x98d9, buf, sizeof(buf));
+       if (ret)
+               goto error;
 
-               .rc.core = {
-                       .protocol         = RC_TYPE_NEC,
-                       .module_name      = "af9015",
-                       .rc_query         = af9015_rc_query,
-                       .rc_interval      = AF9015_RC_INTERVAL,
-                       .allowed_protos   = RC_TYPE_NEC,
-               },
+       /* If any of these are non-zero, assume invalid data */
+       if (buf[1] || buf[2] || buf[3])
+               return ret;
 
-               .i2c_algo = &af9015_i2c_algo,
-
-               .num_device_descs = 10, /* check max from dvb-usb.h */
-               .devices = {
-                       {
-                               .name = "Xtensions XD-380",
-                               .cold_ids = {
-                                       &af9015_usb_table[XTENSIONS_380U],
-                               },
-                       }, {
-                               .name = "MSI DIGIVOX Duo",
-                               .cold_ids = {
-                                       &af9015_usb_table[MSI_DIGIVOX_DUO],
-                               },
-                       }, {
-                               .name = "Fujitsu-Siemens Slim Mobile USB DVB-T",
-                               .cold_ids = {
-                                       &af9015_usb_table[AVERTV_VOLAR_X_REV2],
-                               },
-                       }, {
-                               .name = "Telestar Starstick 2",
-                               .cold_ids = {
-                                       &af9015_usb_table[TELESTAR_STARSTICK_2],
-                               },
-                       }, {
-                               .name = "AVerMedia A309",
-                               .cold_ids = {
-                                       &af9015_usb_table[AVERMEDIA_A309_USB],
-                               },
-                       }, {
-                               .name = "MSI Digi VOX mini III",
-                               .cold_ids = {
-                                       &af9015_usb_table[MSI_DIGIVOX_MINI_III],
-                               },
-                       }, {
-                               .name = "KWorld USB DVB-T TV Stick II " \
-                                       "(VS-DVB-T 395U)",
-                               .cold_ids = {
-                                       &af9015_usb_table[KWORLD_E396],
-                                       &af9015_usb_table[KWORLD_E39B],
-                                       &af9015_usb_table[KWORLD_E395],
-                                       &af9015_usb_table[KWORLD_E39A],
-                               },
-                       }, {
-                               .name = "TrekStor DVB-T USB Stick",
-                               .cold_ids = {
-                                       &af9015_usb_table[TREKSTOR_DVBT],
-                               },
-                       }, {
-                               .name = "AverMedia AVerTV Volar Black HD " \
-                                       "(A850)",
-                               .cold_ids = {
-                                       &af9015_usb_table[AVERTV_A850],
-                               },
-                       }, {
-                               .name = "Sveon STV22 Dual USB DVB-T Tuner HDTV",
-                               .cold_ids = {
-                                       &af9015_usb_table[SVEON_STV22],
-                               },
-                       },
-               }
-       }, {
-               .caps = DVB_USB_IS_AN_I2C_ADAPTER,
-
-               .usb_ctrl = DEVICE_SPECIFIC,
-               .download_firmware = af9015_download_firmware,
-               .firmware = "dvb-usb-af9015.fw",
-               .no_reconnect = 1,
-
-               .size_of_priv = sizeof(struct af9015_state),
-
-               .num_adapters = 2,
-               .adapter = {
-                       {
-                               .num_frontends = 1,
-                               .fe = {
-                                       {
-                                               .caps = DVB_USB_ADAP_HAS_PID_FILTER |
-                                                       DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
-
-                                               .pid_filter_count = 32,
-                                               .pid_filter       = af9015_pid_filter,
-                                               .pid_filter_ctrl  = af9015_pid_filter_ctrl,
-
-                                               .frontend_attach = af9015_af9013_frontend_attach,
-                                               .tuner_attach    = af9015_tuner_attach,
-                                               .stream = {
-                                                       .type = USB_BULK,
-                                                       .count = 6,
-                                                       .endpoint = 0x84,
-                                               },
-                                       }
-                               },
-                       },
-                       {
-                               .num_frontends = 1,
-                               .fe = {
-                                       {
-                                               .frontend_attach = af9015_af9013_frontend_attach,
-                                               .tuner_attach    = af9015_tuner_attach,
-                                               .stream = {
-                                                       .type = USB_BULK,
-                                                       .count = 6,
-                                                       .endpoint = 0x85,
-                                                       .u = {
-                                                               .bulk = {
-                                                                       .buffersize = TS_USB20_FRAME_SIZE,
-                                                               }
-                                                       }
-                                               },
-                                       }
-                               },
-                       }
-               },
+       /* Check for repeat of previous code */
+       if ((state->rc_repeat != buf[6] || buf[0]) &&
+                       !memcmp(&buf[12], state->rc_last, 4)) {
+               deb_rc("%s: key repeated\n", __func__);
+               rc_keydown(d->rc_dev, state->rc_keycode, 0);
+               state->rc_repeat = buf[6];
+               return ret;
+       }
 
-               .identify_state = af9015_identify_state,
+       /* Only process key if canary killed */
+       if (buf[16] != 0xff && buf[0] != 0x01) {
+               deb_rc("%s: key pressed %02x %02x %02x %02x\n", __func__,
+                       buf[12], buf[13], buf[14], buf[15]);
 
-               .rc.core = {
-                       .protocol         = RC_TYPE_NEC,
-                       .module_name      = "af9015",
-                       .rc_query         = af9015_rc_query,
-                       .rc_interval      = AF9015_RC_INTERVAL,
-                       .allowed_protos   = RC_TYPE_NEC,
-               },
+               /* Reset the canary */
+               ret = af9015_write_reg(d, 0x98e9, 0xff);
+               if (ret)
+                       goto error;
 
-               .i2c_algo = &af9015_i2c_algo,
-
-               .num_device_descs = 9, /* check max from dvb-usb.h */
-               .devices = {
-                       {
-                               .name = "AverMedia AVerTV Volar GPS 805 (A805)",
-                               .cold_ids = {
-                                       &af9015_usb_table[AVERTV_A805],
-                               },
-                       }, {
-                               .name = "Conceptronic USB2.0 DVB-T CTVDIGRCU " \
-                                       "V3.0",
-                               .cold_ids = {
-                                       &af9015_usb_table[CONCEPTRONIC_CTVDIGRCU],
-                               },
-                       }, {
-                               .name = "KWorld Digial MC-810",
-                               .cold_ids = {
-                                       &af9015_usb_table[KWORLD_MC810],
-                               },
-                       }, {
-                               .name = "Genius TVGo DVB-T03",
-                               .cold_ids = {
-                                       &af9015_usb_table[GENIUS_TVGO_DVB_T03],
-                               },
-                       }, {
-                               .name = "KWorld PlusTV DVB-T PCI Pro Card " \
-                                       "(DVB-T PC160-T)",
-                               .cold_ids = {
-                                       &af9015_usb_table[KWORLD_PC160_T],
-                               },
-                       }, {
-                               .name = "Sveon STV20 Tuner USB DVB-T HDTV",
-                               .cold_ids = {
-                                       &af9015_usb_table[SVEON_STV20],
-                               },
-                       }, {
-                               .name = "Leadtek WinFast DTV2000DS",
-                               .cold_ids = {
-                                       &af9015_usb_table[WINFAST_DTV2000DS],
-                               },
-                       }, {
-                               .name = "KWorld USB DVB-T Stick Mobile " \
-                                       "(UB383-T)",
-                               .cold_ids = {
-                                       &af9015_usb_table[KWORLD_UB383_T],
-                               },
-                       }, {
-                               .name = "AverMedia AVerTV Volar M (A815Mac)",
-                               .cold_ids = {
-                                       &af9015_usb_table[AVERMEDIA_A815M],
-                               },
-                       },
+               /* Remember this key */
+               memcpy(state->rc_last, &buf[12], 4);
+               if (buf[14] == (u8) ~buf[15]) {
+                       if (buf[12] == (u8) ~buf[13]) {
+                               /* NEC */
+                               state->rc_keycode = buf[12] << 8 | buf[14];
+                       } else {
+                               /* NEC extended*/
+                               state->rc_keycode = buf[12] << 16 |
+                                       buf[13] << 8 | buf[14];
+                       }
+               } else {
+                       /* 32 bit NEC */
+                       state->rc_keycode = buf[12] << 24 | buf[13] << 16 |
+                                       buf[14] << 8 | buf[15];
                }
-       },
-};
+               rc_keydown(d->rc_dev, state->rc_keycode, 0);
+       } else {
+               deb_rc("%s: no key press\n", __func__);
+               /* Invalidate last keypress */
+               /* Not really needed, but helps with debug */
+               state->rc_last[2] = state->rc_last[3];
+       }
+
+       state->rc_repeat = buf[6];
+
+error:
+       if (ret)
+               err("%s: failed:%d", __func__, ret);
+
+       return ret;
+}
 
-static int af9015_usb_probe(struct usb_interface *intf,
-                           const struct usb_device_id *id)
+static int af9015_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
 {
-       int ret = 0;
-       struct dvb_usb_device *d = NULL;
-       struct usb_device *udev = interface_to_usbdev(intf);
-       u8 i;
-
-       deb_info("%s: interface:%d\n", __func__,
-               intf->cur_altsetting->desc.bInterfaceNumber);
-
-       /* interface 0 is used by DVB-T receiver and
-          interface 1 is for remote controller (HID) */
-       if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
-               ret = af9015_read_config(udev);
-               if (ret)
-                       return ret;
-
-               for (i = 0; i < af9015_properties_count; i++) {
-                       ret = dvb_usb_device_init(intf, &af9015_properties[i],
-                               THIS_MODULE, &d, adapter_nr);
-                       if (!ret)
-                               break;
-                       if (ret != -ENODEV)
-                               return ret;
-               }
-               if (ret)
-                       return ret;
+       struct af9015_state *state = d->priv;
+       u16 vid = le16_to_cpu(d->udev->descriptor.idVendor);
+
+       if (state->ir_mode == AF9015_IR_MODE_DISABLED)
+               return 0;
+
+       /* try to load remote based module param */
+       rc->map_name = af9015_rc_setup_match(dvb_usb_af9015_remote,
+                       af9015_rc_setup_modparam);
 
-               if (d)
-                       ret = af9015_init(d);
+       /* try to load remote based eeprom hash */
+       if (!rc->map_name)
+               rc->map_name = af9015_rc_setup_match(state->eeprom_sum,
+                               af9015_rc_setup_hashes);
+
+       /* try to load remote based USB iManufacturer string */
+       if (!rc->map_name && vid == USB_VID_AFATECH) {
+               /* Check USB manufacturer and product strings and try
+                  to determine correct remote in case of chip vendor
+                  reference IDs are used.
+                  DO NOT ADD ANYTHING NEW HERE. Use hashes instead. */
+               char manufacturer[10];
+               memset(manufacturer, 0, sizeof(manufacturer));
+               usb_string(d->udev, d->udev->descriptor.iManufacturer,
+                       manufacturer, sizeof(manufacturer));
+               if (!strcmp("MSI", manufacturer)) {
+                       /* iManufacturer 1 MSI
+                          iProduct      2 MSI K-VOX */
+                       rc->map_name = af9015_rc_setup_match(
+                                       AF9015_REMOTE_MSI_DIGIVOX_MINI_II_V3,
+                                       af9015_rc_setup_modparam);
+               }
        }
 
-       return ret;
+       rc->allowed_protos = RC_TYPE_NEC;
+       rc->query = af9015_rc_query;
+       rc->interval = 500;
+
+       return 0;
 }
 
+/* interface 0 is used by DVB-T receiver and
+   interface 1 is for remote controller (HID) */
+static struct dvb_usb_device_properties af9015_props = {
+       .driver_name = KBUILD_MODNAME,
+       .owner = THIS_MODULE,
+       .adapter_nr = adapter_nr,
+       .size_of_priv = sizeof(struct af9015_state),
+
+       .generic_bulk_ctrl_endpoint = 0x02,
+       .generic_bulk_ctrl_endpoint_response = 0x81,
+
+       .identify_state = af9015_identify_state,
+       .firmware = "dvb-usb-af9015.fw",
+       .download_firmware = af9015_download_firmware,
+
+       .read_config = af9015_read_config,
+       .i2c_algo = &af9015_i2c_algo,
+       .init = af9015_init,
+       .get_rc_config = af9015_get_rc_config,
+       .get_usb_stream_config = af9015_get_usb_stream_config,
+
+       .get_adapter_count = af9015_get_adapter_count,
+       .adapter = {
+               {
+                       .caps = DVB_USB_ADAP_HAS_PID_FILTER |
+                               DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
+                       .pid_filter_count = 32,
+                       .pid_filter = af9015_pid_filter,
+                       .pid_filter_ctrl = af9015_pid_filter_ctrl,
+                       .frontend_attach = af9015_af9013_frontend_attach,
+                       .tuner_attach = af9015_tuner_attach,
+               },
+               {
+                       .frontend_attach = af9015_af9013_frontend_attach,
+                       .tuner_attach = af9015_tuner_attach,
+               }
+       },
+};
+
+static const struct usb_device_id af9015_id_table[] = {
+       { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9015,
+               &af9015_props, "Afatech AF9015 reference design", NULL) },
+       { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9016,
+               &af9015_props, "Afatech AF9015 reference design", NULL) },
+       { DVB_USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV_DONGLE_GOLD,
+               &af9015_props, "Leadtek WinFast DTV Dongle Gold", RC_MAP_LEADTEK_Y04G0051) },
+       { DVB_USB_DEVICE(USB_VID_PINNACLE, USB_PID_PINNACLE_PCTV71E,
+               &af9015_props, "Pinnacle PCTV 71e", NULL) },
+       { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U,
+               &af9015_props, "KWorld PlusTV Dual DVB-T Stick (DVB-T 399U)", NULL) },
+       { DVB_USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TINYTWIN,
+               &af9015_props, "DigitalNow TinyTwin", RC_MAP_AZUREWAVE_AD_TU700) },
+       { DVB_USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_AZUREWAVE_AD_TU700,
+               &af9015_props, "TwinHan AzureWave AD-TU700(704J)", RC_MAP_AZUREWAVE_AD_TU700) },
+       { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_USB_XE_REV2,
+               &af9015_props, "TerraTec Cinergy T USB XE", NULL) },
+       { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_2T,
+               &af9015_props, "KWorld PlusTV Dual DVB-T PCI (DVB-T PC160-2T)", NULL) },
+       { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X,
+               &af9015_props, "AVerMedia AVerTV DVB-T Volar X", RC_MAP_AVERMEDIA_M135A) },
+       { DVB_USB_DEVICE(USB_VID_XTENSIONS, USB_PID_XTENSIONS_XD_380,
+               &af9015_props, "Xtensions XD-380", NULL) },
+       { DVB_USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGIVOX_DUO,
+               &af9015_props, "MSI DIGIVOX Duo", RC_MAP_MSI_DIGIVOX_III) },
+       { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X_2,
+               &af9015_props, "Fujitsu-Siemens Slim Mobile USB DVB-T", NULL) },
+       { DVB_USB_DEVICE(USB_VID_TELESTAR,  USB_PID_TELESTAR_STARSTICK_2,
+               &af9015_props, "Telestar Starstick 2", NULL) },
+       { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A309,
+               &af9015_props, "AVerMedia A309", NULL) },
+       { DVB_USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGI_VOX_MINI_III,
+               &af9015_props, "MSI Digi VOX mini III", RC_MAP_MSI_DIGIVOX_III) },
+       { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U,
+               &af9015_props, "KWorld USB DVB-T TV Stick II (VS-DVB-T 395U)", NULL) },
+       { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_2,
+               &af9015_props, "KWorld USB DVB-T TV Stick II (VS-DVB-T 395U)", NULL) },
+       { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_3,
+               &af9015_props, "KWorld USB DVB-T TV Stick II (VS-DVB-T 395U)", NULL) },
+       { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_TREKSTOR_DVBT,
+               &af9015_props, "TrekStor DVB-T USB Stick", RC_MAP_TREKSTOR) },
+       { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A850,
+               &af9015_props, "AverMedia AVerTV Volar Black HD (A850)", NULL) },
+       { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A805,
+               &af9015_props, "AverMedia AVerTV Volar GPS 805 (A805)", NULL) },
+       { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_CONCEPTRONIC_CTVDIGRCU,
+               &af9015_props, "Conceptronic USB2.0 DVB-T CTVDIGRCU V3.0", NULL) },
+       { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_MC810,
+               &af9015_props, "KWorld Digial MC-810", NULL) },
+       { DVB_USB_DEVICE(USB_VID_KYE, USB_PID_GENIUS_TVGO_DVB_T03,
+               &af9015_props, "Genius TVGo DVB-T03", NULL) },
+       { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U_2,
+               &af9015_props, "KWorld PlusTV Dual DVB-T Stick (DVB-T 399U)", NULL) },
+       { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_T,
+               &af9015_props, "KWorld PlusTV DVB-T PCI Pro Card (DVB-T PC160-T)", NULL) },
+       { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV20,
+               &af9015_props, "Sveon STV20 Tuner USB DVB-T HDTV", NULL) },
+       { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_TINYTWIN_2,
+               &af9015_props, "DigitalNow TinyTwin v2", RC_MAP_DIGITALNOW_TINYTWIN) },
+       { DVB_USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV2000DS,
+               &af9015_props, "Leadtek WinFast DTV2000DS", RC_MAP_LEADTEK_Y04G0051) },
+       { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_UB383_T,
+               &af9015_props, "KWorld USB DVB-T Stick Mobile (UB383-T)", NULL) },
+       { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_4,
+               &af9015_props, "KWorld USB DVB-T TV Stick II (VS-DVB-T 395U)", NULL) },
+       { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A815M,
+               &af9015_props, "AverMedia AVerTV Volar M (A815Mac)", NULL) },
+       { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_RC,
+               &af9015_props, "TerraTec Cinergy T Stick RC", RC_MAP_TERRATEC_SLIM_2) },
+       { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_DUAL_RC,
+               &af9015_props, "TerraTec Cinergy T Stick Dual RC", RC_MAP_TERRATEC_SLIM) },
+       { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A850T,
+               &af9015_props, "AverMedia AVerTV Red HD+ (A850T)", NULL) },
+       { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_TINYTWIN_3,
+               &af9015_props, "DigitalNow TinyTwin v3", RC_MAP_DIGITALNOW_TINYTWIN) },
+       { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV22,
+               &af9015_props, "Sveon STV22 Dual USB DVB-T Tuner HDTV", RC_MAP_MSI_DIGIVOX_III) },
+       { }
+};
+MODULE_DEVICE_TABLE(usb, af9015_id_table);
+
 /* usb specific object needed to register this driver with the usb subsystem */
 static struct usb_driver af9015_usb_driver = {
-       .name = "dvb_usb_af9015",
-       .probe = af9015_usb_probe,
-       .disconnect = dvb_usb_device_exit,
-       .id_table = af9015_usb_table,
+       .name = KBUILD_MODNAME,
+       .probe = dvb_usbv2_probe,
+       .disconnect = dvb_usbv2_disconnect,
+       .id_table = af9015_id_table,
+       .no_dynamic_id = 1,
 };
 
 module_usb_driver(af9015_usb_driver);
index 2f68419..b41ee73 100644 (file)
  *
  */
 
-#ifndef _DVB_USB_AF9015_H_
-#define _DVB_USB_AF9015_H_
+#ifndef AF9015_H
+#define AF9015_H
+
+#include <linux/hash.h>
+#include "dvb_usb.h"
+#include "af9013.h"
+#include "dvb-pll.h"
+#include "mt2060.h"
+#include "qt1010.h"
+#include "tda18271.h"
+#include "mxl5005s.h"
+#include "mc44s803.h"
+#include "tda18218.h"
+#include "mxl5007t.h"
 
 #define DVB_USB_LOG_PREFIX "af9015"
-#include "dvb-usb.h"
+
+#ifdef CONFIG_DVB_USB_DEBUG
+#define dprintk(var, level, args...) \
+       do { if ((var & level)) printk(args); } while (0)
+#define DVB_USB_DEBUG_STATUS
+#else
+#define dprintk(args...)
+#define DVB_USB_DEBUG_STATUS " (debugging is not enabled)"
+#endif
 
 #define deb_info(args...) dprintk(dvb_usb_af9015_debug, 0x01, args)
 #define deb_rc(args...)   dprintk(dvb_usb_af9015_debug, 0x02, args)
-#define deb_xfer(args...) dprintk(dvb_usb_af9015_debug, 0x04, args)
-#define deb_reg(args...)  dprintk(dvb_usb_af9015_debug, 0x08, args)
-#define deb_i2c(args...)  dprintk(dvb_usb_af9015_debug, 0x10, args)
-#define deb_fw(args...)   dprintk(dvb_usb_af9015_debug, 0x20, args)
+
+#undef err
+#define err(format, arg...) \
+       printk(KERN_ERR     DVB_USB_LOG_PREFIX ": " format "\n" , ## arg)
+#undef warn
+#define warn(format, arg...) \
+       printk(KERN_WARNING DVB_USB_LOG_PREFIX ": " format "\n" , ## arg)
+
+/* Windows driver uses packet count 21 for USB1.1 and 348 for USB2.0.
+   We use smaller - about 1/4 from the original, 5 and 87. */
+#define TS_PACKET_SIZE            188
+
+#define TS_USB20_PACKET_COUNT      87
+#define TS_USB20_FRAME_SIZE       (TS_PACKET_SIZE*TS_USB20_PACKET_COUNT)
+
+#define TS_USB11_PACKET_COUNT       5
+#define TS_USB11_FRAME_SIZE       (TS_PACKET_SIZE*TS_USB11_PACKET_COUNT)
+
+#define TS_USB20_MAX_PACKET_SIZE  512
+#define TS_USB11_MAX_PACKET_SIZE   64
 
 #define AF9015_I2C_EEPROM  0xa0
 #define AF9015_I2C_DEMOD   0x38
@@ -99,9 +135,17 @@ enum af9015_ir_mode {
 };
 
 struct af9015_state {
+       u8 ir_mode;
        u8 rc_repeat;
        u32 rc_keycode;
        u8 rc_last[4];
+       u8 dual_mode;
+       u8 seq; /* packet sequence number */
+       u16 mt2060_if1[2];
+       u16 firmware_size;
+       u16 firmware_checksum;
+       u32 eeprom_sum;
+       struct af9013_config af9013_config[2];
 
        /* for demod callback override */
        int (*set_frontend[2]) (struct dvb_frontend *fe);
@@ -110,14 +154,7 @@ struct af9015_state {
        int (*sleep[2]) (struct dvb_frontend *fe);
        int (*tuner_init[2]) (struct dvb_frontend *fe);
        int (*tuner_sleep[2]) (struct dvb_frontend *fe);
-};
-
-struct af9015_config {
-       u8  dual_mode:1;
-       u16 mt2060_if1[2];
-       u16 firmware_size;
-       u16 firmware_checksum;
-       u32 eeprom_sum;
+       struct mutex fe_mutex;
 };
 
 enum af9015_remote {