Project

General

Profile

RE: Terratec CINERGY S2 PCIe DUAL?? Firmware for Ubuntu ยป dw2102.c

Fredrik Ling, 2013-07-05 17:40

 
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 ([email protected])
7
 *
8
 *	This program is free software; you can redistribute it and/or modify it
9
 *	under the terms of the GNU General Public License as published by the
10
 *	Free Software Foundation, version 2.
11
 *
12
 * see Documentation/dvb/README.dvb-usb for more information
13
 */
14
#include "dw2102.h"
15
#include "si21xx.h"
16
#include "stv0299.h"
17
#include "z0194a.h"
18
#include "stv0288.h"
19
#include "stb6000.h"
20
#include "eds1547.h"
21
#include "cx24116.h"
22
#include "tda1002x.h"
23
#include "mt312.h"
24
#include "zl10039.h"
25
#include "ds3000.h"
26
#include "ds3103.h"
27
#include "stv0900.h"
28
#include "stv6110.h"
29
#include "stb6100.h"
30
#include "stb6100_proc.h"
31
#include "m88rs2000.h"
32
#include "ts2020.h"
33
#include "ts2022.h"
34

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

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

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

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

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

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

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

    
63
#ifndef USB_PID_TEVII_S480_1
64
#define USB_PID_TEVII_S480_1 0x481
65
#endif
66

    
67
#ifndef USB_PID_TEVII_S480_2
68
#define USB_PID_TEVII_S480_2 0x482
69
#endif
70

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

    
75
#ifndef USB_PID_TEVII_S421
76
#define USB_PID_TEVII_S421 0xd421
77
#endif
78

    
79
#ifndef USB_PID_TEVII_S632
80
#define USB_PID_TEVII_S632 0xd632
81
#endif
82

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

    
87
#ifndef USB_PID_TEVII_S482_1
88
#define USB_PID_TEVII_S482_1 0x1181
89
#endif
90

    
91
#ifndef USB_PID_TEVII_S482_2
92
#define USB_PID_TEVII_S482_2 0x1182
93
#endif
94

    
95
#define DW210X_READ_MSG 0
96
#define DW210X_WRITE_MSG 1
97

    
98
#define REG_1F_SYMBOLRATE_BYTE0 0x1f
99
#define REG_20_SYMBOLRATE_BYTE1 0x20
100
#define REG_21_SYMBOLRATE_BYTE2 0x21
101
/* on my own*/
102
#define DW2102_VOLTAGE_CTRL (0x1800)
103
#define SU3000_STREAM_CTRL (0x1900)
104
#define DW2102_RC_QUERY (0x1a00)
105
#define DW2102_LED_CTRL (0x1b00)
106

    
107
#define DW2101_FIRMWARE "dvb-usb-dw2101.fw"
108
#define DW2102_FIRMWARE "dvb-usb-dw2102.fw"
109
#define DW2104_FIRMWARE "dvb-usb-dw2104.fw"
110
#define DW3101_FIRMWARE "dvb-usb-dw3101.fw"
111
#define S630_FIRMWARE   "dvb-usb-s630.fw"
112
#define S660_FIRMWARE   "dvb-usb-s660.fw"
113
#define P1100_FIRMWARE  "dvb-usb-p1100.fw"
114
#define P7500_FIRMWARE  "dvb-usb-p7500.fw"
115

    
116
#define	err_str "did not find the firmware file. (%s) " \
117
		"Please see linux/Documentation/dvb/ for more details " \
118
		"on firmware-problems."
119

    
120
struct rc_map_dvb_usb_table_table {
121
	struct rc_map_table *rc_keys;
122
	int rc_keys_size;
123
};
124

    
125
struct su3000_state {
126
	u8 initialized;
127
};
128

    
129
struct s6x0_state {
130
	int (*old_set_voltage)(struct dvb_frontend *f, fe_sec_voltage_t v);
131
};
132

    
133
/* debug */
134
static int dvb_usb_dw2102_debug;
135
module_param_named(debug, dvb_usb_dw2102_debug, int, 0644);
136
MODULE_PARM_DESC(debug, "set debugging level (1=info 2=xfer 4=rc(or-able))."
137
						DVB_USB_DEBUG_STATUS);
138

    
139
/* keymaps */
140
static int ir_keymap;
141
module_param_named(keymap, ir_keymap, int, 0644);
142
MODULE_PARM_DESC(keymap, "set keymap 0=default 1=dvbworld 2=tevii 3=tbs  ..."
143
			" 256=none");
144

    
145
/* demod probe */
146
static int demod_probe = 1;
147
module_param_named(demod, demod_probe, int, 0644);
148
MODULE_PARM_DESC(demod, "demod to probe (1=cx24116 2=stv0903+stv6110 "
149
			"4=stv0903+stb6100(or-able)).");
150

    
151
DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
152

    
153
static int dw210x_op_rw(struct usb_device *dev, u8 request, u16 value,
154
			u16 index, u8 * data, u16 len, int flags)
155
{
156
	int ret;
157
	u8 *u8buf;
158
	unsigned int pipe = (flags == DW210X_READ_MSG) ?
159
				usb_rcvctrlpipe(dev, 0) : usb_sndctrlpipe(dev, 0);
160
	u8 request_type = (flags == DW210X_READ_MSG) ? USB_DIR_IN : USB_DIR_OUT;
161

    
162
	u8buf = kmalloc(len, GFP_KERNEL);
163
	if (!u8buf)
164
		return -ENOMEM;
165

    
166

    
167
	if (flags == DW210X_WRITE_MSG)
168
		memcpy(u8buf, data, len);
169
	ret = usb_control_msg(dev, pipe, request, request_type | USB_TYPE_VENDOR,
170
				value, index , u8buf, len, 2000);
171

    
172
	if (flags == DW210X_READ_MSG)
173
		memcpy(data, u8buf, len);
174

    
175
	kfree(u8buf);
176
	return ret;
177
}
178

    
179
/* I2C */
180
static int dw2102_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[],
181
		int num)
182
{
183
	struct dvb_usb_device *d = i2c_get_adapdata(adap);
184
	int i = 0;
185
	u8 buf6[] = {0x2c, 0x05, 0xc0, 0, 0, 0, 0};
186
	u16 value;
187

    
188
	if (!d)
189
		return -ENODEV;
190
	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
191
		return -EAGAIN;
192

    
193
	switch (num) {
194
	case 2:
195
		/* read stv0299 register */
196
		value = msg[0].buf[0];/* register */
197
		for (i = 0; i < msg[1].len; i++) {
198
			dw210x_op_rw(d->udev, 0xb5, value + i, 0,
199
					buf6, 2, DW210X_READ_MSG);
200
			msg[1].buf[i] = buf6[0];
201
		}
202
		break;
203
	case 1:
204
		switch (msg[0].addr) {
205
		case 0x68:
206
			/* write to stv0299 register */
207
			buf6[0] = 0x2a;
208
			buf6[1] = msg[0].buf[0];
209
			buf6[2] = msg[0].buf[1];
210
			dw210x_op_rw(d->udev, 0xb2, 0, 0,
211
					buf6, 3, DW210X_WRITE_MSG);
212
			break;
213
		case 0x60:
214
			if (msg[0].flags == 0) {
215
			/* write to tuner pll */
216
				buf6[0] = 0x2c;
217
				buf6[1] = 5;
218
				buf6[2] = 0xc0;
219
				buf6[3] = msg[0].buf[0];
220
				buf6[4] = msg[0].buf[1];
221
				buf6[5] = msg[0].buf[2];
222
				buf6[6] = msg[0].buf[3];
223
				dw210x_op_rw(d->udev, 0xb2, 0, 0,
224
						buf6, 7, DW210X_WRITE_MSG);
225
			} else {
226
			/* read from tuner */
227
				dw210x_op_rw(d->udev, 0xb5, 0, 0,
228
						buf6, 1, DW210X_READ_MSG);
229
				msg[0].buf[0] = buf6[0];
230
			}
231
			break;
232
		case (DW2102_RC_QUERY):
233
			dw210x_op_rw(d->udev, 0xb8, 0, 0,
234
					buf6, 2, DW210X_READ_MSG);
235
			msg[0].buf[0] = buf6[0];
236
			msg[0].buf[1] = buf6[1];
237
			break;
238
		case (DW2102_VOLTAGE_CTRL):
239
			buf6[0] = 0x30;
240
			buf6[1] = msg[0].buf[0];
241
			dw210x_op_rw(d->udev, 0xb2, 0, 0,
242
					buf6, 2, DW210X_WRITE_MSG);
243
			break;
244
		}
245

    
246
		break;
247
	}
248

    
249
	mutex_unlock(&d->i2c_mutex);
250
	return num;
251
}
252

    
253
static int dw2102_serit_i2c_transfer(struct i2c_adapter *adap,
254
						struct i2c_msg msg[], int num)
