dw2102.c 55.3 KB
Newer Older
1
/* DVB USB framework compliant Linux driver for the
2
 *	DVBWorld DVB-S 2101, 2102, DVB-S2 2104, DVB-C 3101,
3
 *	TeVii S421, S480, S482, S600, S630, S632, S650, S660, S662,
4
 *	Prof 1100, 7500,
5
 *	Geniatech SU3000, T220,
6 7
 *	TechnoTrend S2-4600,
 *	Terratec Cinergy S2 cards
8
 * Copyright (C) 2008-2012 Igor M. Liplianin (liplianin@me.by)
9 10 11 12 13 14 15
 *
 *	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, version 2.
 *
 * see Documentation/dvb/README.dvb-usb for more information
 */
16
#include "dvb-usb-ids.h"
17
#include "dw2102.h"
18
#include "si21xx.h"
19 20
#include "stv0299.h"
#include "z0194a.h"
21 22 23
#include "stv0288.h"
#include "stb6000.h"
#include "eds1547.h"
24
#include "cx24116.h"
25
#include "tda1002x.h"
26 27
#include "mt312.h"
#include "zl10039.h"
28
#include "ts2020.h"
29 30 31 32 33
#include "ds3000.h"
#include "stv0900.h"
#include "stv6110.h"
#include "stb6100.h"
#include "stb6100_proc.h"
34
#include "m88rs2000.h"
35 36
#include "tda18271.h"
#include "cxd2820r.h"
37
#include "m88ds3103.h"
38

39 40 41
/* Max transfer size done by I2C transfer functions */
#define MAX_XFER_SIZE  64

42

43 44
#define DW210X_READ_MSG 0
#define DW210X_WRITE_MSG 1
45 46 47 48

#define REG_1F_SYMBOLRATE_BYTE0 0x1f
#define REG_20_SYMBOLRATE_BYTE1 0x20
#define REG_21_SYMBOLRATE_BYTE2 0x21
49
/* on my own*/
50
#define DW2102_VOLTAGE_CTRL (0x1800)
51
#define SU3000_STREAM_CTRL (0x1900)
52
#define DW2102_RC_QUERY (0x1a00)
53
#define DW2102_LED_CTRL (0x1b00)
54

55 56 57 58 59 60 61 62 63
#define DW2101_FIRMWARE "dvb-usb-dw2101.fw"
#define DW2102_FIRMWARE "dvb-usb-dw2102.fw"
#define DW2104_FIRMWARE "dvb-usb-dw2104.fw"
#define DW3101_FIRMWARE "dvb-usb-dw3101.fw"
#define S630_FIRMWARE   "dvb-usb-s630.fw"
#define S660_FIRMWARE   "dvb-usb-s660.fw"
#define P1100_FIRMWARE  "dvb-usb-p1100.fw"
#define P7500_FIRMWARE  "dvb-usb-p7500.fw"

64 65 66 67
#define	err_str "did not find the firmware file. (%s) " \
		"Please see linux/Documentation/dvb/ for more details " \
		"on firmware-problems."

68
struct dw2102_state {
69
	u8 initialized;
70
	u8 last_lock;
71
	struct i2c_client *i2c_client_demod;
72
	struct i2c_client *i2c_client_tuner;
73 74

	/* fe hook functions*/
75
	int (*old_set_voltage)(struct dvb_frontend *f, enum fe_sec_voltage v);
76
	int (*fe_read_status)(struct dvb_frontend *fe,
77
			      enum fe_status *status);
78 79
};

80 81 82
/* debug */
static int dvb_usb_dw2102_debug;
module_param_named(debug, dvb_usb_dw2102_debug, int, 0644);
83 84 85
MODULE_PARM_DESC(debug, "set debugging level (1=info 2=xfer 4=rc(or-able))."
						DVB_USB_DEBUG_STATUS);

86 87 88
/* demod probe */
static int demod_probe = 1;
module_param_named(demod, demod_probe, int, 0644);
89
MODULE_PARM_DESC(demod, "demod to probe (1=cx24116 2=stv0903+stv6110 4=stv0903+stb6100(or-able)).");
90

91 92
DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);

93
static int dw210x_op_rw(struct usb_device *dev, u8 request, u16 value,
94
			u16 index, u8 * data, u16 len, int flags)
95 96
{
	int ret;
97
	u8 *u8buf;
98
	unsigned int pipe = (flags == DW210X_READ_MSG) ?
99
				usb_rcvctrlpipe(dev, 0) : usb_sndctrlpipe(dev, 0);
100
	u8 request_type = (flags == DW210X_READ_MSG) ? USB_DIR_IN : USB_DIR_OUT;
101

102 103 104 105 106
	u8buf = kmalloc(len, GFP_KERNEL);
	if (!u8buf)
		return -ENOMEM;


107
	if (flags == DW210X_WRITE_MSG)
108
		memcpy(u8buf, data, len);
109 110
	ret = usb_control_msg(dev, pipe, request, request_type | USB_TYPE_VENDOR,
				value, index , u8buf, len, 2000);
111

112
	if (flags == DW210X_READ_MSG)
113
		memcpy(data, u8buf, len);
114 115

	kfree(u8buf);
116 117 118 119 120 121 122
	return ret;
}

/* I2C */
static int dw2102_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[],
		int num)
{
123
	struct dvb_usb_device *d = i2c_get_adapdata(adap);
124
	int i = 0;
125 126 127 128 129 130 131 132 133 134 135 136 137
	u8 buf6[] = {0x2c, 0x05, 0xc0, 0, 0, 0, 0};
	u16 value;

	if (!d)
		return -ENODEV;
	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
		return -EAGAIN;

	switch (num) {
	case 2:
		/* read stv0299 register */
		value = msg[0].buf[0];/* register */
		for (i = 0; i < msg[1].len; i++) {
138
			dw210x_op_rw(d->udev, 0xb5, value + i, 0,
139
					buf6, 2, DW210X_READ_MSG);
140 141 142 143 144 145 146 147 148 149
			msg[1].buf[i] = buf6[0];
		}
		break;
	case 1:
		switch (msg[0].addr) {
		case 0x68:
			/* write to stv0299 register */
			buf6[0] = 0x2a;
			buf6[1] = msg[0].buf[0];
			buf6[2] = msg[0].buf[1];
150
			dw210x_op_rw(d->udev, 0xb2, 0, 0,
151
					buf6, 3, DW210X_WRITE_MSG);
152 153 154 155 156 157 158 159 160 161 162
			break;
		case 0x60:
			if (msg[0].flags == 0) {
			/* write to tuner pll */
				buf6[0] = 0x2c;
				buf6[1] = 5;
				buf6[2] = 0xc0;
				buf6[3] = msg[0].buf[0];
				buf6[4] = msg[0].buf[1];
				buf6[5] = msg[0].buf[2];
				buf6[6] = msg[0].buf[3];
163
				dw210x_op_rw(d->udev, 0xb2, 0, 0,
164
						buf6, 7, DW210X_WRITE_MSG);
165
			} else {
166
			/* read from tuner */
167
				dw210x_op_rw(d->udev, 0xb5, 0, 0,
168
						buf6, 1, DW210X_READ_MSG);
169 170 171 172
				msg[0].buf[0] = buf6[0];
			}
			break;
		case (DW2102_RC_QUERY):
173
			dw210x_op_rw(d->udev, 0xb8, 0, 0,
174
					buf6, 2, DW210X_READ_MSG);
175 176 177 178 179 180
			msg[0].buf[0] = buf6[0];
			msg[0].buf[1] = buf6[1];
			break;
		case (DW2102_VOLTAGE_CTRL):
			buf6[0] = 0x30;
			buf6[1] = msg[0].buf[0];
181
			dw210x_op_rw(d->udev, 0xb2, 0, 0,
182
					buf6, 2, DW210X_WRITE_MSG);
183 184 185 186 187 188 189 190 191 192
			break;
		}

		break;
	}

	mutex_unlock(&d->i2c_mutex);
	return num;
}

193 194 195 196 197 198 199 200 201 202 203 204 205 206
static int dw2102_serit_i2c_transfer(struct i2c_adapter *adap,
						struct i2c_msg msg[], int num)
{
	struct dvb_usb_device *d = i2c_get_adapdata(adap);
	u8 buf6[] = {0, 0, 0, 0, 0, 0, 0};

	if (!d)
		return -ENODEV;
	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
		return -EAGAIN;

	switch (num) {
	case 2:
		/* read si2109 register by number */
207
		buf6[0] = msg[0].addr << 1;
208 209
		buf6[1] = msg[0].len;
		buf6[2] = msg[0].buf[0];
210
		dw210x_op_rw(d->udev, 0xc2, 0, 0,
211 212
				buf6, msg[0].len + 2, DW210X_WRITE_MSG);
		/* read si2109 register */
213
		dw210x_op_rw(d->udev, 0xc3, 0xd0, 0,
214 215 216 217 218 219 220 221
				buf6, msg[1].len + 2, DW210X_READ_MSG);
		memcpy(msg[1].buf, buf6 + 2, msg[1].len);

		break;
	case 1:
		switch (msg[0].addr) {
		case 0x68:
			/* write to si2109 register */
222
			buf6[0] = msg[0].addr << 1;
223 224
			buf6[1] = msg[0].len;
			memcpy(buf6 + 2, msg[0].buf, msg[0].len);
225
			dw210x_op_rw(d->udev, 0xc2, 0, 0, buf6,
226 227 228
					msg[0].len + 2, DW210X_WRITE_MSG);
			break;
		case(DW2102_RC_QUERY):
229
			dw210x_op_rw(d->udev, 0xb8, 0, 0,
230 231 232 233 234 235 236
					buf6, 2, DW210X_READ_MSG);
			msg[0].buf[0] = buf6[0];
			msg[0].buf[1] = buf6[1];
			break;
		case(DW2102_VOLTAGE_CTRL):
			buf6[0] = 0x30;
			buf6[1] = msg[0].buf[0];
237
			dw210x_op_rw(d->udev, 0xb2, 0, 0,
238 239 240 241 242 243 244 245 246
					buf6, 2, DW210X_WRITE_MSG);
			break;
		}
		break;
	}

	mutex_unlock(&d->i2c_mutex);
	return num;
}
247

