18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/* Frontend part of the Linux driver for the WideView/ Yakumo/ Hama/
38c2ecf20Sopenharmony_ci * Typhoon/ Yuan DVB-T USB2.0 receiver.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2005 Patrick Boettcher <patrick.boettcher@posteo.de>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * see Documentation/driver-api/media/drivers/dvb-usb.rst for more information
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci#include "dtt200u.h"
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_cistruct dtt200u_fe_state {
128c2ecf20Sopenharmony_ci	struct dvb_usb_device *d;
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci	enum fe_status stat;
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci	struct dtv_frontend_properties fep;
178c2ecf20Sopenharmony_ci	struct dvb_frontend frontend;
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci	unsigned char data[80];
208c2ecf20Sopenharmony_ci	struct mutex data_mutex;
218c2ecf20Sopenharmony_ci};
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cistatic int dtt200u_fe_read_status(struct dvb_frontend *fe,
248c2ecf20Sopenharmony_ci				  enum fe_status *stat)
258c2ecf20Sopenharmony_ci{
268c2ecf20Sopenharmony_ci	struct dtt200u_fe_state *state = fe->demodulator_priv;
278c2ecf20Sopenharmony_ci	int ret;
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci	mutex_lock(&state->data_mutex);
308c2ecf20Sopenharmony_ci	state->data[0] = GET_TUNE_STATUS;
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	ret = dvb_usb_generic_rw(state->d, state->data, 1, state->data, 3, 0);
338c2ecf20Sopenharmony_ci	if (ret < 0) {
348c2ecf20Sopenharmony_ci		*stat = 0;
358c2ecf20Sopenharmony_ci		mutex_unlock(&state->data_mutex);
368c2ecf20Sopenharmony_ci		return ret;
378c2ecf20Sopenharmony_ci	}
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	switch (state->data[0]) {
408c2ecf20Sopenharmony_ci		case 0x01:
418c2ecf20Sopenharmony_ci			*stat = FE_HAS_SIGNAL | FE_HAS_CARRIER |
428c2ecf20Sopenharmony_ci				FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
438c2ecf20Sopenharmony_ci			break;
448c2ecf20Sopenharmony_ci		case 0x00: /* pending */
458c2ecf20Sopenharmony_ci			*stat = FE_TIMEDOUT; /* during set_frontend */
468c2ecf20Sopenharmony_ci			break;
478c2ecf20Sopenharmony_ci		default:
488c2ecf20Sopenharmony_ci		case 0x02: /* failed */
498c2ecf20Sopenharmony_ci			*stat = 0;
508c2ecf20Sopenharmony_ci			break;
518c2ecf20Sopenharmony_ci	}
528c2ecf20Sopenharmony_ci	mutex_unlock(&state->data_mutex);
538c2ecf20Sopenharmony_ci	return 0;
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic int dtt200u_fe_read_ber(struct dvb_frontend* fe, u32 *ber)
578c2ecf20Sopenharmony_ci{
588c2ecf20Sopenharmony_ci	struct dtt200u_fe_state *state = fe->demodulator_priv;
598c2ecf20Sopenharmony_ci	int ret;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	mutex_lock(&state->data_mutex);
628c2ecf20Sopenharmony_ci	state->data[0] = GET_VIT_ERR_CNT;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	ret = dvb_usb_generic_rw(state->d, state->data, 1, state->data, 3, 0);
658c2ecf20Sopenharmony_ci	if (ret >= 0)
668c2ecf20Sopenharmony_ci		*ber = (state->data[0] << 16) | (state->data[1] << 8) | state->data[2];
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	mutex_unlock(&state->data_mutex);
698c2ecf20Sopenharmony_ci	return ret;
708c2ecf20Sopenharmony_ci}
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cistatic int dtt200u_fe_read_unc_blocks(struct dvb_frontend* fe, u32 *unc)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	struct dtt200u_fe_state *state = fe->demodulator_priv;
758c2ecf20Sopenharmony_ci	int ret;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	mutex_lock(&state->data_mutex);
788c2ecf20Sopenharmony_ci	state->data[0] = GET_RS_UNCOR_BLK_CNT;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	ret = dvb_usb_generic_rw(state->d, state->data, 1, state->data, 2, 0);
818c2ecf20Sopenharmony_ci	if (ret >= 0)
828c2ecf20Sopenharmony_ci		*unc = (state->data[0] << 8) | state->data[1];
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	mutex_unlock(&state->data_mutex);
858c2ecf20Sopenharmony_ci	return ret;
868c2ecf20Sopenharmony_ci}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic int dtt200u_fe_read_signal_strength(struct dvb_frontend* fe, u16 *strength)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	struct dtt200u_fe_state *state = fe->demodulator_priv;
918c2ecf20Sopenharmony_ci	int ret;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	mutex_lock(&state->data_mutex);
948c2ecf20Sopenharmony_ci	state->data[0] = GET_AGC;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	ret = dvb_usb_generic_rw(state->d, state->data, 1, state->data, 1, 0);
978c2ecf20Sopenharmony_ci	if (ret >= 0)
988c2ecf20Sopenharmony_ci		*strength = (state->data[0] << 8) | state->data[0];
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	mutex_unlock(&state->data_mutex);
1018c2ecf20Sopenharmony_ci	return ret;
1028c2ecf20Sopenharmony_ci}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistatic int dtt200u_fe_read_snr(struct dvb_frontend* fe, u16 *snr)
1058c2ecf20Sopenharmony_ci{
1068c2ecf20Sopenharmony_ci	struct dtt200u_fe_state *state = fe->demodulator_priv;
1078c2ecf20Sopenharmony_ci	int ret;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	mutex_lock(&state->data_mutex);
1108c2ecf20Sopenharmony_ci	state->data[0] = GET_SNR;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	ret = dvb_usb_generic_rw(state->d, state->data, 1, state->data, 1, 0);
1138c2ecf20Sopenharmony_ci	if (ret >= 0)
1148c2ecf20Sopenharmony_ci		*snr = ~((state->data[0] << 8) | state->data[0]);
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	mutex_unlock(&state->data_mutex);
1178c2ecf20Sopenharmony_ci	return ret;
1188c2ecf20Sopenharmony_ci}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_cistatic int dtt200u_fe_init(struct dvb_frontend* fe)
1218c2ecf20Sopenharmony_ci{
1228c2ecf20Sopenharmony_ci	struct dtt200u_fe_state *state = fe->demodulator_priv;
1238c2ecf20Sopenharmony_ci	int ret;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	mutex_lock(&state->data_mutex);
1268c2ecf20Sopenharmony_ci	state->data[0] = SET_INIT;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	ret = dvb_usb_generic_write(state->d, state->data, 1);
1298c2ecf20Sopenharmony_ci	mutex_unlock(&state->data_mutex);
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	return ret;
1328c2ecf20Sopenharmony_ci}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_cistatic int dtt200u_fe_sleep(struct dvb_frontend* fe)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	return dtt200u_fe_init(fe);
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cistatic int dtt200u_fe_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings *tune)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	tune->min_delay_ms = 1500;
1428c2ecf20Sopenharmony_ci	tune->step_size = 0;
1438c2ecf20Sopenharmony_ci	tune->max_drift = 0;
1448c2ecf20Sopenharmony_ci	return 0;
1458c2ecf20Sopenharmony_ci}
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_cistatic int dtt200u_fe_set_frontend(struct dvb_frontend *fe)
1488c2ecf20Sopenharmony_ci{
1498c2ecf20Sopenharmony_ci	struct dtv_frontend_properties *fep = &fe->dtv_property_cache;
1508c2ecf20Sopenharmony_ci	struct dtt200u_fe_state *state = fe->demodulator_priv;
1518c2ecf20Sopenharmony_ci	int ret;
1528c2ecf20Sopenharmony_ci	u16 freq = fep->frequency / 250000;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	mutex_lock(&state->data_mutex);
1558c2ecf20Sopenharmony_ci	state->data[0] = SET_BANDWIDTH;
1568c2ecf20Sopenharmony_ci	switch (fep->bandwidth_hz) {
1578c2ecf20Sopenharmony_ci	case 8000000:
1588c2ecf20Sopenharmony_ci		state->data[1] = 8;
1598c2ecf20Sopenharmony_ci		break;
1608c2ecf20Sopenharmony_ci	case 7000000:
1618c2ecf20Sopenharmony_ci		state->data[1] = 7;
1628c2ecf20Sopenharmony_ci		break;
1638c2ecf20Sopenharmony_ci	case 6000000:
1648c2ecf20Sopenharmony_ci		state->data[1] = 6;
1658c2ecf20Sopenharmony_ci		break;
1668c2ecf20Sopenharmony_ci	default:
1678c2ecf20Sopenharmony_ci		ret = -EINVAL;
1688c2ecf20Sopenharmony_ci		goto ret;
1698c2ecf20Sopenharmony_ci	}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	ret = dvb_usb_generic_write(state->d, state->data, 2);
1728c2ecf20Sopenharmony_ci	if (ret < 0)
1738c2ecf20Sopenharmony_ci		goto ret;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	state->data[0] = SET_RF_FREQ;
1768c2ecf20Sopenharmony_ci	state->data[1] = freq & 0xff;
1778c2ecf20Sopenharmony_ci	state->data[2] = (freq >> 8) & 0xff;
1788c2ecf20Sopenharmony_ci	ret = dvb_usb_generic_write(state->d, state->data, 3);
1798c2ecf20Sopenharmony_ci	if (ret < 0)
1808c2ecf20Sopenharmony_ci		goto ret;
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ciret:
1838c2ecf20Sopenharmony_ci	mutex_unlock(&state->data_mutex);
1848c2ecf20Sopenharmony_ci	return ret;
1858c2ecf20Sopenharmony_ci}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cistatic int dtt200u_fe_get_frontend(struct dvb_frontend* fe,
1888c2ecf20Sopenharmony_ci				   struct dtv_frontend_properties *fep)
1898c2ecf20Sopenharmony_ci{
1908c2ecf20Sopenharmony_ci	struct dtt200u_fe_state *state = fe->demodulator_priv;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	memcpy(fep, &state->fep, sizeof(struct dtv_frontend_properties));
1938c2ecf20Sopenharmony_ci	return 0;
1948c2ecf20Sopenharmony_ci}
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_cistatic void dtt200u_fe_release(struct dvb_frontend* fe)
1978c2ecf20Sopenharmony_ci{
1988c2ecf20Sopenharmony_ci	struct dtt200u_fe_state *state = (struct dtt200u_fe_state*) fe->demodulator_priv;
1998c2ecf20Sopenharmony_ci	kfree(state);
2008c2ecf20Sopenharmony_ci}
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_cistatic const struct dvb_frontend_ops dtt200u_fe_ops;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_cistruct dvb_frontend* dtt200u_fe_attach(struct dvb_usb_device *d)
2058c2ecf20Sopenharmony_ci{
2068c2ecf20Sopenharmony_ci	struct dtt200u_fe_state* state = NULL;
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	/* allocate memory for the internal state */
2098c2ecf20Sopenharmony_ci	state = kzalloc(sizeof(struct dtt200u_fe_state), GFP_KERNEL);
2108c2ecf20Sopenharmony_ci	if (state == NULL)
2118c2ecf20Sopenharmony_ci		goto error;
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	deb_info("attaching frontend dtt200u\n");
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	state->d = d;
2168c2ecf20Sopenharmony_ci	mutex_init(&state->data_mutex);
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	memcpy(&state->frontend.ops,&dtt200u_fe_ops,sizeof(struct dvb_frontend_ops));
2198c2ecf20Sopenharmony_ci	state->frontend.demodulator_priv = state;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	return &state->frontend;
2228c2ecf20Sopenharmony_cierror:
2238c2ecf20Sopenharmony_ci	return NULL;
2248c2ecf20Sopenharmony_ci}
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_cistatic const struct dvb_frontend_ops dtt200u_fe_ops = {
2278c2ecf20Sopenharmony_ci	.delsys = { SYS_DVBT },
2288c2ecf20Sopenharmony_ci	.info = {
2298c2ecf20Sopenharmony_ci		.name			= "WideView USB DVB-T",
2308c2ecf20Sopenharmony_ci		.frequency_min_hz	=  44250 * kHz,
2318c2ecf20Sopenharmony_ci		.frequency_max_hz	= 867250 * kHz,
2328c2ecf20Sopenharmony_ci		.frequency_stepsize_hz	=    250 * kHz,
2338c2ecf20Sopenharmony_ci		.caps = FE_CAN_INVERSION_AUTO |
2348c2ecf20Sopenharmony_ci				FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
2358c2ecf20Sopenharmony_ci				FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
2368c2ecf20Sopenharmony_ci				FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
2378c2ecf20Sopenharmony_ci				FE_CAN_TRANSMISSION_MODE_AUTO |
2388c2ecf20Sopenharmony_ci				FE_CAN_GUARD_INTERVAL_AUTO |
2398c2ecf20Sopenharmony_ci				FE_CAN_RECOVER |
2408c2ecf20Sopenharmony_ci				FE_CAN_HIERARCHY_AUTO,
2418c2ecf20Sopenharmony_ci	},
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	.release = dtt200u_fe_release,
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	.init = dtt200u_fe_init,
2468c2ecf20Sopenharmony_ci	.sleep = dtt200u_fe_sleep,
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	.set_frontend = dtt200u_fe_set_frontend,
2498c2ecf20Sopenharmony_ci	.get_frontend = dtt200u_fe_get_frontend,
2508c2ecf20Sopenharmony_ci	.get_tune_settings = dtt200u_fe_get_tune_settings,
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	.read_status = dtt200u_fe_read_status,
2538c2ecf20Sopenharmony_ci	.read_ber = dtt200u_fe_read_ber,
2548c2ecf20Sopenharmony_ci	.read_signal_strength = dtt200u_fe_read_signal_strength,
2558c2ecf20Sopenharmony_ci	.read_snr = dtt200u_fe_read_snr,
2568c2ecf20Sopenharmony_ci	.read_ucblocks = dtt200u_fe_read_unc_blocks,
2578c2ecf20Sopenharmony_ci};
258