blob: f36f471deae26e0327299e2469d662e938b1caa9 [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 Priestleyd2f918b2010-09-02 17:29:30 -030022 * For firmware see Documentation/dvb/lmedm04.txt
23 *
24 * I2C addresses:
25 * 0xd0 - STV0288 - Demodulator
26 * 0xc0 - Sharp IX2505V - Tuner
Malcolm Priestley9d4540c2011-02-12 22:35:08 -030027 * --
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -030028 * 0x1c - TDA10086 - Demodulator
29 * 0xc0 - TDA8263 - Tuner
Malcolm Priestley9d4540c2011-02-12 22:35:08 -030030 * --
31 * 0xd0 - STV0299 - Demodulator
32 * 0xc0 - IX2410 - Tuner
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -030033 *
34 *
35 * VID = 3344 PID LME2510=1122 LME2510C=1120
36 *
37 * Copyright (C) 2010 Malcolm Priestley (tvboxspy@gmail.com)
38 * LME2510(C)(C) Leaguerme (Shenzhen) MicroElectronics Co., Ltd.
39 *
40 * This program is free software; you can redistribute it and/or modify
41 * it under the terms of the GNU General Public License Version 2, as
42 * published by the Free Software Foundation.
43 *
44 * This program is distributed in the hope that it will be useful,
45 * but WITHOUT ANY WARRANTY; without even the implied warranty of
46 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
47 * GNU General Public License for more details.
48 *
49 * You should have received a copy of the GNU General Public License
50 * along with this program; if not, write to the Free Software
51 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
52 *
53 *
54 * see Documentation/dvb/README.dvb-usb for more information
55 *
56 * Known Issues :
57 * LME2510: Non Intel USB chipsets fail to maintain High Speed on
58 * Boot or Hot Plug.
59 *
Malcolm Priestleyab599a62010-10-16 16:44:43 -030060 * QQbox suffers from noise on LNB voltage.
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -030061 *
Malcolm Priestley9d4540c2011-02-12 22:35:08 -030062 * LME2510: SHARP:BS2F7HZ0194(MV0194) cannot cold reset and share system
63 * with other tuners. After a cold reset streaming will not start.
64 *
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -030065 */
66#define DVB_USB_LOG_PREFIX "LME2510(C)"
67#include <linux/usb.h>
68#include <linux/usb/input.h>
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -030069#include <media/rc-core.h>
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -030070
71#include "dvb-usb.h"
72#include "lmedm04.h"
73#include "tda826x.h"
74#include "tda10086.h"
75#include "stv0288.h"
76#include "ix2505v.h"
Malcolm Priestley9d4540c2011-02-12 22:35:08 -030077#include "stv0299.h"
78#include "dvb-pll.h"
79#include "z0194a.h"
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -030080
81
82
83/* debug */
84static int dvb_usb_lme2510_debug;
85#define l_dprintk(var, level, args...) do { \
86 if ((var >= level)) \
87 printk(KERN_DEBUG DVB_USB_LOG_PREFIX ": " args); \
88} while (0)
89
90#define deb_info(level, args...) l_dprintk(dvb_usb_lme2510_debug, level, args)
91#define debug_data_snipet(level, name, p) \
92 deb_info(level, name" (%02x%02x%02x%02x%02x%02x%02x%02x)", \
93 *p, *(p+1), *(p+2), *(p+3), *(p+4), \
94 *(p+5), *(p+6), *(p+7));
95
96
97module_param_named(debug, dvb_usb_lme2510_debug, int, 0644);
98MODULE_PARM_DESC(debug, "set debugging level (1=info (or-able))."
99 DVB_USB_DEBUG_STATUS);
100
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300101static int dvb_usb_lme2510_firmware;
102module_param_named(firmware, dvb_usb_lme2510_firmware, int, 0644);
103MODULE_PARM_DESC(firmware, "set default firmware 0=Sharp7395 1=LG");
104
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300105static int pid_filter;
106module_param_named(pid, pid_filter, int, 0644);
107MODULE_PARM_DESC(pid, "set default 0=on 1=off");
108
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300109
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300110DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300111
112#define TUNER_DEFAULT 0x0
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300113#define TUNER_LG 0x1
114#define TUNER_S7395 0x2
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300115#define TUNER_S0194 0x3
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300116
117struct lme2510_state {
118 u8 id;
119 u8 tuner_config;
120 u8 signal_lock;
121 u8 signal_level;
122 u8 signal_sn;
123 u8 time_key;
124 u8 i2c_talk_onoff;
125 u8 i2c_gate;
126 u8 i2c_tuner_gate_w;
127 u8 i2c_tuner_gate_r;
128 u8 i2c_tuner_addr;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300129 u8 stream_on;
Malcolm Priestley449a0ba2011-04-22 06:20:57 -0300130 u8 pid_size;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300131 void *buffer;
132 struct urb *lme_urb;
133 void *usb_buffer;
134
135};
136
137static int lme2510_bulk_write(struct usb_device *dev,
138 u8 *snd, int len, u8 pipe)
139{
140 int ret, actual_l;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300141
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300142 ret = usb_bulk_msg(dev, usb_sndbulkpipe(dev, pipe),
Malcolm Priestley25ad9842010-11-04 17:17:51 -0300143 snd, len , &actual_l, 100);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300144 return ret;
145}
146
147static int lme2510_bulk_read(struct usb_device *dev,
148 u8 *rev, int len, u8 pipe)
149{
150 int ret, actual_l;
151
152 ret = usb_bulk_msg(dev, usb_rcvbulkpipe(dev, pipe),
Malcolm Priestley25ad9842010-11-04 17:17:51 -0300153 rev, len , &actual_l, 200);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300154 return ret;
155}
156
157static int lme2510_usb_talk(struct dvb_usb_device *d,
158 u8 *wbuf, int wlen, u8 *rbuf, int rlen)
159{
160 struct lme2510_state *st = d->priv;
161 u8 *buff;
162 int ret = 0;
163
164 if (st->usb_buffer == NULL) {
165 st->usb_buffer = kmalloc(512, GFP_KERNEL);
166 if (st->usb_buffer == NULL) {
167 info("MEM Error no memory");
168 return -ENOMEM;
169 }
170 }
171 buff = st->usb_buffer;
172
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300173 ret = mutex_lock_interruptible(&d->usb_mutex);
174
175 if (ret < 0)
176 return -EAGAIN;
177
Malcolm Priestleya760d632011-04-22 06:07:40 -0300178 /* the read/write capped at 512 */
179 memcpy(buff, wbuf, (wlen > 512) ? 512 : wlen);
180
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300181 ret |= usb_clear_halt(d->udev, usb_sndbulkpipe(d->udev, 0x01));
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300182
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300183 ret |= lme2510_bulk_write(d->udev, buff, wlen , 0x01);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300184
Malcolm Priestley25ad9842010-11-04 17:17:51 -0300185 msleep(10);
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300186
187 ret |= usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, 0x01));
188
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300189 ret |= lme2510_bulk_read(d->udev, buff, (rlen > 512) ?
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300190 512 : rlen , 0x01);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300191
192 if (rlen > 0)
193 memcpy(rbuf, buff, rlen);
194
195 mutex_unlock(&d->usb_mutex);
196
197 return (ret < 0) ? -ENODEV : 0;
198}
199
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300200static int lme2510_stream_restart(struct dvb_usb_device *d)
201{
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300202 static u8 stream_on[] = LME_ST_ON_W;
203 int ret;
204 u8 rbuff[10];
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300205 /*Restart Stream Command*/
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300206 ret = lme2510_usb_talk(d, stream_on, sizeof(stream_on),
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300207 rbuff, sizeof(rbuff));
208 return ret;
209}
Malcolm Priestleyf8931f52011-01-23 18:22:45 -0300210static int lme2510_remote_keypress(struct dvb_usb_adapter *adap, u32 keypress)
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300211{
212 struct dvb_usb_device *d = adap->dev;
213
214 deb_info(1, "INT Key Keypress =%04x", keypress);
215
216 if (keypress > 0)
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300217 rc_keydown(d->rc_dev, keypress, 0);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300218
219 return 0;
220}
221
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300222static int lme2510_enable_pid(struct dvb_usb_device *d, u8 index, u16 pid_out)
223{
224 struct lme2510_state *st = d->priv;
225 static u8 pid_buff[] = LME_ZERO_PID;
226 static u8 rbuf[1];
227 u8 pid_no = index * 2;
Malcolm Priestley449a0ba2011-04-22 06:20:57 -0300228 u8 pid_len = pid_no + 2;
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300229 int ret = 0;
230 deb_info(1, "PID Setting Pid %04x", pid_out);
231
Malcolm Priestley449a0ba2011-04-22 06:20:57 -0300232 if (st->pid_size == 0)
233 ret |= lme2510_stream_restart(d);
234
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300235 pid_buff[2] = pid_no;
236 pid_buff[3] = (u8)pid_out & 0xff;
237 pid_buff[4] = pid_no + 1;
238 pid_buff[5] = (u8)(pid_out >> 8);
239
Malcolm Priestley449a0ba2011-04-22 06:20:57 -0300240 if (pid_len > st->pid_size)
241 st->pid_size = pid_len;
242 pid_buff[7] = 0x80 + st->pid_size;
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300243
244 ret |= lme2510_usb_talk(d, pid_buff ,
245 sizeof(pid_buff) , rbuf, sizeof(rbuf));
246
Malcolm Priestley449a0ba2011-04-22 06:20:57 -0300247 if (st->stream_on)
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300248 ret |= lme2510_stream_restart(d);
249
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300250 return ret;
251}
252
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300253static void lme2510_int_response(struct urb *lme_urb)
254{
255 struct dvb_usb_adapter *adap = lme_urb->context;
256 struct lme2510_state *st = adap->dev->priv;
257 static u8 *ibuf, *rbuf;
258 int i = 0, offset;
259
260 switch (lme_urb->status) {
261 case 0:
262 case -ETIMEDOUT:
263 break;
264 case -ECONNRESET:
265 case -ENOENT:
266 case -ESHUTDOWN:
267 return;
268 default:
269 info("Error %x", lme_urb->status);
270 break;
271 }
272
273 rbuf = (u8 *) lme_urb->transfer_buffer;
274
275 offset = ((lme_urb->actual_length/8) > 4)
276 ? 4 : (lme_urb->actual_length/8) ;
277
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300278 for (i = 0; i < offset; ++i) {
279 ibuf = (u8 *)&rbuf[i*8];
280 deb_info(5, "INT O/S C =%02x C/O=%02x Type =%02x%02x",
281 offset, i, ibuf[0], ibuf[1]);
282
283 switch (ibuf[0]) {
284 case 0xaa:
285 debug_data_snipet(1, "INT Remote data snipet in", ibuf);
286 lme2510_remote_keypress(adap,
Malcolm Priestleyf8931f52011-01-23 18:22:45 -0300287 (u32)(ibuf[2] << 24) + (ibuf[3] << 16) +
288 (ibuf[4] << 8) + ibuf[5]);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300289 break;
290 case 0xbb:
291 switch (st->tuner_config) {
292 case TUNER_LG:
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300293 if (ibuf[2] > 0)
294 st->signal_lock = ibuf[2];
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300295 st->signal_level = ibuf[4];
296 st->signal_sn = ibuf[3];
297 st->time_key = ibuf[7];
298 break;
299 case TUNER_S7395:
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300300 case TUNER_S0194:
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300301 /* Tweak for earlier firmware*/
302 if (ibuf[1] == 0x03) {
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300303 if (ibuf[2] > 1)
304 st->signal_lock = ibuf[2];
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300305 st->signal_level = ibuf[3];
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300306 st->signal_sn = ibuf[4];
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300307 } else {
308 st->signal_level = ibuf[4];
309 st->signal_sn = ibuf[5];
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300310 st->signal_lock =
311 (st->signal_lock & 0xf7) +
312 ((ibuf[2] & 0x01) << 0x03);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300313 }
314 break;
315 default:
316 break;
317 }
318 debug_data_snipet(5, "INT Remote data snipet in", ibuf);
319 break;
320 case 0xcc:
321 debug_data_snipet(1, "INT Control data snipet", ibuf);
322 break;
323 default:
324 debug_data_snipet(1, "INT Unknown data snipet", ibuf);
325 break;
326 }
327 }
328 usb_submit_urb(lme_urb, GFP_ATOMIC);
329}
330
331static int lme2510_int_read(struct dvb_usb_adapter *adap)
332{
333 struct lme2510_state *lme_int = adap->dev->priv;
334
335 lme_int->lme_urb = usb_alloc_urb(0, GFP_ATOMIC);
336
337 if (lme_int->lme_urb == NULL)
338 return -ENOMEM;
339
340 lme_int->buffer = usb_alloc_coherent(adap->dev->udev, 5000, GFP_ATOMIC,
341 &lme_int->lme_urb->transfer_dma);
342
343 if (lme_int->buffer == NULL)
344 return -ENOMEM;
345
346 usb_fill_int_urb(lme_int->lme_urb,
347 adap->dev->udev,
348 usb_rcvintpipe(adap->dev->udev, 0xa),
349 lme_int->buffer,
350 4096,
351 lme2510_int_response,
352 adap,
353 11);
354
355 lme_int->lme_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
356
357 usb_submit_urb(lme_int->lme_urb, GFP_ATOMIC);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300358 info("INT Interrupt Service Started");
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300359
360 return 0;
361}
362
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300363static int lme2510_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
364{
Malcolm Priestley449a0ba2011-04-22 06:20:57 -0300365 struct lme2510_state *st = adap->dev->priv;
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300366 static u8 clear_pid_reg[] = LME_CLEAR_PID;
367 static u8 rbuf[1];
Malcolm Priestley449a0ba2011-04-22 06:20:57 -0300368 int ret;
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300369
370 deb_info(1, "PID Clearing Filter");
371
372 ret = mutex_lock_interruptible(&adap->dev->i2c_mutex);
Malcolm Priestley449a0ba2011-04-22 06:20:57 -0300373 if (ret < 0)
374 return -EAGAIN;
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300375
376 if (!onoff)
377 ret |= lme2510_usb_talk(adap->dev, clear_pid_reg,
378 sizeof(clear_pid_reg), rbuf, sizeof(rbuf));
379
Malcolm Priestley449a0ba2011-04-22 06:20:57 -0300380 st->pid_size = 0;
381
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300382 mutex_unlock(&adap->dev->i2c_mutex);
383
384 return 0;
385}
386
387static int lme2510_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
388 int onoff)
389{
390 int ret = 0;
391
392 deb_info(3, "%s PID=%04x Index=%04x onoff=%02x", __func__,
393 pid, index, onoff);
394
395 if (onoff)
Malcolm Priestley449a0ba2011-04-22 06:20:57 -0300396 if (!pid_filter) {
397 ret = mutex_lock_interruptible(&adap->dev->i2c_mutex);
398 if (ret < 0)
399 return -EAGAIN;
400 ret |= lme2510_enable_pid(adap->dev, index, pid);
401 mutex_unlock(&adap->dev->i2c_mutex);
402 }
403
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300404
405 return ret;
406}
407
408
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300409static int lme2510_return_status(struct usb_device *dev)
410{
411 int ret = 0;
Florian Mickler945b8762011-03-20 18:50:52 -0300412 u8 *data;
413
414 data = kzalloc(10, GFP_KERNEL);
415 if (!data)
416 return -ENOMEM;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300417
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300418 ret |= usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
419 0x06, 0x80, 0x0302, 0x00, data, 0x0006, 200);
420 info("Firmware Status: %x (%x)", ret , data[2]);
421
Florian Mickler945b8762011-03-20 18:50:52 -0300422 ret = (ret < 0) ? -ENODEV : data[2];
423 kfree(data);
424 return ret;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300425}
426
427static int lme2510_msg(struct dvb_usb_device *d,
428 u8 *wbuf, int wlen, u8 *rbuf, int rlen)
429{
430 int ret = 0;
431 struct lme2510_state *st = d->priv;
432
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300433 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300434 return -EAGAIN;
435
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300436 if (st->i2c_talk_onoff == 1) {
437
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300438 ret = lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
439
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300440 switch (st->tuner_config) {
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300441 case TUNER_LG:
442 if (wbuf[2] == 0x1c) {
443 if (wbuf[3] == 0x0e) {
444 st->signal_lock = rbuf[1];
445 if ((st->stream_on & 1) &&
446 (st->signal_lock & 0x10)) {
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300447 lme2510_stream_restart(d);
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300448 st->i2c_talk_onoff = 0;
449 }
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300450 msleep(80);
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300451 }
452 }
453 break;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300454 case TUNER_S7395:
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300455 if (wbuf[2] == 0xd0) {
456 if (wbuf[3] == 0x24) {
457 st->signal_lock = rbuf[1];
458 if ((st->stream_on & 1) &&
459 (st->signal_lock & 0x8)) {
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300460 lme2510_stream_restart(d);
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300461 st->i2c_talk_onoff = 0;
462 }
463 }
464 if ((wbuf[3] != 0x6) & (wbuf[3] != 0x5))
465 msleep(5);
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300466 }
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300467 break;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300468 case TUNER_S0194:
469 if (wbuf[2] == 0xd0) {
470 if (wbuf[3] == 0x1b) {
471 st->signal_lock = rbuf[1];
472 if ((st->stream_on & 1) &&
473 (st->signal_lock & 0x8)) {
474 lme2510_stream_restart(d);
475 st->i2c_talk_onoff = 0;
476 }
477 }
478 }
479 break;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300480 default:
481 break;
482 }
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300483 } else {
484 switch (st->tuner_config) {
485 case TUNER_LG:
486 switch (wbuf[3]) {
487 case 0x0e:
488 rbuf[0] = 0x55;
489 rbuf[1] = st->signal_lock;
490 break;
491 case 0x43:
492 rbuf[0] = 0x55;
493 rbuf[1] = st->signal_level;
494 break;
495 case 0x1c:
496 rbuf[0] = 0x55;
497 rbuf[1] = st->signal_sn;
498 break;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300499 case 0x15:
500 case 0x16:
501 case 0x17:
502 case 0x18:
503 rbuf[0] = 0x55;
504 rbuf[1] = 0x00;
505 break;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300506 default:
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300507 lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
508 st->i2c_talk_onoff = 1;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300509 break;
510 }
511 break;
512 case TUNER_S7395:
513 switch (wbuf[3]) {
514 case 0x10:
515 rbuf[0] = 0x55;
516 rbuf[1] = (st->signal_level & 0x80)
517 ? 0 : (st->signal_level * 2);
518 break;
519 case 0x2d:
520 rbuf[0] = 0x55;
521 rbuf[1] = st->signal_sn;
522 break;
523 case 0x24:
524 rbuf[0] = 0x55;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300525 rbuf[1] = st->signal_lock;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300526 break;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300527 case 0x2e:
528 case 0x26:
529 case 0x27:
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300530 rbuf[0] = 0x55;
531 rbuf[1] = 0x00;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300532 break;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300533 default:
534 lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
535 st->i2c_talk_onoff = 1;
536 break;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300537 }
538 break;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300539 case TUNER_S0194:
540 switch (wbuf[3]) {
541 case 0x18:
542 rbuf[0] = 0x55;
543 rbuf[1] = (st->signal_level & 0x80)
544 ? 0 : (st->signal_level * 2);
545 break;
546 case 0x24:
547 rbuf[0] = 0x55;
548 rbuf[1] = st->signal_sn;
549 break;
550 case 0x1b:
551 rbuf[0] = 0x55;
552 rbuf[1] = st->signal_lock;
553 break;
554 case 0x19:
555 case 0x25:
556 case 0x1e:
557 case 0x1d:
558 rbuf[0] = 0x55;
559 rbuf[1] = 0x00;
560 break;
561 default:
562 lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
563 st->i2c_talk_onoff = 1;
564 break;
565 }
566 break;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300567 default:
568 break;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300569 }
570
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300571 deb_info(4, "I2C From Interrupt Message out(%02x) in(%02x)",
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300572 wbuf[3], rbuf[1]);
573
574 }
575
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300576 mutex_unlock(&d->i2c_mutex);
577
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300578 return ret;
579}
580
581
582static int lme2510_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
583 int num)
584{
585 struct dvb_usb_device *d = i2c_get_adapdata(adap);
586 struct lme2510_state *st = d->priv;
587 static u8 obuf[64], ibuf[512];
588 int i, read, read_o;
589 u16 len;
590 u8 gate = st->i2c_gate;
591
592 if (gate == 0)
593 gate = 5;
594
595 if (num > 2)
596 warn("more than 2 i2c messages"
597 "at a time is not handled yet. TODO.");
598
599 for (i = 0; i < num; i++) {
600 read_o = 1 & (msg[i].flags & I2C_M_RD);
601 read = i+1 < num && (msg[i+1].flags & I2C_M_RD);
602 read |= read_o;
603 gate = (msg[i].addr == st->i2c_tuner_addr)
604 ? (read) ? st->i2c_tuner_gate_r
605 : st->i2c_tuner_gate_w
606 : st->i2c_gate;
607 obuf[0] = gate | (read << 7);
608
609 if (gate == 5)
610 obuf[1] = (read) ? 2 : msg[i].len + 1;
611 else
612 obuf[1] = msg[i].len + read + 1;
613
614 obuf[2] = msg[i].addr;
615 if (read) {
616 if (read_o)
617 len = 3;
618 else {
619 memcpy(&obuf[3], msg[i].buf, msg[i].len);
620 obuf[msg[i].len+3] = msg[i+1].len;
621 len = msg[i].len+4;
622 }
623 } else {
624 memcpy(&obuf[3], msg[i].buf, msg[i].len);
625 len = msg[i].len+3;
626 }
627
628 if (lme2510_msg(d, obuf, len, ibuf, 512) < 0) {
629 deb_info(1, "i2c transfer failed.");
630 return -EAGAIN;
631 }
632
633 if (read) {
634 if (read_o)
635 memcpy(msg[i].buf, &ibuf[1], msg[i].len);
636 else {
637 memcpy(msg[i+1].buf, &ibuf[1], msg[i+1].len);
638 i++;
639 }
640 }
641 }
642 return i;
643}
644
645static u32 lme2510_i2c_func(struct i2c_adapter *adapter)
646{
647 return I2C_FUNC_I2C;
648}
649
650static struct i2c_algorithm lme2510_i2c_algo = {
651 .master_xfer = lme2510_i2c_xfer,
652 .functionality = lme2510_i2c_func,
653};
654
655/* Callbacks for DVB USB */
656static int lme2510_identify_state(struct usb_device *udev,
657 struct dvb_usb_device_properties *props,
658 struct dvb_usb_device_description **desc,
659 int *cold)
660{
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300661 *cold = 0;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300662 return 0;
663}
664
665static int lme2510_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
666{
667 struct lme2510_state *st = adap->dev->priv;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300668 static u8 clear_reg_3[] = LME_CLEAR_PID;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300669 static u8 rbuf[1];
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300670 int ret = 0, rlen = sizeof(rbuf);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300671
672 deb_info(1, "STM (%02x)", onoff);
673
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300674 /* Streaming is started by FE_HAS_LOCK */
675 if (onoff == 1)
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300676 st->stream_on = 1;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300677 else {
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300678 deb_info(1, "STM Steam Off");
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300679 /* mutex is here only to avoid collision with I2C */
Alexey Khoroshilov6f030ab2011-04-15 17:40:17 -0300680 if (mutex_lock_interruptible(&adap->dev->i2c_mutex) < 0)
681 return -EAGAIN;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300682
Alexey Khoroshilov6f030ab2011-04-15 17:40:17 -0300683 ret = lme2510_usb_talk(adap->dev, clear_reg_3,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300684 sizeof(clear_reg_3), rbuf, rlen);
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300685 st->stream_on = 0;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300686 st->i2c_talk_onoff = 1;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300687
688 mutex_unlock(&adap->dev->i2c_mutex);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300689 }
690
691 return (ret < 0) ? -ENODEV : 0;
692}
693
694static int lme2510_int_service(struct dvb_usb_adapter *adap)
695{
696 struct dvb_usb_device *d = adap->dev;
David Härdemand8b4b582010-10-29 16:08:23 -0300697 struct rc_dev *rc;
698 int ret;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300699
700 info("STA Configuring Remote");
701
David Härdemand8b4b582010-10-29 16:08:23 -0300702 rc = rc_allocate_device();
703 if (!rc)
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300704 return -ENOMEM;
705
David Härdemand8b4b582010-10-29 16:08:23 -0300706 usb_make_path(d->udev, d->rc_phys, sizeof(d->rc_phys));
707 strlcat(d->rc_phys, "/ir0", sizeof(d->rc_phys));
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300708
David Härdemand8b4b582010-10-29 16:08:23 -0300709 rc->input_name = "LME2510 Remote Control";
710 rc->input_phys = d->rc_phys;
711 rc->map_name = RC_MAP_LME2510;
712 rc->driver_name = "LME 2510";
713 usb_to_input_id(d->udev, &rc->input_id);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300714
David Härdemand8b4b582010-10-29 16:08:23 -0300715 ret = rc_register_device(rc);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300716 if (ret) {
David Härdemand8b4b582010-10-29 16:08:23 -0300717 rc_free_device(rc);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300718 return ret;
719 }
David Härdemand8b4b582010-10-29 16:08:23 -0300720 d->rc_dev = rc;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300721
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300722 /* Start the Interrupt */
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300723 ret = lme2510_int_read(adap);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300724 if (ret < 0) {
David Härdemand8b4b582010-10-29 16:08:23 -0300725 rc_unregister_device(rc);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300726 info("INT Unable to start Interrupt Service");
David Härdemand8b4b582010-10-29 16:08:23 -0300727 return -ENODEV;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300728 }
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300729
David Härdemand8b4b582010-10-29 16:08:23 -0300730 return 0;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300731}
732
733static u8 check_sum(u8 *p, u8 len)
734{
735 u8 sum = 0;
736 while (len--)
737 sum += *p++;
738 return sum;
739}
740
741static int lme2510_download_firmware(struct usb_device *dev,
742 const struct firmware *fw)
743{
744 int ret = 0;
Florian Mickler945b8762011-03-20 18:50:52 -0300745 u8 *data;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300746 u16 j, wlen, len_in, start, end;
747 u8 packet_size, dlen, i;
748 u8 *fw_data;
749
750 packet_size = 0x31;
751 len_in = 1;
752
Florian Mickler945b8762011-03-20 18:50:52 -0300753 data = kzalloc(512, GFP_KERNEL);
754 if (!data) {
755 info("FRM Could not start Firmware Download (Buffer allocation failed)");
756 return -ENOMEM;
757 }
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300758
759 info("FRM Starting Firmware Download");
760
761 for (i = 1; i < 3; i++) {
762 start = (i == 1) ? 0 : 512;
763 end = (i == 1) ? 512 : fw->size;
764 for (j = start; j < end; j += (packet_size+1)) {
765 fw_data = (u8 *)(fw->data + j);
766 if ((end - j) > packet_size) {
767 data[0] = i;
768 dlen = packet_size;
769 } else {
770 data[0] = i | 0x80;
771 dlen = (u8)(end - j)-1;
772 }
Florian Micklerb9f7b732011-03-20 18:50:51 -0300773 data[1] = dlen;
774 memcpy(&data[2], fw_data, dlen+1);
775 wlen = (u8) dlen + 4;
776 data[wlen-1] = check_sum(fw_data, dlen+1);
777 deb_info(1, "Data S=%02x:E=%02x CS= %02x", data[3],
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300778 data[dlen+2], data[dlen+3]);
Florian Micklerb9f7b732011-03-20 18:50:51 -0300779 ret |= lme2510_bulk_write(dev, data, wlen, 1);
780 ret |= lme2510_bulk_read(dev, data, len_in , 1);
781 ret |= (data[0] == 0x88) ? 0 : -1;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300782 }
783 }
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300784
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300785 usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
786 0x06, 0x80, 0x0200, 0x00, data, 0x0109, 1000);
787
788
789 data[0] = 0x8a;
790 len_in = 1;
791 msleep(2000);
792 ret |= lme2510_bulk_write(dev, data , len_in, 1); /*Resetting*/
793 ret |= lme2510_bulk_read(dev, data, len_in, 1);
794 msleep(400);
795
796 if (ret < 0)
797 info("FRM Firmware Download Failed (%04x)" , ret);
798 else
799 info("FRM Firmware Download Completed - Resetting Device");
800
Florian Mickler945b8762011-03-20 18:50:52 -0300801 kfree(data);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300802 return (ret < 0) ? -ENODEV : 0;
803}
804
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300805static void lme_coldreset(struct usb_device *dev)
806{
807 int ret = 0, len_in;
808 u8 data[512] = {0};
809
810 data[0] = 0x0a;
811 len_in = 1;
812 info("FRM Firmware Cold Reset");
813 ret |= lme2510_bulk_write(dev, data , len_in, 1); /*Cold Resetting*/
814 ret |= lme2510_bulk_read(dev, data, len_in, 1);
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300815
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300816 return;
817}
818
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300819static int lme_firmware_switch(struct usb_device *udev, int cold)
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300820{
821 const struct firmware *fw = NULL;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300822 const char fw_c_s7395[] = "dvb-usb-lme2510c-s7395.fw";
823 const char fw_c_lg[] = "dvb-usb-lme2510c-lg.fw";
824 const char fw_c_s0194[] = "dvb-usb-lme2510c-s0194.fw";
825 const char fw_lg[] = "dvb-usb-lme2510-lg.fw";
826 const char fw_s0194[] = "dvb-usb-lme2510-s0194.fw";
827 const char *fw_lme;
828 int ret, cold_fw;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300829
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300830 cold = (cold > 0) ? (cold & 1) : 0;
831
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300832 cold_fw = !cold;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300833
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300834 if (udev->descriptor.idProduct == 0x1122) {
835 switch (dvb_usb_lme2510_firmware) {
836 default:
837 dvb_usb_lme2510_firmware = TUNER_S0194;
838 case TUNER_S0194:
839 fw_lme = fw_s0194;
840 ret = request_firmware(&fw, fw_lme, &udev->dev);
841 if (ret == 0) {
Malcolm Priestley30bd3b52011-02-13 20:12:15 -0300842 cold = 0;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300843 break;
844 }
845 dvb_usb_lme2510_firmware = TUNER_LG;
846 case TUNER_LG:
847 fw_lme = fw_lg;
848 ret = request_firmware(&fw, fw_lme, &udev->dev);
849 if (ret == 0)
850 break;
851 info("FRM No Firmware Found - please install");
852 dvb_usb_lme2510_firmware = TUNER_DEFAULT;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300853 cold = 0;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300854 cold_fw = 0;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300855 break;
856 }
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300857 } else {
858 switch (dvb_usb_lme2510_firmware) {
859 default:
860 dvb_usb_lme2510_firmware = TUNER_S7395;
861 case TUNER_S7395:
862 fw_lme = fw_c_s7395;
863 ret = request_firmware(&fw, fw_lme, &udev->dev);
Malcolm Priestley30bd3b52011-02-13 20:12:15 -0300864 if (ret == 0) {
865 cold = 0;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300866 break;
Malcolm Priestley30bd3b52011-02-13 20:12:15 -0300867 }
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300868 dvb_usb_lme2510_firmware = TUNER_LG;
869 case TUNER_LG:
870 fw_lme = fw_c_lg;
871 ret = request_firmware(&fw, fw_lme, &udev->dev);
872 if (ret == 0)
873 break;
874 dvb_usb_lme2510_firmware = TUNER_S0194;
875 case TUNER_S0194:
876 fw_lme = fw_c_s0194;
877 ret = request_firmware(&fw, fw_lme, &udev->dev);
878 if (ret == 0)
879 break;
880 info("FRM No Firmware Found - please install");
881 dvb_usb_lme2510_firmware = TUNER_DEFAULT;
882 cold = 0;
883 cold_fw = 0;
884 break;
885 }
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300886 }
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300887
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300888 if (cold_fw) {
889 info("FRM Loading %s file", fw_lme);
890 ret = lme2510_download_firmware(udev, fw);
891 }
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300892
Malcolm Priestley30bd3b52011-02-13 20:12:15 -0300893 release_firmware(fw);
894
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300895 if (cold) {
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300896 info("FRM Changing to %s firmware", fw_lme);
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300897 lme_coldreset(udev);
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300898 return -ENODEV;
899 }
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300900
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300901 return ret;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300902}
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300903
904static int lme2510_kill_urb(struct usb_data_stream *stream)
905{
906 int i;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300907
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300908 for (i = 0; i < stream->urbs_submitted; i++) {
909 deb_info(3, "killing URB no. %d.", i);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300910 /* stop the URB */
911 usb_kill_urb(stream->urb_list[i]);
912 }
913 stream->urbs_submitted = 0;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300914
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300915 return 0;
916}
917
918static struct tda10086_config tda10086_config = {
919 .demod_address = 0x1c,
920 .invert = 0,
921 .diseqc_tone = 1,
922 .xtal_freq = TDA10086_XTAL_16M,
923};
924
925static struct stv0288_config lme_config = {
926 .demod_address = 0xd0,
927 .min_delay_ms = 15,
928 .inittab = s7395_inittab,
929};
930
931static struct ix2505v_config lme_tuner = {
932 .tuner_address = 0xc0,
933 .min_delay_ms = 100,
934 .tuner_gain = 0x0,
935 .tuner_chargepump = 0x3,
936};
937
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300938static struct stv0299_config sharp_z0194_config = {
939 .demod_address = 0xd0,
940 .inittab = sharp_z0194a_inittab,
941 .mclk = 88000000UL,
942 .invert = 0,
943 .skip_reinit = 0,
944 .lock_output = STV0299_LOCKOUTPUT_1,
945 .volt13_op0_op1 = STV0299_VOLT13_OP1,
946 .min_delay_ms = 100,
947 .set_symbol_rate = sharp_z0194a_set_symbol_rate,
948};
949
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300950static int dm04_lme2510_set_voltage(struct dvb_frontend *fe,
951 fe_sec_voltage_t voltage)
952{
953 struct dvb_usb_adapter *adap = fe->dvb->priv;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300954 static u8 voltage_low[] = LME_VOLTAGE_L;
955 static u8 voltage_high[] = LME_VOLTAGE_H;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300956 static u8 rbuf[1];
957 int ret = 0, len = 3, rlen = 1;
958
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300959 if (mutex_lock_interruptible(&adap->dev->i2c_mutex) < 0)
960 return -EAGAIN;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300961
962 switch (voltage) {
963 case SEC_VOLTAGE_18:
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300964 ret |= lme2510_usb_talk(adap->dev,
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300965 voltage_high, len, rbuf, rlen);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300966 break;
967
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300968 case SEC_VOLTAGE_OFF:
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300969 case SEC_VOLTAGE_13:
970 default:
971 ret |= lme2510_usb_talk(adap->dev,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300972 voltage_low, len, rbuf, rlen);
973 break;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300974 }
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300975
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300976 mutex_unlock(&adap->dev->i2c_mutex);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300977
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300978 return (ret < 0) ? -ENODEV : 0;
979}
980
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300981static int lme_name(struct dvb_usb_adapter *adap)
982{
983 struct lme2510_state *st = adap->dev->priv;
984 const char *desc = adap->dev->desc->name;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300985 char *fe_name[] = {"", " LG TDQY-P001F", " SHARP:BS2F7HZ7395",
986 " SHARP:BS2F7HZ0194"};
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300987 char *name = adap->fe->ops.info.name;
988
989 strlcpy(name, desc, 128);
990 strlcat(name, fe_name[st->tuner_config], 128);
991
992 return 0;
993}
994
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300995static int dm04_lme2510_frontend_attach(struct dvb_usb_adapter *adap)
996{
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300997 struct lme2510_state *st = adap->dev->priv;
998
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300999 int ret = 0;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001000
1001 st->i2c_talk_onoff = 1;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001002
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001003 st->i2c_gate = 4;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001004 adap->fe = dvb_attach(tda10086_attach, &tda10086_config,
1005 &adap->dev->i2c_adap);
1006
1007 if (adap->fe) {
1008 info("TUN Found Frontend TDA10086");
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001009 st->i2c_tuner_gate_w = 4;
1010 st->i2c_tuner_gate_r = 4;
1011 st->i2c_tuner_addr = 0xc0;
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001012 st->tuner_config = TUNER_LG;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -03001013 if (dvb_usb_lme2510_firmware != TUNER_LG) {
1014 dvb_usb_lme2510_firmware = TUNER_LG;
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001015 ret = lme_firmware_switch(adap->dev->udev, 1);
Malcolm Priestley9d4540c2011-02-12 22:35:08 -03001016 }
1017 goto end;
1018 }
1019
1020 st->i2c_gate = 4;
1021 adap->fe = dvb_attach(stv0299_attach, &sharp_z0194_config,
1022 &adap->dev->i2c_adap);
1023 if (adap->fe) {
1024 info("FE Found Stv0299");
1025 st->i2c_tuner_gate_w = 4;
1026 st->i2c_tuner_gate_r = 5;
1027 st->i2c_tuner_addr = 0xc0;
1028 st->tuner_config = TUNER_S0194;
1029 if (dvb_usb_lme2510_firmware != TUNER_S0194) {
1030 dvb_usb_lme2510_firmware = TUNER_S0194;
1031 ret = lme_firmware_switch(adap->dev->udev, 1);
1032 }
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001033 goto end;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001034 }
Malcolm Priestleyf23e6612010-10-27 19:50:36 -03001035
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001036 st->i2c_gate = 5;
1037 adap->fe = dvb_attach(stv0288_attach, &lme_config,
1038 &adap->dev->i2c_adap);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001039 if (adap->fe) {
1040 info("FE Found Stv0288");
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001041 st->i2c_tuner_gate_w = 4;
1042 st->i2c_tuner_gate_r = 5;
1043 st->i2c_tuner_addr = 0xc0;
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001044 st->tuner_config = TUNER_S7395;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -03001045 if (dvb_usb_lme2510_firmware != TUNER_S7395) {
1046 dvb_usb_lme2510_firmware = TUNER_S7395;
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001047 ret = lme_firmware_switch(adap->dev->udev, 1);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001048 }
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001049 } else {
1050 info("DM04 Not Supported");
1051 return -ENODEV;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001052 }
1053
Malcolm Priestley9d4540c2011-02-12 22:35:08 -03001054
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001055end: if (ret) {
1056 kfree(adap->fe);
1057 adap->fe = NULL;
1058 return -ENODEV;
1059 }
1060
1061 adap->fe->ops.set_voltage = dm04_lme2510_set_voltage;
1062 ret = lme_name(adap);
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001063 return ret;
1064}
1065
1066static int dm04_lme2510_tuner(struct dvb_usb_adapter *adap)
1067{
1068 struct lme2510_state *st = adap->dev->priv;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -03001069 char *tun_msg[] = {"", "TDA8263", "IX2505V", "DVB_PLL_OPERA"};
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001070 int ret = 0;
1071
1072 switch (st->tuner_config) {
1073 case TUNER_LG:
1074 if (dvb_attach(tda826x_attach, adap->fe, 0xc0,
1075 &adap->dev->i2c_adap, 1))
1076 ret = st->tuner_config;
1077 break;
1078 case TUNER_S7395:
1079 if (dvb_attach(ix2505v_attach , adap->fe, &lme_tuner,
1080 &adap->dev->i2c_adap))
1081 ret = st->tuner_config;
1082 break;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -03001083 case TUNER_S0194:
1084 if (dvb_attach(dvb_pll_attach , adap->fe, 0xc0,
1085 &adap->dev->i2c_adap, DVB_PLL_OPERA1))
1086 ret = st->tuner_config;
1087 break;
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001088 default:
1089 break;
1090 }
1091
1092 if (ret)
1093 info("TUN Found %s tuner", tun_msg[ret]);
1094 else {
1095 info("TUN No tuner found --- reseting device");
1096 lme_coldreset(adap->dev->udev);
1097 return -ENODEV;
1098 }
1099
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001100 /* Start the Interrupt & Remote*/
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001101 ret = lme2510_int_service(adap);
1102
1103 return ret;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001104}
1105
1106static int lme2510_powerup(struct dvb_usb_device *d, int onoff)
1107{
1108 struct lme2510_state *st = d->priv;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -03001109 static u8 lnb_on[] = LNB_ON;
1110 static u8 lnb_off[] = LNB_OFF;
1111 static u8 rbuf[1];
1112 int ret, len = 3, rlen = 1;
1113
Alexey Khoroshilov6f030ab2011-04-15 17:40:17 -03001114 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
1115 return -EAGAIN;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -03001116
1117 if (onoff)
Alexey Khoroshilov6f030ab2011-04-15 17:40:17 -03001118 ret = lme2510_usb_talk(d, lnb_on, len, rbuf, rlen);
Malcolm Priestleyf23e6612010-10-27 19:50:36 -03001119 else
Alexey Khoroshilov6f030ab2011-04-15 17:40:17 -03001120 ret = lme2510_usb_talk(d, lnb_off, len, rbuf, rlen);
Malcolm Priestleyf23e6612010-10-27 19:50:36 -03001121
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001122 st->i2c_talk_onoff = 1;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -03001123
1124 mutex_unlock(&d->i2c_mutex);
1125
1126 return ret;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001127}
1128
1129/* DVB USB Driver stuff */
1130static struct dvb_usb_device_properties lme2510_properties;
1131static struct dvb_usb_device_properties lme2510c_properties;
1132
1133static int lme2510_probe(struct usb_interface *intf,
1134 const struct usb_device_id *id)
1135{
1136 struct usb_device *udev = interface_to_usbdev(intf);
1137 int ret = 0;
1138
1139 usb_reset_configuration(udev);
1140
1141 usb_set_interface(udev, intf->cur_altsetting->desc.bInterfaceNumber, 1);
1142
1143 if (udev->speed != USB_SPEED_HIGH) {
1144 ret = usb_reset_device(udev);
1145 info("DEV Failed to connect in HIGH SPEED mode");
1146 return -ENODEV;
1147 }
1148
Malcolm Priestley9d4540c2011-02-12 22:35:08 -03001149 if (lme2510_return_status(udev) == 0x44) {
1150 lme_firmware_switch(udev, 0);
1151 return -ENODEV;
1152 }
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001153
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001154 if (0 == dvb_usb_device_init(intf, &lme2510_properties,
1155 THIS_MODULE, NULL, adapter_nr)) {
1156 info("DEV registering device driver");
1157 return 0;
1158 }
1159 if (0 == dvb_usb_device_init(intf, &lme2510c_properties,
1160 THIS_MODULE, NULL, adapter_nr)) {
1161 info("DEV registering device driver");
1162 return 0;
1163 }
1164
1165 info("DEV lme2510 Error");
1166 return -ENODEV;
1167
1168}
1169
1170static struct usb_device_id lme2510_table[] = {
1171 { USB_DEVICE(0x3344, 0x1122) }, /* LME2510 */
1172 { USB_DEVICE(0x3344, 0x1120) }, /* LME2510C */
1173 {} /* Terminating entry */
1174};
1175
1176MODULE_DEVICE_TABLE(usb, lme2510_table);
1177
1178static struct dvb_usb_device_properties lme2510_properties = {
1179 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001180 .size_of_priv = sizeof(struct lme2510_state),
1181 .num_adapters = 1,
1182 .adapter = {
1183 {
Malcolm Priestleyeb02d852011-04-02 18:59:29 -03001184 .caps = DVB_USB_ADAP_HAS_PID_FILTER|
1185 DVB_USB_ADAP_NEED_PID_FILTERING|
1186 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001187 .streaming_ctrl = lme2510_streaming_ctrl,
Malcolm Priestleyeb02d852011-04-02 18:59:29 -03001188 .pid_filter_count = 15,
1189 .pid_filter = lme2510_pid_filter,
1190 .pid_filter_ctrl = lme2510_pid_filter_ctrl,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001191 .frontend_attach = dm04_lme2510_frontend_attach,
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001192 .tuner_attach = dm04_lme2510_tuner,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001193 /* parameter for the MPEG2-data transfer */
1194 .stream = {
1195 .type = USB_BULK,
1196 .count = 10,
1197 .endpoint = 0x06,
1198 .u = {
1199 .bulk = {
1200 .buffersize = 4096,
1201
1202 }
1203 }
1204 }
1205 }
1206 },
1207 .power_ctrl = lme2510_powerup,
1208 .identify_state = lme2510_identify_state,
1209 .i2c_algo = &lme2510_i2c_algo,
1210 .generic_bulk_ctrl_endpoint = 0,
1211 .num_device_descs = 1,
1212 .devices = {
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001213 { "DM04_LME2510_DVB-S",
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001214 { &lme2510_table[0], NULL },
1215 },
1216
1217 }
1218};
1219
1220static struct dvb_usb_device_properties lme2510c_properties = {
1221 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001222 .size_of_priv = sizeof(struct lme2510_state),
1223 .num_adapters = 1,
1224 .adapter = {
1225 {
Malcolm Priestleyeb02d852011-04-02 18:59:29 -03001226 .caps = DVB_USB_ADAP_HAS_PID_FILTER|
1227 DVB_USB_ADAP_NEED_PID_FILTERING|
1228 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001229 .streaming_ctrl = lme2510_streaming_ctrl,
Malcolm Priestleyeb02d852011-04-02 18:59:29 -03001230 .pid_filter_count = 15,
1231 .pid_filter = lme2510_pid_filter,
1232 .pid_filter_ctrl = lme2510_pid_filter_ctrl,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001233 .frontend_attach = dm04_lme2510_frontend_attach,
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001234 .tuner_attach = dm04_lme2510_tuner,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001235 /* parameter for the MPEG2-data transfer */
1236 .stream = {
1237 .type = USB_BULK,
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001238 .count = 10,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001239 .endpoint = 0x8,
1240 .u = {
1241 .bulk = {
1242 .buffersize = 4096,
1243
1244 }
1245 }
1246 }
1247 }
1248 },
1249 .power_ctrl = lme2510_powerup,
1250 .identify_state = lme2510_identify_state,
1251 .i2c_algo = &lme2510_i2c_algo,
1252 .generic_bulk_ctrl_endpoint = 0,
1253 .num_device_descs = 1,
1254 .devices = {
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001255 { "DM04_LME2510C_DVB-S",
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001256 { &lme2510_table[1], NULL },
1257 },
1258 }
1259};
1260
Malcolm Priestleya760d632011-04-22 06:07:40 -03001261static void *lme2510_exit_int(struct dvb_usb_device *d)
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001262{
1263 struct lme2510_state *st = d->priv;
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001264 struct dvb_usb_adapter *adap = &d->adapter[0];
1265 void *buffer = NULL;
1266
1267 if (adap != NULL) {
1268 lme2510_kill_urb(&adap->stream);
1269 adap->feedcount = 0;
1270 }
1271
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001272 if (st->lme_urb != NULL) {
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001273 st->i2c_talk_onoff = 1;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001274 st->signal_lock = 0;
1275 st->signal_level = 0;
1276 st->signal_sn = 0;
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001277 buffer = st->usb_buffer;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001278 usb_kill_urb(st->lme_urb);
1279 usb_free_coherent(d->udev, 5000, st->buffer,
1280 st->lme_urb->transfer_dma);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001281 info("Interrupt Service Stopped");
David Härdemand8b4b582010-10-29 16:08:23 -03001282 rc_unregister_device(d->rc_dev);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001283 info("Remote Stopped");
1284 }
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001285 return buffer;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001286}
1287
Malcolm Priestleya760d632011-04-22 06:07:40 -03001288static void lme2510_exit(struct usb_interface *intf)
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001289{
1290 struct dvb_usb_device *d = usb_get_intfdata(intf);
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001291 void *usb_buffer;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001292
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001293 if (d != NULL) {
1294 usb_buffer = lme2510_exit_int(d);
1295 dvb_usb_device_exit(intf);
1296 kfree(usb_buffer);
1297 }
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001298}
1299
1300static struct usb_driver lme2510_driver = {
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001301 .name = "LME2510C_DVB-S",
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001302 .probe = lme2510_probe,
1303 .disconnect = lme2510_exit,
1304 .id_table = lme2510_table,
1305};
1306
1307/* module stuff */
1308static int __init lme2510_module_init(void)
1309{
1310 int result = usb_register(&lme2510_driver);
1311 if (result) {
1312 err("usb_register failed. Error number %d", result);
1313 return result;
1314 }
1315
1316 return 0;
1317}
1318
1319static void __exit lme2510_module_exit(void)
1320{
1321 /* deregister this driver from the USB subsystem */
1322 usb_deregister(&lme2510_driver);
1323}
1324
1325module_init(lme2510_module_init);
1326module_exit(lme2510_module_exit);
1327
1328MODULE_AUTHOR("Malcolm Priestley <tvboxspy@gmail.com>");
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001329MODULE_DESCRIPTION("LME2510(C) DVB-S USB2.0");
Malcolm Priestley449a0ba2011-04-22 06:20:57 -03001330MODULE_VERSION("1.86");
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001331MODULE_LICENSE("GPL");