blob: be911d76daa7fd35f9e8d19bd99a6dd8ff7e8e51 [file] [log] [blame]
Manu Abraham41e840b2009-12-02 21:57:10 -03001/*
2 Mantis PCI bridge driver
3 Copyright (C) 2005, 2006 Manu Abraham (abraham.manu@gmail.com)
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/
19
Manu Abrahamb3b96142009-12-04 05:41:11 -030020#include <linux/kernel.h>
Manu Abraham41e840b2009-12-02 21:57:10 -030021#include <linux/bitops.h>
Manu Abrahamb3b96142009-12-04 05:41:11 -030022
23#include <asm/irq.h>
24#include <linux/signal.h>
25#include <linux/sched.h>
26#include <linux/interrupt.h>
27#include <linux/pci.h>
28#include <linux/i2c.h>
Manu Abraham41e840b2009-12-02 21:57:10 -030029
30#include "dmxdev.h"
31#include "dvbdev.h"
32#include "dvb_demux.h"
33#include "dvb_frontend.h"
Manu Abrahamb3b96142009-12-04 05:41:11 -030034#include "dvb_net.h"
35
36#include "mantis_common.h"
37#include "mantis_dma.h"
38#include "mantis_ca.h"
39#include "mantis_ioc.h"
40#include "mantis_dvb.h"
Manu Abraham41e840b2009-12-02 21:57:10 -030041
Manu Abraham616f75e2009-12-04 05:33:56 -030042DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
43
Manu Abrahamb3b96142009-12-04 05:41:11 -030044int mantis_frontend_power(struct mantis_pci *mantis, enum mantis_power power)
Manu Abraham41e840b2009-12-02 21:57:10 -030045{
Manu Abrahamb3b96142009-12-04 05:41:11 -030046 struct mantis_hwconfig *config = mantis->hwconfig;
Manu Abraham41e840b2009-12-02 21:57:10 -030047
Manu Abrahamb3b96142009-12-04 05:41:11 -030048 switch (power) {
49 case POWER_ON:
50 dprintk(MANTIS_DEBUG, 1, "Power ON");
51 gpio_set_bits(mantis, config->power, POWER_ON);
52 msleep(100);
53 gpio_set_bits(mantis, config->power, POWER_ON);
54 msleep(100);
55 break;
Manu Abraham41e840b2009-12-02 21:57:10 -030056
Manu Abrahamb3b96142009-12-04 05:41:11 -030057 case POWER_OFF:
58 dprintk(MANTIS_DEBUG, 1, "Power OFF");
59 gpio_set_bits(mantis, config->power, POWER_OFF);
60 msleep(100);
61 break;
Manu Abraham41e840b2009-12-02 21:57:10 -030062
Manu Abrahamb3b96142009-12-04 05:41:11 -030063 default:
64 dprintk(MANTIS_DEBUG, 1, "Unknown state <%02x>", power);
65 return -1;
66 }
Manu Abraham41e840b2009-12-02 21:57:10 -030067
68 return 0;
69}
Manu Abrahamb3b96142009-12-04 05:41:11 -030070EXPORT_SYMBOL_GPL(mantis_frontend_power);
Manu Abraham41e840b2009-12-02 21:57:10 -030071
Manu Abrahamb3b96142009-12-04 05:41:11 -030072void mantis_frontend_soft_reset(struct mantis_pci *mantis)
Manu Abraham41e840b2009-12-02 21:57:10 -030073{
Manu Abrahamb3b96142009-12-04 05:41:11 -030074 struct mantis_hwconfig *config = mantis->hwconfig;
75
76 dprintk(MANTIS_DEBUG, 1, "Frontend RESET");
77 gpio_set_bits(mantis, config->reset, 0);
78 msleep(100);
79 gpio_set_bits(mantis, config->reset, 0);
80 msleep(100);
81 gpio_set_bits(mantis, config->reset, 1);
82 msleep(100);
83 gpio_set_bits(mantis, config->reset, 1);
84 msleep(100);
85
86 return;
87}
88EXPORT_SYMBOL_GPL(mantis_frontend_soft_reset);
89
90static int mantis_frontend_shutdown(struct mantis_pci *mantis)
91{
92 int err;
93
94 mantis_frontend_soft_reset(mantis);
95 err = mantis_frontend_power(mantis, POWER_OFF);
96 if (err != 0) {
97 dprintk(MANTIS_ERROR, 1, "Frontend POWER OFF failed! <%d>", err);
98 return 1;
99 }
Manu Abraham41e840b2009-12-02 21:57:10 -0300100
101 return 0;
102}
103
104static int mantis_dvb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
105{
106 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
107 struct mantis_pci *mantis = dvbdmx->priv;
108
Manu Abrahamb3b96142009-12-04 05:41:11 -0300109 dprintk(MANTIS_DEBUG, 1, "Mantis DVB Start feed");
Manu Abraham41e840b2009-12-02 21:57:10 -0300110 if (!dvbdmx->dmx.frontend) {
Manu Abrahamb3b96142009-12-04 05:41:11 -0300111 dprintk(MANTIS_DEBUG, 1, "no frontend ?");
Manu Abraham41e840b2009-12-02 21:57:10 -0300112 return -EINVAL;
113 }
Manu Abrahamb3b96142009-12-04 05:41:11 -0300114
Manu Abraham41e840b2009-12-02 21:57:10 -0300115 mantis->feeds++;
Manu Abrahamb3b96142009-12-04 05:41:11 -0300116 dprintk(MANTIS_DEBUG, 1, "mantis start feed, feeds=%d", mantis->feeds);
Manu Abraham41e840b2009-12-02 21:57:10 -0300117
118 if (mantis->feeds == 1) {
Manu Abrahamb3b96142009-12-04 05:41:11 -0300119 dprintk(MANTIS_DEBUG, 1, "mantis start feed & dma");
Manu Abraham41e840b2009-12-02 21:57:10 -0300120 printk("mantis start feed & dma\n");
121 mantis_dma_start(mantis);
122 }
123
124 return mantis->feeds;
125}
126
127static int mantis_dvb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
128{
129 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
130 struct mantis_pci *mantis = dvbdmx->priv;
131
Manu Abrahamb3b96142009-12-04 05:41:11 -0300132 dprintk(MANTIS_DEBUG, 1, "Mantis DVB Stop feed");
Manu Abraham41e840b2009-12-02 21:57:10 -0300133 if (!dvbdmx->dmx.frontend) {
Manu Abrahamb3b96142009-12-04 05:41:11 -0300134 dprintk(MANTIS_DEBUG, 1, "no frontend ?");
Manu Abraham41e840b2009-12-02 21:57:10 -0300135 return -EINVAL;
136 }
Manu Abrahamb3b96142009-12-04 05:41:11 -0300137
Manu Abraham41e840b2009-12-02 21:57:10 -0300138 mantis->feeds--;
139 if (mantis->feeds == 0) {
Manu Abrahamb3b96142009-12-04 05:41:11 -0300140 dprintk(MANTIS_DEBUG, 1, "mantis stop feed and dma");
Manu Abraham41e840b2009-12-02 21:57:10 -0300141 printk("mantis stop feed and dma\n");
142 mantis_dma_stop(mantis);
143 }
Manu Abrahamb3b96142009-12-04 05:41:11 -0300144
Manu Abraham41e840b2009-12-02 21:57:10 -0300145 return 0;
146}
147
148int __devinit mantis_dvb_init(struct mantis_pci *mantis)
149{
Manu Abrahamb3b96142009-12-04 05:41:11 -0300150 struct mantis_hwconfig *config = mantis->hwconfig;
151 int result = -1;
Manu Abraham41e840b2009-12-02 21:57:10 -0300152
Manu Abrahamb3b96142009-12-04 05:41:11 -0300153 dprintk(MANTIS_DEBUG, 1, "dvb_register_adapter");
Manu Abraham41e840b2009-12-02 21:57:10 -0300154
Manu Abrahamb3b96142009-12-04 05:41:11 -0300155 result = dvb_register_adapter(&mantis->dvb_adapter,
156 "Mantis DVB adapter",
157 THIS_MODULE,
158 &mantis->pdev->dev,
159 adapter_nr);
160
161 if (result < 0) {
162
163 dprintk(MANTIS_ERROR, 1, "Error registering adapter");
Manu Abraham41e840b2009-12-02 21:57:10 -0300164 return -ENODEV;
165 }
Manu Abrahamb3b96142009-12-04 05:41:11 -0300166
167 mantis->dvb_adapter.priv = mantis;
168 mantis->demux.dmx.capabilities = DMX_TS_FILTERING |
Manu Abraham41e840b2009-12-02 21:57:10 -0300169 DMX_SECTION_FILTERING |
170 DMX_MEMORY_BASED_FILTERING;
171
Manu Abrahamb3b96142009-12-04 05:41:11 -0300172 mantis->demux.priv = mantis;
173 mantis->demux.filternum = 256;
174 mantis->demux.feednum = 256;
175 mantis->demux.start_feed = mantis_dvb_start_feed;
176 mantis->demux.stop_feed = mantis_dvb_stop_feed;
177 mantis->demux.write_to_decoder = NULL;
178
179 dprintk(MANTIS_DEBUG, 1, "dvb_dmx_init");
180 result = dvb_dmx_init(&mantis->demux);
181 if (result < 0) {
182 dprintk(MANTIS_ERROR, 1, "dvb_dmx_init failed, ERROR=%d", result);
Manu Abraham41e840b2009-12-02 21:57:10 -0300183
184 goto err0;
185 }
Manu Abraham41e840b2009-12-02 21:57:10 -0300186
Manu Abrahamb3b96142009-12-04 05:41:11 -0300187 mantis->dmxdev.filternum = 256;
188 mantis->dmxdev.demux = &mantis->demux.dmx;
189 mantis->dmxdev.capabilities = 0;
190 dprintk(MANTIS_DEBUG, 1, "dvb_dmxdev_init");
191
192 result = dvb_dmxdev_init(&mantis->dmxdev, &mantis->dvb_adapter);
193 if (result < 0) {
194
195 dprintk(MANTIS_ERROR, 1, "dvb_dmxdev_init failed, ERROR=%d", result);
Manu Abraham41e840b2009-12-02 21:57:10 -0300196 goto err1;
197 }
Manu Abraham41e840b2009-12-02 21:57:10 -0300198
Manu Abrahamb3b96142009-12-04 05:41:11 -0300199 mantis->fe_hw.source = DMX_FRONTEND_0;
200 result = mantis->demux.dmx.add_frontend(&mantis->demux.dmx, &mantis->fe_hw);
201 if (result < 0) {
Manu Abraham41e840b2009-12-02 21:57:10 -0300202
Manu Abrahamb3b96142009-12-04 05:41:11 -0300203 dprintk(MANTIS_ERROR, 1, "dvb_dmx_init failed, ERROR=%d", result);
Manu Abraham41e840b2009-12-02 21:57:10 -0300204 goto err2;
205 }
Manu Abraham41e840b2009-12-02 21:57:10 -0300206
Manu Abrahamb3b96142009-12-04 05:41:11 -0300207 mantis->fe_mem.source = DMX_MEMORY_FE;
208 result = mantis->demux.dmx.add_frontend(&mantis->demux.dmx,&mantis->fe_mem);
209 if (result < 0) {
210 dprintk(MANTIS_ERROR, 1,"dvb_dmx_init failed, ERROR=%d", result);
Manu Abraham41e840b2009-12-02 21:57:10 -0300211 goto err3;
212 }
Manu Abraham41e840b2009-12-02 21:57:10 -0300213
Manu Abrahamb3b96142009-12-04 05:41:11 -0300214 result = mantis->demux.dmx.connect_frontend(&mantis->demux.dmx, &mantis->fe_hw);
215 if (result < 0) {
216 dprintk(MANTIS_ERROR, 1, "dvb_dmx_init failed, ERROR=%d", result);
Manu Abraham41e840b2009-12-02 21:57:10 -0300217 goto err4;
218 }
Manu Abrahamb3b96142009-12-04 05:41:11 -0300219
Manu Abraham41e840b2009-12-02 21:57:10 -0300220 dvb_net_init(&mantis->dvb_adapter, &mantis->dvbnet, &mantis->demux.dmx);
221 tasklet_init(&mantis->tasklet, mantis_dma_xfer, (unsigned long) mantis);
Manu Abrahamb3b96142009-12-04 05:41:11 -0300222 if (mantis->hwconfig) {
223 result = config->frontend_init(mantis, mantis->fe);
224 if (result < 0) {
225 dprintk(MANTIS_ERROR, 1, "!!! NO Frontends found !!!");
226 goto err5;
227 } else {
228// if (mantis->dvb_adapter == NULL) {
229// dprintk(MANTIS_ERROR, 1, "DVB adapter <NULL>");
230// goto err5;
231// }
232 if (mantis->fe == NULL) {
233 dprintk(MANTIS_ERROR, 1, "FE <NULL>");
234 goto err5;
235 }
236
237 if (dvb_register_frontend(&mantis->dvb_adapter, mantis->fe)) {
238 dprintk(MANTIS_ERROR, 1, "ERROR: Frontend registration failed");
239
240 if (mantis->fe->ops.release)
241 mantis->fe->ops.release(mantis->fe);
242
243 mantis->fe = NULL;
244 goto err5;
245 }
246 }
247 }
Manu Abrahamd9dd5f72009-12-04 05:07:41 -0300248
Manu Abraham41e840b2009-12-02 21:57:10 -0300249 return 0;
250
Manu Abrahamb3b96142009-12-04 05:41:11 -0300251 /* Error conditions .. */
252err5:
253 tasklet_kill(&mantis->tasklet);
254 dvb_net_release(&mantis->dvbnet);
Manu Abraham41e840b2009-12-02 21:57:10 -0300255err4:
256 mantis->demux.dmx.remove_frontend(&mantis->demux.dmx, &mantis->fe_mem);
257err3:
258 mantis->demux.dmx.remove_frontend(&mantis->demux.dmx, &mantis->fe_hw);
259err2:
260 dvb_dmxdev_release(&mantis->dmxdev);
261err1:
262 dvb_dmx_release(&mantis->demux);
263err0:
264 dvb_unregister_adapter(&mantis->dvb_adapter);
265
266 return result;
267}
Manu Abrahamb3b96142009-12-04 05:41:11 -0300268EXPORT_SYMBOL_GPL(mantis_dvb_init);
Manu Abraham41e840b2009-12-02 21:57:10 -0300269
270int __devexit mantis_dvb_exit(struct mantis_pci *mantis)
271{
Manu Abrahamb3b96142009-12-04 05:41:11 -0300272 int err;
273
274 err = mantis_frontend_shutdown(mantis);
275 if (err != 0)
276 dprintk(MANTIS_ERROR, 1, "Frontend exit while POWER ON! <%d>", err);
277
278// mantis_ca_exit(mantis);
Manu Abraham41e840b2009-12-02 21:57:10 -0300279 tasklet_kill(&mantis->tasklet);
280 dvb_net_release(&mantis->dvbnet);
281 mantis->demux.dmx.remove_frontend(&mantis->demux.dmx, &mantis->fe_mem);
282 mantis->demux.dmx.remove_frontend(&mantis->demux.dmx, &mantis->fe_hw);
283 dvb_dmxdev_release(&mantis->dmxdev);
284 dvb_dmx_release(&mantis->demux);
285
286 if (mantis->fe)
287 dvb_unregister_frontend(mantis->fe);
Manu Abrahamb3b96142009-12-04 05:41:11 -0300288
289 dprintk(MANTIS_DEBUG, 1, "dvb_unregister_adapter");
Manu Abraham41e840b2009-12-02 21:57:10 -0300290 dvb_unregister_adapter(&mantis->dvb_adapter);
291
292 return 0;
293}
Manu Abrahamb3b96142009-12-04 05:41:11 -0300294EXPORT_SYMBOL_GPL(mantis_dvb_exit);