Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * E3C EC168 DVB USB driver
0004  *
0005  * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
0006  */
0007 
0008 #include "ec168.h"
0009 #include "ec100.h"
0010 #include "mxl5005s.h"
0011 
0012 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
0013 
0014 static int ec168_ctrl_msg(struct dvb_usb_device *d, struct ec168_req *req)
0015 {
0016     int ret;
0017     unsigned int pipe;
0018     u8 request, requesttype;
0019     u8 *buf;
0020 
0021     switch (req->cmd) {
0022     case DOWNLOAD_FIRMWARE:
0023     case GPIO:
0024     case WRITE_I2C:
0025     case STREAMING_CTRL:
0026         requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
0027         request = req->cmd;
0028         break;
0029     case READ_I2C:
0030         requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
0031         request = req->cmd;
0032         break;
0033     case GET_CONFIG:
0034         requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
0035         request = CONFIG;
0036         break;
0037     case SET_CONFIG:
0038         requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
0039         request = CONFIG;
0040         break;
0041     case WRITE_DEMOD:
0042         requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
0043         request = DEMOD_RW;
0044         break;
0045     case READ_DEMOD:
0046         requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
0047         request = DEMOD_RW;
0048         break;
0049     default:
0050         dev_err(&d->udev->dev, "%s: unknown command=%02x\n",
0051                 KBUILD_MODNAME, req->cmd);
0052         ret = -EINVAL;
0053         goto error;
0054     }
0055 
0056     buf = kmalloc(req->size, GFP_KERNEL);
0057     if (!buf) {
0058         ret = -ENOMEM;
0059         goto error;
0060     }
0061 
0062     if (requesttype == (USB_TYPE_VENDOR | USB_DIR_OUT)) {
0063         /* write */
0064         memcpy(buf, req->data, req->size);
0065         pipe = usb_sndctrlpipe(d->udev, 0);
0066     } else {
0067         /* read */
0068         pipe = usb_rcvctrlpipe(d->udev, 0);
0069     }
0070 
0071     msleep(1); /* avoid I2C errors */
0072 
0073     ret = usb_control_msg(d->udev, pipe, request, requesttype, req->value,
0074         req->index, buf, req->size, EC168_USB_TIMEOUT);
0075 
0076     dvb_usb_dbg_usb_control_msg(d->udev, request, requesttype, req->value,
0077             req->index, buf, req->size);
0078 
0079     if (ret < 0)
0080         goto err_dealloc;
0081     else
0082         ret = 0;
0083 
0084     /* read request, copy returned data to return buf */
0085     if (!ret && requesttype == (USB_TYPE_VENDOR | USB_DIR_IN))
0086         memcpy(req->data, buf, req->size);
0087 
0088     kfree(buf);
0089     return ret;
0090 
0091 err_dealloc:
0092     kfree(buf);
0093 error:
0094     dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
0095     return ret;
0096 }
0097 
0098 /* I2C */
0099 static struct ec100_config ec168_ec100_config;
0100 
0101 static int ec168_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
0102     int num)
0103 {
0104     struct dvb_usb_device *d = i2c_get_adapdata(adap);
0105     struct ec168_req req;
0106     int i = 0;
0107     int ret;
0108 
0109     if (num > 2)
0110         return -EINVAL;
0111 
0112     if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
0113         return -EAGAIN;
0114 
0115     while (i < num) {
0116         if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) {
0117             if (msg[i].addr == ec168_ec100_config.demod_address) {
0118                 req.cmd = READ_DEMOD;
0119                 req.value = 0;
0120                 req.index = 0xff00 + msg[i].buf[0]; /* reg */
0121                 req.size = msg[i+1].len; /* bytes to read */
0122                 req.data = &msg[i+1].buf[0];
0123                 ret = ec168_ctrl_msg(d, &req);
0124                 i += 2;
0125             } else {
0126                 dev_err(&d->udev->dev, "%s: I2C read not " \
0127                         "implemented\n",
0128                         KBUILD_MODNAME);
0129                 ret = -EOPNOTSUPP;
0130                 i += 2;
0131             }
0132         } else {
0133             if (msg[i].addr == ec168_ec100_config.demod_address) {
0134                 req.cmd = WRITE_DEMOD;
0135                 req.value = msg[i].buf[1]; /* val */
0136                 req.index = 0xff00 + msg[i].buf[0]; /* reg */
0137                 req.size = 0;
0138                 req.data = NULL;
0139                 ret = ec168_ctrl_msg(d, &req);
0140                 i += 1;
0141             } else {
0142                 req.cmd = WRITE_I2C;
0143                 req.value = msg[i].buf[0]; /* val */
0144                 req.index = 0x0100 + msg[i].addr; /* I2C addr */
0145                 req.size = msg[i].len-1;
0146                 req.data = &msg[i].buf[1];
0147                 ret = ec168_ctrl_msg(d, &req);
0148                 i += 1;
0149             }
0150         }
0151         if (ret)
0152             goto error;
0153 
0154     }
0155     ret = i;
0156 
0157 error:
0158     mutex_unlock(&d->i2c_mutex);
0159     return ret;
0160 }
0161 
0162 static u32 ec168_i2c_func(struct i2c_adapter *adapter)
0163 {
0164     return I2C_FUNC_I2C;
0165 }
0166 
0167 static struct i2c_algorithm ec168_i2c_algo = {
0168     .master_xfer   = ec168_i2c_xfer,
0169     .functionality = ec168_i2c_func,
0170 };
0171 
0172 /* Callbacks for DVB USB */
0173 static int ec168_identify_state(struct dvb_usb_device *d, const char **name)
0174 {
0175     int ret;
0176     u8 reply;
0177     struct ec168_req req = {GET_CONFIG, 0, 1, sizeof(reply), &reply};
0178     dev_dbg(&d->udev->dev, "%s:\n", __func__);
0179 
0180     ret = ec168_ctrl_msg(d, &req);
0181     if (ret)
0182         goto error;
0183 
0184     dev_dbg(&d->udev->dev, "%s: reply=%02x\n", __func__, reply);
0185 
0186     if (reply == 0x01)
0187         ret = WARM;
0188     else
0189         ret = COLD;
0190 
0191     return ret;
0192 error:
0193     dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
0194     return ret;
0195 }
0196 
0197 static int ec168_download_firmware(struct dvb_usb_device *d,
0198         const struct firmware *fw)
0199 {
0200     int ret, len, remaining;
0201     struct ec168_req req = {DOWNLOAD_FIRMWARE, 0, 0, 0, NULL};
0202     dev_dbg(&d->udev->dev, "%s:\n", __func__);
0203 
0204     #define LEN_MAX 2048 /* max packet size */
0205     for (remaining = fw->size; remaining > 0; remaining -= LEN_MAX) {
0206         len = remaining;
0207         if (len > LEN_MAX)
0208             len = LEN_MAX;
0209 
0210         req.size = len;
0211         req.data = (u8 *) &fw->data[fw->size - remaining];
0212         req.index = fw->size - remaining;
0213 
0214         ret = ec168_ctrl_msg(d, &req);
0215         if (ret) {
0216             dev_err(&d->udev->dev,
0217                     "%s: firmware download failed=%d\n",
0218                     KBUILD_MODNAME, ret);
0219             goto error;
0220         }
0221     }
0222 
0223     req.size = 0;
0224 
0225     /* set "warm"? */
0226     req.cmd = SET_CONFIG;
0227     req.value = 0;
0228     req.index = 0x0001;
0229     ret = ec168_ctrl_msg(d, &req);
0230     if (ret)
0231         goto error;
0232 
0233     /* really needed - no idea what does */
0234     req.cmd = GPIO;
0235     req.value = 0;
0236     req.index = 0x0206;
0237     ret = ec168_ctrl_msg(d, &req);
0238     if (ret)
0239         goto error;
0240 
0241     /* activate tuner I2C? */
0242     req.cmd = WRITE_I2C;
0243     req.value = 0;
0244     req.index = 0x00c6;
0245     ret = ec168_ctrl_msg(d, &req);
0246     if (ret)
0247         goto error;
0248 
0249     return ret;
0250 error:
0251     dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
0252     return ret;
0253 }
0254 
0255 static struct ec100_config ec168_ec100_config = {
0256     .demod_address = 0xff, /* not real address, demod is integrated */
0257 };
0258 
0259 static int ec168_ec100_frontend_attach(struct dvb_usb_adapter *adap)
0260 {
0261     struct dvb_usb_device *d = adap_to_d(adap);
0262     dev_dbg(&d->udev->dev, "%s:\n", __func__);
0263 
0264     adap->fe[0] = dvb_attach(ec100_attach, &ec168_ec100_config,
0265             &d->i2c_adap);
0266     if (adap->fe[0] == NULL)
0267         return -ENODEV;
0268 
0269     return 0;
0270 }
0271 
0272 static struct mxl5005s_config ec168_mxl5003s_config = {
0273     .i2c_address     = 0xc6,
0274     .if_freq         = IF_FREQ_4570000HZ,
0275     .xtal_freq       = CRYSTAL_FREQ_16000000HZ,
0276     .agc_mode        = MXL_SINGLE_AGC,
0277     .tracking_filter = MXL_TF_OFF,
0278     .rssi_enable     = MXL_RSSI_ENABLE,
0279     .cap_select      = MXL_CAP_SEL_ENABLE,
0280     .div_out         = MXL_DIV_OUT_4,
0281     .clock_out       = MXL_CLOCK_OUT_DISABLE,
0282     .output_load     = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
0283     .top         = MXL5005S_TOP_25P2,
0284     .mod_mode        = MXL_DIGITAL_MODE,
0285     .if_mode         = MXL_ZERO_IF,
0286     .AgcMasterByte   = 0x00,
0287 };
0288 
0289 static int ec168_mxl5003s_tuner_attach(struct dvb_usb_adapter *adap)
0290 {
0291     struct dvb_usb_device *d = adap_to_d(adap);
0292     dev_dbg(&d->udev->dev, "%s:\n", __func__);
0293 
0294     return dvb_attach(mxl5005s_attach, adap->fe[0], &d->i2c_adap,
0295             &ec168_mxl5003s_config) == NULL ? -ENODEV : 0;
0296 }
0297 
0298 static int ec168_streaming_ctrl(struct dvb_frontend *fe, int onoff)
0299 {
0300     struct dvb_usb_device *d = fe_to_d(fe);
0301     struct ec168_req req = {STREAMING_CTRL, 0x7f01, 0x0202, 0, NULL};
0302     dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
0303 
0304     if (onoff)
0305         req.index = 0x0102;
0306     return ec168_ctrl_msg(d, &req);
0307 }
0308 
0309 /* DVB USB Driver stuff */
0310 /* bInterfaceNumber 0 is HID
0311  * bInterfaceNumber 1 is DVB-T */
0312 static const struct dvb_usb_device_properties ec168_props = {
0313     .driver_name = KBUILD_MODNAME,
0314     .owner = THIS_MODULE,
0315     .adapter_nr = adapter_nr,
0316     .bInterfaceNumber = 1,
0317 
0318     .identify_state = ec168_identify_state,
0319     .firmware = EC168_FIRMWARE,
0320     .download_firmware = ec168_download_firmware,
0321 
0322     .i2c_algo = &ec168_i2c_algo,
0323     .frontend_attach = ec168_ec100_frontend_attach,
0324     .tuner_attach = ec168_mxl5003s_tuner_attach,
0325     .streaming_ctrl = ec168_streaming_ctrl,
0326 
0327     .num_adapters = 1,
0328     .adapter = {
0329         {
0330             .stream = DVB_USB_STREAM_BULK(0x82, 6, 32 * 512),
0331         }
0332     },
0333 };
0334 
0335 static const struct usb_device_id ec168_id[] = {
0336     { DVB_USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168,
0337              &ec168_props, "E3C EC168 reference design", NULL)},
0338     { DVB_USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_2,
0339              &ec168_props, "E3C EC168 reference design", NULL)},
0340     { DVB_USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_3,
0341              &ec168_props, "E3C EC168 reference design", NULL)},
0342     { DVB_USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_4,
0343              &ec168_props, "E3C EC168 reference design", NULL)},
0344     { DVB_USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_5,
0345              &ec168_props, "E3C EC168 reference design", NULL)},
0346     {}
0347 };
0348 MODULE_DEVICE_TABLE(usb, ec168_id);
0349 
0350 static struct usb_driver ec168_driver = {
0351     .name = KBUILD_MODNAME,
0352     .id_table = ec168_id,
0353     .probe = dvb_usbv2_probe,
0354     .disconnect = dvb_usbv2_disconnect,
0355     .suspend = dvb_usbv2_suspend,
0356     .resume = dvb_usbv2_resume,
0357     .no_dynamic_id = 1,
0358     .soft_unbind = 1,
0359 };
0360 
0361 module_usb_driver(ec168_driver);
0362 
0363 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
0364 MODULE_DESCRIPTION("E3C EC168 driver");
0365 MODULE_LICENSE("GPL");
0366 MODULE_FIRMWARE(EC168_FIRMWARE);