11541Srgrimes// SPDX-License-Identifier: GPL-2.0-or-later 21541Srgrimes/* 31541Srgrimes * E3C EC168 DVB USB driver 41541Srgrimes * 51541Srgrimes * Copyright (C) 2009 Antti Palosaari <crope@iki.fi> 61541Srgrimes */ 71541Srgrimes 81541Srgrimes#include "ec168.h" 91541Srgrimes#include "ec100.h" 101541Srgrimes#include "mxl5005s.h" 111541Srgrimes 121541SrgrimesDVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); 131541Srgrimes 141541Srgrimesstatic int ec168_ctrl_msg(struct dvb_usb_device *d, struct ec168_req *req) 151541Srgrimes{ 161541Srgrimes int ret; 171541Srgrimes unsigned int pipe; 181541Srgrimes u8 request, requesttype; 191541Srgrimes u8 *buf; 201541Srgrimes 211541Srgrimes switch (req->cmd) { 221541Srgrimes case DOWNLOAD_FIRMWARE: 231541Srgrimes case GPIO: 241541Srgrimes case WRITE_I2C: 251541Srgrimes case STREAMING_CTRL: 261541Srgrimes requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT); 271541Srgrimes request = req->cmd; 281541Srgrimes break; 291541Srgrimes case READ_I2C: 301541Srgrimes requesttype = (USB_TYPE_VENDOR | USB_DIR_IN); 311541Srgrimes request = req->cmd; 321541Srgrimes break; 331541Srgrimes case GET_CONFIG: 3450477Speter requesttype = (USB_TYPE_VENDOR | USB_DIR_IN); 351541Srgrimes request = CONFIG; 361541Srgrimes break; 371541Srgrimes case SET_CONFIG: 381541Srgrimes requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT); 391541Srgrimes request = CONFIG; 408876Srgrimes break; 418876Srgrimes case WRITE_DEMOD: 421541Srgrimes requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT); 431541Srgrimes request = DEMOD_RW; 441541Srgrimes break; 458876Srgrimes case READ_DEMOD: 461541Srgrimes requesttype = (USB_TYPE_VENDOR | USB_DIR_IN); 471541Srgrimes request = DEMOD_RW; 481541Srgrimes break; 491541Srgrimes default: 5020020Sbde dev_err(&d->udev->dev, "%s: unknown command=%02x\n", 511541Srgrimes KBUILD_MODNAME, req->cmd); 521541Srgrimes ret = -EINVAL; 531541Srgrimes goto error; 5420020Sbde } 558876Srgrimes 561541Srgrimes buf = kmalloc(req->size, GFP_KERNEL); 571541Srgrimes if (!buf) { 5820020Sbde ret = -ENOMEM; 5969322Sjkh goto error; 601541Srgrimes } 6169322Sjkh 621541Srgrimes if (requesttype == (USB_TYPE_VENDOR | USB_DIR_OUT)) { 631541Srgrimes /* write */ 641541Srgrimes memcpy(buf, req->data, req->size); 651541Srgrimes pipe = usb_sndctrlpipe(d->udev, 0); 6620020Sbde } else { 671541Srgrimes /* read */ 681541Srgrimes pipe = usb_rcvctrlpipe(d->udev, 0); 691541Srgrimes } 701541Srgrimes 711541Srgrimes msleep(1); /* avoid I2C errors */ 721541Srgrimes 731541Srgrimes ret = usb_control_msg(d->udev, pipe, request, requesttype, req->value, 741541Srgrimes req->index, buf, req->size, EC168_USB_TIMEOUT); 751541Srgrimes 761541Srgrimes dvb_usb_dbg_usb_control_msg(d->udev, request, requesttype, req->value, 7720020Sbde req->index, buf, req->size); 781541Srgrimes 791541Srgrimes if (ret < 0) 801541Srgrimes goto err_dealloc; 811541Srgrimes else 828449Sbde ret = 0; 831541Srgrimes 841541Srgrimes /* read request, copy returned data to return buf */ 858449Sbde if (!ret && requesttype == (USB_TYPE_VENDOR | USB_DIR_IN)) 861541Srgrimes memcpy(req->data, buf, req->size); 871541Srgrimes 881541Srgrimes kfree(buf); 891541Srgrimes return ret; 901541Srgrimes 911541Srgrimeserr_dealloc: 921541Srgrimes kfree(buf); 931541Srgrimeserror: 941541Srgrimes dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 951541Srgrimes return ret; 961541Srgrimes} 971541Srgrimes 981541Srgrimes/* I2C */ 991541Srgrimesstatic struct ec100_config ec168_ec100_config; 1001541Srgrimes 1011541Srgrimesstatic int ec168_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[], 1021541Srgrimes int num) 1031541Srgrimes{ 1041541Srgrimes struct dvb_usb_device *d = i2c_get_adapdata(adap); 1051541Srgrimes struct ec168_req req; 1061541Srgrimes int i = 0; 1071541Srgrimes int ret; 1081541Srgrimes 1091541Srgrimes if (num > 2) 1101541Srgrimes return -EINVAL; 1111541Srgrimes 1121541Srgrimes if (mutex_lock_interruptible(&d->i2c_mutex) < 0) 1131541Srgrimes return -EAGAIN; 1141541Srgrimes 11573421Sassar while (i < num) { 11673421Sassar if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) { 11773421Sassar if (msg[i].addr == ec168_ec100_config.demod_address) { 1181541Srgrimes if (msg[i].len < 1) { 1191541Srgrimes i = -EOPNOTSUPP; 1201541Srgrimes break; 1211541Srgrimes } 1221541Srgrimes req.cmd = READ_DEMOD; 1231541Srgrimes req.value = 0; 1241541Srgrimes req.index = 0xff00 + msg[i].buf[0]; /* reg */ 1251541Srgrimes req.size = msg[i+1].len; /* bytes to read */ 1261541Srgrimes req.data = &msg[i+1].buf[0]; 1271541Srgrimes ret = ec168_ctrl_msg(d, &req); 1281541Srgrimes i += 2; 1291541Srgrimes } else { 1301541Srgrimes dev_err(&d->udev->dev, "%s: I2C read not " \ 1311541Srgrimes "implemented\n", 1321541Srgrimes KBUILD_MODNAME); 1331541Srgrimes ret = -EOPNOTSUPP; 1341541Srgrimes i += 2; 1351541Srgrimes } 1361541Srgrimes } else { 1371541Srgrimes if (msg[i].addr == ec168_ec100_config.demod_address) { 1381541Srgrimes if (msg[i].len < 1) { 1391549Srgrimes i = -EOPNOTSUPP; 1401541Srgrimes break; 1418187Sbde } 1421549Srgrimes req.cmd = WRITE_DEMOD; 1438187Sbde req.value = msg[i].buf[1]; /* val */ 1448187Sbde req.index = 0xff00 + msg[i].buf[0]; /* reg */ 1451541Srgrimes req.size = 0; 1461541Srgrimes req.data = NULL; 1471541Srgrimes ret = ec168_ctrl_msg(d, &req); 1488876Srgrimes i += 1; 1491541Srgrimes } else { 1501541Srgrimes if (msg[i].len < 1) { 1511541Srgrimes i = -EOPNOTSUPP; 1521541Srgrimes break; 1531541Srgrimes } 1541541Srgrimes req.cmd = WRITE_I2C; 1551541Srgrimes req.value = msg[i].buf[0]; /* val */ 1561541Srgrimes req.index = 0x0100 + msg[i].addr; /* I2C addr */ 1571541Srgrimes req.size = msg[i].len-1; 1581541Srgrimes req.data = &msg[i].buf[1]; 1591541Srgrimes ret = ec168_ctrl_msg(d, &req); 1601541Srgrimes i += 1; 1611541Srgrimes } 1621541Srgrimes } 1631541Srgrimes if (ret) 1641541Srgrimes goto error; 1651541Srgrimes 1661541Srgrimes } 1671541Srgrimes ret = i; 1681541Srgrimes 1691541Srgrimeserror: 1701541Srgrimes mutex_unlock(&d->i2c_mutex); 1711541Srgrimes return ret; 1721541Srgrimes} 1731541Srgrimes 1741541Srgrimesstatic u32 ec168_i2c_func(struct i2c_adapter *adapter) 1751541Srgrimes{ 1761541Srgrimes return I2C_FUNC_I2C; 1771541Srgrimes} 1781541Srgrimes 1791541Srgrimesstatic struct i2c_algorithm ec168_i2c_algo = { 1801541Srgrimes .master_xfer = ec168_i2c_xfer, 1811541Srgrimes .functionality = ec168_i2c_func, 18235755Sjb}; 1831541Srgrimes 18435755Sjb/* Callbacks for DVB USB */ 1851541Srgrimesstatic int ec168_identify_state(struct dvb_usb_device *d, const char **name) 1861541Srgrimes{ 1871541Srgrimes int ret; 1881541Srgrimes u8 reply; 1891541Srgrimes struct ec168_req req = {GET_CONFIG, 0, 1, sizeof(reply), &reply}; 1901541Srgrimes dev_dbg(&d->udev->dev, "%s:\n", __func__); 1911541Srgrimes 1928187Sbde ret = ec168_ctrl_msg(d, &req); 1938187Sbde if (ret) 1941541Srgrimes goto error; 1951541Srgrimes 1968876Srgrimes dev_dbg(&d->udev->dev, "%s: reply=%02x\n", __func__, reply); 1971541Srgrimes 1981541Srgrimes if (reply == 0x01) 1991541Srgrimes ret = WARM; 2001541Srgrimes else 2011541Srgrimes ret = COLD; 2021541Srgrimes 2031541Srgrimes return ret; 2041541Srgrimeserror: 2051541Srgrimes dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 2061541Srgrimes return ret; 2071541Srgrimes} 2081541Srgrimes 2091541Srgrimesstatic int ec168_download_firmware(struct dvb_usb_device *d, 2101541Srgrimes const struct firmware *fw) 2111541Srgrimes{ 2121541Srgrimes int ret, len, remaining; 2131541Srgrimes struct ec168_req req = {DOWNLOAD_FIRMWARE, 0, 0, 0, NULL}; 2141541Srgrimes dev_dbg(&d->udev->dev, "%s:\n", __func__); 2151541Srgrimes 2161541Srgrimes #define LEN_MAX 2048 /* max packet size */ 2171541Srgrimes for (remaining = fw->size; remaining > 0; remaining -= LEN_MAX) { 2181541Srgrimes len = remaining; 2191541Srgrimes if (len > LEN_MAX) 2201541Srgrimes len = LEN_MAX; 2211541Srgrimes 2221541Srgrimes req.size = len; 2231541Srgrimes req.data = (u8 *) &fw->data[fw->size - remaining]; 2241541Srgrimes req.index = fw->size - remaining; 2251541Srgrimes 2261541Srgrimes ret = ec168_ctrl_msg(d, &req); 2271541Srgrimes if (ret) { 2281541Srgrimes dev_err(&d->udev->dev, 2291541Srgrimes "%s: firmware download failed=%d\n", 2301541Srgrimes KBUILD_MODNAME, ret); 2311541Srgrimes goto error; 2321541Srgrimes } 2331541Srgrimes } 2341541Srgrimes 2351541Srgrimes req.size = 0; 2361541Srgrimes 23755205Speter /* set "warm"? */ 2381541Srgrimes req.cmd = SET_CONFIG; 2391541Srgrimes req.value = 0; 2401541Srgrimes req.index = 0x0001; 2411541Srgrimes ret = ec168_ctrl_msg(d, &req); 2421541Srgrimes if (ret) 2431541Srgrimes goto error; 2441541Srgrimes 2451541Srgrimes /* really needed - no idea what does */ 2461541Srgrimes req.cmd = GPIO; 2471541Srgrimes req.value = 0; 2481541Srgrimes req.index = 0x0206; 2491541Srgrimes ret = ec168_ctrl_msg(d, &req); 2501541Srgrimes if (ret) 2511541Srgrimes goto error; 2521541Srgrimes 2531541Srgrimes /* activate tuner I2C? */ 2541541Srgrimes req.cmd = WRITE_I2C; 2551541Srgrimes req.value = 0; 2561541Srgrimes req.index = 0x00c6; 2571541Srgrimes ret = ec168_ctrl_msg(d, &req); 2581541Srgrimes if (ret) 2591541Srgrimes goto error; 2601541Srgrimes 2611541Srgrimes return ret; 2621541Srgrimeserror: 2631541Srgrimes dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret); 2641541Srgrimes return ret; 2651541Srgrimes} 2661541Srgrimes 26755205Speterstatic struct ec100_config ec168_ec100_config = { 2681541Srgrimes .demod_address = 0xff, /* not real address, demod is integrated */ 2691541Srgrimes}; 2701541Srgrimes 2711541Srgrimesstatic int ec168_ec100_frontend_attach(struct dvb_usb_adapter *adap) 2721541Srgrimes{ 2731541Srgrimes struct dvb_usb_device *d = adap_to_d(adap); 2741541Srgrimes dev_dbg(&d->udev->dev, "%s:\n", __func__); 2751541Srgrimes 2761541Srgrimes adap->fe[0] = dvb_attach(ec100_attach, &ec168_ec100_config, 2771541Srgrimes &d->i2c_adap); 2781541Srgrimes if (adap->fe[0] == NULL) 2791541Srgrimes return -ENODEV; 2801541Srgrimes 2811541Srgrimes return 0; 2821541Srgrimes} 2831541Srgrimes 2841541Srgrimesstatic struct mxl5005s_config ec168_mxl5003s_config = { 2851541Srgrimes .i2c_address = 0xc6, 2861541Srgrimes .if_freq = IF_FREQ_4570000HZ, 287 .xtal_freq = CRYSTAL_FREQ_16000000HZ, 288 .agc_mode = MXL_SINGLE_AGC, 289 .tracking_filter = MXL_TF_OFF, 290 .rssi_enable = MXL_RSSI_ENABLE, 291 .cap_select = MXL_CAP_SEL_ENABLE, 292 .div_out = MXL_DIV_OUT_4, 293 .clock_out = MXL_CLOCK_OUT_DISABLE, 294 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM, 295 .top = MXL5005S_TOP_25P2, 296 .mod_mode = MXL_DIGITAL_MODE, 297 .if_mode = MXL_ZERO_IF, 298 .AgcMasterByte = 0x00, 299}; 300 301static int ec168_mxl5003s_tuner_attach(struct dvb_usb_adapter *adap) 302{ 303 struct dvb_usb_device *d = adap_to_d(adap); 304 dev_dbg(&d->udev->dev, "%s:\n", __func__); 305 306 return dvb_attach(mxl5005s_attach, adap->fe[0], &d->i2c_adap, 307 &ec168_mxl5003s_config) == NULL ? -ENODEV : 0; 308} 309 310static int ec168_streaming_ctrl(struct dvb_frontend *fe, int onoff) 311{ 312 struct dvb_usb_device *d = fe_to_d(fe); 313 struct ec168_req req = {STREAMING_CTRL, 0x7f01, 0x0202, 0, NULL}; 314 dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff); 315 316 if (onoff) 317 req.index = 0x0102; 318 return ec168_ctrl_msg(d, &req); 319} 320 321/* DVB USB Driver stuff */ 322/* bInterfaceNumber 0 is HID 323 * bInterfaceNumber 1 is DVB-T */ 324static const struct dvb_usb_device_properties ec168_props = { 325 .driver_name = KBUILD_MODNAME, 326 .owner = THIS_MODULE, 327 .adapter_nr = adapter_nr, 328 .bInterfaceNumber = 1, 329 330 .identify_state = ec168_identify_state, 331 .firmware = EC168_FIRMWARE, 332 .download_firmware = ec168_download_firmware, 333 334 .i2c_algo = &ec168_i2c_algo, 335 .frontend_attach = ec168_ec100_frontend_attach, 336 .tuner_attach = ec168_mxl5003s_tuner_attach, 337 .streaming_ctrl = ec168_streaming_ctrl, 338 339 .num_adapters = 1, 340 .adapter = { 341 { 342 .stream = DVB_USB_STREAM_BULK(0x82, 6, 32 * 512), 343 } 344 }, 345}; 346 347static const struct usb_device_id ec168_id[] = { 348 { DVB_USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168, 349 &ec168_props, "E3C EC168 reference design", NULL)}, 350 { DVB_USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_2, 351 &ec168_props, "E3C EC168 reference design", NULL)}, 352 { DVB_USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_3, 353 &ec168_props, "E3C EC168 reference design", NULL)}, 354 { DVB_USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_4, 355 &ec168_props, "E3C EC168 reference design", NULL)}, 356 { DVB_USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_5, 357 &ec168_props, "E3C EC168 reference design", NULL)}, 358 {} 359}; 360MODULE_DEVICE_TABLE(usb, ec168_id); 361 362static struct usb_driver ec168_driver = { 363 .name = KBUILD_MODNAME, 364 .id_table = ec168_id, 365 .probe = dvb_usbv2_probe, 366 .disconnect = dvb_usbv2_disconnect, 367 .suspend = dvb_usbv2_suspend, 368 .resume = dvb_usbv2_resume, 369 .no_dynamic_id = 1, 370 .soft_unbind = 1, 371}; 372 373module_usb_driver(ec168_driver); 374 375MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); 376MODULE_DESCRIPTION("E3C EC168 driver"); 377MODULE_LICENSE("GPL"); 378MODULE_FIRMWARE(EC168_FIRMWARE); 379