[media] dvb_usb_lmedm04: don't crash if firmware is not loaded
[linux-2.6.git] / drivers / media / dvb / dvb-usb / lmedm04.c
blob26ba5bc92c615a068bcea4e42328d8b3629d1168
1 /* DVB USB compliant linux driver for
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
9 * MVB7395 (LME2510C+SHARP:BS2F7HZ7395)
10 * SHARP:BS2F7HZ7395 = (STV0288+Sharp IX2505V)
12 * MV001F (LME2510+LGTDQY-P001F)
13 * LG TDQY - P001F =(TDA8263 + TDA10086H)
15 * MVB0001F (LME2510C+LGTDQT-P001F)
17 * MV0194 (LME2510+SHARP:BS2F7HZ0194)
18 * SHARP:BS2F7HZ0194 = (STV0299+IX2410)
20 * MVB0194 (LME2510C+SHARP0194)
22 * For firmware see Documentation/dvb/lmedm04.txt
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
35 * VID = 3344 PID LME2510=1122 LME2510C=1120
37 * Copyright (C) 2010 Malcolm Priestley (tvboxspy@gmail.com)
38 * LME2510(C)(C) Leaguerme (Shenzhen) MicroElectronics Co., Ltd.
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.
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.
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.
54 * see Documentation/dvb/README.dvb-usb for more information
56 * Known Issues :
57 * LME2510: Non Intel USB chipsets fail to maintain High Speed on
58 * Boot or Hot Plug.
60 * QQbox suffers from noise on LNB voltage.
62 * LME2510: SHARP:BS2F7HZ0194(MV0194) cannot cold reset and share system
63 * with other tuners. After a cold reset streaming will not start.
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>
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 #include "m88rs2000.h"
84 /* debug */
85 static int dvb_usb_lme2510_debug;
86 #define l_dprintk(var, level, args...) do { \
87 if ((var >= level)) \
88 printk(KERN_DEBUG DVB_USB_LOG_PREFIX ": " args); \
89 } while (0)
91 #define deb_info(level, args...) l_dprintk(dvb_usb_lme2510_debug, level, args)
92 #define debug_data_snipet(level, name, p) \
93 deb_info(level, name" (%02x%02x%02x%02x%02x%02x%02x%02x)", \
94 *p, *(p+1), *(p+2), *(p+3), *(p+4), \
95 *(p+5), *(p+6), *(p+7));
98 module_param_named(debug, dvb_usb_lme2510_debug, int, 0644);
99 MODULE_PARM_DESC(debug, "set debugging level (1=info (or-able))."
100 DVB_USB_DEBUG_STATUS);
102 static int dvb_usb_lme2510_firmware;
103 module_param_named(firmware, dvb_usb_lme2510_firmware, int, 0644);
104 MODULE_PARM_DESC(firmware, "set default firmware 0=Sharp7395 1=LG");
106 static int pid_filter;
107 module_param_named(pid, pid_filter, int, 0644);
108 MODULE_PARM_DESC(pid, "set default 0=default 1=off 2=on");
111 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
113 #define TUNER_DEFAULT 0x0
114 #define TUNER_LG 0x1
115 #define TUNER_S7395 0x2
116 #define TUNER_S0194 0x3
117 #define TUNER_RS2000 0x4
119 struct lme2510_state {
120 u8 id;
121 u8 tuner_config;
122 u8 signal_lock;
123 u8 signal_level;
124 u8 signal_sn;
125 u8 time_key;
126 u8 last_key;
127 u8 key_timeout;
128 u8 i2c_talk_onoff;
129 u8 i2c_gate;
130 u8 i2c_tuner_gate_w;
131 u8 i2c_tuner_gate_r;
132 u8 i2c_tuner_addr;
133 u8 stream_on;
134 u8 pid_size;
135 u8 pid_off;
136 void *buffer;
137 struct urb *lme_urb;
138 void *usb_buffer;
142 static int lme2510_bulk_write(struct usb_device *dev,
143 u8 *snd, int len, u8 pipe)
145 int ret, actual_l;
147 ret = usb_bulk_msg(dev, usb_sndbulkpipe(dev, pipe),
148 snd, len , &actual_l, 100);
149 return ret;
152 static int lme2510_bulk_read(struct usb_device *dev,
153 u8 *rev, int len, u8 pipe)
155 int ret, actual_l;
157 ret = usb_bulk_msg(dev, usb_rcvbulkpipe(dev, pipe),
158 rev, len , &actual_l, 200);
159 return ret;
162 static int lme2510_usb_talk(struct dvb_usb_device *d,
163 u8 *wbuf, int wlen, u8 *rbuf, int rlen)
165 struct lme2510_state *st = d->priv;
166 u8 *buff;
167 int ret = 0;
169 if (st->usb_buffer == NULL) {
170 st->usb_buffer = kmalloc(64, GFP_KERNEL);
171 if (st->usb_buffer == NULL) {
172 info("MEM Error no memory");
173 return -ENOMEM;
176 buff = st->usb_buffer;
178 ret = mutex_lock_interruptible(&d->usb_mutex);
180 if (ret < 0)
181 return -EAGAIN;
183 /* the read/write capped at 64 */
184 memcpy(buff, wbuf, (wlen < 64) ? wlen : 64);
186 ret |= lme2510_bulk_write(d->udev, buff, wlen , 0x01);
188 ret |= lme2510_bulk_read(d->udev, buff, (rlen < 64) ?
189 rlen : 64 , 0x01);
191 if (rlen > 0)
192 memcpy(rbuf, buff, rlen);
194 mutex_unlock(&d->usb_mutex);
196 return (ret < 0) ? -ENODEV : 0;
199 static int lme2510_stream_restart(struct dvb_usb_device *d)
201 struct lme2510_state *st = d->priv;
202 u8 all_pids[] = LME_ALL_PIDS;
203 u8 stream_on[] = LME_ST_ON_W;
204 int ret;
205 u8 rbuff[1];
206 if (st->pid_off)
207 ret = lme2510_usb_talk(d, all_pids, sizeof(all_pids),
208 rbuff, sizeof(rbuff));
209 /*Restart Stream Command*/
210 ret = lme2510_usb_talk(d, stream_on, sizeof(stream_on),
211 rbuff, sizeof(rbuff));
212 return ret;
215 static int lme2510_enable_pid(struct dvb_usb_device *d, u8 index, u16 pid_out)
217 struct lme2510_state *st = d->priv;
218 static u8 pid_buff[] = LME_ZERO_PID;
219 static u8 rbuf[1];
220 u8 pid_no = index * 2;
221 u8 pid_len = pid_no + 2;
222 int ret = 0;
223 deb_info(1, "PID Setting Pid %04x", pid_out);
225 if (st->pid_size == 0)
226 ret |= lme2510_stream_restart(d);
228 pid_buff[2] = pid_no;
229 pid_buff[3] = (u8)pid_out & 0xff;
230 pid_buff[4] = pid_no + 1;
231 pid_buff[5] = (u8)(pid_out >> 8);
233 if (pid_len > st->pid_size)
234 st->pid_size = pid_len;
235 pid_buff[7] = 0x80 + st->pid_size;
237 ret |= lme2510_usb_talk(d, pid_buff ,
238 sizeof(pid_buff) , rbuf, sizeof(rbuf));
240 if (st->stream_on)
241 ret |= lme2510_stream_restart(d);
243 return ret;
246 static void lme2510_int_response(struct urb *lme_urb)
248 struct dvb_usb_adapter *adap = lme_urb->context;
249 struct lme2510_state *st = adap->dev->priv;
250 static u8 *ibuf, *rbuf;
251 int i = 0, offset;
252 u32 key;
254 switch (lme_urb->status) {
255 case 0:
256 case -ETIMEDOUT:
257 break;
258 case -ECONNRESET:
259 case -ENOENT:
260 case -ESHUTDOWN:
261 return;
262 default:
263 info("Error %x", lme_urb->status);
264 break;
267 rbuf = (u8 *) lme_urb->transfer_buffer;
269 offset = ((lme_urb->actual_length/8) > 4)
270 ? 4 : (lme_urb->actual_length/8) ;
272 for (i = 0; i < offset; ++i) {
273 ibuf = (u8 *)&rbuf[i*8];
274 deb_info(5, "INT O/S C =%02x C/O=%02x Type =%02x%02x",
275 offset, i, ibuf[0], ibuf[1]);
277 switch (ibuf[0]) {
278 case 0xaa:
279 debug_data_snipet(1, "INT Remote data snipet", ibuf);
280 if ((ibuf[4] + ibuf[5]) == 0xff) {
281 key = ibuf[5];
282 key += (ibuf[3] > 0)
283 ? (ibuf[3] ^ 0xff) << 8 : 0;
284 key += (ibuf[2] ^ 0xff) << 16;
285 deb_info(1, "INT Key =%08x", key);
286 if (adap->dev->rc_dev != NULL)
287 rc_keydown(adap->dev->rc_dev, key, 0);
289 break;
290 case 0xbb:
291 switch (st->tuner_config) {
292 case TUNER_LG:
293 if (ibuf[2] > 0)
294 st->signal_lock = ibuf[2];
295 st->signal_level = ibuf[4];
296 st->signal_sn = ibuf[3];
297 st->time_key = ibuf[7];
298 break;
299 case TUNER_S7395:
300 case TUNER_S0194:
301 /* Tweak for earlier firmware*/
302 if (ibuf[1] == 0x03) {
303 if (ibuf[2] > 1)
304 st->signal_lock = ibuf[2];
305 st->signal_level = ibuf[3];
306 st->signal_sn = ibuf[4];
307 } else {
308 st->signal_level = ibuf[4];
309 st->signal_sn = ibuf[5];
310 st->signal_lock =
311 (st->signal_lock & 0xf7) +
312 ((ibuf[2] & 0x01) << 0x03);
314 break;
315 case TUNER_RS2000:
316 if (ibuf[2] > 0)
317 st->signal_lock = 0xff;
318 else
319 st->signal_lock = 0xf0;
320 st->signal_level = ibuf[4];
321 st->signal_sn = ibuf[5];
322 st->time_key = ibuf[7];
323 default:
324 break;
326 debug_data_snipet(5, "INT Remote data snipet in", ibuf);
327 break;
328 case 0xcc:
329 debug_data_snipet(1, "INT Control data snipet", ibuf);
330 break;
331 default:
332 debug_data_snipet(1, "INT Unknown data snipet", ibuf);
333 break;
336 usb_submit_urb(lme_urb, GFP_ATOMIC);
339 static int lme2510_int_read(struct dvb_usb_adapter *adap)
341 struct lme2510_state *lme_int = adap->dev->priv;
343 lme_int->lme_urb = usb_alloc_urb(0, GFP_ATOMIC);
345 if (lme_int->lme_urb == NULL)
346 return -ENOMEM;
348 lme_int->buffer = usb_alloc_coherent(adap->dev->udev, 128, GFP_ATOMIC,
349 &lme_int->lme_urb->transfer_dma);
351 if (lme_int->buffer == NULL)
352 return -ENOMEM;
354 usb_fill_int_urb(lme_int->lme_urb,
355 adap->dev->udev,
356 usb_rcvintpipe(adap->dev->udev, 0xa),
357 lme_int->buffer,
358 128,
359 lme2510_int_response,
360 adap,
363 lme_int->lme_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
365 usb_submit_urb(lme_int->lme_urb, GFP_ATOMIC);
366 info("INT Interrupt Service Started");
368 return 0;
371 static int lme2510_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
373 struct lme2510_state *st = adap->dev->priv;
374 static u8 clear_pid_reg[] = LME_ALL_PIDS;
375 static u8 rbuf[1];
376 int ret = 0;
378 deb_info(1, "PID Clearing Filter");
380 mutex_lock(&adap->dev->i2c_mutex);
382 if (!onoff) {
383 ret |= lme2510_usb_talk(adap->dev, clear_pid_reg,
384 sizeof(clear_pid_reg), rbuf, sizeof(rbuf));
385 st->pid_off = true;
386 } else
387 st->pid_off = false;
389 st->pid_size = 0;
391 mutex_unlock(&adap->dev->i2c_mutex);
393 return 0;
396 static int lme2510_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
397 int onoff)
399 int ret = 0;
401 deb_info(3, "%s PID=%04x Index=%04x onoff=%02x", __func__,
402 pid, index, onoff);
404 if (onoff) {
405 mutex_lock(&adap->dev->i2c_mutex);
406 ret |= lme2510_enable_pid(adap->dev, index, pid);
407 mutex_unlock(&adap->dev->i2c_mutex);
411 return ret;
415 static int lme2510_return_status(struct usb_device *dev)
417 int ret = 0;
418 u8 *data;
420 data = kzalloc(10, GFP_KERNEL);
421 if (!data)
422 return -ENOMEM;
424 ret |= usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
425 0x06, 0x80, 0x0302, 0x00, data, 0x0006, 200);
426 info("Firmware Status: %x (%x)", ret , data[2]);
428 ret = (ret < 0) ? -ENODEV : data[2];
429 kfree(data);
430 return ret;
433 static int lme2510_msg(struct dvb_usb_device *d,
434 u8 *wbuf, int wlen, u8 *rbuf, int rlen)
436 int ret = 0;
437 struct lme2510_state *st = d->priv;
439 if (st->i2c_talk_onoff == 1) {
441 ret = lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
443 switch (st->tuner_config) {
444 case TUNER_LG:
445 if (wbuf[2] == 0x1c) {
446 if (wbuf[3] == 0x0e) {
447 st->signal_lock = rbuf[1];
448 if ((st->stream_on & 1) &&
449 (st->signal_lock & 0x10)) {
450 lme2510_stream_restart(d);
451 st->i2c_talk_onoff = 0;
453 msleep(80);
456 break;
457 case TUNER_S7395:
458 if (wbuf[2] == 0xd0) {
459 if (wbuf[3] == 0x24) {
460 st->signal_lock = rbuf[1];
461 if ((st->stream_on & 1) &&
462 (st->signal_lock & 0x8)) {
463 lme2510_stream_restart(d);
464 st->i2c_talk_onoff = 0;
468 break;
469 case TUNER_S0194:
470 if (wbuf[2] == 0xd0) {
471 if (wbuf[3] == 0x1b) {
472 st->signal_lock = rbuf[1];
473 if ((st->stream_on & 1) &&
474 (st->signal_lock & 0x8)) {
475 lme2510_stream_restart(d);
476 st->i2c_talk_onoff = 0;
480 break;
481 case TUNER_RS2000:
482 default:
483 break;
485 } else {
486 /* TODO rewrite this section */
487 switch (st->tuner_config) {
488 case TUNER_LG:
489 switch (wbuf[3]) {
490 case 0x0e:
491 rbuf[0] = 0x55;
492 rbuf[1] = st->signal_lock;
493 break;
494 case 0x43:
495 rbuf[0] = 0x55;
496 rbuf[1] = st->signal_level;
497 break;
498 case 0x1c:
499 rbuf[0] = 0x55;
500 rbuf[1] = st->signal_sn;
501 break;
502 case 0x15:
503 case 0x16:
504 case 0x17:
505 case 0x18:
506 rbuf[0] = 0x55;
507 rbuf[1] = 0x00;
508 break;
509 default:
510 lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
511 st->i2c_talk_onoff = 1;
512 break;
514 break;
515 case TUNER_S7395:
516 switch (wbuf[3]) {
517 case 0x10:
518 rbuf[0] = 0x55;
519 rbuf[1] = (st->signal_level & 0x80)
520 ? 0 : (st->signal_level * 2);
521 break;
522 case 0x2d:
523 rbuf[0] = 0x55;
524 rbuf[1] = st->signal_sn;
525 break;
526 case 0x24:
527 rbuf[0] = 0x55;
528 rbuf[1] = st->signal_lock;
529 break;
530 case 0x2e:
531 case 0x26:
532 case 0x27:
533 rbuf[0] = 0x55;
534 rbuf[1] = 0x00;
535 break;
536 default:
537 lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
538 st->i2c_talk_onoff = 1;
539 break;
541 break;
542 case TUNER_S0194:
543 switch (wbuf[3]) {
544 case 0x18:
545 rbuf[0] = 0x55;
546 rbuf[1] = (st->signal_level & 0x80)
547 ? 0 : (st->signal_level * 2);
548 break;
549 case 0x24:
550 rbuf[0] = 0x55;
551 rbuf[1] = st->signal_sn;
552 break;
553 case 0x1b:
554 rbuf[0] = 0x55;
555 rbuf[1] = st->signal_lock;
556 break;
557 case 0x19:
558 case 0x25:
559 case 0x1e:
560 case 0x1d:
561 rbuf[0] = 0x55;
562 rbuf[1] = 0x00;
563 break;
564 default:
565 lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
566 st->i2c_talk_onoff = 1;
567 break;
569 break;
570 case TUNER_RS2000:
571 switch (wbuf[3]) {
572 case 0x8c:
573 rbuf[0] = 0x55;
574 rbuf[1] = 0xff;
575 if (st->last_key == st->time_key) {
576 st->key_timeout++;
577 if (st->key_timeout > 5)
578 rbuf[1] = 0;
579 } else
580 st->key_timeout = 0;
581 st->last_key = st->time_key;
582 break;
583 default:
584 lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
585 st->i2c_talk_onoff = 1;
586 break;
588 default:
589 break;
592 deb_info(4, "I2C From Interrupt Message out(%02x) in(%02x)",
593 wbuf[3], rbuf[1]);
597 return ret;
601 static int lme2510_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
602 int num)
604 struct dvb_usb_device *d = i2c_get_adapdata(adap);
605 struct lme2510_state *st = d->priv;
606 static u8 obuf[64], ibuf[64];
607 int i, read, read_o;
608 u16 len;
609 u8 gate = st->i2c_gate;
611 mutex_lock(&d->i2c_mutex);
613 if (gate == 0)
614 gate = 5;
616 if (num > 2)
617 warn("more than 2 i2c messages"
618 "at a time is not handled yet. TODO.");
620 for (i = 0; i < num; i++) {
621 read_o = 1 & (msg[i].flags & I2C_M_RD);
622 read = i+1 < num && (msg[i+1].flags & I2C_M_RD);
623 read |= read_o;
624 gate = (msg[i].addr == st->i2c_tuner_addr)
625 ? (read) ? st->i2c_tuner_gate_r
626 : st->i2c_tuner_gate_w
627 : st->i2c_gate;
628 obuf[0] = gate | (read << 7);
630 if (gate == 5)
631 obuf[1] = (read) ? 2 : msg[i].len + 1;
632 else
633 obuf[1] = msg[i].len + read + 1;
635 obuf[2] = msg[i].addr;
636 if (read) {
637 if (read_o)
638 len = 3;
639 else {
640 memcpy(&obuf[3], msg[i].buf, msg[i].len);
641 obuf[msg[i].len+3] = msg[i+1].len;
642 len = msg[i].len+4;
644 } else {
645 memcpy(&obuf[3], msg[i].buf, msg[i].len);
646 len = msg[i].len+3;
649 if (lme2510_msg(d, obuf, len, ibuf, 64) < 0) {
650 deb_info(1, "i2c transfer failed.");
651 mutex_unlock(&d->i2c_mutex);
652 return -EAGAIN;
655 if (read) {
656 if (read_o)
657 memcpy(msg[i].buf, &ibuf[1], msg[i].len);
658 else {
659 memcpy(msg[i+1].buf, &ibuf[1], msg[i+1].len);
660 i++;
665 mutex_unlock(&d->i2c_mutex);
666 return i;
669 static u32 lme2510_i2c_func(struct i2c_adapter *adapter)
671 return I2C_FUNC_I2C;
674 static struct i2c_algorithm lme2510_i2c_algo = {
675 .master_xfer = lme2510_i2c_xfer,
676 .functionality = lme2510_i2c_func,
679 /* Callbacks for DVB USB */
680 static int lme2510_identify_state(struct usb_device *udev,
681 struct dvb_usb_device_properties *props,
682 struct dvb_usb_device_description **desc,
683 int *cold)
685 if (pid_filter != 2)
686 props->adapter[0].fe[0].caps &=
687 ~DVB_USB_ADAP_NEED_PID_FILTERING;
688 *cold = 0;
689 return 0;
692 static int lme2510_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
694 struct lme2510_state *st = adap->dev->priv;
695 static u8 clear_reg_3[] = LME_ALL_PIDS;
696 static u8 rbuf[1];
697 int ret = 0, rlen = sizeof(rbuf);
699 deb_info(1, "STM (%02x)", onoff);
701 /* Streaming is started by FE_HAS_LOCK */
702 if (onoff == 1)
703 st->stream_on = 1;
704 else {
705 deb_info(1, "STM Steam Off");
706 /* mutex is here only to avoid collision with I2C */
707 mutex_lock(&adap->dev->i2c_mutex);
709 ret = lme2510_usb_talk(adap->dev, clear_reg_3,
710 sizeof(clear_reg_3), rbuf, rlen);
711 st->stream_on = 0;
712 st->i2c_talk_onoff = 1;
714 mutex_unlock(&adap->dev->i2c_mutex);
717 return (ret < 0) ? -ENODEV : 0;
720 static u8 check_sum(u8 *p, u8 len)
722 u8 sum = 0;
723 while (len--)
724 sum += *p++;
725 return sum;
728 static int lme2510_download_firmware(struct usb_device *dev,
729 const struct firmware *fw)
731 int ret = 0;
732 u8 *data;
733 u16 j, wlen, len_in, start, end;
734 u8 packet_size, dlen, i;
735 u8 *fw_data;
737 packet_size = 0x31;
738 len_in = 1;
740 data = kzalloc(512, GFP_KERNEL);
741 if (!data) {
742 info("FRM Could not start Firmware Download (Buffer allocation failed)");
743 return -ENOMEM;
746 info("FRM Starting Firmware Download");
748 for (i = 1; i < 3; i++) {
749 start = (i == 1) ? 0 : 512;
750 end = (i == 1) ? 512 : fw->size;
751 for (j = start; j < end; j += (packet_size+1)) {
752 fw_data = (u8 *)(fw->data + j);
753 if ((end - j) > packet_size) {
754 data[0] = i;
755 dlen = packet_size;
756 } else {
757 data[0] = i | 0x80;
758 dlen = (u8)(end - j)-1;
760 data[1] = dlen;
761 memcpy(&data[2], fw_data, dlen+1);
762 wlen = (u8) dlen + 4;
763 data[wlen-1] = check_sum(fw_data, dlen+1);
764 deb_info(1, "Data S=%02x:E=%02x CS= %02x", data[3],
765 data[dlen+2], data[dlen+3]);
766 ret |= lme2510_bulk_write(dev, data, wlen, 1);
767 ret |= lme2510_bulk_read(dev, data, len_in , 1);
768 ret |= (data[0] == 0x88) ? 0 : -1;
772 usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
773 0x06, 0x80, 0x0200, 0x00, data, 0x0109, 1000);
776 data[0] = 0x8a;
777 len_in = 1;
778 msleep(2000);
779 ret |= lme2510_bulk_write(dev, data , len_in, 1); /*Resetting*/
780 ret |= lme2510_bulk_read(dev, data, len_in, 1);
781 msleep(400);
783 if (ret < 0)
784 info("FRM Firmware Download Failed (%04x)" , ret);
785 else
786 info("FRM Firmware Download Completed - Resetting Device");
788 kfree(data);
789 return (ret < 0) ? -ENODEV : 0;
792 static void lme_coldreset(struct usb_device *dev)
794 int ret = 0, len_in;
795 u8 data[512] = {0};
797 data[0] = 0x0a;
798 len_in = 1;
799 info("FRM Firmware Cold Reset");
800 ret |= lme2510_bulk_write(dev, data , len_in, 1); /*Cold Resetting*/
801 ret |= lme2510_bulk_read(dev, data, len_in, 1);
803 return;
806 static int lme_firmware_switch(struct usb_device *udev, int cold)
808 const struct firmware *fw = NULL;
809 const char fw_c_s7395[] = "dvb-usb-lme2510c-s7395.fw";
810 const char fw_c_lg[] = "dvb-usb-lme2510c-lg.fw";
811 const char fw_c_s0194[] = "dvb-usb-lme2510c-s0194.fw";
812 const char fw_c_rs2000[] = "dvb-usb-lme2510c-rs2000.fw";
813 const char fw_lg[] = "dvb-usb-lme2510-lg.fw";
814 const char fw_s0194[] = "dvb-usb-lme2510-s0194.fw";
815 const char *fw_lme;
816 int ret = 0, cold_fw;
818 cold = (cold > 0) ? (cold & 1) : 0;
820 cold_fw = !cold;
822 switch (le16_to_cpu(udev->descriptor.idProduct)) {
823 case 0x1122:
824 switch (dvb_usb_lme2510_firmware) {
825 default:
826 dvb_usb_lme2510_firmware = TUNER_S0194;
827 case TUNER_S0194:
828 fw_lme = fw_s0194;
829 ret = request_firmware(&fw, fw_lme, &udev->dev);
830 if (ret == 0) {
831 cold = 0;
832 break;
834 dvb_usb_lme2510_firmware = TUNER_LG;
835 case TUNER_LG:
836 fw_lme = fw_lg;
837 ret = request_firmware(&fw, fw_lme, &udev->dev);
838 if (ret == 0)
839 break;
840 info("FRM No Firmware Found - please install");
841 dvb_usb_lme2510_firmware = TUNER_DEFAULT;
842 cold = 0;
843 cold_fw = 0;
844 break;
846 break;
847 case 0x1120:
848 switch (dvb_usb_lme2510_firmware) {
849 default:
850 dvb_usb_lme2510_firmware = TUNER_S7395;
851 case TUNER_S7395:
852 fw_lme = fw_c_s7395;
853 ret = request_firmware(&fw, fw_lme, &udev->dev);
854 if (ret == 0) {
855 cold = 0;
856 break;
858 dvb_usb_lme2510_firmware = TUNER_LG;
859 case TUNER_LG:
860 fw_lme = fw_c_lg;
861 ret = request_firmware(&fw, fw_lme, &udev->dev);
862 if (ret == 0)
863 break;
864 dvb_usb_lme2510_firmware = TUNER_S0194;
865 case TUNER_S0194:
866 fw_lme = fw_c_s0194;
867 ret = request_firmware(&fw, fw_lme, &udev->dev);
868 if (ret == 0)
869 break;
870 info("FRM No Firmware Found - please install");
871 dvb_usb_lme2510_firmware = TUNER_DEFAULT;
872 cold = 0;
873 cold_fw = 0;
874 break;
876 break;
877 case 0x22f0:
878 fw_lme = fw_c_rs2000;
879 ret = request_firmware(&fw, fw_lme, &udev->dev);
880 dvb_usb_lme2510_firmware = TUNER_RS2000;
881 if (ret == 0)
882 break;
883 info("FRM No Firmware Found - please install");
884 cold_fw = 0;
885 break;
886 default:
887 fw_lme = fw_c_s7395;
891 if (cold_fw) {
892 info("FRM Loading %s file", fw_lme);
893 ret = lme2510_download_firmware(udev, fw);
896 release_firmware(fw);
898 if (cold) {
899 info("FRM Changing to %s firmware", fw_lme);
900 lme_coldreset(udev);
901 return -ENODEV;
904 return ret;
907 static int lme2510_kill_urb(struct usb_data_stream *stream)
909 int i;
911 for (i = 0; i < stream->urbs_submitted; i++) {
912 deb_info(3, "killing URB no. %d.", i);
913 /* stop the URB */
914 usb_kill_urb(stream->urb_list[i]);
916 stream->urbs_submitted = 0;
918 return 0;
921 static struct tda10086_config tda10086_config = {
922 .demod_address = 0x1c,
923 .invert = 0,
924 .diseqc_tone = 1,
925 .xtal_freq = TDA10086_XTAL_16M,
928 static struct stv0288_config lme_config = {
929 .demod_address = 0xd0,
930 .min_delay_ms = 15,
931 .inittab = s7395_inittab,
934 static struct ix2505v_config lme_tuner = {
935 .tuner_address = 0xc0,
936 .min_delay_ms = 100,
937 .tuner_gain = 0x0,
938 .tuner_chargepump = 0x3,
941 static struct stv0299_config sharp_z0194_config = {
942 .demod_address = 0xd0,
943 .inittab = sharp_z0194a_inittab,
944 .mclk = 88000000UL,
945 .invert = 0,
946 .skip_reinit = 0,
947 .lock_output = STV0299_LOCKOUTPUT_1,
948 .volt13_op0_op1 = STV0299_VOLT13_OP1,
949 .min_delay_ms = 100,
950 .set_symbol_rate = sharp_z0194a_set_symbol_rate,
953 static int dm04_rs2000_set_ts_param(struct dvb_frontend *fe,
954 int caller)
956 struct dvb_usb_adapter *adap = fe->dvb->priv;
957 struct dvb_usb_device *d = adap->dev;
958 struct lme2510_state *st = d->priv;
960 mutex_lock(&d->i2c_mutex);
961 if ((st->i2c_talk_onoff == 1) && (st->stream_on & 1)) {
962 st->i2c_talk_onoff = 0;
963 lme2510_stream_restart(d);
965 mutex_unlock(&d->i2c_mutex);
967 return 0;
970 static struct m88rs2000_config m88rs2000_config = {
971 .demod_addr = 0xd0,
972 .tuner_addr = 0xc0,
973 .set_ts_params = dm04_rs2000_set_ts_param,
976 static int dm04_lme2510_set_voltage(struct dvb_frontend *fe,
977 fe_sec_voltage_t voltage)
979 struct dvb_usb_adapter *adap = fe->dvb->priv;
980 static u8 voltage_low[] = LME_VOLTAGE_L;
981 static u8 voltage_high[] = LME_VOLTAGE_H;
982 static u8 rbuf[1];
983 int ret = 0, len = 3, rlen = 1;
985 mutex_lock(&adap->dev->i2c_mutex);
987 switch (voltage) {
988 case SEC_VOLTAGE_18:
989 ret |= lme2510_usb_talk(adap->dev,
990 voltage_high, len, rbuf, rlen);
991 break;
993 case SEC_VOLTAGE_OFF:
994 case SEC_VOLTAGE_13:
995 default:
996 ret |= lme2510_usb_talk(adap->dev,
997 voltage_low, len, rbuf, rlen);
998 break;
1001 mutex_unlock(&adap->dev->i2c_mutex);
1003 return (ret < 0) ? -ENODEV : 0;
1006 static int dm04_rs2000_read_signal_strength(struct dvb_frontend *fe,
1007 u16 *strength)
1009 struct dvb_usb_adapter *adap = fe->dvb->priv;
1010 struct lme2510_state *st = adap->dev->priv;
1012 *strength = (u16)((u32)st->signal_level * 0xffff / 0x7f);
1013 return 0;
1016 static int dm04_rs2000_read_snr(struct dvb_frontend *fe, u16 *snr)
1018 struct dvb_usb_adapter *adap = fe->dvb->priv;
1019 struct lme2510_state *st = adap->dev->priv;
1021 *snr = (u16)((u32)st->signal_sn * 0xffff / 0xff);
1022 return 0;
1025 static int lme_name(struct dvb_usb_adapter *adap)
1027 struct lme2510_state *st = adap->dev->priv;
1028 const char *desc = adap->dev->desc->name;
1029 char *fe_name[] = {"", " LG TDQY-P001F", " SHARP:BS2F7HZ7395",
1030 " SHARP:BS2F7HZ0194", " RS2000"};
1031 char *name = adap->fe_adap[0].fe->ops.info.name;
1033 strlcpy(name, desc, 128);
1034 strlcat(name, fe_name[st->tuner_config], 128);
1036 return 0;
1039 static int dm04_lme2510_frontend_attach(struct dvb_usb_adapter *adap)
1041 struct lme2510_state *st = adap->dev->priv;
1043 int ret = 0;
1045 st->i2c_talk_onoff = 1;
1046 switch (le16_to_cpu(adap->dev->udev->descriptor.idProduct)) {
1047 case 0x1122:
1048 case 0x1120:
1049 st->i2c_gate = 4;
1050 adap->fe_adap[0].fe = dvb_attach(tda10086_attach,
1051 &tda10086_config, &adap->dev->i2c_adap);
1052 if (adap->fe_adap[0].fe) {
1053 info("TUN Found Frontend TDA10086");
1054 st->i2c_tuner_gate_w = 4;
1055 st->i2c_tuner_gate_r = 4;
1056 st->i2c_tuner_addr = 0xc0;
1057 st->tuner_config = TUNER_LG;
1058 if (dvb_usb_lme2510_firmware != TUNER_LG) {
1059 dvb_usb_lme2510_firmware = TUNER_LG;
1060 ret = lme_firmware_switch(adap->dev->udev, 1);
1062 break;
1065 st->i2c_gate = 4;
1066 adap->fe_adap[0].fe = dvb_attach(stv0299_attach,
1067 &sharp_z0194_config, &adap->dev->i2c_adap);
1068 if (adap->fe_adap[0].fe) {
1069 info("FE Found Stv0299");
1070 st->i2c_tuner_gate_w = 4;
1071 st->i2c_tuner_gate_r = 5;
1072 st->i2c_tuner_addr = 0xc0;
1073 st->tuner_config = TUNER_S0194;
1074 if (dvb_usb_lme2510_firmware != TUNER_S0194) {
1075 dvb_usb_lme2510_firmware = TUNER_S0194;
1076 ret = lme_firmware_switch(adap->dev->udev, 1);
1078 break;
1081 st->i2c_gate = 5;
1082 adap->fe_adap[0].fe = dvb_attach(stv0288_attach, &lme_config,
1083 &adap->dev->i2c_adap);
1085 if (adap->fe_adap[0].fe) {
1086 info("FE Found Stv0288");
1087 st->i2c_tuner_gate_w = 4;
1088 st->i2c_tuner_gate_r = 5;
1089 st->i2c_tuner_addr = 0xc0;
1090 st->tuner_config = TUNER_S7395;
1091 if (dvb_usb_lme2510_firmware != TUNER_S7395) {
1092 dvb_usb_lme2510_firmware = TUNER_S7395;
1093 ret = lme_firmware_switch(adap->dev->udev, 1);
1095 break;
1097 case 0x22f0:
1098 st->i2c_gate = 5;
1099 adap->fe_adap[0].fe = dvb_attach(m88rs2000_attach,
1100 &m88rs2000_config, &adap->dev->i2c_adap);
1102 if (adap->fe_adap[0].fe) {
1103 info("FE Found M88RS2000");
1104 st->i2c_tuner_gate_w = 5;
1105 st->i2c_tuner_gate_r = 5;
1106 st->i2c_tuner_addr = 0xc0;
1107 st->tuner_config = TUNER_RS2000;
1108 adap->fe_adap[0].fe->ops.read_signal_strength =
1109 dm04_rs2000_read_signal_strength;
1110 adap->fe_adap[0].fe->ops.read_snr =
1111 dm04_rs2000_read_snr;
1113 break;
1116 if (adap->fe_adap[0].fe == NULL) {
1117 info("DM04/QQBOX Not Powered up or not Supported");
1118 return -ENODEV;
1121 if (ret) {
1122 if (adap->fe_adap[0].fe) {
1123 dvb_frontend_detach(adap->fe_adap[0].fe);
1124 adap->fe_adap[0].fe = NULL;
1126 adap->dev->props.rc.core.rc_codes = NULL;
1127 return -ENODEV;
1130 adap->fe_adap[0].fe->ops.set_voltage = dm04_lme2510_set_voltage;
1131 ret = lme_name(adap);
1132 return ret;
1135 static int dm04_lme2510_tuner(struct dvb_usb_adapter *adap)
1137 struct lme2510_state *st = adap->dev->priv;
1138 char *tun_msg[] = {"", "TDA8263", "IX2505V", "DVB_PLL_OPERA", "RS2000"};
1139 int ret = 0;
1141 switch (st->tuner_config) {
1142 case TUNER_LG:
1143 if (dvb_attach(tda826x_attach, adap->fe_adap[0].fe, 0xc0,
1144 &adap->dev->i2c_adap, 1))
1145 ret = st->tuner_config;
1146 break;
1147 case TUNER_S7395:
1148 if (dvb_attach(ix2505v_attach , adap->fe_adap[0].fe, &lme_tuner,
1149 &adap->dev->i2c_adap))
1150 ret = st->tuner_config;
1151 break;
1152 case TUNER_S0194:
1153 if (dvb_attach(dvb_pll_attach , adap->fe_adap[0].fe, 0xc0,
1154 &adap->dev->i2c_adap, DVB_PLL_OPERA1))
1155 ret = st->tuner_config;
1156 break;
1157 case TUNER_RS2000:
1158 ret = st->tuner_config;
1159 break;
1160 default:
1161 break;
1164 if (ret)
1165 info("TUN Found %s tuner", tun_msg[ret]);
1166 else {
1167 info("TUN No tuner found --- resetting device");
1168 lme_coldreset(adap->dev->udev);
1169 return -ENODEV;
1172 /* Start the Interrupt*/
1173 ret = lme2510_int_read(adap);
1174 if (ret < 0) {
1175 info("INT Unable to start Interrupt Service");
1176 return -ENODEV;
1179 return ret;
1182 static int lme2510_powerup(struct dvb_usb_device *d, int onoff)
1184 struct lme2510_state *st = d->priv;
1185 static u8 lnb_on[] = LNB_ON;
1186 static u8 lnb_off[] = LNB_OFF;
1187 static u8 rbuf[1];
1188 int ret = 0, len = 3, rlen = 1;
1190 mutex_lock(&d->i2c_mutex);
1192 if (onoff)
1193 ret = lme2510_usb_talk(d, lnb_on, len, rbuf, rlen);
1194 else
1195 ret = lme2510_usb_talk(d, lnb_off, len, rbuf, rlen);
1197 st->i2c_talk_onoff = 1;
1199 mutex_unlock(&d->i2c_mutex);
1201 return ret;
1204 /* DVB USB Driver stuff */
1205 static struct dvb_usb_device_properties lme2510_properties;
1206 static struct dvb_usb_device_properties lme2510c_properties;
1208 static int lme2510_probe(struct usb_interface *intf,
1209 const struct usb_device_id *id)
1211 struct usb_device *udev = interface_to_usbdev(intf);
1213 usb_reset_configuration(udev);
1215 usb_set_interface(udev, intf->cur_altsetting->desc.bInterfaceNumber, 1);
1217 if (udev->speed != USB_SPEED_HIGH) {
1218 usb_reset_device(udev);
1219 info("DEV Failed to connect in HIGH SPEED mode");
1220 return -ENODEV;
1223 if (lme2510_return_status(udev) == 0x44) {
1224 lme_firmware_switch(udev, 0);
1225 return -ENODEV;
1228 if (0 == dvb_usb_device_init(intf, &lme2510_properties,
1229 THIS_MODULE, NULL, adapter_nr)) {
1230 info("DEV registering device driver");
1231 return 0;
1233 if (0 == dvb_usb_device_init(intf, &lme2510c_properties,
1234 THIS_MODULE, NULL, adapter_nr)) {
1235 info("DEV registering device driver");
1236 return 0;
1239 info("DEV lme2510 Error");
1240 return -ENODEV;
1244 static struct usb_device_id lme2510_table[] = {
1245 { USB_DEVICE(0x3344, 0x1122) }, /* LME2510 */
1246 { USB_DEVICE(0x3344, 0x1120) }, /* LME2510C */
1247 { USB_DEVICE(0x3344, 0x22f0) }, /* LME2510C RS2000 */
1248 {} /* Terminating entry */
1251 MODULE_DEVICE_TABLE(usb, lme2510_table);
1253 static struct dvb_usb_device_properties lme2510_properties = {
1254 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1255 .size_of_priv = sizeof(struct lme2510_state),
1256 .num_adapters = 1,
1257 .adapter = {
1259 .num_frontends = 1,
1260 .fe = {{
1261 .caps = DVB_USB_ADAP_HAS_PID_FILTER|
1262 DVB_USB_ADAP_NEED_PID_FILTERING|
1263 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1264 .streaming_ctrl = lme2510_streaming_ctrl,
1265 .pid_filter_count = 32,
1266 .pid_filter = lme2510_pid_filter,
1267 .pid_filter_ctrl = lme2510_pid_filter_ctrl,
1268 .frontend_attach = dm04_lme2510_frontend_attach,
1269 .tuner_attach = dm04_lme2510_tuner,
1270 /* parameter for the MPEG2-data transfer */
1271 .stream = {
1272 .type = USB_BULK,
1273 .count = 10,
1274 .endpoint = 0x06,
1275 .u = {
1276 .bulk = {
1277 .buffersize = 4096,
1285 .rc.core = {
1286 .protocol = RC_TYPE_NEC,
1287 .module_name = "LME2510 Remote Control",
1288 .allowed_protos = RC_TYPE_NEC,
1289 .rc_codes = RC_MAP_LME2510,
1291 .power_ctrl = lme2510_powerup,
1292 .identify_state = lme2510_identify_state,
1293 .i2c_algo = &lme2510_i2c_algo,
1294 .generic_bulk_ctrl_endpoint = 0,
1295 .num_device_descs = 1,
1296 .devices = {
1297 { "DM04_LME2510_DVB-S",
1298 { &lme2510_table[0], NULL },
1304 static struct dvb_usb_device_properties lme2510c_properties = {
1305 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1306 .size_of_priv = sizeof(struct lme2510_state),
1307 .num_adapters = 1,
1308 .adapter = {
1310 .num_frontends = 1,
1311 .fe = {{
1312 .caps = DVB_USB_ADAP_HAS_PID_FILTER|
1313 DVB_USB_ADAP_NEED_PID_FILTERING|
1314 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1315 .streaming_ctrl = lme2510_streaming_ctrl,
1316 .pid_filter_count = 32,
1317 .pid_filter = lme2510_pid_filter,
1318 .pid_filter_ctrl = lme2510_pid_filter_ctrl,
1319 .frontend_attach = dm04_lme2510_frontend_attach,
1320 .tuner_attach = dm04_lme2510_tuner,
1321 /* parameter for the MPEG2-data transfer */
1322 .stream = {
1323 .type = USB_BULK,
1324 .count = 10,
1325 .endpoint = 0x8,
1326 .u = {
1327 .bulk = {
1328 .buffersize = 4096,
1336 .rc.core = {
1337 .protocol = RC_TYPE_NEC,
1338 .module_name = "LME2510 Remote Control",
1339 .allowed_protos = RC_TYPE_NEC,
1340 .rc_codes = RC_MAP_LME2510,
1342 .power_ctrl = lme2510_powerup,
1343 .identify_state = lme2510_identify_state,
1344 .i2c_algo = &lme2510_i2c_algo,
1345 .generic_bulk_ctrl_endpoint = 0,
1346 .num_device_descs = 2,
1347 .devices = {
1348 { "DM04_LME2510C_DVB-S",
1349 { &lme2510_table[1], NULL },
1351 { "DM04_LME2510C_DVB-S RS2000",
1352 { &lme2510_table[2], NULL },
1357 static void *lme2510_exit_int(struct dvb_usb_device *d)
1359 struct lme2510_state *st = d->priv;
1360 struct dvb_usb_adapter *adap = &d->adapter[0];
1361 void *buffer = NULL;
1363 if (adap != NULL) {
1364 lme2510_kill_urb(&adap->fe_adap[0].stream);
1365 adap->feedcount = 0;
1368 if (st->usb_buffer != NULL) {
1369 st->i2c_talk_onoff = 1;
1370 st->signal_lock = 0;
1371 st->signal_level = 0;
1372 st->signal_sn = 0;
1373 buffer = st->usb_buffer;
1376 if (st->lme_urb != NULL) {
1377 usb_kill_urb(st->lme_urb);
1378 usb_free_coherent(d->udev, 128, st->buffer,
1379 st->lme_urb->transfer_dma);
1380 info("Interrupt Service Stopped");
1383 return buffer;
1386 static void lme2510_exit(struct usb_interface *intf)
1388 struct dvb_usb_device *d = usb_get_intfdata(intf);
1389 void *usb_buffer;
1391 if (d != NULL) {
1392 usb_buffer = lme2510_exit_int(d);
1393 dvb_usb_device_exit(intf);
1394 if (usb_buffer != NULL)
1395 kfree(usb_buffer);
1399 static struct usb_driver lme2510_driver = {
1400 .name = "LME2510C_DVB-S",
1401 .probe = lme2510_probe,
1402 .disconnect = lme2510_exit,
1403 .id_table = lme2510_table,
1406 module_usb_driver(lme2510_driver);
1408 MODULE_AUTHOR("Malcolm Priestley <tvboxspy@gmail.com>");
1409 MODULE_DESCRIPTION("LME2510(C) DVB-S USB2.0");
1410 MODULE_VERSION("1.99");
1411 MODULE_LICENSE("GPL");