dtt200u-fe.c 5.8 KB
Newer Older
1 2
/* Frontend part of the Linux driver for the WideView/ Yakumo/ Hama/
 * Typhoon/ Yuan DVB-T USB2.0 receiver.
3
 *
4
 * Copyright (C) 2005 Patrick Boettcher <patrick.boettcher@posteo.de>
5 6 7 8 9 10 11 12 13 14 15 16
 *
 *	This program is free software; you can redistribute it and/or modify it
 *	under the terms of the GNU General Public License as published by the Free
 *	Software Foundation, version 2.
 *
 * see Documentation/dvb/README.dvb-usb for more information
 */
#include "dtt200u.h"

struct dtt200u_fe_state {
	struct dvb_usb_device *d;

17
	enum fe_status stat;
18

19
	struct dtv_frontend_properties fep;
20
	struct dvb_frontend frontend;
21 22 23

	unsigned char data[80];
	struct mutex data_mutex;
24 25
};

26 27
static int dtt200u_fe_read_status(struct dvb_frontend *fe,
				  enum fe_status *stat)
28 29
{
	struct dtt200u_fe_state *state = fe->demodulator_priv;
30

31 32 33 34
	mutex_lock(&state->data_mutex);
	state->data[0] = GET_TUNE_STATUS;

	dvb_usb_generic_rw(state->d, state->data, 1, state->data, 3, 0);
35

36
	switch (state->data[0]) {
37
		case 0x01:
38 39
			*stat = FE_HAS_SIGNAL | FE_HAS_CARRIER |
				FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
40
			break;
41 42
		case 0x00: /* pending */
			*stat = FE_TIMEDOUT; /* during set_frontend */
43 44
			break;
		default:
45 46
		case 0x02: /* failed */
			*stat = 0;
47 48
			break;
	}
49
	mutex_unlock(&state->data_mutex);
50 51
	return 0;
}
52

53 54 55
static int dtt200u_fe_read_ber(struct dvb_frontend* fe, u32 *ber)
{
	struct dtt200u_fe_state *state = fe->demodulator_priv;
56 57 58 59 60 61 62 63

	mutex_lock(&state->data_mutex);
	state->data[0] = GET_VIT_ERR_CNT;

	dvb_usb_generic_rw(state->d, state->data, 1, state->data, 3, 0);
	*ber = (state->data[0] << 16) | (state->data[1] << 8) | state->data[2];

	mutex_unlock(&state->data_mutex);
64 65 66 67 68 69
	return 0;
}

static int dtt200u_fe_read_unc_blocks(struct dvb_frontend* fe, u32 *unc)
{
	struct dtt200u_fe_state *state = fe->demodulator_priv;
70

71 72 73 74 75 76 77
	mutex_lock(&state->data_mutex);
	state->data[0] = GET_RS_UNCOR_BLK_CNT;

	dvb_usb_generic_rw(state->d, state->data, 1, state->data, 2, 0);

	mutex_unlock(&state->data_mutex);
	return ret;
78 79 80 81 82
}

static int dtt200u_fe_read_signal_strength(struct dvb_frontend* fe, u16 *strength)
{
	struct dtt200u_fe_state *state = fe->demodulator_priv;
83 84 85 86 87 88 89 90

	mutex_lock(&state->data_mutex);
	state->data[0] = GET_AGC;

	dvb_usb_generic_rw(state->d, state->data, 1, state->data, 1, 0);

	mutex_unlock(&state->data_mutex);
	return ret;
91 92 93 94 95
}

static int dtt200u_fe_read_snr(struct dvb_frontend* fe, u16 *snr)
{
	struct dtt200u_fe_state *state = fe->demodulator_priv;
96 97 98 99 100 101 102 103

	mutex_lock(&state->data_mutex);
	state->data[0] = GET_SNR;

	dvb_usb_generic_rw(state->d, state->data, 1, state->data, 1, 0);

	mutex_unlock(&state->data_mutex);
	return ret;
104 105 106 107 108
}

static int dtt200u_fe_init(struct dvb_frontend* fe)
{
	struct dtt200u_fe_state *state = fe->demodulator_priv;
109 110 111 112 113 114 115 116 117
	int ret;

	mutex_lock(&state->data_mutex);
	state->data[0] = SET_INIT;

	ret = dvb_usb_generic_write(state->d, state->data, 1);
	mutex_unlock(&state->data_mutex);

	return ret;
118 119 120 121 122 123 124 125 126 127
}

