hwrng: core - Don't use a stack buffer in add_early_randomness()
[linux/fpc-iii.git] / drivers / media / usb / dvb-usb-v2 / lmedm04.c
blob0e8fb89896c40ee26b02df2db275a64a85cbe866
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 * LME2510C + M88RS2000
24 * For firmware see Documentation/dvb/lmedm04.txt
26 * I2C addresses:
27 * 0xd0 - STV0288 - Demodulator
28 * 0xc0 - Sharp IX2505V - Tuner
29 * --
30 * 0x1c - TDA10086 - Demodulator
31 * 0xc0 - TDA8263 - Tuner
32 * --
33 * 0xd0 - STV0299 - Demodulator
34 * 0xc0 - IX2410 - Tuner
37 * VID = 3344 PID LME2510=1122 LME2510C=1120
39 * Copyright (C) 2010 Malcolm Priestley (tvboxspy@gmail.com)
40 * LME2510(C)(C) Leaguerme (Shenzhen) MicroElectronics Co., Ltd.
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.
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.
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.
56 * see Documentation/dvb/README.dvb-usb for more information
58 * Known Issues :
59 * LME2510: Non Intel USB chipsets fail to maintain High Speed on
60 * Boot or Hot Plug.
62 * QQbox suffers from noise on LNB voltage.
64 * LME2510: SHARP:BS2F7HZ0194(MV0194) cannot cold reset and share system
65 * with other tuners. After a cold reset streaming will not start.
67 * M88RS2000 suffers from loss of lock.
69 #define DVB_USB_LOG_PREFIX "LME2510(C)"
70 #include <linux/usb.h>
71 #include <linux/usb/input.h>
72 #include <media/rc-core.h>
74 #include "dvb_usb.h"
75 #include "lmedm04.h"
76 #include "tda826x.h"
77 #include "tda10086.h"
78 #include "stv0288.h"
79 #include "ix2505v.h"
80 #include "stv0299.h"
81 #include "dvb-pll.h"
82 #include "z0194a.h"
83 #include "m88rs2000.h"
84 #include "ts2020.h"
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";
94 /* debug */
95 static int dvb_usb_lme2510_debug;
96 #define lme_debug(var, level, args...) do { \
97 if ((var >= level)) \
98 pr_debug(DVB_USB_LOG_PREFIX": " args); \
99 } while (0)
100 #define deb_info(level, args...) lme_debug(dvb_usb_lme2510_debug, level, args)
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));
105 #define info(args...) pr_info(DVB_USB_LOG_PREFIX": "args)
107 module_param_named(debug, dvb_usb_lme2510_debug, int, 0644);
108 MODULE_PARM_DESC(debug, "set debugging level (1=info (or-able)).");
110 static int dvb_usb_lme2510_firmware;
111 module_param_named(firmware, dvb_usb_lme2510_firmware, int, 0644);
112 MODULE_PARM_DESC(firmware, "set default firmware 0=Sharp7395 1=LG");
114 static int pid_filter;
115 module_param_named(pid, pid_filter, int, 0644);
116 MODULE_PARM_DESC(pid, "set default 0=default 1=off 2=on");
119 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
121 #define TUNER_DEFAULT 0x0
122 #define TUNER_LG 0x1
123 #define TUNER_S7395 0x2
124 #define TUNER_S0194 0x3
125 #define TUNER_RS2000 0x4
127 struct lme2510_state {
128 unsigned long int_urb_due;
129 enum fe_status lock_status;
130 u8 id;
131 u8 tuner_config;
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;
140 u8 stream_on;
141 u8 pid_size;
142 u8 pid_off;
143 void *buffer;
144 struct urb *lme_urb;
145 void *usb_buffer;
146 /* Frontend original calls */
147 int (*fe_read_status)(struct dvb_frontend *, enum fe_status *);
148 int (*fe_read_signal_strength)(struct dvb_frontend *, u16 *);
149 int (*fe_read_snr)(struct dvb_frontend *, u16 *);
150 int (*fe_read_ber)(struct dvb_frontend *, u32 *);
151 int (*fe_read_ucblocks)(struct dvb_frontend *, u32 *);
152 int (*fe_set_voltage)(struct dvb_frontend *, enum fe_sec_voltage);
153 u8 dvb_usb_lme2510_firmware;
156 static int lme2510_bulk_write(struct usb_device *dev,
157 u8 *snd, int len, u8 pipe)
159 int ret, actual_l;
161 ret = usb_bulk_msg(dev, usb_sndbulkpipe(dev, pipe),
162 snd, len , &actual_l, 100);
163 return ret;
166 static int lme2510_bulk_read(struct usb_device *dev,
167 u8 *rev, int len, u8 pipe)
169 int ret, actual_l;
171 ret = usb_bulk_msg(dev, usb_rcvbulkpipe(dev, pipe),
172 rev, len , &actual_l, 200);
173 return ret;
176 static int lme2510_usb_talk(struct dvb_usb_device *d,
177 u8 *wbuf, int wlen, u8 *rbuf, int rlen)
179 struct lme2510_state *st = d->priv;
180 u8 *buff;
181 int ret = 0;
183 if (st->usb_buffer == NULL) {
184 st->usb_buffer = kmalloc(64, GFP_KERNEL);
185 if (st->usb_buffer == NULL) {
186 info("MEM Error no memory");
187 return -ENOMEM;
190 buff = st->usb_buffer;
192 ret = mutex_lock_interruptible(&d->usb_mutex);
194 if (ret < 0)
195 return -EAGAIN;
197 /* the read/write capped at 64 */
198 memcpy(buff, wbuf, (wlen < 64) ? wlen : 64);
200 ret |= lme2510_bulk_write(d->udev, buff, wlen , 0x01);
202 ret |= lme2510_bulk_read(d->udev, buff, (rlen < 64) ?
203 rlen : 64 , 0x01);
205 if (rlen > 0)
206 memcpy(rbuf, buff, rlen);
208 mutex_unlock(&d->usb_mutex);
210 return (ret < 0) ? -ENODEV : 0;
213 static int lme2510_stream_restart(struct dvb_usb_device *d)
215 struct lme2510_state *st = d->priv;
216 u8 all_pids[] = LME_ALL_PIDS;
217 u8 stream_on[] = LME_ST_ON_W;
218 int ret;
219 u8 rbuff[1];
220 if (st->pid_off)
221 ret = lme2510_usb_talk(d, all_pids, sizeof(all_pids),
222 rbuff, sizeof(rbuff));
223 /*Restart Stream Command*/
224 ret = lme2510_usb_talk(d, stream_on, sizeof(stream_on),
225 rbuff, sizeof(rbuff));
226 return ret;
229 static int lme2510_enable_pid(struct dvb_usb_device *d, u8 index, u16 pid_out)
231 struct lme2510_state *st = d->priv;
232 static u8 pid_buff[] = LME_ZERO_PID;
233 static u8 rbuf[1];
234 u8 pid_no = index * 2;
235 u8 pid_len = pid_no + 2;
236 int ret = 0;
237 deb_info(1, "PID Setting Pid %04x", pid_out);
239 if (st->pid_size == 0)
240 ret |= lme2510_stream_restart(d);
242 pid_buff[2] = pid_no;
243 pid_buff[3] = (u8)pid_out & 0xff;
244 pid_buff[4] = pid_no + 1;
245 pid_buff[5] = (u8)(pid_out >> 8);
247 if (pid_len > st->pid_size)
248 st->pid_size = pid_len;
249 pid_buff[7] = 0x80 + st->pid_size;
251 ret |= lme2510_usb_talk(d, pid_buff ,
252 sizeof(pid_buff) , rbuf, sizeof(rbuf));
254 if (st->stream_on)
255 ret |= lme2510_stream_restart(d);
257 return ret;
260 /* Convert range from 0x00-0xff to 0x0000-0xffff */
261 #define reg_to_16bits(x) ((x) | ((x) << 8))
263 static void lme2510_update_stats(struct dvb_usb_adapter *adap)
265 struct lme2510_state *st = adap_to_priv(adap);
266 struct dvb_frontend *fe = adap->fe[0];
267 struct dtv_frontend_properties *c;
268 u32 s_tmp = 0, c_tmp = 0;
270 if (!fe)
271 return;
273 c = &fe->dtv_property_cache;
275 c->block_count.len = 1;
276 c->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
277 c->block_error.len = 1;
278 c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
279 c->post_bit_count.len = 1;
280 c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
281 c->post_bit_error.len = 1;
282 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
284 if (st->i2c_talk_onoff) {
285 c->strength.len = 1;
286 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
287 c->cnr.len = 1;
288 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
289 return;
292 switch (st->tuner_config) {
293 case TUNER_LG:
294 s_tmp = reg_to_16bits(0xff - st->signal_level);
295 c_tmp = reg_to_16bits(0xff - st->signal_sn);
296 break;
297 case TUNER_S7395:
298 case TUNER_S0194:
299 s_tmp = 0xffff - (((st->signal_level * 2) << 8) * 5 / 4);
300 c_tmp = reg_to_16bits((0xff - st->signal_sn - 0xa1) * 3);
301 break;
302 case TUNER_RS2000:
303 s_tmp = reg_to_16bits(st->signal_level);
304 c_tmp = reg_to_16bits(st->signal_sn);
307 c->strength.len = 1;
308 c->strength.stat[0].scale = FE_SCALE_RELATIVE;
309 c->strength.stat[0].uvalue = (u64)s_tmp;
311 c->cnr.len = 1;
312 c->cnr.stat[0].scale = FE_SCALE_RELATIVE;
313 c->cnr.stat[0].uvalue = (u64)c_tmp;
316 static void lme2510_int_response(struct urb *lme_urb)
318 struct dvb_usb_adapter *adap = lme_urb->context;
319 struct lme2510_state *st = adap_to_priv(adap);
320 static u8 *ibuf, *rbuf;
321 int i = 0, offset;
322 u32 key;
323 u8 signal_lock = 0;
325 switch (lme_urb->status) {
326 case 0:
327 case -ETIMEDOUT:
328 break;
329 case -ECONNRESET:
330 case -ENOENT:
331 case -ESHUTDOWN:
332 return;
333 default:
334 info("Error %x", lme_urb->status);
335 break;
338 rbuf = (u8 *) lme_urb->transfer_buffer;
340 offset = ((lme_urb->actual_length/8) > 4)
341 ? 4 : (lme_urb->actual_length/8) ;
343 for (i = 0; i < offset; ++i) {
344 ibuf = (u8 *)&rbuf[i*8];
345 deb_info(5, "INT O/S C =%02x C/O=%02x Type =%02x%02x",
346 offset, i, ibuf[0], ibuf[1]);
348 switch (ibuf[0]) {
349 case 0xaa:
350 debug_data_snipet(1, "INT Remote data snipet", ibuf);
351 if (!adap_to_d(adap)->rc_dev)
352 break;
354 key = RC_SCANCODE_NEC32(ibuf[2] << 24 |
355 ibuf[3] << 16 |
356 ibuf[4] << 8 |
357 ibuf[5]);
359 deb_info(1, "INT Key = 0x%08x", key);
360 rc_keydown(adap_to_d(adap)->rc_dev, RC_TYPE_NEC32, key,
362 break;
363 case 0xbb:
364 switch (st->tuner_config) {
365 case TUNER_LG:
366 signal_lock = ibuf[2] & BIT(5);
367 st->signal_level = ibuf[4];
368 st->signal_sn = ibuf[3];
369 st->time_key = ibuf[7];
370 break;
371 case TUNER_S7395:
372 case TUNER_S0194:
373 /* Tweak for earlier firmware*/
374 if (ibuf[1] == 0x03) {
375 signal_lock = ibuf[2] & BIT(4);
376 st->signal_level = ibuf[3];
377 st->signal_sn = ibuf[4];
378 } else {
379 st->signal_level = ibuf[4];
380 st->signal_sn = ibuf[5];
382 break;
383 case TUNER_RS2000:
384 signal_lock = ibuf[2] & 0xee;
385 st->signal_level = ibuf[5];
386 st->signal_sn = ibuf[4];
387 st->time_key = ibuf[7];
388 default:
389 break;
392 /* Interrupt will also throw just BIT 0 as lock */
393 signal_lock |= ibuf[2] & BIT(0);
395 if (!signal_lock)
396 st->lock_status &= ~FE_HAS_LOCK;
398 lme2510_update_stats(adap);
400 debug_data_snipet(5, "INT Remote data snipet in", ibuf);
401 break;
402 case 0xcc:
403 debug_data_snipet(1, "INT Control data snipet", ibuf);
404 break;
405 default:
406 debug_data_snipet(1, "INT Unknown data snipet", ibuf);
407 break;
411 usb_submit_urb(lme_urb, GFP_ATOMIC);
413 /* Interrupt urb is due every 48 msecs while streaming the buffer
414 * stores up to 4 periods if missed. Allow 200 msec for next interrupt.
416 st->int_urb_due = jiffies + msecs_to_jiffies(200);
419 static int lme2510_int_read(struct dvb_usb_adapter *adap)
421 struct dvb_usb_device *d = adap_to_d(adap);
422 struct lme2510_state *lme_int = adap_to_priv(adap);
423 struct usb_host_endpoint *ep;
425 lme_int->lme_urb = usb_alloc_urb(0, GFP_ATOMIC);
427 if (lme_int->lme_urb == NULL)
428 return -ENOMEM;
430 lme_int->buffer = usb_alloc_coherent(d->udev, 128, GFP_ATOMIC,
431 &lme_int->lme_urb->transfer_dma);
433 if (lme_int->buffer == NULL)
434 return -ENOMEM;
436 usb_fill_int_urb(lme_int->lme_urb,
437 d->udev,
438 usb_rcvintpipe(d->udev, 0xa),
439 lme_int->buffer,
440 128,
441 lme2510_int_response,
442 adap,
445 /* Quirk of pipe reporting PIPE_BULK but behaves as interrupt */
446 ep = usb_pipe_endpoint(d->udev, lme_int->lme_urb->pipe);
448 if (usb_endpoint_type(&ep->desc) == USB_ENDPOINT_XFER_BULK)
449 lme_int->lme_urb->pipe = usb_rcvbulkpipe(d->udev, 0xa),
451 lme_int->lme_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
453 usb_submit_urb(lme_int->lme_urb, GFP_ATOMIC);
454 info("INT Interrupt Service Started");
456 return 0;
459 static int lme2510_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
461 struct dvb_usb_device *d = adap_to_d(adap);
462 struct lme2510_state *st = adap_to_priv(adap);
463 static u8 clear_pid_reg[] = LME_ALL_PIDS;
464 static u8 rbuf[1];
465 int ret = 0;
467 deb_info(1, "PID Clearing Filter");
469 mutex_lock(&d->i2c_mutex);
471 if (!onoff) {
472 ret |= lme2510_usb_talk(d, clear_pid_reg,
473 sizeof(clear_pid_reg), rbuf, sizeof(rbuf));
474 st->pid_off = true;
475 } else
476 st->pid_off = false;
478 st->pid_size = 0;
480 mutex_unlock(&d->i2c_mutex);
482 return 0;
485 static int lme2510_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
486 int onoff)
488 struct dvb_usb_device *d = adap_to_d(adap);
489 int ret = 0;
491 deb_info(3, "%s PID=%04x Index=%04x onoff=%02x", __func__,
492 pid, index, onoff);
494 if (onoff) {
495 mutex_lock(&d->i2c_mutex);
496 ret |= lme2510_enable_pid(d, index, pid);
497 mutex_unlock(&d->i2c_mutex);
501 return ret;
505 static int lme2510_return_status(struct dvb_usb_device *d)
507 int ret = 0;
508 u8 *data;
510 data = kzalloc(10, GFP_KERNEL);
511 if (!data)
512 return -ENOMEM;
514 ret |= usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0),
515 0x06, 0x80, 0x0302, 0x00, data, 0x0006, 200);
516 info("Firmware Status: %x (%x)", ret , data[2]);
518 ret = (ret < 0) ? -ENODEV : data[2];
519 kfree(data);
520 return ret;
523 static int lme2510_msg(struct dvb_usb_device *d,
524 u8 *wbuf, int wlen, u8 *rbuf, int rlen)
526 struct lme2510_state *st = d->priv;
528 st->i2c_talk_onoff = 1;
530 return lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
533 static int lme2510_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
534 int num)
536 struct dvb_usb_device *d = i2c_get_adapdata(adap);
537 struct lme2510_state *st = d->priv;
538 static u8 obuf[64], ibuf[64];
539 int i, read, read_o;
540 u16 len;
541 u8 gate = st->i2c_gate;
543 mutex_lock(&d->i2c_mutex);
545 if (gate == 0)
546 gate = 5;
548 for (i = 0; i < num; i++) {
549 read_o = msg[i].flags & I2C_M_RD;
550 read = i + 1 < num && msg[i + 1].flags & I2C_M_RD;
551 read |= read_o;
552 gate = (msg[i].addr == st->i2c_tuner_addr)
553 ? (read) ? st->i2c_tuner_gate_r
554 : st->i2c_tuner_gate_w
555 : st->i2c_gate;
556 obuf[0] = gate | (read << 7);
558 if (gate == 5)
559 obuf[1] = (read) ? 2 : msg[i].len + 1;
560 else
561 obuf[1] = msg[i].len + read + 1;
563 obuf[2] = msg[i].addr << 1;
565 if (read) {
566 if (read_o)
567 len = 3;
568 else {
569 memcpy(&obuf[3], msg[i].buf, msg[i].len);
570 obuf[msg[i].len+3] = msg[i+1].len;
571 len = msg[i].len+4;
573 } else {
574 memcpy(&obuf[3], msg[i].buf, msg[i].len);
575 len = msg[i].len+3;
578 if (lme2510_msg(d, obuf, len, ibuf, 64) < 0) {
579 deb_info(1, "i2c transfer failed.");
580 mutex_unlock(&d->i2c_mutex);
581 return -EAGAIN;
584 if (read) {
585 if (read_o)
586 memcpy(msg[i].buf, &ibuf[1], msg[i].len);
587 else {
588 memcpy(msg[i+1].buf, &ibuf[1], msg[i+1].len);
589 i++;
594 mutex_unlock(&d->i2c_mutex);
595 return i;
598 static u32 lme2510_i2c_func(struct i2c_adapter *adapter)
600 return I2C_FUNC_I2C;
603 static struct i2c_algorithm lme2510_i2c_algo = {
604 .master_xfer = lme2510_i2c_xfer,
605 .functionality = lme2510_i2c_func,
608 static int lme2510_streaming_ctrl(struct dvb_frontend *fe, int onoff)
610 struct dvb_usb_adapter *adap = fe_to_adap(fe);
611 struct dvb_usb_device *d = adap_to_d(adap);
612 struct lme2510_state *st = adap_to_priv(adap);
613 static u8 clear_reg_3[] = LME_ALL_PIDS;
614 static u8 rbuf[1];
615 int ret = 0, rlen = sizeof(rbuf);
617 deb_info(1, "STM (%02x)", onoff);
619 /* Streaming is started by FE_HAS_LOCK */
620 if (onoff == 1)
621 st->stream_on = 1;
622 else {
623 deb_info(1, "STM Steam Off");
624 /* mutex is here only to avoid collision with I2C */
625 mutex_lock(&d->i2c_mutex);
627 ret = lme2510_usb_talk(d, clear_reg_3,
628 sizeof(clear_reg_3), rbuf, rlen);
629 st->stream_on = 0;
630 st->i2c_talk_onoff = 1;
632 mutex_unlock(&d->i2c_mutex);
635 return (ret < 0) ? -ENODEV : 0;
638 static u8 check_sum(u8 *p, u8 len)
640 u8 sum = 0;
641 while (len--)
642 sum += *p++;
643 return sum;
646 static int lme2510_download_firmware(struct dvb_usb_device *d,
647 const struct firmware *fw)
649 int ret = 0;
650 u8 *data;
651 u16 j, wlen, len_in, start, end;
652 u8 packet_size, dlen, i;
653 u8 *fw_data;
655 packet_size = 0x31;
656 len_in = 1;
658 data = kzalloc(128, GFP_KERNEL);
659 if (!data) {
660 info("FRM Could not start Firmware Download"\
661 "(Buffer allocation failed)");
662 return -ENOMEM;
665 info("FRM Starting Firmware Download");
667 for (i = 1; i < 3; i++) {
668 start = (i == 1) ? 0 : 512;
669 end = (i == 1) ? 512 : fw->size;
670 for (j = start; j < end; j += (packet_size+1)) {
671 fw_data = (u8 *)(fw->data + j);
672 if ((end - j) > packet_size) {
673 data[0] = i;
674 dlen = packet_size;
675 } else {
676 data[0] = i | 0x80;
677 dlen = (u8)(end - j)-1;
679 data[1] = dlen;
680 memcpy(&data[2], fw_data, dlen+1);
681 wlen = (u8) dlen + 4;
682 data[wlen-1] = check_sum(fw_data, dlen+1);
683 deb_info(1, "Data S=%02x:E=%02x CS= %02x", data[3],
684 data[dlen+2], data[dlen+3]);
685 lme2510_usb_talk(d, data, wlen, data, len_in);
686 ret |= (data[0] == 0x88) ? 0 : -1;
690 data[0] = 0x8a;
691 len_in = 1;
692 msleep(2000);
693 lme2510_usb_talk(d, data, len_in, data, len_in);
694 msleep(400);
696 if (ret < 0)
697 info("FRM Firmware Download Failed (%04x)" , ret);
698 else
699 info("FRM Firmware Download Completed - Resetting Device");
701 kfree(data);
702 return RECONNECTS_USB;
705 static void lme_coldreset(struct dvb_usb_device *d)
707 u8 data[1] = {0};
708 data[0] = 0x0a;
709 info("FRM Firmware Cold Reset");
711 lme2510_usb_talk(d, data, sizeof(data), data, sizeof(data));
713 return;
716 static const char fw_c_s7395[] = LME2510_C_S7395;
717 static const char fw_c_lg[] = LME2510_C_LG;
718 static const char fw_c_s0194[] = LME2510_C_S0194;
719 static const char fw_c_rs2000[] = LME2510_C_RS2000;
720 static const char fw_lg[] = LME2510_LG;
721 static const char fw_s0194[] = LME2510_S0194;
723 static const char *lme_firmware_switch(struct dvb_usb_device *d, int cold)
725 struct lme2510_state *st = d->priv;
726 struct usb_device *udev = d->udev;
727 const struct firmware *fw = NULL;
728 const char *fw_lme;
729 int ret = 0;
731 cold = (cold > 0) ? (cold & 1) : 0;
733 switch (le16_to_cpu(udev->descriptor.idProduct)) {
734 case 0x1122:
735 switch (st->dvb_usb_lme2510_firmware) {
736 default:
737 case TUNER_S0194:
738 fw_lme = fw_s0194;
739 ret = request_firmware(&fw, fw_lme, &udev->dev);
740 if (ret == 0) {
741 st->dvb_usb_lme2510_firmware = TUNER_S0194;
742 cold = 0;
743 break;
745 /* fall through */
746 case TUNER_LG:
747 fw_lme = fw_lg;
748 ret = request_firmware(&fw, fw_lme, &udev->dev);
749 if (ret == 0) {
750 st->dvb_usb_lme2510_firmware = TUNER_LG;
751 break;
753 st->dvb_usb_lme2510_firmware = TUNER_DEFAULT;
754 break;
756 break;
757 case 0x1120:
758 switch (st->dvb_usb_lme2510_firmware) {
759 default:
760 case TUNER_S7395:
761 fw_lme = fw_c_s7395;
762 ret = request_firmware(&fw, fw_lme, &udev->dev);
763 if (ret == 0) {
764 st->dvb_usb_lme2510_firmware = TUNER_S7395;
765 cold = 0;
766 break;
768 /* fall through */
769 case TUNER_LG:
770 fw_lme = fw_c_lg;
771 ret = request_firmware(&fw, fw_lme, &udev->dev);
772 if (ret == 0) {
773 st->dvb_usb_lme2510_firmware = TUNER_LG;
774 break;
776 /* fall through */
777 case TUNER_S0194:
778 fw_lme = fw_c_s0194;
779 ret = request_firmware(&fw, fw_lme, &udev->dev);
780 if (ret == 0) {
781 st->dvb_usb_lme2510_firmware = TUNER_S0194;
782 break;
784 st->dvb_usb_lme2510_firmware = TUNER_DEFAULT;
785 cold = 0;
786 break;
788 break;
789 case 0x22f0:
790 fw_lme = fw_c_rs2000;
791 st->dvb_usb_lme2510_firmware = TUNER_RS2000;
792 break;
793 default:
794 fw_lme = fw_c_s7395;
797 release_firmware(fw);
799 if (cold) {
800 dvb_usb_lme2510_firmware = st->dvb_usb_lme2510_firmware;
801 info("FRM Changing to %s firmware", fw_lme);
802 lme_coldreset(d);
803 return NULL;
806 return fw_lme;
809 static int lme2510_kill_urb(struct usb_data_stream *stream)
811 int i;
813 for (i = 0; i < stream->urbs_submitted; i++) {
814 deb_info(3, "killing URB no. %d.", i);
815 /* stop the URB */
816 usb_kill_urb(stream->urb_list[i]);
818 stream->urbs_submitted = 0;
820 return 0;
823 static struct tda10086_config tda10086_config = {
824 .demod_address = 0x0e,
825 .invert = 0,
826 .diseqc_tone = 1,
827 .xtal_freq = TDA10086_XTAL_16M,
830 static struct stv0288_config lme_config = {
831 .demod_address = 0x68,
832 .min_delay_ms = 15,
833 .inittab = s7395_inittab,
836 static struct ix2505v_config lme_tuner = {
837 .tuner_address = 0x60,
838 .min_delay_ms = 100,
839 .tuner_gain = 0x0,
840 .tuner_chargepump = 0x3,
843 static struct stv0299_config sharp_z0194_config = {
844 .demod_address = 0x68,
845 .inittab = sharp_z0194a_inittab,
846 .mclk = 88000000UL,
847 .invert = 0,
848 .skip_reinit = 0,
849 .lock_output = STV0299_LOCKOUTPUT_1,
850 .volt13_op0_op1 = STV0299_VOLT13_OP1,
851 .min_delay_ms = 100,
852 .set_symbol_rate = sharp_z0194a_set_symbol_rate,
855 static struct m88rs2000_config m88rs2000_config = {
856 .demod_addr = 0x68
859 static struct ts2020_config ts2020_config = {
860 .tuner_address = 0x60,
861 .clk_out_div = 7,
862 .dont_poll = true
865 static int dm04_lme2510_set_voltage(struct dvb_frontend *fe,
866 enum fe_sec_voltage voltage)
868 struct dvb_usb_device *d = fe_to_d(fe);
869 struct lme2510_state *st = fe_to_priv(fe);
870 static u8 voltage_low[] = LME_VOLTAGE_L;
871 static u8 voltage_high[] = LME_VOLTAGE_H;
872 static u8 rbuf[1];
873 int ret = 0, len = 3, rlen = 1;
875 mutex_lock(&d->i2c_mutex);
877 switch (voltage) {
878 case SEC_VOLTAGE_18:
879 ret |= lme2510_usb_talk(d,
880 voltage_high, len, rbuf, rlen);
881 break;
883 case SEC_VOLTAGE_OFF:
884 case SEC_VOLTAGE_13:
885 default:
886 ret |= lme2510_usb_talk(d,
887 voltage_low, len, rbuf, rlen);
888 break;
891 mutex_unlock(&d->i2c_mutex);
893 if (st->tuner_config == TUNER_RS2000)
894 if (st->fe_set_voltage)
895 st->fe_set_voltage(fe, voltage);
898 return (ret < 0) ? -ENODEV : 0;
901 static int dm04_read_status(struct dvb_frontend *fe, enum fe_status *status)
903 struct dvb_usb_device *d = fe_to_d(fe);
904 struct lme2510_state *st = d->priv;
905 int ret = 0;
907 if (st->i2c_talk_onoff) {
908 if (st->fe_read_status) {
909 ret = st->fe_read_status(fe, status);
910 if (ret < 0)
911 return ret;
914 st->lock_status = *status;
916 if (*status & FE_HAS_LOCK && st->stream_on) {
917 mutex_lock(&d->i2c_mutex);
919 st->i2c_talk_onoff = 0;
920 ret = lme2510_stream_restart(d);
922 mutex_unlock(&d->i2c_mutex);
925 return ret;
928 /* Timeout of interrupt reached on RS2000 */
929 if (st->tuner_config == TUNER_RS2000 &&
930 time_after(jiffies, st->int_urb_due))
931 st->lock_status &= ~FE_HAS_LOCK;
933 *status = st->lock_status;
935 if (!(*status & FE_HAS_LOCK)) {
936 struct dvb_usb_adapter *adap = fe_to_adap(fe);
938 st->i2c_talk_onoff = 1;
940 lme2510_update_stats(adap);
943 return ret;
946 static int dm04_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
948 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
949 struct lme2510_state *st = fe_to_priv(fe);
951 if (st->fe_read_signal_strength && !st->stream_on)
952 return st->fe_read_signal_strength(fe, strength);
954 if (c->strength.stat[0].scale == FE_SCALE_RELATIVE)
955 *strength = (u16)c->strength.stat[0].uvalue;
956 else
957 *strength = 0;
959 return 0;
962 static int dm04_read_snr(struct dvb_frontend *fe, u16 *snr)
964 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
965 struct lme2510_state *st = fe_to_priv(fe);
967 if (st->fe_read_snr && !st->stream_on)
968 return st->fe_read_snr(fe, snr);
970 if (c->cnr.stat[0].scale == FE_SCALE_RELATIVE)
971 *snr = (u16)c->cnr.stat[0].uvalue;
972 else
973 *snr = 0;
975 return 0;
978 static int dm04_read_ber(struct dvb_frontend *fe, u32 *ber)
980 struct lme2510_state *st = fe_to_priv(fe);
982 if (st->fe_read_ber && !st->stream_on)
983 return st->fe_read_ber(fe, ber);
985 *ber = 0;
987 return 0;
990 static int dm04_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
992 struct lme2510_state *st = fe_to_priv(fe);
994 if (st->fe_read_ucblocks && !st->stream_on)
995 return st->fe_read_ucblocks(fe, ucblocks);
997 *ucblocks = 0;
999 return 0;
1002 static int lme_name(struct dvb_usb_adapter *adap)
1004 struct dvb_usb_device *d = adap_to_d(adap);
1005 struct lme2510_state *st = adap_to_priv(adap);
1006 const char *desc = d->name;
1007 char *fe_name[] = {"", " LG TDQY-P001F", " SHARP:BS2F7HZ7395",
1008 " SHARP:BS2F7HZ0194", " RS2000"};
1009 char *name = adap->fe[0]->ops.info.name;
1011 strlcpy(name, desc, 128);
1012 strlcat(name, fe_name[st->tuner_config], 128);
1014 return 0;
1017 static int dm04_lme2510_frontend_attach(struct dvb_usb_adapter *adap)
1019 struct dvb_usb_device *d = adap_to_d(adap);
1020 struct lme2510_state *st = d->priv;
1021 int ret = 0;
1023 st->i2c_talk_onoff = 1;
1024 switch (le16_to_cpu(d->udev->descriptor.idProduct)) {
1025 case 0x1122:
1026 case 0x1120:
1027 st->i2c_gate = 4;
1028 adap->fe[0] = dvb_attach(tda10086_attach,
1029 &tda10086_config, &d->i2c_adap);
1030 if (adap->fe[0]) {
1031 info("TUN Found Frontend TDA10086");
1032 st->i2c_tuner_gate_w = 4;
1033 st->i2c_tuner_gate_r = 4;
1034 st->i2c_tuner_addr = 0x60;
1035 st->tuner_config = TUNER_LG;
1036 if (st->dvb_usb_lme2510_firmware != TUNER_LG) {
1037 st->dvb_usb_lme2510_firmware = TUNER_LG;
1038 ret = lme_firmware_switch(d, 1) ? 0 : -ENODEV;
1040 break;
1043 st->i2c_gate = 4;
1044 adap->fe[0] = dvb_attach(stv0299_attach,
1045 &sharp_z0194_config, &d->i2c_adap);
1046 if (adap->fe[0]) {
1047 info("FE Found Stv0299");
1048 st->i2c_tuner_gate_w = 4;
1049 st->i2c_tuner_gate_r = 5;
1050 st->i2c_tuner_addr = 0x60;
1051 st->tuner_config = TUNER_S0194;
1052 if (st->dvb_usb_lme2510_firmware != TUNER_S0194) {
1053 st->dvb_usb_lme2510_firmware = TUNER_S0194;
1054 ret = lme_firmware_switch(d, 1) ? 0 : -ENODEV;
1056 break;
1059 st->i2c_gate = 5;
1060 adap->fe[0] = dvb_attach(stv0288_attach, &lme_config,
1061 &d->i2c_adap);
1063 if (adap->fe[0]) {
1064 info("FE Found Stv0288");
1065 st->i2c_tuner_gate_w = 4;
1066 st->i2c_tuner_gate_r = 5;
1067 st->i2c_tuner_addr = 0x60;
1068 st->tuner_config = TUNER_S7395;
1069 if (st->dvb_usb_lme2510_firmware != TUNER_S7395) {
1070 st->dvb_usb_lme2510_firmware = TUNER_S7395;
1071 ret = lme_firmware_switch(d, 1) ? 0 : -ENODEV;
1073 break;
1075 case 0x22f0:
1076 st->i2c_gate = 5;
1077 adap->fe[0] = dvb_attach(m88rs2000_attach,
1078 &m88rs2000_config, &d->i2c_adap);
1080 if (adap->fe[0]) {
1081 info("FE Found M88RS2000");
1082 dvb_attach(ts2020_attach, adap->fe[0], &ts2020_config,
1083 &d->i2c_adap);
1084 st->i2c_tuner_gate_w = 5;
1085 st->i2c_tuner_gate_r = 5;
1086 st->i2c_tuner_addr = 0x60;
1087 st->tuner_config = TUNER_RS2000;
1088 st->fe_set_voltage =
1089 adap->fe[0]->ops.set_voltage;
1091 break;
1094 if (adap->fe[0] == NULL) {
1095 info("DM04/QQBOX Not Powered up or not Supported");
1096 return -ENODEV;
1099 if (ret) {
1100 if (adap->fe[0]) {
1101 dvb_frontend_detach(adap->fe[0]);
1102 adap->fe[0] = NULL;
1104 d->rc_map = NULL;
1105 return -ENODEV;
1108 st->fe_read_status = adap->fe[0]->ops.read_status;
1109 st->fe_read_signal_strength = adap->fe[0]->ops.read_signal_strength;
1110 st->fe_read_snr = adap->fe[0]->ops.read_snr;
1111 st->fe_read_ber = adap->fe[0]->ops.read_ber;
1112 st->fe_read_ucblocks = adap->fe[0]->ops.read_ucblocks;
1114 adap->fe[0]->ops.read_status = dm04_read_status;
1115 adap->fe[0]->ops.read_signal_strength = dm04_read_signal_strength;
1116 adap->fe[0]->ops.read_snr = dm04_read_snr;
1117 adap->fe[0]->ops.read_ber = dm04_read_ber;
1118 adap->fe[0]->ops.read_ucblocks = dm04_read_ucblocks;
1119 adap->fe[0]->ops.set_voltage = dm04_lme2510_set_voltage;
1121 ret = lme_name(adap);
1122 return ret;
1125 static int dm04_lme2510_tuner(struct dvb_usb_adapter *adap)
1127 struct dvb_usb_device *d = adap_to_d(adap);
1128 struct lme2510_state *st = adap_to_priv(adap);
1129 char *tun_msg[] = {"", "TDA8263", "IX2505V", "DVB_PLL_OPERA", "RS2000"};
1130 int ret = 0;
1132 switch (st->tuner_config) {
1133 case TUNER_LG:
1134 if (dvb_attach(tda826x_attach, adap->fe[0], 0x60,
1135 &d->i2c_adap, 1))
1136 ret = st->tuner_config;
1137 break;
1138 case TUNER_S7395:
1139 if (dvb_attach(ix2505v_attach , adap->fe[0], &lme_tuner,
1140 &d->i2c_adap))
1141 ret = st->tuner_config;
1142 break;
1143 case TUNER_S0194:
1144 if (dvb_attach(dvb_pll_attach , adap->fe[0], 0x60,
1145 &d->i2c_adap, DVB_PLL_OPERA1))
1146 ret = st->tuner_config;
1147 break;
1148 case TUNER_RS2000:
1149 ret = st->tuner_config;
1150 break;
1151 default:
1152 break;
1155 if (ret)
1156 info("TUN Found %s tuner", tun_msg[ret]);
1157 else {
1158 info("TUN No tuner found --- resetting device");
1159 lme_coldreset(d);
1160 return -ENODEV;
1163 /* Start the Interrupt*/
1164 ret = lme2510_int_read(adap);
1165 if (ret < 0) {
1166 info("INT Unable to start Interrupt Service");
1167 return -ENODEV;
1170 return ret;
1173 static int lme2510_powerup(struct dvb_usb_device *d, int onoff)
1175 struct lme2510_state *st = d->priv;
1176 static u8 lnb_on[] = LNB_ON;
1177 static u8 lnb_off[] = LNB_OFF;
1178 static u8 rbuf[1];
1179 int ret = 0, len = 3, rlen = 1;
1181 mutex_lock(&d->i2c_mutex);
1183 if (onoff)
1184 ret = lme2510_usb_talk(d, lnb_on, len, rbuf, rlen);
1185 else
1186 ret = lme2510_usb_talk(d, lnb_off, len, rbuf, rlen);
1188 st->i2c_talk_onoff = 1;
1190 mutex_unlock(&d->i2c_mutex);
1192 return ret;
1195 static int lme2510_get_adapter_count(struct dvb_usb_device *d)
1197 return 1;
1200 static int lme2510_identify_state(struct dvb_usb_device *d, const char **name)
1202 struct lme2510_state *st = d->priv;
1204 usb_reset_configuration(d->udev);
1206 usb_set_interface(d->udev,
1207 d->props->bInterfaceNumber, 1);
1209 st->dvb_usb_lme2510_firmware = dvb_usb_lme2510_firmware;
1211 if (lme2510_return_status(d) == 0x44) {
1212 *name = lme_firmware_switch(d, 0);
1213 return COLD;
1216 return 0;
1219 static int lme2510_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
1220 struct usb_data_stream_properties *stream)
1222 struct dvb_usb_adapter *adap = fe_to_adap(fe);
1223 struct dvb_usb_device *d;
1225 if (adap == NULL)
1226 return 0;
1228 d = adap_to_d(adap);
1230 /* Turn PID filter on the fly by module option */
1231 if (pid_filter == 2) {
1232 adap->pid_filtering = true;
1233 adap->max_feed_count = 15;
1236 if (!(le16_to_cpu(d->udev->descriptor.idProduct)
1237 == 0x1122))
1238 stream->endpoint = 0x8;
1240 return 0;
1243 static int lme2510_get_rc_config(struct dvb_usb_device *d,
1244 struct dvb_usb_rc *rc)
1246 rc->allowed_protos = RC_BIT_NEC32;
1247 return 0;
1250 static void *lme2510_exit_int(struct dvb_usb_device *d)
1252 struct lme2510_state *st = d->priv;
1253 struct dvb_usb_adapter *adap = &d->adapter[0];
1254 void *buffer = NULL;
1256 if (adap != NULL) {
1257 lme2510_kill_urb(&adap->stream);
1260 if (st->usb_buffer != NULL) {
1261 st->i2c_talk_onoff = 1;
1262 st->signal_level = 0;
1263 st->signal_sn = 0;
1264 buffer = st->usb_buffer;
1267 if (st->lme_urb != NULL) {
1268 usb_kill_urb(st->lme_urb);
1269 usb_free_coherent(d->udev, 128, st->buffer,
1270 st->lme_urb->transfer_dma);
1271 info("Interrupt Service Stopped");
1274 return buffer;
1277 static void lme2510_exit(struct dvb_usb_device *d)
1279 void *usb_buffer;
1281 if (d != NULL) {
1282 usb_buffer = lme2510_exit_int(d);
1283 kfree(usb_buffer);
1287 static struct dvb_usb_device_properties lme2510_props = {
1288 .driver_name = KBUILD_MODNAME,
1289 .owner = THIS_MODULE,
1290 .bInterfaceNumber = 0,
1291 .adapter_nr = adapter_nr,
1292 .size_of_priv = sizeof(struct lme2510_state),
1294 .download_firmware = lme2510_download_firmware,
1296 .power_ctrl = lme2510_powerup,
1297 .identify_state = lme2510_identify_state,
1298 .i2c_algo = &lme2510_i2c_algo,
1300 .frontend_attach = dm04_lme2510_frontend_attach,
1301 .tuner_attach = dm04_lme2510_tuner,
1302 .get_stream_config = lme2510_get_stream_config,
1303 .get_adapter_count = lme2510_get_adapter_count,
1304 .streaming_ctrl = lme2510_streaming_ctrl,
1306 .get_rc_config = lme2510_get_rc_config,
1308 .exit = lme2510_exit,
1309 .adapter = {
1311 .caps = DVB_USB_ADAP_HAS_PID_FILTER|
1312 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1313 .pid_filter_count = 15,
1314 .pid_filter = lme2510_pid_filter,
1315 .pid_filter_ctrl = lme2510_pid_filter_ctrl,
1316 .stream =
1317 DVB_USB_STREAM_BULK(0x86, 10, 4096),
1324 static const struct usb_device_id lme2510_id_table[] = {
1325 { DVB_USB_DEVICE(0x3344, 0x1122, &lme2510_props,
1326 "DM04_LME2510_DVB-S", RC_MAP_LME2510) },
1327 { DVB_USB_DEVICE(0x3344, 0x1120, &lme2510_props,
1328 "DM04_LME2510C_DVB-S", RC_MAP_LME2510) },
1329 { DVB_USB_DEVICE(0x3344, 0x22f0, &lme2510_props,
1330 "DM04_LME2510C_DVB-S RS2000", RC_MAP_LME2510) },
1331 {} /* Terminating entry */
1334 MODULE_DEVICE_TABLE(usb, lme2510_id_table);
1336 static struct usb_driver lme2510_driver = {
1337 .name = KBUILD_MODNAME,
1338 .probe = dvb_usbv2_probe,
1339 .disconnect = dvb_usbv2_disconnect,
1340 .id_table = lme2510_id_table,
1341 .no_dynamic_id = 1,
1342 .soft_unbind = 1,
1345 module_usb_driver(lme2510_driver);
1347 MODULE_AUTHOR("Malcolm Priestley <tvboxspy@gmail.com>");
1348 MODULE_DESCRIPTION("LME2510(C) DVB-S USB2.0");
1349 MODULE_VERSION("2.07");
1350 MODULE_LICENSE("GPL");
1351 MODULE_FIRMWARE(LME2510_C_S7395);
1352 MODULE_FIRMWARE(LME2510_C_LG);
1353 MODULE_FIRMWARE(LME2510_C_S0194);
1354 MODULE_FIRMWARE(LME2510_C_RS2000);
1355 MODULE_FIRMWARE(LME2510_LG);
1356 MODULE_FIRMWARE(LME2510_S0194);