248 249 250
static int dw2102_earda_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[], int num)
{
	struct dvb_usb_device *d = i2c_get_adapdata(adap);
251
	int ret;
252 253 254 255 256 257 258 259 260 261

	if (!d)
		return -ENODEV;
	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
		return -EAGAIN;

	switch (num) {
	case 2: {
		/* read */
		/* first write first register number */
262 263 264 265 266
		u8 ibuf[MAX_XFER_SIZE], obuf[3];

		if (2 + msg[1].len > sizeof(ibuf)) {
			warn("i2c rd: len=%d is too big!\n",
			     msg[1].len);
267 268
			ret = -EOPNOTSUPP;
			goto unlock;
269 270
		}

271
		obuf[0] = msg[0].addr << 1;
272 273
		obuf[1] = msg[0].len;
		obuf[2] = msg[0].buf[0];
274
		dw210x_op_rw(d->udev, 0xc2, 0, 0,
275 276
				obuf, msg[0].len + 2, DW210X_WRITE_MSG);
		/* second read registers */
277
		dw210x_op_rw(d->udev, 0xc3, 0xd1 , 0,
278 279 280 281 282 283 284 285 286
				ibuf, msg[1].len + 2, DW210X_READ_MSG);
		memcpy(msg[1].buf, ibuf + 2, msg[1].len);

		break;
	}
	case 1:
		switch (msg[0].addr) {
		case 0x68: {
			/* write to register */
287 288 289 290 291
			u8 obuf[MAX_XFER_SIZE];

			if (2 + msg[0].len > sizeof(obuf)) {
				warn("i2c wr: len=%d is too big!\n",
				     msg[1].len);
292 293
				ret = -EOPNOTSUPP;
				goto unlock;
294 295
			}

296
			obuf[0] = msg[0].addr << 1;
297 298
			obuf[1] = msg[0].len;
			memcpy(obuf + 2, msg[0].buf, msg[0].len);
299
			dw210x_op_rw(d->udev, 0xc2, 0, 0,
300 301 302 303 304
					obuf, msg[0].len + 2, DW210X_WRITE_MSG);
			break;
		}
		case 0x61: {
			/* write to tuner */
305 306 307 308 309
			u8 obuf[MAX_XFER_SIZE];

			if (2 + msg[0].len > sizeof(obuf)) {
				warn("i2c wr: len=%d is too big!\n",
				     msg[1].len);
310 311
				ret = -EOPNOTSUPP;
				goto unlock;
312 313
			}

314
			obuf[0] = msg[0].addr << 1;
315 316
			obuf[1] = msg[0].len;
			memcpy(obuf + 2, msg[0].buf, msg[0].len);
317
			dw210x_op_rw(d->udev, 0xc2, 0, 0,
318 319 320 321 322
					obuf, msg[0].len + 2, DW210X_WRITE_MSG);
			break;
		}
		case(DW2102_RC_QUERY): {
			u8 ibuf[2];
323
			dw210x_op_rw(d->udev, 0xb8, 0, 0,
324 325 326 327 328 329 330 331
					ibuf, 2, DW210X_READ_MSG);
			memcpy(msg[0].buf, ibuf , 2);
			break;
		}
		case(DW2102_VOLTAGE_CTRL): {
			u8 obuf[2];
			obuf[0] = 0x30;
			obuf[1] = msg[0].buf[0];
332
			dw210x_op_rw(d->udev, 0xb2, 0, 0,
333 334 335 336 337 338 339
					obuf, 2, DW210X_WRITE_MSG);
			break;
		}
		}

		break;
	}
340
	ret = num;
341

342
unlock:
343
	mutex_unlock(&d->i2c_mutex);
344
	return ret;
345
}
346

347 348 349
static int dw2104_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[], int num)
{
	struct dvb_usb_device *d = i2c_get_adapdata(adap);
350
	int len, i, j, ret;
351 352 353 354 355 356

	if (!d)
		return -ENODEV;
	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
		return -EAGAIN;

357 358
	for (j = 0; j < num; j++) {
		switch (msg[j].addr) {
359 360
		case(DW2102_RC_QUERY): {
			u8 ibuf[2];
361
			dw210x_op_rw(d->udev, 0xb8, 0, 0,
362
					ibuf, 2, DW210X_READ_MSG);
363
			memcpy(msg[j].buf, ibuf , 2);
364 365 366 367 368
			break;
		}
		case(DW2102_VOLTAGE_CTRL): {
			u8 obuf[2];
			obuf[0] = 0x30;
369
			obuf[1] = msg[j].buf[0];
370
			dw210x_op_rw(d->udev, 0xb2, 0, 0,
371
					obuf, 2, DW210X_WRITE_MSG);
372 373
			break;
		}
374 375 376 377 378 379 380
		/*case 0x55: cx24116
		case 0x6a: stv0903
		case 0x68: ds3000, stv0903
		case 0x60: ts2020, stv6110, stb6100 */
		default: {
			if (msg[j].flags == I2C_M_RD) {
				/* read registers */
381 382 383 384 385
				u8  ibuf[MAX_XFER_SIZE];

				if (2 + msg[j].len > sizeof(ibuf)) {
					warn("i2c rd: len=%d is too big!\n",
					     msg[j].len);
386 387
					ret = -EOPNOTSUPP;
					goto unlock;
388 389
				}

390
				dw210x_op_rw(d->udev, 0xc3,
391 392 393 394
						(msg[j].addr << 1) + 1, 0,
						ibuf, msg[j].len + 2,
						DW210X_READ_MSG);
				memcpy(msg[j].buf, ibuf + 2, msg[j].len);
395
				mdelay(10);
396 397 398 399 400 401 402 403 404 405 406 407 408 409
			} else if (((msg[j].buf[0] == 0xb0) &&
						(msg[j].addr == 0x68)) ||
						((msg[j].buf[0] == 0xf7) &&
						(msg[j].addr == 0x55))) {
				/* write firmware */
				u8 obuf[19];
				obuf[0] = msg[j].addr << 1;
				obuf[1] = (msg[j].len > 15 ? 17 : msg[j].len);
				obuf[2] = msg[j].buf[0];
				len = msg[j].len - 1;
				i = 1;
				do {
					memcpy(obuf + 3, msg[j].buf + i,
							(len > 16 ? 16 : len));
410
					dw210x_op_rw(d->udev, 0xc2, 0, 0,
411 412 413 414 415 416 417
						obuf, (len > 16 ? 16 : len) + 3,
						DW210X_WRITE_MSG);
					i += 16;
					len -= 16;
				} while (len > 0);
			} else {
				/* write registers */
418 419 420 421 422
				u8 obuf[MAX_XFER_SIZE];

				if (2 + msg[j].len > sizeof(obuf)) {
					warn("i2c wr: len=%d is too big!\n",
					     msg[j].len);
423 424
					ret = -EOPNOTSUPP;
					goto unlock;
425 426
				}

427 428 429
				obuf[0] = msg[j].addr << 1;
				obuf[1] = msg[j].len;
				memcpy(obuf + 2, msg[j].buf, msg[j].len);
430
				dw210x_op_rw(d->udev, 0xc2, 0, 0,
431 432 433 434 435
						obuf, msg[j].len + 2,
						DW210X_WRITE_MSG);
			}
			break;
		}
436 437 438
		}

	}
439
	ret = num;
440

441
unlock:
442
	mutex_unlock(&d->i2c_mutex);
443
	return ret;
444 445
}

446 447 448 449
static int dw3101_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[],
								int num)
{
	struct dvb_usb_device *d = i2c_get_adapdata(adap);
450
	int ret;
451
	int i;
452 453 454 455 456 457 458 459 460 461

	if (!d)
		return -ENODEV;
	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
		return -EAGAIN;

	switch (num) {
	case 2: {
		/* read */
		/* first write first register number */
462 463 464 465 466
		u8 ibuf[MAX_XFER_SIZE], obuf[3];

		if (2 + msg[1].len > sizeof(ibuf)) {
			warn("i2c rd: len=%d is too big!\n",
			     msg[1].len);
467 468
			ret = -EOPNOTSUPP;
			goto unlock;
469
		}
470 471 472
		obuf[0] = msg[0].addr << 1;
		obuf[1] = msg[0].len;
		obuf[2] = msg[0].buf[0];
473
		dw210x_op_rw(d->udev, 0xc2, 0, 0,
474 475
				obuf, msg[0].len + 2, DW210X_WRITE_MSG);
		/* second read registers */
476
		dw210x_op_rw(d->udev, 0xc3, 0x19 , 0,
477 478 479 480 481 482 483 484 485 486
				ibuf, msg[1].len + 2, DW210X_READ_MSG);
		memcpy(msg[1].buf, ibuf + 2, msg[1].len);

		break;
	}
	case 1:
		switch (msg[0].addr) {
		case 0x60:
		case 0x0c: {
			/* write to register */
487 488 489 490 491
			u8 obuf[MAX_XFER_SIZE];

			if (2 + msg[0].len > sizeof(obuf)) {
				warn("i2c wr: len=%d is too big!\n",
				     msg[0].len);
492 493
				ret = -EOPNOTSUPP;
				goto unlock;
494
			}
495 496 497
			obuf[0] = msg[0].addr << 1;
			obuf[1] = msg[0].len;
			memcpy(obuf + 2, msg[0].buf, msg[0].len);
498
			dw210x_op_rw(d->udev, 0xc2, 0, 0,
499 500 501 502 503
					obuf, msg[0].len + 2, DW210X_WRITE_MSG);
			break;
		}
		case(DW2102_RC_QUERY): {
			u8 ibuf[2];
504
			dw210x_op_rw(d->udev, 0xb8, 0, 0,
505 506 507 508 509 510 511 512 513 514 515 516 517 518
					ibuf, 2, DW210X_READ_MSG);
			memcpy(msg[0].buf, ibuf , 2);
			break;
		}
		}

		break;
	}

	for (i = 0; i < num; i++) {
		deb_xfer("%02x:%02x: %s ", i, msg[i].addr,
				msg[i].flags == 0 ? ">>>" : "<<<");
		debug_dump(msg[i].buf, msg[i].len, deb_xfer);
	}
519
	ret = num;
520

521
unlock:
522
	mutex_unlock(&d->i2c_mutex);
523
	return ret;
524 525
}

526
static int s6x0_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[],
527 528 529
								int num)
{
	struct dvb_usb_device *d = i2c_get_adapdata(adap);
530
	struct usb_device *udev;
531
	int len, i, j, ret;
532 533 534

	if (!d)
		return -ENODEV;
535
	udev = d->udev;
536 537 538
	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
		return -EAGAIN;

539 540
	for (j = 0; j < num; j++) {
		switch (msg[j].addr) {
541
		case (DW2102_RC_QUERY): {
542
			u8 ibuf[5];
543
			dw210x_op_rw(d->udev, 0xb8, 0, 0,
544 545
					ibuf, 5, DW210X_READ_MSG);
			memcpy(msg[j].buf, ibuf + 3, 2);
546 547 548 549
			break;
		}
		case (DW2102_VOLTAGE_CTRL): {
			u8 obuf[2];
550 551 552

			obuf[0] = 1;
			obuf[1] = msg[j].buf[1];/* off-on */
553
			dw210x_op_rw(d->udev, 0x8a, 0, 0,
554
					obuf, 2, DW210X_WRITE_MSG);
555
			obuf[0] = 3;
556
			obuf[1] = msg[j].buf[0];/* 13v-18v */
557
			dw210x_op_rw(d->udev, 0x8a, 0, 0,
558 559 560
					obuf, 2, DW210X_WRITE_MSG);
			break;
		}
561 562 563 564 565
		case (DW2102_LED_CTRL): {
			u8 obuf[2];

			obuf[0] = 5;
			obuf[1] = msg[j].buf[0];
566
			dw210x_op_rw(d->udev, 0x8a, 0, 0,
567 568 569
					obuf, 2, DW210X_WRITE_MSG);
			break;
		}
570 571
		/*case 0x55: cx24116
		case 0x6a: stv0903
572
		case 0x68: ds3000, stv0903, rs2000
573 574 575 576 577
		case 0x60: ts2020, stv6110, stb6100
		case 0xa0: eeprom */
		default: {
			if (msg[j].flags == I2C_M_RD) {
				/* read registers */
578 579 580 581 582
				u8 ibuf[MAX_XFER_SIZE];

				if (msg[j].len > sizeof(ibuf)) {
					warn("i2c rd: len=%d is too big!\n",
					     msg[j].len);
583 584
					ret = -EOPNOTSUPP;
					goto unlock;
585 586
				}

587
				dw210x_op_rw(d->udev, 0x91, 0, 0,
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
						ibuf, msg[j].len,
						DW210X_READ_MSG);
				memcpy(msg[j].buf, ibuf, msg[j].len);
				break;
			} else if ((msg[j].buf[0] == 0xb0) &&
						(msg[j].addr == 0x68)) {
				/* write firmware */
				u8 obuf[19];
				obuf[0] = (msg[j].len > 16 ?
						18 : msg[j].len + 1);
				obuf[1] = msg[j].addr << 1;
				obuf[2] = msg[j].buf[0];
				len = msg[j].len - 1;
				i = 1;
				do {
					memcpy(obuf + 3, msg[j].buf + i,
							(len > 16 ? 16 : len));
605
					dw210x_op_rw(d->udev, 0x80, 0, 0,
606 607 608 609 610
						obuf, (len > 16 ? 16 : len) + 3,
						DW210X_WRITE_MSG);
					i += 16;
					len -= 16;
				} while (len > 0);
611
			} else if (j < (num - 1)) {
612
				/* write register addr before read */
613 614 615 616 617
				u8 obuf[MAX_XFER_SIZE];

				if (2 + msg[j].len > sizeof(obuf)) {
					warn("i2c wr: len=%d is too big!\n",
					     msg[j].len);
618 619
					ret = -EOPNOTSUPP;
					goto unlock;
620 621
				}

622 623 624
				obuf[0] = msg[j + 1].len;
				obuf[1] = (msg[j].addr << 1);
				memcpy(obuf + 2, msg[j].buf, msg[j].len);
625
				dw210x_op_rw(d->udev,
626
						le16_to_cpu(udev->descriptor.idProduct) ==
627
						0x7500 ? 0x92 : 0x90, 0, 0,
628 629 630
						obuf, msg[j].len + 2,
						DW210X_WRITE_MSG);
				break;
631 632
			} else {
				/* write registers */
633 634 635 636 637
				u8 obuf[MAX_XFER_SIZE];

				if (2 + msg[j].len > sizeof(obuf)) {
					warn("i2c wr: len=%d is too big!\n",
					     msg[j].len);
638 639
					ret = -EOPNOTSUPP;
					goto unlock;
640
				}
641 642 643
				obuf[0] = msg[j].len + 1;
				obuf[1] = (msg[j].addr << 1);
				memcpy(obuf + 2, msg[j].buf, msg[j].len);
644
				dw210x_op_rw(d->udev, 0x80, 0, 0,
645 646 647 648 649 650
						obuf, msg[j].len + 2,
						DW210X_WRITE_MSG);
				break;
			}
			break;
		}
651 652
		}
	}
653
	ret = num;
654

655
unlock:
656
	mutex_unlock(&d->i2c_mutex);
657
	return ret;
658 659
}

