ec168.c 9.3 KB

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