dw2102.c 51.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 S600, S630, S650, S660, S480, S421, S632
4
 *	Prof 1100, 7500,
5
 *	Geniatech SU3000 Cards
6
 * Copyright (C) 2008-2012 Igor M. Liplianin (liplianin@me.by)
7 8 9 10 11 12 13
 *
 *	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
 */
14
#include "dw2102.h"
15
#include "si21xx.h"
16 17
#include "stv0299.h"
#include "z0194a.h"
18 19 20
#include "stv0288.h"
#include "stb6000.h"
#include "eds1547.h"
21
#include "cx24116.h"
22
#include "tda1002x.h"
23 24
#include "mt312.h"
#include "zl10039.h"
25
#include "ts2020.h"
26 27 28 29 30
#include "ds3000.h"
#include "stv0900.h"
#include "stv6110.h"
#include "stb6100.h"
#include "stb6100_proc.h"
31
#include "m88rs2000.h"
32

33 34 35
/* Max transfer size done by I2C transfer functions */
#define MAX_XFER_SIZE  64

36 37 38 39
#ifndef USB_PID_DW2102
#define USB_PID_DW2102 0x2102
#endif

40 41 42 43
#ifndef USB_PID_DW2104
#define USB_PID_DW2104 0x2104
#endif

44 45 46 47
#ifndef USB_PID_DW3101
#define USB_PID_DW3101 0x3101
#endif

48 49 50 51
#ifndef USB_PID_CINERGY_S
#define USB_PID_CINERGY_S 0x0064
#endif

52 53 54 55
#ifndef USB_PID_TEVII_S630
#define USB_PID_TEVII_S630 0xd630
#endif

56 57 58 59
#ifndef USB_PID_TEVII_S650
#define USB_PID_TEVII_S650 0xd650
#endif

60 61 62 63
#ifndef USB_PID_TEVII_S660
#define USB_PID_TEVII_S660 0xd660
#endif

64 65 66 67 68 69 70 71
#ifndef USB_PID_TEVII_S480_1
#define USB_PID_TEVII_S480_1 0xd481
#endif

#ifndef USB_PID_TEVII_S480_2
#define USB_PID_TEVII_S480_2 0xd482
#endif

72 73
#ifndef USB_PID_PROF_1100
#define USB_PID_PROF_1100 0xb012
74 75
#endif

76 77 78 79 80 81 82 83
#ifndef USB_PID_TEVII_S421
#define USB_PID_TEVII_S421 0xd421
#endif

#ifndef USB_PID_TEVII_S632
#define USB_PID_TEVII_S632 0xd632
#endif

84 85 86 87
#ifndef USB_PID_GOTVIEW_SAT_HD
#define USB_PID_GOTVIEW_SAT_HD 0x5456
#endif

88 89
#define DW210X_READ_MSG 0
#define DW210X_WRITE_MSG 1
90 91 92 93

#define REG_1F_SYMBOLRATE_BYTE0 0x1f
#define REG_20_SYMBOLRATE_BYTE1 0x20
#define REG_21_SYMBOLRATE_BYTE2 0x21
94
/* on my own*/
95
#define DW2102_VOLTAGE_CTRL (0x1800)
96
#define SU3000_STREAM_CTRL (0x1900)
97
#define DW2102_RC_QUERY (0x1a00)
98
#define DW2102_LED_CTRL (0x1b00)
99

100 101 102 103 104 105 106 107 108
#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"

109 110 111 112
#define	err_str "did not find the firmware file. (%s) " \
		"Please see linux/Documentation/dvb/ for more details " \
		"on firmware-problems."

113 114
struct rc_map_dvb_usb_table_table {
	struct rc_map_table *rc_keys;
115
	int rc_keys_size;
116 117
};

118 119 120 121
struct su3000_state {
	u8 initialized;
};

122 123 124 125
struct s6x0_state {
	int (*old_set_voltage)(struct dvb_frontend *f, fe_sec_voltage_t v);
};

126 127 128
/* debug */
static int dvb_usb_dw2102_debug;
module_param_named(debug, dvb_usb_dw2102_debug, int, 0644);
129 130 131
MODULE_PARM_DESC(debug, "set debugging level (1=info 2=xfer 4=rc(or-able))."
						DVB_USB_DEBUG_STATUS);

132 133 134
/* keymaps */
static int ir_keymap;
module_param_named(keymap, ir_keymap, int, 0644);
135 136
MODULE_PARM_DESC(keymap, "set keymap 0=default 1=dvbworld 2=tevii 3=tbs  ..."
			" 256=none");
137

138 139 140 141 142 143
/* demod probe */
static int demod_probe = 1;
module_param_named(demod, demod_probe, int, 0644);
MODULE_PARM_DESC(demod, "demod to probe (1=cx24116 2=stv0903+stv6110 "
			"4=stv0903+stb6100(or-able)).");

144 145
DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);

146
static int dw210x_op_rw(struct usb_device *dev, u8 request, u16 value,
147
			u16 index, u8 * data, u16 len, int flags)
148 149
{
	int ret;
150
	u8 *u8buf;
151
	unsigned int pipe = (flags == DW210X_READ_MSG) ?
152
				usb_rcvctrlpipe(dev, 0) : usb_sndctrlpipe(dev, 0);
153
	u8 request_type = (flags == DW210X_READ_MSG) ? USB_DIR_IN : USB_DIR_OUT;
154

155 156 157 158 159
	u8buf = kmalloc(len, GFP_KERNEL);
	if (!u8buf)
		return -ENOMEM;


160
	if (flags == DW210X_WRITE_MSG)
161
		memcpy(u8buf, data, len);
162 163
	ret = usb_control_msg(dev, pipe, request, request_type | USB_TYPE_VENDOR,
				value, index , u8buf, len, 2000);
164

165
	if (flags == DW210X_READ_MSG)
166
		memcpy(data, u8buf, len);
167 168

	kfree(u8buf);
169 170 171 172 173 174 175
	return ret;
}

/* I2C */
static int dw2102_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[],
		int num)
{
176
	struct dvb_usb_device *d = i2c_get_adapdata(adap);
177
	int i = 0;
178 179 180 181 182 183 184 185 186 187 188 189 190
	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++) {
191
			dw210x_op_rw(d->udev, 0xb5, value + i, 0,
192
					buf6, 2, DW210X_READ_MSG);
193 194 195 196 197 198 199 200 201 202
			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];
203
			dw210x_op_rw(d->udev, 0xb2, 0, 0,
204
					buf6, 3, DW210X_WRITE_MSG);
205 206 207 208 209 210 211 212 213 214 215
			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];
216
				dw210x_op_rw(d->udev, 0xb2, 0, 0,
217
						buf6, 7, DW210X_WRITE_MSG);
218
			} else {
219
			/* read from tuner */
220
				dw210x_op_rw(d->udev, 0xb5, 0, 0,
221
						buf6, 1, DW210X_READ_MSG);
222 223 224 225
				msg[0].buf[0] = buf6[0];
			}
			break;
		case (DW2102_RC_QUERY):
226
			dw210x_op_rw(d->udev, 0xb8, 0, 0,
227
					buf6, 2, DW210X_READ_MSG);
228 229 230 231 232 233
			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];
234
			dw210x_op_rw(d->udev, 0xb2, 0, 0,
235
					buf6, 2, DW210X_WRITE_MSG);
236 237 238 239 240 241 242 243 244 245
			break;
		}

		break;
	}

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

