dtv5100.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. #include "dtv5100.h"
  20. #include "zl10353.h"
  21. #include "qt1010.h"
  22. /* debug */
  23. static int dvb_usb_dtv5100_debug;
  24. module_param_named(debug, dvb_usb_dtv5100_debug, int, 0644);
  25. MODULE_PARM_DESC(debug, "set debugging level" DVB_USB_DEBUG_STATUS);
  26. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  27. struct dtv5100_state {
  28. unsigned char data[80];
  29. };
  30. static int dtv5100_i2c_msg(struct dvb_usb_device *d, u8 addr,
  31. u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen)
  32. {
  33. struct dtv5100_state *st = d->priv;
  34. unsigned int pipe;
  35. u8 request;
  36. u8 type;
  37. u16 value;
  38. u16 index;
  39. switch (wlen) {
  40. case 1:
  41. /* write { reg }, read { value } */
  42. pipe = usb_rcvctrlpipe(d->udev, 0);
  43. request = (addr == DTV5100_DEMOD_ADDR ? DTV5100_DEMOD_READ :
  44. DTV5100_TUNER_READ);
  45. type = USB_TYPE_VENDOR | USB_DIR_IN;
  46. value = 0;
  47. break;
  48. case 2:
  49. /* write { reg, value } */
  50. pipe = usb_sndctrlpipe(d->udev, 0);
  51. request = (addr == DTV5100_DEMOD_ADDR ? DTV5100_DEMOD_WRITE :
  52. DTV5100_TUNER_WRITE);
  53. type = USB_TYPE_VENDOR | USB_DIR_OUT;
  54. value = wbuf[1];
  55. break;
  56. default:
  57. warn("wlen = %x, aborting.", wlen);
  58. return -EINVAL;
  59. }
  60. index = (addr << 8) + wbuf[0];
  61. memcpy(st->data, rbuf, rlen);
  62. msleep(1); /* avoid I2C errors */
  63. return usb_control_msg(d->udev, pipe, request,
  64. type, value, index, st->data, rlen,
  65. DTV5100_USB_TIMEOUT);
  66. }
  67. /* I2C */
  68. static int dtv5100_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
  69. int num)
  70. {
  71. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  72. int i;
  73. if (num > 2)
  74. return -EINVAL;
  75. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  76. return -EAGAIN;
  77. for (i = 0; i < num; i++) {
  78. /* write/read request */
  79. if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) {
  80. if (dtv5100_i2c_msg(d, msg[i].addr, msg[i].buf,
  81. msg[i].len, msg[i+1].buf,
  82. msg[i+1].len) < 0)
  83. break;
  84. i++;
  85. } else if (dtv5100_i2c_msg(d, msg[i].addr, msg[i].buf,
  86. msg[i].len, NULL, 0) < 0)
  87. break;
  88. }
  89. mutex_unlock(&d->i2c_mutex);
  90. return i;
  91. }
  92. static u32 dtv5100_i2c_func(struct i2c_adapter *adapter)
  93. {
  94. return I2C_FUNC_I2C;
  95. }
  96. static struct i2c_algorithm dtv5100_i2c_algo = {
  97. .master_xfer = dtv5100_i2c_xfer,
  98. .functionality = dtv5100_i2c_func,
  99. };
  100. /* Callbacks for DVB USB */
  101. static struct zl10353_config dtv5100_zl10353_config = {
  102. .demod_address = DTV5100_DEMOD_ADDR,
  103. .no_tuner = 1,
  104. .parallel_ts = 1,
  105. };
  106. static int dtv5100_frontend_attach(struct dvb_usb_adapter *adap)
  107. {
  108. adap->fe_adap[0].fe = dvb_attach(zl10353_attach, &dtv5100_zl10353_config,
  109. &adap->dev->i2c_adap);
  110. if (adap->fe_adap[0].fe == NULL)
  111. return -EIO;
  112. /* disable i2c gate, or it won't work... is this safe? */
  113. adap->fe_adap[0].fe->ops.i2c_gate_ctrl = NULL;
  114. return 0;
  115. }
  116. static struct qt1010_config dtv5100_qt1010_config = {
  117. .i2c_address = DTV5100_TUNER_ADDR
  118. };
  119. static int dtv5100_tuner_attach(struct dvb_usb_adapter *adap)
  120. {
  121. return dvb_attach(qt1010_attach,
  122. adap->fe_adap[0].fe, &adap->dev->i2c_adap,
  123. &dtv5100_qt1010_config) == NULL ? -ENODEV : 0;
  124. }
  125. /* DVB USB Driver stuff */
  126. static struct dvb_usb_device_properties dtv5100_properties;
  127. static int dtv5100_probe(struct usb_interface *intf,
  128. const struct usb_device_id *id)
  129. {
  130. int i, ret;
  131. struct usb_device *udev = interface_to_usbdev(intf);
  132. /* initialize non qt1010/zl10353 part? */
  133. for (i = 0; dtv5100_init[i].request; i++) {
  134. ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  135. dtv5100_init[i].request,
  136. USB_TYPE_VENDOR | USB_DIR_OUT,
  137. dtv5100_init[i].value,
  138. dtv5100_init[i].index, NULL, 0,
  139. DTV5100_USB_TIMEOUT);
  140. if (ret)
  141. return ret;
  142. }
  143. ret = dvb_usb_device_init(intf, &dtv5100_properties,
  144. THIS_MODULE, NULL, adapter_nr);
  145. if (ret)
  146. return ret;
  147. return 0;
  148. }
  149. static struct usb_device_id dtv5100_table[] = {
  150. { USB_DEVICE(0x06be, 0xa232) },
  151. { } /* Terminating entry */
  152. };
  153. MODULE_DEVICE_TABLE(usb, dtv5100_table);
  154. static struct dvb_usb_device_properties dtv5100_properties = {
  155. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  156. .usb_ctrl = DEVICE_SPECIFIC,
  157. .size_of_priv = sizeof(struct dtv5100_state),
  158. .num_adapters = 1,
  159. .adapter = {{
  160. .num_frontends = 1,
  161. .fe = {{
  162. .frontend_attach = dtv5100_frontend_attach,
  163. .tuner_attach = dtv5100_tuner_attach,
  164. .stream = {
  165. .type = USB_BULK,
  166. .count = 8,
  167. .endpoint = 0x82,
  168. .u = {
  169. .bulk = {
  170. .buffersize = 4096,
  171. }
  172. }
  173. },
  174. }},
  175. } },
  176. .i2c_algo = &dtv5100_i2c_algo,
  177. .num_device_descs = 1,
  178. .devices = {
  179. {
  180. .name = "AME DTV-5100 USB2.0 DVB-T",
  181. .cold_ids = { NULL },
  182. .warm_ids = { &dtv5100_table[0], NULL },
  183. },
  184. }
  185. };
  186. static struct usb_driver dtv5100_driver = {
  187. .name = "dvb_usb_dtv5100",
  188. .probe = dtv5100_probe,
  189. .disconnect = dvb_usb_device_exit,
  190. .id_table = dtv5100_table,
  191. };
  192. module_usb_driver(dtv5100_driver);
  193. MODULE_AUTHOR(DRIVER_AUTHOR);
  194. MODULE_DESCRIPTION(DRIVER_DESC);
  195. MODULE_LICENSE("GPL");