xref: /linux/drivers/media/usb/dvb-usb-v2/lmedm04.c (revision 64b14a184e83eb62ea0615e31a409956049d40e7)
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 	if (ret)
427 		return -EREMOTEIO;
428 
429 	return 0;
430 }
431 
432 static int lme2510_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
433 	int onoff)
434 {
435 	struct dvb_usb_device *d = adap_to_d(adap);
436 	int ret = 0;
437 
438 	deb_info(3, "%s PID=%04x Index=%04x onoff=%02x", __func__,
439 		pid, index, onoff);
440 
441 	if (onoff) {
442 		mutex_lock(&d->i2c_mutex);
443 		ret |= lme2510_enable_pid(d, index, pid);
444 		mutex_unlock(&d->i2c_mutex);
445 	}
446 
447 
448 	return ret;
449 }
450 
451 
452 static int lme2510_return_status(struct dvb_usb_device *d)
453 {
454 	int ret;
455 	u8 *data;
456 
457 	data = kzalloc(6, GFP_KERNEL);
458 	if (!data)
459 		return -ENOMEM;
460 
461 	ret = usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0),
462 			      0x06, 0x80, 0x0302, 0x00,
463 			      data, 0x6, 200);
464 	if (ret != 6)
465 		ret = -EINVAL;
466 	else
467 		ret = data[2];
468 
469 	info("Firmware Status: %6ph", data);
470 
471 	kfree(data);
472 	return ret;
473 }
474 
475 static int lme2510_msg(struct dvb_usb_device *d,
476 		u8 *wbuf, int wlen, u8 *rbuf, int rlen)
477 {
478 	struct lme2510_state *st = d->priv;
479 
480 	st->i2c_talk_onoff = 1;
481 
482 	return lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
483 }
484 
485 static int lme2510_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
486 				 int num)
487 {
488 	struct dvb_usb_device *d = i2c_get_adapdata(adap);
489 	struct lme2510_state *st = d->priv;
490 	static u8 obuf[64], ibuf[64];
491 	int i, read, read_o;
492 	u16 len;
493 	u8 gate;
494 
495 	mutex_lock(&d->i2c_mutex);
496 
497 	for (i = 0; i < num; i++) {
498 		read_o = msg[i].flags & I2C_M_RD;
499 		read = i + 1 < num && msg[i + 1].flags & I2C_M_RD;
500 		read |= read_o;
501 		gate = (msg[i].addr == st->i2c_tuner_addr)
502 			? (read)	? st->i2c_tuner_gate_r
503 					: st->i2c_tuner_gate_w
504 			: st->i2c_gate;
505 		obuf[0] = gate | (read << 7);
506 
507 		if (gate == 5)
508 			obuf[1] = (read) ? 2 : msg[i].len + 1;
509 		else
510 			obuf[1] = msg[i].len + read + 1;
511 
512 		obuf[2] = msg[i].addr << 1;
513 
514 		if (read) {
515 			if (read_o)
516 				len = 3;
517 			else {
518 				memcpy(&obuf[3], msg[i].buf, msg[i].len);
519 				obuf[msg[i].len+3] = msg[i+1].len;
520 				len = msg[i].len+4;
521 			}
522 		} else {
523 			memcpy(&obuf[3], msg[i].buf, msg[i].len);
524 			len = msg[i].len+3;
525 		}
526 
527 		if (lme2510_msg(d, obuf, len, ibuf, 64) < 0) {
528 			deb_info(1, "i2c transfer failed.");
529 			mutex_unlock(&d->i2c_mutex);
530 			return -EAGAIN;
531 		}
532 
533 		if (read) {
534 			if (read_o)
535 				memcpy(msg[i].buf, &ibuf[1], msg[i].len);
536 			else {
537 				memcpy(msg[i+1].buf, &ibuf[1], msg[i+1].len);
538 				i++;
539 			}
540 		}
541 	}
542 
543 	mutex_unlock(&d->i2c_mutex);
544 	return i;
545 }
546 
547 static u32 lme2510_i2c_func(struct i2c_adapter *adapter)
548 {
549 	return I2C_FUNC_I2C;
550 }
551 
552 static struct i2c_algorithm lme2510_i2c_algo = {
553 	.master_xfer   = lme2510_i2c_xfer,
554 	.functionality = lme2510_i2c_func,
555 };
556 
557 static int lme2510_streaming_ctrl(struct dvb_frontend *fe, int onoff)
558 {
559 	struct dvb_usb_adapter *adap = fe_to_adap(fe);
560 	struct dvb_usb_device *d = adap_to_d(adap);
561 	struct lme2510_state *st = adap_to_priv(adap);
562 	static u8 clear_reg_3[] = LME_ALL_PIDS;
563 	static u8 rbuf[1];
564 	int ret = 0, rlen = sizeof(rbuf);
565 
566 	deb_info(1, "STM  (%02x)", onoff);
567 
568 	/* Streaming is started by FE_HAS_LOCK */
569 	if (onoff == 1)
570 		st->stream_on = 1;
571 	else {
572 		deb_info(1, "STM Steam Off");
573 		/* mutex is here only to avoid collision with I2C */
574 		mutex_lock(&d->i2c_mutex);
575 
576 		ret = lme2510_usb_talk(d, clear_reg_3,
577 				sizeof(clear_reg_3), rbuf, rlen);
578 		st->stream_on = 0;
579 		st->i2c_talk_onoff = 1;
580 
581 		mutex_unlock(&d->i2c_mutex);
582 	}
583 
584 	return (ret < 0) ? -ENODEV : 0;
585 }
586 
587 static u8 check_sum(u8 *p, u8 len)
588 {
589 	u8 sum = 0;
590 	while (len--)
591 		sum += *p++;
592 	return sum;
593 }
594 
595 static int lme2510_download_firmware(struct dvb_usb_device *d,
596 					const struct firmware *fw)
597 {
598 	int ret = 0;
599 	u8 *data;
600 	u16 j, wlen, len_in, start, end;
601 	u8 packet_size, dlen, i;
602 	u8 *fw_data;
603 
604 	packet_size = 0x31;
605 	len_in = 1;
606 
607 	data = kzalloc(128, GFP_KERNEL);
608 	if (!data) {
609 		info("FRM Could not start Firmware Download"\
610 			"(Buffer allocation failed)");
611 		return -ENOMEM;
612 	}
613 
614 	info("FRM Starting Firmware Download");
615 
616 	for (i = 1; i < 3; i++) {
617 		start = (i == 1) ? 0 : 512;
618 		end = (i == 1) ? 512 : fw->size;
619 		for (j = start; j < end; j += (packet_size+1)) {
620 			fw_data = (u8 *)(fw->data + j);
621 			if ((end - j) > packet_size) {
622 				data[0] = i;
623 				dlen = packet_size;
624 			} else {
625 				data[0] = i | 0x80;
626 				dlen = (u8)(end - j)-1;
627 			}
628 			data[1] = dlen;
629 			memcpy(&data[2], fw_data, dlen+1);
630 			wlen = (u8) dlen + 4;
631 			data[wlen-1] = check_sum(fw_data, dlen+1);
632 			deb_info(1, "Data S=%02x:E=%02x CS= %02x", data[3],
633 				data[dlen+2], data[dlen+3]);
634 			lme2510_usb_talk(d, data, wlen, data, len_in);
635 			ret |= (data[0] == 0x88) ? 0 : -1;
636 		}
637 	}
638 
639 	data[0] = 0x8a;
640 	len_in = 1;
641 	msleep(2000);
642 	lme2510_usb_talk(d, data, len_in, data, len_in);
643 	msleep(400);
644 
645 	if (ret < 0)
646 		info("FRM Firmware Download Failed (%04x)" , ret);
647 	else
648 		info("FRM Firmware Download Completed - Resetting Device");
649 
650 	kfree(data);
651 	return RECONNECTS_USB;
652 }
653 
654 static void lme_coldreset(struct dvb_usb_device *d)
655 {
656 	u8 data[1] = {0};
657 	data[0] = 0x0a;
658 	info("FRM Firmware Cold Reset");
659 
660 	lme2510_usb_talk(d, data, sizeof(data), data, sizeof(data));
661 
662 	return;
663 }
664 
665 static const char fw_c_s7395[] = LME2510_C_S7395;
666 static const char fw_c_lg[] = LME2510_C_LG;
667 static const char fw_c_s0194[] = LME2510_C_S0194;
668 static const char fw_c_rs2000[] = LME2510_C_RS2000;
669 static const char fw_lg[] = LME2510_LG;
670 static const char fw_s0194[] = LME2510_S0194;
671 
672 static const char *lme_firmware_switch(struct dvb_usb_device *d, int cold)
673 {
674 	struct lme2510_state *st = d->priv;
675 	struct usb_device *udev = d->udev;
676 	const struct firmware *fw = NULL;
677 	const char *fw_lme;
678 	int ret = 0;
679 
680 	cold = (cold > 0) ? (cold & 1) : 0;
681 
682 	switch (le16_to_cpu(udev->descriptor.idProduct)) {
683 	case 0x1122:
684 		switch (st->dvb_usb_lme2510_firmware) {
685 		default:
686 		case TUNER_S0194:
687 			fw_lme = fw_s0194;
688 			ret = request_firmware(&fw, fw_lme, &udev->dev);
689 			if (ret == 0) {
690 				st->dvb_usb_lme2510_firmware = TUNER_S0194;
691 				cold = 0;
692 				break;
693 			}
694 			fallthrough;
695 		case TUNER_LG:
696 			fw_lme = fw_lg;
697 			ret = request_firmware(&fw, fw_lme, &udev->dev);
698 			if (ret == 0) {
699 				st->dvb_usb_lme2510_firmware = TUNER_LG;
700 				break;
701 			}
702 			st->dvb_usb_lme2510_firmware = TUNER_DEFAULT;
703 			break;
704 		}
705 		break;
706 	case 0x1120:
707 		switch (st->dvb_usb_lme2510_firmware) {
708 		default:
709 		case TUNER_S7395:
710 			fw_lme = fw_c_s7395;
711 			ret = request_firmware(&fw, fw_lme, &udev->dev);
712 			if (ret == 0) {
713 				st->dvb_usb_lme2510_firmware = TUNER_S7395;
714 				cold = 0;
715 				break;
716 			}
717 			fallthrough;
718 		case TUNER_LG:
719 			fw_lme = fw_c_lg;
720 			ret = request_firmware(&fw, fw_lme, &udev->dev);
721 			if (ret == 0) {
722 				st->dvb_usb_lme2510_firmware = TUNER_LG;
723 				break;
724 			}
725 			fallthrough;
726 		case TUNER_S0194:
727 			fw_lme = fw_c_s0194;
728 			ret = request_firmware(&fw, fw_lme, &udev->dev);
729 			if (ret == 0) {
730 				st->dvb_usb_lme2510_firmware = TUNER_S0194;
731 				break;
732 			}
733 			st->dvb_usb_lme2510_firmware = TUNER_DEFAULT;
734 			cold = 0;
735 			break;
736 		}
737 		break;
738 	case 0x22f0:
739 		fw_lme = fw_c_rs2000;
740 		st->dvb_usb_lme2510_firmware = TUNER_RS2000;
741 		break;
742 	default:
743 		fw_lme = fw_c_s7395;
744 	}
745 
746 	release_firmware(fw);
747 
748 	if (cold) {
749 		dvb_usb_lme2510_firmware = st->dvb_usb_lme2510_firmware;
750 		info("FRM Changing to %s firmware", fw_lme);
751 		lme_coldreset(d);
752 		return NULL;
753 	}
754 
755 	return fw_lme;
756 }
757 
758 static struct tda10086_config tda10086_config = {
759 	.demod_address = 0x0e,
760 	.invert = 0,
761 	.diseqc_tone = 1,
762 	.xtal_freq = TDA10086_XTAL_16M,
763 };
764 
765 static struct stv0288_config lme_config = {
766 	.demod_address = 0x68,
767 	.min_delay_ms = 15,
768 	.inittab = s7395_inittab,
769 };
770 
771 static struct ix2505v_config lme_tuner = {
772 	.tuner_address = 0x60,
773 	.min_delay_ms = 100,
774 	.tuner_gain = 0x0,
775 	.tuner_chargepump = 0x3,
776 };
777 
778 static struct stv0299_config sharp_z0194_config = {
779 	.demod_address = 0x68,
780 	.inittab = sharp_z0194a_inittab,
781 	.mclk = 88000000UL,
782 	.invert = 0,
783 	.skip_reinit = 0,
784 	.lock_output = STV0299_LOCKOUTPUT_1,
785 	.volt13_op0_op1 = STV0299_VOLT13_OP1,
786 	.min_delay_ms = 100,
787 	.set_symbol_rate = sharp_z0194a_set_symbol_rate,
788 };
789 
790 static struct m88rs2000_config m88rs2000_config = {
791 	.demod_addr = 0x68
792 };
793 
794 static struct ts2020_config ts2020_config = {
795 	.tuner_address = 0x60,
796 	.clk_out_div = 7,
797 	.dont_poll = true
798 };
799 
800 static int dm04_lme2510_set_voltage(struct dvb_frontend *fe,
801 				    enum fe_sec_voltage voltage)
802 {
803 	struct dvb_usb_device *d = fe_to_d(fe);
804 	struct lme2510_state *st = fe_to_priv(fe);
805 	static u8 voltage_low[] = LME_VOLTAGE_L;
806 	static u8 voltage_high[] = LME_VOLTAGE_H;
807 	static u8 rbuf[1];
808 	int ret = 0, len = 3, rlen = 1;
809 
810 	mutex_lock(&d->i2c_mutex);
811 
812 	switch (voltage) {
813 	case SEC_VOLTAGE_18:
814 		ret |= lme2510_usb_talk(d,
815 			voltage_high, len, rbuf, rlen);
816 		break;
817 
818 	case SEC_VOLTAGE_OFF:
819 	case SEC_VOLTAGE_13:
820 	default:
821 		ret |= lme2510_usb_talk(d,
822 				voltage_low, len, rbuf, rlen);
823 		break;
824 	}
825 
826 	mutex_unlock(&d->i2c_mutex);
827 
828 	if (st->tuner_config == TUNER_RS2000)
829 		if (st->fe_set_voltage)
830 			st->fe_set_voltage(fe, voltage);
831 
832 
833 	return (ret < 0) ? -ENODEV : 0;
834 }
835 
836 static int dm04_read_status(struct dvb_frontend *fe, enum fe_status *status)
837 {
838 	struct dvb_usb_device *d = fe_to_d(fe);
839 	struct lme2510_state *st = d->priv;
840 	int ret = 0;
841 
842 	if (st->i2c_talk_onoff) {
843 		if (st->fe_read_status) {
844 			ret = st->fe_read_status(fe, status);
845 			if (ret < 0)
846 				return ret;
847 		}
848 
849 		st->lock_status = *status;
850 
851 		if (*status & FE_HAS_LOCK && st->stream_on) {
852 			mutex_lock(&d->i2c_mutex);
853 
854 			st->i2c_talk_onoff = 0;
855 			ret = lme2510_stream_restart(d);
856 
857 			mutex_unlock(&d->i2c_mutex);
858 		}
859 
860 		return ret;
861 	}
862 
863 	/* Timeout of interrupt reached on RS2000 */
864 	if (st->tuner_config == TUNER_RS2000 &&
865 	    time_after(jiffies, st->int_urb_due))
866 		st->lock_status &= ~FE_HAS_LOCK;
867 
868 	*status = st->lock_status;
869 
870 	if (!(*status & FE_HAS_LOCK)) {
871 		struct dvb_usb_adapter *adap = fe_to_adap(fe);
872 
873 		st->i2c_talk_onoff = 1;
874 
875 		lme2510_update_stats(adap);
876 	}
877 
878 	return ret;
879 }
880 
881 static int dm04_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
882 {
883 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
884 	struct lme2510_state *st = fe_to_priv(fe);
885 
886 	if (st->fe_read_signal_strength && !st->stream_on)
887 		return st->fe_read_signal_strength(fe, strength);
888 
889 	if (c->strength.stat[0].scale == FE_SCALE_RELATIVE)
890 		*strength = (u16)c->strength.stat[0].uvalue;
891 	else
892 		*strength = 0;
893 
894 	return 0;
895 }
896 
897 static int dm04_read_snr(struct dvb_frontend *fe, u16 *snr)
898 {
899 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
900 	struct lme2510_state *st = fe_to_priv(fe);
901 
902 	if (st->fe_read_snr && !st->stream_on)
903 		return st->fe_read_snr(fe, snr);
904 
905 	if (c->cnr.stat[0].scale == FE_SCALE_RELATIVE)
906 		*snr = (u16)c->cnr.stat[0].uvalue;
907 	else
908 		*snr = 0;
909 
910 	return 0;
911 }
912 
913 static int dm04_read_ber(struct dvb_frontend *fe, u32 *ber)
914 {
915 	struct lme2510_state *st = fe_to_priv(fe);
916 
917 	if (st->fe_read_ber && !st->stream_on)
918 		return st->fe_read_ber(fe, ber);
919 
920 	*ber = 0;
921 
922 	return 0;
923 }
924 
925 static int dm04_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
926 {
927 	struct lme2510_state *st = fe_to_priv(fe);
928 
929 	if (st->fe_read_ucblocks && !st->stream_on)
930 		return st->fe_read_ucblocks(fe, ucblocks);
931 
932 	*ucblocks = 0;
933 
934 	return 0;
935 }
936 
937 static int lme_name(struct dvb_usb_adapter *adap)
938 {
939 	struct dvb_usb_device *d = adap_to_d(adap);
940 	struct lme2510_state *st = adap_to_priv(adap);
941 	const char *desc = d->name;
942 	static const char * const fe_name[] = {
943 		"", " LG TDQY-P001F", " SHARP:BS2F7HZ7395",
944 		" SHARP:BS2F7HZ0194", " RS2000"};
945 	char *name = adap->fe[0]->ops.info.name;
946 
947 	strscpy(name, desc, 128);
948 	strlcat(name, fe_name[st->tuner_config], 128);
949 
950 	return 0;
951 }
952 
953 static int dm04_lme2510_frontend_attach(struct dvb_usb_adapter *adap)
954 {
955 	struct dvb_usb_device *d = adap_to_d(adap);
956 	struct lme2510_state *st = d->priv;
957 	int ret = 0;
958 
959 	st->i2c_talk_onoff = 1;
960 	switch (le16_to_cpu(d->udev->descriptor.idProduct)) {
961 	case 0x1122:
962 	case 0x1120:
963 		st->i2c_gate = 4;
964 		adap->fe[0] = dvb_attach(tda10086_attach,
965 			&tda10086_config, &d->i2c_adap);
966 		if (adap->fe[0]) {
967 			info("TUN Found Frontend TDA10086");
968 			st->i2c_tuner_gate_w = 4;
969 			st->i2c_tuner_gate_r = 4;
970 			st->i2c_tuner_addr = 0x60;
971 			st->tuner_config = TUNER_LG;
972 			if (st->dvb_usb_lme2510_firmware != TUNER_LG) {
973 				st->dvb_usb_lme2510_firmware = TUNER_LG;
974 				ret = lme_firmware_switch(d, 1) ? 0 : -ENODEV;
975 			}
976 			break;
977 		}
978 
979 		st->i2c_gate = 4;
980 		adap->fe[0] = dvb_attach(stv0299_attach,
981 				&sharp_z0194_config, &d->i2c_adap);
982 		if (adap->fe[0]) {
983 			info("FE Found Stv0299");
984 			st->i2c_tuner_gate_w = 4;
985 			st->i2c_tuner_gate_r = 5;
986 			st->i2c_tuner_addr = 0x60;
987 			st->tuner_config = TUNER_S0194;
988 			if (st->dvb_usb_lme2510_firmware != TUNER_S0194) {
989 				st->dvb_usb_lme2510_firmware = TUNER_S0194;
990 				ret = lme_firmware_switch(d, 1) ? 0 : -ENODEV;
991 			}
992 			break;
993 		}
994 
995 		st->i2c_gate = 5;
996 		adap->fe[0] = dvb_attach(stv0288_attach, &lme_config,
997 			&d->i2c_adap);
998 
999 		if (adap->fe[0]) {
1000 			info("FE Found Stv0288");
1001 			st->i2c_tuner_gate_w = 4;
1002 			st->i2c_tuner_gate_r = 5;
1003 			st->i2c_tuner_addr = 0x60;
1004 			st->tuner_config = TUNER_S7395;
1005 			if (st->dvb_usb_lme2510_firmware != TUNER_S7395) {
1006 				st->dvb_usb_lme2510_firmware = TUNER_S7395;
1007 				ret = lme_firmware_switch(d, 1) ? 0 : -ENODEV;
1008 			}
1009 			break;
1010 		}
1011 		fallthrough;
1012 	case 0x22f0:
1013 		st->i2c_gate = 5;
1014 		adap->fe[0] = dvb_attach(m88rs2000_attach,
1015 			&m88rs2000_config, &d->i2c_adap);
1016 
1017 		if (adap->fe[0]) {
1018 			info("FE Found M88RS2000");
1019 			st->i2c_tuner_gate_w = 5;
1020 			st->i2c_tuner_gate_r = 5;
1021 			st->i2c_tuner_addr = 0x60;
1022 			st->tuner_config = TUNER_RS2000;
1023 			st->fe_set_voltage =
1024 				adap->fe[0]->ops.set_voltage;
1025 		}
1026 		break;
1027 	}
1028 
1029 	if (adap->fe[0] == NULL) {
1030 		info("DM04/QQBOX Not Powered up or not Supported");
1031 		return -ENODEV;
1032 	}
1033 
1034 	if (ret) {
1035 		if (adap->fe[0]) {
1036 			dvb_frontend_detach(adap->fe[0]);
1037 			adap->fe[0] = NULL;
1038 		}
1039 		d->rc_map = NULL;
1040 		return -ENODEV;
1041 	}
1042 
1043 	st->fe_read_status = adap->fe[0]->ops.read_status;
1044 	st->fe_read_signal_strength = adap->fe[0]->ops.read_signal_strength;
1045 	st->fe_read_snr = adap->fe[0]->ops.read_snr;
1046 	st->fe_read_ber = adap->fe[0]->ops.read_ber;
1047 	st->fe_read_ucblocks = adap->fe[0]->ops.read_ucblocks;
1048 
1049 	adap->fe[0]->ops.read_status = dm04_read_status;
1050 	adap->fe[0]->ops.read_signal_strength = dm04_read_signal_strength;
1051 	adap->fe[0]->ops.read_snr = dm04_read_snr;
1052 	adap->fe[0]->ops.read_ber = dm04_read_ber;
1053 	adap->fe[0]->ops.read_ucblocks = dm04_read_ucblocks;
1054 	adap->fe[0]->ops.set_voltage = dm04_lme2510_set_voltage;
1055 
1056 	ret = lme_name(adap);
1057 	return ret;
1058 }
1059 
1060 static int dm04_lme2510_tuner(struct dvb_usb_adapter *adap)
1061 {
1062 	struct dvb_usb_device *d = adap_to_d(adap);
1063 	struct lme2510_state *st = adap_to_priv(adap);
1064 	static const char * const tun_msg[] = {"", "TDA8263", "IX2505V", "DVB_PLL_OPERA", "RS2000"};
1065 	int ret = 0;
1066 
1067 	switch (st->tuner_config) {
1068 	case TUNER_LG:
1069 		if (dvb_attach(tda826x_attach, adap->fe[0], 0x60,
1070 			&d->i2c_adap, 1))
1071 			ret = st->tuner_config;
1072 		break;
1073 	case TUNER_S7395:
1074 		if (dvb_attach(ix2505v_attach , adap->fe[0], &lme_tuner,
1075 			&d->i2c_adap))
1076 			ret = st->tuner_config;
1077 		break;
1078 	case TUNER_S0194:
1079 		if (dvb_attach(dvb_pll_attach , adap->fe[0], 0x60,
1080 			&d->i2c_adap, DVB_PLL_OPERA1))
1081 			ret = st->tuner_config;
1082 		break;
1083 	case TUNER_RS2000:
1084 		if (dvb_attach(ts2020_attach, adap->fe[0],
1085 			       &ts2020_config, &d->i2c_adap))
1086 			ret = st->tuner_config;
1087 		break;
1088 	default:
1089 		break;
1090 	}
1091 
1092 	if (ret) {
1093 		info("TUN Found %s tuner", tun_msg[ret]);
1094 	} else {
1095 		info("TUN No tuner found");
1096 		return -ENODEV;
1097 	}
1098 
1099 	/* Start the Interrupt*/
1100 	ret = lme2510_int_read(adap);
1101 	if (ret < 0) {
1102 		info("INT Unable to start Interrupt Service");
1103 		return -ENODEV;
1104 	}
1105 
1106 	return ret;
1107 }
1108 
1109 static int lme2510_powerup(struct dvb_usb_device *d, int onoff)
1110 {
1111 	struct lme2510_state *st = d->priv;
1112 	static u8 lnb_on[] = LNB_ON;
1113 	static u8 lnb_off[] = LNB_OFF;
1114 	static u8 rbuf[1];
1115 	int ret = 0, len = 3, rlen = 1;
1116 
1117 	mutex_lock(&d->i2c_mutex);
1118 
1119 	ret = lme2510_usb_talk(d, onoff ? lnb_on : lnb_off, len, rbuf, rlen);
1120 
1121 	st->i2c_talk_onoff = 1;
1122 
1123 	mutex_unlock(&d->i2c_mutex);
1124 
1125 	return ret;
1126 }
1127 
1128 static int lme2510_identify_state(struct dvb_usb_device *d, const char **name)
1129 {
1130 	struct lme2510_state *st = d->priv;
1131 	int status;
1132 
1133 	usb_reset_configuration(d->udev);
1134 
1135 	usb_set_interface(d->udev,
1136 		d->props->bInterfaceNumber, 1);
1137 
1138 	st->dvb_usb_lme2510_firmware = dvb_usb_lme2510_firmware;
1139 
1140 	status = lme2510_return_status(d);
1141 	if (status == 0x44) {
1142 		*name = lme_firmware_switch(d, 0);
1143 		return COLD;
1144 	}
1145 
1146 	if (status != 0x47)
1147 		return -EINVAL;
1148 
1149 	return WARM;
1150 }
1151 
1152 static int lme2510_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
1153 		struct usb_data_stream_properties *stream)
1154 {
1155 	struct dvb_usb_adapter *adap = fe_to_adap(fe);
1156 	struct dvb_usb_device *d;
1157 
1158 	if (adap == NULL)
1159 		return 0;
1160 
1161 	d = adap_to_d(adap);
1162 
1163 	/* Turn PID filter on the fly by module option */
1164 	if (pid_filter == 2) {
1165 		adap->pid_filtering  = true;
1166 		adap->max_feed_count = 15;
1167 	}
1168 
1169 	if (!(le16_to_cpu(d->udev->descriptor.idProduct)
1170 		== 0x1122))
1171 		stream->endpoint = 0x8;
1172 
1173 	return 0;
1174 }
1175 
1176 static int lme2510_get_rc_config(struct dvb_usb_device *d,
1177 	struct dvb_usb_rc *rc)
1178 {
1179 	rc->allowed_protos = RC_PROTO_BIT_NEC32;
1180 	return 0;
1181 }
1182 
1183 static void lme2510_exit(struct dvb_usb_device *d)
1184 {
1185 	struct lme2510_state *st = d->priv;
1186 
1187 	if (st->lme_urb) {
1188 		usb_kill_urb(st->lme_urb);
1189 		usb_free_urb(st->lme_urb);
1190 		info("Interrupt Service Stopped");
1191 	}
1192 }
1193 
1194 static struct dvb_usb_device_properties lme2510_props = {
1195 	.driver_name = KBUILD_MODNAME,
1196 	.owner = THIS_MODULE,
1197 	.bInterfaceNumber = 0,
1198 	.adapter_nr = adapter_nr,
1199 	.size_of_priv = sizeof(struct lme2510_state),
1200 	.generic_bulk_ctrl_endpoint = 0x01,
1201 	.generic_bulk_ctrl_endpoint_response = 0x01,
1202 
1203 	.download_firmware = lme2510_download_firmware,
1204 
1205 	.power_ctrl       = lme2510_powerup,
1206 	.identify_state   = lme2510_identify_state,
1207 	.i2c_algo         = &lme2510_i2c_algo,
1208 
1209 	.frontend_attach  = dm04_lme2510_frontend_attach,
1210 	.tuner_attach = dm04_lme2510_tuner,
1211 	.get_stream_config = lme2510_get_stream_config,
1212 	.streaming_ctrl   = lme2510_streaming_ctrl,
1213 
1214 	.get_rc_config = lme2510_get_rc_config,
1215 
1216 	.exit = lme2510_exit,
1217 	.num_adapters = 1,
1218 	.adapter = {
1219 		{
1220 			.caps = DVB_USB_ADAP_HAS_PID_FILTER|
1221 				DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1222 			.pid_filter_count = 15,
1223 			.pid_filter = lme2510_pid_filter,
1224 			.pid_filter_ctrl  = lme2510_pid_filter_ctrl,
1225 			.stream =
1226 			DVB_USB_STREAM_BULK(0x86, 10, 4096),
1227 		},
1228 	},
1229 };
1230 
1231 static const struct usb_device_id lme2510_id_table[] = {
1232 	{	DVB_USB_DEVICE(0x3344, 0x1122, &lme2510_props,
1233 		"DM04_LME2510_DVB-S", RC_MAP_LME2510)	},
1234 	{	DVB_USB_DEVICE(0x3344, 0x1120, &lme2510_props,
1235 		"DM04_LME2510C_DVB-S", RC_MAP_LME2510)	},
1236 	{	DVB_USB_DEVICE(0x3344, 0x22f0, &lme2510_props,
1237 		"DM04_LME2510C_DVB-S RS2000", RC_MAP_LME2510)	},
1238 	{}		/* Terminating entry */
1239 };
1240 
1241 MODULE_DEVICE_TABLE(usb, lme2510_id_table);
1242 
1243 static struct usb_driver lme2510_driver = {
1244 	.name		= KBUILD_MODNAME,
1245 	.probe		= dvb_usbv2_probe,
1246 	.disconnect	= dvb_usbv2_disconnect,
1247 	.id_table	= lme2510_id_table,
1248 	.no_dynamic_id = 1,
1249 	.soft_unbind = 1,
1250 };
1251 
1252 module_usb_driver(lme2510_driver);
1253 
1254 MODULE_AUTHOR("Malcolm Priestley <tvboxspy@gmail.com>");
1255 MODULE_DESCRIPTION("LME2510(C) DVB-S USB2.0");
1256 MODULE_VERSION("2.07");
1257 MODULE_LICENSE("GPL");
1258 MODULE_FIRMWARE(LME2510_C_S7395);
1259 MODULE_FIRMWARE(LME2510_C_LG);
1260 MODULE_FIRMWARE(LME2510_C_S0194);
1261 MODULE_FIRMWARE(LME2510_C_RS2000);
1262 MODULE_FIRMWARE(LME2510_LG);
1263 MODULE_FIRMWARE(LME2510_S0194);
1264 
1265