gl861.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. 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;
  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 = 0x1e,
  89. .no_tuner = 1,
  90. };
  91. static int gl861_frontend_attach(struct dvb_usb_adapter *adap)
  92. {
  93. if ((adap->fe = dvb_attach(zl10353_attach, &gl861_zl10353_config,
  94. &adap->dev->i2c_adap)) != NULL) {
  95. return 0;
  96. }
  97. return -EIO;
  98. }
  99. /* DVB USB Driver stuff */
  100. static struct dvb_usb_device_properties gl861_properties;
  101. static int gl861_probe(struct usb_interface *intf,
  102. const struct usb_device_id *id)
  103. {
  104. struct dvb_usb_device *d;
  105. struct usb_host_interface *alt;
  106. int ret;
  107. if (intf->num_altsetting < 2)
  108. return -ENODEV;
  109. if ((ret = dvb_usb_device_init(intf, &gl861_properties, THIS_MODULE, &d)) == 0) {
  110. alt = usb_altnum_to_altsetting(intf, 0);
  111. if (alt == NULL) {
  112. deb_rc("not alt found!\n");
  113. return -ENODEV;
  114. }
  115. ret = usb_set_interface(d->udev, alt->desc.bInterfaceNumber,
  116. alt->desc.bAlternateSetting);
  117. }
  118. return ret;
  119. }
  120. static struct usb_device_id gl861_table [] = {
  121. { USB_DEVICE(USB_VID_MSI, USB_PID_MSI_MEGASKY580_55801) },
  122. { } /* Terminating entry */
  123. };
  124. MODULE_DEVICE_TABLE (usb, gl861_table);
  125. static struct dvb_usb_device_properties gl861_properties = {
  126. .usb_ctrl = DEVICE_SPECIFIC,
  127. .size_of_priv = 0,
  128. .identify_state = gl861_identify_state,
  129. .num_adapters = 1,
  130. .adapter = {{
  131. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  132. .frontend_attach = gl861_frontend_attach,
  133. .tuner_attach = qt1010_tuner_attach,
  134. .stream = {
  135. .type = USB_BULK,
  136. .count = 7,
  137. .endpoint = 0x81,
  138. .u = {
  139. .bulk = {
  140. .buffersize = 512,
  141. }
  142. }
  143. },
  144. }},
  145. .i2c_algo = &gl861_i2c_algo,
  146. .generic_bulk_ctrl_endpoint = 0x01,
  147. .num_device_descs = 1,
  148. .devices = {
  149. { "MSI Mega Sky 55801 DVB-T USB2.0",
  150. { &gl861_table[0], NULL },
  151. { NULL },
  152. },
  153. }
  154. };
  155. static struct usb_driver gl861_driver = {
  156. .name = "dvb_usb_gl861",
  157. .probe = gl861_probe,
  158. .disconnect = dvb_usb_device_exit,
  159. .id_table = gl861_table,
  160. };
  161. /* module stuff */
  162. static int __init gl861_module_init(void)
  163. {
  164. int ret;
  165. if ((ret = usb_register(&gl861_driver))) {
  166. err("usb_register failed. Error number %d", ret);
  167. return ret;
  168. }
  169. return 0;
  170. }
  171. static void __exit gl861_module_exit(void)
  172. {
  173. /* deregister this driver from the USB subsystem */
  174. usb_deregister(&gl861_driver);
  175. }
  176. module_init (gl861_module_init);
  177. module_exit (gl861_module_exit);
  178. MODULE_AUTHOR("Carl Lundqvist <comabug@gmail.com>");
  179. MODULE_DESCRIPTION("Driver MSI Mega Sky 580 DVB-T USB2.0 / GL861");
  180. MODULE_VERSION("1.0");
  181. MODULE_LICENSE("GPL");