flexcop.c 8.8 KB
Newer Older
1
/*
2 3 4 5
 * Linux driver for digital TV devices equipped with B2C2 FlexcopII(b)/III
 * flexcop.c - main module part
 * Copyright (C) 2004-9 Patrick Boettcher <patrick.boettcher@desy.de>
 * based on skystar2-driver Copyright (C) 2003 Vadim Catana, skystar@moldova.cc
6 7
 *
 * Acknowledgements:
8 9 10
 *   John Jurrius from BBTI, Inc. for extensive support
 *                    with code examples and data books
 *   Bjarne Steinsbo, bjarne at steinsbo.com (some ideas for rewriting)
11 12
 *
 * Contributions to the skystar2-driver have been done by
13 14 15 16 17
 *   Vincenzo Di Massa, hawk.it at tiscalinet.it (several DiSEqC fixes)
 *   Roberto Ragusa, r.ragusa at libero.it (polishing, restyling the code)
 *   Uwe Bugla, uwe.bugla at gmx.de (doing tests, restyling code, writing docu)
 *   Niklas Peinecke, peinecke at gdv.uni-hannover.de (hardware pid/mac
 *               filtering)
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1
 * 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 Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#include "flexcop.h"

#define DRIVER_NAME "B2C2 FlexcopII/II(b)/III digital TV receiver chip"
#define DRIVER_AUTHOR "Patrick Boettcher <patrick.boettcher@desy.de"

#ifdef CONFIG_DVB_B2C2_FLEXCOP_DEBUG
#define DEBSTATUS ""
#else
#define DEBSTATUS " (debugging is not enabled)"
#endif

int b2c2_flexcop_debug;
module_param_named(debug, b2c2_flexcop_debug,  int, 0644);
47 48 49 50
MODULE_PARM_DESC(debug,
		"set debug level (1=info,2=tuner,4=i2c,8=ts,"
		"16=sram,32=reg (|-able))."
		DEBSTATUS);
51 52
#undef DEBSTATUS

53 54
DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);

55 56 57 58 59 60
/* global zero for ibi values */
flexcop_ibi_value ibi_zero;

static int flexcop_dvb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
{
	struct flexcop_device *fc = dvbdmxfeed->demux->priv;
61
	return flexcop_pid_feed_control(fc, dvbdmxfeed, 1);
62 63 64 65 66
}

static int flexcop_dvb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
{
	struct flexcop_device *fc = dvbdmxfeed->demux->priv;
67
	return flexcop_pid_feed_control(fc, dvbdmxfeed, 0);
68 69 70 71
}

