Linux-2.6.12-rc2
[linux-flexiantxendom0-natty.git] / drivers / media / dvb / ttusb-dec / ttusbdecfe.c
1 /*
2  * TTUSB DEC Frontend Driver
3  *
4  * Copyright (C) 2003-2004 Alex Woods <linux-dvb@giblets.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */
21
22 #include "dvb_frontend.h"
23 #include "ttusbdecfe.h"
24
25
26 #define LOF_HI                  10600000
27 #define LOF_LO                  9750000
28
29 struct ttusbdecfe_state {
30
31         struct dvb_frontend_ops ops;
32
33         /* configuration settings */
34         const struct ttusbdecfe_config* config;
35
36         struct dvb_frontend frontend;
37
38         u8 hi_band;
39         u8 voltage;
40 };
41
42
43 static int ttusbdecfe_read_status(struct dvb_frontend* fe, fe_status_t* status)
44 {
45         *status = FE_HAS_SIGNAL | FE_HAS_VITERBI |
46                   FE_HAS_SYNC | FE_HAS_CARRIER | FE_HAS_LOCK;
47
48         return 0;
49 }
50
51 static int ttusbdecfe_dvbt_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
52 {
53         struct ttusbdecfe_state* state = (struct ttusbdecfe_state*) fe->demodulator_priv;
54         u8 b[] = { 0x00, 0x00, 0x00, 0x03,
55                    0x00, 0x00, 0x00, 0x00,
56                    0x00, 0x00, 0x00, 0x01,
57                    0x00, 0x00, 0x00, 0xff,
58                    0x00, 0x00, 0x00, 0xff };
59
60         u32 freq = htonl(p->frequency / 1000);
61         memcpy(&b[4], &freq, sizeof (u32));
62         state->config->send_command(fe, 0x71, sizeof(b), b, NULL, NULL);
63
64         return 0;
65 }
66
67 static int ttusbdecfe_dvbs_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
68 {
69         struct ttusbdecfe_state* state = (struct ttusbdecfe_state*) fe->demodulator_priv;
70
71         u8 b[] = { 0x00, 0x00, 0x00, 0x01,
72                    0x00, 0x00, 0x00, 0x00,
73                    0x00, 0x00, 0x00, 0x01,
74                    0x00, 0x00, 0x00, 0x00,
75                    0x00, 0x00, 0x00, 0x00,
76                    0x00, 0x00, 0x00, 0x00,
77                    0x00, 0x00, 0x00, 0x00,
78                    0x00, 0x00, 0x00, 0x00,
79                    0x00, 0x00, 0x00, 0x00,
80                    0x00, 0x00, 0x00, 0x00 };
81         u32 freq;
82         u32 sym_rate;
83         u32 band;
84         u32 lnb_voltage;
85
86         freq = htonl(p->frequency +
87                (state->hi_band ? LOF_HI : LOF_LO));
88         memcpy(&b[4], &freq, sizeof(u32));
89         sym_rate = htonl(p->u.qam.symbol_rate);
90         memcpy(&b[12], &sym_rate, sizeof(u32));
91         band = htonl(state->hi_band ? LOF_HI : LOF_LO);
92         memcpy(&b[24], &band, sizeof(u32));
93         lnb_voltage = htonl(state->voltage);
94         memcpy(&b[28], &lnb_voltage, sizeof(u32));
95
96         state->config->send_command(fe, 0x71, sizeof(b), b, NULL, NULL);
97
98         return 0;
99 }
100
101 static int ttusbdecfe_dvbs_diseqc_send_master_cmd(struct dvb_frontend* fe, struct dvb_diseqc_master_cmd *cmd)
102 {
103         struct ttusbdecfe_state* state = (struct ttusbdecfe_state*) fe->demodulator_priv;
104         u8 b[] = { 0x00, 0xff, 0x00, 0x00,
105                    0x00, 0x00, 0x00, 0x00,
106                    0x00, 0x00 };
107
108         memcpy(&b[4], cmd->msg, cmd->msg_len);
109
110         state->config->send_command(fe, 0x72,
111                                     sizeof(b) - (6 - cmd->msg_len), b,
112                                     NULL, NULL);
113
114         return 0;
115 }
116
117
118 static int ttusbdecfe_dvbs_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
119 {
120         struct ttusbdecfe_state* state = (struct ttusbdecfe_state*) fe->demodulator_priv;
121
122         state->hi_band = (SEC_TONE_ON == tone);
123
124         return 0;
125 }
126
127
128 static int ttusbdecfe_dvbs_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
129 {
130         struct ttusbdecfe_state* state = (struct ttusbdecfe_state*) fe->demodulator_priv;
131
132         switch (voltage) {
133         case SEC_VOLTAGE_13:
134                 state->voltage = 13;
135                 break;
136         case SEC_VOLTAGE_18:
137                 state->voltage = 18;
138                 break;
139         default:
140                 return -EINVAL;
141         }
142
143         return 0;
144 }
145
146 static void ttusbdecfe_release(struct dvb_frontend* fe)
147 {
148         struct ttusbdecfe_state* state = (struct ttusbdecfe_state*) fe->demodulator_priv;
149         kfree(state);
150 }
151
152 static struct dvb_frontend_ops ttusbdecfe_dvbt_ops;
153
154 struct dvb_frontend* ttusbdecfe_dvbt_attach(const struct ttusbdecfe_config* config)
155 {
156         struct ttusbdecfe_state* state = NULL;
157
158         /* allocate memory for the internal state */
159         state = (struct ttusbdecfe_state*) kmalloc(sizeof(struct ttusbdecfe_state), GFP_KERNEL);
160         if (state == NULL) goto error;
161
162         /* setup the state */
163         state->config = config;
164         memcpy(&state->ops, &ttusbdecfe_dvbt_ops, sizeof(struct dvb_frontend_ops));
165
166         /* create dvb_frontend */
167         state->frontend.ops = &state->ops;
168         state->frontend.demodulator_priv = state;
169         return &state->frontend;
170
171 error:
172         kfree(state);
173         return NULL;
174 }
175
176 static struct dvb_frontend_ops ttusbdecfe_dvbs_ops;
177
178 struct dvb_frontend* ttusbdecfe_dvbs_attach(const struct ttusbdecfe_config* config)
179 {
180         struct ttusbdecfe_state* state = NULL;
181
182         /* allocate memory for the internal state */
183         state = (struct ttusbdecfe_state*) kmalloc(sizeof(struct ttusbdecfe_state), GFP_KERNEL);
184         if (state == NULL) goto error;
185
186         /* setup the state */
187         state->config = config;
188         state->voltage = 0;
189         state->hi_band = 0;
190         memcpy(&state->ops, &ttusbdecfe_dvbs_ops, sizeof(struct dvb_frontend_ops));
191
192         /* create dvb_frontend */
193         state->frontend.ops = &state->ops;
194         state->frontend.demodulator_priv = state;
195         return &state->frontend;
196
197 error:
198         kfree(state);
199         return NULL;
200 }
201
202 static struct dvb_frontend_ops ttusbdecfe_dvbt_ops = {
203
204         .info = {
205                 .name                   = "TechnoTrend/Hauppauge DEC2000-t Frontend",
206                 .type                   = FE_OFDM,
207                 .frequency_min          = 51000000,
208                 .frequency_max          = 858000000,
209                 .frequency_stepsize     = 62500,
210                 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
211                         FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
212                         FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
213                         FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
214                         FE_CAN_HIERARCHY_AUTO,
215         },
216
217         .release = ttusbdecfe_release,
218
219         .set_frontend = ttusbdecfe_dvbt_set_frontend,
220
221         .read_status = ttusbdecfe_read_status,
222 };
223
224 static struct dvb_frontend_ops ttusbdecfe_dvbs_ops = {
225
226         .info = {
227                 .name                   = "TechnoTrend/Hauppauge DEC3000-s Frontend",
228                 .type                   = FE_QPSK,
229                 .frequency_min          = 950000,
230                 .frequency_max          = 2150000,
231                 .frequency_stepsize     = 125,
232                 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
233                         FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
234                         FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
235                         FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
236                         FE_CAN_HIERARCHY_AUTO,
237         },
238
239         .release = ttusbdecfe_release,
240
241         .set_frontend = ttusbdecfe_dvbs_set_frontend,
242
243         .read_status = ttusbdecfe_read_status,
244
245         .diseqc_send_master_cmd = ttusbdecfe_dvbs_diseqc_send_master_cmd,
246         .set_voltage = ttusbdecfe_dvbs_set_voltage,
247         .set_tone = ttusbdecfe_dvbs_set_tone,
248 };
249
250 MODULE_DESCRIPTION("TTUSB DEC DVB-T/S Demodulator driver");
251 MODULE_AUTHOR("Alex Woods/Andrew de Quincey");
252 MODULE_LICENSE("GPL");
253
254 EXPORT_SYMBOL(ttusbdecfe_dvbt_attach);
255 EXPORT_SYMBOL(ttusbdecfe_dvbs_attach);