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