dst.c 32.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*

3 4 5
	Frontend/Card driver for TwinHan DST Frontend
	Copyright (C) 2003 Jamie Honan
	Copyright (C) 2004, 2005 Manu Abraham (manu@kromtek.com)
L
Linus Torvalds 已提交
6

7 8 9 10
	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 已提交
11

12 13 14 15
	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 已提交
16

17 18 19
	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 已提交
20 21
*/

22

L
Linus Torvalds 已提交
23 24 25 26 27 28 29 30 31 32 33
#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"
34 35 36 37 38 39 40 41 42 43 44 45 46
#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 debug = 1;
module_param(debug, int, 0644);
MODULE_PARM_DESC(debug, "debug messages, default is 0 (yes)");

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

49
#define dprintk	if (debug) printk
L
Linus Torvalds 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62

#define HAS_LOCK	1
#define ATTEMPT_TUNE	2
#define HAS_POWER	4

static void dst_packsize(struct dst_state* state, int psize)
{
	union dst_gpio_packet bits;

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

63
int dst_gpio_outb(struct dst_state* state, u32 mask, u32 enbb, u32 outhigh, int delay)
L
Linus Torvalds 已提交
64 65 66 67 68 69 70
{
	union dst_gpio_packet enb;
	union dst_gpio_packet bits;
	int err;

	enb.enb.mask = mask;
	enb.enb.enable = enbb;
71 72 73
	if (verbose > 4)
		dprintk("%s: mask=[%04x], enbb=[%04x], outhigh=[%04x]\n", __FUNCTION__, mask, enbb, outhigh);

L
Linus Torvalds 已提交
74
	if ((err = bt878_device_control(state->bt, DST_IG_ENABLE, &enb)) < 0) {
75
		dprintk("%s: dst_gpio_enb error (err == %i, mask == %02x, enb == %02x)\n", __FUNCTION__, err, mask, enbb);
L
Linus Torvalds 已提交
76 77
		return -EREMOTEIO;
	}
78
	udelay(1000);
L
Linus Torvalds 已提交
79 80 81 82
	/* because complete disabling means no output, no need to do output packet */
	if (enbb == 0)
		return 0;

83 84 85
	if (delay)
		msleep(10);

L
Linus Torvalds 已提交
86 87 88 89
	bits.outp.mask = enbb;
	bits.outp.highvals = outhigh;

	if ((err = bt878_device_control(state->bt, DST_IG_WRITE, &bits)) < 0) {
90
		dprintk("%s: dst_gpio_outb error (err == %i, enbb == %02x, outhigh == %02x)\n", __FUNCTION__, err, enbb, outhigh);
L
Linus Torvalds 已提交
91 92 93 94
		return -EREMOTEIO;
	}
	return 0;
}
95
EXPORT_SYMBOL(dst_gpio_outb);
L
Linus Torvalds 已提交
96

97
int dst_gpio_inb(struct dst_state *state, u8 * result)
L
Linus Torvalds 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111
{
	union dst_gpio_packet rd_packet;
	int err;

	*result = 0;

	if ((err = bt878_device_control(state->bt, DST_IG_READ, &rd_packet)) < 0) {
		dprintk("%s: dst_gpio_inb error (err == %i)\n", __FUNCTION__, err);
		return -EREMOTEIO;
	}

	*result = (u8) rd_packet.rd.value;
	return 0;
}
112
EXPORT_SYMBOL(dst_gpio_inb);
L
Linus Torvalds 已提交
113

114
int rdc_reset_state(struct dst_state *state)
L
Linus Torvalds 已提交
115
{
116 117 118 119 120 121 122 123
	if (verbose > 1)
		dprintk("%s: Resetting state machine\n", __FUNCTION__);

	if (dst_gpio_outb(state, RDC_8820_INT, RDC_8820_INT, 0, NO_DELAY) < 0) {
		dprintk("%s: dst_gpio_outb ERROR !\n", __FUNCTION__);
		return -1;
	}

L
Linus Torvalds 已提交
124
	msleep(10);
125 126 127 128 129 130 131

	if (dst_gpio_outb(state, RDC_8820_INT, RDC_8820_INT, RDC_8820_INT, NO_DELAY) < 0) {
		dprintk("%s: dst_gpio_outb ERROR !\n", __FUNCTION__);
		msleep(10);
		return -1;
	}

L
Linus Torvalds 已提交
132 133
	return 0;
}
134
EXPORT_SYMBOL(rdc_reset_state);
L
Linus Torvalds 已提交
135

136
int rdc_8820_reset(struct dst_state *state)
L
Linus Torvalds 已提交
137
{
138 139 140 141 142 143 144
	if (verbose > 1)
		dprintk("%s: Resetting DST\n", __FUNCTION__);

	if (dst_gpio_outb(state, RDC_8820_RESET, RDC_8820_RESET, 0, NO_DELAY) < 0) {
		dprintk("%s: dst_gpio_outb ERROR !\n", __FUNCTION__);
		return -1;
	}
145
	udelay(1000);
146 147 148 149 150
	if (dst_gpio_outb(state, RDC_8820_RESET, RDC_8820_RESET, RDC_8820_RESET, DELAY) < 0) {
		dprintk("%s: dst_gpio_outb ERROR !\n", __FUNCTION__);
		return -1;
	}

L
Linus Torvalds 已提交
151 152
	return 0;
}
153
EXPORT_SYMBOL(rdc_8820_reset);
L
Linus Torvalds 已提交
154

155
int dst_pio_enable(struct dst_state *state)
L
Linus Torvalds 已提交
156
{
157 158 159 160
	if (dst_gpio_outb(state, ~0, RDC_8820_PIO_0_ENABLE, 0, NO_DELAY) < 0) {
		dprintk("%s: dst_gpio_outb ERROR !\n", __FUNCTION__);
		return -1;
	}
161
	udelay(1000);
162 163 164 165 166 167 168 169 170 171
	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) {
		dprintk("%s: dst_gpio_outb ERROR !\n", __FUNCTION__);
		return -1;
	}
172 173
	if (state->type_flags & DST_TYPE_HAS_FW_1)
		udelay(1000);
174

L
Linus Torvalds 已提交
175 176
	return 0;
}
177
EXPORT_SYMBOL(dst_pio_disable);
L
Linus Torvalds 已提交
178

