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