cxusb.c 6.5 KB

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