179
int dst_wait_dst_ready(struct dst_state *state, u8 delay_mode)
L
Linus Torvalds 已提交
180 181 182
{
	u8 reply;
	int i;
183

L
Linus Torvalds 已提交
184
	for (i = 0; i < 200; i++) {
185 186 187 188 189 190 191 192
		if (dst_gpio_inb(state, &reply) < 0) {
			dprintk("%s: dst_gpio_inb ERROR !\n", __FUNCTION__);
			return -1;
		}

		if ((reply & RDC_8820_PIO_0_ENABLE) == 0) {
			if (verbose > 4)
				dprintk("%s: dst wait ready after %d\n", __FUNCTION__, i);
L
Linus Torvalds 已提交
193 194
			return 1;
		}
195
		msleep(10);
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
	}
	if (verbose > 1)
		dprintk("%s: dst wait NOT ready after %d\n", __FUNCTION__, i);

	return 0;
}
EXPORT_SYMBOL(dst_wait_dst_ready);

int dst_error_recovery(struct dst_state *state)
{
	dprintk("%s: Trying to return from previous errors...\n", __FUNCTION__);
	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)
{
	dprintk("%s: Trying to bailout from previous error...\n", __FUNCTION__);
	rdc_8820_reset(state);
	dst_pio_disable(state);
	msleep(10);

	return 0;
}
EXPORT_SYMBOL(dst_error_bailout);


int dst_comm_init(struct dst_state* state)
{
	if (verbose > 1)
		dprintk ("%s: Initializing DST..\n", __FUNCTION__);
	if ((dst_pio_enable(state)) < 0) {
		dprintk("%s: PIO Enable Failed.\n", __FUNCTION__);
		return -1;
	}
	if ((rdc_reset_state(state)) < 0) {
		dprintk("%s: RDC 8820 State RESET Failed.\n", __FUNCTION__);
		return -1;
L
Linus Torvalds 已提交
239
	}
240 241 242 243 244
	if (state->type_flags & DST_TYPE_HAS_FW_1)
		msleep(100);
	else
		msleep(5);

L
Linus Torvalds 已提交
245 246
	return 0;
}
247
EXPORT_SYMBOL(dst_comm_init);
L
Linus Torvalds 已提交
248

249 250

int write_dst(struct dst_state *state, u8 *data, u8 len)
L
Linus Torvalds 已提交
251 252 253 254
{
	struct i2c_msg msg = {
		.addr = state->config->demod_address,.flags = 0,.buf = data,.len = len
	};
255

L
Linus Torvalds 已提交
256 257
	int err;
	int cnt;
258
	if (debug && (verbose > 4)) {
L
Linus Torvalds 已提交
259
		u8 i;
260 261 262 263 264
		if (verbose > 4) {
			dprintk("%s writing", __FUNCTION__);
			for (i = 0; i < len; i++)
				dprintk(" %02x", data[i]);
			dprintk("\n");
L
Linus Torvalds 已提交
265 266
		}
	}
267
	for (cnt = 0; cnt < 2; cnt++) {
L
Linus Torvalds 已提交
268
		if ((err = i2c_transfer(state->i2c, &msg, 1)) < 0) {
269 270
			dprintk("%s: _write_dst error (err == %i, len == 0x%02x, b0 == 0x%02x)\n", __FUNCTION__, err, len, data[0]);
			dst_error_recovery(state);
L
Linus Torvalds 已提交
271 272 273 274
			continue;
		} else
			break;
	}
275 276 277 278 279 280 281 282 283

	if (cnt >= 2) {
		if (verbose > 1)
			printk("%s: RDC 8820 RESET...\n", __FUNCTION__);
		dst_error_bailout(state);

		return -1;
	}

L
Linus Torvalds 已提交
284 285
	return 0;
}
286
EXPORT_SYMBOL(write_dst);
L
Linus Torvalds 已提交
287

288
int read_dst(struct dst_state *state, u8 * ret, u8 len)
L
Linus Torvalds 已提交
289 290 291 292 293
{
	struct i2c_msg msg = {.addr = state->config->demod_address,.flags = I2C_M_RD,.buf = ret,.len = len };
	int err;
	int cnt;

294
	for (cnt = 0; cnt < 2; cnt++) {
L
Linus Torvalds 已提交
295
		if ((err = i2c_transfer(state->i2c, &msg, 1)) < 0) {
296

L
Linus Torvalds 已提交
297
			dprintk("%s: read_dst error (err == %i, len == 0x%02x, b0 == 0x%02x)\n", __FUNCTION__, err, len, ret[0]);
298 299
			dst_error_recovery(state);

L
Linus Torvalds 已提交
300 301 302 303
			continue;
		} else
			break;
	}
304 305 306 307 308 309 310 311 312
	if (cnt >= 2) {
		if (verbose > 1)
			printk("%s: RDC 8820 RESET...\n", __FUNCTION__);
		dst_error_bailout(state);

		return -1;
	}
	if (debug && (verbose > 4)) {
		dprintk("%s reply is 0x%x\n", __FUNCTION__, ret[0]);
L
Linus Torvalds 已提交
313 314 315 316 317
		for (err = 1; err < len; err++)
			dprintk(" 0x%x", ret[err]);
		if (err > 1)
			dprintk("\n");
	}
318

L
Linus Torvalds 已提交
319 320
	return 0;
}
321
EXPORT_SYMBOL(read_dst);
L
Linus Torvalds 已提交
322 323 324 325 326 327