255
{
256
	struct dvb_usb_device *d = i2c_get_adapdata(adap);
257
	u8 buf6[] = {0, 0, 0, 0, 0, 0, 0};
258

    
259
	if (!d)
260
		return -ENODEV;
261
	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
262
		return -EAGAIN;
263

    
264
	switch (num) {
265
	case 2:
266
		/* read si2109 register by number */
267
		buf6[0] = msg[0].addr << 1;
268
		buf6[1] = msg[0].len;
269
		buf6[2] = msg[0].buf[0];
270
		dw210x_op_rw(d->udev, 0xc2, 0, 0,
271
				buf6, msg[0].len + 2, DW210X_WRITE_MSG);
272
		/* read si2109 register */
273
		dw210x_op_rw(d->udev, 0xc3, 0xd0, 0,
274
				buf6, msg[1].len + 2, DW210X_READ_MSG);
275
		memcpy(msg[1].buf, buf6 + 2, msg[1].len);
276

    
277
		break;
278
	case 1:
279
		switch (msg[0].addr) {
280
		case 0x68:
281
			/* write to si2109 register */
282
			buf6[0] = msg[0].addr << 1;
283
			buf6[1] = msg[0].len;
284
			memcpy(buf6 + 2, msg[0].buf, msg[0].len);
285
			dw210x_op_rw(d->udev, 0xc2, 0, 0, buf6,
286
					msg[0].len + 2, DW210X_WRITE_MSG);
287
			break;
288
		case(DW2102_RC_QUERY):
289
			dw210x_op_rw(d->udev, 0xb8, 0, 0,
290
					buf6, 2, DW210X_READ_MSG);
291
			msg[0].buf[0] = buf6[0];
292
			msg[0].buf[1] = buf6[1];
293
			break;
294
		case(DW2102_VOLTAGE_CTRL):
295
			buf6[0] = 0x30;
296
			buf6[1] = msg[0].buf[0];
297
			dw210x_op_rw(d->udev, 0xb2, 0, 0,
298
					buf6, 2, DW210X_WRITE_MSG);
299
			break;
300
		}
301
		break;
302
	}
303

    
304
	mutex_unlock(&d->i2c_mutex);
305
	return num;
306
}
307

    
308
static int dw2102_earda_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[], int num)
309
{
310
	struct dvb_usb_device *d = i2c_get_adapdata(adap);
311

    
312
	if (!d)
313
		return -ENODEV;
314
	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
315
		return -EAGAIN;
316

    
317
	switch (num) {
318
	case 2: {
319
		/* read */
320
		/* first write first register number */
321
		u8 ibuf[msg[1].len + 2], obuf[3];
322
		obuf[0] = msg[0].addr << 1;
323
		obuf[1] = msg[0].len;
324
		obuf[2] = msg[0].buf[0];
325
		dw210x_op_rw(d->udev, 0xc2, 0, 0,
326
				obuf, msg[0].len + 2, DW210X_WRITE_MSG);
327
		/* second read registers */
328
		dw210x_op_rw(d->udev, 0xc3, 0xd1 , 0,
329
				ibuf, msg[1].len + 2, DW210X_READ_MSG);
330
		memcpy(msg[1].buf, ibuf + 2, msg[1].len);
331

    
332
		break;
333
	}
334
	case 1:
335
		switch (msg[0].addr) {
336
		case 0x68: {
337
			/* write to register */
338
			u8 obuf[msg[0].len + 2];
339
			obuf[0] = msg[0].addr << 1;
340
			obuf[1] = msg[0].len;
341
			memcpy(obuf + 2, msg[0].buf, msg[0].len);
342
			dw210x_op_rw(d->udev, 0xc2, 0, 0,
343
					obuf, msg[0].len + 2, DW210X_WRITE_MSG);
344
			break;
345
		}
346
		case 0x61: {
347
			/* write to tuner */
348
			u8 obuf[msg[0].len + 2];
349
			obuf[0] = msg[0].addr << 1;
350
			obuf[1] = msg[0].len;
351
			memcpy(obuf + 2, msg[0].buf, msg[0].len);
352
			dw210x_op_rw(d->udev, 0xc2, 0, 0,
353
					obuf, msg[0].len + 2, DW210X_WRITE_MSG);
354
			break;
355
		}
356
		case(DW2102_RC_QUERY): {
357
			u8 ibuf[2];
358
			dw210x_op_rw(d->udev, 0xb8, 0, 0,
359
					ibuf, 2, DW210X_READ_MSG);
360
			memcpy(msg[0].buf, ibuf , 2);
361
			break;
362
		}
363
		case(DW2102_VOLTAGE_CTRL): {
364
			u8 obuf[2];
365
			obuf[0] = 0x30;
366
			obuf[1] = msg[0].buf[0];
367
			dw210x_op_rw(d->udev, 0xb2, 0, 0,
368
					obuf, 2, DW210X_WRITE_MSG);
369
			break;
370
		}
371
		}
372

    
373
		break;
374
	}
375

    
376
	mutex_unlock(&d->i2c_mutex);
377
	return num;
378
}
379

    
380
static int dw2104_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[], int num)
381
{
382
	struct dvb_usb_device *d = i2c_get_adapdata(adap);
383
	int len, i, j;
384

    
385
	if (!d)
386
		return -ENODEV;
387
	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
388
		return -EAGAIN;
389

    
390
	for (j = 0; j < num; j++) {
391
		switch (msg[j].addr) {
392
		case(DW2102_RC_QUERY): {
393
			u8 ibuf[2];
394
			dw210x_op_rw(d->udev, 0xb8, 0, 0,
395
					ibuf, 2, DW210X_READ_MSG);
396
			memcpy(msg[j].buf, ibuf , 2);
397
			break;
398
		}
399
		case(DW2102_VOLTAGE_CTRL): {
400
			u8 obuf[2];
401
			obuf[0] = 0x30;
402
			obuf[1] = msg[j].buf[0];
403
			dw210x_op_rw(d->udev, 0xb2, 0, 0,
404
					obuf, 2, DW210X_WRITE_MSG);
405
			break;
406
		}
407
		/*case 0x55: cx24116
408
		case 0x6a: stv0903
409
		case 0x68: ds3000, stv0903
410
		case 0x60: ts2020, stv6110, stb6100 */
411
		default: {
412
			if (msg[j].flags == I2C_M_RD) {
413
				/* read registers */
414
				u8  ibuf[msg[j].len + 2];
415
				dw210x_op_rw(d->udev, 0xc3,
416
						(msg[j].addr << 1) + 1, 0,
417
						ibuf, msg[j].len + 2,
418
						DW210X_READ_MSG);
419
				memcpy(msg[j].buf, ibuf + 2, msg[j].len);
420
			mdelay(10);
421
			} else if (((msg[j].buf[0] == 0xb0) &&
422
						(msg[j].addr == 0x68)) ||
423
						((msg[j].buf[0] == 0xf7) &&
424
						(msg[j].addr == 0x55))) {
425
				/* write firmware */
426
				u8 obuf[19];
427
				obuf[0] = msg[j].addr << 1;
428
				obuf[1] = (msg[j].len > 15 ? 17 : msg[j].len);
429
				obuf[2] = msg[j].buf[0];
430
				len = msg[j].len - 1;
431
				i = 1;
432
				do {
433
					memcpy(obuf + 3, msg[j].buf + i,
434
							(len > 16 ? 16 : len));
435
					dw210x_op_rw(d->udev, 0xc2, 0, 0,
436
						obuf, (len > 16 ? 16 : len) + 3,
437
						DW210X_WRITE_MSG);
438
					i += 16;
439
					len -= 16;
440
				} while (len > 0);
441
			} else {
442
				/* write registers */
443
				u8 obuf[msg[j].len + 2];
444
				obuf[0] = msg[j].addr << 1;
445
				obuf[1] = msg[j].len;
446
				memcpy(obuf + 2, msg[j].buf, msg[j].len);
447
				dw210x_op_rw(d->udev, 0xc2, 0, 0,
448
						obuf, msg[j].len + 2,
449
						DW210X_WRITE_MSG);
450
			}
451
			break;
452
		}
453
		}
454

    
455
	}
456

    
457
	mutex_unlock(&d->i2c_mutex);
458
	return num;
459
}
460

    
461
static int dw3101_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[],
462
								int num)
463
{
464
	struct dvb_usb_device *d = i2c_get_adapdata(adap);
465
	int i;
466

    
467
	if (!d)
468
		return -ENODEV;
469
	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
470
		return -EAGAIN;
471

    
472
	switch (num) {
473
	case 2: {
474
		/* read */
475
		/* first write first register number */
476
		u8 ibuf[msg[1].len + 2], obuf[3];
477
		obuf[0] = msg[0].addr << 1;
478
		obuf[1] = msg[0].len;
479
		obuf[2] = msg[0].buf[0];
480
		dw210x_op_rw(d->udev, 0xc2, 0, 0,
481
				obuf, msg[0].len + 2, DW210X_WRITE_MSG);
482
		/* second read registers */
483
		dw210x_op_rw(d->udev, 0xc3, 0x19 , 0,
484
				ibuf, msg[1].len + 2, DW210X_READ_MSG);
485
		memcpy(msg[1].buf, ibuf + 2, msg[1].len);
486

    
487
		break;
488
	}
489
	case 1:
490
		switch (msg[0].addr) {
491
		case 0x60:
492
		case 0x0c: {
493
			/* write to register */
494
			u8 obuf[msg[0].len + 2];
495
			obuf[0] = msg[0].addr << 1;
496
			obuf[1] = msg[0].len;
497
			memcpy(obuf + 2, msg[0].buf, msg[0].len);
498
			dw210x_op_rw(d->udev, 0xc2, 0, 0,
499
					obuf, msg[0].len + 2, DW210X_WRITE_MSG);
500
			break;
501
		}
502
		case(DW2102_RC_QUERY): {
503
			u8 ibuf[2];
504
			dw210x_op_rw(d->udev, 0xb8, 0, 0,
505
					ibuf, 2, DW210X_READ_MSG);
506
			memcpy(msg[0].buf, ibuf , 2);
507
			break;
508
		}
509
		}
510

    
511
		break;
512
	}
513

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

    
520
	mutex_unlock(&d->i2c_mutex);
521
	return num;
522
}
523

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

    
531
	if (!d)
