mantis_dvb.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. Mantis PCI bridge driver
  3. Copyright (C) Manu Abraham (abraham.manu@gmail.com)
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/bitops.h>
  18. #include <linux/signal.h>
  19. #include <linux/sched.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/pci.h>
  22. #include <linux/i2c.h>
  23. #include "dmxdev.h"
  24. #include "dvbdev.h"
  25. #include "dvb_demux.h"
  26. #include "dvb_frontend.h"
  27. #include "dvb_net.h"
  28. #include "mantis_common.h"
  29. #include "mantis_dma.h"
  30. #include "mantis_ca.h"
  31. #include "mantis_ioc.h"
  32. #include "mantis_dvb.h"
  33. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  34. int mantis_frontend_power(struct mantis_pci *mantis, enum mantis_power power)
  35. {
  36. struct mantis_hwconfig *config = mantis->hwconfig;
  37. switch (power) {
  38. case POWER_ON:
  39. dprintk(MANTIS_DEBUG, 1, "Power ON");
  40. gpio_set_bits(mantis, config->power, POWER_ON);
  41. msleep(100);
  42. gpio_set_bits(mantis, config->power, POWER_ON);
  43. msleep(100);
  44. break;
  45. case POWER_OFF:
  46. dprintk(MANTIS_DEBUG, 1, "Power OFF");
  47. gpio_set_bits(mantis, config->power, POWER_OFF);
  48. msleep(100);
  49. break;
  50. default:
  51. dprintk(MANTIS_DEBUG, 1, "Unknown state <%02x>", power);
  52. return -1;
  53. }
  54. return 0;
  55. }
  56. EXPORT_SYMBOL_GPL(mantis_frontend_power);
  57. void mantis_frontend_soft_reset(struct mantis_pci *mantis)
  58. {
  59. struct mantis_hwconfig *config = mantis->hwconfig;
  60. dprintk(MANTIS_DEBUG, 1, "Frontend RESET");
  61. gpio_set_bits(mantis, config->reset, 0);
  62. msleep(100);
  63. gpio_set_bits(mantis, config->reset, 0);
  64. msleep(100);
  65. gpio_set_bits(mantis, config->reset, 1);
  66. msleep(100);
  67. gpio_set_bits(mantis, config->reset, 1);
  68. msleep(100);
  69. return;
  70. }
  71. EXPORT_SYMBOL_GPL(mantis_frontend_soft_reset);
  72. static int mantis_frontend_shutdown(struct mantis_pci *mantis)
  73. {
  74. int err;
  75. mantis_frontend_soft_reset(mantis);
  76. err = mantis_frontend_power(mantis, POWER_OFF);
  77. if (err != 0) {
  78. dprintk(MANTIS_ERROR, 1, "Frontend POWER OFF failed! <%d>", err);
  79. return 1;
  80. }
  81. return 0;
  82. }
  83. static int mantis_dvb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
  84. {
  85. struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
  86. struct mantis_pci *mantis = dvbdmx->priv;
  87. dprintk(MANTIS_DEBUG, 1, "Mantis DVB Start feed");
  88. if (!dvbdmx->dmx.frontend) {
  89. dprintk(MANTIS_DEBUG, 1, "no frontend ?");
  90. return -EINVAL;
  91. }
  92. mantis->feeds++;
  93. dprintk(MANTIS_DEBUG, 1, "mantis start feed, feeds=%d", mantis->feeds);
  94. if (mantis->feeds == 1) {
  95. dprintk(MANTIS_DEBUG, 1, "mantis start feed & dma");
  96. mantis_dma_start(mantis);
  97. }
  98. return mantis->feeds;
  99. }
  100. static int mantis_dvb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
  101. {
  102. struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
  103. struct mantis_pci *mantis = dvbdmx->priv;
  104. dprintk(MANTIS_DEBUG, 1, "Mantis DVB Stop feed");
  105. if (!dvbdmx->dmx.frontend) {
  106. dprintk(MANTIS_DEBUG, 1, "no frontend ?");
  107. return -EINVAL;
  108. }
  109. mantis->feeds--;
  110. if (mantis->feeds == 0) {
  111. dprintk(MANTIS_DEBUG, 1, "mantis stop feed and dma");
  112. mantis_dma_stop(mantis);
  113. }
  114. return 0;
  115. }
  116. int __devinit mantis_dvb_init(struct mantis_pci *mantis)
  117. {
  118. struct mantis_hwconfig *config = mantis->hwconfig;
  119. int result = -1;
  120. dprintk(MANTIS_DEBUG, 1, "dvb_register_adapter");
  121. result = dvb_register_adapter(&mantis->dvb_adapter,
  122. "Mantis DVB adapter",
  123. THIS_MODULE,
  124. &mantis->pdev->dev,
  125. adapter_nr);
  126. if (result < 0) {
  127. dprintk(MANTIS_ERROR, 1, "Error registering adapter");
  128. return -ENODEV;
  129. }
  130. mantis->dvb_adapter.priv = mantis;
  131. mantis->demux.dmx.capabilities = DMX_TS_FILTERING |
  132. DMX_SECTION_FILTERING |
  133. DMX_MEMORY_BASED_FILTERING;
  134. mantis->demux.priv = mantis;
  135. mantis->demux.filternum = 256;
  136. mantis->demux.feednum = 256;
  137. mantis->demux.start_feed = mantis_dvb_start_feed;
  138. mantis->demux.stop_feed = mantis_dvb_stop_feed;
  139. mantis->demux.write_to_decoder = NULL;
  140. dprintk(MANTIS_DEBUG, 1, "dvb_dmx_init");
  141. result = dvb_dmx_init(&mantis->demux);
  142. if (result < 0) {
  143. dprintk(MANTIS_ERROR, 1, "dvb_dmx_init failed, ERROR=%d", result);
  144. goto err0;
  145. }
  146. mantis->dmxdev.filternum = 256;
  147. mantis->dmxdev.demux = &mantis->demux.dmx;
  148. mantis->dmxdev.capabilities = 0;
  149. dprintk(MANTIS_DEBUG, 1, "dvb_dmxdev_init");
  150. result = dvb_dmxdev_init(&mantis->dmxdev, &mantis->dvb_adapter);
  151. if (result < 0) {
  152. dprintk(MANTIS_ERROR, 1, "dvb_dmxdev_init failed, ERROR=%d", result);
  153. goto err1;
  154. }
  155. mantis->fe_hw.source = DMX_FRONTEND_0;
  156. result = mantis->demux.dmx.add_frontend(&mantis->demux.dmx, &mantis->fe_hw);
  157. if (result < 0) {
  158. dprintk(MANTIS_ERROR, 1, "dvb_dmx_init failed, ERROR=%d", result);
  159. goto err2;
  160. }
  161. mantis->fe_mem.source = DMX_MEMORY_FE;
  162. result = mantis->demux.dmx.add_frontend(&mantis->demux.dmx, &mantis->fe_mem);
  163. if (result < 0) {
  164. dprintk(MANTIS_ERROR, 1, "dvb_dmx_init failed, ERROR=%d", result);
  165. goto err3;
  166. }
  167. result = mantis->demux.dmx.connect_frontend(&mantis->demux.dmx, &mantis->fe_hw);
  168. if (result < 0) {
  169. dprintk(MANTIS_ERROR, 1, "dvb_dmx_init failed, ERROR=%d", result);
  170. goto err4;
  171. }
  172. dvb_net_init(&mantis->dvb_adapter, &mantis->dvbnet, &mantis->demux.dmx);
  173. tasklet_init(&mantis->tasklet, mantis_dma_xfer, (unsigned long) mantis);
  174. if (mantis->hwconfig) {
  175. result = config->frontend_init(mantis, mantis->fe);
  176. if (result < 0) {
  177. dprintk(MANTIS_ERROR, 1, "!!! NO Frontends found !!!");
  178. goto err5;
  179. } else {
  180. if (mantis->fe == NULL) {
  181. dprintk(MANTIS_ERROR, 1, "FE <NULL>");
  182. goto err5;
  183. }
  184. if (dvb_register_frontend(&mantis->dvb_adapter, mantis->fe)) {
  185. dprintk(MANTIS_ERROR, 1, "ERROR: Frontend registration failed");
  186. if (mantis->fe->ops.release)
  187. mantis->fe->ops.release(mantis->fe);
  188. mantis->fe = NULL;
  189. goto err5;
  190. }
  191. }
  192. }
  193. return 0;
  194. /* Error conditions .. */
  195. err5:
  196. tasklet_kill(&mantis->tasklet);
  197. dvb_net_release(&mantis->dvbnet);
  198. dvb_unregister_frontend(mantis->fe);
  199. dvb_frontend_detach(mantis->fe);
  200. err4:
  201. mantis->demux.dmx.remove_frontend(&mantis->demux.dmx, &mantis->fe_mem);
  202. err3:
  203. mantis->demux.dmx.remove_frontend(&mantis->demux.dmx, &mantis->fe_hw);
  204. err2:
  205. dvb_dmxdev_release(&mantis->dmxdev);
  206. err1:
  207. dvb_dmx_release(&mantis->demux);
  208. err0:
  209. dvb_unregister_adapter(&mantis->dvb_adapter);
  210. return result;
  211. }
  212. EXPORT_SYMBOL_GPL(mantis_dvb_init);
  213. int __devexit mantis_dvb_exit(struct mantis_pci *mantis)
  214. {
  215. int err;
  216. if (mantis->fe) {
  217. /* mantis_ca_exit(mantis); */
  218. err = mantis_frontend_shutdown(mantis);
  219. if (err != 0)
  220. dprintk(MANTIS_ERROR, 1, "Frontend exit while POWER ON! <%d>", err);
  221. dvb_unregister_frontend(mantis->fe);
  222. dvb_frontend_detach(mantis->fe);
  223. }
  224. tasklet_kill(&mantis->tasklet);
  225. dvb_net_release(&mantis->dvbnet);
  226. mantis->demux.dmx.remove_frontend(&mantis->demux.dmx, &mantis->fe_mem);
  227. mantis->demux.dmx.remove_frontend(&mantis->demux.dmx, &mantis->fe_hw);
  228. dvb_dmxdev_release(&mantis->dmxdev);
  229. dvb_dmx_release(&mantis->demux);
  230. dprintk(MANTIS_DEBUG, 1, "dvb_unregister_adapter");
  231. dvb_unregister_adapter(&mantis->dvb_adapter);
  232. return 0;
  233. }
  234. EXPORT_SYMBOL_GPL(mantis_dvb_exit);