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