gl861.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* DVB USB compliant linux driver for GL861 USB2.0 devices.
  2. *
  3. * This program is free software; you can redistribute it and/or modify it
  4. * under the terms of the GNU General Public License as published by the Free
  5. * Software Foundation, version 2.
  6. *
  7. * see Documentation/dvb/README.dvb-usb for more information
  8. */
  9. #include "gl861.h"
  10. #include "zl10353.h"
  11. #include "qt1010.h"
  12. /* debug */
  13. static int dvb_usb_gl861_debug;
  14. module_param_named(debug,dvb_usb_gl861_debug, int, 0644);
  15. MODULE_PARM_DESC(debug, "set debugging level (1=rc (or-able))." DVB_USB_DEBUG_STATUS);
  16. static int gl861_i2c_msg(struct dvb_usb_device *d, u8 addr,
  17. u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen)
  18. {
  19. u16 index;
  20. u16 value = addr << (8 + 1);
  21. int wo = (rbuf == NULL || rlen == 0); /* write-only */
  22. u8 req, type;
  23. if (wo) {
  24. req = GL861_REQ_I2C_WRITE;
  25. type = GL861_WRITE;
  26. } else { /* rw */
  27. req = GL861_REQ_I2C_READ;
  28. type = GL861_READ;
  29. }
  30. switch (wlen) {
  31. case 1:
  32. index = wbuf[0];
  33. break;
  34. case 2:
  35. index = wbuf[0];
  36. value = value + wbuf[1];
  37. break;
  38. default:
  39. warn("wlen = %x, aborting.", wlen);
  40. return -EINVAL;
  41. }
  42. return usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0), req, type,
  43. value, index, rbuf, rlen, 2000);
  44. }
  45. /* I2C */
  46. static int gl861_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
  47. int num)
  48. {
  49. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  50. int i;
  51. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  52. return -EAGAIN;
  53. if (num > 2)
  54. return -EINVAL;
  55. for (i = 0; i < num; i++) {
  56. /* write/read request */
  57. if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) {
  58. if (gl861_i2c_msg(d, msg[i].addr, msg[i].buf,
  59. msg[i].len, msg[i+1].buf, msg[i+1].len) < 0)
  60. break;
  61. i++;
  62. } else
  63. if (gl861_i2c_msg(d, msg[i].addr, msg[i].buf,
  64. msg[i].len, NULL, 0) < 0)
  65. break;
  66. }
  67. mutex_unlock(&d->i2c_mutex);
  68. return i;
  69. }
  70. static u32 gl861_i2c_func(struct i2c_adapter *adapter)
  71. {
  72. return I2C_FUNC_I2C;
  73. }
  74. static struct i2c_algorithm gl861_i2c_algo = {
  75. .master_xfer = gl861_i2c_xfer,
  76. .functionality = gl861_i2c_func,
  77. };
  78. /* Callbacks for DVB USB */
  79. static int gl861_identify_state(struct usb_device *udev,
  80. struct dvb_usb_device_properties *props,
  81. struct dvb_usb_device_description **desc,
  82. int *cold)
  83. {
  84. *cold = 0;
  85. return 0;
  86. }
  87. static struct zl10353_config gl861_zl10353_config = {
  88. .demod_address = 0x0f,
  89. .no_tuner = 1,
  90. .parallel_ts = 1,
  91. };
  92. static int gl861_frontend_attach(struct dvb_usb_adapter *adap)
  93. {
  94. if ((adap->fe = dvb_attach(zl10353_attach, &gl861_zl10353_config,
  95. &adap->dev->i2c_adap)) != NULL) {
  96. return 0;
  97. }
  98. return -EIO;
  99. }
  100. static struct qt1010_config gl861_qt1010_config = {
  101. .i2c_address = 0x62
  102. };
  103. static int gl861_tuner_attach(struct dvb_usb_adapter *adap)
  104. {
  105. return dvb_attach(qt1010_attach,
  106. adap->fe, &adap->dev->i2c_adap,
  107. &gl861_qt1010_config) == NULL ? -ENODEV : 0;
  108. }
  109. /* DVB USB Driver stuff */
  110. static struct dvb_usb_device_properties gl861_properties;
  111. static int gl861_probe(struct usb_interface *intf,
  112. const struct usb_device_id *id)
  113. {
  114. struct dvb_usb_device *d;
  115. struct usb_host_interface *alt;
  116. int ret;
  117. if (intf->num_altsetting < 2)
  118. return -ENODEV;
  119. if ((ret = dvb_usb_device_init(intf, &gl861_properties, THIS_MODULE, &d)) == 0) {
  120. alt = usb_altnum_to_altsetting(intf, 0);
  121. if (alt == NULL) {
  122. deb_rc("not alt found!\n");
  123. return -ENODEV;
  124. }
  125. ret = usb_set_interface(d->udev, alt->desc.bInterfaceNumber,
  126. alt->desc.bAlternateSetting);
  127. }
  128. return ret;
  129. }
  130. static struct usb_device_id gl861_table [] = {
  131. { USB_DEVICE(USB_VID_MSI, USB_PID_MSI_MEGASKY580_55801) },
  132. { } /* Terminating entry */
  133. };
  134. MODULE_DEVICE_TABLE (usb, gl861_table);
  135. static struct dvb_usb_device_properties gl861_properties = {
  136. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  137. .usb_ctrl = DEVICE_SPECIFIC,
  138. .size_of_priv = 0,
  139. .identify_state = gl861_identify_state,
  140. .num_adapters = 1,
  141. .adapter = {{
  142. .frontend_attach = gl861_frontend_attach,
  143. .tuner_attach = gl861_tuner_attach,
  144. .stream = {
  145. .type = USB_BULK,
  146. .count = 7,
  147. .endpoint = 0x81,
  148. .u = {
  149. .bulk = {
  150. .buffersize = 512,
  151. }
  152. }
  153. },
  154. }},
  155. .i2c_algo = &gl861_i2c_algo,
  156. .num_device_descs = 1,
  157. .devices = {
  158. { "MSI Mega Sky 55801 DVB-T USB2.0",
  159. { &gl861_table[0], NULL },
  160. { NULL },
  161. },
  162. }
  163. };
  164. static struct usb_driver gl861_driver = {
  165. .name = "dvb_usb_gl861",
  166. .probe = gl861_probe,
  167. .disconnect = dvb_usb_device_exit,
  168. .id_table = gl861_table,
  169. };
  170. /* module stuff */
  171. static int __init gl861_module_init(void)
  172. {
  173. int ret;
  174. if ((ret = usb_register(&gl861_driver))) {
  175. err("usb_register failed. Error number %d", ret);
  176. return ret;
  177. }
  178. return 0;
  179. }
  180. static void __exit gl861_module_exit(void)
  181. {
  182. /* deregister this driver from the USB subsystem */
  183. usb_deregister(&gl861_driver);
  184. }
  185. module_init (gl861_module_init);
  186. module_exit (gl861_module_exit);
  187. MODULE_AUTHOR("Carl Lundqvist <comabug@gmail.com>");
  188. MODULE_DESCRIPTION("Driver MSI Mega Sky 580 DVB-T USB2.0 / GL861");
  189. MODULE_VERSION("0.1");
  190. MODULE_LICENSE("GPL");