fixes for /dev/misc/pvr opening problems in some conditions
[enigma2.git] / lib / dvb / sec.h
1 #ifndef __dvb_sec_h
2 #define __dvb_sec_h
3
4 #include <lib/dvb/idvb.h>
5 #include <list>
6
7 #ifndef SWIG
8 class eSecCommand
9 {
10 public:
11         enum { modeStatic, modeDynamic };
12         enum {
13                 NONE, SLEEP, SET_VOLTAGE, SET_TONE, GOTO,
14                 SEND_DISEQC, SEND_TONEBURST, SET_FRONTEND,
15                 SET_TIMEOUT, IF_TIMEOUT_GOTO, 
16                 IF_VOLTAGE_GOTO, IF_NOT_VOLTAGE_GOTO,
17                 SET_POWER_LIMITING_MODE,
18                 SET_ROTOR_DISEQC_RETRYS, IF_NO_MORE_ROTOR_DISEQC_RETRYS_GOTO,
19                 MEASURE_IDLE_INPUTPOWER, MEASURE_RUNNING_INPUTPOWER,
20                 IF_MEASURE_IDLE_WAS_NOT_OK_GOTO, IF_INPUTPOWER_DELTA_GOTO,
21                 UPDATE_CURRENT_ROTORPARAMS, INVALIDATE_CURRENT_ROTORPARMS,
22                 UPDATE_CURRENT_SWITCHPARMS, INVALIDATE_CURRENT_SWITCHPARMS,
23                 IF_ROTORPOS_VALID_GOTO,
24                 IF_TUNER_LOCKED_GOTO,
25                 IF_TONE_GOTO, IF_NOT_TONE_GOTO,
26                 START_TUNE_TIMEOUT,
27                 SET_ROTOR_MOVING,
28                 SET_ROTOR_STOPPED
29         };
30         int cmd;
31         struct rotor
32         {
33                 union {
34                         int deltaA;   // difference in mA between running and stopped rotor
35                         int lastSignal;
36                 };
37                 int okcount;  // counter
38                 int steps;    // goto steps
39                 int direction;
40         };
41         struct pair
42         {
43                 union
44                 {
45                         int voltage;
46                         int tone;
47                         int val;
48                 };
49                 int steps;
50         };
51         union
52         {
53                 int val;
54                 int steps;
55                 int timeout;
56                 int voltage;
57                 int tone;
58                 int toneburst;
59                 int msec;
60                 int mode;
61                 rotor measure;
62                 eDVBDiseqcCommand diseqc;
63                 pair compare;
64         };
65         eSecCommand( int cmd )
66                 :cmd(cmd)
67         {}
68         eSecCommand( int cmd, int val )
69                 :cmd(cmd), val(val)
70         {}
71         eSecCommand( int cmd, eDVBDiseqcCommand diseqc )
72                 :cmd(cmd), diseqc(diseqc)
73         {}
74         eSecCommand( int cmd, rotor measure )
75                 :cmd(cmd), measure(measure)
76         {}
77         eSecCommand( int cmd, pair compare )
78                 :cmd(cmd), compare(compare)
79         {}
80         eSecCommand()
81                 :cmd(NONE)
82         {}
83 };
84
85 class eSecCommandList
86 {
87         typedef std::list<eSecCommand> List;
88         List secSequence;
89 public:
90         typedef List::iterator iterator;
91 private:
92         iterator cur;
93 public:
94         eSecCommandList()
95                 :cur(secSequence.end())
96         {
97         }
98         void push_front(const eSecCommand &cmd)
99         {
100                 secSequence.push_front(cmd);
101         }
102         void push_back(const eSecCommand &cmd)
103         {
104                 secSequence.push_back(cmd);
105         }
106         void clear()
107         {
108                 secSequence.clear();
109                 cur=secSequence.end();
110         }
111         inline iterator &current()
112         {
113                 return cur;
114         }
115         inline iterator begin()
116         {
117                 return secSequence.begin();
118         }
119         inline iterator end()
120         {
121                 return secSequence.end();
122         }
123         int size() const
124         {
125                 return secSequence.size();
126         }
127         operator bool() const
128         {
129                 return secSequence.size();
130         }
131 };
132 #endif
133
134 class eDVBSatelliteDiseqcParameters
135 {
136 #ifdef SWIG
137         eDVBSatelliteDiseqcParameters();
138         ~eDVBSatelliteDiseqcParameters();
139 #endif
140 public:
141         enum { AA=0, AB=1, BA=2, BB=3, SENDNO=4 /* and 0xF0 .. 0xFF*/  };       // DiSEqC Parameter
142         enum t_diseqc_mode { NONE=0, V1_0=1, V1_1=2, V1_2=3, SMATV=4 }; // DiSEqC Mode
143         enum t_toneburst_param { NO=0, A=1, B=2 };
144 #ifndef SWIG
145         __u8 m_committed_cmd;
146         t_diseqc_mode m_diseqc_mode;
147         t_toneburst_param m_toneburst_param;
148
149         __u8 m_repeats; // for cascaded switches
150         bool m_use_fast;        // send no DiSEqC on H/V or Lo/Hi change
151         bool m_seq_repeat;      // send the complete DiSEqC Sequence twice...
152         __u8 m_command_order;
153         /*      diseqc 1.0)
154                         0) commited, toneburst
155                         1) toneburst, committed
156                 diseqc > 1.0)
157                         2) committed, uncommitted, toneburst
158                         3) toneburst, committed, uncommitted
159                         4) uncommitted, committed, toneburst
160                         5) toneburst, uncommitted, committed */
161         __u8 m_uncommitted_cmd; // state of the 4 uncommitted switches..
162 #endif
163 };
164
165 class eDVBSatelliteSwitchParameters
166 {
167 #ifdef SWIG
168         eDVBSatelliteSwitchParameters();
169         ~eDVBSatelliteSwitchParameters();
170 #endif
171 public:
172         enum t_22khz_signal {   HILO=0, ON=1, OFF=2     }; // 22 Khz
173         enum t_voltage_mode     {       HV=0, _14V=1, _18V=2, _0V=3, HV_13=4 }; // 14/18 V
174 #ifndef SWIG
175         t_voltage_mode m_voltage_mode;
176         t_22khz_signal m_22khz_signal;
177         __u8 m_rotorPosNum; // 0 is disable.. then use gotoxx
178 #endif
179 };
180
181 class eDVBSatelliteRotorParameters
182 {
183 #ifdef SWIG
184         eDVBSatelliteRotorParameters();
185         ~eDVBSatelliteRotorParameters();
186 #endif
187 public:
188         enum { NORTH, SOUTH, EAST, WEST };
189         enum { FAST, SLOW };
190 #ifndef SWIG
191         eDVBSatelliteRotorParameters() { setDefaultOptions(); }
192
193         struct eDVBSatelliteRotorInputpowerParameters
194         {
195                 bool m_use;     // can we use rotor inputpower to detect rotor running state ?
196                 __u8 m_delta;   // delta between running and stopped rotor
197                 unsigned int m_turning_speed; // SLOW, FAST, or fast turning epoch
198         };
199         eDVBSatelliteRotorInputpowerParameters m_inputpower_parameters;
200
201         struct eDVBSatelliteRotorGotoxxParameters
202         {
203                 __u8 m_lo_direction;    // EAST, WEST
204                 __u8 m_la_direction;    // NORT, SOUTH
205                 double m_longitude;     // longitude for gotoXX? function
206                 double m_latitude;      // latitude for gotoXX? function
207         };
208         eDVBSatelliteRotorGotoxxParameters m_gotoxx_parameters;
209
210         void setDefaultOptions() // set default rotor options
211         {
212                 m_inputpower_parameters.m_turning_speed = FAST; // fast turning
213                 m_inputpower_parameters.m_use = true;
214                 m_inputpower_parameters.m_delta = 60;
215                 m_gotoxx_parameters.m_lo_direction = EAST;
216                 m_gotoxx_parameters.m_la_direction = NORTH;
217                 m_gotoxx_parameters.m_longitude = 0.0;
218                 m_gotoxx_parameters.m_latitude = 0.0;
219         }
220 #endif
221 };
222
223 class eDVBSatelliteLNBParameters
224 {
225 #ifdef SWIG
226         eDVBSatelliteLNBParameters();
227         ~eDVBSatelliteLNBParameters();
228 #endif
229 public:
230         enum t_12V_relais_state { OFF=0, ON };
231 #ifndef SWIG
232         t_12V_relais_state m_12V_relais_state;  // 12V relais output on/off
233
234         int m_slot_mask; // useable by slot ( 1 | 2 | 4...)
235
236         unsigned int m_lof_hi,  // for 2 band universal lnb 10600 Mhz (high band offset frequency)
237                                 m_lof_lo,       // for 2 band universal lnb  9750 Mhz (low band offset frequency)
238                                 m_lof_threshold;        // for 2 band universal lnb 11750 Mhz (band switch frequency)
239
240         bool m_increased_voltage; // use increased voltage ( 14/18V )
241
242         std::map<int, eDVBSatelliteSwitchParameters> m_satellites;
243         eDVBSatelliteDiseqcParameters m_diseqc_parameters;
244         eDVBSatelliteRotorParameters m_rotor_parameters;
245
246         int m_prio; // to override automatic tuner management ... -1 is Auto
247 #endif
248 public:
249 #define guard_offset_min -8000
250 #define guard_offset_max 8000
251 #define guard_offset_step 8000
252 #define MAX_SATCR 8
253 #define MAX_LNBNUM 32
254
255         int SatCR_idx;
256         unsigned int SatCRvco;
257         unsigned int UnicableTuningWord;
258         unsigned int UnicableConfigWord;
259         int old_frequency;
260         int old_polarisation;
261         int old_orbital_position;
262         int guard_offset_old;
263         int guard_offset;
264         int LNBNum;
265 };
266
267 class eDVBRegisteredFrontend;
268
269 class eDVBSatelliteEquipmentControl: public iDVBSatelliteEquipmentControl
270 {
271         DECLARE_REF(eDVBSatelliteEquipmentControl);
272 public:
273         enum {
274                 DELAY_AFTER_CONT_TONE_DISABLE_BEFORE_DISEQC=0,  // delay after continuous tone disable before diseqc command
275                 DELAY_AFTER_FINAL_CONT_TONE_CHANGE, // delay after continuous tone change before tune
276                 DELAY_AFTER_FINAL_VOLTAGE_CHANGE, // delay after voltage change at end of complete sequence
277                 DELAY_BETWEEN_DISEQC_REPEATS, // delay between repeated diseqc commands
278                 DELAY_AFTER_LAST_DISEQC_CMD, // delay after last diseqc command
279                 DELAY_AFTER_TONEBURST, // delay after toneburst
280                 DELAY_AFTER_ENABLE_VOLTAGE_BEFORE_SWITCH_CMDS, // delay after enable voltage before transmit toneburst/diseqc
281                 DELAY_BETWEEN_SWITCH_AND_MOTOR_CMD, // delay after transmit toneburst / diseqc and before transmit motor command
282                 DELAY_AFTER_VOLTAGE_CHANGE_BEFORE_MEASURE_IDLE_INPUTPOWER, // delay after voltage change before measure idle input power
283                 DELAY_AFTER_ENABLE_VOLTAGE_BEFORE_MOTOR_CMD, // delay after enable voltage before transmit motor command
284                 DELAY_AFTER_MOTOR_STOP_CMD, // delay after transmit motor stop
285                 DELAY_AFTER_VOLTAGE_CHANGE_BEFORE_MOTOR_CMD, // delay after voltage change before transmit motor command
286                 DELAY_BEFORE_SEQUENCE_REPEAT, // delay before the complete sequence is repeated (when enabled)
287                 MOTOR_COMMAND_RETRIES, // max transmit tries of rotor command when the rotor dont start turning (with power measurement)
288                 MOTOR_RUNNING_TIMEOUT, // max motor running time before timeout
289                 DELAY_AFTER_VOLTAGE_CHANGE_BEFORE_SWITCH_CMDS, // delay after change voltage before transmit toneburst/diseqc
290                 DELAY_AFTER_DISEQC_RESET_CMD,
291                 DELAY_AFTER_DISEQC_PERIPHERIAL_POWERON_CMD,
292                 MAX_PARAMS
293         };
294 private:
295 #ifndef SWIG
296         static eDVBSatelliteEquipmentControl *instance;
297         eDVBSatelliteLNBParameters m_lnbs[144]; // i think its enough
298         int m_lnbidx; // current index for set parameters
299         std::map<int, eDVBSatelliteSwitchParameters>::iterator m_curSat;
300         eSmartPtrList<eDVBRegisteredFrontend> &m_avail_frontends, &m_avail_simulate_frontends;
301         bool m_rotorMoving;
302         int m_not_linked_slot_mask;
303         bool m_canMeasureInputPower;
304 #endif
305 #ifdef SWIG
306         eDVBSatelliteEquipmentControl();
307         ~eDVBSatelliteEquipmentControl();
308 #endif
309         static int m_params[MAX_PARAMS];
310 public:
311 #ifndef SWIG
312         eDVBSatelliteEquipmentControl(eSmartPtrList<eDVBRegisteredFrontend> &avail_frontends, eSmartPtrList<eDVBRegisteredFrontend> &avail_simulate_frontends);
313         RESULT prepare(iDVBFrontend &frontend, FRONTENDPARAMETERS &parm, const eDVBFrontendParametersSatellite &sat, int frontend_id, unsigned int tunetimeout);
314         int canTune(const eDVBFrontendParametersSatellite &feparm, iDVBFrontend *, int frontend_id, int *highest_score_lnb=0);
315         bool currentLNBValid() { return m_lnbidx > -1 && m_lnbidx < (int)(sizeof(m_lnbs) / sizeof(eDVBSatelliteLNBParameters)); }
316 #endif
317         static eDVBSatelliteEquipmentControl *getInstance() { return instance; }
318         static void setParam(int param, int value);
319         RESULT clear();
320 /* LNB Specific Parameters */
321         RESULT addLNB();
322         RESULT setLNBSlotMask(int slotmask);
323         RESULT setLNBLOFL(int lofl);
324         RESULT setLNBLOFH(int lofh);
325         RESULT setLNBThreshold(int threshold);
326         RESULT setLNBIncreasedVoltage(bool onoff);
327         RESULT setLNBPrio(int prio);
328         RESULT setLNBNum(int LNBNum);
329 /* DiSEqC Specific Parameters */
330         RESULT setDiSEqCMode(int diseqcmode);
331         RESULT setToneburst(int toneburst);
332         RESULT setRepeats(int repeats);
333         RESULT setCommittedCommand(int command);
334         RESULT setUncommittedCommand(int command);
335         RESULT setCommandOrder(int order);
336         RESULT setFastDiSEqC(bool onoff);
337         RESULT setSeqRepeat(bool onoff); // send the complete switch sequence twice (without rotor command)
338 /* Rotor Specific Parameters */
339         RESULT setLongitude(float longitude);
340         RESULT setLatitude(float latitude);
341         RESULT setLoDirection(int direction);
342         RESULT setLaDirection(int direction);
343         RESULT setUseInputpower(bool onoff);
344         RESULT setInputpowerDelta(int delta);  // delta between running and stopped rotor
345         RESULT setRotorTurningSpeed(int speed);  // set turning speed..
346 /* Unicable Specific Parameters */
347         RESULT setLNBSatCR(int SatCR_idx);
348         RESULT setLNBSatCRvco(int SatCRvco);
349 //      RESULT checkGuardOffset(const eDVBFrontendParametersSatellite &sat);
350         RESULT getLNBSatCR();
351         RESULT getLNBSatCRvco();
352 /* Satellite Specific Parameters */
353         RESULT addSatellite(int orbital_position);
354         RESULT setVoltageMode(int mode);
355         RESULT setToneMode(int mode);
356         RESULT setRotorPosNum(int rotor_pos_num);
357 /* Tuner Specific Parameters */
358         RESULT setTunerLinked(int from, int to);
359         RESULT setTunerDepends(int from, int to);
360         void setSlotNotLinked(int tuner_no);
361
362         void setRotorMoving(bool); // called from the frontend's
363         bool isRotorMoving();
364         bool canMeasureInputPower() { return m_canMeasureInputPower; }
365 };
366
367 #endif