perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / drivers / media / usb / dvb-usb / vp702x-fe.c
blob9eb811452f2ef2f6ae96f1caaf5cdd555f522d34
1 /* DVB frontend part of the Linux driver for the TwinhanDTV StarBox USB2.0
2 * DVB-S receiver.
4 * Copyright (C) 2005 Ralph Metzler <rjkm@metzlerbros.de>
5 * Metzler Brothers Systementwicklung GbR
7 * Copyright (C) 2005 Patrick Boettcher <patrick.boettcher@posteo.de>
9 * Thanks to Twinhan who kindly provided hardware and information.
11 * This file can be removed soon, after the DST-driver is rewritten to provice
12 * the frontend-controlling separately.
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.
18 * see Documentation/media/dvb-drivers/dvb-usb.rst for more information
21 #include "vp702x.h"
23 struct vp702x_fe_state {
24 struct dvb_frontend fe;
25 struct dvb_usb_device *d;
27 struct dvb_frontend_ops ops;
29 enum fe_sec_voltage voltage;
30 enum fe_sec_tone_mode tone_mode;
32 u8 lnb_buf[8];
34 u8 lock;
35 u8 sig;
36 u8 snr;
38 unsigned long next_status_check;
39 unsigned long status_check_interval;
42 static int vp702x_fe_refresh_state(struct vp702x_fe_state *st)
44 struct vp702x_device_state *dst = st->d->priv;
45 u8 *buf;
47 if (time_after(jiffies, st->next_status_check)) {
48 mutex_lock(&dst->buf_mutex);
49 buf = dst->buf;
51 vp702x_usb_in_op(st->d, READ_STATUS, 0, 0, buf, 10);
52 st->lock = buf[4];
54 vp702x_usb_in_op(st->d, READ_TUNER_REG_REQ, 0x11, 0, buf, 1);
55 st->snr = buf[0];
57 vp702x_usb_in_op(st->d, READ_TUNER_REG_REQ, 0x15, 0, buf, 1);
58 st->sig = buf[0];
60 mutex_unlock(&dst->buf_mutex);
61 st->next_status_check = jiffies + (st->status_check_interval*HZ)/1000;
63 return 0;
66 static u8 vp702x_chksum(u8 *buf,int f, int count)
68 u8 s = 0;
69 int i;
70 for (i = f; i < f+count; i++)
71 s += buf[i];
72 return ~s+1;
75 static int vp702x_fe_read_status(struct dvb_frontend *fe,
76 enum fe_status *status)
78 struct vp702x_fe_state *st = fe->demodulator_priv;
79 vp702x_fe_refresh_state(st);
80 deb_fe("%s\n",__func__);
82 if (st->lock == 0)
83 *status = FE_HAS_LOCK | FE_HAS_SYNC | FE_HAS_VITERBI | FE_HAS_SIGNAL | FE_HAS_CARRIER;
84 else
85 *status = 0;
87 if (*status & FE_HAS_LOCK)
88 st->status_check_interval = 1000;
89 else
90 st->status_check_interval = 250;
91 return 0;
94 /* not supported by this Frontend */
95 static int vp702x_fe_read_ber(struct dvb_frontend* fe, u32 *ber)
97 struct vp702x_fe_state *st = fe->demodulator_priv;
98 vp702x_fe_refresh_state(st);
99 *ber = 0;
100 return 0;
103 /* not supported by this Frontend */
104 static int vp702x_fe_read_unc_blocks(struct dvb_frontend* fe, u32 *unc)
106 struct vp702x_fe_state *st = fe->demodulator_priv;
107 vp702x_fe_refresh_state(st);
108 *unc = 0;
109 return 0;
112 static int vp702x_fe_read_signal_strength(struct dvb_frontend* fe, u16 *strength)
114 struct vp702x_fe_state *st = fe->demodulator_priv;
115 vp702x_fe_refresh_state(st);
117 *strength = (st->sig << 8) | st->sig;
118 return 0;
121 static int vp702x_fe_read_snr(struct dvb_frontend* fe, u16 *snr)
123 u8 _snr;
124 struct vp702x_fe_state *st = fe->demodulator_priv;
125 vp702x_fe_refresh_state(st);
127 _snr = (st->snr & 0x1f) * 0xff / 0x1f;
128 *snr = (_snr << 8) | _snr;
129 return 0;
132 static int vp702x_fe_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings *tune)
134 deb_fe("%s\n",__func__);
135 tune->min_delay_ms = 2000;
136 return 0;
139 static int vp702x_fe_set_frontend(struct dvb_frontend *fe)
141 struct dtv_frontend_properties *fep = &fe->dtv_property_cache;
142 struct vp702x_fe_state *st = fe->demodulator_priv;
143 struct vp702x_device_state *dst = st->d->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;
148 u8 *cmd;
150 mutex_lock(&dst->buf_mutex);
152 cmd = dst->buf;
153 memset(cmd, 0, 10);
155 cmd[0] = (freq >> 8) & 0x7f;
156 cmd[1] = freq & 0xff;
157 cmd[2] = 1; /* divrate == 4 -> frequencyRef[1] -> 1 here */
159 sr = (u64) (fep->symbol_rate/1000) << 20;
160 do_div(sr,88000);
161 cmd[3] = (sr >> 12) & 0xff;
162 cmd[4] = (sr >> 4) & 0xff;
163 cmd[5] = (sr << 4) & 0xf0;
165 deb_fe("setting frontend to: %u -> %u (%x) LNB-based GHz, symbolrate: %d -> %lu (%lx)\n",
166 fep->frequency, freq, freq, fep->symbol_rate,
167 (unsigned long) sr, (unsigned long) sr);
169 /* if (fep->inversion == INVERSION_ON)
170 cmd[6] |= 0x80; */
172 if (st->voltage == SEC_VOLTAGE_18)
173 cmd[6] |= 0x40;
175 /* if (fep->symbol_rate > 8000000)
176 cmd[6] |= 0x20;
178 if (fep->frequency < 1531000)
179 cmd[6] |= 0x04;
181 if (st->tone_mode == SEC_TONE_ON)
182 cmd[6] |= 0x01;*/
184 cmd[7] = vp702x_chksum(cmd,0,7);
186 st->status_check_interval = 250;
187 st->next_status_check = jiffies;
189 vp702x_usb_inout_op(st->d, cmd, 8, cmd, 10, 100);
191 if (cmd[2] == 0 && cmd[3] == 0)
192 deb_fe("tuning failed.\n");
193 else
194 deb_fe("tuning succeeded.\n");
196 mutex_unlock(&dst->buf_mutex);
198 return 0;
201 static int vp702x_fe_init(struct dvb_frontend *fe)
203 struct vp702x_fe_state *st = fe->demodulator_priv;
204 deb_fe("%s\n",__func__);
205 vp702x_usb_in_op(st->d, RESET_TUNER, 0, 0, NULL, 0);
206 return 0;
209 static int vp702x_fe_sleep(struct dvb_frontend *fe)
211 deb_fe("%s\n",__func__);
212 return 0;
215 static int vp702x_fe_send_diseqc_msg (struct dvb_frontend* fe,
216 struct dvb_diseqc_master_cmd *m)
218 u8 *cmd;
219 struct vp702x_fe_state *st = fe->demodulator_priv;
220 struct vp702x_device_state *dst = st->d->priv;
222 deb_fe("%s\n",__func__);
224 if (m->msg_len > 4)
225 return -EINVAL;
227 mutex_lock(&dst->buf_mutex);
229 cmd = dst->buf;
230 cmd[1] = SET_DISEQC_CMD;
231 cmd[2] = m->msg_len;
232 memcpy(&cmd[3], m->msg, m->msg_len);
233 cmd[7] = vp702x_chksum(cmd, 0, 7);
235 vp702x_usb_inout_op(st->d, cmd, 8, cmd, 10, 100);
237 if (cmd[2] == 0 && cmd[3] == 0)
238 deb_fe("diseqc cmd failed.\n");
239 else
240 deb_fe("diseqc cmd succeeded.\n");
242 mutex_unlock(&dst->buf_mutex);
244 return 0;
247 static int vp702x_fe_send_diseqc_burst(struct dvb_frontend *fe,
248 enum fe_sec_mini_cmd burst)
250 deb_fe("%s\n",__func__);
251 return 0;
254 static int vp702x_fe_set_tone(struct dvb_frontend *fe,
255 enum fe_sec_tone_mode tone)
257 struct vp702x_fe_state *st = fe->demodulator_priv;
258 struct vp702x_device_state *dst = st->d->priv;
259 u8 *buf;
261 deb_fe("%s\n",__func__);
263 st->tone_mode = tone;
265 if (tone == SEC_TONE_ON)
266 st->lnb_buf[2] = 0x02;
267 else
268 st->lnb_buf[2] = 0x00;
270 st->lnb_buf[7] = vp702x_chksum(st->lnb_buf, 0, 7);
272 mutex_lock(&dst->buf_mutex);
274 buf = dst->buf;
275 memcpy(buf, st->lnb_buf, 8);
277 vp702x_usb_inout_op(st->d, buf, 8, buf, 10, 100);
278 if (buf[2] == 0 && buf[3] == 0)
279 deb_fe("set_tone cmd failed.\n");
280 else
281 deb_fe("set_tone cmd succeeded.\n");
283 mutex_unlock(&dst->buf_mutex);
285 return 0;
288 static int vp702x_fe_set_voltage(struct dvb_frontend *fe,
289 enum fe_sec_voltage voltage)
291 struct vp702x_fe_state *st = fe->demodulator_priv;
292 struct vp702x_device_state *dst = st->d->priv;
293 u8 *buf;
294 deb_fe("%s\n",__func__);
296 st->voltage = voltage;
298 if (voltage != SEC_VOLTAGE_OFF)
299 st->lnb_buf[4] = 0x01;
300 else
301 st->lnb_buf[4] = 0x00;
303 st->lnb_buf[7] = vp702x_chksum(st->lnb_buf, 0, 7);
305 mutex_lock(&dst->buf_mutex);
307 buf = dst->buf;
308 memcpy(buf, st->lnb_buf, 8);
310 vp702x_usb_inout_op(st->d, buf, 8, buf, 10, 100);
311 if (buf[2] == 0 && buf[3] == 0)
312 deb_fe("set_voltage cmd failed.\n");
313 else
314 deb_fe("set_voltage cmd succeeded.\n");
316 mutex_unlock(&dst->buf_mutex);
317 return 0;
320 static void vp702x_fe_release(struct dvb_frontend* fe)
322 struct vp702x_fe_state *st = fe->demodulator_priv;
323 kfree(st);
326 static const struct dvb_frontend_ops vp702x_fe_ops;
328 struct dvb_frontend * vp702x_fe_attach(struct dvb_usb_device *d)
330 struct vp702x_fe_state *s = kzalloc(sizeof(struct vp702x_fe_state), GFP_KERNEL);
331 if (s == NULL)
332 goto error;
334 s->d = d;
336 memcpy(&s->fe.ops,&vp702x_fe_ops,sizeof(struct dvb_frontend_ops));
337 s->fe.demodulator_priv = s;
339 s->lnb_buf[1] = SET_LNB_POWER;
340 s->lnb_buf[3] = 0xff; /* 0=tone burst, 2=data burst, ff=off */
342 return &s->fe;
343 error:
344 return NULL;
348 static const struct dvb_frontend_ops vp702x_fe_ops = {
349 .delsys = { SYS_DVBS },
350 .info = {
351 .name = "Twinhan DST-like frontend (VP7021/VP7020) DVB-S",
352 .frequency_min_hz = 950 * MHz,
353 .frequency_max_hz = 2150 * MHz,
354 .frequency_stepsize_hz = 1 * MHz,
355 .symbol_rate_min = 1000000,
356 .symbol_rate_max = 45000000,
357 .symbol_rate_tolerance = 500, /* ppm */
358 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
359 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
360 FE_CAN_QPSK |
361 FE_CAN_FEC_AUTO
363 .release = vp702x_fe_release,
365 .init = vp702x_fe_init,
366 .sleep = vp702x_fe_sleep,
368 .set_frontend = vp702x_fe_set_frontend,
369 .get_tune_settings = vp702x_fe_get_tune_settings,
371 .read_status = vp702x_fe_read_status,
372 .read_ber = vp702x_fe_read_ber,
373 .read_signal_strength = vp702x_fe_read_signal_strength,
374 .read_snr = vp702x_fe_read_snr,
375 .read_ucblocks = vp702x_fe_read_unc_blocks,
377 .diseqc_send_master_cmd = vp702x_fe_send_diseqc_msg,
378 .diseqc_send_burst = vp702x_fe_send_diseqc_burst,
379 .set_tone = vp702x_fe_set_tone,
380 .set_voltage = vp702x_fe_set_voltage,