blob: 5b0f43791f77e9db0dd7279d04496372903186c1 [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 Priestleyd2f918b2010-09-02 17:29:30 -0300130 void *buffer;
131 struct urb *lme_urb;
132 void *usb_buffer;
133
134};
135
136static int lme2510_bulk_write(struct usb_device *dev,
137 u8 *snd, int len, u8 pipe)
138{
139 int ret, actual_l;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300140
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300141 ret = usb_bulk_msg(dev, usb_sndbulkpipe(dev, pipe),
Malcolm Priestley25ad9842010-11-04 17:17:51 -0300142 snd, len , &actual_l, 100);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300143 return ret;
144}
145
146static int lme2510_bulk_read(struct usb_device *dev,
147 u8 *rev, int len, u8 pipe)
148{
149 int ret, actual_l;
150
151 ret = usb_bulk_msg(dev, usb_rcvbulkpipe(dev, pipe),
Malcolm Priestley25ad9842010-11-04 17:17:51 -0300152 rev, len , &actual_l, 200);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300153 return ret;
154}
155
156static int lme2510_usb_talk(struct dvb_usb_device *d,
157 u8 *wbuf, int wlen, u8 *rbuf, int rlen)
158{
159 struct lme2510_state *st = d->priv;
160 u8 *buff;
161 int ret = 0;
162
163 if (st->usb_buffer == NULL) {
164 st->usb_buffer = kmalloc(512, GFP_KERNEL);
165 if (st->usb_buffer == NULL) {
166 info("MEM Error no memory");
167 return -ENOMEM;
168 }
169 }
170 buff = st->usb_buffer;
171
172 /* the read/write capped at 512 */
173 memcpy(buff, wbuf, (wlen > 512) ? 512 : wlen);
174
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300175 ret = mutex_lock_interruptible(&d->usb_mutex);
176
177 if (ret < 0)
178 return -EAGAIN;
179
180 ret |= usb_clear_halt(d->udev, usb_sndbulkpipe(d->udev, 0x01));
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300181
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300182 ret |= lme2510_bulk_write(d->udev, buff, wlen , 0x01);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300183
Malcolm Priestley25ad9842010-11-04 17:17:51 -0300184 msleep(10);
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300185
186 ret |= usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, 0x01));
187
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300188 ret |= lme2510_bulk_read(d->udev, buff, (rlen > 512) ?
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300189 512 : rlen , 0x01);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300190
191 if (rlen > 0)
192 memcpy(rbuf, buff, rlen);
193
194 mutex_unlock(&d->usb_mutex);
195
196 return (ret < 0) ? -ENODEV : 0;
197}
198
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300199static int lme2510_stream_restart(struct dvb_usb_device *d)
200{
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300201 static u8 stream_on[] = LME_ST_ON_W;
202 int ret;
203 u8 rbuff[10];
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300204 /*Restart Stream Command*/
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300205 ret = lme2510_usb_talk(d, stream_on, sizeof(stream_on),
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300206 rbuff, sizeof(rbuff));
207 return ret;
208}
Malcolm Priestleyf8931f52011-01-23 18:22:45 -0300209static int lme2510_remote_keypress(struct dvb_usb_adapter *adap, u32 keypress)
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300210{
211 struct dvb_usb_device *d = adap->dev;
212
213 deb_info(1, "INT Key Keypress =%04x", keypress);
214
215 if (keypress > 0)
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300216 rc_keydown(d->rc_dev, keypress, 0);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300217
218 return 0;
219}
220
Malcolm Priestleyeb02d852011-04-02 18:59:29 -0300221static int lme2510_enable_pid(struct dvb_usb_device *d, u8 index, u16 pid_out)
222{
223 struct lme2510_state *st = d->priv;
224 static u8 pid_buff[] = LME_ZERO_PID;
225 static u8 rbuf[1];
226 u8 pid_no = index * 2;
227 int ret = 0;
228 deb_info(1, "PID Setting Pid %04x", pid_out);
229
230 pid_buff[2] = pid_no;
231 pid_buff[3] = (u8)pid_out & 0xff;
232 pid_buff[4] = pid_no + 1;
233 pid_buff[5] = (u8)(pid_out >> 8);
234
235 /* wait for i2c mutex */
236 ret = mutex_lock_interruptible(&d->i2c_mutex);
237 if (ret < 0) {
238 ret = -EAGAIN;
239 return ret;
240 }
241
242 ret |= lme2510_usb_talk(d, pid_buff ,
243 sizeof(pid_buff) , rbuf, sizeof(rbuf));
244
245 if (st->stream_on & 1)
246 ret |= lme2510_stream_restart(d);
247
248 mutex_unlock(&d->i2c_mutex);
249
250 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{
365 static u8 clear_pid_reg[] = LME_CLEAR_PID;
366 static u8 rbuf[1];
367 int ret = 0;
368
369 deb_info(1, "PID Clearing Filter");
370
371 ret = mutex_lock_interruptible(&adap->dev->i2c_mutex);
372
373 if (!onoff)
374 ret |= lme2510_usb_talk(adap->dev, clear_pid_reg,
375 sizeof(clear_pid_reg), rbuf, sizeof(rbuf));
376
377 mutex_unlock(&adap->dev->i2c_mutex);
378
379 return 0;
380}
381
382static int lme2510_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
383 int onoff)
384{
385 int ret = 0;
386
387 deb_info(3, "%s PID=%04x Index=%04x onoff=%02x", __func__,
388 pid, index, onoff);
389
390 if (onoff)
391 if (!pid_filter)
392 ret = lme2510_enable_pid(adap->dev, index, pid);
393
394 return ret;
395}
396
397
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300398static int lme2510_return_status(struct usb_device *dev)
399{
400 int ret = 0;
Florian Mickler945b8762011-03-20 18:50:52 -0300401 u8 *data;
402
403 data = kzalloc(10, GFP_KERNEL);
404 if (!data)
405 return -ENOMEM;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300406
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300407 ret |= usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
408 0x06, 0x80, 0x0302, 0x00, data, 0x0006, 200);
409 info("Firmware Status: %x (%x)", ret , data[2]);
410
Florian Mickler945b8762011-03-20 18:50:52 -0300411 ret = (ret < 0) ? -ENODEV : data[2];
412 kfree(data);
413 return ret;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300414}
415
416static int lme2510_msg(struct dvb_usb_device *d,
417 u8 *wbuf, int wlen, u8 *rbuf, int rlen)
418{
419 int ret = 0;
420 struct lme2510_state *st = d->priv;
421
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300422 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300423 return -EAGAIN;
424
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300425 if (st->i2c_talk_onoff == 1) {
426
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300427 ret = lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
428
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300429 switch (st->tuner_config) {
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300430 case TUNER_LG:
431 if (wbuf[2] == 0x1c) {
432 if (wbuf[3] == 0x0e) {
433 st->signal_lock = rbuf[1];
434 if ((st->stream_on & 1) &&
435 (st->signal_lock & 0x10)) {
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300436 lme2510_stream_restart(d);
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300437 st->i2c_talk_onoff = 0;
438 }
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300439 msleep(80);
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300440 }
441 }
442 break;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300443 case TUNER_S7395:
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300444 if (wbuf[2] == 0xd0) {
445 if (wbuf[3] == 0x24) {
446 st->signal_lock = rbuf[1];
447 if ((st->stream_on & 1) &&
448 (st->signal_lock & 0x8)) {
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300449 lme2510_stream_restart(d);
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300450 st->i2c_talk_onoff = 0;
451 }
452 }
453 if ((wbuf[3] != 0x6) & (wbuf[3] != 0x5))
454 msleep(5);
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300455 }
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300456 break;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300457 case TUNER_S0194:
458 if (wbuf[2] == 0xd0) {
459 if (wbuf[3] == 0x1b) {
460 st->signal_lock = rbuf[1];
461 if ((st->stream_on & 1) &&
462 (st->signal_lock & 0x8)) {
463 lme2510_stream_restart(d);
464 st->i2c_talk_onoff = 0;
465 }
466 }
467 }
468 break;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300469 default:
470 break;
471 }
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300472 } else {
473 switch (st->tuner_config) {
474 case TUNER_LG:
475 switch (wbuf[3]) {
476 case 0x0e:
477 rbuf[0] = 0x55;
478 rbuf[1] = st->signal_lock;
479 break;
480 case 0x43:
481 rbuf[0] = 0x55;
482 rbuf[1] = st->signal_level;
483 break;
484 case 0x1c:
485 rbuf[0] = 0x55;
486 rbuf[1] = st->signal_sn;
487 break;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300488 case 0x15:
489 case 0x16:
490 case 0x17:
491 case 0x18:
492 rbuf[0] = 0x55;
493 rbuf[1] = 0x00;
494 break;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300495 default:
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300496 lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
497 st->i2c_talk_onoff = 1;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300498 break;
499 }
500 break;
501 case TUNER_S7395:
502 switch (wbuf[3]) {
503 case 0x10:
504 rbuf[0] = 0x55;
505 rbuf[1] = (st->signal_level & 0x80)
506 ? 0 : (st->signal_level * 2);
507 break;
508 case 0x2d:
509 rbuf[0] = 0x55;
510 rbuf[1] = st->signal_sn;
511 break;
512 case 0x24:
513 rbuf[0] = 0x55;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300514 rbuf[1] = st->signal_lock;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300515 break;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300516 case 0x2e:
517 case 0x26:
518 case 0x27:
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300519 rbuf[0] = 0x55;
520 rbuf[1] = 0x00;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300521 break;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300522 default:
523 lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
524 st->i2c_talk_onoff = 1;
525 break;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300526 }
527 break;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300528 case TUNER_S0194:
529 switch (wbuf[3]) {
530 case 0x18:
531 rbuf[0] = 0x55;
532 rbuf[1] = (st->signal_level & 0x80)
533 ? 0 : (st->signal_level * 2);
534 break;
535 case 0x24:
536 rbuf[0] = 0x55;
537 rbuf[1] = st->signal_sn;
538 break;
539 case 0x1b:
540 rbuf[0] = 0x55;
541 rbuf[1] = st->signal_lock;
542 break;
543 case 0x19:
544 case 0x25:
545 case 0x1e:
546 case 0x1d:
547 rbuf[0] = 0x55;
548 rbuf[1] = 0x00;
549 break;
550 default:
551 lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
552 st->i2c_talk_onoff = 1;
553 break;
554 }
555 break;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300556 default:
557 break;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300558 }
559
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300560 deb_info(4, "I2C From Interrupt Message out(%02x) in(%02x)",
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300561 wbuf[3], rbuf[1]);
562
563 }
564
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300565 mutex_unlock(&d->i2c_mutex);
566
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300567 return ret;
568}
569
570
571static int lme2510_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
572 int num)
573{
574 struct dvb_usb_device *d = i2c_get_adapdata(adap);
575 struct lme2510_state *st = d->priv;
576 static u8 obuf[64], ibuf[512];
577 int i, read, read_o;
578 u16 len;
579 u8 gate = st->i2c_gate;
580
581 if (gate == 0)
582 gate = 5;
583
584 if (num > 2)
585 warn("more than 2 i2c messages"
586 "at a time is not handled yet. TODO.");
587
588 for (i = 0; i < num; i++) {
589 read_o = 1 & (msg[i].flags & I2C_M_RD);
590 read = i+1 < num && (msg[i+1].flags & I2C_M_RD);
591 read |= read_o;
592 gate = (msg[i].addr == st->i2c_tuner_addr)
593 ? (read) ? st->i2c_tuner_gate_r
594 : st->i2c_tuner_gate_w
595 : st->i2c_gate;
596 obuf[0] = gate | (read << 7);
597
598 if (gate == 5)
599 obuf[1] = (read) ? 2 : msg[i].len + 1;
600 else
601 obuf[1] = msg[i].len + read + 1;
602
603 obuf[2] = msg[i].addr;
604 if (read) {
605 if (read_o)
606 len = 3;
607 else {
608 memcpy(&obuf[3], msg[i].buf, msg[i].len);
609 obuf[msg[i].len+3] = msg[i+1].len;
610 len = msg[i].len+4;
611 }
612 } else {
613 memcpy(&obuf[3], msg[i].buf, msg[i].len);
614 len = msg[i].len+3;
615 }
616
617 if (lme2510_msg(d, obuf, len, ibuf, 512) < 0) {
618 deb_info(1, "i2c transfer failed.");
619 return -EAGAIN;
620 }
621
622 if (read) {
623 if (read_o)
624 memcpy(msg[i].buf, &ibuf[1], msg[i].len);
625 else {
626 memcpy(msg[i+1].buf, &ibuf[1], msg[i+1].len);
627 i++;
628 }
629 }
630 }
631 return i;
632}
633
634static u32 lme2510_i2c_func(struct i2c_adapter *adapter)
635{
636 return I2C_FUNC_I2C;
637}
638
639static struct i2c_algorithm lme2510_i2c_algo = {
640 .master_xfer = lme2510_i2c_xfer,
641 .functionality = lme2510_i2c_func,
642};
643
644/* Callbacks for DVB USB */
645static int lme2510_identify_state(struct usb_device *udev,
646 struct dvb_usb_device_properties *props,
647 struct dvb_usb_device_description **desc,
648 int *cold)
649{
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300650 *cold = 0;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300651 return 0;
652}
653
654static int lme2510_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
655{
656 struct lme2510_state *st = adap->dev->priv;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300657 static u8 clear_reg_3[] = LME_CLEAR_PID;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300658 static u8 rbuf[1];
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300659 int ret = 0, rlen = sizeof(rbuf);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300660
661 deb_info(1, "STM (%02x)", onoff);
662
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300663 /* Streaming is started by FE_HAS_LOCK */
664 if (onoff == 1)
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300665 st->stream_on = 1;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300666 else {
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300667 deb_info(1, "STM Steam Off");
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300668 /* mutex is here only to avoid collision with I2C */
669 ret = mutex_lock_interruptible(&adap->dev->i2c_mutex);
670
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300671 ret |= lme2510_usb_talk(adap->dev, clear_reg_3,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300672 sizeof(clear_reg_3), rbuf, rlen);
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300673 st->stream_on = 0;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300674 st->i2c_talk_onoff = 1;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300675
676 mutex_unlock(&adap->dev->i2c_mutex);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300677 }
678
679 return (ret < 0) ? -ENODEV : 0;
680}
681
682static int lme2510_int_service(struct dvb_usb_adapter *adap)
683{
684 struct dvb_usb_device *d = adap->dev;
David Härdemand8b4b582010-10-29 16:08:23 -0300685 struct rc_dev *rc;
686 int ret;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300687
688 info("STA Configuring Remote");
689
David Härdemand8b4b582010-10-29 16:08:23 -0300690 rc = rc_allocate_device();
691 if (!rc)
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300692 return -ENOMEM;
693
David Härdemand8b4b582010-10-29 16:08:23 -0300694 usb_make_path(d->udev, d->rc_phys, sizeof(d->rc_phys));
695 strlcat(d->rc_phys, "/ir0", sizeof(d->rc_phys));
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300696
David Härdemand8b4b582010-10-29 16:08:23 -0300697 rc->input_name = "LME2510 Remote Control";
698 rc->input_phys = d->rc_phys;
699 rc->map_name = RC_MAP_LME2510;
700 rc->driver_name = "LME 2510";
701 usb_to_input_id(d->udev, &rc->input_id);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300702
David Härdemand8b4b582010-10-29 16:08:23 -0300703 ret = rc_register_device(rc);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300704 if (ret) {
David Härdemand8b4b582010-10-29 16:08:23 -0300705 rc_free_device(rc);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300706 return ret;
707 }
David Härdemand8b4b582010-10-29 16:08:23 -0300708 d->rc_dev = rc;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300709
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300710 /* Start the Interrupt */
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300711 ret = lme2510_int_read(adap);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300712 if (ret < 0) {
David Härdemand8b4b582010-10-29 16:08:23 -0300713 rc_unregister_device(rc);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300714 info("INT Unable to start Interrupt Service");
David Härdemand8b4b582010-10-29 16:08:23 -0300715 return -ENODEV;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300716 }
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300717
David Härdemand8b4b582010-10-29 16:08:23 -0300718 return 0;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300719}
720
721static u8 check_sum(u8 *p, u8 len)
722{
723 u8 sum = 0;
724 while (len--)
725 sum += *p++;
726 return sum;
727}
728
729static int lme2510_download_firmware(struct usb_device *dev,
730 const struct firmware *fw)
731{
732 int ret = 0;
Florian Mickler945b8762011-03-20 18:50:52 -0300733 u8 *data;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300734 u16 j, wlen, len_in, start, end;
735 u8 packet_size, dlen, i;
736 u8 *fw_data;
737
738 packet_size = 0x31;
739 len_in = 1;
740
Florian Mickler945b8762011-03-20 18:50:52 -0300741 data = kzalloc(512, GFP_KERNEL);
742 if (!data) {
743 info("FRM Could not start Firmware Download (Buffer allocation failed)");
744 return -ENOMEM;
745 }
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300746
747 info("FRM Starting Firmware Download");
748
749 for (i = 1; i < 3; i++) {
750 start = (i == 1) ? 0 : 512;
751 end = (i == 1) ? 512 : fw->size;
752 for (j = start; j < end; j += (packet_size+1)) {
753 fw_data = (u8 *)(fw->data + j);
754 if ((end - j) > packet_size) {
755 data[0] = i;
756 dlen = packet_size;
757 } else {
758 data[0] = i | 0x80;
759 dlen = (u8)(end - j)-1;
760 }
Florian Micklerb9f7b732011-03-20 18:50:51 -0300761 data[1] = dlen;
762 memcpy(&data[2], fw_data, dlen+1);
763 wlen = (u8) dlen + 4;
764 data[wlen-1] = check_sum(fw_data, dlen+1);
765 deb_info(1, "Data S=%02x:E=%02x CS= %02x", data[3],
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300766 data[dlen+2], data[dlen+3]);
Florian Micklerb9f7b732011-03-20 18:50:51 -0300767 ret |= lme2510_bulk_write(dev, data, wlen, 1);
768 ret |= lme2510_bulk_read(dev, data, len_in , 1);
769 ret |= (data[0] == 0x88) ? 0 : -1;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300770 }
771 }
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300772
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300773 usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
774 0x06, 0x80, 0x0200, 0x00, data, 0x0109, 1000);
775
776
777 data[0] = 0x8a;
778 len_in = 1;
779 msleep(2000);
780 ret |= lme2510_bulk_write(dev, data , len_in, 1); /*Resetting*/
781 ret |= lme2510_bulk_read(dev, data, len_in, 1);
782 msleep(400);
783
784 if (ret < 0)
785 info("FRM Firmware Download Failed (%04x)" , ret);
786 else
787 info("FRM Firmware Download Completed - Resetting Device");
788
Florian Mickler945b8762011-03-20 18:50:52 -0300789 kfree(data);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300790 return (ret < 0) ? -ENODEV : 0;
791}
792
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300793static void lme_coldreset(struct usb_device *dev)
794{
795 int ret = 0, len_in;
796 u8 data[512] = {0};
797
798 data[0] = 0x0a;
799 len_in = 1;
800 info("FRM Firmware Cold Reset");
801 ret |= lme2510_bulk_write(dev, data , len_in, 1); /*Cold Resetting*/
802 ret |= lme2510_bulk_read(dev, data, len_in, 1);
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300803
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300804 return;
805}
806
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300807static int lme_firmware_switch(struct usb_device *udev, int cold)
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300808{
809 const struct firmware *fw = NULL;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300810 const char fw_c_s7395[] = "dvb-usb-lme2510c-s7395.fw";
811 const char fw_c_lg[] = "dvb-usb-lme2510c-lg.fw";
812 const char fw_c_s0194[] = "dvb-usb-lme2510c-s0194.fw";
813 const char fw_lg[] = "dvb-usb-lme2510-lg.fw";
814 const char fw_s0194[] = "dvb-usb-lme2510-s0194.fw";
815 const char *fw_lme;
816 int ret, cold_fw;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300817
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300818 cold = (cold > 0) ? (cold & 1) : 0;
819
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300820 cold_fw = !cold;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300821
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300822 if (udev->descriptor.idProduct == 0x1122) {
823 switch (dvb_usb_lme2510_firmware) {
824 default:
825 dvb_usb_lme2510_firmware = TUNER_S0194;
826 case TUNER_S0194:
827 fw_lme = fw_s0194;
828 ret = request_firmware(&fw, fw_lme, &udev->dev);
829 if (ret == 0) {
Malcolm Priestley30bd3b52011-02-13 20:12:15 -0300830 cold = 0;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300831 break;
832 }
833 dvb_usb_lme2510_firmware = TUNER_LG;
834 case TUNER_LG:
835 fw_lme = fw_lg;
836 ret = request_firmware(&fw, fw_lme, &udev->dev);
837 if (ret == 0)
838 break;
839 info("FRM No Firmware Found - please install");
840 dvb_usb_lme2510_firmware = TUNER_DEFAULT;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300841 cold = 0;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300842 cold_fw = 0;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300843 break;
844 }
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300845 } else {
846 switch (dvb_usb_lme2510_firmware) {
847 default:
848 dvb_usb_lme2510_firmware = TUNER_S7395;
849 case TUNER_S7395:
850 fw_lme = fw_c_s7395;
851 ret = request_firmware(&fw, fw_lme, &udev->dev);
Malcolm Priestley30bd3b52011-02-13 20:12:15 -0300852 if (ret == 0) {
853 cold = 0;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300854 break;
Malcolm Priestley30bd3b52011-02-13 20:12:15 -0300855 }
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300856 dvb_usb_lme2510_firmware = TUNER_LG;
857 case TUNER_LG:
858 fw_lme = fw_c_lg;
859 ret = request_firmware(&fw, fw_lme, &udev->dev);
860 if (ret == 0)
861 break;
862 dvb_usb_lme2510_firmware = TUNER_S0194;
863 case TUNER_S0194:
864 fw_lme = fw_c_s0194;
865 ret = request_firmware(&fw, fw_lme, &udev->dev);
866 if (ret == 0)
867 break;
868 info("FRM No Firmware Found - please install");
869 dvb_usb_lme2510_firmware = TUNER_DEFAULT;
870 cold = 0;
871 cold_fw = 0;
872 break;
873 }
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300874 }
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300875
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300876 if (cold_fw) {
877 info("FRM Loading %s file", fw_lme);
878 ret = lme2510_download_firmware(udev, fw);
879 }
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300880
Malcolm Priestley30bd3b52011-02-13 20:12:15 -0300881 release_firmware(fw);
882
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300883 if (cold) {
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300884 info("FRM Changing to %s firmware", fw_lme);
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300885 lme_coldreset(udev);
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300886 return -ENODEV;
887 }
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300888
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300889 return ret;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300890}
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300891
892static int lme2510_kill_urb(struct usb_data_stream *stream)
893{
894 int i;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300895
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300896 for (i = 0; i < stream->urbs_submitted; i++) {
897 deb_info(3, "killing URB no. %d.", i);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300898 /* stop the URB */
899 usb_kill_urb(stream->urb_list[i]);
900 }
901 stream->urbs_submitted = 0;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300902
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300903 return 0;
904}
905
906static struct tda10086_config tda10086_config = {
907 .demod_address = 0x1c,
908 .invert = 0,
909 .diseqc_tone = 1,
910 .xtal_freq = TDA10086_XTAL_16M,
911};
912
913static struct stv0288_config lme_config = {
914 .demod_address = 0xd0,
915 .min_delay_ms = 15,
916 .inittab = s7395_inittab,
917};
918
919static struct ix2505v_config lme_tuner = {
920 .tuner_address = 0xc0,
921 .min_delay_ms = 100,
922 .tuner_gain = 0x0,
923 .tuner_chargepump = 0x3,
924};
925
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300926static struct stv0299_config sharp_z0194_config = {
927 .demod_address = 0xd0,
928 .inittab = sharp_z0194a_inittab,
929 .mclk = 88000000UL,
930 .invert = 0,
931 .skip_reinit = 0,
932 .lock_output = STV0299_LOCKOUTPUT_1,
933 .volt13_op0_op1 = STV0299_VOLT13_OP1,
934 .min_delay_ms = 100,
935 .set_symbol_rate = sharp_z0194a_set_symbol_rate,
936};
937
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300938static int dm04_lme2510_set_voltage(struct dvb_frontend *fe,
939 fe_sec_voltage_t voltage)
940{
941 struct dvb_usb_adapter *adap = fe->dvb->priv;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300942 static u8 voltage_low[] = LME_VOLTAGE_L;
943 static u8 voltage_high[] = LME_VOLTAGE_H;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300944 static u8 rbuf[1];
945 int ret = 0, len = 3, rlen = 1;
946
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300947 if (mutex_lock_interruptible(&adap->dev->i2c_mutex) < 0)
948 return -EAGAIN;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300949
950 switch (voltage) {
951 case SEC_VOLTAGE_18:
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300952 ret |= lme2510_usb_talk(adap->dev,
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300953 voltage_high, len, rbuf, rlen);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300954 break;
955
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300956 case SEC_VOLTAGE_OFF:
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300957 case SEC_VOLTAGE_13:
958 default:
959 ret |= lme2510_usb_talk(adap->dev,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300960 voltage_low, len, rbuf, rlen);
961 break;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300962 }
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300963
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300964 mutex_unlock(&adap->dev->i2c_mutex);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300965
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300966 return (ret < 0) ? -ENODEV : 0;
967}
968
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300969static int lme_name(struct dvb_usb_adapter *adap)
970{
971 struct lme2510_state *st = adap->dev->priv;
972 const char *desc = adap->dev->desc->name;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300973 char *fe_name[] = {"", " LG TDQY-P001F", " SHARP:BS2F7HZ7395",
974 " SHARP:BS2F7HZ0194"};
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300975 char *name = adap->fe->ops.info.name;
976
977 strlcpy(name, desc, 128);
978 strlcat(name, fe_name[st->tuner_config], 128);
979
980 return 0;
981}
982
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300983static int dm04_lme2510_frontend_attach(struct dvb_usb_adapter *adap)
984{
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300985 struct lme2510_state *st = adap->dev->priv;
986
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300987 int ret = 0;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300988
989 st->i2c_talk_onoff = 1;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300990
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300991 st->i2c_gate = 4;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300992 adap->fe = dvb_attach(tda10086_attach, &tda10086_config,
993 &adap->dev->i2c_adap);
994
995 if (adap->fe) {
996 info("TUN Found Frontend TDA10086");
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300997 st->i2c_tuner_gate_w = 4;
998 st->i2c_tuner_gate_r = 4;
999 st->i2c_tuner_addr = 0xc0;
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001000 st->tuner_config = TUNER_LG;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -03001001 if (dvb_usb_lme2510_firmware != TUNER_LG) {
1002 dvb_usb_lme2510_firmware = TUNER_LG;
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001003 ret = lme_firmware_switch(adap->dev->udev, 1);
Malcolm Priestley9d4540c2011-02-12 22:35:08 -03001004 }
1005 goto end;
1006 }
1007
1008 st->i2c_gate = 4;
1009 adap->fe = dvb_attach(stv0299_attach, &sharp_z0194_config,
1010 &adap->dev->i2c_adap);
1011 if (adap->fe) {
1012 info("FE Found Stv0299");
1013 st->i2c_tuner_gate_w = 4;
1014 st->i2c_tuner_gate_r = 5;
1015 st->i2c_tuner_addr = 0xc0;
1016 st->tuner_config = TUNER_S0194;
1017 if (dvb_usb_lme2510_firmware != TUNER_S0194) {
1018 dvb_usb_lme2510_firmware = TUNER_S0194;
1019 ret = lme_firmware_switch(adap->dev->udev, 1);
1020 }
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001021 goto end;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001022 }
Malcolm Priestleyf23e6612010-10-27 19:50:36 -03001023
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001024 st->i2c_gate = 5;
1025 adap->fe = dvb_attach(stv0288_attach, &lme_config,
1026 &adap->dev->i2c_adap);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001027 if (adap->fe) {
1028 info("FE Found Stv0288");
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001029 st->i2c_tuner_gate_w = 4;
1030 st->i2c_tuner_gate_r = 5;
1031 st->i2c_tuner_addr = 0xc0;
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001032 st->tuner_config = TUNER_S7395;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -03001033 if (dvb_usb_lme2510_firmware != TUNER_S7395) {
1034 dvb_usb_lme2510_firmware = TUNER_S7395;
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001035 ret = lme_firmware_switch(adap->dev->udev, 1);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001036 }
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001037 } else {
1038 info("DM04 Not Supported");
1039 return -ENODEV;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001040 }
1041
Malcolm Priestley9d4540c2011-02-12 22:35:08 -03001042
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001043end: if (ret) {
1044 kfree(adap->fe);
1045 adap->fe = NULL;
1046 return -ENODEV;
1047 }
1048
1049 adap->fe->ops.set_voltage = dm04_lme2510_set_voltage;
1050 ret = lme_name(adap);
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001051 return ret;
1052}
1053
1054static int dm04_lme2510_tuner(struct dvb_usb_adapter *adap)
1055{
1056 struct lme2510_state *st = adap->dev->priv;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -03001057 char *tun_msg[] = {"", "TDA8263", "IX2505V", "DVB_PLL_OPERA"};
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001058 int ret = 0;
1059
1060 switch (st->tuner_config) {
1061 case TUNER_LG:
1062 if (dvb_attach(tda826x_attach, adap->fe, 0xc0,
1063 &adap->dev->i2c_adap, 1))
1064 ret = st->tuner_config;
1065 break;
1066 case TUNER_S7395:
1067 if (dvb_attach(ix2505v_attach , adap->fe, &lme_tuner,
1068 &adap->dev->i2c_adap))
1069 ret = st->tuner_config;
1070 break;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -03001071 case TUNER_S0194:
1072 if (dvb_attach(dvb_pll_attach , adap->fe, 0xc0,
1073 &adap->dev->i2c_adap, DVB_PLL_OPERA1))
1074 ret = st->tuner_config;
1075 break;
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001076 default:
1077 break;
1078 }
1079
1080 if (ret)
1081 info("TUN Found %s tuner", tun_msg[ret]);
1082 else {
1083 info("TUN No tuner found --- reseting device");
1084 lme_coldreset(adap->dev->udev);
1085 return -ENODEV;
1086 }
1087
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001088 /* Start the Interrupt & Remote*/
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001089 ret = lme2510_int_service(adap);
1090
1091 return ret;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001092}
1093
1094static int lme2510_powerup(struct dvb_usb_device *d, int onoff)
1095{
1096 struct lme2510_state *st = d->priv;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -03001097 static u8 lnb_on[] = LNB_ON;
1098 static u8 lnb_off[] = LNB_OFF;
1099 static u8 rbuf[1];
1100 int ret, len = 3, rlen = 1;
1101
1102 ret = mutex_lock_interruptible(&d->i2c_mutex);
1103
1104 if (onoff)
1105 ret |= lme2510_usb_talk(d, lnb_on, len, rbuf, rlen);
1106 else
1107 ret |= lme2510_usb_talk(d, lnb_off, len, rbuf, rlen);
1108
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001109 st->i2c_talk_onoff = 1;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -03001110
1111 mutex_unlock(&d->i2c_mutex);
1112
1113 return ret;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001114}
1115
1116/* DVB USB Driver stuff */
1117static struct dvb_usb_device_properties lme2510_properties;
1118static struct dvb_usb_device_properties lme2510c_properties;
1119
1120static int lme2510_probe(struct usb_interface *intf,
1121 const struct usb_device_id *id)
1122{
1123 struct usb_device *udev = interface_to_usbdev(intf);
1124 int ret = 0;
1125
1126 usb_reset_configuration(udev);
1127
1128 usb_set_interface(udev, intf->cur_altsetting->desc.bInterfaceNumber, 1);
1129
1130 if (udev->speed != USB_SPEED_HIGH) {
1131 ret = usb_reset_device(udev);
1132 info("DEV Failed to connect in HIGH SPEED mode");
1133 return -ENODEV;
1134 }
1135
Malcolm Priestley9d4540c2011-02-12 22:35:08 -03001136 if (lme2510_return_status(udev) == 0x44) {
1137 lme_firmware_switch(udev, 0);
1138 return -ENODEV;
1139 }
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001140
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001141 if (0 == dvb_usb_device_init(intf, &lme2510_properties,
1142 THIS_MODULE, NULL, adapter_nr)) {
1143 info("DEV registering device driver");
1144 return 0;
1145 }
1146 if (0 == dvb_usb_device_init(intf, &lme2510c_properties,
1147 THIS_MODULE, NULL, adapter_nr)) {
1148 info("DEV registering device driver");
1149 return 0;
1150 }
1151
1152 info("DEV lme2510 Error");
1153 return -ENODEV;
1154
1155}
1156
1157static struct usb_device_id lme2510_table[] = {
1158 { USB_DEVICE(0x3344, 0x1122) }, /* LME2510 */
1159 { USB_DEVICE(0x3344, 0x1120) }, /* LME2510C */
1160 {} /* Terminating entry */
1161};
1162
1163MODULE_DEVICE_TABLE(usb, lme2510_table);
1164
1165static struct dvb_usb_device_properties lme2510_properties = {
1166 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001167 .size_of_priv = sizeof(struct lme2510_state),
1168 .num_adapters = 1,
1169 .adapter = {
1170 {
Malcolm Priestleyeb02d852011-04-02 18:59:29 -03001171 .caps = DVB_USB_ADAP_HAS_PID_FILTER|
1172 DVB_USB_ADAP_NEED_PID_FILTERING|
1173 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001174 .streaming_ctrl = lme2510_streaming_ctrl,
Malcolm Priestleyeb02d852011-04-02 18:59:29 -03001175 .pid_filter_count = 15,
1176 .pid_filter = lme2510_pid_filter,
1177 .pid_filter_ctrl = lme2510_pid_filter_ctrl,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001178 .frontend_attach = dm04_lme2510_frontend_attach,
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001179 .tuner_attach = dm04_lme2510_tuner,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001180 /* parameter for the MPEG2-data transfer */
1181 .stream = {
1182 .type = USB_BULK,
1183 .count = 10,
1184 .endpoint = 0x06,
1185 .u = {
1186 .bulk = {
1187 .buffersize = 4096,
1188
1189 }
1190 }
1191 }
1192 }
1193 },
1194 .power_ctrl = lme2510_powerup,
1195 .identify_state = lme2510_identify_state,
1196 .i2c_algo = &lme2510_i2c_algo,
1197 .generic_bulk_ctrl_endpoint = 0,
1198 .num_device_descs = 1,
1199 .devices = {
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001200 { "DM04_LME2510_DVB-S",
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001201 { &lme2510_table[0], NULL },
1202 },
1203
1204 }
1205};
1206
1207static struct dvb_usb_device_properties lme2510c_properties = {
1208 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001209 .size_of_priv = sizeof(struct lme2510_state),
1210 .num_adapters = 1,
1211 .adapter = {
1212 {
Malcolm Priestleyeb02d852011-04-02 18:59:29 -03001213 .caps = DVB_USB_ADAP_HAS_PID_FILTER|
1214 DVB_USB_ADAP_NEED_PID_FILTERING|
1215 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001216 .streaming_ctrl = lme2510_streaming_ctrl,
Malcolm Priestleyeb02d852011-04-02 18:59:29 -03001217 .pid_filter_count = 15,
1218 .pid_filter = lme2510_pid_filter,
1219 .pid_filter_ctrl = lme2510_pid_filter_ctrl,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001220 .frontend_attach = dm04_lme2510_frontend_attach,
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001221 .tuner_attach = dm04_lme2510_tuner,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001222 /* parameter for the MPEG2-data transfer */
1223 .stream = {
1224 .type = USB_BULK,
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001225 .count = 10,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001226 .endpoint = 0x8,
1227 .u = {
1228 .bulk = {
1229 .buffersize = 4096,
1230
1231 }
1232 }
1233 }
1234 }
1235 },
1236 .power_ctrl = lme2510_powerup,
1237 .identify_state = lme2510_identify_state,
1238 .i2c_algo = &lme2510_i2c_algo,
1239 .generic_bulk_ctrl_endpoint = 0,
1240 .num_device_descs = 1,
1241 .devices = {
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001242 { "DM04_LME2510C_DVB-S",
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001243 { &lme2510_table[1], NULL },
1244 },
1245 }
1246};
1247
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001248void *lme2510_exit_int(struct dvb_usb_device *d)
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001249{
1250 struct lme2510_state *st = d->priv;
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001251 struct dvb_usb_adapter *adap = &d->adapter[0];
1252 void *buffer = NULL;
1253
1254 if (adap != NULL) {
1255 lme2510_kill_urb(&adap->stream);
1256 adap->feedcount = 0;
1257 }
1258
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001259 if (st->lme_urb != NULL) {
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001260 st->i2c_talk_onoff = 1;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001261 st->signal_lock = 0;
1262 st->signal_level = 0;
1263 st->signal_sn = 0;
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001264 buffer = st->usb_buffer;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001265 usb_kill_urb(st->lme_urb);
1266 usb_free_coherent(d->udev, 5000, st->buffer,
1267 st->lme_urb->transfer_dma);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001268 info("Interrupt Service Stopped");
David Härdemand8b4b582010-10-29 16:08:23 -03001269 rc_unregister_device(d->rc_dev);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001270 info("Remote Stopped");
1271 }
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001272 return buffer;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001273}
1274
1275void lme2510_exit(struct usb_interface *intf)
1276{
1277 struct dvb_usb_device *d = usb_get_intfdata(intf);
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);
1282 dvb_usb_device_exit(intf);
1283 kfree(usb_buffer);
1284 }
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001285}
1286
1287static struct usb_driver lme2510_driver = {
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001288 .name = "LME2510C_DVB-S",
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001289 .probe = lme2510_probe,
1290 .disconnect = lme2510_exit,
1291 .id_table = lme2510_table,
1292};
1293
1294/* module stuff */
1295static int __init lme2510_module_init(void)
1296{
1297 int result = usb_register(&lme2510_driver);
1298 if (result) {
1299 err("usb_register failed. Error number %d", result);
1300 return result;
1301 }
1302
1303 return 0;
1304}
1305
1306static void __exit lme2510_module_exit(void)
1307{
1308 /* deregister this driver from the USB subsystem */
1309 usb_deregister(&lme2510_driver);
1310}
1311
1312module_init(lme2510_module_init);
1313module_exit(lme2510_module_exit);
1314
1315MODULE_AUTHOR("Malcolm Priestley <tvboxspy@gmail.com>");
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001316MODULE_DESCRIPTION("LME2510(C) DVB-S USB2.0");
Malcolm Priestleyeb02d852011-04-02 18:59:29 -03001317MODULE_VERSION("1.84");
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001318MODULE_LICENSE("GPL");