246 247 248 249 250 251 252 253 254 255 256 257 258 259
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 */
260
		buf6[0] = msg[0].addr << 1;
261 262
		buf6[1] = msg[0].len;
		buf6[2] = msg[0].buf[0];
263
		dw210x_op_rw(d->udev, 0xc2, 0, 0,
264 265
				buf6, msg[0].len + 2, DW210X_WRITE_MSG);
		/* read si2109 register */
266
		dw210x_op_rw(d->udev, 0xc3, 0xd0, 0,
267 268 269 270 271 272 273 274
				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 */
275
			buf6[0] = msg[0].addr << 1;
276 277
			buf6[1] = msg[0].len;
			memcpy(buf6 + 2, msg[0].buf, msg[0].len);
278
			dw210x_op_rw(d->udev, 0xc2, 0, 0, buf6,
279 280 281
					msg[0].len + 2, DW210X_WRITE_MSG);
			break;
		case(DW2102_RC_QUERY):
282
			dw210x_op_rw(d->udev, 0xb8, 0, 0,
283 284 285 286 287 288 289
					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];
290
			dw210x_op_rw(d->udev, 0xb2, 0, 0,
291 292 293 294 295 296 297 298 299
					buf6, 2, DW210X_WRITE_MSG);
			break;
		}
		break;
	}

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

301 302 303 304 305 306 307 308 309 310 311 312 313
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);

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

	switch (num) {
	case 2: {
		/* read */
		/* first write first register number */
314 315 316 317 318 319 320 321
		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);
			return -EOPNOTSUPP;
		}

322
		obuf[0] = msg[0].addr << 1;
323 324
		obuf[1] = msg[0].len;
		obuf[2] = msg[0].buf[0];
325
		dw210x_op_rw(d->udev, 0xc2, 0, 0,
326 327
				obuf, msg[0].len + 2, DW210X_WRITE_MSG);
		/* second read registers */
328
		dw210x_op_rw(d->udev, 0xc3, 0xd1 , 0,
329 330 331 332 333 334 335 336 337
				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 */
338 339 340 341 342 343 344 345
			u8 obuf[MAX_XFER_SIZE];

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

346
			obuf[0] = msg[0].addr << 1;
347 348
			obuf[1] = msg[0].len;
			memcpy(obuf + 2, msg[0].buf, msg[0].len);
349
			dw210x_op_rw(d->udev, 0xc2, 0, 0,
350 351 352 353 354
					obuf, msg[0].len + 2, DW210X_WRITE_MSG);
			break;
		}
		case 0x61: {
			/* write to tuner */
355 356 357 358 359 360 361 362
			u8 obuf[MAX_XFER_SIZE];

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

363
			obuf[0] = msg[0].addr << 1;
364 365
			obuf[1] = msg[0].len;
			memcpy(obuf + 2, msg[0].buf, msg[0].len);
366
			dw210x_op_rw(d->udev, 0xc2, 0, 0,
367 368 369 370 371
					obuf, msg[0].len + 2, DW210X_WRITE_MSG);
			break;
		}
		case(DW2102_RC_QUERY): {
			u8 ibuf[2];
372
			dw210x_op_rw(d->udev, 0xb8, 0, 0,
373 374 375 376 377 378 379 380
					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];
381
			dw210x_op_rw(d->udev, 0xb2, 0, 0,
382 383 384 385 386 387 388 389 390 391 392
					obuf, 2, DW210X_WRITE_MSG);
			break;
		}
		}

		break;
	}

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

394 395 396
static int dw2104_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[], int num)
{
	struct dvb_usb_device *d = i2c_get_adapdata(adap);
397
	int len, i, j;
398 399 400 401 402 403

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

404 405
	for (j = 0; j < num; j++) {
		switch (msg[j].addr) {
406 407
		case(DW2102_RC_QUERY): {
			u8 ibuf[2];
408
			dw210x_op_rw(d->udev, 0xb8, 0, 0,
409
					ibuf, 2, DW210X_READ_MSG);
410
			memcpy(msg[j].buf, ibuf , 2);
411 412 413 414 415
			break;
		}
		case(DW2102_VOLTAGE_CTRL): {
			u8 obuf[2];
			obuf[0] = 0x30;
416
			obuf[1] = msg[j].buf[0];
417
			dw210x_op_rw(d->udev, 0xb2, 0, 0,
418
					obuf, 2, DW210X_WRITE_MSG);
419 420
			break;
		}
421 422 423 424 425 426 427
		/*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 */
428 429 430 431 432 433 434 435
				u8  ibuf[MAX_XFER_SIZE];

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

436
				dw210x_op_rw(d->udev, 0xc3,
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
						(msg[j].addr << 1) + 1, 0,
						ibuf, msg[j].len + 2,
						DW210X_READ_MSG);
				memcpy(msg[j].buf, ibuf + 2, msg[j].len);
			mdelay(10);
			} 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));
456
					dw210x_op_rw(d->udev, 0xc2, 0, 0,
457 458 459 460 461 462 463
						obuf, (len > 16 ? 16 : len) + 3,
						DW210X_WRITE_MSG);
					i += 16;
					len -= 16;
				} while (len > 0);
			} else {
				/* write registers */
464 465 466 467 468 469 470 471
				u8 obuf[MAX_XFER_SIZE];

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

472 473 474
				obuf[0] = msg[j].addr << 1;
				obuf[1] = msg[j].len;
				memcpy(obuf + 2, msg[j].buf, msg[j].len);
475
				dw210x_op_rw(d->udev, 0xc2, 0, 0,
476 477 478 479 480
						obuf, msg[j].len + 2,
						DW210X_WRITE_MSG);
			}
			break;
		}
481 482 483 484 485 486 487 488
		}

	}

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

