ec168.c 10 KB

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