660 661 662 663 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 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
static int su3000_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[],
								int num)
{
	struct dvb_usb_device *d = i2c_get_adapdata(adap);
	u8 obuf[0x40], ibuf[0x40];

	if (!d)
		return -ENODEV;
	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
		return -EAGAIN;

	switch (num) {
	case 1:
		switch (msg[0].addr) {
		case SU3000_STREAM_CTRL:
			obuf[0] = msg[0].buf[0] + 0x36;
			obuf[1] = 3;
			obuf[2] = 0;
			if (dvb_usb_generic_rw(d, obuf, 3, ibuf, 0, 0) < 0)
				err("i2c transfer failed.");
			break;
		case DW2102_RC_QUERY:
			obuf[0] = 0x10;
			if (dvb_usb_generic_rw(d, obuf, 1, ibuf, 2, 0) < 0)
				err("i2c transfer failed.");
			msg[0].buf[1] = ibuf[0];
			msg[0].buf[0] = ibuf[1];
			break;
		default:
			/* always i2c write*/
			obuf[0] = 0x08;
			obuf[1] = msg[0].addr;
			obuf[2] = msg[0].len;

			memcpy(&obuf[3], msg[0].buf, msg[0].len);

			if (dvb_usb_generic_rw(d, obuf, msg[0].len + 3,
						ibuf, 1, 0) < 0)
				err("i2c transfer failed.");

		}
		break;
	case 2:
		/* always i2c read */
		obuf[0] = 0x09;
		obuf[1] = msg[0].len;
		obuf[2] = msg[1].len;
		obuf[3] = msg[0].addr;
		memcpy(&obuf[4], msg[0].buf, msg[0].len);

		if (dvb_usb_generic_rw(d, obuf, msg[0].len + 4,
					ibuf, msg[1].len + 1, 0) < 0)
			err("i2c transfer failed.");

		memcpy(msg[1].buf, &ibuf[1], msg[1].len);
		break;
	default:
		warn("more than 2 i2c messages at a time is not handled yet.");
		break;
	}
	mutex_unlock(&d->i2c_mutex);
	return num;
}

724
static u32 dw210x_i2c_func(struct i2c_adapter *adapter)
725 726 727 728 729 730
{
	return I2C_FUNC_I2C;
}

static struct i2c_algorithm dw2102_i2c_algo = {
	.master_xfer = dw2102_i2c_transfer,
731 732 733 734 735 736
	.functionality = dw210x_i2c_func,
};

static struct i2c_algorithm dw2102_serit_i2c_algo = {
	.master_xfer = dw2102_serit_i2c_transfer,
	.functionality = dw210x_i2c_func,
737 738
};

739 740 741 742 743
static struct i2c_algorithm dw2102_earda_i2c_algo = {
	.master_xfer = dw2102_earda_i2c_transfer,
	.functionality = dw210x_i2c_func,
};

744 745
static struct i2c_algorithm dw2104_i2c_algo = {
	.master_xfer = dw2104_i2c_transfer,
746
	.functionality = dw210x_i2c_func,
747 748
};

749 750 751 752 753
static struct i2c_algorithm dw3101_i2c_algo = {
	.master_xfer = dw3101_i2c_transfer,
	.functionality = dw210x_i2c_func,
};

754 755
static struct i2c_algorithm s6x0_i2c_algo = {
	.master_xfer = s6x0_i2c_transfer,
756 757 758
	.functionality = dw210x_i2c_func,
};

759 760 761 762 763
static struct i2c_algorithm su3000_i2c_algo = {
	.master_xfer = su3000_i2c_transfer,
	.functionality = dw210x_i2c_func,
};

764
static int dw210x_read_mac_address(struct dvb_usb_device *d, u8 mac[6])
765 766 767 768 769 770
{
	int i;
	u8 ibuf[] = {0, 0};
	u8 eeprom[256], eepromline[16];

	for (i = 0; i < 256; i++) {
771
		if (dw210x_op_rw(d->udev, 0xb6, 0xa0 , i, ibuf, 2, DW210X_READ_MSG) < 0) {
772 773 774 775 776 777 778 779 780 781 782
			err("read eeprom failed.");
			return -1;
		} else {
			eepromline[i%16] = ibuf[0];
			eeprom[i] = ibuf[0];
		}
		if ((i % 16) == 15) {
			deb_xfer("%02x: ", i - 15);
			debug_dump(eepromline, 16, deb_xfer);
		}
	}
783

784 785 786 787
	memcpy(mac, eeprom + 8, 6);
	return 0;
};

788
static int s6x0_read_mac_address(struct dvb_usb_device *d, u8 mac[6])
789 790
{
	int i, ret;
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
	u8 ibuf[] = { 0 }, obuf[] = { 0 };
	u8 eeprom[256], eepromline[16];
	struct i2c_msg msg[] = {
		{
			.addr = 0xa0 >> 1,
			.flags = 0,
			.buf = obuf,
			.len = 1,
		}, {
			.addr = 0xa0 >> 1,
			.flags = I2C_M_RD,
			.buf = ibuf,
			.len = 1,
		}
	};
806 807

	for (i = 0; i < 256; i++) {
808 809 810
		obuf[0] = i;
		ret = s6x0_i2c_transfer(&d->i2c_adap, msg, 2);
		if (ret != 2) {
811 812 813
			err("read eeprom failed.");
			return -1;
		} else {
814 815
			eepromline[i % 16] = ibuf[0];
			eeprom[i] = ibuf[0];
816 817 818 819 820 821 822 823 824 825 826 827
		}

		if ((i % 16) == 15) {
			deb_xfer("%02x: ", i - 15);
			debug_dump(eepromline, 16, deb_xfer);
		}
	}

	memcpy(mac, eeprom + 16, 6);
	return 0;
};

828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
static int su3000_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
{
	static u8 command_start[] = {0x00};
	static u8 command_stop[] = {0x01};
	struct i2c_msg msg = {
		.addr = SU3000_STREAM_CTRL,
		.flags = 0,
		.buf = onoff ? command_start : command_stop,
		.len = 1
	};

	i2c_transfer(&adap->dev->i2c_adap, &msg, 1);

	return 0;
}

static int su3000_power_ctrl(struct dvb_usb_device *d, int i)
{
846
	struct dw2102_state *state = (struct dw2102_state *)d->priv;
847 848
	u8 obuf[] = {0xde, 0};

849
	info("%s: %d, initialized %d", __func__, i, state->initialized);
850 851 852 853

	if (i && !state->initialized) {
		state->initialized = 1;
		/* reset board */
854
		return dvb_usb_generic_rw(d, obuf, 2, NULL, 0, 0);
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 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
	}

	return 0;
}

static int su3000_read_mac_address(struct dvb_usb_device *d, u8 mac[6])
{
	int i;
	u8 obuf[] = { 0x1f, 0xf0 };
	u8 ibuf[] = { 0 };
	struct i2c_msg msg[] = {
		{
			.addr = 0x51,
			.flags = 0,
			.buf = obuf,
			.len = 2,
		}, {
			.addr = 0x51,
			.flags = I2C_M_RD,
			.buf = ibuf,
			.len = 1,

		}
	};

	for (i = 0; i < 6; i++) {
		obuf[1] = 0xf0 + i;
		if (i2c_transfer(&d->i2c_adap, msg, 2) != 2)
			break;
		else
			mac[i] = ibuf[0];
	}

	return 0;
}

static int su3000_identify_state(struct usb_device *udev,
				 struct dvb_usb_device_properties *props,
				 struct dvb_usb_device_description **desc,
				 int *cold)
{
896
	info("%s", __func__);
897 898 899 900 901

	*cold = 0;
	return 0;
}

902 903
static int dw210x_set_voltage(struct dvb_frontend *fe,
			      enum fe_sec_voltage voltage)
904
{
905 906 907 908 909 910 911 912
	static u8 command_13v[] = {0x00, 0x01};
	static u8 command_18v[] = {0x01, 0x01};
	static u8 command_off[] = {0x00, 0x00};
	struct i2c_msg msg = {
		.addr = DW2102_VOLTAGE_CTRL,
		.flags = 0,
		.buf = command_off,
		.len = 2,
913 914 915 916 917
	};

	struct dvb_usb_adapter *udev_adap =
		(struct dvb_usb_adapter *)(fe->dvb->priv);
	if (voltage == SEC_VOLTAGE_18)
918 919 920 921 922 923
		msg.buf = command_18v;
	else if (voltage == SEC_VOLTAGE_13)
		msg.buf = command_13v;

	i2c_transfer(&udev_adap->dev->i2c_adap, &msg, 1);

924 925 926
	return 0;
}

927 928
static int s660_set_voltage(struct dvb_frontend *fe,
			    enum fe_sec_voltage voltage)
929 930 931
{
	struct dvb_usb_adapter *d =
		(struct dvb_usb_adapter *)(fe->dvb->priv);
932
	struct dw2102_state *st = (struct dw2102_state *)d->dev->priv;
933 934 935 936 937 938 939 940

	dw210x_set_voltage(fe, voltage);
	if (st->old_set_voltage)
		st->old_set_voltage(fe, voltage);

	return 0;
}

