dst.c 49.0 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2 3 4
	Frontend/Card driver for TwinHan DST Frontend
	Copyright (C) 2003 Jamie Honan
	Copyright (C) 2004, 2005 Manu Abraham (manu@kromtek.com)
L
Linus Torvalds 已提交
5

6 7 8 9
	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.
L
Linus Torvalds 已提交
10

11 12 13 14
	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.
L
Linus Torvalds 已提交
15

16 17 18
	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.
L
Linus Torvalds 已提交
19 20 21 22 23 24 25 26 27 28 29 30
*/

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/delay.h>
#include <asm/div64.h>
#include "dvb_frontend.h"
#include "dst_priv.h"
31 32 33 34 35 36 37 38
#include "dst_common.h"

static unsigned int verbose = 1;
module_param(verbose, int, 0644);
MODULE_PARM_DESC(verbose, "verbose startup messages, default is 1 (yes)");

static unsigned int dst_addons;
module_param(dst_addons, int, 0644);
39
MODULE_PARM_DESC(dst_addons, "CA daughterboard, default is 0 (No addons)");
L
Linus Torvalds 已提交
40

41 42 43 44
static unsigned int dst_algo;
module_param(dst_algo, int, 0644);
MODULE_PARM_DESC(dst_algo, "tuning algo: default is 0=(SW), 1=(HW)");

45 46 47 48 49 50 51 52 53
#define HAS_LOCK		1
#define ATTEMPT_TUNE		2
#define HAS_POWER		4

