Merge git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi-rc-fixes-2.6
[linux-2.6] / drivers / media / dvb / dvb-usb / gp8psk-fe.c
1 /* DVB USB compliant Linux driver for the
2  *  - GENPIX 8pks/qpsk/DCII USB2.0 DVB-S module
3  *
4  * Copyright (C) 2006,2007 Alan Nisota (alannisota@gmail.com)
5  * Copyright (C) 2006,2007 Genpix Electronics (genpix@genpix-electronics.com)
6  *
7  * Thanks to GENPIX for the sample code used to implement this module.
8  *
9  * This module is based off the vp7045 and vp702x modules
10  *
11  *      This program is free software; you can redistribute it and/or modify it
12  *      under the terms of the GNU General Public License as published by the Free
13  *      Software Foundation, version 2.
14  *
15  * see Documentation/dvb/README.dvb-usb for more information
16  */
17 #include "gp8psk.h"
18
19 struct gp8psk_fe_state {
20         struct dvb_frontend fe;
21         struct dvb_usb_device *d;
22         u8 lock;
23         u16 snr;
24         unsigned long next_status_check;
25         unsigned long status_check_interval;
26 };
27
28 static int gp8psk_fe_update_status(struct gp8psk_fe_state *st)
29 {
30         u8 buf[6];
31         if (time_after(jiffies,st->next_status_check)) {
32                 gp8psk_usb_in_op(st->d, GET_SIGNAL_LOCK, 0,0,&st->lock,1);
33                 gp8psk_usb_in_op(st->d, GET_SIGNAL_STRENGTH, 0,0,buf,6);
34                 st->snr = (buf[1]) << 8 | buf[0];
35                 st->next_status_check = jiffies + (st->status_check_interval*HZ)/1000;
36         }
37         return 0;
38 }
39
40 static int gp8psk_fe_read_status(struct dvb_frontend* fe, fe_status_t *status)
41 {
42         struct gp8psk_fe_state *st = fe->demodulator_priv;
43         gp8psk_fe_update_status(st);
44
45         if (st->lock)
46                 *status = FE_HAS_LOCK | FE_HAS_SYNC | FE_HAS_VITERBI | FE_HAS_SIGNAL | FE_HAS_CARRIER;
47         else
48                 *status = 0;
49
50         if (*status & FE_HAS_LOCK)
51                 st->status_check_interval = 1000;
52         else
53                 st->status_check_interval = 100;
54         return 0;
55 }
56
57 /* not supported by this Frontend */
58 static int gp8psk_fe_read_ber(struct dvb_frontend* fe, u32 *ber)
59 {
60         (void) fe;
61         *ber = 0;
62         return 0;
63 }
64
65 /* not supported by this Frontend */
66 static int gp8psk_fe_read_unc_blocks(struct dvb_frontend* fe, u32 *unc)
67 {
68         (void) fe;
69         *unc = 0;
70         return 0;
71 }
72
73 static int gp8psk_fe_read_snr(struct dvb_frontend* fe, u16 *snr)
74 {
75         struct gp8psk_fe_state *st = fe->demodulator_priv;
76         gp8psk_fe_update_status(st);
77         /* snr is reported in dBu*256 */
78         *snr = st->snr;
79         return 0;
80 }
81
82 static int gp8psk_fe_read_signal_strength(struct dvb_frontend* fe, u16 *strength)
83 {
84         struct gp8psk_fe_state *st = fe->demodulator_priv;
85         gp8psk_fe_update_status(st);
86         /* snr is reported in dBu*256 */
87         /* snr / 38.4 ~= 100% strength */
88         /* snr * 17 returns 100% strength as 65535 */
89         if (st->snr > 0xf00)
90                 *strength = 0xffff;
91         else
92                 *strength = (st->snr << 4) + st->snr; /* snr*17 */
93         return 0;
94 }
95
96 static int gp8psk_fe_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings *tune)
97 {
98         tune->min_delay_ms = 200;
99         return 0;
100 }
101
102 static int gp8psk_fe_set_frontend(struct dvb_frontend* fe,
103                                   struct dvb_frontend_parameters *fep)
104 {
105         struct gp8psk_fe_state *state = fe->demodulator_priv;
106         u8 cmd[10];
107         u32 freq = fep->frequency * 1000;
108
109         cmd[4] = freq         & 0xff;
110         cmd[5] = (freq >> 8)  & 0xff;
111         cmd[6] = (freq >> 16) & 0xff;
112         cmd[7] = (freq >> 24) & 0xff;
113
114         switch(fe->ops.info.type) {
115         case FE_QPSK:
116                 cmd[0] =  fep->u.qpsk.symbol_rate        & 0xff;
117                 cmd[1] = (fep->u.qpsk.symbol_rate >>  8) & 0xff;
118                 cmd[2] = (fep->u.qpsk.symbol_rate >> 16) & 0xff;
119                 cmd[3] = (fep->u.qpsk.symbol_rate >> 24) & 0xff;
120                 cmd[8] = ADV_MOD_DVB_QPSK;
121                 cmd[9] = 0x03; /*ADV_MOD_FEC_XXX*/
122                 break;
123         default:
124                 // other modes are unsuported right now
125                 cmd[0] = 0;
126                 cmd[1] = 0;
127                 cmd[2] = 0;
128                 cmd[3] = 0;
129                 cmd[8] = 0;
130                 cmd[9] = 0;
131                 break;
132         }
133
134         gp8psk_usb_out_op(state->d,TUNE_8PSK,0,0,cmd,10);
135
136         state->lock = 0;
137         state->next_status_check = jiffies;
138         state->status_check_interval = 200;
139
140         return 0;
141 }
142
143 static int gp8psk_fe_get_frontend(struct dvb_frontend* fe,
144                                   struct dvb_frontend_parameters *fep)
145 {
146         return 0;
147 }
148
149
150 static int gp8psk_fe_send_diseqc_msg (struct dvb_frontend* fe,
151                                     struct dvb_diseqc_master_cmd *m)
152 {
153         struct gp8psk_fe_state *st = fe->demodulator_priv;
154
155         deb_fe("%s\n",__func__);
156
157         if (gp8psk_usb_out_op(st->d,SEND_DISEQC_COMMAND, m->msg[0], 0,
158                         m->msg, m->msg_len)) {
159                 return -EINVAL;
160         }
161         return 0;
162 }
163
164 static int gp8psk_fe_send_diseqc_burst (struct dvb_frontend* fe,
165                                     fe_sec_mini_cmd_t burst)
166 {
167         struct gp8psk_fe_state *st = fe->demodulator_priv;
168         u8 cmd;
169
170         deb_fe("%s\n",__func__);
171
172         /* These commands are certainly wrong */
173         cmd = (burst == SEC_MINI_A) ? 0x00 : 0x01;
174
175         if (gp8psk_usb_out_op(st->d,SEND_DISEQC_COMMAND, cmd, 0,
176                         &cmd, 0)) {
177                 return -EINVAL;
178         }
179         return 0;
180 }
181
182 static int gp8psk_fe_set_tone (struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
183 {
184         struct gp8psk_fe_state* state = fe->demodulator_priv;
185
186         if (gp8psk_usb_out_op(state->d,SET_22KHZ_TONE,
187                  (tone == SEC_TONE_ON), 0, NULL, 0)) {
188                 return -EINVAL;
189         }
190         return 0;
191 }
192
193 static int gp8psk_fe_set_voltage (struct dvb_frontend* fe, fe_sec_voltage_t voltage)
194 {
195         struct gp8psk_fe_state* state = fe->demodulator_priv;
196
197         if (gp8psk_usb_out_op(state->d,SET_LNB_VOLTAGE,
198                          voltage == SEC_VOLTAGE_18, 0, NULL, 0)) {
199                 return -EINVAL;
200         }
201         return 0;
202 }
203
204 static int gp8psk_fe_enable_high_lnb_voltage(struct dvb_frontend* fe, long onoff)
205 {
206         struct gp8psk_fe_state* state = fe->demodulator_priv;
207         return gp8psk_usb_out_op(state->d, USE_EXTRA_VOLT, onoff, 0,NULL,0);
208 }
209
210 static int gp8psk_fe_send_legacy_dish_cmd (struct dvb_frontend* fe, unsigned long sw_cmd)
211 {
212         struct gp8psk_fe_state* state = fe->demodulator_priv;
213         u8 cmd = sw_cmd & 0x7f;
214
215         if (gp8psk_usb_out_op(state->d,SET_DN_SWITCH, cmd, 0,
216                         NULL, 0)) {
217                 return -EINVAL;
218         }
219         if (gp8psk_usb_out_op(state->d,SET_LNB_VOLTAGE, !!(sw_cmd & 0x80),
220                         0, NULL, 0)) {
221                 return -EINVAL;
222         }
223
224         return 0;
225 }
226
227 static void gp8psk_fe_release(struct dvb_frontend* fe)
228 {
229         struct gp8psk_fe_state *state = fe->demodulator_priv;
230         kfree(state);
231 }
232
233 static struct dvb_frontend_ops gp8psk_fe_ops;
234
235 struct dvb_frontend * gp8psk_fe_attach(struct dvb_usb_device *d)
236 {
237         struct gp8psk_fe_state *s = kzalloc(sizeof(struct gp8psk_fe_state), GFP_KERNEL);
238         if (s == NULL)
239                 goto error;
240
241         s->d = d;
242         memcpy(&s->fe.ops, &gp8psk_fe_ops, sizeof(struct dvb_frontend_ops));
243         s->fe.demodulator_priv = s;
244
245         goto success;
246 error:
247         return NULL;
248 success:
249         return &s->fe;
250 }
251
252
253 static struct dvb_frontend_ops gp8psk_fe_ops = {
254         .info = {
255                 .name                   = "Genpix 8psk-to-USB2 DVB-S",
256                 .type                   = FE_QPSK,
257                 .frequency_min          = 800000,
258                 .frequency_max          = 2250000,
259                 .frequency_stepsize     = 100,
260                 .symbol_rate_min        = 1000000,
261                 .symbol_rate_max        = 45000000,
262                 .symbol_rate_tolerance  = 500,  /* ppm */
263                 .caps = FE_CAN_INVERSION_AUTO |
264                                 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
265                                 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
266                                 FE_CAN_QPSK
267         },
268
269         .release = gp8psk_fe_release,
270
271         .init = NULL,
272         .sleep = NULL,
273
274         .set_frontend = gp8psk_fe_set_frontend,
275         .get_frontend = gp8psk_fe_get_frontend,
276         .get_tune_settings = gp8psk_fe_get_tune_settings,
277
278         .read_status = gp8psk_fe_read_status,
279         .read_ber = gp8psk_fe_read_ber,
280         .read_signal_strength = gp8psk_fe_read_signal_strength,
281         .read_snr = gp8psk_fe_read_snr,
282         .read_ucblocks = gp8psk_fe_read_unc_blocks,
283
284         .diseqc_send_master_cmd = gp8psk_fe_send_diseqc_msg,
285         .diseqc_send_burst = gp8psk_fe_send_diseqc_burst,
286         .set_tone = gp8psk_fe_set_tone,
287         .set_voltage = gp8psk_fe_set_voltage,
288         .dishnetwork_send_legacy_command = gp8psk_fe_send_legacy_dish_cmd,
289         .enable_high_lnb_voltage = gp8psk_fe_enable_high_lnb_voltage
290 };