Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
lmedm04.c
Go to the documentation of this file.
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 ([email protected])
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 
85 
86 #define LME2510_C_S7395 "dvb-usb-lme2510c-s7395.fw";
87 #define LME2510_C_LG "dvb-usb-lme2510c-lg.fw";
88 #define LME2510_C_S0194 "dvb-usb-lme2510c-s0194.fw";
89 #define LME2510_C_RS2000 "dvb-usb-lme2510c-rs2000.fw";
90 #define LME2510_LG "dvb-usb-lme2510-lg.fw";
91 #define LME2510_S0194 "dvb-usb-lme2510-s0194.fw";
92 
93 /* debug */
94 static int dvb_usb_lme2510_debug;
95 #define lme_debug(var, level, args...) do { \
96  if ((var >= level)) \
97  pr_debug(DVB_USB_LOG_PREFIX": " args); \
98 } while (0)
99 #define deb_info(level, args...) lme_debug(dvb_usb_lme2510_debug, level, args)
100 #define debug_data_snipet(level, name, p) \
101  deb_info(level, name" (%02x%02x%02x%02x%02x%02x%02x%02x)", \
102  *p, *(p+1), *(p+2), *(p+3), *(p+4), \
103  *(p+5), *(p+6), *(p+7));
104 #define info(args...) pr_info(DVB_USB_LOG_PREFIX": "args)
105 
106 module_param_named(debug, dvb_usb_lme2510_debug, int, 0644);
107 MODULE_PARM_DESC(debug, "set debugging level (1=info (or-able)).");
108 
109 static int dvb_usb_lme2510_firmware;
110 module_param_named(firmware, dvb_usb_lme2510_firmware, int, 0644);
111 MODULE_PARM_DESC(firmware, "set default firmware 0=Sharp7395 1=LG");
112 
113 static int pid_filter;
114 module_param_named(pid, pid_filter, int, 0644);
115 MODULE_PARM_DESC(pid, "set default 0=default 1=off 2=on");
116 
117 
119 
120 #define TUNER_DEFAULT 0x0
121 #define TUNER_LG 0x1
122 #define TUNER_S7395 0x2
123 #define TUNER_S0194 0x3
124 #define TUNER_RS2000 0x4
125 
143  void *buffer;
144  struct urb *lme_urb;
145  void *usb_buffer;
148 };
149 
150 static int lme2510_bulk_write(struct usb_device *dev,
151  u8 *snd, int len, u8 pipe)
152 {
153  int ret, actual_l;
154 
155  ret = usb_bulk_msg(dev, usb_sndbulkpipe(dev, pipe),
156  snd, len , &actual_l, 100);
157  return ret;
158 }
159 
160 static int lme2510_bulk_read(struct usb_device *dev,
161  u8 *rev, int len, u8 pipe)
162 {
163  int ret, actual_l;
164 
165  ret = usb_bulk_msg(dev, usb_rcvbulkpipe(dev, pipe),
166  rev, len , &actual_l, 200);
167  return ret;
168 }
169 
170 static int lme2510_usb_talk(struct dvb_usb_device *d,
171  u8 *wbuf, int wlen, u8 *rbuf, int rlen)
172 {
173  struct lme2510_state *st = d->priv;
174  u8 *buff;
175  int ret = 0;
176 
177  if (st->usb_buffer == NULL) {
178  st->usb_buffer = kmalloc(64, GFP_KERNEL);
179  if (st->usb_buffer == NULL) {
180  info("MEM Error no memory");
181  return -ENOMEM;
182  }
183  }
184  buff = st->usb_buffer;
185 
187 
188  if (ret < 0)
189  return -EAGAIN;
190 
191  /* the read/write capped at 64 */
192  memcpy(buff, wbuf, (wlen < 64) ? wlen : 64);
193 
194  ret |= lme2510_bulk_write(d->udev, buff, wlen , 0x01);
195 
196  ret |= lme2510_bulk_read(d->udev, buff, (rlen < 64) ?
197  rlen : 64 , 0x01);
198 
199  if (rlen > 0)
200  memcpy(rbuf, buff, rlen);
201 
202  mutex_unlock(&d->usb_mutex);
203 
204  return (ret < 0) ? -ENODEV : 0;
205 }
206 
207 static int lme2510_stream_restart(struct dvb_usb_device *d)
208 {
209  struct lme2510_state *st = d->priv;
210  u8 all_pids[] = LME_ALL_PIDS;
212  int ret;
213  u8 rbuff[1];
214  if (st->pid_off)
215  ret = lme2510_usb_talk(d, all_pids, sizeof(all_pids),
216  rbuff, sizeof(rbuff));
217  /*Restart Stream Command*/
218  ret = lme2510_usb_talk(d, stream_on, sizeof(stream_on),
219  rbuff, sizeof(rbuff));
220  return ret;
221 }
222 
223 static int lme2510_enable_pid(struct dvb_usb_device *d, u8 index, u16 pid_out)
224 {
225  struct lme2510_state *st = d->priv;
226  static u8 pid_buff[] = LME_ZERO_PID;
227  static u8 rbuf[1];
228  u8 pid_no = index * 2;
229  u8 pid_len = pid_no + 2;
230  int ret = 0;
231  deb_info(1, "PID Setting Pid %04x", pid_out);
232 
233  if (st->pid_size == 0)
234  ret |= lme2510_stream_restart(d);
235 
236  pid_buff[2] = pid_no;
237  pid_buff[3] = (u8)pid_out & 0xff;
238  pid_buff[4] = pid_no + 1;
239  pid_buff[5] = (u8)(pid_out >> 8);
240 
241  if (pid_len > st->pid_size)
242  st->pid_size = pid_len;
243  pid_buff[7] = 0x80 + st->pid_size;
244 
245  ret |= lme2510_usb_talk(d, pid_buff ,
246  sizeof(pid_buff) , rbuf, sizeof(rbuf));
247 
248  if (st->stream_on)
249  ret |= lme2510_stream_restart(d);
250 
251  return ret;
252 }
253 
254 static void lme2510_int_response(struct urb *lme_urb)
255 {
256  struct dvb_usb_adapter *adap = lme_urb->context;
257  struct lme2510_state *st = adap_to_priv(adap);
258  static u8 *ibuf, *rbuf;
259  int i = 0, offset;
260  u32 key;
261 
262  switch (lme_urb->status) {
263  case 0:
264  case -ETIMEDOUT:
265  break;
266  case -ECONNRESET:
267  case -ENOENT:
268  case -ESHUTDOWN:
269  return;
270  default:
271  info("Error %x", lme_urb->status);
272  break;
273  }
274 
275  rbuf = (u8 *) lme_urb->transfer_buffer;
276 
277  offset = ((lme_urb->actual_length/8) > 4)
278  ? 4 : (lme_urb->actual_length/8) ;
279 
280  for (i = 0; i < offset; ++i) {
281  ibuf = (u8 *)&rbuf[i*8];
282  deb_info(5, "INT O/S C =%02x C/O=%02x Type =%02x%02x",
283  offset, i, ibuf[0], ibuf[1]);
284 
285  switch (ibuf[0]) {
286  case 0xaa:
287  debug_data_snipet(1, "INT Remote data snipet", ibuf);
288  if ((ibuf[4] + ibuf[5]) == 0xff) {
289  key = ibuf[5];
290  key += (ibuf[3] > 0)
291  ? (ibuf[3] ^ 0xff) << 8 : 0;
292  key += (ibuf[2] ^ 0xff) << 16;
293  deb_info(1, "INT Key =%08x", key);
294  if (adap_to_d(adap)->rc_dev != NULL)
295  rc_keydown(adap_to_d(adap)->rc_dev,
296  key, 0);
297  }
298  break;
299  case 0xbb:
300  switch (st->tuner_config) {
301  case TUNER_LG:
302  if (ibuf[2] > 0)
303  st->signal_lock = ibuf[2];
304  st->signal_level = ibuf[4];
305  st->signal_sn = ibuf[3];
306  st->time_key = ibuf[7];
307  break;
308  case TUNER_S7395:
309  case TUNER_S0194:
310  /* Tweak for earlier firmware*/
311  if (ibuf[1] == 0x03) {
312  if (ibuf[2] > 1)
313  st->signal_lock = ibuf[2];
314  st->signal_level = ibuf[3];
315  st->signal_sn = ibuf[4];
316  } else {
317  st->signal_level = ibuf[4];
318  st->signal_sn = ibuf[5];
319  st->signal_lock =
320  (st->signal_lock & 0xf7) +
321  ((ibuf[2] & 0x01) << 0x03);
322  }
323  break;
324  case TUNER_RS2000:
325  if (ibuf[1] == 0x3 && ibuf[6] == 0xff)
326  st->signal_lock = 0xff;
327  else
328  st->signal_lock = 0x00;
329  st->signal_level = ibuf[5];
330  st->signal_sn = ibuf[4];
331  st->time_key = ibuf[7];
332  default:
333  break;
334  }
335  debug_data_snipet(5, "INT Remote data snipet in", ibuf);
336  break;
337  case 0xcc:
338  debug_data_snipet(1, "INT Control data snipet", ibuf);
339  break;
340  default:
341  debug_data_snipet(1, "INT Unknown data snipet", ibuf);
342  break;
343  }
344  }
345  usb_submit_urb(lme_urb, GFP_ATOMIC);
346 }
347 
348 static int lme2510_int_read(struct dvb_usb_adapter *adap)
349 {
350  struct dvb_usb_device *d = adap_to_d(adap);
351  struct lme2510_state *lme_int = adap_to_priv(adap);
352 
353  lme_int->lme_urb = usb_alloc_urb(0, GFP_ATOMIC);
354 
355  if (lme_int->lme_urb == NULL)
356  return -ENOMEM;
357 
358  lme_int->buffer = usb_alloc_coherent(d->udev, 128, GFP_ATOMIC,
359  &lme_int->lme_urb->transfer_dma);
360 
361  if (lme_int->buffer == NULL)
362  return -ENOMEM;
363 
364  usb_fill_int_urb(lme_int->lme_urb,
365  d->udev,
366  usb_rcvintpipe(d->udev, 0xa),
367  lme_int->buffer,
368  128,
369  lme2510_int_response,
370  adap,
371  8);
372 
373  lme_int->lme_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
374 
375  usb_submit_urb(lme_int->lme_urb, GFP_ATOMIC);
376  info("INT Interrupt Service Started");
377 
378  return 0;
379 }
380 
381 static int lme2510_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
382 {
383  struct dvb_usb_device *d = adap_to_d(adap);
384  struct lme2510_state *st = adap_to_priv(adap);
385  static u8 clear_pid_reg[] = LME_ALL_PIDS;
386  static u8 rbuf[1];
387  int ret = 0;
388 
389  deb_info(1, "PID Clearing Filter");
390 
391  mutex_lock(&d->i2c_mutex);
392 
393  if (!onoff) {
394  ret |= lme2510_usb_talk(d, clear_pid_reg,
395  sizeof(clear_pid_reg), rbuf, sizeof(rbuf));
396  st->pid_off = true;
397  } else
398  st->pid_off = false;
399 
400  st->pid_size = 0;
401 
402  mutex_unlock(&d->i2c_mutex);
403 
404  return 0;
405 }
406 
407 static int lme2510_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
408  int onoff)
409 {
410  struct dvb_usb_device *d = adap_to_d(adap);
411  int ret = 0;
412 
413  deb_info(3, "%s PID=%04x Index=%04x onoff=%02x", __func__,
414  pid, index, onoff);
415 
416  if (onoff) {
417  mutex_lock(&d->i2c_mutex);
418  ret |= lme2510_enable_pid(d, index, pid);
419  mutex_unlock(&d->i2c_mutex);
420  }
421 
422 
423  return ret;
424 }
425 
426 
427 static int lme2510_return_status(struct dvb_usb_device *d)
428 {
429  int ret = 0;
430  u8 *data;
431 
432  data = kzalloc(10, GFP_KERNEL);
433  if (!data)
434  return -ENOMEM;
435 
436  ret |= usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0),
437  0x06, 0x80, 0x0302, 0x00, data, 0x0006, 200);
438  info("Firmware Status: %x (%x)", ret , data[2]);
439 
440  ret = (ret < 0) ? -ENODEV : data[2];
441  kfree(data);
442  return ret;
443 }
444 
445 static int lme2510_msg(struct dvb_usb_device *d,
446  u8 *wbuf, int wlen, u8 *rbuf, int rlen)
447 {
448  int ret = 0;
449  struct lme2510_state *st = d->priv;
450 
451  if (st->i2c_talk_onoff == 1) {
452 
453  ret = lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
454 
455  switch (st->tuner_config) {
456  case TUNER_LG:
457  if (wbuf[2] == 0x1c) {
458  if (wbuf[3] == 0x0e) {
459  st->signal_lock = rbuf[1];
460  if ((st->stream_on & 1) &&
461  (st->signal_lock & 0x10)) {
462  lme2510_stream_restart(d);
463  st->i2c_talk_onoff = 0;
464  }
465  msleep(80);
466  }
467  }
468  break;
469  case TUNER_S7395:
470  if (wbuf[2] == 0xd0) {
471  if (wbuf[3] == 0x24) {
472  st->signal_lock = rbuf[1];
473  if ((st->stream_on & 1) &&
474  (st->signal_lock & 0x8)) {
475  lme2510_stream_restart(d);
476  st->i2c_talk_onoff = 0;
477  }
478  }
479  }
480  break;
481  case TUNER_S0194:
482  if (wbuf[2] == 0xd0) {
483  if (wbuf[3] == 0x1b) {
484  st->signal_lock = rbuf[1];
485  if ((st->stream_on & 1) &&
486  (st->signal_lock & 0x8)) {
487  lme2510_stream_restart(d);
488  st->i2c_talk_onoff = 0;
489  }
490  }
491  }
492  break;
493  case TUNER_RS2000:
494  default:
495  break;
496  }
497  } else {
498  /* TODO rewrite this section */
499  switch (st->tuner_config) {
500  case TUNER_LG:
501  switch (wbuf[3]) {
502  case 0x0e:
503  rbuf[0] = 0x55;
504  rbuf[1] = st->signal_lock;
505  break;
506  case 0x43:
507  rbuf[0] = 0x55;
508  rbuf[1] = st->signal_level;
509  break;
510  case 0x1c:
511  rbuf[0] = 0x55;
512  rbuf[1] = st->signal_sn;
513  break;
514  case 0x15:
515  case 0x16:
516  case 0x17:
517  case 0x18:
518  rbuf[0] = 0x55;
519  rbuf[1] = 0x00;
520  break;
521  default:
522  lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
523  st->i2c_talk_onoff = 1;
524  break;
525  }
526  break;
527  case TUNER_S7395:
528  switch (wbuf[3]) {
529  case 0x10:
530  rbuf[0] = 0x55;
531  rbuf[1] = (st->signal_level & 0x80)
532  ? 0 : (st->signal_level * 2);
533  break;
534  case 0x2d:
535  rbuf[0] = 0x55;
536  rbuf[1] = st->signal_sn;
537  break;
538  case 0x24:
539  rbuf[0] = 0x55;
540  rbuf[1] = st->signal_lock;
541  break;
542  case 0x2e:
543  case 0x26:
544  case 0x27:
545  rbuf[0] = 0x55;
546  rbuf[1] = 0x00;
547  break;
548  default:
549  lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
550  st->i2c_talk_onoff = 1;
551  break;
552  }
553  break;
554  case TUNER_S0194:
555  switch (wbuf[3]) {
556  case 0x18:
557  rbuf[0] = 0x55;
558  rbuf[1] = (st->signal_level & 0x80)
559  ? 0 : (st->signal_level * 2);
560  break;
561  case 0x24:
562  rbuf[0] = 0x55;
563  rbuf[1] = st->signal_sn;
564  break;
565  case 0x1b:
566  rbuf[0] = 0x55;
567  rbuf[1] = st->signal_lock;
568  break;
569  case 0x19:
570  case 0x25:
571  case 0x1e:
572  case 0x1d:
573  rbuf[0] = 0x55;
574  rbuf[1] = 0x00;
575  break;
576  default:
577  lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
578  st->i2c_talk_onoff = 1;
579  break;
580  }
581  break;
582  case TUNER_RS2000:
583  switch (wbuf[3]) {
584  case 0x8c:
585  rbuf[0] = 0x55;
586  rbuf[1] = 0xff;
587  if (st->last_key == st->time_key) {
588  st->key_timeout++;
589  if (st->key_timeout > 5)
590  rbuf[1] = 0;
591  } else
592  st->key_timeout = 0;
593  st->last_key = st->time_key;
594  break;
595  default:
596  lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
597  st->i2c_talk_onoff = 1;
598  break;
599  }
600  default:
601  break;
602  }
603 
604  deb_info(4, "I2C From Interrupt Message out(%02x) in(%02x)",
605  wbuf[3], rbuf[1]);
606 
607  }
608 
609  return ret;
610 }
611 
612 
613 static int lme2510_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
614  int num)
615 {
616  struct dvb_usb_device *d = i2c_get_adapdata(adap);
617  struct lme2510_state *st = d->priv;
618  static u8 obuf[64], ibuf[64];
619  int i, read, read_o;
620  u16 len;
621  u8 gate = st->i2c_gate;
622 
623  mutex_lock(&d->i2c_mutex);
624 
625  if (gate == 0)
626  gate = 5;
627 
628  for (i = 0; i < num; i++) {
629  read_o = 1 & (msg[i].flags & I2C_M_RD);
630  read = i+1 < num && (msg[i+1].flags & I2C_M_RD);
631  read |= read_o;
632  gate = (msg[i].addr == st->i2c_tuner_addr)
633  ? (read) ? st->i2c_tuner_gate_r
634  : st->i2c_tuner_gate_w
635  : st->i2c_gate;
636  obuf[0] = gate | (read << 7);
637 
638  if (gate == 5)
639  obuf[1] = (read) ? 2 : msg[i].len + 1;
640  else
641  obuf[1] = msg[i].len + read + 1;
642 
643  obuf[2] = msg[i].addr;
644  if (read) {
645  if (read_o)
646  len = 3;
647  else {
648  memcpy(&obuf[3], msg[i].buf, msg[i].len);
649  obuf[msg[i].len+3] = msg[i+1].len;
650  len = msg[i].len+4;
651  }
652  } else {
653  memcpy(&obuf[3], msg[i].buf, msg[i].len);
654  len = msg[i].len+3;
655  }
656 
657  if (lme2510_msg(d, obuf, len, ibuf, 64) < 0) {
658  deb_info(1, "i2c transfer failed.");
659  mutex_unlock(&d->i2c_mutex);
660  return -EAGAIN;
661  }
662 
663  if (read) {
664  if (read_o)
665  memcpy(msg[i].buf, &ibuf[1], msg[i].len);
666  else {
667  memcpy(msg[i+1].buf, &ibuf[1], msg[i+1].len);
668  i++;
669  }
670  }
671  }
672 
673  mutex_unlock(&d->i2c_mutex);
674  return i;
675 }
676 
677 static u32 lme2510_i2c_func(struct i2c_adapter *adapter)
678 {
679  return I2C_FUNC_I2C;
680 }
681 
682 static struct i2c_algorithm lme2510_i2c_algo = {
683  .master_xfer = lme2510_i2c_xfer,
684  .functionality = lme2510_i2c_func,
685 };
686 
687 static int lme2510_streaming_ctrl(struct dvb_frontend *fe, int onoff)
688 {
689  struct dvb_usb_adapter *adap = fe_to_adap(fe);
690  struct dvb_usb_device *d = adap_to_d(adap);
691  struct lme2510_state *st = adap_to_priv(adap);
692  static u8 clear_reg_3[] = LME_ALL_PIDS;
693  static u8 rbuf[1];
694  int ret = 0, rlen = sizeof(rbuf);
695 
696  deb_info(1, "STM (%02x)", onoff);
697 
698  /* Streaming is started by FE_HAS_LOCK */
699  if (onoff == 1)
700  st->stream_on = 1;
701  else {
702  deb_info(1, "STM Steam Off");
703  /* mutex is here only to avoid collision with I2C */
704  mutex_lock(&d->i2c_mutex);
705 
706  ret = lme2510_usb_talk(d, clear_reg_3,
707  sizeof(clear_reg_3), rbuf, rlen);
708  st->stream_on = 0;
709  st->i2c_talk_onoff = 1;
710 
711  mutex_unlock(&d->i2c_mutex);
712  }
713 
714  return (ret < 0) ? -ENODEV : 0;
715 }
716 
717 static u8 check_sum(u8 *p, u8 len)
718 {
719  u8 sum = 0;
720  while (len--)
721  sum += *p++;
722  return sum;
723 }
724 
725 static int lme2510_download_firmware(struct dvb_usb_device *d,
726  const struct firmware *fw)
727 {
728  int ret = 0;
729  u8 *data;
730  u16 j, wlen, len_in, start, end;
731  u8 packet_size, dlen, i;
732  u8 *fw_data;
733 
734  packet_size = 0x31;
735  len_in = 1;
736 
737  data = kzalloc(128, GFP_KERNEL);
738  if (!data) {
739  info("FRM Could not start Firmware Download"\
740  "(Buffer allocation failed)");
741  return -ENOMEM;
742  }
743 
744  info("FRM Starting Firmware Download");
745 
746  for (i = 1; i < 3; i++) {
747  start = (i == 1) ? 0 : 512;
748  end = (i == 1) ? 512 : fw->size;
749  for (j = start; j < end; j += (packet_size+1)) {
750  fw_data = (u8 *)(fw->data + j);
751  if ((end - j) > packet_size) {
752  data[0] = i;
753  dlen = packet_size;
754  } else {
755  data[0] = i | 0x80;
756  dlen = (u8)(end - j)-1;
757  }
758  data[1] = dlen;
759  memcpy(&data[2], fw_data, dlen+1);
760  wlen = (u8) dlen + 4;
761  data[wlen-1] = check_sum(fw_data, dlen+1);
762  deb_info(1, "Data S=%02x:E=%02x CS= %02x", data[3],
763  data[dlen+2], data[dlen+3]);
764  lme2510_usb_talk(d, data, wlen, data, len_in);
765  ret |= (data[0] == 0x88) ? 0 : -1;
766  }
767  }
768 
769  data[0] = 0x8a;
770  len_in = 1;
771  msleep(2000);
772  lme2510_usb_talk(d, data, len_in, data, len_in);
773  msleep(400);
774 
775  if (ret < 0)
776  info("FRM Firmware Download Failed (%04x)" , ret);
777  else
778  info("FRM Firmware Download Completed - Resetting Device");
779 
780  kfree(data);
781  return RECONNECTS_USB;
782 }
783 
784 static void lme_coldreset(struct dvb_usb_device *d)
785 {
786  u8 data[1] = {0};
787  data[0] = 0x0a;
788  info("FRM Firmware Cold Reset");
789 
790  lme2510_usb_talk(d, data, sizeof(data), data, sizeof(data));
791 
792  return;
793 }
794 
795 static const char fw_c_s7395[] = LME2510_C_S7395;
796 static const char fw_c_lg[] = LME2510_C_LG;
797 static const char fw_c_s0194[] = LME2510_C_S0194;
798 static const char fw_c_rs2000[] = LME2510_C_RS2000;
799 static const char fw_lg[] = LME2510_LG;
800 static const char fw_s0194[] = LME2510_S0194;
801 
802 const char *lme_firmware_switch(struct dvb_usb_device *d, int cold)
803 {
804  struct lme2510_state *st = d->priv;
805  struct usb_device *udev = d->udev;
806  const struct firmware *fw = NULL;
807  const char *fw_lme;
808  int ret = 0;
809 
810  cold = (cold > 0) ? (cold & 1) : 0;
811 
812  switch (le16_to_cpu(udev->descriptor.idProduct)) {
813  case 0x1122:
814  switch (st->dvb_usb_lme2510_firmware) {
815  default:
817  case TUNER_S0194:
818  fw_lme = fw_s0194;
819  ret = request_firmware(&fw, fw_lme, &udev->dev);
820  if (ret == 0) {
821  cold = 0;
822  break;
823  }
825  case TUNER_LG:
826  fw_lme = fw_lg;
827  ret = request_firmware(&fw, fw_lme, &udev->dev);
828  if (ret == 0)
829  break;
831  break;
832  }
833  break;
834  case 0x1120:
835  switch (st->dvb_usb_lme2510_firmware) {
836  default:
838  case TUNER_S7395:
839  fw_lme = fw_c_s7395;
840  ret = request_firmware(&fw, fw_lme, &udev->dev);
841  if (ret == 0) {
842  cold = 0;
843  break;
844  }
846  case TUNER_LG:
847  fw_lme = fw_c_lg;
848  ret = request_firmware(&fw, fw_lme, &udev->dev);
849  if (ret == 0)
850  break;
852  case TUNER_S0194:
853  fw_lme = fw_c_s0194;
854  ret = request_firmware(&fw, fw_lme, &udev->dev);
855  if (ret == 0)
856  break;
858  cold = 0;
859  break;
860  }
861  break;
862  case 0x22f0:
863  fw_lme = fw_c_rs2000;
865  break;
866  default:
867  fw_lme = fw_c_s7395;
868  }
869 
870  release_firmware(fw);
871 
872  if (cold) {
873  dvb_usb_lme2510_firmware = st->dvb_usb_lme2510_firmware;
874  info("FRM Changing to %s firmware", fw_lme);
875  lme_coldreset(d);
876  return NULL;
877  }
878 
879  return fw_lme;
880 }
881 
882 static int lme2510_kill_urb(struct usb_data_stream *stream)
883 {
884  int i;
885 
886  for (i = 0; i < stream->urbs_submitted; i++) {
887  deb_info(3, "killing URB no. %d.", i);
888  /* stop the URB */
889  usb_kill_urb(stream->urb_list[i]);
890  }
891  stream->urbs_submitted = 0;
892 
893  return 0;
894 }
895 
896 static struct tda10086_config tda10086_config = {
897  .demod_address = 0x1c,
898  .invert = 0,
899  .diseqc_tone = 1,
900  .xtal_freq = TDA10086_XTAL_16M,
901 };
902 
903 static struct stv0288_config lme_config = {
904  .demod_address = 0xd0,
905  .min_delay_ms = 15,
906  .inittab = s7395_inittab,
907 };
908 
909 static struct ix2505v_config lme_tuner = {
910  .tuner_address = 0xc0,
911  .min_delay_ms = 100,
912  .tuner_gain = 0x0,
913  .tuner_chargepump = 0x3,
914 };
915 
916 static struct stv0299_config sharp_z0194_config = {
917  .demod_address = 0xd0,
918  .inittab = sharp_z0194a_inittab,
919  .mclk = 88000000UL,
920  .invert = 0,
921  .skip_reinit = 0,
922  .lock_output = STV0299_LOCKOUTPUT_1,
923  .volt13_op0_op1 = STV0299_VOLT13_OP1,
924  .min_delay_ms = 100,
925  .set_symbol_rate = sharp_z0194a_set_symbol_rate,
926 };
927 
928 static int dm04_rs2000_set_ts_param(struct dvb_frontend *fe,
929  int caller)
930 {
931  struct dvb_usb_adapter *adap = fe_to_adap(fe);
932  struct dvb_usb_device *d = adap_to_d(adap);
933  struct lme2510_state *st = d->priv;
934 
935  mutex_lock(&d->i2c_mutex);
936  if ((st->i2c_talk_onoff == 1) && (st->stream_on & 1)) {
937  st->i2c_talk_onoff = 0;
938  lme2510_stream_restart(d);
939  }
940  mutex_unlock(&d->i2c_mutex);
941 
942  return 0;
943 }
944 
945 static struct m88rs2000_config m88rs2000_config = {
946  .demod_addr = 0xd0,
947  .tuner_addr = 0xc0,
948  .set_ts_params = dm04_rs2000_set_ts_param,
949 };
950 
951 static int dm04_lme2510_set_voltage(struct dvb_frontend *fe,
952  fe_sec_voltage_t voltage)
953 {
954  struct dvb_usb_device *d = fe_to_d(fe);
955  struct lme2510_state *st = fe_to_priv(fe);
956  static u8 voltage_low[] = LME_VOLTAGE_L;
957  static u8 voltage_high[] = LME_VOLTAGE_H;
958  static u8 rbuf[1];
959  int ret = 0, len = 3, rlen = 1;
960 
961  mutex_lock(&d->i2c_mutex);
962 
963  switch (voltage) {
964  case SEC_VOLTAGE_18:
965  ret |= lme2510_usb_talk(d,
966  voltage_high, len, rbuf, rlen);
967  break;
968 
969  case SEC_VOLTAGE_OFF:
970  case SEC_VOLTAGE_13:
971  default:
972  ret |= lme2510_usb_talk(d,
973  voltage_low, len, rbuf, rlen);
974  break;
975  }
976 
977  mutex_unlock(&d->i2c_mutex);
978 
979  if (st->tuner_config == TUNER_RS2000)
980  if (st->fe_set_voltage)
981  st->fe_set_voltage(fe, voltage);
982 
983 
984  return (ret < 0) ? -ENODEV : 0;
985 }
986 
987 static int dm04_rs2000_read_signal_strength(struct dvb_frontend *fe,
988  u16 *strength)
989 {
990  struct lme2510_state *st = fe_to_priv(fe);
991 
992  *strength = (u16)((u32)st->signal_level * 0xffff / 0xff);
993 
994  return 0;
995 }
996 
997 static int dm04_rs2000_read_snr(struct dvb_frontend *fe, u16 *snr)
998 {
999  struct lme2510_state *st = fe_to_priv(fe);
1000 
1001  *snr = (u16)((u32)st->signal_sn * 0xffff / 0x7f);
1002 
1003  return 0;
1004 }
1005 
1006 static int dm04_read_ber(struct dvb_frontend *fe, u32 *ber)
1007 {
1008  *ber = 0;
1009 
1010  return 0;
1011 }
1012 
1013 static int dm04_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
1014 {
1015  *ucblocks = 0;
1016 
1017  return 0;
1018 }
1019 
1020 static int lme_name(struct dvb_usb_adapter *adap)
1021 {
1022  struct dvb_usb_device *d = adap_to_d(adap);
1023  struct lme2510_state *st = adap_to_priv(adap);
1024  const char *desc = d->name;
1025  char *fe_name[] = {"", " LG TDQY-P001F", " SHARP:BS2F7HZ7395",
1026  " SHARP:BS2F7HZ0194", " RS2000"};
1027  char *name = adap->fe[0]->ops.info.name;
1028 
1029  strlcpy(name, desc, 128);
1030  strlcat(name, fe_name[st->tuner_config], 128);
1031 
1032  return 0;
1033 }
1034 
1035 static int dm04_lme2510_frontend_attach(struct dvb_usb_adapter *adap)
1036 {
1037  struct dvb_usb_device *d = adap_to_d(adap);
1038  struct lme2510_state *st = d->priv;
1039  int ret = 0;
1040 
1041  st->i2c_talk_onoff = 1;
1042  switch (le16_to_cpu(d->udev->descriptor.idProduct)) {
1043  case 0x1122:
1044  case 0x1120:
1045  st->i2c_gate = 4;
1046  adap->fe[0] = dvb_attach(tda10086_attach,
1047  &tda10086_config, &d->i2c_adap);
1048  if (adap->fe[0]) {
1049  info("TUN Found Frontend TDA10086");
1050  st->i2c_tuner_gate_w = 4;
1051  st->i2c_tuner_gate_r = 4;
1052  st->i2c_tuner_addr = 0xc0;
1053  st->tuner_config = TUNER_LG;
1054  if (st->dvb_usb_lme2510_firmware != TUNER_LG) {
1056  ret = lme_firmware_switch(d, 1) ? 0 : -ENODEV;
1057  }
1058  break;
1059  }
1060 
1061  st->i2c_gate = 4;
1062  adap->fe[0] = dvb_attach(stv0299_attach,
1063  &sharp_z0194_config, &d->i2c_adap);
1064  if (adap->fe[0]) {
1065  info("FE Found Stv0299");
1066  st->i2c_tuner_gate_w = 4;
1067  st->i2c_tuner_gate_r = 5;
1068  st->i2c_tuner_addr = 0xc0;
1069  st->tuner_config = TUNER_S0194;
1072  ret = lme_firmware_switch(d, 1) ? 0 : -ENODEV;
1073  }
1074  break;
1075  }
1076 
1077  st->i2c_gate = 5;
1078  adap->fe[0] = dvb_attach(stv0288_attach, &lme_config,
1079  &d->i2c_adap);
1080 
1081  if (adap->fe[0]) {
1082  info("FE Found Stv0288");
1083  st->i2c_tuner_gate_w = 4;
1084  st->i2c_tuner_gate_r = 5;
1085  st->i2c_tuner_addr = 0xc0;
1086  st->tuner_config = TUNER_S7395;
1089  ret = lme_firmware_switch(d, 1) ? 0 : -ENODEV;
1090  }
1091  break;
1092  }
1093  case 0x22f0:
1094  st->i2c_gate = 5;
1095  adap->fe[0] = dvb_attach(m88rs2000_attach,
1096  &m88rs2000_config, &d->i2c_adap);
1097 
1098  if (adap->fe[0]) {
1099  info("FE Found M88RS2000");
1100  st->i2c_tuner_gate_w = 5;
1101  st->i2c_tuner_gate_r = 5;
1102  st->i2c_tuner_addr = 0xc0;
1103  st->tuner_config = TUNER_RS2000;
1104  st->fe_set_voltage =
1105  adap->fe[0]->ops.set_voltage;
1106 
1107  adap->fe[0]->ops.read_signal_strength =
1108  dm04_rs2000_read_signal_strength;
1109  adap->fe[0]->ops.read_snr =
1110  dm04_rs2000_read_snr;
1111  adap->fe[0]->ops.read_ber =
1112  dm04_read_ber;
1113  adap->fe[0]->ops.read_ucblocks =
1114  dm04_read_ucblocks;
1115  }
1116  break;
1117  }
1118 
1119  if (adap->fe[0] == NULL) {
1120  info("DM04/QQBOX Not Powered up or not Supported");
1121  return -ENODEV;
1122  }
1123 
1124  if (ret) {
1125  if (adap->fe[0]) {
1126  dvb_frontend_detach(adap->fe[0]);
1127  adap->fe[0] = NULL;
1128  }
1129  d->rc_map = NULL;
1130  return -ENODEV;
1131  }
1132 
1133  adap->fe[0]->ops.set_voltage = dm04_lme2510_set_voltage;
1134  ret = lme_name(adap);
1135  return ret;
1136 }
1137 
1138 static int dm04_lme2510_tuner(struct dvb_usb_adapter *adap)
1139 {
1140  struct dvb_usb_device *d = adap_to_d(adap);
1141  struct lme2510_state *st = adap_to_priv(adap);
1142  char *tun_msg[] = {"", "TDA8263", "IX2505V", "DVB_PLL_OPERA", "RS2000"};
1143  int ret = 0;
1144 
1145  switch (st->tuner_config) {
1146  case TUNER_LG:
1147  if (dvb_attach(tda826x_attach, adap->fe[0], 0xc0,
1148  &d->i2c_adap, 1))
1149  ret = st->tuner_config;
1150  break;
1151  case TUNER_S7395:
1152  if (dvb_attach(ix2505v_attach , adap->fe[0], &lme_tuner,
1153  &d->i2c_adap))
1154  ret = st->tuner_config;
1155  break;
1156  case TUNER_S0194:
1157  if (dvb_attach(dvb_pll_attach , adap->fe[0], 0xc0,
1158  &d->i2c_adap, DVB_PLL_OPERA1))
1159  ret = st->tuner_config;
1160  break;
1161  case TUNER_RS2000:
1162  ret = st->tuner_config;
1163  break;
1164  default:
1165  break;
1166  }
1167 
1168  if (ret)
1169  info("TUN Found %s tuner", tun_msg[ret]);
1170  else {
1171  info("TUN No tuner found --- resetting device");
1172  lme_coldreset(d);
1173  return -ENODEV;
1174  }
1175 
1176  /* Start the Interrupt*/
1177  ret = lme2510_int_read(adap);
1178  if (ret < 0) {
1179  info("INT Unable to start Interrupt Service");
1180  return -ENODEV;
1181  }
1182 
1183  return ret;
1184 }
1185 
1186 static int lme2510_powerup(struct dvb_usb_device *d, int onoff)
1187 {
1188  struct lme2510_state *st = d->priv;
1189  static u8 lnb_on[] = LNB_ON;
1190  static u8 lnb_off[] = LNB_OFF;
1191  static u8 rbuf[1];
1192  int ret = 0, len = 3, rlen = 1;
1193 
1194  mutex_lock(&d->i2c_mutex);
1195 
1196  if (onoff)
1197  ret = lme2510_usb_talk(d, lnb_on, len, rbuf, rlen);
1198  else
1199  ret = lme2510_usb_talk(d, lnb_off, len, rbuf, rlen);
1200 
1201  st->i2c_talk_onoff = 1;
1202 
1203  mutex_unlock(&d->i2c_mutex);
1204 
1205  return ret;
1206 }
1207 
1208 static int lme2510_get_adapter_count(struct dvb_usb_device *d)
1209 {
1210  return 1;
1211 }
1212 
1213 static int lme2510_identify_state(struct dvb_usb_device *d, const char **name)
1214 {
1215  struct lme2510_state *st = d->priv;
1216 
1218 
1220  d->intf->cur_altsetting->desc.bInterfaceNumber, 1);
1221 
1223 
1224  if (lme2510_return_status(d) == 0x44) {
1225  *name = lme_firmware_switch(d, 0);
1226  return COLD;
1227  }
1228 
1229  return 0;
1230 }
1231 
1232 static int lme2510_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
1233  struct usb_data_stream_properties *stream)
1234 {
1235  struct dvb_usb_adapter *adap = fe_to_adap(fe);
1236  struct dvb_usb_device *d = adap_to_d(adap);
1237 
1238  if (adap == NULL)
1239  return 0;
1240  /* Turn PID filter on the fly by module option */
1241  if (pid_filter == 2) {
1242  adap->pid_filtering = 1;
1243  adap->max_feed_count = 15;
1244  }
1245 
1246  if (!(le16_to_cpu(d->udev->descriptor.idProduct)
1247  == 0x1122))
1248  stream->endpoint = 0x8;
1249 
1250  return 0;
1251 }
1252 
1253 static int lme2510_get_rc_config(struct dvb_usb_device *d,
1254  struct dvb_usb_rc *rc)
1255 {
1257  return 0;
1258 }
1259 
1260 static void *lme2510_exit_int(struct dvb_usb_device *d)
1261 {
1262  struct lme2510_state *st = d->priv;
1263  struct dvb_usb_adapter *adap = &d->adapter[0];
1264  void *buffer = NULL;
1265 
1266  if (adap != NULL) {
1267  lme2510_kill_urb(&adap->stream);
1268  }
1269 
1270  if (st->usb_buffer != NULL) {
1271  st->i2c_talk_onoff = 1;
1272  st->signal_lock = 0;
1273  st->signal_level = 0;
1274  st->signal_sn = 0;
1275  buffer = st->usb_buffer;
1276  }
1277 
1278  if (st->lme_urb != NULL) {
1279  usb_kill_urb(st->lme_urb);
1280  usb_free_coherent(d->udev, 128, st->buffer,
1281  st->lme_urb->transfer_dma);
1282  info("Interrupt Service Stopped");
1283  }
1284 
1285  return buffer;
1286 }
1287 
1288 static void lme2510_exit(struct dvb_usb_device *d)
1289 {
1290  void *usb_buffer;
1291 
1292  if (d != NULL) {
1293  usb_buffer = lme2510_exit_int(d);
1294  if (usb_buffer != NULL)
1295  kfree(usb_buffer);
1296  }
1297 }
1298 
1299 static struct dvb_usb_device_properties lme2510_props = {
1300  .driver_name = KBUILD_MODNAME,
1301  .owner = THIS_MODULE,
1302  .bInterfaceNumber = 0,
1303  .adapter_nr = adapter_nr,
1304  .size_of_priv = sizeof(struct lme2510_state),
1305 
1306  .download_firmware = lme2510_download_firmware,
1307 
1308  .power_ctrl = lme2510_powerup,
1309  .identify_state = lme2510_identify_state,
1310  .i2c_algo = &lme2510_i2c_algo,
1311 
1312  .frontend_attach = dm04_lme2510_frontend_attach,
1313  .tuner_attach = dm04_lme2510_tuner,
1314  .get_stream_config = lme2510_get_stream_config,
1315  .get_adapter_count = lme2510_get_adapter_count,
1316  .streaming_ctrl = lme2510_streaming_ctrl,
1317 
1318  .get_rc_config = lme2510_get_rc_config,
1319 
1320  .exit = lme2510_exit,
1321  .adapter = {
1322  {
1325  .pid_filter_count = 15,
1326  .pid_filter = lme2510_pid_filter,
1327  .pid_filter_ctrl = lme2510_pid_filter_ctrl,
1328  .stream =
1329  DVB_USB_STREAM_BULK(0x86, 10, 4096),
1330  },
1331  {
1332  }
1333  },
1334 };
1335 
1336 static const struct usb_device_id lme2510_id_table[] = {
1337  { DVB_USB_DEVICE(0x3344, 0x1122, &lme2510_props,
1338  "DM04_LME2510_DVB-S", RC_MAP_LME2510) },
1339  { DVB_USB_DEVICE(0x3344, 0x1120, &lme2510_props,
1340  "DM04_LME2510C_DVB-S", RC_MAP_LME2510) },
1341  { DVB_USB_DEVICE(0x3344, 0x22f0, &lme2510_props,
1342  "DM04_LME2510C_DVB-S RS2000", RC_MAP_LME2510) },
1343  {} /* Terminating entry */
1344 };
1345 
1346 MODULE_DEVICE_TABLE(usb, lme2510_id_table);
1347 
1348 static struct usb_driver lme2510_driver = {
1349  .name = KBUILD_MODNAME,
1350  .probe = dvb_usbv2_probe,
1351  .disconnect = dvb_usbv2_disconnect,
1352  .id_table = lme2510_id_table,
1353  .no_dynamic_id = 1,
1354  .soft_unbind = 1,
1355 };
1356 
1357 module_usb_driver(lme2510_driver);
1358 
1359 MODULE_AUTHOR("Malcolm Priestley <[email protected]>");
1360 MODULE_DESCRIPTION("LME2510(C) DVB-S USB2.0");
1361 MODULE_VERSION("2.06");
1362 MODULE_LICENSE("GPL");
1369