532
		return -ENODEV;
533
	udev = d->udev;
534
	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
535
		return -EAGAIN;
536

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

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

    
562
			obuf[0] = 5;
563
			obuf[1] = msg[j].buf[0];
564
			dw210x_op_rw(d->udev, 0x8a, 0, 0,
565
					obuf, 2, DW210X_WRITE_MSG);
566
			break;
567
		}
568
		/*case 0x55: cx24116
569
		case 0x6a: stv0903
570
		case 0x68: ds3000, stv0903, rs2000
571
		case 0x60: ts2020, stv6110, stb6100
572
		case 0xa0: eeprom */
573
		default: {
574
			if (msg[j].flags == I2C_M_RD) {
575
				/* read registers */
576
				u8 ibuf[msg[j].len];
577
				dw210x_op_rw(d->udev, 0x91, 0, 0,
578
						ibuf, msg[j].len,
579
						DW210X_READ_MSG);
580
				memcpy(msg[j].buf, ibuf, msg[j].len);
581
				break;
582
			} else if ((msg[j].buf[0] == 0xb0) &&
583
						(msg[j].addr == 0x68)) {
584
				/* write firmware */
585
				u8 obuf[19];
586
				obuf[0] = (msg[j].len > 16 ?
587
						18 : msg[j].len + 1);
588
				obuf[1] = msg[j].addr << 1;
589
				obuf[2] = msg[j].buf[0];
590
				len = msg[j].len - 1;
591
				i = 1;
592
				do {
593
					memcpy(obuf + 3, msg[j].buf + i,
594
							(len > 16 ? 16 : len));
595
					dw210x_op_rw(d->udev, 0x80, 0, 0,
596
						obuf, (len > 16 ? 16 : len) + 3,
597
						DW210X_WRITE_MSG);
598
					i += 16;
599
					len -= 16;
600
				} while (len > 0);
601
			} else if (j < (num - 1)) {
602
				/* write register addr before read */
603
				u8 obuf[msg[j].len + 2];
604
				obuf[0] = msg[j + 1].len;
605
				obuf[1] = (msg[j].addr << 1);
606
				memcpy(obuf + 2, msg[j].buf, msg[j].len);
607
				dw210x_op_rw(d->udev,
608
						udev->descriptor.idProduct ==
609
						0x7500 ? 0x92 : 0x90, 0, 0,
610
						obuf, msg[j].len + 2,
611
						DW210X_WRITE_MSG);
612
				break;
613
			} else {
614
				/* write registers */
615
				u8 obuf[msg[j].len + 2];
616
				obuf[0] = msg[j].len + 1;
617
				obuf[1] = (msg[j].addr << 1);
618
				memcpy(obuf + 2, msg[j].buf, msg[j].len);
619
				dw210x_op_rw(d->udev, 0x80, 0, 0,
620
						obuf, msg[j].len + 2,
621
						DW210X_WRITE_MSG);
622
				break;
623
			}
624
			break;
625
		}
626
		}
627
	}
628

    
629
	mutex_unlock(&d->i2c_mutex);
630
	return num;
631
}
632

    
633
static int su3000_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[],
634
								int num)
635
{
636
	struct dvb_usb_device *d = i2c_get_adapdata(adap);
637
	u8 obuf[0x40], ibuf[0x40];
638

    
639
	if (!d)
640
		return -ENODEV;
641
	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
642
		return -EAGAIN;
643

    
644
	switch (num) {
645
	case 1:
646
		switch (msg[0].addr) {
647
		case SU3000_STREAM_CTRL:
648
			obuf[0] = msg[0].buf[0] + 0x36;
649
			obuf[1] = 3;
650
			obuf[2] = 0;
651
			if (dvb_usb_generic_rw(d, obuf, 3, ibuf, 0, 0) < 0)
652
				err("i2c transfer failed.");
653
			break;
654
		case DW2102_RC_QUERY:
655
			obuf[0] = 0x10;
656
			if (dvb_usb_generic_rw(d, obuf, 1, ibuf, 2, 0) < 0)
657
				err("i2c transfer failed.");
658
			msg[0].buf[1] = ibuf[0];
659
			msg[0].buf[0] = ibuf[1];
660
			break;
661
		default:
662
			/* always i2c write*/
663
			obuf[0] = 0x08;
664
			obuf[1] = msg[0].addr;
665
			obuf[2] = msg[0].len;
666

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

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

    
673
		}
674
		break;
675
	case 2:
676
		/* always i2c read */
677
		obuf[0] = 0x09;
678
		obuf[1] = msg[0].len;
679
		obuf[2] = msg[1].len;
680
		obuf[3] = msg[0].addr;
681
		memcpy(&obuf[4], msg[0].buf, msg[0].len);
682

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

    
687
		memcpy(msg[1].buf, &ibuf[1], msg[1].len);
688
		break;
689
	default:
690
		warn("more than 2 i2c messages at a time is not handled yet.");
691
		break;
692
	}
693
	mutex_unlock(&d->i2c_mutex);
694
	return num;
695
}
696

    
697
static u32 dw210x_i2c_func(struct i2c_adapter *adapter)
698
{
699
	return I2C_FUNC_I2C;
700
}
701

    
702
static struct i2c_algorithm dw2102_i2c_algo = {
703
	.master_xfer = dw2102_i2c_transfer,
704
	.functionality = dw210x_i2c_func,
705
};
706

    
707
static struct i2c_algorithm dw2102_serit_i2c_algo = {
708
	.master_xfer = dw2102_serit_i2c_transfer,
709
	.functionality = dw210x_i2c_func,
710
};
711

    
712
static struct i2c_algorithm dw2102_earda_i2c_algo = {
713
	.master_xfer = dw2102_earda_i2c_transfer,
714
	.functionality = dw210x_i2c_func,
715
};
716

    
717
static struct i2c_algorithm dw2104_i2c_algo = {
718
	.master_xfer = dw2104_i2c_transfer,
719
	.functionality = dw210x_i2c_func,
720
};
721

    
722
static struct i2c_algorithm dw3101_i2c_algo = {
723
	.master_xfer = dw3101_i2c_transfer,
724
	.functionality = dw210x_i2c_func,
725
};
726

    
727
static struct i2c_algorithm s6x0_i2c_algo = {
728
	.master_xfer = s6x0_i2c_transfer,
729
	.functionality = dw210x_i2c_func,
730
};
731

    
732
static struct i2c_algorithm su3000_i2c_algo = {
733
	.master_xfer = su3000_i2c_transfer,
734
	.functionality = dw210x_i2c_func,
735
};
736

    
737
static int dw210x_read_mac_address(struct dvb_usb_device *d, u8 mac[6])
738
{
739
	int i;
740
	u8 ibuf[] = {0, 0};
741
	u8 eeprom[256], eepromline[16];
742

    
743
	for (i = 0; i < 256; i++) {
744
		if (dw210x_op_rw(d->udev, 0xb6, 0xa0 , i, ibuf, 2, DW210X_READ_MSG) < 0) {
745
			err("read eeprom failed.");
746
			return -1;
747
		} else {
748
			eepromline[i%16] = ibuf[0];
749
			eeprom[i] = ibuf[0];
750
		}
751
		if ((i % 16) == 15) {
752
			deb_xfer("%02x: ", i - 15);
753
			debug_dump(eepromline, 16, deb_xfer);
754
		}
755
	}
756

    
757
	memcpy(mac, eeprom + 8, 6);
758
	return 0;
759
};
760

    
761
static int s6x0_read_mac_address(struct dvb_usb_device *d, u8 mac[6])
762
{
763
	int i, ret;
764
	u8 ibuf[] = { 0 }, obuf[] = { 0 };
765
	u8 eeprom[256], eepromline[16];
766
	struct i2c_msg msg[] = {
767
		{
768
			.addr = 0xa0 >> 1,
769
			.flags = 0,
770
			.buf = obuf,
771
			.len = 1,
772
		}, {
773
			.addr = 0xa0 >> 1,
774
			.flags = I2C_M_RD,
775
			.buf = ibuf,
776
			.len = 1,
777
		}
778
	};
779

    
780
	for (i = 0; i < 256; i++) {
781
		obuf[0] = i;
782
		ret = s6x0_i2c_transfer(&d->i2c_adap, msg, 2);
783
		if (ret != 2) {
784
			err("read eeprom failed.");
785
			return -1;
786
		} else {
787
			eepromline[i % 16] = ibuf[0];
788
			eeprom[i] = ibuf[0];
789
		}
790

    
791
		if ((i % 16) == 15) {
792
			deb_xfer("%02x: ", i - 15);
793
			debug_dump(eepromline, 16, deb_xfer);
794
		}
795
	}
796

    
797
	memcpy(mac, eeprom + 16, 6);
798
	return 0;
799
};
800

    
801
static int su3000_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
802
{
803
	static u8 command_start[] = {0x00};
804
	static u8 command_stop[] = {0x01};
805
	struct i2c_msg msg = {
806
		.addr = SU3000_STREAM_CTRL,
807
		.flags = 0,
808
		.buf = onoff ? command_start : command_stop,
809
		.len = 1
810
	};
811

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

    
814
	return 0;
815
}
816

    
817
static int su3000_power_ctrl(struct dvb_usb_device *d, int i)
818
{
819
	struct su3000_state *state = (struct su3000_state *)d->priv;
820
	u8 obuf[] = {0xde, 0};
821

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

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

    
830
	return 0;
831
}
832

    
833
static int su3000_read_mac_address(struct dvb_usb_device *d, u8 mac[6])
834
{
835
	int i;
836
	u8 obuf[] = { 0x1f, 0xf0 };
837
	u8 ibuf[] = { 0 };
838
	struct i2c_msg msg[] = {
839
		{
840
			.addr = 0x51,
841
			.flags = 0,
842
			.buf = obuf,
843
			.len = 2,
844
		}, {
845
			.addr = 0x51,
846
			.flags = I2C_M_RD,
847
			.buf = ibuf,
848
			.len = 1,
849

    
850
		}
851
	};
852

    
853
	for (i = 0; i < 6; i++) {
854
		obuf[1] = 0xf0 + i;
855
		if (i2c_transfer(&d->i2c_adap, msg, 2) != 2)
856
			break;
857
		else
858
			mac[i] = ibuf[0];
859

    
860
	}
861

    
862
	debug_dump(mac, 6, printk);
863

    
864
	return 0;
865
}
866

    
867
static int su3000_identify_state(struct usb_device *udev,
868
				 struct dvb_usb_device_properties *props,
869
				 struct dvb_usb_device_description **desc,
870
				 int *cold)
