1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Frontend part of the Linux driver for the WideView/ Yakumo/ Hama/
3 * Typhoon/ Yuan DVB-T USB2.0 receiver.
5 * Copyright (C) 2005 Patrick Boettcher <patrick.boettcher@posteo.de>
7 * see Documentation/media/dvb-drivers/dvb-usb.rst for more information
11 struct dtt200u_fe_state
{
12 struct dvb_usb_device
*d
;
16 struct dtv_frontend_properties fep
;
17 struct dvb_frontend frontend
;
19 unsigned char data
[80];
20 struct mutex data_mutex
;
23 static int dtt200u_fe_read_status(struct dvb_frontend
*fe
,
26 struct dtt200u_fe_state
*state
= fe
->demodulator_priv
;
29 mutex_lock(&state
->data_mutex
);
30 state
->data
[0] = GET_TUNE_STATUS
;
32 ret
= dvb_usb_generic_rw(state
->d
, state
->data
, 1, state
->data
, 3, 0);
35 mutex_unlock(&state
->data_mutex
);
39 switch (state
->data
[0]) {
41 *stat
= FE_HAS_SIGNAL
| FE_HAS_CARRIER
|
42 FE_HAS_VITERBI
| FE_HAS_SYNC
| FE_HAS_LOCK
;
44 case 0x00: /* pending */
45 *stat
= FE_TIMEDOUT
; /* during set_frontend */
48 case 0x02: /* failed */
52 mutex_unlock(&state
->data_mutex
);
56 static int dtt200u_fe_read_ber(struct dvb_frontend
* fe
, u32
*ber
)
58 struct dtt200u_fe_state
*state
= fe
->demodulator_priv
;
61 mutex_lock(&state
->data_mutex
);
62 state
->data
[0] = GET_VIT_ERR_CNT
;
64 ret
= dvb_usb_generic_rw(state
->d
, state
->data
, 1, state
->data
, 3, 0);
66 *ber
= (state
->data
[0] << 16) | (state
->data
[1] << 8) | state
->data
[2];
68 mutex_unlock(&state
->data_mutex
);
72 static int dtt200u_fe_read_unc_blocks(struct dvb_frontend
* fe
, u32
*unc
)
74 struct dtt200u_fe_state
*state
= fe
->demodulator_priv
;
77 mutex_lock(&state
->data_mutex
);
78 state
->data
[0] = GET_RS_UNCOR_BLK_CNT
;
80 ret
= dvb_usb_generic_rw(state
->d
, state
->data
, 1, state
->data
, 2, 0);
82 *unc
= (state
->data
[0] << 8) | state
->data
[1];
84 mutex_unlock(&state
->data_mutex
);
88 static int dtt200u_fe_read_signal_strength(struct dvb_frontend
* fe
, u16
*strength
)
90 struct dtt200u_fe_state
*state
= fe
->demodulator_priv
;
93 mutex_lock(&state
->data_mutex
);
94 state
->data
[0] = GET_AGC
;
96 ret
= dvb_usb_generic_rw(state
->d
, state
->data
, 1, state
->data
, 1, 0);
98 *strength
= (state
->data
[0] << 8) | state
->data
[0];
100 mutex_unlock(&state
->data_mutex
);
104 static int dtt200u_fe_read_snr(struct dvb_frontend
* fe
, u16
*snr
)
106 struct dtt200u_fe_state
*state
= fe
->demodulator_priv
;
109 mutex_lock(&state
->data_mutex
);
110 state
->data
[0] = GET_SNR
;
112 ret
= dvb_usb_generic_rw(state
->d
, state
->data
, 1, state
->data
, 1, 0);
114 *snr
= ~((state
->data
[0] << 8) | state
->data
[0]);
116 mutex_unlock(&state
->data_mutex
);
120 static int dtt200u_fe_init(struct dvb_frontend
* fe
)
122 struct dtt200u_fe_state
*state
= fe
->demodulator_priv
;
125 mutex_lock(&state
->data_mutex
);
126 state
->data
[0] = SET_INIT
;
128 ret
= dvb_usb_generic_write(state
->d
, state
->data
, 1);
129 mutex_unlock(&state
->data_mutex
);
134 static int dtt200u_fe_sleep(struct dvb_frontend
* fe
)
136 return dtt200u_fe_init(fe
);
139 static int dtt200u_fe_get_tune_settings(struct dvb_frontend
* fe
, struct dvb_frontend_tune_settings
*tune
)
141 tune
->min_delay_ms
= 1500;
147 static int dtt200u_fe_set_frontend(struct dvb_frontend
*fe
)
149 struct dtv_frontend_properties
*fep
= &fe
->dtv_property_cache
;
150 struct dtt200u_fe_state
*state
= fe
->demodulator_priv
;
152 u16 freq
= fep
->frequency
/ 250000;
154 mutex_lock(&state
->data_mutex
);
155 state
->data
[0] = SET_BANDWIDTH
;
156 switch (fep
->bandwidth_hz
) {
171 ret
= dvb_usb_generic_write(state
->d
, state
->data
, 2);
175 state
->data
[0] = SET_RF_FREQ
;
176 state
->data
[1] = freq
& 0xff;
177 state
->data
[2] = (freq
>> 8) & 0xff;
178 ret
= dvb_usb_generic_write(state
->d
, state
->data
, 3);
183 mutex_unlock(&state
->data_mutex
);
187 static int dtt200u_fe_get_frontend(struct dvb_frontend
* fe
,
188 struct dtv_frontend_properties
*fep
)
190 struct dtt200u_fe_state
*state
= fe
->demodulator_priv
;
192 memcpy(fep
, &state
->fep
, sizeof(struct dtv_frontend_properties
));
196 static void dtt200u_fe_release(struct dvb_frontend
* fe
)
198 struct dtt200u_fe_state
*state
= (struct dtt200u_fe_state
*) fe
->demodulator_priv
;
202 static const struct dvb_frontend_ops dtt200u_fe_ops
;
204 struct dvb_frontend
* dtt200u_fe_attach(struct dvb_usb_device
*d
)
206 struct dtt200u_fe_state
* state
= NULL
;
208 /* allocate memory for the internal state */
209 state
= kzalloc(sizeof(struct dtt200u_fe_state
), GFP_KERNEL
);
213 deb_info("attaching frontend dtt200u\n");
216 mutex_init(&state
->data_mutex
);
218 memcpy(&state
->frontend
.ops
,&dtt200u_fe_ops
,sizeof(struct dvb_frontend_ops
));
219 state
->frontend
.demodulator_priv
= state
;
221 return &state
->frontend
;
226 static const struct dvb_frontend_ops dtt200u_fe_ops
= {
227 .delsys
= { SYS_DVBT
},
229 .name
= "WideView USB DVB-T",
230 .frequency_min_hz
= 44250 * kHz
,
231 .frequency_max_hz
= 867250 * kHz
,
232 .frequency_stepsize_hz
= 250 * kHz
,
233 .caps
= FE_CAN_INVERSION_AUTO
|
234 FE_CAN_FEC_1_2
| FE_CAN_FEC_2_3
| FE_CAN_FEC_3_4
|
235 FE_CAN_FEC_5_6
| FE_CAN_FEC_7_8
| FE_CAN_FEC_AUTO
|
236 FE_CAN_QPSK
| FE_CAN_QAM_16
| FE_CAN_QAM_64
| FE_CAN_QAM_AUTO
|
237 FE_CAN_TRANSMISSION_MODE_AUTO
|
238 FE_CAN_GUARD_INTERVAL_AUTO
|
240 FE_CAN_HIERARCHY_AUTO
,
243 .release
= dtt200u_fe_release
,
245 .init
= dtt200u_fe_init
,
246 .sleep
= dtt200u_fe_sleep
,
248 .set_frontend
= dtt200u_fe_set_frontend
,
249 .get_frontend
= dtt200u_fe_get_frontend
,
250 .get_tune_settings
= dtt200u_fe_get_tune_settings
,
252 .read_status
= dtt200u_fe_read_status
,
253 .read_ber
= dtt200u_fe_read_ber
,
254 .read_signal_strength
= dtt200u_fe_read_signal_strength
,
255 .read_snr
= dtt200u_fe_read_snr
,
256 .read_ucblocks
= dtt200u_fe_read_unc_blocks
,