Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* DVB USB compliant linux driver for GL861 USB2.0 devices.
0003  *
0004  * see Documentation/driver-api/media/drivers/dvb-usb.rst for more information
0005  */
0006 #include <linux/string.h>
0007 
0008 #include "dvb_usb.h"
0009 
0010 #include "zl10353.h"
0011 #include "qt1010.h"
0012 #include "tc90522.h"
0013 #include "dvb-pll.h"
0014 
0015 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
0016 
0017 struct gl861 {
0018     /* USB control message buffer */
0019     u8 buf[16];
0020 
0021     struct i2c_adapter *demod_sub_i2c;
0022     struct i2c_client  *i2c_client_demod;
0023     struct i2c_client  *i2c_client_tuner;
0024 };
0025 
0026 #define CMD_WRITE_SHORT     0x01
0027 #define CMD_READ            0x02
0028 #define CMD_WRITE           0x03
0029 
0030 static int gl861_ctrl_msg(struct dvb_usb_device *d, u8 request, u16 value,
0031               u16 index, void *data, u16 size)
0032 {
0033     struct gl861 *ctx = d_to_priv(d);
0034     struct usb_interface *intf = d->intf;
0035     int ret;
0036     unsigned int pipe;
0037     u8 requesttype;
0038 
0039     mutex_lock(&d->usb_mutex);
0040 
0041     switch (request) {
0042     case CMD_WRITE:
0043         memcpy(ctx->buf, data, size);
0044         fallthrough;
0045     case CMD_WRITE_SHORT:
0046         pipe = usb_sndctrlpipe(d->udev, 0);
0047         requesttype = USB_TYPE_VENDOR | USB_DIR_OUT;
0048         break;
0049     case CMD_READ:
0050         pipe = usb_rcvctrlpipe(d->udev, 0);
0051         requesttype = USB_TYPE_VENDOR | USB_DIR_IN;
0052         break;
0053     default:
0054         ret = -EINVAL;
0055         goto err_mutex_unlock;
0056     }
0057 
0058     ret = usb_control_msg(d->udev, pipe, request, requesttype, value,
0059                   index, ctx->buf, size, 200);
0060     dev_dbg(&intf->dev, "%d | %02x %02x %*ph %*ph %*ph %s %*ph\n",
0061         ret, requesttype, request, 2, &value, 2, &index, 2, &size,
0062         (requesttype & USB_DIR_IN) ? "<<<" : ">>>", size, ctx->buf);
0063     if (ret < 0)
0064         goto err_mutex_unlock;
0065 
0066     if (request == CMD_READ)
0067         memcpy(data, ctx->buf, size);
0068 
0069     usleep_range(1000, 2000); /* Avoid I2C errors */
0070 
0071     mutex_unlock(&d->usb_mutex);
0072 
0073     return 0;
0074 
0075 err_mutex_unlock:
0076     mutex_unlock(&d->usb_mutex);
0077     dev_dbg(&intf->dev, "failed %d\n", ret);
0078     return ret;
0079 }
0080 
0081 static int gl861_short_write(struct dvb_usb_device *d, u8 addr, u8 reg, u8 val)
0082 {
0083     return gl861_ctrl_msg(d, CMD_WRITE_SHORT,
0084                   (addr << 9) | val, reg, NULL, 0);
0085 }
0086 
0087 static int gl861_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
0088                  int num)
0089 {
0090     struct dvb_usb_device *d = i2c_get_adapdata(adap);
0091     struct usb_interface *intf = d->intf;
0092     struct gl861 *ctx = d_to_priv(d);
0093     int ret;
0094     u8 request, *data;
0095     u16 value, index, size;
0096 
0097     /* XXX: I2C adapter maximum data lengths are not tested */
0098     if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
0099         /* I2C write */
0100         if (msg[0].len < 2 || msg[0].len > sizeof(ctx->buf)) {
0101             ret = -EOPNOTSUPP;
0102             goto err;
0103         }
0104 
0105         value = (msg[0].addr << 1) << 8;
0106         index = msg[0].buf[0];
0107 
0108         if (msg[0].len == 2) {
0109             request = CMD_WRITE_SHORT;
0110             value |= msg[0].buf[1];
0111             size = 0;
0112             data = NULL;
0113         } else {
0114             request = CMD_WRITE;
0115             size = msg[0].len - 1;
0116             data = &msg[0].buf[1];
0117         }
0118 
0119         ret = gl861_ctrl_msg(d, request, value, index, data, size);
0120     } else if (num == 2 && !(msg[0].flags & I2C_M_RD) &&
0121            (msg[1].flags & I2C_M_RD)) {
0122         /* I2C write + read */
0123         if (msg[0].len > 1 || msg[1].len > sizeof(ctx->buf)) {
0124             ret = -EOPNOTSUPP;
0125             goto err;
0126         }
0127 
0128         value = (msg[0].addr << 1) << 8;
0129         index = msg[0].buf[0];
0130         request = CMD_READ;
0131 
0132         ret = gl861_ctrl_msg(d, request, value, index,
0133                      msg[1].buf, msg[1].len);
0134     } else if (num == 1 && (msg[0].flags & I2C_M_RD)) {
0135         /* I2C read */
0136         if (msg[0].len > sizeof(ctx->buf)) {
0137             ret = -EOPNOTSUPP;
0138             goto err;
0139         }
0140         value = (msg[0].addr << 1) << 8;
0141         index = 0x0100;
0142         request = CMD_READ;
0143 
0144         ret = gl861_ctrl_msg(d, request, value, index,
0145                      msg[0].buf, msg[0].len);
0146     } else {
0147         /* Unsupported I2C message */
0148         dev_dbg(&intf->dev, "unknown i2c msg, num %u\n", num);
0149         ret = -EOPNOTSUPP;
0150     }
0151     if (ret)
0152         goto err;
0153 
0154     return num;
0155 err:
0156     dev_dbg(&intf->dev, "failed %d\n", ret);
0157     return ret;
0158 }
0159 
0160 static u32 gl861_i2c_functionality(struct i2c_adapter *adapter)
0161 {
0162     return I2C_FUNC_I2C;
0163 }
0164 
0165 static struct i2c_algorithm gl861_i2c_algo = {
0166     .master_xfer   = gl861_i2c_master_xfer,
0167     .functionality = gl861_i2c_functionality,
0168 };
0169 
0170 /* Callbacks for DVB USB */
0171 static struct zl10353_config gl861_zl10353_config = {
0172     .demod_address = 0x0f,
0173     .no_tuner = 1,
0174     .parallel_ts = 1,
0175 };
0176 
0177 static int gl861_frontend_attach(struct dvb_usb_adapter *adap)
0178 {
0179 
0180     adap->fe[0] = dvb_attach(zl10353_attach, &gl861_zl10353_config,
0181         &adap_to_d(adap)->i2c_adap);
0182     if (adap->fe[0] == NULL)
0183         return -EIO;
0184 
0185     return 0;
0186 }
0187 
0188 static struct qt1010_config gl861_qt1010_config = {
0189     .i2c_address = 0x62
0190 };
0191 
0192 static int gl861_tuner_attach(struct dvb_usb_adapter *adap)
0193 {
0194     return dvb_attach(qt1010_attach,
0195               adap->fe[0], &adap_to_d(adap)->i2c_adap,
0196               &gl861_qt1010_config) == NULL ? -ENODEV : 0;
0197 }
0198 
0199 static int gl861_init(struct dvb_usb_device *d)
0200 {
0201     /*
0202      * There is 2 interfaces. Interface 0 is for TV and interface 1 is
0203      * for HID remote controller. Interface 0 has 2 alternate settings.
0204      * For some reason we need to set interface explicitly, defaulted
0205      * as alternate setting 1?
0206      */
0207     return usb_set_interface(d->udev, 0, 0);
0208 }
0209 
0210 /* DVB USB Driver stuff */
0211 static struct dvb_usb_device_properties gl861_props = {
0212     .driver_name = KBUILD_MODNAME,
0213     .owner = THIS_MODULE,
0214     .adapter_nr = adapter_nr,
0215 
0216     .size_of_priv = sizeof(struct gl861),
0217 
0218     .i2c_algo = &gl861_i2c_algo,
0219     .frontend_attach = gl861_frontend_attach,
0220     .tuner_attach = gl861_tuner_attach,
0221     .init = gl861_init,
0222 
0223     .num_adapters = 1,
0224     .adapter = {
0225         {
0226             .stream = DVB_USB_STREAM_BULK(0x81, 7, 512),
0227         }
0228     }
0229 };
0230 
0231 
0232 /*
0233  * For Friio
0234  */
0235 struct friio_config {
0236     struct i2c_board_info demod_info;
0237     struct tc90522_config demod_cfg;
0238 
0239     struct i2c_board_info tuner_info;
0240     struct dvb_pll_config tuner_cfg;
0241 };
0242 
0243 static const struct friio_config friio_config = {
0244     .demod_info = { I2C_BOARD_INFO(TC90522_I2C_DEV_TER, 0x18), },
0245     .demod_cfg = { .split_tuner_read_i2c = true, },
0246     .tuner_info = { I2C_BOARD_INFO("tua6034_friio", 0x60), },
0247 };
0248 
0249 
0250 /* GPIO control in Friio */
0251 
0252 #define FRIIO_CTL_LNB (1 << 0)
0253 #define FRIIO_CTL_STROBE (1 << 1)
0254 #define FRIIO_CTL_CLK (1 << 2)
0255 #define FRIIO_CTL_LED (1 << 3)
0256 
0257 #define FRIIO_LED_RUNNING 0x6400ff64
0258 #define FRIIO_LED_STOPPED 0x96ff00ff
0259 
0260 /* control PIC16F676 attached to Friio */
0261 static int friio_ext_ctl(struct dvb_usb_device *d,
0262                 u32 sat_color, int power_on)
0263 {
0264     int i, ret;
0265     struct i2c_msg msg;
0266     u8 *buf;
0267     u32 mask;
0268     u8 power = (power_on) ? FRIIO_CTL_LNB : 0;
0269 
0270     buf = kmalloc(2, GFP_KERNEL);
0271     if (!buf)
0272         return -ENOMEM;
0273 
0274     msg.addr = 0x00;
0275     msg.flags = 0;
0276     msg.len = 2;
0277     msg.buf = buf;
0278     buf[0] = 0x00;
0279 
0280     /* send 2bit header (&B10) */
0281     buf[1] = power | FRIIO_CTL_LED | FRIIO_CTL_STROBE;
0282     ret = i2c_transfer(&d->i2c_adap, &msg, 1);
0283     buf[1] |= FRIIO_CTL_CLK;
0284     ret += i2c_transfer(&d->i2c_adap, &msg, 1);
0285 
0286     buf[1] = power | FRIIO_CTL_STROBE;
0287     ret += i2c_transfer(&d->i2c_adap, &msg, 1);
0288     buf[1] |= FRIIO_CTL_CLK;
0289     ret += i2c_transfer(&d->i2c_adap, &msg, 1);
0290 
0291     /* send 32bit(satur, R, G, B) data in serial */
0292     mask = 1UL << 31;
0293     for (i = 0; i < 32; i++) {
0294         buf[1] = power | FRIIO_CTL_STROBE;
0295         if (sat_color & mask)
0296             buf[1] |= FRIIO_CTL_LED;
0297         ret += i2c_transfer(&d->i2c_adap, &msg, 1);
0298         buf[1] |= FRIIO_CTL_CLK;
0299         ret += i2c_transfer(&d->i2c_adap, &msg, 1);
0300         mask >>= 1;
0301     }
0302 
0303     /* set the strobe off */
0304     buf[1] = power;
0305     ret += i2c_transfer(&d->i2c_adap, &msg, 1);
0306     buf[1] |= FRIIO_CTL_CLK;
0307     ret += i2c_transfer(&d->i2c_adap, &msg, 1);
0308 
0309     kfree(buf);
0310     return (ret == 70) ? 0 : -EREMOTEIO;
0311 }
0312 
0313 /* init/config of gl861 for Friio */
0314 /* NOTE:
0315  * This function cannot be moved to friio_init()/dvb_usbv2_init(),
0316  * because the init defined here includes a whole device reset,
0317  * it must be run early before any activities like I2C,
0318  * but friio_init() is called by dvb-usbv2 after {_frontend, _tuner}_attach(),
0319  * where I2C communication is used.
0320  * In addition, this reset is required in reset_resume() as well.
0321  * Thus this function is set to be called from _power_ctl().
0322  *
0323  * Since it will be called on the early init stage
0324  * where the i2c adapter is not initialized yet,
0325  * we cannot use i2c_transfer() here.
0326  */
0327 static int friio_reset(struct dvb_usb_device *d)
0328 {
0329     int i, ret;
0330     u8 wbuf[1], rbuf[2];
0331 
0332     static const u8 friio_init_cmds[][2] = {
0333         {0x33, 0x08}, {0x37, 0x40}, {0x3a, 0x1f}, {0x3b, 0xff},
0334         {0x3c, 0x1f}, {0x3d, 0xff}, {0x38, 0x00}, {0x35, 0x00},
0335         {0x39, 0x00}, {0x36, 0x00},
0336     };
0337 
0338     ret = usb_set_interface(d->udev, 0, 0);
0339     if (ret < 0)
0340         return ret;
0341 
0342     ret = gl861_short_write(d, 0x00, 0x11, 0x02);
0343     if (ret < 0)
0344         return ret;
0345     usleep_range(2000, 3000);
0346 
0347     ret = gl861_short_write(d, 0x00, 0x11, 0x00);
0348     if (ret < 0)
0349         return ret;
0350 
0351     /*
0352      * Check if the dev is really a Friio White, since it might be
0353      * another device, Friio Black, with the same VID/PID.
0354      */
0355 
0356     usleep_range(1000, 2000);
0357     wbuf[0] = 0x80;
0358     ret = gl861_ctrl_msg(d, CMD_WRITE, 0x09 << 9, 0x03, wbuf, 1);
0359     if (ret < 0)
0360         return ret;
0361 
0362     usleep_range(2000, 3000);
0363     ret = gl861_ctrl_msg(d, CMD_READ, 0x09 << 9, 0x0100, rbuf, 2);
0364     if (ret < 0)
0365         return ret;
0366     if (rbuf[0] != 0xff || rbuf[1] != 0xff)
0367         return -ENODEV;
0368 
0369 
0370     usleep_range(1000, 2000);
0371     wbuf[0] = 0x80;
0372     ret = gl861_ctrl_msg(d, CMD_WRITE, 0x48 << 9, 0x03, wbuf, 1);
0373     if (ret < 0)
0374         return ret;
0375 
0376     usleep_range(2000, 3000);
0377     ret = gl861_ctrl_msg(d, CMD_READ, 0x48 << 9, 0x0100, rbuf, 2);
0378     if (ret < 0)
0379         return ret;
0380     if (rbuf[0] != 0xff || rbuf[1] != 0xff)
0381         return -ENODEV;
0382 
0383     ret = gl861_short_write(d, 0x00, 0x30, 0x04);
0384     if (ret < 0)
0385         return ret;
0386 
0387     ret = gl861_short_write(d, 0x00, 0x00, 0x01);
0388     if (ret < 0)
0389         return ret;
0390 
0391     ret = gl861_short_write(d, 0x00, 0x06, 0x0f);
0392     if (ret < 0)
0393         return ret;
0394 
0395     for (i = 0; i < ARRAY_SIZE(friio_init_cmds); i++) {
0396         ret = gl861_short_write(d, 0x00, friio_init_cmds[i][0],
0397                     friio_init_cmds[i][1]);
0398         if (ret < 0)
0399             return ret;
0400     }
0401     return 0;
0402 }
0403 
0404 /*
0405  * DVB callbacks for Friio
0406  */
0407 
0408 static int friio_power_ctrl(struct dvb_usb_device *d, int onoff)
0409 {
0410     return onoff ? friio_reset(d) : 0;
0411 }
0412 
0413 static int friio_frontend_attach(struct dvb_usb_adapter *adap)
0414 {
0415     const struct i2c_board_info *info;
0416     struct dvb_usb_device *d;
0417     struct tc90522_config cfg;
0418     struct i2c_client *cl;
0419     struct gl861 *priv;
0420 
0421     info = &friio_config.demod_info;
0422     cfg = friio_config.demod_cfg;
0423     d = adap_to_d(adap);
0424     cl = dvb_module_probe("tc90522", info->type,
0425                   &d->i2c_adap, info->addr, &cfg);
0426     if (!cl)
0427         return -ENODEV;
0428     adap->fe[0] = cfg.fe;
0429 
0430     priv = adap_to_priv(adap);
0431     priv->i2c_client_demod = cl;
0432     priv->demod_sub_i2c = cfg.tuner_i2c;
0433     return 0;
0434 }
0435 
0436 static int friio_frontend_detach(struct dvb_usb_adapter *adap)
0437 {
0438     struct gl861 *priv;
0439 
0440     priv = adap_to_priv(adap);
0441     dvb_module_release(priv->i2c_client_demod);
0442     return 0;
0443 }
0444 
0445 static int friio_tuner_attach(struct dvb_usb_adapter *adap)
0446 {
0447     const struct i2c_board_info *info;
0448     struct dvb_pll_config cfg;
0449     struct i2c_client *cl;
0450     struct gl861 *priv;
0451 
0452     priv = adap_to_priv(adap);
0453     info = &friio_config.tuner_info;
0454     cfg = friio_config.tuner_cfg;
0455     cfg.fe = adap->fe[0];
0456 
0457     cl = dvb_module_probe("dvb_pll", info->type,
0458                   priv->demod_sub_i2c, info->addr, &cfg);
0459     if (!cl)
0460         return -ENODEV;
0461     priv->i2c_client_tuner = cl;
0462     return 0;
0463 }
0464 
0465 static int friio_tuner_detach(struct dvb_usb_adapter *adap)
0466 {
0467     struct gl861 *priv;
0468 
0469     priv = adap_to_priv(adap);
0470     dvb_module_release(priv->i2c_client_tuner);
0471     return 0;
0472 }
0473 
0474 static int friio_init(struct dvb_usb_device *d)
0475 {
0476     int i;
0477     int ret;
0478     struct gl861 *priv;
0479 
0480     static const u8 demod_init[][2] = {
0481         {0x01, 0x40}, {0x04, 0x38}, {0x05, 0x40}, {0x07, 0x40},
0482         {0x0f, 0x4f}, {0x11, 0x21}, {0x12, 0x0b}, {0x13, 0x2f},
0483         {0x14, 0x31}, {0x16, 0x02}, {0x21, 0xc4}, {0x22, 0x20},
0484         {0x2c, 0x79}, {0x2d, 0x34}, {0x2f, 0x00}, {0x30, 0x28},
0485         {0x31, 0x31}, {0x32, 0xdf}, {0x38, 0x01}, {0x39, 0x78},
0486         {0x3b, 0x33}, {0x3c, 0x33}, {0x48, 0x90}, {0x51, 0x68},
0487         {0x5e, 0x38}, {0x71, 0x00}, {0x72, 0x08}, {0x77, 0x00},
0488         {0xc0, 0x21}, {0xc1, 0x10}, {0xe4, 0x1a}, {0xea, 0x1f},
0489         {0x77, 0x00}, {0x71, 0x00}, {0x71, 0x00}, {0x76, 0x0c},
0490     };
0491 
0492     /* power on LNA? */
0493     ret = friio_ext_ctl(d, FRIIO_LED_STOPPED, true);
0494     if (ret < 0)
0495         return ret;
0496     msleep(20);
0497 
0498     /* init/config demod */
0499     priv = d_to_priv(d);
0500     for (i = 0; i < ARRAY_SIZE(demod_init); i++) {
0501         int ret;
0502 
0503         ret = i2c_master_send(priv->i2c_client_demod, demod_init[i], 2);
0504         if (ret < 0)
0505             return ret;
0506     }
0507     msleep(100);
0508     return 0;
0509 }
0510 
0511 static void friio_exit(struct dvb_usb_device *d)
0512 {
0513     friio_ext_ctl(d, FRIIO_LED_STOPPED, false);
0514 }
0515 
0516 static int friio_streaming_ctrl(struct dvb_frontend *fe, int onoff)
0517 {
0518     u32 led_color;
0519 
0520     led_color = onoff ? FRIIO_LED_RUNNING : FRIIO_LED_STOPPED;
0521     return friio_ext_ctl(fe_to_d(fe), led_color, true);
0522 }
0523 
0524 
0525 static struct dvb_usb_device_properties friio_props = {
0526     .driver_name = KBUILD_MODNAME,
0527     .owner = THIS_MODULE,
0528     .adapter_nr = adapter_nr,
0529 
0530     .size_of_priv = sizeof(struct gl861),
0531 
0532     .i2c_algo = &gl861_i2c_algo,
0533     .power_ctrl = friio_power_ctrl,
0534     .frontend_attach = friio_frontend_attach,
0535     .frontend_detach = friio_frontend_detach,
0536     .tuner_attach = friio_tuner_attach,
0537     .tuner_detach = friio_tuner_detach,
0538     .init = friio_init,
0539     .exit = friio_exit,
0540     .streaming_ctrl = friio_streaming_ctrl,
0541 
0542     .num_adapters = 1,
0543     .adapter = {
0544         {
0545             .stream = DVB_USB_STREAM_BULK(0x01, 8, 16384),
0546         }
0547     }
0548 };
0549 
0550 static const struct usb_device_id gl861_id_table[] = {
0551     { DVB_USB_DEVICE(USB_VID_MSI, USB_PID_MSI_MEGASKY580_55801,
0552         &gl861_props, "MSI Mega Sky 55801 DVB-T USB2.0", NULL) },
0553     { DVB_USB_DEVICE(USB_VID_ALINK, USB_PID_ALINK_DTU,
0554         &gl861_props, "A-LINK DTU DVB-T USB2.0", NULL) },
0555     { DVB_USB_DEVICE(USB_VID_774, USB_PID_FRIIO_WHITE,
0556         &friio_props, "774 Friio White ISDB-T USB2.0", NULL) },
0557     { }
0558 };
0559 MODULE_DEVICE_TABLE(usb, gl861_id_table);
0560 
0561 static struct usb_driver gl861_usb_driver = {
0562     .name = KBUILD_MODNAME,
0563     .id_table = gl861_id_table,
0564     .probe = dvb_usbv2_probe,
0565     .disconnect = dvb_usbv2_disconnect,
0566     .suspend = dvb_usbv2_suspend,
0567     .resume = dvb_usbv2_resume,
0568     .reset_resume = dvb_usbv2_reset_resume,
0569     .no_dynamic_id = 1,
0570     .soft_unbind = 1,
0571 };
0572 
0573 module_usb_driver(gl861_usb_driver);
0574 
0575 MODULE_AUTHOR("Carl Lundqvist <comabug@gmail.com>");
0576 MODULE_DESCRIPTION("Driver MSI Mega Sky 580 DVB-T USB2.0 / GL861");
0577 MODULE_VERSION("0.1");
0578 MODULE_LICENSE("GPL");