489 490 491 492
static int dw3101_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[],
								int num)
{
	struct dvb_usb_device *d = i2c_get_adapdata(adap);
493
	int i;
494 495 496 497 498 499 500 501 502 503

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

	switch (num) {
	case 2: {
		/* read */
		/* first write first register number */
504 505 506 507 508 509 510
		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);
			return -EOPNOTSUPP;
		}
511 512 513
		obuf[0] = msg[0].addr << 1;
		obuf[1] = msg[0].len;
		obuf[2] = msg[0].buf[0];
514
		dw210x_op_rw(d->udev, 0xc2, 0, 0,
515 516
				obuf, msg[0].len + 2, DW210X_WRITE_MSG);
		/* second read registers */
517
		dw210x_op_rw(d->udev, 0xc3, 0x19 , 0,
518 519 520 521 522 523 524 525 526 527
				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 */
528 529 530 531 532 533 534
			u8 obuf[MAX_XFER_SIZE];

			if (2 + msg[0].len > sizeof(obuf)) {
				warn("i2c wr: len=%d is too big!\n",
				     msg[0].len);
				return -EOPNOTSUPP;
			}
535 536 537
			obuf[0] = msg[0].addr << 1;
			obuf[1] = msg[0].len;
			memcpy(obuf + 2, msg[0].buf, msg[0].len);
538
			dw210x_op_rw(d->udev, 0xc2, 0, 0,
539 540 541 542 543
					obuf, msg[0].len + 2, DW210X_WRITE_MSG);
			break;
		}
		case(DW2102_RC_QUERY): {
			u8 ibuf[2];
544
			dw210x_op_rw(d->udev, 0xb8, 0, 0,
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
					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);
	}

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

564
static int s6x0_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[],
565 566 567
								int num)
{
	struct dvb_usb_device *d = i2c_get_adapdata(adap);
568
	struct usb_device *udev;
569
	int len, i, j;
570 571 572

	if (!d)
		return -ENODEV;
573
	udev = d->udev;
574 575 576
	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
		return -EAGAIN;

577 578
	for (j = 0; j < num; j++) {
		switch (msg[j].addr) {
579
		case (DW2102_RC_QUERY): {
580
			u8 ibuf[5];
581
			dw210x_op_rw(d->udev, 0xb8, 0, 0,
582 583
					ibuf, 5, DW210X_READ_MSG);
			memcpy(msg[j].buf, ibuf + 3, 2);
584 585 586 587
			break;
		}
		case (DW2102_VOLTAGE_CTRL): {
			u8 obuf[2];
588 589 590

			obuf[0] = 1;
			obuf[1] = msg[j].buf[1];/* off-on */
591
			dw210x_op_rw(d->udev, 0x8a, 0, 0,
592
					obuf, 2, DW210X_WRITE_MSG);
593
			obuf[0] = 3;
594
			obuf[1] = msg[j].buf[0];/* 13v-18v */
595
			dw210x_op_rw(d->udev, 0x8a, 0, 0,
596 597 598
					obuf, 2, DW210X_WRITE_MSG);
			break;
		}
599 600 601 602 603
		case (DW2102_LED_CTRL): {
			u8 obuf[2];

			obuf[0] = 5;
			obuf[1] = msg[j].buf[0];
604
			dw210x_op_rw(d->udev, 0x8a, 0, 0,
605 606 607
					obuf, 2, DW210X_WRITE_MSG);
			break;
		}
608 609
		/*case 0x55: cx24116
		case 0x6a: stv0903
610
		case 0x68: ds3000, stv0903, rs2000
611 612 613 614 615
		case 0x60: ts2020, stv6110, stb6100
		case 0xa0: eeprom */
		default: {
			if (msg[j].flags == I2C_M_RD) {
				/* read registers */
616 617 618 619 620 621 622 623
				u8 ibuf[MAX_XFER_SIZE];

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

624
				dw210x_op_rw(d->udev, 0x91, 0, 0,
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
						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));
642
					dw210x_op_rw(d->udev, 0x80, 0, 0,
643 644 645 646 647
						obuf, (len > 16 ? 16 : len) + 3,
						DW210X_WRITE_MSG);
					i += 16;
					len -= 16;
				} while (len > 0);
648
			} else if (j < (num - 1)) {
649
				/* write register addr before read */
650 651 652 653 654 655 656 657
				u8 obuf[MAX_XFER_SIZE];

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

658 659 660
				obuf[0] = msg[j + 1].len;
				obuf[1] = (msg[j].addr << 1);
				memcpy(obuf + 2, msg[j].buf, msg[j].len);
661
				dw210x_op_rw(d->udev,
662 663
						udev->descriptor.idProduct ==
						0x7500 ? 0x92 : 0x90, 0, 0,
664 665 666
						obuf, msg[j].len + 2,
						DW210X_WRITE_MSG);
				break;
667 668
			} else {
				/* write registers */
669 670 671 672 673 674 675
				u8 obuf[MAX_XFER_SIZE];

				if (2 + msg[j].len > sizeof(obuf)) {
					warn("i2c wr: len=%d is too big!\n",
					     msg[j].len);
					return -EOPNOTSUPP;
				}
676 677 678
				obuf[0] = msg[j].len + 1;
				obuf[1] = (msg[j].addr << 1);
				memcpy(obuf + 2, msg[j].buf, msg[j].len);
679
				dw210x_op_rw(d->udev, 0x80, 0, 0,
680 681 682 683 684 685
						obuf, msg[j].len + 2,
						DW210X_WRITE_MSG);
				break;
			}
			break;
		}
686 687 688 689 690 691 692
		}
	}

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

693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
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;
}

757
static u32 dw210x_i2c_func(struct i2c_adapter *adapter)
758 759 760 761 762 763
{
	return I2C_FUNC_I2C;
}

static struct i2c_algorithm dw2102_i2c_algo = {
	.master_xfer = dw2102_i2c_transfer,
764 765 766 767 768 769
	.functionality = dw210x_i2c_func,
};

static struct i2c_algorithm dw2102_serit_i2c_algo = {
	.master_xfer = dw2102_serit_i2c_transfer,
	.functionality = dw210x_i2c_func,
770 771
};

772 773 774 775 776
static struct i2c_algorithm dw2102_earda_i2c_algo = {
	.master_xfer = dw2102_earda_i2c_transfer,
	.functionality = dw210x_i2c_func,
};

777 778
static struct i2c_algorithm dw2104_i2c_algo = {
	.master_xfer = dw2104_i2c_transfer,
779
	.functionality = dw210x_i2c_func,
780 781
};

782 783 784 785 786
static struct i2c_algorithm dw3101_i2c_algo = {
	.master_xfer = dw3101_i2c_transfer,
	.functionality = dw210x_i2c_func,
};

787 788
static struct i2c_algorithm s6x0_i2c_algo = {
	.master_xfer = s6x0_i2c_transfer,
789 790 791
	.functionality = dw210x_i2c_func,
};

792 793 794 795 796
static struct i2c_algorithm su3000_i2c_algo = {
	.master_xfer = su3000_i2c_transfer,
	.functionality = dw210x_i2c_func,
};

797
static int dw210x_read_mac_address(struct dvb_usb_device *d, u8 mac[6])
798 799 800 801 802 803
{
	int i;
	u8 ibuf[] = {0, 0};
	u8 eeprom[256], eepromline[16];

	for (i = 0; i < 256; i++) {
804
		if (dw210x_op_rw(d->udev, 0xb6, 0xa0 , i, ibuf, 2, DW210X_READ_MSG) < 0) {
805 806 807 808 809 810 811 812 813 814 815
			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);
		}
	}
816

817 818 819 820
	memcpy(mac, eeprom + 8, 6);
	return 0;
};

821
static int s6x0_read_mac_address(struct dvb_usb_device *d, u8 mac[6])
822 823
{
	int i, ret;
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
	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,
		}
	};
839 840

	for (i = 0; i < 256; i++) {
841 842 843
		obuf[0] = i;
		ret = s6x0_i2c_transfer(&d->i2c_adap, msg, 2);
		if (ret != 2) {
844 845 846
			err("read eeprom failed.");
			return -1;
		} else {
847 848
			eepromline[i % 16] = ibuf[0];
			eeprom[i] = ibuf[0];
849 850 851 852 853 854 855 856 857 858 859 860
		}

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

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

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 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
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)
{
	struct su3000_state *state = (struct su3000_state *)d->priv;
	u8 obuf[] = {0xde, 0};

	info("%s: %d, initialized %d\n", __func__, i, state->initialized);

	if (i && !state->initialized) {
		state->initialized = 1;
		/* reset board */
		dvb_usb_generic_rw(d, obuf, 2, NULL, 0, 0);
	}

	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];

		debug_dump(mac, 6, printk);
	}

	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)
{
	info("%s\n", __func__);

	*cold = 0;
	return 0;
}

937
static int dw210x_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
938
{
939 940 941 942 943 944 945 946
	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,
947 948 949 950 951
	};

	struct dvb_usb_adapter *udev_adap =
		(struct dvb_usb_adapter *)(fe->dvb->priv);
	if (voltage == SEC_VOLTAGE_18)
952 953 954 955 956 957
		msg.buf = command_18v;
	else if (voltage == SEC_VOLTAGE_13)
		msg.buf = command_13v;

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

