2 * TerraTec Cinergy T2/qanu USB2 DVB-T adapter.
4 * Copyright (C) 2007 Tomi Orava (tomimo@ncircle.nullnet.fi)
6 * Based on the dvb-usb-framework code and the
7 * original Terratec Cinergy T2 driver by:
9 * Copyright (C) 2004 Daniel Mack <daniel@qanu.de> and
10 * Holger Waechtler <holger@qanu.de>
12 * Protocol Spec published on http://qanu.de/specs/terratec_cinergyT2.pdf
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 #include "cinergyT2.h"
34 * convert linux-dvb frontend parameter set into TPS.
35 * See ETSI ETS-300744, section 4.6.2, table 9 for details.
37 * This function is probably reusable and may better get placed in a support
40 * We replace errornous fields by default TPS fields (the ones with value 0).
43 static uint16_t compute_tps(struct dvb_frontend_parameters *p)
45 struct dvb_ofdm_parameters *op = &p->u.ofdm;
48 switch (op->code_rate_HP) {
64 /* tps |= (0 << 7) */;
67 switch (op->code_rate_LP) {
83 /* tps |= (0 << 4) */;
86 switch (op->constellation) {
95 /* tps |= (0 << 13) */;
98 switch (op->transmission_mode) {
99 case TRANSMISSION_MODE_8K:
102 case TRANSMISSION_MODE_2K:
104 /* tps |= (0 << 0) */;
107 switch (op->guard_interval) {
108 case GUARD_INTERVAL_1_16:
111 case GUARD_INTERVAL_1_8:
114 case GUARD_INTERVAL_1_4:
117 case GUARD_INTERVAL_1_32:
119 /* tps |= (0 << 2) */;
122 switch (op->hierarchy_information) {
134 /* tps |= (0 << 10) */;
140 struct cinergyt2_fe_state {
141 struct dvb_frontend fe;
142 struct dvb_usb_device *d;
145 static int cinergyt2_fe_read_status(struct dvb_frontend *fe,
148 struct cinergyt2_fe_state *state = fe->demodulator_priv;
149 struct dvbt_get_status_msg result;
150 u8 cmd[] = { CINERGYT2_EP1_GET_TUNER_STATUS };
153 ret = dvb_usb_generic_rw(state->d, cmd, sizeof(cmd), (u8 *)&result,
160 if (0xffff - le16_to_cpu(result.gain) > 30)
161 *status |= FE_HAS_SIGNAL;
162 if (result.lock_bits & (1 << 6))
163 *status |= FE_HAS_LOCK;
164 if (result.lock_bits & (1 << 5))
165 *status |= FE_HAS_SYNC;
166 if (result.lock_bits & (1 << 4))
167 *status |= FE_HAS_CARRIER;
168 if (result.lock_bits & (1 << 1))
169 *status |= FE_HAS_VITERBI;
171 if ((*status & (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC)) !=
172 (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC))
173 *status &= ~FE_HAS_LOCK;
178 static int cinergyt2_fe_read_ber(struct dvb_frontend *fe, u32 *ber)
180 struct cinergyt2_fe_state *state = fe->demodulator_priv;
181 struct dvbt_get_status_msg status;
182 char cmd[] = { CINERGYT2_EP1_GET_TUNER_STATUS };
185 ret = dvb_usb_generic_rw(state->d, cmd, sizeof(cmd), (char *)&status,
190 *ber = le32_to_cpu(status.viterbi_error_rate);
194 static int cinergyt2_fe_read_unc_blocks(struct dvb_frontend *fe, u32 *unc)
196 struct cinergyt2_fe_state *state = fe->demodulator_priv;
197 struct dvbt_get_status_msg status;
198 u8 cmd[] = { CINERGYT2_EP1_GET_TUNER_STATUS };
201 ret = dvb_usb_generic_rw(state->d, cmd, sizeof(cmd), (u8 *)&status,
204 err("cinergyt2_fe_read_unc_blocks() Failed! (Error=%d)\n",
208 *unc = le32_to_cpu(status.uncorrected_block_count);
212 static int cinergyt2_fe_read_signal_strength(struct dvb_frontend *fe,
215 struct cinergyt2_fe_state *state = fe->demodulator_priv;
216 struct dvbt_get_status_msg status;
217 char cmd[] = { CINERGYT2_EP1_GET_TUNER_STATUS };
220 ret = dvb_usb_generic_rw(state->d, cmd, sizeof(cmd), (char *)&status,
223 err("cinergyt2_fe_read_signal_strength() Failed!"
224 " (Error=%d)\n", ret);
227 *strength = (0xffff - le16_to_cpu(status.gain));
231 static int cinergyt2_fe_read_snr(struct dvb_frontend *fe, u16 *snr)
233 struct cinergyt2_fe_state *state = fe->demodulator_priv;
234 struct dvbt_get_status_msg status;
235 char cmd[] = { CINERGYT2_EP1_GET_TUNER_STATUS };
238 ret = dvb_usb_generic_rw(state->d, cmd, sizeof(cmd), (char *)&status,
241 err("cinergyt2_fe_read_snr() Failed! (Error=%d)\n", ret);
244 *snr = (status.snr << 8) | status.snr;
248 static int cinergyt2_fe_init(struct dvb_frontend *fe)
253 static int cinergyt2_fe_sleep(struct dvb_frontend *fe)
255 deb_info("cinergyt2_fe_sleep() Called\n");
259 static int cinergyt2_fe_get_tune_settings(struct dvb_frontend *fe,
260 struct dvb_frontend_tune_settings *tune)
262 tune->min_delay_ms = 800;
266 static int cinergyt2_fe_set_frontend(struct dvb_frontend *fe,
267 struct dvb_frontend_parameters *fep)
269 struct cinergyt2_fe_state *state = fe->demodulator_priv;
270 struct dvbt_set_parameters_msg param;
274 param.cmd = CINERGYT2_EP1_SET_TUNER_PARAMETERS;
275 param.tps = cpu_to_le16(compute_tps(fep));
276 param.freq = cpu_to_le32(fep->frequency / 1000);
277 param.bandwidth = 8 - fep->u.ofdm.bandwidth - BANDWIDTH_8_MHZ;
279 err = dvb_usb_generic_rw(state->d,
280 (char *)¶m, sizeof(param),
281 result, sizeof(result), 0);
283 err("cinergyt2_fe_set_frontend() Failed! err=%d\n", err);
285 return (err < 0) ? err : 0;
288 static int cinergyt2_fe_get_frontend(struct dvb_frontend *fe,
289 struct dvb_frontend_parameters *fep)
294 static void cinergyt2_fe_release(struct dvb_frontend *fe)
296 struct cinergyt2_fe_state *state = fe->demodulator_priv;
301 static struct dvb_frontend_ops cinergyt2_fe_ops;
303 struct dvb_frontend *cinergyt2_fe_attach(struct dvb_usb_device *d)
305 struct cinergyt2_fe_state *s = kzalloc(sizeof(
306 struct cinergyt2_fe_state), GFP_KERNEL);
311 memcpy(&s->fe.ops, &cinergyt2_fe_ops, sizeof(struct dvb_frontend_ops));
312 s->fe.demodulator_priv = s;
317 static struct dvb_frontend_ops cinergyt2_fe_ops = {
321 .frequency_min = 174000000,
322 .frequency_max = 862000000,
323 .frequency_stepsize = 166667,
324 .caps = FE_CAN_INVERSION_AUTO | FE_CAN_FEC_1_2
325 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4
326 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8
327 | FE_CAN_FEC_AUTO | FE_CAN_QPSK
328 | FE_CAN_QAM_16 | FE_CAN_QAM_64
330 | FE_CAN_TRANSMISSION_MODE_AUTO
331 | FE_CAN_GUARD_INTERVAL_AUTO
332 | FE_CAN_HIERARCHY_AUTO
337 .release = cinergyt2_fe_release,
339 .init = cinergyt2_fe_init,
340 .sleep = cinergyt2_fe_sleep,
342 .set_frontend = cinergyt2_fe_set_frontend,
343 .get_frontend = cinergyt2_fe_get_frontend,
344 .get_tune_settings = cinergyt2_fe_get_tune_settings,
346 .read_status = cinergyt2_fe_read_status,
347 .read_ber = cinergyt2_fe_read_ber,
348 .read_signal_strength = cinergyt2_fe_read_signal_strength,
349 .read_snr = cinergyt2_fe_read_snr,
350 .read_ucblocks = cinergyt2_fe_read_unc_blocks,