cxusb.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. * FIXME: We're getting a lock and signal, but the isochronous transfer is empty
  18. * for DVB-T.
  19. *
  20. * Copyright (C) 2005 Patrick Boettcher (patrick.boettcher@desy.de)
  21. *
  22. * This program is free software; you can redistribute it and/or modify it
  23. * under the terms of the GNU General Public License as published by the Free
  24. * Software Foundation, version 2.
  25. *
  26. * see Documentation/dvb/README.dvb-usb for more information
  27. */
  28. #include "cxusb.h"
  29. #include "cx22702.h"
  30. /* debug */
  31. int dvb_usb_cxusb_debug;
  32. module_param_named(debug,dvb_usb_cxusb_debug, int, 0644);
  33. MODULE_PARM_DESC(debug, "set debugging level (1=rc (or-able))." DVB_USB_DEBUG_STATUS);
  34. static int cxusb_ctrl_msg(struct dvb_usb_device *d,
  35. u8 cmd, u8 *wbuf, int wlen, u8 *rbuf, int rlen)
  36. {
  37. int wo = (rbuf == NULL || rlen == 0); /* write-only */
  38. u8 sndbuf[1+wlen];
  39. memset(sndbuf,0,1+wlen);
  40. sndbuf[0] = cmd;
  41. memcpy(&sndbuf[1],wbuf,wlen);
  42. if (wo)
  43. dvb_usb_generic_write(d,sndbuf,1+wlen);
  44. else
  45. dvb_usb_generic_rw(d,sndbuf,1+wlen,rbuf,rlen,0);
  46. return 0;
  47. }
  48. /* I2C */
  49. static void cxusb_set_i2c_path(struct dvb_usb_device *d, enum cxusb_i2c_pathes path)
  50. {
  51. struct cxusb_state *st = d->priv;
  52. u8 o[2],i;
  53. if (path == st->cur_i2c_path)
  54. return;
  55. o[0] = IOCTL_SET_I2C_PATH;
  56. switch (path) {
  57. case PATH_CX22702:
  58. o[1] = 0;
  59. break;
  60. case PATH_TUNER_OTHER:
  61. o[1] = 1;
  62. break;
  63. default:
  64. err("unkown i2c path");
  65. return;
  66. }
  67. cxusb_ctrl_msg(d,CMD_IOCTL,o,2,&i,1);
  68. if (i != 0x01)
  69. deb_info("i2c_path setting failed.\n");
  70. st->cur_i2c_path = path;
  71. }
  72. static int cxusb_i2c_xfer(struct i2c_adapter *adap,struct i2c_msg msg[],int num)
  73. {
  74. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  75. int i;
  76. if (down_interruptible(&d->i2c_sem) < 0)
  77. return -EAGAIN;
  78. if (num > 2)
  79. warn("more than 2 i2c messages at a time is not handled yet. TODO.");
  80. for (i = 0; i < num; i++) {
  81. switch (msg[i].addr) {
  82. case 0x63:
  83. cxusb_set_i2c_path(d,PATH_CX22702);
  84. break;
  85. default:
  86. cxusb_set_i2c_path(d,PATH_TUNER_OTHER);
  87. break;
  88. }
  89. /* read request */
  90. if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) {
  91. u8 obuf[3+msg[i].len], ibuf[1+msg[i+1].len];
  92. obuf[0] = msg[i].len;
  93. obuf[1] = msg[i+1].len;
  94. obuf[2] = msg[i].addr;
  95. memcpy(&obuf[3],msg[i].buf,msg[i].len);
  96. if (cxusb_ctrl_msg(d, CMD_I2C_READ,
  97. obuf, 3+msg[i].len,
  98. ibuf, 1+msg[i+1].len) < 0)
  99. break;
  100. if (ibuf[0] != 0x08)
  101. deb_info("i2c read could have been failed\n");
  102. memcpy(msg[i+1].buf,&ibuf[1],msg[i+1].len);
  103. i++;
  104. } else { /* write */
  105. u8 obuf[2+msg[i].len], ibuf;
  106. obuf[0] = msg[i].addr;
  107. obuf[1] = msg[i].len;
  108. memcpy(&obuf[2],msg[i].buf,msg[i].len);
  109. if (cxusb_ctrl_msg(d,CMD_I2C_WRITE, obuf, 2+msg[i].len, &ibuf,1) < 0)
  110. break;
  111. if (ibuf != 0x08)
  112. deb_info("i2c write could have been failed\n");
  113. }
  114. }
  115. up(&d->i2c_sem);
  116. return i;
  117. }
  118. static u32 cxusb_i2c_func(struct i2c_adapter *adapter)
  119. {
  120. return I2C_FUNC_I2C;
  121. }
  122. static struct i2c_algorithm cxusb_i2c_algo = {
  123. .name = "Conexant USB I2C algorithm",
  124. .id = I2C_ALGO_BIT,
  125. .master_xfer = cxusb_i2c_xfer,
  126. .functionality = cxusb_i2c_func,
  127. };
  128. static int cxusb_power_ctrl(struct dvb_usb_device *d, int onoff)
  129. {
  130. return 0;
  131. }
  132. static int cxusb_streaming_ctrl(struct dvb_usb_device *d, int onoff)
  133. {
  134. return 0;
  135. }
  136. struct cx22702_config cxusb_cx22702_config = {
  137. .demod_address = 0x63,
  138. .pll_init = dvb_usb_pll_init_i2c,
  139. .pll_set = dvb_usb_pll_set_i2c,
  140. };
  141. /* Callbacks for DVB USB */
  142. static int cxusb_tuner_attach(struct dvb_usb_device *d)
  143. {
  144. u8 bpll[4] = { 0x0b, 0xdc, 0x9c, 0xa0 };
  145. d->pll_addr = 0x61;
  146. memcpy(d->pll_init,bpll,4);
  147. d->pll_desc = &dvb_pll_fmd1216me;
  148. return 0;
  149. }
  150. static int cxusb_frontend_attach(struct dvb_usb_device *d)
  151. {
  152. u8 buf[2] = { 0x03, 0x00 };
  153. u8 b = 0;
  154. cxusb_ctrl_msg(d,0xde,&b,0,NULL,0);
  155. cxusb_set_i2c_path(d,PATH_TUNER_OTHER);
  156. cxusb_ctrl_msg(d,CMD_POWER_OFF, NULL, 0, &b, 1);
  157. if (usb_set_interface(d->udev,0,6) < 0)
  158. err("set interface failed\n");
  159. cxusb_ctrl_msg(d,0x36, buf, 2, NULL, 0);
  160. cxusb_set_i2c_path(d,PATH_CX22702);
  161. cxusb_ctrl_msg(d,CMD_POWER_ON, NULL, 0, &b, 1);
  162. if ((d->fe = cx22702_attach(&cxusb_cx22702_config, &d->i2c_adap)) != NULL)
  163. return 0;
  164. return -EIO;
  165. }
  166. /* DVB USB Driver stuff */
  167. static struct dvb_usb_properties cxusb_properties;
  168. static int cxusb_probe(struct usb_interface *intf,
  169. const struct usb_device_id *id)
  170. {
  171. return dvb_usb_device_init(intf,&cxusb_properties,THIS_MODULE);
  172. }
  173. static struct usb_device_id cxusb_table [] = {
  174. { USB_DEVICE(USB_VID_MEDION, USB_PID_MEDION_MD95700) },
  175. {} /* Terminating entry */
  176. };
  177. MODULE_DEVICE_TABLE (usb, cxusb_table);
  178. static struct dvb_usb_properties cxusb_properties = {
  179. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  180. .usb_ctrl = CYPRESS_FX2,
  181. .size_of_priv = sizeof(struct cxusb_state),
  182. .streaming_ctrl = cxusb_streaming_ctrl,
  183. .power_ctrl = cxusb_power_ctrl,
  184. .frontend_attach = cxusb_frontend_attach,
  185. .tuner_attach = cxusb_tuner_attach,
  186. .i2c_algo = &cxusb_i2c_algo,
  187. .generic_bulk_ctrl_endpoint = 0x01,
  188. /* parameter for the MPEG2-data transfer */
  189. .urb = {
  190. .type = DVB_USB_ISOC,
  191. .count = 5,
  192. .endpoint = 0x02,
  193. .u = {
  194. .isoc = {
  195. .framesperurb = 64,
  196. .framesize = 940*3,
  197. .interval = 1,
  198. }
  199. }
  200. },
  201. .num_device_descs = 1,
  202. .devices = {
  203. { "Medion MD95700 (MDUSBTV-HYBRID)",
  204. { NULL },
  205. { &cxusb_table[0], NULL },
  206. },
  207. }
  208. };
  209. static struct usb_driver cxusb_driver = {
  210. .owner = THIS_MODULE,
  211. .name = "cxusb",
  212. .probe = cxusb_probe,
  213. .disconnect = dvb_usb_device_exit,
  214. .id_table = cxusb_table,
  215. };
  216. /* module stuff */
  217. static int __init cxusb_module_init(void)
  218. {
  219. int result;
  220. if ((result = usb_register(&cxusb_driver))) {
  221. err("usb_register failed. Error number %d",result);
  222. return result;
  223. }
  224. return 0;
  225. }
  226. static void __exit cxusb_module_exit(void)
  227. {
  228. /* deregister this driver from the USB subsystem */
  229. usb_deregister(&cxusb_driver);
  230. }
  231. module_init (cxusb_module_init);
  232. module_exit (cxusb_module_exit);
  233. MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@desy.de>");
  234. MODULE_DESCRIPTION("Driver for Conexant USB2.0 hybrid reference design");
  235. MODULE_VERSION("1.0-alpha");
  236. MODULE_LICENSE("GPL");