958 959 960
	return 0;
}

961 962 963 964 965 966 967 968 969 970 971 972 973
static int s660_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
{
	struct dvb_usb_adapter *d =
		(struct dvb_usb_adapter *)(fe->dvb->priv);
	struct s6x0_state *st = (struct s6x0_state *)d->dev->priv;

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

	return 0;
}

974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
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);
}

992 993 994 995 996 997 998 999 1000 1001 1002 1003
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,
};

1004 1005
static struct cx24116_config dw2104_config = {
	.demod_address = 0x55,
1006
	.mpg_clk_pos_pol = 0x01,
1007 1008
};

1009 1010 1011 1012 1013 1014
static struct si21xx_config serit_sp1511lhb_config = {
	.demod_address = 0x68,
	.min_delay_ms = 100,

};

1015 1016 1017 1018 1019
static struct tda10023_config dw3101_tda10023_config = {
	.demod_address = 0x0c,
	.invert = 1,
};

1020 1021 1022 1023
static struct mt312_config zl313_config = {
	.demod_address = 0x0e,
};

1024 1025 1026 1027
static struct ds3000_config dw2104_ds3000_config = {
	.demod_address = 0x68,
};

1028
static struct ts2020_config dw2104_ts2020_config = {
1029
	.tuner_address = 0x60,
1030
	.clk_out_div = 1,
1031
	.frequency_div = 1060000,
1032 1033
};

1034 1035
static struct ds3000_config s660_ds3000_config = {
	.demod_address = 0x68,
1036
	.ci_mode = 1,
1037 1038 1039
	.set_lock_led = dw210x_led_ctrl,
};

1040 1041 1042 1043 1044 1045
static struct ts2020_config s660_ts2020_config = {
	.tuner_address = 0x60,
	.clk_out_div = 1,
	.frequency_div = 1146000,
};

1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
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,
};

1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
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,
1089
	.set_lock_led = dw210x_led_ctrl,
1090 1091
};

1092 1093 1094
static struct ds3000_config su3000_ds3000_config = {
	.demod_address = 0x68,
	.ci_mode = 1,
1095
	.set_lock_led = dw210x_led_ctrl,
1096 1097
};

1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
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,
};

1121 1122
static int dw2104_frontend_attach(struct dvb_usb_adapter *d)
{
1123 1124 1125
	struct dvb_tuner_ops *tuner_ops = NULL;

	if (demod_probe & 4) {
1126
		d->fe_adap[0].fe = dvb_attach(stv0900_attach, &dw2104a_stv0900_config,
1127
				&d->dev->i2c_adap, 0);
1128 1129
		if (d->fe_adap[0].fe != NULL) {
			if (dvb_attach(stb6100_attach, d->fe_adap[0].fe,
1130 1131
					&dw2104a_stb6100_config,
					&d->dev->i2c_adap)) {
1132
				tuner_ops = &d->fe_adap[0].fe->ops.tuner_ops;
1133 1134 1135 1136
				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;
1137
				d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1138 1139 1140 1141 1142 1143 1144
				info("Attached STV0900+STB6100!\n");
				return 0;
			}
		}
	}

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

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

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

1178 1179 1180
	return -EIO;
}

1181
static struct dvb_usb_device_properties dw2102_properties;
1182
static struct dvb_usb_device_properties dw2104_properties;
1183
static struct dvb_usb_device_properties s6x0_properties;
1184

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

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

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

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

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

1248 1249 1250 1251 1252
	return -EIO;
}

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

1255
	d->fe_adap[0].fe = dvb_attach(stv0288_attach, &earda_config,
1256 1257
			&d->dev->i2c_adap);

1258
	if (d->fe_adap[0].fe == NULL)
1259 1260
		return -EIO;

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

1264
	d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1265 1266 1267 1268 1269 1270 1271

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

	info("Attached stv0288+stb6000!\n");

	return 0;

1272 1273 1274 1275
}

static int ds3000_frontend_attach(struct dvb_usb_adapter *d)
{
1276
	struct s6x0_state *st = (struct s6x0_state *)d->dev->priv;
1277
	u8 obuf[] = {7, 1};
1278

1279
	d->fe_adap[0].fe = dvb_attach(ds3000_attach, &s660_ds3000_config,
1280 1281
			&d->dev->i2c_adap);

1282
	if (d->fe_adap[0].fe == NULL)
1283 1284
		return -EIO;

1285
	dvb_attach(ts2020_attach, d->fe_adap[0].fe, &s660_ts2020_config,
1286 1287
		&d->dev->i2c_adap);

1288 1289
	st->old_set_voltage = d->fe_adap[0].fe->ops.set_voltage;
	d->fe_adap[0].fe->ops.set_voltage = s660_set_voltage;
1290 1291 1292

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

1293
	info("Attached ds3000+ts2020!\n");
1294 1295

	return 0;
1296 1297
}

1298 1299
static int prof_7500_frontend_attach(struct dvb_usb_adapter *d)
{
1300 1301
	u8 obuf[] = {7, 1};

1302
	d->fe_adap[0].fe = dvb_attach(stv0900_attach, &prof_7500_stv0900_config,
1303
					&d->dev->i2c_adap, 0);
1304
	if (d->fe_adap[0].fe == NULL)
1305
		return -EIO;
1306

1307
	d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1308

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

1311 1312 1313 1314 1315
	info("Attached STV0900+STB6100A!\n");

	return 0;
}

1316 1317 1318 1319 1320 1321 1322 1323
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.");

1324 1325 1326 1327 1328 1329 1330 1331
	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);

1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
	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.");

1351
	d->fe_adap[0].fe = dvb_attach(ds3000_attach, &su3000_ds3000_config,
1352
					&d->dev->i2c_adap);
1353
	if (d->fe_adap[0].fe == NULL)
1354 1355
		return -EIO;

1356 1357 1358 1359 1360 1361
	if (dvb_attach(ts2020_attach, d->fe_adap[0].fe,
				&dw2104_ts2020_config,
				&d->dev->i2c_adap)) {
		info("Attached DS3000/TS2020!\n");
		return 0;
	}
1362

1363 1364
	info("Failed to attach DS3000/TS2020!\n");
	return -EIO;
1365 1366
}

1367 1368 1369 1370 1371 1372 1373 1374 1375 1376
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);
1377

1378 1379 1380
	if (d->fe_adap[0].fe == NULL)
		return -EIO;

1381 1382 1383 1384 1385 1386
	if (dvb_attach(ts2020_attach, d->fe_adap[0].fe,
				&dw2104_ts2020_config,
				&d->dev->i2c_adap)) {
		info("Attached RS2000/TS2020!\n");
		return 0;
	}
1387

1388 1389
	info("Failed to attach RS2000/TS2020!\n");
	return -EIO;
1390 1391
}

1392 1393
static int dw2102_tuner_attach(struct dvb_usb_adapter *adap)
{
1394
	dvb_attach(dvb_pll_attach, adap->fe_adap[0].fe, 0x60,
1395 1396 1397 1398
		&adap->dev->i2c_adap, DVB_PLL_OPERA1);
	return 0;
}

1399 1400
static int dw3101_tuner_attach(struct dvb_usb_adapter *adap)
{
1401
	dvb_attach(dvb_pll_attach, adap->fe_adap[0].fe, 0x60,
1402 1403 1404 1405 1406
		&adap->dev->i2c_adap, DVB_PLL_TUA6034);

	return 0;
}