static int dst_set_freq(struct dst_state *state, u32 freq)
{
	u8 *val;

	state->frequency = freq;
328 329
	if (debug > 4)
		dprintk("%s: set Frequency %u\n", __FUNCTION__, freq);
L
Linus Torvalds 已提交
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 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 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452

	if (state->dst_type == DST_TYPE_IS_SAT) {
		freq = freq / 1000;
		if (freq < 950 || freq > 2150)
			return -EINVAL;
		val = &state->tx_tuna[0];
		val[2] = (freq >> 8) & 0x7f;
		val[3] = (u8) freq;
		val[4] = 1;
		val[8] &= ~4;
		if (freq < 1531)
			val[8] |= 4;
	} else if (state->dst_type == DST_TYPE_IS_TERR) {
		freq = freq / 1000;
		if (freq < 137000 || freq > 858000)
			return -EINVAL;
		val = &state->tx_tuna[0];
		val[2] = (freq >> 16) & 0xff;
		val[3] = (freq >> 8) & 0xff;
		val[4] = (u8) freq;
		val[5] = 0;
		switch (state->bandwidth) {
		case BANDWIDTH_6_MHZ:
			val[6] = 6;
			break;

		case BANDWIDTH_7_MHZ:
		case BANDWIDTH_AUTO:
			val[6] = 7;
			break;

		case BANDWIDTH_8_MHZ:
			val[6] = 8;
			break;
		}

		val[7] = 0;
		val[8] = 0;
	} else if (state->dst_type == DST_TYPE_IS_CABLE) {
		/* guess till will get one */
		freq = freq / 1000;
		val = &state->tx_tuna[0];
		val[2] = (freq >> 16) & 0xff;
		val[3] = (freq >> 8) & 0xff;
		val[4] = (u8) freq;
	} else
		return -EINVAL;
	return 0;
}

static int dst_set_bandwidth(struct dst_state* state, fe_bandwidth_t bandwidth)
{
	u8 *val;

	state->bandwidth = bandwidth;

	if (state->dst_type != DST_TYPE_IS_TERR)
		return 0;

	val = &state->tx_tuna[0];
	switch (bandwidth) {
	case BANDWIDTH_6_MHZ:
		val[6] = 6;
		break;

	case BANDWIDTH_7_MHZ:
		val[6] = 7;
		break;

	case BANDWIDTH_8_MHZ:
		val[6] = 8;
		break;

	default:
		return -EINVAL;
	}
	return 0;
}

static int dst_set_inversion(struct dst_state* state, fe_spectral_inversion_t inversion)
{
	u8 *val;

	state->inversion = inversion;

	val = &state->tx_tuna[0];

	val[8] &= ~0x80;

	switch (inversion) {
	case INVERSION_OFF:
		break;
	case INVERSION_ON:
		val[8] |= 0x80;
		break;
	default:
		return -EINVAL;
	}
	return 0;
}

static int dst_set_fec(struct dst_state* state, fe_code_rate_t fec)
{
	state->fec = fec;
	return 0;
}

static fe_code_rate_t dst_get_fec(struct dst_state* state)
{
	return state->fec;
}

static int dst_set_symbolrate(struct dst_state* state, u32 srate)
{
	u8 *val;
	u32 symcalc;
	u64 sval;

	state->symbol_rate = srate;

	if (state->dst_type == DST_TYPE_IS_TERR) {
		return 0;
	}
453 454
	if (debug > 4)
		dprintk("%s: set symrate %u\n", __FUNCTION__, srate);
L
Linus Torvalds 已提交
455 456 457 458 459 460 461 462
	srate /= 1000;
	val = &state->tx_tuna[0];

	if (state->type_flags & DST_TYPE_HAS_SYMDIV) {
		sval = srate;
		sval <<= 20;
		do_div(sval, 88000);
		symcalc = (u32) sval;
463 464 465 466

		if (debug > 4)
			dprintk("%s: set symcalc %u\n", __FUNCTION__, symcalc);

L
Linus Torvalds 已提交
467 468 469 470 471 472 473 474 475 476 477 478 479 480
		val[5] = (u8) (symcalc >> 12);
		val[6] = (u8) (symcalc >> 4);
		val[7] = (u8) (symcalc << 4);
	} else {
		val[5] = (u8) (srate >> 16) & 0x7f;
		val[6] = (u8) (srate >> 8);
		val[7] = (u8) srate;
	}
	val[8] &= ~0x20;
	if (srate > 8000)
		val[8] |= 0x20;
	return 0;
}

481
u8 dst_check_sum(u8 * buf, u32 len)
L
Linus Torvalds 已提交
482 483 484 485 486 487 488 489 490 491
{
	u32 i;
	u8 val = 0;
	if (!len)
		return 0;
	for (i = 0; i < len; i++) {
		val += buf[i];
	}
	return ((~val) + 1);
}
492
EXPORT_SYMBOL(dst_check_sum);
L
Linus Torvalds 已提交
493 494 495 496 497 498 499 500 501 502

static void dst_type_flags_print(u32 type_flags)
{
	printk("DST type flags :");
	if (type_flags & DST_TYPE_HAS_NEWTUNE)
		printk(" 0x%x newtuner", DST_TYPE_HAS_NEWTUNE);
	if (type_flags & DST_TYPE_HAS_TS204)
		printk(" 0x%x ts204", DST_TYPE_HAS_TS204);
	if (type_flags & DST_TYPE_HAS_SYMDIV)
		printk(" 0x%x symdiv", DST_TYPE_HAS_SYMDIV);
503 504 505 506 507 508
	if (type_flags & DST_TYPE_HAS_FW_1)
		printk(" 0x%x firmware version = 1", DST_TYPE_HAS_FW_1);
	if (type_flags & DST_TYPE_HAS_FW_2)
		printk(" 0x%x firmware version = 2", DST_TYPE_HAS_FW_2);
	if (type_flags & DST_TYPE_HAS_FW_3)
		printk(" 0x%x firmware version = 3", DST_TYPE_HAS_FW_3);
509
//	if ((type_flags & DST_TYPE_HAS_FW_BUILD) && new_fw)
510

L
Linus Torvalds 已提交
511 512 513
	printk("\n");
}

