mantis_dvb.c 7.5 KB
Newer Older
1 2
/*
	Mantis PCI bridge driver
3
	Copyright (C) Manu Abraham (abraham.manu@gmail.com)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

20
#include <linux/kernel.h>
21
#include <linux/bitops.h>
22 23 24 25 26 27

#include <linux/signal.h>
#include <linux/sched.h>
#include <linux/interrupt.h>
#include <linux/pci.h>
#include <linux/i2c.h>
28 29 30 31 32

#include "dmxdev.h"
#include "dvbdev.h"
#include "dvb_demux.h"
#include "dvb_frontend.h"
33
#include "dvb_net.h"
34

35 36 37 38 39
#include "mantis_common.h"
#include "mantis_dma.h"
#include "mantis_ca.h"
#include "mantis_ioc.h"
#include "mantis_dvb.h"
40

41
DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
42

43
int mantis_frontend_power(struct mantis_pci *mantis, enum mantis_power power)
44
{
45 46 47 48 49
	struct mantis_hwconfig *config = mantis->hwconfig;

	switch (power) {
	case POWER_ON:
		dprintk(MANTIS_DEBUG, 1, "Power ON");
50
		mantis_gpio_set_bits(mantis, config->power, POWER_ON);
51
		msleep(100);
52
		mantis_gpio_set_bits(mantis, config->power, POWER_ON);
53 54
		msleep(100);
		break;
55

56 57
	case POWER_OFF:
		dprintk(MANTIS_DEBUG, 1, "Power OFF");
58
		mantis_gpio_set_bits(mantis, config->power, POWER_OFF);
59 60
		msleep(100);
		break;
61

62 63 64 65
	default:
		dprintk(MANTIS_DEBUG, 1, "Unknown state <%02x>", power);
		return -1;
	}
66 67 68

	return 0;
}
69
EXPORT_SYMBOL_GPL(mantis_frontend_power);
70

71
void mantis_frontend_soft_reset(struct mantis_pci *mantis)
72
{
73 74 75
	struct mantis_hwconfig *config = mantis->hwconfig;

	dprintk(MANTIS_DEBUG, 1, "Frontend RESET");
76
	mantis_gpio_set_bits(mantis, config->reset, 0);
77
	msleep(100);
78
	mantis_gpio_set_bits(mantis, config->reset, 0);
79
	msleep(100);
80
	mantis_gpio_set_bits(mantis, config->reset, 1);
81
	msleep(100);
82
	mantis_gpio_set_bits(mantis, config->reset, 1);
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
	msleep(100);

	return;
}
EXPORT_SYMBOL_GPL(mantis_frontend_soft_reset);

static int mantis_frontend_shutdown(struct mantis_pci *mantis)
{
	int err;

	mantis_frontend_soft_reset(mantis);
	err = mantis_frontend_power(mantis, POWER_OFF);
	if (err != 0) {
		dprintk(MANTIS_ERROR, 1, "Frontend POWER OFF failed! <%d>", err);
		return 1;
	}
99 100 101 102 103 104 105 106 107

	return 0;
}

static int mantis_dvb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
{
	struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
	struct mantis_pci *mantis = dvbdmx->priv;

108
	dprintk(MANTIS_DEBUG, 1, "Mantis DVB Start feed");
109
	if (!dvbdmx->dmx.frontend) {
110
		dprintk(MANTIS_DEBUG, 1, "no frontend ?");
111 112
		return -EINVAL;
	}
113

114
	mantis->feeds++;
115
	dprintk(MANTIS_DEBUG, 1, "mantis start feed, feeds=%d",	mantis->feeds);
116 117

	if (mantis->feeds == 1)	 {
118
		dprintk(MANTIS_DEBUG, 1, "mantis start feed & dma");
119
		mantis_dma_start(mantis);
120
		tasklet_enable(&mantis->tasklet);
121 122 123 124 125 126 127 128 129 130
	}

	return mantis->feeds;
}

static int mantis_dvb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
{
	struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
	struct mantis_pci *mantis = dvbdmx->priv;

131
	dprintk(MANTIS_DEBUG, 1, "Mantis DVB Stop feed");
132
	if (!dvbdmx->dmx.frontend) {
133
		dprintk(MANTIS_DEBUG, 1, "no frontend ?");
134 135
		return -EINVAL;
	}
136

137 138
	mantis->feeds--;
	if (mantis->feeds == 0) {
139
		dprintk(MANTIS_DEBUG, 1, "mantis stop feed and dma");
140
		tasklet_disable(&mantis->tasklet);
141 142
		mantis_dma_stop(mantis);
	}
143

144 145 146 147 148
	return 0;
}

int __devinit mantis_dvb_init(struct mantis_pci *mantis)
{
149 150 151 152 153 154 155 156 157 158
	struct mantis_hwconfig *config = mantis->hwconfig;
	int result = -1;

	dprintk(MANTIS_DEBUG, 1, "dvb_register_adapter");

	result = dvb_register_adapter(&mantis->dvb_adapter,
				      "Mantis DVB adapter",
				      THIS_MODULE,
				      &mantis->pdev->dev,
				      adapter_nr);
159

160
	if (result < 0) {
161

162
		dprintk(MANTIS_ERROR, 1, "Error registering adapter");
163 164
		return -ENODEV;
	}
165 166 167

	mantis->dvb_adapter.priv	= mantis;
	mantis->demux.dmx.capabilities	= DMX_TS_FILTERING	|
168 169 170
					 DMX_SECTION_FILTERING	|
					 DMX_MEMORY_BASED_FILTERING;

171 172 173 174 175 176 177 178 179 180 181
	mantis->demux.priv		= mantis;
	mantis->demux.filternum		= 256;
	mantis->demux.feednum		= 256;
	mantis->demux.start_feed	= mantis_dvb_start_feed;
	mantis->demux.stop_feed		= mantis_dvb_stop_feed;
	mantis->demux.write_to_decoder	= NULL;

	dprintk(MANTIS_DEBUG, 1, "dvb_dmx_init");
	result = dvb_dmx_init(&mantis->demux);
	if (result < 0) {
		dprintk(MANTIS_ERROR, 1, "dvb_dmx_init failed, ERROR=%d", result);
182 183 184

		goto err0;
	}
185 186 187 188 189 190 191 192 193 194

	mantis->dmxdev.filternum	= 256;
	mantis->dmxdev.demux		= &mantis->demux.dmx;
	mantis->dmxdev.capabilities	= 0;
	dprintk(MANTIS_DEBUG, 1, "dvb_dmxdev_init");

	result = dvb_dmxdev_init(&mantis->dmxdev, &mantis->dvb_adapter);
	if (result < 0) {

		dprintk(MANTIS_ERROR, 1, "dvb_dmxdev_init failed, ERROR=%d", result);
195 196 197
		goto err1;
	}

198 199 200
	mantis->fe_hw.source		= DMX_FRONTEND_0;
	result = mantis->demux.dmx.add_frontend(&mantis->demux.dmx, &mantis->fe_hw);
	if (result < 0) {
201

202
		dprintk(MANTIS_ERROR, 1, "dvb_dmx_init failed, ERROR=%d", result);
203 204 205
		goto err2;
	}

206
	mantis->fe_mem.source		= DMX_MEMORY_FE;
207
	result = mantis->demux.dmx.add_frontend(&mantis->demux.dmx, &mantis->fe_mem);
208
	if (result < 0) {
209
		dprintk(MANTIS_ERROR, 1, "dvb_dmx_init failed, ERROR=%d", result);
210 211 212
		goto err3;
	}

213 214 215
	result = mantis->demux.dmx.connect_frontend(&mantis->demux.dmx, &mantis->fe_hw);
	if (result < 0) {
		dprintk(MANTIS_ERROR, 1, "dvb_dmx_init failed, ERROR=%d", result);
216 217
		goto err4;
	}
218

219 220
	dvb_net_init(&mantis->dvb_adapter, &mantis->dvbnet, &mantis->demux.dmx);
	tasklet_init(&mantis->tasklet, mantis_dma_xfer, (unsigned long) mantis);
221
	tasklet_disable(&mantis->tasklet);
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
	if (mantis->hwconfig) {
		result = config->frontend_init(mantis, mantis->fe);
		if (result < 0) {
			dprintk(MANTIS_ERROR, 1, "!!! NO Frontends found !!!");
			goto err5;
		} else {
			if (mantis->fe == NULL) {
				dprintk(MANTIS_ERROR, 1, "FE <NULL>");
				goto err5;
			}

			if (dvb_register_frontend(&mantis->dvb_adapter, mantis->fe)) {
				dprintk(MANTIS_ERROR, 1, "ERROR: Frontend registration failed");

				if (mantis->fe->ops.release)
					mantis->fe->ops.release(mantis->fe);

				mantis->fe = NULL;
				goto err5;
			}
		}
	}
244

245 246
	return 0;

247 248 249 250
	/* Error conditions ..	*/
