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 dtv_frontend_properties
*op
)
47 switch (op
->code_rate_HP
) {
63 /* tps |= (0 << 7) */;
66 switch (op
->code_rate_LP
) {
82 /* tps |= (0 << 4) */;
85 switch (op
->modulation
) {
94 /* tps |= (0 << 13) */;
97 switch (op
->transmission_mode
) {
98 case TRANSMISSION_MODE_8K
:
101 case TRANSMISSION_MODE_2K
:
103 /* tps |= (0 << 0) */;
106 switch (op
->guard_interval
) {
107 case GUARD_INTERVAL_1_16
:
110 case GUARD_INTERVAL_1_8
:
113 case GUARD_INTERVAL_1_4
:
116 case GUARD_INTERVAL_1_32
:
118 /* tps |= (0 << 2) */;
121 switch (op
->hierarchy
) {
133 /* tps |= (0 << 10) */;
139 struct cinergyt2_fe_state
{
140 struct dvb_frontend fe
;
141 struct dvb_usb_device
*d
;
144 static int cinergyt2_fe_read_status(struct dvb_frontend
*fe
,
147 struct cinergyt2_fe_state
*state
= fe
->demodulator_priv
;
148 struct dvbt_get_status_msg result
;
149 u8 cmd
[] = { CINERGYT2_EP1_GET_TUNER_STATUS
};
152 ret
= dvb_usb_generic_rw(state
->d
, cmd
, sizeof(cmd
), (u8
*)&result
,
159 if (0xffff - le16_to_cpu(result
.gain
) > 30)
160 *status
|= FE_HAS_SIGNAL
;
161 if (result
.lock_bits
& (1 << 6))
162 *status
|= FE_HAS_LOCK
;
163 if (result
.lock_bits
& (1 << 5))
164 *status
|= FE_HAS_SYNC
;
165 if (result
.lock_bits
& (1 << 4))
166 *status
|= FE_HAS_CARRIER
;
167 if (result
.lock_bits
& (1 << 1))
168 *status
|= FE_HAS_VITERBI
;
170 if ((*status
& (FE_HAS_CARRIER
| FE_HAS_VITERBI
| FE_HAS_SYNC
)) !=
171 (FE_HAS_CARRIER
| FE_HAS_VITERBI
| FE_HAS_SYNC
))
172 *status
&= ~FE_HAS_LOCK
;
177 static int cinergyt2_fe_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
179 struct cinergyt2_fe_state
*state
= fe
->demodulator_priv
;
180 struct dvbt_get_status_msg status
;
181 char cmd
[] = { CINERGYT2_EP1_GET_TUNER_STATUS
};
184 ret
= dvb_usb_generic_rw(state
->d
, cmd
, sizeof(cmd
), (char *)&status
,
189 *ber
= le32_to_cpu(status
.viterbi_error_rate
);
193 static int cinergyt2_fe_read_unc_blocks(struct dvb_frontend
*fe
, u32
*unc
)
195 struct cinergyt2_fe_state
*state
= fe
->demodulator_priv
;
196 struct dvbt_get_status_msg status
;
197 u8 cmd
[] = { CINERGYT2_EP1_GET_TUNER_STATUS
};
200 ret
= dvb_usb_generic_rw(state
->d
, cmd
, sizeof(cmd
), (u8
*)&status
,
203 err("cinergyt2_fe_read_unc_blocks() Failed! (Error=%d)\n",
207 *unc
= le32_to_cpu(status
.uncorrected_block_count
);
211 static int cinergyt2_fe_read_signal_strength(struct dvb_frontend
*fe
,
214 struct cinergyt2_fe_state
*state
= fe
->demodulator_priv
;
215 struct dvbt_get_status_msg status
;
216 char cmd
[] = { CINERGYT2_EP1_GET_TUNER_STATUS
};
219 ret
= dvb_usb_generic_rw(state
->d
, cmd
, sizeof(cmd
), (char *)&status
,
222 err("cinergyt2_fe_read_signal_strength() Failed!"
223 " (Error=%d)\n", ret
);
226 *strength
= (0xffff - le16_to_cpu(status
.gain
));
230 static int cinergyt2_fe_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
232 struct cinergyt2_fe_state
*state
= fe
->demodulator_priv
;
233 struct dvbt_get_status_msg status
;
234 char cmd
[] = { CINERGYT2_EP1_GET_TUNER_STATUS
};
237 ret
= dvb_usb_generic_rw(state
->d
, cmd
, sizeof(cmd
), (char *)&status
,
240 err("cinergyt2_fe_read_snr() Failed! (Error=%d)\n", ret
);
243 *snr
= (status
.snr
<< 8) | status
.snr
;
247 static int cinergyt2_fe_init(struct dvb_frontend
*fe
)
252 static int cinergyt2_fe_sleep(struct dvb_frontend
*fe
)
254 deb_info("cinergyt2_fe_sleep() Called\n");
258 static int cinergyt2_fe_get_tune_settings(struct dvb_frontend
*fe
,
259 struct dvb_frontend_tune_settings
*tune
)
261 tune
->min_delay_ms
= 800;
265 static int cinergyt2_fe_set_frontend(struct dvb_frontend
*fe
)
267 struct dtv_frontend_properties
*fep
= &fe
->dtv_property_cache
;
268 struct cinergyt2_fe_state
*state
= fe
->demodulator_priv
;
269 struct dvbt_set_parameters_msg param
;
273 param
.cmd
= CINERGYT2_EP1_SET_TUNER_PARAMETERS
;
274 param
.tps
= cpu_to_le16(compute_tps(fep
));
275 param
.freq
= cpu_to_le32(fep
->frequency
/ 1000);
278 switch (fep
->bandwidth_hz
) {
291 err
= dvb_usb_generic_rw(state
->d
,
292 (char *)¶m
, sizeof(param
),
293 result
, sizeof(result
), 0);
295 err("cinergyt2_fe_set_frontend() Failed! err=%d\n", err
);
297 return (err
< 0) ? err
: 0;
300 static void cinergyt2_fe_release(struct dvb_frontend
*fe
)
302 struct cinergyt2_fe_state
*state
= fe
->demodulator_priv
;
306 static struct dvb_frontend_ops cinergyt2_fe_ops
;
308 struct dvb_frontend
*cinergyt2_fe_attach(struct dvb_usb_device
*d
)
310 struct cinergyt2_fe_state
*s
= kzalloc(sizeof(
311 struct cinergyt2_fe_state
), GFP_KERNEL
);
316 memcpy(&s
->fe
.ops
, &cinergyt2_fe_ops
, sizeof(struct dvb_frontend_ops
));
317 s
->fe
.demodulator_priv
= s
;
322 static struct dvb_frontend_ops cinergyt2_fe_ops
= {
323 .delsys
= { SYS_DVBT
},
326 .frequency_min
= 174000000,
327 .frequency_max
= 862000000,
328 .frequency_stepsize
= 166667,
329 .caps
= FE_CAN_INVERSION_AUTO
| FE_CAN_FEC_1_2
330 | FE_CAN_FEC_2_3
| FE_CAN_FEC_3_4
331 | FE_CAN_FEC_5_6
| FE_CAN_FEC_7_8
332 | FE_CAN_FEC_AUTO
| FE_CAN_QPSK
333 | FE_CAN_QAM_16
| FE_CAN_QAM_64
335 | FE_CAN_TRANSMISSION_MODE_AUTO
336 | FE_CAN_GUARD_INTERVAL_AUTO
337 | FE_CAN_HIERARCHY_AUTO
342 .release
= cinergyt2_fe_release
,
344 .init
= cinergyt2_fe_init
,
345 .sleep
= cinergyt2_fe_sleep
,
347 .set_frontend
= cinergyt2_fe_set_frontend
,
348 .get_tune_settings
= cinergyt2_fe_get_tune_settings
,
350 .read_status
= cinergyt2_fe_read_status
,
351 .read_ber
= cinergyt2_fe_read_ber
,
352 .read_signal_strength
= cinergyt2_fe_read_signal_strength
,
353 .read_snr
= cinergyt2_fe_read_snr
,
354 .read_ucblocks
= cinergyt2_fe_read_unc_blocks
,