blob: cd26e7c1536a674d235430c80d8fe37a8b5bbd46 [file] [log] [blame]
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001/* DVB USB compliant linux driver for
2 *
3 * DM04/QQBOX DVB-S USB BOX LME2510C + SHARP:BS2F7HZ7395
Malcolm Priestleyab599a62010-10-16 16:44:43 -03004 * LME2510C + LG TDQY-P001F
Malcolm Priestley9d4540c2011-02-12 22:35:08 -03005 * LME2510C + BS2F7HZ0194
Malcolm Priestleyab599a62010-10-16 16:44:43 -03006 * LME2510 + LG TDQY-P001F
Malcolm Priestley9d4540c2011-02-12 22:35:08 -03007 * LME2510 + BS2F7HZ0194
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03008 *
9 * MVB7395 (LME2510C+SHARP:BS2F7HZ7395)
10 * SHARP:BS2F7HZ7395 = (STV0288+Sharp IX2505V)
11 *
Malcolm Priestleyab599a62010-10-16 16:44:43 -030012 * MV001F (LME2510+LGTDQY-P001F)
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -030013 * LG TDQY - P001F =(TDA8263 + TDA10086H)
14 *
Malcolm Priestleyab599a62010-10-16 16:44:43 -030015 * MVB0001F (LME2510C+LGTDQT-P001F)
16 *
Malcolm Priestley9d4540c2011-02-12 22:35:08 -030017 * MV0194 (LME2510+SHARP:BS2F7HZ0194)
18 * SHARP:BS2F7HZ0194 = (STV0299+IX2410)
19 *
20 * MVB0194 (LME2510C+SHARP0194)
21 *
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -030022 * For firmware see Documentation/dvb/lmedm04.txt
23 *
24 * I2C addresses:
25 * 0xd0 - STV0288 - Demodulator
26 * 0xc0 - Sharp IX2505V - Tuner
Malcolm Priestley9d4540c2011-02-12 22:35:08 -030027 * --
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -030028 * 0x1c - TDA10086 - Demodulator
29 * 0xc0 - TDA8263 - Tuner
Malcolm Priestley9d4540c2011-02-12 22:35:08 -030030 * --
31 * 0xd0 - STV0299 - Demodulator
32 * 0xc0 - IX2410 - Tuner
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -030033 *
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 *
Malcolm Priestleyab599a62010-10-16 16:44:43 -030060 * QQbox suffers from noise on LNB voltage.
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -030061 *
Malcolm Priestley9d4540c2011-02-12 22:35:08 -030062 * LME2510: SHARP:BS2F7HZ0194(MV0194) cannot cold reset and share system
63 * with other tuners. After a cold reset streaming will not start.
64 *
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -030065 * 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>
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -030071#include <media/rc-core.h>
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -030072
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"
Malcolm Priestley9d4540c2011-02-12 22:35:08 -030079#include "stv0299.h"
80#include "dvb-pll.h"
81#include "z0194a.h"
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -030082
83
84
85/* debug */
86static 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
99module_param_named(debug, dvb_usb_lme2510_debug, int, 0644);
100MODULE_PARM_DESC(debug, "set debugging level (1=info (or-able))."
101 DVB_USB_DEBUG_STATUS);
102
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300103static int dvb_usb_lme2510_firmware;
104module_param_named(firmware, dvb_usb_lme2510_firmware, int, 0644);
105MODULE_PARM_DESC(firmware, "set default firmware 0=Sharp7395 1=LG");
106
107
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300108DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300109
110#define TUNER_DEFAULT 0x0
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300111#define TUNER_LG 0x1
112#define TUNER_S7395 0x2
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300113#define TUNER_S0194 0x3
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300114
115struct 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;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300127 u8 stream_on;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300128 void *buffer;
129 struct urb *lme_urb;
130 void *usb_buffer;
131
132};
133
134static int lme2510_bulk_write(struct usb_device *dev,
135 u8 *snd, int len, u8 pipe)
136{
137 int ret, actual_l;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300138
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300139 ret = usb_bulk_msg(dev, usb_sndbulkpipe(dev, pipe),
Malcolm Priestley25ad9842010-11-04 17:17:51 -0300140 snd, len , &actual_l, 100);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300141 return ret;
142}
143
144static 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),
Malcolm Priestley25ad9842010-11-04 17:17:51 -0300150 rev, len , &actual_l, 200);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300151 return ret;
152}
153
154static 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
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300173 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));
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300179
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300180 ret |= lme2510_bulk_write(d->udev, buff, wlen , 0x01);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300181
Malcolm Priestley25ad9842010-11-04 17:17:51 -0300182 msleep(10);
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300183
184 ret |= usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, 0x01));
185
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300186 ret |= lme2510_bulk_read(d->udev, buff, (rlen > 512) ?
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300187 512 : rlen , 0x01);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300188
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
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300197static int lme2510_stream_restart(struct dvb_usb_device *d)
198{
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300199 static u8 stream_on[] = LME_ST_ON_W;
200 int ret;
201 u8 rbuff[10];
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300202 /*Restart Stream Command*/
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300203 ret = lme2510_usb_talk(d, stream_on, sizeof(stream_on),
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300204 rbuff, sizeof(rbuff));
205 return ret;
206}
Malcolm Priestleyf8931f52011-01-23 18:22:45 -0300207static int lme2510_remote_keypress(struct dvb_usb_adapter *adap, u32 keypress)
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300208{
209 struct dvb_usb_device *d = adap->dev;
210
211 deb_info(1, "INT Key Keypress =%04x", keypress);
212
213 if (keypress > 0)
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300214 rc_keydown(d->rc_dev, keypress, 0);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300215
216 return 0;
217}
218
219static 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
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300244 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,
Malcolm Priestleyf8931f52011-01-23 18:22:45 -0300253 (u32)(ibuf[2] << 24) + (ibuf[3] << 16) +
254 (ibuf[4] << 8) + ibuf[5]);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300255 break;
256 case 0xbb:
257 switch (st->tuner_config) {
258 case TUNER_LG:
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300259 if (ibuf[2] > 0)
260 st->signal_lock = ibuf[2];
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300261 st->signal_level = ibuf[4];
262 st->signal_sn = ibuf[3];
263 st->time_key = ibuf[7];
264 break;
265 case TUNER_S7395:
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300266 case TUNER_S0194:
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300267 /* Tweak for earlier firmware*/
268 if (ibuf[1] == 0x03) {
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300269 if (ibuf[2] > 1)
270 st->signal_lock = ibuf[2];
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300271 st->signal_level = ibuf[3];
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300272 st->signal_sn = ibuf[4];
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300273 } else {
274 st->signal_level = ibuf[4];
275 st->signal_sn = ibuf[5];
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300276 st->signal_lock =
277 (st->signal_lock & 0xf7) +
278 ((ibuf[2] & 0x01) << 0x03);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300279 }
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
297static 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 Interupt Service Started");
325
326 return 0;
327}
328
329static int lme2510_return_status(struct usb_device *dev)
330{
331 int ret = 0;
332 u8 data[10] = {0};
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300333
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300334 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
341static 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
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300347 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300348 return -EAGAIN;
349
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300350 if (st->i2c_talk_onoff == 1) {
351
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300352 ret = lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
353
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300354 switch (st->tuner_config) {
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300355 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)) {
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300361 lme2510_stream_restart(d);
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300362 st->i2c_talk_onoff = 0;
363 }
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300364 msleep(80);
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300365 }
366 }
367 break;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300368 case TUNER_S7395:
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300369 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)) {
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300374 lme2510_stream_restart(d);
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300375 st->i2c_talk_onoff = 0;
376 }
377 }
378 if ((wbuf[3] != 0x6) & (wbuf[3] != 0x5))
379 msleep(5);
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300380 }
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300381 break;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300382 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;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300394 default:
395 break;
396 }
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300397 } 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;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300413 case 0x15:
414 case 0x16:
415 case 0x17:
416 case 0x18:
417 rbuf[0] = 0x55;
418 rbuf[1] = 0x00;
419 break;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300420 default:
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300421 lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
422 st->i2c_talk_onoff = 1;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300423 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;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300439 rbuf[1] = st->signal_lock;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300440 break;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300441 case 0x2e:
442 case 0x26:
443 case 0x27:
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300444 rbuf[0] = 0x55;
445 rbuf[1] = 0x00;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300446 break;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300447 default:
448 lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
449 st->i2c_talk_onoff = 1;
450 break;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300451 }
452 break;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300453 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;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300481 default:
482 break;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300483 }
484
485 deb_info(4, "I2C From Interupt Message out(%02x) in(%02x)",
486 wbuf[3], rbuf[1]);
487
488 }
489
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300490 mutex_unlock(&d->i2c_mutex);
491
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300492 return ret;
493}
494
495
496static 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
559static u32 lme2510_i2c_func(struct i2c_adapter *adapter)
560{
561 return I2C_FUNC_I2C;
562}
563
564static struct i2c_algorithm lme2510_i2c_algo = {
565 .master_xfer = lme2510_i2c_xfer,
566 .functionality = lme2510_i2c_func,
567};
568
569/* Callbacks for DVB USB */
570static 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{
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300575 *cold = 0;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300576 return 0;
577}
578
579static int lme2510_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
580{
581 struct lme2510_state *st = adap->dev->priv;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300582 static u8 clear_reg_3[] = LME_CLEAR_PID;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300583 static u8 rbuf[1];
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300584 int ret = 0, rlen = sizeof(rbuf);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300585
586 deb_info(1, "STM (%02x)", onoff);
587
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300588 /* Streaming is started by FE_HAS_LOCK */
589 if (onoff == 1)
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300590 st->stream_on = 1;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300591 else {
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300592 deb_info(1, "STM Steam Off");
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300593 /* mutex is here only to avoid collision with I2C */
594 ret = mutex_lock_interruptible(&adap->dev->i2c_mutex);
595
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300596 ret |= lme2510_usb_talk(adap->dev, clear_reg_3,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300597 sizeof(clear_reg_3), rbuf, rlen);
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300598 st->stream_on = 0;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300599 st->i2c_talk_onoff = 1;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300600
601 mutex_unlock(&adap->dev->i2c_mutex);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300602 }
603
604 return (ret < 0) ? -ENODEV : 0;
605}
606
607static int lme2510_int_service(struct dvb_usb_adapter *adap)
608{
609 struct dvb_usb_device *d = adap->dev;
David Härdemand8b4b582010-10-29 16:08:23 -0300610 struct rc_dev *rc;
611 int ret;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300612
613 info("STA Configuring Remote");
614
David Härdemand8b4b582010-10-29 16:08:23 -0300615 rc = rc_allocate_device();
616 if (!rc)
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300617 return -ENOMEM;
618
David Härdemand8b4b582010-10-29 16:08:23 -0300619 usb_make_path(d->udev, d->rc_phys, sizeof(d->rc_phys));
620 strlcat(d->rc_phys, "/ir0", sizeof(d->rc_phys));
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300621
David Härdemand8b4b582010-10-29 16:08:23 -0300622 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);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300627
David Härdemand8b4b582010-10-29 16:08:23 -0300628 ret = rc_register_device(rc);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300629 if (ret) {
David Härdemand8b4b582010-10-29 16:08:23 -0300630 rc_free_device(rc);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300631 return ret;
632 }
David Härdemand8b4b582010-10-29 16:08:23 -0300633 d->rc_dev = rc;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300634
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300635 /* Start the Interupt */
636 ret = lme2510_int_read(adap);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300637 if (ret < 0) {
David Härdemand8b4b582010-10-29 16:08:23 -0300638 rc_unregister_device(rc);
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300639 info("INT Unable to start Interupt Service");
David Härdemand8b4b582010-10-29 16:08:23 -0300640 return -ENODEV;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300641 }
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300642
David Härdemand8b4b582010-10-29 16:08:23 -0300643 return 0;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300644}
645
646static u8 check_sum(u8 *p, u8 len)
647{
648 u8 sum = 0;
649 while (len--)
650 sum += *p++;
651 return sum;
652}
653
654static 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 }
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300692
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300693 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
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300713static 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);
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300723
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300724 return;
725}
726
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300727static int lme_firmware_switch(struct usb_device *udev, int cold)
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300728{
729 const struct firmware *fw = NULL;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300730 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;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300737
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300738 cold = (cold > 0) ? (cold & 1) : 0;
739
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300740 cold_fw = !cold;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300741
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300742 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;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300761 cold = 0;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300762 cold_fw = 0;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300763 break;
764 }
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300765 } 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 }
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300792 }
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300793
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300794 if (cold_fw) {
795 info("FRM Loading %s file", fw_lme);
796 ret = lme2510_download_firmware(udev, fw);
797 }
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300798
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300799 if (cold) {
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300800 info("FRM Changing to %s firmware", fw_lme);
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300801 lme_coldreset(udev);
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300802 return -ENODEV;
803 }
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300804
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300805 release_firmware(fw);
806
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300807 return ret;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300808}
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300809
810static int lme2510_kill_urb(struct usb_data_stream *stream)
811{
812 int i;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300813
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300814 for (i = 0; i < stream->urbs_submitted; i++) {
815 deb_info(3, "killing URB no. %d.", i);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300816 /* stop the URB */
817 usb_kill_urb(stream->urb_list[i]);
818 }
819 stream->urbs_submitted = 0;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300820
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300821 return 0;
822}
823
824static struct tda10086_config tda10086_config = {
825 .demod_address = 0x1c,
826 .invert = 0,
827 .diseqc_tone = 1,
828 .xtal_freq = TDA10086_XTAL_16M,
829};
830
831static struct stv0288_config lme_config = {
832 .demod_address = 0xd0,
833 .min_delay_ms = 15,
834 .inittab = s7395_inittab,
835};
836
837static struct ix2505v_config lme_tuner = {
838 .tuner_address = 0xc0,
839 .min_delay_ms = 100,
840 .tuner_gain = 0x0,
841 .tuner_chargepump = 0x3,
842};
843
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300844static 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
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300856static 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;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300860 static u8 voltage_low[] = LME_VOLTAGE_L;
861 static u8 voltage_high[] = LME_VOLTAGE_H;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300862 static u8 rbuf[1];
863 int ret = 0, len = 3, rlen = 1;
864
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300865 if (mutex_lock_interruptible(&adap->dev->i2c_mutex) < 0)
866 return -EAGAIN;
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300867
868 switch (voltage) {
869 case SEC_VOLTAGE_18:
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300870 ret |= lme2510_usb_talk(adap->dev,
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300871 voltage_high, len, rbuf, rlen);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300872 break;
873
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300874 case SEC_VOLTAGE_OFF:
Malcolm Priestleyab599a62010-10-16 16:44:43 -0300875 case SEC_VOLTAGE_13:
876 default:
877 ret |= lme2510_usb_talk(adap->dev,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300878 voltage_low, len, rbuf, rlen);
879 break;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300880 }
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300881
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300882 mutex_unlock(&adap->dev->i2c_mutex);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300883
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300884 return (ret < 0) ? -ENODEV : 0;
885}
886
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300887static 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;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300891 char *fe_name[] = {"", " LG TDQY-P001F", " SHARP:BS2F7HZ7395",
892 " SHARP:BS2F7HZ0194"};
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300893 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
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300901static int dm04_lme2510_frontend_attach(struct dvb_usb_adapter *adap)
902{
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300903 struct lme2510_state *st = adap->dev->priv;
904
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300905 int ret = 0;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300906
907 st->i2c_talk_onoff = 1;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300908
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300909 st->i2c_gate = 4;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300910 adap->fe = dvb_attach(tda10086_attach, &tda10086_config,
911 &adap->dev->i2c_adap);
912
913 if (adap->fe) {
914 info("TUN Found Frontend TDA10086");
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300915 st->i2c_tuner_gate_w = 4;
916 st->i2c_tuner_gate_r = 4;
917 st->i2c_tuner_addr = 0xc0;
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300918 st->tuner_config = TUNER_LG;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300919 if (dvb_usb_lme2510_firmware != TUNER_LG) {
920 dvb_usb_lme2510_firmware = TUNER_LG;
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300921 ret = lme_firmware_switch(adap->dev->udev, 1);
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300922 }
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 }
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300939 goto end;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300940 }
Malcolm Priestleyf23e6612010-10-27 19:50:36 -0300941
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300942 st->i2c_gate = 5;
943 adap->fe = dvb_attach(stv0288_attach, &lme_config,
944 &adap->dev->i2c_adap);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300945 if (adap->fe) {
946 info("FE Found Stv0288");
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300947 st->i2c_tuner_gate_w = 4;
948 st->i2c_tuner_gate_r = 5;
949 st->i2c_tuner_addr = 0xc0;
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300950 st->tuner_config = TUNER_S7395;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300951 if (dvb_usb_lme2510_firmware != TUNER_S7395) {
952 dvb_usb_lme2510_firmware = TUNER_S7395;
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300953 ret = lme_firmware_switch(adap->dev->udev, 1);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300954 }
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300955 } else {
956 info("DM04 Not Supported");
957 return -ENODEV;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -0300958 }
959
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300960
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300961end: 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);
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300969 return ret;
970}
971
972static int dm04_lme2510_tuner(struct dvb_usb_adapter *adap)
973{
974 struct lme2510_state *st = adap->dev->priv;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300975 char *tun_msg[] = {"", "TDA8263", "IX2505V", "DVB_PLL_OPERA"};
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300976 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;
Malcolm Priestley9d4540c2011-02-12 22:35:08 -0300989 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;
Malcolm Priestley75aecc32010-12-07 19:49:24 -0300994 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 Interupt & Remote*/
1007 ret = lme2510_int_service(adap);
1008
1009 return ret;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001010}
1011
1012static int lme2510_powerup(struct dvb_usb_device *d, int onoff)
1013{
1014 struct lme2510_state *st = d->priv;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -03001015 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
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001027 st->i2c_talk_onoff = 1;
Malcolm Priestleyf23e6612010-10-27 19:50:36 -03001028
1029 mutex_unlock(&d->i2c_mutex);
1030
1031 return ret;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001032}
1033
1034/* DVB USB Driver stuff */
1035static struct dvb_usb_device_properties lme2510_properties;
1036static struct dvb_usb_device_properties lme2510c_properties;
1037
1038static 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
Malcolm Priestley9d4540c2011-02-12 22:35:08 -03001054 if (lme2510_return_status(udev) == 0x44) {
1055 lme_firmware_switch(udev, 0);
1056 return -ENODEV;
1057 }
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001058
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001059 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
1075static struct usb_device_id lme2510_table[] = {
1076 { USB_DEVICE(0x3344, 0x1122) }, /* LME2510 */
1077 { USB_DEVICE(0x3344, 0x1120) }, /* LME2510C */
1078 {} /* Terminating entry */
1079};
1080
1081MODULE_DEVICE_TABLE(usb, lme2510_table);
1082
1083static struct dvb_usb_device_properties lme2510_properties = {
1084 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001085 .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,
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001091 .tuner_attach = dm04_lme2510_tuner,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001092 /* 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 = {
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001112 { "DM04_LME2510_DVB-S",
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001113 { &lme2510_table[0], NULL },
1114 },
1115
1116 }
1117};
1118
1119static struct dvb_usb_device_properties lme2510c_properties = {
1120 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001121 .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,
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001127 .tuner_attach = dm04_lme2510_tuner,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001128 /* parameter for the MPEG2-data transfer */
1129 .stream = {
1130 .type = USB_BULK,
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001131 .count = 10,
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001132 .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 = {
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001148 { "DM04_LME2510C_DVB-S",
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001149 { &lme2510_table[1], NULL },
1150 },
1151 }
1152};
1153
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001154void *lme2510_exit_int(struct dvb_usb_device *d)
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001155{
1156 struct lme2510_state *st = d->priv;
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001157 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
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001165 if (st->lme_urb != NULL) {
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001166 st->i2c_talk_onoff = 1;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001167 st->signal_lock = 0;
1168 st->signal_level = 0;
1169 st->signal_sn = 0;
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001170 buffer = st->usb_buffer;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001171 usb_kill_urb(st->lme_urb);
1172 usb_free_coherent(d->udev, 5000, st->buffer,
1173 st->lme_urb->transfer_dma);
1174 info("Interupt Service Stopped");
David Härdemand8b4b582010-10-29 16:08:23 -03001175 rc_unregister_device(d->rc_dev);
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001176 info("Remote Stopped");
1177 }
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001178 return buffer;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001179}
1180
1181void lme2510_exit(struct usb_interface *intf)
1182{
1183 struct dvb_usb_device *d = usb_get_intfdata(intf);
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001184 void *usb_buffer;
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001185
Malcolm Priestleyab599a62010-10-16 16:44:43 -03001186 if (d != NULL) {
1187 usb_buffer = lme2510_exit_int(d);
1188 dvb_usb_device_exit(intf);
1189 kfree(usb_buffer);
1190 }
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001191}
1192
1193static struct usb_driver lme2510_driver = {
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001194 .name = "LME2510C_DVB-S",
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001195 .probe = lme2510_probe,
1196 .disconnect = lme2510_exit,
1197 .id_table = lme2510_table,
1198};
1199
1200/* module stuff */
1201static 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
1212static void __exit lme2510_module_exit(void)
1213{
1214 /* deregister this driver from the USB subsystem */
1215 usb_deregister(&lme2510_driver);
1216}
1217
1218module_init(lme2510_module_init);
1219module_exit(lme2510_module_exit);
1220
1221MODULE_AUTHOR("Malcolm Priestley <tvboxspy@gmail.com>");
Malcolm Priestley75aecc32010-12-07 19:49:24 -03001222MODULE_DESCRIPTION("LME2510(C) DVB-S USB2.0");
Malcolm Priestley9d4540c2011-02-12 22:35:08 -03001223MODULE_VERSION("1.80");
Malcolm Priestleyd2f918b2010-09-02 17:29:30 -03001224MODULE_LICENSE("GPL");