941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
static void dw210x_led_ctrl(struct dvb_frontend *fe, int offon)
{
	static u8 led_off[] = { 0 };
	static u8 led_on[] = { 1 };
	struct i2c_msg msg = {
		.addr = DW2102_LED_CTRL,
		.flags = 0,
		.buf = led_off,
		.len = 1
	};
	struct dvb_usb_adapter *udev_adap =
		(struct dvb_usb_adapter *)(fe->dvb->priv);

	if (offon)
		msg.buf = led_on;
	i2c_transfer(&udev_adap->dev->i2c_adap, &msg, 1);
}

959 960
static int tt_s2_4600_read_status(struct dvb_frontend *fe,
				  enum fe_status *status)
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
{
	struct dvb_usb_adapter *d =
		(struct dvb_usb_adapter *)(fe->dvb->priv);
	struct dw2102_state *st = (struct dw2102_state *)d->dev->priv;
	int ret;

	ret = st->fe_read_status(fe, status);

	/* resync slave fifo when signal change from unlock to lock */
	if ((*status & FE_HAS_LOCK) && (!st->last_lock))
		su3000_streaming_ctrl(d, 1);

	st->last_lock = (*status & FE_HAS_LOCK) ? 1 : 0;
	return ret;
}

977 978 979 980 981 982 983 984 985 986 987 988
static struct stv0299_config sharp_z0194a_config = {
	.demod_address = 0x68,
	.inittab = sharp_z0194a_inittab,
	.mclk = 88000000UL,
	.invert = 1,
	.skip_reinit = 0,
	.lock_output = STV0299_LOCKOUTPUT_1,
	.volt13_op0_op1 = STV0299_VOLT13_OP1,
	.min_delay_ms = 100,
	.set_symbol_rate = sharp_z0194a_set_symbol_rate,
};

989 990
static struct cx24116_config dw2104_config = {
	.demod_address = 0x55,
991
	.mpg_clk_pos_pol = 0x01,
992 993
};

994 995 996 997 998 999
static struct si21xx_config serit_sp1511lhb_config = {
	.demod_address = 0x68,
	.min_delay_ms = 100,

};

1000 1001 1002 1003 1004
static struct tda10023_config dw3101_tda10023_config = {
	.demod_address = 0x0c,
	.invert = 1,
};

1005 1006 1007 1008
static struct mt312_config zl313_config = {
	.demod_address = 0x0e,
};

1009 1010 1011 1012
static struct ds3000_config dw2104_ds3000_config = {
	.demod_address = 0x68,
};

1013
static struct ts2020_config dw2104_ts2020_config = {
1014
	.tuner_address = 0x60,
1015
	.clk_out_div = 1,
1016
	.frequency_div = 1060000,
1017 1018
};

1019 1020
static struct ds3000_config s660_ds3000_config = {
	.demod_address = 0x68,
1021
	.ci_mode = 1,
1022 1023 1024
	.set_lock_led = dw210x_led_ctrl,
};

1025 1026 1027 1028 1029 1030
static struct ts2020_config s660_ts2020_config = {
	.tuner_address = 0x60,
	.clk_out_div = 1,
	.frequency_div = 1146000,
};

1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
static struct stv0900_config dw2104a_stv0900_config = {
	.demod_address = 0x6a,
	.demod_mode = 0,
	.xtal = 27000000,
	.clkmode = 3,/* 0-CLKI, 2-XTALI, else AUTO */
	.diseqc_mode = 2,/* 2/3 PWM */
	.tun1_maddress = 0,/* 0x60 */
	.tun1_adc = 0,/* 2 Vpp */
	.path1_mode = 3,
};

static struct stb6100_config dw2104a_stb6100_config = {
	.tuner_address = 0x60,
	.refclock = 27000000,
};

static struct stv0900_config dw2104_stv0900_config = {
	.demod_address = 0x68,
	.demod_mode = 0,
	.xtal = 8000000,
	.clkmode = 3,
	.diseqc_mode = 2,
	.tun1_maddress = 0,
	.tun1_adc = 1,/* 1 Vpp */
	.path1_mode = 3,
};

static struct stv6110_config dw2104_stv6110_config = {
	.i2c_address = 0x60,
	.mclk = 16000000,
	.clk_div = 1,
};

1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
static struct stv0900_config prof_7500_stv0900_config = {
	.demod_address = 0x6a,
	.demod_mode = 0,
	.xtal = 27000000,
	.clkmode = 3,/* 0-CLKI, 2-XTALI, else AUTO */
	.diseqc_mode = 2,/* 2/3 PWM */
	.tun1_maddress = 0,/* 0x60 */
	.tun1_adc = 0,/* 2 Vpp */
	.path1_mode = 3,
	.tun1_type = 3,
1074
	.set_lock_led = dw210x_led_ctrl,
1075 1076
};

1077 1078 1079
static struct ds3000_config su3000_ds3000_config = {
	.demod_address = 0x68,
	.ci_mode = 1,
1080
	.set_lock_led = dw210x_led_ctrl,
1081 1082
};

1083 1084 1085
static struct cxd2820r_config cxd2820r_config = {
	.i2c_address = 0x6c, /* (0xd8 >> 1) */
	.ts_mode = 0x38,
1086
	.ts_clock_inv = 1,
1087 1088 1089 1090 1091 1092 1093
};

static struct tda18271_config tda18271_config = {
	.output_opt = TDA18271_OUTPUT_LT_OFF,
	.gate = TDA18271_GATE_DIGITAL,
};

1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
static u8 m88rs2000_inittab[] = {
	DEMOD_WRITE, 0x9a, 0x30,
	DEMOD_WRITE, 0x00, 0x01,
	WRITE_DELAY, 0x19, 0x00,
	DEMOD_WRITE, 0x00, 0x00,
	DEMOD_WRITE, 0x9a, 0xb0,
	DEMOD_WRITE, 0x81, 0xc1,
	DEMOD_WRITE, 0x81, 0x81,
	DEMOD_WRITE, 0x86, 0xc6,
	DEMOD_WRITE, 0x9a, 0x30,
	DEMOD_WRITE, 0xf0, 0x80,
	DEMOD_WRITE, 0xf1, 0xbf,
	DEMOD_WRITE, 0xb0, 0x45,
	DEMOD_WRITE, 0xb2, 0x01,
	DEMOD_WRITE, 0x9a, 0xb0,
	0xff, 0xaa, 0xff
};

static struct m88rs2000_config s421_m88rs2000_config = {
	.demod_addr = 0x68,
	.inittab = m88rs2000_inittab,
};

1117 1118
static int dw2104_frontend_attach(struct dvb_usb_adapter *d)
{
1119 1120 1121
	struct dvb_tuner_ops *tuner_ops = NULL;

	if (demod_probe & 4) {
1122
		d->fe_adap[0].fe = dvb_attach(stv0900_attach, &dw2104a_stv0900_config,
1123
				&d->dev->i2c_adap, 0);
1124 1125
		if (d->fe_adap[0].fe != NULL) {
			if (dvb_attach(stb6100_attach, d->fe_adap[0].fe,
1126 1127
					&dw2104a_stb6100_config,
					&d->dev->i2c_adap)) {
1128
				tuner_ops = &d->fe_adap[0].fe->ops.tuner_ops;
1129 1130 1131 1132
				tuner_ops->set_frequency = stb6100_set_freq;
				tuner_ops->get_frequency = stb6100_get_freq;
				tuner_ops->set_bandwidth = stb6100_set_bandw;
				tuner_ops->get_bandwidth = stb6100_get_bandw;
1133
				d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1134
				info("Attached STV0900+STB6100!");
1135 1136 1137 1138 1139 1140
				return 0;
			}
		}
	}

	if (demod_probe & 2) {
1141
		d->fe_adap[0].fe = dvb_attach(stv0900_attach, &dw2104_stv0900_config,
1142
				&d->dev->i2c_adap, 0);
1143 1144
		if (d->fe_adap[0].fe != NULL) {
			if (dvb_attach(stv6110_attach, d->fe_adap[0].fe,
1145 1146
					&dw2104_stv6110_config,
					&d->dev->i2c_adap)) {
1147
				d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1148
				info("Attached STV0900+STV6110A!");
1149 1150 1151 1152 1153 1154
				return 0;
			}
		}
	}

	if (demod_probe & 1) {
1155
		d->fe_adap[0].fe = dvb_attach(cx24116_attach, &dw2104_config,
1156
				&d->dev->i2c_adap);
1157 1158
		if (d->fe_adap[0].fe != NULL) {
			d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1159
			info("Attached cx24116!");
1160 1161 1162 1163
			return 0;
		}
	}

1164
	d->fe_adap[0].fe = dvb_attach(ds3000_attach, &dw2104_ds3000_config,
1165
			&d->dev->i2c_adap);
1166
	if (d->fe_adap[0].fe != NULL) {
1167 1168
		dvb_attach(ts2020_attach, d->fe_adap[0].fe,
			&dw2104_ts2020_config, &d->dev->i2c_adap);
1169
		d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1170
		info("Attached DS3000!");
1171 1172
		return 0;
	}
1173

1174 1175 1176
	return -EIO;
}

1177
static struct dvb_usb_device_properties dw2102_properties;
1178
static struct dvb_usb_device_properties dw2104_properties;
1179
static struct dvb_usb_device_properties s6x0_properties;
1180

1181 1182
static int dw2102_frontend_attach(struct dvb_usb_adapter *d)
{
1183 1184
	if (dw2102_properties.i2c_algo == &dw2102_serit_i2c_algo) {
		/*dw2102_properties.adapter->tuner_attach = NULL;*/
1185
		d->fe_adap[0].fe = dvb_attach(si21xx_attach, &serit_sp1511lhb_config,
1186
					&d->dev->i2c_adap);
1187 1188
		if (d->fe_adap[0].fe != NULL) {
			d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1189
			info("Attached si21xx!");
1190 1191 1192
			return 0;
		}
	}
1193

1194
	if (dw2102_properties.i2c_algo == &dw2102_earda_i2c_algo) {
1195
		d->fe_adap[0].fe = dvb_attach(stv0288_attach, &earda_config,
1196
					&d->dev->i2c_adap);
1197 1198
		if (d->fe_adap[0].fe != NULL) {
			if (dvb_attach(stb6000_attach, d->fe_adap[0].fe, 0x61,
1199
					&d->dev->i2c_adap)) {
1200
				d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1201
				info("Attached stv0288!");
1202 1203
				return 0;
			}
1204 1205 1206
		}
	}

1207 1208
	if (dw2102_properties.i2c_algo == &dw2102_i2c_algo) {
		/*dw2102_properties.adapter->tuner_attach = dw2102_tuner_attach;*/
1209
		d->fe_adap[0].fe = dvb_attach(stv0299_attach, &sharp_z0194a_config,
1210
					&d->dev->i2c_adap);
1211 1212
		if (d->fe_adap[0].fe != NULL) {
			d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1213
			info("Attached stv0299!");
1214 1215
			return 0;
		}
1216 1217 1218 1219
	}
	return -EIO;
}

1220 1221
static int dw3101_frontend_attach(struct dvb_usb_adapter *d)
{
1222
	d->fe_adap[0].fe = dvb_attach(tda10023_attach, &dw3101_tda10023_config,
1223
				&d->dev->i2c_adap, 0x48);
1224
	if (d->fe_adap[0].fe != NULL) {
1225
		info("Attached tda10023!");
1226 1227 1228 1229 1230
		return 0;
	}
	return -EIO;
}

