dtv5100.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * DVB USB Linux driver for AME DTV-5100 USB2.0 DVB-T
  3. *
  4. * Copyright (C) 2008 Antoine Jacquet <royale@zerezo.com>
  5. * http://royale.zerezo.com/dtv5100/
  6. *
  7. * Inspired by gl861.c and au6610.c drivers
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include "dtv5100.h"
  24. #include "zl10353.h"
  25. #include "qt1010.h"
  26. /* debug */
  27. static int dvb_usb_dtv5100_debug;
  28. module_param_named(debug, dvb_usb_dtv5100_debug, int, 0644);
  29. MODULE_PARM_DESC(debug, "set debugging level" DVB_USB_DEBUG_STATUS);
  30. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  31. static int dtv5100_i2c_msg(struct dvb_usb_device *d, u8 addr,
  32. u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen)
  33. {
  34. u8 request;
  35. u8 type;
  36. u16 value;
  37. u16 index;
  38. switch (wlen) {
  39. case 1:
  40. /* write { reg }, read { value } */
  41. request = (addr == DTV5100_DEMOD_ADDR ? DTV5100_DEMOD_READ :
  42. DTV5100_TUNER_READ);
  43. type = USB_TYPE_VENDOR | USB_DIR_IN;
  44. value = 0;
  45. break;
  46. case 2:
  47. /* write { reg, value } */
  48. request = (addr == DTV5100_DEMOD_ADDR ? DTV5100_DEMOD_WRITE :
  49. DTV5100_TUNER_WRITE);
  50. type = USB_TYPE_VENDOR | USB_DIR_OUT;
  51. value = wbuf[1];
  52. break;
  53. default:
  54. warn("wlen = %x, aborting.", wlen);
  55. return -EINVAL;
  56. }
  57. index = (addr << 8) + wbuf[0];
  58. msleep(1); /* avoid I2C errors */
  59. return usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0), request,
  60. type, value, index, rbuf, rlen,
  61. DTV5100_USB_TIMEOUT);
  62. }
  63. /* I2C */
  64. static int dtv5100_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
  65. int num)
  66. {
  67. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  68. int i;
  69. if (num > 2)
  70. return -EINVAL;
  71. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  72. return -EAGAIN;
  73. for (i = 0; i < num; i++) {
  74. /* write/read request */
  75. if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) {
  76. if (dtv5100_i2c_msg(d, msg[i].addr, msg[i].buf,
  77. msg[i].len, msg[i+1].buf,
  78. msg[i+1].len) < 0)
  79. break;
  80. i++;
  81. } else if (dtv5100_i2c_msg(d, msg[i].addr, msg[i].buf,
  82. msg[i].len, NULL, 0) < 0)
  83. break;
  84. }
  85. mutex_unlock(&d->i2c_mutex);
  86. return i;
  87. }
  88. static u32 dtv5100_i2c_func(struct i2c_adapter *adapter)
  89. {
  90. return I2C_FUNC_I2C;
  91. }
  92. static struct i2c_algorithm dtv5100_i2c_algo = {
  93. .master_xfer = dtv5100_i2c_xfer,
  94. .functionality = dtv5100_i2c_func,
  95. };
  96. /* Callbacks for DVB USB */
  97. static struct zl10353_config dtv5100_zl10353_config = {
  98. .demod_address = DTV5100_DEMOD_ADDR,
  99. .no_tuner = 1,
  100. .parallel_ts = 1,
  101. };
  102. static int dtv5100_frontend_attach(struct dvb_usb_adapter *adap)
  103. {
  104. adap->fe = dvb_attach(zl10353_attach, &dtv5100_zl10353_config,
  105. &adap->dev->i2c_adap);
  106. if (adap->fe == NULL)
  107. return -EIO;
  108. /* disable i2c gate, or it won't work... is this safe? */
  109. adap->fe->ops.i2c_gate_ctrl = NULL;
  110. return 0;
  111. }
  112. static struct qt1010_config dtv5100_qt1010_config = {
  113. .i2c_address = DTV5100_TUNER_ADDR
  114. };
  115. static int dtv5100_tuner_attach(struct dvb_usb_adapter *adap)
  116. {
  117. return dvb_attach(qt1010_attach,
  118. adap->fe, &adap->dev->i2c_adap,
  119. &dtv5100_qt1010_config) == NULL ? -ENODEV : 0;
  120. }
  121. /* DVB USB Driver stuff */
  122. static struct dvb_usb_device_properties dtv5100_properties;
  123. static int dtv5100_probe(struct usb_interface *intf,
  124. const struct usb_device_id *id)
  125. {
  126. int i, ret;
  127. struct usb_device *udev = interface_to_usbdev(intf);
  128. /* initialize non qt1010/zl10353 part? */
  129. for (i = 0; dtv5100_init[i].request; i++) {
  130. ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  131. dtv5100_init[i].request,
  132. USB_TYPE_VENDOR | USB_DIR_OUT,
  133. dtv5100_init[i].value,
  134. dtv5100_init[i].index, NULL, 0,
  135. DTV5100_USB_TIMEOUT);
  136. if (ret)
  137. return ret;
  138. }
  139. ret = dvb_usb_device_init(intf, &dtv5100_properties,
  140. THIS_MODULE, NULL, adapter_nr);
  141. if (ret)
  142. return ret;
  143. return 0;
  144. }
  145. static struct usb_device_id dtv5100_table[] = {
  146. { USB_DEVICE(0x06be, 0xa232) },
  147. { } /* Terminating entry */
  148. };
  149. MODULE_DEVICE_TABLE(usb, dtv5100_table);
  150. static struct dvb_usb_device_properties dtv5100_properties = {
  151. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  152. .usb_ctrl = DEVICE_SPECIFIC,
  153. .size_of_priv = 0,
  154. .num_adapters = 1,
  155. .adapter = {{
  156. .frontend_attach = dtv5100_frontend_attach,
  157. .tuner_attach = dtv5100_tuner_attach,
  158. .stream = {
  159. .type = USB_BULK,
  160. .count = 8,
  161. .endpoint = 0x82,
  162. .u = {
  163. .bulk = {
  164. .buffersize = 4096,
  165. }
  166. }
  167. },
  168. } },
  169. .i2c_algo = &dtv5100_i2c_algo,
  170. .num_device_descs = 1,
  171. .devices = {
  172. {
  173. .name = "AME DTV-5100 USB2.0 DVB-T",
  174. .cold_ids = { NULL },
  175. .warm_ids = { &dtv5100_table[0], NULL },
  176. },
  177. }
  178. };
  179. static struct usb_driver dtv5100_driver = {
  180. .name = "dvb_usb_dtv5100",
  181. .probe = dtv5100_probe,
  182. .disconnect = dvb_usb_device_exit,
  183. .id_table = dtv5100_table,
  184. };
  185. /* module stuff */
  186. static int __init dtv5100_module_init(void)
  187. {
  188. int ret;
  189. ret = usb_register(&dtv5100_driver);
  190. if (ret)
  191. err("usb_register failed. Error number %d", ret);
  192. return ret;
  193. }
  194. static void __exit dtv5100_module_exit(void)
  195. {
  196. /* deregister this driver from the USB subsystem */
  197. usb_deregister(&dtv5100_driver);
  198. }
  199. module_init(dtv5100_module_init);
  200. module_exit(dtv5100_module_exit);
  201. MODULE_AUTHOR(DRIVER_AUTHOR);
  202. MODULE_DESCRIPTION(DRIVER_DESC);
  203. MODULE_LICENSE("GPL");