#define DST_ERROR		0
#define DST_NOTICE		1
#define DST_INFO		2
#define DST_DEBUG		3

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
#define dprintk(x, y, z, format, arg...) do {				\
	if (z) {							\
		if	((x > DST_ERROR) && (x > y))			\
			printk(KERN_ERR "dst(%d) %s: " format "\n",	\
				state->bt->nr, __func__ , ##arg);	\
		else if	((x > DST_NOTICE) && (x > y))			\
			printk(KERN_NOTICE "dst(%d) %s: " format "\n",  \
				state->bt->nr, __func__ , ##arg);	\
		else if ((x > DST_INFO) && (x > y))			\
			printk(KERN_INFO "dst(%d) %s: " format "\n",	\
				state->bt->nr, __func__ , ##arg);	\
		else if ((x > DST_DEBUG) && (x > y))			\
			printk(KERN_DEBUG "dst(%d) %s: " format "\n",	\
				state->bt->nr,  __func__ , ##arg);	\
	} else {							\
		if (x > y)						\
			printk(format, ##arg);				\
	}								\
72 73 74 75
} while(0)


static void dst_packsize(struct dst_state *state, int psize)
L
Linus Torvalds 已提交
76 77 78 79 80 81 82
{
	union dst_gpio_packet bits;

	bits.psize = psize;
	bt878_device_control(state->bt, DST_IG_TS, &bits);
}

83
int dst_gpio_outb(struct dst_state *state, u32 mask, u32 enbb, u32 outhigh, int delay)
L
Linus Torvalds 已提交
84 85 86 87 88 89 90
{
	union dst_gpio_packet enb;
	union dst_gpio_packet bits;
	int err;

	enb.enb.mask = mask;
	enb.enb.enable = enbb;
91

92
	dprintk(verbose, DST_INFO, 1, "mask=[%04x], enbb=[%04x], outhigh=[%04x]", mask, enbb, outhigh);
L
Linus Torvalds 已提交
93
	if ((err = bt878_device_control(state->bt, DST_IG_ENABLE, &enb)) < 0) {
94
		dprintk(verbose, DST_INFO, 1, "dst_gpio_enb error (err == %i, mask == %02x, enb == %02x)", err, mask, enbb);
L
Linus Torvalds 已提交
95 96
		return -EREMOTEIO;
	}
97
	udelay(1000);
L
Linus Torvalds 已提交
98 99 100
	/* because complete disabling means no output, no need to do output packet */
	if (enbb == 0)
		return 0;
101 102
	if (delay)
		msleep(10);
L
Linus Torvalds 已提交
103 104 105
	bits.outp.mask = enbb;
	bits.outp.highvals = outhigh;
	if ((err = bt878_device_control(state->bt, DST_IG_WRITE, &bits)) < 0) {
106
		dprintk(verbose, DST_INFO, 1, "dst_gpio_outb error (err == %i, enbb == %02x, outhigh == %02x)", err, enbb, outhigh);
L
Linus Torvalds 已提交
107 108
		return -EREMOTEIO;
	}
109

L
Linus Torvalds 已提交
110 111
	return 0;
}
112
EXPORT_SYMBOL(dst_gpio_outb);
L
Linus Torvalds 已提交
113

114
int dst_gpio_inb(struct dst_state *state, u8 *result)
L
Linus Torvalds 已提交
115 116 117 118 119 120
{
	union dst_gpio_packet rd_packet;
	int err;

	*result = 0;
	if ((err = bt878_device_control(state->bt, DST_IG_READ, &rd_packet)) < 0) {
121
		dprintk(verbose, DST_ERROR, 1, "dst_gpio_inb error (err == %i)", err);
L
Linus Torvalds 已提交
122 123 124
		return -EREMOTEIO;
	}
	*result = (u8) rd_packet.rd.value;
125

L
Linus Torvalds 已提交
126 127
	return 0;
}
128
EXPORT_SYMBOL(dst_gpio_inb);
L
Linus Torvalds 已提交
129

130
int rdc_reset_state(struct dst_state *state)
L
Linus Torvalds 已提交
131
{
132
	dprintk(verbose, DST_INFO, 1, "Resetting state machine");
133
	if (dst_gpio_outb(state, RDC_8820_INT, RDC_8820_INT, 0, NO_DELAY) < 0) {
134
		dprintk(verbose, DST_ERROR, 1, "dst_gpio_outb ERROR !");
135 136
		return -1;
	}
L
Linus Torvalds 已提交
137
	msleep(10);
138
	if (dst_gpio_outb(state, RDC_8820_INT, RDC_8820_INT, RDC_8820_INT, NO_DELAY) < 0) {
139
		dprintk(verbose, DST_ERROR, 1, "dst_gpio_outb ERROR !");
140 141 142 143
		msleep(10);
		return -1;
	}

L
Linus Torvalds 已提交
144 145
	return 0;
}
146
EXPORT_SYMBOL(rdc_reset_state);
L
Linus Torvalds 已提交
147

148
int rdc_8820_reset(struct dst_state *state)
L
Linus Torvalds 已提交
149
{
150
	dprintk(verbose, DST_DEBUG, 1, "Resetting DST");
151
	if (dst_gpio_outb(state, RDC_8820_RESET, RDC_8820_RESET, 0, NO_DELAY) < 0) {
152
		dprintk(verbose, DST_ERROR, 1, "dst_gpio_outb ERROR !");
153 154
		return -1;
	}
155
	udelay(1000);
156
	if (dst_gpio_outb(state, RDC_8820_RESET, RDC_8820_RESET, RDC_8820_RESET, DELAY) < 0) {
157
		dprintk(verbose, DST_ERROR, 1, "dst_gpio_outb ERROR !");
158 159 160
		return -1;
	}

L
Linus Torvalds 已提交
161 162
	return 0;
}
163
EXPORT_SYMBOL(rdc_8820_reset);
L
Linus Torvalds 已提交
164

165
int dst_pio_enable(struct dst_state *state)
L
Linus Torvalds 已提交
166
{
167
	if (dst_gpio_outb(state, ~0, RDC_8820_PIO_0_ENABLE, 0, NO_DELAY) < 0) {
168
		dprintk(verbose, DST_ERROR, 1, "dst_gpio_outb ERROR !");
169 170
		return -1;
	}
171
	udelay(1000);
172

173 174 175 176 177 178 179
	return 0;
}
EXPORT_SYMBOL(dst_pio_enable);

int dst_pio_disable(struct dst_state *state)
{
	if (dst_gpio_outb(state, ~0, RDC_8820_PIO_0_DISABLE, RDC_8820_PIO_0_DISABLE, NO_DELAY) < 0) {
180
		dprintk(verbose, DST_ERROR, 1, "dst_gpio_outb ERROR !");
181 182
		return -1;
	}
183 184
	if (state->type_flags & DST_TYPE_HAS_FW_1)
		udelay(1000);
185

L
Linus Torvalds 已提交
186 187
	return 0;
}
188
EXPORT_SYMBOL(dst_pio_disable);
L
Linus Torvalds 已提交
189

190
int dst_wait_dst_ready(struct dst_state *state, u8 delay_mode)
L
Linus Torvalds 已提交
191 192 193
{
	u8 reply;
	int i;
194

L
Linus Torvalds 已提交
195
	for (i = 0; i < 200; i++) {
196
		if (dst_gpio_inb(state, &reply) < 0) {
197
			dprintk(verbose, DST_ERROR, 1, "dst_gpio_inb ERROR !");
198 199 200
			return -1;
		}
		if ((reply & RDC_8820_PIO_0_ENABLE) == 0) {
201
			dprintk(verbose, DST_INFO, 1, "dst wait ready after %d", i);
L
Linus Torvalds 已提交
202 203
			return 1;
		}
204
		msleep(10);
205
	}
206
	dprintk(verbose, DST_NOTICE, 1, "dst wait NOT ready after %d", i);
207 208 209 210 211 212 213

	return 0;
}
EXPORT_SYMBOL(dst_wait_dst_ready);

int dst_error_recovery(struct dst_state *state)
{
214
	dprintk(verbose, DST_NOTICE, 1, "Trying to return from previous errors.");
215 216 217 218 219 220 221 222 223 224 225
	dst_pio_disable(state);
	msleep(10);
	dst_pio_enable(state);
	msleep(10);

	return 0;
}
EXPORT_SYMBOL(dst_error_recovery);

int dst_error_bailout(struct dst_state *state)
{
226
	dprintk(verbose, DST_INFO, 1, "Trying to bailout from previous error.");
227 228 229 230 231 232 233 234
	rdc_8820_reset(state);
	dst_pio_disable(state);
	msleep(10);

	return 0;
}
EXPORT_SYMBOL(dst_error_bailout);

235
int dst_comm_init(struct dst_state *state)
236
{
237
	dprintk(verbose, DST_INFO, 1, "Initializing DST.");
238
	if ((dst_pio_enable(state)) < 0) {
239
		dprintk(verbose, DST_ERROR, 1, "PIO Enable Failed");
240 241 242
		return -1;
	}
	if ((rdc_reset_state(state)) < 0) {
243
		dprintk(verbose, DST_ERROR, 1, "RDC 8820 State RESET Failed.");
244
		return -1;
L
Linus Torvalds 已提交
245
	}
246 247 248 249 250
	if (state->type_flags & DST_TYPE_HAS_FW_1)
		msleep(100);
	else
		msleep(5);

L
Linus Torvalds 已提交
251 252
	return 0;
}
253
EXPORT_SYMBOL(dst_comm_init);
L
Linus Torvalds 已提交
254

255
int write_dst(struct dst_state *state, u8 *data, u8 len)
L
Linus Torvalds 已提交
256 257
{
	struct i2c_msg msg = {
258 259 260 261
		.addr = state->config->demod_address,
		.flags = 0,
		.buf = data,
		.len = len
L
Linus Torvalds 已提交
262
	};
263

L
Linus Torvalds 已提交
264
	int err;
265 266 267 268 269 270 271
	u8 cnt, i;

	dprintk(verbose, DST_NOTICE, 0, "writing [ ");
	for (i = 0; i < len; i++)
		dprintk(verbose, DST_NOTICE, 0, "%02x ", data[i]);
	dprintk(verbose, DST_NOTICE, 0, "]\n");

272
	for (cnt = 0; cnt < 2; cnt++) {
L
Linus Torvalds 已提交
273
		if ((err = i2c_transfer(state->i2c, &msg, 1)) < 0) {
274
			dprintk(verbose, DST_INFO, 1, "_write_dst error (err == %i, len == 0x%02x, b0 == 0x%02x)", err, len, data[0]);
275
			dst_error_recovery(state);
L
Linus Torvalds 已提交
276 277 278 279
			continue;
		} else
			break;
	}
280
	if (cnt >= 2) {
281
		dprintk(verbose, DST_INFO, 1, "RDC 8820 RESET");
282 283 284 285 286
		dst_error_bailout(state);

		return -1;
	}

L
Linus Torvalds 已提交
287 288
	return 0;
}
289
EXPORT_SYMBOL(write_dst);
L
Linus Torvalds 已提交
290

291
int read_dst(struct dst_state *state, u8 *ret, u8 len)
L
Linus Torvalds 已提交
292
{
293 294 295 296 297 298 299
	struct i2c_msg msg = {
		.addr = state->config->demod_address,
		.flags = I2C_M_RD,
		.buf = ret,
		.len = len
	};

L
Linus Torvalds 已提交
300 301 302
	int err;
	int cnt;

303
	for (cnt = 0; cnt < 2; cnt++) {
L
Linus Torvalds 已提交
304
		if ((err = i2c_transfer(state->i2c, &msg, 1)) < 0) {
305
			dprintk(verbose, DST_INFO, 1, "read_dst error (err == %i, len == 0x%02x, b0 == 0x%02x)", err, len, ret[0]);
306
			dst_error_recovery(state);
L
Linus Torvalds 已提交
307 308 309 310
			continue;
		} else
			break;
	}
311
	if (cnt >= 2) {
312
		dprintk(verbose, DST_INFO, 1, "RDC 8820 RESET");
313 314 315 316
		dst_error_bailout(state);

		return -1;
	}
317 318 319 320 321
	dprintk(verbose, DST_DEBUG, 1, "reply is 0x%x", ret[0]);
	for (err = 1; err < len; err++)
		dprintk(verbose, DST_DEBUG, 0, " 0x%x", ret[err]);
	if (err > 1)
		dprintk(verbose, DST_DEBUG, 0, "\n");
322

L
Linus Torvalds 已提交
323 324
	return 0;
}
325
EXPORT_SYMBOL(read_dst);
L
Linus Torvalds 已提交
326

327
static int dst_set_polarization(struct dst_state *state)
L
Linus Torvalds 已提交
328
{
329
	switch (state->voltage) {
330 331 332 333 334 335 336 337 338 339
	case SEC_VOLTAGE_13:	/*	Vertical	*/
		dprintk(verbose, DST_INFO, 1, "Polarization=[Vertical]");
		state->tx_tuna[8] &= ~0x40;
		break;
	case SEC_VOLTAGE_18:	/*	Horizontal	*/
		dprintk(verbose, DST_INFO, 1, "Polarization=[Horizontal]");
		state->tx_tuna[8] |= 0x40;
		break;
	case SEC_VOLTAGE_OFF:
		break;
340 341 342 343 344 345 346
	}

	return 0;
}

static int dst_set_freq(struct dst_state *state, u32 freq)
{
L
Linus Torvalds 已提交
347
	state->frequency = freq;
348
	dprintk(verbose, DST_INFO, 1, "set Frequency %u", freq);
L
Linus Torvalds 已提交
349 350 351 352 353

	if (state->dst_type == DST_TYPE_IS_SAT) {
		freq = freq / 1000;
		if (freq < 950 || freq > 2150)
			return -EINVAL;
354 355 356 357 358 359 360 361
		state->tx_tuna[2] = (freq >> 8);
		state->tx_tuna[3] = (u8) freq;
		state->tx_tuna[4] = 0x01;
		state->tx_tuna[8] &= ~0x04;
		if (state->type_flags & DST_TYPE_HAS_OBS_REGS) {
			if (freq < 1531)
				state->tx_tuna[8] |= 0x04;
		}
L
Linus Torvalds 已提交
362 363 364 365
	} else if (state->dst_type == DST_TYPE_IS_TERR) {
		freq = freq / 1000;
		if (freq < 137000 || freq > 858000)
			return -EINVAL;
366 367 368
		state->tx_tuna[2] = (freq >> 16) & 0xff;
		state->tx_tuna[3] = (freq >> 8) & 0xff;
		state->tx_tuna[4] = (u8) freq;
L
Linus Torvalds 已提交
369
	} else if (state->dst_type == DST_TYPE_IS_CABLE) {
370
		freq = freq / 1000;
371 372 373
		state->tx_tuna[2] = (freq >> 16) & 0xff;
		state->tx_tuna[3] = (freq >> 8) & 0xff;
		state->tx_tuna[4] = (u8) freq;
374 375 376 377 378 379 380 381 382 383 384
	} else if (state->dst_type == DST_TYPE_IS_ATSC) {
		freq = freq / 1000;
		if (freq < 51000 || freq > 858000)
			return -EINVAL;
		state->tx_tuna[2] = (freq >> 16) & 0xff;
		state->tx_tuna[3] = (freq >>  8) & 0xff;
		state->tx_tuna[4] = (u8) freq;
		state->tx_tuna[5] = 0x00;		/*	ATSC	*/
		state->tx_tuna[6] = 0x00;
		if (state->dst_hw_cap & DST_TYPE_HAS_ANALOG)
			state->tx_tuna[7] = 0x00;	/*	Digital	*/
L
Linus Torvalds 已提交
385 386
	} else
		return -EINVAL;
387

L
Linus Torvalds 已提交
388 389 390
	return 0;
}

391
static int dst_set_bandwidth(struct dst_state *state, fe_bandwidth_t bandwidth)
L
Linus Torvalds 已提交
392 393 394 395
{
	state->bandwidth = bandwidth;

	if (state->dst_type != DST_TYPE_IS_TERR)
396
		return -EOPNOTSUPP;
L
Linus Torvalds 已提交
397 398

	switch (bandwidth) {
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
	case BANDWIDTH_6_MHZ:
		if (state->dst_hw_cap & DST_TYPE_HAS_CA)
			state->tx_tuna[7] = 0x06;
		else {
			state->tx_tuna[6] = 0x06;
			state->tx_tuna[7] = 0x00;
		}
		break;
	case BANDWIDTH_7_MHZ:
		if (state->dst_hw_cap & DST_TYPE_HAS_CA)
			state->tx_tuna[7] = 0x07;
		else {
			state->tx_tuna[6] = 0x07;
			state->tx_tuna[7] = 0x00;
		}
		break;
	case BANDWIDTH_8_MHZ:
		if (state->dst_hw_cap & DST_TYPE_HAS_CA)
			state->tx_tuna[7] = 0x08;
		else {
			state->tx_tuna[6] = 0x08;
			state->tx_tuna[7] = 0x00;
		}
		break;
	default:
		return -EINVAL;
L
Linus Torvalds 已提交
425
	}
426

L
Linus Torvalds 已提交
427 428 429
	return 0;
}

430
static int dst_set_inversion(struct dst_state *state, fe_spectral_inversion_t inversion)
L
Linus Torvalds 已提交
431 432 433
{
	state->inversion = inversion;
	switch (inversion) {
434 435 436 437 438 439 440 441
	case INVERSION_OFF:	/*	Inversion = Normal	*/
		state->tx_tuna[8] &= ~0x80;
		break;
	case INVERSION_ON:
		state->tx_tuna[8] |= 0x80;
		break;
	default:
		return -EINVAL;
L
Linus Torvalds 已提交
442
	}
443

L
Linus Torvalds 已提交
444 445 446
	return 0;
}

447
static int dst_set_fec(struct dst_state *state, fe_code_rate_t fec)
L
Linus Torvalds 已提交
448 449 450 451 452
{
	state->fec = fec;
	return 0;
}

453
static fe_code_rate_t dst_get_fec(struct dst_state *state)
L
Linus Torvalds 已提交
454 455 456 457
{
	return state->fec;
}

458
static int dst_set_symbolrate(struct dst_state *state, u32 srate)
L
Linus Torvalds 已提交
459 460 461 462 463 464
{
	u32 symcalc;
	u64 sval;

	state->symbol_rate = srate;
	if (state->dst_type == DST_TYPE_IS_TERR) {
465
		return -EOPNOTSUPP;
L
Linus Torvalds 已提交
466
	}
467
	dprintk(verbose, DST_INFO, 1, "set symrate %u", srate);
L
Linus Torvalds 已提交
468
	srate /= 1000;
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
	if (state->dst_type == DST_TYPE_IS_SAT) {
		if (state->type_flags & DST_TYPE_HAS_SYMDIV) {
			sval = srate;
			sval <<= 20;
			do_div(sval, 88000);
			symcalc = (u32) sval;
			dprintk(verbose, DST_INFO, 1, "set symcalc %u", symcalc);
			state->tx_tuna[5] = (u8) (symcalc >> 12);
			state->tx_tuna[6] = (u8) (symcalc >> 4);
			state->tx_tuna[7] = (u8) (symcalc << 4);
		} else {
			state->tx_tuna[5] = (u8) (srate >> 16) & 0x7f;
			state->tx_tuna[6] = (u8) (srate >> 8);
			state->tx_tuna[7] = (u8) srate;
		}
		state->tx_tuna[8] &= ~0x20;
		if (state->type_flags & DST_TYPE_HAS_OBS_REGS) {
			if (srate > 8000)
				state->tx_tuna[8] |= 0x20;
		}
	} else if (state->dst_type == DST_TYPE_IS_CABLE) {
		dprintk(verbose, DST_DEBUG, 1, "%s", state->fw_name);
		if (!strncmp(state->fw_name, "DCTNEW", 6)) {
			state->tx_tuna[5] = (u8) (srate >> 8);
			state->tx_tuna[6] = (u8) srate;
			state->tx_tuna[7] = 0x00;
		} else if (!strncmp(state->fw_name, "DCT-CI", 6)) {
			state->tx_tuna[5] = 0x00;
			state->tx_tuna[6] = (u8) (srate >> 8);
			state->tx_tuna[7] = (u8) srate;
		}
L
Linus Torvalds 已提交
500 501 502 503
	}
	return 0;
}

504 505 506
static int dst_set_modulation(struct dst_state *state, fe_modulation_t modulation)
{
	if (state->dst_type != DST_TYPE_IS_CABLE)
507
		return -EOPNOTSUPP;
508 509 510

	state->modulation = modulation;
	switch (modulation) {
511 512 513 514 515 516 517 518 519 520 521 522 523
	case QAM_16:
		state->tx_tuna[8] = 0x10;
		break;
	case QAM_32:
		state->tx_tuna[8] = 0x20;
		break;
	case QAM_64:
		state->tx_tuna[8] = 0x40;
		break;
	case QAM_128:
		state->tx_tuna[8] = 0x80;
		break;
	case QAM_256:
524 525 526 527
		if (!strncmp(state->fw_name, "DCTNEW", 6))
			state->tx_tuna[8] = 0xff;
		else if (!strncmp(state->fw_name, "DCT-CI", 6))
			state->tx_tuna[8] = 0x00;
528 529 530 531 532 533 534
		break;
	case QPSK:
	case QAM_AUTO:
	case VSB_8:
	case VSB_16:
	default:
		return -EINVAL;
535 536 537 538 539 540 541 542 543 544 545 546

	}

	return 0;
}

static fe_modulation_t dst_get_modulation(struct dst_state *state)
{
	return state->modulation;
}


547
u8 dst_check_sum(u8 *buf, u32 len)
L
Linus Torvalds 已提交
548 549 550 551 552 553 554 555 556 557
{
	u32 i;
	u8 val = 0;
	if (!len)
		return 0;
	for (i = 0; i < len; i++) {
		val += buf[i];
	}
	return ((~val) + 1);
}
558
EXPORT_SYMBOL(dst_check_sum);
L
Linus Torvalds 已提交
559

560
static void dst_type_flags_print(struct dst_state *state)
L
Linus Torvalds 已提交
561
{
562 563
	u32 type_flags = state->type_flags;

564
	dprintk(verbose, DST_ERROR, 0, "DST type flags :");
565 566
	if (type_flags & DST_TYPE_HAS_TS188)
		dprintk(verbose, DST_ERROR, 0, " 0x%x newtuner", DST_TYPE_HAS_TS188);
567 568
	if (type_flags & DST_TYPE_HAS_NEWTUNE_2)
		dprintk(verbose, DST_ERROR, 0, " 0x%x newtuner 2", DST_TYPE_HAS_NEWTUNE_2);
L
Linus Torvalds 已提交
569
	if (type_flags & DST_TYPE_HAS_TS204)
570
		dprintk(verbose, DST_ERROR, 0, " 0x%x ts204", DST_TYPE_HAS_TS204);
M
Manu Abraham 已提交
571 572
	if (type_flags & DST_TYPE_HAS_VLF)
		dprintk(verbose, DST_ERROR, 0, " 0x%x VLF", DST_TYPE_HAS_VLF);
L
Linus Torvalds 已提交
573
	if (type_flags & DST_TYPE_HAS_SYMDIV)
574
		dprintk(verbose, DST_ERROR, 0, " 0x%x symdiv", DST_TYPE_HAS_SYMDIV);
575
	if (type_flags & DST_TYPE_HAS_FW_1)
576
		dprintk(verbose, DST_ERROR, 0, " 0x%x firmware version = 1", DST_TYPE_HAS_FW_1);
577
	if (type_flags & DST_TYPE_HAS_FW_2)
578
		dprintk(verbose, DST_ERROR, 0, " 0x%x firmware version = 2", DST_TYPE_HAS_FW_2);
579
	if (type_flags & DST_TYPE_HAS_FW_3)
580 581
		dprintk(verbose, DST_ERROR, 0, " 0x%x firmware version = 3", DST_TYPE_HAS_FW_3);
	dprintk(verbose, DST_ERROR, 0, "\n");
L
Linus Torvalds 已提交
582 583
}

584

585
static int dst_type_print(struct dst_state *state, u8 type)
L
Linus Torvalds 已提交
586 587 588 589 590 591
{
	char *otype;
	switch (type) {
	case DST_TYPE_IS_SAT:
		otype = "satellite";
		break;
592

L
Linus Torvalds 已提交
593 594 595
	case DST_TYPE_IS_TERR:
		otype = "terrestrial";
		break;
596

L
Linus Torvalds 已提交
597 598 599
	case DST_TYPE_IS_CABLE:
		otype = "cable";
		break;
600

601 602 603 604
	case DST_TYPE_IS_ATSC:
		otype = "atsc";
		break;

L
Linus Torvalds 已提交
605
	default:
606
		dprintk(verbose, DST_INFO, 1, "invalid dst type %d", type);
L
Linus Torvalds 已提交
607 608
		return -EINVAL;
	}
609
	dprintk(verbose, DST_INFO, 1, "DST type: %s", otype);
610

L
Linus Torvalds 已提交
611 612 613
	return 0;
}

614 615
struct tuner_types tuner_list[] = {
	{
616
		.tuner_type = TUNER_TYPE_L64724,
M
Manu Abraham 已提交
617
		.tuner_name = "L 64724",
618 619
		.board_name = "UNKNOWN",
		.fw_name    = "UNKNOWN"
620 621 622
	},

	{
623
		.tuner_type = TUNER_TYPE_STV0299,
M
Manu Abraham 已提交
624
		.tuner_name = "STV 0299",
625 626
		.board_name = "VP1020",
		.fw_name    = "DST-MOT"
627 628 629
	},

	{
630 631 632 633 634 635 636 637
		.tuner_type = TUNER_TYPE_STV0299,
		.tuner_name = "STV 0299",
		.board_name = "VP1020",
		.fw_name    = "DST-03T"
	},

	{
		.tuner_type = TUNER_TYPE_MB86A15,
M
Manu Abraham 已提交
638
		.tuner_name = "MB 86A15",
639 640
		.board_name = "VP1022",
		.fw_name    = "DST-03T"
641
	},
M
Manu Abraham 已提交
642 643

	{
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
		.tuner_type = TUNER_TYPE_MB86A15,
		.tuner_name = "MB 86A15",
		.board_name = "VP1025",
		.fw_name    = "DST-03T"
	},

	{
		.tuner_type = TUNER_TYPE_STV0299,
		.tuner_name = "STV 0299",
		.board_name = "VP1030",
		.fw_name    = "DST-CI"
	},

	{
		.tuner_type = TUNER_TYPE_STV0299,
		.tuner_name = "STV 0299",
		.board_name = "VP1030",
		.fw_name    = "DSTMCI"
	},

664 665 666 667 668 669 670
	{
		.tuner_type = TUNER_TYPE_UNKNOWN,
		.tuner_name = "UNKNOWN",
		.board_name = "VP2021",
		.fw_name    = "DCTNEW"
	},

671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
	{
		.tuner_type = TUNER_TYPE_UNKNOWN,
		.tuner_name = "UNKNOWN",
		.board_name = "VP2030",
		.fw_name    = "DCT-CI"
	},

	{
		.tuner_type = TUNER_TYPE_UNKNOWN,
		.tuner_name = "UNKNOWN",
		.board_name = "VP2031",
		.fw_name    = "DCT-CI"
	},

	{
		.tuner_type = TUNER_TYPE_UNKNOWN,
		.tuner_name = "UNKNOWN",
		.board_name = "VP2040",
		.fw_name    = "DCT-CI"
	},

	{
		.tuner_type = TUNER_TYPE_UNKNOWN,
		.tuner_name = "UNKNOWN",
		.board_name = "VP3020",
		.fw_name    = "DTTFTA"
	},

	{
		.tuner_type = TUNER_TYPE_UNKNOWN,
		.tuner_name = "UNKNOWN",
		.board_name = "VP3021",
		.fw_name    = "DTTFTA"
	},

	{
		.tuner_type = TUNER_TYPE_TDA10046,
		.tuner_name = "TDA10046",
		.board_name = "VP3040",
		.fw_name    = "DTT-CI"
	},

	{
		.tuner_type = TUNER_TYPE_UNKNOWN,
		.tuner_name = "UNKNOWN",
		.board_name = "VP3051",
		.fw_name    = "DTTNXT"
	},

	{
		.tuner_type = TUNER_TYPE_NXT200x,
		.tuner_name = "NXT200x",
		.board_name = "VP3220",
		.fw_name    = "ATSCDI"
	},

	{
		.tuner_type = TUNER_TYPE_NXT200x,
		.tuner_name = "NXT200x",
		.board_name = "VP3250",
		.fw_name    = "ATSCAD"
	},
733 734
};

735 736 737 738
/*
	Known cards list
	Satellite
	-------------------
739
		  200103A
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
	VP-1020   DST-MOT	LG(old), TS=188

	VP-1020   DST-03T	LG(new), TS=204
	VP-1022   DST-03T	LG(new), TS=204
	VP-1025   DST-03T	LG(new), TS=204

	VP-1030   DSTMCI,	LG(new), TS=188
	VP-1032   DSTMCI,	LG(new), TS=188

	Cable
	-------------------
	VP-2030   DCT-CI,	Samsung, TS=204
	VP-2021   DCT-CI,	Unknown, TS=204
	VP-2031   DCT-CI,	Philips, TS=188
	VP-2040   DCT-CI,	Philips, TS=188, with CA daughter board
	VP-2040   DCT-CI,	Philips, TS=204, without CA daughter board

	Terrestrial
	-------------------
	VP-3050  DTTNXT			 TS=188
	VP-3040  DTT-CI,	Philips, TS=188
	VP-3040  DTT-CI,	Philips, TS=204

	ATSC
	-------------------
	VP-3220  ATSCDI,		 TS=188
	VP-3250  ATSCAD,		 TS=188

*/

770
static struct dst_types dst_tlist[] = {
771 772 773 774
	{
		.device_id = "200103A",
		.offset = 0,
		.dst_type =  DST_TYPE_IS_SAT,
775
		.type_flags = DST_TYPE_HAS_SYMDIV | DST_TYPE_HAS_FW_1 | DST_TYPE_HAS_OBS_REGS,
776 777
		.dst_feature = 0,
		.tuner_type = 0
778 779
	},	/*	obsolete	*/

780 781 782 783 784
	{
		.device_id = "DST-020",
		.offset = 0,
		.dst_type =  DST_TYPE_IS_SAT,
		.type_flags = DST_TYPE_HAS_SYMDIV | DST_TYPE_HAS_FW_1,
785 786
		.dst_feature = 0,
		.tuner_type = 0
787 788 789 790 791 792
	},	/*	obsolete	*/

	{
		.device_id = "DST-030",
		.offset =  0,
		.dst_type = DST_TYPE_IS_SAT,
793
		.type_flags = DST_TYPE_HAS_TS204 | DST_TYPE_HAS_TS188 | DST_TYPE_HAS_FW_1,
794 795
		.dst_feature = 0,
		.tuner_type = 0
796 797 798 799 800 801 802 803
	},	/*	obsolete	*/

	{
		.device_id = "DST-03T",
		.offset = 0,
		.dst_type = DST_TYPE_IS_SAT,
		.type_flags = DST_TYPE_HAS_SYMDIV | DST_TYPE_HAS_TS204 | DST_TYPE_HAS_FW_2,
		.dst_feature = DST_TYPE_HAS_DISEQC3 | DST_TYPE_HAS_DISEQC4 | DST_TYPE_HAS_DISEQC5
804
							 | DST_TYPE_HAS_MAC | DST_TYPE_HAS_MOTO,
805
		.tuner_type = TUNER_TYPE_MULTI
806 807 808 809 810 811 812
	 },

	{
		.device_id = "DST-MOT",
		.offset =  0,
		.dst_type = DST_TYPE_IS_SAT,
		.type_flags = DST_TYPE_HAS_SYMDIV | DST_TYPE_HAS_FW_1,
813 814
		.dst_feature = 0,
		.tuner_type = 0
815 816 817 818 819 820
	},	/*	obsolete	*/

	{
		.device_id = "DST-CI",
		.offset = 1,
		.dst_type = DST_TYPE_IS_SAT,
821
		.type_flags = DST_TYPE_HAS_TS204 | DST_TYPE_HAS_FW_1,
822 823
		.dst_feature = DST_TYPE_HAS_CA,
		.tuner_type = 0
824
	},	/*	An OEM board	*/
825 826 827 828 829

	{
		.device_id = "DSTMCI",
		.offset = 1,
		.dst_type = DST_TYPE_IS_SAT,
M
Manu Abraham 已提交
830
		.type_flags = DST_TYPE_HAS_TS188 | DST_TYPE_HAS_FW_2 | DST_TYPE_HAS_FW_BUILD | DST_TYPE_HAS_INC_COUNT | DST_TYPE_HAS_VLF,
831
		.dst_feature = DST_TYPE_HAS_CA | DST_TYPE_HAS_DISEQC3 | DST_TYPE_HAS_DISEQC4
832 833
							| DST_TYPE_HAS_MOTO | DST_TYPE_HAS_MAC,
		.tuner_type = TUNER_TYPE_MULTI
834 835 836 837 838 839
	},

	{
		.device_id = "DSTFCI",
		.offset = 1,
		.dst_type = DST_TYPE_IS_SAT,
840
		.type_flags = DST_TYPE_HAS_TS188 | DST_TYPE_HAS_FW_1,
841 842
		.dst_feature = 0,
		.tuner_type = 0
843 844 845 846 847 848
	},	/* unknown to vendor	*/

	{
		.device_id = "DCT-CI",
		.offset = 1,
		.dst_type = DST_TYPE_IS_CABLE,
M
Manu Abraham 已提交
849
		.type_flags = DST_TYPE_HAS_MULTI_FE | DST_TYPE_HAS_FW_1	| DST_TYPE_HAS_FW_2 | DST_TYPE_HAS_VLF,
850 851
		.dst_feature = DST_TYPE_HAS_CA,
		.tuner_type = 0
852 853 854 855 856 857
	},

	{
		.device_id = "DCTNEW",
		.offset = 1,
		.dst_type = DST_TYPE_IS_CABLE,
858
		.type_flags = DST_TYPE_HAS_TS188 | DST_TYPE_HAS_FW_3 | DST_TYPE_HAS_FW_BUILD | DST_TYPE_HAS_MULTI_FE,
859 860
		.dst_feature = 0,
		.tuner_type = 0
861 862 863 864 865 866
	},

	{
		.device_id = "DTT-CI",
		.offset = 1,
		.dst_type = DST_TYPE_IS_TERR,
M
Manu Abraham 已提交
867
		.type_flags = DST_TYPE_HAS_FW_2 | DST_TYPE_HAS_MULTI_FE | DST_TYPE_HAS_VLF,
868 869
		.dst_feature = DST_TYPE_HAS_CA,
		.tuner_type = 0
870 871 872 873 874 875 876
	},

	{
		.device_id = "DTTDIG",
		.offset = 1,
		.dst_type = DST_TYPE_IS_TERR,
		.type_flags = DST_TYPE_HAS_FW_2,
877 878
		.dst_feature = 0,
		.tuner_type = 0
879 880 881 882 883 884 885
	},

	{
		.device_id = "DTTNXT",
		.offset = 1,
		.dst_type = DST_TYPE_IS_TERR,
		.type_flags = DST_TYPE_HAS_FW_2,
886 887
		.dst_feature = DST_TYPE_HAS_ANALOG,
		.tuner_type = 0
888 889 890 891 892 893 894
	},

	{
		.device_id = "ATSCDI",
		.offset = 1,
		.dst_type = DST_TYPE_IS_ATSC,
		.type_flags = DST_TYPE_HAS_FW_2,
895 896
		.dst_feature = 0,
		.tuner_type = 0
897 898 899 900 901 902
	},

	{
		.device_id = "ATSCAD",
		.offset = 1,
		.dst_type = DST_TYPE_IS_ATSC,
903
		.type_flags = DST_TYPE_HAS_MULTI_FE | DST_TYPE_HAS_FW_2 | DST_TYPE_HAS_FW_BUILD,
904 905
		.dst_feature = DST_TYPE_HAS_MAC | DST_TYPE_HAS_ANALOG,
		.tuner_type = 0
906 907 908 909 910 911
	},

	{ }

};

M
Manu Abraham 已提交
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
static int dst_get_mac(struct dst_state *state)
{
	u8 get_mac[] = { 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
	get_mac[7] = dst_check_sum(get_mac, 7);
	if (dst_command(state, get_mac, 8) < 0) {
		dprintk(verbose, DST_INFO, 1, "Unsupported Command");
		return -1;
	}
	memset(&state->mac_address, '\0', 8);
	memcpy(&state->mac_address, &state->rxbuffer, 6);
	dprintk(verbose, DST_ERROR, 1, "MAC Address=[%02x:%02x:%02x:%02x:%02x:%02x]",
		state->mac_address[0], state->mac_address[1], state->mac_address[2],
		state->mac_address[4], state->mac_address[5], state->mac_address[6]);

	return 0;
}

static int dst_fw_ver(struct dst_state *state)
{
	u8 get_ver[] = { 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
	get_ver[7] = dst_check_sum(get_ver, 7);
	if (dst_command(state, get_ver, 8) < 0) {
		dprintk(verbose, DST_INFO, 1, "Unsupported Command");
		return -1;
	}
	memset(&state->fw_version, '\0', 8);
	memcpy(&state->fw_version, &state->rxbuffer, 8);
	dprintk(verbose, DST_ERROR, 1, "Firmware Ver = %x.%x Build = %02x, on %x:%x, %x-%x-20%02x",
		state->fw_version[0] >> 4, state->fw_version[0] & 0x0f,
		state->fw_version[1],
		state->fw_version[5], state->fw_version[6],
		state->fw_version[4], state->fw_version[3], state->fw_version[2]);

	return 0;
}

static int dst_card_type(struct dst_state *state)
{
M
Manu Abraham 已提交
950 951 952
	int j;
	struct tuner_types *p_tuner_list = NULL;

M
Manu Abraham 已提交
953 954 955 956 957 958 959
	u8 get_type[] = { 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
	get_type[7] = dst_check_sum(get_type, 7);
	if (dst_command(state, get_type, 8) < 0) {
		dprintk(verbose, DST_INFO, 1, "Unsupported Command");
		return -1;
	}
	memset(&state->card_info, '\0', 8);
M
Manu Abraham 已提交
960
	memcpy(&state->card_info, &state->rxbuffer, 7);
M
Manu Abraham 已提交
961 962
	dprintk(verbose, DST_ERROR, 1, "Device Model=[%s]", &state->card_info[0]);

M
Manu Abraham 已提交
963 964 965
	for (j = 0, p_tuner_list = tuner_list; j < ARRAY_SIZE(tuner_list); j++, p_tuner_list++) {
		if (!strcmp(&state->card_info[0], p_tuner_list->board_name)) {
			state->tuner_type = p_tuner_list->tuner_type;
966
			dprintk(verbose, DST_ERROR, 1, "DST has [%s] tuner, tuner type=[%d]",
M
Manu Abraham 已提交
967 968 969 970
				p_tuner_list->tuner_name, p_tuner_list->tuner_type);
		}
	}

M
Manu Abraham 已提交
971 972 973 974 975 976 977 978 979 980 981 982
	return 0;
}

static int dst_get_vendor(struct dst_state *state)
{
	u8 get_vendor[] = { 0x00, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
	get_vendor[7] = dst_check_sum(get_vendor, 7);
	if (dst_command(state, get_vendor, 8) < 0) {
		dprintk(verbose, DST_INFO, 1, "Unsupported Command");
		return -1;
	}
	memset(&state->vendor, '\0', 8);
M
Manu Abraham 已提交
983
	memcpy(&state->vendor, &state->rxbuffer, 7);
M
Manu Abraham 已提交
984 985 986 987
	dprintk(verbose, DST_ERROR, 1, "Vendor=[%s]", &state->vendor[0]);

	return 0;
}
988

989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
static void debug_dst_buffer(struct dst_state *state)
{
	int i;

	if (verbose > 2) {
		printk("%s: [", __func__);
		for (i = 0; i < 8; i++)
			printk(" %02x", state->rxbuffer[i]);
		printk("]\n");
	}
}

static int dst_check_stv0299(struct dst_state *state)
{
	u8 check_stv0299[] = { 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

	check_stv0299[7] = dst_check_sum(check_stv0299, 7);
	if (dst_command(state, check_stv0299, 8) < 0) {
		dprintk(verbose, DST_ERROR, 1, "Cmd=[0x04] failed");
		return -1;
	}
	debug_dst_buffer(state);

	if (memcmp(&check_stv0299, &state->rxbuffer, 8)) {
		dprintk(verbose, DST_ERROR, 1, "Found a STV0299 NIM");
		state->tuner_type = TUNER_TYPE_STV0299;
		return 0;
	}

	return -1;
}

static int dst_check_mb86a15(struct dst_state *state)
{
	u8 check_mb86a15[] = { 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

	check_mb86a15[7] = dst_check_sum(check_mb86a15, 7);
	if (dst_command(state, check_mb86a15, 8) < 0) {
		dprintk(verbose, DST_ERROR, 1, "Cmd=[0x10], failed");
		return -1;
	}
	debug_dst_buffer(state);

	if (memcmp(&check_mb86a15, &state->rxbuffer, 8) < 0) {
		dprintk(verbose, DST_ERROR, 1, "Found a MB86A15 NIM");
		state->tuner_type = TUNER_TYPE_MB86A15;
		return 0;
	}

	return -1;
}

1041 1042 1043 1044 1045 1046 1047
static int dst_get_tuner_info(struct dst_state *state)
{
	u8 get_tuner_1[] = { 0x00, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
	u8 get_tuner_2[] = { 0x00, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

	get_tuner_1[7] = dst_check_sum(get_tuner_1, 7);
	get_tuner_2[7] = dst_check_sum(get_tuner_2, 7);
1048
	dprintk(verbose, DST_ERROR, 1, "DST TYpe = MULTI FE");
1049
	if (state->type_flags & DST_TYPE_HAS_MULTI_FE) {
1050 1051
		if (dst_command(state, get_tuner_1, 8) < 0) {
			dprintk(verbose, DST_INFO, 1, "Cmd=[0x13], Unsupported");
M
Manu Abraham 已提交
1052
			goto force;
1053 1054
		}
	} else {
1055 1056
		if (dst_command(state, get_tuner_2, 8) < 0) {
			dprintk(verbose, DST_INFO, 1, "Cmd=[0xb], Unsupported");
M
Manu Abraham 已提交
1057
			goto force;
1058 1059 1060 1061 1062
		}
	}
	memset(&state->board_info, '\0', 8);
	memcpy(&state->board_info, &state->rxbuffer, 8);
	if (state->type_flags & DST_TYPE_HAS_MULTI_FE) {
1063 1064 1065
		dprintk(verbose, DST_ERROR, 1, "DST type has TS=188");
	}
	if (state->board_info[0] == 0xbc) {
1066
		if (state->type_flags != DST_TYPE_IS_ATSC)
1067
			state->type_flags |= DST_TYPE_HAS_TS188;
1068
		else
1069
			state->type_flags |= DST_TYPE_HAS_NEWTUNE_2;
1070

1071 1072 1073 1074
		if (state->board_info[1] == 0x01) {
			state->dst_hw_cap |= DST_TYPE_HAS_DBOARD;
			dprintk(verbose, DST_ERROR, 1, "DST has Daughterboard");
		}
1075 1076 1077
	}

	return 0;
M
Manu Abraham 已提交
1078 1079 1080 1081 1082 1083 1084
force:
	if (!strncmp(state->fw_name, "DCT-CI", 6)) {
		state->type_flags |= DST_TYPE_HAS_TS204;
		dprintk(verbose, DST_ERROR, 1, "Forcing [%s] to TS188", state->fw_name);
	}

	return -1;
1085 1086
}

1087
static int dst_get_device_id(struct dst_state *state)
L
Linus Torvalds 已提交
1088
{
1089 1090
	u8 reply;

1091
	int i, j;
1092 1093
	struct dst_types *p_dst_type = NULL;
	struct tuner_types *p_tuner_list = NULL;
1094

1095 1096
	u8 use_dst_type = 0;
	u32 use_type_flags = 0;
L
Linus Torvalds 已提交
1097

1098
	static u8 device_type[8] = {0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff};
L
Linus Torvalds 已提交
1099

1100
	state->tuner_type = 0;
1101 1102 1103 1104 1105 1106 1107 1108 1109
	device_type[7] = dst_check_sum(device_type, 7);

	if (write_dst(state, device_type, FIXED_COMM))
		return -1;		/*	Write failed		*/
	if ((dst_pio_disable(state)) < 0)
		return -1;
	if (read_dst(state, &reply, GET_ACK))
		return -1;		/*	Read failure		*/
	if (reply != ACK) {
1110
		dprintk(verbose, DST_INFO, 1, "Write not Acknowledged! [Reply=0x%02x]", reply);
1111
		return -1;		/*	Unack'd write		*/
L
Linus Torvalds 已提交
1112
	}
1113 1114 1115 1116 1117 1118 1119
	if (!dst_wait_dst_ready(state, DEVICE_INIT))
		return -1;		/*	DST not ready yet	*/
	if (read_dst(state, state->rxbuffer, FIXED_COMM))
		return -1;

	dst_pio_disable(state);
	if (state->rxbuffer[7] != dst_check_sum(state->rxbuffer, 7)) {
1120
		dprintk(verbose, DST_INFO, 1, "Checksum failure!");
1121
		return -1;		/*	Checksum failure	*/
L
Linus Torvalds 已提交
1122
	}
1123 1124
	state->rxbuffer[7] = '\0';

1125
	for (i = 0, p_dst_type = dst_tlist; i < ARRAY_SIZE(dst_tlist); i++, p_dst_type++) {
1126 1127 1128 1129 1130 1131
		if (!strncmp (&state->rxbuffer[p_dst_type->offset], p_dst_type->device_id, strlen (p_dst_type->device_id))) {
			use_type_flags = p_dst_type->type_flags;
			use_dst_type = p_dst_type->dst_type;

			/*	Card capabilities	*/
			state->dst_hw_cap = p_dst_type->dst_feature;
1132
			dprintk(verbose, DST_ERROR, 1, "Recognise [%s]", p_dst_type->device_id);
1133
			strncpy(&state->fw_name[0], p_dst_type->device_id, 6);
1134 1135
			/*	Multiple tuners		*/
			if (p_dst_type->tuner_type & TUNER_TYPE_MULTI) {
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
				switch (use_dst_type) {
				case DST_TYPE_IS_SAT:
					/*	STV0299 check	*/
					if (dst_check_stv0299(state) < 0) {
						dprintk(verbose, DST_ERROR, 1, "Unsupported");
						state->tuner_type = TUNER_TYPE_MB86A15;
					}
					break;
				default:
					break;
				}
1147 1148 1149 1150
				if (dst_check_mb86a15(state) < 0)
					dprintk(verbose, DST_ERROR, 1, "Unsupported");
			/*	Single tuner		*/
			} else {
1151
				state->tuner_type = p_dst_type->tuner_type;
1152 1153 1154 1155 1156 1157
			}
			for (j = 0, p_tuner_list = tuner_list; j < ARRAY_SIZE(tuner_list); j++, p_tuner_list++) {
				if (!(strncmp(p_dst_type->device_id, p_tuner_list->fw_name, 7)) &&
					p_tuner_list->tuner_type == state->tuner_type) {
					dprintk(verbose, DST_ERROR, 1, "[%s] has a [%s]",
						p_dst_type->device_id, p_tuner_list->tuner_name);
1158 1159
				}
			}
L
Linus Torvalds 已提交
1160 1161 1162
			break;
		}
	}
1163 1164

	if (i >= sizeof (dst_tlist) / sizeof (dst_tlist [0])) {
1165 1166
		dprintk(verbose, DST_ERROR, 1, "Unable to recognize %s or %s", &state->rxbuffer[0], &state->rxbuffer[1]);
		dprintk(verbose, DST_ERROR, 1, "please email linux-dvb@linuxtv.org with this type in");
L
Linus Torvalds 已提交
1167 1168 1169
		use_dst_type = DST_TYPE_IS_SAT;
		use_type_flags = DST_TYPE_HAS_SYMDIV;
	}
1170
	dst_type_print(state, use_dst_type);
L
Linus Torvalds 已提交
1171 1172
	state->type_flags = use_type_flags;
	state->dst_type = use_dst_type;
1173
	dst_type_flags_print(state);
L
Linus Torvalds 已提交
1174 1175 1176 1177

	return 0;
}

1178 1179
static int dst_probe(struct dst_state *state)
{
1180
	mutex_init(&state->dst_mutex);
1181 1182 1183 1184 1185
	if (dst_addons & DST_TYPE_HAS_CA) {
		if ((rdc_8820_reset(state)) < 0) {
			dprintk(verbose, DST_ERROR, 1, "RDC 8820 RESET Failed.");
			return -1;
		}
1186
		msleep(4000);
1187
	} else {
1188
		msleep(100);
1189
	}
1190
	if ((dst_comm_init(state)) < 0) {
1191
		dprintk(verbose, DST_ERROR, 1, "DST Initialization Failed.");
1192 1193
		return -1;
	}
1194
	msleep(100);
1195
	if (dst_get_device_id(state) < 0) {
1196
		dprintk(verbose, DST_ERROR, 1, "unknown device.");
1197 1198
		return -1;
	}
M
Manu Abraham 已提交
1199 1200 1201
	if (dst_get_mac(state) < 0) {
		dprintk(verbose, DST_INFO, 1, "MAC: Unsupported command");
	}
1202 1203 1204 1205
	if ((state->type_flags & DST_TYPE_HAS_MULTI_FE) || (state->type_flags & DST_TYPE_HAS_FW_BUILD)) {
		if (dst_get_tuner_info(state) < 0)
			dprintk(verbose, DST_INFO, 1, "Tuner: Unsupported command");
	}
1206 1207 1208
	if (state->type_flags & DST_TYPE_HAS_TS204) {
		dst_packsize(state, 204);
	}
M
Manu Abraham 已提交
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
	if (state->type_flags & DST_TYPE_HAS_FW_BUILD) {
		if (dst_fw_ver(state) < 0) {
			dprintk(verbose, DST_INFO, 1, "FW: Unsupported command");
			return 0;
		}
		if (dst_card_type(state) < 0) {
			dprintk(verbose, DST_INFO, 1, "Card: Unsupported command");
			return 0;
		}
		if (dst_get_vendor(state) < 0) {
			dprintk(verbose, DST_INFO, 1, "Vendor: Unsupported command");
			return 0;
		}
	}
1223 1224 1225 1226

	return 0;
}

1227
int dst_command(struct dst_state *state, u8 *data, u8 len)
L
Linus Torvalds 已提交
1228 1229
{
	u8 reply;
1230

1231
	mutex_lock(&state->dst_mutex);
1232
	if ((dst_comm_init(state)) < 0) {
1233
		dprintk(verbose, DST_NOTICE, 1, "DST Communication Initialization Failed.");
1234
		goto error;
1235 1236
	}
	if (write_dst(state, data, len)) {
1237
		dprintk(verbose, DST_INFO, 1, "Trying to recover.. ");
1238
		if ((dst_error_recovery(state)) < 0) {
1239
			dprintk(verbose, DST_ERROR, 1, "Recovery Failed.");
1240
			goto error;
1241
		}
1242
		goto error;
L
Linus Torvalds 已提交
1243
	}
1244
	if ((dst_pio_disable(state)) < 0) {
1245
		dprintk(verbose, DST_ERROR, 1, "PIO Disable Failed.");
1246
		goto error;
L
Linus Torvalds 已提交
1247
	}
1248 1249
	if (state->type_flags & DST_TYPE_HAS_FW_1)
		udelay(3000);
1250
	if (read_dst(state, &reply, GET_ACK)) {
1251
		dprintk(verbose, DST_DEBUG, 1, "Trying to recover.. ");
1252
		if ((dst_error_recovery(state)) < 0) {
1253
			dprintk(verbose, DST_INFO, 1, "Recovery Failed.");
1254
			goto error;
1255
		}
1256
		goto error;
1257 1258
	}
	if (reply != ACK) {
1259
		dprintk(verbose, DST_INFO, 1, "write not acknowledged 0x%02x ", reply);
1260
		goto error;
L
Linus Torvalds 已提交
1261 1262
	}
	if (len >= 2 && data[0] == 0 && (data[1] == 1 || data[1] == 3))
1263
		goto error;
1264 1265 1266 1267
	if (state->type_flags & DST_TYPE_HAS_FW_1)
		udelay(3000);
	else
		udelay(2000);
1268
	if (!dst_wait_dst_ready(state, NO_DELAY))
1269
		goto error;
1270
	if (read_dst(state, state->rxbuffer, FIXED_COMM)) {
1271
		dprintk(verbose, DST_DEBUG, 1, "Trying to recover.. ");
1272
		if ((dst_error_recovery(state)) < 0) {
1273
			dprintk(verbose, DST_INFO, 1, "Recovery failed.");
1274
			goto error;
1275
		}
1276
		goto error;
L
Linus Torvalds 已提交
1277 1278
	}
	if (state->rxbuffer[7] != dst_check_sum(state->rxbuffer, 7)) {
1279
		dprintk(verbose, DST_INFO, 1, "checksum failure");
1280
		goto error;
L
Linus Torvalds 已提交
1281
	}
1282
	mutex_unlock(&state->dst_mutex);
L
Linus Torvalds 已提交
1283
	return 0;
1284 1285

error:
1286
	mutex_unlock(&state->dst_mutex);
1287 1288
	return -EIO;

L
Linus Torvalds 已提交
1289
}
1290
EXPORT_SYMBOL(dst_command);
L
Linus Torvalds 已提交
1291

1292
static int dst_get_signal(struct dst_state *state)
L
Linus Torvalds 已提交
1293 1294 1295
{
	int retval;
	u8 get_signal[] = { 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfb };
1296
	//dprintk("%s: Getting Signal strength and other parameters\n", __FUNCTION__);
L
Linus Torvalds 已提交
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
	if ((state->diseq_flags & ATTEMPT_TUNE) == 0) {
		state->decode_lock = state->decode_strength = state->decode_snr = 0;
		return 0;
	}
	if (0 == (state->diseq_flags & HAS_LOCK)) {
		state->decode_lock = state->decode_strength = state->decode_snr = 0;
		return 0;
	}
	if (time_after_eq(jiffies, state->cur_jiff + (HZ / 5))) {
		retval = dst_command(state, get_signal, 8);
		if (retval < 0)
			return retval;
		if (state->dst_type == DST_TYPE_IS_SAT) {
			state->decode_lock = ((state->rxbuffer[6] & 0x10) == 0) ? 1 : 0;
			state->decode_strength = state->rxbuffer[5] << 8;
			state->decode_snr = state->rxbuffer[2] << 8 | state->rxbuffer[3];
		} else if ((state->dst_type == DST_TYPE_IS_TERR) || (state->dst_type == DST_TYPE_IS_CABLE)) {
			state->decode_lock = (state->rxbuffer[1]) ? 1 : 0;
			state->decode_strength = state->rxbuffer[4] << 8;
			state->decode_snr = state->rxbuffer[3] << 8;
1317 1318 1319 1320
		} else if (state->dst_type == DST_TYPE_IS_ATSC) {
			state->decode_lock = (state->rxbuffer[6] == 0x00) ? 1 : 0;
			state->decode_strength = state->rxbuffer[4] << 8;
			state->decode_snr = state->rxbuffer[2] << 8 | state->rxbuffer[3];
L
Linus Torvalds 已提交
1321 1322 1323 1324 1325 1326
		}
		state->cur_jiff = jiffies;
	}
	return 0;
}

1327
static int dst_tone_power_cmd(struct dst_state *state)
L
Linus Torvalds 已提交
1328 1329 1330
{
	u8 paket[8] = { 0x00, 0x09, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00 };

1331 1332
	if (state->dst_type != DST_TYPE_IS_SAT)
		return -EOPNOTSUPP;
1333
	paket[4] = state->tx_tuna[4];
M
Manu Abraham 已提交
1334
	paket[2] = state->tx_tuna[2];
M
Manu Abraham 已提交
1335
	paket[3] = state->tx_tuna[3];
1336
	paket[7] = dst_check_sum (paket, 7);
1337
	return dst_command(state, paket, 8);
L
Linus Torvalds 已提交
1338 1339
}

1340
static int dst_get_tuna(struct dst_state *state)
L
Linus Torvalds 已提交
1341 1342
{
	int retval;
1343

L
Linus Torvalds 已提交
1344 1345 1346
	if ((state->diseq_flags & ATTEMPT_TUNE) == 0)
		return 0;
	state->diseq_flags &= ~(HAS_LOCK);
1347
	if (!dst_wait_dst_ready(state, NO_DELAY))
1348
		return -EIO;
M
Manu Abraham 已提交
1349
	if ((state->type_flags & DST_TYPE_HAS_VLF) &&
1350 1351
		!(state->dst_type == DST_TYPE_IS_ATSC))

L
Linus Torvalds 已提交
1352
		retval = read_dst(state, state->rx_tuna, 10);
1353
	else
1354
		retval = read_dst(state, &state->rx_tuna[2], FIXED_COMM);
L
Linus Torvalds 已提交
1355
	if (retval < 0) {
1356
		dprintk(verbose, DST_DEBUG, 1, "read not successful");
1357
		return retval;
L
Linus Torvalds 已提交
1358
	}
M
Manu Abraham 已提交
1359
	if ((state->type_flags & DST_TYPE_HAS_VLF) &&
1360
		!(state->dst_type == DST_TYPE_IS_CABLE) &&
1361
		!(state->dst_type == DST_TYPE_IS_ATSC)) {
1362

L
Linus Torvalds 已提交
1363
		if (state->rx_tuna[9] != dst_check_sum(&state->rx_tuna[0], 9)) {
1364
			dprintk(verbose, DST_INFO, 1, "checksum failure ? ");
1365
			return -EIO;
L
Linus Torvalds 已提交
1366 1367 1368
		}
	} else {
		if (state->rx_tuna[9] != dst_check_sum(&state->rx_tuna[2], 7)) {
1369
			dprintk(verbose, DST_INFO, 1, "checksum failure? ");
1370
			return -EIO;
L
Linus Torvalds 已提交
1371 1372 1373 1374
		}
	}
	if (state->rx_tuna[2] == 0 && state->rx_tuna[3] == 0)
		return 0;
1375 1376 1377 1378 1379 1380
	if (state->dst_type == DST_TYPE_IS_SAT) {
		state->decode_freq = ((state->rx_tuna[2] & 0x7f) << 8) + state->rx_tuna[3];
	} else {
		state->decode_freq = ((state->rx_tuna[2] & 0x7f) << 16) + (state->rx_tuna[3] << 8) + state->rx_tuna[4];
	}
	state->decode_freq = state->decode_freq * 1000;
L
Linus Torvalds 已提交
1381 1382
	state->decode_lock = 1;
	state->diseq_flags |= HAS_LOCK;
1383

L
Linus Torvalds 已提交
1384 1385 1386
	return 1;
}

1387
static int dst_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage);
L
Linus Torvalds 已提交
1388

1389
static int dst_write_tuna(struct dvb_frontend *fe)
L
Linus Torvalds 已提交
1390
{
1391
	struct dst_state *state = fe->demodulator_priv;
L
Linus Torvalds 已提交
1392 1393 1394
	int retval;
	u8 reply;

1395
	dprintk(verbose, DST_INFO, 1, "type_flags 0x%x ", state->type_flags);
L
Linus Torvalds 已提交
1396 1397 1398 1399 1400 1401 1402
	state->decode_freq = 0;
	state->decode_lock = state->decode_strength = state->decode_snr = 0;
	if (state->dst_type == DST_TYPE_IS_SAT) {
		if (!(state->diseq_flags & HAS_POWER))
			dst_set_voltage(fe, SEC_VOLTAGE_13);
	}
	state->diseq_flags &= ~(HAS_LOCK | ATTEMPT_TUNE);
1403
	mutex_lock(&state->dst_mutex);
1404
	if ((dst_comm_init(state)) < 0) {
1405
		dprintk(verbose, DST_DEBUG, 1, "DST Communication initialization failed.");
1406
		goto error;
1407
	}
1408
//	if (state->type_flags & DST_TYPE_HAS_NEWTUNE) {
M
Manu Abraham 已提交
1409
	if ((state->type_flags & DST_TYPE_HAS_VLF) &&
1410 1411
		(!(state->dst_type == DST_TYPE_IS_ATSC))) {

L
Linus Torvalds 已提交
1412 1413 1414 1415
		state->tx_tuna[9] = dst_check_sum(&state->tx_tuna[0], 9);
		retval = write_dst(state, &state->tx_tuna[0], 10);
	} else {
		state->tx_tuna[9] = dst_check_sum(&state->tx_tuna[2], 7);
1416
		retval = write_dst(state, &state->tx_tuna[2], FIXED_COMM);
L
Linus Torvalds 已提交
1417 1418
	}
	if (retval < 0) {
1419
		dst_pio_disable(state);
1420
		dprintk(verbose, DST_DEBUG, 1, "write not successful");
1421
		goto werr;
L
Linus Torvalds 已提交
1422
	}
1423
	if ((dst_pio_disable(state)) < 0) {
1424
		dprintk(verbose, DST_DEBUG, 1, "DST PIO disable failed !");
1425
		goto error;
1426 1427
	}
	if ((read_dst(state, &reply, GET_ACK) < 0)) {
1428
		dprintk(verbose, DST_DEBUG, 1, "read verify not successful.");
1429
		goto error;
L
Linus Torvalds 已提交
1430
	}
1431
	if (reply != ACK) {
1432
		dprintk(verbose, DST_DEBUG, 1, "write not acknowledged 0x%02x ", reply);
1433
		goto error;
L
Linus Torvalds 已提交
1434 1435
	}
	state->diseq_flags |= ATTEMPT_TUNE;
1436 1437
	retval = dst_get_tuna(state);
werr:
1438
	mutex_unlock(&state->dst_mutex);
1439
	return retval;
1440

1441
error:
1442
	mutex_unlock(&state->dst_mutex);
1443
	return -EIO;
L
Linus Torvalds 已提交
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
}

/*
 * line22k0    0x00, 0x09, 0x00, 0xff, 0x01, 0x00, 0x00, 0x00
 * line22k1    0x00, 0x09, 0x01, 0xff, 0x01, 0x00, 0x00, 0x00
 * line22k2    0x00, 0x09, 0x02, 0xff, 0x01, 0x00, 0x00, 0x00
 * tone        0x00, 0x09, 0xff, 0x00, 0x01, 0x00, 0x00, 0x00
 * data        0x00, 0x09, 0xff, 0x01, 0x01, 0x00, 0x00, 0x00
 * power_off   0x00, 0x09, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00
 * power_on    0x00, 0x09, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00
 * Diseqc 1    0x00, 0x08, 0x04, 0xe0, 0x10, 0x38, 0xf0, 0xec
 * Diseqc 2    0x00, 0x08, 0x04, 0xe0, 0x10, 0x38, 0xf4, 0xe8
 * Diseqc 3    0x00, 0x08, 0x04, 0xe0, 0x10, 0x38, 0xf8, 0xe4
 * Diseqc 4    0x00, 0x08, 0x04, 0xe0, 0x10, 0x38, 0xfc, 0xe0
 */

1460
static int dst_set_diseqc(struct dvb_frontend *fe, struct dvb_diseqc_master_cmd *cmd)
L
Linus Torvalds 已提交
1461
{
1462
	struct dst_state *state = fe->demodulator_priv;
L
Linus Torvalds 已提交
1463 1464
	u8 paket[8] = { 0x00, 0x08, 0x04, 0xe0, 0x10, 0x38, 0xf0, 0xec };

M
Manu Abraham 已提交
1465
	if (state->dst_type != DST_TYPE_IS_SAT)
1466
		return -EOPNOTSUPP;
1467 1468 1469 1470 1471
	if (cmd->msg_len > 0 && cmd->msg_len < 5)
		memcpy(&paket[3], cmd->msg, cmd->msg_len);
	else if (cmd->msg_len == 5 && state->dst_hw_cap & DST_TYPE_HAS_DISEQC5)
		memcpy(&paket[2], cmd->msg, cmd->msg_len);
	else
L
Linus Torvalds 已提交
1472 1473
		return -EINVAL;
	paket[7] = dst_check_sum(&paket[0], 7);
1474
	return dst_command(state, paket, 8);
L
Linus Torvalds 已提交
1475 1476
}

1477
static int dst_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
L
Linus Torvalds 已提交
1478
{
1479
	int need_cmd, retval = 0;
1480
	struct dst_state *state = fe->demodulator_priv;
L
Linus Torvalds 已提交
1481 1482

	state->voltage = voltage;
M
Manu Abraham 已提交
1483
	if (state->dst_type != DST_TYPE_IS_SAT)
1484
		return -EOPNOTSUPP;
L
Linus Torvalds 已提交
1485 1486

	need_cmd = 0;
1487

1488 1489 1490 1491
	switch (voltage) {
	case SEC_VOLTAGE_13:
	case SEC_VOLTAGE_18:
		if ((state->diseq_flags & HAS_POWER) == 0)
L
Linus Torvalds 已提交
1492
			need_cmd = 1;
1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
		state->diseq_flags |= HAS_POWER;
		state->tx_tuna[4] = 0x01;
		break;
	case SEC_VOLTAGE_OFF:
		need_cmd = 1;
		state->diseq_flags &= ~(HAS_POWER | HAS_LOCK | ATTEMPT_TUNE);
		state->tx_tuna[4] = 0x00;
		break;
	default:
		return -EINVAL;
L
Linus Torvalds 已提交
1503
	}
1504

1505
	if (need_cmd)
1506
		retval = dst_tone_power_cmd(state);
1507

1508
	return retval;
L
Linus Torvalds 已提交
1509 1510
}

1511
static int dst_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t tone)
L
Linus Torvalds 已提交
1512
{
1513
	struct dst_state *state = fe->demodulator_priv;
L
Linus Torvalds 已提交
1514 1515

	state->tone = tone;
M
Manu Abraham 已提交
1516
	if (state->dst_type != DST_TYPE_IS_SAT)
1517
		return -EOPNOTSUPP;
L
Linus Torvalds 已提交
1518 1519

	switch (tone) {
1520 1521 1522 1523 1524 1525
	case SEC_TONE_OFF:
		if (state->type_flags & DST_TYPE_HAS_OBS_REGS)
		    state->tx_tuna[2] = 0x00;
		else
		    state->tx_tuna[2] = 0xff;
		break;
1526

1527 1528 1529 1530 1531
	case SEC_TONE_ON:
		state->tx_tuna[2] = 0x02;
		break;
	default:
		return -EINVAL;
L
Linus Torvalds 已提交
1532
	}
1533
	return dst_tone_power_cmd(state);
L
Linus Torvalds 已提交
1534 1535
}

M
Manu Abraham 已提交
1536 1537 1538 1539
static int dst_send_burst(struct dvb_frontend *fe, fe_sec_mini_cmd_t minicmd)
{
	struct dst_state *state = fe->demodulator_priv;

M
Manu Abraham 已提交
1540
	if (state->dst_type != DST_TYPE_IS_SAT)
1541
		return -EOPNOTSUPP;
M
Manu Abraham 已提交
1542 1543
	state->minicmd = minicmd;
	switch (minicmd) {
1544 1545 1546 1547 1548 1549
	case SEC_MINI_A:
		state->tx_tuna[3] = 0x02;
		break;
	case SEC_MINI_B:
		state->tx_tuna[3] = 0xff;
		break;
M
Manu Abraham 已提交
1550
	}
1551
	return dst_tone_power_cmd(state);
M
Manu Abraham 已提交
1552 1553 1554
}


1555
static int dst_init(struct dvb_frontend *fe)
L
Linus Torvalds 已提交
1556
{
1557 1558 1559 1560 1561 1562
	struct dst_state *state = fe->demodulator_priv;

	static u8 sat_tuna_188[] = { 0x09, 0x00, 0x03, 0xb6, 0x01, 0x00, 0x73, 0x21, 0x00, 0x00 };
	static u8 sat_tuna_204[] = { 0x00, 0x00, 0x03, 0xb6, 0x01, 0x55, 0xbd, 0x50, 0x00, 0x00 };
	static u8 ter_tuna_188[] = { 0x09, 0x00, 0x03, 0xb6, 0x01, 0x07, 0x00, 0x00, 0x00, 0x00 };
	static u8 ter_tuna_204[] = { 0x00, 0x00, 0x03, 0xb6, 0x01, 0x07, 0x00, 0x00, 0x00, 0x00 };
M
Manu Abraham 已提交
1563 1564
	static u8 cab_tuna_188[] = { 0x09, 0x00, 0x03, 0xb6, 0x01, 0x07, 0x00, 0x00, 0x00, 0x00 };
	static u8 cab_tuna_204[] = { 0x00, 0x00, 0x03, 0xb6, 0x01, 0x07, 0x00, 0x00, 0x00, 0x00 };
1565
	static u8 atsc_tuner[] = { 0x00, 0x00, 0x03, 0xb6, 0x01, 0x07, 0x00, 0x00, 0x00, 0x00 };
1566

1567
	state->inversion = INVERSION_OFF;
L
Linus Torvalds 已提交
1568 1569 1570 1571 1572 1573
	state->voltage = SEC_VOLTAGE_13;
	state->tone = SEC_TONE_OFF;
	state->diseq_flags = 0;
	state->k22 = 0x02;
	state->bandwidth = BANDWIDTH_7_MHZ;
	state->cur_jiff = jiffies;
1574
	if (state->dst_type == DST_TYPE_IS_SAT)
M
Manu Abraham 已提交
1575
		memcpy(state->tx_tuna, ((state->type_flags & DST_TYPE_HAS_VLF) ? sat_tuna_188 : sat_tuna_204), sizeof (sat_tuna_204));
1576
	else if (state->dst_type == DST_TYPE_IS_TERR)
M
Manu Abraham 已提交
1577
		memcpy(state->tx_tuna, ((state->type_flags & DST_TYPE_HAS_VLF) ? ter_tuna_188 : ter_tuna_204), sizeof (ter_tuna_204));
1578
	else if (state->dst_type == DST_TYPE_IS_CABLE)
M
Manu Abraham 已提交
1579
		memcpy(state->tx_tuna, ((state->type_flags & DST_TYPE_HAS_VLF) ? cab_tuna_188 : cab_tuna_204), sizeof (cab_tuna_204));
1580
	else if (state->dst_type == DST_TYPE_IS_ATSC)
1581
		memcpy(state->tx_tuna, atsc_tuner, sizeof (atsc_tuner));
L
Linus Torvalds 已提交
1582 1583 1584 1585

	return 0;
}

1586
static int dst_read_status(struct dvb_frontend *fe, fe_status_t *status)
L
Linus Torvalds 已提交
1587
{
1588
	struct dst_state *state = fe->demodulator_priv;
L
Linus Torvalds 已提交
1589 1590 1591

	*status = 0;
	if (state->diseq_flags & HAS_LOCK) {
1592
//		dst_get_signal(state);	// don't require(?) to ask MCU
L
Linus Torvalds 已提交
1593 1594 1595 1596 1597 1598 1599
		if (state->decode_lock)
			*status |= FE_HAS_LOCK | FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_SYNC | FE_HAS_VITERBI;
	}

	return 0;
}

1600
static int dst_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
L
Linus Torvalds 已提交
1601
{
1602
	struct dst_state *state = fe->demodulator_priv;
L
Linus Torvalds 已提交
1603

1604
	int retval = dst_get_signal(state);
L
Linus Torvalds 已提交
1605 1606
	*strength = state->decode_strength;

1607
	return retval;
L
Linus Torvalds 已提交
1608 1609
}

1610
static int dst_read_snr(struct dvb_frontend *fe, u16 *snr)
L
Linus Torvalds 已提交
1611
{
1612
	struct dst_state *state = fe->demodulator_priv;
L
Linus Torvalds 已提交
1613

1614
	int retval = dst_get_signal(state);
L
Linus Torvalds 已提交
1615 1616
	*snr = state->decode_snr;

1617
	return retval;
L
Linus Torvalds 已提交
1618 1619
}

1620 1621
static int dst_set_frontend(struct dvb_frontend *fe, struct dvb_frontend_parameters *p)
{
1622
	int retval = -EINVAL;
1623 1624 1625
	struct dst_state *state = fe->demodulator_priv;

	if (p != NULL) {
1626 1627 1628
		retval = dst_set_freq(state, p->frequency);
		if(retval != 0)
			return retval;
1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645
		dprintk(verbose, DST_DEBUG, 1, "Set Frequency=[%d]", p->frequency);

		if (state->dst_type == DST_TYPE_IS_SAT) {
			if (state->type_flags & DST_TYPE_HAS_OBS_REGS)
				dst_set_inversion(state, p->inversion);
			dst_set_fec(state, p->u.qpsk.fec_inner);
			dst_set_symbolrate(state, p->u.qpsk.symbol_rate);
			dst_set_polarization(state);
			dprintk(verbose, DST_DEBUG, 1, "Set Symbolrate=[%d]", p->u.qpsk.symbol_rate);

		} else if (state->dst_type == DST_TYPE_IS_TERR)
			dst_set_bandwidth(state, p->u.ofdm.bandwidth);
		else if (state->dst_type == DST_TYPE_IS_CABLE) {
			dst_set_fec(state, p->u.qam.fec_inner);
			dst_set_symbolrate(state, p->u.qam.symbol_rate);
			dst_set_modulation(state, p->u.qam.modulation);
		}
1646
		retval = dst_write_tuna(fe);
1647 1648
	}

1649
	return retval;
1650 1651 1652
}

static int dst_tune_frontend(struct dvb_frontend* fe,
1653 1654 1655 1656
			    struct dvb_frontend_parameters* p,
			    unsigned int mode_flags,
			    int *delay,
			    fe_status_t *status)
L
Linus Torvalds 已提交
1657
{
1658
	struct dst_state *state = fe->demodulator_priv;
L
Linus Torvalds 已提交
1659

1660 1661 1662
	if (p != NULL) {
		dst_set_freq(state, p->frequency);
		dprintk(verbose, DST_DEBUG, 1, "Set Frequency=[%d]", p->frequency);
1663

1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679
		if (state->dst_type == DST_TYPE_IS_SAT) {
			if (state->type_flags & DST_TYPE_HAS_OBS_REGS)
				dst_set_inversion(state, p->inversion);
			dst_set_fec(state, p->u.qpsk.fec_inner);
			dst_set_symbolrate(state, p->u.qpsk.symbol_rate);
			dst_set_polarization(state);
			dprintk(verbose, DST_DEBUG, 1, "Set Symbolrate=[%d]", p->u.qpsk.symbol_rate);

		} else if (state->dst_type == DST_TYPE_IS_TERR)
			dst_set_bandwidth(state, p->u.ofdm.bandwidth);
		else if (state->dst_type == DST_TYPE_IS_CABLE) {
			dst_set_fec(state, p->u.qam.fec_inner);
			dst_set_symbolrate(state, p->u.qam.symbol_rate);
			dst_set_modulation(state, p->u.qam.modulation);
		}
		dst_write_tuna(fe);
L
Linus Torvalds 已提交
1680 1681
	}

1682 1683 1684 1685
	if (!(mode_flags & FE_TUNE_MODE_ONESHOT))
		dst_read_status(fe, status);

	*delay = HZ/10;
L
Linus Torvalds 已提交
1686 1687 1688
	return 0;
}

1689 1690 1691 1692 1693
static int dst_get_tuning_algo(struct dvb_frontend *fe)
{
	return dst_algo;
}

1694
static int dst_get_frontend(struct dvb_frontend *fe, struct dvb_frontend_parameters *p)
L
Linus Torvalds 已提交
1695
{
1696
	struct dst_state *state = fe->demodulator_priv;
L
Linus Torvalds 已提交
1697 1698 1699

	p->frequency = state->decode_freq;
	if (state->dst_type == DST_TYPE_IS_SAT) {
1700 1701
		if (state->type_flags & DST_TYPE_HAS_OBS_REGS)
			p->inversion = state->inversion;
L
Linus Torvalds 已提交
1702 1703 1704 1705 1706 1707 1708
		p->u.qpsk.symbol_rate = state->symbol_rate;
		p->u.qpsk.fec_inner = dst_get_fec(state);
	} else if (state->dst_type == DST_TYPE_IS_TERR) {
		p->u.ofdm.bandwidth = state->bandwidth;
	} else if (state->dst_type == DST_TYPE_IS_CABLE) {
		p->u.qam.symbol_rate = state->symbol_rate;
		p->u.qam.fec_inner = dst_get_fec(state);
1709
		p->u.qam.modulation = dst_get_modulation(state);
L
Linus Torvalds 已提交
1710 1711 1712 1713 1714
	}

	return 0;
}

1715
static void dst_release(struct dvb_frontend *fe)
L
Linus Torvalds 已提交
1716
{
1717
	struct dst_state *state = fe->demodulator_priv;
L
Linus Torvalds 已提交
1718 1719 1720 1721 1722 1723
	kfree(state);
}

static struct dvb_frontend_ops dst_dvbt_ops;
static struct dvb_frontend_ops dst_dvbs_ops;
static struct dvb_frontend_ops dst_dvbc_ops;
1724
static struct dvb_frontend_ops dst_atsc_ops;
L
Linus Torvalds 已提交
1725

1726
struct dst_state *dst_attach(struct dst_state *state, struct dvb_adapter *dvb_adapter)
L
Linus Torvalds 已提交
1727
{
1728 1729
	/* check if the ASIC is there */
	if (dst_probe(state) < 0) {
J
Jesper Juhl 已提交
1730
		kfree(state);
1731 1732
		return NULL;
	}
L
Linus Torvalds 已提交
1733
	/* determine settings based on type */
1734
	/* create dvb_frontend */
L
Linus Torvalds 已提交
1735 1736
	switch (state->dst_type) {
	case DST_TYPE_IS_TERR:
1737
		memcpy(&state->frontend.ops, &dst_dvbt_ops, sizeof(struct dvb_frontend_ops));
L
Linus Torvalds 已提交
1738 1739
		break;
	case DST_TYPE_IS_CABLE:
1740
		memcpy(&state->frontend.ops, &dst_dvbc_ops, sizeof(struct dvb_frontend_ops));
L
Linus Torvalds 已提交
1741 1742
		break;
	case DST_TYPE_IS_SAT:
1743
		memcpy(&state->frontend.ops, &dst_dvbs_ops, sizeof(struct dvb_frontend_ops));
L
Linus Torvalds 已提交
1744
		break;
1745 1746 1747
	case DST_TYPE_IS_ATSC:
		memcpy(&state->frontend.ops, &dst_atsc_ops, sizeof(struct dvb_frontend_ops));
		break;
L
Linus Torvalds 已提交
1748
	default:
1749
		dprintk(verbose, DST_ERROR, 1, "unknown DST type. please report to the LinuxTV.org DVB mailinglist.");
J
Jesper Juhl 已提交
1750
		kfree(state);
1751
		return NULL;
L
Linus Torvalds 已提交
1752 1753 1754
	}
	state->frontend.demodulator_priv = state;

1755 1756 1757 1758 1759
	/*	Attach other DST peripherals if any		*/
	/*	Conditional Access device			*/
	if (state->dst_hw_cap & DST_TYPE_HAS_CA)
		dst_ca_attach(state, dvb_adapter);

1760
	return state;				/*	Manu (DST is a card not a frontend)	*/
L
Linus Torvalds 已提交
1761 1762
}

1763 1764
EXPORT_SYMBOL(dst_attach);

L
Linus Torvalds 已提交
1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777
static struct dvb_frontend_ops dst_dvbt_ops = {

	.info = {
		.name = "DST DVB-T",
		.type = FE_OFDM,
		.frequency_min = 137000000,
		.frequency_max = 858000000,
		.frequency_stepsize = 166667,
		.caps = FE_CAN_FEC_AUTO | FE_CAN_QAM_AUTO | FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO
	},

	.release = dst_release,
	.init = dst_init,
1778 1779
	.tune = dst_tune_frontend,
	.set_frontend = dst_set_frontend,
L
Linus Torvalds 已提交
1780
	.get_frontend = dst_get_frontend,
1781
	.get_frontend_algo = dst_get_tuning_algo,
L
Linus Torvalds 已提交
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803
	.read_status = dst_read_status,
	.read_signal_strength = dst_read_signal_strength,
	.read_snr = dst_read_snr,
};

static struct dvb_frontend_ops dst_dvbs_ops = {

	.info = {
		.name = "DST DVB-S",
		.type = FE_QPSK,
		.frequency_min = 950000,
		.frequency_max = 2150000,
		.frequency_stepsize = 1000,	/* kHz for QPSK frontends */
		.frequency_tolerance = 29500,
		.symbol_rate_min = 1000000,
		.symbol_rate_max = 45000000,
	/*     . symbol_rate_tolerance	=	???,*/
		.caps = FE_CAN_FEC_AUTO | FE_CAN_QPSK
	},

	.release = dst_release,
	.init = dst_init,
1804 1805
	.tune = dst_tune_frontend,
	.set_frontend = dst_set_frontend,
L
Linus Torvalds 已提交
1806
	.get_frontend = dst_get_frontend,
1807
	.get_frontend_algo = dst_get_tuning_algo,
L
Linus Torvalds 已提交
1808 1809 1810
	.read_status = dst_read_status,
	.read_signal_strength = dst_read_signal_strength,
	.read_snr = dst_read_snr,
M
Manu Abraham 已提交
1811
	.diseqc_send_burst = dst_send_burst,
L
Linus Torvalds 已提交
1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832
	.diseqc_send_master_cmd = dst_set_diseqc,
	.set_voltage = dst_set_voltage,
	.set_tone = dst_set_tone,
};

static struct dvb_frontend_ops dst_dvbc_ops = {

	.info = {
		.name = "DST DVB-C",
		.type = FE_QAM,
		.frequency_stepsize = 62500,
		.frequency_min = 51000000,
		.frequency_max = 858000000,
		.symbol_rate_min = 1000000,
		.symbol_rate_max = 45000000,
	/*     . symbol_rate_tolerance	=	???,*/
		.caps = FE_CAN_FEC_AUTO | FE_CAN_QAM_AUTO
	},

	.release = dst_release,
	.init = dst_init,
1833 1834
	.tune = dst_tune_frontend,
	.set_frontend = dst_set_frontend,
L
Linus Torvalds 已提交
1835
	.get_frontend = dst_get_frontend,
1836
	.get_frontend_algo = dst_get_tuning_algo,
L
Linus Torvalds 已提交
1837 1838 1839 1840 1841
	.read_status = dst_read_status,
	.read_signal_strength = dst_read_signal_strength,
	.read_snr = dst_read_snr,
};

1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855
static struct dvb_frontend_ops dst_atsc_ops = {
	.info = {
		.name = "DST ATSC",
		.type = FE_ATSC,
		.frequency_stepsize = 62500,
		.frequency_min = 510000000,
		.frequency_max = 858000000,
		.symbol_rate_min = 1000000,
		.symbol_rate_max = 45000000,
		.caps = FE_CAN_FEC_AUTO | FE_CAN_QAM_AUTO | FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
	},

	.release = dst_release,
	.init = dst_init,
1856 1857
	.tune = dst_tune_frontend,
	.set_frontend = dst_set_frontend,
1858
	.get_frontend = dst_get_frontend,
1859
	.get_frontend_algo = dst_get_tuning_algo,
1860 1861 1862 1863 1864 1865
	.read_status = dst_read_status,
	.read_signal_strength = dst_read_signal_strength,
	.read_snr = dst_read_snr,
};

MODULE_DESCRIPTION("DST DVB-S/T/C/ATSC Combo Frontend driver");
1866
MODULE_AUTHOR("Jamie Honan, Manu Abraham");
L
Linus Torvalds 已提交
1867
MODULE_LICENSE("GPL");