1407
static struct rc_map_table rc_map_dw210x_table[] = {
1408 1409
	{ 0xf80a, KEY_POWER2 },		/*power*/
	{ 0xf80c, KEY_MUTE },		/*mute*/
1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
	{ 0xf811, KEY_1 },
	{ 0xf812, KEY_2 },
	{ 0xf813, KEY_3 },
	{ 0xf814, KEY_4 },
	{ 0xf815, KEY_5 },
	{ 0xf816, KEY_6 },
	{ 0xf817, KEY_7 },
	{ 0xf818, KEY_8 },
	{ 0xf819, KEY_9 },
	{ 0xf810, KEY_0 },
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
	{ 0xf81c, KEY_CHANNELUP },	/*ch+*/
	{ 0xf80f, KEY_CHANNELDOWN },	/*ch-*/
	{ 0xf81a, KEY_VOLUMEUP },	/*vol+*/
	{ 0xf80e, KEY_VOLUMEDOWN },	/*vol-*/
	{ 0xf804, KEY_RECORD },		/*rec*/
	{ 0xf809, KEY_FAVORITES },	/*fav*/
	{ 0xf808, KEY_REWIND },		/*rewind*/
	{ 0xf807, KEY_FASTFORWARD },	/*fast*/
	{ 0xf80b, KEY_PAUSE },		/*pause*/
	{ 0xf802, KEY_ESC },		/*cancel*/
	{ 0xf803, KEY_TAB },		/*tab*/
1431
	{ 0xf800, KEY_UP },		/*up*/
1432 1433 1434 1435 1436 1437 1438
	{ 0xf81f, KEY_OK },		/*ok*/
	{ 0xf801, KEY_DOWN },		/*down*/
	{ 0xf805, KEY_CAMERA },		/*cap*/
	{ 0xf806, KEY_STOP },		/*stop*/
	{ 0xf840, KEY_ZOOM },		/*full*/
	{ 0xf81e, KEY_TV },		/*tvmode*/
	{ 0xf81b, KEY_LAST },		/*recall*/
1439
};
1440

1441
static struct rc_map_table rc_map_tevii_table[] = {
1442 1443 1444 1445 1446 1447 1448 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 1480 1481 1482 1483 1484 1485 1486 1487 1488
	{ 0xf80a, KEY_POWER },
	{ 0xf80c, KEY_MUTE },
	{ 0xf811, KEY_1 },
	{ 0xf812, KEY_2 },
	{ 0xf813, KEY_3 },
	{ 0xf814, KEY_4 },
	{ 0xf815, KEY_5 },
	{ 0xf816, KEY_6 },
	{ 0xf817, KEY_7 },
	{ 0xf818, KEY_8 },
	{ 0xf819, KEY_9 },
	{ 0xf810, KEY_0 },
	{ 0xf81c, KEY_MENU },
	{ 0xf80f, KEY_VOLUMEDOWN },
	{ 0xf81a, KEY_LAST },
	{ 0xf80e, KEY_OPEN },
	{ 0xf804, KEY_RECORD },
	{ 0xf809, KEY_VOLUMEUP },
	{ 0xf808, KEY_CHANNELUP },
	{ 0xf807, KEY_PVR },
	{ 0xf80b, KEY_TIME },
	{ 0xf802, KEY_RIGHT },
	{ 0xf803, KEY_LEFT },
	{ 0xf800, KEY_UP },
	{ 0xf81f, KEY_OK },
	{ 0xf801, KEY_DOWN },
	{ 0xf805, KEY_TUNER },
	{ 0xf806, KEY_CHANNELDOWN },
	{ 0xf840, KEY_PLAYPAUSE },
	{ 0xf81e, KEY_REWIND },
	{ 0xf81b, KEY_FAVORITES },
	{ 0xf81d, KEY_BACK },
	{ 0xf84d, KEY_FASTFORWARD },
	{ 0xf844, KEY_EPG },
	{ 0xf84c, KEY_INFO },
	{ 0xf841, KEY_AB },
	{ 0xf843, KEY_AUDIO },
	{ 0xf845, KEY_SUBTITLE },
	{ 0xf84a, KEY_LIST },
	{ 0xf846, KEY_F1 },
	{ 0xf847, KEY_F2 },
	{ 0xf85e, KEY_F3 },
	{ 0xf85c, KEY_F4 },
	{ 0xf852, KEY_F5 },
	{ 0xf85a, KEY_F6 },
	{ 0xf856, KEY_MODE },
	{ 0xf858, KEY_SWITCHVIDEOMODE },
1489 1490
};

1491
static struct rc_map_table rc_map_tbs_table[] = {
1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
	{ 0xf884, KEY_POWER },
	{ 0xf894, KEY_MUTE },
	{ 0xf887, KEY_1 },
	{ 0xf886, KEY_2 },
	{ 0xf885, KEY_3 },
	{ 0xf88b, KEY_4 },
	{ 0xf88a, KEY_5 },
	{ 0xf889, KEY_6 },
	{ 0xf88f, KEY_7 },
	{ 0xf88e, KEY_8 },
	{ 0xf88d, KEY_9 },
	{ 0xf892, KEY_0 },
	{ 0xf896, KEY_CHANNELUP },
	{ 0xf891, KEY_CHANNELDOWN },
	{ 0xf893, KEY_VOLUMEUP },
	{ 0xf88c, KEY_VOLUMEDOWN },
	{ 0xf883, KEY_RECORD },
	{ 0xf898, KEY_PAUSE  },
	{ 0xf899, KEY_OK },
	{ 0xf89a, KEY_SHUFFLE },
	{ 0xf881, KEY_UP },
	{ 0xf890, KEY_LEFT },
	{ 0xf882, KEY_RIGHT },
	{ 0xf888, KEY_DOWN },
	{ 0xf895, KEY_FAVORITES },
	{ 0xf897, KEY_SUBTITLE },
	{ 0xf89d, KEY_ZOOM },
	{ 0xf89f, KEY_EXIT },
	{ 0xf89e, KEY_MENU },
	{ 0xf89c, KEY_EPG },
	{ 0xf880, KEY_PREVIOUS },
	{ 0xf89b, KEY_MODE }
1524
};
1525

1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
static struct rc_map_table rc_map_su3000_table[] = {
	{ 0x25, KEY_POWER },	/* right-bottom Red */
	{ 0x0a, KEY_MUTE },	/* -/-- */
	{ 0x01, KEY_1 },
	{ 0x02, KEY_2 },
	{ 0x03, KEY_3 },
	{ 0x04, KEY_4 },
	{ 0x05, KEY_5 },
	{ 0x06, KEY_6 },
	{ 0x07, KEY_7 },
	{ 0x08, KEY_8 },
	{ 0x09, KEY_9 },
	{ 0x00, KEY_0 },
	{ 0x20, KEY_UP },	/* CH+ */
	{ 0x21, KEY_DOWN },	/* CH+ */
	{ 0x12, KEY_VOLUMEUP },	/* Brightness Up */
	{ 0x13, KEY_VOLUMEDOWN },/* Brightness Down */
	{ 0x1f, KEY_RECORD },
	{ 0x17, KEY_PLAY },
	{ 0x16, KEY_PAUSE },
	{ 0x0b, KEY_STOP },
	{ 0x27, KEY_FASTFORWARD },/* >> */
	{ 0x26, KEY_REWIND },	/* << */
	{ 0x0d, KEY_OK },	/* Mute */
	{ 0x11, KEY_LEFT },	/* VOL- */
	{ 0x10, KEY_RIGHT },	/* VOL+ */
	{ 0x29, KEY_BACK },	/* button under 9 */
	{ 0x2c, KEY_MENU },	/* TTX */
	{ 0x2b, KEY_EPG },	/* EPG */
	{ 0x1e, KEY_RED },	/* OSD */
	{ 0x0e, KEY_GREEN },	/* Window */
	{ 0x2d, KEY_YELLOW },	/* button under << */
	{ 0x0f, KEY_BLUE },	/* bottom yellow button */
	{ 0x14, KEY_AUDIO },	/* Snapshot */
	{ 0x38, KEY_TV },	/* TV/Radio */
1561
	{ 0x0c, KEY_ESC }	/* upper Red button */
1562 1563
};

