ce6230.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * DVB USB Linux driver for Intel CE6230 DVB-T USB2.0 receiver
  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 "ce6230.h"
  22. #include "zl10353.h"
  23. #include "mxl5005s.h"
  24. /* debug */
  25. static int dvb_usb_ce6230_debug;
  26. module_param_named(debug, dvb_usb_ce6230_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 zl10353_config ce6230_zl10353_config;
  30. static int ce6230_rw_udev(struct usb_device *udev, struct req_t *req)
  31. {
  32. int ret;
  33. unsigned int pipe;
  34. u8 request;
  35. u8 requesttype;
  36. u16 value;
  37. u16 index;
  38. u8 buf[req->data_len];
  39. request = req->cmd;
  40. value = req->value;
  41. index = req->index;
  42. switch (req->cmd) {
  43. case I2C_READ:
  44. case DEMOD_READ:
  45. case REG_READ:
  46. requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
  47. break;
  48. case I2C_WRITE:
  49. case DEMOD_WRITE:
  50. case REG_WRITE:
  51. requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
  52. break;
  53. default:
  54. err("unknown command:%02x", req->cmd);
  55. ret = -EPERM;
  56. goto error;
  57. }
  58. if (requesttype == (USB_TYPE_VENDOR | USB_DIR_OUT)) {
  59. /* write */
  60. memcpy(buf, req->data, req->data_len);
  61. pipe = usb_sndctrlpipe(udev, 0);
  62. } else {
  63. /* read */
  64. pipe = usb_rcvctrlpipe(udev, 0);
  65. }
  66. msleep(1); /* avoid I2C errors */
  67. ret = usb_control_msg(udev, pipe, request, requesttype, value, index,
  68. buf, sizeof(buf), CE6230_USB_TIMEOUT);
  69. ce6230_debug_dump(request, requesttype, value, index, buf,
  70. req->data_len, deb_xfer);
  71. if (ret < 0)
  72. deb_info("%s: usb_control_msg failed:%d\n", __func__, ret);
  73. else
  74. ret = 0;
  75. /* read request, copy returned data to return buf */
  76. if (!ret && requesttype == (USB_TYPE_VENDOR | USB_DIR_IN))
  77. memcpy(req->data, buf, req->data_len);
  78. error:
  79. return ret;
  80. }
  81. static int ce6230_ctrl_msg(struct dvb_usb_device *d, struct req_t *req)
  82. {
  83. return ce6230_rw_udev(d->udev, req);
  84. }
  85. /* I2C */
  86. static int ce6230_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
  87. int num)
  88. {
  89. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  90. int i = 0;
  91. struct req_t req;
  92. int ret = 0;
  93. memset(&req, 0, sizeof(req));
  94. if (num > 2)
  95. return -EINVAL;
  96. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  97. return -EAGAIN;
  98. while (i < num) {
  99. if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) {
  100. if (msg[i].addr ==
  101. ce6230_zl10353_config.demod_address) {
  102. req.cmd = DEMOD_READ;
  103. req.value = msg[i].addr >> 1;
  104. req.index = msg[i].buf[0];
  105. req.data_len = msg[i+1].len;
  106. req.data = &msg[i+1].buf[0];
  107. ret = ce6230_ctrl_msg(d, &req);
  108. } else {
  109. err("i2c read not implemented");
  110. ret = -EPERM;
  111. }
  112. i += 2;
  113. } else {
  114. if (msg[i].addr ==
  115. ce6230_zl10353_config.demod_address) {
  116. req.cmd = DEMOD_WRITE;
  117. req.value = msg[i].addr >> 1;
  118. req.index = msg[i].buf[0];
  119. req.data_len = msg[i].len-1;
  120. req.data = &msg[i].buf[1];
  121. ret = ce6230_ctrl_msg(d, &req);
  122. } else {
  123. req.cmd = I2C_WRITE;
  124. req.value = 0x2000 + (msg[i].addr >> 1);
  125. req.index = 0x0000;
  126. req.data_len = msg[i].len;
  127. req.data = &msg[i].buf[0];
  128. ret = ce6230_ctrl_msg(d, &req);
  129. }
  130. i += 1;
  131. }
  132. if (ret)
  133. break;
  134. }
  135. mutex_unlock(&d->i2c_mutex);
  136. return ret ? ret : i;
  137. }
  138. static u32 ce6230_i2c_func(struct i2c_adapter *adapter)
  139. {
  140. return I2C_FUNC_I2C;
  141. }
  142. static struct i2c_algorithm ce6230_i2c_algo = {
  143. .master_xfer = ce6230_i2c_xfer,
  144. .functionality = ce6230_i2c_func,
  145. };
  146. /* Callbacks for DVB USB */
  147. static struct zl10353_config ce6230_zl10353_config = {
  148. .demod_address = 0x1e,
  149. .adc_clock = 450000,
  150. .if2 = 45700,
  151. .no_tuner = 1,
  152. .parallel_ts = 1,
  153. .clock_ctl_1 = 0x34,
  154. .pll_0 = 0x0e,
  155. };
  156. static int ce6230_zl10353_frontend_attach(struct dvb_usb_adapter *adap)
  157. {
  158. deb_info("%s:\n", __func__);
  159. adap->fe = dvb_attach(zl10353_attach, &ce6230_zl10353_config,
  160. &adap->dev->i2c_adap);
  161. if (adap->fe == NULL)
  162. return -ENODEV;
  163. return 0;
  164. }
  165. static struct mxl5005s_config ce6230_mxl5003s_config = {
  166. .i2c_address = 0xc6,
  167. .if_freq = IF_FREQ_4570000HZ,
  168. .xtal_freq = CRYSTAL_FREQ_16000000HZ,
  169. .agc_mode = MXL_SINGLE_AGC,
  170. .tracking_filter = MXL_TF_DEFAULT,
  171. .rssi_enable = MXL_RSSI_ENABLE,
  172. .cap_select = MXL_CAP_SEL_ENABLE,
  173. .div_out = MXL_DIV_OUT_4,
  174. .clock_out = MXL_CLOCK_OUT_DISABLE,
  175. .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
  176. .top = MXL5005S_TOP_25P2,
  177. .mod_mode = MXL_DIGITAL_MODE,
  178. .if_mode = MXL_ZERO_IF,
  179. .AgcMasterByte = 0x00,
  180. };
  181. static int ce6230_mxl5003s_tuner_attach(struct dvb_usb_adapter *adap)
  182. {
  183. int ret;
  184. deb_info("%s:\n", __func__);
  185. ret = dvb_attach(mxl5005s_attach, adap->fe, &adap->dev->i2c_adap,
  186. &ce6230_mxl5003s_config) == NULL ? -ENODEV : 0;
  187. return ret;
  188. }
  189. static int ce6230_power_ctrl(struct dvb_usb_device *d, int onoff)
  190. {
  191. int ret;
  192. deb_info("%s: onoff:%d\n", __func__, onoff);
  193. /* InterfaceNumber 1 / AlternateSetting 0 idle
  194. InterfaceNumber 1 / AlternateSetting 1 streaming */
  195. ret = usb_set_interface(d->udev, 1, onoff);
  196. if (ret)
  197. err("usb_set_interface failed with error:%d", ret);
  198. return ret;
  199. }
  200. /* DVB USB Driver stuff */
  201. static struct dvb_usb_device_properties ce6230_properties;
  202. static int ce6230_probe(struct usb_interface *intf,
  203. const struct usb_device_id *id)
  204. {
  205. int ret = 0;
  206. struct dvb_usb_device *d = NULL;
  207. deb_info("%s: interface:%d\n", __func__,
  208. intf->cur_altsetting->desc.bInterfaceNumber);
  209. if (intf->cur_altsetting->desc.bInterfaceNumber == 1) {
  210. ret = dvb_usb_device_init(intf, &ce6230_properties, THIS_MODULE,
  211. &d, adapter_nr);
  212. if (ret)
  213. err("init failed with error:%d\n", ret);
  214. }
  215. return ret;
  216. }
  217. static struct usb_device_id ce6230_table[] = {
  218. { USB_DEVICE(USB_VID_INTEL, USB_PID_INTEL_CE9500) },
  219. { USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A310) },
  220. { } /* Terminating entry */
  221. };
  222. MODULE_DEVICE_TABLE(usb, ce6230_table);
  223. static struct dvb_usb_device_properties ce6230_properties = {
  224. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  225. .usb_ctrl = DEVICE_SPECIFIC,
  226. .no_reconnect = 1,
  227. .size_of_priv = 0,
  228. .num_adapters = 1,
  229. .adapter = {
  230. {
  231. .frontend_attach = ce6230_zl10353_frontend_attach,
  232. .tuner_attach = ce6230_mxl5003s_tuner_attach,
  233. .stream = {
  234. .type = USB_BULK,
  235. .count = 6,
  236. .endpoint = 0x82,
  237. .u = {
  238. .bulk = {
  239. .buffersize = (16*512),
  240. }
  241. }
  242. },
  243. }
  244. },
  245. .power_ctrl = ce6230_power_ctrl,
  246. .i2c_algo = &ce6230_i2c_algo,
  247. .num_device_descs = 2,
  248. .devices = {
  249. {
  250. .name = "Intel CE9500 reference design",
  251. .cold_ids = {NULL},
  252. .warm_ids = {&ce6230_table[0], NULL},
  253. },
  254. {
  255. .name = "AVerMedia A310 USB 2.0 DVB-T tuner",
  256. .cold_ids = {NULL},
  257. .warm_ids = {&ce6230_table[1], NULL},
  258. },
  259. }
  260. };
  261. static struct usb_driver ce6230_driver = {
  262. .name = "dvb_usb_ce6230",
  263. .probe = ce6230_probe,
  264. .disconnect = dvb_usb_device_exit,
  265. .id_table = ce6230_table,
  266. };
  267. /* module stuff */
  268. static int __init ce6230_module_init(void)
  269. {
  270. int ret;
  271. deb_info("%s:\n", __func__);
  272. ret = usb_register(&ce6230_driver);
  273. if (ret)
  274. err("usb_register failed with error:%d", ret);
  275. return ret;
  276. }
  277. static void __exit ce6230_module_exit(void)
  278. {
  279. deb_info("%s:\n", __func__);
  280. /* deregister this driver from the USB subsystem */
  281. usb_deregister(&ce6230_driver);
  282. }
  283. module_init(ce6230_module_init);
  284. module_exit(ce6230_module_exit);
  285. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  286. MODULE_DESCRIPTION("Driver for Intel CE6230 DVB-T USB2.0");
  287. MODULE_LICENSE("GPL");