blob: 7468a3839c5df440e3d795c9ac05c29b76976050 [file] [log] [blame]
Patrick Boettcher3706a4d2005-09-09 13:02:41 -07001/* DVB frontend part of the Linux driver for the TwinhanDTV StarBox USB2.0
2 * DVB-S receiver.
3 *
4 * Copyright (C) 2005 Ralph Metzler <rjkm@metzlerbros.de>
5 * Metzler Brothers Systementwicklung GbR
6 *
7 * Copyright (C) 2005 Patrick Boettcher <patrick.boettcher@desy.de>
8 *
9 * Thanks to Twinhan who kindly provided hardware and information.
10 *
11 * This file can be removed soon, after the DST-driver is rewritten to provice
12 * the frontend-controlling separately.
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the Free
16 * Software Foundation, version 2.
17 *
18 * see Documentation/dvb/README.dvb-usb for more information
19 *
20 */
21#include "vp702x.h"
22
23struct vp702x_fe_state {
24 struct dvb_frontend fe;
25 struct dvb_usb_device *d;
26
Patrick Boettcher0540c492006-09-19 12:51:43 -030027 struct dvb_frontend_ops ops;
28
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070029 fe_sec_voltage_t voltage;
30 fe_sec_tone_mode_t tone_mode;
31
32 u8 lnb_buf[8];
33
34 u8 lock;
35 u8 sig;
36 u8 snr;
37
38 unsigned long next_status_check;
39 unsigned long status_check_interval;
40};
41
42static int vp702x_fe_refresh_state(struct vp702x_fe_state *st)
43{
Florian Mickler57873c72011-03-21 07:19:10 -030044 u8 *buf;
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070045
Florian Mickler57873c72011-03-21 07:19:10 -030046 if (time_after(jiffies, st->next_status_check)) {
47 buf = kmalloc(10, GFP_KERNEL);
48 if (!buf) {
49 deb_fe("%s: buffer alloc failed\n", __func__);
50 return -ENOMEM;
51 }
52 vp702x_usb_in_op(st->d, READ_STATUS, 0, 0, buf, 10);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070053 st->lock = buf[4];
Florian Mickler57873c72011-03-21 07:19:10 -030054
55 vp702x_usb_in_op(st->d, READ_TUNER_REG_REQ, 0x11, 0, buf, 1);
56 st->snr = buf[0];
57
58 vp702x_usb_in_op(st->d, READ_TUNER_REG_REQ, 0x15, 0, buf, 1);
59 st->sig = buf[0];
60
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070061
62 st->next_status_check = jiffies + (st->status_check_interval*HZ)/1000;
Florian Mickler57873c72011-03-21 07:19:10 -030063 kfree(buf);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070064 }
65 return 0;
66}
67
68static u8 vp702x_chksum(u8 *buf,int f, int count)
69{
70 u8 s = 0;
71 int i;
72 for (i = f; i < f+count; i++)
73 s += buf[i];
74 return ~s+1;
75}
76
77static int vp702x_fe_read_status(struct dvb_frontend* fe, fe_status_t *status)
78{
79 struct vp702x_fe_state *st = fe->demodulator_priv;
80 vp702x_fe_refresh_state(st);
Harvey Harrison708bebd2008-04-08 23:20:00 -030081 deb_fe("%s\n",__func__);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070082
83 if (st->lock == 0)
84 *status = FE_HAS_LOCK | FE_HAS_SYNC | FE_HAS_VITERBI | FE_HAS_SIGNAL | FE_HAS_CARRIER;
85 else
86 *status = 0;
87
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070088 if (*status & FE_HAS_LOCK)
89 st->status_check_interval = 1000;
90 else
91 st->status_check_interval = 250;
92 return 0;
93}
94
95/* not supported by this Frontend */
96static int vp702x_fe_read_ber(struct dvb_frontend* fe, u32 *ber)
97{
98 struct vp702x_fe_state *st = fe->demodulator_priv;
99 vp702x_fe_refresh_state(st);
100 *ber = 0;
101 return 0;
102}
103
104/* not supported by this Frontend */
105static int vp702x_fe_read_unc_blocks(struct dvb_frontend* fe, u32 *unc)
106{
107 struct vp702x_fe_state *st = fe->demodulator_priv;
108 vp702x_fe_refresh_state(st);
109 *unc = 0;
110 return 0;
111}
112
113static int vp702x_fe_read_signal_strength(struct dvb_frontend* fe, u16 *strength)
114{
115 struct vp702x_fe_state *st = fe->demodulator_priv;
116 vp702x_fe_refresh_state(st);
117
118 *strength = (st->sig << 8) | st->sig;
119 return 0;
120}
121
122static int vp702x_fe_read_snr(struct dvb_frontend* fe, u16 *snr)
123{
124 u8 _snr;
125 struct vp702x_fe_state *st = fe->demodulator_priv;
126 vp702x_fe_refresh_state(st);
127
128 _snr = (st->snr & 0x1f) * 0xff / 0x1f;
129 *snr = (_snr << 8) | _snr;
130 return 0;
131}
132
133static int vp702x_fe_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings *tune)
134{
Harvey Harrison708bebd2008-04-08 23:20:00 -0300135 deb_fe("%s\n",__func__);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700136 tune->min_delay_ms = 2000;
137 return 0;
138}
139
140static int vp702x_fe_set_frontend(struct dvb_frontend* fe,
141 struct dvb_frontend_parameters *fep)
142{
143 struct vp702x_fe_state *st = fe->demodulator_priv;
144 u32 freq = fep->frequency/1000;
145 /*CalFrequency*/
146/* u16 frequencyRef[16] = { 2, 4, 8, 16, 32, 64, 128, 256, 24, 5, 10, 20, 40, 80, 160, 320 }; */
147 u64 sr;
Florian Mickler57873c72011-03-21 07:19:10 -0300148 u8 *cmd;
149
150 cmd = kzalloc(10, GFP_KERNEL);
151 if (!cmd)
152 return -ENOMEM;
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700153
154 cmd[0] = (freq >> 8) & 0x7f;
155 cmd[1] = freq & 0xff;
156 cmd[2] = 1; /* divrate == 4 -> frequencyRef[1] -> 1 here */
157
158 sr = (u64) (fep->u.qpsk.symbol_rate/1000) << 20;
159 do_div(sr,88000);
160 cmd[3] = (sr >> 12) & 0xff;
161 cmd[4] = (sr >> 4) & 0xff;
162 cmd[5] = (sr << 4) & 0xf0;
163
Mauro Carvalho Chehab4ae5c2e2006-03-25 15:53:38 -0300164 deb_fe("setting frontend to: %u -> %u (%x) LNB-based GHz, symbolrate: %d -> %lu (%lx)\n",
165 fep->frequency,freq,freq, fep->u.qpsk.symbol_rate,
166 (unsigned long) sr, (unsigned long) sr);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700167
168/* if (fep->inversion == INVERSION_ON)
169 cmd[6] |= 0x80; */
170
171 if (st->voltage == SEC_VOLTAGE_18)
172 cmd[6] |= 0x40;
173
174/* if (fep->u.qpsk.symbol_rate > 8000000)
175 cmd[6] |= 0x20;
176
177 if (fep->frequency < 1531000)
178 cmd[6] |= 0x04;
179
180 if (st->tone_mode == SEC_TONE_ON)
181 cmd[6] |= 0x01;*/
182
183 cmd[7] = vp702x_chksum(cmd,0,7);
184
185 st->status_check_interval = 250;
186 st->next_status_check = jiffies;
187
Florian Mickler57873c72011-03-21 07:19:10 -0300188 vp702x_usb_inout_op(st->d, cmd, 8, cmd, 10, 100);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700189
Florian Mickler57873c72011-03-21 07:19:10 -0300190 if (cmd[2] == 0 && cmd[3] == 0)
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700191 deb_fe("tuning failed.\n");
192 else
193 deb_fe("tuning succeeded.\n");
194
Florian Mickler57873c72011-03-21 07:19:10 -0300195 kfree(cmd);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700196 return 0;
197}
198
Patrick Boettcher0540c492006-09-19 12:51:43 -0300199static int vp702x_fe_init(struct dvb_frontend *fe)
200{
201 struct vp702x_fe_state *st = fe->demodulator_priv;
Harvey Harrison708bebd2008-04-08 23:20:00 -0300202 deb_fe("%s\n",__func__);
Patrick Boettcher0540c492006-09-19 12:51:43 -0300203 vp702x_usb_in_op(st->d, RESET_TUNER, 0, 0, NULL, 0);
204 return 0;
205}
206
207static int vp702x_fe_sleep(struct dvb_frontend *fe)
208{
Harvey Harrison708bebd2008-04-08 23:20:00 -0300209 deb_fe("%s\n",__func__);
Patrick Boettcher0540c492006-09-19 12:51:43 -0300210 return 0;
211}
212
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700213static int vp702x_fe_get_frontend(struct dvb_frontend* fe,
214 struct dvb_frontend_parameters *fep)
215{
Harvey Harrison708bebd2008-04-08 23:20:00 -0300216 deb_fe("%s\n",__func__);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700217 return 0;
218}
219
220static int vp702x_fe_send_diseqc_msg (struct dvb_frontend* fe,
Michael Krufky50c25ff2006-01-09 15:25:34 -0200221 struct dvb_diseqc_master_cmd *m)
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700222{
Florian Mickler57873c72011-03-21 07:19:10 -0300223 int ret;
224 u8 *cmd;
Pat Erley5ad5e482007-05-08 09:36:09 -0300225 struct vp702x_fe_state *st = fe->demodulator_priv;
Florian Mickler57873c72011-03-21 07:19:10 -0300226
227 cmd = kzalloc(10, GFP_KERNEL);
228 if (!cmd)
229 return -ENOMEM;
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700230
Harvey Harrison708bebd2008-04-08 23:20:00 -0300231 deb_fe("%s\n",__func__);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700232
Florian Mickler57873c72011-03-21 07:19:10 -0300233 if (m->msg_len > 4) {
234 ret = -EINVAL;
235 goto out;
236 }
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700237
238 cmd[1] = SET_DISEQC_CMD;
239 cmd[2] = m->msg_len;
240 memcpy(&cmd[3], m->msg, m->msg_len);
Florian Mickler57873c72011-03-21 07:19:10 -0300241 cmd[7] = vp702x_chksum(cmd, 0, 7);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700242
Florian Mickler57873c72011-03-21 07:19:10 -0300243 vp702x_usb_inout_op(st->d, cmd, 8, cmd, 10, 100);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700244
Florian Mickler57873c72011-03-21 07:19:10 -0300245 if (cmd[2] == 0 && cmd[3] == 0)
Pat Erley5ad5e482007-05-08 09:36:09 -0300246 deb_fe("diseqc cmd failed.\n");
247 else
248 deb_fe("diseqc cmd succeeded.\n");
Florian Mickler57873c72011-03-21 07:19:10 -0300249 ret = 0;
250out:
251 kfree(cmd);
252 return ret;
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700253}
254
255static int vp702x_fe_send_diseqc_burst (struct dvb_frontend* fe, fe_sec_mini_cmd_t burst)
256{
Harvey Harrison708bebd2008-04-08 23:20:00 -0300257 deb_fe("%s\n",__func__);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700258 return 0;
259}
260
261static int vp702x_fe_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
262{
263 struct vp702x_fe_state *st = fe->demodulator_priv;
Florian Mickler57873c72011-03-21 07:19:10 -0300264 u8 *buf;
265
Harvey Harrison708bebd2008-04-08 23:20:00 -0300266 deb_fe("%s\n",__func__);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700267
Florian Mickler57873c72011-03-21 07:19:10 -0300268 buf = kmalloc(10, GFP_KERNEL);
269 if (!buf)
270 return -ENOMEM;
271
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700272 st->tone_mode = tone;
273
274 if (tone == SEC_TONE_ON)
275 st->lnb_buf[2] = 0x02;
276 else
277 st->lnb_buf[2] = 0x00;
278
Florian Mickler57873c72011-03-21 07:19:10 -0300279 st->lnb_buf[7] = vp702x_chksum(st->lnb_buf, 0, 7);
280 memcpy(buf, st->lnb_buf, 8);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700281
Florian Mickler57873c72011-03-21 07:19:10 -0300282 vp702x_usb_inout_op(st->d, buf, 8, buf, 10, 100);
283 if (buf[2] == 0 && buf[3] == 0)
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700284 deb_fe("set_tone cmd failed.\n");
285 else
286 deb_fe("set_tone cmd succeeded.\n");
287
Florian Mickler57873c72011-03-21 07:19:10 -0300288 kfree(buf);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700289 return 0;
290}
291
292static int vp702x_fe_set_voltage (struct dvb_frontend* fe, fe_sec_voltage_t
293 voltage)
294{
295 struct vp702x_fe_state *st = fe->demodulator_priv;
Florian Mickler57873c72011-03-21 07:19:10 -0300296 u8 *buf;
Harvey Harrison708bebd2008-04-08 23:20:00 -0300297 deb_fe("%s\n",__func__);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700298
Florian Mickler57873c72011-03-21 07:19:10 -0300299 buf = kmalloc(10, GFP_KERNEL);
300 if (!buf)
301 return -ENOMEM;
302
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700303 st->voltage = voltage;
304
305 if (voltage != SEC_VOLTAGE_OFF)
306 st->lnb_buf[4] = 0x01;
307 else
308 st->lnb_buf[4] = 0x00;
309
Florian Mickler57873c72011-03-21 07:19:10 -0300310 st->lnb_buf[7] = vp702x_chksum(st->lnb_buf, 0, 7);
311 memcpy(buf, st->lnb_buf, 8);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700312
Florian Mickler57873c72011-03-21 07:19:10 -0300313 vp702x_usb_inout_op(st->d, buf, 8, buf, 10, 100);
314 if (buf[2] == 0 && buf[3] == 0)
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700315 deb_fe("set_voltage cmd failed.\n");
316 else
317 deb_fe("set_voltage cmd succeeded.\n");
318
Florian Mickler57873c72011-03-21 07:19:10 -0300319 kfree(buf);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700320 return 0;
321}
322
323static void vp702x_fe_release(struct dvb_frontend* fe)
324{
325 struct vp702x_fe_state *st = fe->demodulator_priv;
326 kfree(st);
327}
328
329static struct dvb_frontend_ops vp702x_fe_ops;
330
331struct dvb_frontend * vp702x_fe_attach(struct dvb_usb_device *d)
332{
Panagiotis Issaris74081872006-01-11 19:40:56 -0200333 struct vp702x_fe_state *s = kzalloc(sizeof(struct vp702x_fe_state), GFP_KERNEL);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700334 if (s == NULL)
335 goto error;
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700336
337 s->d = d;
Patrick Boettcherdea74862006-05-14 05:01:31 -0300338
339 memcpy(&s->fe.ops,&vp702x_fe_ops,sizeof(struct dvb_frontend_ops));
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700340 s->fe.demodulator_priv = s;
341
342 s->lnb_buf[1] = SET_LNB_POWER;
343 s->lnb_buf[3] = 0xff; /* 0=tone burst, 2=data burst, ff=off */
344
Trent Piepho83977032006-05-12 20:36:24 -0300345 return &s->fe;
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700346error:
347 return NULL;
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700348}
349
350
351static struct dvb_frontend_ops vp702x_fe_ops = {
352 .info = {
353 .name = "Twinhan DST-like frontend (VP7021/VP7020) DVB-S",
354 .type = FE_QPSK,
355 .frequency_min = 950000,
356 .frequency_max = 2150000,
357 .frequency_stepsize = 1000, /* kHz for QPSK frontends */
358 .frequency_tolerance = 0,
359 .symbol_rate_min = 1000000,
360 .symbol_rate_max = 45000000,
361 .symbol_rate_tolerance = 500, /* ppm */
362 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
363 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
364 FE_CAN_QPSK |
365 FE_CAN_FEC_AUTO
366 },
367 .release = vp702x_fe_release,
368
Patrick Boettcher0540c492006-09-19 12:51:43 -0300369 .init = vp702x_fe_init,
370 .sleep = vp702x_fe_sleep,
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700371
372 .set_frontend = vp702x_fe_set_frontend,
373 .get_frontend = vp702x_fe_get_frontend,
374 .get_tune_settings = vp702x_fe_get_tune_settings,
375
376 .read_status = vp702x_fe_read_status,
377 .read_ber = vp702x_fe_read_ber,
378 .read_signal_strength = vp702x_fe_read_signal_strength,
379 .read_snr = vp702x_fe_read_snr,
380 .read_ucblocks = vp702x_fe_read_unc_blocks,
381
382 .diseqc_send_master_cmd = vp702x_fe_send_diseqc_msg,
383 .diseqc_send_burst = vp702x_fe_send_diseqc_burst,
384 .set_tone = vp702x_fe_set_tone,
385 .set_voltage = vp702x_fe_set_voltage,
386};