1231
static int zl100313_frontend_attach(struct dvb_usb_adapter *d)
1232
{
1233
	d->fe_adap[0].fe = dvb_attach(mt312_attach, &zl313_config,
1234
			&d->dev->i2c_adap);
1235 1236
	if (d->fe_adap[0].fe != NULL) {
		if (dvb_attach(zl10039_attach, d->fe_adap[0].fe, 0x60,
1237
				&d->dev->i2c_adap)) {
1238
			d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1239
			info("Attached zl100313+zl10039!");
1240 1241 1242 1243
			return 0;
		}
	}

1244 1245 1246 1247 1248
	return -EIO;
}

static int stv0288_frontend_attach(struct dvb_usb_adapter *d)
{
1249 1250
	u8 obuf[] = {7, 1};

1251
	d->fe_adap[0].fe = dvb_attach(stv0288_attach, &earda_config,
1252 1253
			&d->dev->i2c_adap);

1254
	if (d->fe_adap[0].fe == NULL)
1255 1256
		return -EIO;

1257
	if (NULL == dvb_attach(stb6000_attach, d->fe_adap[0].fe, 0x61, &d->dev->i2c_adap))
1258 1259
		return -EIO;

1260
	d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1261 1262 1263

	dw210x_op_rw(d->dev->udev, 0x8a, 0, 0, obuf, 2, DW210X_WRITE_MSG);

1264
	info("Attached stv0288+stb6000!");
1265 1266 1267

	return 0;

1268 1269 1270 1271
}

static int ds3000_frontend_attach(struct dvb_usb_adapter *d)
{
1272
	struct dw2102_state *st = d->dev->priv;
1273
	u8 obuf[] = {7, 1};
1274

1275
	d->fe_adap[0].fe = dvb_attach(ds3000_attach, &s660_ds3000_config,
1276 1277
			&d->dev->i2c_adap);

1278
	if (d->fe_adap[0].fe == NULL)
1279 1280
		return -EIO;

1281
	dvb_attach(ts2020_attach, d->fe_adap[0].fe, &s660_ts2020_config,
1282 1283
		&d->dev->i2c_adap);

1284 1285
	st->old_set_voltage = d->fe_adap[0].fe->ops.set_voltage;
	d->fe_adap[0].fe->ops.set_voltage = s660_set_voltage;
1286 1287 1288

	dw210x_op_rw(d->dev->udev, 0x8a, 0, 0, obuf, 2, DW210X_WRITE_MSG);

1289
	info("Attached ds3000+ts2020!");
1290 1291

	return 0;
1292 1293
}

1294 1295
static int prof_7500_frontend_attach(struct dvb_usb_adapter *d)
{
1296 1297
	u8 obuf[] = {7, 1};

1298
	d->fe_adap[0].fe = dvb_attach(stv0900_attach, &prof_7500_stv0900_config,
1299
					&d->dev->i2c_adap, 0);
1300
	if (d->fe_adap[0].fe == NULL)
1301
		return -EIO;
1302

1303
	d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1304

1305 1306
	dw210x_op_rw(d->dev->udev, 0x8a, 0, 0, obuf, 2, DW210X_WRITE_MSG);

1307
	info("Attached STV0900+STB6100A!");
1308 1309 1310 1311

	return 0;
}

1312 1313 1314 1315 1316 1317 1318 1319
static int su3000_frontend_attach(struct dvb_usb_adapter *d)
{
	u8 obuf[3] = { 0xe, 0x80, 0 };
	u8 ibuf[] = { 0 };

	if (dvb_usb_generic_rw(d->dev, obuf, 3, ibuf, 1, 0) < 0)
		err("command 0x0e transfer failed.");

1320 1321 1322 1323 1324 1325 1326 1327
	obuf[0] = 0xe;
	obuf[1] = 0x02;
	obuf[2] = 1;

	if (dvb_usb_generic_rw(d->dev, obuf, 3, ibuf, 1, 0) < 0)
		err("command 0x0e transfer failed.");
	msleep(300);

1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
	obuf[0] = 0xe;
	obuf[1] = 0x83;
	obuf[2] = 0;

	if (dvb_usb_generic_rw(d->dev, obuf, 3, ibuf, 1, 0) < 0)
		err("command 0x0e transfer failed.");

	obuf[0] = 0xe;
	obuf[1] = 0x83;
	obuf[2] = 1;

	if (dvb_usb_generic_rw(d->dev, obuf, 3, ibuf, 1, 0) < 0)
		err("command 0x0e transfer failed.");

	obuf[0] = 0x51;

	if (dvb_usb_generic_rw(d->dev, obuf, 1, ibuf, 1, 0) < 0)
		err("command 0x51 transfer failed.");

1347
	d->fe_adap[0].fe = dvb_attach(ds3000_attach, &su3000_ds3000_config,
1348
					&d->dev->i2c_adap);
1349
	if (d->fe_adap[0].fe == NULL)
1350 1351
		return -EIO;

1352 1353 1354
	if (dvb_attach(ts2020_attach, d->fe_adap[0].fe,
				&dw2104_ts2020_config,
				&d->dev->i2c_adap)) {
1355
		info("Attached DS3000/TS2020!");
1356 1357
		return 0;
	}
1358

1359
	info("Failed to attach DS3000/TS2020!");
1360
	return -EIO;
1361 1362
}

1363 1364
static int t220_frontend_attach(struct dvb_usb_adapter *d)
{
1365
	u8 obuf[3] = { 0xe, 0x87, 0 };
1366 1367 1368 1369 1370 1371
	u8 ibuf[] = { 0 };

	if (dvb_usb_generic_rw(d->dev, obuf, 3, ibuf, 1, 0) < 0)
		err("command 0x0e transfer failed.");

	obuf[0] = 0xe;
1372 1373 1374 1375 1376 1377 1378 1379
	obuf[1] = 0x86;
	obuf[2] = 1;

	if (dvb_usb_generic_rw(d->dev, obuf, 3, ibuf, 1, 0) < 0)
		err("command 0x0e transfer failed.");

	obuf[0] = 0xe;
	obuf[1] = 0x80;
1380 1381 1382 1383 1384
	obuf[2] = 0;

	if (dvb_usb_generic_rw(d->dev, obuf, 3, ibuf, 1, 0) < 0)
		err("command 0x0e transfer failed.");

1385
	msleep(50);
1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403

	obuf[0] = 0xe;
	obuf[1] = 0x80;
	obuf[2] = 1;

	if (dvb_usb_generic_rw(d->dev, obuf, 3, ibuf, 1, 0) < 0)
		err("command 0x0e transfer failed.");

	obuf[0] = 0x51;

	if (dvb_usb_generic_rw(d->dev, obuf, 1, ibuf, 1, 0) < 0)
		err("command 0x51 transfer failed.");

	d->fe_adap[0].fe = dvb_attach(cxd2820r_attach, &cxd2820r_config,
					&d->dev->i2c_adap, NULL);
	if (d->fe_adap[0].fe != NULL) {
		if (dvb_attach(tda18271_attach, d->fe_adap[0].fe, 0x60,
					&d->dev->i2c_adap, &tda18271_config)) {
1404
			info("Attached TDA18271HD/CXD2820R!");
1405 1406 1407 1408
			return 0;
		}
	}

1409
	info("Failed to attach TDA18271HD/CXD2820R!");
1410 1411 1412
	return -EIO;
}

1413 1414 1415 1416 1417 1418 1419 1420 1421 1422
static int m88rs2000_frontend_attach(struct dvb_usb_adapter *d)
{
	u8 obuf[] = { 0x51 };
	u8 ibuf[] = { 0 };

	if (dvb_usb_generic_rw(d->dev, obuf, 1, ibuf, 1, 0) < 0)
		err("command 0x51 transfer failed.");

	d->fe_adap[0].fe = dvb_attach(m88rs2000_attach, &s421_m88rs2000_config,
					&d->dev->i2c_adap);
1423

1424 1425 1426
	if (d->fe_adap[0].fe == NULL)
		return -EIO;

1427 1428 1429
	if (dvb_attach(ts2020_attach, d->fe_adap[0].fe,
				&dw2104_ts2020_config,
				&d->dev->i2c_adap)) {
1430
		info("Attached RS2000/TS2020!");
1431 1432
		return 0;
	}
1433

1434
	info("Failed to attach RS2000/TS2020!");
1435
	return -EIO;
1436 1437
}

1438 1439 1440 1441 1442 1443 1444 1445
static int tt_s2_4600_frontend_attach(struct dvb_usb_adapter *adap)
{
	struct dvb_usb_device *d = adap->dev;
	struct dw2102_state *state = d->priv;
	u8 obuf[3] = { 0xe, 0x80, 0 };
	u8 ibuf[] = { 0 };
	struct i2c_adapter *i2c_adapter;
	struct i2c_client *client;
1446 1447
	struct i2c_board_info board_info;
	struct m88ds3103_platform_data m88ds3103_pdata = {};
1448
	struct ts2020_config ts2020_config = {};
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479

	if (dvb_usb_generic_rw(d, obuf, 3, ibuf, 1, 0) < 0)
		err("command 0x0e transfer failed.");

	obuf[0] = 0xe;
	obuf[1] = 0x02;
	obuf[2] = 1;

	if (dvb_usb_generic_rw(d, obuf, 3, ibuf, 1, 0) < 0)
		err("command 0x0e transfer failed.");
	msleep(300);

	obuf[0] = 0xe;
	obuf[1] = 0x83;
	obuf[2] = 0;

	if (dvb_usb_generic_rw(d, obuf, 3, ibuf, 1, 0) < 0)
		err("command 0x0e transfer failed.");

	obuf[0] = 0xe;
	obuf[1] = 0x83;
	obuf[2] = 1;

	if (dvb_usb_generic_rw(d, obuf, 3, ibuf, 1, 0) < 0)
		err("command 0x0e transfer failed.");

	obuf[0] = 0x51;

	if (dvb_usb_generic_rw(d, obuf, 1, ibuf, 1, 0) < 0)
		err("command 0x51 transfer failed.");

1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499
	/* attach demod */
	m88ds3103_pdata.clk = 27000000;
	m88ds3103_pdata.i2c_wr_max = 33;
	m88ds3103_pdata.ts_mode = M88DS3103_TS_CI;
	m88ds3103_pdata.ts_clk = 16000;
	m88ds3103_pdata.ts_clk_pol = 0;
	m88ds3103_pdata.spec_inv = 0;
	m88ds3103_pdata.agc = 0x99;
	m88ds3103_pdata.agc_inv = 0;
	m88ds3103_pdata.clk_out = M88DS3103_CLOCK_OUT_ENABLED;
	m88ds3103_pdata.envelope_mode = 0;
	m88ds3103_pdata.lnb_hv_pol = 1;
	m88ds3103_pdata.lnb_en_pol = 0;
	memset(&board_info, 0, sizeof(board_info));
	strlcpy(board_info.type, "m88ds3103", I2C_NAME_SIZE);
	board_info.addr = 0x68;
	board_info.platform_data = &m88ds3103_pdata;
	request_module("m88ds3103");
	client = i2c_new_device(&d->i2c_adap, &board_info);
	if (client == NULL || client->dev.driver == NULL)
1500
		return -ENODEV;
1501 1502 1503 1504 1505 1506 1507 1508
	if (!try_module_get(client->dev.driver->owner)) {
		i2c_unregister_device(client);
		return -ENODEV;
	}
	adap->fe_adap[0].fe = m88ds3103_pdata.get_dvb_frontend(client);
	i2c_adapter = m88ds3103_pdata.get_i2c_adapter(client);

	state->i2c_client_demod = client;
1509 1510

	/* attach tuner */
1511
	ts2020_config.fe = adap->fe_adap[0].fe;
1512 1513 1514 1515
	memset(&board_info, 0, sizeof(board_info));
	strlcpy(board_info.type, "ts2022", I2C_NAME_SIZE);
	board_info.addr = 0x60;
	board_info.platform_data = &ts2020_config;
1516
	request_module("ts2020");
1517
	client = i2c_new_device(i2c_adapter, &board_info);
1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535

	if (client == NULL || client->dev.driver == NULL) {
		dvb_frontend_detach(adap->fe_adap[0].fe);
		return -ENODEV;
	}

	if (!try_module_get(client->dev.driver->owner)) {
		i2c_unregister_device(client);
		dvb_frontend_detach(adap->fe_adap[0].fe);
		return -ENODEV;
	}

	/* delegate signal strength measurement to tuner */
	adap->fe_adap[0].fe->ops.read_signal_strength =
			adap->fe_adap[0].fe->ops.tuner_ops.get_rf_strength;

	state->i2c_client_tuner = client;

1536 1537 1538 1539 1540 1541
	/* hook fe: need to resync the slave fifo when signal locks */
	state->fe_read_status = adap->fe_adap[0].fe->ops.read_status;
	adap->fe_adap[0].fe->ops.read_status = tt_s2_4600_read_status;

	state->last_lock = 0;

1542 1543 1544
	return 0;
}

