blob: 5a71e1791cf5a00ab1d198520c380cf7a44c1890 [file] [log] [blame]
Manu Abraham41e840b2009-12-02 21:57:10 -03001/*
2 Mantis PCI bridge driver
Manu Abraham8825a092009-12-15 09:13:49 -03003 Copyright (C) Manu Abraham (abraham.manu@gmail.com)
Manu Abraham41e840b2009-12-02 21:57:10 -03004
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
Manu Abrahamb3b96142009-12-04 05:41:11 -030023#include <linux/signal.h>
24#include <linux/sched.h>
25#include <linux/interrupt.h>
26#include <linux/pci.h>
27#include <linux/i2c.h>
Manu Abraham41e840b2009-12-02 21:57:10 -030028
29#include "dmxdev.h"
30#include "dvbdev.h"
31#include "dvb_demux.h"
32#include "dvb_frontend.h"
Manu Abrahamb3b96142009-12-04 05:41:11 -030033#include "dvb_net.h"
34
35#include "mantis_common.h"
36#include "mantis_dma.h"
37#include "mantis_ca.h"
38#include "mantis_ioc.h"
39#include "mantis_dvb.h"
Manu Abraham41e840b2009-12-02 21:57:10 -030040
Manu Abraham616f75e2009-12-04 05:33:56 -030041DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
42
Manu Abrahamb3b96142009-12-04 05:41:11 -030043int mantis_frontend_power(struct mantis_pci *mantis, enum mantis_power power)
Manu Abraham41e840b2009-12-02 21:57:10 -030044{
Manu Abrahamb3b96142009-12-04 05:41:11 -030045 struct mantis_hwconfig *config = mantis->hwconfig;
Manu Abraham41e840b2009-12-02 21:57:10 -030046
Manu Abrahamb3b96142009-12-04 05:41:11 -030047 switch (power) {
48 case POWER_ON:
49 dprintk(MANTIS_DEBUG, 1, "Power ON");
Ben Hutchings3037fd12010-11-14 14:56:00 -030050 mantis_gpio_set_bits(mantis, config->power, POWER_ON);
Manu Abrahamb3b96142009-12-04 05:41:11 -030051 msleep(100);
Ben Hutchings3037fd12010-11-14 14:56:00 -030052 mantis_gpio_set_bits(mantis, config->power, POWER_ON);
Manu Abrahamb3b96142009-12-04 05:41:11 -030053 msleep(100);
54 break;
Manu Abraham41e840b2009-12-02 21:57:10 -030055
Manu Abrahamb3b96142009-12-04 05:41:11 -030056 case POWER_OFF:
57 dprintk(MANTIS_DEBUG, 1, "Power OFF");
Ben Hutchings3037fd12010-11-14 14:56:00 -030058 mantis_gpio_set_bits(mantis, config->power, POWER_OFF);
Manu Abrahamb3b96142009-12-04 05:41:11 -030059 msleep(100);
60 break;
Manu Abraham41e840b2009-12-02 21:57:10 -030061
Manu Abrahamb3b96142009-12-04 05:41:11 -030062 default:
63 dprintk(MANTIS_DEBUG, 1, "Unknown state <%02x>", power);
64 return -1;
65 }
Manu Abraham41e840b2009-12-02 21:57:10 -030066
67 return 0;
68}
Manu Abrahamb3b96142009-12-04 05:41:11 -030069EXPORT_SYMBOL_GPL(mantis_frontend_power);
Manu Abraham41e840b2009-12-02 21:57:10 -030070
Manu Abrahamb3b96142009-12-04 05:41:11 -030071void mantis_frontend_soft_reset(struct mantis_pci *mantis)
Manu Abraham41e840b2009-12-02 21:57:10 -030072{
Manu Abrahamb3b96142009-12-04 05:41:11 -030073 struct mantis_hwconfig *config = mantis->hwconfig;
74
75 dprintk(MANTIS_DEBUG, 1, "Frontend RESET");
Ben Hutchings3037fd12010-11-14 14:56:00 -030076 mantis_gpio_set_bits(mantis, config->reset, 0);
Manu Abrahamb3b96142009-12-04 05:41:11 -030077 msleep(100);
Ben Hutchings3037fd12010-11-14 14:56:00 -030078 mantis_gpio_set_bits(mantis, config->reset, 0);
Manu Abrahamb3b96142009-12-04 05:41:11 -030079 msleep(100);
Ben Hutchings3037fd12010-11-14 14:56:00 -030080 mantis_gpio_set_bits(mantis, config->reset, 1);
Manu Abrahamb3b96142009-12-04 05:41:11 -030081 msleep(100);
Ben Hutchings3037fd12010-11-14 14:56:00 -030082 mantis_gpio_set_bits(mantis, config->reset, 1);
Manu Abrahamb3b96142009-12-04 05:41:11 -030083 msleep(100);
84
85 return;
86}
87EXPORT_SYMBOL_GPL(mantis_frontend_soft_reset);
88
89static int mantis_frontend_shutdown(struct mantis_pci *mantis)
90{
91 int err;
92
93 mantis_frontend_soft_reset(mantis);
94 err = mantis_frontend_power(mantis, POWER_OFF);
95 if (err != 0) {
96 dprintk(MANTIS_ERROR, 1, "Frontend POWER OFF failed! <%d>", err);
97 return 1;
98 }
Manu Abraham41e840b2009-12-02 21:57:10 -030099
100 return 0;
101}
102
103static int mantis_dvb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
104{
105 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
106 struct mantis_pci *mantis = dvbdmx->priv;
107
Manu Abrahamb3b96142009-12-04 05:41:11 -0300108 dprintk(MANTIS_DEBUG, 1, "Mantis DVB Start feed");
Manu Abraham41e840b2009-12-02 21:57:10 -0300109 if (!dvbdmx->dmx.frontend) {
Manu Abrahamb3b96142009-12-04 05:41:11 -0300110 dprintk(MANTIS_DEBUG, 1, "no frontend ?");
Manu Abraham41e840b2009-12-02 21:57:10 -0300111 return -EINVAL;
112 }
Manu Abrahamb3b96142009-12-04 05:41:11 -0300113
Manu Abraham41e840b2009-12-02 21:57:10 -0300114 mantis->feeds++;
Manu Abrahamb3b96142009-12-04 05:41:11 -0300115 dprintk(MANTIS_DEBUG, 1, "mantis start feed, feeds=%d", mantis->feeds);
Manu Abraham41e840b2009-12-02 21:57:10 -0300116
117 if (mantis->feeds == 1) {
Manu Abrahamb3b96142009-12-04 05:41:11 -0300118 dprintk(MANTIS_DEBUG, 1, "mantis start feed & dma");
Manu Abraham41e840b2009-12-02 21:57:10 -0300119 mantis_dma_start(mantis);
Marko Ristola9c814962010-11-14 14:09:04 -0300120 tasklet_enable(&mantis->tasklet);
Manu Abraham41e840b2009-12-02 21:57:10 -0300121 }
122
123 return mantis->feeds;
124}
125
126static int mantis_dvb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
127{
128 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
129 struct mantis_pci *mantis = dvbdmx->priv;
130
Manu Abrahamb3b96142009-12-04 05:41:11 -0300131 dprintk(MANTIS_DEBUG, 1, "Mantis DVB Stop feed");
Manu Abraham41e840b2009-12-02 21:57:10 -0300132 if (!dvbdmx->dmx.frontend) {
Manu Abrahamb3b96142009-12-04 05:41:11 -0300133 dprintk(MANTIS_DEBUG, 1, "no frontend ?");
Manu Abraham41e840b2009-12-02 21:57:10 -0300134 return -EINVAL;
135 }
Manu Abrahamb3b96142009-12-04 05:41:11 -0300136
Manu Abraham41e840b2009-12-02 21:57:10 -0300137 mantis->feeds--;
138 if (mantis->feeds == 0) {
Manu Abrahamb3b96142009-12-04 05:41:11 -0300139 dprintk(MANTIS_DEBUG, 1, "mantis stop feed and dma");
Marko Ristola9c814962010-11-14 14:09:04 -0300140 tasklet_disable(&mantis->tasklet);
Manu Abraham41e840b2009-12-02 21:57:10 -0300141 mantis_dma_stop(mantis);
142 }
Manu Abrahamb3b96142009-12-04 05:41:11 -0300143
Manu Abraham41e840b2009-12-02 21:57:10 -0300144 return 0;
145}
146
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800147int mantis_dvb_init(struct mantis_pci *mantis)
Manu Abraham41e840b2009-12-02 21:57:10 -0300148{
Manu Abrahamb3b96142009-12-04 05:41:11 -0300149 struct mantis_hwconfig *config = mantis->hwconfig;
150 int result = -1;
Manu Abraham41e840b2009-12-02 21:57:10 -0300151
Manu Abrahamb3b96142009-12-04 05:41:11 -0300152 dprintk(MANTIS_DEBUG, 1, "dvb_register_adapter");
Manu Abraham41e840b2009-12-02 21:57:10 -0300153
Manu Abrahamb3b96142009-12-04 05:41:11 -0300154 result = dvb_register_adapter(&mantis->dvb_adapter,
155 "Mantis DVB adapter",
156 THIS_MODULE,
157 &mantis->pdev->dev,
158 adapter_nr);
159
160 if (result < 0) {
161
162 dprintk(MANTIS_ERROR, 1, "Error registering adapter");
Manu Abraham41e840b2009-12-02 21:57:10 -0300163 return -ENODEV;
164 }
Manu Abrahamb3b96142009-12-04 05:41:11 -0300165
166 mantis->dvb_adapter.priv = mantis;
167 mantis->demux.dmx.capabilities = DMX_TS_FILTERING |
Manu Abraham41e840b2009-12-02 21:57:10 -0300168 DMX_SECTION_FILTERING |
169 DMX_MEMORY_BASED_FILTERING;
170
Manu Abrahamb3b96142009-12-04 05:41:11 -0300171 mantis->demux.priv = mantis;
172 mantis->demux.filternum = 256;
173 mantis->demux.feednum = 256;
174 mantis->demux.start_feed = mantis_dvb_start_feed;
175 mantis->demux.stop_feed = mantis_dvb_stop_feed;
176 mantis->demux.write_to_decoder = NULL;
177
178 dprintk(MANTIS_DEBUG, 1, "dvb_dmx_init");
179 result = dvb_dmx_init(&mantis->demux);
180 if (result < 0) {
181 dprintk(MANTIS_ERROR, 1, "dvb_dmx_init failed, ERROR=%d", result);
Manu Abraham41e840b2009-12-02 21:57:10 -0300182
183 goto err0;
184 }
Manu Abraham41e840b2009-12-02 21:57:10 -0300185
Manu Abrahamb3b96142009-12-04 05:41:11 -0300186 mantis->dmxdev.filternum = 256;
187 mantis->dmxdev.demux = &mantis->demux.dmx;
188 mantis->dmxdev.capabilities = 0;
189 dprintk(MANTIS_DEBUG, 1, "dvb_dmxdev_init");
190
191 result = dvb_dmxdev_init(&mantis->dmxdev, &mantis->dvb_adapter);
192 if (result < 0) {
193
194 dprintk(MANTIS_ERROR, 1, "dvb_dmxdev_init failed, ERROR=%d", result);
Manu Abraham41e840b2009-12-02 21:57:10 -0300195 goto err1;
196 }
Manu Abraham41e840b2009-12-02 21:57:10 -0300197
Manu Abrahamb3b96142009-12-04 05:41:11 -0300198 mantis->fe_hw.source = DMX_FRONTEND_0;
199 result = mantis->demux.dmx.add_frontend(&mantis->demux.dmx, &mantis->fe_hw);
200 if (result < 0) {
Manu Abraham41e840b2009-12-02 21:57:10 -0300201
Manu Abrahamb3b96142009-12-04 05:41:11 -0300202 dprintk(MANTIS_ERROR, 1, "dvb_dmx_init failed, ERROR=%d", result);
Manu Abraham41e840b2009-12-02 21:57:10 -0300203 goto err2;
204 }
Manu Abraham41e840b2009-12-02 21:57:10 -0300205
Manu Abrahamb3b96142009-12-04 05:41:11 -0300206 mantis->fe_mem.source = DMX_MEMORY_FE;
Manu Abrahamf5ae4f62009-12-15 08:47:21 -0300207 result = mantis->demux.dmx.add_frontend(&mantis->demux.dmx, &mantis->fe_mem);
Manu Abrahamb3b96142009-12-04 05:41:11 -0300208 if (result < 0) {
Manu Abrahamf5ae4f62009-12-15 08:47:21 -0300209 dprintk(MANTIS_ERROR, 1, "dvb_dmx_init failed, ERROR=%d", result);
Manu Abraham41e840b2009-12-02 21:57:10 -0300210 goto err3;
211 }
Manu Abraham41e840b2009-12-02 21:57:10 -0300212
Manu Abrahamb3b96142009-12-04 05:41:11 -0300213 result = mantis->demux.dmx.connect_frontend(&mantis->demux.dmx, &mantis->fe_hw);
214 if (result < 0) {
215 dprintk(MANTIS_ERROR, 1, "dvb_dmx_init failed, ERROR=%d", result);
Manu Abraham41e840b2009-12-02 21:57:10 -0300216 goto err4;
217 }
Manu Abrahamb3b96142009-12-04 05:41:11 -0300218
Manu Abraham41e840b2009-12-02 21:57:10 -0300219 dvb_net_init(&mantis->dvb_adapter, &mantis->dvbnet, &mantis->demux.dmx);
220 tasklet_init(&mantis->tasklet, mantis_dma_xfer, (unsigned long) mantis);
Marko Ristola9c814962010-11-14 14:09:04 -0300221 tasklet_disable(&mantis->tasklet);
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 {
Manu Abrahamb3b96142009-12-04 05:41:11 -0300228 if (mantis->fe == NULL) {
229 dprintk(MANTIS_ERROR, 1, "FE <NULL>");
230 goto err5;
231 }
232
233 if (dvb_register_frontend(&mantis->dvb_adapter, mantis->fe)) {
234 dprintk(MANTIS_ERROR, 1, "ERROR: Frontend registration failed");
235
236 if (mantis->fe->ops.release)
237 mantis->fe->ops.release(mantis->fe);
238
239 mantis->fe = NULL;
240 goto err5;
241 }
242 }
243 }
Manu Abrahamd9dd5f72009-12-04 05:07:41 -0300244
Manu Abraham41e840b2009-12-02 21:57:10 -0300245 return 0;
246
Manu Abrahamb3b96142009-12-04 05:41:11 -0300247 /* Error conditions .. */
248err5:
249 tasklet_kill(&mantis->tasklet);
250 dvb_net_release(&mantis->dvbnet);
Alan Coxe1d45ae2012-08-09 12:33:52 -0300251 if (mantis->fe) {
252 dvb_unregister_frontend(mantis->fe);
253 dvb_frontend_detach(mantis->fe);
254 }
Manu Abraham41e840b2009-12-02 21:57:10 -0300255err4:
256 mantis->demux.dmx.remove_frontend(&mantis->demux.dmx, &mantis->fe_mem);
Manu Abraham3e978a82009-12-04 05:56:35 -0300257
Manu Abraham41e840b2009-12-02 21:57:10 -0300258err3:
259 mantis->demux.dmx.remove_frontend(&mantis->demux.dmx, &mantis->fe_hw);
Manu Abraham3e978a82009-12-04 05:56:35 -0300260
Manu Abraham41e840b2009-12-02 21:57:10 -0300261err2:
262 dvb_dmxdev_release(&mantis->dmxdev);
Manu Abraham3e978a82009-12-04 05:56:35 -0300263
Manu Abraham41e840b2009-12-02 21:57:10 -0300264err1:
265 dvb_dmx_release(&mantis->demux);
Manu Abraham3e978a82009-12-04 05:56:35 -0300266
Manu Abraham41e840b2009-12-02 21:57:10 -0300267err0:
268 dvb_unregister_adapter(&mantis->dvb_adapter);
269
270 return result;
271}
Manu Abrahamb3b96142009-12-04 05:41:11 -0300272EXPORT_SYMBOL_GPL(mantis_dvb_init);
Manu Abraham41e840b2009-12-02 21:57:10 -0300273
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800274int mantis_dvb_exit(struct mantis_pci *mantis)
Manu Abraham41e840b2009-12-02 21:57:10 -0300275{
Manu Abrahamb3b96142009-12-04 05:41:11 -0300276 int err;
277
Manu Abraham3e978a82009-12-04 05:56:35 -0300278 if (mantis->fe) {
Manu Abrahamf5ae4f62009-12-15 08:47:21 -0300279 /* mantis_ca_exit(mantis); */
Manu Abraham3e978a82009-12-04 05:56:35 -0300280 err = mantis_frontend_shutdown(mantis);
281 if (err != 0)
282 dprintk(MANTIS_ERROR, 1, "Frontend exit while POWER ON! <%d>", err);
Manu Abraham3e978a82009-12-04 05:56:35 -0300283 dvb_unregister_frontend(mantis->fe);
Manu Abrahamc5e598a2009-12-09 19:59:26 -0300284 dvb_frontend_detach(mantis->fe);
Manu Abraham3e978a82009-12-04 05:56:35 -0300285 }
286
Manu Abraham41e840b2009-12-02 21:57:10 -0300287 tasklet_kill(&mantis->tasklet);
288 dvb_net_release(&mantis->dvbnet);
Manu Abraham3e978a82009-12-04 05:56:35 -0300289
Manu Abraham41e840b2009-12-02 21:57:10 -0300290 mantis->demux.dmx.remove_frontend(&mantis->demux.dmx, &mantis->fe_mem);
291 mantis->demux.dmx.remove_frontend(&mantis->demux.dmx, &mantis->fe_hw);
Manu Abraham3e978a82009-12-04 05:56:35 -0300292
Manu Abraham41e840b2009-12-02 21:57:10 -0300293 dvb_dmxdev_release(&mantis->dmxdev);
294 dvb_dmx_release(&mantis->demux);
295
Manu Abrahamb3b96142009-12-04 05:41:11 -0300296 dprintk(MANTIS_DEBUG, 1, "dvb_unregister_adapter");
Manu Abraham41e840b2009-12-02 21:57:10 -0300297 dvb_unregister_adapter(&mantis->dvb_adapter);
298
299 return 0;
300}
Manu Abrahamb3b96142009-12-04 05:41:11 -0300301EXPORT_SYMBOL_GPL(mantis_dvb_exit);