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