disable WSS when 4:3 letterbox or 4:3 panscan is selected and "WSS on 4:3"
[enigma2.git] / lib / dvb / frontend.cpp
1 #include <lib/dvb/dvb.h>
2 #include <lib/base/eerror.h>
3 #include <lib/base/nconfig.h> // access to python config
4 #include <errno.h>
5 #include <unistd.h>
6 #include <fcntl.h>
7 #include <sys/ioctl.h>
8
9 #ifndef I2C_SLAVE_FORCE
10 #define I2C_SLAVE_FORCE 0x0706
11 #endif
12
13 #if HAVE_DVB_API_VERSION < 3
14 #include <ost/frontend.h>
15 #include <ost/sec.h>
16 #define QAM_AUTO                                (Modulation)6
17 #define TRANSMISSION_MODE_AUTO  (TransmitMode)2
18 #define BANDWIDTH_AUTO                  (BandWidth)3
19 #define GUARD_INTERVAL_AUTO             (GuardInterval)4
20 #define HIERARCHY_AUTO                  (Hierarchy)4
21 #define parm_frequency parm.Frequency
22 #define parm_inversion parm.Inversion
23 #define parm_u_qpsk_symbol_rate parm.u.qpsk.SymbolRate
24 #define parm_u_qpsk_fec_inner parm.u.qpsk.FEC_inner
25 #define parm_u_qam_symbol_rate parm.u.qam.SymbolRate
26 #define parm_u_qam_fec_inner parm.u.qam.FEC_inner
27 #define parm_u_qam_modulation parm.u.qam.QAM
28 #define parm_u_ofdm_bandwidth parm.u.ofdm.bandWidth
29 #define parm_u_ofdm_code_rate_LP parm.u.ofdm.LP_CodeRate
30 #define parm_u_ofdm_code_rate_HP parm.u.ofdm.HP_CodeRate
31 #define parm_u_ofdm_constellation parm.u.ofdm.Constellation
32 #define parm_u_ofdm_transmission_mode parm.u.ofdm.TransmissionMode
33 #define parm_u_ofdm_guard_interval parm.u.ofdm.guardInterval
34 #define parm_u_ofdm_hierarchy_information parm.u.ofdm.HierarchyInformation
35 #else
36 #include <linux/dvb/frontend.h>
37 #define parm_frequency parm.frequency
38 #define parm_inversion parm.inversion
39 #define parm_u_qpsk_symbol_rate parm.u.qpsk.symbol_rate
40 #define parm_u_qpsk_fec_inner parm.u.qpsk.fec_inner
41 #define parm_u_qam_symbol_rate parm.u.qam.symbol_rate
42 #define parm_u_qam_fec_inner parm.u.qam.fec_inner
43 #define parm_u_qam_modulation parm.u.qam.modulation
44 #define parm_u_ofdm_bandwidth parm.u.ofdm.bandwidth
45 #define parm_u_ofdm_code_rate_LP parm.u.ofdm.code_rate_LP
46 #define parm_u_ofdm_code_rate_HP parm.u.ofdm.code_rate_HP
47 #define parm_u_ofdm_constellation parm.u.ofdm.constellation
48 #define parm_u_ofdm_transmission_mode parm.u.ofdm.transmission_mode
49 #define parm_u_ofdm_guard_interval parm.u.ofdm.guard_interval
50 #define parm_u_ofdm_hierarchy_information parm.u.ofdm.hierarchy_information
51 #ifdef FEC_9_10
52         #warning "FEC_9_10 already exist in dvb api ... it seems it is now ready for DVB-S2"
53 #else
54         #define FEC_S2_QPSK_1_2 (fe_code_rate_t)(FEC_AUTO+1)
55         #define FEC_S2_QPSK_2_3 (fe_code_rate_t)(FEC_S2_QPSK_1_2+1)
56         #define FEC_S2_QPSK_3_4 (fe_code_rate_t)(FEC_S2_QPSK_2_3+1)
57         #define FEC_S2_QPSK_5_6 (fe_code_rate_t)(FEC_S2_QPSK_3_4+1)
58         #define FEC_S2_QPSK_7_8 (fe_code_rate_t)(FEC_S2_QPSK_5_6+1)
59         #define FEC_S2_QPSK_8_9 (fe_code_rate_t)(FEC_S2_QPSK_7_8+1)
60         #define FEC_S2_QPSK_3_5 (fe_code_rate_t)(FEC_S2_QPSK_8_9+1)
61         #define FEC_S2_QPSK_4_5 (fe_code_rate_t)(FEC_S2_QPSK_3_5+1)
62         #define FEC_S2_QPSK_9_10 (fe_code_rate_t)(FEC_S2_QPSK_4_5+1)
63         #define FEC_S2_8PSK_1_2 (fe_code_rate_t)(FEC_S2_QPSK_9_10+1)
64         #define FEC_S2_8PSK_2_3 (fe_code_rate_t)(FEC_S2_8PSK_1_2+1)
65         #define FEC_S2_8PSK_3_4 (fe_code_rate_t)(FEC_S2_8PSK_2_3+1)
66         #define FEC_S2_8PSK_5_6 (fe_code_rate_t)(FEC_S2_8PSK_3_4+1)
67         #define FEC_S2_8PSK_7_8 (fe_code_rate_t)(FEC_S2_8PSK_5_6+1)
68         #define FEC_S2_8PSK_8_9 (fe_code_rate_t)(FEC_S2_8PSK_7_8+1)
69         #define FEC_S2_8PSK_3_5 (fe_code_rate_t)(FEC_S2_8PSK_8_9+1)
70         #define FEC_S2_8PSK_4_5 (fe_code_rate_t)(FEC_S2_8PSK_3_5+1)
71         #define FEC_S2_8PSK_9_10 (fe_code_rate_t)(FEC_S2_8PSK_4_5+1)
72 #endif
73 #endif
74
75 #include <dvbsi++/satellite_delivery_system_descriptor.h>
76 #include <dvbsi++/cable_delivery_system_descriptor.h>
77 #include <dvbsi++/terrestrial_delivery_system_descriptor.h>
78
79 void eDVBDiseqcCommand::setCommandString(const char *str)
80 {
81         if (!str)
82                 return;
83         len=0;
84         int slen = strlen(str);
85         if (slen % 2)
86         {
87                 eDebug("invalid diseqc command string length (not 2 byte aligned)");
88                 return;
89         }
90         if (slen > MAX_DISEQC_LENGTH*2)
91         {
92                 eDebug("invalid diseqc command string length (string is to long)");
93                 return;
94         }
95         unsigned char val=0;
96         for (int i=0; i < slen; ++i)
97         {
98                 unsigned char c = str[i];
99                 switch(c)
100                 {
101                         case '0' ... '9': c-=48; break;
102                         case 'a' ... 'f': c-=87; break;
103                         case 'A' ... 'F': c-=55; break;
104                         default:
105                                 eDebug("invalid character in hex string..ignore complete diseqc command !");
106                                 return;
107                 }
108                 if ( i % 2 )
109                 {
110                         val |= c;
111                         data[i/2] = val;
112                 }
113                 else
114                         val = c << 4;
115         }
116         len = slen/2;
117 }
118
119 void eDVBFrontendParametersSatellite::set(const SatelliteDeliverySystemDescriptor &descriptor)
120 {
121         frequency    = descriptor.getFrequency() * 10;
122         symbol_rate  = descriptor.getSymbolRate() * 100;
123         polarisation = descriptor.getPolarization();
124         fec = descriptor.getFecInner();
125         if ( fec != FEC::fNone && fec > FEC::f9_10 )
126                 fec = FEC::fAuto;
127         inversion = Inversion::Unknown;
128         orbital_position  = ((descriptor.getOrbitalPosition() >> 12) & 0xF) * 1000;
129         orbital_position += ((descriptor.getOrbitalPosition() >> 8) & 0xF) * 100;
130         orbital_position += ((descriptor.getOrbitalPosition() >> 4) & 0xF) * 10;
131         orbital_position += ((descriptor.getOrbitalPosition()) & 0xF);
132         if (orbital_position && (!descriptor.getWestEastFlag()))
133                 orbital_position = 3600 - orbital_position;
134         system = descriptor.getModulationSystem();
135         modulation = descriptor.getModulation();
136         if (system == System::DVB_S && modulation == Modulation::M8PSK)
137         {
138                 eDebug("satellite_delivery_descriptor non valid modulation type.. force QPSK");
139                 modulation=QPSK;
140         }
141         roll_off = descriptor.getRollOff();
142         if (system == System::DVB_S2)
143         {
144                 eDebug("SAT DVB-S2 freq %d, %s, pos %d, sr %d, fec %d, modulation %d, roll_off %d",
145                         frequency,
146                         polarisation ? "hor" : "vert",
147                         orbital_position,
148                         symbol_rate, fec,
149                         modulation,
150                         roll_off);
151         }
152         else
153         {
154                 eDebug("SAT DVB-S freq %d, %s, pos %d, sr %d, fec %d",
155                         frequency,
156                         polarisation ? "hor" : "vert",
157                         orbital_position,
158                         symbol_rate, fec);
159         }
160 }
161
162 void eDVBFrontendParametersCable::set(const CableDeliverySystemDescriptor &descriptor)
163 {
164         frequency = descriptor.getFrequency() / 10;
165         symbol_rate = descriptor.getSymbolRate() * 100;
166         fec_inner = descriptor.getFecInner();
167         if ( fec_inner == 0xF )
168                 fec_inner = FEC::fNone;
169         modulation = descriptor.getModulation();
170         if ( modulation > 0x5 )
171                 modulation = Modulation::Auto;
172         inversion = Inversion::Unknown;
173         eDebug("Cable freq %d, mod %d, sr %d, fec %d",
174                 frequency,
175                 modulation, symbol_rate, fec_inner);
176 }
177
178 void eDVBFrontendParametersTerrestrial::set(const TerrestrialDeliverySystemDescriptor &descriptor)
179 {
180         frequency = descriptor.getCentreFrequency() * 10;
181         bandwidth = descriptor.getBandwidth();
182         if ( bandwidth > 2 ) // 5Mhz forced to auto
183                 bandwidth = Bandwidth::BwAuto;
184         code_rate_HP = descriptor.getCodeRateHpStream();
185         if (code_rate_HP > 4)
186                 code_rate_HP = FEC::fAuto;
187         code_rate_LP = descriptor.getCodeRateLpStream();
188         if (code_rate_LP > 4)
189                 code_rate_LP = FEC::fAuto;
190         transmission_mode = descriptor.getTransmissionMode();
191         if (transmission_mode > 1) // TM4k forced to auto
192                 transmission_mode = TransmissionMode::TMAuto;
193         guard_interval = descriptor.getGuardInterval();
194         if (guard_interval > 3)
195                 guard_interval = GuardInterval::GI_Auto;
196         hierarchy = descriptor.getHierarchyInformation()&3;
197         modulation = descriptor.getConstellation();
198         if (modulation > 2)
199                 modulation = Modulation::Auto;
200         inversion = Inversion::Unknown;
201         eDebug("Terr freq %d, bw %d, cr_hp %d, cr_lp %d, tm_mode %d, guard %d, hierarchy %d, const %d",
202                 frequency, bandwidth, code_rate_HP, code_rate_LP, transmission_mode,
203                 guard_interval, hierarchy, modulation);
204 }
205
206 eDVBFrontendParameters::eDVBFrontendParameters(): m_type(-1)
207 {
208 }
209
210 DEFINE_REF(eDVBFrontendParameters);
211
212 RESULT eDVBFrontendParameters::getSystem(int &t) const
213 {
214         if (m_type == -1)
215                 return -1;
216         t = m_type;
217         return 0;
218 }
219
220 RESULT eDVBFrontendParameters::getDVBS(eDVBFrontendParametersSatellite &p) const
221 {
222         if (m_type != iDVBFrontend::feSatellite)
223                 return -1;
224         p = sat;
225         return 0;
226 }
227
228 RESULT eDVBFrontendParameters::getDVBC(eDVBFrontendParametersCable &p) const
229 {
230         if (m_type != iDVBFrontend::feCable)
231                 return -1;
232         p = cable;
233         return 0;
234 }
235
236 RESULT eDVBFrontendParameters::getDVBT(eDVBFrontendParametersTerrestrial &p) const
237 {
238         if (m_type != iDVBFrontend::feTerrestrial)
239                 return -1;
240         p = terrestrial;
241         return 0;
242 }
243
244 RESULT eDVBFrontendParameters::setDVBS(const eDVBFrontendParametersSatellite &p, bool no_rotor_command_on_tune)
245 {
246         sat = p;
247         sat.no_rotor_command_on_tune = no_rotor_command_on_tune;
248         m_type = iDVBFrontend::feSatellite;
249         return 0;
250 }
251
252 RESULT eDVBFrontendParameters::setDVBC(const eDVBFrontendParametersCable &p)
253 {
254         cable = p;
255         m_type = iDVBFrontend::feCable;
256         return 0;
257 }
258
259 RESULT eDVBFrontendParameters::setDVBT(const eDVBFrontendParametersTerrestrial &p)
260 {
261         terrestrial = p;
262         m_type = iDVBFrontend::feTerrestrial;
263         return 0;
264 }
265
266 RESULT eDVBFrontendParameters::calculateDifference(const iDVBFrontendParameters *parm, int &diff, bool exact) const
267 {
268         if (!parm)
269                 return -1;
270         int type;
271         if (parm->getSystem(type))
272                 return -1;
273         if (type != m_type)
274         {
275                 diff = 1<<30; // big difference
276                 return 0;
277         }
278
279         switch (type)
280         {
281         case iDVBFrontend::feSatellite:
282         {
283                 eDVBFrontendParametersSatellite osat;
284                 if (parm->getDVBS(osat))
285                         return -2;
286
287                 if (sat.orbital_position != osat.orbital_position)
288                         diff = 1<<29;
289                 else if (sat.polarisation != osat.polarisation)
290                         diff = 1<<28;
291                 else if (exact && sat.fec != osat.fec && sat.fec != eDVBFrontendParametersSatellite::FEC::fAuto && osat.fec != eDVBFrontendParametersSatellite::FEC::fAuto)
292                         diff = 1<<27;
293                 else if (exact && sat.modulation != osat.modulation && sat.modulation != eDVBFrontendParametersSatellite::Modulation::Auto && osat.modulation != eDVBFrontendParametersSatellite::Modulation::Auto)
294                         diff = 1<<27;
295                 else
296                 {
297                         diff = abs(sat.frequency - osat.frequency);
298                         diff += abs(sat.symbol_rate - osat.symbol_rate);
299                 }
300                 return 0;
301         }
302         case iDVBFrontend::feCable:
303                 eDVBFrontendParametersCable ocable;
304                 if (parm->getDVBC(ocable))
305                         return -2;
306
307                 if (exact && cable.modulation != ocable.modulation
308                         && cable.modulation != eDVBFrontendParametersCable::Modulation::Auto
309                         && ocable.modulation != eDVBFrontendParametersCable::Modulation::Auto)
310                         diff = 1 << 29;
311                 else if (exact && cable.fec_inner != ocable.fec_inner && cable.fec_inner != eDVBFrontendParametersCable::FEC::fAuto && ocable.fec_inner != eDVBFrontendParametersCable::FEC::fAuto)
312                         diff = 1 << 27;
313                 else
314                 {
315                         diff = abs(cable.frequency - ocable.frequency);
316                         diff += abs(cable.symbol_rate - ocable.symbol_rate);
317                 }
318                 return 0;
319         case iDVBFrontend::feTerrestrial:
320                 eDVBFrontendParametersTerrestrial oterrestrial;
321                 if (parm->getDVBT(oterrestrial))
322                         return -2;
323
324
325                 if (exact && oterrestrial.bandwidth != terrestrial.bandwidth &&
326                         oterrestrial.bandwidth != eDVBFrontendParametersTerrestrial::Bandwidth::BwAuto &&
327                         terrestrial.bandwidth != eDVBFrontendParametersTerrestrial::Bandwidth::BwAuto)
328                         diff = 1 << 30;
329                 else if (exact && oterrestrial.modulation != terrestrial.modulation &&
330                         oterrestrial.modulation != eDVBFrontendParametersTerrestrial::Modulation::Auto &&
331                         terrestrial.modulation != eDVBFrontendParametersTerrestrial::Modulation::Auto)
332                         diff = 1 << 30;
333                 else if (exact && oterrestrial.transmission_mode != terrestrial.transmission_mode &&
334                         oterrestrial.transmission_mode != eDVBFrontendParametersTerrestrial::TransmissionMode::TMAuto &&
335                         terrestrial.transmission_mode != eDVBFrontendParametersTerrestrial::TransmissionMode::TMAuto)
336                         diff = 1 << 30;
337                 else if (exact && oterrestrial.guard_interval != terrestrial.guard_interval &&
338                         oterrestrial.guard_interval != eDVBFrontendParametersTerrestrial::GuardInterval::GI_Auto &&
339                         terrestrial.guard_interval != eDVBFrontendParametersTerrestrial::GuardInterval::GI_Auto)
340                         diff = 1 << 30;
341                 else if (exact && oterrestrial.hierarchy != terrestrial.hierarchy &&
342                         oterrestrial.hierarchy != eDVBFrontendParametersTerrestrial::Hierarchy::HAuto &&
343                         terrestrial.hierarchy != eDVBFrontendParametersTerrestrial::Hierarchy::HAuto)
344                         diff = 1 << 30;
345                 else if (exact && oterrestrial.code_rate_LP != terrestrial.code_rate_LP &&
346                         oterrestrial.code_rate_LP != eDVBFrontendParametersTerrestrial::FEC::fAuto &&
347                         terrestrial.code_rate_LP != eDVBFrontendParametersTerrestrial::FEC::fAuto)
348                         diff = 1 << 30;
349                 else if (exact && oterrestrial.code_rate_HP != terrestrial.code_rate_HP &&
350                         oterrestrial.code_rate_HP != eDVBFrontendParametersTerrestrial::FEC::fAuto &&
351                         terrestrial.code_rate_HP != eDVBFrontendParametersTerrestrial::FEC::fAuto)
352                         diff = 1 << 30;
353                 else
354                         diff = abs(terrestrial.frequency - oterrestrial.frequency);
355                 return 0;
356         default:
357                 return -1;
358         }
359         return 0;
360 }
361
362 RESULT eDVBFrontendParameters::getHash(unsigned long &hash) const
363 {
364         switch (m_type)
365         {
366         case iDVBFrontend::feSatellite:
367         {
368                 hash = (sat.orbital_position << 16);
369                 hash |= ((sat.frequency/1000)&0xFFFF)|((sat.polarisation&1) << 15);
370                 return 0;
371         }
372         case iDVBFrontend::feCable:
373                 hash = 0xFFFF0000;
374                 return 0;
375         case iDVBFrontend::feTerrestrial:
376                 hash = 0xEEEE0000;
377                 return 0;
378         default:
379                 return -1;
380         }
381 }
382
383 DEFINE_REF(eDVBFrontend);
384
385 int eDVBFrontend::PriorityOrder=0;
386
387 eDVBFrontend::eDVBFrontend(int adap, int fe, int &ok)
388         :m_enabled(false), m_type(-1), m_dvbid(fe), m_slotid(fe)
389         ,m_fd(-1), m_need_rotor_workaround(false), m_sn(0), m_timeout(0), m_tuneTimer(0)
390 #if HAVE_DVB_API_VERSION < 3
391         ,m_secfd(-1)
392 #endif
393 {
394 #if HAVE_DVB_API_VERSION < 3
395         sprintf(m_filename, "/dev/dvb/card%d/frontend%d", adap, fe);
396         sprintf(m_sec_filename, "/dev/dvb/card%d/sec%d", adap, fe);
397 #else
398         sprintf(m_filename, "/dev/dvb/adapter%d/frontend%d", adap, fe);
399 #endif
400         m_timeout = new eTimer(eApp);
401         CONNECT(m_timeout->timeout, eDVBFrontend::timeout);
402
403         m_tuneTimer = new eTimer(eApp);
404         CONNECT(m_tuneTimer->timeout, eDVBFrontend::tuneLoop);
405
406         for (int i=0; i<eDVBFrontend::NUM_DATA_ENTRIES; ++i)
407                 m_data[i] = -1;
408
409         m_idleInputpower[0]=m_idleInputpower[1]=0;
410
411         ok = !openFrontend();
412         closeFrontend();
413 }
414
415 int eDVBFrontend::openFrontend()
416 {
417         if (m_sn)
418                 return -1;  // already opened
419
420         m_state=0;
421         m_tuning=0;
422
423 #if HAVE_DVB_API_VERSION < 3
424         FrontendInfo fe_info;
425 #else
426         dvb_frontend_info fe_info;
427 #endif
428         eDebug("opening frontend %d", m_dvbid);
429         if (m_fd < 0)
430         {
431                 m_fd = ::open(m_filename, O_RDWR|O_NONBLOCK);
432                 if (m_fd < 0)
433                 {
434                         eWarning("failed! (%s) %m", m_filename);
435                         return -1;
436                 }
437         }
438         else
439                 eWarning("frontend %d already opened", m_dvbid);
440         if (m_type == -1)
441         {
442                 if (::ioctl(m_fd, FE_GET_INFO, &fe_info) < 0)
443                 {
444                         eWarning("ioctl FE_GET_INFO failed");
445                         ::close(m_fd);
446                         m_fd = -1;
447                         return -1;
448                 }
449
450                 switch (fe_info.type)
451                 {
452                 case FE_QPSK:
453                         m_type = iDVBFrontend::feSatellite;
454                         break;
455                 case FE_QAM:
456                         m_type = iDVBFrontend::feCable;
457                         break;
458                 case FE_OFDM:
459                         m_type = iDVBFrontend::feTerrestrial;
460                         break;
461                 default:
462                         eWarning("unknown frontend type.");
463                         ::close(m_fd);
464                         m_fd = -1;
465                         return -1;
466                 }
467                 eDebug("detected %s frontend", "satellite\0cable\0    terrestrial"+fe_info.type*10);
468         }
469
470 #if HAVE_DVB_API_VERSION < 3
471         if (m_type == iDVBFrontend::feSatellite)
472         {
473                         if (m_secfd < 0)
474                         {
475                                 m_secfd = ::open(m_sec_filename, O_RDWR);
476                                 if (m_secfd < 0)
477                                 {
478                                         eWarning("failed! (%s) %m", m_sec_filename);
479                                         ::close(m_fd);
480                                         m_fd=-1;
481                                         return -1;
482                                 }
483                         }
484                         else
485                                 eWarning("sec %d already opened", m_dvbid);
486         }
487 #endif
488
489         setTone(iDVBFrontend::toneOff);
490         setVoltage(iDVBFrontend::voltageOff);
491
492         m_sn = new eSocketNotifier(eApp, m_fd, eSocketNotifier::Read);
493         CONNECT(m_sn->activated, eDVBFrontend::feEvent);
494
495         return 0;
496 }
497
498 int eDVBFrontend::closeFrontend()
499 {
500         eDVBRegisteredFrontend *linked_fe = (eDVBRegisteredFrontend*)m_data[LINKED_NEXT_PTR];
501         while (linked_fe != (eDVBRegisteredFrontend*)-1)
502         {
503                 if (linked_fe->m_inuse)
504                 {
505                         eDebug("dont close frontend %d until the linked frontend %d in slot %d is still in use",
506                                 m_dvbid, linked_fe->m_frontend->getDVBID(), linked_fe->m_frontend->getSlotID());
507                         return -1;
508                 }
509                 linked_fe->m_frontend->getData(LINKED_NEXT_PTR, (int&)linked_fe);
510         }
511         if (m_fd >= 0)
512         {
513                 eDebug("close frontend %d", m_dvbid);
514                 m_tuneTimer->stop();
515                 setTone(iDVBFrontend::toneOff);
516                 setVoltage(iDVBFrontend::voltageOff);
517                 if (m_sec)
518                         m_sec->setRotorMoving(false);
519                 if (!::close(m_fd))
520                         m_fd=-1;
521                 else
522                         eWarning("couldnt close frontend %d", m_dvbid);
523                 m_data[CSW] = m_data[UCSW] = m_data[TONEBURST] = -1;
524         }
525 #if HAVE_DVB_API_VERSION < 3
526         if (m_secfd >= 0)
527         {
528                 if (!::close(m_secfd))
529                         m_secfd=-1;
530                 else
531                         eWarning("couldnt close sec %d", m_dvbid);
532         }
533 #endif
534         delete m_sn;
535         m_sn=0;
536
537         return 0;
538 }
539
540 eDVBFrontend::~eDVBFrontend()
541 {
542         closeFrontend();
543         delete m_timeout;
544         delete m_tuneTimer;
545 }
546
547 void eDVBFrontend::feEvent(int w)
548 {
549         while (1)
550         {
551 #if HAVE_DVB_API_VERSION < 3
552                 FrontendEvent event;
553 #else
554                 dvb_frontend_event event;
555 #endif
556                 int res;
557                 int state;
558                 res = ::ioctl(m_fd, FE_GET_EVENT, &event);
559
560                 if (res && (errno == EAGAIN))
561                         break;
562
563                 if (res)
564                 {
565                         eWarning("FE_GET_EVENT failed! %m");
566                         return;
567                 }
568
569                 if (w < 0)
570                         continue;
571
572 #if HAVE_DVB_API_VERSION < 3
573                 if (event.type == FE_COMPLETION_EV)
574 #else
575                 eDebug("(%d)fe event: status %x, inversion %s", m_dvbid, event.status, (event.parameters.inversion == INVERSION_ON) ? "on" : "off");
576                 if (event.status & FE_HAS_LOCK)
577 #endif
578                 {
579                         state = stateLock;
580                 } else
581                 {
582                         if (m_tuning)
583                                 state = stateTuning;
584                         else
585                         {
586                                 eDebug("stateLostLock");
587                                 state = stateLostLock;
588                                 m_data[CSW] = m_data[UCSW] = m_data[TONEBURST] = -1; // reset diseqc
589                         }
590                 }
591                 if (m_state != state)
592                 {
593                         m_state = state;
594                         m_stateChanged(this);
595                 }
596         }
597 }
598
599 void eDVBFrontend::timeout()
600 {
601         m_tuning = 0;
602         if (m_state == stateTuning)
603         {
604                 m_state = stateFailed;
605                 m_stateChanged(this);
606         }
607 }
608
609 int eDVBFrontend::readFrontendData(int type)
610 {
611         switch(type)
612         {
613                 case bitErrorRate:
614                 {
615                         uint32_t ber=0;
616                         if (ioctl(m_fd, FE_READ_BER, &ber) < 0 && errno != ERANGE)
617                                 eDebug("FE_READ_BER failed (%m)");
618                         return ber;
619                 }
620                 case signalPower:
621                 {
622                         uint16_t snr=0;
623                         if (ioctl(m_fd, FE_READ_SNR, &snr) < 0 && errno != ERANGE)
624                                 eDebug("FE_READ_SNR failed (%m)");
625                         return snr;
626                 }
627                 case signalPowerdB: /* this will move into the driver */
628                 {
629                         uint16_t snr=0;
630                         if (ioctl(m_fd, FE_READ_SNR, &snr) < 0 && errno != ERANGE)
631                                 eDebug("FE_READ_SNR failed (%m)");
632                         if (!strcmp(m_description, "BCM4501 (internal)"))
633                         {
634                                 unsigned int SDS_SNRE = snr << 16;
635
636                                 static float SNR_COEFF[6] = {
637                                         100.0 / 4194304.0,
638                                         -7136.0 / 4194304.0,
639                                         197418.0 / 4194304.0,
640                                         -2602183.0 / 4194304.0,
641                                         20377212.0 / 4194304.0,
642                                         -37791203.0 / 4194304.0,
643                                 };
644                         
645                                 float fval1, fval2, snr_in_db;
646                                 int i;
647                                 fval1 = 12.44714 - (2.0 * log10(SDS_SNRE / 256.0));
648                                 fval2 = pow(10.0, fval1)-1;
649                                 fval1 = 10.0 * log10(fval2);
650                         
651                                 if (fval1 < 10.0)
652                                 {
653                                         fval2 = SNR_COEFF[0];
654                                         for (i=0; i<6; ++i)
655                                         {
656                                                 fval2 *= fval1;
657                                                 fval2 += SNR_COEFF[i];
658                                         }
659                                         fval1 = fval2;
660                                 }
661                                 snr_in_db = fval1;
662                         
663                                 return (int)(snr_in_db * 100.0);
664                         }
665                         else if (!strcmp(m_description, "Alps BSBE1 702A") ||  // some frontends with STV0299
666                                 !strcmp(m_description, "Alps -S") ||
667                                 !strcmp(m_description, "Philips -S") ||
668                                 !strcmp(m_description, "LG -S") )
669                         {
670                                 float snr_in_db=(snr-39075)/1764.7;
671                                 return (int)(snr_in_db * 100.0);
672                         }
673                         else
674                                 eDebug("no SNR dB caluclation for frontendtype %s yet", m_description);
675                         return 0x12345678;
676                 }
677                 case signalQuality:
678                 {
679                         uint16_t strength=0;
680                         if (ioctl(m_fd, FE_READ_SIGNAL_STRENGTH, &strength) < 0 && errno != ERANGE)
681                                 eDebug("FE_READ_SIGNAL_STRENGTH failed (%m)");
682                         return strength;
683                 }
684                 case locked:
685                 {
686 #if HAVE_DVB_API_VERSION < 3
687                         FrontendStatus status=0;
688 #else
689                         fe_status_t status;
690 #endif
691                         if ( ioctl(m_fd, FE_READ_STATUS, &status) < 0 && errno != ERANGE )
692                                 eDebug("FE_READ_STATUS failed (%m)");
693                         return !!(status&FE_HAS_LOCK);
694                 }
695                 case synced:
696                 {
697 #if HAVE_DVB_API_VERSION < 3
698                         FrontendStatus status=0;
699 #else
700                         fe_status_t status;
701 #endif
702                         if ( ioctl(m_fd, FE_READ_STATUS, &status) < 0 && errno != ERANGE )
703                                 eDebug("FE_READ_STATUS failed (%m)");
704                         return !!(status&FE_HAS_SYNC);
705                 }
706                 case frontendNumber:
707                         return m_slotid;
708         }
709         return 0;
710 }
711
712 void PutToDict(ePyObject &dict, const char*key, long value)
713 {
714         ePyObject item = PyInt_FromLong(value);
715         if (item)
716         {
717                 if (PyDict_SetItemString(dict, key, item))
718                         eDebug("put %s to dict failed", key);
719                 Py_DECREF(item);
720         }
721         else
722                 eDebug("could not create PyObject for %s", key);
723 }
724
725 void PutToDict(ePyObject &dict, const char*key, ePyObject item)
726 {
727         if (item)
728         {
729                 if (PyDict_SetItemString(dict, key, item))
730                         eDebug("put %s to dict failed", key);
731                 Py_DECREF(item);
732         }
733         else
734                 eDebug("invalid PyObject for %s", key);
735 }
736
737 void PutToDict(ePyObject &dict, const char*key, const char *value)
738 {
739         ePyObject item = PyString_FromString(value);
740         if (item)
741         {
742                 if (PyDict_SetItemString(dict, key, item))
743                         eDebug("put %s to dict failed", key);
744                 Py_DECREF(item);
745         }
746         else
747                 eDebug("could not create PyObject for %s", key);
748 }
749
750 void fillDictWithSatelliteData(ePyObject dict, const FRONTENDPARAMETERS &parm, eDVBFrontend *fe)
751 {
752         int freq_offset=0;
753         int csw=0;
754         const char *tmp=0;
755         fe->getData(eDVBFrontend::CSW, csw);
756         fe->getData(eDVBFrontend::FREQ_OFFSET, freq_offset);
757         int frequency = parm_frequency + freq_offset;
758         PutToDict(dict, "frequency", frequency);
759         PutToDict(dict, "symbol_rate", parm_u_qpsk_symbol_rate);
760         switch(parm_u_qpsk_fec_inner)
761         {
762         case FEC_1_2:
763                 tmp = "FEC_1_2";
764                 break;
765         case FEC_2_3:
766                 tmp = "FEC_2_3";
767                 break;
768         case FEC_3_4:
769                 tmp = "FEC_3_4";
770                 break;
771         case FEC_5_6:
772                 tmp = "FEC_5_6";
773                 break;
774         case FEC_7_8:
775                 tmp = "FEC_7_8";
776                 break;
777         case FEC_NONE:
778                 tmp = "FEC_NONE";
779         default:
780         case FEC_AUTO:
781                 tmp = "FEC_AUTO";
782                 break;
783 #if HAVE_DVB_API_VERSION >=3
784         case FEC_S2_8PSK_1_2:
785         case FEC_S2_QPSK_1_2:
786                 tmp = "FEC_1_2";
787                 break;
788         case FEC_S2_8PSK_2_3:
789         case FEC_S2_QPSK_2_3:
790                 tmp = "FEC_2_3";
791                 break;
792         case FEC_S2_8PSK_3_4:
793         case FEC_S2_QPSK_3_4:
794                 tmp = "FEC_3_4";
795                 break;
796         case FEC_S2_8PSK_5_6:
797         case FEC_S2_QPSK_5_6:
798                 tmp = "FEC_5_6";
799                 break;
800         case FEC_S2_8PSK_7_8:
801         case FEC_S2_QPSK_7_8:
802                 tmp = "FEC_7_8";
803                 break;
804         case FEC_S2_8PSK_8_9:
805         case FEC_S2_QPSK_8_9:
806                 tmp = "FEC_8_9";
807                 break;
808         case FEC_S2_8PSK_3_5:
809         case FEC_S2_QPSK_3_5:
810                 tmp = "FEC_3_5";
811                 break;
812         case FEC_S2_8PSK_4_5:
813         case FEC_S2_QPSK_4_5:
814                 tmp = "FEC_4_5";
815                 break;
816         case FEC_S2_8PSK_9_10:
817         case FEC_S2_QPSK_9_10:
818                 tmp = "FEC_9_10";
819                 break;
820 #endif
821         }
822 #if HAVE_DVB_API_VERSION >=3
823         PutToDict(dict, "modulation",
824                 parm_u_qpsk_fec_inner > FEC_S2_QPSK_9_10 ? "8PSK": "QPSK" );
825 #else
826         PutToDict(dict, "modulation", "QPSK" );
827 #endif
828         PutToDict(dict, "fec_inner", tmp);
829         tmp = parm_u_qpsk_fec_inner > FEC_AUTO ?
830                 "DVB-S2" : "DVB-S";
831         PutToDict(dict, "system", tmp);
832 }
833
834 void fillDictWithCableData(ePyObject dict, const FRONTENDPARAMETERS &parm)
835 {
836         const char *tmp=0;
837         PutToDict(dict, "frequency", parm_frequency/1000);
838         PutToDict(dict, "symbol_rate", parm_u_qam_symbol_rate);
839         switch(parm_u_qam_fec_inner)
840         {
841         case FEC_NONE:
842                 tmp = "FEC_NONE";
843                 break;
844         case FEC_1_2:
845                 tmp = "FEC_1_2";
846                 break;
847         case FEC_2_3:
848                 tmp = "FEC_2_3";
849                 break;
850         case FEC_3_4:
851                 tmp = "FEC_3_4";
852                 break;
853         case FEC_5_6:
854                 tmp = "FEC_5_6";
855                 break;
856         case FEC_7_8:
857                 tmp = "FEC_7_8";
858                 break;
859 #if HAVE_DVB_API_VERSION >= 3
860         case FEC_8_9:
861                 tmp = "FEC_8_9";
862                 break;
863 #endif
864         default:
865         case FEC_AUTO:
866                 tmp = "FEC_AUTO";
867                 break;
868         }
869         PutToDict(dict, "fec_inner", tmp);
870         switch(parm_u_qam_modulation)
871         {
872         case QAM_16:
873                 tmp = "QAM_16";
874                 break;
875         case QAM_32:
876                 tmp = "QAM_32";
877                 break;
878         case QAM_64:
879                 tmp = "QAM_64";
880                 break;
881         case QAM_128:
882                 tmp = "QAM_128";
883                 break;
884         case QAM_256:
885                 tmp = "QAM_256";
886                 break;
887         default:
888         case QAM_AUTO:
889                 tmp = "QAM_AUTO";
890                 break;
891         }
892         PutToDict(dict, "modulation", tmp);
893 }
894
895 void fillDictWithTerrestrialData(ePyObject dict, const FRONTENDPARAMETERS &parm)
896 {
897         const char *tmp=0;
898         PutToDict(dict, "frequency", parm_frequency);
899         switch (parm_u_ofdm_bandwidth)
900         {
901         case BANDWIDTH_8_MHZ:
902                 tmp = "BANDWIDTH_8_MHZ";
903                 break;
904         case BANDWIDTH_7_MHZ:
905                 tmp = "BANDWIDTH_7_MHZ";
906                 break;
907         case BANDWIDTH_6_MHZ:
908                 tmp = "BANDWIDTH_6_MHZ";
909                 break;
910         default:
911         case BANDWIDTH_AUTO:
912                 tmp = "BANDWIDTH_AUTO";
913                 break;
914         }
915         PutToDict(dict, "bandwidth", tmp);
916         switch (parm_u_ofdm_code_rate_LP)
917         {
918         case FEC_1_2:
919                 tmp = "FEC_1_2";
920                 break;
921         case FEC_2_3:
922                 tmp = "FEC_2_3";
923                 break;
924         case FEC_3_4:
925                 tmp = "FEC_3_4";
926                 break;
927         case FEC_5_6:
928                 tmp = "FEC_5_6";
929                 break;
930         case FEC_7_8:
931                 tmp = "FEC_7_8";
932                 break;
933         default:
934         case FEC_AUTO:
935                 tmp = "FEC_AUTO";
936                 break;
937         }
938         PutToDict(dict, "code_rate_lp", tmp);
939         switch (parm_u_ofdm_code_rate_HP)
940         {
941         case FEC_1_2:
942                 tmp = "FEC_1_2";
943                 break;
944         case FEC_2_3:
945                 tmp = "FEC_2_3";
946                 break;
947         case FEC_3_4:
948                 tmp = "FEC_3_4";
949                 break;
950         case FEC_5_6:
951                 tmp = "FEC_5_6";
952                 break;
953         case FEC_7_8:
954                 tmp = "FEC_7_8";
955                 break;
956         default:
957         case FEC_AUTO:
958                 tmp = "FEC_AUTO";
959                 break;
960         }
961         PutToDict(dict, "code_rate_hp", tmp);
962         switch (parm_u_ofdm_constellation)
963         {
964         case QPSK:
965                 tmp = "QPSK";
966                 break;
967         case QAM_16:
968                 tmp = "QAM_16";
969                 break;
970         case QAM_64:
971                 tmp = "QAM_64";
972                 break;
973         default:
974         case QAM_AUTO:
975                 tmp = "QAM_AUTO";
976                 break;
977         }
978         PutToDict(dict, "constellation", tmp);
979         switch (parm_u_ofdm_transmission_mode)
980         {
981         case TRANSMISSION_MODE_2K:
982                 tmp = "TRANSMISSION_MODE_2K";
983                 break;
984         case TRANSMISSION_MODE_8K:
985                 tmp = "TRANSMISSION_MODE_8K";
986                 break;
987         default:
988         case TRANSMISSION_MODE_AUTO:
989                 tmp = "TRANSMISSION_MODE_AUTO";
990                 break;
991         }
992         PutToDict(dict, "transmission_mode", tmp);
993         switch (parm_u_ofdm_guard_interval)
994         {
995                 case GUARD_INTERVAL_1_32:
996                         tmp = "GUARD_INTERVAL_1_32";
997                         break;
998                 case GUARD_INTERVAL_1_16:
999                         tmp = "GUARD_INTERVAL_1_16";
1000                         break;
1001                 case GUARD_INTERVAL_1_8:
1002                         tmp = "GUARD_INTERVAL_1_8";
1003                         break;
1004                 case GUARD_INTERVAL_1_4:
1005                         tmp = "GUARD_INTERVAL_1_4";
1006                         break;
1007                 default:
1008                 case GUARD_INTERVAL_AUTO:
1009                         tmp = "GUARD_INTERVAL_AUTO";
1010                         break;
1011         }
1012         PutToDict(dict, "guard_interval", tmp);
1013         switch (parm_u_ofdm_hierarchy_information)
1014         {
1015                 case HIERARCHY_NONE:
1016                         tmp = "HIERARCHY_NONE";
1017                         break;
1018                 case HIERARCHY_1:
1019                         tmp = "HIERARCHY_1";
1020                         break;
1021                 case HIERARCHY_2:
1022                         tmp = "HIERARCHY_2";
1023                         break;
1024                 case HIERARCHY_4:
1025                         tmp = "HIERARCHY_4";
1026                         break;
1027                 default:
1028                 case HIERARCHY_AUTO:
1029                         tmp = "HIERARCHY_AUTO";
1030                         break;
1031         }
1032         PutToDict(dict, "hierarchy_information", tmp);
1033 }
1034
1035 void eDVBFrontend::getFrontendStatus(ePyObject dest)
1036 {
1037         if (dest && PyDict_Check(dest))
1038         {
1039                 const char *tmp = "UNKNOWN";
1040                 switch(m_state)
1041                 {
1042                         case stateIdle:
1043                                 tmp="IDLE";
1044                                 break;
1045                         case stateTuning:
1046                                 tmp="TUNING";
1047                                 break;
1048                         case stateFailed:
1049                                 tmp="FAILED";
1050                                 break;
1051                         case stateLock:
1052                                 tmp="LOCKED";
1053                                 break;
1054                         case stateLostLock:
1055                                 tmp="LOSTLOCK";
1056                                 break;
1057                         default:
1058                                 break;
1059                 }
1060                 PutToDict(dest, "tuner_state", tmp);
1061                 PutToDict(dest, "tuner_locked", readFrontendData(locked));
1062                 PutToDict(dest, "tuner_synced", readFrontendData(synced));
1063                 PutToDict(dest, "tuner_bit_error_rate", readFrontendData(bitErrorRate));
1064                 PutToDict(dest, "tuner_signal_power", readFrontendData(signalPower));
1065                 int sigPowerdB = readFrontendData(signalPowerdB);
1066                 if (sigPowerdB == 0x12345678) // not support yet
1067                 {
1068                         ePyObject obj=Py_None;
1069                         Py_INCREF(obj);
1070                         PutToDict(dest, "tuner_signal_power_db", obj);
1071                 }
1072                 else
1073                         PutToDict(dest, "tuner_signal_power_db", sigPowerdB);
1074                 PutToDict(dest, "tuner_signal_quality", readFrontendData(signalQuality));
1075         }
1076 }
1077
1078 void eDVBFrontend::getTransponderData(ePyObject dest, bool original)
1079 {
1080         if (m_fd != -1 && dest && PyDict_Check(dest))
1081         {
1082                 switch(m_type)
1083                 {
1084                         case feSatellite:
1085                         case feCable:
1086                         case feTerrestrial:
1087                         {
1088                                 FRONTENDPARAMETERS front;
1089                                 if (!original && ioctl(m_fd, FE_GET_FRONTEND, &front)<0)
1090                                         eDebug("FE_GET_FRONTEND (%m)");
1091                                 else
1092                                 {
1093                                         const FRONTENDPARAMETERS &parm = original ? this->parm : front;
1094                                         const char *tmp = "INVERSION_AUTO";
1095                                         switch(parm_inversion)
1096                                         {
1097                                                 case INVERSION_ON:
1098                                                         tmp = "INVERSION_ON";
1099                                                         break;
1100                                                 case INVERSION_OFF:
1101                                                         tmp = "INVERSION_OFF";
1102                                                         break;
1103                                                 default:
1104                                                         break;
1105                                         }
1106                                         if (tmp)
1107                                                 PutToDict(dest, "inversion", tmp);
1108
1109                                         switch(m_type)
1110                                         {
1111                                                 case feSatellite:
1112                                                         fillDictWithSatelliteData(dest, original?parm:front, this);
1113                                                         break;
1114                                                 case feCable:
1115                                                         fillDictWithCableData(dest, original?parm:front);
1116                                                         break;
1117                                                 case feTerrestrial:
1118                                                         fillDictWithTerrestrialData(dest, original?parm:front);
1119                                                         break;
1120                                         }
1121                                 }
1122                         }
1123                         default:
1124                                 break;
1125                 }
1126         }
1127 }
1128
1129 void eDVBFrontend::getFrontendData(ePyObject dest)
1130 {
1131         if (dest && PyDict_Check(dest))
1132         {
1133                 const char *tmp=0;
1134                 PutToDict(dest, "tuner_number", m_dvbid);
1135                 switch(m_type)
1136                 {
1137                         case feSatellite:
1138                                 tmp = "DVB-S";
1139                                 break;
1140                         case feCable:
1141                                 tmp = "DVB-C";
1142                                 break;
1143                         case feTerrestrial:
1144                                 tmp = "DVB-T";
1145                                 break;
1146                         default:
1147                                 tmp = "UNKNOWN";
1148                                 break;
1149                 }
1150                 PutToDict(dest, "tuner_type", tmp);
1151         }
1152 }
1153
1154 #ifndef FP_IOCTL_GET_ID
1155 #define FP_IOCTL_GET_ID 0
1156 #endif
1157 int eDVBFrontend::readInputpower()
1158 {
1159         int power=m_slotid;  // this is needed for read inputpower from the correct tuner !
1160
1161         // open front prozessor
1162         int fp=::open("/dev/dbox/fp0", O_RDWR);
1163         if (fp < 0)
1164         {
1165                 eDebug("couldn't open fp");
1166                 return -1;
1167         }
1168         static bool old_fp = (::ioctl(fp, FP_IOCTL_GET_ID) < 0);
1169         if ( ioctl( fp, old_fp ? 9 : 0x100, &power ) < 0 )
1170         {
1171                 eDebug("FP_IOCTL_GET_LNB_CURRENT failed (%m)");
1172                 return -1;
1173         }
1174         ::close(fp);
1175
1176         return power;
1177 }
1178
1179 bool eDVBFrontend::setSecSequencePos(int steps)
1180 {
1181         eDebug("set sequence pos %d", steps);
1182         if (!steps)
1183                 return false;
1184         while( steps > 0 )
1185         {
1186                 if (m_sec_sequence.current() != m_sec_sequence.end())
1187                         ++m_sec_sequence.current();
1188                 --steps;
1189         }
1190         while( steps < 0 )
1191         {
1192                 if (m_sec_sequence.current() != m_sec_sequence.begin() && m_sec_sequence.current() != m_sec_sequence.end())
1193                         --m_sec_sequence.current();
1194                 ++steps;
1195         }
1196         return true;
1197 }
1198
1199 void eDVBFrontend::setRotorData(int pos, int cmd)
1200 {
1201         m_data[ROTOR_CMD] = cmd;
1202         m_data[ROTOR_POS] = pos;
1203         if ( m_data[SATPOS_DEPENDS_PTR] != -1 )
1204         {
1205                 eDVBRegisteredFrontend *satpos_depends_to_fe = (eDVBRegisteredFrontend*) m_data[SATPOS_DEPENDS_PTR];
1206                 satpos_depends_to_fe->m_frontend->m_data[ROTOR_CMD] = cmd;
1207                 satpos_depends_to_fe->m_frontend->m_data[ROTOR_POS] = pos;
1208         }
1209         else
1210         {
1211                 eDVBRegisteredFrontend *next = (eDVBRegisteredFrontend *)m_data[LINKED_NEXT_PTR];
1212                 while ( (int)next != -1 )
1213                 {
1214                         next->m_frontend->m_data[ROTOR_CMD] = cmd;
1215                         next->m_frontend->m_data[ROTOR_POS] = pos;
1216                         next = (eDVBRegisteredFrontend *)next->m_frontend->m_data[LINKED_NEXT_PTR];
1217                 }
1218                 eDVBRegisteredFrontend *prev = (eDVBRegisteredFrontend *)m_data[LINKED_PREV_PTR];
1219                 while ( (int)prev != -1 )
1220                 {
1221                         prev->m_frontend->m_data[ROTOR_CMD] = cmd;
1222                         prev->m_frontend->m_data[ROTOR_POS] = pos;
1223                         prev = (eDVBRegisteredFrontend *)prev->m_frontend->m_data[LINKED_PREV_PTR];
1224                 }
1225         }
1226 }
1227
1228 void eDVBFrontend::tuneLoop()  // called by m_tuneTimer
1229 {
1230         int delay=0;
1231         if ( m_sec_sequence && m_sec_sequence.current() != m_sec_sequence.end() )
1232         {
1233 //              eDebug("tuneLoop %d\n", m_sec_sequence.current()->cmd);
1234                 switch (m_sec_sequence.current()->cmd)
1235                 {
1236                         case eSecCommand::SLEEP:
1237                                 delay = m_sec_sequence.current()++->msec;
1238                                 eDebug("[SEC] sleep %dms", delay);
1239                                 break;
1240                         case eSecCommand::GOTO:
1241                                 if ( !setSecSequencePos(m_sec_sequence.current()->steps) )
1242                                         ++m_sec_sequence.current();
1243                                 break;
1244                         case eSecCommand::SET_VOLTAGE:
1245                         {
1246                                 int voltage = m_sec_sequence.current()++->voltage;
1247                                 eDebug("[SEC] setVoltage %d", voltage);
1248                                 setVoltage(voltage);
1249                                 break;
1250                         }
1251                         case eSecCommand::IF_VOLTAGE_GOTO:
1252                         {
1253                                 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1254                                 if ( compare.voltage == m_data[CUR_VOLTAGE] && setSecSequencePos(compare.steps) )
1255                                         break;
1256                                 ++m_sec_sequence.current();
1257                                 break;
1258                         }
1259                         case eSecCommand::IF_NOT_VOLTAGE_GOTO:
1260                         {
1261                                 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1262                                 if ( compare.voltage != m_data[CUR_VOLTAGE] && setSecSequencePos(compare.steps) )
1263                                         break;
1264                                 ++m_sec_sequence.current();
1265                                 break;
1266                         }
1267                         case eSecCommand::IF_TONE_GOTO:
1268                         {
1269                                 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1270                                 if ( compare.tone == m_data[CUR_TONE] && setSecSequencePos(compare.steps) )
1271                                         break;
1272                                 ++m_sec_sequence.current();
1273                                 break;
1274                         }
1275                         case eSecCommand::IF_NOT_TONE_GOTO:
1276                         {
1277                                 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1278                                 if ( compare.tone != m_data[CUR_TONE] && setSecSequencePos(compare.steps) )
1279                                         break;
1280                                 ++m_sec_sequence.current();
1281                                 break;
1282                         }
1283                         case eSecCommand::SET_TONE:
1284                                 eDebug("[SEC] setTone %d", m_sec_sequence.current()->tone);
1285                                 setTone(m_sec_sequence.current()++->tone);
1286                                 break;
1287                         case eSecCommand::SEND_DISEQC:
1288                                 sendDiseqc(m_sec_sequence.current()->diseqc);
1289                                 eDebugNoNewLine("[SEC] sendDiseqc: ");
1290                                 for (int i=0; i < m_sec_sequence.current()->diseqc.len; ++i)
1291                                     eDebugNoNewLine("%02x", m_sec_sequence.current()->diseqc.data[i]);
1292                                 eDebug("");
1293                                 ++m_sec_sequence.current();
1294                                 break;
1295                         case eSecCommand::SEND_TONEBURST:
1296                                 eDebug("[SEC] sendToneburst: %d", m_sec_sequence.current()->toneburst);
1297                                 sendToneburst(m_sec_sequence.current()++->toneburst);
1298                                 break;
1299                         case eSecCommand::SET_FRONTEND:
1300                                 eDebug("[SEC] setFrontend");
1301                                 setFrontend();
1302                                 ++m_sec_sequence.current();
1303                                 break;
1304                         case eSecCommand::START_TUNE_TIMEOUT:
1305                                 m_timeout->start(5000, 1); // 5 sec timeout. TODO: symbolrate dependent
1306                                 ++m_sec_sequence.current();
1307                                 break;
1308                         case eSecCommand::SET_TIMEOUT:
1309                                 m_timeoutCount = m_sec_sequence.current()++->val;
1310                                 eDebug("[SEC] set timeout %d", m_timeoutCount);
1311                                 break;
1312                         case eSecCommand::IF_TIMEOUT_GOTO:
1313                                 if (!m_timeoutCount)
1314                                 {
1315                                         eDebug("[SEC] rotor timout");
1316                                         m_sec->setRotorMoving(false);
1317                                         setSecSequencePos(m_sec_sequence.current()->steps);
1318                                 }
1319                                 else
1320                                         ++m_sec_sequence.current();
1321                                 break;
1322                         case eSecCommand::MEASURE_IDLE_INPUTPOWER:
1323                         {
1324                                 int idx = m_sec_sequence.current()++->val;
1325                                 if ( idx == 0 || idx == 1 )
1326                                 {
1327                                         m_idleInputpower[idx] = readInputpower();
1328                                         eDebug("[SEC] idleInputpower[%d] is %d", idx, m_idleInputpower[idx]);
1329                                 }
1330                                 else
1331                                         eDebug("[SEC] idleInputpower measure index(%d) out of bound !!!", idx);
1332                                 break;
1333                         }
1334                         case eSecCommand::IF_MEASURE_IDLE_WAS_NOT_OK_GOTO:
1335                         {
1336                                 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1337                                 int idx = compare.val;
1338                                 if ( idx == 0 || idx == 1 )
1339                                 {
1340                                         int idle = readInputpower();
1341                                         int diff = abs(idle-m_idleInputpower[idx]);
1342                                         if ( diff > 0)
1343                                         {
1344                                                 eDebug("measure idle(%d) was not okay.. (%d - %d = %d) retry", idx, m_idleInputpower[idx], idle, diff);
1345                                                 setSecSequencePos(compare.steps);
1346                                                 break;
1347                                         }
1348                                 }
1349                                 ++m_sec_sequence.current();
1350                                 break;
1351                         }
1352                         case eSecCommand::IF_TUNER_LOCKED_GOTO:
1353                         {
1354                                 eSecCommand::rotor &cmd = m_sec_sequence.current()->measure;
1355                                 if (readFrontendData(locked))
1356                                 {
1357                                         eDebug("[SEC] locked step %d ok", cmd.okcount);
1358                                         ++cmd.okcount;
1359                                         if (cmd.okcount > 12)
1360                                         {
1361                                                 eDebug("ok > 12 .. goto %d\n",m_sec_sequence.current()->steps);
1362                                                 setSecSequencePos(cmd.steps);
1363                                                 break;
1364                                         }
1365                                 }
1366                                 else
1367                                 {
1368                                         eDebug("[SEC] rotor locked step %d failed", cmd.okcount);
1369                                         --m_timeoutCount;
1370                                         if (!m_timeoutCount && m_retryCount > 0)
1371                                                 --m_retryCount;
1372                                         cmd.okcount=0;
1373                                 }
1374                                 ++m_sec_sequence.current();
1375                                 break;
1376                         }
1377                         case eSecCommand::MEASURE_RUNNING_INPUTPOWER:
1378                                 m_runningInputpower = readInputpower();
1379                                 eDebug("[SEC] runningInputpower is %d", m_runningInputpower);
1380                                 ++m_sec_sequence.current();
1381                                 break;
1382                         case eSecCommand::IF_INPUTPOWER_DELTA_GOTO:
1383                         {
1384                                 int idleInputpower = m_idleInputpower[ (m_data[CUR_VOLTAGE]&1) ? 0 : 1];
1385                                 eSecCommand::rotor &cmd = m_sec_sequence.current()->measure;
1386                                 const char *txt = cmd.direction ? "running" : "stopped";
1387                                 eDebug("[SEC] waiting for rotor %s %d, idle %d, delta %d",
1388                                         txt,
1389                                         m_runningInputpower,
1390                                         idleInputpower,
1391                                         cmd.deltaA);
1392                                 if ( (cmd.direction && abs(m_runningInputpower - idleInputpower) >= cmd.deltaA)
1393                                         || (!cmd.direction && abs(m_runningInputpower - idleInputpower) <= cmd.deltaA) )
1394                                 {
1395                                         ++cmd.okcount;
1396                                         eDebug("[SEC] rotor %s step %d ok", txt, cmd.okcount);
1397                                         if ( cmd.okcount > 6 )
1398                                         {
1399                                                 m_sec->setRotorMoving(cmd.direction);
1400                                                 eDebug("[SEC] rotor is %s", txt);
1401                                                 if (setSecSequencePos(cmd.steps))
1402                                                         break;
1403                                         }
1404                                 }
1405                                 else
1406                                 {
1407                                         eDebug("[SEC] rotor not %s... reset counter.. increase timeout", txt);
1408                                         --m_timeoutCount;
1409                                         if (!m_timeoutCount && m_retryCount > 0)
1410                                                 --m_retryCount;
1411                                         cmd.okcount=0;
1412                                 }
1413                                 ++m_sec_sequence.current();
1414                                 break;
1415                         }
1416                         case eSecCommand::IF_ROTORPOS_VALID_GOTO:
1417                                 if (m_data[ROTOR_CMD] != -1 && m_data[ROTOR_POS] != -1)
1418                                         setSecSequencePos(m_sec_sequence.current()->steps);
1419                                 else
1420                                         ++m_sec_sequence.current();
1421                                 break;
1422                         case eSecCommand::INVALIDATE_CURRENT_ROTORPARMS:
1423                                 eDebug("[SEC] invalidate current rotorparams");
1424                                 setRotorData(-1,-1);    
1425                                 ++m_sec_sequence.current();
1426                                 break;
1427                         case eSecCommand::UPDATE_CURRENT_ROTORPARAMS:
1428                                 setRotorData(m_data[NEW_ROTOR_POS], m_data[NEW_ROTOR_CMD]);
1429                                 eDebug("[SEC] update current rotorparams %d %04x %d", m_timeoutCount, m_data[ROTOR_CMD], m_data[ROTOR_POS]);
1430                                 ++m_sec_sequence.current();
1431                                 break;
1432                         case eSecCommand::SET_ROTOR_DISEQC_RETRYS:
1433                                 m_retryCount = m_sec_sequence.current()++->val;
1434                                 eDebug("[SEC] set rotor retries %d", m_retryCount);
1435                                 break;
1436                         case eSecCommand::IF_NO_MORE_ROTOR_DISEQC_RETRYS_GOTO:
1437                                 if (!m_retryCount)
1438                                 {
1439                                         eDebug("[SEC] no more rotor retrys");
1440                                         setSecSequencePos(m_sec_sequence.current()->steps);
1441                                 }
1442                                 else
1443                                         ++m_sec_sequence.current();
1444                                 break;
1445                         case eSecCommand::SET_POWER_LIMITING_MODE:
1446                         {
1447                                 if (!m_need_rotor_workaround)
1448                                         break;
1449
1450                                 char dev[16];
1451
1452                                 // FIXMEEEEEE hardcoded i2c devices for dm7025 and dm8000
1453                                 if (m_slotid < 2)
1454                                         sprintf(dev, "/dev/i2c/%d", m_slotid);
1455                                 else if (m_slotid == 2)
1456                                         sprintf(dev, "/dev/i2c/2"); // first nim socket on DM8000 use /dev/i2c/2
1457                                 else if (m_slotid == 3)
1458                                         sprintf(dev, "/dev/i2c/4"); // second nim socket on DM8000 use /dev/i2c/4
1459                                 int fd = ::open(dev, O_RDWR);
1460
1461                                 unsigned char data[2];
1462                                 ::ioctl(fd, I2C_SLAVE_FORCE, 0x10 >> 1);
1463                                 if(::read(fd, data, 1) != 1)
1464                                         eDebug("[SEC] error read lnbp (%m)");
1465                                 if ( m_sec_sequence.current()->mode == eSecCommand::modeStatic )
1466                                 {
1467                                         data[0] |= 0x80;  // enable static current limiting
1468                                         eDebug("[SEC] set static current limiting");
1469                                 }
1470                                 else
1471                                 {
1472                                         data[0] &= ~0x80;  // enable dynamic current limiting
1473                                         eDebug("[SEC] set dynamic current limiting");
1474                                 }
1475                                 if(::write(fd, data, 1) != 1)
1476                                         eDebug("[SEC] error write lnbp (%m)");
1477                                 ::close(fd);
1478                                 ++m_sec_sequence.current();
1479                                 break;
1480                         }
1481                         default:
1482                                 eDebug("[SEC] unhandled sec command %d",
1483                                         ++m_sec_sequence.current()->cmd);
1484                                 ++m_sec_sequence.current();
1485                 }
1486                 m_tuneTimer->start(delay,true);
1487         }
1488 }
1489
1490 void eDVBFrontend::setFrontend()
1491 {
1492         eDebug("setting frontend %d", m_dvbid);
1493         m_sn->start();
1494         feEvent(-1);
1495         if (ioctl(m_fd, FE_SET_FRONTEND, &parm) == -1)
1496         {
1497                 perror("FE_SET_FRONTEND failed");
1498                 return;
1499         }
1500 }
1501
1502 RESULT eDVBFrontend::getFrontendType(int &t)
1503 {
1504         if (m_type == -1)
1505                 return -ENODEV;
1506         t = m_type;
1507         return 0;
1508 }
1509
1510 RESULT eDVBFrontend::prepare_sat(const eDVBFrontendParametersSatellite &feparm)
1511 {
1512         int res;
1513         if (!m_sec)
1514         {
1515                 eWarning("no SEC module active!");
1516                 return -ENOENT;
1517         }
1518         res = m_sec->prepare(*this, parm, feparm, 1 << m_slotid);
1519         if (!res)
1520         {
1521                 eDebug("prepare_sat System %d Freq %d Pol %d SR %d INV %d FEC %d orbpos %d",
1522                         feparm.system,
1523                         feparm.frequency,
1524                         feparm.polarisation,
1525                         feparm.symbol_rate,
1526                         feparm.inversion,
1527                         feparm.fec,
1528                         feparm.orbital_position);
1529                 parm_u_qpsk_symbol_rate = feparm.symbol_rate;
1530                 switch (feparm.inversion)
1531                 {
1532                         case eDVBFrontendParametersSatellite::Inversion::On:
1533                                 parm_inversion = INVERSION_ON;
1534                                 break;
1535                         case eDVBFrontendParametersSatellite::Inversion::Off:
1536                                 parm_inversion = INVERSION_OFF;
1537                                 break;
1538                         default:
1539                         case eDVBFrontendParametersSatellite::Inversion::Unknown:
1540                                 parm_inversion = INVERSION_AUTO;
1541                                 break;
1542                 }
1543                 if (feparm.system == eDVBFrontendParametersSatellite::System::DVB_S)
1544                         switch (feparm.fec)
1545                         {
1546                                 case eDVBFrontendParametersSatellite::FEC::fNone:
1547                                         parm_u_qpsk_fec_inner = FEC_NONE;
1548                                         break;
1549                                 case eDVBFrontendParametersSatellite::FEC::f1_2:
1550                                         parm_u_qpsk_fec_inner = FEC_1_2;
1551                                         break;
1552                                 case eDVBFrontendParametersSatellite::FEC::f2_3:
1553                                         parm_u_qpsk_fec_inner = FEC_2_3;
1554                                         break;
1555                                 case eDVBFrontendParametersSatellite::FEC::f3_4:
1556                                         parm_u_qpsk_fec_inner = FEC_3_4;
1557                                         break;
1558                                 case eDVBFrontendParametersSatellite::FEC::f5_6:
1559                                         parm_u_qpsk_fec_inner = FEC_5_6;
1560                                         break;
1561                                 case eDVBFrontendParametersSatellite::FEC::f7_8:
1562                                         parm_u_qpsk_fec_inner = FEC_7_8;
1563                                         break;
1564                                 default:
1565                                         eDebug("no valid fec for DVB-S set.. assume auto");
1566                                 case eDVBFrontendParametersSatellite::FEC::fAuto:
1567                                         parm_u_qpsk_fec_inner = FEC_AUTO;
1568                                         break;
1569                         }
1570 #if HAVE_DVB_API_VERSION >= 3
1571                 else // DVB_S2
1572                 {
1573                         switch (feparm.fec)
1574                         {
1575                                 case eDVBFrontendParametersSatellite::FEC::f1_2:
1576                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_1_2;
1577                                         break;
1578                                 case eDVBFrontendParametersSatellite::FEC::f2_3:
1579                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_2_3;
1580                                         break;
1581                                 case eDVBFrontendParametersSatellite::FEC::f3_4:
1582                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_3_4;
1583                                         break;
1584                                 case eDVBFrontendParametersSatellite::FEC::f3_5:
1585                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_3_5;
1586                                         break;
1587                                 case eDVBFrontendParametersSatellite::FEC::f4_5:
1588                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_4_5;
1589                                         break;
1590                                 case eDVBFrontendParametersSatellite::FEC::f5_6:
1591                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_5_6;
1592                                         break;
1593                                 case eDVBFrontendParametersSatellite::FEC::f7_8:
1594                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_7_8;
1595                                         break;
1596                                 case eDVBFrontendParametersSatellite::FEC::f8_9:
1597                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_8_9;
1598                                         break;
1599                                 case eDVBFrontendParametersSatellite::FEC::f9_10:
1600                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_9_10;
1601                                         break;
1602                                 default:
1603                                         eDebug("no valid fec for DVB-S2 set.. abort !!");
1604                                         return -EINVAL;
1605                         }
1606                         if (feparm.modulation == eDVBFrontendParametersSatellite::Modulation::M8PSK)
1607                                 parm_u_qpsk_fec_inner = (fe_code_rate_t)((int)parm_u_qpsk_fec_inner+9);
1608                                 // 8PSK fec driver values are decimal 9 bigger
1609                 }
1610 #endif
1611                 // FIXME !!! get frequency range from tuner
1612                 if ( parm_frequency < 900000 || parm_frequency > 2200000 )
1613                 {
1614                         eDebug("%d mhz out of tuner range.. dont tune", parm_frequency/1000);
1615                         return -EINVAL;
1616                 }
1617                 eDebug("tuning to %d mhz", parm_frequency/1000);
1618         }
1619         return res;
1620 }
1621
1622 RESULT eDVBFrontend::prepare_cable(const eDVBFrontendParametersCable &feparm)
1623 {
1624         parm_frequency = feparm.frequency * 1000;
1625         parm_u_qam_symbol_rate = feparm.symbol_rate;
1626         switch (feparm.modulation)
1627         {
1628         case eDVBFrontendParametersCable::Modulation::QAM16:
1629                 parm_u_qam_modulation = QAM_16;
1630                 break;
1631         case eDVBFrontendParametersCable::Modulation::QAM32:
1632                 parm_u_qam_modulation = QAM_32;
1633                 break;
1634         case eDVBFrontendParametersCable::Modulation::QAM64:
1635                 parm_u_qam_modulation = QAM_64;
1636                 break;
1637         case eDVBFrontendParametersCable::Modulation::QAM128:
1638                 parm_u_qam_modulation = QAM_128;
1639                 break;
1640         case eDVBFrontendParametersCable::Modulation::QAM256:
1641                 parm_u_qam_modulation = QAM_256;
1642                 break;
1643         default:
1644         case eDVBFrontendParametersCable::Modulation::Auto:
1645                 parm_u_qam_modulation = QAM_AUTO;
1646                 break;
1647         }
1648         switch (feparm.inversion)
1649         {
1650         case eDVBFrontendParametersCable::Inversion::On:
1651                 parm_inversion = INVERSION_ON;
1652                 break;
1653         case eDVBFrontendParametersCable::Inversion::Off:
1654                 parm_inversion = INVERSION_OFF;
1655                 break;
1656         default:
1657         case eDVBFrontendParametersCable::Inversion::Unknown:
1658                 parm_inversion = INVERSION_AUTO;
1659                 break;
1660         }
1661         switch (feparm.fec_inner)
1662         {
1663         case eDVBFrontendParametersCable::FEC::fNone:
1664                 parm_u_qam_fec_inner = FEC_NONE;
1665                 break;
1666         case eDVBFrontendParametersCable::FEC::f1_2:
1667                 parm_u_qam_fec_inner = FEC_1_2;
1668                 break;
1669         case eDVBFrontendParametersCable::FEC::f2_3:
1670                 parm_u_qam_fec_inner = FEC_2_3;
1671                 break;
1672         case eDVBFrontendParametersCable::FEC::f3_4:
1673                 parm_u_qam_fec_inner = FEC_3_4;
1674                 break;
1675         case eDVBFrontendParametersCable::FEC::f5_6:
1676                 parm_u_qam_fec_inner = FEC_5_6;
1677                 break;
1678         case eDVBFrontendParametersCable::FEC::f7_8:
1679                 parm_u_qam_fec_inner = FEC_7_8;
1680                 break;
1681 #if HAVE_DVB_API_VERSION >= 3
1682         case eDVBFrontendParametersCable::FEC::f8_9:
1683                 parm_u_qam_fec_inner = FEC_8_9;
1684                 break;
1685 #endif
1686         default:
1687         case eDVBFrontendParametersCable::FEC::fAuto:
1688                 parm_u_qam_fec_inner = FEC_AUTO;
1689                 break;
1690         }
1691         eDebug("tuning to %d khz, sr %d, fec %d, modulation %d, inversion %d",
1692                 parm_frequency/1000,
1693                 parm_u_qam_symbol_rate,
1694                 parm_u_qam_fec_inner,
1695                 parm_u_qam_modulation,
1696                 parm_inversion);
1697         return 0;
1698 }
1699
1700 RESULT eDVBFrontend::prepare_terrestrial(const eDVBFrontendParametersTerrestrial &feparm)
1701 {
1702         parm_frequency = feparm.frequency;
1703
1704         switch (feparm.bandwidth)
1705         {
1706         case eDVBFrontendParametersTerrestrial::Bandwidth::Bw8MHz:
1707                 parm_u_ofdm_bandwidth = BANDWIDTH_8_MHZ;
1708                 break;
1709         case eDVBFrontendParametersTerrestrial::Bandwidth::Bw7MHz:
1710                 parm_u_ofdm_bandwidth = BANDWIDTH_7_MHZ;
1711                 break;
1712         case eDVBFrontendParametersTerrestrial::Bandwidth::Bw6MHz:
1713                 parm_u_ofdm_bandwidth = BANDWIDTH_6_MHZ;
1714                 break;
1715         default:
1716         case eDVBFrontendParametersTerrestrial::Bandwidth::BwAuto:
1717                 parm_u_ofdm_bandwidth = BANDWIDTH_AUTO;
1718                 break;
1719         }
1720         switch (feparm.code_rate_LP)
1721         {
1722         case eDVBFrontendParametersTerrestrial::FEC::f1_2:
1723                 parm_u_ofdm_code_rate_LP = FEC_1_2;
1724                 break;
1725         case eDVBFrontendParametersTerrestrial::FEC::f2_3:
1726                 parm_u_ofdm_code_rate_LP = FEC_2_3;
1727                 break;
1728         case eDVBFrontendParametersTerrestrial::FEC::f3_4:
1729                 parm_u_ofdm_code_rate_LP = FEC_3_4;
1730                 break;
1731         case eDVBFrontendParametersTerrestrial::FEC::f5_6:
1732                 parm_u_ofdm_code_rate_LP = FEC_5_6;
1733                 break;
1734         case eDVBFrontendParametersTerrestrial::FEC::f7_8:
1735                 parm_u_ofdm_code_rate_LP = FEC_7_8;
1736                 break;
1737         default:
1738         case eDVBFrontendParametersTerrestrial::FEC::fAuto:
1739                 parm_u_ofdm_code_rate_LP = FEC_AUTO;
1740                 break;
1741         }
1742         switch (feparm.code_rate_HP)
1743         {
1744         case eDVBFrontendParametersTerrestrial::FEC::f1_2:
1745                 parm_u_ofdm_code_rate_HP = FEC_1_2;
1746                 break;
1747         case eDVBFrontendParametersTerrestrial::FEC::f2_3:
1748                 parm_u_ofdm_code_rate_HP = FEC_2_3;
1749                 break;
1750         case eDVBFrontendParametersTerrestrial::FEC::f3_4:
1751                 parm_u_ofdm_code_rate_HP = FEC_3_4;
1752                 break;
1753         case eDVBFrontendParametersTerrestrial::FEC::f5_6:
1754                 parm_u_ofdm_code_rate_HP = FEC_5_6;
1755                 break;
1756         case eDVBFrontendParametersTerrestrial::FEC::f7_8:
1757                 parm_u_ofdm_code_rate_HP = FEC_7_8;
1758                 break;
1759         default:
1760         case eDVBFrontendParametersTerrestrial::FEC::fAuto:
1761                 parm_u_ofdm_code_rate_HP = FEC_AUTO;
1762                 break;
1763         }
1764         switch (feparm.modulation)
1765         {
1766         case eDVBFrontendParametersTerrestrial::Modulation::QPSK:
1767                 parm_u_ofdm_constellation = QPSK;
1768                 break;
1769         case eDVBFrontendParametersTerrestrial::Modulation::QAM16:
1770                 parm_u_ofdm_constellation = QAM_16;
1771                 break;
1772         case eDVBFrontendParametersTerrestrial::Modulation::QAM64:
1773                 parm_u_ofdm_constellation = QAM_64;
1774                 break;
1775         default:
1776         case eDVBFrontendParametersTerrestrial::Modulation::Auto:
1777                 parm_u_ofdm_constellation = QAM_AUTO;
1778                 break;
1779         }
1780         switch (feparm.transmission_mode)
1781         {
1782         case eDVBFrontendParametersTerrestrial::TransmissionMode::TM2k:
1783                 parm_u_ofdm_transmission_mode = TRANSMISSION_MODE_2K;
1784                 break;
1785         case eDVBFrontendParametersTerrestrial::TransmissionMode::TM8k:
1786                 parm_u_ofdm_transmission_mode = TRANSMISSION_MODE_8K;
1787                 break;
1788         default:
1789         case eDVBFrontendParametersTerrestrial::TransmissionMode::TMAuto:
1790                 parm_u_ofdm_transmission_mode = TRANSMISSION_MODE_AUTO;
1791                 break;
1792         }
1793         switch (feparm.guard_interval)
1794         {
1795                 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_32:
1796                         parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_32;
1797                         break;
1798                 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_16:
1799                         parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_16;
1800                         break;
1801                 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_8:
1802                         parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_8;
1803                         break;
1804                 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_4:
1805                         parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_4;
1806                         break;
1807                 default:
1808                 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_Auto:
1809                         parm_u_ofdm_guard_interval = GUARD_INTERVAL_AUTO;
1810                         break;
1811         }
1812         switch (feparm.hierarchy)
1813         {
1814                 case eDVBFrontendParametersTerrestrial::Hierarchy::HNone:
1815                         parm_u_ofdm_hierarchy_information = HIERARCHY_NONE;
1816                         break;
1817                 case eDVBFrontendParametersTerrestrial::Hierarchy::H1:
1818                         parm_u_ofdm_hierarchy_information = HIERARCHY_1;
1819                         break;
1820                 case eDVBFrontendParametersTerrestrial::Hierarchy::H2:
1821                         parm_u_ofdm_hierarchy_information = HIERARCHY_2;
1822                         break;
1823                 case eDVBFrontendParametersTerrestrial::Hierarchy::H4:
1824                         parm_u_ofdm_hierarchy_information = HIERARCHY_4;
1825                         break;
1826                 default:
1827                 case eDVBFrontendParametersTerrestrial::Hierarchy::HAuto:
1828                         parm_u_ofdm_hierarchy_information = HIERARCHY_AUTO;
1829                         break;
1830         }
1831         switch (feparm.inversion)
1832         {
1833         case eDVBFrontendParametersTerrestrial::Inversion::On:
1834                 parm_inversion = INVERSION_ON;
1835                 break;
1836         case eDVBFrontendParametersTerrestrial::Inversion::Off:
1837                 parm_inversion = INVERSION_OFF;
1838                 break;
1839         default:
1840         case eDVBFrontendParametersTerrestrial::Inversion::Unknown:
1841                 parm_inversion = INVERSION_AUTO;
1842                 break;
1843         }
1844         return 0;
1845 }
1846
1847 RESULT eDVBFrontend::tune(const iDVBFrontendParameters &where)
1848 {
1849         eDebug("(%d)tune", m_dvbid);
1850
1851         m_timeout->stop();
1852
1853         int res=0;
1854
1855         if (!m_sn)
1856         {
1857                 eDebug("no frontend device opened... do not try to tune !!!");
1858                 res = -ENODEV;
1859                 goto tune_error;
1860         }
1861
1862         if (m_type == -1)
1863         {
1864                 res = -ENODEV;
1865                 goto tune_error;
1866         }
1867
1868         m_sn->stop();
1869         m_sec_sequence.clear();
1870
1871         switch (m_type)
1872         {
1873         case feSatellite:
1874         {
1875                 eDVBFrontendParametersSatellite feparm;
1876                 if (where.getDVBS(feparm))
1877                 {
1878                         eDebug("no dvbs data!");
1879                         res = -EINVAL;
1880                         goto tune_error;
1881                 }
1882                 m_sec->setRotorMoving(false);
1883                 res=prepare_sat(feparm);
1884                 if (res)
1885                         goto tune_error;
1886
1887                 break;
1888         }
1889         case feCable:
1890         {
1891                 eDVBFrontendParametersCable feparm;
1892                 if (where.getDVBC(feparm))
1893                 {
1894                         res = -EINVAL;
1895                         goto tune_error;
1896                 }
1897                 res=prepare_cable(feparm);
1898                 if (res)
1899                         goto tune_error;
1900
1901                 m_sec_sequence.push_back( eSecCommand(eSecCommand::START_TUNE_TIMEOUT) );
1902                 m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
1903                 break;
1904         }
1905         case feTerrestrial:
1906         {
1907                 eDVBFrontendParametersTerrestrial feparm;
1908                 if (where.getDVBT(feparm))
1909                 {
1910                         eDebug("no -T data");
1911                         res = -EINVAL;
1912                         goto tune_error;
1913                 }
1914                 res=prepare_terrestrial(feparm);
1915                 if (res)
1916                         goto tune_error;
1917
1918                 std::string enable_5V;
1919                 char configStr[255];
1920                 snprintf(configStr, 255, "config.Nims.%d.terrestrial_5V", m_slotid);
1921                 m_sec_sequence.push_back( eSecCommand(eSecCommand::START_TUNE_TIMEOUT) );
1922                 ePythonConfigQuery::getConfigValue(configStr, enable_5V);
1923                 if (enable_5V == "True")
1924                         m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, iDVBFrontend::voltage13) );
1925                 else
1926                         m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, iDVBFrontend::voltageOff) );
1927                 m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
1928
1929                 break;
1930         }
1931         }
1932
1933         m_tuneTimer->start(0,true);
1934         m_sec_sequence.current() = m_sec_sequence.begin();
1935
1936         if (m_state != stateTuning)
1937         {
1938                 m_tuning = 1;
1939                 m_state = stateTuning;
1940                 m_stateChanged(this);
1941         }
1942
1943         return res;
1944
1945 tune_error:
1946         m_tuneTimer->stop();
1947         return res;
1948 }
1949
1950 RESULT eDVBFrontend::connectStateChange(const Slot1<void,iDVBFrontend*> &stateChange, ePtr<eConnection> &connection)
1951 {
1952         connection = new eConnection(this, m_stateChanged.connect(stateChange));
1953         return 0;
1954 }
1955
1956 RESULT eDVBFrontend::setVoltage(int voltage)
1957 {
1958         if (m_type == feCable)
1959                 return -1;
1960 #if HAVE_DVB_API_VERSION < 3
1961         secVoltage vlt;
1962 #else
1963         bool increased=false;
1964         fe_sec_voltage_t vlt;
1965 #endif
1966         m_data[CUR_VOLTAGE]=voltage;
1967         switch (voltage)
1968         {
1969         case voltageOff:
1970                 for (int i=0; i < 3; ++i)  // reset diseqc
1971                         m_data[i]=-1;
1972                 vlt = SEC_VOLTAGE_OFF;
1973                 break;
1974         case voltage13_5:
1975 #if HAVE_DVB_API_VERSION < 3
1976                 vlt = SEC_VOLTAGE_13_5;
1977                 break;
1978 #else
1979                 increased = true;
1980 #endif
1981         case voltage13:
1982                 vlt = SEC_VOLTAGE_13;
1983                 break;
1984         case voltage18_5:
1985 #if HAVE_DVB_API_VERSION < 3
1986                 vlt = SEC_VOLTAGE_18_5;
1987                 break;
1988 #else
1989                 increased = true;
1990 #endif
1991         case voltage18:
1992                 vlt = SEC_VOLTAGE_18;
1993                 break;
1994         default:
1995                 return -ENODEV;
1996         }
1997 #if HAVE_DVB_API_VERSION < 3
1998         return ::ioctl(m_secfd, SEC_SET_VOLTAGE, vlt);
1999 #else
2000         if (m_type == feSatellite && ::ioctl(m_fd, FE_ENABLE_HIGH_LNB_VOLTAGE, increased) < 0)
2001                 perror("FE_ENABLE_HIGH_LNB_VOLTAGE");
2002         return ::ioctl(m_fd, FE_SET_VOLTAGE, vlt);
2003 #endif
2004 }
2005
2006 RESULT eDVBFrontend::getState(int &state)
2007 {
2008         state = m_state;
2009         return 0;
2010 }
2011
2012 RESULT eDVBFrontend::setTone(int t)
2013 {
2014         if (m_type != feSatellite)
2015                 return -1;
2016 #if HAVE_DVB_API_VERSION < 3
2017         secToneMode_t tone;
2018 #else
2019         fe_sec_tone_mode_t tone;
2020 #endif
2021         m_data[CUR_TONE]=t;
2022         switch (t)
2023         {
2024         case toneOn:
2025                 tone = SEC_TONE_ON;
2026                 break;
2027         case toneOff:
2028                 tone = SEC_TONE_OFF;
2029                 break;
2030         default:
2031                 return -ENODEV;
2032         }
2033 #if HAVE_DVB_API_VERSION < 3    
2034         return ::ioctl(m_secfd, SEC_SET_TONE, tone);
2035 #else   
2036         return ::ioctl(m_fd, FE_SET_TONE, tone);
2037 #endif
2038 }
2039
2040 #if HAVE_DVB_API_VERSION < 3 && !defined(SEC_DISEQC_SEND_MASTER_CMD)
2041         #define SEC_DISEQC_SEND_MASTER_CMD _IOW('o', 97, struct secCommand *)
2042 #endif
2043
2044 RESULT eDVBFrontend::sendDiseqc(const eDVBDiseqcCommand &diseqc)
2045 {
2046 #if HAVE_DVB_API_VERSION < 3
2047         struct secCommand cmd;
2048         cmd.type = SEC_CMDTYPE_DISEQC_RAW;
2049         cmd.u.diseqc.cmdtype = diseqc.data[0];
2050         cmd.u.diseqc.addr = diseqc.data[1];
2051         cmd.u.diseqc.cmd = diseqc.data[2];
2052         cmd.u.diseqc.numParams = diseqc.len-3;
2053         memcpy(cmd.u.diseqc.params, diseqc.data+3, diseqc.len-3);
2054         if (::ioctl(m_secfd, SEC_DISEQC_SEND_MASTER_CMD, &cmd))
2055 #else
2056         struct dvb_diseqc_master_cmd cmd;
2057         memcpy(cmd.msg, diseqc.data, diseqc.len);
2058         cmd.msg_len = diseqc.len;
2059         if (::ioctl(m_fd, FE_DISEQC_SEND_MASTER_CMD, &cmd))
2060 #endif
2061                 return -EINVAL;
2062         return 0;
2063 }
2064
2065 #if HAVE_DVB_API_VERSION < 3 && !defined(SEC_DISEQC_SEND_BURST)
2066         #define SEC_DISEQC_SEND_BURST _IO('o', 96)
2067 #endif
2068 RESULT eDVBFrontend::sendToneburst(int burst)
2069 {
2070 #if HAVE_DVB_API_VERSION < 3
2071         secMiniCmd cmd = SEC_MINI_NONE;
2072 #else
2073         fe_sec_mini_cmd_t cmd = SEC_MINI_A;
2074 #endif
2075         if ( burst == eDVBSatelliteDiseqcParameters::A )
2076                 cmd = SEC_MINI_A;
2077         else if ( burst == eDVBSatelliteDiseqcParameters::B )
2078                 cmd = SEC_MINI_B;
2079 #if HAVE_DVB_API_VERSION < 3
2080         if (::ioctl(m_secfd, SEC_DISEQC_SEND_BURST, cmd))
2081                 return -EINVAL;
2082 #else
2083         if (::ioctl(m_fd, FE_DISEQC_SEND_BURST, cmd))
2084                 return -EINVAL;
2085 #endif
2086         return 0;
2087 }
2088
2089 RESULT eDVBFrontend::setSEC(iDVBSatelliteEquipmentControl *sec)
2090 {
2091         m_sec = sec;
2092         return 0;
2093 }
2094
2095 RESULT eDVBFrontend::setSecSequence(const eSecCommandList &list)
2096 {
2097         m_sec_sequence = list;
2098         return 0;
2099 }
2100
2101 RESULT eDVBFrontend::getData(int num, int &data)
2102 {
2103         if ( num < NUM_DATA_ENTRIES )
2104         {
2105                 data = m_data[num];
2106                 return 0;
2107         }
2108         return -EINVAL;
2109 }
2110
2111 RESULT eDVBFrontend::setData(int num, int val)
2112 {
2113         if ( num < NUM_DATA_ENTRIES )
2114         {
2115                 m_data[num] = val;
2116                 return 0;
2117         }
2118         return -EINVAL;
2119 }
2120
2121 int eDVBFrontend::isCompatibleWith(ePtr<iDVBFrontendParameters> &feparm)
2122 {
2123         int type;
2124         if (feparm->getSystem(type) || type != m_type || !m_enabled)
2125                 return 0;
2126
2127         if (m_type == eDVBFrontend::feSatellite)
2128         {
2129                 ASSERT(m_sec);
2130                 eDVBFrontendParametersSatellite sat_parm;
2131                 int ret = feparm->getDVBS(sat_parm);
2132                 ASSERT(!ret);
2133                 return m_sec->canTune(sat_parm, this, 1 << m_slotid);
2134         }
2135         else if (m_type == eDVBFrontend::feCable)
2136                 return 2;  // more prio for cable frontends
2137         return 1;
2138 }
2139
2140 bool eDVBFrontend::setSlotInfo(ePyObject obj)
2141 {
2142         ePyObject Id, Descr, Enabled;
2143         if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 3)
2144                 goto arg_error;
2145         Id = PyTuple_GET_ITEM(obj, 0);
2146         Descr = PyTuple_GET_ITEM(obj, 1);
2147         Enabled = PyTuple_GET_ITEM(obj, 2);
2148         if (!PyInt_Check(Id) || !PyString_Check(Descr) || !PyBool_Check(Enabled))
2149                 goto arg_error;
2150         strcpy(m_description, PyString_AS_STRING(Descr));
2151         m_slotid = PyInt_AsLong(Id);
2152         m_enabled = Enabled == Py_True;
2153         // HACK.. the rotor workaround is neede for all NIMs with LNBP21 voltage regulator...
2154         m_need_rotor_workaround = !!strstr(m_description, "Alps BSBE1") ||
2155                 !!strstr(m_description, "Alps BSBE2") ||
2156                 !!strstr(m_description, "Alps -S");
2157         eDebug("setSlotInfo for dvb frontend %d to slotid %d, descr %s, need rotorworkaround %s, enabled %s",
2158                 m_dvbid, m_slotid, m_description, m_need_rotor_workaround ? "Yes" : "No", m_enabled ? "Yes" : "No" );
2159         return true;
2160 arg_error:
2161         PyErr_SetString(PyExc_StandardError,
2162                 "eDVBFrontend::setSlotInfo must get a tuple with first param slotid, second param slot description and third param enabled boolean");
2163         return false;
2164 }