514 515

static int dst_type_print (u8 type)
L
Linus Torvalds 已提交
516 517 518 519 520 521
{
	char *otype;
	switch (type) {
	case DST_TYPE_IS_SAT:
		otype = "satellite";
		break;
522

L
Linus Torvalds 已提交
523 524 525
	case DST_TYPE_IS_TERR:
		otype = "terrestrial";
		break;
526

L
Linus Torvalds 已提交
527 528 529
	case DST_TYPE_IS_CABLE:
		otype = "cable";
		break;
530

L
Linus Torvalds 已提交
531 532 533 534 535
	default:
		printk("%s: invalid dst type %d\n", __FUNCTION__, type);
		return -EINVAL;
	}
	printk("DST type : %s\n", otype);
536

L
Linus Torvalds 已提交
537 538 539
	return 0;
}

540 541 542 543
/*
	Known cards list
	Satellite
	-------------------
544
		  200103A
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
	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

*/

struct dst_types dst_tlist[] = {
576 577 578 579 580 581 582 583
	{
		.device_id = "200103A",
		.offset = 0,
		.dst_type =  DST_TYPE_IS_SAT,
		.type_flags = DST_TYPE_HAS_SYMDIV | DST_TYPE_HAS_FW_1,
		.dst_feature = 0
	},	/*	obsolete	*/

584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
	{
		.device_id = "DST-020",
		.offset = 0,
		.dst_type =  DST_TYPE_IS_SAT,
		.type_flags = DST_TYPE_HAS_SYMDIV | DST_TYPE_HAS_FW_1,
		.dst_feature = 0
	},	/*	obsolete	*/

	{
		.device_id = "DST-030",
		.offset =  0,
		.dst_type = DST_TYPE_IS_SAT,
		.type_flags = DST_TYPE_HAS_TS204 | DST_TYPE_HAS_NEWTUNE | DST_TYPE_HAS_FW_1,
		.dst_feature = 0
	},	/*	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
							 | DST_TYPE_HAS_MAC | DST_TYPE_HAS_MOTO
	 },

	{
		.device_id = "DST-MOT",
		.offset =  0,
		.dst_type = DST_TYPE_IS_SAT,
		.type_flags = DST_TYPE_HAS_SYMDIV | DST_TYPE_HAS_FW_1,
		.dst_feature = 0
	},	/*	obsolete	*/

	{
		.device_id = "DST-CI",
		.offset = 1,
		.dst_type = DST_TYPE_IS_SAT,
		.type_flags = DST_TYPE_HAS_TS204 | DST_TYPE_HAS_NEWTUNE | DST_TYPE_HAS_FW_1,
		.dst_feature = DST_TYPE_HAS_CA
623
	},	/*	An OEM board	*/
624 625 626 627 628

	{
		.device_id = "DSTMCI",
		.offset = 1,
		.dst_type = DST_TYPE_IS_SAT,
629
		.type_flags = DST_TYPE_HAS_NEWTUNE | DST_TYPE_HAS_FW_2 | DST_TYPE_HAS_FW_BUILD,
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
		.dst_feature = DST_TYPE_HAS_CA | DST_TYPE_HAS_DISEQC3 | DST_TYPE_HAS_DISEQC4
							| DST_TYPE_HAS_MOTO | DST_TYPE_HAS_MAC
	},

	{
		.device_id = "DSTFCI",
		.offset = 1,
		.dst_type = DST_TYPE_IS_SAT,
		.type_flags = DST_TYPE_HAS_NEWTUNE | DST_TYPE_HAS_FW_1,
		.dst_feature = 0
	},	/* unknown to vendor	*/

	{
		.device_id = "DCT-CI",
		.offset = 1,
		.dst_type = DST_TYPE_IS_CABLE,
646 647
		.type_flags = DST_TYPE_HAS_TS204 | DST_TYPE_HAS_NEWTUNE | DST_TYPE_HAS_FW_1
							| DST_TYPE_HAS_FW_2 | DST_TYPE_HAS_FW_BUILD,
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
		.dst_feature = DST_TYPE_HAS_CA
	},

	{
		.device_id = "DCTNEW",
		.offset = 1,
		.dst_type = DST_TYPE_IS_CABLE,
		.type_flags = DST_TYPE_HAS_NEWTUNE | DST_TYPE_HAS_FW_3,
		.dst_feature = 0
	},

	{
		.device_id = "DTT-CI",
		.offset = 1,
		.dst_type = DST_TYPE_IS_TERR,
663
		.type_flags = DST_TYPE_HAS_TS204 | DST_TYPE_HAS_FW_2 | DST_TYPE_HAS_FW_BUILD,
664 665 666 667 668 669 670 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
		.dst_feature = 0
	},

	{
		.device_id = "DTTDIG",
		.offset = 1,
		.dst_type = DST_TYPE_IS_TERR,
		.type_flags = DST_TYPE_HAS_FW_2,
		.dst_feature = 0
	},

	{
		.device_id = "DTTNXT",
		.offset = 1,
		.dst_type = DST_TYPE_IS_TERR,
		.type_flags = DST_TYPE_HAS_FW_2,
		.dst_feature = DST_TYPE_HAS_ANALOG
	},

