Merge branch 'master'
[linux-2.6] / drivers / media / dvb / dvb-usb / vp702x-fe.c
1 /* 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
23 struct vp702x_fe_state {
24         struct dvb_frontend fe;
25         struct dvb_usb_device *d;
26
27         fe_sec_voltage_t voltage;
28         fe_sec_tone_mode_t tone_mode;
29
30         u8 lnb_buf[8];
31
32         u8 lock;
33         u8 sig;
34         u8 snr;
35
36         unsigned long next_status_check;
37         unsigned long status_check_interval;
38 };
39
40 static int vp702x_fe_refresh_state(struct vp702x_fe_state *st)
41 {
42         u8 buf[10];
43         if (time_after(jiffies,st->next_status_check)) {
44                 vp702x_usb_in_op(st->d,READ_STATUS,0,0,buf,10);
45
46                 st->lock = buf[4];
47                 vp702x_usb_in_op(st->d,READ_TUNER_REG_REQ,0x11,0,&st->snr,1);
48                 vp702x_usb_in_op(st->d,READ_TUNER_REG_REQ,0x15,0,&st->sig,1);
49
50                 st->next_status_check = jiffies + (st->status_check_interval*HZ)/1000;
51         }
52         return 0;
53 }
54
55 static u8 vp702x_chksum(u8 *buf,int f, int count)
56 {
57         u8 s = 0;
58         int i;
59         for (i = f; i < f+count; i++)
60                 s += buf[i];
61         return ~s+1;
62 }
63
64 static int vp702x_fe_read_status(struct dvb_frontend* fe, fe_status_t *status)
65 {
66         struct vp702x_fe_state *st = fe->demodulator_priv;
67         vp702x_fe_refresh_state(st);
68         deb_fe("%s\n",__FUNCTION__);
69
70         if (st->lock == 0)
71                 *status = FE_HAS_LOCK | FE_HAS_SYNC | FE_HAS_VITERBI | FE_HAS_SIGNAL | FE_HAS_CARRIER;
72         else
73                 *status = 0;
74
75         deb_fe("real state: %x\n",*status);
76         *status = 0x1f;
77
78         if (*status & FE_HAS_LOCK)
79                 st->status_check_interval = 1000;
80         else
81                 st->status_check_interval = 250;
82         return 0;
83 }
84
85 /* not supported by this Frontend */
86 static int vp702x_fe_read_ber(struct dvb_frontend* fe, u32 *ber)
87 {
88         struct vp702x_fe_state *st = fe->demodulator_priv;
89         vp702x_fe_refresh_state(st);
90         *ber = 0;
91         return 0;
92 }
93
94 /* not supported by this Frontend */
95 static int vp702x_fe_read_unc_blocks(struct dvb_frontend* fe, u32 *unc)
96 {
97         struct vp702x_fe_state *st = fe->demodulator_priv;
98         vp702x_fe_refresh_state(st);
99         *unc = 0;
100         return 0;
101 }
102
103 static int vp702x_fe_read_signal_strength(struct dvb_frontend* fe, u16 *strength)
104 {
105         struct vp702x_fe_state *st = fe->demodulator_priv;
106         vp702x_fe_refresh_state(st);
107
108         *strength = (st->sig << 8) | st->sig;
109         return 0;
110 }
111
112 static int vp702x_fe_read_snr(struct dvb_frontend* fe, u16 *snr)
113 {
114         u8 _snr;
115         struct vp702x_fe_state *st = fe->demodulator_priv;
116         vp702x_fe_refresh_state(st);
117
118         _snr = (st->snr & 0x1f) * 0xff / 0x1f;
119         *snr = (_snr << 8) | _snr;
120         return 0;
121 }
122
123 static int vp702x_fe_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings *tune)
124 {
125         deb_fe("%s\n",__FUNCTION__);
126         tune->min_delay_ms = 2000;
127         return 0;
128 }
129
130 static int vp702x_fe_set_frontend(struct dvb_frontend* fe,
131                                   struct dvb_frontend_parameters *fep)
132 {
133         struct vp702x_fe_state *st = fe->demodulator_priv;
134         u32 freq = fep->frequency/1000;
135         /*CalFrequency*/
136 /*      u16 frequencyRef[16] = { 2, 4, 8, 16, 32, 64, 128, 256, 24, 5, 10, 20, 40, 80, 160, 320 }; */
137         u64 sr;
138         u8 cmd[8] = { 0 },ibuf[10];
139
140         cmd[0] = (freq >> 8) & 0x7f;
141         cmd[1] =  freq       & 0xff;
142         cmd[2] = 1; /* divrate == 4 -> frequencyRef[1] -> 1 here */
143
144         sr = (u64) (fep->u.qpsk.symbol_rate/1000) << 20;
145         do_div(sr,88000);
146         cmd[3] = (sr >> 12) & 0xff;
147         cmd[4] = (sr >> 4)  & 0xff;
148         cmd[5] = (sr << 4)  & 0xf0;
149
150         deb_fe("setting frontend to: %u -> %u (%x) LNB-based GHz, symbolrate: %d -> %Lu (%Lx)\n",
151                         fep->frequency,freq,freq, fep->u.qpsk.symbol_rate, sr, sr);
152
153 /*      if (fep->inversion == INVERSION_ON)
154                 cmd[6] |= 0x80; */
155
156         if (st->voltage == SEC_VOLTAGE_18)
157                 cmd[6] |= 0x40;
158
159 /*      if (fep->u.qpsk.symbol_rate > 8000000)
160                 cmd[6] |= 0x20;
161
162         if (fep->frequency < 1531000)
163                 cmd[6] |= 0x04;
164
165         if (st->tone_mode == SEC_TONE_ON)
166                 cmd[6] |= 0x01;*/
167
168         cmd[7] = vp702x_chksum(cmd,0,7);
169
170         st->status_check_interval = 250;
171         st->next_status_check = jiffies;
172
173         vp702x_usb_in_op(st->d, RESET_TUNER, 0, 0, NULL, 0);
174         msleep(30);
175         vp702x_usb_inout_op(st->d,cmd,8,ibuf,10,100);
176
177         if (ibuf[2] == 0 && ibuf[3] == 0)
178                 deb_fe("tuning failed.\n");
179         else
180                 deb_fe("tuning succeeded.\n");
181
182         return 0;
183 }
184
185 static int vp702x_fe_get_frontend(struct dvb_frontend* fe,
186                                   struct dvb_frontend_parameters *fep)
187 {
188         deb_fe("%s\n",__FUNCTION__);
189         return 0;
190 }
191
192 static int vp702x_fe_send_diseqc_msg (struct dvb_frontend* fe,
193                                     struct dvb_diseqc_master_cmd *m)
194 {
195         struct vp702x_fe_state *st = fe->demodulator_priv;
196         u8 cmd[8],ibuf[10];
197         memset(cmd,0,8);
198
199         deb_fe("%s\n",__FUNCTION__);
200
201         if (m->msg_len > 4)
202                 return -EINVAL;
203
204         cmd[1] = SET_DISEQC_CMD;
205         cmd[2] = m->msg_len;
206         memcpy(&cmd[3], m->msg, m->msg_len);
207         cmd[7] = vp702x_chksum(cmd,0,7);
208
209         vp702x_usb_inout_op(st->d,cmd,8,ibuf,10,100);
210
211         if (ibuf[2] == 0 && ibuf[3] == 0)
212                 deb_fe("diseqc cmd failed.\n");
213         else
214                 deb_fe("diseqc cmd succeeded.\n");
215
216         return 0;
217 }
218
219 static int vp702x_fe_send_diseqc_burst (struct dvb_frontend* fe, fe_sec_mini_cmd_t burst)
220 {
221         deb_fe("%s\n",__FUNCTION__);
222         return 0;
223 }
224
225 static int vp702x_fe_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
226 {
227         struct vp702x_fe_state *st = fe->demodulator_priv;
228         u8 ibuf[10];
229         deb_fe("%s\n",__FUNCTION__);
230
231         st->tone_mode = tone;
232
233         if (tone == SEC_TONE_ON)
234                 st->lnb_buf[2] = 0x02;
235         else
236                 st->lnb_buf[2] = 0x00;
237
238         st->lnb_buf[7] = vp702x_chksum(st->lnb_buf,0,7);
239
240         vp702x_usb_inout_op(st->d,st->lnb_buf,8,ibuf,10,100);
241         if (ibuf[2] == 0 && ibuf[3] == 0)
242                 deb_fe("set_tone cmd failed.\n");
243         else
244                 deb_fe("set_tone cmd succeeded.\n");
245
246         return 0;
247 }
248
249 static int vp702x_fe_set_voltage (struct dvb_frontend* fe, fe_sec_voltage_t
250                 voltage)
251 {
252         struct vp702x_fe_state *st = fe->demodulator_priv;
253         u8 ibuf[10];
254         deb_fe("%s\n",__FUNCTION__);
255
256         st->voltage = voltage;
257
258         if (voltage != SEC_VOLTAGE_OFF)
259                 st->lnb_buf[4] = 0x01;
260         else
261                 st->lnb_buf[4] = 0x00;
262
263         st->lnb_buf[7] = vp702x_chksum(st->lnb_buf,0,7);
264
265         vp702x_usb_inout_op(st->d,st->lnb_buf,8,ibuf,10,100);
266         if (ibuf[2] == 0 && ibuf[3] == 0)
267                 deb_fe("set_voltage cmd failed.\n");
268         else
269                 deb_fe("set_voltage cmd succeeded.\n");
270
271         return 0;
272 }
273
274 static void vp702x_fe_release(struct dvb_frontend* fe)
275 {
276         struct vp702x_fe_state *st = fe->demodulator_priv;
277         kfree(st);
278 }
279
280 static struct dvb_frontend_ops vp702x_fe_ops;
281
282 struct dvb_frontend * vp702x_fe_attach(struct dvb_usb_device *d)
283 {
284         struct vp702x_fe_state *s = kmalloc(sizeof(struct vp702x_fe_state), GFP_KERNEL);
285         if (s == NULL)
286                 goto error;
287         memset(s,0,sizeof(struct vp702x_fe_state));
288
289         s->d = d;
290         s->fe.ops = &vp702x_fe_ops;
291         s->fe.demodulator_priv = s;
292
293         s->lnb_buf[1] = SET_LNB_POWER;
294         s->lnb_buf[3] = 0xff; /* 0=tone burst, 2=data burst, ff=off */
295
296         goto success;
297 error:
298         return NULL;
299 success:
300         return &s->fe;
301 }
302
303
304 static struct dvb_frontend_ops vp702x_fe_ops = {
305         .info = {
306                 .name           = "Twinhan DST-like frontend (VP7021/VP7020) DVB-S",
307                 .type           = FE_QPSK,
308                 .frequency_min       = 950000,
309                 .frequency_max       = 2150000,
310                 .frequency_stepsize  = 1000,   /* kHz for QPSK frontends */
311                 .frequency_tolerance = 0,
312                 .symbol_rate_min     = 1000000,
313                 .symbol_rate_max     = 45000000,
314                 .symbol_rate_tolerance = 500,  /* ppm */
315                 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
316                 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
317                 FE_CAN_QPSK |
318                 FE_CAN_FEC_AUTO
319         },
320         .release = vp702x_fe_release,
321
322         .init = NULL,
323         .sleep = NULL,
324
325         .set_frontend = vp702x_fe_set_frontend,
326         .get_frontend = vp702x_fe_get_frontend,
327         .get_tune_settings = vp702x_fe_get_tune_settings,
328
329         .read_status = vp702x_fe_read_status,
330         .read_ber = vp702x_fe_read_ber,
331         .read_signal_strength = vp702x_fe_read_signal_strength,
332         .read_snr = vp702x_fe_read_snr,
333         .read_ucblocks = vp702x_fe_read_unc_blocks,
334
335         .diseqc_send_master_cmd = vp702x_fe_send_diseqc_msg,
336         .diseqc_send_burst = vp702x_fe_send_diseqc_burst,
337         .set_tone = vp702x_fe_set_tone,
338         .set_voltage = vp702x_fe_set_voltage,
339 };