ce6230.c 7.7 KB

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