	{
		.device_id = "ATSCDI",
		.offset = 1,
		.dst_type = DST_TYPE_IS_ATSC,
		.type_flags = DST_TYPE_HAS_FW_2,
		.dst_feature = 0
	},

	{
		.device_id = "ATSCAD",
		.offset = 1,
		.dst_type = DST_TYPE_IS_ATSC,
		.type_flags = DST_TYPE_HAS_FW_2,
		.dst_feature = 0
	},

	{ }

};


static int dst_get_device_id(struct dst_state *state)
L
Linus Torvalds 已提交
705
{
706 707
	u8 reply;

L
Linus Torvalds 已提交
708
	int i;
709 710 711
	struct dst_types *p_dst_type;
	u8 use_dst_type = 0;
	u32 use_type_flags = 0;
L
Linus Torvalds 已提交
712

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

715 716 717 718 719 720 721 722 723 724 725 726 727 728
	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) {
		dprintk("%s: Write not Acknowledged! [Reply=0x%02x]\n", __FUNCTION__, reply);
		return -1;		/*	Unack'd write		*/
L
Linus Torvalds 已提交
729
	}
730 731 732 733 734 735 736 737 738 739 740 741

	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)) {
		dprintk("%s: Checksum failure! \n", __FUNCTION__);
		return -1;		/*	Checksum failure	*/
L
Linus Torvalds 已提交
742
	}
743 744 745 746 747 748 749 750 751 752 753 754

	state->rxbuffer[7] = '\0';

	for (i = 0, p_dst_type = dst_tlist; i < ARRAY_SIZE (dst_tlist); i++, p_dst_type++) {
		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;
			printk ("%s: Recognise [%s]\n", __FUNCTION__, p_dst_type->device_id);

L
Linus Torvalds 已提交
755 756 757
			break;
		}
	}
758 759 760 761

	if (i >= sizeof (dst_tlist) / sizeof (dst_tlist [0])) {
		printk("%s: Unable to recognize %s or %s\n", __FUNCTION__, &state->rxbuffer[0], &state->rxbuffer[1]);
		printk("%s: please email linux-dvb@linuxtv.org with this type in\n", __FUNCTION__);
L
Linus Torvalds 已提交
762 763 764 765
		use_dst_type = DST_TYPE_IS_SAT;
		use_type_flags = DST_TYPE_HAS_SYMDIV;
	}

766
	dst_type_print(use_dst_type);
L
Linus Torvalds 已提交
767 768 769 770 771 772 773
	state->type_flags = use_type_flags;
	state->dst_type = use_dst_type;
	dst_type_flags_print(state->type_flags);

	if (state->type_flags & DST_TYPE_HAS_TS204) {
		dst_packsize(state, 204);
	}
774

L
Linus Torvalds 已提交
775 776 777
	return 0;
}

778 779 780 781 782 783
static int dst_probe(struct dst_state *state)
{
	if ((rdc_8820_reset(state)) < 0) {
		dprintk("%s: RDC 8820 RESET Failed.\n", __FUNCTION__);
		return -1;
	}
784 785 786 787 788
	if (dst_addons & DST_TYPE_HAS_CA)
		msleep(4000);
	else
		msleep(100);

789 790 791 792
	if ((dst_comm_init(state)) < 0) {
		dprintk("%s: DST Initialization Failed.\n", __FUNCTION__);
		return -1;
	}
793
	msleep(100);
794 795 796 797 798 799 800 801 802
	if (dst_get_device_id(state) < 0) {
		dprintk("%s: unknown device.\n", __FUNCTION__);
		return -1;
	}

	return 0;
}

int dst_command(struct dst_state* state, u8 * data, u8 len)
L
Linus Torvalds 已提交
803 804
{
	u8 reply;
805 806 807 808
	if ((dst_comm_init(state)) < 0) {
		dprintk("%s: DST Communication Initialization Failed.\n", __FUNCTION__);
		return -1;
	}
L
Linus Torvalds 已提交
809

810 811 812 813 814 815 816 817
	if (write_dst(state, data, len)) {
		if (verbose > 1)
			dprintk("%s: Tring to recover.. \n", __FUNCTION__);
		if ((dst_error_recovery(state)) < 0) {
			dprintk("%s: Recovery Failed.\n", __FUNCTION__);
			return -1;
		}
		return -1;
L
Linus Torvalds 已提交
818
	}
819 820 821
	if ((dst_pio_disable(state)) < 0) {
		dprintk("%s: PIO Disable Failed.\n", __FUNCTION__);
		return -1;
L
Linus Torvalds 已提交
822
	}
823 824
	if (state->type_flags & DST_TYPE_HAS_FW_1)
		udelay(3000);
825 826 827 828 829 830 831 832 833 834 835 836 837 838

	if (read_dst(state, &reply, GET_ACK)) {
		if (verbose > 1)
			dprintk("%s: Trying to recover.. \n", __FUNCTION__);
		if ((dst_error_recovery(state)) < 0) {
			dprintk("%s: Recovery Failed.\n", __FUNCTION__);
			return -1;
		}
		return -1;
	}

	if (reply != ACK) {
		dprintk("%s: write not acknowledged 0x%02x \n", __FUNCTION__, reply);
		return -1;
L
Linus Torvalds 已提交
839 840 841
	}
	if (len >= 2 && data[0] == 0 && (data[1] == 1 || data[1] == 3))
		return 0;
842 843 844 845 846 847 848

//	udelay(3000);
	if (state->type_flags & DST_TYPE_HAS_FW_1)
		udelay(3000);
	else
		udelay(2000);

849 850 851 852 853 854 855 856 857 858 859
	if (!dst_wait_dst_ready(state, NO_DELAY))
		return -1;

	if (read_dst(state, state->rxbuffer, FIXED_COMM)) {
		if (verbose > 1)
			dprintk("%s: Trying to recover.. \n", __FUNCTION__);
		if ((dst_error_recovery(state)) < 0) {
			dprintk("%s: Recovery failed.\n", __FUNCTION__);
			return -1;
		}
		return -1;
L
Linus Torvalds 已提交
860
	}
861

L
Linus Torvalds 已提交
862 863
	if (state->rxbuffer[7] != dst_check_sum(state->rxbuffer, 7)) {
		dprintk("%s: checksum failure\n", __FUNCTION__);
864
		return -1;
L
Linus Torvalds 已提交
865
	}
866

L
Linus Torvalds 已提交
867 868
	return 0;
}
869
EXPORT_SYMBOL(dst_command);
L
Linus Torvalds 已提交
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908

