gl861.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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
  5. * Free 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. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  13. static int gl861_i2c_msg(struct dvb_usb_device *d, u8 addr,
  14. u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen)
  15. {
  16. u16 index;
  17. u16 value = addr << (8 + 1);
  18. int wo = (rbuf == NULL || rlen == 0); /* write-only */
  19. u8 req, type;
  20. u8 *buf;
  21. int ret;
  22. if (wo) {
  23. req = GL861_REQ_I2C_WRITE;
  24. type = GL861_WRITE;
  25. } else { /* rw */
  26. req = GL861_REQ_I2C_READ;
  27. type = GL861_READ;
  28. }
  29. switch (wlen) {
  30. case 1:
  31. index = wbuf[0];
  32. break;
  33. case 2:
  34. index = wbuf[0];
  35. value = value + wbuf[1];
  36. break;
  37. default:
  38. dev_err(&d->udev->dev, "%s: wlen=%d, aborting\n",
  39. KBUILD_MODNAME, wlen);
  40. return -EINVAL;
  41. }
  42. buf = NULL;
  43. if (rlen > 0) {
  44. buf = kmalloc(rlen, GFP_KERNEL);
  45. if (!buf)
  46. return -ENOMEM;
  47. }
  48. usleep_range(1000, 2000); /* avoid I2C errors */
  49. ret = usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0), req, type,
  50. value, index, buf, rlen, 2000);
  51. if (rlen > 0) {
  52. if (ret > 0)
  53. memcpy(rbuf, buf, rlen);
  54. kfree(buf);
  55. }
  56. return ret;
  57. }
  58. /* I2C */
  59. static int gl861_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
  60. int num)
  61. {
  62. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  63. int i;
  64. if (num > 2)
  65. return -EINVAL;
  66. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  67. return -EAGAIN;
  68. for (i = 0; i < num; i++) {
  69. /* write/read request */
  70. if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) {
  71. if (gl861_i2c_msg(d, msg[i].addr, msg[i].buf,
  72. msg[i].len, msg[i+1].buf, msg[i+1].len) < 0)
  73. break;
  74. i++;
  75. } else
  76. if (gl861_i2c_msg(d, msg[i].addr, msg[i].buf,
  77. msg[i].len, NULL, 0) < 0)
  78. break;
  79. }
  80. mutex_unlock(&d->i2c_mutex);
  81. return i;
  82. }
  83. static u32 gl861_i2c_func(struct i2c_adapter *adapter)
  84. {
  85. return I2C_FUNC_I2C;
  86. }
  87. static struct i2c_algorithm gl861_i2c_algo = {
  88. .master_xfer = gl861_i2c_xfer,
  89. .functionality = gl861_i2c_func,
  90. };
  91. /* Callbacks for DVB USB */
  92. static struct zl10353_config gl861_zl10353_config = {
  93. .demod_address = 0x0f,
  94. .no_tuner = 1,
  95. .parallel_ts = 1,
  96. };
  97. static int gl861_frontend_attach(struct dvb_usb_adapter *adap)
  98. {
  99. adap->fe[0] = dvb_attach(zl10353_attach, &gl861_zl10353_config,
  100. &adap_to_d(adap)->i2c_adap);
  101. if (adap->fe[0] == NULL)
  102. return -EIO;
  103. return 0;
  104. }
  105. static struct qt1010_config gl861_qt1010_config = {
  106. .i2c_address = 0x62
  107. };
  108. static int gl861_tuner_attach(struct dvb_usb_adapter *adap)
  109. {
  110. return dvb_attach(qt1010_attach,
  111. adap->fe[0], &adap_to_d(adap)->i2c_adap,
  112. &gl861_qt1010_config) == NULL ? -ENODEV : 0;
  113. }
  114. static int gl861_init(struct dvb_usb_device *d)
  115. {
  116. /*
  117. * There is 2 interfaces. Interface 0 is for TV and interface 1 is
  118. * for HID remote controller. Interface 0 has 2 alternate settings.
  119. * For some reason we need to set interface explicitly, defaulted
  120. * as alternate setting 1?
  121. */
  122. return usb_set_interface(d->udev, 0, 0);
  123. }
  124. /* DVB USB Driver stuff */
  125. static struct dvb_usb_device_properties gl861_props = {
  126. .driver_name = KBUILD_MODNAME,
  127. .owner = THIS_MODULE,
  128. .adapter_nr = adapter_nr,
  129. .i2c_algo = &gl861_i2c_algo,
  130. .frontend_attach = gl861_frontend_attach,
  131. .tuner_attach = gl861_tuner_attach,
  132. .init = gl861_init,
  133. .num_adapters = 1,
  134. .adapter = {
  135. {
  136. .stream = DVB_USB_STREAM_BULK(0x81, 7, 512),
  137. }
  138. }
  139. };
  140. static const struct usb_device_id gl861_id_table[] = {
  141. { DVB_USB_DEVICE(USB_VID_MSI, USB_PID_MSI_MEGASKY580_55801,
  142. &gl861_props, "MSI Mega Sky 55801 DVB-T USB2.0", NULL) },
  143. { DVB_USB_DEVICE(USB_VID_ALINK, USB_VID_ALINK_DTU,
  144. &gl861_props, "A-LINK DTU DVB-T USB2.0", NULL) },
  145. { }
  146. };
  147. MODULE_DEVICE_TABLE(usb, gl861_id_table);
  148. static struct usb_driver gl861_usb_driver = {
  149. .name = KBUILD_MODNAME,
  150. .id_table = gl861_id_table,
  151. .probe = dvb_usbv2_probe,
  152. .disconnect = dvb_usbv2_disconnect,
  153. .suspend = dvb_usbv2_suspend,
  154. .resume = dvb_usbv2_resume,
  155. .reset_resume = dvb_usbv2_reset_resume,
  156. .no_dynamic_id = 1,
  157. .soft_unbind = 1,
  158. };
  159. module_usb_driver(gl861_usb_driver);
  160. MODULE_AUTHOR("Carl Lundqvist <comabug@gmail.com>");
  161. MODULE_DESCRIPTION("Driver MSI Mega Sky 580 DVB-T USB2.0 / GL861");
  162. MODULE_VERSION("0.1");
  163. MODULE_LICENSE("GPL");