linux/drivers/media/usb/dvb-usb-v2/ec168.c
<<
>>
Prefs
   1/*
   2 * E3C EC168 DVB USB driver
   3 *
   4 * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
   5 *
   6 *    This program is free software; you can redistribute it and/or modify
   7 *    it under the terms of the GNU General Public License as published by
   8 *    the Free Software Foundation; either version 2 of the License, or
   9 *    (at your option) any later version.
  10 *
  11 *    This program is distributed in the hope that it will be useful,
  12 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14 *    GNU General Public License for more details.
  15 *
  16 *    You should have received a copy of the GNU General Public License
  17 *    along with this program; if not, write to the Free Software
  18 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19 *
  20 */
  21
  22#include "ec168.h"
  23#include "ec100.h"
  24#include "mxl5005s.h"
  25
  26DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  27
  28static int ec168_ctrl_msg(struct dvb_usb_device *d, struct ec168_req *req)
  29{
  30        int ret;
  31        unsigned int pipe;
  32        u8 request, requesttype;
  33        u8 *buf;
  34
  35        switch (req->cmd) {
  36        case DOWNLOAD_FIRMWARE:
  37        case GPIO:
  38        case WRITE_I2C:
  39        case STREAMING_CTRL:
  40                requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
  41                request = req->cmd;
  42                break;
  43        case READ_I2C:
  44                requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
  45                request = req->cmd;
  46                break;
  47        case GET_CONFIG:
  48                requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
  49                request = CONFIG;
  50                break;
  51        case SET_CONFIG:
  52                requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
  53                request = CONFIG;
  54                break;
  55        case WRITE_DEMOD:
  56                requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
  57                request = DEMOD_RW;
  58                break;
  59        case READ_DEMOD:
  60                requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
  61                request = DEMOD_RW;
  62                break;
  63        default:
  64                dev_err(&d->udev->dev, "%s: unknown command=%02x\n",
  65                                KBUILD_MODNAME, req->cmd);
  66                ret = -EINVAL;
  67                goto error;
  68        }
  69
  70        buf = kmalloc(req->size, GFP_KERNEL);
  71        if (!buf) {
  72                ret = -ENOMEM;
  73                goto error;
  74        }
  75
  76        if (requesttype == (USB_TYPE_VENDOR | USB_DIR_OUT)) {
  77                /* write */
  78                memcpy(buf, req->data, req->size);
  79                pipe = usb_sndctrlpipe(d->udev, 0);
  80        } else {
  81                /* read */
  82                pipe = usb_rcvctrlpipe(d->udev, 0);
  83        }
  84
  85        msleep(1); /* avoid I2C errors */
  86
  87        ret = usb_control_msg(d->udev, pipe, request, requesttype, req->value,
  88                req->index, buf, req->size, EC168_USB_TIMEOUT);
  89
  90        dvb_usb_dbg_usb_control_msg(d->udev, request, requesttype, req->value,
  91                        req->index, buf, req->size);
  92
  93        if (ret < 0)
  94                goto err_dealloc;
  95        else
  96                ret = 0;
  97
  98        /* read request, copy returned data to return buf */
  99        if (!ret && requesttype == (USB_TYPE_VENDOR | USB_DIR_IN))
 100                memcpy(req->data, buf, req->size);
 101
 102        kfree(buf);
 103        return ret;
 104
 105err_dealloc:
 106        kfree(buf);
 107error:
 108        dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
 109        return ret;
 110}
 111
 112/* I2C */
 113static struct ec100_config ec168_ec100_config;
 114
 115static int ec168_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
 116        int num)
 117{
 118        struct dvb_usb_device *d = i2c_get_adapdata(adap);
 119        struct ec168_req req;
 120        int i = 0;
 121        int ret;
 122
 123        if (num > 2)
 124                return -EINVAL;
 125
 126        if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
 127                return -EAGAIN;
 128
 129        while (i < num) {
 130                if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) {
 131                        if (msg[i].addr == ec168_ec100_config.demod_address) {
 132                                req.cmd = READ_DEMOD;
 133                                req.value = 0;
 134                                req.index = 0xff00 + msg[i].buf[0]; /* reg */
 135                                req.size = msg[i+1].len; /* bytes to read */
 136                                req.data = &msg[i+1].buf[0];
 137                                ret = ec168_ctrl_msg(d, &req);
 138                                i += 2;
 139                        } else {
 140                                dev_err(&d->udev->dev, "%s: I2C read not " \
 141                                                "implemented\n",
 142                                                KBUILD_MODNAME);
 143                                ret = -EOPNOTSUPP;
 144                                i += 2;
 145                        }
 146                } else {
 147                        if (msg[i].addr == ec168_ec100_config.demod_address) {
 148                                req.cmd = WRITE_DEMOD;
 149                                req.value = msg[i].buf[1]; /* val */
 150                                req.index = 0xff00 + msg[i].buf[0]; /* reg */
 151                                req.size = 0;
 152                                req.data = NULL;
 153                                ret = ec168_ctrl_msg(d, &req);
 154                                i += 1;
 155                        } else {
 156                                req.cmd = WRITE_I2C;
 157                                req.value = msg[i].buf[0]; /* val */
 158                                req.index = 0x0100 + msg[i].addr; /* I2C addr */
 159                                req.size = msg[i].len-1;
 160                                req.data = &msg[i].buf[1];
 161                                ret = ec168_ctrl_msg(d, &req);
 162                                i += 1;
 163                        }
 164                }
 165                if (ret)
 166                        goto error;
 167
 168        }
 169        ret = i;
 170
 171error:
 172        mutex_unlock(&d->i2c_mutex);
 173        return ret;
 174}
 175
 176static u32 ec168_i2c_func(struct i2c_adapter *adapter)
 177{
 178        return I2C_FUNC_I2C;
 179}
 180
 181static struct i2c_algorithm ec168_i2c_algo = {
 182        .master_xfer   = ec168_i2c_xfer,
 183        .functionality = ec168_i2c_func,
 184};
 185
 186/* Callbacks for DVB USB */
 187static int ec168_identify_state(struct dvb_usb_device *d, const char **name)
 188{
 189        int ret;
 190        u8 reply;
 191        struct ec168_req req = {GET_CONFIG, 0, 1, sizeof(reply), &reply};
 192        dev_dbg(&d->udev->dev, "%s:\n", __func__);
 193
 194        ret = ec168_ctrl_msg(d, &req);
 195        if (ret)
 196                goto error;
 197
 198        dev_dbg(&d->udev->dev, "%s: reply=%02x\n", __func__, reply);
 199
 200        if (reply == 0x01)
 201                ret = WARM;
 202        else
 203                ret = COLD;
 204
 205        return ret;
 206error:
 207        dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
 208        return ret;
 209}
 210
 211static int ec168_download_firmware(struct dvb_usb_device *d,
 212                const struct firmware *fw)
 213{
 214        int ret, len, remaining;
 215        struct ec168_req req = {DOWNLOAD_FIRMWARE, 0, 0, 0, NULL};
 216        dev_dbg(&d->udev->dev, "%s:\n", __func__);
 217
 218        #define LEN_MAX 2048 /* max packet size */
 219        for (remaining = fw->size; remaining > 0; remaining -= LEN_MAX) {
 220                len = remaining;
 221                if (len > LEN_MAX)
 222                        len = LEN_MAX;
 223
 224                req.size = len;
 225                req.data = (u8 *) &fw->data[fw->size - remaining];
 226                req.index = fw->size - remaining;
 227
 228                ret = ec168_ctrl_msg(d, &req);
 229                if (ret) {
 230                        dev_err(&d->udev->dev,
 231                                        "%s: firmware download failed=%d\n",
 232                                        KBUILD_MODNAME, ret);
 233                        goto error;
 234                }
 235        }
 236
 237        req.size = 0;
 238
 239        /* set "warm"? */
 240        req.cmd = SET_CONFIG;
 241        req.value = 0;
 242        req.index = 0x0001;
 243        ret = ec168_ctrl_msg(d, &req);
 244        if (ret)
 245                goto error;
 246
 247        /* really needed - no idea what does */
 248        req.cmd = GPIO;
 249        req.value = 0;
 250        req.index = 0x0206;
 251        ret = ec168_ctrl_msg(d, &req);
 252        if (ret)
 253                goto error;
 254
 255        /* activate tuner I2C? */
 256        req.cmd = WRITE_I2C;
 257        req.value = 0;
 258        req.index = 0x00c6;
 259        ret = ec168_ctrl_msg(d, &req);
 260        if (ret)
 261                goto error;
 262
 263        return ret;
 264error:
 265        dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
 266        return ret;
 267}
 268
 269static struct ec100_config ec168_ec100_config = {
 270        .demod_address = 0xff, /* not real address, demod is integrated */
 271};
 272
 273static int ec168_ec100_frontend_attach(struct dvb_usb_adapter *adap)
 274{
 275        struct dvb_usb_device *d = adap_to_d(adap);
 276        dev_dbg(&d->udev->dev, "%s:\n", __func__);
 277
 278        adap->fe[0] = dvb_attach(ec100_attach, &ec168_ec100_config,
 279                        &d->i2c_adap);
 280        if (adap->fe[0] == NULL)
 281                return -ENODEV;
 282
 283        return 0;
 284}
 285
 286static struct mxl5005s_config ec168_mxl5003s_config = {
 287        .i2c_address     = 0xc6,
 288        .if_freq         = IF_FREQ_4570000HZ,
 289        .xtal_freq       = CRYSTAL_FREQ_16000000HZ,
 290        .agc_mode        = MXL_SINGLE_AGC,
 291        .tracking_filter = MXL_TF_OFF,
 292        .rssi_enable     = MXL_RSSI_ENABLE,
 293        .cap_select      = MXL_CAP_SEL_ENABLE,
 294        .div_out         = MXL_DIV_OUT_4,
 295        .clock_out       = MXL_CLOCK_OUT_DISABLE,
 296        .output_load     = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
 297        .top             = MXL5005S_TOP_25P2,
 298        .mod_mode        = MXL_DIGITAL_MODE,
 299        .if_mode         = MXL_ZERO_IF,
 300        .AgcMasterByte   = 0x00,
 301};
 302
 303static int ec168_mxl5003s_tuner_attach(struct dvb_usb_adapter *adap)
 304{
 305        struct dvb_usb_device *d = adap_to_d(adap);
 306        dev_dbg(&d->udev->dev, "%s:\n", __func__);
 307
 308        return dvb_attach(mxl5005s_attach, adap->fe[0], &d->i2c_adap,
 309                        &ec168_mxl5003s_config) == NULL ? -ENODEV : 0;
 310}
 311
 312static int ec168_streaming_ctrl(struct dvb_frontend *fe, int onoff)
 313{
 314        struct dvb_usb_device *d = fe_to_d(fe);
 315        struct ec168_req req = {STREAMING_CTRL, 0x7f01, 0x0202, 0, NULL};
 316        dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
 317
 318        if (onoff)
 319                req.index = 0x0102;
 320        return ec168_ctrl_msg(d, &req);
 321}
 322
 323/* DVB USB Driver stuff */
 324/* bInterfaceNumber 0 is HID
 325 * bInterfaceNumber 1 is DVB-T */
 326static struct dvb_usb_device_properties ec168_props = {
 327        .driver_name = KBUILD_MODNAME,
 328        .owner = THIS_MODULE,
 329        .adapter_nr = adapter_nr,
 330        .bInterfaceNumber = 1,
 331
 332        .identify_state = ec168_identify_state,
 333        .firmware = EC168_FIRMWARE,
 334        .download_firmware = ec168_download_firmware,
 335
 336        .i2c_algo = &ec168_i2c_algo,
 337        .frontend_attach = ec168_ec100_frontend_attach,
 338        .tuner_attach = ec168_mxl5003s_tuner_attach,
 339        .streaming_ctrl = ec168_streaming_ctrl,
 340
 341        .num_adapters = 1,
 342        .adapter = {
 343                {
 344                        .stream = DVB_USB_STREAM_BULK(0x82, 6, 32 * 512),
 345                }
 346        },
 347};
 348
 349static const struct dvb_usb_driver_info ec168_driver_info = {
 350        .name = "E3C EC168 reference design",
 351        .props = &ec168_props,
 352};
 353
 354static const struct usb_device_id ec168_id[] = {
 355        { USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168),
 356                .driver_info = (kernel_ulong_t) &ec168_driver_info },
 357        { USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_2),
 358                .driver_info = (kernel_ulong_t) &ec168_driver_info },
 359        { USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_3),
 360                .driver_info = (kernel_ulong_t) &ec168_driver_info },
 361        { USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_4),
 362                .driver_info = (kernel_ulong_t) &ec168_driver_info },
 363        { USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_5),
 364                .driver_info = (kernel_ulong_t) &ec168_driver_info },
 365        {}
 366};
 367MODULE_DEVICE_TABLE(usb, ec168_id);
 368
 369static struct usb_driver ec168_driver = {
 370        .name = KBUILD_MODNAME,
 371        .id_table = ec168_id,
 372        .probe = dvb_usbv2_probe,
 373        .disconnect = dvb_usbv2_disconnect,
 374        .suspend = dvb_usbv2_suspend,
 375        .resume = dvb_usbv2_resume,
 376        .no_dynamic_id = 1,
 377        .soft_unbind = 1,
 378};
 379
 380module_usb_driver(ec168_driver);
 381
 382MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
 383MODULE_DESCRIPTION("E3C EC168 driver");
 384MODULE_LICENSE("GPL");
 385MODULE_FIRMWARE(EC168_FIRMWARE);
 386