static int dst_get_signal(struct dst_state* state)
{
	int retval;
	u8 get_signal[] = { 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfb };

	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;
		}
		state->cur_jiff = jiffies;
	}
	return 0;
}

static int dst_tone_power_cmd(struct dst_state* state)
{
	u8 paket[8] = { 0x00, 0x09, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00 };

	if (state->dst_type == DST_TYPE_IS_TERR)
		return 0;

909
	paket[4] = state->tx_tuna[4];
910

L
Linus Torvalds 已提交
911
	if (state->tone == SEC_TONE_ON)
912
		paket[2] = 0x02;
L
Linus Torvalds 已提交
913 914
	else
		paket[2] = 0;
915

M
Manu Abraham 已提交
916
	paket[3] = state->tx_tuna[3];
917
	paket[7] = dst_check_sum (paket, 7);
L
Linus Torvalds 已提交
918
	dst_command(state, paket, 8);
M
Manu Abraham 已提交
919

L
Linus Torvalds 已提交
920 921 922 923 924 925
	return 0;
}

static int dst_get_tuna(struct dst_state* state)
{
	int retval;
926

L
Linus Torvalds 已提交
927 928
	if ((state->diseq_flags & ATTEMPT_TUNE) == 0)
		return 0;
929

L
Linus Torvalds 已提交
930
	state->diseq_flags &= ~(HAS_LOCK);
931
	if (!dst_wait_dst_ready(state, NO_DELAY))
L
Linus Torvalds 已提交
932
		return 0;
933

L
Linus Torvalds 已提交
934 935 936 937
	if (state->type_flags & DST_TYPE_HAS_NEWTUNE) {
		/* how to get variable length reply ???? */
		retval = read_dst(state, state->rx_tuna, 10);
	} else {
938
		retval = read_dst(state, &state->rx_tuna[2], FIXED_COMM);
L
Linus Torvalds 已提交
939
	}
940

L
Linus Torvalds 已提交
941 942 943 944
	if (retval < 0) {
		dprintk("%s: read not successful\n", __FUNCTION__);
		return 0;
	}
945

L
Linus Torvalds 已提交
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
	if (state->type_flags & DST_TYPE_HAS_NEWTUNE) {
		if (state->rx_tuna[9] != dst_check_sum(&state->rx_tuna[0], 9)) {
			dprintk("%s: checksum failure?\n", __FUNCTION__);
			return 0;
		}
	} else {
		if (state->rx_tuna[9] != dst_check_sum(&state->rx_tuna[2], 7)) {
			dprintk("%s: checksum failure?\n", __FUNCTION__);
			return 0;
		}
	}
	if (state->rx_tuna[2] == 0 && state->rx_tuna[3] == 0)
		return 0;
	state->decode_freq = ((state->rx_tuna[2] & 0x7f) << 8) + state->rx_tuna[3];

	state->decode_lock = 1;
	/*
	   dst->decode_n1 = (dst->rx_tuna[4] << 8) +
	   (dst->rx_tuna[5]);

	   dst->decode_n2 = (dst->rx_tuna[8] << 8) +
	   (dst->rx_tuna[7]);
	 */
	state->diseq_flags |= HAS_LOCK;
	/* dst->cur_jiff = jiffies; */
	return 1;
}

static int dst_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage);

static int dst_write_tuna(struct dvb_frontend* fe)
{
978
	struct dst_state* state = fe->demodulator_priv;
L
Linus Torvalds 已提交
979 980 981
	int retval;
	u8 reply;

982 983 984
	if (debug > 4)
		dprintk("%s: type_flags 0x%x \n", __FUNCTION__, state->type_flags);

L
Linus Torvalds 已提交
985 986 987 988 989 990 991
	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);
992 993 994 995 996 997

	if ((dst_comm_init(state)) < 0) {
		dprintk("%s: DST Communication initialization failed.\n", __FUNCTION__);
		return -1;
	}

L
Linus Torvalds 已提交
998 999 1000
	if (state->type_flags & DST_TYPE_HAS_NEWTUNE) {
		state->tx_tuna[9] = dst_check_sum(&state->tx_tuna[0], 9);
		retval = write_dst(state, &state->tx_tuna[0], 10);
1001

L
Linus Torvalds 已提交
1002 1003
	} else {
		state->tx_tuna[9] = dst_check_sum(&state->tx_tuna[2], 7);
1004
		retval = write_dst(state, &state->tx_tuna[2], FIXED_COMM);
L
Linus Torvalds 已提交
1005 1006
	}
	if (retval < 0) {
1007
		dst_pio_disable(state);
L
Linus Torvalds 已提交
1008 1009 1010
		dprintk("%s: write not successful\n", __FUNCTION__);
		return retval;
	}
1011 1012 1013 1014 1015 1016 1017 1018 1019

	if ((dst_pio_disable(state)) < 0) {
		dprintk("%s: DST PIO disable failed !\n", __FUNCTION__);
		return -1;
	}

	if ((read_dst(state, &reply, GET_ACK) < 0)) {
		dprintk("%s: read verify not successful.\n", __FUNCTION__);
		return -1;
L
Linus Torvalds 已提交
1020
	}
