1 /* DVB frontend part of the Linux driver for the TwinhanDTV StarBox USB2.0
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
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
;
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
;
47 if (time_after(jiffies
, st
->next_status_check
)) {
48 mutex_lock(&dst
->buf_mutex
);
51 vp702x_usb_in_op(st
->d
, READ_STATUS
, 0, 0, buf
, 10);
54 vp702x_usb_in_op(st
->d
, READ_TUNER_REG_REQ
, 0x11, 0, buf
, 1);
57 vp702x_usb_in_op(st
->d
, READ_TUNER_REG_REQ
, 0x15, 0, buf
, 1);
60 mutex_unlock(&dst
->buf_mutex
);
61 st
->next_status_check
= jiffies
+ (st
->status_check_interval
*HZ
)/1000;
66 static u8
vp702x_chksum(u8
*buf
,int f
, int count
)
70 for (i
= f
; i
< f
+count
; i
++)
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__
);
83 *status
= FE_HAS_LOCK
| FE_HAS_SYNC
| FE_HAS_VITERBI
| FE_HAS_SIGNAL
| FE_HAS_CARRIER
;
87 if (*status
& FE_HAS_LOCK
)
88 st
->status_check_interval
= 1000;
90 st
->status_check_interval
= 250;
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
);
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
);
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
;
121 static int vp702x_fe_read_snr(struct dvb_frontend
* fe
, u16
*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
;
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;
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;
146 /* u16 frequencyRef[16] = { 2, 4, 8, 16, 32, 64, 128, 256, 24, 5, 10, 20, 40, 80, 160, 320 }; */
150 mutex_lock(&dst
->buf_mutex
);
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;
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)
172 if (st
->voltage
== SEC_VOLTAGE_18
)
175 /* if (fep->symbol_rate > 8000000)
178 if (fep->frequency < 1531000)
181 if (st->tone_mode == SEC_TONE_ON)
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");
194 deb_fe("tuning succeeded.\n");
196 mutex_unlock(&dst
->buf_mutex
);
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);
209 static int vp702x_fe_sleep(struct dvb_frontend
*fe
)
211 deb_fe("%s\n",__func__
);
215 static int vp702x_fe_send_diseqc_msg (struct dvb_frontend
* fe
,
216 struct dvb_diseqc_master_cmd
*m
)
219 struct vp702x_fe_state
*st
= fe
->demodulator_priv
;
220 struct vp702x_device_state
*dst
= st
->d
->priv
;
222 deb_fe("%s\n",__func__
);
227 mutex_lock(&dst
->buf_mutex
);
230 cmd
[1] = SET_DISEQC_CMD
;
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");
240 deb_fe("diseqc cmd succeeded.\n");
242 mutex_unlock(&dst
->buf_mutex
);
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__
);
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
;
261 deb_fe("%s\n",__func__
);
263 st
->tone_mode
= tone
;
265 if (tone
== SEC_TONE_ON
)
266 st
->lnb_buf
[2] = 0x02;
268 st
->lnb_buf
[2] = 0x00;
270 st
->lnb_buf
[7] = vp702x_chksum(st
->lnb_buf
, 0, 7);
272 mutex_lock(&dst
->buf_mutex
);
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");
281 deb_fe("set_tone cmd succeeded.\n");
283 mutex_unlock(&dst
->buf_mutex
);
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
;
294 deb_fe("%s\n",__func__
);
296 st
->voltage
= voltage
;
298 if (voltage
!= SEC_VOLTAGE_OFF
)
299 st
->lnb_buf
[4] = 0x01;
301 st
->lnb_buf
[4] = 0x00;
303 st
->lnb_buf
[7] = vp702x_chksum(st
->lnb_buf
, 0, 7);
305 mutex_lock(&dst
->buf_mutex
);
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");
314 deb_fe("set_voltage cmd succeeded.\n");
316 mutex_unlock(&dst
->buf_mutex
);
320 static void vp702x_fe_release(struct dvb_frontend
* fe
)
322 struct vp702x_fe_state
*st
= fe
->demodulator_priv
;
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
);
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 */
348 static const struct dvb_frontend_ops vp702x_fe_ops
= {
349 .delsys
= { SYS_DVBS
},
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
|
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
,