1545 1546
static int dw2102_tuner_attach(struct dvb_usb_adapter *adap)
{
1547
	dvb_attach(dvb_pll_attach, adap->fe_adap[0].fe, 0x60,
1548 1549 1550 1551
		&adap->dev->i2c_adap, DVB_PLL_OPERA1);
	return 0;
}

1552 1553
static int dw3101_tuner_attach(struct dvb_usb_adapter *adap)
{
1554
	dvb_attach(dvb_pll_attach, adap->fe_adap[0].fe, 0x60,
1555 1556 1557 1558 1559
		&adap->dev->i2c_adap, DVB_PLL_TUA6034);

	return 0;
}

1560 1561 1562 1563 1564 1565 1566 1567 1568
static int dw2102_rc_query(struct dvb_usb_device *d)
{
	u8 key[2];
	struct i2c_msg msg = {
		.addr = DW2102_RC_QUERY,
		.flags = I2C_M_RD,
		.buf = key,
		.len = 2
	};
1569

1570 1571 1572 1573
	if (d->props.i2c_algo->master_xfer(&d->i2c_adap, &msg, 1) == 1) {
		if (msg.buf[0] != 0xff) {
			deb_rc("%s: rc code: %x, %x\n",
					__func__, key[0], key[1]);
1574
			rc_keydown(d->rc_dev, RC_TYPE_UNKNOWN, key[0], 0);
1575 1576
		}
	}
1577

1578 1579
	return 0;
}
1580

1581
static int prof_rc_query(struct dvb_usb_device *d)
1582 1583
{
	u8 key[2];
1584 1585 1586 1587 1588
	struct i2c_msg msg = {
		.addr = DW2102_RC_QUERY,
		.flags = I2C_M_RD,
		.buf = key,
		.len = 2
1589
	};
1590

1591 1592 1593 1594
	if (d->props.i2c_algo->master_xfer(&d->i2c_adap, &msg, 1) == 1) {
		if (msg.buf[0] != 0xff) {
			deb_rc("%s: rc code: %x, %x\n",
					__func__, key[0], key[1]);
1595
			rc_keydown(d->rc_dev, RC_TYPE_UNKNOWN, key[0]^0xff, 0);
1596
		}
1597
	}
1598

1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
	return 0;
}

static int su3000_rc_query(struct dvb_usb_device *d)
{
	u8 key[2];
	struct i2c_msg msg = {
		.addr = DW2102_RC_QUERY,
		.flags = I2C_M_RD,
		.buf = key,
		.len = 2
	};
1611

1612 1613 1614 1615
	if (d->props.i2c_algo->master_xfer(&d->i2c_adap, &msg, 1) == 1) {
		if (msg.buf[0] != 0xff) {
			deb_rc("%s: rc code: %x, %x\n",
					__func__, key[0], key[1]);
1616 1617
			rc_keydown(d->rc_dev, RC_TYPE_RC5,
				   RC_SCANCODE_RC5(key[1], key[0]), 0);
1618
		}
1619
	}
1620

1621 1622 1623
	return 0;
}

1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639
enum dw2102_table_entry {
	CYPRESS_DW2102,
	CYPRESS_DW2101,
	CYPRESS_DW2104,
	TEVII_S650,
	TERRATEC_CINERGY_S,
	CYPRESS_DW3101,
	TEVII_S630,
	PROF_1100,
	TEVII_S660,
	PROF_7500,
	GENIATECH_SU3000,
	TERRATEC_CINERGY_S2,
	TEVII_S480_1,
	TEVII_S480_2,
	X3M_SPC1400HD,
1640 1641
	TEVII_S421,
	TEVII_S632,
1642
	TERRATEC_CINERGY_S2_R2,
1643
	TERRATEC_CINERGY_S2_R3,
1644
	GOTVIEW_SAT_HD,
1645
	GENIATECH_T220,
1646
	TECHNOTREND_S2_4600,
1647 1648
	TEVII_S482_1,
	TEVII_S482_2,
1649
	TERRATEC_CINERGY_S2_BOX,
1650
	TEVII_S662
1651 1652
};

1653
static struct usb_device_id dw2102_table[] = {
1654 1655 1656 1657
	[CYPRESS_DW2102] = {USB_DEVICE(USB_VID_CYPRESS, USB_PID_DW2102)},
	[CYPRESS_DW2101] = {USB_DEVICE(USB_VID_CYPRESS, 0x2101)},
	[CYPRESS_DW2104] = {USB_DEVICE(USB_VID_CYPRESS, USB_PID_DW2104)},
	[TEVII_S650] = {USB_DEVICE(0x9022, USB_PID_TEVII_S650)},
1658
	[TERRATEC_CINERGY_S] = {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_S)},
1659 1660 1661 1662 1663 1664
	[CYPRESS_DW3101] = {USB_DEVICE(USB_VID_CYPRESS, USB_PID_DW3101)},
	[TEVII_S630] = {USB_DEVICE(0x9022, USB_PID_TEVII_S630)},
	[PROF_1100] = {USB_DEVICE(0x3011, USB_PID_PROF_1100)},
	[TEVII_S660] = {USB_DEVICE(0x9022, USB_PID_TEVII_S660)},
	[PROF_7500] = {USB_DEVICE(0x3034, 0x7500)},
	[GENIATECH_SU3000] = {USB_DEVICE(0x1f4d, 0x3000)},
1665
	[TERRATEC_CINERGY_S2] = {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_S2_R1)},
1666 1667 1668
	[TEVII_S480_1] = {USB_DEVICE(0x9022, USB_PID_TEVII_S480_1)},
	[TEVII_S480_2] = {USB_DEVICE(0x9022, USB_PID_TEVII_S480_2)},
	[X3M_SPC1400HD] = {USB_DEVICE(0x1f4d, 0x3100)},
1669 1670
	[TEVII_S421] = {USB_DEVICE(0x9022, USB_PID_TEVII_S421)},
	[TEVII_S632] = {USB_DEVICE(0x9022, USB_PID_TEVII_S632)},
1671
	[TERRATEC_CINERGY_S2_R2] = {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_S2_R2)},
1672
	[TERRATEC_CINERGY_S2_R3] = {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_S2_R3)},
1673
	[GOTVIEW_SAT_HD] = {USB_DEVICE(0x1FE1, USB_PID_GOTVIEW_SAT_HD)},
1674
	[GENIATECH_T220] = {USB_DEVICE(0x1f4d, 0xD220)},
1675 1676
	[TECHNOTREND_S2_4600] = {USB_DEVICE(USB_VID_TECHNOTREND,
		USB_PID_TECHNOTREND_CONNECT_S2_4600)},
1677 1678
	[TEVII_S482_1] = {USB_DEVICE(0x9022, 0xd483)},
	[TEVII_S482_2] = {USB_DEVICE(0x9022, 0xd484)},
1679
	[TERRATEC_CINERGY_S2_BOX] = {USB_DEVICE(USB_VID_TERRATEC, 0x0105)},
1680
	[TEVII_S662] = {USB_DEVICE(0x9022, USB_PID_TEVII_S662)},
1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691
	{ }
};

MODULE_DEVICE_TABLE(usb, dw2102_table);

static int dw2102_load_firmware(struct usb_device *dev,
			const struct firmware *frmwr)
{
	u8 *b, *p;
	int ret = 0, i;
	u8 reset;
1692
	u8 reset16[] = {0, 0, 0, 0, 0, 0, 0};
1693
	const struct firmware *fw;
1694

1695
	switch (le16_to_cpu(dev->descriptor.idProduct)) {
1696
	case 0x2101:
1697
		ret = request_firmware(&fw, DW2101_FIRMWARE, &dev->dev);
1698
		if (ret != 0) {
1699
			err(err_str, DW2101_FIRMWARE);
1700 1701 1702
			return ret;
		}
		break;
1703
	default:
1704 1705 1706
		fw = frmwr;
		break;
	}
1707
	info("start downloading DW210X firmware");
1708 1709 1710
	p = kmalloc(fw->size, GFP_KERNEL);
	reset = 1;
	/*stop the CPU*/
1711 1712
	dw210x_op_rw(dev, 0xa0, 0x7f92, 0, &reset, 1, DW210X_WRITE_MSG);
	dw210x_op_rw(dev, 0xa0, 0xe600, 0, &reset, 1, DW210X_WRITE_MSG);
1713 1714 1715 1716 1717

	if (p != NULL) {
		memcpy(p, fw->data, fw->size);
		for (i = 0; i < fw->size; i += 0x40) {
			b = (u8 *) p + i;
1718 1719
			if (dw210x_op_rw(dev, 0xa0, i, 0, b , 0x40,
					DW210X_WRITE_MSG) != 0x40) {
1720 1721 1722 1723 1724 1725 1726
				err("error while transferring firmware");
				ret = -EINVAL;
				break;
			}
		}
		/* restart the CPU */
		reset = 0;
1727 1728
		if (ret || dw210x_op_rw(dev, 0xa0, 0x7f92, 0, &reset, 1,
					DW210X_WRITE_MSG) != 1) {
1729 1730 1731
			err("could not restart the USB controller CPU.");
			ret = -EINVAL;
		}
1732 1733
		if (ret || dw210x_op_rw(dev, 0xa0, 0xe600, 0, &reset, 1,
					DW210X_WRITE_MSG) != 1) {
1734 1735 1736 1737
			err("could not restart the USB controller CPU.");
			ret = -EINVAL;
		}
		/* init registers */
1738
		switch (le16_to_cpu(dev->descriptor.idProduct)) {
1739
		case USB_PID_TEVII_S650:
1740
			dw2104_properties.rc.core.rc_codes = RC_MAP_TEVII_NEC;
1741
		case USB_PID_DW2104:
1742
			reset = 1;
1743 1744
			dw210x_op_rw(dev, 0xc4, 0x0000, 0, &reset, 1,
					DW210X_WRITE_MSG);
1745 1746
			/* break omitted intentionally */
		case USB_PID_DW3101:
1747
			reset = 0;
1748 1749
			dw210x_op_rw(dev, 0xbf, 0x0040, 0, &reset, 0,
					DW210X_WRITE_MSG);
1750
			break;
1751
		case USB_PID_TERRATEC_CINERGY_S:
1752
		case USB_PID_DW2102:
1753 1754 1755 1756 1757 1758 1759
			dw210x_op_rw(dev, 0xbf, 0x0040, 0, &reset, 0,
					DW210X_WRITE_MSG);
			dw210x_op_rw(dev, 0xb9, 0x0000, 0, &reset16[0], 2,
					DW210X_READ_MSG);
			/* check STV0299 frontend  */
			dw210x_op_rw(dev, 0xb5, 0, 0, &reset16[0], 2,
					DW210X_READ_MSG);
1760
			if ((reset16[0] == 0xa1) || (reset16[0] == 0x80)) {
1761
				dw2102_properties.i2c_algo = &dw2102_i2c_algo;
1762
				dw2102_properties.adapter->fe[0].tuner_attach = &dw2102_tuner_attach;
1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777
				break;
			} else {
				/* check STV0288 frontend  */
				reset16[0] = 0xd0;
				reset16[1] = 1;
				reset16[2] = 0;
				dw210x_op_rw(dev, 0xc2, 0, 0, &reset16[0], 3,
						DW210X_WRITE_MSG);
				dw210x_op_rw(dev, 0xc3, 0xd1, 0, &reset16[0], 3,
						DW210X_READ_MSG);
				if (reset16[2] == 0x11) {
					dw2102_properties.i2c_algo = &dw2102_earda_i2c_algo;
					break;
				}
			}
1778
		case 0x2101:
1779 1780 1781 1782 1783 1784 1785 1786
			dw210x_op_rw(dev, 0xbc, 0x0030, 0, &reset16[0], 2,
					DW210X_READ_MSG);
			dw210x_op_rw(dev, 0xba, 0x0000, 0, &reset16[0], 7,
					DW210X_READ_MSG);
			dw210x_op_rw(dev, 0xba, 0x0000, 0, &reset16[0], 7,
					DW210X_READ_MSG);
			dw210x_op_rw(dev, 0xb9, 0x0000, 0, &reset16[0], 2,
					DW210X_READ_MSG);
1787 1788
			break;
		}
1789

1790
		msleep(100);
1791 1792
		kfree(p);
	}
1793 1794 1795

	if (le16_to_cpu(dev->descriptor.idProduct) == 0x2101)
		release_firmware(fw);
1796 1797 1798 1799 1800 1801
	return ret;
}

