cxusb.c 6.9 KB

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