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