1021 1022
	if (reply != ACK) {
		dprintk("%s: write not acknowledged 0x%02x \n", __FUNCTION__, reply);
L
Linus Torvalds 已提交
1023 1024 1025
		return 0;
	}
	state->diseq_flags |= ATTEMPT_TUNE;
1026

L
Linus Torvalds 已提交
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
	return dst_get_tuna(state);
}

/*
 * 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
 */

static int dst_set_diseqc(struct dvb_frontend* fe, struct dvb_diseqc_master_cmd* cmd)
{
1046
	struct dst_state* state = fe->demodulator_priv;
L
Linus Torvalds 已提交
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
	u8 paket[8] = { 0x00, 0x08, 0x04, 0xe0, 0x10, 0x38, 0xf0, 0xec };

	if (state->dst_type == DST_TYPE_IS_TERR)
		return 0;

	if (cmd->msg_len == 0 || cmd->msg_len > 4)
		return -EINVAL;
	memcpy(&paket[3], cmd->msg, cmd->msg_len);
	paket[7] = dst_check_sum(&paket[0], 7);
	dst_command(state, paket, 8);
	return 0;
}

static int dst_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
{
	int need_cmd;
1063
	struct dst_state* state = fe->demodulator_priv;
L
Linus Torvalds 已提交
1064 1065 1066 1067 1068 1069 1070 1071

	state->voltage = voltage;

	if (state->dst_type == DST_TYPE_IS_TERR)
		return 0;

	need_cmd = 0;
	switch (voltage) {
1072 1073 1074 1075 1076 1077 1078
		case SEC_VOLTAGE_13:
		case SEC_VOLTAGE_18:
			if ((state->diseq_flags & HAS_POWER) == 0)
				need_cmd = 1;
			state->diseq_flags |= HAS_POWER;
			state->tx_tuna[4] = 0x01;
			break;
1079

1080
		case SEC_VOLTAGE_OFF:
L
Linus Torvalds 已提交
1081
			need_cmd = 1;
1082 1083 1084
			state->diseq_flags &= ~(HAS_POWER | HAS_LOCK | ATTEMPT_TUNE);
			state->tx_tuna[4] = 0x00;
			break;
1085

1086 1087
		default:
			return -EINVAL;
L
Linus Torvalds 已提交
1088
	}
1089
	if (need_cmd)
L
Linus Torvalds 已提交
1090
		dst_tone_power_cmd(state);
1091

L
Linus Torvalds 已提交
1092 1093 1094 1095 1096 1097
	return 0;
}

static int dst_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
{
	u8 *val;
1098
	struct dst_state* state = fe->demodulator_priv;
L
Linus Torvalds 已提交
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111

	state->tone = tone;

	if (state->dst_type == DST_TYPE_IS_TERR)
		return 0;

	val = &state->tx_tuna[0];

	val[8] &= ~0x1;

	switch (tone) {
	case SEC_TONE_OFF:
		break;
1112

L
Linus Torvalds 已提交
1113 1114 1115
	case SEC_TONE_ON:
		val[8] |= 1;
		break;
1116

L
Linus Torvalds 已提交
1117 1118 1119 1120
	default:
		return -EINVAL;
	}
	dst_tone_power_cmd(state);
1121

L
Linus Torvalds 已提交
1122 1123 1124
	return 0;
}

M
Manu Abraham 已提交
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
static int dst_send_burst(struct dvb_frontend *fe, fe_sec_mini_cmd_t minicmd)
{
	struct dst_state *state = fe->demodulator_priv;

	if ((state->dst_type == DST_TYPE_IS_TERR) || (state->dst_type == DST_TYPE_IS_CABLE))
		return 0;

	state->minicmd = minicmd;

	switch (minicmd) {
		case SEC_MINI_A:
			state->tx_tuna[3] = 0x02;
			break;
		case SEC_MINI_B:
			state->tx_tuna[3] = 0xff;
			break;
	}
	dst_tone_power_cmd(state);

	return 0;
}


L
Linus Torvalds 已提交
1148 1149
static int dst_init(struct dvb_frontend* fe)
{
1150
	struct dst_state* state = fe->demodulator_priv;
L
Linus Torvalds 已提交
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
	static u8 ini_satci_tuna[] = { 9, 0, 3, 0xb6, 1, 0, 0x73, 0x21, 0, 0 };
	static u8 ini_satfta_tuna[] = { 0, 0, 3, 0xb6, 1, 0x55, 0xbd, 0x50, 0, 0 };
	static u8 ini_tvfta_tuna[] = { 0, 0, 3, 0xb6, 1, 7, 0x0, 0x0, 0, 0 };
	static u8 ini_tvci_tuna[] = { 9, 0, 3, 0xb6, 1, 7, 0x0, 0x0, 0, 0 };
	static u8 ini_cabfta_tuna[] = { 0, 0, 3, 0xb6, 1, 7, 0x0, 0x0, 0, 0 };
	static u8 ini_cabci_tuna[] = { 9, 0, 3, 0xb6, 1, 7, 0x0, 0x0, 0, 0 };
	state->inversion = INVERSION_ON;
	state->voltage = SEC_VOLTAGE_13;
	state->tone = SEC_TONE_OFF;
	state->symbol_rate = 29473000;
	state->fec = FEC_AUTO;
	state->diseq_flags = 0;
	state->k22 = 0x02;
	state->bandwidth = BANDWIDTH_7_MHZ;
	state->cur_jiff = jiffies;
	if (state->dst_type == DST_TYPE_IS_SAT) {
		state->frequency = 950000;
		memcpy(state->tx_tuna, ((state->type_flags & DST_TYPE_HAS_NEWTUNE) ? ini_satci_tuna : ini_satfta_tuna), sizeof(ini_satfta_tuna));
	} else if (state->dst_type == DST_TYPE_IS_TERR) {
		state->frequency = 137000000;
		memcpy(state->tx_tuna, ((state->type_flags & DST_TYPE_HAS_NEWTUNE) ? ini_tvci_tuna : ini_tvfta_tuna), sizeof(ini_tvfta_tuna));
	} else if (state->dst_type == DST_TYPE_IS_CABLE) {
		state->frequency = 51000000;
		memcpy(state->tx_tuna, ((state->type_flags & DST_TYPE_HAS_NEWTUNE) ? ini_cabci_tuna : ini_cabfta_tuna), sizeof(ini_cabfta_tuna));
	}

	return 0;
}