static struct dvb_usb_device_properties dw2102_properties = {
	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
	.usb_ctrl = DEVICE_SPECIFIC,
1802
	.firmware = DW2102_FIRMWARE,
1803 1804
	.no_reconnect = 1,

1805
	.i2c_algo = &dw2102_serit_i2c_algo,
1806

1807
	.rc.core = {
1808
		.rc_interval = 150,
1809 1810 1811
		.rc_codes = RC_MAP_DM1105_NEC,
		.module_name = "dw2102",
		.allowed_protos   = RC_BIT_NEC,
1812 1813
		.rc_query = dw2102_rc_query,
	},
1814 1815 1816 1817 1818

	.generic_bulk_ctrl_endpoint = 0x81,
	/* parameter for the MPEG2-data transfer */
	.num_adapters = 1,
	.download_firmware = dw2102_load_firmware,
1819
	.read_mac_address = dw210x_read_mac_address,
1820
	.adapter = {
1821
		{
1822 1823
		.num_frontends = 1,
		.fe = {{
1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834
			.frontend_attach = dw2102_frontend_attach,
			.stream = {
				.type = USB_BULK,
				.count = 8,
				.endpoint = 0x82,
				.u = {
					.bulk = {
						.buffersize = 4096,
					}
				}
			},
1835
		}},
1836 1837
		}
	},
1838
	.num_device_descs = 3,
1839 1840
	.devices = {
		{"DVBWorld DVB-S 2102 USB2.0",
1841
			{&dw2102_table[CYPRESS_DW2102], NULL},
1842 1843 1844
			{NULL},
		},
		{"DVBWorld DVB-S 2101 USB2.0",
1845
			{&dw2102_table[CYPRESS_DW2101], NULL},
1846
			{NULL},
1847 1848
		},
		{"TerraTec Cinergy S USB",
1849
			{&dw2102_table[TERRATEC_CINERGY_S], NULL},
1850
			{NULL},
1851 1852 1853 1854
		},
	}
};

1855 1856 1857
static struct dvb_usb_device_properties dw2104_properties = {
	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
	.usb_ctrl = DEVICE_SPECIFIC,
1858
	.firmware = DW2104_FIRMWARE,
1859 1860 1861
	.no_reconnect = 1,

	.i2c_algo = &dw2104_i2c_algo,
1862
	.rc.core = {
1863
		.rc_interval = 150,
1864 1865 1866
		.rc_codes = RC_MAP_DM1105_NEC,
		.module_name = "dw2102",
		.allowed_protos   = RC_BIT_NEC,
1867 1868
		.rc_query = dw2102_rc_query,
	},
1869 1870 1871 1872 1873

	.generic_bulk_ctrl_endpoint = 0x81,
	/* parameter for the MPEG2-data transfer */
	.num_adapters = 1,
	.download_firmware = dw2102_load_firmware,
1874
	.read_mac_address = dw210x_read_mac_address,
1875 1876
	.adapter = {
		{
1877 1878
		.num_frontends = 1,
		.fe = {{
1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889
			.frontend_attach = dw2104_frontend_attach,
			.stream = {
				.type = USB_BULK,
				.count = 8,
				.endpoint = 0x82,
				.u = {
					.bulk = {
						.buffersize = 4096,
					}
				}
			},
1890
		}},
1891 1892 1893 1894 1895
		}
	},
	.num_device_descs = 2,
	.devices = {
		{ "DVBWorld DW2104 USB2.0",
1896
			{&dw2102_table[CYPRESS_DW2104], NULL},
1897 1898 1899
			{NULL},
		},
		{ "TeVii S650 USB2.0",
1900
			{&dw2102_table[TEVII_S650], NULL},
1901 1902 1903 1904 1905
			{NULL},
		},
	}
};

1906 1907 1908
static struct dvb_usb_device_properties dw3101_properties = {
	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
	.usb_ctrl = DEVICE_SPECIFIC,
1909
	.firmware = DW3101_FIRMWARE,
1910 1911 1912
	.no_reconnect = 1,

	.i2c_algo = &dw3101_i2c_algo,
1913
	.rc.core = {
1914
		.rc_interval = 150,
1915 1916 1917
		.rc_codes = RC_MAP_DM1105_NEC,
		.module_name = "dw2102",
		.allowed_protos   = RC_BIT_NEC,
1918 1919
		.rc_query = dw2102_rc_query,
	},
1920 1921 1922 1923 1924 1925 1926 1927

	.generic_bulk_ctrl_endpoint = 0x81,
	/* parameter for the MPEG2-data transfer */
	.num_adapters = 1,
	.download_firmware = dw2102_load_firmware,
	.read_mac_address = dw210x_read_mac_address,
	.adapter = {
		{
1928 1929
		.num_frontends = 1,
		.fe = {{
1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941
			.frontend_attach = dw3101_frontend_attach,
			.tuner_attach = dw3101_tuner_attach,
			.stream = {
				.type = USB_BULK,
				.count = 8,
				.endpoint = 0x82,
				.u = {
					.bulk = {
						.buffersize = 4096,
					}
				}
			},
1942
		}},
1943 1944 1945 1946 1947
		}
	},
	.num_device_descs = 1,
	.devices = {
		{ "DVBWorld DVB-C 3101 USB2.0",
1948
			{&dw2102_table[CYPRESS_DW3101], NULL},
1949 1950 1951 1952 1953
			{NULL},
		},
	}
};

1954
static struct dvb_usb_device_properties s6x0_properties = {
1955 1956
	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
	.usb_ctrl = DEVICE_SPECIFIC,
1957
	.size_of_priv = sizeof(struct dw2102_state),
1958
	.firmware = S630_FIRMWARE,
1959 1960
	.no_reconnect = 1,

1961
	.i2c_algo = &s6x0_i2c_algo,
1962
	.rc.core = {
1963
		.rc_interval = 150,
1964 1965 1966
		.rc_codes = RC_MAP_TEVII_NEC,
		.module_name = "dw2102",
		.allowed_protos   = RC_BIT_NEC,
1967 1968
		.rc_query = dw2102_rc_query,
	},
1969 1970 1971 1972

	.generic_bulk_ctrl_endpoint = 0x81,
	.num_adapters = 1,
	.download_firmware = dw2102_load_firmware,
1973
	.read_mac_address = s6x0_read_mac_address,
1974 1975
	.adapter = {
		{
1976 1977
		.num_frontends = 1,
		.fe = {{
1978
			.frontend_attach = zl100313_frontend_attach,
1979 1980 1981 1982 1983 1984 1985 1986 1987 1988
			.stream = {
				.type = USB_BULK,
				.count = 8,
				.endpoint = 0x82,
				.u = {
					.bulk = {
						.buffersize = 4096,
					}
				}
			},
1989
		}},
1990 1991
		}
	},
1992
	.num_device_descs = 1,
1993 1994
	.devices = {
		{"TeVii S630 USB",
1995
			{&dw2102_table[TEVII_S630], NULL},
1996 1997 1998 1999 2000
			{NULL},
		},
	}
};

2001
static struct dvb_usb_device_properties *p1100;
2002 2003
static struct dvb_usb_device_description d1100 = {
	"Prof 1100 USB ",
2004
	{&dw2102_table[PROF_1100], NULL},
2005 2006 2007
	{NULL},
};

2008
static struct dvb_usb_device_properties *s660;
2009 2010
static struct dvb_usb_device_description d660 = {
	"TeVii S660 USB",
2011
	{&dw2102_table[TEVII_S660], NULL},
2012 2013 2014
	{NULL},
};

2015 2016
static struct dvb_usb_device_description d480_1 = {
	"TeVii S480.1 USB",
2017
	{&dw2102_table[TEVII_S480_1], NULL},
2018 2019 2020 2021 2022
	{NULL},
};

static struct dvb_usb_device_description d480_2 = {
	"TeVii S480.2 USB",
2023
	{&dw2102_table[TEVII_S480_2], NULL},
2024 2025 2026
	{NULL},
};

2027
static struct dvb_usb_device_properties *p7500;
2028 2029
static struct dvb_usb_device_description d7500 = {
	"Prof 7500 USB DVB-S2",
2030
	{&dw2102_table[PROF_7500], NULL},
2031 2032 2033
	{NULL},
};

2034
static struct dvb_usb_device_properties *s421;
2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046
static struct dvb_usb_device_description d421 = {
	"TeVii S421 PCI",
	{&dw2102_table[TEVII_S421], NULL},
	{NULL},
};

static struct dvb_usb_device_description d632 = {
	"TeVii S632 USB",
	{&dw2102_table[TEVII_S632], NULL},
	{NULL},
};

2047 2048 2049
static struct dvb_usb_device_properties su3000_properties = {
	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
	.usb_ctrl = DEVICE_SPECIFIC,
2050
	.size_of_priv = sizeof(struct dw2102_state),
2051 2052 2053 2054 2055
	.power_ctrl = su3000_power_ctrl,
	.num_adapters = 1,
	.identify_state	= su3000_identify_state,
	.i2c_algo = &su3000_i2c_algo,

2056
	.rc.core = {
2057
		.rc_interval = 150,
2058 2059 2060 2061
		.rc_codes = RC_MAP_SU3000,
		.module_name = "dw2102",
		.allowed_protos   = RC_BIT_RC5,
		.rc_query = su3000_rc_query,
2062 2063 2064 2065 2066 2067 2068 2069
	},

	.read_mac_address = su3000_read_mac_address,

