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