mantis_dvb.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. mantis_gpio_set_bits(mantis, config->power, POWER_ON);
  41. msleep(100);
  42. mantis_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. mantis_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. mantis_gpio_set_bits(mantis, config->reset, 0);
  62. msleep(100);
  63. mantis_gpio_set_bits(mantis, config->reset, 0);
  64. msleep(100);
  65. mantis_gpio_set_bits(mantis, config->reset, 1);
  66. msleep(100);
  67. mantis_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. tasklet_enable(&mantis->tasklet);
  98. }
  99. return mantis->feeds;
  100. }
  101. static int mantis_dvb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
  102. {
  103. struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
  104. struct mantis_pci *mantis = dvbdmx->priv;
  105. dprintk(MANTIS_DEBUG, 1, "Mantis DVB Stop feed");
  106. if (!dvbdmx->dmx.frontend) {
  107. dprintk(MANTIS_DEBUG, 1, "no frontend ?");
  108. return -EINVAL;
  109. }
  110. mantis->feeds--;
  111. if (mantis->feeds == 0) {
  112. dprintk(MANTIS_DEBUG, 1, "mantis stop feed and dma");
  113. tasklet_disable(&mantis->tasklet);
  114. mantis_dma_stop(mantis);
  115. }
  116. return 0;
  117. }
  118. int mantis_dvb_init(struct mantis_pci *mantis)
  119. {
  120. struct mantis_hwconfig *config = mantis->hwconfig;
  121. int result = -1;
  122. dprintk(MANTIS_DEBUG, 1, "dvb_register_adapter");
  123. result = dvb_register_adapter(&mantis->dvb_adapter,
  124. "Mantis DVB adapter",
  125. THIS_MODULE,
  126. &mantis->pdev->dev,
  127. adapter_nr);
  128. if (result < 0) {
  129. dprintk(MANTIS_ERROR, 1, "Error registering adapter");
  130. return -ENODEV;
  131. }
  132. mantis->dvb_adapter.priv = mantis;
  133. mantis->demux.dmx.capabilities = DMX_TS_FILTERING |
  134. DMX_SECTION_FILTERING |
  135. DMX_MEMORY_BASED_FILTERING;
  136. mantis->demux.priv = mantis;
  137. mantis->demux.filternum = 256;
  138. mantis->demux.feednum = 256;
  139. mantis->demux.start_feed = mantis_dvb_start_feed;
  140. mantis->demux.stop_feed = mantis_dvb_stop_feed;
  141. mantis->demux.write_to_decoder = NULL;
  142. dprintk(MANTIS_DEBUG, 1, "dvb_dmx_init");
  143. result = dvb_dmx_init(&mantis->demux);
  144. if (result < 0) {
  145. dprintk(MANTIS_ERROR, 1, "dvb_dmx_init failed, ERROR=%d", result);
  146. goto err0;
  147. }
  148. mantis->dmxdev.filternum = 256;
  149. mantis->dmxdev.demux = &mantis->demux.dmx;
  150. mantis->dmxdev.capabilities = 0;
  151. dprintk(MANTIS_DEBUG, 1, "dvb_dmxdev_init");
  152. result = dvb_dmxdev_init(&mantis->dmxdev, &mantis->dvb_adapter);
  153. if (result < 0) {
  154. dprintk(MANTIS_ERROR, 1, "dvb_dmxdev_init failed, ERROR=%d", result);
  155. goto err1;
  156. }
  157. mantis->fe_hw.source = DMX_FRONTEND_0;
  158. result = mantis->demux.dmx.add_frontend(&mantis->demux.dmx, &mantis->fe_hw);
  159. if (result < 0) {
  160. dprintk(MANTIS_ERROR, 1, "dvb_dmx_init failed, ERROR=%d", result);
  161. goto err2;
  162. }
  163. mantis->fe_mem.source = DMX_MEMORY_FE;
  164. result = mantis->demux.dmx.add_frontend(&mantis->demux.dmx, &mantis->fe_mem);
  165. if (result < 0) {
  166. dprintk(MANTIS_ERROR, 1, "dvb_dmx_init failed, ERROR=%d", result);
  167. goto err3;
  168. }
  169. result = mantis->demux.dmx.connect_frontend(&mantis->demux.dmx, &mantis->fe_hw);
  170. if (result < 0) {
  171. dprintk(MANTIS_ERROR, 1, "dvb_dmx_init failed, ERROR=%d", result);
  172. goto err4;
  173. }
  174. dvb_net_init(&mantis->dvb_adapter, &mantis->dvbnet, &mantis->demux.dmx);
  175. tasklet_init(&mantis->tasklet, mantis_dma_xfer, (unsigned long) mantis);
  176. tasklet_disable(&mantis->tasklet);
  177. if (mantis->hwconfig) {
  178. result = config->frontend_init(mantis, mantis->fe);
  179. if (result < 0) {
  180. dprintk(MANTIS_ERROR, 1, "!!! NO Frontends found !!!");
  181. goto err5;
  182. } else {
  183. if (mantis->fe == NULL) {
  184. dprintk(MANTIS_ERROR, 1, "FE <NULL>");
  185. goto err5;
  186. }
  187. if (dvb_register_frontend(&mantis->dvb_adapter, mantis->fe)) {
  188. dprintk(MANTIS_ERROR, 1, "ERROR: Frontend registration failed");
  189. if (mantis->fe->ops.release)
  190. mantis->fe->ops.release(mantis->fe);
  191. mantis->fe = NULL;
  192. goto err5;
  193. }
  194. }
  195. }
  196. return 0;
  197. /* Error conditions .. */
  198. err5:
  199. tasklet_kill(&mantis->tasklet);
  200. dvb_net_release(&mantis->dvbnet);
  201. if (mantis->fe) {
  202. dvb_unregister_frontend(mantis->fe);
  203. dvb_frontend_detach(mantis->fe);
  204. }
  205. err4:
  206. mantis->demux.dmx.remove_frontend(&mantis->demux.dmx, &mantis->fe_mem);
  207. err3:
  208. mantis->demux.dmx.remove_frontend(&mantis->demux.dmx, &mantis->fe_hw);
  209. err2:
  210. dvb_dmxdev_release(&mantis->dmxdev);
  211. err1:
  212. dvb_dmx_release(&mantis->demux);
  213. err0:
  214. dvb_unregister_adapter(&mantis->dvb_adapter);
  215. return result;
  216. }
  217. EXPORT_SYMBOL_GPL(mantis_dvb_init);
  218. int mantis_dvb_exit(struct mantis_pci *mantis)
  219. {
  220. int err;
  221. if (mantis->fe) {
  222. /* mantis_ca_exit(mantis); */
  223. err = mantis_frontend_shutdown(mantis);
  224. if (err != 0)
  225. dprintk(MANTIS_ERROR, 1, "Frontend exit while POWER ON! <%d>", err);
  226. dvb_unregister_frontend(mantis->fe);
  227. dvb_frontend_detach(mantis->fe);
  228. }
  229. tasklet_kill(&mantis->tasklet);
  230. dvb_net_release(&mantis->dvbnet);
  231. mantis->demux.dmx.remove_frontend(&mantis->demux.dmx, &mantis->fe_mem);
  232. mantis->demux.dmx.remove_frontend(&mantis->demux.dmx, &mantis->fe_hw);
  233. dvb_dmxdev_release(&mantis->dmxdev);
  234. dvb_dmx_release(&mantis->demux);
  235. dprintk(MANTIS_DEBUG, 1, "dvb_unregister_adapter");
  236. dvb_unregister_adapter(&mantis->dvb_adapter);
  237. return 0;
  238. }
  239. EXPORT_SYMBOL_GPL(mantis_dvb_exit);