ec168.c 9.1 KB

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