[media] af9035: remove I2C client differently
[deliverable/linux.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"
b858c331 84#include "ts2020.h"
d2f918bb
MP
85
86
e41543a2
MP
87#define LME2510_C_S7395 "dvb-usb-lme2510c-s7395.fw";
88#define LME2510_C_LG "dvb-usb-lme2510c-lg.fw";
89#define LME2510_C_S0194 "dvb-usb-lme2510c-s0194.fw";
90#define LME2510_C_RS2000 "dvb-usb-lme2510c-rs2000.fw";
91#define LME2510_LG "dvb-usb-lme2510-lg.fw";
92#define LME2510_S0194 "dvb-usb-lme2510-s0194.fw";
d2f918bb
MP
93
94/* debug */
95static int dvb_usb_lme2510_debug;
e41543a2 96#define lme_debug(var, level, args...) do { \
d2f918bb 97 if ((var >= level)) \
e41543a2 98 pr_debug(DVB_USB_LOG_PREFIX": " args); \
d2f918bb 99} while (0)
e41543a2 100#define deb_info(level, args...) lme_debug(dvb_usb_lme2510_debug, level, args)
d2f918bb
MP
101#define debug_data_snipet(level, name, p) \
102 deb_info(level, name" (%02x%02x%02x%02x%02x%02x%02x%02x)", \
103 *p, *(p+1), *(p+2), *(p+3), *(p+4), \
104 *(p+5), *(p+6), *(p+7));
e41543a2 105#define info(args...) pr_info(DVB_USB_LOG_PREFIX": "args)
d2f918bb
MP
106
107module_param_named(debug, dvb_usb_lme2510_debug, int, 0644);
e41543a2 108MODULE_PARM_DESC(debug, "set debugging level (1=info (or-able)).");
d2f918bb 109
ab599a6d
MP
110static int dvb_usb_lme2510_firmware;
111module_param_named(firmware, dvb_usb_lme2510_firmware, int, 0644);
112MODULE_PARM_DESC(firmware, "set default firmware 0=Sharp7395 1=LG");
113
eb02d857
MP
114static int pid_filter;
115module_param_named(pid, pid_filter, int, 0644);
88495030 116MODULE_PARM_DESC(pid, "set default 0=default 1=off 2=on");
ab599a6d 117
4f3b7fed 118
d2f918bb 119DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
9d4540c9
MP
120
121#define TUNER_DEFAULT 0x0
ab599a6d
MP
122#define TUNER_LG 0x1
123#define TUNER_S7395 0x2
9d4540c9 124#define TUNER_S0194 0x3
4f3b7fed 125#define TUNER_RS2000 0x4
d2f918bb
MP
126
127struct lme2510_state {
3a370222 128 unsigned long int_urb_due;
d2f918bb
MP
129 u8 id;
130 u8 tuner_config;
131 u8 signal_lock;
132 u8 signal_level;
133 u8 signal_sn;
134 u8 time_key;
135 u8 i2c_talk_onoff;
136 u8 i2c_gate;
137 u8 i2c_tuner_gate_w;
138 u8 i2c_tuner_gate_r;
139 u8 i2c_tuner_addr;
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) {
120703f9
DH
289 key = RC_SCANCODE_NECX((ibuf[2] ^ 0xff) << 8 |
290 (ibuf[3] > 0) ? (ibuf[3] ^ 0xff) : 0,
291 ibuf[5]);
616a4b83 292 deb_info(1, "INT Key =%08x", key);
e41543a2
MP
293 if (adap_to_d(adap)->rc_dev != NULL)
294 rc_keydown(adap_to_d(adap)->rc_dev,
120703f9 295 RC_TYPE_NEC, key, 0);
616a4b83 296 }
d2f918bb
MP
297 break;
298 case 0xbb:
299 switch (st->tuner_config) {
300 case TUNER_LG:
ab599a6d
MP
301 if (ibuf[2] > 0)
302 st->signal_lock = ibuf[2];
d2f918bb
MP
303 st->signal_level = ibuf[4];
304 st->signal_sn = ibuf[3];
305 st->time_key = ibuf[7];
306 break;
307 case TUNER_S7395:
9d4540c9 308 case TUNER_S0194:
d2f918bb
MP
309 /* Tweak for earlier firmware*/
310 if (ibuf[1] == 0x03) {
f23e6616
MP
311 if (ibuf[2] > 1)
312 st->signal_lock = ibuf[2];
d2f918bb 313 st->signal_level = ibuf[3];
ab599a6d 314 st->signal_sn = ibuf[4];
d2f918bb
MP
315 } else {
316 st->signal_level = ibuf[4];
317 st->signal_sn = ibuf[5];
f23e6616
MP
318 st->signal_lock =
319 (st->signal_lock & 0xf7) +
320 ((ibuf[2] & 0x01) << 0x03);
d2f918bb
MP
321 }
322 break;
4f3b7fed 323 case TUNER_RS2000:
3a370222 324 if (ibuf[2] & 0x1)
4f3b7fed
MP
325 st->signal_lock = 0xff;
326 else
e41543a2
MP
327 st->signal_lock = 0x00;
328 st->signal_level = ibuf[5];
329 st->signal_sn = ibuf[4];
4f3b7fed 330 st->time_key = ibuf[7];
d2f918bb
MP
331 default:
332 break;
333 }
334 debug_data_snipet(5, "INT Remote data snipet in", ibuf);
335 break;
336 case 0xcc:
337 debug_data_snipet(1, "INT Control data snipet", ibuf);
338 break;
339 default:
340 debug_data_snipet(1, "INT Unknown data snipet", ibuf);
341 break;
342 }
343 }
3a370222 344
d2f918bb 345 usb_submit_urb(lme_urb, GFP_ATOMIC);
3a370222
MP
346
347 /* interrupt urb is due every 48 msecs while streaming
348 * add 12msecs for system lag */
349 st->int_urb_due = jiffies + msecs_to_jiffies(60);
d2f918bb
MP
350}
351
352static int lme2510_int_read(struct dvb_usb_adapter *adap)
353{
e41543a2
MP
354 struct dvb_usb_device *d = adap_to_d(adap);
355 struct lme2510_state *lme_int = adap_to_priv(adap);
d2f918bb
MP
356
357 lme_int->lme_urb = usb_alloc_urb(0, GFP_ATOMIC);
358
359 if (lme_int->lme_urb == NULL)
360 return -ENOMEM;
361
e41543a2 362 lme_int->buffer = usb_alloc_coherent(d->udev, 128, GFP_ATOMIC,
d2f918bb
MP
363 &lme_int->lme_urb->transfer_dma);
364
365 if (lme_int->buffer == NULL)
366 return -ENOMEM;
367
368 usb_fill_int_urb(lme_int->lme_urb,
e41543a2
MP
369 d->udev,
370 usb_rcvintpipe(d->udev, 0xa),
d2f918bb 371 lme_int->buffer,
95c21c41 372 128,
d2f918bb
MP
373 lme2510_int_response,
374 adap,
95c21c41 375 8);
d2f918bb
MP
376
377 lme_int->lme_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
378
379 usb_submit_urb(lme_int->lme_urb, GFP_ATOMIC);
25985edc 380 info("INT Interrupt Service Started");
d2f918bb
MP
381
382 return 0;
383}
384
eb02d857
MP
385static int lme2510_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
386{
e41543a2
MP
387 struct dvb_usb_device *d = adap_to_d(adap);
388 struct lme2510_state *st = adap_to_priv(adap);
4f3b7fed 389 static u8 clear_pid_reg[] = LME_ALL_PIDS;
eb02d857 390 static u8 rbuf[1];
b7dc4cd1 391 int ret = 0;
eb02d857
MP
392
393 deb_info(1, "PID Clearing Filter");
394
e41543a2 395 mutex_lock(&d->i2c_mutex);
eb02d857 396
4f3b7fed 397 if (!onoff) {
e41543a2 398 ret |= lme2510_usb_talk(d, clear_pid_reg,
eb02d857 399 sizeof(clear_pid_reg), rbuf, sizeof(rbuf));
4f3b7fed
MP
400 st->pid_off = true;
401 } else
402 st->pid_off = false;
eb02d857 403
449a0bad
MP
404 st->pid_size = 0;
405
e41543a2 406 mutex_unlock(&d->i2c_mutex);
eb02d857
MP
407
408 return 0;
409}
410
411static int lme2510_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
412 int onoff)
413{
e41543a2 414 struct dvb_usb_device *d = adap_to_d(adap);
eb02d857
MP
415 int ret = 0;
416
417 deb_info(3, "%s PID=%04x Index=%04x onoff=%02x", __func__,
418 pid, index, onoff);
419
4c316b55 420 if (onoff) {
e41543a2
MP
421 mutex_lock(&d->i2c_mutex);
422 ret |= lme2510_enable_pid(d, index, pid);
423 mutex_unlock(&d->i2c_mutex);
449a0bad
MP
424 }
425
eb02d857
MP
426
427 return ret;
428}
429
430
e41543a2 431static int lme2510_return_status(struct dvb_usb_device *d)
d2f918bb
MP
432{
433 int ret = 0;
945b8766
FM
434 u8 *data;
435
436 data = kzalloc(10, GFP_KERNEL);
437 if (!data)
438 return -ENOMEM;
ab599a6d 439
e41543a2 440 ret |= usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0),
d2f918bb
MP
441 0x06, 0x80, 0x0302, 0x00, data, 0x0006, 200);
442 info("Firmware Status: %x (%x)", ret , data[2]);
443
945b8766
FM
444 ret = (ret < 0) ? -ENODEV : data[2];
445 kfree(data);
446 return ret;
d2f918bb
MP
447}
448
449static int lme2510_msg(struct dvb_usb_device *d,
450 u8 *wbuf, int wlen, u8 *rbuf, int rlen)
451{
452 int ret = 0;
453 struct lme2510_state *st = d->priv;
454
ab599a6d
MP
455 if (st->i2c_talk_onoff == 1) {
456
d2f918bb
MP
457 ret = lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
458
d2f918bb 459 switch (st->tuner_config) {
ab599a6d
MP
460 case TUNER_LG:
461 if (wbuf[2] == 0x1c) {
462 if (wbuf[3] == 0x0e) {
463 st->signal_lock = rbuf[1];
464 if ((st->stream_on & 1) &&
465 (st->signal_lock & 0x10)) {
f23e6616 466 lme2510_stream_restart(d);
ab599a6d
MP
467 st->i2c_talk_onoff = 0;
468 }
f23e6616 469 msleep(80);
ab599a6d
MP
470 }
471 }
472 break;
d2f918bb 473 case TUNER_S7395:
ab599a6d
MP
474 if (wbuf[2] == 0xd0) {
475 if (wbuf[3] == 0x24) {
476 st->signal_lock = rbuf[1];
477 if ((st->stream_on & 1) &&
478 (st->signal_lock & 0x8)) {
f23e6616 479 lme2510_stream_restart(d);
ab599a6d
MP
480 st->i2c_talk_onoff = 0;
481 }
482 }
ab599a6d 483 }
d2f918bb 484 break;
9d4540c9
MP
485 case TUNER_S0194:
486 if (wbuf[2] == 0xd0) {
487 if (wbuf[3] == 0x1b) {
488 st->signal_lock = rbuf[1];
489 if ((st->stream_on & 1) &&
490 (st->signal_lock & 0x8)) {
491 lme2510_stream_restart(d);
492 st->i2c_talk_onoff = 0;
493 }
494 }
495 }
496 break;
4f3b7fed 497 case TUNER_RS2000:
d2f918bb
MP
498 default:
499 break;
500 }
d2f918bb 501 } else {
4f3b7fed 502 /* TODO rewrite this section */
d2f918bb
MP
503 switch (st->tuner_config) {
504 case TUNER_LG:
505 switch (wbuf[3]) {
506 case 0x0e:
507 rbuf[0] = 0x55;
508 rbuf[1] = st->signal_lock;
509 break;
510 case 0x43:
511 rbuf[0] = 0x55;
512 rbuf[1] = st->signal_level;
513 break;
514 case 0x1c:
515 rbuf[0] = 0x55;
516 rbuf[1] = st->signal_sn;
517 break;
f23e6616
MP
518 case 0x15:
519 case 0x16:
520 case 0x17:
521 case 0x18:
522 rbuf[0] = 0x55;
523 rbuf[1] = 0x00;
524 break;
d2f918bb 525 default:
f23e6616
MP
526 lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
527 st->i2c_talk_onoff = 1;
d2f918bb
MP
528 break;
529 }
530 break;
531 case TUNER_S7395:
532 switch (wbuf[3]) {
533 case 0x10:
534 rbuf[0] = 0x55;
535 rbuf[1] = (st->signal_level & 0x80)
536 ? 0 : (st->signal_level * 2);
537 break;
538 case 0x2d:
539 rbuf[0] = 0x55;
540 rbuf[1] = st->signal_sn;
541 break;
542 case 0x24:
543 rbuf[0] = 0x55;
f23e6616 544 rbuf[1] = st->signal_lock;
ab599a6d 545 break;
f23e6616
MP
546 case 0x2e:
547 case 0x26:
548 case 0x27:
ab599a6d
MP
549 rbuf[0] = 0x55;
550 rbuf[1] = 0x00;
d2f918bb 551 break;
f23e6616
MP
552 default:
553 lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
554 st->i2c_talk_onoff = 1;
555 break;
d2f918bb
MP
556 }
557 break;
9d4540c9
MP
558 case TUNER_S0194:
559 switch (wbuf[3]) {
560 case 0x18:
561 rbuf[0] = 0x55;
562 rbuf[1] = (st->signal_level & 0x80)
563 ? 0 : (st->signal_level * 2);
564 break;
565 case 0x24:
566 rbuf[0] = 0x55;
567 rbuf[1] = st->signal_sn;
568 break;
569 case 0x1b:
570 rbuf[0] = 0x55;
571 rbuf[1] = st->signal_lock;
572 break;
573 case 0x19:
574 case 0x25:
575 case 0x1e:
576 case 0x1d:
577 rbuf[0] = 0x55;
578 rbuf[1] = 0x00;
579 break;
580 default:
581 lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
582 st->i2c_talk_onoff = 1;
583 break;
584 }
585 break;
4f3b7fed
MP
586 case TUNER_RS2000:
587 switch (wbuf[3]) {
588 case 0x8c:
589 rbuf[0] = 0x55;
3a370222
MP
590 rbuf[1] = st->signal_lock;
591
592 /* If int_urb_due overdue
593 * set rbuf[1] to 0 to clear lock */
594 if (time_after(jiffies, st->int_urb_due))
595 rbuf[1] = 0;
596
4f3b7fed
MP
597 break;
598 default:
599 lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
600 st->i2c_talk_onoff = 1;
601 break;
602 }
d2f918bb
MP
603 default:
604 break;
d2f918bb
MP
605 }
606
25985edc 607 deb_info(4, "I2C From Interrupt Message out(%02x) in(%02x)",
d2f918bb
MP
608 wbuf[3], rbuf[1]);
609
610 }
611
612 return ret;
613}
614
615
616static int lme2510_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
617 int num)
618{
619 struct dvb_usb_device *d = i2c_get_adapdata(adap);
620 struct lme2510_state *st = d->priv;
810fda9b 621 static u8 obuf[64], ibuf[64];
d2f918bb
MP
622 int i, read, read_o;
623 u16 len;
624 u8 gate = st->i2c_gate;
625
4f3b7fed
MP
626 mutex_lock(&d->i2c_mutex);
627
d2f918bb
MP
628 if (gate == 0)
629 gate = 5;
630
d2f918bb 631 for (i = 0; i < num; i++) {
71dc98be
MP
632 read_o = msg[i].flags & I2C_M_RD;
633 read = i + 1 < num && msg[i + 1].flags & I2C_M_RD;
d2f918bb
MP
634 read |= read_o;
635 gate = (msg[i].addr == st->i2c_tuner_addr)
636 ? (read) ? st->i2c_tuner_gate_r
637 : st->i2c_tuner_gate_w
638 : st->i2c_gate;
639 obuf[0] = gate | (read << 7);
640
641 if (gate == 5)
642 obuf[1] = (read) ? 2 : msg[i].len + 1;
643 else
644 obuf[1] = msg[i].len + read + 1;
645
71dc98be
MP
646 obuf[2] = msg[i].addr << 1;
647
d2f918bb
MP
648 if (read) {
649 if (read_o)
650 len = 3;
651 else {
652 memcpy(&obuf[3], msg[i].buf, msg[i].len);
653 obuf[msg[i].len+3] = msg[i+1].len;
654 len = msg[i].len+4;
655 }
656 } else {
657 memcpy(&obuf[3], msg[i].buf, msg[i].len);
658 len = msg[i].len+3;
659 }
660
810fda9b 661 if (lme2510_msg(d, obuf, len, ibuf, 64) < 0) {
d2f918bb 662 deb_info(1, "i2c transfer failed.");
4f3b7fed 663 mutex_unlock(&d->i2c_mutex);
d2f918bb
MP
664 return -EAGAIN;
665 }
666
667 if (read) {
668 if (read_o)
669 memcpy(msg[i].buf, &ibuf[1], msg[i].len);
670 else {
671 memcpy(msg[i+1].buf, &ibuf[1], msg[i+1].len);
672 i++;
673 }
674 }
675 }
4f3b7fed
MP
676
677 mutex_unlock(&d->i2c_mutex);
d2f918bb
MP
678 return i;
679}
680
681static u32 lme2510_i2c_func(struct i2c_adapter *adapter)
682{
683 return I2C_FUNC_I2C;
684}
685
686static struct i2c_algorithm lme2510_i2c_algo = {
687 .master_xfer = lme2510_i2c_xfer,
688 .functionality = lme2510_i2c_func,
689};
690
e41543a2 691static int lme2510_streaming_ctrl(struct dvb_frontend *fe, int onoff)
d2f918bb 692{
e41543a2
MP
693 struct dvb_usb_adapter *adap = fe_to_adap(fe);
694 struct dvb_usb_device *d = adap_to_d(adap);
695 struct lme2510_state *st = adap_to_priv(adap);
4f3b7fed 696 static u8 clear_reg_3[] = LME_ALL_PIDS;
d2f918bb 697 static u8 rbuf[1];
f23e6616 698 int ret = 0, rlen = sizeof(rbuf);
d2f918bb
MP
699
700 deb_info(1, "STM (%02x)", onoff);
701
f23e6616
MP
702 /* Streaming is started by FE_HAS_LOCK */
703 if (onoff == 1)
ab599a6d 704 st->stream_on = 1;
f23e6616 705 else {
d2f918bb 706 deb_info(1, "STM Steam Off");
f23e6616 707 /* mutex is here only to avoid collision with I2C */
e41543a2 708 mutex_lock(&d->i2c_mutex);
f23e6616 709
e41543a2 710 ret = lme2510_usb_talk(d, clear_reg_3,
d2f918bb 711 sizeof(clear_reg_3), rbuf, rlen);
ab599a6d 712 st->stream_on = 0;
d2f918bb 713 st->i2c_talk_onoff = 1;
f23e6616 714
e41543a2 715 mutex_unlock(&d->i2c_mutex);
d2f918bb
MP
716 }
717
718 return (ret < 0) ? -ENODEV : 0;
719}
720
d2f918bb
MP
721static u8 check_sum(u8 *p, u8 len)
722{
723 u8 sum = 0;
724 while (len--)
725 sum += *p++;
726 return sum;
727}
728
e41543a2 729static int lme2510_download_firmware(struct dvb_usb_device *d,
d2f918bb
MP
730 const struct firmware *fw)
731{
732 int ret = 0;
945b8766 733 u8 *data;
d2f918bb
MP
734 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
e41543a2 741 data = kzalloc(128, GFP_KERNEL);
945b8766 742 if (!data) {
e41543a2
MP
743 info("FRM Could not start Firmware Download"\
744 "(Buffer allocation failed)");
945b8766
FM
745 return -ENOMEM;
746 }
d2f918bb
MP
747
748 info("FRM Starting Firmware Download");
749
750 for (i = 1; i < 3; i++) {
751 start = (i == 1) ? 0 : 512;
752 end = (i == 1) ? 512 : fw->size;
753 for (j = start; j < end; j += (packet_size+1)) {
754 fw_data = (u8 *)(fw->data + j);
755 if ((end - j) > packet_size) {
756 data[0] = i;
757 dlen = packet_size;
758 } else {
759 data[0] = i | 0x80;
760 dlen = (u8)(end - j)-1;
761 }
b9f7b73c
FM
762 data[1] = dlen;
763 memcpy(&data[2], fw_data, dlen+1);
764 wlen = (u8) dlen + 4;
765 data[wlen-1] = check_sum(fw_data, dlen+1);
766 deb_info(1, "Data S=%02x:E=%02x CS= %02x", data[3],
d2f918bb 767 data[dlen+2], data[dlen+3]);
e41543a2 768 lme2510_usb_talk(d, data, wlen, data, len_in);
b9f7b73c 769 ret |= (data[0] == 0x88) ? 0 : -1;
d2f918bb
MP
770 }
771 }
f23e6616 772
d2f918bb
MP
773 data[0] = 0x8a;
774 len_in = 1;
775 msleep(2000);
e41543a2 776 lme2510_usb_talk(d, data, len_in, data, len_in);
d2f918bb
MP
777 msleep(400);
778
779 if (ret < 0)
780 info("FRM Firmware Download Failed (%04x)" , ret);
781 else
782 info("FRM Firmware Download Completed - Resetting Device");
783
945b8766 784 kfree(data);
e41543a2 785 return RECONNECTS_USB;
d2f918bb
MP
786}
787
e41543a2 788static void lme_coldreset(struct dvb_usb_device *d)
ab599a6d 789{
e41543a2 790 u8 data[1] = {0};
ab599a6d 791 data[0] = 0x0a;
ab599a6d 792 info("FRM Firmware Cold Reset");
e41543a2
MP
793
794 lme2510_usb_talk(d, data, sizeof(data), data, sizeof(data));
f23e6616 795
ab599a6d
MP
796 return;
797}
798
e41543a2
MP
799static const char fw_c_s7395[] = LME2510_C_S7395;
800static const char fw_c_lg[] = LME2510_C_LG;
801static const char fw_c_s0194[] = LME2510_C_S0194;
802static const char fw_c_rs2000[] = LME2510_C_RS2000;
803static const char fw_lg[] = LME2510_LG;
804static const char fw_s0194[] = LME2510_S0194;
805
1885fd2d 806static const char *lme_firmware_switch(struct dvb_usb_device *d, int cold)
ab599a6d 807{
e41543a2
MP
808 struct lme2510_state *st = d->priv;
809 struct usb_device *udev = d->udev;
ab599a6d 810 const struct firmware *fw = NULL;
9d4540c9 811 const char *fw_lme;
e41543a2 812 int ret = 0;
ab599a6d 813
f23e6616
MP
814 cold = (cold > 0) ? (cold & 1) : 0;
815
4f3b7fed
MP
816 switch (le16_to_cpu(udev->descriptor.idProduct)) {
817 case 0x1122:
e41543a2 818 switch (st->dvb_usb_lme2510_firmware) {
9d4540c9 819 default:
e41543a2 820 st->dvb_usb_lme2510_firmware = TUNER_S0194;
9d4540c9
MP
821 case TUNER_S0194:
822 fw_lme = fw_s0194;
823 ret = request_firmware(&fw, fw_lme, &udev->dev);
824 if (ret == 0) {
30bd3b56 825 cold = 0;
9d4540c9
MP
826 break;
827 }
e41543a2 828 st->dvb_usb_lme2510_firmware = TUNER_LG;
9d4540c9
MP
829 case TUNER_LG:
830 fw_lme = fw_lg;
831 ret = request_firmware(&fw, fw_lme, &udev->dev);
832 if (ret == 0)
833 break;
e41543a2 834 st->dvb_usb_lme2510_firmware = TUNER_DEFAULT;
ab599a6d
MP
835 break;
836 }
4f3b7fed
MP
837 break;
838 case 0x1120:
e41543a2 839 switch (st->dvb_usb_lme2510_firmware) {
9d4540c9 840 default:
e41543a2 841 st->dvb_usb_lme2510_firmware = TUNER_S7395;
9d4540c9
MP
842 case TUNER_S7395:
843 fw_lme = fw_c_s7395;
844 ret = request_firmware(&fw, fw_lme, &udev->dev);
30bd3b56
MP
845 if (ret == 0) {
846 cold = 0;
9d4540c9 847 break;
30bd3b56 848 }
e41543a2 849 st->dvb_usb_lme2510_firmware = TUNER_LG;
9d4540c9
MP
850 case TUNER_LG:
851 fw_lme = fw_c_lg;
852 ret = request_firmware(&fw, fw_lme, &udev->dev);
853 if (ret == 0)
854 break;
e41543a2 855 st->dvb_usb_lme2510_firmware = TUNER_S0194;
9d4540c9
MP
856 case TUNER_S0194:
857 fw_lme = fw_c_s0194;
858 ret = request_firmware(&fw, fw_lme, &udev->dev);
859 if (ret == 0)
860 break;
e41543a2 861 st->dvb_usb_lme2510_firmware = TUNER_DEFAULT;
ab599a6d 862 cold = 0;
ab599a6d
MP
863 break;
864 }
4f3b7fed
MP
865 break;
866 case 0x22f0:
867 fw_lme = fw_c_rs2000;
e41543a2 868 st->dvb_usb_lme2510_firmware = TUNER_RS2000;
4f3b7fed
MP
869 break;
870 default:
871 fw_lme = fw_c_s7395;
ab599a6d 872 }
f23e6616 873
30bd3b56
MP
874 release_firmware(fw);
875
75aecc3a 876 if (cold) {
e41543a2 877 dvb_usb_lme2510_firmware = st->dvb_usb_lme2510_firmware;
9d4540c9 878 info("FRM Changing to %s firmware", fw_lme);
e41543a2
MP
879 lme_coldreset(d);
880 return NULL;
75aecc3a 881 }
f23e6616 882
e41543a2 883 return fw_lme;
ab599a6d 884}
d2f918bb
MP
885
886static int lme2510_kill_urb(struct usb_data_stream *stream)
887{
888 int i;
f23e6616 889
d2f918bb
MP
890 for (i = 0; i < stream->urbs_submitted; i++) {
891 deb_info(3, "killing URB no. %d.", i);
d2f918bb
MP
892 /* stop the URB */
893 usb_kill_urb(stream->urb_list[i]);
894 }
895 stream->urbs_submitted = 0;
f23e6616 896
d2f918bb
MP
897 return 0;
898}
899
900static struct tda10086_config tda10086_config = {
71dc98be 901 .demod_address = 0x0e,
d2f918bb
MP
902 .invert = 0,
903 .diseqc_tone = 1,
904 .xtal_freq = TDA10086_XTAL_16M,
905};
906
907static struct stv0288_config lme_config = {
71dc98be 908 .demod_address = 0x68,
d2f918bb
MP
909 .min_delay_ms = 15,
910 .inittab = s7395_inittab,
911};
912
913static struct ix2505v_config lme_tuner = {
71dc98be 914 .tuner_address = 0x60,
d2f918bb
MP
915 .min_delay_ms = 100,
916 .tuner_gain = 0x0,
917 .tuner_chargepump = 0x3,
918};
919
9d4540c9 920static struct stv0299_config sharp_z0194_config = {
71dc98be 921 .demod_address = 0x68,
9d4540c9
MP
922 .inittab = sharp_z0194a_inittab,
923 .mclk = 88000000UL,
924 .invert = 0,
925 .skip_reinit = 0,
926 .lock_output = STV0299_LOCKOUTPUT_1,
927 .volt13_op0_op1 = STV0299_VOLT13_OP1,
928 .min_delay_ms = 100,
929 .set_symbol_rate = sharp_z0194a_set_symbol_rate,
930};
931
4f3b7fed
MP
932static int dm04_rs2000_set_ts_param(struct dvb_frontend *fe,
933 int caller)
934{
e41543a2
MP
935 struct dvb_usb_adapter *adap = fe_to_adap(fe);
936 struct dvb_usb_device *d = adap_to_d(adap);
4f3b7fed
MP
937 struct lme2510_state *st = d->priv;
938
939 mutex_lock(&d->i2c_mutex);
940 if ((st->i2c_talk_onoff == 1) && (st->stream_on & 1)) {
941 st->i2c_talk_onoff = 0;
942 lme2510_stream_restart(d);
943 }
944 mutex_unlock(&d->i2c_mutex);
945
946 return 0;
947}
948
949static struct m88rs2000_config m88rs2000_config = {
71dc98be 950 .demod_addr = 0x68,
4f3b7fed
MP
951 .set_ts_params = dm04_rs2000_set_ts_param,
952};
953
b858c331
IL
954static struct ts2020_config ts2020_config = {
955 .tuner_address = 0x60,
956 .clk_out_div = 7,
957};
958
d2f918bb
MP
959static int dm04_lme2510_set_voltage(struct dvb_frontend *fe,
960 fe_sec_voltage_t voltage)
961{
e41543a2
MP
962 struct dvb_usb_device *d = fe_to_d(fe);
963 struct lme2510_state *st = fe_to_priv(fe);
964 static u8 voltage_low[] = LME_VOLTAGE_L;
d2f918bb 965 static u8 voltage_high[] = LME_VOLTAGE_H;
d2f918bb
MP
966 static u8 rbuf[1];
967 int ret = 0, len = 3, rlen = 1;
968
e41543a2 969 mutex_lock(&d->i2c_mutex);
d2f918bb 970
ab599a6d
MP
971 switch (voltage) {
972 case SEC_VOLTAGE_18:
e41543a2 973 ret |= lme2510_usb_talk(d,
ab599a6d 974 voltage_high, len, rbuf, rlen);
d2f918bb
MP
975 break;
976
ab599a6d 977 case SEC_VOLTAGE_OFF:
ab599a6d
MP
978 case SEC_VOLTAGE_13:
979 default:
e41543a2 980 ret |= lme2510_usb_talk(d,
d2f918bb
MP
981 voltage_low, len, rbuf, rlen);
982 break;
f23e6616 983 }
d2f918bb 984
e41543a2
MP
985 mutex_unlock(&d->i2c_mutex);
986
987 if (st->tuner_config == TUNER_RS2000)
988 if (st->fe_set_voltage)
989 st->fe_set_voltage(fe, voltage);
990
d2f918bb 991
d2f918bb
MP
992 return (ret < 0) ? -ENODEV : 0;
993}
994
4f3b7fed
MP
995static int dm04_rs2000_read_signal_strength(struct dvb_frontend *fe,
996 u16 *strength)
997{
e41543a2
MP
998 struct lme2510_state *st = fe_to_priv(fe);
999
1000 *strength = (u16)((u32)st->signal_level * 0xffff / 0xff);
4f3b7fed 1001
4f3b7fed
MP
1002 return 0;
1003}
1004
1005static int dm04_rs2000_read_snr(struct dvb_frontend *fe, u16 *snr)
1006{
e41543a2
MP
1007 struct lme2510_state *st = fe_to_priv(fe);
1008
1009 *snr = (u16)((u32)st->signal_sn * 0xffff / 0x7f);
1010
1011 return 0;
1012}
1013
1014static int dm04_read_ber(struct dvb_frontend *fe, u32 *ber)
1015{
1016 *ber = 0;
1017
1018 return 0;
1019}
1020
1021static int dm04_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
1022{
1023 *ucblocks = 0;
4f3b7fed 1024
4f3b7fed
MP
1025 return 0;
1026}
1027
75aecc3a
MP
1028static int lme_name(struct dvb_usb_adapter *adap)
1029{
e41543a2
MP
1030 struct dvb_usb_device *d = adap_to_d(adap);
1031 struct lme2510_state *st = adap_to_priv(adap);
1032 const char *desc = d->name;
9d4540c9 1033 char *fe_name[] = {"", " LG TDQY-P001F", " SHARP:BS2F7HZ7395",
4f3b7fed 1034 " SHARP:BS2F7HZ0194", " RS2000"};
e41543a2 1035 char *name = adap->fe[0]->ops.info.name;
75aecc3a
MP
1036
1037 strlcpy(name, desc, 128);
1038 strlcat(name, fe_name[st->tuner_config], 128);
1039
1040 return 0;
1041}
1042
d2f918bb
MP
1043static int dm04_lme2510_frontend_attach(struct dvb_usb_adapter *adap)
1044{
e41543a2
MP
1045 struct dvb_usb_device *d = adap_to_d(adap);
1046 struct lme2510_state *st = d->priv;
75aecc3a 1047 int ret = 0;
d2f918bb
MP
1048
1049 st->i2c_talk_onoff = 1;
e41543a2 1050 switch (le16_to_cpu(d->udev->descriptor.idProduct)) {
4f3b7fed
MP
1051 case 0x1122:
1052 case 0x1120:
1053 st->i2c_gate = 4;
e41543a2
MP
1054 adap->fe[0] = dvb_attach(tda10086_attach,
1055 &tda10086_config, &d->i2c_adap);
1056 if (adap->fe[0]) {
4f3b7fed
MP
1057 info("TUN Found Frontend TDA10086");
1058 st->i2c_tuner_gate_w = 4;
1059 st->i2c_tuner_gate_r = 4;
71dc98be 1060 st->i2c_tuner_addr = 0x60;
4f3b7fed 1061 st->tuner_config = TUNER_LG;
e41543a2
MP
1062 if (st->dvb_usb_lme2510_firmware != TUNER_LG) {
1063 st->dvb_usb_lme2510_firmware = TUNER_LG;
1064 ret = lme_firmware_switch(d, 1) ? 0 : -ENODEV;
4f3b7fed
MP
1065 }
1066 break;
1067 }
d2f918bb 1068
4f3b7fed 1069 st->i2c_gate = 4;
e41543a2
MP
1070 adap->fe[0] = dvb_attach(stv0299_attach,
1071 &sharp_z0194_config, &d->i2c_adap);
1072 if (adap->fe[0]) {
4f3b7fed
MP
1073 info("FE Found Stv0299");
1074 st->i2c_tuner_gate_w = 4;
1075 st->i2c_tuner_gate_r = 5;
71dc98be 1076 st->i2c_tuner_addr = 0x60;
4f3b7fed 1077 st->tuner_config = TUNER_S0194;
e41543a2
MP
1078 if (st->dvb_usb_lme2510_firmware != TUNER_S0194) {
1079 st->dvb_usb_lme2510_firmware = TUNER_S0194;
1080 ret = lme_firmware_switch(d, 1) ? 0 : -ENODEV;
4f3b7fed
MP
1081 }
1082 break;
9d4540c9 1083 }
9d4540c9 1084
4f3b7fed 1085 st->i2c_gate = 5;
e41543a2
MP
1086 adap->fe[0] = dvb_attach(stv0288_attach, &lme_config,
1087 &d->i2c_adap);
4f3b7fed 1088
e41543a2 1089 if (adap->fe[0]) {
4f3b7fed
MP
1090 info("FE Found Stv0288");
1091 st->i2c_tuner_gate_w = 4;
1092 st->i2c_tuner_gate_r = 5;
71dc98be 1093 st->i2c_tuner_addr = 0x60;
4f3b7fed 1094 st->tuner_config = TUNER_S7395;
e41543a2
MP
1095 if (st->dvb_usb_lme2510_firmware != TUNER_S7395) {
1096 st->dvb_usb_lme2510_firmware = TUNER_S7395;
1097 ret = lme_firmware_switch(d, 1) ? 0 : -ENODEV;
4f3b7fed
MP
1098 }
1099 break;
9d4540c9 1100 }
4f3b7fed
MP
1101 case 0x22f0:
1102 st->i2c_gate = 5;
e41543a2
MP
1103 adap->fe[0] = dvb_attach(m88rs2000_attach,
1104 &m88rs2000_config, &d->i2c_adap);
f23e6616 1105
e41543a2 1106 if (adap->fe[0]) {
4f3b7fed 1107 info("FE Found M88RS2000");
b858c331
IL
1108 dvb_attach(ts2020_attach, adap->fe[0], &ts2020_config,
1109 &d->i2c_adap);
4f3b7fed
MP
1110 st->i2c_tuner_gate_w = 5;
1111 st->i2c_tuner_gate_r = 5;
71dc98be 1112 st->i2c_tuner_addr = 0x60;
4f3b7fed 1113 st->tuner_config = TUNER_RS2000;
e41543a2
MP
1114 st->fe_set_voltage =
1115 adap->fe[0]->ops.set_voltage;
1116
1117 adap->fe[0]->ops.read_signal_strength =
4f3b7fed 1118 dm04_rs2000_read_signal_strength;
e41543a2 1119 adap->fe[0]->ops.read_snr =
4f3b7fed 1120 dm04_rs2000_read_snr;
e41543a2
MP
1121 adap->fe[0]->ops.read_ber =
1122 dm04_read_ber;
1123 adap->fe[0]->ops.read_ucblocks =
1124 dm04_read_ucblocks;
d2f918bb 1125 }
4f3b7fed 1126 break;
75aecc3a
MP
1127 }
1128
e41543a2
MP
1129 if (adap->fe[0] == NULL) {
1130 info("DM04/QQBOX Not Powered up or not Supported");
1131 return -ENODEV;
4f3b7fed 1132 }
9d4540c9 1133
4f3b7fed 1134 if (ret) {
e41543a2
MP
1135 if (adap->fe[0]) {
1136 dvb_frontend_detach(adap->fe[0]);
1137 adap->fe[0] = NULL;
616a4b83 1138 }
e41543a2 1139 d->rc_map = NULL;
75aecc3a 1140 return -ENODEV;
d2f918bb
MP
1141 }
1142
e41543a2 1143 adap->fe[0]->ops.set_voltage = dm04_lme2510_set_voltage;
75aecc3a 1144 ret = lme_name(adap);
75aecc3a
MP
1145 return ret;
1146}
1147
1148static int dm04_lme2510_tuner(struct dvb_usb_adapter *adap)
1149{
e41543a2
MP
1150 struct dvb_usb_device *d = adap_to_d(adap);
1151 struct lme2510_state *st = adap_to_priv(adap);
4f3b7fed 1152 char *tun_msg[] = {"", "TDA8263", "IX2505V", "DVB_PLL_OPERA", "RS2000"};
75aecc3a
MP
1153 int ret = 0;
1154
1155 switch (st->tuner_config) {
1156 case TUNER_LG:
71dc98be 1157 if (dvb_attach(tda826x_attach, adap->fe[0], 0x60,
e41543a2 1158 &d->i2c_adap, 1))
75aecc3a
MP
1159 ret = st->tuner_config;
1160 break;
1161 case TUNER_S7395:
e41543a2
MP
1162 if (dvb_attach(ix2505v_attach , adap->fe[0], &lme_tuner,
1163 &d->i2c_adap))
75aecc3a
MP
1164 ret = st->tuner_config;
1165 break;
9d4540c9 1166 case TUNER_S0194:
71dc98be 1167 if (dvb_attach(dvb_pll_attach , adap->fe[0], 0x60,
e41543a2 1168 &d->i2c_adap, DVB_PLL_OPERA1))
9d4540c9
MP
1169 ret = st->tuner_config;
1170 break;
4f3b7fed
MP
1171 case TUNER_RS2000:
1172 ret = st->tuner_config;
1173 break;
75aecc3a
MP
1174 default:
1175 break;
1176 }
1177
1178 if (ret)
1179 info("TUN Found %s tuner", tun_msg[ret]);
1180 else {
0d616ca3 1181 info("TUN No tuner found --- resetting device");
e41543a2 1182 lme_coldreset(d);
75aecc3a
MP
1183 return -ENODEV;
1184 }
1185
616a4b83
MP
1186 /* Start the Interrupt*/
1187 ret = lme2510_int_read(adap);
1188 if (ret < 0) {
1189 info("INT Unable to start Interrupt Service");
1190 return -ENODEV;
1191 }
75aecc3a
MP
1192
1193 return ret;
d2f918bb
MP
1194}
1195
1196static int lme2510_powerup(struct dvb_usb_device *d, int onoff)
1197{
1198 struct lme2510_state *st = d->priv;
f23e6616
MP
1199 static u8 lnb_on[] = LNB_ON;
1200 static u8 lnb_off[] = LNB_OFF;
1201 static u8 rbuf[1];
4f3b7fed 1202 int ret = 0, len = 3, rlen = 1;
f23e6616 1203
4f3b7fed 1204 mutex_lock(&d->i2c_mutex);
f23e6616
MP
1205
1206 if (onoff)
6f030abf 1207 ret = lme2510_usb_talk(d, lnb_on, len, rbuf, rlen);
f23e6616 1208 else
6f030abf 1209 ret = lme2510_usb_talk(d, lnb_off, len, rbuf, rlen);
f23e6616 1210
d2f918bb 1211 st->i2c_talk_onoff = 1;
f23e6616
MP
1212
1213 mutex_unlock(&d->i2c_mutex);
1214
1215 return ret;
d2f918bb
MP
1216}
1217
e41543a2 1218static int lme2510_get_adapter_count(struct dvb_usb_device *d)
d2f918bb 1219{
e41543a2
MP
1220 return 1;
1221}
d2f918bb 1222
e41543a2
MP
1223static int lme2510_identify_state(struct dvb_usb_device *d, const char **name)
1224{
1225 struct lme2510_state *st = d->priv;
d2f918bb 1226
e41543a2 1227 usb_reset_configuration(d->udev);
d2f918bb 1228
e41543a2 1229 usb_set_interface(d->udev,
e42efde4 1230 d->props->bInterfaceNumber, 1);
d2f918bb 1231
e41543a2 1232 st->dvb_usb_lme2510_firmware = dvb_usb_lme2510_firmware;
ab599a6d 1233
e41543a2
MP
1234 if (lme2510_return_status(d) == 0x44) {
1235 *name = lme_firmware_switch(d, 0);
1236 return COLD;
d2f918bb
MP
1237 }
1238
e41543a2 1239 return 0;
d2f918bb
MP
1240}
1241
e41543a2
MP
1242static int lme2510_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
1243 struct usb_data_stream_properties *stream)
1244{
1245 struct dvb_usb_adapter *adap = fe_to_adap(fe);
06f950f4 1246 struct dvb_usb_device *d;
d2f918bb 1247
e41543a2
MP
1248 if (adap == NULL)
1249 return 0;
06f950f4
SS
1250
1251 d = adap_to_d(adap);
1252
e41543a2
MP
1253 /* Turn PID filter on the fly by module option */
1254 if (pid_filter == 2) {
235d89ec 1255 adap->pid_filtering = true;
e41543a2 1256 adap->max_feed_count = 15;
d2f918bb 1257 }
d2f918bb 1258
e41543a2
MP
1259 if (!(le16_to_cpu(d->udev->descriptor.idProduct)
1260 == 0x1122))
1261 stream->endpoint = 0x8;
d2f918bb 1262
e41543a2
MP
1263 return 0;
1264}
1265
1266static int lme2510_get_rc_config(struct dvb_usb_device *d,
1267 struct dvb_usb_rc *rc)
1268{
c003ab1b 1269 rc->allowed_protos = RC_BIT_NEC;
e41543a2
MP
1270 return 0;
1271}
d2f918bb 1272
a760d638 1273static void *lme2510_exit_int(struct dvb_usb_device *d)
d2f918bb
MP
1274{
1275 struct lme2510_state *st = d->priv;
ab599a6d
MP
1276 struct dvb_usb_adapter *adap = &d->adapter[0];
1277 void *buffer = NULL;
1278
1279 if (adap != NULL) {
e41543a2 1280 lme2510_kill_urb(&adap->stream);
ab599a6d
MP
1281 }
1282
616a4b83 1283 if (st->usb_buffer != NULL) {
ab599a6d 1284 st->i2c_talk_onoff = 1;
d2f918bb
MP
1285 st->signal_lock = 0;
1286 st->signal_level = 0;
1287 st->signal_sn = 0;
ab599a6d 1288 buffer = st->usb_buffer;
616a4b83
MP
1289 }
1290
1291 if (st->lme_urb != NULL) {
d2f918bb 1292 usb_kill_urb(st->lme_urb);
95c21c41 1293 usb_free_coherent(d->udev, 128, st->buffer,
d2f918bb 1294 st->lme_urb->transfer_dma);
25985edc 1295 info("Interrupt Service Stopped");
d2f918bb 1296 }
616a4b83 1297
ab599a6d 1298 return buffer;
d2f918bb
MP
1299}
1300
e41543a2 1301static void lme2510_exit(struct dvb_usb_device *d)
d2f918bb 1302{
ab599a6d
MP
1303 void *usb_buffer;
1304
d2f918bb 1305 if (d != NULL) {
ab599a6d 1306 usb_buffer = lme2510_exit_int(d);
72873e51 1307 kfree(usb_buffer);
d2f918bb 1308 }
d2f918bb
MP
1309}
1310
e41543a2
MP
1311static struct dvb_usb_device_properties lme2510_props = {
1312 .driver_name = KBUILD_MODNAME,
1313 .owner = THIS_MODULE,
1314 .bInterfaceNumber = 0,
1315 .adapter_nr = adapter_nr,
1316 .size_of_priv = sizeof(struct lme2510_state),
1317
1318 .download_firmware = lme2510_download_firmware,
1319
1320 .power_ctrl = lme2510_powerup,
1321 .identify_state = lme2510_identify_state,
1322 .i2c_algo = &lme2510_i2c_algo,
1323
1324 .frontend_attach = dm04_lme2510_frontend_attach,
1325 .tuner_attach = dm04_lme2510_tuner,
1326 .get_stream_config = lme2510_get_stream_config,
1327 .get_adapter_count = lme2510_get_adapter_count,
1328 .streaming_ctrl = lme2510_streaming_ctrl,
1329
1330 .get_rc_config = lme2510_get_rc_config,
1331
1332 .exit = lme2510_exit,
1333 .adapter = {
1334 {
1335 .caps = DVB_USB_ADAP_HAS_PID_FILTER|
1336 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1337 .pid_filter_count = 15,
1338 .pid_filter = lme2510_pid_filter,
1339 .pid_filter_ctrl = lme2510_pid_filter_ctrl,
1340 .stream =
1341 DVB_USB_STREAM_BULK(0x86, 10, 4096),
1342 },
1343 {
1344 }
1345 },
1346};
1347
1348static const struct usb_device_id lme2510_id_table[] = {
1349 { DVB_USB_DEVICE(0x3344, 0x1122, &lme2510_props,
1350 "DM04_LME2510_DVB-S", RC_MAP_LME2510) },
1351 { DVB_USB_DEVICE(0x3344, 0x1120, &lme2510_props,
1352 "DM04_LME2510C_DVB-S", RC_MAP_LME2510) },
1353 { DVB_USB_DEVICE(0x3344, 0x22f0, &lme2510_props,
1354 "DM04_LME2510C_DVB-S RS2000", RC_MAP_LME2510) },
1355 {} /* Terminating entry */
1356};
1357
1358MODULE_DEVICE_TABLE(usb, lme2510_id_table);
1359
d2f918bb 1360static struct usb_driver lme2510_driver = {
e41543a2
MP
1361 .name = KBUILD_MODNAME,
1362 .probe = dvb_usbv2_probe,
1363 .disconnect = dvb_usbv2_disconnect,
1364 .id_table = lme2510_id_table,
1365 .no_dynamic_id = 1,
1366 .soft_unbind = 1,
d2f918bb
MP
1367};
1368
ecb3b2b3 1369module_usb_driver(lme2510_driver);
d2f918bb
MP
1370
1371MODULE_AUTHOR("Malcolm Priestley <tvboxspy@gmail.com>");
75aecc3a 1372MODULE_DESCRIPTION("LME2510(C) DVB-S USB2.0");
e41543a2 1373MODULE_VERSION("2.06");
d2f918bb 1374MODULE_LICENSE("GPL");
e41543a2
MP
1375MODULE_FIRMWARE(LME2510_C_S7395);
1376MODULE_FIRMWARE(LME2510_C_LG);
1377MODULE_FIRMWARE(LME2510_C_S0194);
1378MODULE_FIRMWARE(LME2510_C_RS2000);
1379MODULE_FIRMWARE(LME2510_LG);
1380MODULE_FIRMWARE(LME2510_S0194);
1381
This page took 0.382475 seconds and 5 git commands to generate.