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