1 /*
2  * Driver for DVBSky USB2.0 receiver
3  *
4  * Copyright (C) 2013 Max nibble <nibble.max@gmail.com>
5  *
6  *    This program is free software; you can redistribute it and/or modify
7  *    it under the terms of the GNU General Public License as published by
8  *    the Free Software Foundation; either version 2 of the License, or
9  *    (at your option) any later version.
10  *
11  *    This program is distributed in the hope that it will be useful,
12  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *    GNU General Public License for more details.
15  */
16 
17 #include "dvb_usb.h"
18 #include "m88ds3103.h"
19 #include "ts2020.h"
20 #include "sp2.h"
21 #include "si2168.h"
22 #include "si2157.h"
23 
24 #define DVBSKY_MSG_DELAY	0/*2000*/
25 #define DVBSKY_BUF_LEN	64
26 
27 static int dvb_usb_dvbsky_disable_rc;
28 module_param_named(disable_rc, dvb_usb_dvbsky_disable_rc, int, 0644);
29 MODULE_PARM_DESC(disable_rc, "Disable inbuilt IR receiver.");
30 
31 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
32 
33 struct dvbsky_state {
34 	struct mutex stream_mutex;
35 	u8 ibuf[DVBSKY_BUF_LEN];
36 	u8 obuf[DVBSKY_BUF_LEN];
37 	u8 last_lock;
38 	struct i2c_client *i2c_client_demod;
39 	struct i2c_client *i2c_client_tuner;
40 	struct i2c_client *i2c_client_ci;
41 
42 	/* fe hook functions*/
43 	int (*fe_set_voltage)(struct dvb_frontend *fe,
44 		enum fe_sec_voltage voltage);
45 	int (*fe_read_status)(struct dvb_frontend *fe,
46 		enum fe_status *status);
47 };
48 
49 static int dvbsky_usb_generic_rw(struct dvb_usb_device *d,
50 		u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen)
51 {
52 	int ret;
53 	struct dvbsky_state *state = d_to_priv(d);
54 
55 	mutex_lock(&d->usb_mutex);
56 	if (wlen != 0)
57 		memcpy(state->obuf, wbuf, wlen);
58 
59 	ret = dvb_usbv2_generic_rw_locked(d, state->obuf, wlen,
60 			state->ibuf, rlen);
61 
62 	if (!ret && (rlen != 0))
63 		memcpy(rbuf, state->ibuf, rlen);
64 
65 	mutex_unlock(&d->usb_mutex);
66 	return ret;
67 }
68 
69 static int dvbsky_stream_ctrl(struct dvb_usb_device *d, u8 onoff)
70 {
71 	struct dvbsky_state *state = d_to_priv(d);
72 	int ret;
73 	u8 obuf_pre[3] = { 0x37, 0, 0 };
74 	u8 obuf_post[3] = { 0x36, 3, 0 };
75 
76 	mutex_lock(&state->stream_mutex);
77 	ret = dvbsky_usb_generic_rw(d, obuf_pre, 3, NULL, 0);
78 	if (!ret && onoff) {
79 		msleep(20);
80 		ret = dvbsky_usb_generic_rw(d, obuf_post, 3, NULL, 0);
81 	}
82 	mutex_unlock(&state->stream_mutex);
83 	return ret;
84 }
85 
86 static int dvbsky_streaming_ctrl(struct dvb_frontend *fe, int onoff)
87 {
88 	struct dvb_usb_device *d = fe_to_d(fe);
89 
90 	return dvbsky_stream_ctrl(d, (onoff == 0) ? 0 : 1);
91 }
92 
93 /* GPIO */
94 static int dvbsky_gpio_ctrl(struct dvb_usb_device *d, u8 gport, u8 value)
95 {
96 	int ret;
97 	u8 obuf[3], ibuf[2];
98 
99 	obuf[0] = 0x0e;
100 	obuf[1] = gport;
101 	obuf[2] = value;
102 	ret = dvbsky_usb_generic_rw(d, obuf, 3, ibuf, 1);
103 	if (ret)
104 		dev_err(&d->udev->dev, "failed=%d\n", ret);
105 	return ret;
106 }
107 
108 /* I2C */
109 static int dvbsky_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
110 	int num)
111 {
112 	struct dvb_usb_device *d = i2c_get_adapdata(adap);
113 	int ret = 0;
114 	u8 ibuf[64], obuf[64];
115 
116 	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
117 		return -EAGAIN;
118 
119 	if (num > 2) {
120 		dev_err(&d->udev->dev,
121 		"too many i2c messages[%d], max 2.", num);
122 		ret = -EOPNOTSUPP;
123 		goto i2c_error;
124 	}
125 
126 	if (num == 1) {
127 		if (msg[0].len > 60) {
128 			dev_err(&d->udev->dev,
129 			"too many i2c bytes[%d], max 60.",
130 			msg[0].len);
131 			ret = -EOPNOTSUPP;
132 			goto i2c_error;
133 		}
134 		if (msg[0].flags & I2C_M_RD) {
135 			/* single read */
136 			obuf[0] = 0x09;
137 			obuf[1] = 0;
138 			obuf[2] = msg[0].len;
139 			obuf[3] = msg[0].addr;
140 			ret = dvbsky_usb_generic_rw(d, obuf, 4,
141 					ibuf, msg[0].len + 1);
142 			if (ret)
143 				dev_err(&d->udev->dev, "failed=%d\n", ret);
144 			if (!ret)
145 				memcpy(msg[0].buf, &ibuf[1], msg[0].len);
146 		} else {
147 			/* write */
148 			obuf[0] = 0x08;
149 			obuf[1] = msg[0].addr;
150 			obuf[2] = msg[0].len;
151 			memcpy(&obuf[3], msg[0].buf, msg[0].len);
152 			ret = dvbsky_usb_generic_rw(d, obuf,
153 					msg[0].len + 3, ibuf, 1);
154 			if (ret)
155 				dev_err(&d->udev->dev, "failed=%d\n", ret);
156 		}
157 	} else {
158 		if ((msg[0].len > 60) || (msg[1].len > 60)) {
159 			dev_err(&d->udev->dev,
160 			"too many i2c bytes[w-%d][r-%d], max 60.",
161 			msg[0].len, msg[1].len);
162 			ret = -EOPNOTSUPP;
163 			goto i2c_error;
164 		}
165 		/* write then read */
166 		obuf[0] = 0x09;
167 		obuf[1] = msg[0].len;
168 		obuf[2] = msg[1].len;
169 		obuf[3] = msg[0].addr;
170 		memcpy(&obuf[4], msg[0].buf, msg[0].len);
171 		ret = dvbsky_usb_generic_rw(d, obuf,
172 			msg[0].len + 4, ibuf, msg[1].len + 1);
173 		if (ret)
174 			dev_err(&d->udev->dev, "failed=%d\n", ret);
175 
176 		if (!ret)
177 			memcpy(msg[1].buf, &ibuf[1], msg[1].len);
178 	}
179 i2c_error:
180 	mutex_unlock(&d->i2c_mutex);
181 	return (ret) ? ret : num;
182 }
183 
184 static u32 dvbsky_i2c_func(struct i2c_adapter *adapter)
185 {
186 	return I2C_FUNC_I2C;
187 }
188 
189 static struct i2c_algorithm dvbsky_i2c_algo = {
190 	.master_xfer   = dvbsky_i2c_xfer,
191 	.functionality = dvbsky_i2c_func,
192 };
193 
194 #if IS_ENABLED(CONFIG_RC_CORE)
195 static int dvbsky_rc_query(struct dvb_usb_device *d)
196 {
197 	u32 code = 0xffff, scancode;
198 	u8 rc5_command, rc5_system;
199 	u8 obuf[2], ibuf[2], toggle;
200 	int ret;
201 
202 	obuf[0] = 0x10;
203 	ret = dvbsky_usb_generic_rw(d, obuf, 1, ibuf, 2);
204 	if (ret)
205 		dev_err(&d->udev->dev, "failed=%d\n", ret);
206 	if (ret == 0)
207 		code = (ibuf[0] << 8) | ibuf[1];
208 	if (code != 0xffff) {
209 		dev_dbg(&d->udev->dev, "rc code: %x\n", code);
210 		rc5_command = code & 0x3F;
211 		rc5_system = (code & 0x7C0) >> 6;
212 		toggle = (code & 0x800) ? 1 : 0;
213 		scancode = rc5_system << 8 | rc5_command;
214 		rc_keydown(d->rc_dev, RC_PROTO_RC5, scancode, toggle);
215 	}
216 	return 0;
217 }
218 
219 static int dvbsky_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
220 {
221 	if (dvb_usb_dvbsky_disable_rc) {
222 		rc->map_name = NULL;
223 		return 0;
224 	}
225 
226 	rc->allowed_protos = RC_PROTO_BIT_RC5;
227 	rc->query          = dvbsky_rc_query;
228 	rc->interval       = 300;
229 	return 0;
230 }
231 #else
232 	#define dvbsky_get_rc_config NULL
233 #endif
234 
235 static int dvbsky_usb_set_voltage(struct dvb_frontend *fe,
236 	enum fe_sec_voltage voltage)
237 {
238 	struct dvb_usb_device *d = fe_to_d(fe);
239 	struct dvbsky_state *state = d_to_priv(d);
240 	u8 value;
241 
242 	if (voltage == SEC_VOLTAGE_OFF)
243 		value = 0;
244 	else
245 		value = 1;
246 	dvbsky_gpio_ctrl(d, 0x80, value);
247 
248 	return state->fe_set_voltage(fe, voltage);
249 }
250 
251 static int dvbsky_read_mac_addr(struct dvb_usb_adapter *adap, u8 mac[6])
252 {
253 	struct dvb_usb_device *d = adap_to_d(adap);
254 	u8 obuf[] = { 0x1e, 0x00 };
255 	u8 ibuf[6] = { 0 };
256 	struct i2c_msg msg[] = {
257 		{
258 			.addr = 0x51,
259 			.flags = 0,
260 			.buf = obuf,
261 			.len = 2,
262 		}, {
263 			.addr = 0x51,
264 			.flags = I2C_M_RD,
265 			.buf = ibuf,
266 			.len = 6,
267 		}
268 	};
269 
270 	if (i2c_transfer(&d->i2c_adap, msg, 2) == 2)
271 		memcpy(mac, ibuf, 6);
272 
273 	return 0;
274 }
275 
276 static int dvbsky_usb_read_status(struct dvb_frontend *fe,
277 				  enum fe_status *status)
278 {
279 	struct dvb_usb_device *d = fe_to_d(fe);
280 	struct dvbsky_state *state = d_to_priv(d);
281 	int ret;
282 
283 	ret = state->fe_read_status(fe, status);
284 
285 	/* it need resync slave fifo when signal change from unlock to lock.*/
286 	if ((*status & FE_HAS_LOCK) && (!state->last_lock))
287 		dvbsky_stream_ctrl(d, 1);
288 
289 	state->last_lock = (*status & FE_HAS_LOCK) ? 1 : 0;
290 	return ret;
291 }
292 
293 static const struct m88ds3103_config dvbsky_s960_m88ds3103_config = {
294 	.i2c_addr = 0x68,
295 	.clock = 27000000,
296 	.i2c_wr_max = 33,
297 	.clock_out = 0,
298 	.ts_mode = M88DS3103_TS_CI,
299 	.ts_clk = 16000,
300 	.ts_clk_pol = 0,
301 	.agc = 0x99,
302 	.lnb_hv_pol = 1,
303 	.lnb_en_pol = 1,
304 };
305 
306 static int dvbsky_s960_attach(struct dvb_usb_adapter *adap)
307 {
308 	struct dvbsky_state *state = adap_to_priv(adap);
309 	struct dvb_usb_device *d = adap_to_d(adap);
310 	int ret = 0;
311 	/* demod I2C adapter */
312 	struct i2c_adapter *i2c_adapter = NULL;
313 	struct i2c_client *client;
314 	struct i2c_board_info info;
315 	struct ts2020_config ts2020_config = {};
316 	memset(&info, 0, sizeof(struct i2c_board_info));
317 
318 	/* attach demod */
319 	adap->fe[0] = dvb_attach(m88ds3103_attach,
320 			&dvbsky_s960_m88ds3103_config,
321 			&d->i2c_adap,
322 			&i2c_adapter);
323 	if (!adap->fe[0]) {
324 		dev_err(&d->udev->dev, "dvbsky_s960_attach fail.\n");
325 		ret = -ENODEV;
326 		goto fail_attach;
327 	}
328 
329 	/* attach tuner */
330 	ts2020_config.fe = adap->fe[0];
331 	ts2020_config.get_agc_pwm = m88ds3103_get_agc_pwm;
332 	strlcpy(info.type, "ts2020", I2C_NAME_SIZE);
333 	info.addr = 0x60;
334 	info.platform_data = &ts2020_config;
335 	request_module("ts2020");
336 	client = i2c_new_device(i2c_adapter, &info);
337 	if (client == NULL || client->dev.driver == NULL) {
338 		dvb_frontend_detach(adap->fe[0]);
339 		ret = -ENODEV;
340 		goto fail_attach;
341 	}
342 
343 	if (!try_module_get(client->dev.driver->owner)) {
344 		i2c_unregister_device(client);
345 		dvb_frontend_detach(adap->fe[0]);
346 		ret = -ENODEV;
347 		goto fail_attach;
348 	}
349 
350 	/* delegate signal strength measurement to tuner */
351 	adap->fe[0]->ops.read_signal_strength =
352 			adap->fe[0]->ops.tuner_ops.get_rf_strength;
353 
354 	/* hook fe: need to resync the slave fifo when signal locks. */
355 	state->fe_read_status = adap->fe[0]->ops.read_status;
356 	adap->fe[0]->ops.read_status = dvbsky_usb_read_status;
357 
358 	/* hook fe: LNB off/on is control by Cypress usb chip. */
359 	state->fe_set_voltage = adap->fe[0]->ops.set_voltage;
360 	adap->fe[0]->ops.set_voltage = dvbsky_usb_set_voltage;
361 
362 	state->i2c_client_tuner = client;
363 
364 fail_attach:
365 	return ret;
366 }
367 
368 static int dvbsky_usb_ci_set_voltage(struct dvb_frontend *fe,
369 	enum fe_sec_voltage voltage)
370 {
371 	struct dvb_usb_device *d = fe_to_d(fe);
372 	struct dvbsky_state *state = d_to_priv(d);
373 	u8 value;
374 
375 	if (voltage == SEC_VOLTAGE_OFF)
376 		value = 0;
377 	else
378 		value = 1;
379 	dvbsky_gpio_ctrl(d, 0x00, value);
380 
381 	return state->fe_set_voltage(fe, voltage);
382 }
383 
384 static int dvbsky_ci_ctrl(void *priv, u8 read, int addr,
385 					u8 data, int *mem)
386 {
387 	struct dvb_usb_device *d = priv;
388 	int ret = 0;
389 	u8 command[4], respond[2], command_size, respond_size;
390 
391 	command[1] = (u8)((addr >> 8) & 0xff); /*high part of address*/
392 	command[2] = (u8)(addr & 0xff); /*low part of address*/
393 	if (read) {
394 		command[0] = 0x71;
395 		command_size = 3;
396 		respond_size = 2;
397 	} else {
398 		command[0] = 0x70;
399 		command[3] = data;
400 		command_size = 4;
401 		respond_size = 1;
402 	}
403 	ret = dvbsky_usb_generic_rw(d, command, command_size,
404 			respond, respond_size);
405 	if (ret)
406 		goto err;
407 	if (read)
408 		*mem = respond[1];
409 	return ret;
410 err:
411 	dev_err(&d->udev->dev, "ci control failed=%d\n", ret);
412 	return ret;
413 }
414 
415 static const struct m88ds3103_config dvbsky_s960c_m88ds3103_config = {
416 	.i2c_addr = 0x68,
417 	.clock = 27000000,
418 	.i2c_wr_max = 33,
419 	.clock_out = 0,
420 	.ts_mode = M88DS3103_TS_CI,
421 	.ts_clk = 10000,
422 	.ts_clk_pol = 1,
423 	.agc = 0x99,
424 	.lnb_hv_pol = 0,
425 	.lnb_en_pol = 1,
426 };
427 
428 static int dvbsky_s960c_attach(struct dvb_usb_adapter *adap)
429 {
430 	struct dvbsky_state *state = adap_to_priv(adap);
431 	struct dvb_usb_device *d = adap_to_d(adap);
432 	int ret = 0;
433 	/* demod I2C adapter */
434 	struct i2c_adapter *i2c_adapter = NULL;
435 	struct i2c_client *client_tuner, *client_ci;
436 	struct i2c_board_info info;
437 	struct sp2_config sp2_config;
438 	struct ts2020_config ts2020_config = {};
439 	memset(&info, 0, sizeof(struct i2c_board_info));
440 
441 	/* attach demod */
442 	adap->fe[0] = dvb_attach(m88ds3103_attach,
443 			&dvbsky_s960c_m88ds3103_config,
444 			&d->i2c_adap,
445 			&i2c_adapter);
446 	if (!adap->fe[0]) {
447 		dev_err(&d->udev->dev, "dvbsky_s960ci_attach fail.\n");
448 		ret = -ENODEV;
449 		goto fail_attach;
450 	}
451 
452 	/* attach tuner */
453 	ts2020_config.fe = adap->fe[0];
454 	ts2020_config.get_agc_pwm = m88ds3103_get_agc_pwm;
455 	strlcpy(info.type, "ts2020", I2C_NAME_SIZE);
456 	info.addr = 0x60;
457 	info.platform_data = &ts2020_config;
458 	request_module("ts2020");
459 	client_tuner = i2c_new_device(i2c_adapter, &info);
460 	if (client_tuner == NULL || client_tuner->dev.driver == NULL) {
461 		ret = -ENODEV;
462 		goto fail_tuner_device;
463 	}
464 
465 	if (!try_module_get(client_tuner->dev.driver->owner)) {
466 		ret = -ENODEV;
467 		goto fail_tuner_module;
468 	}
469 
470 	/* attach ci controller */
471 	memset(&sp2_config, 0, sizeof(sp2_config));
472 	sp2_config.dvb_adap = &adap->dvb_adap;
473 	sp2_config.priv = d;
474 	sp2_config.ci_control = dvbsky_ci_ctrl;
475 	memset(&info, 0, sizeof(struct i2c_board_info));
476 	strlcpy(info.type, "sp2", I2C_NAME_SIZE);
477 	info.addr = 0x40;
478 	info.platform_data = &sp2_config;
479 	request_module("sp2");
480 	client_ci = i2c_new_device(&d->i2c_adap, &info);
481 	if (client_ci == NULL || client_ci->dev.driver == NULL) {
482 		ret = -ENODEV;
483 		goto fail_ci_device;
484 	}
485 
486 	if (!try_module_get(client_ci->dev.driver->owner)) {
487 		ret = -ENODEV;
488 		goto fail_ci_module;
489 	}
490 
491 	/* delegate signal strength measurement to tuner */
492 	adap->fe[0]->ops.read_signal_strength =
493 			adap->fe[0]->ops.tuner_ops.get_rf_strength;
494 
495 	/* hook fe: need to resync the slave fifo when signal locks. */
496 	state->fe_read_status = adap->fe[0]->ops.read_status;
497 	adap->fe[0]->ops.read_status = dvbsky_usb_read_status;
498 
499 	/* hook fe: LNB off/on is control by Cypress usb chip. */
500 	state->fe_set_voltage = adap->fe[0]->ops.set_voltage;
501 	adap->fe[0]->ops.set_voltage = dvbsky_usb_ci_set_voltage;
502 
503 	state->i2c_client_tuner = client_tuner;
504 	state->i2c_client_ci = client_ci;
505 	return ret;
506 fail_ci_module:
507 	i2c_unregister_device(client_ci);
508 fail_ci_device:
509 	module_put(client_tuner->dev.driver->owner);
510 fail_tuner_module:
511 	i2c_unregister_device(client_tuner);
512 fail_tuner_device:
513 	dvb_frontend_detach(adap->fe[0]);
514 fail_attach:
515 	return ret;
516 }
517 
518 static int dvbsky_t680c_attach(struct dvb_usb_adapter *adap)
519 {
520 	struct dvbsky_state *state = adap_to_priv(adap);
521 	struct dvb_usb_device *d = adap_to_d(adap);
522 	int ret = 0;
523 	struct i2c_adapter *i2c_adapter;
524 	struct i2c_client *client_demod, *client_tuner, *client_ci;
525 	struct i2c_board_info info;
526 	struct si2168_config si2168_config;
527 	struct si2157_config si2157_config;
528 	struct sp2_config sp2_config;
529 
530 	/* attach demod */
531 	memset(&si2168_config, 0, sizeof(si2168_config));
532 	si2168_config.i2c_adapter = &i2c_adapter;
533 	si2168_config.fe = &adap->fe[0];
534 	si2168_config.ts_mode = SI2168_TS_PARALLEL;
535 	memset(&info, 0, sizeof(struct i2c_board_info));
536 	strlcpy(info.type, "si2168", I2C_NAME_SIZE);
537 	info.addr = 0x64;
538 	info.platform_data = &si2168_config;
539 
540 	request_module(info.type);
541 	client_demod = i2c_new_device(&d->i2c_adap, &info);
542 	if (client_demod == NULL ||
543 			client_demod->dev.driver == NULL)
544 		goto fail_demod_device;
545 	if (!try_module_get(client_demod->dev.driver->owner))
546 		goto fail_demod_module;
547 
548 	/* attach tuner */
549 	memset(&si2157_config, 0, sizeof(si2157_config));
550 	si2157_config.fe = adap->fe[0];
551 	si2157_config.if_port = 1;
552 	memset(&info, 0, sizeof(struct i2c_board_info));
553 	strlcpy(info.type, "si2157", I2C_NAME_SIZE);
554 	info.addr = 0x60;
555 	info.platform_data = &si2157_config;
556 
557 	request_module(info.type);
558 	client_tuner = i2c_new_device(i2c_adapter, &info);
559 	if (client_tuner == NULL ||
560 			client_tuner->dev.driver == NULL)
561 		goto fail_tuner_device;
562 	if (!try_module_get(client_tuner->dev.driver->owner))
563 		goto fail_tuner_module;
564 
565 	/* attach ci controller */
566 	memset(&sp2_config, 0, sizeof(sp2_config));
567 	sp2_config.dvb_adap = &adap->dvb_adap;
568 	sp2_config.priv = d;
569 	sp2_config.ci_control = dvbsky_ci_ctrl;
570 	memset(&info, 0, sizeof(struct i2c_board_info));
571 	strlcpy(info.type, "sp2", I2C_NAME_SIZE);
572 	info.addr = 0x40;
573 	info.platform_data = &sp2_config;
574 
575 	request_module(info.type);
576 	client_ci = i2c_new_device(&d->i2c_adap, &info);
577 
578 	if (client_ci == NULL || client_ci->dev.driver == NULL)
579 		goto fail_ci_device;
580 
581 	if (!try_module_get(client_ci->dev.driver->owner))
582 		goto fail_ci_module;
583 
584 	state->i2c_client_demod = client_demod;
585 	state->i2c_client_tuner = client_tuner;
586 	state->i2c_client_ci = client_ci;
587 	return ret;
588 fail_ci_module:
589 	i2c_unregister_device(client_ci);
590 fail_ci_device:
591 	module_put(client_tuner->dev.driver->owner);
592 fail_tuner_module:
593 	i2c_unregister_device(client_tuner);
594 fail_tuner_device:
595 	module_put(client_demod->dev.driver->owner);
596 fail_demod_module:
597 	i2c_unregister_device(client_demod);
598 fail_demod_device:
599 	ret = -ENODEV;
600 	return ret;
601 }
602 
603 static int dvbsky_t330_attach(struct dvb_usb_adapter *adap)
604 {
605 	struct dvbsky_state *state = adap_to_priv(adap);
606 	struct dvb_usb_device *d = adap_to_d(adap);
607 	int ret = 0;
608 	struct i2c_adapter *i2c_adapter;
609 	struct i2c_client *client_demod, *client_tuner;
610 	struct i2c_board_info info;
611 	struct si2168_config si2168_config;
612 	struct si2157_config si2157_config;
613 
614 	/* attach demod */
615 	memset(&si2168_config, 0, sizeof(si2168_config));
616 	si2168_config.i2c_adapter = &i2c_adapter;
617 	si2168_config.fe = &adap->fe[0];
618 	si2168_config.ts_mode = SI2168_TS_PARALLEL;
619 	si2168_config.ts_clock_gapped = true;
620 	memset(&info, 0, sizeof(struct i2c_board_info));
621 	strlcpy(info.type, "si2168", I2C_NAME_SIZE);
622 	info.addr = 0x64;
623 	info.platform_data = &si2168_config;
624 
625 	request_module(info.type);
626 	client_demod = i2c_new_device(&d->i2c_adap, &info);
627 	if (client_demod == NULL ||
628 			client_demod->dev.driver == NULL)
629 		goto fail_demod_device;
630 	if (!try_module_get(client_demod->dev.driver->owner))
631 		goto fail_demod_module;
632 
633 	/* attach tuner */
634 	memset(&si2157_config, 0, sizeof(si2157_config));
635 	si2157_config.fe = adap->fe[0];
636 	si2157_config.if_port = 1;
637 	memset(&info, 0, sizeof(struct i2c_board_info));
638 	strlcpy(info.type, "si2157", I2C_NAME_SIZE);
639 	info.addr = 0x60;
640 	info.platform_data = &si2157_config;
641 
642 	request_module(info.type);
643 	client_tuner = i2c_new_device(i2c_adapter, &info);
644 	if (client_tuner == NULL ||
645 			client_tuner->dev.driver == NULL)
646 		goto fail_tuner_device;
647 	if (!try_module_get(client_tuner->dev.driver->owner))
648 		goto fail_tuner_module;
649 
650 	state->i2c_client_demod = client_demod;
651 	state->i2c_client_tuner = client_tuner;
652 	return ret;
653 fail_tuner_module:
654 	i2c_unregister_device(client_tuner);
655 fail_tuner_device:
656 	module_put(client_demod->dev.driver->owner);
657 fail_demod_module:
658 	i2c_unregister_device(client_demod);
659 fail_demod_device:
660 	ret = -ENODEV;
661 	return ret;
662 }
663 
664 static int dvbsky_mygica_t230c_attach(struct dvb_usb_adapter *adap)
665 {
666 	struct dvbsky_state *state = adap_to_priv(adap);
667 	struct dvb_usb_device *d = adap_to_d(adap);
668 	struct i2c_adapter *i2c_adapter;
669 	struct i2c_client *client_demod, *client_tuner;
670 	struct i2c_board_info info;
671 	struct si2168_config si2168_config;
672 	struct si2157_config si2157_config;
673 
674 	/* attach demod */
675 	memset(&si2168_config, 0, sizeof(si2168_config));
676 	si2168_config.i2c_adapter = &i2c_adapter;
677 	si2168_config.fe = &adap->fe[0];
678 	si2168_config.ts_mode = SI2168_TS_PARALLEL;
679 	si2168_config.ts_clock_inv = 1;
680 	memset(&info, 0, sizeof(struct i2c_board_info));
681 	strlcpy(info.type, "si2168", sizeof(info.type));
682 	info.addr = 0x64;
683 	info.platform_data = &si2168_config;
684 
685 	request_module("si2168");
686 	client_demod = i2c_new_device(&d->i2c_adap, &info);
687 	if (!client_demod || !client_demod->dev.driver)
688 		goto fail_demod_device;
689 	if (!try_module_get(client_demod->dev.driver->owner))
690 		goto fail_demod_module;
691 
692 	/* attach tuner */
693 	memset(&si2157_config, 0, sizeof(si2157_config));
694 	si2157_config.fe = adap->fe[0];
695 	si2157_config.if_port = 0;
696 	memset(&info, 0, sizeof(struct i2c_board_info));
697 	strlcpy(info.type, "si2141", sizeof(info.type));
698 	info.addr = 0x60;
699 	info.platform_data = &si2157_config;
700 
701 	request_module("si2157");
702 	client_tuner = i2c_new_device(i2c_adapter, &info);
703 	if (!client_tuner || !client_tuner->dev.driver)
704 		goto fail_tuner_device;
705 	if (!try_module_get(client_tuner->dev.driver->owner))
706 		goto fail_tuner_module;
707 
708 	state->i2c_client_demod = client_demod;
709 	state->i2c_client_tuner = client_tuner;
710 	return 0;
711 
712 fail_tuner_module:
713 	i2c_unregister_device(client_tuner);
714 fail_tuner_device:
715 	module_put(client_demod->dev.driver->owner);
716 fail_demod_module:
717 	i2c_unregister_device(client_demod);
718 fail_demod_device:
719 	return -ENODEV;
720 }
721 
722 
723 static int dvbsky_identify_state(struct dvb_usb_device *d, const char **name)
724 {
725 	dvbsky_gpio_ctrl(d, 0x04, 1);
726 	msleep(20);
727 	dvbsky_gpio_ctrl(d, 0x83, 0);
728 	dvbsky_gpio_ctrl(d, 0xc0, 1);
729 	msleep(100);
730 	dvbsky_gpio_ctrl(d, 0x83, 1);
731 	dvbsky_gpio_ctrl(d, 0xc0, 0);
732 	msleep(50);
733 
734 	return WARM;
735 }
736 
737 static int dvbsky_init(struct dvb_usb_device *d)
738 {
739 	struct dvbsky_state *state = d_to_priv(d);
740 
741 	/* use default interface */
742 	/*
743 	ret = usb_set_interface(d->udev, 0, 0);
744 	if (ret)
745 		return ret;
746 	*/
747 	mutex_init(&state->stream_mutex);
748 
749 	state->last_lock = 0;
750 
751 	return 0;
752 }
753 
754 static void dvbsky_exit(struct dvb_usb_device *d)
755 {
756 	struct dvbsky_state *state = d_to_priv(d);
757 	struct i2c_client *client;
758 
759 	client = state->i2c_client_tuner;
760 	/* remove I2C tuner */
761 	if (client) {
762 		module_put(client->dev.driver->owner);
763 		i2c_unregister_device(client);
764 	}
765 	client = state->i2c_client_demod;
766 	/* remove I2C demod */
767 	if (client) {
768 		module_put(client->dev.driver->owner);
769 		i2c_unregister_device(client);
770 	}
771 	client = state->i2c_client_ci;
772 	/* remove I2C ci */
773 	if (client) {
774 		module_put(client->dev.driver->owner);
775 		i2c_unregister_device(client);
776 	}
777 }
778 
779 /* DVB USB Driver stuff */
780 static struct dvb_usb_device_properties dvbsky_s960_props = {
781 	.driver_name = KBUILD_MODNAME,
782 	.owner = THIS_MODULE,
783 	.adapter_nr = adapter_nr,
784 	.size_of_priv = sizeof(struct dvbsky_state),
785 
786 	.generic_bulk_ctrl_endpoint = 0x01,
787 	.generic_bulk_ctrl_endpoint_response = 0x81,
788 	.generic_bulk_ctrl_delay = DVBSKY_MSG_DELAY,
789 
790 	.i2c_algo         = &dvbsky_i2c_algo,
791 	.frontend_attach  = dvbsky_s960_attach,
792 	.init             = dvbsky_init,
793 	.get_rc_config    = dvbsky_get_rc_config,
794 	.streaming_ctrl   = dvbsky_streaming_ctrl,
795 	.identify_state	  = dvbsky_identify_state,
796 	.exit             = dvbsky_exit,
797 	.read_mac_address = dvbsky_read_mac_addr,
798 
799 	.num_adapters = 1,
800 	.adapter = {
801 		{
802 			.stream = DVB_USB_STREAM_BULK(0x82, 8, 4096),
803 		}
804 	}
805 };
806 
807 static struct dvb_usb_device_properties dvbsky_s960c_props = {
808 	.driver_name = KBUILD_MODNAME,
809 	.owner = THIS_MODULE,
810 	.adapter_nr = adapter_nr,
811 	.size_of_priv = sizeof(struct dvbsky_state),
812 
813 	.generic_bulk_ctrl_endpoint = 0x01,
814 	.generic_bulk_ctrl_endpoint_response = 0x81,
815 	.generic_bulk_ctrl_delay = DVBSKY_MSG_DELAY,
816 
817 	.i2c_algo         = &dvbsky_i2c_algo,
818 	.frontend_attach  = dvbsky_s960c_attach,
819 	.init             = dvbsky_init,
820 	.get_rc_config    = dvbsky_get_rc_config,
821 	.streaming_ctrl   = dvbsky_streaming_ctrl,
822 	.identify_state	  = dvbsky_identify_state,
823 	.exit             = dvbsky_exit,
824 	.read_mac_address = dvbsky_read_mac_addr,
825 
826 	.num_adapters = 1,
827 	.adapter = {
828 		{
829 			.stream = DVB_USB_STREAM_BULK(0x82, 8, 4096),
830 		}
831 	}
832 };
833 
834 static struct dvb_usb_device_properties dvbsky_t680c_props = {
835 	.driver_name = KBUILD_MODNAME,
836 	.owner = THIS_MODULE,
837 	.adapter_nr = adapter_nr,
838 	.size_of_priv = sizeof(struct dvbsky_state),
839 
840 	.generic_bulk_ctrl_endpoint = 0x01,
841 	.generic_bulk_ctrl_endpoint_response = 0x81,
842 	.generic_bulk_ctrl_delay = DVBSKY_MSG_DELAY,
843 
844 	.i2c_algo         = &dvbsky_i2c_algo,
845 	.frontend_attach  = dvbsky_t680c_attach,
846 	.init             = dvbsky_init,
847 	.get_rc_config    = dvbsky_get_rc_config,
848 	.streaming_ctrl   = dvbsky_streaming_ctrl,
849 	.identify_state	  = dvbsky_identify_state,
850 	.exit             = dvbsky_exit,
851 	.read_mac_address = dvbsky_read_mac_addr,
852 
853 	.num_adapters = 1,
854 	.adapter = {
855 		{
856 			.stream = DVB_USB_STREAM_BULK(0x82, 8, 4096),
857 		}
858 	}
859 };
860 
861 static struct dvb_usb_device_properties dvbsky_t330_props = {
862 	.driver_name = KBUILD_MODNAME,
863 	.owner = THIS_MODULE,
864 	.adapter_nr = adapter_nr,
865 	.size_of_priv = sizeof(struct dvbsky_state),
866 
867 	.generic_bulk_ctrl_endpoint = 0x01,
868 	.generic_bulk_ctrl_endpoint_response = 0x81,
869 	.generic_bulk_ctrl_delay = DVBSKY_MSG_DELAY,
870 
871 	.i2c_algo         = &dvbsky_i2c_algo,
872 	.frontend_attach  = dvbsky_t330_attach,
873 	.init             = dvbsky_init,
874 	.get_rc_config    = dvbsky_get_rc_config,
875 	.streaming_ctrl   = dvbsky_streaming_ctrl,
876 	.identify_state	  = dvbsky_identify_state,
877 	.exit             = dvbsky_exit,
878 	.read_mac_address = dvbsky_read_mac_addr,
879 
880 	.num_adapters = 1,
881 	.adapter = {
882 		{
883 			.stream = DVB_USB_STREAM_BULK(0x82, 8, 4096),
884 		}
885 	}
886 };
887 
888 static struct dvb_usb_device_properties mygica_t230c_props = {
889 	.driver_name = KBUILD_MODNAME,
890 	.owner = THIS_MODULE,
891 	.adapter_nr = adapter_nr,
892 	.size_of_priv = sizeof(struct dvbsky_state),
893 
894 	.generic_bulk_ctrl_endpoint = 0x01,
895 	.generic_bulk_ctrl_endpoint_response = 0x81,
896 	.generic_bulk_ctrl_delay = DVBSKY_MSG_DELAY,
897 
898 	.i2c_algo         = &dvbsky_i2c_algo,
899 	.frontend_attach  = dvbsky_mygica_t230c_attach,
900 	.init             = dvbsky_init,
901 	.get_rc_config    = dvbsky_get_rc_config,
902 	.streaming_ctrl   = dvbsky_streaming_ctrl,
903 	.identify_state	  = dvbsky_identify_state,
904 	.exit             = dvbsky_exit,
905 
906 	.num_adapters = 1,
907 	.adapter = {
908 		{
909 			.stream = DVB_USB_STREAM_BULK(0x82, 8, 4096),
910 		}
911 	}
912 };
913 
914 static const struct usb_device_id dvbsky_id_table[] = {
915 	{ DVB_USB_DEVICE(0x0572, 0x6831,
916 		&dvbsky_s960_props, "DVBSky S960/S860", RC_MAP_DVBSKY) },
917 	{ DVB_USB_DEVICE(0x0572, 0x960c,
918 		&dvbsky_s960c_props, "DVBSky S960CI", RC_MAP_DVBSKY) },
919 	{ DVB_USB_DEVICE(0x0572, 0x680c,
920 		&dvbsky_t680c_props, "DVBSky T680CI", RC_MAP_DVBSKY) },
921 	{ DVB_USB_DEVICE(0x0572, 0x0320,
922 		&dvbsky_t330_props, "DVBSky T330", RC_MAP_DVBSKY) },
923 	{ DVB_USB_DEVICE(USB_VID_TECHNOTREND,
924 		USB_PID_TECHNOTREND_TVSTICK_CT2_4400,
925 		&dvbsky_t330_props, "TechnoTrend TVStick CT2-4400",
926 		RC_MAP_TT_1500) },
927 	{ DVB_USB_DEVICE(USB_VID_TECHNOTREND,
928 		USB_PID_TECHNOTREND_CONNECT_CT2_4650_CI,
929 		&dvbsky_t680c_props, "TechnoTrend TT-connect CT2-4650 CI",
930 		RC_MAP_TT_1500) },
931 	{ DVB_USB_DEVICE(USB_VID_TECHNOTREND,
932 		USB_PID_TECHNOTREND_CONNECT_CT2_4650_CI_2,
933 		&dvbsky_t680c_props, "TechnoTrend TT-connect CT2-4650 CI v1.1",
934 		RC_MAP_TT_1500) },
935 	{ DVB_USB_DEVICE(USB_VID_TECHNOTREND,
936 		USB_PID_TECHNOTREND_CONNECT_S2_4650_CI,
937 		&dvbsky_s960c_props, "TechnoTrend TT-connect S2-4650 CI",
938 		RC_MAP_TT_1500) },
939 	{ DVB_USB_DEVICE(USB_VID_TERRATEC,
940 		USB_PID_TERRATEC_H7_3,
941 		&dvbsky_t680c_props, "Terratec H7 Rev.4",
942 		RC_MAP_TT_1500) },
943 	{ DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_S2_R4,
944 		&dvbsky_s960_props, "Terratec Cinergy S2 Rev.4",
945 		RC_MAP_DVBSKY) },
946 	{ DVB_USB_DEVICE(USB_VID_CONEXANT, USB_PID_MYGICA_T230C,
947 		&mygica_t230c_props, "MyGica Mini DVB-T2 USB Stick T230C",
948 		RC_MAP_TOTAL_MEDIA_IN_HAND_02) },
949 	{ }
950 };
951 MODULE_DEVICE_TABLE(usb, dvbsky_id_table);
952 
953 static struct usb_driver dvbsky_usb_driver = {
954 	.name = KBUILD_MODNAME,
955 	.id_table = dvbsky_id_table,
956 	.probe = dvb_usbv2_probe,
957 	.disconnect = dvb_usbv2_disconnect,
958 	.suspend = dvb_usbv2_suspend,
959 	.resume = dvb_usbv2_resume,
960 	.reset_resume = dvb_usbv2_reset_resume,
961 	.no_dynamic_id = 1,
962 	.soft_unbind = 1,
963 };
964 
965 module_usb_driver(dvbsky_usb_driver);
966 
967 MODULE_AUTHOR("Max nibble <nibble.max@gmail.com>");
968 MODULE_DESCRIPTION("Driver for DVBSky USB");
969 MODULE_LICENSE("GPL");
970