1564 1565 1566 1567
static struct rc_map_dvb_usb_table_table keys_tables[] = {
	{ rc_map_dw210x_table, ARRAY_SIZE(rc_map_dw210x_table) },
	{ rc_map_tevii_table, ARRAY_SIZE(rc_map_tevii_table) },
	{ rc_map_tbs_table, ARRAY_SIZE(rc_map_tbs_table) },
1568
	{ rc_map_su3000_table, ARRAY_SIZE(rc_map_su3000_table) },
1569
};
1570 1571 1572

static int dw2102_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
{
1573 1574
	struct rc_map_table *keymap = d->props.rc.legacy.rc_map_table;
	int keymap_size = d->props.rc.legacy.rc_map_size;
1575
	u8 key[2];
1576 1577 1578 1579 1580
	struct i2c_msg msg = {
		.addr = DW2102_RC_QUERY,
		.flags = I2C_M_RD,
		.buf = key,
		.len = 2
1581 1582
	};
	int i;
1583 1584 1585 1586
	/* override keymap */
	if ((ir_keymap > 0) && (ir_keymap <= ARRAY_SIZE(keys_tables))) {
		keymap = keys_tables[ir_keymap - 1].rc_keys ;
		keymap_size = keys_tables[ir_keymap - 1].rc_keys_size;
1587 1588
	} else if (ir_keymap > ARRAY_SIZE(keys_tables))
		return 0; /* none */
1589 1590

	*state = REMOTE_NO_KEY_PRESSED;
1591
	if (d->props.i2c_algo->master_xfer(&d->i2c_adap, &msg, 1) == 1) {
1592
		for (i = 0; i < keymap_size ; i++) {
1593
			if (rc5_data(&keymap[i]) == msg.buf[0]) {
1594
				*state = REMOTE_KEY_PRESSED;
1595
				*event = keymap[i].keycode;
1596 1597
				break;
			}
1598

1599
		}
1600 1601 1602 1603 1604 1605 1606 1607

		if ((*state) == REMOTE_KEY_PRESSED)
			deb_rc("%s: found rc key: %x, %x, event: %x\n",
					__func__, key[0], key[1], (*event));
		else if (key[0] != 0xff)
			deb_rc("%s: unknown rc key: %x, %x\n",
					__func__, key[0], key[1]);

1608
	}
1609

1610 1611 1612
	return 0;
}

1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628
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,
1629 1630
	TEVII_S421,
	TEVII_S632,
1631
	TERRATEC_CINERGY_S2_R2,
1632
	GOTVIEW_SAT_HD,
1633 1634
};

1635
static struct usb_device_id dw2102_table[] = {
1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650
	[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)},
	[TERRATEC_CINERGY_S] = {USB_DEVICE(USB_VID_TERRATEC, USB_PID_CINERGY_S)},
	[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)},
	[TERRATEC_CINERGY_S2] = {USB_DEVICE(USB_VID_TERRATEC, 0x00a8)},
	[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)},
1651 1652
	[TEVII_S421] = {USB_DEVICE(0x9022, USB_PID_TEVII_S421)},
	[TEVII_S632] = {USB_DEVICE(0x9022, USB_PID_TEVII_S632)},
1653
	[TERRATEC_CINERGY_S2_R2] = {USB_DEVICE(USB_VID_TERRATEC, 0x00b0)},
1654
	[GOTVIEW_SAT_HD] = {USB_DEVICE(0x1FE1, USB_PID_GOTVIEW_SAT_HD)},
1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665
	{ }
};

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;
1666
	u8 reset16[] = {0, 0, 0, 0, 0, 0, 0};
1667
	const struct firmware *fw;
1668

1669 1670
	switch (dev->descriptor.idProduct) {
	case 0x2101:
1671
		ret = request_firmware(&fw, DW2101_FIRMWARE, &dev->dev);
1672
		if (ret != 0) {
1673
			err(err_str, DW2101_FIRMWARE);
1674 1675 1676
			return ret;
		}
		break;
1677
	default:
1678 1679 1680
		fw = frmwr;
		break;
	}
1681
	info("start downloading DW210X firmware");
1682 1683 1684
	p = kmalloc(fw->size, GFP_KERNEL);
	reset = 1;
	/*stop the CPU*/
1685 1686
	dw210x_op_rw(dev, 0xa0, 0x7f92, 0, &reset, 1, DW210X_WRITE_MSG);
	dw210x_op_rw(dev, 0xa0, 0xe600, 0, &reset, 1, DW210X_WRITE_MSG);
1687 1688 1689 1690 1691

	if (p != NULL) {
		memcpy(p, fw->data, fw->size);
		for (i = 0; i < fw->size; i += 0x40) {
			b = (u8 *) p + i;
1692 1693
			if (dw210x_op_rw(dev, 0xa0, i, 0, b , 0x40,
					DW210X_WRITE_MSG) != 0x40) {
1694 1695 1696 1697 1698 1699 1700
				err("error while transferring firmware");
				ret = -EINVAL;
				break;
			}
		}
		/* restart the CPU */
		reset = 0;
1701 1702
		if (ret || dw210x_op_rw(dev, 0xa0, 0x7f92, 0, &reset, 1,
					DW210X_WRITE_MSG) != 1) {
1703 1704 1705
			err("could not restart the USB controller CPU.");
			ret = -EINVAL;
		}
1706 1707
		if (ret || dw210x_op_rw(dev, 0xa0, 0xe600, 0, &reset, 1,
					DW210X_WRITE_MSG) != 1) {
1708 1709 1710 1711 1712
			err("could not restart the USB controller CPU.");
			ret = -EINVAL;
		}
		/* init registers */
		switch (dev->descriptor.idProduct) {
1713
		case USB_PID_TEVII_S650:
1714 1715 1716
			dw2104_properties.rc.legacy.rc_map_table = rc_map_tevii_table;
			dw2104_properties.rc.legacy.rc_map_size =
					ARRAY_SIZE(rc_map_tevii_table);
1717
		case USB_PID_DW2104:
1718
			reset = 1;
1719 1720
			dw210x_op_rw(dev, 0xc4, 0x0000, 0, &reset, 1,
					DW210X_WRITE_MSG);
1721 1722
			/* break omitted intentionally */
		case USB_PID_DW3101:
1723
			reset = 0;
1724 1725
			dw210x_op_rw(dev, 0xbf, 0x0040, 0, &reset, 0,
					DW210X_WRITE_MSG);
1726
			break;
1727
		case USB_PID_CINERGY_S:
1728
		case USB_PID_DW2102:
1729 1730 1731 1732 1733 1734 1735
			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);
1736
			if ((reset16[0] == 0xa1) || (reset16[0] == 0x80)) {
1737
				dw2102_properties.i2c_algo = &dw2102_i2c_algo;
1738
				dw2102_properties.adapter->fe[0].tuner_attach = &dw2102_tuner_attach;
1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753
				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;
				}
			}