static int flexcop_dvb_init(struct flexcop_device *fc)
{
72
	int ret = dvb_register_adapter(&fc->dvb_adapter,
73 74
			"FlexCop Digital TV device", fc->owner,
			fc->dev, adapter_nr);
75
	if (ret < 0) {
76 77 78 79 80
		err("error registering DVB adapter");
		return ret;
	}
	fc->dvb_adapter.priv = fc;

81 82
	fc->demux.dmx.capabilities = (DMX_TS_FILTERING | DMX_SECTION_FILTERING
			| DMX_MEMORY_BASED_FILTERING);
83 84 85 86 87 88
	fc->demux.priv = fc;
	fc->demux.filternum = fc->demux.feednum = FC_MAX_FEED;
	fc->demux.start_feed = flexcop_dvb_start_feed;
	fc->demux.stop_feed = flexcop_dvb_stop_feed;
	fc->demux.write_to_decoder = NULL;

89 90
	ret = dvb_dmx_init(&fc->demux);
	if (ret < 0) {
91
		err("dvb_dmx failed: error %d", ret);
92 93 94 95 96 97 98 99
		goto err_dmx;
	}

	fc->hw_frontend.source = DMX_FRONTEND_0;

	fc->dmxdev.filternum = fc->demux.feednum;
	fc->dmxdev.demux = &fc->demux.dmx;
	fc->dmxdev.capabilities = 0;
100 101
	ret = dvb_dmxdev_init(&fc->dmxdev, &fc->dvb_adapter);
	if (ret < 0) {
102
		err("dvb_dmxdev_init failed: error %d", ret);
103 104 105
		goto err_dmx_dev;
	}

106 107
	ret = fc->demux.dmx.add_frontend(&fc->demux.dmx, &fc->hw_frontend);
	if (ret < 0) {
108
		err("adding hw_frontend to dmx failed: error %d", ret);
109 110 111 112
		goto err_dmx_add_hw_frontend;
	}

	fc->mem_frontend.source = DMX_MEMORY_FE;
113 114
	ret = fc->demux.dmx.add_frontend(&fc->demux.dmx, &fc->mem_frontend);
	if (ret < 0) {
115
		err("adding mem_frontend to dmx failed: error %d", ret);
116 117 118
		goto err_dmx_add_mem_frontend;
	}

119 120
	ret = fc->demux.dmx.connect_frontend(&fc->demux.dmx, &fc->hw_frontend);
	if (ret < 0) {
121
		err("connect frontend failed: error %d", ret);
122 123 124
		goto err_connect_frontend;
	}

125 126 127 128 129
	ret = dvb_net_init(&fc->dvb_adapter, &fc->dvbnet, &fc->demux.dmx);
	if (ret < 0) {
		err("dvb_net_init failed: error %d", ret);
		goto err_net;
	}
130 131

	fc->init_state |= FC_STATE_DVB_INIT;
132
	return 0;
133

134 135
err_net:
	fc->demux.dmx.disconnect_frontend(&fc->demux.dmx);
136
err_connect_frontend:
137
	fc->demux.dmx.remove_frontend(&fc->demux.dmx, &fc->mem_frontend);
138
err_dmx_add_mem_frontend:
139
	fc->demux.dmx.remove_frontend(&fc->demux.dmx, &fc->hw_frontend);
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
err_dmx_add_hw_frontend:
	dvb_dmxdev_release(&fc->dmxdev);
err_dmx_dev:
	dvb_dmx_release(&fc->demux);
err_dmx:
	dvb_unregister_adapter(&fc->dvb_adapter);
	return ret;
}

static void flexcop_dvb_exit(struct flexcop_device *fc)
{
	if (fc->init_state & FC_STATE_DVB_INIT) {
		dvb_net_release(&fc->dvbnet);

		fc->demux.dmx.close(&fc->demux.dmx);
155 156 157 158
		fc->demux.dmx.remove_frontend(&fc->demux.dmx,
			&fc->mem_frontend);
		fc->demux.dmx.remove_frontend(&fc->demux.dmx,
			&fc->hw_frontend);
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
		dvb_dmxdev_release(&fc->dmxdev);
		dvb_dmx_release(&fc->demux);
		dvb_unregister_adapter(&fc->dvb_adapter);
		deb_info("deinitialized dvb stuff\n");
	}
	fc->init_state &= ~FC_STATE_DVB_INIT;
}

/* these methods are necessary to achieve the long-term-goal of hiding the
 * struct flexcop_device from the bus-parts */
void flexcop_pass_dmx_data(struct flexcop_device *fc, u8 *buf, u32 len)
{
	dvb_dmx_swfilter(&fc->demux, buf, len);
}
EXPORT_SYMBOL(flexcop_pass_dmx_data);

void flexcop_pass_dmx_packets(struct flexcop_device *fc, u8 *buf, u32 no)
{
	dvb_dmx_swfilter_packets(&fc->demux, buf, no);
}
EXPORT_SYMBOL(flexcop_pass_dmx_packets);

static void flexcop_reset(struct flexcop_device *fc)
{
183
	flexcop_ibi_value v210, v204;
184

185
	/* reset the flexcop itself */
186 187 188
	fc->write_ibi_reg(fc,ctrl_208,ibi_zero);

	v210.raw = 0;
189 190 191 192 193 194 195 196
	v210.sw_reset_210.reset_block_000 = 1;
	v210.sw_reset_210.reset_block_100 = 1;
	v210.sw_reset_210.reset_block_200 = 1;
	v210.sw_reset_210.reset_block_300 = 1;
	v210.sw_reset_210.reset_block_400 = 1;
	v210.sw_reset_210.reset_block_500 = 1;
	v210.sw_reset_210.reset_block_600 = 1;
	v210.sw_reset_210.reset_block_700 = 1;
197
	v210.sw_reset_210.Block_reset_enable = 0xb2;
198
	v210.sw_reset_210.Special_controls = 0xc259;
199
	fc->write_ibi_reg(fc,sw_reset_210,v210);
200
	msleep(1);
201

202
	/* reset the periphical devices */
203 204 205 206

	v204 = fc->read_ibi_reg(fc,misc_204);
	v204.misc_204.Per_reset_sig = 0;
	fc->write_ibi_reg(fc,misc_204,v204);
207
	msleep(1);
208 209 210 211
	v204.misc_204.Per_reset_sig = 1;
	fc->write_ibi_reg(fc,misc_204,v204);
}