871
{
872
	info("%s\n", __func__);
873

    
874
	*cold = 0;
875
	return 0;
876
}
877

    
878
static int dw210x_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
879
{
880
	static u8 command_13v[] = {0x00, 0x01};
881
	static u8 command_18v[] = {0x01, 0x01};
882
	static u8 command_off[] = {0x00, 0x00};
883
	struct i2c_msg msg = {
884
		.addr = DW2102_VOLTAGE_CTRL,
885
		.flags = 0,
886
		.buf = command_off,
887
		.len = 2,
888
	};
889

    
890
	struct dvb_usb_adapter *udev_adap =
891
		(struct dvb_usb_adapter *)(fe->dvb->priv);
892
	if (voltage == SEC_VOLTAGE_18)
893
		msg.buf = command_18v;
894
	else if (voltage == SEC_VOLTAGE_13)
895
		msg.buf = command_13v;
896

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

    
899
	return 0;
900
}
901

    
902
static int s660_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
903
{
904
	struct dvb_usb_adapter *d =
905
		(struct dvb_usb_adapter *)(fe->dvb->priv);
906
	struct s6x0_state *st = (struct s6x0_state *)d->dev->priv;
907

    
908
	dw210x_set_voltage(fe, voltage);
909
	if (st->old_set_voltage)
910
		st->old_set_voltage(fe, voltage);
911

    
912
	return 0;
913
}
914

    
915
static void dw210x_led_ctrl(struct dvb_frontend *fe, int offon)
916
{
917
	static u8 led_off[] = { 0 };
918
	static u8 led_on[] = { 1 };
919
	struct i2c_msg msg = {
920
		.addr = DW2102_LED_CTRL,
921
		.flags = 0,
922
		.buf = led_off,
923
		.len = 1
924
	};
925
	struct dvb_usb_adapter *udev_adap =
926
		(struct dvb_usb_adapter *)(fe->dvb->priv);
927

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

    
933
static struct stv0299_config sharp_z0194a_config = {
934
	.demod_address = 0x68,
935
	.inittab = sharp_z0194a_inittab,
936
	.mclk = 88000000UL,
937
	.invert = 1,
938
	.skip_reinit = 0,
939
	.lock_output = STV0299_LOCKOUTPUT_1,
940
	.volt13_op0_op1 = STV0299_VOLT13_OP1,
941
	.min_delay_ms = 100,
942
	.set_symbol_rate = sharp_z0194a_set_symbol_rate,
943
};
944

    
945
static struct cx24116_config dw2104_config = {
946
	.demod_address = 0x55,
947
	.mpg_clk_pos_pol = 0x01,
948
};
949

    
950
static struct si21xx_config serit_sp1511lhb_config = {
951
	.demod_address = 0x68,
952
	.min_delay_ms = 100,
953

    
954
};
955

    
956
static struct tda10023_config dw3101_tda10023_config = {
957
	.demod_address = 0x0c,
958
	.invert = 1,
959
};
960

    
961
static struct mt312_config zl313_config = {
962
	.demod_address = 0x0e,
963
};
964

    
965
static struct ds3000_config dw2104_ds3000_config = {
966
	.demod_address = 0x68,
967
};
968

    
969
static struct ts2020_config dw2104_ts2020_config  = {
970
	.tuner_address = 0x60,
971
	.clk_out_div = 1,
972
};
973

    
974
static struct ds3000_config s660_ds3000_config = {
975
	.demod_address = 0x68,
976
	.ci_mode = 1,
977
	.set_lock_led = dw210x_led_ctrl,
978
};
979

    
980
static struct stv0900_config dw2104a_stv0900_config = {
981
	.demod_address = 0x6a,
982
	.demod_mode = 0,
983
	.xtal = 27000000,
984
	.clkmode = 3,/* 0-CLKI, 2-XTALI, else AUTO */
985
	.diseqc_mode = 2,/* 2/3 PWM */
986
	.tun1_maddress = 0,/* 0x60 */
987
	.tun1_adc = 0,/* 2 Vpp */
988
	.path1_mode = 3,
989
};
990

    
991
static struct stb6100_config dw2104a_stb6100_config = {
992
	.tuner_address = 0x60,
993
	.refclock = 27000000,
994
};
995

    
996
static struct stv0900_config dw2104_stv0900_config = {
997
	.demod_address = 0x68,
998
	.demod_mode = 0,
999
	.xtal = 8000000,
1000
	.clkmode = 3,
1001
	.diseqc_mode = 2,
1002
	.tun1_maddress = 0,
1003
	.tun1_adc = 1,/* 1 Vpp */
1004
	.path1_mode = 3,
1005
};
1006

    
1007
static struct stv6110_config dw2104_stv6110_config = {
1008
	.i2c_address = 0x60,
1009
	.mclk = 16000000,
1010
	.clk_div = 1,
1011
};
1012

    
1013
static struct stv0900_config prof_7500_stv0900_config = {
1014
	.demod_address = 0x6a,
1015
	.demod_mode = 0,
1016
	.xtal = 27000000,
1017
	.clkmode = 3,/* 0-CLKI, 2-XTALI, else AUTO */
1018
	.diseqc_mode = 2,/* 2/3 PWM */
1019
	.tun1_maddress = 0,/* 0x60 */
1020
	.tun1_adc = 0,/* 2 Vpp */
1021
	.path1_mode = 3,
1022
	.tun1_type = 3,
1023
	.set_lock_led = dw210x_led_ctrl,
1024
};
1025

    
1026
static struct ds3000_config su3000_ds3000_config = {
1027
	.demod_address = 0x68,
1028
	.ci_mode = 1,
1029
	.set_lock_led = dw210x_led_ctrl,
1030
};
1031

    
1032
static struct ds3103_config su3000_ds3103_config = {
1033
	.demod_address = 0x68,
1034
	.ci_mode = 0,
1035
	.set_lock_led = dw210x_led_ctrl,
1036
};
1037

    
1038
static u8 m88rs2000_inittab[] = {
1039
	DEMOD_WRITE, 0x9a, 0x30,
1040
	DEMOD_WRITE, 0x00, 0x01,
1041
	WRITE_DELAY, 0x19, 0x00,
1042
	DEMOD_WRITE, 0x00, 0x00,
1043
	DEMOD_WRITE, 0x9a, 0xb0,
1044
	DEMOD_WRITE, 0x81, 0xc1,
1045
	DEMOD_WRITE, 0x81, 0x81,
1046
	DEMOD_WRITE, 0x86, 0xc6,
1047
	DEMOD_WRITE, 0x9a, 0x30,
1048
	DEMOD_WRITE, 0xf0, 0x80,
1049
	DEMOD_WRITE, 0xf1, 0xbf,
1050
	DEMOD_WRITE, 0xb0, 0x45,
1051
	DEMOD_WRITE, 0xb2, 0x01,
1052
	DEMOD_WRITE, 0x9a, 0xb0,
1053
	0xff, 0xaa, 0xff
1054
};
1055

    
1056
static struct m88rs2000_config s421_m88rs2000_config = {
1057
	.demod_addr = 0x68,
1058
	.inittab = m88rs2000_inittab,
1059
};
1060

    
1061
static int dw2104_frontend_attach(struct dvb_usb_adapter *d)
1062
{
1063
	struct dvb_tuner_ops *tuner_ops = NULL;
1064

    
1065
	if (demod_probe & 4) {
1066
		d->fe_adap[0].fe = dvb_attach(stv0900_attach, &dw2104a_stv0900_config,
1067
				&d->dev->i2c_adap, 0);
1068
		if (d->fe_adap[0].fe != NULL) {
1069
			if (dvb_attach(stb6100_attach, d->fe_adap[0].fe,
1070
					&dw2104a_stb6100_config,
1071
					&d->dev->i2c_adap)) {
1072
				tuner_ops = &d->fe_adap[0].fe->ops.tuner_ops;
1073
				tuner_ops->set_frequency = stb6100_set_freq;
1074
				tuner_ops->get_frequency = stb6100_get_freq;
1075
				tuner_ops->set_bandwidth = stb6100_set_bandw;
1076
				tuner_ops->get_bandwidth = stb6100_get_bandw;
1077
				d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1078
				info("Attached STV0900+STB6100!\n");
1079
				return 0;
1080
			}
1081
		}
1082
	}
1083

    
1084
	if (demod_probe & 2) {
1085
		d->fe_adap[0].fe = dvb_attach(stv0900_attach, &dw2104_stv0900_config,
1086
				&d->dev->i2c_adap, 0);
1087
		if (d->fe_adap[0].fe != NULL) {
1088
			if (dvb_attach(stv6110_attach, d->fe_adap[0].fe,
1089
					&dw2104_stv6110_config,
1090
					&d->dev->i2c_adap)) {
1091
				d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1092
				info("Attached STV0900+STV6110A!\n");
1093
				return 0;
1094
			}
1095
		}
1096
	}
1097

    
1098
	if (demod_probe & 1) {
1099
		d->fe_adap[0].fe = dvb_attach(cx24116_attach, &dw2104_config,
1100
				&d->dev->i2c_adap);
1101
		if (d->fe_adap[0].fe != NULL) {
1102
			d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1103
			info("Attached cx24116!\n");
1104
			return 0;
1105
		}
1106
	}
1107

    
1108
	d->fe_adap[0].fe = dvb_attach(ds3000_attach, &dw2104_ds3000_config,
1109
			&d->dev->i2c_adap);
1110
	if (d->fe_adap[0].fe != NULL) {
1111
		dvb_attach(ts2020_attach, d->fe_adap[0].fe,
1112
			&dw2104_ts2020_config, &d->dev->i2c_adap);
1113
		d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1114
		info("Attached DS3000!\n");
1115
		return 0;
1116
	}
1117

    
1118
	return -EIO;
1119
}
1120

    
1121
static struct dvb_usb_device_properties dw2102_properties;
1122
static struct dvb_usb_device_properties dw2104_properties;
1123
static struct dvb_usb_device_properties s6x0_properties;
1124

    
1125
static int dw2102_frontend_attach(struct dvb_usb_adapter *d)
1126
{
1127
	if (dw2102_properties.i2c_algo == &dw2102_serit_i2c_algo) {
1128
		/*dw2102_properties.adapter->tuner_attach = NULL;*/
1129
		d->fe_adap[0].fe = dvb_attach(si21xx_attach, &serit_sp1511lhb_config,
1130
					&d->dev->i2c_adap);
1131
		if (d->fe_adap[0].fe != NULL) {
1132
			d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1133
			info("Attached si21xx!\n");
1134
			return 0;
1135
		}
1136
	}
1137

    
1138
	if (dw2102_properties.i2c_algo == &dw2102_earda_i2c_algo) {
1139
		d->fe_adap[0].fe = dvb_attach(stv0288_attach, &earda_config,
1140
					&d->dev->i2c_adap);
1141
		if (d->fe_adap[0].fe != NULL) {
1142
			if (dvb_attach(stb6000_attach, d->fe_adap[0].fe, 0x61,
1143
					&d->dev->i2c_adap)) {
1144
				d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1145
				info("Attached stv0288!\n");
1146
				return 0;
1147
			}
1148
		}
1149
	}
1150

    
1151
	if (dw2102_properties.i2c_algo == &dw2102_i2c_algo) {
1152
		/*dw2102_properties.adapter->tuner_attach = dw2102_tuner_attach;*/
1153
		d->fe_adap[0].fe = dvb_attach(stv0299_attach, &sharp_z0194a_config,
1154
					&d->dev->i2c_adap);
1155
		if (d->fe_adap[0].fe != NULL) {
1156
			d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1157
			info("Attached stv0299!\n");
1158
			return 0;
1159
		}
1160
	}
1161
	return -EIO;
1162
}
1163

    
1164
static int dw3101_frontend_attach(struct dvb_usb_adapter *d)
1165
{
1166
	d->fe_adap[0].fe = dvb_attach(tda10023_attach, &dw3101_tda10023_config,
1167
				&d->dev->i2c_adap, 0x48);
1168
	if (d->fe_adap[0].fe != NULL) {
1169
		info("Attached tda10023!\n");
1170
		return 0;
1171
	}
1172
	return -EIO;
1173
}
1174

    
1175
static int zl100313_frontend_attach(struct dvb_usb_adapter *d)
1176
{
1177
	d->fe_adap[0].fe = dvb_attach(mt312_attach, &zl313_config,
1178
			&d->dev->i2c_adap);
1179
	if (d->fe_adap[0].fe != NULL) {
1180
		if (dvb_attach(zl10039_attach, d->fe_adap[0].fe, 0x60,
1181
				&d->dev->i2c_adap)) {
1182
			d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1183
			info("Attached zl100313+zl10039!\n");
1184
			return 0;
1185
		}
1186
	}
1187

    
1188
	return -EIO;
1189
}
1190

    
1191
static int stv0288_frontend_attach(struct dvb_usb_adapter *d)
1192
{
1193
	u8 obuf[] = {7, 1};
1194

    
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)
1199
		return -EIO;
1200

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

    
1204
	d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1205

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

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

    
1210
	return 0;
1211

    
1212
}
1213

    
1214
static int ds3000_frontend_attach(struct dvb_usb_adapter *d)
1215
{
1216
	struct s6x0_state *st = (struct s6x0_state *)d->dev->priv;
1217
	u8 obuf[] = {7, 1};
1218

    
1219
	d->fe_adap[0].fe = dvb_attach(ds3000_attach, &s660_ds3000_config,
1220
			&d->dev->i2c_adap);
1221

    
1222
	if (d->fe_adap[0].fe == NULL)
1223
		return -EIO;
1224

    
1225
	dvb_attach(ts2020_attach, d->fe_adap[0].fe, &dw2104_ts2020_config,
1226
		&d->dev->i2c_adap);
1227

    
1228
	st->old_set_voltage = d->fe_adap[0].fe->ops.set_voltage;
1229
	d->fe_adap[0].fe->ops.set_voltage = s660_set_voltage;
1230

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

    
1233
	info("Attached ds3000+ds2020!\n");
1234

    
1235
	return 0;
1236
}
1237

    
1238
static int prof_7500_frontend_attach(struct dvb_usb_adapter *d)
1239
{
1240
	u8 obuf[] = {7, 1};
1241

    
1242
	d->fe_adap[0].fe = dvb_attach(stv0900_attach, &prof_7500_stv0900_config,
1243
					&d->dev->i2c_adap, 0);
1244
	if (d->fe_adap[0].fe == NULL)
1245
		return -EIO;
1246

    
1247
	d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1248

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

    
1251
	info("Attached STV0900+STB6100A!\n");
1252

    
1253
	return 0;
1254
}
1255

    
1256
static int su3000_frontend_attach(struct dvb_usb_adapter *d)
1257
{
1258
	u8 obuf[3] = { 0xe, 0x80, 0 };
1259
	u8 ibuf[] = { 0 };
1260

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

    
1264
	obuf[0] = 0xe;
1265
	obuf[1] = 0x02;
1266
	obuf[2] = 1;
1267

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

    
1272
	obuf[0] = 0xe;
1273
	obuf[1] = 0x83;
1274
	obuf[2] = 0;
1275

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

    
1279
	obuf[0] = 0xe;
1280
	obuf[1] = 0x83;
1281
	obuf[2] = 1;
1282

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

    
1286
	obuf[0] = 0x51;
1287

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

    
1291
	d->fe_adap[0].fe = dvb_attach(ds3000_attach, &su3000_ds3000_config,
1292
					&d->dev->i2c_adap);
1293
	if (d->fe_adap[0].fe != NULL) {
1294
		if (dvb_attach(ts2020_attach, d->fe_adap[0].fe,
1295
					&dw2104_ts2020_config,
1296
					&d->dev->i2c_adap)) {
1297
			info("Attached DS3000/TS2020!\n");
1298
			return 0;
1299
		}
1300
	}
1301

    
1302
	d->fe_adap[0].fe = dvb_attach(ds3103_attach,
1303
					&su3000_ds3103_config,
1304
					&d->dev->i2c_adap);
1305
	if (d->fe_adap[0].fe != NULL) {
1306
		if (dvb_attach(ts2022_attach, d->fe_adap[0].fe, 0x60,
1307
				&d->dev->i2c_adap)) {
1308
			info("Attached DS3103/TS2022!\n");
1309
			return 0;
1310
		}
1311
	}
1312

    
1313
	info("Failed to attach DS3000/TS2020!\n");
1314
	return -EIO;
1315
}
1316

    
1317
static int m88rs2000_frontend_attach(struct dvb_usb_adapter *d)
1318
{
1319
	u8 obuf[] = { 0x51 };
1320
	u8 ibuf[] = { 0 };
1321

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

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

    
1328
	if (d->fe_adap[0].fe == NULL)
1329
		return -EIO;
1330

    
1331
	if (dvb_attach(ts2020_attach, d->fe_adap[0].fe,
1332
				&dw2104_ts2020_config,
1333
				&d->dev->i2c_adap)) {
1334
		info("Attached RS2000/TS2020!\n");
1335
		return 0;
1336
	}
1337

    
1338
	info("Failed to attach RS2000/TS2020!\n");
1339
	return -EIO;
1340
}
1341

    
1342
static int dw2102_tuner_attach(struct dvb_usb_adapter *adap)
1343
{
1344
	dvb_attach(dvb_pll_attach, adap->fe_adap[0].fe, 0x60,
1345
		&adap->dev->i2c_adap, DVB_PLL_OPERA1);
1346
	return 0;
1347
}
1348

    
1349
static int dw3101_tuner_attach(struct dvb_usb_adapter *adap)
1350
{
1351
	dvb_attach(dvb_pll_attach, adap->fe_adap[0].fe, 0x60,
1352
		&adap->dev->i2c_adap, DVB_PLL_TUA6034);
1353

    
1354
	return 0;
1355
}
1356

    
1357
static struct rc_map_table rc_map_dw210x_table[] = {
1358
	{ 0xf80a, KEY_POWER2 },		/*power*/
1359
	{ 0xf80c, KEY_MUTE },		/*mute*/
1360
	{ 0xf811, KEY_1 },
1361
	{ 0xf812, KEY_2 },
1362
	{ 0xf813, KEY_3 },
1363
	{ 0xf814, KEY_4 },
1364
	{ 0xf815, KEY_5 },
1365
	{ 0xf816, KEY_6 },
1366
	{ 0xf817, KEY_7 },
1367
	{ 0xf818, KEY_8 },
1368
	{ 0xf819, KEY_9 },
1369
	{ 0xf810, KEY_0 },
1370
	{ 0xf81c, KEY_CHANNELUP },	/*ch+*/
1371
	{ 0xf80f, KEY_CHANNELDOWN },	/*ch-*/
1372
	{ 0xf81a, KEY_VOLUMEUP },	/*vol+*/
1373
	{ 0xf80e, KEY_VOLUMEDOWN },	/*vol-*/
1374
	{ 0xf804, KEY_RECORD },		/*rec*/
1375
	{ 0xf809, KEY_FAVORITES },	/*fav*/
1376
	{ 0xf808, KEY_REWIND },		/*rewind*/
1377
	{ 0xf807, KEY_FASTFORWARD },	/*fast*/
1378
	{ 0xf80b, KEY_PAUSE },		/*pause*/
1379
	{ 0xf802, KEY_ESC },		/*cancel*/
1380
	{ 0xf803, KEY_TAB },		/*tab*/
1381
	{ 0xf800, KEY_UP },		/*up*/
1382
	{ 0xf81f, KEY_OK },		/*ok*/
1383
	{ 0xf801, KEY_DOWN },		/*down*/
1384
	{ 0xf805, KEY_CAMERA },		/*cap*/
1385
	{ 0xf806, KEY_STOP },		/*stop*/
1386
	{ 0xf840, KEY_ZOOM },		/*full*/
1387
	{ 0xf81e, KEY_TV },		/*tvmode*/
1388
	{ 0xf81b, KEY_LAST },		/*recall*/
1389
};
1390

    
1391
static struct rc_map_table rc_map_tevii_table[] = {
1392
	{ 0xf80a, KEY_POWER },
1393
	{ 0xf80c, KEY_MUTE },
1394
	{ 0xf811, KEY_1 },
1395
	{ 0xf812, KEY_2 },
1396
	{ 0xf813, KEY_3 },
1397
	{ 0xf814, KEY_4 },
1398
	{ 0xf815, KEY_5 },
1399
	{ 0xf816, KEY_6 },
1400
	{ 0xf817, KEY_7 },
1401
	{ 0xf818, KEY_8 },
1402
	{ 0xf819, KEY_9 },
1403
	{ 0xf810, KEY_0 },
1404
	{ 0xf81c, KEY_MENU },
1405
	{ 0xf80f, KEY_VOLUMEDOWN },
1406
	{ 0xf81a, KEY_LAST },
1407
	{ 0xf80e, KEY_OPEN },
1408
	{ 0xf804, KEY_RECORD },
1409
	{ 0xf809, KEY_VOLUMEUP },
1410
	{ 0xf808, KEY_CHANNELUP },
1411
	{ 0xf807, KEY_PVR },
1412
	{ 0xf80b, KEY_TIME },
1413
	{ 0xf802, KEY_RIGHT },
1414
	{ 0xf803, KEY_LEFT },
1415
	{ 0xf800, KEY_UP },
1416
	{ 0xf81f, KEY_OK },
1417
	{ 0xf801, KEY_DOWN },
1418
	{ 0xf805, KEY_TUNER },
1419
	{ 0xf806, KEY_CHANNELDOWN },
1420
	{ 0xf840, KEY_PLAYPAUSE },
1421
	{ 0xf81e, KEY_REWIND },
1422
	{ 0xf81b, KEY_FAVORITES },
1423
	{ 0xf81d, KEY_BACK },
1424
	{ 0xf84d, KEY_FASTFORWARD },
1425
	{ 0xf844, KEY_EPG },
1426
	{ 0xf84c, KEY_INFO },
1427
	{ 0xf841, KEY_AB },
1428
	{ 0xf843, KEY_AUDIO },
1429
	{ 0xf845, KEY_SUBTITLE },
1430
	{ 0xf84a, KEY_LIST },
1431
	{ 0xf846, KEY_F1 },
1432
	{ 0xf847, KEY_F2 },
1433
	{ 0xf85e, KEY_F3 },
1434
	{ 0xf85c, KEY_F4 },
1435
	{ 0xf852, KEY_F5 },
1436
	{ 0xf85a, KEY_F6 },
1437
	{ 0xf856, KEY_MODE },
1438
	{ 0xf858, KEY_SWITCHVIDEOMODE },
1439
};
1440

    
1441
static struct rc_map_table rc_map_tbs_table[] = {
1442
	{ 0xf884, KEY_POWER },
1443
	{ 0xf894, KEY_MUTE },
1444
	{ 0xf887, KEY_1 },
1445
	{ 0xf886, KEY_2 },
1446
	{ 0xf885, KEY_3 },
1447
	{ 0xf88b, KEY_4 },
1448
	{ 0xf88a, KEY_5 },
1449
	{ 0xf889, KEY_6 },
1450
	{ 0xf88f, KEY_7 },
1451
	{ 0xf88e, KEY_8 },
1452
	{ 0xf88d, KEY_9 },
1453
	{ 0xf892, KEY_0 },
1454
	{ 0xf896, KEY_CHANNELUP },
1455
	{ 0xf891, KEY_CHANNELDOWN },
1456
	{ 0xf893, KEY_VOLUMEUP },
1457
	{ 0xf88c, KEY_VOLUMEDOWN },
1458
	{ 0xf883, KEY_RECORD },
1459
	{ 0xf898, KEY_PAUSE  },
1460
	{ 0xf899, KEY_OK },
1461
	{ 0xf89a, KEY_SHUFFLE },
1462
	{ 0xf881, KEY_UP },
1463
	{ 0xf890, KEY_LEFT },
1464
	{ 0xf882, KEY_RIGHT },
1465
	{ 0xf888, KEY_DOWN },
1466
	{ 0xf895, KEY_FAVORITES },
1467
	{ 0xf897, KEY_SUBTITLE },
1468
	{ 0xf89d, KEY_ZOOM },
1469
	{ 0xf89f, KEY_EXIT },
1470
	{ 0xf89e, KEY_MENU },
1471
	{ 0xf89c, KEY_EPG },
1472
	{ 0xf880, KEY_PREVIOUS },
1473
	{ 0xf89b, KEY_MODE }
1474
};
1475

    
1476
static struct rc_map_table rc_map_su3000_table[] = {
1477
	{ 0x25, KEY_POWER },	/* right-bottom Red */
1478
	{ 0x0a, KEY_MUTE },	/* -/-- */
1479
	{ 0x01, KEY_1 },
1480
	{ 0x02, KEY_2 },
1481
	{ 0x03, KEY_3 },
1482
	{ 0x04, KEY_4 },
1483
	{ 0x05, KEY_5 },
1484
	{ 0x06, KEY_6 },
1485
	{ 0x07, KEY_7 },
1486
	{ 0x08, KEY_8 },
1487
	{ 0x09, KEY_9 },
1488
	{ 0x00, KEY_0 },
1489
	{ 0x20, KEY_UP },	/* CH+ */
1490
	{ 0x21, KEY_DOWN },	/* CH+ */
1491
	{ 0x12, KEY_VOLUMEUP },	/* Brightness Up */
1492
	{ 0x13, KEY_VOLUMEDOWN },/* Brightness Down */
1493
	{ 0x1f, KEY_RECORD },
1494
	{ 0x17, KEY_PLAY },
1495
	{ 0x16, KEY_PAUSE },
1496
	{ 0x0b, KEY_STOP },
1497
	{ 0x27, KEY_FASTFORWARD },/* >> */
1498
	{ 0x26, KEY_REWIND },	/* << */
1499
	{ 0x0d, KEY_OK },	/* Mute */
1500
	{ 0x11, KEY_LEFT },	/* VOL- */
1501
	{ 0x10, KEY_RIGHT },	/* VOL+ */
1502
	{ 0x29, KEY_BACK },	/* button under 9 */
1503
	{ 0x2c, KEY_MENU },	/* TTX */
1504
	{ 0x2b, KEY_EPG },	/* EPG */
1505
	{ 0x1e, KEY_RED },	/* OSD */
1506
	{ 0x0e, KEY_GREEN },	/* Window */
1507
	{ 0x2d, KEY_YELLOW },	/* button under << */
1508
	{ 0x0f, KEY_BLUE },	/* bottom yellow button */
1509
	{ 0x14, KEY_AUDIO },	/* Snapshot */
1510
	{ 0x38, KEY_TV },	/* TV/Radio */
1511
	{ 0x0c, KEY_ESC }	/* upper Red button */
1512
};
1513

    
1514
static struct rc_map_table rc_map_tt_4600_table[] = {
1515
	{ 0x41, KEY_POWER },
1516
	{ 0x42, KEY_SHUFFLE },
1517
	{ 0x43, KEY_1 },
1518
	{ 0x44, KEY_2 },
1519
	{ 0x45, KEY_3 },
1520
	{ 0x46, KEY_4 },
1521
	{ 0x47, KEY_5 },
1522
	{ 0x48, KEY_6 },
1523
	{ 0x49, KEY_7 },
1524
	{ 0x4a, KEY_8 },
1525
	{ 0x4b, KEY_9 },
1526
	{ 0x4c, KEY_0 },
1527
	{ 0x4d, KEY_UP },
1528
	{ 0x4e, KEY_LEFT },
1529
	{ 0x4f, KEY_OK },
1530
	{ 0x50, KEY_RIGHT },
1531
	{ 0x51, KEY_DOWN },
1532
	{ 0x52, KEY_INFO },
1533
	{ 0x53, KEY_EXIT },
1534
	{ 0x54, KEY_RED },
1535
	{ 0x55, KEY_GREEN },
1536
	{ 0x56, KEY_YELLOW },
1537
	{ 0x57, KEY_BLUE },
1538
	{ 0x58, KEY_MUTE },
1539
	{ 0x59, KEY_TEXT },
1540
	{ 0x5a, KEY_MODE },
1541
	{ 0x61, KEY_OPTION },
1542
	{ 0x62, KEY_EPG },
1543
	{ 0x63, KEY_CHANNELUP },
1544
	{ 0x64, KEY_CHANNELDOWN },
1545
	{ 0x65, KEY_VOLUMEUP },
1546
	{ 0x66, KEY_VOLUMEDOWN },
1547
	{ 0x67, KEY_SETUP },
1548
	{ 0x7a, KEY_RECORD },
1549
	{ 0x7b, KEY_PLAY },
1550
	{ 0x7c, KEY_STOP },
1551
	{ 0x7d, KEY_REWIND },
1552
	{ 0x7e, KEY_PAUSE },
1553
	{ 0x7f, KEY_FORWARD },
1554
};
1555

    
1556
static struct rc_map_dvb_usb_table_table keys_tables[] = {
1557
	{ rc_map_dw210x_table, ARRAY_SIZE(rc_map_dw210x_table) },
1558
	{ rc_map_tevii_table, ARRAY_SIZE(rc_map_tevii_table) },
1559
	{ rc_map_tbs_table, ARRAY_SIZE(rc_map_tbs_table) },
1560
	{ rc_map_su3000_table, ARRAY_SIZE(rc_map_su3000_table) },
1561
	{ rc_map_tt_4600_table, ARRAY_SIZE(rc_map_tt_4600_table) },
1562
};
1563

    
1564
static int dw2102_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
1565
{
1566
	struct rc_map_table *keymap = d->props.rc.legacy.rc_map_table;
1567
	int keymap_size = d->props.rc.legacy.rc_map_size;
1568
	u8 key[2];
1569
	struct i2c_msg msg = {
1570
		.addr = DW2102_RC_QUERY,
1571
		.flags = I2C_M_RD,
1572
		.buf = key,
1573
		.len = 2
1574
	};
1575
	int i;
1576
	/* override keymap */
1577
	if ((ir_keymap > 0) && (ir_keymap <= ARRAY_SIZE(keys_tables))) {
1578
		keymap = keys_tables[ir_keymap - 1].rc_keys ;
1579
		keymap_size = keys_tables[ir_keymap - 1].rc_keys_size;
1580
	} else if (ir_keymap > ARRAY_SIZE(keys_tables))
1581
		return 0; /* none */
1582

    
1583
	*state = REMOTE_NO_KEY_PRESSED;
1584
	if (d->props.i2c_algo->master_xfer(&d->i2c_adap, &msg, 1) != 1) 
1585
		return 0;
1586

    
1587
	for (i = 0; i < keymap_size ; i++) {
1588
		if (rc5_data(&keymap[i]) == msg.buf[0]) {
1589
			*state = REMOTE_KEY_PRESSED;
1590
			*event = keymap[i].keycode;
1591
			break;
1592
		}
1593

    
1594
	}
1595

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

    
1603
	return 0;
1604
}
1605

    
1606
enum dw2102_table_entry {
1607
	CYPRESS_DW2102,
1608
	CYPRESS_DW2101,
1609
	CYPRESS_DW2104,
1610
	TEVII_S650,
1611
	TERRATEC_CINERGY_S,
1612
	CYPRESS_DW3101,
1613
	TEVII_S630,
1614
	PROF_1100,
1615
	TEVII_S660,
1616
	PROF_7500,
1617
	GENIATECH_SU3000,
1618
	TERRATEC_CINERGY_S2,
1619
	TEVII_S480_1,
1620
	TEVII_S480_2,
1621
	X3M_SPC1400HD,
1622
	TEVII_S421,
1623
	TEVII_S632,
1624
	TERRATEC_CINERGY_S2_R2,
1625
	TT_S2_4600,
1626
	GOTVIEW_SAT_HD,
1627
	VP2000,
1628
	TEVII_S482_1,
1629
	TEVII_S482_2,
1630
};
1631

    
1632
static struct usb_device_id dw2102_table[] = {
1633
	[CYPRESS_DW2102] = {USB_DEVICE(USB_VID_CYPRESS, USB_PID_DW2102)},
1634
	[CYPRESS_DW2101] = {USB_DEVICE(USB_VID_CYPRESS, 0x2101)},
1635
	[CYPRESS_DW2104] = {USB_DEVICE(USB_VID_CYPRESS, USB_PID_DW2104)},
1636
	[TEVII_S650] = {USB_DEVICE(0x9022, USB_PID_TEVII_S650)},
1637
	[TERRATEC_CINERGY_S] = {USB_DEVICE(USB_VID_TERRATEC, USB_PID_CINERGY_S)},
1638
	[CYPRESS_DW3101] = {USB_DEVICE(USB_VID_CYPRESS, USB_PID_DW3101)},
1639
	[TEVII_S630] = {USB_DEVICE(0x9022, USB_PID_TEVII_S630)},
1640
	[PROF_1100] = {USB_DEVICE(0x3011, USB_PID_PROF_1100)},
1641
	[TEVII_S660] = {USB_DEVICE(0x9022, USB_PID_TEVII_S660)},
1642
	[PROF_7500] = {USB_DEVICE(0x3034, 0x7500)},
1643
	[GENIATECH_SU3000] = {USB_DEVICE(0x1f4d, 0x3000)},
1644
	[TERRATEC_CINERGY_S2] = {USB_DEVICE(USB_VID_TERRATEC, 0x00a8)},
1645
	[TEVII_S480_1] = {USB_DEVICE(0x153B, USB_PID_TEVII_S480_1)},
1646
	[TEVII_S480_2] = {USB_DEVICE(0x153B, USB_PID_TEVII_S480_2)},
1647
	[X3M_SPC1400HD] = {USB_DEVICE(0x1f4d, 0x3100)},
1648
	[TEVII_S421] = {USB_DEVICE(0x9022, USB_PID_TEVII_S421)},
1649
	[TEVII_S632] = {USB_DEVICE(0x9022, USB_PID_TEVII_S632)},
1650
	[TERRATEC_CINERGY_S2_R2] = {USB_DEVICE(USB_VID_TERRATEC, 0x00b0)},
1651
	[TT_S2_4600] = {USB_DEVICE(0x0b48, 0x3011)},
1652
	[GOTVIEW_SAT_HD] = {USB_DEVICE(0x1FE1, USB_PID_GOTVIEW_SAT_HD)},
1653
	[VP2000] = {USB_DEVICE(0x9022, 0x2000)},
1654
	[TEVII_S482_1] = {USB_DEVICE(0x153B, USB_PID_TEVII_S482_1)},
1655
	[TEVII_S482_2] = {USB_DEVICE(0x153B, USB_PID_TEVII_S482_2)},
1656
	{ },
1657
};
1658

    
1659
MODULE_DEVICE_TABLE(usb, dw2102_table);
1660

    
1661
static int dw2102_load_firmware(struct usb_device *dev,
1662
			const struct firmware *frmwr)