err5:
	tasklet_kill(&mantis->tasklet);
	dvb_net_release(&mantis->dvbnet);
A
Alan Cox 已提交
251 252 253 254
	if (mantis->fe) {
		dvb_unregister_frontend(mantis->fe);
		dvb_frontend_detach(mantis->fe);
	}
255 256
err4:
	mantis->demux.dmx.remove_frontend(&mantis->demux.dmx, &mantis->fe_mem);
257

258 259
err3:
	mantis->demux.dmx.remove_frontend(&mantis->demux.dmx, &mantis->fe_hw);
260

261 262
err2:
	dvb_dmxdev_release(&mantis->dmxdev);
263

264 265
err1:
	dvb_dmx_release(&mantis->demux);
266

267 268 269 270 271
err0:
	dvb_unregister_adapter(&mantis->dvb_adapter);

	return result;
}
272
EXPORT_SYMBOL_GPL(mantis_dvb_init);
273

274
int __devexit mantis_dvb_exit(struct mantis_pci *mantis)
275
{
276
	int err;
277

278
	if (mantis->fe) {
279
		/* mantis_ca_exit(mantis); */
280 281 282 283
		err = mantis_frontend_shutdown(mantis);
		if (err != 0)
			dprintk(MANTIS_ERROR, 1, "Frontend exit while POWER ON! <%d>", err);
		dvb_unregister_frontend(mantis->fe);
284
		dvb_frontend_detach(mantis->fe);
285
	}
286 287 288

	tasklet_kill(&mantis->tasklet);
	dvb_net_release(&mantis->dvbnet);
289

290 291
	mantis->demux.dmx.remove_frontend(&mantis->demux.dmx, &mantis->fe_mem);
	mantis->demux.dmx.remove_frontend(&mantis->demux.dmx, &mantis->fe_hw);
292

293 294 295
	dvb_dmxdev_release(&mantis->dmxdev);
	dvb_dmx_release(&mantis->demux);

296
	dprintk(MANTIS_DEBUG, 1, "dvb_unregister_adapter");
297 298 299 300
	dvb_unregister_adapter(&mantis->dvb_adapter);

	return 0;
}
301
EXPORT_SYMBOL_GPL(mantis_dvb_exit);