212 213
void flexcop_reset_block_300(struct flexcop_device *fc)
{
214 215
	flexcop_ibi_value v208_save = fc->read_ibi_reg(fc, ctrl_208),
			  v210 = fc->read_ibi_reg(fc, sw_reset_210);
216

217
	deb_rdump("208: %08x, 210: %08x\n", v208_save.raw, v210.raw);
218 219 220 221 222 223 224 225 226
	fc->write_ibi_reg(fc,ctrl_208,ibi_zero);

	v210.sw_reset_210.reset_block_300 = 1;
	v210.sw_reset_210.Block_reset_enable = 0xb2;

	fc->write_ibi_reg(fc,sw_reset_210,v210);
	fc->write_ibi_reg(fc,ctrl_208,v208_save);
}

227 228 229
struct flexcop_device *flexcop_device_kmalloc(size_t bus_specific_len)
{
	void *bus;
230 231
	struct flexcop_device *fc = kzalloc(sizeof(struct flexcop_device),
				GFP_KERNEL);
232 233 234 235 236
	if (!fc) {
		err("no memory");
		return NULL;
	}

P
 
Panagiotis Issaris 已提交
237
	bus = kzalloc(bus_specific_len, GFP_KERNEL);
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
	if (!bus) {
		err("no memory");
		kfree(fc);
		return NULL;
	}

	fc->bus_specific = bus;

	return fc;
}
EXPORT_SYMBOL(flexcop_device_kmalloc);

void flexcop_device_kfree(struct flexcop_device *fc)
{
	kfree(fc->bus_specific);
	kfree(fc);
}
EXPORT_SYMBOL(flexcop_device_kfree);

int flexcop_device_initialize(struct flexcop_device *fc)
{
	int ret;
	ibi_zero.raw = 0;

	flexcop_reset(fc);
	flexcop_determine_revision(fc);
	flexcop_sram_init(fc);
	flexcop_hw_filter_init(fc);
	flexcop_smc_ctrl(fc, 0);

268 269
	ret = flexcop_dvb_init(fc);
	if (ret)
270 271
		goto error;

272 273 274 275 276 277
	/* i2c has to be done before doing EEProm stuff -
	 * because the EEProm is accessed via i2c */
	ret = flexcop_i2c_init(fc);
	if (ret)
		goto error;

278
	/* do the MAC address reading after initializing the dvb_adapter */
279
	if (fc->get_mac_addr(fc, 0) == 0) {
280
		u8 *b = fc->dvb_adapter.proposed_mac;
J
Johannes Berg 已提交
281
		info("MAC address = %pM", b);
282
		flexcop_set_mac_filter(fc,b);
283 284 285 286
		flexcop_mac_filter_ctrl(fc,1);
	} else
		warn("reading of MAC address failed.\n");

287 288
	ret = flexcop_frontend_init(fc);
	if (ret)
289 290 291
		goto error;

	flexcop_device_name(fc,"initialization of","complete");
292 293
	return 0;

294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
error:
	flexcop_device_exit(fc);
	return ret;
}
EXPORT_SYMBOL(flexcop_device_initialize);

void flexcop_device_exit(struct flexcop_device *fc)
{
	flexcop_frontend_exit(fc);
	flexcop_i2c_exit(fc);
	flexcop_dvb_exit(fc);
}
EXPORT_SYMBOL(flexcop_device_exit);

static int flexcop_module_init(void)
{
	info(DRIVER_NAME " loaded successfully");
	return 0;
}

static void flexcop_module_cleanup(void)
{
	info(DRIVER_NAME " unloaded successfully");
}

module_init(flexcop_module_init);
module_exit(flexcop_module_cleanup);

MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_NAME);
MODULE_LICENSE("GPL");