1754
		case 0x2101:
1755 1756 1757 1758 1759 1760 1761 1762
			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);
1763 1764
			break;
		}
1765

1766
		msleep(100);
1767 1768 1769 1770 1771 1772 1773 1774
		kfree(p);
	}
	return ret;
}

static struct dvb_usb_device_properties dw2102_properties = {
	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
	.usb_ctrl = DEVICE_SPECIFIC,
1775
	.firmware = DW2102_FIRMWARE,
1776 1777
	.no_reconnect = 1,

1778
	.i2c_algo = &dw2102_serit_i2c_algo,
1779 1780

	.rc.legacy = {
1781 1782
		.rc_map_table = rc_map_dw210x_table,
		.rc_map_size = ARRAY_SIZE(rc_map_dw210x_table),
1783 1784 1785
		.rc_interval = 150,
		.rc_query = dw2102_rc_query,
	},
1786 1787 1788 1789 1790

	.generic_bulk_ctrl_endpoint = 0x81,
	/* parameter for the MPEG2-data transfer */
	.num_adapters = 1,
	.download_firmware = dw2102_load_firmware,
1791
	.read_mac_address = dw210x_read_mac_address,
1792
	.adapter = {
1793
		{
1794 1795
		.num_frontends = 1,
		.fe = {{
1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806
			.frontend_attach = dw2102_frontend_attach,
			.stream = {
				.type = USB_BULK,
				.count = 8,
				.endpoint = 0x82,
				.u = {
					.bulk = {
						.buffersize = 4096,
					}
				}
			},
1807
		}},
1808 1809
		}
	},
1810
	.num_device_descs = 3,
1811 1812
	.devices = {
		{"DVBWorld DVB-S 2102 USB2.0",
1813
			{&dw2102_table[CYPRESS_DW2102], NULL},
1814 1815 1816
			{NULL},
		},
		{"DVBWorld DVB-S 2101 USB2.0",
1817
			{&dw2102_table[CYPRESS_DW2101], NULL},
1818
			{NULL},
1819 1820
		},
		{"TerraTec Cinergy S USB",
1821
			{&dw2102_table[TERRATEC_CINERGY_S], NULL},
1822
			{NULL},
1823 1824 1825 1826
		},
	}
};

1827 1828 1829
static struct dvb_usb_device_properties dw2104_properties = {
	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
	.usb_ctrl = DEVICE_SPECIFIC,
1830
	.firmware = DW2104_FIRMWARE,
1831 1832 1833
	.no_reconnect = 1,

	.i2c_algo = &dw2104_i2c_algo,
1834
	.rc.legacy = {
1835 1836
		.rc_map_table = rc_map_dw210x_table,
		.rc_map_size = ARRAY_SIZE(rc_map_dw210x_table),
1837 1838 1839
		.rc_interval = 150,
		.rc_query = dw2102_rc_query,
	},
1840 1841 1842 1843 1844

	.generic_bulk_ctrl_endpoint = 0x81,
	/* parameter for the MPEG2-data transfer */
	.num_adapters = 1,
	.download_firmware = dw2102_load_firmware,
1845
	.read_mac_address = dw210x_read_mac_address,
1846 1847
	.adapter = {
		{
1848 1849
		.num_frontends = 1,
		.fe = {{
1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860
			.frontend_attach = dw2104_frontend_attach,
			.stream = {
				.type = USB_BULK,
				.count = 8,
				.endpoint = 0x82,
				.u = {
					.bulk = {
						.buffersize = 4096,
					}
				}
			},
1861
		}},
1862 1863 1864 1865 1866
		}
	},
	.num_device_descs = 2,
	.devices = {
		{ "DVBWorld DW2104 USB2.0",
1867
			{&dw2102_table[CYPRESS_DW2104], NULL},
1868 1869 1870
			{NULL},
		},
		{ "TeVii S650 USB2.0",
1871
			{&dw2102_table[TEVII_S650], NULL},
1872 1873 1874 1875 1876
			{NULL},
		},
	}
};

1877 1878 1879
static struct dvb_usb_device_properties dw3101_properties = {
	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
	.usb_ctrl = DEVICE_SPECIFIC,
1880
	.firmware = DW3101_FIRMWARE,
1881 1882 1883
	.no_reconnect = 1,

	.i2c_algo = &dw3101_i2c_algo,
1884
	.rc.legacy = {
1885 1886
		.rc_map_table = rc_map_dw210x_table,
		.rc_map_size = ARRAY_SIZE(rc_map_dw210x_table),
1887 1888 1889
		.rc_interval = 150,
		.rc_query = dw2102_rc_query,
	},
1890 1891 1892 1893 1894 1895 1896 1897

	.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 = {
		{
1898 1899
		.num_frontends = 1,
		.fe = {{
1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911
			.frontend_attach = dw3101_frontend_attach,
			.tuner_attach = dw3101_tuner_attach,
			.stream = {
				.type = USB_BULK,
				.count = 8,
				.endpoint = 0x82,
				.u = {
					.bulk = {
						.buffersize = 4096,
					}
				}
			},
1912
		}},
1913 1914 1915 1916 1917
		}
	},
	.num_device_descs = 1,
	.devices = {
		{ "DVBWorld DVB-C 3101 USB2.0",
1918
			{&dw2102_table[CYPRESS_DW3101], NULL},
1919 1920 1921 1922 1923
			{NULL},
		},
	}
};

1924
static struct dvb_usb_device_properties s6x0_properties = {
1925 1926
	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
	.usb_ctrl = DEVICE_SPECIFIC,
1927
	.size_of_priv = sizeof(struct s6x0_state),
1928
	.firmware = S630_FIRMWARE,
1929 1930
	.no_reconnect = 1,

1931
	.i2c_algo = &s6x0_i2c_algo,
1932
	.rc.legacy = {
1933 1934
		.rc_map_table = rc_map_tevii_table,
		.rc_map_size = ARRAY_SIZE(rc_map_tevii_table),
1935 1936 1937
		.rc_interval = 150,
		.rc_query = dw2102_rc_query,
	},
1938 1939 1940 1941

	.generic_bulk_ctrl_endpoint = 0x81,
	.num_adapters = 1,
	.download_firmware = dw2102_load_firmware,
1942
	.read_mac_address = s6x0_read_mac_address,
1943 1944
	.adapter = {
		{
1945 1946
		.num_frontends = 1,
		.fe = {{
1947
			.frontend_attach = zl100313_frontend_attach,
1948 1949 1950 1951 1952 1953 1954 1955 1956 1957
			.stream = {
				.type = USB_BULK,
				.count = 8,
				.endpoint = 0x82,
				.u = {
					.bulk = {
						.buffersize = 4096,
					}
				}
			},
1958
		}},
1959 1960
		}
	},
1961
	.num_device_descs = 1,
1962 1963
	.devices = {
		{"TeVii S630 USB",
1964
			{&dw2102_table[TEVII_S630], NULL},
1965 1966 1967 1968 1969
			{NULL},
		},
	}
};

1970 1971 1972
struct dvb_usb_device_properties *p1100;
static struct dvb_usb_device_description d1100 = {
	"Prof 1100 USB ",
1973
	{&dw2102_table[PROF_1100], NULL},
1974 1975 1976 1977 1978 1979
	{NULL},
};

struct dvb_usb_device_properties *s660;
static struct dvb_usb_device_description d660 = {
	"TeVii S660 USB",
1980
	{&dw2102_table[TEVII_S660], NULL},
1981 1982 1983
	{NULL},
};

