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