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 "m88ts2022.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 		fe_sec_voltage_t voltage);
49 	int (*fe_read_status)(struct dvb_frontend *fe,
50 		fe_status_t *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 	fe_sec_voltage_t 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, fe_status_t *status)
281 {
282 	struct dvb_usb_device *d = fe_to_d(fe);
283 	struct dvbsky_state *state = d_to_priv(d);
284 	int ret;
285 
286 	ret = state->fe_read_status(fe, status);
287 
288 	/* it need resync slave fifo when signal change from unlock to lock.*/
289 	if ((*status & FE_HAS_LOCK) && (!state->last_lock))
290 		dvbsky_stream_ctrl(d, 1);
291 
292 	state->last_lock = (*status & FE_HAS_LOCK) ? 1 : 0;
293 	return ret;
294 }
295 
296 static const struct m88ds3103_config dvbsky_s960_m88ds3103_config = {
297 	.i2c_addr = 0x68,
298 	.clock = 27000000,
299 	.i2c_wr_max = 33,
300 	.clock_out = 0,
301 	.ts_mode = M88DS3103_TS_CI,
302 	.ts_clk = 16000,
303 	.ts_clk_pol = 0,
304 	.agc = 0x99,
305 	.lnb_hv_pol = 1,
306 	.lnb_en_pol = 1,
307 };
308 
309 static int dvbsky_s960_attach(struct dvb_usb_adapter *adap)
310 {
311 	struct dvbsky_state *state = adap_to_priv(adap);
312 	struct dvb_usb_device *d = adap_to_d(adap);
313 	int ret = 0;
314 	/* demod I2C adapter */
315 	struct i2c_adapter *i2c_adapter;
316 	struct i2c_client *client;
317 	struct i2c_board_info info;
318 	struct m88ts2022_config m88ts2022_config = {
319 			.clock = 27000000,
320 		};
321 	memset(&info, 0, sizeof(struct i2c_board_info));
322 
323 	/* attach demod */
324 	adap->fe[0] = dvb_attach(m88ds3103_attach,
325 			&dvbsky_s960_m88ds3103_config,
326 			&d->i2c_adap,
327 			&i2c_adapter);
328 	if (!adap->fe[0]) {
329 		dev_err(&d->udev->dev, "dvbsky_s960_attach fail.\n");
330 		ret = -ENODEV;
331 		goto fail_attach;
332 	}
333 
334 	/* attach tuner */
335 	m88ts2022_config.fe = adap->fe[0];
336 	strlcpy(info.type, "m88ts2022", I2C_NAME_SIZE);
337 	info.addr = 0x60;
338 	info.platform_data = &m88ts2022_config;
339 	request_module("m88ts2022");
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 	fe_sec_voltage_t 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 m88ts2022_config m88ts2022_config = {
443 			.clock = 27000000,
444 		};
445 	memset(&info, 0, sizeof(struct i2c_board_info));
446 
447 	/* attach demod */
448 	adap->fe[0] = dvb_attach(m88ds3103_attach,
449 			&dvbsky_s960c_m88ds3103_config,
450 			&d->i2c_adap,
451 			&i2c_adapter);
452 	if (!adap->fe[0]) {
453 		dev_err(&d->udev->dev, "dvbsky_s960ci_attach fail.\n");
454 		ret = -ENODEV;
455 		goto fail_attach;
456 	}
457 
458 	/* attach tuner */
459 	m88ts2022_config.fe = adap->fe[0];
460 	strlcpy(info.type, "m88ts2022", I2C_NAME_SIZE);
461 	info.addr = 0x60;
462 	info.platform_data = &m88ts2022_config;
463 	request_module("m88ts2022");
464 	client_tuner = i2c_new_device(i2c_adapter, &info);
465 	if (client_tuner == NULL || client_tuner->dev.driver == NULL) {
466 		ret = -ENODEV;
467 		goto fail_tuner_device;
468 	}
469 
470 	if (!try_module_get(client_tuner->dev.driver->owner)) {
471 		ret = -ENODEV;
472 		goto fail_tuner_module;
473 	}
474 
475 	/* attach ci controller */
476 	memset(&sp2_config, 0, sizeof(sp2_config));
477 	sp2_config.dvb_adap = &adap->dvb_adap;
478 	sp2_config.priv = d;
479 	sp2_config.ci_control = dvbsky_ci_ctrl;
480 	memset(&info, 0, sizeof(struct i2c_board_info));
481 	strlcpy(info.type, "sp2", I2C_NAME_SIZE);
482 	info.addr = 0x40;
483 	info.platform_data = &sp2_config;
484 	request_module("sp2");
485 	client_ci = i2c_new_device(&d->i2c_adap, &info);
486 	if (client_ci == NULL || client_ci->dev.driver == NULL) {
487 		ret = -ENODEV;
488 		goto fail_ci_device;
489 	}
490 
491 	if (!try_module_get(client_ci->dev.driver->owner)) {
492 		ret = -ENODEV;
493 		goto fail_ci_module;
494 	}
495 
496 	/* delegate signal strength measurement to tuner */
497 	adap->fe[0]->ops.read_signal_strength =
498 			adap->fe[0]->ops.tuner_ops.get_rf_strength;
499 
500 	/* hook fe: need to resync the slave fifo when signal locks. */
501 	state->fe_read_status = adap->fe[0]->ops.read_status;
502 	adap->fe[0]->ops.read_status = dvbsky_usb_read_status;
503 
504 	/* hook fe: LNB off/on is control by Cypress usb chip. */
505 	state->fe_set_voltage = adap->fe[0]->ops.set_voltage;
506 	adap->fe[0]->ops.set_voltage = dvbsky_usb_ci_set_voltage;
507 
508 	state->i2c_client_tuner = client_tuner;
509 	state->i2c_client_ci = client_ci;
510 	return ret;
511 fail_ci_module:
512 	i2c_unregister_device(client_ci);
513 fail_ci_device:
514 	module_put(client_tuner->dev.driver->owner);
515 fail_tuner_module:
516 	i2c_unregister_device(client_tuner);
517 fail_tuner_device:
518 	dvb_frontend_detach(adap->fe[0]);
519 fail_attach:
520 	return ret;
521 }
522 
523 static int dvbsky_t680c_attach(struct dvb_usb_adapter *adap)
524 {
525 	struct dvbsky_state *state = adap_to_priv(adap);
526 	struct dvb_usb_device *d = adap_to_d(adap);
527 	int ret = 0;
528 	struct i2c_adapter *i2c_adapter;
529 	struct i2c_client *client_demod, *client_tuner, *client_ci;
530 	struct i2c_board_info info;
531 	struct si2168_config si2168_config;
532 	struct si2157_config si2157_config;
533 	struct sp2_config sp2_config;
534 
535 	/* attach demod */
536 	memset(&si2168_config, 0, sizeof(si2168_config));
537 	si2168_config.i2c_adapter = &i2c_adapter;
538 	si2168_config.fe = &adap->fe[0];
539 	si2168_config.ts_mode = SI2168_TS_PARALLEL;
540 	memset(&info, 0, sizeof(struct i2c_board_info));
541 	strlcpy(info.type, "si2168", I2C_NAME_SIZE);
542 	info.addr = 0x64;
543 	info.platform_data = &si2168_config;
544 
545 	request_module(info.type);
546 	client_demod = i2c_new_device(&d->i2c_adap, &info);
547 	if (client_demod == NULL ||
548 			client_demod->dev.driver == NULL)
549 		goto fail_demod_device;
550 	if (!try_module_get(client_demod->dev.driver->owner))
551 		goto fail_demod_module;
552 
553 	/* attach tuner */
554 	memset(&si2157_config, 0, sizeof(si2157_config));
555 	si2157_config.fe = adap->fe[0];
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 | 0x40;
623 	memset(&info, 0, sizeof(struct i2c_board_info));
624 	strlcpy(info.type, "si2168", I2C_NAME_SIZE);
625 	info.addr = 0x64;
626 	info.platform_data = &si2168_config;
627 
628 	request_module(info.type);
629 	client_demod = i2c_new_device(&d->i2c_adap, &info);
630 	if (client_demod == NULL ||
631 			client_demod->dev.driver == NULL)
632 		goto fail_demod_device;
633 	if (!try_module_get(client_demod->dev.driver->owner))
634 		goto fail_demod_module;
635 
636 	/* attach tuner */
637 	memset(&si2157_config, 0, sizeof(si2157_config));
638 	si2157_config.fe = adap->fe[0];
639 	memset(&info, 0, sizeof(struct i2c_board_info));
640 	strlcpy(info.type, "si2157", I2C_NAME_SIZE);
641 	info.addr = 0x60;
642 	info.platform_data = &si2157_config;
643 
644 	request_module(info.type);
645 	client_tuner = i2c_new_device(i2c_adapter, &info);
646 	if (client_tuner == NULL ||
647 			client_tuner->dev.driver == NULL)
648 		goto fail_tuner_device;
649 	if (!try_module_get(client_tuner->dev.driver->owner))
650 		goto fail_tuner_module;
651 
652 	state->i2c_client_demod = client_demod;
653 	state->i2c_client_tuner = client_tuner;
654 	return ret;
655 fail_tuner_module:
656 	i2c_unregister_device(client_tuner);
657 fail_tuner_device:
658 	module_put(client_demod->dev.driver->owner);
659 fail_demod_module:
660 	i2c_unregister_device(client_demod);
661 fail_demod_device:
662 	ret = -ENODEV;
663 	return ret;
664 }
665 
666 static int dvbsky_identify_state(struct dvb_usb_device *d, const char **name)
667 {
668 	dvbsky_gpio_ctrl(d, 0x04, 1);
669 	msleep(20);
670 	dvbsky_gpio_ctrl(d, 0x83, 0);
671 	dvbsky_gpio_ctrl(d, 0xc0, 1);
672 	msleep(100);
673 	dvbsky_gpio_ctrl(d, 0x83, 1);
674 	dvbsky_gpio_ctrl(d, 0xc0, 0);
675 	msleep(50);
676 
677 	return WARM;
678 }
679 
680 static int dvbsky_init(struct dvb_usb_device *d)
681 {
682 	struct dvbsky_state *state = d_to_priv(d);
683 
684 	/* use default interface */
685 	/*
686 	ret = usb_set_interface(d->udev, 0, 0);
687 	if (ret)
688 		return ret;
689 	*/
690 	mutex_init(&state->stream_mutex);
691 
692 	state->last_lock = 0;
693 
694 	return 0;
695 }
696 
697 static void dvbsky_exit(struct dvb_usb_device *d)
698 {
699 	struct dvbsky_state *state = d_to_priv(d);
700 	struct i2c_client *client;
701 
702 	client = state->i2c_client_tuner;
703 	/* remove I2C tuner */
704 	if (client) {
705 		module_put(client->dev.driver->owner);
706 		i2c_unregister_device(client);
707 	}
708 	client = state->i2c_client_demod;
709 	/* remove I2C demod */
710 	if (client) {
711 		module_put(client->dev.driver->owner);
712 		i2c_unregister_device(client);
713 	}
714 	client = state->i2c_client_ci;
715 	/* remove I2C ci */
716 	if (client) {
717 		module_put(client->dev.driver->owner);
718 		i2c_unregister_device(client);
719 	}
720 }
721 
722 /* DVB USB Driver stuff */
723 static struct dvb_usb_device_properties dvbsky_s960_props = {
724 	.driver_name = KBUILD_MODNAME,
725 	.owner = THIS_MODULE,
726 	.adapter_nr = adapter_nr,
727 	.size_of_priv = sizeof(struct dvbsky_state),
728 
729 	.generic_bulk_ctrl_endpoint = 0x01,
730 	.generic_bulk_ctrl_endpoint_response = 0x81,
731 	.generic_bulk_ctrl_delay = DVBSKY_MSG_DELAY,
732 
733 	.i2c_algo         = &dvbsky_i2c_algo,
734 	.frontend_attach  = dvbsky_s960_attach,
735 	.init             = dvbsky_init,
736 	.get_rc_config    = dvbsky_get_rc_config,
737 	.streaming_ctrl   = dvbsky_streaming_ctrl,
738 	.identify_state	  = dvbsky_identify_state,
739 	.exit             = dvbsky_exit,
740 	.read_mac_address = dvbsky_read_mac_addr,
741 
742 	.num_adapters = 1,
743 	.adapter = {
744 		{
745 			.stream = DVB_USB_STREAM_BULK(0x82, 8, 4096),
746 		}
747 	}
748 };
749 
750 static struct dvb_usb_device_properties dvbsky_s960c_props = {
751 	.driver_name = KBUILD_MODNAME,
752 	.owner = THIS_MODULE,
753 	.adapter_nr = adapter_nr,
754 	.size_of_priv = sizeof(struct dvbsky_state),
755 
756 	.generic_bulk_ctrl_endpoint = 0x01,
757 	.generic_bulk_ctrl_endpoint_response = 0x81,
758 	.generic_bulk_ctrl_delay = DVBSKY_MSG_DELAY,
759 
760 	.i2c_algo         = &dvbsky_i2c_algo,
761 	.frontend_attach  = dvbsky_s960c_attach,
762 	.init             = dvbsky_init,
763 	.get_rc_config    = dvbsky_get_rc_config,
764 	.streaming_ctrl   = dvbsky_streaming_ctrl,
765 	.identify_state	  = dvbsky_identify_state,
766 	.exit             = dvbsky_exit,
767 	.read_mac_address = dvbsky_read_mac_addr,
768 
769 	.num_adapters = 1,
770 	.adapter = {
771 		{
772 			.stream = DVB_USB_STREAM_BULK(0x82, 8, 4096),
773 		}
774 	}
775 };
776 
777 static struct dvb_usb_device_properties dvbsky_t680c_props = {
778 	.driver_name = KBUILD_MODNAME,
779 	.owner = THIS_MODULE,
780 	.adapter_nr = adapter_nr,
781 	.size_of_priv = sizeof(struct dvbsky_state),
782 
783 	.generic_bulk_ctrl_endpoint = 0x01,
784 	.generic_bulk_ctrl_endpoint_response = 0x81,
785 	.generic_bulk_ctrl_delay = DVBSKY_MSG_DELAY,
786 
787 	.i2c_algo         = &dvbsky_i2c_algo,
788 	.frontend_attach  = dvbsky_t680c_attach,
789 	.init             = dvbsky_init,
790 	.get_rc_config    = dvbsky_get_rc_config,
791 	.streaming_ctrl   = dvbsky_streaming_ctrl,
792 	.identify_state	  = dvbsky_identify_state,
793 	.exit             = dvbsky_exit,
794 	.read_mac_address = dvbsky_read_mac_addr,
795 
796 	.num_adapters = 1,
797 	.adapter = {
798 		{
799 			.stream = DVB_USB_STREAM_BULK(0x82, 8, 4096),
800 		}
801 	}
802 };
803 
804 static struct dvb_usb_device_properties dvbsky_t330_props = {
805 	.driver_name = KBUILD_MODNAME,
806 	.owner = THIS_MODULE,
807 	.adapter_nr = adapter_nr,
808 	.size_of_priv = sizeof(struct dvbsky_state),
809 
810 	.generic_bulk_ctrl_endpoint = 0x01,
811 	.generic_bulk_ctrl_endpoint_response = 0x81,
812 	.generic_bulk_ctrl_delay = DVBSKY_MSG_DELAY,
813 
814 	.i2c_algo         = &dvbsky_i2c_algo,
815 	.frontend_attach  = dvbsky_t330_attach,
816 	.init             = dvbsky_init,
817 	.get_rc_config    = dvbsky_get_rc_config,
818 	.streaming_ctrl   = dvbsky_streaming_ctrl,
819 	.identify_state	  = dvbsky_identify_state,
820 	.exit             = dvbsky_exit,
821 	.read_mac_address = dvbsky_read_mac_addr,
822 
823 	.num_adapters = 1,
824 	.adapter = {
825 		{
826 			.stream = DVB_USB_STREAM_BULK(0x82, 8, 4096),
827 		}
828 	}
829 };
830 
831 static const struct usb_device_id dvbsky_id_table[] = {
832 	{ DVB_USB_DEVICE(0x0572, 0x6831,
833 		&dvbsky_s960_props, "DVBSky S960/S860", RC_MAP_DVBSKY) },
834 	{ DVB_USB_DEVICE(0x0572, 0x960c,
835 		&dvbsky_s960c_props, "DVBSky S960CI", RC_MAP_DVBSKY) },
836 	{ DVB_USB_DEVICE(0x0572, 0x680c,
837 		&dvbsky_t680c_props, "DVBSky T680CI", RC_MAP_DVBSKY) },
838 	{ DVB_USB_DEVICE(0x0572, 0x0320,
839 		&dvbsky_t330_props, "DVBSky T330", RC_MAP_DVBSKY) },
840 	{ DVB_USB_DEVICE(USB_VID_TECHNOTREND,
841 		USB_PID_TECHNOTREND_TVSTICK_CT2_4400,
842 		&dvbsky_t330_props, "TechnoTrend TVStick CT2-4400",
843 		RC_MAP_TT_1500) },
844 	{ DVB_USB_DEVICE(USB_VID_TECHNOTREND,
845 		USB_PID_TECHNOTREND_CONNECT_CT2_4650_CI,
846 		&dvbsky_t680c_props, "TechnoTrend TT-connect CT2-4650 CI",
847 		RC_MAP_TT_1500) },
848 	{ }
849 };
850 MODULE_DEVICE_TABLE(usb, dvbsky_id_table);
851 
852 static struct usb_driver dvbsky_usb_driver = {
853 	.name = KBUILD_MODNAME,
854 	.id_table = dvbsky_id_table,
855 	.probe = dvb_usbv2_probe,
856 	.disconnect = dvb_usbv2_disconnect,
857 	.suspend = dvb_usbv2_suspend,
858 	.resume = dvb_usbv2_resume,
859 	.reset_resume = dvb_usbv2_reset_resume,
860 	.no_dynamic_id = 1,
861 	.soft_unbind = 1,
862 };
863 
864 module_usb_driver(dvbsky_usb_driver);
865 
866 MODULE_AUTHOR("Max nibble <nibble.max@gmail.com>");
867 MODULE_DESCRIPTION("Driver for DVBSky USB");
868 MODULE_LICENSE("GPL");
869