Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / drivers / media / usb / dvb-usb / dtt200u-fe.c
blob00f565fe7cc2c0b86f3c4b1b91234a57dd0df331
1 /* Frontend part of the Linux driver for the WideView/ Yakumo/ Hama/
2 * Typhoon/ Yuan DVB-T USB2.0 receiver.
4 * Copyright (C) 2005 Patrick Boettcher <patrick.boettcher@posteo.de>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation, version 2.
10 * see Documentation/dvb/README.dvb-usb for more information
12 #include "dtt200u.h"
14 struct dtt200u_fe_state {
15 struct dvb_usb_device *d;
17 enum fe_status stat;
19 struct dtv_frontend_properties fep;
20 struct dvb_frontend frontend;
22 unsigned char data[80];
23 struct mutex data_mutex;
26 static int dtt200u_fe_read_status(struct dvb_frontend *fe,
27 enum fe_status *stat)
29 struct dtt200u_fe_state *state = fe->demodulator_priv;
30 int ret;
32 mutex_lock(&state->data_mutex);
33 state->data[0] = GET_TUNE_STATUS;
35 ret = dvb_usb_generic_rw(state->d, state->data, 1, state->data, 3, 0);
36 if (ret < 0) {
37 *stat = 0;
38 mutex_unlock(&state->data_mutex);
39 return ret;
42 switch (state->data[0]) {
43 case 0x01:
44 *stat = FE_HAS_SIGNAL | FE_HAS_CARRIER |
45 FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
46 break;
47 case 0x00: /* pending */
48 *stat = FE_TIMEDOUT; /* during set_frontend */
49 break;
50 default:
51 case 0x02: /* failed */
52 *stat = 0;
53 break;
55 mutex_unlock(&state->data_mutex);
56 return 0;
59 static int dtt200u_fe_read_ber(struct dvb_frontend* fe, u32 *ber)
61 struct dtt200u_fe_state *state = fe->demodulator_priv;
62 int ret;
64 mutex_lock(&state->data_mutex);
65 state->data[0] = GET_VIT_ERR_CNT;
67 ret = dvb_usb_generic_rw(state->d, state->data, 1, state->data, 3, 0);
68 if (ret >= 0)
69 *ber = (state->data[0] << 16) | (state->data[1] << 8) | state->data[2];
71 mutex_unlock(&state->data_mutex);
72 return ret;
75 static int dtt200u_fe_read_unc_blocks(struct dvb_frontend* fe, u32 *unc)
77 struct dtt200u_fe_state *state = fe->demodulator_priv;
78 int ret;
80 mutex_lock(&state->data_mutex);
81 state->data[0] = GET_RS_UNCOR_BLK_CNT;
83 ret = dvb_usb_generic_rw(state->d, state->data, 1, state->data, 2, 0);
84 if (ret >= 0)
85 *unc = (state->data[0] << 8) | state->data[1];
87 mutex_unlock(&state->data_mutex);
88 return ret;
91 static int dtt200u_fe_read_signal_strength(struct dvb_frontend* fe, u16 *strength)
93 struct dtt200u_fe_state *state = fe->demodulator_priv;
94 int ret;
96 mutex_lock(&state->data_mutex);
97 state->data[0] = GET_AGC;
99 ret = dvb_usb_generic_rw(state->d, state->data, 1, state->data, 1, 0);
100 if (ret >= 0)
101 *strength = (state->data[0] << 8) | state->data[0];
103 mutex_unlock(&state->data_mutex);
104 return ret;
107 static int dtt200u_fe_read_snr(struct dvb_frontend* fe, u16 *snr)
109 struct dtt200u_fe_state *state = fe->demodulator_priv;
110 int ret;
112 mutex_lock(&state->data_mutex);
113 state->data[0] = GET_SNR;
115 ret = dvb_usb_generic_rw(state->d, state->data, 1, state->data, 1, 0);
116 if (ret >= 0)
117 *snr = ~((state->data[0] << 8) | state->data[0]);
119 mutex_unlock(&state->data_mutex);
120 return ret;
123 static int dtt200u_fe_init(struct dvb_frontend* fe)
125 struct dtt200u_fe_state *state = fe->demodulator_priv;
126 int ret;
128 mutex_lock(&state->data_mutex);
129 state->data[0] = SET_INIT;
131 ret = dvb_usb_generic_write(state->d, state->data, 1);
132 mutex_unlock(&state->data_mutex);
134 return ret;
137 static int dtt200u_fe_sleep(struct dvb_frontend* fe)
139 return dtt200u_fe_init(fe);
142 static int dtt200u_fe_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings *tune)
144 tune->min_delay_ms = 1500;
145 tune->step_size = 0;
146 tune->max_drift = 0;
147 return 0;
150 static int dtt200u_fe_set_frontend(struct dvb_frontend *fe)
152 struct dtv_frontend_properties *fep = &fe->dtv_property_cache;
153 struct dtt200u_fe_state *state = fe->demodulator_priv;
154 int ret;
155 u16 freq = fep->frequency / 250000;
157 mutex_lock(&state->data_mutex);
158 state->data[0] = SET_BANDWIDTH;
159 switch (fep->bandwidth_hz) {
160 case 8000000:
161 state->data[1] = 8;
162 break;
163 case 7000000:
164 state->data[1] = 7;
165 break;
166 case 6000000:
167 state->data[1] = 6;
168 break;
169 default:
170 ret = -EINVAL;
171 goto ret;
174 ret = dvb_usb_generic_write(state->d, state->data, 2);
175 if (ret < 0)
176 goto ret;
178 state->data[0] = SET_RF_FREQ;
179 state->data[1] = freq & 0xff;
180 state->data[2] = (freq >> 8) & 0xff;
181 ret = dvb_usb_generic_write(state->d, state->data, 3);
182 if (ret < 0)
183 goto ret;
185 ret:
186 mutex_unlock(&state->data_mutex);
187 return ret;
190 static int dtt200u_fe_get_frontend(struct dvb_frontend* fe,
191 struct dtv_frontend_properties *fep)
193 struct dtt200u_fe_state *state = fe->demodulator_priv;
195 memcpy(fep, &state->fep, sizeof(struct dtv_frontend_properties));
196 return 0;
199 static void dtt200u_fe_release(struct dvb_frontend* fe)
201 struct dtt200u_fe_state *state = (struct dtt200u_fe_state*) fe->demodulator_priv;
202 kfree(state);
205 static const struct dvb_frontend_ops dtt200u_fe_ops;
207 struct dvb_frontend* dtt200u_fe_attach(struct dvb_usb_device *d)
209 struct dtt200u_fe_state* state = NULL;
211 /* allocate memory for the internal state */
212 state = kzalloc(sizeof(struct dtt200u_fe_state), GFP_KERNEL);
213 if (state == NULL)
214 goto error;
216 deb_info("attaching frontend dtt200u\n");
218 state->d = d;
219 mutex_init(&state->data_mutex);
221 memcpy(&state->frontend.ops,&dtt200u_fe_ops,sizeof(struct dvb_frontend_ops));
222 state->frontend.demodulator_priv = state;
224 return &state->frontend;
225 error:
226 return NULL;
229 static const struct dvb_frontend_ops dtt200u_fe_ops = {
230 .delsys = { SYS_DVBT },
231 .info = {
232 .name = "WideView USB DVB-T",
233 .frequency_min = 44250000,
234 .frequency_max = 867250000,
235 .frequency_stepsize = 250000,
236 .caps = FE_CAN_INVERSION_AUTO |
237 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
238 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
239 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
240 FE_CAN_TRANSMISSION_MODE_AUTO |
241 FE_CAN_GUARD_INTERVAL_AUTO |
242 FE_CAN_RECOVER |
243 FE_CAN_HIERARCHY_AUTO,
246 .release = dtt200u_fe_release,
248 .init = dtt200u_fe_init,
249 .sleep = dtt200u_fe_sleep,
251 .set_frontend = dtt200u_fe_set_frontend,
252 .get_frontend = dtt200u_fe_get_frontend,
253 .get_tune_settings = dtt200u_fe_get_tune_settings,
255 .read_status = dtt200u_fe_read_status,
256 .read_ber = dtt200u_fe_read_ber,
257 .read_signal_strength = dtt200u_fe_read_signal_strength,
258 .read_snr = dtt200u_fe_read_snr,
259 .read_ucblocks = dtt200u_fe_read_unc_blocks,