cxusb.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /* DVB USB compliant linux driver for Conexant USB reference design.
  2. *
  3. * The Conexant reference design I saw on their website was only for analogue
  4. * capturing (using the cx25842). The box I took to write this driver (reverse
  5. * engineered) is the one labeled Medion MD95700. In addition to the cx25842
  6. * for analogue capturing it also has a cx22702 DVB-T demodulator on the main
  7. * board. Besides it has a atiremote (X10) and a USB2.0 hub onboard.
  8. *
  9. * Maybe it is a little bit premature to call this driver cxusb, but I assume
  10. * the USB protocol is identical or at least inherited from the reference
  11. * design, so it can be reused for the "analogue-only" device (if it will
  12. * appear at all).
  13. *
  14. * TODO: check if the cx25840-driver (from ivtv) can be used for the analogue
  15. * part
  16. *
  17. * Copyright (C) 2005 Patrick Boettcher (patrick.boettcher@desy.de)
  18. *
  19. * This program is free software; you can redistribute it and/or modify it
  20. * under the terms of the GNU General Public License as published by the Free
  21. * Software Foundation, version 2.
  22. *
  23. * see Documentation/dvb/README.dvb-usb for more information
  24. */
  25. #include "cxusb.h"
  26. #include "cx22702.h"
  27. #include "lgdt330x.h"
  28. /* debug */
  29. int dvb_usb_cxusb_debug;
  30. module_param_named(debug,dvb_usb_cxusb_debug, int, 0644);
  31. MODULE_PARM_DESC(debug, "set debugging level (1=rc (or-able))." DVB_USB_DEBUG_STATUS);
  32. static int cxusb_ctrl_msg(struct dvb_usb_device *d,
  33. u8 cmd, u8 *wbuf, int wlen, u8 *rbuf, int rlen)
  34. {
  35. int wo = (rbuf == NULL || rlen == 0); /* write-only */
  36. u8 sndbuf[1+wlen];
  37. memset(sndbuf,0,1+wlen);
  38. sndbuf[0] = cmd;
  39. memcpy(&sndbuf[1],wbuf,wlen);
  40. if (wo)
  41. dvb_usb_generic_write(d,sndbuf,1+wlen);
  42. else
  43. dvb_usb_generic_rw(d,sndbuf,1+wlen,rbuf,rlen,0);
  44. return 0;
  45. }
  46. /* GPIO */
  47. static void cxusb_gpio_tuner(struct dvb_usb_device *d, int onoff)
  48. {
  49. struct cxusb_state *st = d->priv;
  50. u8 o[2],i;
  51. if (st->gpio_write_state[GPIO_TUNER] == onoff)
  52. return;
  53. o[0] = GPIO_TUNER;
  54. o[1] = onoff;
  55. cxusb_ctrl_msg(d,CMD_GPIO_WRITE,o,2,&i,1);
  56. if (i != 0x01)
  57. deb_info("gpio_write failed.\n");
  58. st->gpio_write_state[GPIO_TUNER] = onoff;
  59. }
  60. /* I2C */
  61. static int cxusb_i2c_xfer(struct i2c_adapter *adap,struct i2c_msg msg[],int num)
  62. {
  63. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  64. int i;
  65. if (down_interruptible(&d->i2c_sem) < 0)
  66. return -EAGAIN;
  67. if (num > 2)
  68. warn("more than 2 i2c messages at a time is not handled yet. TODO.");
  69. for (i = 0; i < num; i++) {
  70. switch (msg[i].addr) {
  71. case 0x63:
  72. cxusb_gpio_tuner(d,0);
  73. break;
  74. default:
  75. cxusb_gpio_tuner(d,1);
  76. break;
  77. }
  78. /* read request */
  79. if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) {
  80. u8 obuf[3+msg[i].len], ibuf[1+msg[i+1].len];
  81. obuf[0] = msg[i].len;
  82. obuf[1] = msg[i+1].len;
  83. obuf[2] = msg[i].addr;
  84. memcpy(&obuf[3],msg[i].buf,msg[i].len);
  85. if (cxusb_ctrl_msg(d, CMD_I2C_READ,
  86. obuf, 3+msg[i].len,
  87. ibuf, 1+msg[i+1].len) < 0)
  88. break;
  89. if (ibuf[0] != 0x08)
  90. deb_info("i2c read could have been failed\n");
  91. memcpy(msg[i+1].buf,&ibuf[1],msg[i+1].len);
  92. i++;
  93. } else { /* write */
  94. u8 obuf[2+msg[i].len], ibuf;
  95. obuf[0] = msg[i].addr;
  96. obuf[1] = msg[i].len;
  97. memcpy(&obuf[2],msg[i].buf,msg[i].len);
  98. if (cxusb_ctrl_msg(d,CMD_I2C_WRITE, obuf, 2+msg[i].len, &ibuf,1) < 0)
  99. break;
  100. if (ibuf != 0x08)
  101. deb_info("i2c write could have been failed\n");
  102. }
  103. }
  104. up(&d->i2c_sem);
  105. return i;
  106. }
  107. static u32 cxusb_i2c_func(struct i2c_adapter *adapter)
  108. {
  109. return I2C_FUNC_I2C;
  110. }
  111. static struct i2c_algorithm cxusb_i2c_algo = {
  112. .master_xfer = cxusb_i2c_xfer,
  113. .functionality = cxusb_i2c_func,
  114. };
  115. static int cxusb_power_ctrl(struct dvb_usb_device *d, int onoff)
  116. {
  117. u8 b = 0;
  118. if (onoff)
  119. return cxusb_ctrl_msg(d, CMD_POWER_ON, &b, 1, NULL, 0);
  120. else
  121. return cxusb_ctrl_msg(d, CMD_POWER_OFF, &b, 1, NULL, 0);
  122. }
  123. static int cxusb_streaming_ctrl(struct dvb_usb_device *d, int onoff)
  124. {
  125. u8 buf[2] = { 0x03, 0x00 };
  126. if (onoff)
  127. cxusb_ctrl_msg(d,CMD_STREAMING_ON, buf, 2, NULL, 0);
  128. else
  129. cxusb_ctrl_msg(d,CMD_STREAMING_OFF, NULL, 0, NULL, 0);
  130. return 0;
  131. }
  132. struct cx22702_config cxusb_cx22702_config = {
  133. .demod_address = 0x63,
  134. .output_mode = CX22702_PARALLEL_OUTPUT,
  135. .pll_init = dvb_usb_pll_init_i2c,
  136. .pll_set = dvb_usb_pll_set_i2c,
  137. };
  138. struct lgdt330x_config cxusb_lgdt330x_config = {
  139. .demod_address = 0x0e,
  140. .demod_chip = LGDT3303,
  141. .pll_set = dvb_usb_pll_set_i2c,
  142. };
  143. /* Callbacks for DVB USB */
  144. static int cxusb_fmd1216me_tuner_attach(struct dvb_usb_device *d)
  145. {
  146. u8 bpll[4] = { 0x0b, 0xdc, 0x9c, 0xa0 };
  147. d->pll_addr = 0x61;
  148. memcpy(d->pll_init, bpll, 4);
  149. d->pll_desc = &dvb_pll_fmd1216me;
  150. return 0;
  151. }
  152. static int cxusb_lgh064f_tuner_attach(struct dvb_usb_device *d)
  153. {
  154. u8 bpll[4] = { 0x00, 0x00, 0x18, 0x50 };
  155. /* bpll[2] : unset bit 3, set bits 4&5
  156. bpll[3] : 0x50 - digital, 0x20 - analog */
  157. d->pll_addr = 0x61;
  158. memcpy(d->pll_init, bpll, 4);
  159. d->pll_desc = &dvb_pll_tdvs_tua6034;
  160. return 0;
  161. }
  162. static int cxusb_cx22702_frontend_attach(struct dvb_usb_device *d)
  163. {
  164. u8 b;
  165. if (usb_set_interface(d->udev,0,6) < 0)
  166. err("set interface failed");
  167. cxusb_ctrl_msg(d,CMD_DIGITAL, NULL, 0, &b, 1);
  168. if ((d->fe = cx22702_attach(&cxusb_cx22702_config, &d->i2c_adap)) != NULL)
  169. return 0;
  170. return -EIO;
  171. }
  172. static int cxusb_lgdt330x_frontend_attach(struct dvb_usb_device *d)
  173. {
  174. if (usb_set_interface(d->udev,0,7) < 0)
  175. err("set interface failed");
  176. cxusb_ctrl_msg(d,CMD_DIGITAL, NULL, 0, NULL, 0);
  177. if ((d->fe = lgdt330x_attach(&cxusb_lgdt330x_config, &d->i2c_adap)) != NULL)
  178. return 0;
  179. return -EIO;
  180. }
  181. /* DVB USB Driver stuff */
  182. static struct dvb_usb_properties cxusb_medion_properties;
  183. static struct dvb_usb_properties cxusb_bluebird_lgh064f_properties;
  184. static int cxusb_probe(struct usb_interface *intf,
  185. const struct usb_device_id *id)
  186. {
  187. if (dvb_usb_device_init(intf,&cxusb_medion_properties,THIS_MODULE,NULL) == 0 ||
  188. dvb_usb_device_init(intf,&cxusb_bluebird_lgh064f_properties,THIS_MODULE,NULL) == 0) {
  189. return 0;
  190. }
  191. return -EINVAL;
  192. }
  193. static struct usb_device_id cxusb_table [] = {
  194. { USB_DEVICE(USB_VID_MEDION, USB_PID_MEDION_MD95700) },
  195. { USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_LG064F_COLD) },
  196. { USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_LG064F_WARM) },
  197. {} /* Terminating entry */
  198. };
  199. MODULE_DEVICE_TABLE (usb, cxusb_table);
  200. static struct dvb_usb_properties cxusb_medion_properties = {
  201. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  202. .usb_ctrl = CYPRESS_FX2,
  203. .size_of_priv = sizeof(struct cxusb_state),
  204. .streaming_ctrl = cxusb_streaming_ctrl,
  205. .power_ctrl = cxusb_power_ctrl,
  206. .frontend_attach = cxusb_cx22702_frontend_attach,
  207. .tuner_attach = cxusb_fmd1216me_tuner_attach,
  208. .i2c_algo = &cxusb_i2c_algo,
  209. .generic_bulk_ctrl_endpoint = 0x01,
  210. /* parameter for the MPEG2-data transfer */
  211. .urb = {
  212. .type = DVB_USB_BULK,
  213. .count = 5,
  214. .endpoint = 0x02,
  215. .u = {
  216. .bulk = {
  217. .buffersize = 8192,
  218. }
  219. }
  220. },
  221. .num_device_descs = 1,
  222. .devices = {
  223. { "Medion MD95700 (MDUSBTV-HYBRID)",
  224. { NULL },
  225. { &cxusb_table[0], NULL },
  226. },
  227. }
  228. };
  229. static struct dvb_usb_properties cxusb_bluebird_lgh064f_properties = {
  230. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  231. .usb_ctrl = CYPRESS_FX2,
  232. .firmware = "dvb-usb-bluebird-01.fw",
  233. /* use usb alt setting 0 for EP4 transfer (dvb-t),
  234. use usb alt setting 7 for EP2 transfer (atsc) */
  235. .size_of_priv = sizeof(struct cxusb_state),
  236. .streaming_ctrl = cxusb_streaming_ctrl,
  237. .power_ctrl = cxusb_power_ctrl,
  238. .frontend_attach = cxusb_lgdt330x_frontend_attach,
  239. .tuner_attach = cxusb_lgh064f_tuner_attach,
  240. .i2c_algo = &cxusb_i2c_algo,
  241. .generic_bulk_ctrl_endpoint = 0x01,
  242. /* parameter for the MPEG2-data transfer */
  243. .urb = {
  244. .type = DVB_USB_BULK,
  245. .count = 5,
  246. .endpoint = 0x02,
  247. .u = {
  248. .bulk = {
  249. .buffersize = 8192,
  250. }
  251. }
  252. },
  253. .num_device_descs = 1,
  254. .devices = {
  255. { "DViCO FusionHDTV5 USB Gold",
  256. { &cxusb_table[1], NULL },
  257. { &cxusb_table[2], NULL },
  258. },
  259. }
  260. };
  261. static struct usb_driver cxusb_driver = {
  262. .name = "dvb_usb_cxusb",
  263. .probe = cxusb_probe,
  264. .disconnect = dvb_usb_device_exit,
  265. .id_table = cxusb_table,
  266. };
  267. /* module stuff */
  268. static int __init cxusb_module_init(void)
  269. {
  270. int result;
  271. if ((result = usb_register(&cxusb_driver))) {
  272. err("usb_register failed. Error number %d",result);
  273. return result;
  274. }
  275. return 0;
  276. }
  277. static void __exit cxusb_module_exit(void)
  278. {
  279. /* deregister this driver from the USB subsystem */
  280. usb_deregister(&cxusb_driver);
  281. }
  282. module_init (cxusb_module_init);
  283. module_exit (cxusb_module_exit);
  284. MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@desy.de>");
  285. MODULE_DESCRIPTION("Driver for Conexant USB2.0 hybrid reference design");
  286. MODULE_VERSION("1.0-alpha");
  287. MODULE_LICENSE("GPL");