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 
dvbsky_usb_generic_rw(struct dvb_usb_device * d,u8 * wbuf,u16 wlen,u8 * rbuf,u16 rlen)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 
dvbsky_stream_ctrl(struct dvb_usb_device * d,u8 onoff)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 
dvbsky_streaming_ctrl(struct dvb_frontend * fe,int onoff)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 */
dvbsky_gpio_ctrl(struct dvb_usb_device * d,u8 gport,u8 value)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 */
dvbsky_i2c_xfer(struct i2c_adapter * adap,struct i2c_msg msg[],int num)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 
dvbsky_i2c_func(struct i2c_adapter * adapter)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)
dvbsky_rc_query(struct dvb_usb_device * d)195 static int dvbsky_rc_query(struct dvb_usb_device *d)
196 {
197 	u32 code = 0xffff, scancode;
198 	u8 rc5_command, rc5_system;
199 	u8 obuf[2], ibuf[2], toggle;
200 	int ret;
201 
202 	obuf[0] = 0x10;
203 	ret = dvbsky_usb_generic_rw(d, obuf, 1, ibuf, 2);
204 	if (ret)
205 		dev_err(&d->udev->dev, "failed=%d\n", ret);
206 	if (ret == 0)
207 		code = (ibuf[0] << 8) | ibuf[1];
208 	if (code != 0xffff) {
209 		dev_dbg(&d->udev->dev, "rc code: %x\n", code);
210 		rc5_command = code & 0x3F;
211 		rc5_system = (code & 0x7C0) >> 6;
212 		toggle = (code & 0x800) ? 1 : 0;
213 		scancode = rc5_system << 8 | rc5_command;
214 		rc_keydown(d->rc_dev, RC_PROTO_RC5, scancode, toggle);
215 	}
216 	return 0;
217 }
218 
dvbsky_get_rc_config(struct dvb_usb_device * d,struct dvb_usb_rc * rc)219 static int dvbsky_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
220 {
221 	if (dvb_usb_dvbsky_disable_rc) {
222 		rc->map_name = NULL;
223 		return 0;
224 	}
225 
226 	rc->allowed_protos = RC_PROTO_BIT_RC5;
227 	rc->query          = dvbsky_rc_query;
228 	rc->interval       = 300;
229 	return 0;
230 }
231 #else
232 	#define dvbsky_get_rc_config NULL
233 #endif
234 
dvbsky_usb_set_voltage(struct dvb_frontend * fe,enum fe_sec_voltage voltage)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 
dvbsky_read_mac_addr(struct dvb_usb_adapter * adap,u8 mac[6])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 
dvbsky_usb_read_status(struct dvb_frontend * fe,enum fe_status * status)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 
dvbsky_s960_attach(struct dvb_usb_adapter * adap)293 static int dvbsky_s960_attach(struct dvb_usb_adapter *adap)
294 {
295 	struct dvbsky_state *state = adap_to_priv(adap);
296 	struct dvb_usb_device *d = adap_to_d(adap);
297 	struct i2c_adapter *i2c_adapter;
298 	struct m88ds3103_platform_data m88ds3103_pdata = {};
299 	struct ts2020_config ts2020_config = {};
300 
301 	/* attach demod */
302 	m88ds3103_pdata.clk = 27000000;
303 	m88ds3103_pdata.i2c_wr_max = 33;
304 	m88ds3103_pdata.clk_out = 0;
305 	m88ds3103_pdata.ts_mode = M88DS3103_TS_CI;
306 	m88ds3103_pdata.ts_clk = 16000;
307 	m88ds3103_pdata.ts_clk_pol = 0;
308 	m88ds3103_pdata.agc = 0x99;
309 	m88ds3103_pdata.lnb_hv_pol = 1,
310 	m88ds3103_pdata.lnb_en_pol = 1,
311 
312 	state->i2c_client_demod = dvb_module_probe("m88ds3103", NULL,
313 						   &d->i2c_adap,
314 						   0x68, &m88ds3103_pdata);
315 	if (!state->i2c_client_demod)
316 		return -ENODEV;
317 
318 	adap->fe[0] = m88ds3103_pdata.get_dvb_frontend(state->i2c_client_demod);
319 	i2c_adapter = m88ds3103_pdata.get_i2c_adapter(state->i2c_client_demod);
320 
321 	/* attach tuner */
322 	ts2020_config.fe = adap->fe[0];
323 	ts2020_config.get_agc_pwm = m88ds3103_get_agc_pwm;
324 
325 	state->i2c_client_tuner = dvb_module_probe("ts2020", NULL,
326 						   i2c_adapter,
327 						   0x60, &ts2020_config);
328 	if (!state->i2c_client_tuner) {
329 		dvb_module_release(state->i2c_client_demod);
330 		return -ENODEV;
331 	}
332 
333 	/* delegate signal strength measurement to tuner */
334 	adap->fe[0]->ops.read_signal_strength =
335 			adap->fe[0]->ops.tuner_ops.get_rf_strength;
336 
337 	/* hook fe: need to resync the slave fifo when signal locks. */
338 	state->fe_read_status = adap->fe[0]->ops.read_status;
339 	adap->fe[0]->ops.read_status = dvbsky_usb_read_status;
340 
341 	/* hook fe: LNB off/on is control by Cypress usb chip. */
342 	state->fe_set_voltage = adap->fe[0]->ops.set_voltage;
343 	adap->fe[0]->ops.set_voltage = dvbsky_usb_set_voltage;
344 
345 	return 0;
346 }
347 
dvbsky_usb_ci_set_voltage(struct dvb_frontend * fe,enum fe_sec_voltage voltage)348 static int dvbsky_usb_ci_set_voltage(struct dvb_frontend *fe,
349 	enum fe_sec_voltage voltage)
350 {
351 	struct dvb_usb_device *d = fe_to_d(fe);
352 	struct dvbsky_state *state = d_to_priv(d);
353 	u8 value;
354 
355 	if (voltage == SEC_VOLTAGE_OFF)
356 		value = 0;
357 	else
358 		value = 1;
359 	dvbsky_gpio_ctrl(d, 0x00, value);
360 
361 	return state->fe_set_voltage(fe, voltage);
362 }
363 
dvbsky_ci_ctrl(void * priv,u8 read,int addr,u8 data,int * mem)364 static int dvbsky_ci_ctrl(void *priv, u8 read, int addr,
365 					u8 data, int *mem)
366 {
367 	struct dvb_usb_device *d = priv;
368 	int ret = 0;
369 	u8 command[4], respond[2], command_size, respond_size;
370 
371 	command[1] = (u8)((addr >> 8) & 0xff); /*high part of address*/
372 	command[2] = (u8)(addr & 0xff); /*low part of address*/
373 	if (read) {
374 		command[0] = 0x71;
375 		command_size = 3;
376 		respond_size = 2;
377 	} else {
378 		command[0] = 0x70;
379 		command[3] = data;
380 		command_size = 4;
381 		respond_size = 1;
382 	}
383 	ret = dvbsky_usb_generic_rw(d, command, command_size,
384 			respond, respond_size);
385 	if (ret)
386 		goto err;
387 	if (read)
388 		*mem = respond[1];
389 	return ret;
390 err:
391 	dev_err(&d->udev->dev, "ci control failed=%d\n", ret);
392 	return ret;
393 }
394 
dvbsky_s960c_attach(struct dvb_usb_adapter * adap)395 static int dvbsky_s960c_attach(struct dvb_usb_adapter *adap)
396 {
397 	struct dvbsky_state *state = adap_to_priv(adap);
398 	struct dvb_usb_device *d = adap_to_d(adap);
399 	struct i2c_adapter *i2c_adapter;
400 	struct m88ds3103_platform_data m88ds3103_pdata = {};
401 	struct ts2020_config ts2020_config = {};
402 	struct sp2_config sp2_config = {};
403 
404 	/* attach demod */
405 	m88ds3103_pdata.clk = 27000000,
406 	m88ds3103_pdata.i2c_wr_max = 33,
407 	m88ds3103_pdata.clk_out = 0,
408 	m88ds3103_pdata.ts_mode = M88DS3103_TS_CI,
409 	m88ds3103_pdata.ts_clk = 10000,
410 	m88ds3103_pdata.ts_clk_pol = 1,
411 	m88ds3103_pdata.agc = 0x99,
412 	m88ds3103_pdata.lnb_hv_pol = 0,
413 	m88ds3103_pdata.lnb_en_pol = 1,
414 
415 	state->i2c_client_demod = dvb_module_probe("m88ds3103", NULL,
416 						   &d->i2c_adap,
417 						   0x68, &m88ds3103_pdata);
418 	if (!state->i2c_client_demod)
419 		return -ENODEV;
420 
421 	adap->fe[0] = m88ds3103_pdata.get_dvb_frontend(state->i2c_client_demod);
422 	i2c_adapter = m88ds3103_pdata.get_i2c_adapter(state->i2c_client_demod);
423 
424 	/* attach tuner */
425 	ts2020_config.fe = adap->fe[0];
426 	ts2020_config.get_agc_pwm = m88ds3103_get_agc_pwm;
427 
428 	state->i2c_client_tuner = dvb_module_probe("ts2020", NULL,
429 						   i2c_adapter,
430 						   0x60, &ts2020_config);
431 	if (!state->i2c_client_tuner) {
432 		dvb_module_release(state->i2c_client_demod);
433 		return -ENODEV;
434 	}
435 
436 	/* attach ci controller */
437 	sp2_config.dvb_adap = &adap->dvb_adap;
438 	sp2_config.priv = d;
439 	sp2_config.ci_control = dvbsky_ci_ctrl;
440 
441 	state->i2c_client_ci = dvb_module_probe("sp2", NULL,
442 						&d->i2c_adap,
443 						0x40, &sp2_config);
444 
445 	if (!state->i2c_client_ci) {
446 		dvb_module_release(state->i2c_client_tuner);
447 		dvb_module_release(state->i2c_client_demod);
448 		return -ENODEV;
449 	}
450 
451 	/* delegate signal strength measurement to tuner */
452 	adap->fe[0]->ops.read_signal_strength =
453 			adap->fe[0]->ops.tuner_ops.get_rf_strength;
454 
455 	/* hook fe: need to resync the slave fifo when signal locks. */
456 	state->fe_read_status = adap->fe[0]->ops.read_status;
457 	adap->fe[0]->ops.read_status = dvbsky_usb_read_status;
458 
459 	/* hook fe: LNB off/on is control by Cypress usb chip. */
460 	state->fe_set_voltage = adap->fe[0]->ops.set_voltage;
461 	adap->fe[0]->ops.set_voltage = dvbsky_usb_ci_set_voltage;
462 
463 	return 0;
464 }
465 
dvbsky_t680c_attach(struct dvb_usb_adapter * adap)466 static int dvbsky_t680c_attach(struct dvb_usb_adapter *adap)
467 {
468 	struct dvbsky_state *state = adap_to_priv(adap);
469 	struct dvb_usb_device *d = adap_to_d(adap);
470 	struct i2c_adapter *i2c_adapter;
471 	struct si2168_config si2168_config = {};
472 	struct si2157_config si2157_config = {};
473 	struct sp2_config sp2_config = {};
474 
475 	/* attach demod */
476 	si2168_config.i2c_adapter = &i2c_adapter;
477 	si2168_config.fe = &adap->fe[0];
478 	si2168_config.ts_mode = SI2168_TS_PARALLEL;
479 
480 	state->i2c_client_demod = dvb_module_probe("si2168", NULL,
481 						   &d->i2c_adap,
482 						   0x64, &si2168_config);
483 	if (!state->i2c_client_demod)
484 		return -ENODEV;
485 
486 	/* attach tuner */
487 	si2157_config.fe = adap->fe[0];
488 	si2157_config.if_port = 1;
489 
490 	state->i2c_client_tuner = dvb_module_probe("si2157", NULL,
491 						   i2c_adapter,
492 						   0x60, &si2157_config);
493 	if (!state->i2c_client_tuner) {
494 		dvb_module_release(state->i2c_client_demod);
495 		return -ENODEV;
496 	}
497 
498 	/* attach ci controller */
499 	sp2_config.dvb_adap = &adap->dvb_adap;
500 	sp2_config.priv = d;
501 	sp2_config.ci_control = dvbsky_ci_ctrl;
502 
503 	state->i2c_client_ci = dvb_module_probe("sp2", NULL,
504 						&d->i2c_adap,
505 						0x40, &sp2_config);
506 
507 	if (!state->i2c_client_ci) {
508 		dvb_module_release(state->i2c_client_tuner);
509 		dvb_module_release(state->i2c_client_demod);
510 		return -ENODEV;
511 	}
512 
513 	return 0;
514 }
515 
dvbsky_t330_attach(struct dvb_usb_adapter * adap)516 static int dvbsky_t330_attach(struct dvb_usb_adapter *adap)
517 {
518 	struct dvbsky_state *state = adap_to_priv(adap);
519 	struct dvb_usb_device *d = adap_to_d(adap);
520 	struct i2c_adapter *i2c_adapter;
521 	struct si2168_config si2168_config = {};
522 	struct si2157_config si2157_config = {};
523 
524 	/* attach demod */
525 	si2168_config.i2c_adapter = &i2c_adapter;
526 	si2168_config.fe = &adap->fe[0];
527 	si2168_config.ts_mode = SI2168_TS_PARALLEL;
528 	si2168_config.ts_clock_gapped = true;
529 
530 	state->i2c_client_demod = dvb_module_probe("si2168", NULL,
531 						   &d->i2c_adap,
532 						   0x64, &si2168_config);
533 	if (!state->i2c_client_demod)
534 		return -ENODEV;
535 
536 	/* attach tuner */
537 	si2157_config.fe = adap->fe[0];
538 	si2157_config.if_port = 1;
539 
540 	state->i2c_client_tuner = dvb_module_probe("si2157", NULL,
541 						   i2c_adapter,
542 						   0x60, &si2157_config);
543 	if (!state->i2c_client_tuner) {
544 		dvb_module_release(state->i2c_client_demod);
545 		return -ENODEV;
546 	}
547 
548 	return 0;
549 }
550 
dvbsky_mygica_t230c_attach(struct dvb_usb_adapter * adap)551 static int dvbsky_mygica_t230c_attach(struct dvb_usb_adapter *adap)
552 {
553 	struct dvbsky_state *state = adap_to_priv(adap);
554 	struct dvb_usb_device *d = adap_to_d(adap);
555 	struct i2c_adapter *i2c_adapter;
556 	struct si2168_config si2168_config = {};
557 	struct si2157_config si2157_config = {};
558 
559 	/* attach demod */
560 	si2168_config.i2c_adapter = &i2c_adapter;
561 	si2168_config.fe = &adap->fe[0];
562 	si2168_config.ts_mode = SI2168_TS_PARALLEL;
563 	si2168_config.ts_clock_inv = 1;
564 
565 	state->i2c_client_demod = dvb_module_probe("si2168", NULL,
566 						   &d->i2c_adap,
567 						   0x64, &si2168_config);
568 	if (!state->i2c_client_demod)
569 		return -ENODEV;
570 
571 	/* attach tuner */
572 	si2157_config.fe = adap->fe[0];
573 	si2157_config.if_port = 0;
574 
575 	state->i2c_client_tuner = dvb_module_probe("si2157", "si2141",
576 						   i2c_adapter,
577 						   0x60, &si2157_config);
578 	if (!state->i2c_client_tuner) {
579 		dvb_module_release(state->i2c_client_demod);
580 		return -ENODEV;
581 	}
582 
583 	return 0;
584 }
585 
586 
dvbsky_identify_state(struct dvb_usb_device * d,const char ** name)587 static int dvbsky_identify_state(struct dvb_usb_device *d, const char **name)
588 {
589 	dvbsky_gpio_ctrl(d, 0x04, 1);
590 	msleep(20);
591 	dvbsky_gpio_ctrl(d, 0x83, 0);
592 	dvbsky_gpio_ctrl(d, 0xc0, 1);
593 	msleep(100);
594 	dvbsky_gpio_ctrl(d, 0x83, 1);
595 	dvbsky_gpio_ctrl(d, 0xc0, 0);
596 	msleep(50);
597 
598 	return WARM;
599 }
600 
dvbsky_init(struct dvb_usb_device * d)601 static int dvbsky_init(struct dvb_usb_device *d)
602 {
603 	struct dvbsky_state *state = d_to_priv(d);
604 
605 	/* use default interface */
606 	/*
607 	ret = usb_set_interface(d->udev, 0, 0);
608 	if (ret)
609 		return ret;
610 	*/
611 	mutex_init(&state->stream_mutex);
612 
613 	state->last_lock = 0;
614 
615 	return 0;
616 }
617 
dvbsky_frontend_detach(struct dvb_usb_adapter * adap)618 static int dvbsky_frontend_detach(struct dvb_usb_adapter *adap)
619 {
620 	struct dvb_usb_device *d = adap_to_d(adap);
621 	struct dvbsky_state *state = d_to_priv(d);
622 
623 	dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, adap->id);
624 
625 	dvb_module_release(state->i2c_client_tuner);
626 	dvb_module_release(state->i2c_client_demod);
627 	dvb_module_release(state->i2c_client_ci);
628 
629 	return 0;
630 }
631 
632 /* DVB USB Driver stuff */
633 static struct dvb_usb_device_properties dvbsky_s960_props = {
634 	.driver_name = KBUILD_MODNAME,
635 	.owner = THIS_MODULE,
636 	.adapter_nr = adapter_nr,
637 	.size_of_priv = sizeof(struct dvbsky_state),
638 
639 	.generic_bulk_ctrl_endpoint = 0x01,
640 	.generic_bulk_ctrl_endpoint_response = 0x81,
641 	.generic_bulk_ctrl_delay = DVBSKY_MSG_DELAY,
642 
643 	.i2c_algo         = &dvbsky_i2c_algo,
644 	.frontend_attach  = dvbsky_s960_attach,
645 	.frontend_detach  = dvbsky_frontend_detach,
646 	.init             = dvbsky_init,
647 	.get_rc_config    = dvbsky_get_rc_config,
648 	.streaming_ctrl   = dvbsky_streaming_ctrl,
649 	.identify_state	  = dvbsky_identify_state,
650 	.read_mac_address = dvbsky_read_mac_addr,
651 
652 	.num_adapters = 1,
653 	.adapter = {
654 		{
655 			.stream = DVB_USB_STREAM_BULK(0x82, 8, 4096),
656 		}
657 	}
658 };
659 
660 static struct dvb_usb_device_properties dvbsky_s960c_props = {
661 	.driver_name = KBUILD_MODNAME,
662 	.owner = THIS_MODULE,
663 	.adapter_nr = adapter_nr,
664 	.size_of_priv = sizeof(struct dvbsky_state),
665 
666 	.generic_bulk_ctrl_endpoint = 0x01,
667 	.generic_bulk_ctrl_endpoint_response = 0x81,
668 	.generic_bulk_ctrl_delay = DVBSKY_MSG_DELAY,
669 
670 	.i2c_algo         = &dvbsky_i2c_algo,
671 	.frontend_attach  = dvbsky_s960c_attach,
672 	.frontend_detach  = dvbsky_frontend_detach,
673 	.init             = dvbsky_init,
674 	.get_rc_config    = dvbsky_get_rc_config,
675 	.streaming_ctrl   = dvbsky_streaming_ctrl,
676 	.identify_state	  = dvbsky_identify_state,
677 	.read_mac_address = dvbsky_read_mac_addr,
678 
679 	.num_adapters = 1,
680 	.adapter = {
681 		{
682 			.stream = DVB_USB_STREAM_BULK(0x82, 8, 4096),
683 		}
684 	}
685 };
686 
687 static struct dvb_usb_device_properties dvbsky_t680c_props = {
688 	.driver_name = KBUILD_MODNAME,
689 	.owner = THIS_MODULE,
690 	.adapter_nr = adapter_nr,
691 	.size_of_priv = sizeof(struct dvbsky_state),
692 
693 	.generic_bulk_ctrl_endpoint = 0x01,
694 	.generic_bulk_ctrl_endpoint_response = 0x81,
695 	.generic_bulk_ctrl_delay = DVBSKY_MSG_DELAY,
696 
697 	.i2c_algo         = &dvbsky_i2c_algo,
698 	.frontend_attach  = dvbsky_t680c_attach,
699 	.frontend_detach  = dvbsky_frontend_detach,
700 	.init             = dvbsky_init,
701 	.get_rc_config    = dvbsky_get_rc_config,
702 	.streaming_ctrl   = dvbsky_streaming_ctrl,
703 	.identify_state	  = dvbsky_identify_state,
704 	.read_mac_address = dvbsky_read_mac_addr,
705 
706 	.num_adapters = 1,
707 	.adapter = {
708 		{
709 			.stream = DVB_USB_STREAM_BULK(0x82, 8, 4096),
710 		}
711 	}
712 };
713 
714 static struct dvb_usb_device_properties dvbsky_t330_props = {
715 	.driver_name = KBUILD_MODNAME,
716 	.owner = THIS_MODULE,
717 	.adapter_nr = adapter_nr,
718 	.size_of_priv = sizeof(struct dvbsky_state),
719 
720 	.generic_bulk_ctrl_endpoint = 0x01,
721 	.generic_bulk_ctrl_endpoint_response = 0x81,
722 	.generic_bulk_ctrl_delay = DVBSKY_MSG_DELAY,
723 
724 	.i2c_algo         = &dvbsky_i2c_algo,
725 	.frontend_attach  = dvbsky_t330_attach,
726 	.frontend_detach  = dvbsky_frontend_detach,
727 	.init             = dvbsky_init,
728 	.get_rc_config    = dvbsky_get_rc_config,
729 	.streaming_ctrl   = dvbsky_streaming_ctrl,
730 	.identify_state	  = dvbsky_identify_state,
731 	.read_mac_address = dvbsky_read_mac_addr,
732 
733 	.num_adapters = 1,
734 	.adapter = {
735 		{
736 			.stream = DVB_USB_STREAM_BULK(0x82, 8, 4096),
737 		}
738 	}
739 };
740 
741 static struct dvb_usb_device_properties mygica_t230c_props = {
742 	.driver_name = KBUILD_MODNAME,
743 	.owner = THIS_MODULE,
744 	.adapter_nr = adapter_nr,
745 	.size_of_priv = sizeof(struct dvbsky_state),
746 
747 	.generic_bulk_ctrl_endpoint = 0x01,
748 	.generic_bulk_ctrl_endpoint_response = 0x81,
749 	.generic_bulk_ctrl_delay = DVBSKY_MSG_DELAY,
750 
751 	.i2c_algo         = &dvbsky_i2c_algo,
752 	.frontend_attach  = dvbsky_mygica_t230c_attach,
753 	.frontend_detach  = dvbsky_frontend_detach,
754 	.init             = dvbsky_init,
755 	.get_rc_config    = dvbsky_get_rc_config,
756 	.streaming_ctrl   = dvbsky_streaming_ctrl,
757 	.identify_state	  = dvbsky_identify_state,
758 
759 	.num_adapters = 1,
760 	.adapter = {
761 		{
762 			.stream = DVB_USB_STREAM_BULK(0x82, 8, 4096),
763 		}
764 	}
765 };
766 
767 static const struct usb_device_id dvbsky_id_table[] = {
768 	{ DVB_USB_DEVICE(0x0572, 0x6831,
769 		&dvbsky_s960_props, "DVBSky S960/S860", RC_MAP_DVBSKY) },
770 	{ DVB_USB_DEVICE(0x0572, 0x960c,
771 		&dvbsky_s960c_props, "DVBSky S960CI", RC_MAP_DVBSKY) },
772 	{ DVB_USB_DEVICE(0x0572, 0x680c,
773 		&dvbsky_t680c_props, "DVBSky T680CI", RC_MAP_DVBSKY) },
774 	{ DVB_USB_DEVICE(0x0572, 0x0320,
775 		&dvbsky_t330_props, "DVBSky T330", RC_MAP_DVBSKY) },
776 	{ DVB_USB_DEVICE(USB_VID_TECHNOTREND,
777 		USB_PID_TECHNOTREND_TVSTICK_CT2_4400,
778 		&dvbsky_t330_props, "TechnoTrend TVStick CT2-4400",
779 		RC_MAP_TT_1500) },
780 	{ DVB_USB_DEVICE(USB_VID_TECHNOTREND,
781 		USB_PID_TECHNOTREND_CONNECT_CT2_4650_CI,
782 		&dvbsky_t680c_props, "TechnoTrend TT-connect CT2-4650 CI",
783 		RC_MAP_TT_1500) },
784 	{ DVB_USB_DEVICE(USB_VID_TECHNOTREND,
785 		USB_PID_TECHNOTREND_CONNECT_CT2_4650_CI_2,
786 		&dvbsky_t680c_props, "TechnoTrend TT-connect CT2-4650 CI v1.1",
787 		RC_MAP_TT_1500) },
788 	{ DVB_USB_DEVICE(USB_VID_TECHNOTREND,
789 		USB_PID_TECHNOTREND_CONNECT_S2_4650_CI,
790 		&dvbsky_s960c_props, "TechnoTrend TT-connect S2-4650 CI",
791 		RC_MAP_TT_1500) },
792 	{ DVB_USB_DEVICE(USB_VID_TERRATEC,
793 		USB_PID_TERRATEC_H7_3,
794 		&dvbsky_t680c_props, "Terratec H7 Rev.4",
795 		RC_MAP_TT_1500) },
796 	{ DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_S2_R4,
797 		&dvbsky_s960_props, "Terratec Cinergy S2 Rev.4",
798 		RC_MAP_DVBSKY) },
799 	{ DVB_USB_DEVICE(USB_VID_CONEXANT, USB_PID_MYGICA_T230C,
800 		&mygica_t230c_props, "MyGica Mini DVB-T2 USB Stick T230C",
801 		RC_MAP_TOTAL_MEDIA_IN_HAND_02) },
802 	{ }
803 };
804 MODULE_DEVICE_TABLE(usb, dvbsky_id_table);
805 
806 static struct usb_driver dvbsky_usb_driver = {
807 	.name = KBUILD_MODNAME,
808 	.id_table = dvbsky_id_table,
809 	.probe = dvb_usbv2_probe,
810 	.disconnect = dvb_usbv2_disconnect,
811 	.suspend = dvb_usbv2_suspend,
812 	.resume = dvb_usbv2_resume,
813 	.reset_resume = dvb_usbv2_reset_resume,
814 	.no_dynamic_id = 1,
815 	.soft_unbind = 1,
816 };
817 
818 module_usb_driver(dvbsky_usb_driver);
819 
820 MODULE_AUTHOR("Max nibble <nibble.max@gmail.com>");
821 MODULE_DESCRIPTION("Driver for DVBSky USB");
822 MODULE_LICENSE("GPL");
823