ec168.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. pr_err("%s: unknown command=%02x\n", KBUILD_MODNAME, req->cmd);
  61. ret = -EINVAL;
  62. goto error;
  63. }
  64. buf = kmalloc(req->size, GFP_KERNEL);
  65. if (!buf) {
  66. ret = -ENOMEM;
  67. goto error;
  68. }
  69. if (requesttype == (USB_TYPE_VENDOR | USB_DIR_OUT)) {
  70. /* write */
  71. memcpy(buf, req->data, req->size);
  72. pipe = usb_sndctrlpipe(d->udev, 0);
  73. } else {
  74. /* read */
  75. pipe = usb_rcvctrlpipe(d->udev, 0);
  76. }
  77. msleep(1); /* avoid I2C errors */
  78. ret = usb_control_msg(d->udev, pipe, request, requesttype, req->value,
  79. req->index, buf, req->size, EC168_USB_TIMEOUT);
  80. dvb_usb_dbg_usb_control_msg(d->udev, request, requesttype, req->value,
  81. req->index, buf, req->size);
  82. if (ret < 0)
  83. goto err_dealloc;
  84. else
  85. ret = 0;
  86. /* read request, copy returned data to return buf */
  87. if (!ret && requesttype == (USB_TYPE_VENDOR | USB_DIR_IN))
  88. memcpy(req->data, buf, req->size);
  89. kfree(buf);
  90. return ret;
  91. err_dealloc:
  92. kfree(buf);
  93. error:
  94. pr_debug("%s: failed=%d\n", __func__, ret);
  95. return ret;
  96. }
  97. /* I2C */
  98. static struct ec100_config ec168_ec100_config;
  99. static int ec168_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
  100. int num)
  101. {
  102. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  103. struct ec168_req req;
  104. int i = 0;
  105. int ret;
  106. if (num > 2)
  107. return -EINVAL;
  108. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  109. return -EAGAIN;
  110. while (i < num) {
  111. if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) {
  112. if (msg[i].addr == ec168_ec100_config.demod_address) {
  113. req.cmd = READ_DEMOD;
  114. req.value = 0;
  115. req.index = 0xff00 + msg[i].buf[0]; /* reg */
  116. req.size = msg[i+1].len; /* bytes to read */
  117. req.data = &msg[i+1].buf[0];
  118. ret = ec168_ctrl_msg(d, &req);
  119. i += 2;
  120. } else {
  121. pr_err("%s: I2C read not implemented\n",
  122. KBUILD_MODNAME);
  123. ret = -EOPNOTSUPP;
  124. i += 2;
  125. }
  126. } else {
  127. if (msg[i].addr == ec168_ec100_config.demod_address) {
  128. req.cmd = WRITE_DEMOD;
  129. req.value = msg[i].buf[1]; /* val */
  130. req.index = 0xff00 + msg[i].buf[0]; /* reg */
  131. req.size = 0;
  132. req.data = NULL;
  133. ret = ec168_ctrl_msg(d, &req);
  134. i += 1;
  135. } else {
  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 i;
  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. pr_debug("%s:\n", __func__);
  168. ret = ec168_ctrl_msg(d, &req);
  169. if (ret)
  170. goto error;
  171. pr_debug("%s: reply=%02x\n", __func__, reply);
  172. if (reply == 0x01)
  173. ret = WARM;
  174. else
  175. ret = COLD;
  176. return ret;
  177. error:
  178. pr_debug("%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. pr_debug("%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. pr_err("%s: firmware download failed=%d\n",
  198. KBUILD_MODNAME, ret);
  199. goto error;
  200. }
  201. }
  202. req.size = 0;
  203. /* set "warm"? */
  204. req.cmd = SET_CONFIG;
  205. req.value = 0;
  206. req.index = 0x0001;
  207. ret = ec168_ctrl_msg(d, &req);
  208. if (ret)
  209. goto error;
  210. /* really needed - no idea what does */
  211. req.cmd = GPIO;
  212. req.value = 0;
  213. req.index = 0x0206;
  214. ret = ec168_ctrl_msg(d, &req);
  215. if (ret)
  216. goto error;
  217. /* activate tuner I2C? */
  218. req.cmd = WRITE_I2C;
  219. req.value = 0;
  220. req.index = 0x00c6;
  221. ret = ec168_ctrl_msg(d, &req);
  222. if (ret)
  223. goto error;
  224. return ret;
  225. error:
  226. pr_debug("%s: failed=%d\n", __func__, ret);
  227. return ret;
  228. }
  229. static struct ec100_config ec168_ec100_config = {
  230. .demod_address = 0xff, /* not real address, demod is integrated */
  231. };
  232. static int ec168_ec100_frontend_attach(struct dvb_usb_adapter *adap)
  233. {
  234. pr_debug("%s:\n", __func__);
  235. adap->fe[0] = dvb_attach(ec100_attach, &ec168_ec100_config,
  236. &adap_to_d(adap)->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. pr_debug("%s:\n", __func__);
  260. return dvb_attach(mxl5005s_attach, adap->fe[0],
  261. &adap_to_d(adap)->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 ec168_req req = {STREAMING_CTRL, 0x7f01, 0x0202, 0, NULL};
  267. pr_debug("%s: onoff=%d\n", __func__, onoff);
  268. if (onoff)
  269. req.index = 0x0102;
  270. return ec168_ctrl_msg(fe_to_d(fe), &req);
  271. }
  272. /* DVB USB Driver stuff */
  273. /* bInterfaceNumber 0 is HID
  274. * bInterfaceNumber 1 is DVB-T */
  275. static struct dvb_usb_device_properties ec168_props = {
  276. .driver_name = KBUILD_MODNAME,
  277. .owner = THIS_MODULE,
  278. .adapter_nr = adapter_nr,
  279. .bInterfaceNumber = 1,
  280. .identify_state = ec168_identify_state,
  281. .firmware = EC168_FIRMWARE,
  282. .download_firmware = ec168_download_firmware,
  283. .i2c_algo = &ec168_i2c_algo,
  284. .frontend_attach = ec168_ec100_frontend_attach,
  285. .tuner_attach = ec168_mxl5003s_tuner_attach,
  286. .streaming_ctrl = ec168_streaming_ctrl,
  287. .num_adapters = 1,
  288. .adapter = {
  289. {
  290. .stream = DVB_USB_STREAM_BULK(0x82, 6, 32 * 512),
  291. }
  292. },
  293. };
  294. static const struct dvb_usb_driver_info ec168_driver_info = {
  295. .name = "E3C EC168 reference design",
  296. .props = &ec168_props,
  297. };
  298. static const struct usb_device_id ec168_id[] = {
  299. { USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168),
  300. .driver_info = (kernel_ulong_t) &ec168_driver_info },
  301. { USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_2),
  302. .driver_info = (kernel_ulong_t) &ec168_driver_info },
  303. { USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_3),
  304. .driver_info = (kernel_ulong_t) &ec168_driver_info },
  305. { USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_4),
  306. .driver_info = (kernel_ulong_t) &ec168_driver_info },
  307. { USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_5),
  308. .driver_info = (kernel_ulong_t) &ec168_driver_info },
  309. {}
  310. };
  311. MODULE_DEVICE_TABLE(usb, ec168_id);
  312. static struct usb_driver ec168_driver = {
  313. .name = KBUILD_MODNAME,
  314. .id_table = ec168_id,
  315. .probe = dvb_usbv2_probe,
  316. .disconnect = dvb_usbv2_disconnect,
  317. .suspend = dvb_usbv2_suspend,
  318. .resume = dvb_usbv2_resume,
  319. .no_dynamic_id = 1,
  320. .soft_unbind = 1,
  321. };
  322. module_usb_driver(ec168_driver);
  323. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  324. MODULE_DESCRIPTION("E3C EC168 driver");
  325. MODULE_LICENSE("GPL");
  326. MODULE_FIRMWARE(EC168_FIRMWARE);