]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/usb/dvb-usb-v2/lmedm04.c
[media] dvb-usb-v2: get rid of warning: no previous prototype
[mirror_ubuntu-artful-kernel.git] / drivers / media / usb / dvb-usb-v2 / lmedm04.c
CommitLineData
d2f918bb
MP
1/* DVB USB compliant linux driver for
2 *
3 * DM04/QQBOX DVB-S USB BOX LME2510C + SHARP:BS2F7HZ7395
ab599a6d 4 * LME2510C + LG TDQY-P001F
9d4540c9 5 * LME2510C + BS2F7HZ0194
ab599a6d 6 * LME2510 + LG TDQY-P001F
9d4540c9 7 * LME2510 + BS2F7HZ0194
d2f918bb
MP
8 *
9 * MVB7395 (LME2510C+SHARP:BS2F7HZ7395)
10 * SHARP:BS2F7HZ7395 = (STV0288+Sharp IX2505V)
11 *
ab599a6d 12 * MV001F (LME2510+LGTDQY-P001F)
d2f918bb
MP
13 * LG TDQY - P001F =(TDA8263 + TDA10086H)
14 *
ab599a6d
MP
15 * MVB0001F (LME2510C+LGTDQT-P001F)
16 *
9d4540c9
MP
17 * MV0194 (LME2510+SHARP:BS2F7HZ0194)
18 * SHARP:BS2F7HZ0194 = (STV0299+IX2410)
19 *
20 * MVB0194 (LME2510C+SHARP0194)
21 *
e41543a2
MP
22 * LME2510C + M88RS2000
23 *
d2f918bb
MP
24 * For firmware see Documentation/dvb/lmedm04.txt
25 *
26 * I2C addresses:
27 * 0xd0 - STV0288 - Demodulator
28 * 0xc0 - Sharp IX2505V - Tuner
9d4540c9 29 * --
d2f918bb
MP
30 * 0x1c - TDA10086 - Demodulator
31 * 0xc0 - TDA8263 - Tuner
9d4540c9
MP
32 * --
33 * 0xd0 - STV0299 - Demodulator
34 * 0xc0 - IX2410 - Tuner
d2f918bb
MP
35 *
36 *
37 * VID = 3344 PID LME2510=1122 LME2510C=1120
38 *
39 * Copyright (C) 2010 Malcolm Priestley (tvboxspy@gmail.com)
40 * LME2510(C)(C) Leaguerme (Shenzhen) MicroElectronics Co., Ltd.
41 *
42 * This program is free software; you can redistribute it and/or modify
43 * it under the terms of the GNU General Public License Version 2, as
44 * published by the Free Software Foundation.
45 *
46 * This program is distributed in the hope that it will be useful,
47 * but WITHOUT ANY WARRANTY; without even the implied warranty of
48 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
49 * GNU General Public License for more details.
50 *
51 * You should have received a copy of the GNU General Public License
52 * along with this program; if not, write to the Free Software
53 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
54 *
55 *
56 * see Documentation/dvb/README.dvb-usb for more information
57 *
58 * Known Issues :
59 * LME2510: Non Intel USB chipsets fail to maintain High Speed on
60 * Boot or Hot Plug.
61 *
ab599a6d 62 * QQbox suffers from noise on LNB voltage.
d2f918bb 63 *
9d4540c9
MP
64 * LME2510: SHARP:BS2F7HZ0194(MV0194) cannot cold reset and share system
65 * with other tuners. After a cold reset streaming will not start.
66 *
e41543a2 67 * M88RS2000 suffers from loss of lock.
d2f918bb
MP
68 */
69#define DVB_USB_LOG_PREFIX "LME2510(C)"
70#include <linux/usb.h>
71#include <linux/usb/input.h>
6bda9644 72#include <media/rc-core.h>
d2f918bb 73
e41543a2 74#include "dvb_usb.h"
d2f918bb
MP
75#include "lmedm04.h"
76#include "tda826x.h"
77#include "tda10086.h"
78#include "stv0288.h"
79#include "ix2505v.h"
9d4540c9
MP
80#include "stv0299.h"
81#include "dvb-pll.h"
82#include "z0194a.h"
4f3b7fed 83#include "m88rs2000.h"
d2f918bb
MP
84
85
e41543a2
MP
86#define LME2510_C_S7395 "dvb-usb-lme2510c-s7395.fw";
87#define LME2510_C_LG "dvb-usb-lme2510c-lg.fw";
88#define LME2510_C_S0194 "dvb-usb-lme2510c-s0194.fw";
89#define LME2510_C_RS2000 "dvb-usb-lme2510c-rs2000.fw";
90#define LME2510_LG "dvb-usb-lme2510-lg.fw";
91#define LME2510_S0194 "dvb-usb-lme2510-s0194.fw";
d2f918bb
MP
92
93/* debug */
94static int dvb_usb_lme2510_debug;
e41543a2 95#define lme_debug(var, level, args...) do { \
d2f918bb 96 if ((var >= level)) \
e41543a2 97 pr_debug(DVB_USB_LOG_PREFIX": " args); \
d2f918bb 98} while (0)
e41543a2 99#define deb_info(level, args...) lme_debug(dvb_usb_lme2510_debug, level, args)
d2f918bb
MP
100#define debug_data_snipet(level, name, p) \
101 deb_info(level, name" (%02x%02x%02x%02x%02x%02x%02x%02x)", \
102 *p, *(p+1), *(p+2), *(p+3), *(p+4), \
103 *(p+5), *(p+6), *(p+7));
e41543a2 104#define info(args...) pr_info(DVB_USB_LOG_PREFIX": "args)
d2f918bb
MP
105
106module_param_named(debug, dvb_usb_lme2510_debug, int, 0644);
e41543a2 107MODULE_PARM_DESC(debug, "set debugging level (1=info (or-able)).");
d2f918bb 108
ab599a6d
MP
109static int dvb_usb_lme2510_firmware;
110module_param_named(firmware, dvb_usb_lme2510_firmware, int, 0644);
111MODULE_PARM_DESC(firmware, "set default firmware 0=Sharp7395 1=LG");
112
eb02d857
MP
113static int pid_filter;
114module_param_named(pid, pid_filter, int, 0644);
88495030 115MODULE_PARM_DESC(pid, "set default 0=default 1=off 2=on");
ab599a6d 116
4f3b7fed 117
d2f918bb 118DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
9d4540c9
MP
119
120#define TUNER_DEFAULT 0x0
ab599a6d
MP
121#define TUNER_LG 0x1
122#define TUNER_S7395 0x2
9d4540c9 123#define TUNER_S0194 0x3
4f3b7fed 124#define TUNER_RS2000 0x4
d2f918bb
MP
125
126struct lme2510_state {
127 u8 id;
128 u8 tuner_config;
129 u8 signal_lock;
130 u8 signal_level;
131 u8 signal_sn;
132 u8 time_key;
4f3b7fed
MP
133 u8 last_key;
134 u8 key_timeout;
d2f918bb
MP
135 u8 i2c_talk_onoff;
136 u8 i2c_gate;
137 u8 i2c_tuner_gate_w;
138 u8 i2c_tuner_gate_r;
139 u8 i2c_tuner_addr;
ab599a6d 140 u8 stream_on;
449a0bad 141 u8 pid_size;
4f3b7fed 142 u8 pid_off;
d2f918bb
MP
143 void *buffer;
144 struct urb *lme_urb;
145 void *usb_buffer;
e41543a2
MP
146 int (*fe_set_voltage)(struct dvb_frontend *, fe_sec_voltage_t);
147 u8 dvb_usb_lme2510_firmware;
d2f918bb
MP
148};
149
150static int lme2510_bulk_write(struct usb_device *dev,
151 u8 *snd, int len, u8 pipe)
152{
153 int ret, actual_l;
ab599a6d 154
d2f918bb 155 ret = usb_bulk_msg(dev, usb_sndbulkpipe(dev, pipe),
25ad9847 156 snd, len , &actual_l, 100);
d2f918bb
MP
157 return ret;
158}
159
160static int lme2510_bulk_read(struct usb_device *dev,
161 u8 *rev, int len, u8 pipe)
162{
163 int ret, actual_l;
164
165 ret = usb_bulk_msg(dev, usb_rcvbulkpipe(dev, pipe),
25ad9847 166 rev, len , &actual_l, 200);
d2f918bb
MP
167 return ret;
168}
169
170static int lme2510_usb_talk(struct dvb_usb_device *d,
171 u8 *wbuf, int wlen, u8 *rbuf, int rlen)
172{
173 struct lme2510_state *st = d->priv;
174 u8 *buff;
175 int ret = 0;
176
177 if (st->usb_buffer == NULL) {
810fda9b 178 st->usb_buffer = kmalloc(64, GFP_KERNEL);
d2f918bb
MP
179 if (st->usb_buffer == NULL) {
180 info("MEM Error no memory");
181 return -ENOMEM;
182 }
183 }
184 buff = st->usb_buffer;
185
d2f918bb
MP
186 ret = mutex_lock_interruptible(&d->usb_mutex);
187
188 if (ret < 0)
189 return -EAGAIN;
190
810fda9b 191 /* the read/write capped at 64 */
192 memcpy(buff, wbuf, (wlen < 64) ? wlen : 64);
a760d638 193
ab599a6d
MP
194 ret |= lme2510_bulk_write(d->udev, buff, wlen , 0x01);
195
810fda9b 196 ret |= lme2510_bulk_read(d->udev, buff, (rlen < 64) ?
197 rlen : 64 , 0x01);
d2f918bb
MP
198
199 if (rlen > 0)
200 memcpy(rbuf, buff, rlen);
201
202 mutex_unlock(&d->usb_mutex);
203
204 return (ret < 0) ? -ENODEV : 0;
205}
206
f23e6616
MP
207static int lme2510_stream_restart(struct dvb_usb_device *d)
208{
4f3b7fed
MP
209 struct lme2510_state *st = d->priv;
210 u8 all_pids[] = LME_ALL_PIDS;
211 u8 stream_on[] = LME_ST_ON_W;
ab599a6d 212 int ret;
4f3b7fed
MP
213 u8 rbuff[1];
214 if (st->pid_off)
215 ret = lme2510_usb_talk(d, all_pids, sizeof(all_pids),
216 rbuff, sizeof(rbuff));
ab599a6d 217 /*Restart Stream Command*/
f23e6616 218 ret = lme2510_usb_talk(d, stream_on, sizeof(stream_on),
ab599a6d
MP
219 rbuff, sizeof(rbuff));
220 return ret;
221}
d2f918bb 222
eb02d857
MP
223static int lme2510_enable_pid(struct dvb_usb_device *d, u8 index, u16 pid_out)
224{
225 struct lme2510_state *st = d->priv;
226 static u8 pid_buff[] = LME_ZERO_PID;
227 static u8 rbuf[1];
228 u8 pid_no = index * 2;
449a0bad 229 u8 pid_len = pid_no + 2;
eb02d857
MP
230 int ret = 0;
231 deb_info(1, "PID Setting Pid %04x", pid_out);
232
449a0bad
MP
233 if (st->pid_size == 0)
234 ret |= lme2510_stream_restart(d);
235
eb02d857
MP
236 pid_buff[2] = pid_no;
237 pid_buff[3] = (u8)pid_out & 0xff;
238 pid_buff[4] = pid_no + 1;
239 pid_buff[5] = (u8)(pid_out >> 8);
240
449a0bad
MP
241 if (pid_len > st->pid_size)
242 st->pid_size = pid_len;
243 pid_buff[7] = 0x80 + st->pid_size;
eb02d857
MP
244
245 ret |= lme2510_usb_talk(d, pid_buff ,
246 sizeof(pid_buff) , rbuf, sizeof(rbuf));
247
449a0bad 248 if (st->stream_on)
eb02d857
MP
249 ret |= lme2510_stream_restart(d);
250
eb02d857
MP
251 return ret;
252}
253
d2f918bb
MP
254static void lme2510_int_response(struct urb *lme_urb)
255{
256 struct dvb_usb_adapter *adap = lme_urb->context;
e41543a2 257 struct lme2510_state *st = adap_to_priv(adap);
d2f918bb
MP
258 static u8 *ibuf, *rbuf;
259 int i = 0, offset;
616a4b83 260 u32 key;
d2f918bb
MP
261
262 switch (lme_urb->status) {
263 case 0:
264 case -ETIMEDOUT:
265 break;
266 case -ECONNRESET:
267 case -ENOENT:
268 case -ESHUTDOWN:
269 return;
270 default:
271 info("Error %x", lme_urb->status);
272 break;
273 }
274
275 rbuf = (u8 *) lme_urb->transfer_buffer;
276
277 offset = ((lme_urb->actual_length/8) > 4)
278 ? 4 : (lme_urb->actual_length/8) ;
279
d2f918bb
MP
280 for (i = 0; i < offset; ++i) {
281 ibuf = (u8 *)&rbuf[i*8];
282 deb_info(5, "INT O/S C =%02x C/O=%02x Type =%02x%02x",
283 offset, i, ibuf[0], ibuf[1]);
284
285 switch (ibuf[0]) {
286 case 0xaa:
616a4b83
MP
287 debug_data_snipet(1, "INT Remote data snipet", ibuf);
288 if ((ibuf[4] + ibuf[5]) == 0xff) {
289 key = ibuf[5];
290 key += (ibuf[3] > 0)
291 ? (ibuf[3] ^ 0xff) << 8 : 0;
292 key += (ibuf[2] ^ 0xff) << 16;
293 deb_info(1, "INT Key =%08x", key);
e41543a2
MP
294 if (adap_to_d(adap)->rc_dev != NULL)
295 rc_keydown(adap_to_d(adap)->rc_dev,
296 key, 0);
616a4b83 297 }
d2f918bb
MP
298 break;
299 case 0xbb:
300 switch (st->tuner_config) {
301 case TUNER_LG:
ab599a6d
MP
302 if (ibuf[2] > 0)
303 st->signal_lock = ibuf[2];
d2f918bb
MP
304 st->signal_level = ibuf[4];
305 st->signal_sn = ibuf[3];
306 st->time_key = ibuf[7];
307 break;
308 case TUNER_S7395:
9d4540c9 309 case TUNER_S0194:
d2f918bb
MP
310 /* Tweak for earlier firmware*/
311 if (ibuf[1] == 0x03) {
f23e6616
MP
312 if (ibuf[2] > 1)
313 st->signal_lock = ibuf[2];
d2f918bb 314 st->signal_level = ibuf[3];
ab599a6d 315 st->signal_sn = ibuf[4];
d2f918bb
MP
316 } else {
317 st->signal_level = ibuf[4];
318 st->signal_sn = ibuf[5];
f23e6616
MP
319 st->signal_lock =
320 (st->signal_lock & 0xf7) +
321 ((ibuf[2] & 0x01) << 0x03);
d2f918bb
MP
322 }
323 break;
4f3b7fed 324 case TUNER_RS2000:
e41543a2 325 if (ibuf[1] == 0x3 && ibuf[6] == 0xff)
4f3b7fed
MP
326 st->signal_lock = 0xff;
327 else
e41543a2
MP
328 st->signal_lock = 0x00;
329 st->signal_level = ibuf[5];
330 st->signal_sn = ibuf[4];
4f3b7fed 331 st->time_key = ibuf[7];
d2f918bb
MP
332 default:
333 break;
334 }
335 debug_data_snipet(5, "INT Remote data snipet in", ibuf);
336 break;
337 case 0xcc:
338 debug_data_snipet(1, "INT Control data snipet", ibuf);
339 break;
340 default:
341 debug_data_snipet(1, "INT Unknown data snipet", ibuf);
342 break;
343 }
344 }
345 usb_submit_urb(lme_urb, GFP_ATOMIC);
346}
347
348static int lme2510_int_read(struct dvb_usb_adapter *adap)
349{
e41543a2
MP
350 struct dvb_usb_device *d = adap_to_d(adap);
351 struct lme2510_state *lme_int = adap_to_priv(adap);
d2f918bb
MP
352
353 lme_int->lme_urb = usb_alloc_urb(0, GFP_ATOMIC);
354
355 if (lme_int->lme_urb == NULL)
356 return -ENOMEM;
357
e41543a2 358 lme_int->buffer = usb_alloc_coherent(d->udev, 128, GFP_ATOMIC,
d2f918bb
MP
359 &lme_int->lme_urb->transfer_dma);
360
361 if (lme_int->buffer == NULL)
362 return -ENOMEM;
363
364 usb_fill_int_urb(lme_int->lme_urb,
e41543a2
MP
365 d->udev,
366 usb_rcvintpipe(d->udev, 0xa),
d2f918bb 367 lme_int->buffer,
95c21c41 368 128,
d2f918bb
MP
369 lme2510_int_response,
370 adap,
95c21c41 371 8);
d2f918bb
MP
372
373 lme_int->lme_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
374
375 usb_submit_urb(lme_int->lme_urb, GFP_ATOMIC);
25985edc 376 info("INT Interrupt Service Started");
d2f918bb
MP
377
378 return 0;
379}
380
eb02d857
MP
381static int lme2510_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
382{
e41543a2
MP
383 struct dvb_usb_device *d = adap_to_d(adap);
384 struct lme2510_state *st = adap_to_priv(adap);
4f3b7fed 385 static u8 clear_pid_reg[] = LME_ALL_PIDS;
eb02d857 386 static u8 rbuf[1];
b7dc4cd1 387 int ret = 0;
eb02d857
MP
388
389 deb_info(1, "PID Clearing Filter");
390
e41543a2 391 mutex_lock(&d->i2c_mutex);
eb02d857 392
4f3b7fed 393 if (!onoff) {
e41543a2 394 ret |= lme2510_usb_talk(d, clear_pid_reg,
eb02d857 395 sizeof(clear_pid_reg), rbuf, sizeof(rbuf));
4f3b7fed
MP
396 st->pid_off = true;
397 } else
398 st->pid_off = false;
eb02d857 399
449a0bad
MP
400 st->pid_size = 0;
401
e41543a2 402 mutex_unlock(&d->i2c_mutex);
eb02d857
MP
403
404 return 0;
405}
406
407static int lme2510_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
408 int onoff)
409{
e41543a2 410 struct dvb_usb_device *d = adap_to_d(adap);
eb02d857
MP
411 int ret = 0;
412
413 deb_info(3, "%s PID=%04x Index=%04x onoff=%02x", __func__,
414 pid, index, onoff);
415
4c316b55 416 if (onoff) {
e41543a2
MP
417 mutex_lock(&d->i2c_mutex);
418 ret |= lme2510_enable_pid(d, index, pid);
419 mutex_unlock(&d->i2c_mutex);
449a0bad
MP
420 }
421
eb02d857
MP
422
423 return ret;
424}
425
426
e41543a2 427static int lme2510_return_status(struct dvb_usb_device *d)
d2f918bb
MP
428{
429 int ret = 0;
945b8766
FM
430 u8 *data;
431
432 data = kzalloc(10, GFP_KERNEL);
433 if (!data)
434 return -ENOMEM;
ab599a6d 435
e41543a2 436 ret |= usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0),
d2f918bb
MP
437 0x06, 0x80, 0x0302, 0x00, data, 0x0006, 200);
438 info("Firmware Status: %x (%x)", ret , data[2]);
439
945b8766
FM
440 ret = (ret < 0) ? -ENODEV : data[2];
441 kfree(data);
442 return ret;
d2f918bb
MP
443}
444
445static int lme2510_msg(struct dvb_usb_device *d,
446 u8 *wbuf, int wlen, u8 *rbuf, int rlen)
447{
448 int ret = 0;
449 struct lme2510_state *st = d->priv;
450
ab599a6d
MP
451 if (st->i2c_talk_onoff == 1) {
452
d2f918bb
MP
453 ret = lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
454
d2f918bb 455 switch (st->tuner_config) {
ab599a6d
MP
456 case TUNER_LG:
457 if (wbuf[2] == 0x1c) {
458 if (wbuf[3] == 0x0e) {
459 st->signal_lock = rbuf[1];
460 if ((st->stream_on & 1) &&
461 (st->signal_lock & 0x10)) {
f23e6616 462 lme2510_stream_restart(d);
ab599a6d
MP
463 st->i2c_talk_onoff = 0;
464 }
f23e6616 465 msleep(80);
ab599a6d
MP
466 }
467 }
468 break;
d2f918bb 469 case TUNER_S7395:
ab599a6d
MP
470 if (wbuf[2] == 0xd0) {
471 if (wbuf[3] == 0x24) {
472 st->signal_lock = rbuf[1];
473 if ((st->stream_on & 1) &&
474 (st->signal_lock & 0x8)) {
f23e6616 475 lme2510_stream_restart(d);
ab599a6d
MP
476 st->i2c_talk_onoff = 0;
477 }
478 }
ab599a6d 479 }
d2f918bb 480 break;
9d4540c9
MP
481 case TUNER_S0194:
482 if (wbuf[2] == 0xd0) {
483 if (wbuf[3] == 0x1b) {
484 st->signal_lock = rbuf[1];
485 if ((st->stream_on & 1) &&
486 (st->signal_lock & 0x8)) {
487 lme2510_stream_restart(d);
488 st->i2c_talk_onoff = 0;
489 }
490 }
491 }
492 break;
4f3b7fed 493 case TUNER_RS2000:
d2f918bb
MP
494 default:
495 break;
496 }
d2f918bb 497 } else {
4f3b7fed 498 /* TODO rewrite this section */
d2f918bb
MP
499 switch (st->tuner_config) {
500 case TUNER_LG:
501 switch (wbuf[3]) {
502 case 0x0e:
503 rbuf[0] = 0x55;
504 rbuf[1] = st->signal_lock;
505 break;
506 case 0x43:
507 rbuf[0] = 0x55;
508 rbuf[1] = st->signal_level;
509 break;
510 case 0x1c:
511 rbuf[0] = 0x55;
512 rbuf[1] = st->signal_sn;
513 break;
f23e6616
MP
514 case 0x15:
515 case 0x16:
516 case 0x17:
517 case 0x18:
518 rbuf[0] = 0x55;
519 rbuf[1] = 0x00;
520 break;
d2f918bb 521 default:
f23e6616
MP
522 lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
523 st->i2c_talk_onoff = 1;
d2f918bb
MP
524 break;
525 }
526 break;
527 case TUNER_S7395:
528 switch (wbuf[3]) {
529 case 0x10:
530 rbuf[0] = 0x55;
531 rbuf[1] = (st->signal_level & 0x80)
532 ? 0 : (st->signal_level * 2);
533 break;
534 case 0x2d:
535 rbuf[0] = 0x55;
536 rbuf[1] = st->signal_sn;
537 break;
538 case 0x24:
539 rbuf[0] = 0x55;
f23e6616 540 rbuf[1] = st->signal_lock;
ab599a6d 541 break;
f23e6616
MP
542 case 0x2e:
543 case 0x26:
544 case 0x27:
ab599a6d
MP
545 rbuf[0] = 0x55;
546 rbuf[1] = 0x00;
d2f918bb 547 break;
f23e6616
MP
548 default:
549 lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
550 st->i2c_talk_onoff = 1;
551 break;
d2f918bb
MP
552 }
553 break;
9d4540c9
MP
554 case TUNER_S0194:
555 switch (wbuf[3]) {
556 case 0x18:
557 rbuf[0] = 0x55;
558 rbuf[1] = (st->signal_level & 0x80)
559 ? 0 : (st->signal_level * 2);
560 break;
561 case 0x24:
562 rbuf[0] = 0x55;
563 rbuf[1] = st->signal_sn;
564 break;
565 case 0x1b:
566 rbuf[0] = 0x55;
567 rbuf[1] = st->signal_lock;
568 break;
569 case 0x19:
570 case 0x25:
571 case 0x1e:
572 case 0x1d:
573 rbuf[0] = 0x55;
574 rbuf[1] = 0x00;
575 break;
576 default:
577 lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
578 st->i2c_talk_onoff = 1;
579 break;
580 }
581 break;
4f3b7fed
MP
582 case TUNER_RS2000:
583 switch (wbuf[3]) {
584 case 0x8c:
585 rbuf[0] = 0x55;
586 rbuf[1] = 0xff;
587 if (st->last_key == st->time_key) {
588 st->key_timeout++;
589 if (st->key_timeout > 5)
590 rbuf[1] = 0;
591 } else
592 st->key_timeout = 0;
593 st->last_key = st->time_key;
594 break;
595 default:
596 lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
597 st->i2c_talk_onoff = 1;
598 break;
599 }
d2f918bb
MP
600 default:
601 break;
d2f918bb
MP
602 }
603
25985edc 604 deb_info(4, "I2C From Interrupt Message out(%02x) in(%02x)",
d2f918bb
MP
605 wbuf[3], rbuf[1]);
606
607 }
608
609 return ret;
610}
611
612
613static int lme2510_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
614 int num)
615{
616 struct dvb_usb_device *d = i2c_get_adapdata(adap);
617 struct lme2510_state *st = d->priv;
810fda9b 618 static u8 obuf[64], ibuf[64];
d2f918bb
MP
619 int i, read, read_o;
620 u16 len;
621 u8 gate = st->i2c_gate;
622
4f3b7fed
MP
623 mutex_lock(&d->i2c_mutex);
624
d2f918bb
MP
625 if (gate == 0)
626 gate = 5;
627
d2f918bb
MP
628 for (i = 0; i < num; i++) {
629 read_o = 1 & (msg[i].flags & I2C_M_RD);
630 read = i+1 < num && (msg[i+1].flags & I2C_M_RD);
631 read |= read_o;
632 gate = (msg[i].addr == st->i2c_tuner_addr)
633 ? (read) ? st->i2c_tuner_gate_r
634 : st->i2c_tuner_gate_w
635 : st->i2c_gate;
636 obuf[0] = gate | (read << 7);
637
638 if (gate == 5)
639 obuf[1] = (read) ? 2 : msg[i].len + 1;
640 else
641 obuf[1] = msg[i].len + read + 1;
642
643 obuf[2] = msg[i].addr;
644 if (read) {
645 if (read_o)
646 len = 3;
647 else {
648 memcpy(&obuf[3], msg[i].buf, msg[i].len);
649 obuf[msg[i].len+3] = msg[i+1].len;
650 len = msg[i].len+4;
651 }
652 } else {
653 memcpy(&obuf[3], msg[i].buf, msg[i].len);
654 len = msg[i].len+3;
655 }
656
810fda9b 657 if (lme2510_msg(d, obuf, len, ibuf, 64) < 0) {
d2f918bb 658 deb_info(1, "i2c transfer failed.");
4f3b7fed 659 mutex_unlock(&d->i2c_mutex);
d2f918bb
MP
660 return -EAGAIN;
661 }
662
663 if (read) {
664 if (read_o)
665 memcpy(msg[i].buf, &ibuf[1], msg[i].len);
666 else {
667 memcpy(msg[i+1].buf, &ibuf[1], msg[i+1].len);
668 i++;
669 }
670 }
671 }
4f3b7fed
MP
672
673 mutex_unlock(&d->i2c_mutex);
d2f918bb
MP
674 return i;
675}
676
677static u32 lme2510_i2c_func(struct i2c_adapter *adapter)
678{
679 return I2C_FUNC_I2C;
680}
681
682static struct i2c_algorithm lme2510_i2c_algo = {
683 .master_xfer = lme2510_i2c_xfer,
684 .functionality = lme2510_i2c_func,
685};
686
e41543a2 687static int lme2510_streaming_ctrl(struct dvb_frontend *fe, int onoff)
d2f918bb 688{
e41543a2
MP
689 struct dvb_usb_adapter *adap = fe_to_adap(fe);
690 struct dvb_usb_device *d = adap_to_d(adap);
691 struct lme2510_state *st = adap_to_priv(adap);
4f3b7fed 692 static u8 clear_reg_3[] = LME_ALL_PIDS;
d2f918bb 693 static u8 rbuf[1];
f23e6616 694 int ret = 0, rlen = sizeof(rbuf);
d2f918bb
MP
695
696 deb_info(1, "STM (%02x)", onoff);
697
f23e6616
MP
698 /* Streaming is started by FE_HAS_LOCK */
699 if (onoff == 1)
ab599a6d 700 st->stream_on = 1;
f23e6616 701 else {
d2f918bb 702 deb_info(1, "STM Steam Off");
f23e6616 703 /* mutex is here only to avoid collision with I2C */
e41543a2 704 mutex_lock(&d->i2c_mutex);
f23e6616 705
e41543a2 706 ret = lme2510_usb_talk(d, clear_reg_3,
d2f918bb 707 sizeof(clear_reg_3), rbuf, rlen);
ab599a6d 708 st->stream_on = 0;
d2f918bb 709 st->i2c_talk_onoff = 1;
f23e6616 710
e41543a2 711 mutex_unlock(&d->i2c_mutex);
d2f918bb
MP
712 }
713
714 return (ret < 0) ? -ENODEV : 0;
715}
716
d2f918bb
MP
717static u8 check_sum(u8 *p, u8 len)
718{
719 u8 sum = 0;
720 while (len--)
721 sum += *p++;
722 return sum;
723}
724
e41543a2 725static int lme2510_download_firmware(struct dvb_usb_device *d,
d2f918bb
MP
726 const struct firmware *fw)
727{
728 int ret = 0;
945b8766 729 u8 *data;
d2f918bb
MP
730 u16 j, wlen, len_in, start, end;
731 u8 packet_size, dlen, i;
732 u8 *fw_data;
733
734 packet_size = 0x31;
735 len_in = 1;
736
e41543a2 737 data = kzalloc(128, GFP_KERNEL);
945b8766 738 if (!data) {
e41543a2
MP
739 info("FRM Could not start Firmware Download"\
740 "(Buffer allocation failed)");
945b8766
FM
741 return -ENOMEM;
742 }
d2f918bb
MP
743
744 info("FRM Starting Firmware Download");
745
746 for (i = 1; i < 3; i++) {
747 start = (i == 1) ? 0 : 512;
748 end = (i == 1) ? 512 : fw->size;
749 for (j = start; j < end; j += (packet_size+1)) {
750 fw_data = (u8 *)(fw->data + j);
751 if ((end - j) > packet_size) {
752 data[0] = i;
753 dlen = packet_size;
754 } else {
755 data[0] = i | 0x80;
756 dlen = (u8)(end - j)-1;
757 }
b9f7b73c
FM
758 data[1] = dlen;
759 memcpy(&data[2], fw_data, dlen+1);
760 wlen = (u8) dlen + 4;
761 data[wlen-1] = check_sum(fw_data, dlen+1);
762 deb_info(1, "Data S=%02x:E=%02x CS= %02x", data[3],
d2f918bb 763 data[dlen+2], data[dlen+3]);
e41543a2 764 lme2510_usb_talk(d, data, wlen, data, len_in);
b9f7b73c 765 ret |= (data[0] == 0x88) ? 0 : -1;
d2f918bb
MP
766 }
767 }
f23e6616 768
d2f918bb
MP
769 data[0] = 0x8a;
770 len_in = 1;
771 msleep(2000);
e41543a2 772 lme2510_usb_talk(d, data, len_in, data, len_in);
d2f918bb
MP
773 msleep(400);
774
775 if (ret < 0)
776 info("FRM Firmware Download Failed (%04x)" , ret);
777 else
778 info("FRM Firmware Download Completed - Resetting Device");
779
945b8766 780 kfree(data);
e41543a2 781 return RECONNECTS_USB;
d2f918bb
MP
782}
783
e41543a2 784static void lme_coldreset(struct dvb_usb_device *d)
ab599a6d 785{
e41543a2 786 u8 data[1] = {0};
ab599a6d 787 data[0] = 0x0a;
ab599a6d 788 info("FRM Firmware Cold Reset");
e41543a2
MP
789
790 lme2510_usb_talk(d, data, sizeof(data), data, sizeof(data));
f23e6616 791
ab599a6d
MP
792 return;
793}
794
e41543a2
MP
795static const char fw_c_s7395[] = LME2510_C_S7395;
796static const char fw_c_lg[] = LME2510_C_LG;
797static const char fw_c_s0194[] = LME2510_C_S0194;
798static const char fw_c_rs2000[] = LME2510_C_RS2000;
799static const char fw_lg[] = LME2510_LG;
800static const char fw_s0194[] = LME2510_S0194;
801
802const char *lme_firmware_switch(struct dvb_usb_device *d, int cold)
ab599a6d 803{
e41543a2
MP
804 struct lme2510_state *st = d->priv;
805 struct usb_device *udev = d->udev;
ab599a6d 806 const struct firmware *fw = NULL;
9d4540c9 807 const char *fw_lme;
e41543a2 808 int ret = 0;
ab599a6d 809
f23e6616
MP
810 cold = (cold > 0) ? (cold & 1) : 0;
811
4f3b7fed
MP
812 switch (le16_to_cpu(udev->descriptor.idProduct)) {
813 case 0x1122:
e41543a2 814 switch (st->dvb_usb_lme2510_firmware) {
9d4540c9 815 default:
e41543a2 816 st->dvb_usb_lme2510_firmware = TUNER_S0194;
9d4540c9
MP
817 case TUNER_S0194:
818 fw_lme = fw_s0194;
819 ret = request_firmware(&fw, fw_lme, &udev->dev);
820 if (ret == 0) {
30bd3b56 821 cold = 0;
9d4540c9
MP
822 break;
823 }
e41543a2 824 st->dvb_usb_lme2510_firmware = TUNER_LG;
9d4540c9
MP
825 case TUNER_LG:
826 fw_lme = fw_lg;
827 ret = request_firmware(&fw, fw_lme, &udev->dev);
828 if (ret == 0)
829 break;
e41543a2 830 st->dvb_usb_lme2510_firmware = TUNER_DEFAULT;
ab599a6d
MP
831 break;
832 }
4f3b7fed
MP
833 break;
834 case 0x1120:
e41543a2 835 switch (st->dvb_usb_lme2510_firmware) {
9d4540c9 836 default:
e41543a2 837 st->dvb_usb_lme2510_firmware = TUNER_S7395;
9d4540c9
MP
838 case TUNER_S7395:
839 fw_lme = fw_c_s7395;
840 ret = request_firmware(&fw, fw_lme, &udev->dev);
30bd3b56
MP
841 if (ret == 0) {
842 cold = 0;
9d4540c9 843 break;
30bd3b56 844 }
e41543a2 845 st->dvb_usb_lme2510_firmware = TUNER_LG;
9d4540c9
MP
846 case TUNER_LG:
847 fw_lme = fw_c_lg;
848 ret = request_firmware(&fw, fw_lme, &udev->dev);
849 if (ret == 0)
850 break;
e41543a2 851 st->dvb_usb_lme2510_firmware = TUNER_S0194;
9d4540c9
MP
852 case TUNER_S0194:
853 fw_lme = fw_c_s0194;
854 ret = request_firmware(&fw, fw_lme, &udev->dev);
855 if (ret == 0)
856 break;
e41543a2 857 st->dvb_usb_lme2510_firmware = TUNER_DEFAULT;
ab599a6d 858 cold = 0;
ab599a6d
MP
859 break;
860 }
4f3b7fed
MP
861 break;
862 case 0x22f0:
863 fw_lme = fw_c_rs2000;
e41543a2 864 st->dvb_usb_lme2510_firmware = TUNER_RS2000;
4f3b7fed
MP
865 break;
866 default:
867 fw_lme = fw_c_s7395;
ab599a6d 868 }
f23e6616 869
30bd3b56
MP
870 release_firmware(fw);
871
75aecc3a 872 if (cold) {
e41543a2 873 dvb_usb_lme2510_firmware = st->dvb_usb_lme2510_firmware;
9d4540c9 874 info("FRM Changing to %s firmware", fw_lme);
e41543a2
MP
875 lme_coldreset(d);
876 return NULL;
75aecc3a 877 }
f23e6616 878
e41543a2 879 return fw_lme;
ab599a6d 880}
d2f918bb
MP
881
882static int lme2510_kill_urb(struct usb_data_stream *stream)
883{
884 int i;
f23e6616 885
d2f918bb
MP
886 for (i = 0; i < stream->urbs_submitted; i++) {
887 deb_info(3, "killing URB no. %d.", i);
d2f918bb
MP
888 /* stop the URB */
889 usb_kill_urb(stream->urb_list[i]);
890 }
891 stream->urbs_submitted = 0;
f23e6616 892
d2f918bb
MP
893 return 0;
894}
895
896static struct tda10086_config tda10086_config = {
897 .demod_address = 0x1c,
898 .invert = 0,
899 .diseqc_tone = 1,
900 .xtal_freq = TDA10086_XTAL_16M,
901};
902
903static struct stv0288_config lme_config = {
904 .demod_address = 0xd0,
905 .min_delay_ms = 15,
906 .inittab = s7395_inittab,
907};
908
909static struct ix2505v_config lme_tuner = {
910 .tuner_address = 0xc0,
911 .min_delay_ms = 100,
912 .tuner_gain = 0x0,
913 .tuner_chargepump = 0x3,
914};
915
9d4540c9
MP
916static struct stv0299_config sharp_z0194_config = {
917 .demod_address = 0xd0,
918 .inittab = sharp_z0194a_inittab,
919 .mclk = 88000000UL,
920 .invert = 0,
921 .skip_reinit = 0,
922 .lock_output = STV0299_LOCKOUTPUT_1,
923 .volt13_op0_op1 = STV0299_VOLT13_OP1,
924 .min_delay_ms = 100,
925 .set_symbol_rate = sharp_z0194a_set_symbol_rate,
926};
927
4f3b7fed
MP
928static int dm04_rs2000_set_ts_param(struct dvb_frontend *fe,
929 int caller)
930{
e41543a2
MP
931 struct dvb_usb_adapter *adap = fe_to_adap(fe);
932 struct dvb_usb_device *d = adap_to_d(adap);
4f3b7fed
MP
933 struct lme2510_state *st = d->priv;
934
935 mutex_lock(&d->i2c_mutex);
936 if ((st->i2c_talk_onoff == 1) && (st->stream_on & 1)) {
937 st->i2c_talk_onoff = 0;
938 lme2510_stream_restart(d);
939 }
940 mutex_unlock(&d->i2c_mutex);
941
942 return 0;
943}
944
945static struct m88rs2000_config m88rs2000_config = {
946 .demod_addr = 0xd0,
947 .tuner_addr = 0xc0,
948 .set_ts_params = dm04_rs2000_set_ts_param,
949};
950
d2f918bb
MP
951static int dm04_lme2510_set_voltage(struct dvb_frontend *fe,
952 fe_sec_voltage_t voltage)
953{
e41543a2
MP
954 struct dvb_usb_device *d = fe_to_d(fe);
955 struct lme2510_state *st = fe_to_priv(fe);
956 static u8 voltage_low[] = LME_VOLTAGE_L;
d2f918bb 957 static u8 voltage_high[] = LME_VOLTAGE_H;
d2f918bb
MP
958 static u8 rbuf[1];
959 int ret = 0, len = 3, rlen = 1;
960
e41543a2 961 mutex_lock(&d->i2c_mutex);
d2f918bb 962
ab599a6d
MP
963 switch (voltage) {
964 case SEC_VOLTAGE_18:
e41543a2 965 ret |= lme2510_usb_talk(d,
ab599a6d 966 voltage_high, len, rbuf, rlen);
d2f918bb
MP
967 break;
968
ab599a6d 969 case SEC_VOLTAGE_OFF:
ab599a6d
MP
970 case SEC_VOLTAGE_13:
971 default:
e41543a2 972 ret |= lme2510_usb_talk(d,
d2f918bb
MP
973 voltage_low, len, rbuf, rlen);
974 break;
f23e6616 975 }
d2f918bb 976
e41543a2
MP
977 mutex_unlock(&d->i2c_mutex);
978
979 if (st->tuner_config == TUNER_RS2000)
980 if (st->fe_set_voltage)
981 st->fe_set_voltage(fe, voltage);
982
d2f918bb 983
d2f918bb
MP
984 return (ret < 0) ? -ENODEV : 0;
985}
986
4f3b7fed
MP
987static int dm04_rs2000_read_signal_strength(struct dvb_frontend *fe,
988 u16 *strength)
989{
e41543a2
MP
990 struct lme2510_state *st = fe_to_priv(fe);
991
992 *strength = (u16)((u32)st->signal_level * 0xffff / 0xff);
4f3b7fed 993
4f3b7fed
MP
994 return 0;
995}
996
997static int dm04_rs2000_read_snr(struct dvb_frontend *fe, u16 *snr)
998{
e41543a2
MP
999 struct lme2510_state *st = fe_to_priv(fe);
1000
1001 *snr = (u16)((u32)st->signal_sn * 0xffff / 0x7f);
1002
1003 return 0;
1004}
1005
1006static int dm04_read_ber(struct dvb_frontend *fe, u32 *ber)
1007{
1008 *ber = 0;
1009
1010 return 0;
1011}
1012
1013static int dm04_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
1014{
1015 *ucblocks = 0;
4f3b7fed 1016
4f3b7fed
MP
1017 return 0;
1018}
1019
75aecc3a
MP
1020static int lme_name(struct dvb_usb_adapter *adap)
1021{
e41543a2
MP
1022 struct dvb_usb_device *d = adap_to_d(adap);
1023 struct lme2510_state *st = adap_to_priv(adap);
1024 const char *desc = d->name;
9d4540c9 1025 char *fe_name[] = {"", " LG TDQY-P001F", " SHARP:BS2F7HZ7395",
4f3b7fed 1026 " SHARP:BS2F7HZ0194", " RS2000"};
e41543a2 1027 char *name = adap->fe[0]->ops.info.name;
75aecc3a
MP
1028
1029 strlcpy(name, desc, 128);
1030 strlcat(name, fe_name[st->tuner_config], 128);
1031
1032 return 0;
1033}
1034
d2f918bb
MP
1035static int dm04_lme2510_frontend_attach(struct dvb_usb_adapter *adap)
1036{
e41543a2
MP
1037 struct dvb_usb_device *d = adap_to_d(adap);
1038 struct lme2510_state *st = d->priv;
75aecc3a 1039 int ret = 0;
d2f918bb
MP
1040
1041 st->i2c_talk_onoff = 1;
e41543a2 1042 switch (le16_to_cpu(d->udev->descriptor.idProduct)) {
4f3b7fed
MP
1043 case 0x1122:
1044 case 0x1120:
1045 st->i2c_gate = 4;
e41543a2
MP
1046 adap->fe[0] = dvb_attach(tda10086_attach,
1047 &tda10086_config, &d->i2c_adap);
1048 if (adap->fe[0]) {
4f3b7fed
MP
1049 info("TUN Found Frontend TDA10086");
1050 st->i2c_tuner_gate_w = 4;
1051 st->i2c_tuner_gate_r = 4;
1052 st->i2c_tuner_addr = 0xc0;
1053 st->tuner_config = TUNER_LG;
e41543a2
MP
1054 if (st->dvb_usb_lme2510_firmware != TUNER_LG) {
1055 st->dvb_usb_lme2510_firmware = TUNER_LG;
1056 ret = lme_firmware_switch(d, 1) ? 0 : -ENODEV;
4f3b7fed
MP
1057 }
1058 break;
1059 }
d2f918bb 1060
4f3b7fed 1061 st->i2c_gate = 4;
e41543a2
MP
1062 adap->fe[0] = dvb_attach(stv0299_attach,
1063 &sharp_z0194_config, &d->i2c_adap);
1064 if (adap->fe[0]) {
4f3b7fed
MP
1065 info("FE Found Stv0299");
1066 st->i2c_tuner_gate_w = 4;
1067 st->i2c_tuner_gate_r = 5;
1068 st->i2c_tuner_addr = 0xc0;
1069 st->tuner_config = TUNER_S0194;
e41543a2
MP
1070 if (st->dvb_usb_lme2510_firmware != TUNER_S0194) {
1071 st->dvb_usb_lme2510_firmware = TUNER_S0194;
1072 ret = lme_firmware_switch(d, 1) ? 0 : -ENODEV;
4f3b7fed
MP
1073 }
1074 break;
9d4540c9 1075 }
9d4540c9 1076
4f3b7fed 1077 st->i2c_gate = 5;
e41543a2
MP
1078 adap->fe[0] = dvb_attach(stv0288_attach, &lme_config,
1079 &d->i2c_adap);
4f3b7fed 1080
e41543a2 1081 if (adap->fe[0]) {
4f3b7fed
MP
1082 info("FE Found Stv0288");
1083 st->i2c_tuner_gate_w = 4;
1084 st->i2c_tuner_gate_r = 5;
1085 st->i2c_tuner_addr = 0xc0;
1086 st->tuner_config = TUNER_S7395;
e41543a2
MP
1087 if (st->dvb_usb_lme2510_firmware != TUNER_S7395) {
1088 st->dvb_usb_lme2510_firmware = TUNER_S7395;
1089 ret = lme_firmware_switch(d, 1) ? 0 : -ENODEV;
4f3b7fed
MP
1090 }
1091 break;
9d4540c9 1092 }
4f3b7fed
MP
1093 case 0x22f0:
1094 st->i2c_gate = 5;
e41543a2
MP
1095 adap->fe[0] = dvb_attach(m88rs2000_attach,
1096 &m88rs2000_config, &d->i2c_adap);
f23e6616 1097
e41543a2 1098 if (adap->fe[0]) {
4f3b7fed
MP
1099 info("FE Found M88RS2000");
1100 st->i2c_tuner_gate_w = 5;
1101 st->i2c_tuner_gate_r = 5;
1102 st->i2c_tuner_addr = 0xc0;
1103 st->tuner_config = TUNER_RS2000;
e41543a2
MP
1104 st->fe_set_voltage =
1105 adap->fe[0]->ops.set_voltage;
1106
1107 adap->fe[0]->ops.read_signal_strength =
4f3b7fed 1108 dm04_rs2000_read_signal_strength;
e41543a2 1109 adap->fe[0]->ops.read_snr =
4f3b7fed 1110 dm04_rs2000_read_snr;
e41543a2
MP
1111 adap->fe[0]->ops.read_ber =
1112 dm04_read_ber;
1113 adap->fe[0]->ops.read_ucblocks =
1114 dm04_read_ucblocks;
d2f918bb 1115 }
4f3b7fed 1116 break;
75aecc3a
MP
1117 }
1118
e41543a2
MP
1119 if (adap->fe[0] == NULL) {
1120 info("DM04/QQBOX Not Powered up or not Supported");
1121 return -ENODEV;
4f3b7fed 1122 }
9d4540c9 1123
4f3b7fed 1124 if (ret) {
e41543a2
MP
1125 if (adap->fe[0]) {
1126 dvb_frontend_detach(adap->fe[0]);
1127 adap->fe[0] = NULL;
616a4b83 1128 }
e41543a2 1129 d->rc_map = NULL;
75aecc3a 1130 return -ENODEV;
d2f918bb
MP
1131 }
1132
e41543a2 1133 adap->fe[0]->ops.set_voltage = dm04_lme2510_set_voltage;
75aecc3a 1134 ret = lme_name(adap);
75aecc3a
MP
1135 return ret;
1136}
1137
1138static int dm04_lme2510_tuner(struct dvb_usb_adapter *adap)
1139{
e41543a2
MP
1140 struct dvb_usb_device *d = adap_to_d(adap);
1141 struct lme2510_state *st = adap_to_priv(adap);
4f3b7fed 1142 char *tun_msg[] = {"", "TDA8263", "IX2505V", "DVB_PLL_OPERA", "RS2000"};
75aecc3a
MP
1143 int ret = 0;
1144
1145 switch (st->tuner_config) {
1146 case TUNER_LG:
e41543a2
MP
1147 if (dvb_attach(tda826x_attach, adap->fe[0], 0xc0,
1148 &d->i2c_adap, 1))
75aecc3a
MP
1149 ret = st->tuner_config;
1150 break;
1151 case TUNER_S7395:
e41543a2
MP
1152 if (dvb_attach(ix2505v_attach , adap->fe[0], &lme_tuner,
1153 &d->i2c_adap))
75aecc3a
MP
1154 ret = st->tuner_config;
1155 break;
9d4540c9 1156 case TUNER_S0194:
e41543a2
MP
1157 if (dvb_attach(dvb_pll_attach , adap->fe[0], 0xc0,
1158 &d->i2c_adap, DVB_PLL_OPERA1))
9d4540c9
MP
1159 ret = st->tuner_config;
1160 break;
4f3b7fed
MP
1161 case TUNER_RS2000:
1162 ret = st->tuner_config;
1163 break;
75aecc3a
MP
1164 default:
1165 break;
1166 }
1167
1168 if (ret)
1169 info("TUN Found %s tuner", tun_msg[ret]);
1170 else {
0d616ca3 1171 info("TUN No tuner found --- resetting device");
e41543a2 1172 lme_coldreset(d);
75aecc3a
MP
1173 return -ENODEV;
1174 }
1175
616a4b83
MP
1176 /* Start the Interrupt*/
1177 ret = lme2510_int_read(adap);
1178 if (ret < 0) {
1179 info("INT Unable to start Interrupt Service");
1180 return -ENODEV;
1181 }
75aecc3a
MP
1182
1183 return ret;
d2f918bb
MP
1184}
1185
1186static int lme2510_powerup(struct dvb_usb_device *d, int onoff)
1187{
1188 struct lme2510_state *st = d->priv;
f23e6616
MP
1189 static u8 lnb_on[] = LNB_ON;
1190 static u8 lnb_off[] = LNB_OFF;
1191 static u8 rbuf[1];
4f3b7fed 1192 int ret = 0, len = 3, rlen = 1;
f23e6616 1193
4f3b7fed 1194 mutex_lock(&d->i2c_mutex);
f23e6616
MP
1195
1196 if (onoff)
6f030abf 1197 ret = lme2510_usb_talk(d, lnb_on, len, rbuf, rlen);
f23e6616 1198 else
6f030abf 1199 ret = lme2510_usb_talk(d, lnb_off, len, rbuf, rlen);
f23e6616 1200
d2f918bb 1201 st->i2c_talk_onoff = 1;
f23e6616
MP
1202
1203 mutex_unlock(&d->i2c_mutex);
1204
1205 return ret;
d2f918bb
MP
1206}
1207
e41543a2 1208static int lme2510_get_adapter_count(struct dvb_usb_device *d)
d2f918bb 1209{
e41543a2
MP
1210 return 1;
1211}
d2f918bb 1212
e41543a2
MP
1213static int lme2510_identify_state(struct dvb_usb_device *d, const char **name)
1214{
1215 struct lme2510_state *st = d->priv;
d2f918bb 1216
e41543a2 1217 usb_reset_configuration(d->udev);
d2f918bb 1218
e41543a2
MP
1219 usb_set_interface(d->udev,
1220 d->intf->cur_altsetting->desc.bInterfaceNumber, 1);
d2f918bb 1221
e41543a2 1222 st->dvb_usb_lme2510_firmware = dvb_usb_lme2510_firmware;
ab599a6d 1223
e41543a2
MP
1224 if (lme2510_return_status(d) == 0x44) {
1225 *name = lme_firmware_switch(d, 0);
1226 return COLD;
d2f918bb
MP
1227 }
1228
e41543a2 1229 return 0;
d2f918bb
MP
1230}
1231
e41543a2
MP
1232static int lme2510_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
1233 struct usb_data_stream_properties *stream)
1234{
1235 struct dvb_usb_adapter *adap = fe_to_adap(fe);
1236 struct dvb_usb_device *d = adap_to_d(adap);
d2f918bb 1237
e41543a2
MP
1238 if (adap == NULL)
1239 return 0;
1240 /* Turn PID filter on the fly by module option */
1241 if (pid_filter == 2) {
1242 adap->pid_filtering = 1;
1243 adap->max_feed_count = 15;
d2f918bb 1244 }
d2f918bb 1245
e41543a2
MP
1246 if (!(le16_to_cpu(d->udev->descriptor.idProduct)
1247 == 0x1122))
1248 stream->endpoint = 0x8;
d2f918bb 1249
e41543a2
MP
1250 return 0;
1251}
1252
1253static int lme2510_get_rc_config(struct dvb_usb_device *d,
1254 struct dvb_usb_rc *rc)
1255{
c003ab1b 1256 rc->allowed_protos = RC_BIT_NEC;
e41543a2
MP
1257 return 0;
1258}
d2f918bb 1259
a760d638 1260static void *lme2510_exit_int(struct dvb_usb_device *d)
d2f918bb
MP
1261{
1262 struct lme2510_state *st = d->priv;
ab599a6d
MP
1263 struct dvb_usb_adapter *adap = &d->adapter[0];
1264 void *buffer = NULL;
1265
1266 if (adap != NULL) {
e41543a2 1267 lme2510_kill_urb(&adap->stream);
ab599a6d
MP
1268 }
1269
616a4b83 1270 if (st->usb_buffer != NULL) {
ab599a6d 1271 st->i2c_talk_onoff = 1;
d2f918bb
MP
1272 st->signal_lock = 0;
1273 st->signal_level = 0;
1274 st->signal_sn = 0;
ab599a6d 1275 buffer = st->usb_buffer;
616a4b83
MP
1276 }
1277
1278 if (st->lme_urb != NULL) {
d2f918bb 1279 usb_kill_urb(st->lme_urb);
95c21c41 1280 usb_free_coherent(d->udev, 128, st->buffer,
d2f918bb 1281 st->lme_urb->transfer_dma);
25985edc 1282 info("Interrupt Service Stopped");
d2f918bb 1283 }
616a4b83 1284
ab599a6d 1285 return buffer;
d2f918bb
MP
1286}
1287
e41543a2 1288static void lme2510_exit(struct dvb_usb_device *d)
d2f918bb 1289{
ab599a6d
MP
1290 void *usb_buffer;
1291
d2f918bb 1292 if (d != NULL) {
ab599a6d 1293 usb_buffer = lme2510_exit_int(d);
616a4b83
MP
1294 if (usb_buffer != NULL)
1295 kfree(usb_buffer);
d2f918bb 1296 }
d2f918bb
MP
1297}
1298
e41543a2
MP
1299static struct dvb_usb_device_properties lme2510_props = {
1300 .driver_name = KBUILD_MODNAME,
1301 .owner = THIS_MODULE,
1302 .bInterfaceNumber = 0,
1303 .adapter_nr = adapter_nr,
1304 .size_of_priv = sizeof(struct lme2510_state),
1305
1306 .download_firmware = lme2510_download_firmware,
1307
1308 .power_ctrl = lme2510_powerup,
1309 .identify_state = lme2510_identify_state,
1310 .i2c_algo = &lme2510_i2c_algo,
1311
1312 .frontend_attach = dm04_lme2510_frontend_attach,
1313 .tuner_attach = dm04_lme2510_tuner,
1314 .get_stream_config = lme2510_get_stream_config,
1315 .get_adapter_count = lme2510_get_adapter_count,
1316 .streaming_ctrl = lme2510_streaming_ctrl,
1317
1318 .get_rc_config = lme2510_get_rc_config,
1319
1320 .exit = lme2510_exit,
1321 .adapter = {
1322 {
1323 .caps = DVB_USB_ADAP_HAS_PID_FILTER|
1324 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1325 .pid_filter_count = 15,
1326 .pid_filter = lme2510_pid_filter,
1327 .pid_filter_ctrl = lme2510_pid_filter_ctrl,
1328 .stream =
1329 DVB_USB_STREAM_BULK(0x86, 10, 4096),
1330 },
1331 {
1332 }
1333 },
1334};
1335
1336static const struct usb_device_id lme2510_id_table[] = {
1337 { DVB_USB_DEVICE(0x3344, 0x1122, &lme2510_props,
1338 "DM04_LME2510_DVB-S", RC_MAP_LME2510) },
1339 { DVB_USB_DEVICE(0x3344, 0x1120, &lme2510_props,
1340 "DM04_LME2510C_DVB-S", RC_MAP_LME2510) },
1341 { DVB_USB_DEVICE(0x3344, 0x22f0, &lme2510_props,
1342 "DM04_LME2510C_DVB-S RS2000", RC_MAP_LME2510) },
1343 {} /* Terminating entry */
1344};
1345
1346MODULE_DEVICE_TABLE(usb, lme2510_id_table);
1347
d2f918bb 1348static struct usb_driver lme2510_driver = {
e41543a2
MP
1349 .name = KBUILD_MODNAME,
1350 .probe = dvb_usbv2_probe,
1351 .disconnect = dvb_usbv2_disconnect,
1352 .id_table = lme2510_id_table,
1353 .no_dynamic_id = 1,
1354 .soft_unbind = 1,
d2f918bb
MP
1355};
1356
ecb3b2b3 1357module_usb_driver(lme2510_driver);
d2f918bb
MP
1358
1359MODULE_AUTHOR("Malcolm Priestley <tvboxspy@gmail.com>");
75aecc3a 1360MODULE_DESCRIPTION("LME2510(C) DVB-S USB2.0");
e41543a2 1361MODULE_VERSION("2.06");
d2f918bb 1362MODULE_LICENSE("GPL");
e41543a2
MP
1363MODULE_FIRMWARE(LME2510_C_S7395);
1364MODULE_FIRMWARE(LME2510_C_LG);
1365MODULE_FIRMWARE(LME2510_C_S0194);
1366MODULE_FIRMWARE(LME2510_C_RS2000);
1367MODULE_FIRMWARE(LME2510_LG);
1368MODULE_FIRMWARE(LME2510_S0194);
1369