static int dtt200u_fe_sleep(struct dvb_frontend* fe)
{
	return dtt200u_fe_init(fe);
}

static int dtt200u_fe_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings *tune)
{
	tune->min_delay_ms = 1500;
128 129
	tune->step_size = 0;
	tune->max_drift = 0;
130 131 132
	return 0;
}

133
static int dtt200u_fe_set_frontend(struct dvb_frontend *fe)
134
{
135
	struct dtv_frontend_properties *fep = &fe->dtv_property_cache;
136 137 138
	struct dtt200u_fe_state *state = fe->demodulator_priv;
	u16 freq = fep->frequency / 250000;

139 140
	mutex_lock(&state->data_mutex);
	state->data[0] = SET_BANDWIDTH;
141 142
	switch (fep->bandwidth_hz) {
	case 8000000:
143
		state->data[1] = 8;
144 145
		break;
	case 7000000:
146
		state->data[1] = 7;
147 148
		break;
	case 6000000:
149
		state->data[1] = 6;
150 151
		break;
	default:
152 153
		ret = -EINVAL;
		goto ret;
154 155
	}

156
	dvb_usb_generic_write(state->d, state->data, 2);
157

158 159 160 161
	state->data[0] = SET_RF_FREQ;
	state->data[1] = freq & 0xff;
	state->data[2] = (freq >> 8) & 0xff;
	dvb_usb_generic_write(state->d, state->data, 3);
162

163 164 165
ret:
	mutex_unlock(&state->data_mutex);
	return ret;
166 167
}

168 169
static int dtt200u_fe_get_frontend(struct dvb_frontend* fe,
				   struct dtv_frontend_properties *fep)
170 171
{
	struct dtt200u_fe_state *state = fe->demodulator_priv;
172

173
	memcpy(fep, &state->fep, sizeof(struct dtv_frontend_properties));
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
	return 0;
}

static void dtt200u_fe_release(struct dvb_frontend* fe)
{
	struct dtt200u_fe_state *state = (struct dtt200u_fe_state*) fe->demodulator_priv;
	kfree(state);
}

static struct dvb_frontend_ops dtt200u_fe_ops;

struct dvb_frontend* dtt200u_fe_attach(struct dvb_usb_device *d)
{
	struct dtt200u_fe_state* state = NULL;

	/* allocate memory for the internal state */
P
 
Panagiotis Issaris 已提交
190
	state = kzalloc(sizeof(struct dtt200u_fe_state), GFP_KERNEL);
191 192 193 194 195 196
	if (state == NULL)
		goto error;

	deb_info("attaching frontend dtt200u\n");

	state->d = d;
197
	mutex_init(&state->data_mutex);
198

199
	memcpy(&state->frontend.ops,&dtt200u_fe_ops,sizeof(struct dvb_frontend_ops));
200 201
	state->frontend.demodulator_priv = state;

202
	return &state->frontend;
203 204 205 206 207
error:
	return NULL;
}

static struct dvb_frontend_ops dtt200u_fe_ops = {
208
	.delsys = { SYS_DVBT },
209
	.info = {
210
		.name			= "WideView USB DVB-T",
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
		.frequency_min		= 44250000,
		.frequency_max		= 867250000,
		.frequency_stepsize	= 250000,
		.caps = FE_CAN_INVERSION_AUTO |
				FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
				FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
				FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
				FE_CAN_TRANSMISSION_MODE_AUTO |
				FE_CAN_GUARD_INTERVAL_AUTO |
				FE_CAN_RECOVER |
				FE_CAN_HIERARCHY_AUTO,
	},

	.release = dtt200u_fe_release,

	.init = dtt200u_fe_init,
	.sleep = dtt200u_fe_sleep,

229 230
	.set_frontend = dtt200u_fe_set_frontend,
	.get_frontend = dtt200u_fe_get_frontend,
231 232 233 234 235 236 237 238
	.get_tune_settings = dtt200u_fe_get_tune_settings,

	.read_status = dtt200u_fe_read_status,
	.read_ber = dtt200u_fe_read_ber,
	.read_signal_strength = dtt200u_fe_read_signal_strength,
	.read_snr = dtt200u_fe_read_snr,
	.read_ucblocks = dtt200u_fe_read_unc_blocks,
};