ec168.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. /* debug */
  25. static int dvb_usb_ec168_debug;
  26. module_param_named(debug, dvb_usb_ec168_debug, int, 0644);
  27. MODULE_PARM_DESC(debug, "set debugging level" DVB_USB_DEBUG_STATUS);
  28. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  29. static struct ec100_config ec168_ec100_config;
  30. static int ec168_rw_udev(struct usb_device *udev, struct ec168_req *req)
  31. {
  32. int ret;
  33. unsigned int pipe;
  34. u8 request, requesttype;
  35. u8 buf[req->size];
  36. switch (req->cmd) {
  37. case DOWNLOAD_FIRMWARE:
  38. case GPIO:
  39. case WRITE_I2C:
  40. case STREAMING_CTRL:
  41. requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
  42. request = req->cmd;
  43. break;
  44. case READ_I2C:
  45. requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
  46. request = req->cmd;
  47. break;
  48. case GET_CONFIG:
  49. requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
  50. request = CONFIG;
  51. break;
  52. case SET_CONFIG:
  53. requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
  54. request = CONFIG;
  55. break;
  56. case WRITE_DEMOD:
  57. requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
  58. request = DEMOD_RW;
  59. break;
  60. case READ_DEMOD:
  61. requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
  62. request = DEMOD_RW;
  63. break;
  64. default:
  65. err("unknown command:%02x", req->cmd);
  66. ret = -EPERM;
  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(udev, 0);
  73. } else {
  74. /* read */
  75. pipe = usb_rcvctrlpipe(udev, 0);
  76. }
  77. msleep(1); /* avoid I2C errors */
  78. ret = usb_control_msg(udev, pipe, request, requesttype, req->value,
  79. req->index, buf, sizeof(buf), EC168_USB_TIMEOUT);
  80. ec168_debug_dump(request, requesttype, req->value, req->index, buf,
  81. req->size, deb_xfer);
  82. if (ret < 0)
  83. goto error;
  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. return ret;
  90. error:
  91. deb_info("%s: failed:%d\n", __func__, ret);
  92. return ret;
  93. }
  94. static int ec168_ctrl_msg(struct dvb_usb_device *d, struct ec168_req *req)
  95. {
  96. return ec168_rw_udev(d->udev, req);
  97. }
  98. /* I2C */
  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. err("I2C read not implemented");
  122. ret = -ENOSYS;
  123. i += 2;
  124. }
  125. } else {
  126. if (msg[i].addr == ec168_ec100_config.demod_address) {
  127. req.cmd = WRITE_DEMOD;
  128. req.value = msg[i].buf[1]; /* val */
  129. req.index = 0xff00 + msg[i].buf[0]; /* reg */
  130. req.size = 0;
  131. req.data = NULL;
  132. ret = ec168_ctrl_msg(d, &req);
  133. i += 1;
  134. } else {
  135. req.cmd = WRITE_I2C;
  136. req.value = msg[i].buf[0]; /* val */
  137. req.index = 0x0100 + msg[i].addr; /* I2C addr */
  138. req.size = msg[i].len-1;
  139. req.data = &msg[i].buf[1];
  140. ret = ec168_ctrl_msg(d, &req);
  141. i += 1;
  142. }
  143. }
  144. if (ret)
  145. goto error;
  146. }
  147. ret = i;
  148. error:
  149. mutex_unlock(&d->i2c_mutex);
  150. return i;
  151. }
  152. static u32 ec168_i2c_func(struct i2c_adapter *adapter)
  153. {
  154. return I2C_FUNC_I2C;
  155. }
  156. static struct i2c_algorithm ec168_i2c_algo = {
  157. .master_xfer = ec168_i2c_xfer,
  158. .functionality = ec168_i2c_func,
  159. };
  160. /* Callbacks for DVB USB */
  161. static struct ec100_config ec168_ec100_config = {
  162. .demod_address = 0xff, /* not real address, demod is integrated */
  163. };
  164. static int ec168_ec100_frontend_attach(struct dvb_usb_adapter *adap)
  165. {
  166. deb_info("%s:\n", __func__);
  167. adap->fe = dvb_attach(ec100_attach, &ec168_ec100_config,
  168. &adap->dev->i2c_adap);
  169. if (adap->fe == NULL)
  170. return -ENODEV;
  171. return 0;
  172. }
  173. static struct mxl5005s_config ec168_mxl5003s_config = {
  174. .i2c_address = 0xc6,
  175. .if_freq = IF_FREQ_4570000HZ,
  176. .xtal_freq = CRYSTAL_FREQ_16000000HZ,
  177. .agc_mode = MXL_SINGLE_AGC,
  178. .tracking_filter = MXL_TF_OFF,
  179. .rssi_enable = MXL_RSSI_ENABLE,
  180. .cap_select = MXL_CAP_SEL_ENABLE,
  181. .div_out = MXL_DIV_OUT_4,
  182. .clock_out = MXL_CLOCK_OUT_DISABLE,
  183. .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
  184. .top = MXL5005S_TOP_25P2,
  185. .mod_mode = MXL_DIGITAL_MODE,
  186. .if_mode = MXL_ZERO_IF,
  187. .AgcMasterByte = 0x00,
  188. };
  189. static int ec168_mxl5003s_tuner_attach(struct dvb_usb_adapter *adap)
  190. {
  191. deb_info("%s:\n", __func__);
  192. return dvb_attach(mxl5005s_attach, adap->fe, &adap->dev->i2c_adap,
  193. &ec168_mxl5003s_config) == NULL ? -ENODEV : 0;
  194. }
  195. static int ec168_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
  196. {
  197. struct ec168_req req = {STREAMING_CTRL, 0x7f01, 0x0202, 0, NULL};
  198. deb_info("%s: onoff:%d\n", __func__, onoff);
  199. if (onoff)
  200. req.index = 0x0102;
  201. return ec168_ctrl_msg(adap->dev, &req);
  202. }
  203. static int ec168_download_firmware(struct usb_device *udev,
  204. const struct firmware *fw)
  205. {
  206. int i, len, packets, remainder, ret;
  207. u16 addr = 0x0000; /* firmware start address */
  208. struct ec168_req req = {DOWNLOAD_FIRMWARE, 0, 0, 0, NULL};
  209. deb_info("%s:\n", __func__);
  210. #define FW_PACKET_MAX_DATA 2048
  211. packets = fw->size / FW_PACKET_MAX_DATA;
  212. remainder = fw->size % FW_PACKET_MAX_DATA;
  213. len = FW_PACKET_MAX_DATA;
  214. for (i = 0; i <= packets; i++) {
  215. if (i == packets) /* set size of the last packet */
  216. len = remainder;
  217. req.size = len;
  218. req.data = (u8 *)(fw->data + i * FW_PACKET_MAX_DATA);
  219. req.index = addr;
  220. addr += FW_PACKET_MAX_DATA;
  221. ret = ec168_rw_udev(udev, &req);
  222. if (ret) {
  223. err("firmware download failed:%d packet:%d", ret, i);
  224. goto error;
  225. }
  226. }
  227. req.size = 0;
  228. /* set "warm"? */
  229. req.cmd = SET_CONFIG;
  230. req.value = 0;
  231. req.index = 0x0001;
  232. ret = ec168_rw_udev(udev, &req);
  233. if (ret)
  234. goto error;
  235. /* really needed - no idea what does */
  236. req.cmd = GPIO;
  237. req.value = 0;
  238. req.index = 0x0206;
  239. ret = ec168_rw_udev(udev, &req);
  240. if (ret)
  241. goto error;
  242. /* activate tuner I2C? */
  243. req.cmd = WRITE_I2C;
  244. req.value = 0;
  245. req.index = 0x00c6;
  246. ret = ec168_rw_udev(udev, &req);
  247. if (ret)
  248. goto error;
  249. return ret;
  250. error:
  251. deb_info("%s: failed:%d\n", __func__, ret);
  252. return ret;
  253. }
  254. static int ec168_identify_state(struct usb_device *udev,
  255. struct dvb_usb_device_properties *props,
  256. struct dvb_usb_device_description **desc, int *cold)
  257. {
  258. int ret;
  259. u8 reply;
  260. struct ec168_req req = {GET_CONFIG, 0, 1, sizeof(reply), &reply};
  261. deb_info("%s:\n", __func__);
  262. ret = ec168_rw_udev(udev, &req);
  263. if (ret)
  264. goto error;
  265. deb_info("%s: reply:%02x\n", __func__, reply);
  266. if (reply == 0x01)
  267. *cold = 0;
  268. else
  269. *cold = 1;
  270. return ret;
  271. error:
  272. deb_info("%s: failed:%d\n", __func__, ret);
  273. return ret;
  274. }
  275. /* DVB USB Driver stuff */
  276. static struct dvb_usb_device_properties ec168_properties;
  277. static int ec168_probe(struct usb_interface *intf,
  278. const struct usb_device_id *id)
  279. {
  280. int ret;
  281. deb_info("%s: interface:%d\n", __func__,
  282. intf->cur_altsetting->desc.bInterfaceNumber);
  283. ret = dvb_usb_device_init(intf, &ec168_properties, THIS_MODULE, NULL,
  284. adapter_nr);
  285. if (ret)
  286. goto error;
  287. return ret;
  288. error:
  289. deb_info("%s: failed:%d\n", __func__, ret);
  290. return ret;
  291. }
  292. #define E3C_EC168_1689 0
  293. #define E3C_EC168_FFFA 1
  294. #define E3C_EC168_FFFB 2
  295. #define E3C_EC168_1001 3
  296. #define E3C_EC168_1002 4
  297. static struct usb_device_id ec168_id[] = {
  298. [E3C_EC168_1689] =
  299. {USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168)},
  300. [E3C_EC168_FFFA] =
  301. {USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_2)},
  302. [E3C_EC168_FFFB] =
  303. {USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_3)},
  304. [E3C_EC168_1001] =
  305. {USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_4)},
  306. [E3C_EC168_1002] =
  307. {USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_5)},
  308. {} /* terminating entry */
  309. };
  310. MODULE_DEVICE_TABLE(usb, ec168_id);
  311. static struct dvb_usb_device_properties ec168_properties = {
  312. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  313. .usb_ctrl = DEVICE_SPECIFIC,
  314. .download_firmware = ec168_download_firmware,
  315. .firmware = "dvb-usb-ec168.fw",
  316. .no_reconnect = 1,
  317. .size_of_priv = 0,
  318. .num_adapters = 1,
  319. .adapter = {
  320. {
  321. .streaming_ctrl = ec168_streaming_ctrl,
  322. .frontend_attach = ec168_ec100_frontend_attach,
  323. .tuner_attach = ec168_mxl5003s_tuner_attach,
  324. .stream = {
  325. .type = USB_BULK,
  326. .count = 6,
  327. .endpoint = 0x82,
  328. .u = {
  329. .bulk = {
  330. .buffersize = (32*512),
  331. }
  332. }
  333. },
  334. }
  335. },
  336. .identify_state = ec168_identify_state,
  337. .i2c_algo = &ec168_i2c_algo,
  338. .num_device_descs = 1,
  339. .devices = {
  340. {
  341. .name = "E3C EC168 DVB-T USB2.0 reference design",
  342. .cold_ids = {
  343. &ec168_id[E3C_EC168_1689],
  344. &ec168_id[E3C_EC168_FFFA],
  345. &ec168_id[E3C_EC168_FFFB],
  346. &ec168_id[E3C_EC168_1001],
  347. &ec168_id[E3C_EC168_1002],
  348. NULL},
  349. .warm_ids = {NULL},
  350. },
  351. }
  352. };
  353. static struct usb_driver ec168_driver = {
  354. .name = "dvb_usb_ec168",
  355. .probe = ec168_probe,
  356. .disconnect = dvb_usb_device_exit,
  357. .id_table = ec168_id,
  358. };
  359. /* module stuff */
  360. static int __init ec168_module_init(void)
  361. {
  362. int ret;
  363. deb_info("%s:\n", __func__);
  364. ret = usb_register(&ec168_driver);
  365. if (ret)
  366. err("module init failed:%d", ret);
  367. return ret;
  368. }
  369. static void __exit ec168_module_exit(void)
  370. {
  371. deb_info("%s:\n", __func__);
  372. /* deregister this driver from the USB subsystem */
  373. usb_deregister(&ec168_driver);
  374. }
  375. module_init(ec168_module_init);
  376. module_exit(ec168_module_exit);
  377. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  378. MODULE_DESCRIPTION("E3C EC168 DVB-T USB2.0 driver");
  379. MODULE_LICENSE("GPL");