static int dst_read_status(struct dvb_frontend* fe, fe_status_t* status)
{
1182
	struct dst_state* state = fe->demodulator_priv;
L
Linus Torvalds 已提交
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195

	*status = 0;
	if (state->diseq_flags & HAS_LOCK) {
		dst_get_signal(state);
		if (state->decode_lock)
			*status |= FE_HAS_LOCK | FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_SYNC | FE_HAS_VITERBI;
	}

	return 0;
}

static int dst_read_signal_strength(struct dvb_frontend* fe, u16* strength)
{
1196
	struct dst_state* state = fe->demodulator_priv;
L
Linus Torvalds 已提交
1197 1198 1199 1200 1201 1202 1203 1204 1205

	dst_get_signal(state);
	*strength = state->decode_strength;

	return 0;
}

static int dst_read_snr(struct dvb_frontend* fe, u16* snr)
{
1206
	struct dst_state* state = fe->demodulator_priv;
L
Linus Torvalds 已提交
1207 1208 1209 1210 1211 1212 1213 1214 1215

	dst_get_signal(state);
	*snr = state->decode_snr;

	return 0;
}

static int dst_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
{
1216
	struct dst_state* state = fe->demodulator_priv;
L
Linus Torvalds 已提交
1217 1218

	dst_set_freq(state, p->frequency);
1219 1220 1221
	if (verbose > 4)
		dprintk("Set Frequency = [%d]\n", p->frequency);

L
Linus Torvalds 已提交
1222 1223 1224 1225
	dst_set_inversion(state, p->inversion);
	if (state->dst_type == DST_TYPE_IS_SAT) {
		dst_set_fec(state, p->u.qpsk.fec_inner);
		dst_set_symbolrate(state, p->u.qpsk.symbol_rate);
1226 1227 1228
		if (verbose > 4)
			dprintk("Set Symbolrate = [%d]\n", p->u.qpsk.symbol_rate);

L
Linus Torvalds 已提交
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
	} 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_write_tuna(fe);

	return 0;
}

static int dst_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
{
1242
	struct dst_state* state = fe->demodulator_priv;
L
Linus Torvalds 已提交
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261

	p->frequency = state->decode_freq;
	p->inversion = state->inversion;
	if (state->dst_type == DST_TYPE_IS_SAT) {
		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);
		p->u.qam.modulation = QAM_AUTO;
	}

	return 0;
}

static void dst_release(struct dvb_frontend* fe)
{
1262
	struct dst_state* state = fe->demodulator_priv;
L
Linus Torvalds 已提交
1263 1264 1265 1266 1267 1268 1269
	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;

1270
struct dst_state* dst_attach(struct dst_state *state, struct dvb_adapter *dvb_adapter)
L
Linus Torvalds 已提交
1271 1272
{

1273 1274 1275 1276
	/* check if the ASIC is there */
	if (dst_probe(state) < 0) {
		if (state)
			kfree(state);
L
Linus Torvalds 已提交
1277

1278 1279
		return NULL;
	}
L
Linus Torvalds 已提交
1280 1281 1282 1283 1284
	/* determine settings based on type */
	switch (state->dst_type) {
	case DST_TYPE_IS_TERR:
		memcpy(&state->ops, &dst_dvbt_ops, sizeof(struct dvb_frontend_ops));
		break;
1285

L
Linus Torvalds 已提交
1286 1287 1288
	case DST_TYPE_IS_CABLE:
		memcpy(&state->ops, &dst_dvbc_ops, sizeof(struct dvb_frontend_ops));
		break;
1289

L
Linus Torvalds 已提交
1290 1291 1292
	case DST_TYPE_IS_SAT:
		memcpy(&state->ops, &dst_dvbs_ops, sizeof(struct dvb_frontend_ops));
		break;
1293

L
Linus Torvalds 已提交
1294
	default:
1295 1296 1297 1298 1299
		printk("%s: unknown DST type. please report to the LinuxTV.org DVB mailinglist.\n", __FUNCTION__);
		if (state)
			kfree(state);

		return NULL;
L
Linus Torvalds 已提交
1300 1301 1302 1303 1304 1305
	}

	/* create dvb_frontend */
	state->frontend.ops = &state->ops;
	state->frontend.demodulator_priv = state;

1306
	return state;				/*	Manu (DST is a card not a frontend)	*/
L
Linus Torvalds 已提交
1307 1308
}

1309 1310
EXPORT_SYMBOL(dst_attach);

L
Linus Torvalds 已提交
1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
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,

	.set_frontend = dst_set_frontend,
	.get_frontend = dst_get_frontend,

	.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,

	.set_frontend = dst_set_frontend,
	.get_frontend = dst_get_frontend,

	.read_status = dst_read_status,
	.read_signal_strength = dst_read_signal_strength,
	.read_snr = dst_read_snr,

M
Manu Abraham 已提交
1360
	.diseqc_send_burst = dst_send_burst,
L
Linus Torvalds 已提交
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
	.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,

	.set_frontend = dst_set_frontend,
	.get_frontend = dst_get_frontend,

	.read_status = dst_read_status,
	.read_signal_strength = dst_read_signal_strength,
	.read_snr = dst_read_snr,
};

1392

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