1663
{
1664
	u8 *b, *p;
1665
	int ret = 0, i;
1666
	u8 reset;
1667
	u8 reset16[] = {0, 0, 0, 0, 0, 0, 0};
1668
	const struct firmware *fw;
1669

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

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

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

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

    
1779
	.i2c_algo = &dw2102_serit_i2c_algo,
1780

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

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

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

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

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

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

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

    
1892
	.generic_bulk_ctrl_endpoint = 0x81,
1893
	/* parameter for the MPEG2-data transfer */
1894
	.num_adapters = 1,
1895
	.download_firmware = dw2102_load_firmware,
1896
	.read_mac_address = dw210x_read_mac_address,
1897
	.adapter = {
1898
		{
1899
		.num_frontends = 1,
1900
		.fe = {{
1901
			.frontend_attach = dw3101_frontend_attach,
1902
			.tuner_attach = dw3101_tuner_attach,
1903
			.stream = {
1904
				.type = USB_BULK,
1905
				.count = 8,
1906
				.endpoint = 0x82,
1907
				.u = {
1908
					.bulk = {
1909
						.buffersize = 4096,
1910
					}
1911
				}
1912
			},
1913
		}},
1914
		}
1915
	},
1916
	.num_device_descs = 1,
1917
	.devices = {
1918
		{ "DVBWorld DVB-C 3101 USB2.0",
1919
			{&dw2102_table[CYPRESS_DW3101], NULL},
1920
			{NULL},
1921
		},
1922
	}
1923
};
1924

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

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

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

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

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

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

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

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

    
2004
struct dvb_usb_device_properties *s421;
2005
static struct dvb_usb_device_description d421 = {
2006
	"TeVii S421 PCI",
2007
	{&dw2102_table[TEVII_S421], NULL},
2008
	{NULL},
2009
};
2010

    
2011
static struct dvb_usb_device_description d632 = {
2012
	"TeVii S632 USB",
2013
	{&dw2102_table[TEVII_S632], NULL},
2014
	{NULL},
2015
};
2016

    
2017
static struct dvb_usb_device_properties su3000_properties = {
2018
	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
2019
	.usb_ctrl = DEVICE_SPECIFIC,
2020
	.size_of_priv = sizeof(struct su3000_state),
2021
	.power_ctrl = su3000_power_ctrl,
2022
	.num_adapters = 1,
2023
	.identify_state	= su3000_identify_state,
2024
	.i2c_algo = &su3000_i2c_algo,
2025

    
2026
	.rc.legacy = {
2027
		.rc_map_table = rc_map_su3000_table,
2028
		.rc_map_size = ARRAY_SIZE(rc_map_su3000_table),
2029
		.rc_interval = 150,
2030
		.rc_query = dw2102_rc_query,
2031
	},
2032

    
2033
	.read_mac_address = su3000_read_mac_address,
2034

    
2035
	.generic_bulk_ctrl_endpoint = 0x01,
2036

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

    
2081
static struct dvb_usb_device_description d2000 = {
2082
	"VisionPlus VP2000 USB",
2083
	{&dw2102_table[VP2000], NULL},
2084
	{NULL},
2085
};
2086

    
2087
struct dvb_usb_device_properties *s472;
2088
static struct dvb_usb_device_description d472 = {
2089
	"TT Connect S2 4600",
2090
	{&dw2102_table[TT_S2_4600], NULL},
2091
	{NULL},
2092
};
2093

    
2094
static struct dvb_usb_device_description d482_1 = {
2095
	"TeVii S482.1 USB",
2096
	{&dw2102_table[TEVII_S482_1], NULL},
2097
	{NULL},
2098
};
2099

    
2100
static struct dvb_usb_device_description d482_2 = {
2101
	"TeVii S482.2 USB",
2102
	{&dw2102_table[TEVII_S482_2], NULL},
2103
	{NULL},
2104
};
2105

    
2106
static int dw2102_probe(struct usb_interface *intf,
2107
		const struct usb_device_id *id)
2108
{
2109
	p1100 = kmemdup(&s6x0_properties,
2110
			sizeof(struct dvb_usb_device_properties), GFP_KERNEL);
2111
	if (!p1100)
2112
		return -ENOMEM;
2113
	/* copy default structure */
2114
	/* fill only different fields */
2115
	p1100->firmware = P1100_FIRMWARE;
2116
	p1100->devices[0] = d1100;
2117
	p1100->rc.legacy.rc_map_table = rc_map_tbs_table;
2118
	p1100->rc.legacy.rc_map_size = ARRAY_SIZE(rc_map_tbs_table);
2119
	p1100->adapter->fe[0].frontend_attach = stv0288_frontend_attach;
2120

    
2121
	s660 = kmemdup(&s6x0_properties,
2122
		       sizeof(struct dvb_usb_device_properties), GFP_KERNEL);
2123
	if (!s660) {
2124
		kfree(p1100);
2125
		return -ENOMEM;
2126
	}
2127
	s660->firmware = S660_FIRMWARE;
2128
	s660->num_device_descs = 3;
2129
	s660->devices[0] = d660;
2130
	s660->devices[1] = d480_1;
2131
	s660->devices[2] = d480_2;
2132
	s660->adapter->fe[0].frontend_attach = ds3000_frontend_attach;
2133

    
2134
	p7500 = kmemdup(&s6x0_properties,
2135
			sizeof(struct dvb_usb_device_properties), GFP_KERNEL);
2136
	if (!p7500) {
2137
		kfree(p1100);
2138
		kfree(s660);
2139
		return -ENOMEM;
2140
	}
2141
	p7500->firmware = P7500_FIRMWARE;
2142
	p7500->devices[0] = d7500;
2143
	p7500->rc.legacy.rc_map_table = rc_map_tbs_table;
2144
	p7500->rc.legacy.rc_map_size = ARRAY_SIZE(rc_map_tbs_table);
2145
	p7500->adapter->fe[0].frontend_attach = prof_7500_frontend_attach;
2146

    
2147

    
2148
	s421 = kmemdup(&su3000_properties,
2149
		       sizeof(struct dvb_usb_device_properties), GFP_KERNEL);
2150
	if (!s421) {
2151
		kfree(p1100);
2152
		kfree(s660);
2153
		kfree(p7500);
2154
		return -ENOMEM;
2155
	}
2156
	s421->num_device_descs = 3;
2157
	s421->devices[0] = d421;
2158
	s421->devices[1] = d632;
2159
	s421->devices[2] = d2000;
2160
	s421->adapter->fe[0].frontend_attach = m88rs2000_frontend_attach;
2161

    
2162
	s472 = kmemdup(&su3000_properties,
2163
		       sizeof(struct dvb_usb_device_properties), GFP_KERNEL);
2164
	if (!s472) {
2165
		kfree(s421);
2166
		kfree(p1100);
2167
		kfree(s660);
2168
		kfree(p7500);
2169
		return -ENOMEM;
2170
	}
2171
	s472->num_device_descs = 3;
2172
	s472->devices[0] = d472;
2173
	s472->devices[1] = d482_1;
2174
	s472->devices[2] = d482_2;
2175
	s472->rc.legacy.rc_map_table = rc_map_tt_4600_table;
2176
	s472->rc.legacy.rc_map_size = ARRAY_SIZE(rc_map_tt_4600_table);
2177
	s472->rc.legacy.rc_interval = 250;
2178

    
2179
	if (0 == dvb_usb_device_init(intf, &dw2102_properties,
2180
			THIS_MODULE, NULL, adapter_nr) ||
2181
	    0 == dvb_usb_device_init(intf, &dw2104_properties,
2182
			THIS_MODULE, NULL, adapter_nr) ||
2183
	    0 == dvb_usb_device_init(intf, &dw3101_properties,
2184
			THIS_MODULE, NULL, adapter_nr) ||
2185
	    0 == dvb_usb_device_init(intf, &s6x0_properties,
2186
			THIS_MODULE, NULL, adapter_nr) ||
2187
	    0 == dvb_usb_device_init(intf, p1100,
2188
			THIS_MODULE, NULL, adapter_nr) ||
2189
	    0 == dvb_usb_device_init(intf, s660,
2190
			THIS_MODULE, NULL, adapter_nr) ||
2191
	    0 == dvb_usb_device_init(intf, p7500,
2192
			THIS_MODULE, NULL, adapter_nr) ||
2193
	    0 == dvb_usb_device_init(intf, s421,
2194
			THIS_MODULE, NULL, adapter_nr) ||
2195
	    0 == dvb_usb_device_init(intf, s472,
2196
			THIS_MODULE, NULL, adapter_nr) ||
2197
	    0 == dvb_usb_device_init(intf, &su3000_properties,
2198
				     THIS_MODULE, NULL, adapter_nr))
2199
		return 0;
2200

    
2201
	return -ENODEV;
2202
}
2203

    
2204
static struct usb_driver dw2102_driver = {
2205
	.name = "dw2102",
2206
	.probe = dw2102_probe,
2207
	.disconnect = dvb_usb_device_exit,
2208
	.id_table = dw2102_table,
2209
};
2210

    
2211
module_usb_driver(dw2102_driver);
2212

    
2213
MODULE_AUTHOR("Igor M. Liplianin (c) [email protected]");
2214
MODULE_DESCRIPTION("Driver for DVBWorld DVB-S 2101, 2102, DVB-S2 2104,"
2215
			" DVB-C 3101 USB2.0,"
2216
			" TeVii S600, S630, S650, S660, S480, S421, S632"
2217
			" Prof 1100, 7500 USB2.0,"
2218
			" Geniatech SU3000 devices");
2219
MODULE_VERSION("0.1");
2220
MODULE_LICENSE("GPL");
2221
MODULE_FIRMWARE(DW2101_FIRMWARE);
2222
MODULE_FIRMWARE(DW2102_FIRMWARE);
2223
MODULE_FIRMWARE(DW2104_FIRMWARE);
2224
MODULE_FIRMWARE(DW3101_FIRMWARE);
2225
MODULE_FIRMWARE(S630_FIRMWARE);
2226
MODULE_FIRMWARE(S660_FIRMWARE);
2227
MODULE_FIRMWARE(P1100_FIRMWARE);
2228
MODULE_FIRMWARE(P7500_FIRMWARE);
    (1-1/1)