blob: a91fdad8f8d4b6407fe8cf39c4a7d7f500c65fef [file] [log] [blame]
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001/* DVB USB compliant linux driver for
2 *
3 * DM04/QQBOX DVB-S USB BOX LME2510C + SHARP:BS2F7HZ7395
Malcolm Priestleyab599a62010-10-16 16:44:43 -03004 * LME2510C + LG TDQY-P001F
Malcolm Priestley9d4540c2011-02-12 22:35:08 -03005 * LME2510C + BS2F7HZ0194
Malcolm Priestleyab599a62010-10-16 16:44:43 -03006 * LME2510 + LG TDQY-P001F
Malcolm Priestley9d4540c2011-02-12 22:35:08 -03007 * LME2510 + BS2F7HZ0194
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03008 *
9 * MVB7395 (LME2510C+SHARP:BS2F7HZ7395)
10 * SHARP:BS2F7HZ7395 = (STV0288+Sharp IX2505V)
11 *
Malcolm Priestleyab599a62010-10-16 16:44:43 -030012 * MV001F (LME2510+LGTDQY-P001F)
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -030013 * LG TDQY - P001F =(TDA8263 + TDA10086H)
14 *
Malcolm Priestleyab599a62010-10-16 16:44:43 -030015 * MVB0001F (LME2510C+LGTDQT-P001F)
16 *
Malcolm Priestley9d4540c2011-02-12 22:35:08 -030017 * MV0194 (LME2510+SHARP:BS2F7HZ0194)
18 * SHARP:BS2F7HZ0194 = (STV0299+IX2410)
19 *
20 * MVB0194 (LME2510C+SHARP0194)
21 *
Malcolm Priestleye41543a2012-08-06 16:21:40 -030022 * LME2510C + M88RS2000
23 *
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -030024 * For firmware see Documentation/dvb/lmedm04.txt
25 *
26 * I2C addresses:
27 * 0xd0 - STV0288 - Demodulator
28 * 0xc0 - Sharp IX2505V - Tuner
Malcolm Priestley9d4540c2011-02-12 22:35:08 -030029 * --
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -030030 * 0x1c - TDA10086 - Demodulator
31 * 0xc0 - TDA8263 - Tuner
Malcolm Priestley9d4540c2011-02-12 22:35:08 -030032 * --
33 * 0xd0 - STV0299 - Demodulator
34 * 0xc0 - IX2410 - Tuner
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -030035 *
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 * This program is free software; you can redistribute it and/or modify
43 * it under the terms of the GNU General Public License Version 2, as
44 * published by the Free Software Foundation.
45 *
46 * This program is distributed in the hope that it will be useful,
47 * but WITHOUT ANY WARRANTY; without even the implied warranty of
48 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
49 * GNU General Public License for more details.
50 *
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -030051 *
52 * see Documentation/dvb/README.dvb-usb for more information
53 *
54 * Known Issues :
55 * LME2510: Non Intel USB chipsets fail to maintain High Speed on
56 * Boot or Hot Plug.
57 *
Malcolm Priestleyab599a62010-10-16 16:44:43 -030058 * QQbox suffers from noise on LNB voltage.
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -030059 *
Malcolm Priestley9d4540c2011-02-12 22:35:08 -030060 * LME2510: SHARP:BS2F7HZ0194(MV0194) cannot cold reset and share system
61 * with other tuners. After a cold reset streaming will not start.
62 *
Malcolm Priestleye41543a2012-08-06 16:21:40 -030063 * M88RS2000 suffers from loss of lock.
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -030064 */
65#define DVB_USB_LOG_PREFIX "LME2510(C)"
66#include <linux/usb.h>
67#include <linux/usb/input.h>
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -030068#include <media/rc-core.h>
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -030069
Malcolm Priestleye41543a2012-08-06 16:21:40 -030070#include "dvb_usb.h"
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -030071#include "lmedm04.h"
72#include "tda826x.h"
73#include "tda10086.h"
74#include "stv0288.h"
75#include "ix2505v.h"
Malcolm Priestley9d4540c2011-02-12 22:35:08 -030076#include "stv0299.h"
77#include "dvb-pll.h"
78#include "z0194a.h"
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -030079#include "m88rs2000.h"
Igor M. Liplianinb858c332012-12-28 19:40:33 -030080#include "ts2020.h"
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -030081
82
Malcolm Priestleye41543a2012-08-06 16:21:40 -030083#define LME2510_C_S7395 "dvb-usb-lme2510c-s7395.fw";
84#define LME2510_C_LG "dvb-usb-lme2510c-lg.fw";
85#define LME2510_C_S0194 "dvb-usb-lme2510c-s0194.fw";
86#define LME2510_C_RS2000 "dvb-usb-lme2510c-rs2000.fw";
87#define LME2510_LG "dvb-usb-lme2510-lg.fw";
88#define LME2510_S0194 "dvb-usb-lme2510-s0194.fw";
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -030089
90/* debug */
91static int dvb_usb_lme2510_debug;
Malcolm Priestleye41543a2012-08-06 16:21:40 -030092#define lme_debug(var, level, args...) do { \
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -030093 if ((var >= level)) \
Malcolm Priestleye41543a2012-08-06 16:21:40 -030094 pr_debug(DVB_USB_LOG_PREFIX": " args); \
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -030095} while (0)
Malcolm Priestleye41543a2012-08-06 16:21:40 -030096#define deb_info(level, args...) lme_debug(dvb_usb_lme2510_debug, level, args)
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -030097#define debug_data_snipet(level, name, p) \
Rasmus Villemoes3a3402d2016-11-30 19:39:09 -020098 deb_info(level, name" (%8phN)", p);
Malcolm Priestleye41543a2012-08-06 16:21:40 -030099#define info(args...) pr_info(DVB_USB_LOG_PREFIX": "args)
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300100
101module_param_named(debug, dvb_usb_lme2510_debug, int, 0644);
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300102MODULE_PARM_DESC(debug, "set debugging level (1=info (or-able)).");
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300103
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300104static int dvb_usb_lme2510_firmware;
105module_param_named(firmware, dvb_usb_lme2510_firmware, int, 0644);
106MODULE_PARM_DESC(firmware, "set default firmware 0=Sharp7395 1=LG");
107
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300108static int pid_filter;
109module_param_named(pid, pid_filter, int, 0644);
Malcolm Priestley88495032012-02-06 19:05:01 -0300110MODULE_PARM_DESC(pid, "set default 0=default 1=off 2=on");
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300111
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300112
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300113DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300114
115#define TUNER_DEFAULT 0x0
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300116#define TUNER_LG 0x1
117#define TUNER_S7395 0x2
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300118#define TUNER_S0194 0x3
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300119#define TUNER_RS2000 0x4
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300120
121struct lme2510_state {
Malcolm Priestley3a3702222014-05-24 06:35:57 -0300122 unsigned long int_urb_due;
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300123 enum fe_status lock_status;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300124 u8 id;
125 u8 tuner_config;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300126 u8 signal_level;
127 u8 signal_sn;
128 u8 time_key;
129 u8 i2c_talk_onoff;
130 u8 i2c_gate;
131 u8 i2c_tuner_gate_w;
132 u8 i2c_tuner_gate_r;
133 u8 i2c_tuner_addr;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300134 u8 stream_on;
Malcolm Priestley449a0ba2011-04-22 06:20:57 -0300135 u8 pid_size;
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300136 u8 pid_off;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300137 void *buffer;
138 struct urb *lme_urb;
139 void *usb_buffer;
Malcolm Priestley743ab6a12015-01-02 10:56:30 -0300140 /* Frontend original calls */
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300141 int (*fe_read_status)(struct dvb_frontend *, enum fe_status *);
Malcolm Priestley87169012015-01-02 10:56:31 -0300142 int (*fe_read_signal_strength)(struct dvb_frontend *, u16 *);
143 int (*fe_read_snr)(struct dvb_frontend *, u16 *);
144 int (*fe_read_ber)(struct dvb_frontend *, u32 *);
145 int (*fe_read_ucblocks)(struct dvb_frontend *, u32 *);
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300146 int (*fe_set_voltage)(struct dvb_frontend *, enum fe_sec_voltage);
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300147 u8 dvb_usb_lme2510_firmware;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300148};
149
150static int lme2510_bulk_write(struct usb_device *dev,
151 u8 *snd, int len, u8 pipe)
152{
Masahiro Yamada29a8d972016-09-06 19:52:24 -0300153 int actual_l;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300154
Masahiro Yamada29a8d972016-09-06 19:52:24 -0300155 return usb_bulk_msg(dev, usb_sndbulkpipe(dev, pipe),
156 snd, len, &actual_l, 100);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300157}
158
159static int lme2510_bulk_read(struct usb_device *dev,
160 u8 *rev, int len, u8 pipe)
161{
Masahiro Yamada29a8d972016-09-06 19:52:24 -0300162 int actual_l;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300163
Masahiro Yamada29a8d972016-09-06 19:52:24 -0300164 return usb_bulk_msg(dev, usb_rcvbulkpipe(dev, pipe),
165 rev, len, &actual_l, 200);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300166}
167
168static int lme2510_usb_talk(struct dvb_usb_device *d,
169 u8 *wbuf, int wlen, u8 *rbuf, int rlen)
170{
171 struct lme2510_state *st = d->priv;
172 u8 *buff;
173 int ret = 0;
174
175 if (st->usb_buffer == NULL) {
tvboxspy810fda92011-09-11 19:30:10 -0300176 st->usb_buffer = kmalloc(64, GFP_KERNEL);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300177 if (st->usb_buffer == NULL) {
178 info("MEM Error no memory");
179 return -ENOMEM;
180 }
181 }
182 buff = st->usb_buffer;
183
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300184 ret = mutex_lock_interruptible(&d->usb_mutex);
185
186 if (ret < 0)
187 return -EAGAIN;
188
tvboxspy810fda92011-09-11 19:30:10 -0300189 /* the read/write capped at 64 */
190 memcpy(buff, wbuf, (wlen < 64) ? wlen : 64);
Malcolm Priestleya760d632011-04-22 06:07:40 -0300191
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300192 ret |= lme2510_bulk_write(d->udev, buff, wlen , 0x01);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300193
tvboxspy810fda92011-09-11 19:30:10 -0300194 ret |= lme2510_bulk_read(d->udev, buff, (rlen < 64) ?
195 rlen : 64 , 0x01);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300196
197 if (rlen > 0)
198 memcpy(rbuf, buff, rlen);
199
200 mutex_unlock(&d->usb_mutex);
201
202 return (ret < 0) ? -ENODEV : 0;
203}
204
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300205static int lme2510_stream_restart(struct dvb_usb_device *d)
206{
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300207 struct lme2510_state *st = d->priv;
208 u8 all_pids[] = LME_ALL_PIDS;
209 u8 stream_on[] = LME_ST_ON_W;
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300210 u8 rbuff[1];
211 if (st->pid_off)
Gustavo A. R. Silvad83bc7b2017-06-22 01:22:47 -0300212 lme2510_usb_talk(d, all_pids, sizeof(all_pids),
213 rbuff, sizeof(rbuff));
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300214 /*Restart Stream Command*/
Gustavo A. R. Silvad83bc7b2017-06-22 01:22:47 -0300215 return lme2510_usb_talk(d, stream_on, sizeof(stream_on),
216 rbuff, sizeof(rbuff));
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300217}
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300218
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300219static int lme2510_enable_pid(struct dvb_usb_device *d, u8 index, u16 pid_out)
220{
221 struct lme2510_state *st = d->priv;
222 static u8 pid_buff[] = LME_ZERO_PID;
223 static u8 rbuf[1];
224 u8 pid_no = index * 2;
Malcolm Priestley449a0ba2011-04-22 06:20:57 -0300225 u8 pid_len = pid_no + 2;
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300226 int ret = 0;
227 deb_info(1, "PID Setting Pid %04x", pid_out);
228
Malcolm Priestley449a0ba2011-04-22 06:20:57 -0300229 if (st->pid_size == 0)
230 ret |= lme2510_stream_restart(d);
231
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300232 pid_buff[2] = pid_no;
233 pid_buff[3] = (u8)pid_out & 0xff;
234 pid_buff[4] = pid_no + 1;
235 pid_buff[5] = (u8)(pid_out >> 8);
236
Malcolm Priestley449a0ba2011-04-22 06:20:57 -0300237 if (pid_len > st->pid_size)
238 st->pid_size = pid_len;
239 pid_buff[7] = 0x80 + st->pid_size;
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300240
241 ret |= lme2510_usb_talk(d, pid_buff ,
242 sizeof(pid_buff) , rbuf, sizeof(rbuf));
243
Malcolm Priestley449a0ba2011-04-22 06:20:57 -0300244 if (st->stream_on)
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300245 ret |= lme2510_stream_restart(d);
246
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300247 return ret;
248}
249
Mauro Carvalho Chehabfaebbd82015-06-23 12:29:46 -0300250/* Convert range from 0x00-0xff to 0x0000-0xffff */
251#define reg_to_16bits(x) ((x) | ((x) << 8))
252
Malcolm Priestley48c91aa2015-06-08 19:05:20 -0300253static void lme2510_update_stats(struct dvb_usb_adapter *adap)
254{
255 struct lme2510_state *st = adap_to_priv(adap);
256 struct dvb_frontend *fe = adap->fe[0];
257 struct dtv_frontend_properties *c;
Mauro Carvalho Chehab5ae65db2015-06-23 12:22:06 -0300258 u32 s_tmp = 0, c_tmp = 0;
Malcolm Priestley48c91aa2015-06-08 19:05:20 -0300259
260 if (!fe)
261 return;
262
263 c = &fe->dtv_property_cache;
264
265 c->block_count.len = 1;
266 c->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
267 c->block_error.len = 1;
268 c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
269 c->post_bit_count.len = 1;
270 c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
271 c->post_bit_error.len = 1;
272 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
273
274 if (st->i2c_talk_onoff) {
275 c->strength.len = 1;
276 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
277 c->cnr.len = 1;
278 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
279 return;
280 }
281
282 switch (st->tuner_config) {
283 case TUNER_LG:
Mauro Carvalho Chehabfaebbd82015-06-23 12:29:46 -0300284 s_tmp = reg_to_16bits(0xff - st->signal_level);
285 c_tmp = reg_to_16bits(0xff - st->signal_sn);
Malcolm Priestley48c91aa2015-06-08 19:05:20 -0300286 break;
Malcolm Priestley48c91aa2015-06-08 19:05:20 -0300287 case TUNER_S7395:
288 case TUNER_S0194:
289 s_tmp = 0xffff - (((st->signal_level * 2) << 8) * 5 / 4);
Mauro Carvalho Chehabfaebbd82015-06-23 12:29:46 -0300290 c_tmp = reg_to_16bits((0xff - st->signal_sn - 0xa1) * 3);
Malcolm Priestley48c91aa2015-06-08 19:05:20 -0300291 break;
292 case TUNER_RS2000:
Mauro Carvalho Chehabfaebbd82015-06-23 12:29:46 -0300293 s_tmp = reg_to_16bits(st->signal_level);
294 c_tmp = reg_to_16bits(st->signal_sn);
Malcolm Priestley48c91aa2015-06-08 19:05:20 -0300295 }
296
297 c->strength.len = 1;
298 c->strength.stat[0].scale = FE_SCALE_RELATIVE;
Mauro Carvalho Chehab5ae65db2015-06-23 12:22:06 -0300299 c->strength.stat[0].uvalue = (u64)s_tmp;
Malcolm Priestley48c91aa2015-06-08 19:05:20 -0300300
301 c->cnr.len = 1;
302 c->cnr.stat[0].scale = FE_SCALE_RELATIVE;
Mauro Carvalho Chehab5ae65db2015-06-23 12:22:06 -0300303 c->cnr.stat[0].uvalue = (u64)c_tmp;
Malcolm Priestley48c91aa2015-06-08 19:05:20 -0300304}
305
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300306static void lme2510_int_response(struct urb *lme_urb)
307{
308 struct dvb_usb_adapter *adap = lme_urb->context;
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300309 struct lme2510_state *st = adap_to_priv(adap);
Rasmus Villemoes75759e32016-11-30 19:39:10 -0200310 u8 *ibuf, *rbuf;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300311 int i = 0, offset;
Malcolm Priestley616a4b82011-05-25 18:28:29 -0300312 u32 key;
Malcolm Priestley743ab6a12015-01-02 10:56:30 -0300313 u8 signal_lock = 0;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300314
315 switch (lme_urb->status) {
316 case 0:
317 case -ETIMEDOUT:
318 break;
319 case -ECONNRESET:
320 case -ENOENT:
321 case -ESHUTDOWN:
322 return;
323 default:
324 info("Error %x", lme_urb->status);
325 break;
326 }
327
328 rbuf = (u8 *) lme_urb->transfer_buffer;
329
330 offset = ((lme_urb->actual_length/8) > 4)
331 ? 4 : (lme_urb->actual_length/8) ;
332
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300333 for (i = 0; i < offset; ++i) {
334 ibuf = (u8 *)&rbuf[i*8];
335 deb_info(5, "INT O/S C =%02x C/O=%02x Type =%02x%02x",
336 offset, i, ibuf[0], ibuf[1]);
337
338 switch (ibuf[0]) {
339 case 0xaa:
Malcolm Priestley616a4b82011-05-25 18:28:29 -0300340 debug_data_snipet(1, "INT Remote data snipet", ibuf);
David Härdemanf459aec2015-05-19 19:03:27 -0300341 if (!adap_to_d(adap)->rc_dev)
342 break;
343
344 key = RC_SCANCODE_NEC32(ibuf[2] << 24 |
345 ibuf[3] << 16 |
346 ibuf[4] << 8 |
347 ibuf[5]);
348
349 deb_info(1, "INT Key = 0x%08x", key);
Sean Young2ceeca02016-09-21 06:54:19 -0300350 rc_keydown(adap_to_d(adap)->rc_dev, RC_TYPE_NEC32, key,
351 0);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300352 break;
353 case 0xbb:
354 switch (st->tuner_config) {
355 case TUNER_LG:
Malcolm Priestley743ab6a12015-01-02 10:56:30 -0300356 signal_lock = ibuf[2] & BIT(5);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300357 st->signal_level = ibuf[4];
358 st->signal_sn = ibuf[3];
359 st->time_key = ibuf[7];
360 break;
361 case TUNER_S7395:
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300362 case TUNER_S0194:
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300363 /* Tweak for earlier firmware*/
364 if (ibuf[1] == 0x03) {
Malcolm Priestley743ab6a12015-01-02 10:56:30 -0300365 signal_lock = ibuf[2] & BIT(4);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300366 st->signal_level = ibuf[3];
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300367 st->signal_sn = ibuf[4];
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300368 } else {
369 st->signal_level = ibuf[4];
370 st->signal_sn = ibuf[5];
371 }
372 break;
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300373 case TUNER_RS2000:
Malcolm Priestley743ab6a12015-01-02 10:56:30 -0300374 signal_lock = ibuf[2] & 0xee;
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300375 st->signal_level = ibuf[5];
376 st->signal_sn = ibuf[4];
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300377 st->time_key = ibuf[7];
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300378 default:
379 break;
380 }
Malcolm Priestley743ab6a12015-01-02 10:56:30 -0300381
382 /* Interrupt will also throw just BIT 0 as lock */
383 signal_lock |= ibuf[2] & BIT(0);
384
385 if (!signal_lock)
386 st->lock_status &= ~FE_HAS_LOCK;
387
Malcolm Priestley48c91aa2015-06-08 19:05:20 -0300388 lme2510_update_stats(adap);
389
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300390 debug_data_snipet(5, "INT Remote data snipet in", ibuf);
391 break;
392 case 0xcc:
393 debug_data_snipet(1, "INT Control data snipet", ibuf);
394 break;
395 default:
396 debug_data_snipet(1, "INT Unknown data snipet", ibuf);
397 break;
398 }
399 }
Malcolm Priestley3a3702222014-05-24 06:35:57 -0300400
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300401 usb_submit_urb(lme_urb, GFP_ATOMIC);
Malcolm Priestley3a3702222014-05-24 06:35:57 -0300402
Malcolm Priestleycfcd7b82015-01-02 10:56:27 -0300403 /* Interrupt urb is due every 48 msecs while streaming the buffer
404 * stores up to 4 periods if missed. Allow 200 msec for next interrupt.
405 */
406 st->int_urb_due = jiffies + msecs_to_jiffies(200);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300407}
408
409static int lme2510_int_read(struct dvb_usb_adapter *adap)
410{
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300411 struct dvb_usb_device *d = adap_to_d(adap);
412 struct lme2510_state *lme_int = adap_to_priv(adap);
Malcolm Priestley15e1ce32015-01-02 10:56:28 -0300413 struct usb_host_endpoint *ep;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300414
415 lme_int->lme_urb = usb_alloc_urb(0, GFP_ATOMIC);
416
417 if (lme_int->lme_urb == NULL)
418 return -ENOMEM;
419
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300420 lme_int->buffer = usb_alloc_coherent(d->udev, 128, GFP_ATOMIC,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300421 &lme_int->lme_urb->transfer_dma);
422
423 if (lme_int->buffer == NULL)
424 return -ENOMEM;
425
426 usb_fill_int_urb(lme_int->lme_urb,
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300427 d->udev,
428 usb_rcvintpipe(d->udev, 0xa),
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300429 lme_int->buffer,
tvboxspy95c21c42011-09-11 19:26:50 -0300430 128,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300431 lme2510_int_response,
432 adap,
tvboxspy95c21c42011-09-11 19:26:50 -0300433 8);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300434
Malcolm Priestley15e1ce32015-01-02 10:56:28 -0300435 /* Quirk of pipe reporting PIPE_BULK but behaves as interrupt */
436 ep = usb_pipe_endpoint(d->udev, lme_int->lme_urb->pipe);
437
438 if (usb_endpoint_type(&ep->desc) == USB_ENDPOINT_XFER_BULK)
439 lme_int->lme_urb->pipe = usb_rcvbulkpipe(d->udev, 0xa),
440
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300441 lme_int->lme_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
442
443 usb_submit_urb(lme_int->lme_urb, GFP_ATOMIC);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300444 info("INT Interrupt Service Started");
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300445
446 return 0;
447}
448
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300449static int lme2510_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
450{
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300451 struct dvb_usb_device *d = adap_to_d(adap);
452 struct lme2510_state *st = adap_to_priv(adap);
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300453 static u8 clear_pid_reg[] = LME_ALL_PIDS;
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300454 static u8 rbuf[1];
Il Hanb7dc4cd2012-05-07 12:04:42 -0300455 int ret = 0;
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300456
457 deb_info(1, "PID Clearing Filter");
458
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300459 mutex_lock(&d->i2c_mutex);
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300460
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300461 if (!onoff) {
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300462 ret |= lme2510_usb_talk(d, clear_pid_reg,
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300463 sizeof(clear_pid_reg), rbuf, sizeof(rbuf));
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300464 st->pid_off = true;
465 } else
466 st->pid_off = false;
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300467
Malcolm Priestley449a0ba2011-04-22 06:20:57 -0300468 st->pid_size = 0;
469
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300470 mutex_unlock(&d->i2c_mutex);
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300471
472 return 0;
473}
474
475static int lme2510_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
476 int onoff)
477{
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300478 struct dvb_usb_device *d = adap_to_d(adap);
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300479 int ret = 0;
480
481 deb_info(3, "%s PID=%04x Index=%04x onoff=%02x", __func__,
482 pid, index, onoff);
483
Malcolm Priestley4c316b52011-12-30 11:33:03 -0300484 if (onoff) {
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300485 mutex_lock(&d->i2c_mutex);
486 ret |= lme2510_enable_pid(d, index, pid);
487 mutex_unlock(&d->i2c_mutex);
Malcolm Priestley449a0ba2011-04-22 06:20:57 -0300488 }
489
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300490
491 return ret;
492}
493
494
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300495static int lme2510_return_status(struct dvb_usb_device *d)
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300496{
497 int ret = 0;
Florian Mickler945b8762011-03-20 18:50:52 -0300498 u8 *data;
499
500 data = kzalloc(10, GFP_KERNEL);
501 if (!data)
502 return -ENOMEM;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300503
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300504 ret |= usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0),
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300505 0x06, 0x80, 0x0302, 0x00, data, 0x0006, 200);
506 info("Firmware Status: %x (%x)", ret , data[2]);
507
Florian Mickler945b8762011-03-20 18:50:52 -0300508 ret = (ret < 0) ? -ENODEV : data[2];
509 kfree(data);
510 return ret;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300511}
512
513static int lme2510_msg(struct dvb_usb_device *d,
514 u8 *wbuf, int wlen, u8 *rbuf, int rlen)
515{
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300516 struct lme2510_state *st = d->priv;
517
Malcolm Priestley743ab6a12015-01-02 10:56:30 -0300518 st->i2c_talk_onoff = 1;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300519
Malcolm Priestley743ab6a12015-01-02 10:56:30 -0300520 return lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300521}
522
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300523static int lme2510_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
524 int num)
525{
526 struct dvb_usb_device *d = i2c_get_adapdata(adap);
527 struct lme2510_state *st = d->priv;
tvboxspy810fda92011-09-11 19:30:10 -0300528 static u8 obuf[64], ibuf[64];
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300529 int i, read, read_o;
530 u16 len;
531 u8 gate = st->i2c_gate;
532
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300533 mutex_lock(&d->i2c_mutex);
534
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300535 if (gate == 0)
536 gate = 5;
537
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300538 for (i = 0; i < num; i++) {
Malcolm Priestley71dc98b2012-12-29 07:34:24 -0300539 read_o = msg[i].flags & I2C_M_RD;
540 read = i + 1 < num && msg[i + 1].flags & I2C_M_RD;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300541 read |= read_o;
542 gate = (msg[i].addr == st->i2c_tuner_addr)
543 ? (read) ? st->i2c_tuner_gate_r
544 : st->i2c_tuner_gate_w
545 : st->i2c_gate;
546 obuf[0] = gate | (read << 7);
547
548 if (gate == 5)
549 obuf[1] = (read) ? 2 : msg[i].len + 1;
550 else
551 obuf[1] = msg[i].len + read + 1;
552
Malcolm Priestley71dc98b2012-12-29 07:34:24 -0300553 obuf[2] = msg[i].addr << 1;
554
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300555 if (read) {
556 if (read_o)
557 len = 3;
558 else {
559 memcpy(&obuf[3], msg[i].buf, msg[i].len);
560 obuf[msg[i].len+3] = msg[i+1].len;
561 len = msg[i].len+4;
562 }
563 } else {
564 memcpy(&obuf[3], msg[i].buf, msg[i].len);
565 len = msg[i].len+3;
566 }
567
tvboxspy810fda92011-09-11 19:30:10 -0300568 if (lme2510_msg(d, obuf, len, ibuf, 64) < 0) {
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300569 deb_info(1, "i2c transfer failed.");
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300570 mutex_unlock(&d->i2c_mutex);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300571 return -EAGAIN;
572 }
573
574 if (read) {
575 if (read_o)
576 memcpy(msg[i].buf, &ibuf[1], msg[i].len);
577 else {
578 memcpy(msg[i+1].buf, &ibuf[1], msg[i+1].len);
579 i++;
580 }
581 }
582 }
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300583
584 mutex_unlock(&d->i2c_mutex);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300585 return i;
586}
587
588static u32 lme2510_i2c_func(struct i2c_adapter *adapter)
589{
590 return I2C_FUNC_I2C;
591}
592
593static struct i2c_algorithm lme2510_i2c_algo = {
594 .master_xfer = lme2510_i2c_xfer,
595 .functionality = lme2510_i2c_func,
596};
597
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300598static int lme2510_streaming_ctrl(struct dvb_frontend *fe, int onoff)
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300599{
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300600 struct dvb_usb_adapter *adap = fe_to_adap(fe);
601 struct dvb_usb_device *d = adap_to_d(adap);
602 struct lme2510_state *st = adap_to_priv(adap);
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300603 static u8 clear_reg_3[] = LME_ALL_PIDS;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300604 static u8 rbuf[1];
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300605 int ret = 0, rlen = sizeof(rbuf);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300606
607 deb_info(1, "STM (%02x)", onoff);
608
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300609 /* Streaming is started by FE_HAS_LOCK */
610 if (onoff == 1)
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300611 st->stream_on = 1;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300612 else {
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300613 deb_info(1, "STM Steam Off");
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300614 /* mutex is here only to avoid collision with I2C */
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300615 mutex_lock(&d->i2c_mutex);
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300616
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300617 ret = lme2510_usb_talk(d, clear_reg_3,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300618 sizeof(clear_reg_3), rbuf, rlen);
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300619 st->stream_on = 0;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300620 st->i2c_talk_onoff = 1;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300621
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300622 mutex_unlock(&d->i2c_mutex);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300623 }
624
625 return (ret < 0) ? -ENODEV : 0;
626}
627
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300628static u8 check_sum(u8 *p, u8 len)
629{
630 u8 sum = 0;
631 while (len--)
632 sum += *p++;
633 return sum;
634}
635
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300636static int lme2510_download_firmware(struct dvb_usb_device *d,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300637 const struct firmware *fw)
638{
639 int ret = 0;
Florian Mickler945b8762011-03-20 18:50:52 -0300640 u8 *data;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300641 u16 j, wlen, len_in, start, end;
642 u8 packet_size, dlen, i;
643 u8 *fw_data;
644
645 packet_size = 0x31;
646 len_in = 1;
647
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300648 data = kzalloc(128, GFP_KERNEL);
Florian Mickler945b8762011-03-20 18:50:52 -0300649 if (!data) {
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300650 info("FRM Could not start Firmware Download"\
651 "(Buffer allocation failed)");
Florian Mickler945b8762011-03-20 18:50:52 -0300652 return -ENOMEM;
653 }
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300654
655 info("FRM Starting Firmware Download");
656
657 for (i = 1; i < 3; i++) {
658 start = (i == 1) ? 0 : 512;
659 end = (i == 1) ? 512 : fw->size;
660 for (j = start; j < end; j += (packet_size+1)) {
661 fw_data = (u8 *)(fw->data + j);
662 if ((end - j) > packet_size) {
663 data[0] = i;
664 dlen = packet_size;
665 } else {
666 data[0] = i | 0x80;
667 dlen = (u8)(end - j)-1;
668 }
Florian Micklerb9f7b732011-03-20 18:50:51 -0300669 data[1] = dlen;
670 memcpy(&data[2], fw_data, dlen+1);
671 wlen = (u8) dlen + 4;
672 data[wlen-1] = check_sum(fw_data, dlen+1);
673 deb_info(1, "Data S=%02x:E=%02x CS= %02x", data[3],
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300674 data[dlen+2], data[dlen+3]);
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300675 lme2510_usb_talk(d, data, wlen, data, len_in);
Florian Micklerb9f7b732011-03-20 18:50:51 -0300676 ret |= (data[0] == 0x88) ? 0 : -1;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300677 }
678 }
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300679
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300680 data[0] = 0x8a;
681 len_in = 1;
682 msleep(2000);
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300683 lme2510_usb_talk(d, data, len_in, data, len_in);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300684 msleep(400);
685
686 if (ret < 0)
687 info("FRM Firmware Download Failed (%04x)" , ret);
688 else
689 info("FRM Firmware Download Completed - Resetting Device");
690
Florian Mickler945b8762011-03-20 18:50:52 -0300691 kfree(data);
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300692 return RECONNECTS_USB;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300693}
694
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300695static void lme_coldreset(struct dvb_usb_device *d)
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300696{
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300697 u8 data[1] = {0};
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300698 data[0] = 0x0a;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300699 info("FRM Firmware Cold Reset");
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300700
701 lme2510_usb_talk(d, data, sizeof(data), data, sizeof(data));
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300702
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300703 return;
704}
705
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300706static const char fw_c_s7395[] = LME2510_C_S7395;
707static const char fw_c_lg[] = LME2510_C_LG;
708static const char fw_c_s0194[] = LME2510_C_S0194;
709static const char fw_c_rs2000[] = LME2510_C_RS2000;
710static const char fw_lg[] = LME2510_LG;
711static const char fw_s0194[] = LME2510_S0194;
712
Mauro Carvalho Chehab1885fd22012-10-27 14:11:15 -0300713static const char *lme_firmware_switch(struct dvb_usb_device *d, int cold)
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300714{
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300715 struct lme2510_state *st = d->priv;
716 struct usb_device *udev = d->udev;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300717 const struct firmware *fw = NULL;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300718 const char *fw_lme;
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300719 int ret = 0;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300720
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300721 cold = (cold > 0) ? (cold & 1) : 0;
722
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300723 switch (le16_to_cpu(udev->descriptor.idProduct)) {
724 case 0x1122:
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300725 switch (st->dvb_usb_lme2510_firmware) {
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300726 default:
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300727 case TUNER_S0194:
728 fw_lme = fw_s0194;
729 ret = request_firmware(&fw, fw_lme, &udev->dev);
730 if (ret == 0) {
Mauro Carvalho Chehab504febc2014-11-25 15:46:13 -0200731 st->dvb_usb_lme2510_firmware = TUNER_S0194;
Malcolm Priestley30bd3b52011-02-13 20:12:15 -0300732 cold = 0;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300733 break;
734 }
Mauro Carvalho Chehab504febc2014-11-25 15:46:13 -0200735 /* fall through */
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300736 case TUNER_LG:
737 fw_lme = fw_lg;
738 ret = request_firmware(&fw, fw_lme, &udev->dev);
Mauro Carvalho Chehab504febc2014-11-25 15:46:13 -0200739 if (ret == 0) {
740 st->dvb_usb_lme2510_firmware = TUNER_LG;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300741 break;
Mauro Carvalho Chehab504febc2014-11-25 15:46:13 -0200742 }
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300743 st->dvb_usb_lme2510_firmware = TUNER_DEFAULT;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300744 break;
745 }
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300746 break;
747 case 0x1120:
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300748 switch (st->dvb_usb_lme2510_firmware) {
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300749 default:
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300750 case TUNER_S7395:
751 fw_lme = fw_c_s7395;
752 ret = request_firmware(&fw, fw_lme, &udev->dev);
Malcolm Priestley30bd3b52011-02-13 20:12:15 -0300753 if (ret == 0) {
Mauro Carvalho Chehab504febc2014-11-25 15:46:13 -0200754 st->dvb_usb_lme2510_firmware = TUNER_S7395;
Malcolm Priestley30bd3b52011-02-13 20:12:15 -0300755 cold = 0;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300756 break;
Malcolm Priestley30bd3b52011-02-13 20:12:15 -0300757 }
Mauro Carvalho Chehab504febc2014-11-25 15:46:13 -0200758 /* fall through */
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300759 case TUNER_LG:
760 fw_lme = fw_c_lg;
761 ret = request_firmware(&fw, fw_lme, &udev->dev);
Mauro Carvalho Chehab504febc2014-11-25 15:46:13 -0200762 if (ret == 0) {
763 st->dvb_usb_lme2510_firmware = TUNER_LG;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300764 break;
Mauro Carvalho Chehab504febc2014-11-25 15:46:13 -0200765 }
766 /* fall through */
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300767 case TUNER_S0194:
768 fw_lme = fw_c_s0194;
769 ret = request_firmware(&fw, fw_lme, &udev->dev);
Mauro Carvalho Chehab504febc2014-11-25 15:46:13 -0200770 if (ret == 0) {
771 st->dvb_usb_lme2510_firmware = TUNER_S0194;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300772 break;
Mauro Carvalho Chehab504febc2014-11-25 15:46:13 -0200773 }
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300774 st->dvb_usb_lme2510_firmware = TUNER_DEFAULT;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300775 cold = 0;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300776 break;
777 }
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300778 break;
779 case 0x22f0:
780 fw_lme = fw_c_rs2000;
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300781 st->dvb_usb_lme2510_firmware = TUNER_RS2000;
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300782 break;
783 default:
784 fw_lme = fw_c_s7395;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300785 }
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300786
Malcolm Priestley30bd3b52011-02-13 20:12:15 -0300787 release_firmware(fw);
788
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300789 if (cold) {
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300790 dvb_usb_lme2510_firmware = st->dvb_usb_lme2510_firmware;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300791 info("FRM Changing to %s firmware", fw_lme);
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300792 lme_coldreset(d);
793 return NULL;
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300794 }
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300795
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300796 return fw_lme;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300797}
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300798
799static int lme2510_kill_urb(struct usb_data_stream *stream)
800{
801 int i;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300802
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300803 for (i = 0; i < stream->urbs_submitted; i++) {
804 deb_info(3, "killing URB no. %d.", i);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300805 /* stop the URB */
806 usb_kill_urb(stream->urb_list[i]);
807 }
808 stream->urbs_submitted = 0;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300809
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300810 return 0;
811}
812
813static struct tda10086_config tda10086_config = {
Malcolm Priestley71dc98b2012-12-29 07:34:24 -0300814 .demod_address = 0x0e,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300815 .invert = 0,
816 .diseqc_tone = 1,
817 .xtal_freq = TDA10086_XTAL_16M,
818};
819
820static struct stv0288_config lme_config = {
Malcolm Priestley71dc98b2012-12-29 07:34:24 -0300821 .demod_address = 0x68,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300822 .min_delay_ms = 15,
823 .inittab = s7395_inittab,
824};
825
826static struct ix2505v_config lme_tuner = {
Malcolm Priestley71dc98b2012-12-29 07:34:24 -0300827 .tuner_address = 0x60,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300828 .min_delay_ms = 100,
829 .tuner_gain = 0x0,
830 .tuner_chargepump = 0x3,
831};
832
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300833static struct stv0299_config sharp_z0194_config = {
Malcolm Priestley71dc98b2012-12-29 07:34:24 -0300834 .demod_address = 0x68,
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300835 .inittab = sharp_z0194a_inittab,
836 .mclk = 88000000UL,
837 .invert = 0,
838 .skip_reinit = 0,
839 .lock_output = STV0299_LOCKOUTPUT_1,
840 .volt13_op0_op1 = STV0299_VOLT13_OP1,
841 .min_delay_ms = 100,
842 .set_symbol_rate = sharp_z0194a_set_symbol_rate,
843};
844
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300845static struct m88rs2000_config m88rs2000_config = {
Malcolm Priestley743ab6a12015-01-02 10:56:30 -0300846 .demod_addr = 0x68
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300847};
848
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300849static struct ts2020_config ts2020_config = {
850 .tuner_address = 0x60,
851 .clk_out_div = 7,
Malcolm Priestley9e8715c2015-06-05 07:55:07 -0300852 .dont_poll = true
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300853};
854
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300855static int dm04_lme2510_set_voltage(struct dvb_frontend *fe,
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300856 enum fe_sec_voltage voltage)
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300857{
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300858 struct dvb_usb_device *d = fe_to_d(fe);
859 struct lme2510_state *st = fe_to_priv(fe);
860 static u8 voltage_low[] = LME_VOLTAGE_L;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300861 static u8 voltage_high[] = LME_VOLTAGE_H;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300862 static u8 rbuf[1];
863 int ret = 0, len = 3, rlen = 1;
864
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300865 mutex_lock(&d->i2c_mutex);
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300866
867 switch (voltage) {
868 case SEC_VOLTAGE_18:
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300869 ret |= lme2510_usb_talk(d,
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300870 voltage_high, len, rbuf, rlen);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300871 break;
872
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300873 case SEC_VOLTAGE_OFF:
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300874 case SEC_VOLTAGE_13:
875 default:
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300876 ret |= lme2510_usb_talk(d,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300877 voltage_low, len, rbuf, rlen);
878 break;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300879 }
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300880
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300881 mutex_unlock(&d->i2c_mutex);
882
883 if (st->tuner_config == TUNER_RS2000)
884 if (st->fe_set_voltage)
885 st->fe_set_voltage(fe, voltage);
886
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300887
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300888 return (ret < 0) ? -ENODEV : 0;
889}
890
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300891static int dm04_read_status(struct dvb_frontend *fe, enum fe_status *status)
Malcolm Priestley743ab6a12015-01-02 10:56:30 -0300892{
893 struct dvb_usb_device *d = fe_to_d(fe);
894 struct lme2510_state *st = d->priv;
895 int ret = 0;
896
897 if (st->i2c_talk_onoff) {
898 if (st->fe_read_status) {
899 ret = st->fe_read_status(fe, status);
900 if (ret < 0)
901 return ret;
902 }
903
904 st->lock_status = *status;
905
906 if (*status & FE_HAS_LOCK && st->stream_on) {
907 mutex_lock(&d->i2c_mutex);
908
909 st->i2c_talk_onoff = 0;
910 ret = lme2510_stream_restart(d);
911
912 mutex_unlock(&d->i2c_mutex);
913 }
914
915 return ret;
916 }
917
918 /* Timeout of interrupt reached on RS2000 */
919 if (st->tuner_config == TUNER_RS2000 &&
920 time_after(jiffies, st->int_urb_due))
921 st->lock_status &= ~FE_HAS_LOCK;
922
923 *status = st->lock_status;
924
Malcolm Priestley48c91aa2015-06-08 19:05:20 -0300925 if (!(*status & FE_HAS_LOCK)) {
926 struct dvb_usb_adapter *adap = fe_to_adap(fe);
927
Malcolm Priestley743ab6a12015-01-02 10:56:30 -0300928 st->i2c_talk_onoff = 1;
929
Malcolm Priestley48c91aa2015-06-08 19:05:20 -0300930 lme2510_update_stats(adap);
931 }
932
Malcolm Priestley743ab6a12015-01-02 10:56:30 -0300933 return ret;
934}
935
Malcolm Priestleyd8bad1e2015-01-02 10:56:29 -0300936static int dm04_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300937{
Malcolm Priestley48c91aa2015-06-08 19:05:20 -0300938 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300939 struct lme2510_state *st = fe_to_priv(fe);
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300940
Malcolm Priestley87169012015-01-02 10:56:31 -0300941 if (st->fe_read_signal_strength && !st->stream_on)
942 return st->fe_read_signal_strength(fe, strength);
943
Malcolm Priestley48c91aa2015-06-08 19:05:20 -0300944 if (c->strength.stat[0].scale == FE_SCALE_RELATIVE)
945 *strength = (u16)c->strength.stat[0].uvalue;
946 else
947 *strength = 0;
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300948
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300949 return 0;
950}
951
Malcolm Priestleyd8bad1e2015-01-02 10:56:29 -0300952static int dm04_read_snr(struct dvb_frontend *fe, u16 *snr)
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300953{
Malcolm Priestley48c91aa2015-06-08 19:05:20 -0300954 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300955 struct lme2510_state *st = fe_to_priv(fe);
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300956
Malcolm Priestley87169012015-01-02 10:56:31 -0300957 if (st->fe_read_snr && !st->stream_on)
958 return st->fe_read_snr(fe, snr);
959
Malcolm Priestley48c91aa2015-06-08 19:05:20 -0300960 if (c->cnr.stat[0].scale == FE_SCALE_RELATIVE)
961 *snr = (u16)c->cnr.stat[0].uvalue;
962 else
963 *snr = 0;
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300964
965 return 0;
966}
967
968static int dm04_read_ber(struct dvb_frontend *fe, u32 *ber)
969{
Malcolm Priestley87169012015-01-02 10:56:31 -0300970 struct lme2510_state *st = fe_to_priv(fe);
971
972 if (st->fe_read_ber && !st->stream_on)
973 return st->fe_read_ber(fe, ber);
974
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300975 *ber = 0;
976
977 return 0;
978}
979
980static int dm04_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
981{
Malcolm Priestley87169012015-01-02 10:56:31 -0300982 struct lme2510_state *st = fe_to_priv(fe);
983
984 if (st->fe_read_ucblocks && !st->stream_on)
985 return st->fe_read_ucblocks(fe, ucblocks);
986
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300987 *ucblocks = 0;
988
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300989 return 0;
990}
991
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300992static int lme_name(struct dvb_usb_adapter *adap)
993{
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300994 struct dvb_usb_device *d = adap_to_d(adap);
995 struct lme2510_state *st = adap_to_priv(adap);
996 const char *desc = d->name;
Rasmus Villemoesbfe17172016-11-30 19:39:11 -0200997 static const char * const fe_name[] = {
998 "", " LG TDQY-P001F", " SHARP:BS2F7HZ7395",
999 " SHARP:BS2F7HZ0194", " RS2000"};
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001000 char *name = adap->fe[0]->ops.info.name;
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001001
1002 strlcpy(name, desc, 128);
1003 strlcat(name, fe_name[st->tuner_config], 128);
1004
1005 return 0;
1006}
1007
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001008static int dm04_lme2510_frontend_attach(struct dvb_usb_adapter *adap)
1009{
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001010 struct dvb_usb_device *d = adap_to_d(adap);
1011 struct lme2510_state *st = d->priv;
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001012 int ret = 0;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001013
1014 st->i2c_talk_onoff = 1;
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001015 switch (le16_to_cpu(d->udev->descriptor.idProduct)) {
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001016 case 0x1122:
1017 case 0x1120:
1018 st->i2c_gate = 4;
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001019 adap->fe[0] = dvb_attach(tda10086_attach,
1020 &tda10086_config, &d->i2c_adap);
1021 if (adap->fe[0]) {
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001022 info("TUN Found Frontend TDA10086");
1023 st->i2c_tuner_gate_w = 4;
1024 st->i2c_tuner_gate_r = 4;
Malcolm Priestley71dc98b2012-12-29 07:34:24 -03001025 st->i2c_tuner_addr = 0x60;
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001026 st->tuner_config = TUNER_LG;
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001027 if (st->dvb_usb_lme2510_firmware != TUNER_LG) {
1028 st->dvb_usb_lme2510_firmware = TUNER_LG;
1029 ret = lme_firmware_switch(d, 1) ? 0 : -ENODEV;
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001030 }
1031 break;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -03001032 }
Malcolm Priestley9d4540c2011-02-12 22:35:08 -03001033
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001034 st->i2c_gate = 4;
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001035 adap->fe[0] = dvb_attach(stv0299_attach,
1036 &sharp_z0194_config, &d->i2c_adap);
1037 if (adap->fe[0]) {
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001038 info("FE Found Stv0299");
1039 st->i2c_tuner_gate_w = 4;
1040 st->i2c_tuner_gate_r = 5;
Malcolm Priestley71dc98b2012-12-29 07:34:24 -03001041 st->i2c_tuner_addr = 0x60;
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001042 st->tuner_config = TUNER_S0194;
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001043 if (st->dvb_usb_lme2510_firmware != TUNER_S0194) {
1044 st->dvb_usb_lme2510_firmware = TUNER_S0194;
1045 ret = lme_firmware_switch(d, 1) ? 0 : -ENODEV;
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001046 }
1047 break;
1048 }
1049
1050 st->i2c_gate = 5;
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001051 adap->fe[0] = dvb_attach(stv0288_attach, &lme_config,
1052 &d->i2c_adap);
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001053
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001054 if (adap->fe[0]) {
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001055 info("FE Found Stv0288");
1056 st->i2c_tuner_gate_w = 4;
1057 st->i2c_tuner_gate_r = 5;
Malcolm Priestley71dc98b2012-12-29 07:34:24 -03001058 st->i2c_tuner_addr = 0x60;
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001059 st->tuner_config = TUNER_S7395;
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001060 if (st->dvb_usb_lme2510_firmware != TUNER_S7395) {
1061 st->dvb_usb_lme2510_firmware = TUNER_S7395;
1062 ret = lme_firmware_switch(d, 1) ? 0 : -ENODEV;
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001063 }
1064 break;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -03001065 }
Mauro Carvalho Chehab06eeefe2017-05-18 08:13:28 -03001066 /* fall through */
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001067 case 0x22f0:
1068 st->i2c_gate = 5;
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001069 adap->fe[0] = dvb_attach(m88rs2000_attach,
1070 &m88rs2000_config, &d->i2c_adap);
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001071
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001072 if (adap->fe[0]) {
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001073 info("FE Found M88RS2000");
Igor M. Liplianinb858c332012-12-28 19:40:33 -03001074 dvb_attach(ts2020_attach, adap->fe[0], &ts2020_config,
1075 &d->i2c_adap);
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001076 st->i2c_tuner_gate_w = 5;
1077 st->i2c_tuner_gate_r = 5;
Malcolm Priestley71dc98b2012-12-29 07:34:24 -03001078 st->i2c_tuner_addr = 0x60;
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001079 st->tuner_config = TUNER_RS2000;
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001080 st->fe_set_voltage =
1081 adap->fe[0]->ops.set_voltage;
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001082 }
1083 break;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001084 }
Malcolm Priestleyf23e6612010-10-27 19:50:36 -03001085
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001086 if (adap->fe[0] == NULL) {
1087 info("DM04/QQBOX Not Powered up or not Supported");
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001088 return -ENODEV;
1089 }
1090
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001091 if (ret) {
1092 if (adap->fe[0]) {
1093 dvb_frontend_detach(adap->fe[0]);
1094 adap->fe[0] = NULL;
1095 }
1096 d->rc_map = NULL;
1097 return -ENODEV;
1098 }
1099
Malcolm Priestley743ab6a12015-01-02 10:56:30 -03001100 st->fe_read_status = adap->fe[0]->ops.read_status;
Malcolm Priestley87169012015-01-02 10:56:31 -03001101 st->fe_read_signal_strength = adap->fe[0]->ops.read_signal_strength;
1102 st->fe_read_snr = adap->fe[0]->ops.read_snr;
1103 st->fe_read_ber = adap->fe[0]->ops.read_ber;
1104 st->fe_read_ucblocks = adap->fe[0]->ops.read_ucblocks;
Malcolm Priestley743ab6a12015-01-02 10:56:30 -03001105
1106 adap->fe[0]->ops.read_status = dm04_read_status;
Malcolm Priestleyd8bad1e2015-01-02 10:56:29 -03001107 adap->fe[0]->ops.read_signal_strength = dm04_read_signal_strength;
1108 adap->fe[0]->ops.read_snr = dm04_read_snr;
1109 adap->fe[0]->ops.read_ber = dm04_read_ber;
1110 adap->fe[0]->ops.read_ucblocks = dm04_read_ucblocks;
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001111 adap->fe[0]->ops.set_voltage = dm04_lme2510_set_voltage;
Malcolm Priestleyd8bad1e2015-01-02 10:56:29 -03001112
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001113 ret = lme_name(adap);
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001114 return ret;
1115}
1116
1117static int dm04_lme2510_tuner(struct dvb_usb_adapter *adap)
1118{
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001119 struct dvb_usb_device *d = adap_to_d(adap);
1120 struct lme2510_state *st = adap_to_priv(adap);
Rasmus Villemoesbfe17172016-11-30 19:39:11 -02001121 static const char * const tun_msg[] = {"", "TDA8263", "IX2505V", "DVB_PLL_OPERA", "RS2000"};
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001122 int ret = 0;
1123
1124 switch (st->tuner_config) {
1125 case TUNER_LG:
Malcolm Priestley71dc98b2012-12-29 07:34:24 -03001126 if (dvb_attach(tda826x_attach, adap->fe[0], 0x60,
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001127 &d->i2c_adap, 1))
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001128 ret = st->tuner_config;
1129 break;
1130 case TUNER_S7395:
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001131 if (dvb_attach(ix2505v_attach , adap->fe[0], &lme_tuner,
1132 &d->i2c_adap))
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001133 ret = st->tuner_config;
1134 break;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -03001135 case TUNER_S0194:
Malcolm Priestley71dc98b2012-12-29 07:34:24 -03001136 if (dvb_attach(dvb_pll_attach , adap->fe[0], 0x60,
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001137 &d->i2c_adap, DVB_PLL_OPERA1))
Malcolm Priestley9d4540c2011-02-12 22:35:08 -03001138 ret = st->tuner_config;
1139 break;
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001140 case TUNER_RS2000:
1141 ret = st->tuner_config;
1142 break;
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001143 default:
1144 break;
1145 }
1146
1147 if (ret)
1148 info("TUN Found %s tuner", tun_msg[ret]);
1149 else {
Masanari Iida0d616ca2012-01-27 09:47:04 -03001150 info("TUN No tuner found --- resetting device");
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001151 lme_coldreset(d);
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001152 return -ENODEV;
1153 }
1154
Malcolm Priestley616a4b82011-05-25 18:28:29 -03001155 /* Start the Interrupt*/
1156 ret = lme2510_int_read(adap);
1157 if (ret < 0) {
1158 info("INT Unable to start Interrupt Service");
1159 return -ENODEV;
1160 }
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001161
1162 return ret;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001163}
1164
1165static int lme2510_powerup(struct dvb_usb_device *d, int onoff)
1166{
1167 struct lme2510_state *st = d->priv;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -03001168 static u8 lnb_on[] = LNB_ON;
1169 static u8 lnb_off[] = LNB_OFF;
1170 static u8 rbuf[1];
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001171 int ret = 0, len = 3, rlen = 1;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -03001172
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001173 mutex_lock(&d->i2c_mutex);
Malcolm Priestleyf23e6612010-10-27 19:50:36 -03001174
Rasmus Villemoes974eadf2016-11-30 19:39:12 -02001175 ret = lme2510_usb_talk(d, onoff ? lnb_on : lnb_off, len, rbuf, rlen);
Malcolm Priestleyf23e6612010-10-27 19:50:36 -03001176
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001177 st->i2c_talk_onoff = 1;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -03001178
1179 mutex_unlock(&d->i2c_mutex);
1180
1181 return ret;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001182}
1183
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001184static int lme2510_get_adapter_count(struct dvb_usb_device *d)
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001185{
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001186 return 1;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001187}
1188
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001189static int lme2510_identify_state(struct dvb_usb_device *d, const char **name)
1190{
1191 struct lme2510_state *st = d->priv;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001192
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001193 usb_reset_configuration(d->udev);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001194
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001195 usb_set_interface(d->udev,
Antti Palosaarie42efde2013-07-30 16:33:16 -03001196 d->props->bInterfaceNumber, 1);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001197
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001198 st->dvb_usb_lme2510_firmware = dvb_usb_lme2510_firmware;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001199
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001200 if (lme2510_return_status(d) == 0x44) {
1201 *name = lme_firmware_switch(d, 0);
1202 return COLD;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001203 }
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001204
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001205 return 0;
1206}
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001207
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001208static int lme2510_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
1209 struct usb_data_stream_properties *stream)
1210{
1211 struct dvb_usb_adapter *adap = fe_to_adap(fe);
Syam Sidhardhan06f950f2013-02-24 18:47:18 -03001212 struct dvb_usb_device *d;
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001213
1214 if (adap == NULL)
1215 return 0;
Syam Sidhardhan06f950f2013-02-24 18:47:18 -03001216
1217 d = adap_to_d(adap);
1218
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001219 /* Turn PID filter on the fly by module option */
1220 if (pid_filter == 2) {
Mauro Carvalho Chehab235d89e2014-09-03 16:18:17 -03001221 adap->pid_filtering = true;
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001222 adap->max_feed_count = 15;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001223 }
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001224
1225 if (!(le16_to_cpu(d->udev->descriptor.idProduct)
1226 == 0x1122))
1227 stream->endpoint = 0x8;
1228
1229 return 0;
1230}
1231
1232static int lme2510_get_rc_config(struct dvb_usb_device *d,
1233 struct dvb_usb_rc *rc)
1234{
Sean Young2ceeca02016-09-21 06:54:19 -03001235 rc->allowed_protos = RC_BIT_NEC32;
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001236 return 0;
1237}
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001238
Malcolm Priestleya760d632011-04-22 06:07:40 -03001239static void *lme2510_exit_int(struct dvb_usb_device *d)
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001240{
1241 struct lme2510_state *st = d->priv;
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001242 struct dvb_usb_adapter *adap = &d->adapter[0];
1243 void *buffer = NULL;
1244
1245 if (adap != NULL) {
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001246 lme2510_kill_urb(&adap->stream);
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001247 }
1248
Malcolm Priestley616a4b82011-05-25 18:28:29 -03001249 if (st->usb_buffer != NULL) {
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001250 st->i2c_talk_onoff = 1;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001251 st->signal_level = 0;
1252 st->signal_sn = 0;
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001253 buffer = st->usb_buffer;
Malcolm Priestley616a4b82011-05-25 18:28:29 -03001254 }
1255
1256 if (st->lme_urb != NULL) {
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001257 usb_kill_urb(st->lme_urb);
tvboxspy95c21c42011-09-11 19:26:50 -03001258 usb_free_coherent(d->udev, 128, st->buffer,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001259 st->lme_urb->transfer_dma);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001260 info("Interrupt Service Stopped");
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001261 }
Malcolm Priestley616a4b82011-05-25 18:28:29 -03001262
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001263 return buffer;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001264}
1265
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001266static void lme2510_exit(struct dvb_usb_device *d)
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001267{
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001268 void *usb_buffer;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001269
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001270 if (d != NULL) {
1271 usb_buffer = lme2510_exit_int(d);
Syam Sidhardhan72873e52013-03-06 16:44:46 -03001272 kfree(usb_buffer);
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001273 }
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001274}
1275
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001276static struct dvb_usb_device_properties lme2510_props = {
1277 .driver_name = KBUILD_MODNAME,
1278 .owner = THIS_MODULE,
1279 .bInterfaceNumber = 0,
1280 .adapter_nr = adapter_nr,
1281 .size_of_priv = sizeof(struct lme2510_state),
1282
1283 .download_firmware = lme2510_download_firmware,
1284
1285 .power_ctrl = lme2510_powerup,
1286 .identify_state = lme2510_identify_state,
1287 .i2c_algo = &lme2510_i2c_algo,
1288
1289 .frontend_attach = dm04_lme2510_frontend_attach,
1290 .tuner_attach = dm04_lme2510_tuner,
1291 .get_stream_config = lme2510_get_stream_config,
1292 .get_adapter_count = lme2510_get_adapter_count,
1293 .streaming_ctrl = lme2510_streaming_ctrl,
1294
1295 .get_rc_config = lme2510_get_rc_config,
1296
1297 .exit = lme2510_exit,
1298 .adapter = {
1299 {
1300 .caps = DVB_USB_ADAP_HAS_PID_FILTER|
1301 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1302 .pid_filter_count = 15,
1303 .pid_filter = lme2510_pid_filter,
1304 .pid_filter_ctrl = lme2510_pid_filter_ctrl,
1305 .stream =
1306 DVB_USB_STREAM_BULK(0x86, 10, 4096),
1307 },
1308 {
1309 }
1310 },
1311};
1312
1313static const struct usb_device_id lme2510_id_table[] = {
1314 { DVB_USB_DEVICE(0x3344, 0x1122, &lme2510_props,
1315 "DM04_LME2510_DVB-S", RC_MAP_LME2510) },
1316 { DVB_USB_DEVICE(0x3344, 0x1120, &lme2510_props,
1317 "DM04_LME2510C_DVB-S", RC_MAP_LME2510) },
1318 { DVB_USB_DEVICE(0x3344, 0x22f0, &lme2510_props,
1319 "DM04_LME2510C_DVB-S RS2000", RC_MAP_LME2510) },
1320 {} /* Terminating entry */
1321};
1322
1323MODULE_DEVICE_TABLE(usb, lme2510_id_table);
1324
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001325static struct usb_driver lme2510_driver = {
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001326 .name = KBUILD_MODNAME,
1327 .probe = dvb_usbv2_probe,
1328 .disconnect = dvb_usbv2_disconnect,
1329 .id_table = lme2510_id_table,
1330 .no_dynamic_id = 1,
1331 .soft_unbind = 1,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001332};
1333
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -08001334module_usb_driver(lme2510_driver);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001335
1336MODULE_AUTHOR("Malcolm Priestley <tvboxspy@gmail.com>");
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001337MODULE_DESCRIPTION("LME2510(C) DVB-S USB2.0");
David Härdemanf459aec2015-05-19 19:03:27 -03001338MODULE_VERSION("2.07");
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001339MODULE_LICENSE("GPL");
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001340MODULE_FIRMWARE(LME2510_C_S7395);
1341MODULE_FIRMWARE(LME2510_C_LG);
1342MODULE_FIRMWARE(LME2510_C_S0194);
1343MODULE_FIRMWARE(LME2510_C_RS2000);
1344MODULE_FIRMWARE(LME2510_LG);
1345MODULE_FIRMWARE(LME2510_S0194);
1346