Merge branch 'setns'
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / media / dvb / dvb-usb / dtt200u-fe.c
CommitLineData
fb41f5a7
PB
1/* Frontend part of the Linux driver for the WideView/ Yakumo/ Hama/
2 * Typhoon/ Yuan DVB-T USB2.0 receiver.
776338e1
JS
3 *
4 * Copyright (C) 2005 Patrick Boettcher <patrick.boettcher@desy.de>
5 *
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.
9 *
10 * see Documentation/dvb/README.dvb-usb for more information
11 */
12#include "dtt200u.h"
13
14struct dtt200u_fe_state {
15 struct dvb_usb_device *d;
16
d590f9c2
PB
17 fe_status_t stat;
18
776338e1
JS
19 struct dvb_frontend_parameters fep;
20 struct dvb_frontend frontend;
21};
22
776338e1
JS
23static int dtt200u_fe_read_status(struct dvb_frontend* fe, fe_status_t *stat)
24{
25 struct dtt200u_fe_state *state = fe->demodulator_priv;
d590f9c2
PB
26 u8 st = GET_TUNE_STATUS, b[3];
27
28 dvb_usb_generic_rw(state->d,&st,1,b,3,0);
776338e1 29
d590f9c2 30 switch (b[0]) {
776338e1 31 case 0x01:
d590f9c2
PB
32 *stat = FE_HAS_SIGNAL | FE_HAS_CARRIER |
33 FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
776338e1 34 break;
d590f9c2
PB
35 case 0x00: /* pending */
36 *stat = FE_TIMEDOUT; /* during set_frontend */
776338e1
JS
37 break;
38 default:
d590f9c2
PB
39 case 0x02: /* failed */
40 *stat = 0;
776338e1
JS
41 break;
42 }
776338e1
JS
43 return 0;
44}
d590f9c2 45
776338e1
JS
46static int dtt200u_fe_read_ber(struct dvb_frontend* fe, u32 *ber)
47{
48 struct dtt200u_fe_state *state = fe->demodulator_priv;
d590f9c2
PB
49 u8 bw = GET_VIT_ERR_CNT,b[3];
50 dvb_usb_generic_rw(state->d,&bw,1,b,3,0);
51 *ber = (b[0] << 16) | (b[1] << 8) | b[2];
776338e1
JS
52 return 0;
53}
54
55static int dtt200u_fe_read_unc_blocks(struct dvb_frontend* fe, u32 *unc)
56{
57 struct dtt200u_fe_state *state = fe->demodulator_priv;
d590f9c2
PB
58 u8 bw = GET_RS_UNCOR_BLK_CNT,b[2];
59
60 dvb_usb_generic_rw(state->d,&bw,1,b,2,0);
61 *unc = (b[0] << 8) | b[1];
776338e1
JS
62 return 0;
63}
64
65static int dtt200u_fe_read_signal_strength(struct dvb_frontend* fe, u16 *strength)
66{
67 struct dtt200u_fe_state *state = fe->demodulator_priv;
d590f9c2 68 u8 bw = GET_AGC, b;
776338e1
JS
69 dvb_usb_generic_rw(state->d,&bw,1,&b,1,0);
70 *strength = (b << 8) | b;
71 return 0;
72}
73
74static int dtt200u_fe_read_snr(struct dvb_frontend* fe, u16 *snr)
75{
76 struct dtt200u_fe_state *state = fe->demodulator_priv;
77 u8 bw = GET_SNR,br;
78 dvb_usb_generic_rw(state->d,&bw,1,&br,1,0);
79 *snr = ~((br << 8) | br);
80 return 0;
81}
82
83static int dtt200u_fe_init(struct dvb_frontend* fe)
84{
85 struct dtt200u_fe_state *state = fe->demodulator_priv;
d590f9c2 86 u8 b = SET_INIT;
776338e1
JS
87 return dvb_usb_generic_write(state->d,&b,1);
88}
89
90static int dtt200u_fe_sleep(struct dvb_frontend* fe)
91{
92 return dtt200u_fe_init(fe);
93}
94
95static int dtt200u_fe_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings *tune)
96{
97 tune->min_delay_ms = 1500;
d590f9c2
PB
98 tune->step_size = 0;
99 tune->max_drift = 0;
776338e1
JS
100 return 0;
101}
102
103static int dtt200u_fe_set_frontend(struct dvb_frontend* fe,
104 struct dvb_frontend_parameters *fep)
105{
106 struct dtt200u_fe_state *state = fe->demodulator_priv;
d590f9c2
PB
107 int i;
108 fe_status_t st;
776338e1 109 u16 freq = fep->frequency / 250000;
d590f9c2 110 u8 bwbuf[2] = { SET_BANDWIDTH, 0 },freqbuf[3] = { SET_RF_FREQ, 0, 0 };
776338e1
JS
111
112 switch (fep->u.ofdm.bandwidth) {
d590f9c2
PB
113 case BANDWIDTH_8_MHZ: bwbuf[1] = 8; break;
114 case BANDWIDTH_7_MHZ: bwbuf[1] = 7; break;
115 case BANDWIDTH_6_MHZ: bwbuf[1] = 6; break;
776338e1
JS
116 case BANDWIDTH_AUTO: return -EOPNOTSUPP;
117 default:
118 return -EINVAL;
119 }
776338e1 120
776338e1
JS
121 dvb_usb_generic_write(state->d,bwbuf,2);
122
123 freqbuf[1] = freq & 0xff;
124 freqbuf[2] = (freq >> 8) & 0xff;
125 dvb_usb_generic_write(state->d,freqbuf,3);
126
d590f9c2
PB
127 for (i = 0; i < 30; i++) {
128 msleep(20);
129 dtt200u_fe_read_status(fe, &st);
130 if (st & FE_TIMEDOUT)
131 continue;
132 }
776338e1
JS
133
134 return 0;
135}
136
137static int dtt200u_fe_get_frontend(struct dvb_frontend* fe,
138 struct dvb_frontend_parameters *fep)
139{
140 struct dtt200u_fe_state *state = fe->demodulator_priv;
141 memcpy(fep,&state->fep,sizeof(struct dvb_frontend_parameters));
142 return 0;
143}
144
145static void dtt200u_fe_release(struct dvb_frontend* fe)
146{
147 struct dtt200u_fe_state *state = (struct dtt200u_fe_state*) fe->demodulator_priv;
148 kfree(state);
149}
150
151static struct dvb_frontend_ops dtt200u_fe_ops;
152
153struct dvb_frontend* dtt200u_fe_attach(struct dvb_usb_device *d)
154{
155 struct dtt200u_fe_state* state = NULL;
156
157 /* allocate memory for the internal state */
7408187d 158 state = kzalloc(sizeof(struct dtt200u_fe_state), GFP_KERNEL);
776338e1
JS
159 if (state == NULL)
160 goto error;
776338e1
JS
161
162 deb_info("attaching frontend dtt200u\n");
163
164 state->d = d;
165
dea74869 166 memcpy(&state->frontend.ops,&dtt200u_fe_ops,sizeof(struct dvb_frontend_ops));
776338e1
JS
167 state->frontend.demodulator_priv = state;
168
8397703e 169 return &state->frontend;
776338e1
JS
170error:
171 return NULL;
776338e1
JS
172}
173
174static struct dvb_frontend_ops dtt200u_fe_ops = {
175 .info = {
d590f9c2 176 .name = "WideView USB DVB-T",
776338e1
JS
177 .type = FE_OFDM,
178 .frequency_min = 44250000,
179 .frequency_max = 867250000,
180 .frequency_stepsize = 250000,
181 .caps = FE_CAN_INVERSION_AUTO |
182 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
183 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
184 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
185 FE_CAN_TRANSMISSION_MODE_AUTO |
186 FE_CAN_GUARD_INTERVAL_AUTO |
187 FE_CAN_RECOVER |
188 FE_CAN_HIERARCHY_AUTO,
189 },
190
191 .release = dtt200u_fe_release,
192
193 .init = dtt200u_fe_init,
194 .sleep = dtt200u_fe_sleep,
195
196 .set_frontend = dtt200u_fe_set_frontend,
197 .get_frontend = dtt200u_fe_get_frontend,
198 .get_tune_settings = dtt200u_fe_get_tune_settings,
199
200 .read_status = dtt200u_fe_read_status,
201 .read_ber = dtt200u_fe_read_ber,
202 .read_signal_strength = dtt200u_fe_read_signal_strength,
203 .read_snr = dtt200u_fe_read_snr,
204 .read_ucblocks = dtt200u_fe_read_unc_blocks,
205};