ec168.c 8.7 KB

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