1984 1985
static struct dvb_usb_device_description d480_1 = {
	"TeVii S480.1 USB",
1986
	{&dw2102_table[TEVII_S480_1], NULL},
1987 1988 1989 1990 1991
	{NULL},
};

static struct dvb_usb_device_description d480_2 = {
	"TeVii S480.2 USB",
1992
	{&dw2102_table[TEVII_S480_2], NULL},
1993 1994 1995
	{NULL},
};

1996 1997 1998
struct dvb_usb_device_properties *p7500;
static struct dvb_usb_device_description d7500 = {
	"Prof 7500 USB DVB-S2",
1999
	{&dw2102_table[PROF_7500], NULL},
2000 2001 2002
	{NULL},
};

2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015
struct dvb_usb_device_properties *s421;
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},
};

2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037
static struct dvb_usb_device_properties su3000_properties = {
	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
	.usb_ctrl = DEVICE_SPECIFIC,
	.size_of_priv = sizeof(struct su3000_state),
	.power_ctrl = su3000_power_ctrl,
	.num_adapters = 1,
	.identify_state	= su3000_identify_state,
	.i2c_algo = &su3000_i2c_algo,

	.rc.legacy = {
		.rc_map_table = rc_map_su3000_table,
		.rc_map_size = ARRAY_SIZE(rc_map_su3000_table),
		.rc_interval = 150,
		.rc_query = dw2102_rc_query,
	},

	.read_mac_address = su3000_read_mac_address,

	.generic_bulk_ctrl_endpoint = 0x01,

	.adapter = {
		{
2038 2039
		.num_frontends = 1,
		.fe = {{
2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051
			.streaming_ctrl   = su3000_streaming_ctrl,
			.frontend_attach  = su3000_frontend_attach,
			.stream = {
				.type = USB_BULK,
				.count = 8,
				.endpoint = 0x82,
				.u = {
					.bulk = {
						.buffersize = 4096,
					}
				}
			}
2052
		}},
2053 2054
		}
	},
2055
	.num_device_descs = 5,
2056 2057
	.devices = {
		{ "SU3000HD DVB-S USB2.0",
2058
			{ &dw2102_table[GENIATECH_SU3000], NULL },
2059 2060
			{ NULL },
		},
2061
		{ "Terratec Cinergy S2 USB HD",
2062
			{ &dw2102_table[TERRATEC_CINERGY_S2], NULL },
2063 2064
			{ NULL },
		},
2065
		{ "X3M TV SPC1400HD PCI",
2066
			{ &dw2102_table[X3M_SPC1400HD], NULL },
2067 2068
			{ NULL },
		},
2069 2070 2071 2072
		{ "Terratec Cinergy S2 USB HD Rev.2",
			{ &dw2102_table[TERRATEC_CINERGY_S2_R2], NULL },
			{ NULL },
		},
2073 2074 2075 2076
		{ "GOTVIEW Satellite HD",
			{ &dw2102_table[GOTVIEW_SAT_HD], NULL },
			{ NULL },
		},
2077 2078 2079
	}
};

2080 2081 2082
static int dw2102_probe(struct usb_interface *intf,
		const struct usb_device_id *id)
{
2083 2084
	p1100 = kmemdup(&s6x0_properties,
			sizeof(struct dvb_usb_device_properties), GFP_KERNEL);
2085 2086 2087 2088
	if (!p1100)
		return -ENOMEM;
	/* copy default structure */
	/* fill only different fields */
2089
	p1100->firmware = P1100_FIRMWARE;
2090 2091 2092
	p1100->devices[0] = d1100;
	p1100->rc.legacy.rc_map_table = rc_map_tbs_table;
	p1100->rc.legacy.rc_map_size = ARRAY_SIZE(rc_map_tbs_table);
2093
	p1100->adapter->fe[0].frontend_attach = stv0288_frontend_attach;
2094

2095 2096
	s660 = kmemdup(&s6x0_properties,
		       sizeof(struct dvb_usb_device_properties), GFP_KERNEL);
2097 2098 2099 2100
	if (!s660) {
		kfree(p1100);
		return -ENOMEM;
	}
2101
	s660->firmware = S660_FIRMWARE;
2102
	s660->num_device_descs = 3;
2103
	s660->devices[0] = d660;
2104 2105
	s660->devices[1] = d480_1;
	s660->devices[2] = d480_2;
2106
	s660->adapter->fe[0].frontend_attach = ds3000_frontend_attach;
2107

2108 2109
	p7500 = kmemdup(&s6x0_properties,
			sizeof(struct dvb_usb_device_properties), GFP_KERNEL);
2110 2111 2112
	if (!p7500) {
		kfree(p1100);
		kfree(s660);
2113
		return -ENOMEM;
2114
	}
2115
	p7500->firmware = P7500_FIRMWARE;
2116
	p7500->devices[0] = d7500;
2117 2118
	p7500->rc.legacy.rc_map_table = rc_map_tbs_table;
	p7500->rc.legacy.rc_map_size = ARRAY_SIZE(rc_map_tbs_table);
2119
	p7500->adapter->fe[0].frontend_attach = prof_7500_frontend_attach;
2120

2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134

	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;

2135 2136 2137
	if (0 == dvb_usb_device_init(intf, &dw2102_properties,
			THIS_MODULE, NULL, adapter_nr) ||
	    0 == dvb_usb_device_init(intf, &dw2104_properties,
2138 2139
			THIS_MODULE, NULL, adapter_nr) ||
	    0 == dvb_usb_device_init(intf, &dw3101_properties,
2140
			THIS_MODULE, NULL, adapter_nr) ||
2141
	    0 == dvb_usb_device_init(intf, &s6x0_properties,
2142
			THIS_MODULE, NULL, adapter_nr) ||
2143 2144 2145 2146
	    0 == dvb_usb_device_init(intf, p1100,
			THIS_MODULE, NULL, adapter_nr) ||
	    0 == dvb_usb_device_init(intf, s660,
			THIS_MODULE, NULL, adapter_nr) ||
2147
	    0 == dvb_usb_device_init(intf, p7500,
2148
			THIS_MODULE, NULL, adapter_nr) ||
2149 2150
	    0 == dvb_usb_device_init(intf, s421,
			THIS_MODULE, NULL, adapter_nr) ||
2151 2152
	    0 == dvb_usb_device_init(intf, &su3000_properties,
				     THIS_MODULE, NULL, adapter_nr))
2153
		return 0;
2154

2155
	return -ENODEV;
2156 2157 2158 2159 2160 2161 2162 2163 2164
}

static struct usb_driver dw2102_driver = {
	.name = "dw2102",
	.probe = dw2102_probe,
	.disconnect = dvb_usb_device_exit,
	.id_table = dw2102_table,
};

2165
module_usb_driver(dw2102_driver);
2166 2167

MODULE_AUTHOR("Igor M. Liplianin (c) liplianin@me.by");
2168
MODULE_DESCRIPTION("Driver for DVBWorld DVB-S 2101, 2102, DVB-S2 2104,"
2169 2170 2171 2172
			" DVB-C 3101 USB2.0,"
			" TeVii S600, S630, S650, S660, S480, S421, S632"
			" Prof 1100, 7500 USB2.0,"
			" Geniatech SU3000 devices");
2173 2174
MODULE_VERSION("0.1");
MODULE_LICENSE("GPL");
2175 2176 2177 2178 2179 2180 2181 2182
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);