blob: 3721ee63b8fb0acdc3da50a70813ce80f1174154 [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 *
51 * You should have received a copy of the GNU General Public License
52 * along with this program; if not, write to the Free Software
53 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
54 *
55 *
56 * see Documentation/dvb/README.dvb-usb for more information
57 *
58 * Known Issues :
59 * LME2510: Non Intel USB chipsets fail to maintain High Speed on
60 * Boot or Hot Plug.
61 *
Malcolm Priestleyab599a62010-10-16 16:44:43 -030062 * QQbox suffers from noise on LNB voltage.
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -030063 *
Malcolm Priestley9d4540c2011-02-12 22:35:08 -030064 * LME2510: SHARP:BS2F7HZ0194(MV0194) cannot cold reset and share system
65 * with other tuners. After a cold reset streaming will not start.
66 *
Malcolm Priestleye41543a2012-08-06 16:21:40 -030067 * M88RS2000 suffers from loss of lock.
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -030068 */
69#define DVB_USB_LOG_PREFIX "LME2510(C)"
70#include <linux/usb.h>
71#include <linux/usb/input.h>
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -030072#include <media/rc-core.h>
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -030073
Malcolm Priestleye41543a2012-08-06 16:21:40 -030074#include "dvb_usb.h"
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -030075#include "lmedm04.h"
76#include "tda826x.h"
77#include "tda10086.h"
78#include "stv0288.h"
79#include "ix2505v.h"
Malcolm Priestley9d4540c2011-02-12 22:35:08 -030080#include "stv0299.h"
81#include "dvb-pll.h"
82#include "z0194a.h"
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -030083#include "m88rs2000.h"
Igor M. Liplianinb858c332012-12-28 19:40:33 -030084#include "ts2020.h"
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -030085
86
Malcolm Priestleye41543a2012-08-06 16:21:40 -030087#define LME2510_C_S7395 "dvb-usb-lme2510c-s7395.fw";
88#define LME2510_C_LG "dvb-usb-lme2510c-lg.fw";
89#define LME2510_C_S0194 "dvb-usb-lme2510c-s0194.fw";
90#define LME2510_C_RS2000 "dvb-usb-lme2510c-rs2000.fw";
91#define LME2510_LG "dvb-usb-lme2510-lg.fw";
92#define LME2510_S0194 "dvb-usb-lme2510-s0194.fw";
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -030093
94/* debug */
95static int dvb_usb_lme2510_debug;
Malcolm Priestleye41543a2012-08-06 16:21:40 -030096#define lme_debug(var, level, args...) do { \
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -030097 if ((var >= level)) \
Malcolm Priestleye41543a2012-08-06 16:21:40 -030098 pr_debug(DVB_USB_LOG_PREFIX": " args); \
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -030099} while (0)
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300100#define deb_info(level, args...) lme_debug(dvb_usb_lme2510_debug, level, args)
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300101#define debug_data_snipet(level, name, p) \
102 deb_info(level, name" (%02x%02x%02x%02x%02x%02x%02x%02x)", \
103 *p, *(p+1), *(p+2), *(p+3), *(p+4), \
104 *(p+5), *(p+6), *(p+7));
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300105#define info(args...) pr_info(DVB_USB_LOG_PREFIX": "args)
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300106
107module_param_named(debug, dvb_usb_lme2510_debug, int, 0644);
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300108MODULE_PARM_DESC(debug, "set debugging level (1=info (or-able)).");
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300109
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300110static int dvb_usb_lme2510_firmware;
111module_param_named(firmware, dvb_usb_lme2510_firmware, int, 0644);
112MODULE_PARM_DESC(firmware, "set default firmware 0=Sharp7395 1=LG");
113
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300114static int pid_filter;
115module_param_named(pid, pid_filter, int, 0644);
Malcolm Priestley88495032012-02-06 19:05:01 -0300116MODULE_PARM_DESC(pid, "set default 0=default 1=off 2=on");
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300117
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300118
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300119DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300120
121#define TUNER_DEFAULT 0x0
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300122#define TUNER_LG 0x1
123#define TUNER_S7395 0x2
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300124#define TUNER_S0194 0x3
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300125#define TUNER_RS2000 0x4
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300126
127struct lme2510_state {
Malcolm Priestley3a370222014-05-24 06:35:57 -0300128 unsigned long int_urb_due;
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300129 enum fe_status lock_status;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300130 u8 id;
131 u8 tuner_config;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300132 u8 signal_level;
133 u8 signal_sn;
134 u8 time_key;
135 u8 i2c_talk_onoff;
136 u8 i2c_gate;
137 u8 i2c_tuner_gate_w;
138 u8 i2c_tuner_gate_r;
139 u8 i2c_tuner_addr;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300140 u8 stream_on;
Malcolm Priestley449a0ba2011-04-22 06:20:57 -0300141 u8 pid_size;
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300142 u8 pid_off;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300143 void *buffer;
144 struct urb *lme_urb;
145 void *usb_buffer;
Malcolm Priestley743ab6a12015-01-02 10:56:30 -0300146 /* Frontend original calls */
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300147 int (*fe_read_status)(struct dvb_frontend *, enum fe_status *);
Malcolm Priestley87169012015-01-02 10:56:31 -0300148 int (*fe_read_signal_strength)(struct dvb_frontend *, u16 *);
149 int (*fe_read_snr)(struct dvb_frontend *, u16 *);
150 int (*fe_read_ber)(struct dvb_frontend *, u32 *);
151 int (*fe_read_ucblocks)(struct dvb_frontend *, u32 *);
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300152 int (*fe_set_voltage)(struct dvb_frontend *, enum fe_sec_voltage);
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300153 u8 dvb_usb_lme2510_firmware;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300154};
155
156static int lme2510_bulk_write(struct usb_device *dev,
157 u8 *snd, int len, u8 pipe)
158{
159 int ret, actual_l;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300160
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300161 ret = usb_bulk_msg(dev, usb_sndbulkpipe(dev, pipe),
Malcolm Priestley25ad9842010-11-04 17:17:51 -0300162 snd, len , &actual_l, 100);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300163 return ret;
164}
165
166static int lme2510_bulk_read(struct usb_device *dev,
167 u8 *rev, int len, u8 pipe)
168{
169 int ret, actual_l;
170
171 ret = usb_bulk_msg(dev, usb_rcvbulkpipe(dev, pipe),
Malcolm Priestley25ad9842010-11-04 17:17:51 -0300172 rev, len , &actual_l, 200);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300173 return ret;
174}
175
176static int lme2510_usb_talk(struct dvb_usb_device *d,
177 u8 *wbuf, int wlen, u8 *rbuf, int rlen)
178{
179 struct lme2510_state *st = d->priv;
180 u8 *buff;
181 int ret = 0;
182
183 if (st->usb_buffer == NULL) {
tvboxspy810fda92011-09-11 19:30:10 -0300184 st->usb_buffer = kmalloc(64, GFP_KERNEL);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300185 if (st->usb_buffer == NULL) {
186 info("MEM Error no memory");
187 return -ENOMEM;
188 }
189 }
190 buff = st->usb_buffer;
191
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300192 ret = mutex_lock_interruptible(&d->usb_mutex);
193
194 if (ret < 0)
195 return -EAGAIN;
196
tvboxspy810fda92011-09-11 19:30:10 -0300197 /* the read/write capped at 64 */
198 memcpy(buff, wbuf, (wlen < 64) ? wlen : 64);
Malcolm Priestleya760d632011-04-22 06:07:40 -0300199
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300200 ret |= lme2510_bulk_write(d->udev, buff, wlen , 0x01);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300201
tvboxspy810fda92011-09-11 19:30:10 -0300202 ret |= lme2510_bulk_read(d->udev, buff, (rlen < 64) ?
203 rlen : 64 , 0x01);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300204
205 if (rlen > 0)
206 memcpy(rbuf, buff, rlen);
207
208 mutex_unlock(&d->usb_mutex);
209
210 return (ret < 0) ? -ENODEV : 0;
211}
212
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300213static int lme2510_stream_restart(struct dvb_usb_device *d)
214{
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300215 struct lme2510_state *st = d->priv;
216 u8 all_pids[] = LME_ALL_PIDS;
217 u8 stream_on[] = LME_ST_ON_W;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300218 int ret;
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300219 u8 rbuff[1];
220 if (st->pid_off)
221 ret = lme2510_usb_talk(d, all_pids, sizeof(all_pids),
222 rbuff, sizeof(rbuff));
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300223 /*Restart Stream Command*/
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300224 ret = lme2510_usb_talk(d, stream_on, sizeof(stream_on),
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300225 rbuff, sizeof(rbuff));
226 return ret;
227}
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300228
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300229static int lme2510_enable_pid(struct dvb_usb_device *d, u8 index, u16 pid_out)
230{
231 struct lme2510_state *st = d->priv;
232 static u8 pid_buff[] = LME_ZERO_PID;
233 static u8 rbuf[1];
234 u8 pid_no = index * 2;
Malcolm Priestley449a0ba2011-04-22 06:20:57 -0300235 u8 pid_len = pid_no + 2;
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300236 int ret = 0;
237 deb_info(1, "PID Setting Pid %04x", pid_out);
238
Malcolm Priestley449a0ba2011-04-22 06:20:57 -0300239 if (st->pid_size == 0)
240 ret |= lme2510_stream_restart(d);
241
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300242 pid_buff[2] = pid_no;
243 pid_buff[3] = (u8)pid_out & 0xff;
244 pid_buff[4] = pid_no + 1;
245 pid_buff[5] = (u8)(pid_out >> 8);
246
Malcolm Priestley449a0ba2011-04-22 06:20:57 -0300247 if (pid_len > st->pid_size)
248 st->pid_size = pid_len;
249 pid_buff[7] = 0x80 + st->pid_size;
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300250
251 ret |= lme2510_usb_talk(d, pid_buff ,
252 sizeof(pid_buff) , rbuf, sizeof(rbuf));
253
Malcolm Priestley449a0ba2011-04-22 06:20:57 -0300254 if (st->stream_on)
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300255 ret |= lme2510_stream_restart(d);
256
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300257 return ret;
258}
259
Mauro Carvalho Chehabfaebbd82015-06-23 12:29:46 -0300260/* Convert range from 0x00-0xff to 0x0000-0xffff */
261#define reg_to_16bits(x) ((x) | ((x) << 8))
262
Malcolm Priestley48c91aa2015-06-08 19:05:20 -0300263static void lme2510_update_stats(struct dvb_usb_adapter *adap)
264{
265 struct lme2510_state *st = adap_to_priv(adap);
266 struct dvb_frontend *fe = adap->fe[0];
267 struct dtv_frontend_properties *c;
Mauro Carvalho Chehab5ae65db2015-06-23 12:22:06 -0300268 u32 s_tmp = 0, c_tmp = 0;
Malcolm Priestley48c91aa2015-06-08 19:05:20 -0300269
270 if (!fe)
271 return;
272
273 c = &fe->dtv_property_cache;
274
275 c->block_count.len = 1;
276 c->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
277 c->block_error.len = 1;
278 c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
279 c->post_bit_count.len = 1;
280 c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
281 c->post_bit_error.len = 1;
282 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
283
284 if (st->i2c_talk_onoff) {
285 c->strength.len = 1;
286 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
287 c->cnr.len = 1;
288 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
289 return;
290 }
291
292 switch (st->tuner_config) {
293 case TUNER_LG:
Mauro Carvalho Chehabfaebbd82015-06-23 12:29:46 -0300294 s_tmp = reg_to_16bits(0xff - st->signal_level);
295 c_tmp = reg_to_16bits(0xff - st->signal_sn);
Malcolm Priestley48c91aa2015-06-08 19:05:20 -0300296 break;
Malcolm Priestley48c91aa2015-06-08 19:05:20 -0300297 case TUNER_S7395:
298 case TUNER_S0194:
299 s_tmp = 0xffff - (((st->signal_level * 2) << 8) * 5 / 4);
Mauro Carvalho Chehabfaebbd82015-06-23 12:29:46 -0300300 c_tmp = reg_to_16bits((0xff - st->signal_sn - 0xa1) * 3);
Malcolm Priestley48c91aa2015-06-08 19:05:20 -0300301 break;
302 case TUNER_RS2000:
Mauro Carvalho Chehabfaebbd82015-06-23 12:29:46 -0300303 s_tmp = reg_to_16bits(st->signal_level);
304 c_tmp = reg_to_16bits(st->signal_sn);
Malcolm Priestley48c91aa2015-06-08 19:05:20 -0300305 }
306
307 c->strength.len = 1;
308 c->strength.stat[0].scale = FE_SCALE_RELATIVE;
Mauro Carvalho Chehab5ae65db2015-06-23 12:22:06 -0300309 c->strength.stat[0].uvalue = (u64)s_tmp;
Malcolm Priestley48c91aa2015-06-08 19:05:20 -0300310
311 c->cnr.len = 1;
312 c->cnr.stat[0].scale = FE_SCALE_RELATIVE;
Mauro Carvalho Chehab5ae65db2015-06-23 12:22:06 -0300313 c->cnr.stat[0].uvalue = (u64)c_tmp;
Malcolm Priestley48c91aa2015-06-08 19:05:20 -0300314}
315
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300316static void lme2510_int_response(struct urb *lme_urb)
317{
318 struct dvb_usb_adapter *adap = lme_urb->context;
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300319 struct lme2510_state *st = adap_to_priv(adap);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300320 static u8 *ibuf, *rbuf;
321 int i = 0, offset;
Malcolm Priestley616a4b82011-05-25 18:28:29 -0300322 u32 key;
Malcolm Priestley743ab6a12015-01-02 10:56:30 -0300323 u8 signal_lock = 0;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300324
325 switch (lme_urb->status) {
326 case 0:
327 case -ETIMEDOUT:
328 break;
329 case -ECONNRESET:
330 case -ENOENT:
331 case -ESHUTDOWN:
332 return;
333 default:
334 info("Error %x", lme_urb->status);
335 break;
336 }
337
338 rbuf = (u8 *) lme_urb->transfer_buffer;
339
340 offset = ((lme_urb->actual_length/8) > 4)
341 ? 4 : (lme_urb->actual_length/8) ;
342
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300343 for (i = 0; i < offset; ++i) {
344 ibuf = (u8 *)&rbuf[i*8];
345 deb_info(5, "INT O/S C =%02x C/O=%02x Type =%02x%02x",
346 offset, i, ibuf[0], ibuf[1]);
347
348 switch (ibuf[0]) {
349 case 0xaa:
Malcolm Priestley616a4b82011-05-25 18:28:29 -0300350 debug_data_snipet(1, "INT Remote data snipet", ibuf);
David Härdemanf459aec2015-05-19 19:03:27 -0300351 if (!adap_to_d(adap)->rc_dev)
352 break;
353
354 key = RC_SCANCODE_NEC32(ibuf[2] << 24 |
355 ibuf[3] << 16 |
356 ibuf[4] << 8 |
357 ibuf[5]);
358
359 deb_info(1, "INT Key = 0x%08x", key);
360 rc_keydown(adap_to_d(adap)->rc_dev, RC_TYPE_NEC, key, 0);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300361 break;
362 case 0xbb:
363 switch (st->tuner_config) {
364 case TUNER_LG:
Malcolm Priestley743ab6a12015-01-02 10:56:30 -0300365 signal_lock = ibuf[2] & BIT(5);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300366 st->signal_level = ibuf[4];
367 st->signal_sn = ibuf[3];
368 st->time_key = ibuf[7];
369 break;
370 case TUNER_S7395:
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300371 case TUNER_S0194:
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300372 /* Tweak for earlier firmware*/
373 if (ibuf[1] == 0x03) {
Malcolm Priestley743ab6a12015-01-02 10:56:30 -0300374 signal_lock = ibuf[2] & BIT(4);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300375 st->signal_level = ibuf[3];
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300376 st->signal_sn = ibuf[4];
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300377 } else {
378 st->signal_level = ibuf[4];
379 st->signal_sn = ibuf[5];
380 }
381 break;
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300382 case TUNER_RS2000:
Malcolm Priestley743ab6a12015-01-02 10:56:30 -0300383 signal_lock = ibuf[2] & 0xee;
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300384 st->signal_level = ibuf[5];
385 st->signal_sn = ibuf[4];
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300386 st->time_key = ibuf[7];
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300387 default:
388 break;
389 }
Malcolm Priestley743ab6a12015-01-02 10:56:30 -0300390
391 /* Interrupt will also throw just BIT 0 as lock */
392 signal_lock |= ibuf[2] & BIT(0);
393
394 if (!signal_lock)
395 st->lock_status &= ~FE_HAS_LOCK;
396
Malcolm Priestley48c91aa2015-06-08 19:05:20 -0300397 lme2510_update_stats(adap);
398
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300399 debug_data_snipet(5, "INT Remote data snipet in", ibuf);
400 break;
401 case 0xcc:
402 debug_data_snipet(1, "INT Control data snipet", ibuf);
403 break;
404 default:
405 debug_data_snipet(1, "INT Unknown data snipet", ibuf);
406 break;
407 }
408 }
Malcolm Priestley3a370222014-05-24 06:35:57 -0300409
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300410 usb_submit_urb(lme_urb, GFP_ATOMIC);
Malcolm Priestley3a370222014-05-24 06:35:57 -0300411
Malcolm Priestleycfcd7b82015-01-02 10:56:27 -0300412 /* Interrupt urb is due every 48 msecs while streaming the buffer
413 * stores up to 4 periods if missed. Allow 200 msec for next interrupt.
414 */
415 st->int_urb_due = jiffies + msecs_to_jiffies(200);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300416}
417
418static int lme2510_int_read(struct dvb_usb_adapter *adap)
419{
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300420 struct dvb_usb_device *d = adap_to_d(adap);
421 struct lme2510_state *lme_int = adap_to_priv(adap);
Malcolm Priestley15e1ce32015-01-02 10:56:28 -0300422 struct usb_host_endpoint *ep;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300423
424 lme_int->lme_urb = usb_alloc_urb(0, GFP_ATOMIC);
425
426 if (lme_int->lme_urb == NULL)
427 return -ENOMEM;
428
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300429 lme_int->buffer = usb_alloc_coherent(d->udev, 128, GFP_ATOMIC,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300430 &lme_int->lme_urb->transfer_dma);
431
432 if (lme_int->buffer == NULL)
433 return -ENOMEM;
434
435 usb_fill_int_urb(lme_int->lme_urb,
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300436 d->udev,
437 usb_rcvintpipe(d->udev, 0xa),
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300438 lme_int->buffer,
tvboxspy95c21c42011-09-11 19:26:50 -0300439 128,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300440 lme2510_int_response,
441 adap,
tvboxspy95c21c42011-09-11 19:26:50 -0300442 8);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300443
Malcolm Priestley15e1ce32015-01-02 10:56:28 -0300444 /* Quirk of pipe reporting PIPE_BULK but behaves as interrupt */
445 ep = usb_pipe_endpoint(d->udev, lme_int->lme_urb->pipe);
446
447 if (usb_endpoint_type(&ep->desc) == USB_ENDPOINT_XFER_BULK)
448 lme_int->lme_urb->pipe = usb_rcvbulkpipe(d->udev, 0xa),
449
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300450 lme_int->lme_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
451
452 usb_submit_urb(lme_int->lme_urb, GFP_ATOMIC);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300453 info("INT Interrupt Service Started");
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300454
455 return 0;
456}
457
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300458static int lme2510_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
459{
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300460 struct dvb_usb_device *d = adap_to_d(adap);
461 struct lme2510_state *st = adap_to_priv(adap);
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300462 static u8 clear_pid_reg[] = LME_ALL_PIDS;
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300463 static u8 rbuf[1];
Il Hanb7dc4cd2012-05-07 12:04:42 -0300464 int ret = 0;
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300465
466 deb_info(1, "PID Clearing Filter");
467
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300468 mutex_lock(&d->i2c_mutex);
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300469
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300470 if (!onoff) {
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300471 ret |= lme2510_usb_talk(d, clear_pid_reg,
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300472 sizeof(clear_pid_reg), rbuf, sizeof(rbuf));
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300473 st->pid_off = true;
474 } else
475 st->pid_off = false;
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300476
Malcolm Priestley449a0ba2011-04-22 06:20:57 -0300477 st->pid_size = 0;
478
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300479 mutex_unlock(&d->i2c_mutex);
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300480
481 return 0;
482}
483
484static int lme2510_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
485 int onoff)
486{
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300487 struct dvb_usb_device *d = adap_to_d(adap);
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300488 int ret = 0;
489
490 deb_info(3, "%s PID=%04x Index=%04x onoff=%02x", __func__,
491 pid, index, onoff);
492
Malcolm Priestley4c316b52011-12-30 11:33:03 -0300493 if (onoff) {
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300494 mutex_lock(&d->i2c_mutex);
495 ret |= lme2510_enable_pid(d, index, pid);
496 mutex_unlock(&d->i2c_mutex);
Malcolm Priestley449a0ba2011-04-22 06:20:57 -0300497 }
498
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300499
500 return ret;
501}
502
503
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300504static int lme2510_return_status(struct dvb_usb_device *d)
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300505{
506 int ret = 0;
Florian Mickler945b8762011-03-20 18:50:52 -0300507 u8 *data;
508
509 data = kzalloc(10, GFP_KERNEL);
510 if (!data)
511 return -ENOMEM;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300512
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300513 ret |= usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0),
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300514 0x06, 0x80, 0x0302, 0x00, data, 0x0006, 200);
515 info("Firmware Status: %x (%x)", ret , data[2]);
516
Florian Mickler945b8762011-03-20 18:50:52 -0300517 ret = (ret < 0) ? -ENODEV : data[2];
518 kfree(data);
519 return ret;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300520}
521
522static int lme2510_msg(struct dvb_usb_device *d,
523 u8 *wbuf, int wlen, u8 *rbuf, int rlen)
524{
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300525 struct lme2510_state *st = d->priv;
526
Malcolm Priestley743ab6a12015-01-02 10:56:30 -0300527 st->i2c_talk_onoff = 1;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300528
Malcolm Priestley743ab6a12015-01-02 10:56:30 -0300529 return lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300530}
531
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300532static int lme2510_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
533 int num)
534{
535 struct dvb_usb_device *d = i2c_get_adapdata(adap);
536 struct lme2510_state *st = d->priv;
tvboxspy810fda92011-09-11 19:30:10 -0300537 static u8 obuf[64], ibuf[64];
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300538 int i, read, read_o;
539 u16 len;
540 u8 gate = st->i2c_gate;
541
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300542 mutex_lock(&d->i2c_mutex);
543
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300544 if (gate == 0)
545 gate = 5;
546
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300547 for (i = 0; i < num; i++) {
Malcolm Priestley71dc98b2012-12-29 07:34:24 -0300548 read_o = msg[i].flags & I2C_M_RD;
549 read = i + 1 < num && msg[i + 1].flags & I2C_M_RD;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300550 read |= read_o;
551 gate = (msg[i].addr == st->i2c_tuner_addr)
552 ? (read) ? st->i2c_tuner_gate_r
553 : st->i2c_tuner_gate_w
554 : st->i2c_gate;
555 obuf[0] = gate | (read << 7);
556
557 if (gate == 5)
558 obuf[1] = (read) ? 2 : msg[i].len + 1;
559 else
560 obuf[1] = msg[i].len + read + 1;
561
Malcolm Priestley71dc98b2012-12-29 07:34:24 -0300562 obuf[2] = msg[i].addr << 1;
563
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300564 if (read) {
565 if (read_o)
566 len = 3;
567 else {
568 memcpy(&obuf[3], msg[i].buf, msg[i].len);
569 obuf[msg[i].len+3] = msg[i+1].len;
570 len = msg[i].len+4;
571 }
572 } else {
573 memcpy(&obuf[3], msg[i].buf, msg[i].len);
574 len = msg[i].len+3;
575 }
576
tvboxspy810fda92011-09-11 19:30:10 -0300577 if (lme2510_msg(d, obuf, len, ibuf, 64) < 0) {
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300578 deb_info(1, "i2c transfer failed.");
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300579 mutex_unlock(&d->i2c_mutex);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300580 return -EAGAIN;
581 }
582
583 if (read) {
584 if (read_o)
585 memcpy(msg[i].buf, &ibuf[1], msg[i].len);
586 else {
587 memcpy(msg[i+1].buf, &ibuf[1], msg[i+1].len);
588 i++;
589 }
590 }
591 }
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300592
593 mutex_unlock(&d->i2c_mutex);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300594 return i;
595}
596
597static u32 lme2510_i2c_func(struct i2c_adapter *adapter)
598{
599 return I2C_FUNC_I2C;
600}
601
602static struct i2c_algorithm lme2510_i2c_algo = {
603 .master_xfer = lme2510_i2c_xfer,
604 .functionality = lme2510_i2c_func,
605};
606
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300607static int lme2510_streaming_ctrl(struct dvb_frontend *fe, int onoff)
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300608{
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300609 struct dvb_usb_adapter *adap = fe_to_adap(fe);
610 struct dvb_usb_device *d = adap_to_d(adap);
611 struct lme2510_state *st = adap_to_priv(adap);
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300612 static u8 clear_reg_3[] = LME_ALL_PIDS;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300613 static u8 rbuf[1];
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300614 int ret = 0, rlen = sizeof(rbuf);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300615
616 deb_info(1, "STM (%02x)", onoff);
617
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300618 /* Streaming is started by FE_HAS_LOCK */
619 if (onoff == 1)
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300620 st->stream_on = 1;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300621 else {
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300622 deb_info(1, "STM Steam Off");
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300623 /* mutex is here only to avoid collision with I2C */
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300624 mutex_lock(&d->i2c_mutex);
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300625
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300626 ret = lme2510_usb_talk(d, clear_reg_3,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300627 sizeof(clear_reg_3), rbuf, rlen);
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300628 st->stream_on = 0;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300629 st->i2c_talk_onoff = 1;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300630
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300631 mutex_unlock(&d->i2c_mutex);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300632 }
633
634 return (ret < 0) ? -ENODEV : 0;
635}
636
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300637static u8 check_sum(u8 *p, u8 len)
638{
639 u8 sum = 0;
640 while (len--)
641 sum += *p++;
642 return sum;
643}
644
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300645static int lme2510_download_firmware(struct dvb_usb_device *d,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300646 const struct firmware *fw)
647{
648 int ret = 0;
Florian Mickler945b8762011-03-20 18:50:52 -0300649 u8 *data;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300650 u16 j, wlen, len_in, start, end;
651 u8 packet_size, dlen, i;
652 u8 *fw_data;
653
654 packet_size = 0x31;
655 len_in = 1;
656
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300657 data = kzalloc(128, GFP_KERNEL);
Florian Mickler945b8762011-03-20 18:50:52 -0300658 if (!data) {
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300659 info("FRM Could not start Firmware Download"\
660 "(Buffer allocation failed)");
Florian Mickler945b8762011-03-20 18:50:52 -0300661 return -ENOMEM;
662 }
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300663
664 info("FRM Starting Firmware Download");
665
666 for (i = 1; i < 3; i++) {
667 start = (i == 1) ? 0 : 512;
668 end = (i == 1) ? 512 : fw->size;
669 for (j = start; j < end; j += (packet_size+1)) {
670 fw_data = (u8 *)(fw->data + j);
671 if ((end - j) > packet_size) {
672 data[0] = i;
673 dlen = packet_size;
674 } else {
675 data[0] = i | 0x80;
676 dlen = (u8)(end - j)-1;
677 }
Florian Micklerb9f7b732011-03-20 18:50:51 -0300678 data[1] = dlen;
679 memcpy(&data[2], fw_data, dlen+1);
680 wlen = (u8) dlen + 4;
681 data[wlen-1] = check_sum(fw_data, dlen+1);
682 deb_info(1, "Data S=%02x:E=%02x CS= %02x", data[3],
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300683 data[dlen+2], data[dlen+3]);
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300684 lme2510_usb_talk(d, data, wlen, data, len_in);
Florian Micklerb9f7b732011-03-20 18:50:51 -0300685 ret |= (data[0] == 0x88) ? 0 : -1;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300686 }
687 }
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300688
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300689 data[0] = 0x8a;
690 len_in = 1;
691 msleep(2000);
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300692 lme2510_usb_talk(d, data, len_in, data, len_in);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300693 msleep(400);
694
695 if (ret < 0)
696 info("FRM Firmware Download Failed (%04x)" , ret);
697 else
698 info("FRM Firmware Download Completed - Resetting Device");
699
Florian Mickler945b8762011-03-20 18:50:52 -0300700 kfree(data);
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300701 return RECONNECTS_USB;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300702}
703
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300704static void lme_coldreset(struct dvb_usb_device *d)
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300705{
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300706 u8 data[1] = {0};
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300707 data[0] = 0x0a;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300708 info("FRM Firmware Cold Reset");
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300709
710 lme2510_usb_talk(d, data, sizeof(data), data, sizeof(data));
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300711
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300712 return;
713}
714
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300715static const char fw_c_s7395[] = LME2510_C_S7395;
716static const char fw_c_lg[] = LME2510_C_LG;
717static const char fw_c_s0194[] = LME2510_C_S0194;
718static const char fw_c_rs2000[] = LME2510_C_RS2000;
719static const char fw_lg[] = LME2510_LG;
720static const char fw_s0194[] = LME2510_S0194;
721
Mauro Carvalho Chehab1885fd22012-10-27 14:11:15 -0300722static const char *lme_firmware_switch(struct dvb_usb_device *d, int cold)
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300723{
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300724 struct lme2510_state *st = d->priv;
725 struct usb_device *udev = d->udev;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300726 const struct firmware *fw = NULL;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300727 const char *fw_lme;
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300728 int ret = 0;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300729
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300730 cold = (cold > 0) ? (cold & 1) : 0;
731
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300732 switch (le16_to_cpu(udev->descriptor.idProduct)) {
733 case 0x1122:
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300734 switch (st->dvb_usb_lme2510_firmware) {
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300735 default:
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300736 case TUNER_S0194:
737 fw_lme = fw_s0194;
738 ret = request_firmware(&fw, fw_lme, &udev->dev);
739 if (ret == 0) {
Mauro Carvalho Chehab504febc2014-11-25 15:46:13 -0200740 st->dvb_usb_lme2510_firmware = TUNER_S0194;
Malcolm Priestley30bd3b52011-02-13 20:12:15 -0300741 cold = 0;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300742 break;
743 }
Mauro Carvalho Chehab504febc2014-11-25 15:46:13 -0200744 /* fall through */
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300745 case TUNER_LG:
746 fw_lme = fw_lg;
747 ret = request_firmware(&fw, fw_lme, &udev->dev);
Mauro Carvalho Chehab504febc2014-11-25 15:46:13 -0200748 if (ret == 0) {
749 st->dvb_usb_lme2510_firmware = TUNER_LG;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300750 break;
Mauro Carvalho Chehab504febc2014-11-25 15:46:13 -0200751 }
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300752 st->dvb_usb_lme2510_firmware = TUNER_DEFAULT;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300753 break;
754 }
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300755 break;
756 case 0x1120:
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300757 switch (st->dvb_usb_lme2510_firmware) {
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300758 default:
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300759 case TUNER_S7395:
760 fw_lme = fw_c_s7395;
761 ret = request_firmware(&fw, fw_lme, &udev->dev);
Malcolm Priestley30bd3b52011-02-13 20:12:15 -0300762 if (ret == 0) {
Mauro Carvalho Chehab504febc2014-11-25 15:46:13 -0200763 st->dvb_usb_lme2510_firmware = TUNER_S7395;
Malcolm Priestley30bd3b52011-02-13 20:12:15 -0300764 cold = 0;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300765 break;
Malcolm Priestley30bd3b52011-02-13 20:12:15 -0300766 }
Mauro Carvalho Chehab504febc2014-11-25 15:46:13 -0200767 /* fall through */
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300768 case TUNER_LG:
769 fw_lme = fw_c_lg;
770 ret = request_firmware(&fw, fw_lme, &udev->dev);
Mauro Carvalho Chehab504febc2014-11-25 15:46:13 -0200771 if (ret == 0) {
772 st->dvb_usb_lme2510_firmware = TUNER_LG;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300773 break;
Mauro Carvalho Chehab504febc2014-11-25 15:46:13 -0200774 }
775 /* fall through */
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300776 case TUNER_S0194:
777 fw_lme = fw_c_s0194;
778 ret = request_firmware(&fw, fw_lme, &udev->dev);
Mauro Carvalho Chehab504febc2014-11-25 15:46:13 -0200779 if (ret == 0) {
780 st->dvb_usb_lme2510_firmware = TUNER_S0194;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300781 break;
Mauro Carvalho Chehab504febc2014-11-25 15:46:13 -0200782 }
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300783 st->dvb_usb_lme2510_firmware = TUNER_DEFAULT;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300784 cold = 0;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300785 break;
786 }
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300787 break;
788 case 0x22f0:
789 fw_lme = fw_c_rs2000;
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300790 st->dvb_usb_lme2510_firmware = TUNER_RS2000;
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300791 break;
792 default:
793 fw_lme = fw_c_s7395;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300794 }
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300795
Malcolm Priestley30bd3b52011-02-13 20:12:15 -0300796 release_firmware(fw);
797
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300798 if (cold) {
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300799 dvb_usb_lme2510_firmware = st->dvb_usb_lme2510_firmware;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300800 info("FRM Changing to %s firmware", fw_lme);
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300801 lme_coldreset(d);
802 return NULL;
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300803 }
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300804
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300805 return fw_lme;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300806}
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300807
808static int lme2510_kill_urb(struct usb_data_stream *stream)
809{
810 int i;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300811
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300812 for (i = 0; i < stream->urbs_submitted; i++) {
813 deb_info(3, "killing URB no. %d.", i);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300814 /* stop the URB */
815 usb_kill_urb(stream->urb_list[i]);
816 }
817 stream->urbs_submitted = 0;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300818
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300819 return 0;
820}
821
822static struct tda10086_config tda10086_config = {
Malcolm Priestley71dc98b2012-12-29 07:34:24 -0300823 .demod_address = 0x0e,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300824 .invert = 0,
825 .diseqc_tone = 1,
826 .xtal_freq = TDA10086_XTAL_16M,
827};
828
829static struct stv0288_config lme_config = {
Malcolm Priestley71dc98b2012-12-29 07:34:24 -0300830 .demod_address = 0x68,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300831 .min_delay_ms = 15,
832 .inittab = s7395_inittab,
833};
834
835static struct ix2505v_config lme_tuner = {
Malcolm Priestley71dc98b2012-12-29 07:34:24 -0300836 .tuner_address = 0x60,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300837 .min_delay_ms = 100,
838 .tuner_gain = 0x0,
839 .tuner_chargepump = 0x3,
840};
841
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300842static struct stv0299_config sharp_z0194_config = {
Malcolm Priestley71dc98b2012-12-29 07:34:24 -0300843 .demod_address = 0x68,
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300844 .inittab = sharp_z0194a_inittab,
845 .mclk = 88000000UL,
846 .invert = 0,
847 .skip_reinit = 0,
848 .lock_output = STV0299_LOCKOUTPUT_1,
849 .volt13_op0_op1 = STV0299_VOLT13_OP1,
850 .min_delay_ms = 100,
851 .set_symbol_rate = sharp_z0194a_set_symbol_rate,
852};
853
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300854static struct m88rs2000_config m88rs2000_config = {
Malcolm Priestley743ab6a12015-01-02 10:56:30 -0300855 .demod_addr = 0x68
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300856};
857
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300858static struct ts2020_config ts2020_config = {
859 .tuner_address = 0x60,
860 .clk_out_div = 7,
Malcolm Priestley9e8715c2015-06-05 07:55:07 -0300861 .dont_poll = true
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300862};
863
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300864static int dm04_lme2510_set_voltage(struct dvb_frontend *fe,
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300865 enum fe_sec_voltage voltage)
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300866{
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300867 struct dvb_usb_device *d = fe_to_d(fe);
868 struct lme2510_state *st = fe_to_priv(fe);
869 static u8 voltage_low[] = LME_VOLTAGE_L;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300870 static u8 voltage_high[] = LME_VOLTAGE_H;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300871 static u8 rbuf[1];
872 int ret = 0, len = 3, rlen = 1;
873
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300874 mutex_lock(&d->i2c_mutex);
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300875
876 switch (voltage) {
877 case SEC_VOLTAGE_18:
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300878 ret |= lme2510_usb_talk(d,
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300879 voltage_high, len, rbuf, rlen);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300880 break;
881
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300882 case SEC_VOLTAGE_OFF:
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300883 case SEC_VOLTAGE_13:
884 default:
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300885 ret |= lme2510_usb_talk(d,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300886 voltage_low, len, rbuf, rlen);
887 break;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300888 }
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300889
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300890 mutex_unlock(&d->i2c_mutex);
891
892 if (st->tuner_config == TUNER_RS2000)
893 if (st->fe_set_voltage)
894 st->fe_set_voltage(fe, voltage);
895
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300896
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300897 return (ret < 0) ? -ENODEV : 0;
898}
899
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300900static int dm04_read_status(struct dvb_frontend *fe, enum fe_status *status)
Malcolm Priestley743ab6a12015-01-02 10:56:30 -0300901{
902 struct dvb_usb_device *d = fe_to_d(fe);
903 struct lme2510_state *st = d->priv;
904 int ret = 0;
905
906 if (st->i2c_talk_onoff) {
907 if (st->fe_read_status) {
908 ret = st->fe_read_status(fe, status);
909 if (ret < 0)
910 return ret;
911 }
912
913 st->lock_status = *status;
914
915 if (*status & FE_HAS_LOCK && st->stream_on) {
916 mutex_lock(&d->i2c_mutex);
917
918 st->i2c_talk_onoff = 0;
919 ret = lme2510_stream_restart(d);
920
921 mutex_unlock(&d->i2c_mutex);
922 }
923
924 return ret;
925 }
926
927 /* Timeout of interrupt reached on RS2000 */
928 if (st->tuner_config == TUNER_RS2000 &&
929 time_after(jiffies, st->int_urb_due))
930 st->lock_status &= ~FE_HAS_LOCK;
931
932 *status = st->lock_status;
933
Malcolm Priestley48c91aa2015-06-08 19:05:20 -0300934 if (!(*status & FE_HAS_LOCK)) {
935 struct dvb_usb_adapter *adap = fe_to_adap(fe);
936
Malcolm Priestley743ab6a12015-01-02 10:56:30 -0300937 st->i2c_talk_onoff = 1;
938
Malcolm Priestley48c91aa2015-06-08 19:05:20 -0300939 lme2510_update_stats(adap);
940 }
941
Malcolm Priestley743ab6a12015-01-02 10:56:30 -0300942 return ret;
943}
944
Malcolm Priestleyd8bad1e2015-01-02 10:56:29 -0300945static int dm04_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300946{
Malcolm Priestley48c91aa2015-06-08 19:05:20 -0300947 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300948 struct lme2510_state *st = fe_to_priv(fe);
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300949
Malcolm Priestley87169012015-01-02 10:56:31 -0300950 if (st->fe_read_signal_strength && !st->stream_on)
951 return st->fe_read_signal_strength(fe, strength);
952
Malcolm Priestley48c91aa2015-06-08 19:05:20 -0300953 if (c->strength.stat[0].scale == FE_SCALE_RELATIVE)
954 *strength = (u16)c->strength.stat[0].uvalue;
955 else
956 *strength = 0;
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300957
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300958 return 0;
959}
960
Malcolm Priestleyd8bad1e2015-01-02 10:56:29 -0300961static int dm04_read_snr(struct dvb_frontend *fe, u16 *snr)
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300962{
Malcolm Priestley48c91aa2015-06-08 19:05:20 -0300963 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300964 struct lme2510_state *st = fe_to_priv(fe);
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300965
Malcolm Priestley87169012015-01-02 10:56:31 -0300966 if (st->fe_read_snr && !st->stream_on)
967 return st->fe_read_snr(fe, snr);
968
Malcolm Priestley48c91aa2015-06-08 19:05:20 -0300969 if (c->cnr.stat[0].scale == FE_SCALE_RELATIVE)
970 *snr = (u16)c->cnr.stat[0].uvalue;
971 else
972 *snr = 0;
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300973
974 return 0;
975}
976
977static int dm04_read_ber(struct dvb_frontend *fe, u32 *ber)
978{
Malcolm Priestley87169012015-01-02 10:56:31 -0300979 struct lme2510_state *st = fe_to_priv(fe);
980
981 if (st->fe_read_ber && !st->stream_on)
982 return st->fe_read_ber(fe, ber);
983
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300984 *ber = 0;
985
986 return 0;
987}
988
989static int dm04_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
990{
Malcolm Priestley87169012015-01-02 10:56:31 -0300991 struct lme2510_state *st = fe_to_priv(fe);
992
993 if (st->fe_read_ucblocks && !st->stream_on)
994 return st->fe_read_ucblocks(fe, ucblocks);
995
Malcolm Priestleye41543a2012-08-06 16:21:40 -0300996 *ucblocks = 0;
997
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -0300998 return 0;
999}
1000
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001001static int lme_name(struct dvb_usb_adapter *adap)
1002{
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001003 struct dvb_usb_device *d = adap_to_d(adap);
1004 struct lme2510_state *st = adap_to_priv(adap);
1005 const char *desc = d->name;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -03001006 char *fe_name[] = {"", " LG TDQY-P001F", " SHARP:BS2F7HZ7395",
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001007 " SHARP:BS2F7HZ0194", " RS2000"};
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001008 char *name = adap->fe[0]->ops.info.name;
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001009
1010 strlcpy(name, desc, 128);
1011 strlcat(name, fe_name[st->tuner_config], 128);
1012
1013 return 0;
1014}
1015
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001016static int dm04_lme2510_frontend_attach(struct dvb_usb_adapter *adap)
1017{
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001018 struct dvb_usb_device *d = adap_to_d(adap);
1019 struct lme2510_state *st = d->priv;
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001020 int ret = 0;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001021
1022 st->i2c_talk_onoff = 1;
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001023 switch (le16_to_cpu(d->udev->descriptor.idProduct)) {
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001024 case 0x1122:
1025 case 0x1120:
1026 st->i2c_gate = 4;
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001027 adap->fe[0] = dvb_attach(tda10086_attach,
1028 &tda10086_config, &d->i2c_adap);
1029 if (adap->fe[0]) {
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001030 info("TUN Found Frontend TDA10086");
1031 st->i2c_tuner_gate_w = 4;
1032 st->i2c_tuner_gate_r = 4;
Malcolm Priestley71dc98b2012-12-29 07:34:24 -03001033 st->i2c_tuner_addr = 0x60;
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001034 st->tuner_config = TUNER_LG;
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001035 if (st->dvb_usb_lme2510_firmware != TUNER_LG) {
1036 st->dvb_usb_lme2510_firmware = TUNER_LG;
1037 ret = lme_firmware_switch(d, 1) ? 0 : -ENODEV;
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001038 }
1039 break;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -03001040 }
Malcolm Priestley9d4540c2011-02-12 22:35:08 -03001041
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001042 st->i2c_gate = 4;
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001043 adap->fe[0] = dvb_attach(stv0299_attach,
1044 &sharp_z0194_config, &d->i2c_adap);
1045 if (adap->fe[0]) {
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001046 info("FE Found Stv0299");
1047 st->i2c_tuner_gate_w = 4;
1048 st->i2c_tuner_gate_r = 5;
Malcolm Priestley71dc98b2012-12-29 07:34:24 -03001049 st->i2c_tuner_addr = 0x60;
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001050 st->tuner_config = TUNER_S0194;
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001051 if (st->dvb_usb_lme2510_firmware != TUNER_S0194) {
1052 st->dvb_usb_lme2510_firmware = TUNER_S0194;
1053 ret = lme_firmware_switch(d, 1) ? 0 : -ENODEV;
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001054 }
1055 break;
1056 }
1057
1058 st->i2c_gate = 5;
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001059 adap->fe[0] = dvb_attach(stv0288_attach, &lme_config,
1060 &d->i2c_adap);
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001061
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001062 if (adap->fe[0]) {
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001063 info("FE Found Stv0288");
1064 st->i2c_tuner_gate_w = 4;
1065 st->i2c_tuner_gate_r = 5;
Malcolm Priestley71dc98b2012-12-29 07:34:24 -03001066 st->i2c_tuner_addr = 0x60;
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001067 st->tuner_config = TUNER_S7395;
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001068 if (st->dvb_usb_lme2510_firmware != TUNER_S7395) {
1069 st->dvb_usb_lme2510_firmware = TUNER_S7395;
1070 ret = lme_firmware_switch(d, 1) ? 0 : -ENODEV;
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001071 }
1072 break;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -03001073 }
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001074 case 0x22f0:
1075 st->i2c_gate = 5;
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001076 adap->fe[0] = dvb_attach(m88rs2000_attach,
1077 &m88rs2000_config, &d->i2c_adap);
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001078
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001079 if (adap->fe[0]) {
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001080 info("FE Found M88RS2000");
Igor M. Liplianinb858c332012-12-28 19:40:33 -03001081 dvb_attach(ts2020_attach, adap->fe[0], &ts2020_config,
1082 &d->i2c_adap);
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001083 st->i2c_tuner_gate_w = 5;
1084 st->i2c_tuner_gate_r = 5;
Malcolm Priestley71dc98b2012-12-29 07:34:24 -03001085 st->i2c_tuner_addr = 0x60;
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001086 st->tuner_config = TUNER_RS2000;
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001087 st->fe_set_voltage =
1088 adap->fe[0]->ops.set_voltage;
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001089 }
1090 break;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001091 }
Malcolm Priestleyf23e6612010-10-27 19:50:36 -03001092
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001093 if (adap->fe[0] == NULL) {
1094 info("DM04/QQBOX Not Powered up or not Supported");
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001095 return -ENODEV;
1096 }
1097
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001098 if (ret) {
1099 if (adap->fe[0]) {
1100 dvb_frontend_detach(adap->fe[0]);
1101 adap->fe[0] = NULL;
1102 }
1103 d->rc_map = NULL;
1104 return -ENODEV;
1105 }
1106
Malcolm Priestley743ab6a12015-01-02 10:56:30 -03001107 st->fe_read_status = adap->fe[0]->ops.read_status;
Malcolm Priestley87169012015-01-02 10:56:31 -03001108 st->fe_read_signal_strength = adap->fe[0]->ops.read_signal_strength;
1109 st->fe_read_snr = adap->fe[0]->ops.read_snr;
1110 st->fe_read_ber = adap->fe[0]->ops.read_ber;
1111 st->fe_read_ucblocks = adap->fe[0]->ops.read_ucblocks;
Malcolm Priestley743ab6a12015-01-02 10:56:30 -03001112
1113 adap->fe[0]->ops.read_status = dm04_read_status;
Malcolm Priestleyd8bad1e2015-01-02 10:56:29 -03001114 adap->fe[0]->ops.read_signal_strength = dm04_read_signal_strength;
1115 adap->fe[0]->ops.read_snr = dm04_read_snr;
1116 adap->fe[0]->ops.read_ber = dm04_read_ber;
1117 adap->fe[0]->ops.read_ucblocks = dm04_read_ucblocks;
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001118 adap->fe[0]->ops.set_voltage = dm04_lme2510_set_voltage;
Malcolm Priestleyd8bad1e2015-01-02 10:56:29 -03001119
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001120 ret = lme_name(adap);
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001121 return ret;
1122}
1123
1124static int dm04_lme2510_tuner(struct dvb_usb_adapter *adap)
1125{
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001126 struct dvb_usb_device *d = adap_to_d(adap);
1127 struct lme2510_state *st = adap_to_priv(adap);
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001128 char *tun_msg[] = {"", "TDA8263", "IX2505V", "DVB_PLL_OPERA", "RS2000"};
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001129 int ret = 0;
1130
1131 switch (st->tuner_config) {
1132 case TUNER_LG:
Malcolm Priestley71dc98b2012-12-29 07:34:24 -03001133 if (dvb_attach(tda826x_attach, adap->fe[0], 0x60,
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001134 &d->i2c_adap, 1))
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001135 ret = st->tuner_config;
1136 break;
1137 case TUNER_S7395:
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001138 if (dvb_attach(ix2505v_attach , adap->fe[0], &lme_tuner,
1139 &d->i2c_adap))
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001140 ret = st->tuner_config;
1141 break;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -03001142 case TUNER_S0194:
Malcolm Priestley71dc98b2012-12-29 07:34:24 -03001143 if (dvb_attach(dvb_pll_attach , adap->fe[0], 0x60,
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001144 &d->i2c_adap, DVB_PLL_OPERA1))
Malcolm Priestley9d4540c2011-02-12 22:35:08 -03001145 ret = st->tuner_config;
1146 break;
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001147 case TUNER_RS2000:
1148 ret = st->tuner_config;
1149 break;
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001150 default:
1151 break;
1152 }
1153
1154 if (ret)
1155 info("TUN Found %s tuner", tun_msg[ret]);
1156 else {
Masanari Iida0d616ca2012-01-27 09:47:04 -03001157 info("TUN No tuner found --- resetting device");
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001158 lme_coldreset(d);
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001159 return -ENODEV;
1160 }
1161
Malcolm Priestley616a4b82011-05-25 18:28:29 -03001162 /* Start the Interrupt*/
1163 ret = lme2510_int_read(adap);
1164 if (ret < 0) {
1165 info("INT Unable to start Interrupt Service");
1166 return -ENODEV;
1167 }
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001168
1169 return ret;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001170}
1171
1172static int lme2510_powerup(struct dvb_usb_device *d, int onoff)
1173{
1174 struct lme2510_state *st = d->priv;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -03001175 static u8 lnb_on[] = LNB_ON;
1176 static u8 lnb_off[] = LNB_OFF;
1177 static u8 rbuf[1];
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001178 int ret = 0, len = 3, rlen = 1;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -03001179
Malcolm Priestley4f3b7fe2012-03-07 18:08:10 -03001180 mutex_lock(&d->i2c_mutex);
Malcolm Priestleyf23e6612010-10-27 19:50:36 -03001181
1182 if (onoff)
Alexey Khoroshilov6f030ab2011-04-15 17:40:17 -03001183 ret = lme2510_usb_talk(d, lnb_on, len, rbuf, rlen);
Malcolm Priestleyf23e6612010-10-27 19:50:36 -03001184 else
Alexey Khoroshilov6f030ab2011-04-15 17:40:17 -03001185 ret = lme2510_usb_talk(d, lnb_off, len, rbuf, rlen);
Malcolm Priestleyf23e6612010-10-27 19:50:36 -03001186
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001187 st->i2c_talk_onoff = 1;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -03001188
1189 mutex_unlock(&d->i2c_mutex);
1190
1191 return ret;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001192}
1193
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001194static int lme2510_get_adapter_count(struct dvb_usb_device *d)
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001195{
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001196 return 1;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001197}
1198
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001199static int lme2510_identify_state(struct dvb_usb_device *d, const char **name)
1200{
1201 struct lme2510_state *st = d->priv;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001202
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001203 usb_reset_configuration(d->udev);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001204
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001205 usb_set_interface(d->udev,
Antti Palosaarie42efde2013-07-30 16:33:16 -03001206 d->props->bInterfaceNumber, 1);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001207
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001208 st->dvb_usb_lme2510_firmware = dvb_usb_lme2510_firmware;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001209
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001210 if (lme2510_return_status(d) == 0x44) {
1211 *name = lme_firmware_switch(d, 0);
1212 return COLD;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001213 }
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001214
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001215 return 0;
1216}
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001217
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001218static int lme2510_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
1219 struct usb_data_stream_properties *stream)
1220{
1221 struct dvb_usb_adapter *adap = fe_to_adap(fe);
Syam Sidhardhan06f950f2013-02-24 18:47:18 -03001222 struct dvb_usb_device *d;
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001223
1224 if (adap == NULL)
1225 return 0;
Syam Sidhardhan06f950f2013-02-24 18:47:18 -03001226
1227 d = adap_to_d(adap);
1228
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001229 /* Turn PID filter on the fly by module option */
1230 if (pid_filter == 2) {
Mauro Carvalho Chehab235d89e2014-09-03 16:18:17 -03001231 adap->pid_filtering = true;
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001232 adap->max_feed_count = 15;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001233 }
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001234
1235 if (!(le16_to_cpu(d->udev->descriptor.idProduct)
1236 == 0x1122))
1237 stream->endpoint = 0x8;
1238
1239 return 0;
1240}
1241
1242static int lme2510_get_rc_config(struct dvb_usb_device *d,
1243 struct dvb_usb_rc *rc)
1244{
David Härdemanc003ab12012-10-11 19:11:54 -03001245 rc->allowed_protos = RC_BIT_NEC;
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001246 return 0;
1247}
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001248
Malcolm Priestleya760d632011-04-22 06:07:40 -03001249static void *lme2510_exit_int(struct dvb_usb_device *d)
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001250{
1251 struct lme2510_state *st = d->priv;
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001252 struct dvb_usb_adapter *adap = &d->adapter[0];
1253 void *buffer = NULL;
1254
1255 if (adap != NULL) {
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001256 lme2510_kill_urb(&adap->stream);
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001257 }
1258
Malcolm Priestley616a4b82011-05-25 18:28:29 -03001259 if (st->usb_buffer != NULL) {
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001260 st->i2c_talk_onoff = 1;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001261 st->signal_level = 0;
1262 st->signal_sn = 0;
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001263 buffer = st->usb_buffer;
Malcolm Priestley616a4b82011-05-25 18:28:29 -03001264 }
1265
1266 if (st->lme_urb != NULL) {
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001267 usb_kill_urb(st->lme_urb);
tvboxspy95c21c42011-09-11 19:26:50 -03001268 usb_free_coherent(d->udev, 128, st->buffer,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001269 st->lme_urb->transfer_dma);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001270 info("Interrupt Service Stopped");
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001271 }
Malcolm Priestley616a4b82011-05-25 18:28:29 -03001272
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001273 return buffer;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001274}
1275
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001276static void lme2510_exit(struct dvb_usb_device *d)
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001277{
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001278 void *usb_buffer;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001279
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001280 if (d != NULL) {
1281 usb_buffer = lme2510_exit_int(d);
Syam Sidhardhan72873e52013-03-06 16:44:46 -03001282 kfree(usb_buffer);
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001283 }
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001284}
1285
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001286static struct dvb_usb_device_properties lme2510_props = {
1287 .driver_name = KBUILD_MODNAME,
1288 .owner = THIS_MODULE,
1289 .bInterfaceNumber = 0,
1290 .adapter_nr = adapter_nr,
1291 .size_of_priv = sizeof(struct lme2510_state),
1292
1293 .download_firmware = lme2510_download_firmware,
1294
1295 .power_ctrl = lme2510_powerup,
1296 .identify_state = lme2510_identify_state,
1297 .i2c_algo = &lme2510_i2c_algo,
1298
1299 .frontend_attach = dm04_lme2510_frontend_attach,
1300 .tuner_attach = dm04_lme2510_tuner,
1301 .get_stream_config = lme2510_get_stream_config,
1302 .get_adapter_count = lme2510_get_adapter_count,
1303 .streaming_ctrl = lme2510_streaming_ctrl,
1304
1305 .get_rc_config = lme2510_get_rc_config,
1306
1307 .exit = lme2510_exit,
1308 .adapter = {
1309 {
1310 .caps = DVB_USB_ADAP_HAS_PID_FILTER|
1311 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1312 .pid_filter_count = 15,
1313 .pid_filter = lme2510_pid_filter,
1314 .pid_filter_ctrl = lme2510_pid_filter_ctrl,
1315 .stream =
1316 DVB_USB_STREAM_BULK(0x86, 10, 4096),
1317 },
1318 {
1319 }
1320 },
1321};
1322
1323static const struct usb_device_id lme2510_id_table[] = {
1324 { DVB_USB_DEVICE(0x3344, 0x1122, &lme2510_props,
1325 "DM04_LME2510_DVB-S", RC_MAP_LME2510) },
1326 { DVB_USB_DEVICE(0x3344, 0x1120, &lme2510_props,
1327 "DM04_LME2510C_DVB-S", RC_MAP_LME2510) },
1328 { DVB_USB_DEVICE(0x3344, 0x22f0, &lme2510_props,
1329 "DM04_LME2510C_DVB-S RS2000", RC_MAP_LME2510) },
1330 {} /* Terminating entry */
1331};
1332
1333MODULE_DEVICE_TABLE(usb, lme2510_id_table);
1334
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001335static struct usb_driver lme2510_driver = {
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001336 .name = KBUILD_MODNAME,
1337 .probe = dvb_usbv2_probe,
1338 .disconnect = dvb_usbv2_disconnect,
1339 .id_table = lme2510_id_table,
1340 .no_dynamic_id = 1,
1341 .soft_unbind = 1,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001342};
1343
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -08001344module_usb_driver(lme2510_driver);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001345
1346MODULE_AUTHOR("Malcolm Priestley <tvboxspy@gmail.com>");
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001347MODULE_DESCRIPTION("LME2510(C) DVB-S USB2.0");
David Härdemanf459aec2015-05-19 19:03:27 -03001348MODULE_VERSION("2.07");
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001349MODULE_LICENSE("GPL");
Malcolm Priestleye41543a2012-08-06 16:21:40 -03001350MODULE_FIRMWARE(LME2510_C_S7395);
1351MODULE_FIRMWARE(LME2510_C_LG);
1352MODULE_FIRMWARE(LME2510_C_S0194);
1353MODULE_FIRMWARE(LME2510_C_RS2000);
1354MODULE_FIRMWARE(LME2510_LG);
1355MODULE_FIRMWARE(LME2510_S0194);
1356