xref: /openbmc/linux/drivers/media/usb/dvb-usb/dw2102.c (revision 356484ca)
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, T220,
6  *	TechnoTrend S2-4600 Cards
7  * Copyright (C) 2008-2012 Igor M. Liplianin (liplianin@me.by)
8  *
9  *	This program is free software; you can redistribute it and/or modify it
10  *	under the terms of the GNU General Public License as published by the
11  *	Free Software Foundation, version 2.
12  *
13  * see Documentation/dvb/README.dvb-usb for more information
14  */
15 #include "dw2102.h"
16 #include "si21xx.h"
17 #include "stv0299.h"
18 #include "z0194a.h"
19 #include "stv0288.h"
20 #include "stb6000.h"
21 #include "eds1547.h"
22 #include "cx24116.h"
23 #include "tda1002x.h"
24 #include "mt312.h"
25 #include "zl10039.h"
26 #include "ts2020.h"
27 #include "ds3000.h"
28 #include "stv0900.h"
29 #include "stv6110.h"
30 #include "stb6100.h"
31 #include "stb6100_proc.h"
32 #include "m88rs2000.h"
33 #include "tda18271.h"
34 #include "cxd2820r.h"
35 #include "m88ds3103.h"
36 #include "ts2020.h"
37 
38 /* Max transfer size done by I2C transfer functions */
39 #define MAX_XFER_SIZE  64
40 
41 #ifndef USB_PID_DW2102
42 #define USB_PID_DW2102 0x2102
43 #endif
44 
45 #ifndef USB_PID_DW2104
46 #define USB_PID_DW2104 0x2104
47 #endif
48 
49 #ifndef USB_PID_DW3101
50 #define USB_PID_DW3101 0x3101
51 #endif
52 
53 #ifndef USB_PID_CINERGY_S
54 #define USB_PID_CINERGY_S 0x0064
55 #endif
56 
57 #ifndef USB_PID_TEVII_S630
58 #define USB_PID_TEVII_S630 0xd630
59 #endif
60 
61 #ifndef USB_PID_TEVII_S650
62 #define USB_PID_TEVII_S650 0xd650
63 #endif
64 
65 #ifndef USB_PID_TEVII_S660
66 #define USB_PID_TEVII_S660 0xd660
67 #endif
68 
69 #ifndef USB_PID_TEVII_S480_1
70 #define USB_PID_TEVII_S480_1 0xd481
71 #endif
72 
73 #ifndef USB_PID_TEVII_S480_2
74 #define USB_PID_TEVII_S480_2 0xd482
75 #endif
76 
77 #ifndef USB_PID_PROF_1100
78 #define USB_PID_PROF_1100 0xb012
79 #endif
80 
81 #ifndef USB_PID_TEVII_S421
82 #define USB_PID_TEVII_S421 0xd421
83 #endif
84 
85 #ifndef USB_PID_TEVII_S632
86 #define USB_PID_TEVII_S632 0xd632
87 #endif
88 
89 #ifndef USB_PID_GOTVIEW_SAT_HD
90 #define USB_PID_GOTVIEW_SAT_HD 0x5456
91 #endif
92 
93 #define DW210X_READ_MSG 0
94 #define DW210X_WRITE_MSG 1
95 
96 #define REG_1F_SYMBOLRATE_BYTE0 0x1f
97 #define REG_20_SYMBOLRATE_BYTE1 0x20
98 #define REG_21_SYMBOLRATE_BYTE2 0x21
99 /* on my own*/
100 #define DW2102_VOLTAGE_CTRL (0x1800)
101 #define SU3000_STREAM_CTRL (0x1900)
102 #define DW2102_RC_QUERY (0x1a00)
103 #define DW2102_LED_CTRL (0x1b00)
104 
105 #define DW2101_FIRMWARE "dvb-usb-dw2101.fw"
106 #define DW2102_FIRMWARE "dvb-usb-dw2102.fw"
107 #define DW2104_FIRMWARE "dvb-usb-dw2104.fw"
108 #define DW3101_FIRMWARE "dvb-usb-dw3101.fw"
109 #define S630_FIRMWARE   "dvb-usb-s630.fw"
110 #define S660_FIRMWARE   "dvb-usb-s660.fw"
111 #define P1100_FIRMWARE  "dvb-usb-p1100.fw"
112 #define P7500_FIRMWARE  "dvb-usb-p7500.fw"
113 
114 #define	err_str "did not find the firmware file. (%s) " \
115 		"Please see linux/Documentation/dvb/ for more details " \
116 		"on firmware-problems."
117 
118 struct dw2102_state {
119 	u8 initialized;
120 	u8 last_lock;
121 	struct i2c_client *i2c_client_tuner;
122 
123 	/* fe hook functions*/
124 	int (*old_set_voltage)(struct dvb_frontend *f, fe_sec_voltage_t v);
125 	int (*fe_read_status)(struct dvb_frontend *fe,
126 		fe_status_t *status);
127 };
128 
129 /* debug */
130 static int dvb_usb_dw2102_debug;
131 module_param_named(debug, dvb_usb_dw2102_debug, int, 0644);
132 MODULE_PARM_DESC(debug, "set debugging level (1=info 2=xfer 4=rc(or-able))."
133 						DVB_USB_DEBUG_STATUS);
134 
135 /* demod probe */
136 static int demod_probe = 1;
137 module_param_named(demod, demod_probe, int, 0644);
138 MODULE_PARM_DESC(demod, "demod to probe (1=cx24116 2=stv0903+stv6110 "
139 			"4=stv0903+stb6100(or-able)).");
140 
141 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
142 
143 static int dw210x_op_rw(struct usb_device *dev, u8 request, u16 value,
144 			u16 index, u8 * data, u16 len, int flags)
145 {
146 	int ret;
147 	u8 *u8buf;
148 	unsigned int pipe = (flags == DW210X_READ_MSG) ?
149 				usb_rcvctrlpipe(dev, 0) : usb_sndctrlpipe(dev, 0);
150 	u8 request_type = (flags == DW210X_READ_MSG) ? USB_DIR_IN : USB_DIR_OUT;
151 
152 	u8buf = kmalloc(len, GFP_KERNEL);
153 	if (!u8buf)
154 		return -ENOMEM;
155 
156 
157 	if (flags == DW210X_WRITE_MSG)
158 		memcpy(u8buf, data, len);
159 	ret = usb_control_msg(dev, pipe, request, request_type | USB_TYPE_VENDOR,
160 				value, index , u8buf, len, 2000);
161 
162 	if (flags == DW210X_READ_MSG)
163 		memcpy(data, u8buf, len);
164 
165 	kfree(u8buf);
166 	return ret;
167 }
168 
169 /* I2C */
170 static int dw2102_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[],
171 		int num)
172 {
173 	struct dvb_usb_device *d = i2c_get_adapdata(adap);
174 	int i = 0;
175 	u8 buf6[] = {0x2c, 0x05, 0xc0, 0, 0, 0, 0};
176 	u16 value;
177 
178 	if (!d)
179 		return -ENODEV;
180 	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
181 		return -EAGAIN;
182 
183 	switch (num) {
184 	case 2:
185 		/* read stv0299 register */
186 		value = msg[0].buf[0];/* register */
187 		for (i = 0; i < msg[1].len; i++) {
188 			dw210x_op_rw(d->udev, 0xb5, value + i, 0,
189 					buf6, 2, DW210X_READ_MSG);
190 			msg[1].buf[i] = buf6[0];
191 		}
192 		break;
193 	case 1:
194 		switch (msg[0].addr) {
195 		case 0x68:
196 			/* write to stv0299 register */
197 			buf6[0] = 0x2a;
198 			buf6[1] = msg[0].buf[0];
199 			buf6[2] = msg[0].buf[1];
200 			dw210x_op_rw(d->udev, 0xb2, 0, 0,
201 					buf6, 3, DW210X_WRITE_MSG);
202 			break;
203 		case 0x60:
204 			if (msg[0].flags == 0) {
205 			/* write to tuner pll */
206 				buf6[0] = 0x2c;
207 				buf6[1] = 5;
208 				buf6[2] = 0xc0;
209 				buf6[3] = msg[0].buf[0];
210 				buf6[4] = msg[0].buf[1];
211 				buf6[5] = msg[0].buf[2];
212 				buf6[6] = msg[0].buf[3];
213 				dw210x_op_rw(d->udev, 0xb2, 0, 0,
214 						buf6, 7, DW210X_WRITE_MSG);
215 			} else {
216 			/* read from tuner */
217 				dw210x_op_rw(d->udev, 0xb5, 0, 0,
218 						buf6, 1, DW210X_READ_MSG);
219 				msg[0].buf[0] = buf6[0];
220 			}
221 			break;
222 		case (DW2102_RC_QUERY):
223 			dw210x_op_rw(d->udev, 0xb8, 0, 0,
224 					buf6, 2, DW210X_READ_MSG);
225 			msg[0].buf[0] = buf6[0];
226 			msg[0].buf[1] = buf6[1];
227 			break;
228 		case (DW2102_VOLTAGE_CTRL):
229 			buf6[0] = 0x30;
230 			buf6[1] = msg[0].buf[0];
231 			dw210x_op_rw(d->udev, 0xb2, 0, 0,
232 					buf6, 2, DW210X_WRITE_MSG);
233 			break;
234 		}
235 
236 		break;
237 	}
238 
239 	mutex_unlock(&d->i2c_mutex);
240 	return num;
241 }
242 
243 static int dw2102_serit_i2c_transfer(struct i2c_adapter *adap,
244 						struct i2c_msg msg[], int num)
245 {
246 	struct dvb_usb_device *d = i2c_get_adapdata(adap);
247 	u8 buf6[] = {0, 0, 0, 0, 0, 0, 0};
248 
249 	if (!d)
250 		return -ENODEV;
251 	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
252 		return -EAGAIN;
253 
254 	switch (num) {
255 	case 2:
256 		/* read si2109 register by number */
257 		buf6[0] = msg[0].addr << 1;
258 		buf6[1] = msg[0].len;
259 		buf6[2] = msg[0].buf[0];
260 		dw210x_op_rw(d->udev, 0xc2, 0, 0,
261 				buf6, msg[0].len + 2, DW210X_WRITE_MSG);
262 		/* read si2109 register */
263 		dw210x_op_rw(d->udev, 0xc3, 0xd0, 0,
264 				buf6, msg[1].len + 2, DW210X_READ_MSG);
265 		memcpy(msg[1].buf, buf6 + 2, msg[1].len);
266 
267 		break;
268 	case 1:
269 		switch (msg[0].addr) {
270 		case 0x68:
271 			/* write to si2109 register */
272 			buf6[0] = msg[0].addr << 1;
273 			buf6[1] = msg[0].len;
274 			memcpy(buf6 + 2, msg[0].buf, msg[0].len);
275 			dw210x_op_rw(d->udev, 0xc2, 0, 0, buf6,
276 					msg[0].len + 2, DW210X_WRITE_MSG);
277 			break;
278 		case(DW2102_RC_QUERY):
279 			dw210x_op_rw(d->udev, 0xb8, 0, 0,
280 					buf6, 2, DW210X_READ_MSG);
281 			msg[0].buf[0] = buf6[0];
282 			msg[0].buf[1] = buf6[1];
283 			break;
284 		case(DW2102_VOLTAGE_CTRL):
285 			buf6[0] = 0x30;
286 			buf6[1] = msg[0].buf[0];
287 			dw210x_op_rw(d->udev, 0xb2, 0, 0,
288 					buf6, 2, DW210X_WRITE_MSG);
289 			break;
290 		}
291 		break;
292 	}
293 
294 	mutex_unlock(&d->i2c_mutex);
295 	return num;
296 }
297 
298 static int dw2102_earda_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[], int num)
299 {
300 	struct dvb_usb_device *d = i2c_get_adapdata(adap);
301 	int ret;
302 
303 	if (!d)
304 		return -ENODEV;
305 	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
306 		return -EAGAIN;
307 
308 	switch (num) {
309 	case 2: {
310 		/* read */
311 		/* first write first register number */
312 		u8 ibuf[MAX_XFER_SIZE], obuf[3];
313 
314 		if (2 + msg[1].len > sizeof(ibuf)) {
315 			warn("i2c rd: len=%d is too big!\n",
316 			     msg[1].len);
317 			ret = -EOPNOTSUPP;
318 			goto unlock;
319 		}
320 
321 		obuf[0] = msg[0].addr << 1;
322 		obuf[1] = msg[0].len;
323 		obuf[2] = msg[0].buf[0];
324 		dw210x_op_rw(d->udev, 0xc2, 0, 0,
325 				obuf, msg[0].len + 2, DW210X_WRITE_MSG);
326 		/* second read registers */
327 		dw210x_op_rw(d->udev, 0xc3, 0xd1 , 0,
328 				ibuf, msg[1].len + 2, DW210X_READ_MSG);
329 		memcpy(msg[1].buf, ibuf + 2, msg[1].len);
330 
331 		break;
332 	}
333 	case 1:
334 		switch (msg[0].addr) {
335 		case 0x68: {
336 			/* write to register */
337 			u8 obuf[MAX_XFER_SIZE];
338 
339 			if (2 + msg[0].len > sizeof(obuf)) {
340 				warn("i2c wr: len=%d is too big!\n",
341 				     msg[1].len);
342 				ret = -EOPNOTSUPP;
343 				goto unlock;
344 			}
345 
346 			obuf[0] = msg[0].addr << 1;
347 			obuf[1] = msg[0].len;
348 			memcpy(obuf + 2, msg[0].buf, msg[0].len);
349 			dw210x_op_rw(d->udev, 0xc2, 0, 0,
350 					obuf, msg[0].len + 2, DW210X_WRITE_MSG);
351 			break;
352 		}
353 		case 0x61: {
354 			/* write to tuner */
355 			u8 obuf[MAX_XFER_SIZE];
356 
357 			if (2 + msg[0].len > sizeof(obuf)) {
358 				warn("i2c wr: len=%d is too big!\n",
359 				     msg[1].len);
360 				ret = -EOPNOTSUPP;
361 				goto unlock;
362 			}
363 
364 			obuf[0] = msg[0].addr << 1;
365 			obuf[1] = msg[0].len;
366 			memcpy(obuf + 2, msg[0].buf, msg[0].len);
367 			dw210x_op_rw(d->udev, 0xc2, 0, 0,
368 					obuf, msg[0].len + 2, DW210X_WRITE_MSG);
369 			break;
370 		}
371 		case(DW2102_RC_QUERY): {
372 			u8 ibuf[2];
373 			dw210x_op_rw(d->udev, 0xb8, 0, 0,
374 					ibuf, 2, DW210X_READ_MSG);
375 			memcpy(msg[0].buf, ibuf , 2);
376 			break;
377 		}
378 		case(DW2102_VOLTAGE_CTRL): {
379 			u8 obuf[2];
380 			obuf[0] = 0x30;
381 			obuf[1] = msg[0].buf[0];
382 			dw210x_op_rw(d->udev, 0xb2, 0, 0,
383 					obuf, 2, DW210X_WRITE_MSG);
384 			break;
385 		}
386 		}
387 
388 		break;
389 	}
390 	ret = num;
391 
392 unlock:
393 	mutex_unlock(&d->i2c_mutex);
394 	return ret;
395 }
396 
397 static int dw2104_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[], int num)
398 {
399 	struct dvb_usb_device *d = i2c_get_adapdata(adap);
400 	int len, i, j, ret;
401 
402 	if (!d)
403 		return -ENODEV;
404 	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
405 		return -EAGAIN;
406 
407 	for (j = 0; j < num; j++) {
408 		switch (msg[j].addr) {
409 		case(DW2102_RC_QUERY): {
410 			u8 ibuf[2];
411 			dw210x_op_rw(d->udev, 0xb8, 0, 0,
412 					ibuf, 2, DW210X_READ_MSG);
413 			memcpy(msg[j].buf, ibuf , 2);
414 			break;
415 		}
416 		case(DW2102_VOLTAGE_CTRL): {
417 			u8 obuf[2];
418 			obuf[0] = 0x30;
419 			obuf[1] = msg[j].buf[0];
420 			dw210x_op_rw(d->udev, 0xb2, 0, 0,
421 					obuf, 2, DW210X_WRITE_MSG);
422 			break;
423 		}
424 		/*case 0x55: cx24116
425 		case 0x6a: stv0903
426 		case 0x68: ds3000, stv0903
427 		case 0x60: ts2020, stv6110, stb6100 */
428 		default: {
429 			if (msg[j].flags == I2C_M_RD) {
430 				/* read registers */
431 				u8  ibuf[MAX_XFER_SIZE];
432 
433 				if (2 + msg[j].len > sizeof(ibuf)) {
434 					warn("i2c rd: len=%d is too big!\n",
435 					     msg[j].len);
436 					ret = -EOPNOTSUPP;
437 					goto unlock;
438 				}
439 
440 				dw210x_op_rw(d->udev, 0xc3,
441 						(msg[j].addr << 1) + 1, 0,
442 						ibuf, msg[j].len + 2,
443 						DW210X_READ_MSG);
444 				memcpy(msg[j].buf, ibuf + 2, msg[j].len);
445 				mdelay(10);
446 			} else if (((msg[j].buf[0] == 0xb0) &&
447 						(msg[j].addr == 0x68)) ||
448 						((msg[j].buf[0] == 0xf7) &&
449 						(msg[j].addr == 0x55))) {
450 				/* write firmware */
451 				u8 obuf[19];
452 				obuf[0] = msg[j].addr << 1;
453 				obuf[1] = (msg[j].len > 15 ? 17 : msg[j].len);
454 				obuf[2] = msg[j].buf[0];
455 				len = msg[j].len - 1;
456 				i = 1;
457 				do {
458 					memcpy(obuf + 3, msg[j].buf + i,
459 							(len > 16 ? 16 : len));
460 					dw210x_op_rw(d->udev, 0xc2, 0, 0,
461 						obuf, (len > 16 ? 16 : len) + 3,
462 						DW210X_WRITE_MSG);
463 					i += 16;
464 					len -= 16;
465 				} while (len > 0);
466 			} else {
467 				/* write registers */
468 				u8 obuf[MAX_XFER_SIZE];
469 
470 				if (2 + msg[j].len > sizeof(obuf)) {
471 					warn("i2c wr: len=%d is too big!\n",
472 					     msg[j].len);
473 					ret = -EOPNOTSUPP;
474 					goto unlock;
475 				}
476 
477 				obuf[0] = msg[j].addr << 1;
478 				obuf[1] = msg[j].len;
479 				memcpy(obuf + 2, msg[j].buf, msg[j].len);
480 				dw210x_op_rw(d->udev, 0xc2, 0, 0,
481 						obuf, msg[j].len + 2,
482 						DW210X_WRITE_MSG);
483 			}
484 			break;
485 		}
486 		}
487 
488 	}
489 	ret = num;
490 
491 unlock:
492 	mutex_unlock(&d->i2c_mutex);
493 	return ret;
494 }
495 
496 static int dw3101_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[],
497 								int num)
498 {
499 	struct dvb_usb_device *d = i2c_get_adapdata(adap);
500 	int ret;
501 	int i;
502 
503 	if (!d)
504 		return -ENODEV;
505 	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
506 		return -EAGAIN;
507 
508 	switch (num) {
509 	case 2: {
510 		/* read */
511 		/* first write first register number */
512 		u8 ibuf[MAX_XFER_SIZE], obuf[3];
513 
514 		if (2 + msg[1].len > sizeof(ibuf)) {
515 			warn("i2c rd: len=%d is too big!\n",
516 			     msg[1].len);
517 			ret = -EOPNOTSUPP;
518 			goto unlock;
519 		}
520 		obuf[0] = msg[0].addr << 1;
521 		obuf[1] = msg[0].len;
522 		obuf[2] = msg[0].buf[0];
523 		dw210x_op_rw(d->udev, 0xc2, 0, 0,
524 				obuf, msg[0].len + 2, DW210X_WRITE_MSG);
525 		/* second read registers */
526 		dw210x_op_rw(d->udev, 0xc3, 0x19 , 0,
527 				ibuf, msg[1].len + 2, DW210X_READ_MSG);
528 		memcpy(msg[1].buf, ibuf + 2, msg[1].len);
529 
530 		break;
531 	}
532 	case 1:
533 		switch (msg[0].addr) {
534 		case 0x60:
535 		case 0x0c: {
536 			/* write to register */
537 			u8 obuf[MAX_XFER_SIZE];
538 
539 			if (2 + msg[0].len > sizeof(obuf)) {
540 				warn("i2c wr: len=%d is too big!\n",
541 				     msg[0].len);
542 				ret = -EOPNOTSUPP;
543 				goto unlock;
544 			}
545 			obuf[0] = msg[0].addr << 1;
546 			obuf[1] = msg[0].len;
547 			memcpy(obuf + 2, msg[0].buf, msg[0].len);
548 			dw210x_op_rw(d->udev, 0xc2, 0, 0,
549 					obuf, msg[0].len + 2, DW210X_WRITE_MSG);
550 			break;
551 		}
552 		case(DW2102_RC_QUERY): {
553 			u8 ibuf[2];
554 			dw210x_op_rw(d->udev, 0xb8, 0, 0,
555 					ibuf, 2, DW210X_READ_MSG);
556 			memcpy(msg[0].buf, ibuf , 2);
557 			break;
558 		}
559 		}
560 
561 		break;
562 	}
563 
564 	for (i = 0; i < num; i++) {
565 		deb_xfer("%02x:%02x: %s ", i, msg[i].addr,
566 				msg[i].flags == 0 ? ">>>" : "<<<");
567 		debug_dump(msg[i].buf, msg[i].len, deb_xfer);
568 	}
569 	ret = num;
570 
571 unlock:
572 	mutex_unlock(&d->i2c_mutex);
573 	return ret;
574 }
575 
576 static int s6x0_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[],
577 								int num)
578 {
579 	struct dvb_usb_device *d = i2c_get_adapdata(adap);
580 	struct usb_device *udev;
581 	int len, i, j, ret;
582 
583 	if (!d)
584 		return -ENODEV;
585 	udev = d->udev;
586 	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
587 		return -EAGAIN;
588 
589 	for (j = 0; j < num; j++) {
590 		switch (msg[j].addr) {
591 		case (DW2102_RC_QUERY): {
592 			u8 ibuf[5];
593 			dw210x_op_rw(d->udev, 0xb8, 0, 0,
594 					ibuf, 5, DW210X_READ_MSG);
595 			memcpy(msg[j].buf, ibuf + 3, 2);
596 			break;
597 		}
598 		case (DW2102_VOLTAGE_CTRL): {
599 			u8 obuf[2];
600 
601 			obuf[0] = 1;
602 			obuf[1] = msg[j].buf[1];/* off-on */
603 			dw210x_op_rw(d->udev, 0x8a, 0, 0,
604 					obuf, 2, DW210X_WRITE_MSG);
605 			obuf[0] = 3;
606 			obuf[1] = msg[j].buf[0];/* 13v-18v */
607 			dw210x_op_rw(d->udev, 0x8a, 0, 0,
608 					obuf, 2, DW210X_WRITE_MSG);
609 			break;
610 		}
611 		case (DW2102_LED_CTRL): {
612 			u8 obuf[2];
613 
614 			obuf[0] = 5;
615 			obuf[1] = msg[j].buf[0];
616 			dw210x_op_rw(d->udev, 0x8a, 0, 0,
617 					obuf, 2, DW210X_WRITE_MSG);
618 			break;
619 		}
620 		/*case 0x55: cx24116
621 		case 0x6a: stv0903
622 		case 0x68: ds3000, stv0903, rs2000
623 		case 0x60: ts2020, stv6110, stb6100
624 		case 0xa0: eeprom */
625 		default: {
626 			if (msg[j].flags == I2C_M_RD) {
627 				/* read registers */
628 				u8 ibuf[MAX_XFER_SIZE];
629 
630 				if (msg[j].len > sizeof(ibuf)) {
631 					warn("i2c rd: len=%d is too big!\n",
632 					     msg[j].len);
633 					ret = -EOPNOTSUPP;
634 					goto unlock;
635 				}
636 
637 				dw210x_op_rw(d->udev, 0x91, 0, 0,
638 						ibuf, msg[j].len,
639 						DW210X_READ_MSG);
640 				memcpy(msg[j].buf, ibuf, msg[j].len);
641 				break;
642 			} else if ((msg[j].buf[0] == 0xb0) &&
643 						(msg[j].addr == 0x68)) {
644 				/* write firmware */
645 				u8 obuf[19];
646 				obuf[0] = (msg[j].len > 16 ?
647 						18 : msg[j].len + 1);
648 				obuf[1] = msg[j].addr << 1;
649 				obuf[2] = msg[j].buf[0];
650 				len = msg[j].len - 1;
651 				i = 1;
652 				do {
653 					memcpy(obuf + 3, msg[j].buf + i,
654 							(len > 16 ? 16 : len));
655 					dw210x_op_rw(d->udev, 0x80, 0, 0,
656 						obuf, (len > 16 ? 16 : len) + 3,
657 						DW210X_WRITE_MSG);
658 					i += 16;
659 					len -= 16;
660 				} while (len > 0);
661 			} else if (j < (num - 1)) {
662 				/* write register addr before read */
663 				u8 obuf[MAX_XFER_SIZE];
664 
665 				if (2 + msg[j].len > sizeof(obuf)) {
666 					warn("i2c wr: len=%d is too big!\n",
667 					     msg[j].len);
668 					ret = -EOPNOTSUPP;
669 					goto unlock;
670 				}
671 
672 				obuf[0] = msg[j + 1].len;
673 				obuf[1] = (msg[j].addr << 1);
674 				memcpy(obuf + 2, msg[j].buf, msg[j].len);
675 				dw210x_op_rw(d->udev,
676 						le16_to_cpu(udev->descriptor.idProduct) ==
677 						0x7500 ? 0x92 : 0x90, 0, 0,
678 						obuf, msg[j].len + 2,
679 						DW210X_WRITE_MSG);
680 				break;
681 			} else {
682 				/* write registers */
683 				u8 obuf[MAX_XFER_SIZE];
684 
685 				if (2 + msg[j].len > sizeof(obuf)) {
686 					warn("i2c wr: len=%d is too big!\n",
687 					     msg[j].len);
688 					ret = -EOPNOTSUPP;
689 					goto unlock;
690 				}
691 				obuf[0] = msg[j].len + 1;
692 				obuf[1] = (msg[j].addr << 1);
693 				memcpy(obuf + 2, msg[j].buf, msg[j].len);
694 				dw210x_op_rw(d->udev, 0x80, 0, 0,
695 						obuf, msg[j].len + 2,
696 						DW210X_WRITE_MSG);
697 				break;
698 			}
699 			break;
700 		}
701 		}
702 	}
703 	ret = num;
704 
705 unlock:
706 	mutex_unlock(&d->i2c_mutex);
707 	return ret;
708 }
709 
710 static int su3000_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg msg[],
711 								int num)
712 {
713 	struct dvb_usb_device *d = i2c_get_adapdata(adap);
714 	u8 obuf[0x40], ibuf[0x40];
715 
716 	if (!d)
717 		return -ENODEV;
718 	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
719 		return -EAGAIN;
720 
721 	switch (num) {
722 	case 1:
723 		switch (msg[0].addr) {
724 		case SU3000_STREAM_CTRL:
725 			obuf[0] = msg[0].buf[0] + 0x36;
726 			obuf[1] = 3;
727 			obuf[2] = 0;
728 			if (dvb_usb_generic_rw(d, obuf, 3, ibuf, 0, 0) < 0)
729 				err("i2c transfer failed.");
730 			break;
731 		case DW2102_RC_QUERY:
732 			obuf[0] = 0x10;
733 			if (dvb_usb_generic_rw(d, obuf, 1, ibuf, 2, 0) < 0)
734 				err("i2c transfer failed.");
735 			msg[0].buf[1] = ibuf[0];
736 			msg[0].buf[0] = ibuf[1];
737 			break;
738 		default:
739 			/* always i2c write*/
740 			obuf[0] = 0x08;
741 			obuf[1] = msg[0].addr;
742 			obuf[2] = msg[0].len;
743 
744 			memcpy(&obuf[3], msg[0].buf, msg[0].len);
745 
746 			if (dvb_usb_generic_rw(d, obuf, msg[0].len + 3,
747 						ibuf, 1, 0) < 0)
748 				err("i2c transfer failed.");
749 
750 		}
751 		break;
752 	case 2:
753 		/* always i2c read */
754 		obuf[0] = 0x09;
755 		obuf[1] = msg[0].len;
756 		obuf[2] = msg[1].len;
757 		obuf[3] = msg[0].addr;
758 		memcpy(&obuf[4], msg[0].buf, msg[0].len);
759 
760 		if (dvb_usb_generic_rw(d, obuf, msg[0].len + 4,
761 					ibuf, msg[1].len + 1, 0) < 0)
762 			err("i2c transfer failed.");
763 
764 		memcpy(msg[1].buf, &ibuf[1], msg[1].len);
765 		break;
766 	default:
767 		warn("more than 2 i2c messages at a time is not handled yet.");
768 		break;
769 	}
770 	mutex_unlock(&d->i2c_mutex);
771 	return num;
772 }
773 
774 static u32 dw210x_i2c_func(struct i2c_adapter *adapter)
775 {
776 	return I2C_FUNC_I2C;
777 }
778 
779 static struct i2c_algorithm dw2102_i2c_algo = {
780 	.master_xfer = dw2102_i2c_transfer,
781 	.functionality = dw210x_i2c_func,
782 };
783 
784 static struct i2c_algorithm dw2102_serit_i2c_algo = {
785 	.master_xfer = dw2102_serit_i2c_transfer,
786 	.functionality = dw210x_i2c_func,
787 };
788 
789 static struct i2c_algorithm dw2102_earda_i2c_algo = {
790 	.master_xfer = dw2102_earda_i2c_transfer,
791 	.functionality = dw210x_i2c_func,
792 };
793 
794 static struct i2c_algorithm dw2104_i2c_algo = {
795 	.master_xfer = dw2104_i2c_transfer,
796 	.functionality = dw210x_i2c_func,
797 };
798 
799 static struct i2c_algorithm dw3101_i2c_algo = {
800 	.master_xfer = dw3101_i2c_transfer,
801 	.functionality = dw210x_i2c_func,
802 };
803 
804 static struct i2c_algorithm s6x0_i2c_algo = {
805 	.master_xfer = s6x0_i2c_transfer,
806 	.functionality = dw210x_i2c_func,
807 };
808 
809 static struct i2c_algorithm su3000_i2c_algo = {
810 	.master_xfer = su3000_i2c_transfer,
811 	.functionality = dw210x_i2c_func,
812 };
813 
814 static int dw210x_read_mac_address(struct dvb_usb_device *d, u8 mac[6])
815 {
816 	int i;
817 	u8 ibuf[] = {0, 0};
818 	u8 eeprom[256], eepromline[16];
819 
820 	for (i = 0; i < 256; i++) {
821 		if (dw210x_op_rw(d->udev, 0xb6, 0xa0 , i, ibuf, 2, DW210X_READ_MSG) < 0) {
822 			err("read eeprom failed.");
823 			return -1;
824 		} else {
825 			eepromline[i%16] = ibuf[0];
826 			eeprom[i] = ibuf[0];
827 		}
828 		if ((i % 16) == 15) {
829 			deb_xfer("%02x: ", i - 15);
830 			debug_dump(eepromline, 16, deb_xfer);
831 		}
832 	}
833 
834 	memcpy(mac, eeprom + 8, 6);
835 	return 0;
836 };
837 
838 static int s6x0_read_mac_address(struct dvb_usb_device *d, u8 mac[6])
839 {
840 	int i, ret;
841 	u8 ibuf[] = { 0 }, obuf[] = { 0 };
842 	u8 eeprom[256], eepromline[16];
843 	struct i2c_msg msg[] = {
844 		{
845 			.addr = 0xa0 >> 1,
846 			.flags = 0,
847 			.buf = obuf,
848 			.len = 1,
849 		}, {
850 			.addr = 0xa0 >> 1,
851 			.flags = I2C_M_RD,
852 			.buf = ibuf,
853 			.len = 1,
854 		}
855 	};
856 
857 	for (i = 0; i < 256; i++) {
858 		obuf[0] = i;
859 		ret = s6x0_i2c_transfer(&d->i2c_adap, msg, 2);
860 		if (ret != 2) {
861 			err("read eeprom failed.");
862 			return -1;
863 		} else {
864 			eepromline[i % 16] = ibuf[0];
865 			eeprom[i] = ibuf[0];
866 		}
867 
868 		if ((i % 16) == 15) {
869 			deb_xfer("%02x: ", i - 15);
870 			debug_dump(eepromline, 16, deb_xfer);
871 		}
872 	}
873 
874 	memcpy(mac, eeprom + 16, 6);
875 	return 0;
876 };
877 
878 static int su3000_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
879 {
880 	static u8 command_start[] = {0x00};
881 	static u8 command_stop[] = {0x01};
882 	struct i2c_msg msg = {
883 		.addr = SU3000_STREAM_CTRL,
884 		.flags = 0,
885 		.buf = onoff ? command_start : command_stop,
886 		.len = 1
887 	};
888 
889 	i2c_transfer(&adap->dev->i2c_adap, &msg, 1);
890 
891 	return 0;
892 }
893 
894 static int su3000_power_ctrl(struct dvb_usb_device *d, int i)
895 {
896 	struct dw2102_state *state = (struct dw2102_state *)d->priv;
897 	u8 obuf[] = {0xde, 0};
898 
899 	info("%s: %d, initialized %d\n", __func__, i, state->initialized);
900 
901 	if (i && !state->initialized) {
902 		state->initialized = 1;
903 		/* reset board */
904 		dvb_usb_generic_rw(d, obuf, 2, NULL, 0, 0);
905 	}
906 
907 	return 0;
908 }
909 
910 static int su3000_read_mac_address(struct dvb_usb_device *d, u8 mac[6])
911 {
912 	int i;
913 	u8 obuf[] = { 0x1f, 0xf0 };
914 	u8 ibuf[] = { 0 };
915 	struct i2c_msg msg[] = {
916 		{
917 			.addr = 0x51,
918 			.flags = 0,
919 			.buf = obuf,
920 			.len = 2,
921 		}, {
922 			.addr = 0x51,
923 			.flags = I2C_M_RD,
924 			.buf = ibuf,
925 			.len = 1,
926 
927 		}
928 	};
929 
930 	for (i = 0; i < 6; i++) {
931 		obuf[1] = 0xf0 + i;
932 		if (i2c_transfer(&d->i2c_adap, msg, 2) != 2)
933 			break;
934 		else
935 			mac[i] = ibuf[0];
936 	}
937 
938 	return 0;
939 }
940 
941 static int su3000_identify_state(struct usb_device *udev,
942 				 struct dvb_usb_device_properties *props,
943 				 struct dvb_usb_device_description **desc,
944 				 int *cold)
945 {
946 	info("%s\n", __func__);
947 
948 	*cold = 0;
949 	return 0;
950 }
951 
952 static int dw210x_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
953 {
954 	static u8 command_13v[] = {0x00, 0x01};
955 	static u8 command_18v[] = {0x01, 0x01};
956 	static u8 command_off[] = {0x00, 0x00};
957 	struct i2c_msg msg = {
958 		.addr = DW2102_VOLTAGE_CTRL,
959 		.flags = 0,
960 		.buf = command_off,
961 		.len = 2,
962 	};
963 
964 	struct dvb_usb_adapter *udev_adap =
965 		(struct dvb_usb_adapter *)(fe->dvb->priv);
966 	if (voltage == SEC_VOLTAGE_18)
967 		msg.buf = command_18v;
968 	else if (voltage == SEC_VOLTAGE_13)
969 		msg.buf = command_13v;
970 
971 	i2c_transfer(&udev_adap->dev->i2c_adap, &msg, 1);
972 
973 	return 0;
974 }
975 
976 static int s660_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
977 {
978 	struct dvb_usb_adapter *d =
979 		(struct dvb_usb_adapter *)(fe->dvb->priv);
980 	struct dw2102_state *st = (struct dw2102_state *)d->dev->priv;
981 
982 	dw210x_set_voltage(fe, voltage);
983 	if (st->old_set_voltage)
984 		st->old_set_voltage(fe, voltage);
985 
986 	return 0;
987 }
988 
989 static void dw210x_led_ctrl(struct dvb_frontend *fe, int offon)
990 {
991 	static u8 led_off[] = { 0 };
992 	static u8 led_on[] = { 1 };
993 	struct i2c_msg msg = {
994 		.addr = DW2102_LED_CTRL,
995 		.flags = 0,
996 		.buf = led_off,
997 		.len = 1
998 	};
999 	struct dvb_usb_adapter *udev_adap =
1000 		(struct dvb_usb_adapter *)(fe->dvb->priv);
1001 
1002 	if (offon)
1003 		msg.buf = led_on;
1004 	i2c_transfer(&udev_adap->dev->i2c_adap, &msg, 1);
1005 }
1006 
1007 static int tt_s2_4600_read_status(struct dvb_frontend *fe, fe_status_t *status)
1008 {
1009 	struct dvb_usb_adapter *d =
1010 		(struct dvb_usb_adapter *)(fe->dvb->priv);
1011 	struct dw2102_state *st = (struct dw2102_state *)d->dev->priv;
1012 	int ret;
1013 
1014 	ret = st->fe_read_status(fe, status);
1015 
1016 	/* resync slave fifo when signal change from unlock to lock */
1017 	if ((*status & FE_HAS_LOCK) && (!st->last_lock))
1018 		su3000_streaming_ctrl(d, 1);
1019 
1020 	st->last_lock = (*status & FE_HAS_LOCK) ? 1 : 0;
1021 	return ret;
1022 }
1023 
1024 static struct stv0299_config sharp_z0194a_config = {
1025 	.demod_address = 0x68,
1026 	.inittab = sharp_z0194a_inittab,
1027 	.mclk = 88000000UL,
1028 	.invert = 1,
1029 	.skip_reinit = 0,
1030 	.lock_output = STV0299_LOCKOUTPUT_1,
1031 	.volt13_op0_op1 = STV0299_VOLT13_OP1,
1032 	.min_delay_ms = 100,
1033 	.set_symbol_rate = sharp_z0194a_set_symbol_rate,
1034 };
1035 
1036 static struct cx24116_config dw2104_config = {
1037 	.demod_address = 0x55,
1038 	.mpg_clk_pos_pol = 0x01,
1039 };
1040 
1041 static struct si21xx_config serit_sp1511lhb_config = {
1042 	.demod_address = 0x68,
1043 	.min_delay_ms = 100,
1044 
1045 };
1046 
1047 static struct tda10023_config dw3101_tda10023_config = {
1048 	.demod_address = 0x0c,
1049 	.invert = 1,
1050 };
1051 
1052 static struct mt312_config zl313_config = {
1053 	.demod_address = 0x0e,
1054 };
1055 
1056 static struct ds3000_config dw2104_ds3000_config = {
1057 	.demod_address = 0x68,
1058 };
1059 
1060 static struct ts2020_config dw2104_ts2020_config = {
1061 	.tuner_address = 0x60,
1062 	.clk_out_div = 1,
1063 	.frequency_div = 1060000,
1064 };
1065 
1066 static struct ds3000_config s660_ds3000_config = {
1067 	.demod_address = 0x68,
1068 	.ci_mode = 1,
1069 	.set_lock_led = dw210x_led_ctrl,
1070 };
1071 
1072 static struct ts2020_config s660_ts2020_config = {
1073 	.tuner_address = 0x60,
1074 	.clk_out_div = 1,
1075 	.frequency_div = 1146000,
1076 };
1077 
1078 static struct stv0900_config dw2104a_stv0900_config = {
1079 	.demod_address = 0x6a,
1080 	.demod_mode = 0,
1081 	.xtal = 27000000,
1082 	.clkmode = 3,/* 0-CLKI, 2-XTALI, else AUTO */
1083 	.diseqc_mode = 2,/* 2/3 PWM */
1084 	.tun1_maddress = 0,/* 0x60 */
1085 	.tun1_adc = 0,/* 2 Vpp */
1086 	.path1_mode = 3,
1087 };
1088 
1089 static struct stb6100_config dw2104a_stb6100_config = {
1090 	.tuner_address = 0x60,
1091 	.refclock = 27000000,
1092 };
1093 
1094 static struct stv0900_config dw2104_stv0900_config = {
1095 	.demod_address = 0x68,
1096 	.demod_mode = 0,
1097 	.xtal = 8000000,
1098 	.clkmode = 3,
1099 	.diseqc_mode = 2,
1100 	.tun1_maddress = 0,
1101 	.tun1_adc = 1,/* 1 Vpp */
1102 	.path1_mode = 3,
1103 };
1104 
1105 static struct stv6110_config dw2104_stv6110_config = {
1106 	.i2c_address = 0x60,
1107 	.mclk = 16000000,
1108 	.clk_div = 1,
1109 };
1110 
1111 static struct stv0900_config prof_7500_stv0900_config = {
1112 	.demod_address = 0x6a,
1113 	.demod_mode = 0,
1114 	.xtal = 27000000,
1115 	.clkmode = 3,/* 0-CLKI, 2-XTALI, else AUTO */
1116 	.diseqc_mode = 2,/* 2/3 PWM */
1117 	.tun1_maddress = 0,/* 0x60 */
1118 	.tun1_adc = 0,/* 2 Vpp */
1119 	.path1_mode = 3,
1120 	.tun1_type = 3,
1121 	.set_lock_led = dw210x_led_ctrl,
1122 };
1123 
1124 static struct ds3000_config su3000_ds3000_config = {
1125 	.demod_address = 0x68,
1126 	.ci_mode = 1,
1127 	.set_lock_led = dw210x_led_ctrl,
1128 };
1129 
1130 static struct cxd2820r_config cxd2820r_config = {
1131 	.i2c_address = 0x6c, /* (0xd8 >> 1) */
1132 	.ts_mode = 0x38,
1133 	.ts_clock_inv = 1,
1134 };
1135 
1136 static struct tda18271_config tda18271_config = {
1137 	.output_opt = TDA18271_OUTPUT_LT_OFF,
1138 	.gate = TDA18271_GATE_DIGITAL,
1139 };
1140 
1141 static const struct m88ds3103_config tt_s2_4600_m88ds3103_config = {
1142 	.i2c_addr = 0x68,
1143 	.clock = 27000000,
1144 	.i2c_wr_max = 33,
1145 	.ts_mode = M88DS3103_TS_CI,
1146 	.ts_clk = 16000,
1147 	.ts_clk_pol = 0,
1148 	.spec_inv = 0,
1149 	.agc_inv = 0,
1150 	.clock_out = M88DS3103_CLOCK_OUT_ENABLED,
1151 	.envelope_mode = 0,
1152 	.agc = 0x99,
1153 	.lnb_hv_pol = 1,
1154 	.lnb_en_pol = 0,
1155 };
1156 
1157 static u8 m88rs2000_inittab[] = {
1158 	DEMOD_WRITE, 0x9a, 0x30,
1159 	DEMOD_WRITE, 0x00, 0x01,
1160 	WRITE_DELAY, 0x19, 0x00,
1161 	DEMOD_WRITE, 0x00, 0x00,
1162 	DEMOD_WRITE, 0x9a, 0xb0,
1163 	DEMOD_WRITE, 0x81, 0xc1,
1164 	DEMOD_WRITE, 0x81, 0x81,
1165 	DEMOD_WRITE, 0x86, 0xc6,
1166 	DEMOD_WRITE, 0x9a, 0x30,
1167 	DEMOD_WRITE, 0xf0, 0x80,
1168 	DEMOD_WRITE, 0xf1, 0xbf,
1169 	DEMOD_WRITE, 0xb0, 0x45,
1170 	DEMOD_WRITE, 0xb2, 0x01,
1171 	DEMOD_WRITE, 0x9a, 0xb0,
1172 	0xff, 0xaa, 0xff
1173 };
1174 
1175 static struct m88rs2000_config s421_m88rs2000_config = {
1176 	.demod_addr = 0x68,
1177 	.inittab = m88rs2000_inittab,
1178 };
1179 
1180 static int dw2104_frontend_attach(struct dvb_usb_adapter *d)
1181 {
1182 	struct dvb_tuner_ops *tuner_ops = NULL;
1183 
1184 	if (demod_probe & 4) {
1185 		d->fe_adap[0].fe = dvb_attach(stv0900_attach, &dw2104a_stv0900_config,
1186 				&d->dev->i2c_adap, 0);
1187 		if (d->fe_adap[0].fe != NULL) {
1188 			if (dvb_attach(stb6100_attach, d->fe_adap[0].fe,
1189 					&dw2104a_stb6100_config,
1190 					&d->dev->i2c_adap)) {
1191 				tuner_ops = &d->fe_adap[0].fe->ops.tuner_ops;
1192 				tuner_ops->set_frequency = stb6100_set_freq;
1193 				tuner_ops->get_frequency = stb6100_get_freq;
1194 				tuner_ops->set_bandwidth = stb6100_set_bandw;
1195 				tuner_ops->get_bandwidth = stb6100_get_bandw;
1196 				d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1197 				info("Attached STV0900+STB6100!\n");
1198 				return 0;
1199 			}
1200 		}
1201 	}
1202 
1203 	if (demod_probe & 2) {
1204 		d->fe_adap[0].fe = dvb_attach(stv0900_attach, &dw2104_stv0900_config,
1205 				&d->dev->i2c_adap, 0);
1206 		if (d->fe_adap[0].fe != NULL) {
1207 			if (dvb_attach(stv6110_attach, d->fe_adap[0].fe,
1208 					&dw2104_stv6110_config,
1209 					&d->dev->i2c_adap)) {
1210 				d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1211 				info("Attached STV0900+STV6110A!\n");
1212 				return 0;
1213 			}
1214 		}
1215 	}
1216 
1217 	if (demod_probe & 1) {
1218 		d->fe_adap[0].fe = dvb_attach(cx24116_attach, &dw2104_config,
1219 				&d->dev->i2c_adap);
1220 		if (d->fe_adap[0].fe != NULL) {
1221 			d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1222 			info("Attached cx24116!\n");
1223 			return 0;
1224 		}
1225 	}
1226 
1227 	d->fe_adap[0].fe = dvb_attach(ds3000_attach, &dw2104_ds3000_config,
1228 			&d->dev->i2c_adap);
1229 	if (d->fe_adap[0].fe != NULL) {
1230 		dvb_attach(ts2020_attach, d->fe_adap[0].fe,
1231 			&dw2104_ts2020_config, &d->dev->i2c_adap);
1232 		d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1233 		info("Attached DS3000!\n");
1234 		return 0;
1235 	}
1236 
1237 	return -EIO;
1238 }
1239 
1240 static struct dvb_usb_device_properties dw2102_properties;
1241 static struct dvb_usb_device_properties dw2104_properties;
1242 static struct dvb_usb_device_properties s6x0_properties;
1243 
1244 static int dw2102_frontend_attach(struct dvb_usb_adapter *d)
1245 {
1246 	if (dw2102_properties.i2c_algo == &dw2102_serit_i2c_algo) {
1247 		/*dw2102_properties.adapter->tuner_attach = NULL;*/
1248 		d->fe_adap[0].fe = dvb_attach(si21xx_attach, &serit_sp1511lhb_config,
1249 					&d->dev->i2c_adap);
1250 		if (d->fe_adap[0].fe != NULL) {
1251 			d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1252 			info("Attached si21xx!\n");
1253 			return 0;
1254 		}
1255 	}
1256 
1257 	if (dw2102_properties.i2c_algo == &dw2102_earda_i2c_algo) {
1258 		d->fe_adap[0].fe = dvb_attach(stv0288_attach, &earda_config,
1259 					&d->dev->i2c_adap);
1260 		if (d->fe_adap[0].fe != NULL) {
1261 			if (dvb_attach(stb6000_attach, d->fe_adap[0].fe, 0x61,
1262 					&d->dev->i2c_adap)) {
1263 				d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1264 				info("Attached stv0288!\n");
1265 				return 0;
1266 			}
1267 		}
1268 	}
1269 
1270 	if (dw2102_properties.i2c_algo == &dw2102_i2c_algo) {
1271 		/*dw2102_properties.adapter->tuner_attach = dw2102_tuner_attach;*/
1272 		d->fe_adap[0].fe = dvb_attach(stv0299_attach, &sharp_z0194a_config,
1273 					&d->dev->i2c_adap);
1274 		if (d->fe_adap[0].fe != NULL) {
1275 			d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1276 			info("Attached stv0299!\n");
1277 			return 0;
1278 		}
1279 	}
1280 	return -EIO;
1281 }
1282 
1283 static int dw3101_frontend_attach(struct dvb_usb_adapter *d)
1284 {
1285 	d->fe_adap[0].fe = dvb_attach(tda10023_attach, &dw3101_tda10023_config,
1286 				&d->dev->i2c_adap, 0x48);
1287 	if (d->fe_adap[0].fe != NULL) {
1288 		info("Attached tda10023!\n");
1289 		return 0;
1290 	}
1291 	return -EIO;
1292 }
1293 
1294 static int zl100313_frontend_attach(struct dvb_usb_adapter *d)
1295 {
1296 	d->fe_adap[0].fe = dvb_attach(mt312_attach, &zl313_config,
1297 			&d->dev->i2c_adap);
1298 	if (d->fe_adap[0].fe != NULL) {
1299 		if (dvb_attach(zl10039_attach, d->fe_adap[0].fe, 0x60,
1300 				&d->dev->i2c_adap)) {
1301 			d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1302 			info("Attached zl100313+zl10039!\n");
1303 			return 0;
1304 		}
1305 	}
1306 
1307 	return -EIO;
1308 }
1309 
1310 static int stv0288_frontend_attach(struct dvb_usb_adapter *d)
1311 {
1312 	u8 obuf[] = {7, 1};
1313 
1314 	d->fe_adap[0].fe = dvb_attach(stv0288_attach, &earda_config,
1315 			&d->dev->i2c_adap);
1316 
1317 	if (d->fe_adap[0].fe == NULL)
1318 		return -EIO;
1319 
1320 	if (NULL == dvb_attach(stb6000_attach, d->fe_adap[0].fe, 0x61, &d->dev->i2c_adap))
1321 		return -EIO;
1322 
1323 	d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1324 
1325 	dw210x_op_rw(d->dev->udev, 0x8a, 0, 0, obuf, 2, DW210X_WRITE_MSG);
1326 
1327 	info("Attached stv0288+stb6000!\n");
1328 
1329 	return 0;
1330 
1331 }
1332 
1333 static int ds3000_frontend_attach(struct dvb_usb_adapter *d)
1334 {
1335 	struct dw2102_state *st = d->dev->priv;
1336 	u8 obuf[] = {7, 1};
1337 
1338 	d->fe_adap[0].fe = dvb_attach(ds3000_attach, &s660_ds3000_config,
1339 			&d->dev->i2c_adap);
1340 
1341 	if (d->fe_adap[0].fe == NULL)
1342 		return -EIO;
1343 
1344 	dvb_attach(ts2020_attach, d->fe_adap[0].fe, &s660_ts2020_config,
1345 		&d->dev->i2c_adap);
1346 
1347 	st->old_set_voltage = d->fe_adap[0].fe->ops.set_voltage;
1348 	d->fe_adap[0].fe->ops.set_voltage = s660_set_voltage;
1349 
1350 	dw210x_op_rw(d->dev->udev, 0x8a, 0, 0, obuf, 2, DW210X_WRITE_MSG);
1351 
1352 	info("Attached ds3000+ts2020!\n");
1353 
1354 	return 0;
1355 }
1356 
1357 static int prof_7500_frontend_attach(struct dvb_usb_adapter *d)
1358 {
1359 	u8 obuf[] = {7, 1};
1360 
1361 	d->fe_adap[0].fe = dvb_attach(stv0900_attach, &prof_7500_stv0900_config,
1362 					&d->dev->i2c_adap, 0);
1363 	if (d->fe_adap[0].fe == NULL)
1364 		return -EIO;
1365 
1366 	d->fe_adap[0].fe->ops.set_voltage = dw210x_set_voltage;
1367 
1368 	dw210x_op_rw(d->dev->udev, 0x8a, 0, 0, obuf, 2, DW210X_WRITE_MSG);
1369 
1370 	info("Attached STV0900+STB6100A!\n");
1371 
1372 	return 0;
1373 }
1374 
1375 static int su3000_frontend_attach(struct dvb_usb_adapter *d)
1376 {
1377 	u8 obuf[3] = { 0xe, 0x80, 0 };
1378 	u8 ibuf[] = { 0 };
1379 
1380 	if (dvb_usb_generic_rw(d->dev, obuf, 3, ibuf, 1, 0) < 0)
1381 		err("command 0x0e transfer failed.");
1382 
1383 	obuf[0] = 0xe;
1384 	obuf[1] = 0x02;
1385 	obuf[2] = 1;
1386 
1387 	if (dvb_usb_generic_rw(d->dev, obuf, 3, ibuf, 1, 0) < 0)
1388 		err("command 0x0e transfer failed.");
1389 	msleep(300);
1390 
1391 	obuf[0] = 0xe;
1392 	obuf[1] = 0x83;
1393 	obuf[2] = 0;
1394 
1395 	if (dvb_usb_generic_rw(d->dev, obuf, 3, ibuf, 1, 0) < 0)
1396 		err("command 0x0e transfer failed.");
1397 
1398 	obuf[0] = 0xe;
1399 	obuf[1] = 0x83;
1400 	obuf[2] = 1;
1401 
1402 	if (dvb_usb_generic_rw(d->dev, obuf, 3, ibuf, 1, 0) < 0)
1403 		err("command 0x0e transfer failed.");
1404 
1405 	obuf[0] = 0x51;
1406 
1407 	if (dvb_usb_generic_rw(d->dev, obuf, 1, ibuf, 1, 0) < 0)
1408 		err("command 0x51 transfer failed.");
1409 
1410 	d->fe_adap[0].fe = dvb_attach(ds3000_attach, &su3000_ds3000_config,
1411 					&d->dev->i2c_adap);
1412 	if (d->fe_adap[0].fe == NULL)
1413 		return -EIO;
1414 
1415 	if (dvb_attach(ts2020_attach, d->fe_adap[0].fe,
1416 				&dw2104_ts2020_config,
1417 				&d->dev->i2c_adap)) {
1418 		info("Attached DS3000/TS2020!\n");
1419 		return 0;
1420 	}
1421 
1422 	info("Failed to attach DS3000/TS2020!\n");
1423 	return -EIO;
1424 }
1425 
1426 static int t220_frontend_attach(struct dvb_usb_adapter *d)
1427 {
1428 	u8 obuf[3] = { 0xe, 0x87, 0 };
1429 	u8 ibuf[] = { 0 };
1430 
1431 	if (dvb_usb_generic_rw(d->dev, obuf, 3, ibuf, 1, 0) < 0)
1432 		err("command 0x0e transfer failed.");
1433 
1434 	obuf[0] = 0xe;
1435 	obuf[1] = 0x86;
1436 	obuf[2] = 1;
1437 
1438 	if (dvb_usb_generic_rw(d->dev, obuf, 3, ibuf, 1, 0) < 0)
1439 		err("command 0x0e transfer failed.");
1440 
1441 	obuf[0] = 0xe;
1442 	obuf[1] = 0x80;
1443 	obuf[2] = 0;
1444 
1445 	if (dvb_usb_generic_rw(d->dev, obuf, 3, ibuf, 1, 0) < 0)
1446 		err("command 0x0e transfer failed.");
1447 
1448 	msleep(50);
1449 
1450 	obuf[0] = 0xe;
1451 	obuf[1] = 0x80;
1452 	obuf[2] = 1;
1453 
1454 	if (dvb_usb_generic_rw(d->dev, obuf, 3, ibuf, 1, 0) < 0)
1455 		err("command 0x0e transfer failed.");
1456 
1457 	obuf[0] = 0x51;
1458 
1459 	if (dvb_usb_generic_rw(d->dev, obuf, 1, ibuf, 1, 0) < 0)
1460 		err("command 0x51 transfer failed.");
1461 
1462 	d->fe_adap[0].fe = dvb_attach(cxd2820r_attach, &cxd2820r_config,
1463 					&d->dev->i2c_adap, NULL);
1464 	if (d->fe_adap[0].fe != NULL) {
1465 		if (dvb_attach(tda18271_attach, d->fe_adap[0].fe, 0x60,
1466 					&d->dev->i2c_adap, &tda18271_config)) {
1467 			info("Attached TDA18271HD/CXD2820R!\n");
1468 			return 0;
1469 		}
1470 	}
1471 
1472 	info("Failed to attach TDA18271HD/CXD2820R!\n");
1473 	return -EIO;
1474 }
1475 
1476 static int m88rs2000_frontend_attach(struct dvb_usb_adapter *d)
1477 {
1478 	u8 obuf[] = { 0x51 };
1479 	u8 ibuf[] = { 0 };
1480 
1481 	if (dvb_usb_generic_rw(d->dev, obuf, 1, ibuf, 1, 0) < 0)
1482 		err("command 0x51 transfer failed.");
1483 
1484 	d->fe_adap[0].fe = dvb_attach(m88rs2000_attach, &s421_m88rs2000_config,
1485 					&d->dev->i2c_adap);
1486 
1487 	if (d->fe_adap[0].fe == NULL)
1488 		return -EIO;
1489 
1490 	if (dvb_attach(ts2020_attach, d->fe_adap[0].fe,
1491 				&dw2104_ts2020_config,
1492 				&d->dev->i2c_adap)) {
1493 		info("Attached RS2000/TS2020!\n");
1494 		return 0;
1495 	}
1496 
1497 	info("Failed to attach RS2000/TS2020!\n");
1498 	return -EIO;
1499 }
1500 
1501 static int tt_s2_4600_frontend_attach(struct dvb_usb_adapter *adap)
1502 {
1503 	struct dvb_usb_device *d = adap->dev;
1504 	struct dw2102_state *state = d->priv;
1505 	u8 obuf[3] = { 0xe, 0x80, 0 };
1506 	u8 ibuf[] = { 0 };
1507 	struct i2c_adapter *i2c_adapter;
1508 	struct i2c_client *client;
1509 	struct i2c_board_info info;
1510 	struct ts2020_config ts2020_config = {};
1511 
1512 	if (dvb_usb_generic_rw(d, obuf, 3, ibuf, 1, 0) < 0)
1513 		err("command 0x0e transfer failed.");
1514 
1515 	obuf[0] = 0xe;
1516 	obuf[1] = 0x02;
1517 	obuf[2] = 1;
1518 
1519 	if (dvb_usb_generic_rw(d, obuf, 3, ibuf, 1, 0) < 0)
1520 		err("command 0x0e transfer failed.");
1521 	msleep(300);
1522 
1523 	obuf[0] = 0xe;
1524 	obuf[1] = 0x83;
1525 	obuf[2] = 0;
1526 
1527 	if (dvb_usb_generic_rw(d, obuf, 3, ibuf, 1, 0) < 0)
1528 		err("command 0x0e transfer failed.");
1529 
1530 	obuf[0] = 0xe;
1531 	obuf[1] = 0x83;
1532 	obuf[2] = 1;
1533 
1534 	if (dvb_usb_generic_rw(d, obuf, 3, ibuf, 1, 0) < 0)
1535 		err("command 0x0e transfer failed.");
1536 
1537 	obuf[0] = 0x51;
1538 
1539 	if (dvb_usb_generic_rw(d, obuf, 1, ibuf, 1, 0) < 0)
1540 		err("command 0x51 transfer failed.");
1541 
1542 	memset(&info, 0, sizeof(struct i2c_board_info));
1543 
1544 	adap->fe_adap[0].fe = dvb_attach(m88ds3103_attach,
1545 					&tt_s2_4600_m88ds3103_config,
1546 					&d->i2c_adap,
1547 					&i2c_adapter);
1548 	if (adap->fe_adap[0].fe == NULL)
1549 		return -ENODEV;
1550 
1551 	/* attach tuner */
1552 	ts2020_config.fe = adap->fe_adap[0].fe;
1553 	strlcpy(info.type, "ts2022", I2C_NAME_SIZE);
1554 	info.addr = 0x60;
1555 	info.platform_data = &ts2020_config;
1556 	request_module("ts2020");
1557 	client = i2c_new_device(i2c_adapter, &info);
1558 
1559 	if (client == NULL || client->dev.driver == NULL) {
1560 		dvb_frontend_detach(adap->fe_adap[0].fe);
1561 		return -ENODEV;
1562 	}
1563 
1564 	if (!try_module_get(client->dev.driver->owner)) {
1565 		i2c_unregister_device(client);
1566 		dvb_frontend_detach(adap->fe_adap[0].fe);
1567 		return -ENODEV;
1568 	}
1569 
1570 	/* delegate signal strength measurement to tuner */
1571 	adap->fe_adap[0].fe->ops.read_signal_strength =
1572 			adap->fe_adap[0].fe->ops.tuner_ops.get_rf_strength;
1573 
1574 	state->i2c_client_tuner = client;
1575 
1576 	/* hook fe: need to resync the slave fifo when signal locks */
1577 	state->fe_read_status = adap->fe_adap[0].fe->ops.read_status;
1578 	adap->fe_adap[0].fe->ops.read_status = tt_s2_4600_read_status;
1579 
1580 	state->last_lock = 0;
1581 
1582 	return 0;
1583 }
1584 
1585 static int dw2102_tuner_attach(struct dvb_usb_adapter *adap)
1586 {
1587 	dvb_attach(dvb_pll_attach, adap->fe_adap[0].fe, 0x60,
1588 		&adap->dev->i2c_adap, DVB_PLL_OPERA1);
1589 	return 0;
1590 }
1591 
1592 static int dw3101_tuner_attach(struct dvb_usb_adapter *adap)
1593 {
1594 	dvb_attach(dvb_pll_attach, adap->fe_adap[0].fe, 0x60,
1595 		&adap->dev->i2c_adap, DVB_PLL_TUA6034);
1596 
1597 	return 0;
1598 }
1599 
1600 static int dw2102_rc_query(struct dvb_usb_device *d)
1601 {
1602 	u8 key[2];
1603 	struct i2c_msg msg = {
1604 		.addr = DW2102_RC_QUERY,
1605 		.flags = I2C_M_RD,
1606 		.buf = key,
1607 		.len = 2
1608 	};
1609 
1610 	if (d->props.i2c_algo->master_xfer(&d->i2c_adap, &msg, 1) == 1) {
1611 		if (msg.buf[0] != 0xff) {
1612 			deb_rc("%s: rc code: %x, %x\n",
1613 					__func__, key[0], key[1]);
1614 			rc_keydown(d->rc_dev, RC_TYPE_UNKNOWN, key[0], 0);
1615 		}
1616 	}
1617 
1618 	return 0;
1619 }
1620 
1621 static int prof_rc_query(struct dvb_usb_device *d)
1622 {
1623 	u8 key[2];
1624 	struct i2c_msg msg = {
1625 		.addr = DW2102_RC_QUERY,
1626 		.flags = I2C_M_RD,
1627 		.buf = key,
1628 		.len = 2
1629 	};
1630 
1631 	if (d->props.i2c_algo->master_xfer(&d->i2c_adap, &msg, 1) == 1) {
1632 		if (msg.buf[0] != 0xff) {
1633 			deb_rc("%s: rc code: %x, %x\n",
1634 					__func__, key[0], key[1]);
1635 			rc_keydown(d->rc_dev, RC_TYPE_UNKNOWN, key[0]^0xff, 0);
1636 		}
1637 	}
1638 
1639 	return 0;
1640 }
1641 
1642 static int su3000_rc_query(struct dvb_usb_device *d)
1643 {
1644 	u8 key[2];
1645 	struct i2c_msg msg = {
1646 		.addr = DW2102_RC_QUERY,
1647 		.flags = I2C_M_RD,
1648 		.buf = key,
1649 		.len = 2
1650 	};
1651 
1652 	if (d->props.i2c_algo->master_xfer(&d->i2c_adap, &msg, 1) == 1) {
1653 		if (msg.buf[0] != 0xff) {
1654 			deb_rc("%s: rc code: %x, %x\n",
1655 					__func__, key[0], key[1]);
1656 			rc_keydown(d->rc_dev, RC_TYPE_RC5,
1657 				   RC_SCANCODE_RC5(key[1], key[0]), 0);
1658 		}
1659 	}
1660 
1661 	return 0;
1662 }
1663 
1664 enum dw2102_table_entry {
1665 	CYPRESS_DW2102,
1666 	CYPRESS_DW2101,
1667 	CYPRESS_DW2104,
1668 	TEVII_S650,
1669 	TERRATEC_CINERGY_S,
1670 	CYPRESS_DW3101,
1671 	TEVII_S630,
1672 	PROF_1100,
1673 	TEVII_S660,
1674 	PROF_7500,
1675 	GENIATECH_SU3000,
1676 	TERRATEC_CINERGY_S2,
1677 	TEVII_S480_1,
1678 	TEVII_S480_2,
1679 	X3M_SPC1400HD,
1680 	TEVII_S421,
1681 	TEVII_S632,
1682 	TERRATEC_CINERGY_S2_R2,
1683 	GOTVIEW_SAT_HD,
1684 	GENIATECH_T220,
1685 	TECHNOTREND_S2_4600,
1686 	TEVII_S482_1,
1687 	TEVII_S482_2,
1688 };
1689 
1690 static struct usb_device_id dw2102_table[] = {
1691 	[CYPRESS_DW2102] = {USB_DEVICE(USB_VID_CYPRESS, USB_PID_DW2102)},
1692 	[CYPRESS_DW2101] = {USB_DEVICE(USB_VID_CYPRESS, 0x2101)},
1693 	[CYPRESS_DW2104] = {USB_DEVICE(USB_VID_CYPRESS, USB_PID_DW2104)},
1694 	[TEVII_S650] = {USB_DEVICE(0x9022, USB_PID_TEVII_S650)},
1695 	[TERRATEC_CINERGY_S] = {USB_DEVICE(USB_VID_TERRATEC, USB_PID_CINERGY_S)},
1696 	[CYPRESS_DW3101] = {USB_DEVICE(USB_VID_CYPRESS, USB_PID_DW3101)},
1697 	[TEVII_S630] = {USB_DEVICE(0x9022, USB_PID_TEVII_S630)},
1698 	[PROF_1100] = {USB_DEVICE(0x3011, USB_PID_PROF_1100)},
1699 	[TEVII_S660] = {USB_DEVICE(0x9022, USB_PID_TEVII_S660)},
1700 	[PROF_7500] = {USB_DEVICE(0x3034, 0x7500)},
1701 	[GENIATECH_SU3000] = {USB_DEVICE(0x1f4d, 0x3000)},
1702 	[TERRATEC_CINERGY_S2] = {USB_DEVICE(USB_VID_TERRATEC, 0x00a8)},
1703 	[TEVII_S480_1] = {USB_DEVICE(0x9022, USB_PID_TEVII_S480_1)},
1704 	[TEVII_S480_2] = {USB_DEVICE(0x9022, USB_PID_TEVII_S480_2)},
1705 	[X3M_SPC1400HD] = {USB_DEVICE(0x1f4d, 0x3100)},
1706 	[TEVII_S421] = {USB_DEVICE(0x9022, USB_PID_TEVII_S421)},
1707 	[TEVII_S632] = {USB_DEVICE(0x9022, USB_PID_TEVII_S632)},
1708 	[TERRATEC_CINERGY_S2_R2] = {USB_DEVICE(USB_VID_TERRATEC, 0x00b0)},
1709 	[GOTVIEW_SAT_HD] = {USB_DEVICE(0x1FE1, USB_PID_GOTVIEW_SAT_HD)},
1710 	[GENIATECH_T220] = {USB_DEVICE(0x1f4d, 0xD220)},
1711 	[TECHNOTREND_S2_4600] = {USB_DEVICE(USB_VID_TECHNOTREND,
1712 		USB_PID_TECHNOTREND_CONNECT_S2_4600)},
1713 	[TEVII_S482_1] = {USB_DEVICE(0x9022, 0xd483)},
1714 	[TEVII_S482_2] = {USB_DEVICE(0x9022, 0xd484)},
1715 	{ }
1716 };
1717 
1718 MODULE_DEVICE_TABLE(usb, dw2102_table);
1719 
1720 static int dw2102_load_firmware(struct usb_device *dev,
1721 			const struct firmware *frmwr)
1722 {
1723 	u8 *b, *p;
1724 	int ret = 0, i;
1725 	u8 reset;
1726 	u8 reset16[] = {0, 0, 0, 0, 0, 0, 0};
1727 	const struct firmware *fw;
1728 
1729 	switch (le16_to_cpu(dev->descriptor.idProduct)) {
1730 	case 0x2101:
1731 		ret = request_firmware(&fw, DW2101_FIRMWARE, &dev->dev);
1732 		if (ret != 0) {
1733 			err(err_str, DW2101_FIRMWARE);
1734 			return ret;
1735 		}
1736 		break;
1737 	default:
1738 		fw = frmwr;
1739 		break;
1740 	}
1741 	info("start downloading DW210X firmware");
1742 	p = kmalloc(fw->size, GFP_KERNEL);
1743 	reset = 1;
1744 	/*stop the CPU*/
1745 	dw210x_op_rw(dev, 0xa0, 0x7f92, 0, &reset, 1, DW210X_WRITE_MSG);
1746 	dw210x_op_rw(dev, 0xa0, 0xe600, 0, &reset, 1, DW210X_WRITE_MSG);
1747 
1748 	if (p != NULL) {
1749 		memcpy(p, fw->data, fw->size);
1750 		for (i = 0; i < fw->size; i += 0x40) {
1751 			b = (u8 *) p + i;
1752 			if (dw210x_op_rw(dev, 0xa0, i, 0, b , 0x40,
1753 					DW210X_WRITE_MSG) != 0x40) {
1754 				err("error while transferring firmware");
1755 				ret = -EINVAL;
1756 				break;
1757 			}
1758 		}
1759 		/* restart the CPU */
1760 		reset = 0;
1761 		if (ret || dw210x_op_rw(dev, 0xa0, 0x7f92, 0, &reset, 1,
1762 					DW210X_WRITE_MSG) != 1) {
1763 			err("could not restart the USB controller CPU.");
1764 			ret = -EINVAL;
1765 		}
1766 		if (ret || dw210x_op_rw(dev, 0xa0, 0xe600, 0, &reset, 1,
1767 					DW210X_WRITE_MSG) != 1) {
1768 			err("could not restart the USB controller CPU.");
1769 			ret = -EINVAL;
1770 		}
1771 		/* init registers */
1772 		switch (le16_to_cpu(dev->descriptor.idProduct)) {
1773 		case USB_PID_TEVII_S650:
1774 			dw2104_properties.rc.core.rc_codes = RC_MAP_TEVII_NEC;
1775 		case USB_PID_DW2104:
1776 			reset = 1;
1777 			dw210x_op_rw(dev, 0xc4, 0x0000, 0, &reset, 1,
1778 					DW210X_WRITE_MSG);
1779 			/* break omitted intentionally */
1780 		case USB_PID_DW3101:
1781 			reset = 0;
1782 			dw210x_op_rw(dev, 0xbf, 0x0040, 0, &reset, 0,
1783 					DW210X_WRITE_MSG);
1784 			break;
1785 		case USB_PID_CINERGY_S:
1786 		case USB_PID_DW2102:
1787 			dw210x_op_rw(dev, 0xbf, 0x0040, 0, &reset, 0,
1788 					DW210X_WRITE_MSG);
1789 			dw210x_op_rw(dev, 0xb9, 0x0000, 0, &reset16[0], 2,
1790 					DW210X_READ_MSG);
1791 			/* check STV0299 frontend  */
1792 			dw210x_op_rw(dev, 0xb5, 0, 0, &reset16[0], 2,
1793 					DW210X_READ_MSG);
1794 			if ((reset16[0] == 0xa1) || (reset16[0] == 0x80)) {
1795 				dw2102_properties.i2c_algo = &dw2102_i2c_algo;
1796 				dw2102_properties.adapter->fe[0].tuner_attach = &dw2102_tuner_attach;
1797 				break;
1798 			} else {
1799 				/* check STV0288 frontend  */
1800 				reset16[0] = 0xd0;
1801 				reset16[1] = 1;
1802 				reset16[2] = 0;
1803 				dw210x_op_rw(dev, 0xc2, 0, 0, &reset16[0], 3,
1804 						DW210X_WRITE_MSG);
1805 				dw210x_op_rw(dev, 0xc3, 0xd1, 0, &reset16[0], 3,
1806 						DW210X_READ_MSG);
1807 				if (reset16[2] == 0x11) {
1808 					dw2102_properties.i2c_algo = &dw2102_earda_i2c_algo;
1809 					break;
1810 				}
1811 			}
1812 		case 0x2101:
1813 			dw210x_op_rw(dev, 0xbc, 0x0030, 0, &reset16[0], 2,
1814 					DW210X_READ_MSG);
1815 			dw210x_op_rw(dev, 0xba, 0x0000, 0, &reset16[0], 7,
1816 					DW210X_READ_MSG);
1817 			dw210x_op_rw(dev, 0xba, 0x0000, 0, &reset16[0], 7,
1818 					DW210X_READ_MSG);
1819 			dw210x_op_rw(dev, 0xb9, 0x0000, 0, &reset16[0], 2,
1820 					DW210X_READ_MSG);
1821 			break;
1822 		}
1823 
1824 		msleep(100);
1825 		kfree(p);
1826 	}
1827 	return ret;
1828 }
1829 
1830 static struct dvb_usb_device_properties dw2102_properties = {
1831 	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
1832 	.usb_ctrl = DEVICE_SPECIFIC,
1833 	.firmware = DW2102_FIRMWARE,
1834 	.no_reconnect = 1,
1835 
1836 	.i2c_algo = &dw2102_serit_i2c_algo,
1837 
1838 	.rc.core = {
1839 		.rc_interval = 150,
1840 		.rc_codes = RC_MAP_DM1105_NEC,
1841 		.module_name = "dw2102",
1842 		.allowed_protos   = RC_BIT_NEC,
1843 		.rc_query = dw2102_rc_query,
1844 	},
1845 
1846 	.generic_bulk_ctrl_endpoint = 0x81,
1847 	/* parameter for the MPEG2-data transfer */
1848 	.num_adapters = 1,
1849 	.download_firmware = dw2102_load_firmware,
1850 	.read_mac_address = dw210x_read_mac_address,
1851 	.adapter = {
1852 		{
1853 		.num_frontends = 1,
1854 		.fe = {{
1855 			.frontend_attach = dw2102_frontend_attach,
1856 			.stream = {
1857 				.type = USB_BULK,
1858 				.count = 8,
1859 				.endpoint = 0x82,
1860 				.u = {
1861 					.bulk = {
1862 						.buffersize = 4096,
1863 					}
1864 				}
1865 			},
1866 		}},
1867 		}
1868 	},
1869 	.num_device_descs = 3,
1870 	.devices = {
1871 		{"DVBWorld DVB-S 2102 USB2.0",
1872 			{&dw2102_table[CYPRESS_DW2102], NULL},
1873 			{NULL},
1874 		},
1875 		{"DVBWorld DVB-S 2101 USB2.0",
1876 			{&dw2102_table[CYPRESS_DW2101], NULL},
1877 			{NULL},
1878 		},
1879 		{"TerraTec Cinergy S USB",
1880 			{&dw2102_table[TERRATEC_CINERGY_S], NULL},
1881 			{NULL},
1882 		},
1883 	}
1884 };
1885 
1886 static struct dvb_usb_device_properties dw2104_properties = {
1887 	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
1888 	.usb_ctrl = DEVICE_SPECIFIC,
1889 	.firmware = DW2104_FIRMWARE,
1890 	.no_reconnect = 1,
1891 
1892 	.i2c_algo = &dw2104_i2c_algo,
1893 	.rc.core = {
1894 		.rc_interval = 150,
1895 		.rc_codes = RC_MAP_DM1105_NEC,
1896 		.module_name = "dw2102",
1897 		.allowed_protos   = RC_BIT_NEC,
1898 		.rc_query = dw2102_rc_query,
1899 	},
1900 
1901 	.generic_bulk_ctrl_endpoint = 0x81,
1902 	/* parameter for the MPEG2-data transfer */
1903 	.num_adapters = 1,
1904 	.download_firmware = dw2102_load_firmware,
1905 	.read_mac_address = dw210x_read_mac_address,
1906 	.adapter = {
1907 		{
1908 		.num_frontends = 1,
1909 		.fe = {{
1910 			.frontend_attach = dw2104_frontend_attach,
1911 			.stream = {
1912 				.type = USB_BULK,
1913 				.count = 8,
1914 				.endpoint = 0x82,
1915 				.u = {
1916 					.bulk = {
1917 						.buffersize = 4096,
1918 					}
1919 				}
1920 			},
1921 		}},
1922 		}
1923 	},
1924 	.num_device_descs = 2,
1925 	.devices = {
1926 		{ "DVBWorld DW2104 USB2.0",
1927 			{&dw2102_table[CYPRESS_DW2104], NULL},
1928 			{NULL},
1929 		},
1930 		{ "TeVii S650 USB2.0",
1931 			{&dw2102_table[TEVII_S650], NULL},
1932 			{NULL},
1933 		},
1934 	}
1935 };
1936 
1937 static struct dvb_usb_device_properties dw3101_properties = {
1938 	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
1939 	.usb_ctrl = DEVICE_SPECIFIC,
1940 	.firmware = DW3101_FIRMWARE,
1941 	.no_reconnect = 1,
1942 
1943 	.i2c_algo = &dw3101_i2c_algo,
1944 	.rc.core = {
1945 		.rc_interval = 150,
1946 		.rc_codes = RC_MAP_DM1105_NEC,
1947 		.module_name = "dw2102",
1948 		.allowed_protos   = RC_BIT_NEC,
1949 		.rc_query = dw2102_rc_query,
1950 	},
1951 
1952 	.generic_bulk_ctrl_endpoint = 0x81,
1953 	/* parameter for the MPEG2-data transfer */
1954 	.num_adapters = 1,
1955 	.download_firmware = dw2102_load_firmware,
1956 	.read_mac_address = dw210x_read_mac_address,
1957 	.adapter = {
1958 		{
1959 		.num_frontends = 1,
1960 		.fe = {{
1961 			.frontend_attach = dw3101_frontend_attach,
1962 			.tuner_attach = dw3101_tuner_attach,
1963 			.stream = {
1964 				.type = USB_BULK,
1965 				.count = 8,
1966 				.endpoint = 0x82,
1967 				.u = {
1968 					.bulk = {
1969 						.buffersize = 4096,
1970 					}
1971 				}
1972 			},
1973 		}},
1974 		}
1975 	},
1976 	.num_device_descs = 1,
1977 	.devices = {
1978 		{ "DVBWorld DVB-C 3101 USB2.0",
1979 			{&dw2102_table[CYPRESS_DW3101], NULL},
1980 			{NULL},
1981 		},
1982 	}
1983 };
1984 
1985 static struct dvb_usb_device_properties s6x0_properties = {
1986 	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
1987 	.usb_ctrl = DEVICE_SPECIFIC,
1988 	.size_of_priv = sizeof(struct dw2102_state),
1989 	.firmware = S630_FIRMWARE,
1990 	.no_reconnect = 1,
1991 
1992 	.i2c_algo = &s6x0_i2c_algo,
1993 	.rc.core = {
1994 		.rc_interval = 150,
1995 		.rc_codes = RC_MAP_TEVII_NEC,
1996 		.module_name = "dw2102",
1997 		.allowed_protos   = RC_BIT_NEC,
1998 		.rc_query = dw2102_rc_query,
1999 	},
2000 
2001 	.generic_bulk_ctrl_endpoint = 0x81,
2002 	.num_adapters = 1,
2003 	.download_firmware = dw2102_load_firmware,
2004 	.read_mac_address = s6x0_read_mac_address,
2005 	.adapter = {
2006 		{
2007 		.num_frontends = 1,
2008 		.fe = {{
2009 			.frontend_attach = zl100313_frontend_attach,
2010 			.stream = {
2011 				.type = USB_BULK,
2012 				.count = 8,
2013 				.endpoint = 0x82,
2014 				.u = {
2015 					.bulk = {
2016 						.buffersize = 4096,
2017 					}
2018 				}
2019 			},
2020 		}},
2021 		}
2022 	},
2023 	.num_device_descs = 1,
2024 	.devices = {
2025 		{"TeVii S630 USB",
2026 			{&dw2102_table[TEVII_S630], NULL},
2027 			{NULL},
2028 		},
2029 	}
2030 };
2031 
2032 static struct dvb_usb_device_properties *p1100;
2033 static struct dvb_usb_device_description d1100 = {
2034 	"Prof 1100 USB ",
2035 	{&dw2102_table[PROF_1100], NULL},
2036 	{NULL},
2037 };
2038 
2039 static struct dvb_usb_device_properties *s660;
2040 static struct dvb_usb_device_description d660 = {
2041 	"TeVii S660 USB",
2042 	{&dw2102_table[TEVII_S660], NULL},
2043 	{NULL},
2044 };
2045 
2046 static struct dvb_usb_device_description d480_1 = {
2047 	"TeVii S480.1 USB",
2048 	{&dw2102_table[TEVII_S480_1], NULL},
2049 	{NULL},
2050 };
2051 
2052 static struct dvb_usb_device_description d480_2 = {
2053 	"TeVii S480.2 USB",
2054 	{&dw2102_table[TEVII_S480_2], NULL},
2055 	{NULL},
2056 };
2057 
2058 static struct dvb_usb_device_properties *p7500;
2059 static struct dvb_usb_device_description d7500 = {
2060 	"Prof 7500 USB DVB-S2",
2061 	{&dw2102_table[PROF_7500], NULL},
2062 	{NULL},
2063 };
2064 
2065 static struct dvb_usb_device_properties *s421;
2066 static struct dvb_usb_device_description d421 = {
2067 	"TeVii S421 PCI",
2068 	{&dw2102_table[TEVII_S421], NULL},
2069 	{NULL},
2070 };
2071 
2072 static struct dvb_usb_device_description d632 = {
2073 	"TeVii S632 USB",
2074 	{&dw2102_table[TEVII_S632], NULL},
2075 	{NULL},
2076 };
2077 
2078 static struct dvb_usb_device_properties su3000_properties = {
2079 	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
2080 	.usb_ctrl = DEVICE_SPECIFIC,
2081 	.size_of_priv = sizeof(struct dw2102_state),
2082 	.power_ctrl = su3000_power_ctrl,
2083 	.num_adapters = 1,
2084 	.identify_state	= su3000_identify_state,
2085 	.i2c_algo = &su3000_i2c_algo,
2086 
2087 	.rc.core = {
2088 		.rc_interval = 150,
2089 		.rc_codes = RC_MAP_SU3000,
2090 		.module_name = "dw2102",
2091 		.allowed_protos   = RC_BIT_RC5,
2092 		.rc_query = su3000_rc_query,
2093 	},
2094 
2095 	.read_mac_address = su3000_read_mac_address,
2096 
2097 	.generic_bulk_ctrl_endpoint = 0x01,
2098 
2099 	.adapter = {
2100 		{
2101 		.num_frontends = 1,
2102 		.fe = {{
2103 			.streaming_ctrl   = su3000_streaming_ctrl,
2104 			.frontend_attach  = su3000_frontend_attach,
2105 			.stream = {
2106 				.type = USB_BULK,
2107 				.count = 8,
2108 				.endpoint = 0x82,
2109 				.u = {
2110 					.bulk = {
2111 						.buffersize = 4096,
2112 					}
2113 				}
2114 			}
2115 		}},
2116 		}
2117 	},
2118 	.num_device_descs = 5,
2119 	.devices = {
2120 		{ "SU3000HD DVB-S USB2.0",
2121 			{ &dw2102_table[GENIATECH_SU3000], NULL },
2122 			{ NULL },
2123 		},
2124 		{ "Terratec Cinergy S2 USB HD",
2125 			{ &dw2102_table[TERRATEC_CINERGY_S2], NULL },
2126 			{ NULL },
2127 		},
2128 		{ "X3M TV SPC1400HD PCI",
2129 			{ &dw2102_table[X3M_SPC1400HD], NULL },
2130 			{ NULL },
2131 		},
2132 		{ "Terratec Cinergy S2 USB HD Rev.2",
2133 			{ &dw2102_table[TERRATEC_CINERGY_S2_R2], NULL },
2134 			{ NULL },
2135 		},
2136 		{ "GOTVIEW Satellite HD",
2137 			{ &dw2102_table[GOTVIEW_SAT_HD], NULL },
2138 			{ NULL },
2139 		},
2140 	}
2141 };
2142 
2143 static struct dvb_usb_device_properties t220_properties = {
2144 	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
2145 	.usb_ctrl = DEVICE_SPECIFIC,
2146 	.size_of_priv = sizeof(struct dw2102_state),
2147 	.power_ctrl = su3000_power_ctrl,
2148 	.num_adapters = 1,
2149 	.identify_state	= su3000_identify_state,
2150 	.i2c_algo = &su3000_i2c_algo,
2151 
2152 	.rc.core = {
2153 		.rc_interval = 150,
2154 		.rc_codes = RC_MAP_SU3000,
2155 		.module_name = "dw2102",
2156 		.allowed_protos   = RC_BIT_RC5,
2157 		.rc_query = su3000_rc_query,
2158 	},
2159 
2160 	.read_mac_address = su3000_read_mac_address,
2161 
2162 	.generic_bulk_ctrl_endpoint = 0x01,
2163 
2164 	.adapter = {
2165 		{
2166 		.num_frontends = 1,
2167 		.fe = { {
2168 			.streaming_ctrl   = su3000_streaming_ctrl,
2169 			.frontend_attach  = t220_frontend_attach,
2170 			.stream = {
2171 				.type = USB_BULK,
2172 				.count = 8,
2173 				.endpoint = 0x82,
2174 				.u = {
2175 					.bulk = {
2176 						.buffersize = 4096,
2177 					}
2178 				}
2179 			}
2180 		} },
2181 		}
2182 	},
2183 	.num_device_descs = 1,
2184 	.devices = {
2185 		{ "Geniatech T220 DVB-T/T2 USB2.0",
2186 			{ &dw2102_table[GENIATECH_T220], NULL },
2187 			{ NULL },
2188 		},
2189 	}
2190 };
2191 
2192 static struct dvb_usb_device_properties tt_s2_4600_properties = {
2193 	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
2194 	.usb_ctrl = DEVICE_SPECIFIC,
2195 	.size_of_priv = sizeof(struct dw2102_state),
2196 	.power_ctrl = su3000_power_ctrl,
2197 	.num_adapters = 1,
2198 	.identify_state	= su3000_identify_state,
2199 	.i2c_algo = &su3000_i2c_algo,
2200 
2201 	.rc.core = {
2202 		.rc_interval = 250,
2203 		.rc_codes = RC_MAP_TT_1500,
2204 		.module_name = "dw2102",
2205 		.allowed_protos   = RC_BIT_RC5,
2206 		.rc_query = su3000_rc_query,
2207 	},
2208 
2209 	.read_mac_address = su3000_read_mac_address,
2210 
2211 	.generic_bulk_ctrl_endpoint = 0x01,
2212 
2213 	.adapter = {
2214 		{
2215 		.num_frontends = 1,
2216 		.fe = {{
2217 			.streaming_ctrl   = su3000_streaming_ctrl,
2218 			.frontend_attach  = tt_s2_4600_frontend_attach,
2219 			.stream = {
2220 				.type = USB_BULK,
2221 				.count = 8,
2222 				.endpoint = 0x82,
2223 				.u = {
2224 					.bulk = {
2225 						.buffersize = 4096,
2226 					}
2227 				}
2228 			}
2229 		} },
2230 		}
2231 	},
2232 	.num_device_descs = 3,
2233 	.devices = {
2234 		{ "TechnoTrend TT-connect S2-4600",
2235 			{ &dw2102_table[TECHNOTREND_S2_4600], NULL },
2236 			{ NULL },
2237 		},
2238 		{ "TeVii S482 (tuner 1)",
2239 			{ &dw2102_table[TEVII_S482_1], NULL },
2240 			{ NULL },
2241 		},
2242 		{ "TeVii S482 (tuner 2)",
2243 			{ &dw2102_table[TEVII_S482_2], NULL },
2244 			{ NULL },
2245 		},
2246 	}
2247 };
2248 
2249 static int dw2102_probe(struct usb_interface *intf,
2250 		const struct usb_device_id *id)
2251 {
2252 	p1100 = kmemdup(&s6x0_properties,
2253 			sizeof(struct dvb_usb_device_properties), GFP_KERNEL);
2254 	if (!p1100)
2255 		return -ENOMEM;
2256 	/* copy default structure */
2257 	/* fill only different fields */
2258 	p1100->firmware = P1100_FIRMWARE;
2259 	p1100->devices[0] = d1100;
2260 	p1100->rc.core.rc_query = prof_rc_query;
2261 	p1100->rc.core.rc_codes = RC_MAP_TBS_NEC;
2262 	p1100->adapter->fe[0].frontend_attach = stv0288_frontend_attach;
2263 
2264 	s660 = kmemdup(&s6x0_properties,
2265 		       sizeof(struct dvb_usb_device_properties), GFP_KERNEL);
2266 	if (!s660) {
2267 		kfree(p1100);
2268 		return -ENOMEM;
2269 	}
2270 	s660->firmware = S660_FIRMWARE;
2271 	s660->num_device_descs = 3;
2272 	s660->devices[0] = d660;
2273 	s660->devices[1] = d480_1;
2274 	s660->devices[2] = d480_2;
2275 	s660->adapter->fe[0].frontend_attach = ds3000_frontend_attach;
2276 
2277 	p7500 = kmemdup(&s6x0_properties,
2278 			sizeof(struct dvb_usb_device_properties), GFP_KERNEL);
2279 	if (!p7500) {
2280 		kfree(p1100);
2281 		kfree(s660);
2282 		return -ENOMEM;
2283 	}
2284 	p7500->firmware = P7500_FIRMWARE;
2285 	p7500->devices[0] = d7500;
2286 	p7500->rc.core.rc_query = prof_rc_query;
2287 	p7500->rc.core.rc_codes = RC_MAP_TBS_NEC;
2288 	p7500->adapter->fe[0].frontend_attach = prof_7500_frontend_attach;
2289 
2290 
2291 	s421 = kmemdup(&su3000_properties,
2292 		       sizeof(struct dvb_usb_device_properties), GFP_KERNEL);
2293 	if (!s421) {
2294 		kfree(p1100);
2295 		kfree(s660);
2296 		kfree(p7500);
2297 		return -ENOMEM;
2298 	}
2299 	s421->num_device_descs = 2;
2300 	s421->devices[0] = d421;
2301 	s421->devices[1] = d632;
2302 	s421->adapter->fe[0].frontend_attach = m88rs2000_frontend_attach;
2303 
2304 	if (0 == dvb_usb_device_init(intf, &dw2102_properties,
2305 			THIS_MODULE, NULL, adapter_nr) ||
2306 	    0 == dvb_usb_device_init(intf, &dw2104_properties,
2307 			THIS_MODULE, NULL, adapter_nr) ||
2308 	    0 == dvb_usb_device_init(intf, &dw3101_properties,
2309 			THIS_MODULE, NULL, adapter_nr) ||
2310 	    0 == dvb_usb_device_init(intf, &s6x0_properties,
2311 			THIS_MODULE, NULL, adapter_nr) ||
2312 	    0 == dvb_usb_device_init(intf, p1100,
2313 			THIS_MODULE, NULL, adapter_nr) ||
2314 	    0 == dvb_usb_device_init(intf, s660,
2315 			THIS_MODULE, NULL, adapter_nr) ||
2316 	    0 == dvb_usb_device_init(intf, p7500,
2317 			THIS_MODULE, NULL, adapter_nr) ||
2318 	    0 == dvb_usb_device_init(intf, s421,
2319 			THIS_MODULE, NULL, adapter_nr) ||
2320 	    0 == dvb_usb_device_init(intf, &su3000_properties,
2321 			 THIS_MODULE, NULL, adapter_nr) ||
2322 	    0 == dvb_usb_device_init(intf, &t220_properties,
2323 			 THIS_MODULE, NULL, adapter_nr) ||
2324 	    0 == dvb_usb_device_init(intf, &tt_s2_4600_properties,
2325 			 THIS_MODULE, NULL, adapter_nr))
2326 		return 0;
2327 
2328 	return -ENODEV;
2329 }
2330 
2331 static void dw2102_disconnect(struct usb_interface *intf)
2332 {
2333 	struct dvb_usb_device *d = usb_get_intfdata(intf);
2334 	struct dw2102_state *st = (struct dw2102_state *)d->priv;
2335 	struct i2c_client *client;
2336 
2337 	/* remove I2C client for tuner */
2338 	client = st->i2c_client_tuner;
2339 	if (client) {
2340 		module_put(client->dev.driver->owner);
2341 		i2c_unregister_device(client);
2342 	}
2343 
2344 	dvb_usb_device_exit(intf);
2345 }
2346 
2347 static struct usb_driver dw2102_driver = {
2348 	.name = "dw2102",
2349 	.probe = dw2102_probe,
2350 	.disconnect = dw2102_disconnect,
2351 	.id_table = dw2102_table,
2352 };
2353 
2354 module_usb_driver(dw2102_driver);
2355 
2356 MODULE_AUTHOR("Igor M. Liplianin (c) liplianin@me.by");
2357 MODULE_DESCRIPTION("Driver for DVBWorld DVB-S 2101, 2102, DVB-S2 2104,"
2358 			" DVB-C 3101 USB2.0,"
2359 			" TeVii S600, S630, S650, S660, S480, S421, S632"
2360 			" Prof 1100, 7500 USB2.0,"
2361 			" Geniatech SU3000, T220,"
2362 			" TechnoTrend S2-4600 devices");
2363 MODULE_VERSION("0.1");
2364 MODULE_LICENSE("GPL");
2365 MODULE_FIRMWARE(DW2101_FIRMWARE);
2366 MODULE_FIRMWARE(DW2102_FIRMWARE);
2367 MODULE_FIRMWARE(DW2104_FIRMWARE);
2368 MODULE_FIRMWARE(DW3101_FIRMWARE);
2369 MODULE_FIRMWARE(S630_FIRMWARE);
2370 MODULE_FIRMWARE(S660_FIRMWARE);
2371 MODULE_FIRMWARE(P1100_FIRMWARE);
2372 MODULE_FIRMWARE(P7500_FIRMWARE);
2373