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_TYPE_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_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;
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;
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_identify_state(struct dvb_usb_device *d, const char **name)
665 {
666 	dvbsky_gpio_ctrl(d, 0x04, 1);
667 	msleep(20);
668 	dvbsky_gpio_ctrl(d, 0x83, 0);
669 	dvbsky_gpio_ctrl(d, 0xc0, 1);
670 	msleep(100);
671 	dvbsky_gpio_ctrl(d, 0x83, 1);
672 	dvbsky_gpio_ctrl(d, 0xc0, 0);
673 	msleep(50);
674 
675 	return WARM;
676 }
677 
678 static int dvbsky_init(struct dvb_usb_device *d)
679 {
680 	struct dvbsky_state *state = d_to_priv(d);
681 
682 	/* use default interface */
683 	/*
684 	ret = usb_set_interface(d->udev, 0, 0);
685 	if (ret)
686 		return ret;
687 	*/
688 	mutex_init(&state->stream_mutex);
689 
690 	state->last_lock = 0;
691 
692 	return 0;
693 }
694 
695 static void dvbsky_exit(struct dvb_usb_device *d)
696 {
697 	struct dvbsky_state *state = d_to_priv(d);
698 	struct i2c_client *client;
699 
700 	client = state->i2c_client_tuner;
701 	/* remove I2C tuner */
702 	if (client) {
703 		module_put(client->dev.driver->owner);
704 		i2c_unregister_device(client);
705 	}
706 	client = state->i2c_client_demod;
707 	/* remove I2C demod */
708 	if (client) {
709 		module_put(client->dev.driver->owner);
710 		i2c_unregister_device(client);
711 	}
712 	client = state->i2c_client_ci;
713 	/* remove I2C ci */
714 	if (client) {
715 		module_put(client->dev.driver->owner);
716 		i2c_unregister_device(client);
717 	}
718 }
719 
720 /* DVB USB Driver stuff */
721 static struct dvb_usb_device_properties dvbsky_s960_props = {
722 	.driver_name = KBUILD_MODNAME,
723 	.owner = THIS_MODULE,
724 	.adapter_nr = adapter_nr,
725 	.size_of_priv = sizeof(struct dvbsky_state),
726 
727 	.generic_bulk_ctrl_endpoint = 0x01,
728 	.generic_bulk_ctrl_endpoint_response = 0x81,
729 	.generic_bulk_ctrl_delay = DVBSKY_MSG_DELAY,
730 
731 	.i2c_algo         = &dvbsky_i2c_algo,
732 	.frontend_attach  = dvbsky_s960_attach,
733 	.init             = dvbsky_init,
734 	.get_rc_config    = dvbsky_get_rc_config,
735 	.streaming_ctrl   = dvbsky_streaming_ctrl,
736 	.identify_state	  = dvbsky_identify_state,
737 	.exit             = dvbsky_exit,
738 	.read_mac_address = dvbsky_read_mac_addr,
739 
740 	.num_adapters = 1,
741 	.adapter = {
742 		{
743 			.stream = DVB_USB_STREAM_BULK(0x82, 8, 4096),
744 		}
745 	}
746 };
747 
748 static struct dvb_usb_device_properties dvbsky_s960c_props = {
749 	.driver_name = KBUILD_MODNAME,
750 	.owner = THIS_MODULE,
751 	.adapter_nr = adapter_nr,
752 	.size_of_priv = sizeof(struct dvbsky_state),
753 
754 	.generic_bulk_ctrl_endpoint = 0x01,
755 	.generic_bulk_ctrl_endpoint_response = 0x81,
756 	.generic_bulk_ctrl_delay = DVBSKY_MSG_DELAY,
757 
758 	.i2c_algo         = &dvbsky_i2c_algo,
759 	.frontend_attach  = dvbsky_s960c_attach,
760 	.init             = dvbsky_init,
761 	.get_rc_config    = dvbsky_get_rc_config,
762 	.streaming_ctrl   = dvbsky_streaming_ctrl,
763 	.identify_state	  = dvbsky_identify_state,
764 	.exit             = dvbsky_exit,
765 	.read_mac_address = dvbsky_read_mac_addr,
766 
767 	.num_adapters = 1,
768 	.adapter = {
769 		{
770 			.stream = DVB_USB_STREAM_BULK(0x82, 8, 4096),
771 		}
772 	}
773 };
774 
775 static struct dvb_usb_device_properties dvbsky_t680c_props = {
776 	.driver_name = KBUILD_MODNAME,
777 	.owner = THIS_MODULE,
778 	.adapter_nr = adapter_nr,
779 	.size_of_priv = sizeof(struct dvbsky_state),
780 
781 	.generic_bulk_ctrl_endpoint = 0x01,
782 	.generic_bulk_ctrl_endpoint_response = 0x81,
783 	.generic_bulk_ctrl_delay = DVBSKY_MSG_DELAY,
784 
785 	.i2c_algo         = &dvbsky_i2c_algo,
786 	.frontend_attach  = dvbsky_t680c_attach,
787 	.init             = dvbsky_init,
788 	.get_rc_config    = dvbsky_get_rc_config,
789 	.streaming_ctrl   = dvbsky_streaming_ctrl,
790 	.identify_state	  = dvbsky_identify_state,
791 	.exit             = dvbsky_exit,
792 	.read_mac_address = dvbsky_read_mac_addr,
793 
794 	.num_adapters = 1,
795 	.adapter = {
796 		{
797 			.stream = DVB_USB_STREAM_BULK(0x82, 8, 4096),
798 		}
799 	}
800 };
801 
802 static struct dvb_usb_device_properties dvbsky_t330_props = {
803 	.driver_name = KBUILD_MODNAME,
804 	.owner = THIS_MODULE,
805 	.adapter_nr = adapter_nr,
806 	.size_of_priv = sizeof(struct dvbsky_state),
807 
808 	.generic_bulk_ctrl_endpoint = 0x01,
809 	.generic_bulk_ctrl_endpoint_response = 0x81,
810 	.generic_bulk_ctrl_delay = DVBSKY_MSG_DELAY,
811 
812 	.i2c_algo         = &dvbsky_i2c_algo,
813 	.frontend_attach  = dvbsky_t330_attach,
814 	.init             = dvbsky_init,
815 	.get_rc_config    = dvbsky_get_rc_config,
816 	.streaming_ctrl   = dvbsky_streaming_ctrl,
817 	.identify_state	  = dvbsky_identify_state,
818 	.exit             = dvbsky_exit,
819 	.read_mac_address = dvbsky_read_mac_addr,
820 
821 	.num_adapters = 1,
822 	.adapter = {
823 		{
824 			.stream = DVB_USB_STREAM_BULK(0x82, 8, 4096),
825 		}
826 	}
827 };
828 
829 static const struct usb_device_id dvbsky_id_table[] = {
830 	{ DVB_USB_DEVICE(0x0572, 0x6831,
831 		&dvbsky_s960_props, "DVBSky S960/S860", RC_MAP_DVBSKY) },
832 	{ DVB_USB_DEVICE(0x0572, 0x960c,
833 		&dvbsky_s960c_props, "DVBSky S960CI", RC_MAP_DVBSKY) },
834 	{ DVB_USB_DEVICE(0x0572, 0x680c,
835 		&dvbsky_t680c_props, "DVBSky T680CI", RC_MAP_DVBSKY) },
836 	{ DVB_USB_DEVICE(0x0572, 0x0320,
837 		&dvbsky_t330_props, "DVBSky T330", RC_MAP_DVBSKY) },
838 	{ DVB_USB_DEVICE(USB_VID_TECHNOTREND,
839 		USB_PID_TECHNOTREND_TVSTICK_CT2_4400,
840 		&dvbsky_t330_props, "TechnoTrend TVStick CT2-4400",
841 		RC_MAP_TT_1500) },
842 	{ DVB_USB_DEVICE(USB_VID_TECHNOTREND,
843 		USB_PID_TECHNOTREND_CONNECT_CT2_4650_CI,
844 		&dvbsky_t680c_props, "TechnoTrend TT-connect CT2-4650 CI",
845 		RC_MAP_TT_1500) },
846 	{ DVB_USB_DEVICE(USB_VID_TECHNOTREND,
847 		USB_PID_TECHNOTREND_CONNECT_CT2_4650_CI_2,
848 		&dvbsky_t680c_props, "TechnoTrend TT-connect CT2-4650 CI v1.1",
849 		RC_MAP_TT_1500) },
850 	{ DVB_USB_DEVICE(USB_VID_TECHNOTREND,
851 		USB_PID_TECHNOTREND_CONNECT_S2_4650_CI,
852 		&dvbsky_s960c_props, "TechnoTrend TT-connect S2-4650 CI",
853 		RC_MAP_TT_1500) },
854 	{ DVB_USB_DEVICE(USB_VID_TERRATEC,
855 		USB_PID_TERRATEC_H7_3,
856 		&dvbsky_t680c_props, "Terratec H7 Rev.4",
857 		RC_MAP_TT_1500) },
858 	{ DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_S2_R4,
859 		&dvbsky_s960_props, "Terratec Cinergy S2 Rev.4",
860 		RC_MAP_DVBSKY) },
861 	{ }
862 };
863 MODULE_DEVICE_TABLE(usb, dvbsky_id_table);
864 
865 static struct usb_driver dvbsky_usb_driver = {
866 	.name = KBUILD_MODNAME,
867 	.id_table = dvbsky_id_table,
868 	.probe = dvb_usbv2_probe,
869 	.disconnect = dvb_usbv2_disconnect,
870 	.suspend = dvb_usbv2_suspend,
871 	.resume = dvb_usbv2_resume,
872 	.reset_resume = dvb_usbv2_reset_resume,
873 	.no_dynamic_id = 1,
874 	.soft_unbind = 1,
875 };
876 
877 module_usb_driver(dvbsky_usb_driver);
878 
879 MODULE_AUTHOR("Max nibble <nibble.max@gmail.com>");
880 MODULE_DESCRIPTION("Driver for DVBSky USB");
881 MODULE_LICENSE("GPL");
882