	.generic_bulk_ctrl_endpoint = 0x01,

	.adapter = {
		{
2070 2071
		.num_frontends = 1,
		.fe = {{
2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083
			.streaming_ctrl   = su3000_streaming_ctrl,
			.frontend_attach  = su3000_frontend_attach,
			.stream = {
				.type = USB_BULK,
				.count = 8,
				.endpoint = 0x82,
				.u = {
					.bulk = {
						.buffersize = 4096,
					}
				}
			}
2084
		}},
2085 2086
		}
	},
2087
	.num_device_descs = 6,
2088 2089
	.devices = {
		{ "SU3000HD DVB-S USB2.0",
2090
			{ &dw2102_table[GENIATECH_SU3000], NULL },
2091 2092
			{ NULL },
		},
2093
		{ "Terratec Cinergy S2 USB HD",
2094
			{ &dw2102_table[TERRATEC_CINERGY_S2], NULL },
2095 2096
			{ NULL },
		},
2097
		{ "X3M TV SPC1400HD PCI",
2098
			{ &dw2102_table[X3M_SPC1400HD], NULL },
2099 2100
			{ NULL },
		},
2101 2102 2103 2104
		{ "Terratec Cinergy S2 USB HD Rev.2",
			{ &dw2102_table[TERRATEC_CINERGY_S2_R2], NULL },
			{ NULL },
		},
2105 2106 2107 2108
		{ "Terratec Cinergy S2 USB HD Rev.3",
			{ &dw2102_table[TERRATEC_CINERGY_S2_R3], NULL },
			{ NULL },
		},
2109 2110 2111 2112
		{ "GOTVIEW Satellite HD",
			{ &dw2102_table[GOTVIEW_SAT_HD], NULL },
			{ NULL },
		},
2113 2114 2115
	}
};

2116 2117 2118
static struct dvb_usb_device_properties t220_properties = {
	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
	.usb_ctrl = DEVICE_SPECIFIC,
2119
	.size_of_priv = sizeof(struct dw2102_state),
2120 2121 2122 2123 2124
	.power_ctrl = su3000_power_ctrl,
	.num_adapters = 1,
	.identify_state	= su3000_identify_state,
	.i2c_algo = &su3000_i2c_algo,

2125
	.rc.core = {
2126
		.rc_interval = 150,
2127 2128 2129 2130
		.rc_codes = RC_MAP_SU3000,
		.module_name = "dw2102",
		.allowed_protos   = RC_BIT_RC5,
		.rc_query = su3000_rc_query,
2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164
	},

	.read_mac_address = su3000_read_mac_address,

	.generic_bulk_ctrl_endpoint = 0x01,

	.adapter = {
		{
		.num_frontends = 1,
		.fe = { {
			.streaming_ctrl   = su3000_streaming_ctrl,
			.frontend_attach  = t220_frontend_attach,
			.stream = {
				.type = USB_BULK,
				.count = 8,
				.endpoint = 0x82,
				.u = {
					.bulk = {
						.buffersize = 4096,
					}
				}
			}
		} },
		}
	},
	.num_device_descs = 1,
	.devices = {
		{ "Geniatech T220 DVB-T/T2 USB2.0",
			{ &dw2102_table[GENIATECH_T220], NULL },
			{ NULL },
		},
	}
};

2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204
static struct dvb_usb_device_properties tt_s2_4600_properties = {
	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
	.usb_ctrl = DEVICE_SPECIFIC,
	.size_of_priv = sizeof(struct dw2102_state),
	.power_ctrl = su3000_power_ctrl,
	.num_adapters = 1,
	.identify_state	= su3000_identify_state,
	.i2c_algo = &su3000_i2c_algo,

	.rc.core = {
		.rc_interval = 250,
		.rc_codes = RC_MAP_TT_1500,
		.module_name = "dw2102",
		.allowed_protos   = RC_BIT_RC5,
		.rc_query = su3000_rc_query,
	},

	.read_mac_address = su3000_read_mac_address,

	.generic_bulk_ctrl_endpoint = 0x01,

	.adapter = {
		{
		.num_frontends = 1,
		.fe = {{
			.streaming_ctrl   = su3000_streaming_ctrl,
			.frontend_attach  = tt_s2_4600_frontend_attach,
			.stream = {
				.type = USB_BULK,
				.count = 8,
				.endpoint = 0x82,
				.u = {
					.bulk = {
						.buffersize = 4096,
					}
				}
			}
		} },
		}
	},
2205
	.num_device_descs = 5,
2206 2207 2208 2209 2210
	.devices = {
		{ "TechnoTrend TT-connect S2-4600",
			{ &dw2102_table[TECHNOTREND_S2_4600], NULL },
			{ NULL },
		},
2211 2212 2213 2214 2215 2216 2217 2218
		{ "TeVii S482 (tuner 1)",
			{ &dw2102_table[TEVII_S482_1], NULL },
			{ NULL },
		},
		{ "TeVii S482 (tuner 2)",
			{ &dw2102_table[TEVII_S482_2], NULL },
			{ NULL },
		},
2219 2220 2221 2222
		{ "Terratec Cinergy S2 USB BOX",
			{ &dw2102_table[TERRATEC_CINERGY_S2_BOX], NULL },
			{ NULL },
		},
2223 2224 2225 2226
		{ "TeVii S662",
			{ &dw2102_table[TEVII_S662], NULL },
			{ NULL },
		},
2227 2228 2229
	}
};

2230 2231 2232
static int dw2102_probe(struct usb_interface *intf,
		const struct usb_device_id *id)
{
2233 2234
	p1100 = kmemdup(&s6x0_properties,
			sizeof(struct dvb_usb_device_properties), GFP_KERNEL);
2235 2236 2237 2238
	if (!p1100)
		return -ENOMEM;
	/* copy default structure */
	/* fill only different fields */
2239
	p1100->firmware = P1100_FIRMWARE;
2240
	p1100->devices[0] = d1100;
2241 2242
	p1100->rc.core.rc_query = prof_rc_query;
	p1100->rc.core.rc_codes = RC_MAP_TBS_NEC;
2243
	p1100->adapter->fe[0].frontend_attach = stv0288_frontend_attach;
2244

2245 2246
	s660 = kmemdup(&s6x0_properties,
		       sizeof(struct dvb_usb_device_properties), GFP_KERNEL);
2247 2248 2249 2250
	if (!s660) {
		kfree(p1100);
		return -ENOMEM;
	}
2251
	s660->firmware = S660_FIRMWARE;
2252
	s660->num_device_descs = 3;
2253
	s660->devices[0] = d660;
2254 2255
	s660->devices[1] = d480_1;
	s660->devices[2] = d480_2;
2256
	s660->adapter->fe[0].frontend_attach = ds3000_frontend_attach;
2257

2258 2259
	p7500 = kmemdup(&s6x0_properties,
			sizeof(struct dvb_usb_device_properties), GFP_KERNEL);
2260 2261 2262
	if (!p7500) {
		kfree(p1100);
		kfree(s660);
2263
		return -ENOMEM;
2264
	}
2265
	p7500->firmware = P7500_FIRMWARE;
2266
	p7500->devices[0] = d7500;
2267 2268
	p7500->rc.core.rc_query = prof_rc_query;
	p7500->rc.core.rc_codes = RC_MAP_TBS_NEC;
2269
	p7500->adapter->fe[0].frontend_attach = prof_7500_frontend_attach;
2270

2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284

	s421 = kmemdup(&su3000_properties,
		       sizeof(struct dvb_usb_device_properties), GFP_KERNEL);
	if (!s421) {
		kfree(p1100);
		kfree(s660);
		kfree(p7500);
		return -ENOMEM;
	}
	s421->num_device_descs = 2;
	s421->devices[0] = d421;
	s421->devices[1] = d632;
	s421->adapter->fe[0].frontend_attach = m88rs2000_frontend_attach;

2285 2286 2287
	if (0 == dvb_usb_device_init(intf, &dw2102_properties,
			THIS_MODULE, NULL, adapter_nr) ||
	    0 == dvb_usb_device_init(intf, &dw2104_properties,
2288 2289
			THIS_MODULE, NULL, adapter_nr) ||
	    0 == dvb_usb_device_init(intf, &dw3101_properties,
2290
			THIS_MODULE, NULL, adapter_nr) ||
2291
	    0 == dvb_usb_device_init(intf, &s6x0_properties,
2292
			THIS_MODULE, NULL, adapter_nr) ||
2293 2294 2295 2296
	    0 == dvb_usb_device_init(intf, p1100,
			THIS_MODULE, NULL, adapter_nr) ||
	    0 == dvb_usb_device_init(intf, s660,
			THIS_MODULE, NULL, adapter_nr) ||
2297
	    0 == dvb_usb_device_init(intf, p7500,
2298
			THIS_MODULE, NULL, adapter_nr) ||
2299 2300
	    0 == dvb_usb_device_init(intf, s421,
			THIS_MODULE, NULL, adapter_nr) ||
2301
	    0 == dvb_usb_device_init(intf, &su3000_properties,
2302 2303
			 THIS_MODULE, NULL, adapter_nr) ||
	    0 == dvb_usb_device_init(intf, &t220_properties,
2304 2305
			 THIS_MODULE, NULL, adapter_nr) ||
	    0 == dvb_usb_device_init(intf, &tt_s2_4600_properties,
2306
			 THIS_MODULE, NULL, adapter_nr))
2307
		return 0;
2308

2309
	return -ENODEV;
2310 2311
}

2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324
static void dw2102_disconnect(struct usb_interface *intf)
{
	struct dvb_usb_device *d = usb_get_intfdata(intf);
	struct dw2102_state *st = (struct dw2102_state *)d->priv;
	struct i2c_client *client;

	/* remove I2C client for tuner */
	client = st->i2c_client_tuner;
	if (client) {
		module_put(client->dev.driver->owner);
		i2c_unregister_device(client);
	}

2325 2326 2327 2328 2329 2330 2331
	/* remove I2C client for demodulator */
	client = st->i2c_client_demod;
	if (client) {
		module_put(client->dev.driver->owner);
		i2c_unregister_device(client);
	}

2332 2333 2334
	dvb_usb_device_exit(intf);
}

2335 2336 2337
static struct usb_driver dw2102_driver = {
	.name = "dw2102",
	.probe = dw2102_probe,
2338
	.disconnect = dw2102_disconnect,
2339 2340 2341
	.id_table = dw2102_table,
};

2342
module_usb_driver(dw2102_driver);
2343 2344

MODULE_AUTHOR("Igor M. Liplianin (c) liplianin@me.by");
2345
MODULE_DESCRIPTION("Driver for DVBWorld DVB-S 2101, 2102, DVB-S2 2104, DVB-C 3101 USB2.0, TeVii S421, S480, S482, S600, S630, S632, S650, TeVii S660, S662, Prof 1100, 7500 USB2.0, Geniatech SU3000, T220, TechnoTrend S2-4600, Terratec Cinergy S2 devices");
2346 2347
MODULE_VERSION("0.1");
MODULE_LICENSE("GPL");
2348 2349 2350 2351 2352 2353 2354 2355
MODULE_FIRMWARE(DW2101_FIRMWARE);
MODULE_FIRMWARE(DW2102_FIRMWARE);
MODULE_FIRMWARE(DW2104_FIRMWARE);
MODULE_FIRMWARE(DW3101_FIRMWARE);
MODULE_FIRMWARE(S630_FIRMWARE);
MODULE_FIRMWARE(S660_FIRMWARE);
MODULE_FIRMWARE(P1100_FIRMWARE);
MODULE_FIRMWARE(P7500_FIRMWARE);