]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/media/usb/dvb-usb/dtt200u-fe.c
Merge branch 'timers-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[mirror_ubuntu-jammy-kernel.git] / drivers / media / usb / dvb-usb / dtt200u-fe.c
CommitLineData
a10e763b 1// SPDX-License-Identifier: GPL-2.0-only
fb41f5a7
PB
2/* Frontend part of the Linux driver for the WideView/ Yakumo/ Hama/
3 * Typhoon/ Yuan DVB-T USB2.0 receiver.
776338e1 4 *
99e44da7 5 * Copyright (C) 2005 Patrick Boettcher <patrick.boettcher@posteo.de>
776338e1 6 *
670d7adb 7 * see Documentation/media/dvb-drivers/dvb-usb.rst for more information
776338e1
JS
8 */
9#include "dtt200u.h"
10
11struct dtt200u_fe_state {
12 struct dvb_usb_device *d;
13
0df289a2 14 enum fe_status stat;
d590f9c2 15
adcc8f0e 16 struct dtv_frontend_properties fep;
776338e1 17 struct dvb_frontend frontend;
89919b51
MCC
18
19 unsigned char data[80];
20 struct mutex data_mutex;
776338e1
JS
21};
22
0df289a2
MCC
23static int dtt200u_fe_read_status(struct dvb_frontend *fe,
24 enum fe_status *stat)
776338e1
JS
25{
26 struct dtt200u_fe_state *state = fe->demodulator_priv;
ba705a62 27 int ret;
d590f9c2 28
89919b51
MCC
29 mutex_lock(&state->data_mutex);
30 state->data[0] = GET_TUNE_STATUS;
31
ba705a62
MCC
32 ret = dvb_usb_generic_rw(state->d, state->data, 1, state->data, 3, 0);
33 if (ret < 0) {
34 *stat = 0;
35 mutex_unlock(&state->data_mutex);
36 return ret;
37 }
776338e1 38
89919b51 39 switch (state->data[0]) {
776338e1 40 case 0x01:
d590f9c2
PB
41 *stat = FE_HAS_SIGNAL | FE_HAS_CARRIER |
42 FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
776338e1 43 break;
d590f9c2
PB
44 case 0x00: /* pending */
45 *stat = FE_TIMEDOUT; /* during set_frontend */
776338e1
JS
46 break;
47 default:
d590f9c2
PB
48 case 0x02: /* failed */
49 *stat = 0;
776338e1
JS
50 break;
51 }
89919b51 52 mutex_unlock(&state->data_mutex);
776338e1
JS
53 return 0;
54}
d590f9c2 55
776338e1
JS
56static int dtt200u_fe_read_ber(struct dvb_frontend* fe, u32 *ber)
57{
58 struct dtt200u_fe_state *state = fe->demodulator_priv;
ba705a62 59 int ret;
89919b51
MCC
60
61 mutex_lock(&state->data_mutex);
62 state->data[0] = GET_VIT_ERR_CNT;
63
ba705a62
MCC
64 ret = dvb_usb_generic_rw(state->d, state->data, 1, state->data, 3, 0);
65 if (ret >= 0)
66 *ber = (state->data[0] << 16) | (state->data[1] << 8) | state->data[2];
89919b51
MCC
67
68 mutex_unlock(&state->data_mutex);
ba705a62 69 return ret;
776338e1
JS
70}
71
72static int dtt200u_fe_read_unc_blocks(struct dvb_frontend* fe, u32 *unc)
73{
74 struct dtt200u_fe_state *state = fe->demodulator_priv;
ba705a62 75 int ret;
d590f9c2 76
89919b51
MCC
77 mutex_lock(&state->data_mutex);
78 state->data[0] = GET_RS_UNCOR_BLK_CNT;
79
ba705a62
MCC
80 ret = dvb_usb_generic_rw(state->d, state->data, 1, state->data, 2, 0);
81 if (ret >= 0)
82 *unc = (state->data[0] << 8) | state->data[1];
89919b51
MCC
83
84 mutex_unlock(&state->data_mutex);
85 return ret;
776338e1
JS
86}
87
88static int dtt200u_fe_read_signal_strength(struct dvb_frontend* fe, u16 *strength)
89{
90 struct dtt200u_fe_state *state = fe->demodulator_priv;
ba705a62 91 int ret;
89919b51
MCC
92
93 mutex_lock(&state->data_mutex);
94 state->data[0] = GET_AGC;
95
ba705a62
MCC
96 ret = dvb_usb_generic_rw(state->d, state->data, 1, state->data, 1, 0);
97 if (ret >= 0)
98 *strength = (state->data[0] << 8) | state->data[0];
89919b51
MCC
99
100 mutex_unlock(&state->data_mutex);
101 return ret;
776338e1
JS
102}
103
104static int dtt200u_fe_read_snr(struct dvb_frontend* fe, u16 *snr)
105{
106 struct dtt200u_fe_state *state = fe->demodulator_priv;
ba705a62 107 int ret;
89919b51
MCC
108
109 mutex_lock(&state->data_mutex);
110 state->data[0] = GET_SNR;
111
ba705a62
MCC
112 ret = dvb_usb_generic_rw(state->d, state->data, 1, state->data, 1, 0);
113 if (ret >= 0)
114 *snr = ~((state->data[0] << 8) | state->data[0]);
89919b51
MCC
115
116 mutex_unlock(&state->data_mutex);
117 return ret;
776338e1
JS
118}
119
120static int dtt200u_fe_init(struct dvb_frontend* fe)
121{
122 struct dtt200u_fe_state *state = fe->demodulator_priv;
89919b51
MCC
123 int ret;
124
125 mutex_lock(&state->data_mutex);
126 state->data[0] = SET_INIT;
127
128 ret = dvb_usb_generic_write(state->d, state->data, 1);
129 mutex_unlock(&state->data_mutex);
130
131 return ret;
776338e1
JS
132}
133
134static int dtt200u_fe_sleep(struct dvb_frontend* fe)
135{
136 return dtt200u_fe_init(fe);
137}
138
139static int dtt200u_fe_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings *tune)
140{
141 tune->min_delay_ms = 1500;
d590f9c2
PB
142 tune->step_size = 0;
143 tune->max_drift = 0;
776338e1
JS
144 return 0;
145}
146
adcc8f0e 147static int dtt200u_fe_set_frontend(struct dvb_frontend *fe)
776338e1 148{
adcc8f0e 149 struct dtv_frontend_properties *fep = &fe->dtv_property_cache;
776338e1 150 struct dtt200u_fe_state *state = fe->demodulator_priv;
ba705a62 151 int ret;
776338e1 152 u16 freq = fep->frequency / 250000;
776338e1 153
89919b51
MCC
154 mutex_lock(&state->data_mutex);
155 state->data[0] = SET_BANDWIDTH;
adcc8f0e
MCC
156 switch (fep->bandwidth_hz) {
157 case 8000000:
89919b51 158 state->data[1] = 8;
adcc8f0e
MCC
159 break;
160 case 7000000:
89919b51 161 state->data[1] = 7;
adcc8f0e
MCC
162 break;
163 case 6000000:
89919b51 164 state->data[1] = 6;
adcc8f0e
MCC
165 break;
166 default:
89919b51
MCC
167 ret = -EINVAL;
168 goto ret;
776338e1 169 }
776338e1 170
ba705a62
MCC
171 ret = dvb_usb_generic_write(state->d, state->data, 2);
172 if (ret < 0)
173 goto ret;
776338e1 174
89919b51
MCC
175 state->data[0] = SET_RF_FREQ;
176 state->data[1] = freq & 0xff;
177 state->data[2] = (freq >> 8) & 0xff;
ba705a62
MCC
178 ret = dvb_usb_generic_write(state->d, state->data, 3);
179 if (ret < 0)
180 goto ret;
776338e1 181
89919b51
MCC
182ret:
183 mutex_unlock(&state->data_mutex);
184 return ret;
776338e1
JS
185}
186
7e3e68bc
MCC
187static int dtt200u_fe_get_frontend(struct dvb_frontend* fe,
188 struct dtv_frontend_properties *fep)
776338e1
JS
189{
190 struct dtt200u_fe_state *state = fe->demodulator_priv;
7e3e68bc 191
adcc8f0e 192 memcpy(fep, &state->fep, sizeof(struct dtv_frontend_properties));
776338e1
JS
193 return 0;
194}
195
196static void dtt200u_fe_release(struct dvb_frontend* fe)
197{
198 struct dtt200u_fe_state *state = (struct dtt200u_fe_state*) fe->demodulator_priv;
199 kfree(state);
200}
201
bd336e63 202static const struct dvb_frontend_ops dtt200u_fe_ops;
776338e1
JS
203
204struct dvb_frontend* dtt200u_fe_attach(struct dvb_usb_device *d)
205{
206 struct dtt200u_fe_state* state = NULL;
207
208 /* allocate memory for the internal state */
7408187d 209 state = kzalloc(sizeof(struct dtt200u_fe_state), GFP_KERNEL);
776338e1
JS
210 if (state == NULL)
211 goto error;
776338e1
JS
212
213 deb_info("attaching frontend dtt200u\n");
214
215 state->d = d;
89919b51 216 mutex_init(&state->data_mutex);
776338e1 217
dea74869 218 memcpy(&state->frontend.ops,&dtt200u_fe_ops,sizeof(struct dvb_frontend_ops));
776338e1
JS
219 state->frontend.demodulator_priv = state;
220
8397703e 221 return &state->frontend;
776338e1
JS
222error:
223 return NULL;
776338e1
JS
224}
225
bd336e63 226static const struct dvb_frontend_ops dtt200u_fe_ops = {
adcc8f0e 227 .delsys = { SYS_DVBT },
776338e1 228 .info = {
d590f9c2 229 .name = "WideView USB DVB-T",
f1b1eabf
MCC
230 .frequency_min_hz = 44250 * kHz,
231 .frequency_max_hz = 867250 * kHz,
232 .frequency_stepsize_hz = 250 * kHz,
776338e1
JS
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 |
239 FE_CAN_RECOVER |
240 FE_CAN_HIERARCHY_AUTO,
241 },
242
243 .release = dtt200u_fe_release,
244
245 .init = dtt200u_fe_init,
246 .sleep = dtt200u_fe_sleep,
247
adcc8f0e
MCC
248 .set_frontend = dtt200u_fe_set_frontend,
249 .get_frontend = dtt200u_fe_get_frontend,
776338e1
JS
250 .get_tune_settings = dtt200u_fe_get_tune_settings,
251
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,
257};