1 // SPDX-License-Identifier: GPL-2.0-only
2 /* DVB USB compliant linux driver for
3  *
4  * DM04/QQBOX DVB-S USB BOX	LME2510C + SHARP:BS2F7HZ7395
5  *				LME2510C + LG TDQY-P001F
6  *				LME2510C + BS2F7HZ0194
7  *				LME2510 + LG TDQY-P001F
8  *				LME2510 + BS2F7HZ0194
9  *
10  * MVB7395 (LME2510C+SHARP:BS2F7HZ7395)
11  * SHARP:BS2F7HZ7395 = (STV0288+Sharp IX2505V)
12  *
13  * MV001F (LME2510+LGTDQY-P001F)
14  * LG TDQY - P001F =(TDA8263 + TDA10086H)
15  *
16  * MVB0001F (LME2510C+LGTDQT-P001F)
17  *
18  * MV0194 (LME2510+SHARP:BS2F7HZ0194)
19  * SHARP:BS2F7HZ0194 = (STV0299+IX2410)
20  *
21  * MVB0194 (LME2510C+SHARP0194)
22  *
23  * LME2510C + M88RS2000
24  *
25  * For firmware see Documentation/admin-guide/media/lmedm04.rst
26  *
27  * I2C addresses:
28  * 0xd0 - STV0288	- Demodulator
29  * 0xc0 - Sharp IX2505V	- Tuner
30  * --
31  * 0x1c - TDA10086   - Demodulator
32  * 0xc0 - TDA8263    - Tuner
33  * --
34  * 0xd0 - STV0299	- Demodulator
35  * 0xc0 - IX2410	- Tuner
36  *
37  * VID = 3344  PID LME2510=1122 LME2510C=1120
38  *
39  * Copyright (C) 2010 Malcolm Priestley (tvboxspy@gmail.com)
40  * LME2510(C)(C) Leaguerme (Shenzhen) MicroElectronics Co., Ltd.
41  *
42  * see Documentation/driver-api/media/drivers/dvb-usb.rst for more information
43  *
44  * Known Issues :
45  *	LME2510: Non Intel USB chipsets fail to maintain High Speed on
46  * Boot or Hot Plug.
47  *
48  * QQbox suffers from noise on LNB voltage.
49  *
50  *	LME2510: SHARP:BS2F7HZ0194(MV0194) cannot cold reset and share system
51  * with other tuners. After a cold reset streaming will not start.
52  *
53  * M88RS2000 suffers from loss of lock.
54  */
55 #define DVB_USB_LOG_PREFIX "LME2510(C)"
56 #include <linux/usb.h>
57 #include <linux/usb/input.h>
58 #include <media/rc-core.h>
59 
60 #include "dvb_usb.h"
61 #include "lmedm04.h"
62 #include "tda826x.h"
63 #include "tda10086.h"
64 #include "stv0288.h"
65 #include "ix2505v.h"
66 #include "stv0299.h"
67 #include "dvb-pll.h"
68 #include "z0194a.h"
69 #include "m88rs2000.h"
70 #include "ts2020.h"
71 
72 
73 #define LME2510_C_S7395	"dvb-usb-lme2510c-s7395.fw";
74 #define LME2510_C_LG	"dvb-usb-lme2510c-lg.fw";
75 #define LME2510_C_S0194	"dvb-usb-lme2510c-s0194.fw";
76 #define LME2510_C_RS2000 "dvb-usb-lme2510c-rs2000.fw";
77 #define LME2510_LG	"dvb-usb-lme2510-lg.fw";
78 #define LME2510_S0194	"dvb-usb-lme2510-s0194.fw";
79 
80 /* debug */
81 static int dvb_usb_lme2510_debug;
82 #define lme_debug(var, level, args...) do { \
83 	if ((var >= level)) \
84 		pr_debug(DVB_USB_LOG_PREFIX": " args); \
85 } while (0)
86 #define deb_info(level, args...) lme_debug(dvb_usb_lme2510_debug, level, args)
87 #define debug_data_snipet(level, name, p) \
88 	 deb_info(level, name" (%8phN)", p);
89 #define info(args...) pr_info(DVB_USB_LOG_PREFIX": "args)
90 
91 module_param_named(debug, dvb_usb_lme2510_debug, int, 0644);
92 MODULE_PARM_DESC(debug, "set debugging level (1=info (or-able)).");
93 
94 static int dvb_usb_lme2510_firmware;
95 module_param_named(firmware, dvb_usb_lme2510_firmware, int, 0644);
96 MODULE_PARM_DESC(firmware, "set default firmware 0=Sharp7395 1=LG");
97 
98 static int pid_filter;
99 module_param_named(pid, pid_filter, int, 0644);
100 MODULE_PARM_DESC(pid, "set default 0=default 1=off 2=on");
101 
102 
103 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
104 
105 #define TUNER_DEFAULT	0x0
106 #define TUNER_LG	0x1
107 #define TUNER_S7395	0x2
108 #define TUNER_S0194	0x3
109 #define TUNER_RS2000	0x4
110 
111 struct lme2510_state {
112 	unsigned long int_urb_due;
113 	enum fe_status lock_status;
114 	u8 id;
115 	u8 tuner_config;
116 	u8 signal_level;
117 	u8 signal_sn;
118 	u8 time_key;
119 	u8 i2c_talk_onoff;
120 	u8 i2c_gate;
121 	u8 i2c_tuner_gate_w;
122 	u8 i2c_tuner_gate_r;
123 	u8 i2c_tuner_addr;
124 	u8 stream_on;
125 	u8 pid_size;
126 	u8 pid_off;
127 	u8 int_buffer[128];
128 	struct urb *lme_urb;
129 	u8 usb_buffer[64];
130 	/* Frontend original calls */
131 	int (*fe_read_status)(struct dvb_frontend *, enum fe_status *);
132 	int (*fe_read_signal_strength)(struct dvb_frontend *, u16 *);
133 	int (*fe_read_snr)(struct dvb_frontend *, u16 *);
134 	int (*fe_read_ber)(struct dvb_frontend *, u32 *);
135 	int (*fe_read_ucblocks)(struct dvb_frontend *, u32 *);
136 	int (*fe_set_voltage)(struct dvb_frontend *, enum fe_sec_voltage);
137 	u8 dvb_usb_lme2510_firmware;
138 };
139 
140 static int lme2510_usb_talk(struct dvb_usb_device *d,
141 			    u8 *wbuf, int wlen, u8 *rbuf, int rlen)
142 {
143 	struct lme2510_state *st = d->priv;
144 	int ret = 0;
145 
146 	if (max(wlen, rlen) > sizeof(st->usb_buffer))
147 		return -EINVAL;
148 
149 	ret = mutex_lock_interruptible(&d->usb_mutex);
150 	if (ret < 0)
151 		return -EAGAIN;
152 
153 	memcpy(st->usb_buffer, wbuf, wlen);
154 
155 	ret = dvb_usbv2_generic_rw_locked(d, st->usb_buffer, wlen,
156 					  st->usb_buffer, rlen);
157 
158 	if (rlen)
159 		memcpy(rbuf, st->usb_buffer, rlen);
160 
161 	mutex_unlock(&d->usb_mutex);
162 
163 	return ret;
164 }
165 
166 static int lme2510_stream_restart(struct dvb_usb_device *d)
167 {
168 	struct lme2510_state *st = d->priv;
169 	u8 all_pids[] = LME_ALL_PIDS;
170 	u8 stream_on[] = LME_ST_ON_W;
171 	u8 rbuff[1];
172 	if (st->pid_off)
173 		lme2510_usb_talk(d, all_pids, sizeof(all_pids),
174 				 rbuff, sizeof(rbuff));
175 	/*Restart Stream Command*/
176 	return lme2510_usb_talk(d, stream_on, sizeof(stream_on),
177 				rbuff, sizeof(rbuff));
178 }
179 
180 static int lme2510_enable_pid(struct dvb_usb_device *d, u8 index, u16 pid_out)
181 {
182 	struct lme2510_state *st = d->priv;
183 	static u8 pid_buff[] = LME_ZERO_PID;
184 	static u8 rbuf[1];
185 	u8 pid_no = index * 2;
186 	u8 pid_len = pid_no + 2;
187 	int ret = 0;
188 	deb_info(1, "PID Setting Pid %04x", pid_out);
189 
190 	if (st->pid_size == 0)
191 		ret |= lme2510_stream_restart(d);
192 
193 	pid_buff[2] = pid_no;
194 	pid_buff[3] = (u8)pid_out & 0xff;
195 	pid_buff[4] = pid_no + 1;
196 	pid_buff[5] = (u8)(pid_out >> 8);
197 
198 	if (pid_len > st->pid_size)
199 		st->pid_size = pid_len;
200 	pid_buff[7] = 0x80 + st->pid_size;
201 
202 	ret |= lme2510_usb_talk(d, pid_buff ,
203 		sizeof(pid_buff) , rbuf, sizeof(rbuf));
204 
205 	if (st->stream_on)
206 		ret |= lme2510_stream_restart(d);
207 
208 	return ret;
209 }
210 
211 /* Convert range from 0x00-0xff to 0x0000-0xffff */
212 #define reg_to_16bits(x)	((x) | ((x) << 8))
213 
214 static void lme2510_update_stats(struct dvb_usb_adapter *adap)
215 {
216 	struct lme2510_state *st = adap_to_priv(adap);
217 	struct dvb_frontend *fe = adap->fe[0];
218 	struct dtv_frontend_properties *c;
219 	u32 s_tmp = 0, c_tmp = 0;
220 
221 	if (!fe)
222 		return;
223 
224 	c = &fe->dtv_property_cache;
225 
226 	c->block_count.len = 1;
227 	c->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
228 	c->block_error.len = 1;
229 	c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
230 	c->post_bit_count.len = 1;
231 	c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
232 	c->post_bit_error.len = 1;
233 	c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
234 
235 	if (st->i2c_talk_onoff) {
236 		c->strength.len = 1;
237 		c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
238 		c->cnr.len = 1;
239 		c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
240 		return;
241 	}
242 
243 	switch (st->tuner_config) {
244 	case TUNER_LG:
245 		s_tmp = reg_to_16bits(0xff - st->signal_level);
246 		c_tmp = reg_to_16bits(0xff - st->signal_sn);
247 		break;
248 	case TUNER_S7395:
249 	case TUNER_S0194:
250 		s_tmp = 0xffff - (((st->signal_level * 2) << 8) * 5 / 4);
251 		c_tmp = reg_to_16bits((0xff - st->signal_sn - 0xa1) * 3);
252 		break;
253 	case TUNER_RS2000:
254 		s_tmp = reg_to_16bits(st->signal_level);
255 		c_tmp = reg_to_16bits(st->signal_sn);
256 	}
257 
258 	c->strength.len = 1;
259 	c->strength.stat[0].scale = FE_SCALE_RELATIVE;
260 	c->strength.stat[0].uvalue = (u64)s_tmp;
261 
262 	c->cnr.len = 1;
263 	c->cnr.stat[0].scale = FE_SCALE_RELATIVE;
264 	c->cnr.stat[0].uvalue = (u64)c_tmp;
265 }
266 
267 static void lme2510_int_response(struct urb *lme_urb)
268 {
269 	struct dvb_usb_adapter *adap = lme_urb->context;
270 	struct lme2510_state *st = adap_to_priv(adap);
271 	u8 *ibuf, *rbuf;
272 	int i = 0, offset;
273 	u32 key;
274 	u8 signal_lock = 0;
275 
276 	switch (lme_urb->status) {
277 	case 0:
278 	case -ETIMEDOUT:
279 		break;
280 	case -ECONNRESET:
281 	case -ENOENT:
282 	case -ESHUTDOWN:
283 		return;
284 	default:
285 		info("Error %x", lme_urb->status);
286 		break;
287 	}
288 
289 	rbuf = (u8 *) lme_urb->transfer_buffer;
290 
291 	offset = ((lme_urb->actual_length/8) > 4)
292 			? 4 : (lme_urb->actual_length/8) ;
293 
294 	for (i = 0; i < offset; ++i) {
295 		ibuf = (u8 *)&rbuf[i*8];
296 		deb_info(5, "INT O/S C =%02x C/O=%02x Type =%02x%02x",
297 		offset, i, ibuf[0], ibuf[1]);
298 
299 		switch (ibuf[0]) {
300 		case 0xaa:
301 			debug_data_snipet(1, "INT Remote data snippet", ibuf);
302 			if (!adap_to_d(adap)->rc_dev)
303 				break;
304 
305 			key = RC_SCANCODE_NEC32(ibuf[2] << 24 |
306 						ibuf[3] << 16 |
307 						ibuf[4] << 8  |
308 						ibuf[5]);
309 
310 			deb_info(1, "INT Key = 0x%08x", key);
311 			rc_keydown(adap_to_d(adap)->rc_dev, RC_PROTO_NEC32, key,
312 				   0);
313 			break;
314 		case 0xbb:
315 			switch (st->tuner_config) {
316 			case TUNER_LG:
317 				signal_lock = ibuf[2] & BIT(5);
318 				st->signal_level = ibuf[4];
319 				st->signal_sn = ibuf[3];
320 				st->time_key = ibuf[7];
321 				break;
322 			case TUNER_S7395:
323 			case TUNER_S0194:
324 				/* Tweak for earlier firmware*/
325 				if (ibuf[1] == 0x03) {
326 					signal_lock = ibuf[2] & BIT(4);
327 					st->signal_level = ibuf[3];
328 					st->signal_sn = ibuf[4];
329 				} else {
330 					st->signal_level = ibuf[4];
331 					st->signal_sn = ibuf[5];
332 				}
333 				break;
334 			case TUNER_RS2000:
335 				signal_lock = ibuf[2] & 0xee;
336 				st->signal_level = ibuf[5];
337 				st->signal_sn = ibuf[4];
338 				st->time_key = ibuf[7];
339 				break;
340 			default:
341 				break;
342 			}
343 
344 			/* Interrupt will also throw just BIT 0 as lock */
345 			signal_lock |= ibuf[2] & BIT(0);
346 
347 			if (!signal_lock)
348 				st->lock_status &= ~FE_HAS_LOCK;
349 
350 			lme2510_update_stats(adap);
351 
352 			debug_data_snipet(5, "INT Remote data snippet in", ibuf);
353 		break;
354 		case 0xcc:
355 			debug_data_snipet(1, "INT Control data snippet", ibuf);
356 			break;
357 		default:
358 			debug_data_snipet(1, "INT Unknown data snippet", ibuf);
359 		break;
360 		}
361 	}
362 
363 	usb_submit_urb(lme_urb, GFP_ATOMIC);
364 
365 	/* Interrupt urb is due every 48 msecs while streaming the buffer
366 	 * stores up to 4 periods if missed. Allow 200 msec for next interrupt.
367 	 */
368 	st->int_urb_due = jiffies + msecs_to_jiffies(200);
369 }
370 
371 static int lme2510_int_read(struct dvb_usb_adapter *adap)
372 {
373 	struct dvb_usb_device *d = adap_to_d(adap);
374 	struct lme2510_state *lme_int = adap_to_priv(adap);
375 	struct usb_host_endpoint *ep;
376 
377 	lme_int->lme_urb = usb_alloc_urb(0, GFP_KERNEL);
378 
379 	if (lme_int->lme_urb == NULL)
380 			return -ENOMEM;
381 
382 	usb_fill_int_urb(lme_int->lme_urb,
383 			 d->udev,
384 			 usb_rcvintpipe(d->udev, 0xa),
385 			 lme_int->int_buffer,
386 			 sizeof(lme_int->int_buffer),
387 			 lme2510_int_response,
388 			 adap,
389 			 8);
390 
391 	/* Quirk of pipe reporting PIPE_BULK but behaves as interrupt */
392 	ep = usb_pipe_endpoint(d->udev, lme_int->lme_urb->pipe);
393 
394 	if (usb_endpoint_type(&ep->desc) == USB_ENDPOINT_XFER_BULK)
395 		lme_int->lme_urb->pipe = usb_rcvbulkpipe(d->udev, 0xa);
396 
397 	usb_submit_urb(lme_int->lme_urb, GFP_KERNEL);
398 	info("INT Interrupt Service Started");
399 
400 	return 0;
401 }
402 
403 static int lme2510_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
404 {
405 	struct dvb_usb_device *d = adap_to_d(adap);
406 	struct lme2510_state *st = adap_to_priv(adap);
407 	static u8 clear_pid_reg[] = LME_ALL_PIDS;
408 	static u8 rbuf[1];
409 	int ret = 0;
410 
411 	deb_info(1, "PID Clearing Filter");
412 
413 	mutex_lock(&d->i2c_mutex);
414 
415 	if (!onoff) {
416 		ret |= lme2510_usb_talk(d, clear_pid_reg,
417 			sizeof(clear_pid_reg), rbuf, sizeof(rbuf));
418 		st->pid_off = true;
419 	} else
420 		st->pid_off = false;
421 
422 	st->pid_size = 0;
423 
424 	mutex_unlock(&d->i2c_mutex);
425 
426 	return 0;
427 }
428 
429 static int lme2510_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
430 	int onoff)
431 {
432 	struct dvb_usb_device *d = adap_to_d(adap);
433 	int ret = 0;
434 
435 	deb_info(3, "%s PID=%04x Index=%04x onoff=%02x", __func__,
436 		pid, index, onoff);
437 
438 	if (onoff) {
439 		mutex_lock(&d->i2c_mutex);
440 		ret |= lme2510_enable_pid(d, index, pid);
441 		mutex_unlock(&d->i2c_mutex);
442 	}
443 
444 
445 	return ret;
446 }
447 
448 
449 static int lme2510_return_status(struct dvb_usb_device *d)
450 {
451 	int ret;
452 	u8 *data;
453 
454 	data = kzalloc(6, GFP_KERNEL);
455 	if (!data)
456 		return -ENOMEM;
457 
458 	ret = usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0),
459 			      0x06, 0x80, 0x0302, 0x00,
460 			      data, 0x6, 200);
461 	if (ret != 6)
462 		ret = -EINVAL;
463 	else
464 		ret = data[2];
465 
466 	info("Firmware Status: %6ph", data);
467 
468 	kfree(data);
469 	return ret;
470 }
471 
472 static int lme2510_msg(struct dvb_usb_device *d,
473 		u8 *wbuf, int wlen, u8 *rbuf, int rlen)
474 {
475 	struct lme2510_state *st = d->priv;
476 
477 	st->i2c_talk_onoff = 1;
478 
479 	return lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
480 }
481 
482 static int lme2510_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
483 				 int num)
484 {
485 	struct dvb_usb_device *d = i2c_get_adapdata(adap);
486 	struct lme2510_state *st = d->priv;
487 	static u8 obuf[64], ibuf[64];
488 	int i, read, read_o;
489 	u16 len;
490 	u8 gate;
491 
492 	mutex_lock(&d->i2c_mutex);
493 
494 	for (i = 0; i < num; i++) {
495 		read_o = msg[i].flags & I2C_M_RD;
496 		read = i + 1 < num && msg[i + 1].flags & I2C_M_RD;
497 		read |= read_o;
498 		gate = (msg[i].addr == st->i2c_tuner_addr)
499 			? (read)	? st->i2c_tuner_gate_r
500 					: st->i2c_tuner_gate_w
501 			: st->i2c_gate;
502 		obuf[0] = gate | (read << 7);
503 
504 		if (gate == 5)
505 			obuf[1] = (read) ? 2 : msg[i].len + 1;
506 		else
507 			obuf[1] = msg[i].len + read + 1;
508 
509 		obuf[2] = msg[i].addr << 1;
510 
511 		if (read) {
512 			if (read_o)
513 				len = 3;
514 			else {
515 				memcpy(&obuf[3], msg[i].buf, msg[i].len);
516 				obuf[msg[i].len+3] = msg[i+1].len;
517 				len = msg[i].len+4;
518 			}
519 		} else {
520 			memcpy(&obuf[3], msg[i].buf, msg[i].len);
521 			len = msg[i].len+3;
522 		}
523 
524 		if (lme2510_msg(d, obuf, len, ibuf, 64) < 0) {
525 			deb_info(1, "i2c transfer failed.");
526 			mutex_unlock(&d->i2c_mutex);
527 			return -EAGAIN;
528 		}
529 
530 		if (read) {
531 			if (read_o)
532 				memcpy(msg[i].buf, &ibuf[1], msg[i].len);
533 			else {
534 				memcpy(msg[i+1].buf, &ibuf[1], msg[i+1].len);
535 				i++;
536 			}
537 		}
538 	}
539 
540 	mutex_unlock(&d->i2c_mutex);
541 	return i;
542 }
543 
544 static u32 lme2510_i2c_func(struct i2c_adapter *adapter)
545 {
546 	return I2C_FUNC_I2C;
547 }
548 
549 static struct i2c_algorithm lme2510_i2c_algo = {
550 	.master_xfer   = lme2510_i2c_xfer,
551 	.functionality = lme2510_i2c_func,
552 };
553 
554 static int lme2510_streaming_ctrl(struct dvb_frontend *fe, int onoff)
555 {
556 	struct dvb_usb_adapter *adap = fe_to_adap(fe);
557 	struct dvb_usb_device *d = adap_to_d(adap);
558 	struct lme2510_state *st = adap_to_priv(adap);
559 	static u8 clear_reg_3[] = LME_ALL_PIDS;
560 	static u8 rbuf[1];
561 	int ret = 0, rlen = sizeof(rbuf);
562 
563 	deb_info(1, "STM  (%02x)", onoff);
564 
565 	/* Streaming is started by FE_HAS_LOCK */
566 	if (onoff == 1)
567 		st->stream_on = 1;
568 	else {
569 		deb_info(1, "STM Steam Off");
570 		/* mutex is here only to avoid collision with I2C */
571 		mutex_lock(&d->i2c_mutex);
572 
573 		ret = lme2510_usb_talk(d, clear_reg_3,
574 				sizeof(clear_reg_3), rbuf, rlen);
575 		st->stream_on = 0;
576 		st->i2c_talk_onoff = 1;
577 
578 		mutex_unlock(&d->i2c_mutex);
579 	}
580 
581 	return (ret < 0) ? -ENODEV : 0;
582 }
583 
584 static u8 check_sum(u8 *p, u8 len)
585 {
586 	u8 sum = 0;
587 	while (len--)
588 		sum += *p++;
589 	return sum;
590 }
591 
592 static int lme2510_download_firmware(struct dvb_usb_device *d,
593 					const struct firmware *fw)
594 {
595 	int ret = 0;
596 	u8 *data;
597 	u16 j, wlen, len_in, start, end;
598 	u8 packet_size, dlen, i;
599 	u8 *fw_data;
600 
601 	packet_size = 0x31;
602 	len_in = 1;
603 
604 	data = kzalloc(128, GFP_KERNEL);
605 	if (!data) {
606 		info("FRM Could not start Firmware Download"\
607 			"(Buffer allocation failed)");
608 		return -ENOMEM;
609 	}
610 
611 	info("FRM Starting Firmware Download");
612 
613 	for (i = 1; i < 3; i++) {
614 		start = (i == 1) ? 0 : 512;
615 		end = (i == 1) ? 512 : fw->size;
616 		for (j = start; j < end; j += (packet_size+1)) {
617 			fw_data = (u8 *)(fw->data + j);
618 			if ((end - j) > packet_size) {
619 				data[0] = i;
620 				dlen = packet_size;
621 			} else {
622 				data[0] = i | 0x80;
623 				dlen = (u8)(end - j)-1;
624 			}
625 			data[1] = dlen;
626 			memcpy(&data[2], fw_data, dlen+1);
627 			wlen = (u8) dlen + 4;
628 			data[wlen-1] = check_sum(fw_data, dlen+1);
629 			deb_info(1, "Data S=%02x:E=%02x CS= %02x", data[3],
630 				data[dlen+2], data[dlen+3]);
631 			lme2510_usb_talk(d, data, wlen, data, len_in);
632 			ret |= (data[0] == 0x88) ? 0 : -1;
633 		}
634 	}
635 
636 	data[0] = 0x8a;
637 	len_in = 1;
638 	msleep(2000);
639 	lme2510_usb_talk(d, data, len_in, data, len_in);
640 	msleep(400);
641 
642 	if (ret < 0)
643 		info("FRM Firmware Download Failed (%04x)" , ret);
644 	else
645 		info("FRM Firmware Download Completed - Resetting Device");
646 
647 	kfree(data);
648 	return RECONNECTS_USB;
649 }
650 
651 static void lme_coldreset(struct dvb_usb_device *d)
652 {
653 	u8 data[1] = {0};
654 	data[0] = 0x0a;
655 	info("FRM Firmware Cold Reset");
656 
657 	lme2510_usb_talk(d, data, sizeof(data), data, sizeof(data));
658 
659 	return;
660 }
661 
662 static const char fw_c_s7395[] = LME2510_C_S7395;
663 static const char fw_c_lg[] = LME2510_C_LG;
664 static const char fw_c_s0194[] = LME2510_C_S0194;
665 static const char fw_c_rs2000[] = LME2510_C_RS2000;
666 static const char fw_lg[] = LME2510_LG;
667 static const char fw_s0194[] = LME2510_S0194;
668 
669 static const char *lme_firmware_switch(struct dvb_usb_device *d, int cold)
670 {
671 	struct lme2510_state *st = d->priv;
672 	struct usb_device *udev = d->udev;
673 	const struct firmware *fw = NULL;
674 	const char *fw_lme;
675 	int ret = 0;
676 
677 	cold = (cold > 0) ? (cold & 1) : 0;
678 
679 	switch (le16_to_cpu(udev->descriptor.idProduct)) {
680 	case 0x1122:
681 		switch (st->dvb_usb_lme2510_firmware) {
682 		default:
683 		case TUNER_S0194:
684 			fw_lme = fw_s0194;
685 			ret = request_firmware(&fw, fw_lme, &udev->dev);
686 			if (ret == 0) {
687 				st->dvb_usb_lme2510_firmware = TUNER_S0194;
688 				cold = 0;
689 				break;
690 			}
691 			fallthrough;
692 		case TUNER_LG:
693 			fw_lme = fw_lg;
694 			ret = request_firmware(&fw, fw_lme, &udev->dev);
695 			if (ret == 0) {
696 				st->dvb_usb_lme2510_firmware = TUNER_LG;
697 				break;
698 			}
699 			st->dvb_usb_lme2510_firmware = TUNER_DEFAULT;
700 			break;
701 		}
702 		break;
703 	case 0x1120:
704 		switch (st->dvb_usb_lme2510_firmware) {
705 		default:
706 		case TUNER_S7395:
707 			fw_lme = fw_c_s7395;
708 			ret = request_firmware(&fw, fw_lme, &udev->dev);
709 			if (ret == 0) {
710 				st->dvb_usb_lme2510_firmware = TUNER_S7395;
711 				cold = 0;
712 				break;
713 			}
714 			fallthrough;
715 		case TUNER_LG:
716 			fw_lme = fw_c_lg;
717 			ret = request_firmware(&fw, fw_lme, &udev->dev);
718 			if (ret == 0) {
719 				st->dvb_usb_lme2510_firmware = TUNER_LG;
720 				break;
721 			}
722 			fallthrough;
723 		case TUNER_S0194:
724 			fw_lme = fw_c_s0194;
725 			ret = request_firmware(&fw, fw_lme, &udev->dev);
726 			if (ret == 0) {
727 				st->dvb_usb_lme2510_firmware = TUNER_S0194;
728 				break;
729 			}
730 			st->dvb_usb_lme2510_firmware = TUNER_DEFAULT;
731 			cold = 0;
732 			break;
733 		}
734 		break;
735 	case 0x22f0:
736 		fw_lme = fw_c_rs2000;
737 		st->dvb_usb_lme2510_firmware = TUNER_RS2000;
738 		break;
739 	default:
740 		fw_lme = fw_c_s7395;
741 	}
742 
743 	release_firmware(fw);
744 
745 	if (cold) {
746 		dvb_usb_lme2510_firmware = st->dvb_usb_lme2510_firmware;
747 		info("FRM Changing to %s firmware", fw_lme);
748 		lme_coldreset(d);
749 		return NULL;
750 	}
751 
752 	return fw_lme;
753 }
754 
755 static struct tda10086_config tda10086_config = {
756 	.demod_address = 0x0e,
757 	.invert = 0,
758 	.diseqc_tone = 1,
759 	.xtal_freq = TDA10086_XTAL_16M,
760 };
761 
762 static struct stv0288_config lme_config = {
763 	.demod_address = 0x68,
764 	.min_delay_ms = 15,
765 	.inittab = s7395_inittab,
766 };
767 
768 static struct ix2505v_config lme_tuner = {
769 	.tuner_address = 0x60,
770 	.min_delay_ms = 100,
771 	.tuner_gain = 0x0,
772 	.tuner_chargepump = 0x3,
773 };
774 
775 static struct stv0299_config sharp_z0194_config = {
776 	.demod_address = 0x68,
777 	.inittab = sharp_z0194a_inittab,
778 	.mclk = 88000000UL,
779 	.invert = 0,
780 	.skip_reinit = 0,
781 	.lock_output = STV0299_LOCKOUTPUT_1,
782 	.volt13_op0_op1 = STV0299_VOLT13_OP1,
783 	.min_delay_ms = 100,
784 	.set_symbol_rate = sharp_z0194a_set_symbol_rate,
785 };
786 
787 static struct m88rs2000_config m88rs2000_config = {
788 	.demod_addr = 0x68
789 };
790 
791 static struct ts2020_config ts2020_config = {
792 	.tuner_address = 0x60,
793 	.clk_out_div = 7,
794 	.dont_poll = true
795 };
796 
797 static int dm04_lme2510_set_voltage(struct dvb_frontend *fe,
798 				    enum fe_sec_voltage voltage)
799 {
800 	struct dvb_usb_device *d = fe_to_d(fe);
801 	struct lme2510_state *st = fe_to_priv(fe);
802 	static u8 voltage_low[] = LME_VOLTAGE_L;
803 	static u8 voltage_high[] = LME_VOLTAGE_H;
804 	static u8 rbuf[1];
805 	int ret = 0, len = 3, rlen = 1;
806 
807 	mutex_lock(&d->i2c_mutex);
808 
809 	switch (voltage) {
810 	case SEC_VOLTAGE_18:
811 		ret |= lme2510_usb_talk(d,
812 			voltage_high, len, rbuf, rlen);
813 		break;
814 
815 	case SEC_VOLTAGE_OFF:
816 	case SEC_VOLTAGE_13:
817 	default:
818 		ret |= lme2510_usb_talk(d,
819 				voltage_low, len, rbuf, rlen);
820 		break;
821 	}
822 
823 	mutex_unlock(&d->i2c_mutex);
824 
825 	if (st->tuner_config == TUNER_RS2000)
826 		if (st->fe_set_voltage)
827 			st->fe_set_voltage(fe, voltage);
828 
829 
830 	return (ret < 0) ? -ENODEV : 0;
831 }
832 
833 static int dm04_read_status(struct dvb_frontend *fe, enum fe_status *status)
834 {
835 	struct dvb_usb_device *d = fe_to_d(fe);
836 	struct lme2510_state *st = d->priv;
837 	int ret = 0;
838 
839 	if (st->i2c_talk_onoff) {
840 		if (st->fe_read_status) {
841 			ret = st->fe_read_status(fe, status);
842 			if (ret < 0)
843 				return ret;
844 		}
845 
846 		st->lock_status = *status;
847 
848 		if (*status & FE_HAS_LOCK && st->stream_on) {
849 			mutex_lock(&d->i2c_mutex);
850 
851 			st->i2c_talk_onoff = 0;
852 			ret = lme2510_stream_restart(d);
853 
854 			mutex_unlock(&d->i2c_mutex);
855 		}
856 
857 		return ret;
858 	}
859 
860 	/* Timeout of interrupt reached on RS2000 */
861 	if (st->tuner_config == TUNER_RS2000 &&
862 	    time_after(jiffies, st->int_urb_due))
863 		st->lock_status &= ~FE_HAS_LOCK;
864 
865 	*status = st->lock_status;
866 
867 	if (!(*status & FE_HAS_LOCK)) {
868 		struct dvb_usb_adapter *adap = fe_to_adap(fe);
869 
870 		st->i2c_talk_onoff = 1;
871 
872 		lme2510_update_stats(adap);
873 	}
874 
875 	return ret;
876 }
877 
878 static int dm04_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
879 {
880 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
881 	struct lme2510_state *st = fe_to_priv(fe);
882 
883 	if (st->fe_read_signal_strength && !st->stream_on)
884 		return st->fe_read_signal_strength(fe, strength);
885 
886 	if (c->strength.stat[0].scale == FE_SCALE_RELATIVE)
887 		*strength = (u16)c->strength.stat[0].uvalue;
888 	else
889 		*strength = 0;
890 
891 	return 0;
892 }
893 
894 static int dm04_read_snr(struct dvb_frontend *fe, u16 *snr)
895 {
896 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
897 	struct lme2510_state *st = fe_to_priv(fe);
898 
899 	if (st->fe_read_snr && !st->stream_on)
900 		return st->fe_read_snr(fe, snr);
901 
902 	if (c->cnr.stat[0].scale == FE_SCALE_RELATIVE)
903 		*snr = (u16)c->cnr.stat[0].uvalue;
904 	else
905 		*snr = 0;
906 
907 	return 0;
908 }
909 
910 static int dm04_read_ber(struct dvb_frontend *fe, u32 *ber)
911 {
912 	struct lme2510_state *st = fe_to_priv(fe);
913 
914 	if (st->fe_read_ber && !st->stream_on)
915 		return st->fe_read_ber(fe, ber);
916 
917 	*ber = 0;
918 
919 	return 0;
920 }
921 
922 static int dm04_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
923 {
924 	struct lme2510_state *st = fe_to_priv(fe);
925 
926 	if (st->fe_read_ucblocks && !st->stream_on)
927 		return st->fe_read_ucblocks(fe, ucblocks);
928 
929 	*ucblocks = 0;
930 
931 	return 0;
932 }
933 
934 static int lme_name(struct dvb_usb_adapter *adap)
935 {
936 	struct dvb_usb_device *d = adap_to_d(adap);
937 	struct lme2510_state *st = adap_to_priv(adap);
938 	const char *desc = d->name;
939 	static const char * const fe_name[] = {
940 		"", " LG TDQY-P001F", " SHARP:BS2F7HZ7395",
941 		" SHARP:BS2F7HZ0194", " RS2000"};
942 	char *name = adap->fe[0]->ops.info.name;
943 
944 	strscpy(name, desc, 128);
945 	strlcat(name, fe_name[st->tuner_config], 128);
946 
947 	return 0;
948 }
949 
950 static int dm04_lme2510_frontend_attach(struct dvb_usb_adapter *adap)
951 {
952 	struct dvb_usb_device *d = adap_to_d(adap);
953 	struct lme2510_state *st = d->priv;
954 	int ret = 0;
955 
956 	st->i2c_talk_onoff = 1;
957 	switch (le16_to_cpu(d->udev->descriptor.idProduct)) {
958 	case 0x1122:
959 	case 0x1120:
960 		st->i2c_gate = 4;
961 		adap->fe[0] = dvb_attach(tda10086_attach,
962 			&tda10086_config, &d->i2c_adap);
963 		if (adap->fe[0]) {
964 			info("TUN Found Frontend TDA10086");
965 			st->i2c_tuner_gate_w = 4;
966 			st->i2c_tuner_gate_r = 4;
967 			st->i2c_tuner_addr = 0x60;
968 			st->tuner_config = TUNER_LG;
969 			if (st->dvb_usb_lme2510_firmware != TUNER_LG) {
970 				st->dvb_usb_lme2510_firmware = TUNER_LG;
971 				ret = lme_firmware_switch(d, 1) ? 0 : -ENODEV;
972 			}
973 			break;
974 		}
975 
976 		st->i2c_gate = 4;
977 		adap->fe[0] = dvb_attach(stv0299_attach,
978 				&sharp_z0194_config, &d->i2c_adap);
979 		if (adap->fe[0]) {
980 			info("FE Found Stv0299");
981 			st->i2c_tuner_gate_w = 4;
982 			st->i2c_tuner_gate_r = 5;
983 			st->i2c_tuner_addr = 0x60;
984 			st->tuner_config = TUNER_S0194;
985 			if (st->dvb_usb_lme2510_firmware != TUNER_S0194) {
986 				st->dvb_usb_lme2510_firmware = TUNER_S0194;
987 				ret = lme_firmware_switch(d, 1) ? 0 : -ENODEV;
988 			}
989 			break;
990 		}
991 
992 		st->i2c_gate = 5;
993 		adap->fe[0] = dvb_attach(stv0288_attach, &lme_config,
994 			&d->i2c_adap);
995 
996 		if (adap->fe[0]) {
997 			info("FE Found Stv0288");
998 			st->i2c_tuner_gate_w = 4;
999 			st->i2c_tuner_gate_r = 5;
1000 			st->i2c_tuner_addr = 0x60;
1001 			st->tuner_config = TUNER_S7395;
1002 			if (st->dvb_usb_lme2510_firmware != TUNER_S7395) {
1003 				st->dvb_usb_lme2510_firmware = TUNER_S7395;
1004 				ret = lme_firmware_switch(d, 1) ? 0 : -ENODEV;
1005 			}
1006 			break;
1007 		}
1008 		fallthrough;
1009 	case 0x22f0:
1010 		st->i2c_gate = 5;
1011 		adap->fe[0] = dvb_attach(m88rs2000_attach,
1012 			&m88rs2000_config, &d->i2c_adap);
1013 
1014 		if (adap->fe[0]) {
1015 			info("FE Found M88RS2000");
1016 			st->i2c_tuner_gate_w = 5;
1017 			st->i2c_tuner_gate_r = 5;
1018 			st->i2c_tuner_addr = 0x60;
1019 			st->tuner_config = TUNER_RS2000;
1020 			st->fe_set_voltage =
1021 				adap->fe[0]->ops.set_voltage;
1022 		}
1023 		break;
1024 	}
1025 
1026 	if (adap->fe[0] == NULL) {
1027 		info("DM04/QQBOX Not Powered up or not Supported");
1028 		return -ENODEV;
1029 	}
1030 
1031 	if (ret) {
1032 		if (adap->fe[0]) {
1033 			dvb_frontend_detach(adap->fe[0]);
1034 			adap->fe[0] = NULL;
1035 		}
1036 		d->rc_map = NULL;
1037 		return -ENODEV;
1038 	}
1039 
1040 	st->fe_read_status = adap->fe[0]->ops.read_status;
1041 	st->fe_read_signal_strength = adap->fe[0]->ops.read_signal_strength;
1042 	st->fe_read_snr = adap->fe[0]->ops.read_snr;
1043 	st->fe_read_ber = adap->fe[0]->ops.read_ber;
1044 	st->fe_read_ucblocks = adap->fe[0]->ops.read_ucblocks;
1045 
1046 	adap->fe[0]->ops.read_status = dm04_read_status;
1047 	adap->fe[0]->ops.read_signal_strength = dm04_read_signal_strength;
1048 	adap->fe[0]->ops.read_snr = dm04_read_snr;
1049 	adap->fe[0]->ops.read_ber = dm04_read_ber;
1050 	adap->fe[0]->ops.read_ucblocks = dm04_read_ucblocks;
1051 	adap->fe[0]->ops.set_voltage = dm04_lme2510_set_voltage;
1052 
1053 	ret = lme_name(adap);
1054 	return ret;
1055 }
1056 
1057 static int dm04_lme2510_tuner(struct dvb_usb_adapter *adap)
1058 {
1059 	struct dvb_usb_device *d = adap_to_d(adap);
1060 	struct lme2510_state *st = adap_to_priv(adap);
1061 	static const char * const tun_msg[] = {"", "TDA8263", "IX2505V", "DVB_PLL_OPERA", "RS2000"};
1062 	int ret = 0;
1063 
1064 	switch (st->tuner_config) {
1065 	case TUNER_LG:
1066 		if (dvb_attach(tda826x_attach, adap->fe[0], 0x60,
1067 			&d->i2c_adap, 1))
1068 			ret = st->tuner_config;
1069 		break;
1070 	case TUNER_S7395:
1071 		if (dvb_attach(ix2505v_attach , adap->fe[0], &lme_tuner,
1072 			&d->i2c_adap))
1073 			ret = st->tuner_config;
1074 		break;
1075 	case TUNER_S0194:
1076 		if (dvb_attach(dvb_pll_attach , adap->fe[0], 0x60,
1077 			&d->i2c_adap, DVB_PLL_OPERA1))
1078 			ret = st->tuner_config;
1079 		break;
1080 	case TUNER_RS2000:
1081 		if (dvb_attach(ts2020_attach, adap->fe[0],
1082 			       &ts2020_config, &d->i2c_adap))
1083 			ret = st->tuner_config;
1084 		break;
1085 	default:
1086 		break;
1087 	}
1088 
1089 	if (ret) {
1090 		info("TUN Found %s tuner", tun_msg[ret]);
1091 	} else {
1092 		info("TUN No tuner found");
1093 		return -ENODEV;
1094 	}
1095 
1096 	/* Start the Interrupt*/
1097 	ret = lme2510_int_read(adap);
1098 	if (ret < 0) {
1099 		info("INT Unable to start Interrupt Service");
1100 		return -ENODEV;
1101 	}
1102 
1103 	return ret;
1104 }
1105 
1106 static int lme2510_powerup(struct dvb_usb_device *d, int onoff)
1107 {
1108 	struct lme2510_state *st = d->priv;
1109 	static u8 lnb_on[] = LNB_ON;
1110 	static u8 lnb_off[] = LNB_OFF;
1111 	static u8 rbuf[1];
1112 	int ret = 0, len = 3, rlen = 1;
1113 
1114 	mutex_lock(&d->i2c_mutex);
1115 
1116 	ret = lme2510_usb_talk(d, onoff ? lnb_on : lnb_off, len, rbuf, rlen);
1117 
1118 	st->i2c_talk_onoff = 1;
1119 
1120 	mutex_unlock(&d->i2c_mutex);
1121 
1122 	return ret;
1123 }
1124 
1125 static int lme2510_identify_state(struct dvb_usb_device *d, const char **name)
1126 {
1127 	struct lme2510_state *st = d->priv;
1128 	int status;
1129 
1130 	usb_reset_configuration(d->udev);
1131 
1132 	usb_set_interface(d->udev,
1133 		d->props->bInterfaceNumber, 1);
1134 
1135 	st->dvb_usb_lme2510_firmware = dvb_usb_lme2510_firmware;
1136 
1137 	status = lme2510_return_status(d);
1138 	if (status == 0x44) {
1139 		*name = lme_firmware_switch(d, 0);
1140 		return COLD;
1141 	}
1142 
1143 	if (status != 0x47)
1144 		return -EINVAL;
1145 
1146 	return WARM;
1147 }
1148 
1149 static int lme2510_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
1150 		struct usb_data_stream_properties *stream)
1151 {
1152 	struct dvb_usb_adapter *adap = fe_to_adap(fe);
1153 	struct dvb_usb_device *d;
1154 
1155 	if (adap == NULL)
1156 		return 0;
1157 
1158 	d = adap_to_d(adap);
1159 
1160 	/* Turn PID filter on the fly by module option */
1161 	if (pid_filter == 2) {
1162 		adap->pid_filtering  = true;
1163 		adap->max_feed_count = 15;
1164 	}
1165 
1166 	if (!(le16_to_cpu(d->udev->descriptor.idProduct)
1167 		== 0x1122))
1168 		stream->endpoint = 0x8;
1169 
1170 	return 0;
1171 }
1172 
1173 static int lme2510_get_rc_config(struct dvb_usb_device *d,
1174 	struct dvb_usb_rc *rc)
1175 {
1176 	rc->allowed_protos = RC_PROTO_BIT_NEC32;
1177 	return 0;
1178 }
1179 
1180 static void lme2510_exit(struct dvb_usb_device *d)
1181 {
1182 	struct lme2510_state *st = d->priv;
1183 
1184 	if (st->lme_urb) {
1185 		usb_kill_urb(st->lme_urb);
1186 		usb_free_urb(st->lme_urb);
1187 		info("Interrupt Service Stopped");
1188 	}
1189 }
1190 
1191 static struct dvb_usb_device_properties lme2510_props = {
1192 	.driver_name = KBUILD_MODNAME,
1193 	.owner = THIS_MODULE,
1194 	.bInterfaceNumber = 0,
1195 	.adapter_nr = adapter_nr,
1196 	.size_of_priv = sizeof(struct lme2510_state),
1197 	.generic_bulk_ctrl_endpoint = 0x01,
1198 	.generic_bulk_ctrl_endpoint_response = 0x01,
1199 
1200 	.download_firmware = lme2510_download_firmware,
1201 
1202 	.power_ctrl       = lme2510_powerup,
1203 	.identify_state   = lme2510_identify_state,
1204 	.i2c_algo         = &lme2510_i2c_algo,
1205 
1206 	.frontend_attach  = dm04_lme2510_frontend_attach,
1207 	.tuner_attach = dm04_lme2510_tuner,
1208 	.get_stream_config = lme2510_get_stream_config,
1209 	.streaming_ctrl   = lme2510_streaming_ctrl,
1210 
1211 	.get_rc_config = lme2510_get_rc_config,
1212 
1213 	.exit = lme2510_exit,
1214 	.num_adapters = 1,
1215 	.adapter = {
1216 		{
1217 			.caps = DVB_USB_ADAP_HAS_PID_FILTER|
1218 				DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1219 			.pid_filter_count = 15,
1220 			.pid_filter = lme2510_pid_filter,
1221 			.pid_filter_ctrl  = lme2510_pid_filter_ctrl,
1222 			.stream =
1223 			DVB_USB_STREAM_BULK(0x86, 10, 4096),
1224 		},
1225 	},
1226 };
1227 
1228 static const struct usb_device_id lme2510_id_table[] = {
1229 	{	DVB_USB_DEVICE(0x3344, 0x1122, &lme2510_props,
1230 		"DM04_LME2510_DVB-S", RC_MAP_LME2510)	},
1231 	{	DVB_USB_DEVICE(0x3344, 0x1120, &lme2510_props,
1232 		"DM04_LME2510C_DVB-S", RC_MAP_LME2510)	},
1233 	{	DVB_USB_DEVICE(0x3344, 0x22f0, &lme2510_props,
1234 		"DM04_LME2510C_DVB-S RS2000", RC_MAP_LME2510)	},
1235 	{}		/* Terminating entry */
1236 };
1237 
1238 MODULE_DEVICE_TABLE(usb, lme2510_id_table);
1239 
1240 static struct usb_driver lme2510_driver = {
1241 	.name		= KBUILD_MODNAME,
1242 	.probe		= dvb_usbv2_probe,
1243 	.disconnect	= dvb_usbv2_disconnect,
1244 	.id_table	= lme2510_id_table,
1245 	.no_dynamic_id = 1,
1246 	.soft_unbind = 1,
1247 };
1248 
1249 module_usb_driver(lme2510_driver);
1250 
1251 MODULE_AUTHOR("Malcolm Priestley <tvboxspy@gmail.com>");
1252 MODULE_DESCRIPTION("LME2510(C) DVB-S USB2.0");
1253 MODULE_VERSION("2.07");
1254 MODULE_LICENSE("GPL");
1255 MODULE_FIRMWARE(LME2510_C_S7395);
1256 MODULE_FIRMWARE(LME2510_C_LG);
1257 MODULE_FIRMWARE(LME2510_C_S0194);
1258 MODULE_FIRMWARE(LME2510_C_RS2000);
1259 MODULE_FIRMWARE(LME2510_LG);
1260 MODULE_FIRMWARE(LME2510_S0194);
1261 
1262