disable WSS when 4:3 letterbox or 4:3 panscan is selected and "WSS on 4:3"
[enigma2.git] / lib / dvb / frontend.cpp
index 353f85ad8949428c8c4667d6088c9085e1cbbb02..168ff05227842901a86fb7db46712f34defbbe35 100644 (file)
@@ -1,5 +1,6 @@
 #include <lib/dvb/dvb.h>
 #include <lib/base/eerror.h>
+#include <lib/base/nconfig.h> // access to python config
 #include <errno.h>
 #include <unistd.h>
 #include <fcntl.h>
 #define parm_u_ofdm_transmission_mode parm.u.ofdm.transmission_mode
 #define parm_u_ofdm_guard_interval parm.u.ofdm.guard_interval
 #define parm_u_ofdm_hierarchy_information parm.u.ofdm.hierarchy_information
+#ifdef FEC_9_10
+       #warning "FEC_9_10 already exist in dvb api ... it seems it is now ready for DVB-S2"
+#else
+       #define FEC_S2_QPSK_1_2 (fe_code_rate_t)(FEC_AUTO+1)
+       #define FEC_S2_QPSK_2_3 (fe_code_rate_t)(FEC_S2_QPSK_1_2+1)
+       #define FEC_S2_QPSK_3_4 (fe_code_rate_t)(FEC_S2_QPSK_2_3+1)
+       #define FEC_S2_QPSK_5_6 (fe_code_rate_t)(FEC_S2_QPSK_3_4+1)
+       #define FEC_S2_QPSK_7_8 (fe_code_rate_t)(FEC_S2_QPSK_5_6+1)
+       #define FEC_S2_QPSK_8_9 (fe_code_rate_t)(FEC_S2_QPSK_7_8+1)
+       #define FEC_S2_QPSK_3_5 (fe_code_rate_t)(FEC_S2_QPSK_8_9+1)
+       #define FEC_S2_QPSK_4_5 (fe_code_rate_t)(FEC_S2_QPSK_3_5+1)
+       #define FEC_S2_QPSK_9_10 (fe_code_rate_t)(FEC_S2_QPSK_4_5+1)
+       #define FEC_S2_8PSK_1_2 (fe_code_rate_t)(FEC_S2_QPSK_9_10+1)
+       #define FEC_S2_8PSK_2_3 (fe_code_rate_t)(FEC_S2_8PSK_1_2+1)
+       #define FEC_S2_8PSK_3_4 (fe_code_rate_t)(FEC_S2_8PSK_2_3+1)
+       #define FEC_S2_8PSK_5_6 (fe_code_rate_t)(FEC_S2_8PSK_3_4+1)
+       #define FEC_S2_8PSK_7_8 (fe_code_rate_t)(FEC_S2_8PSK_5_6+1)
+       #define FEC_S2_8PSK_8_9 (fe_code_rate_t)(FEC_S2_8PSK_7_8+1)
+       #define FEC_S2_8PSK_3_5 (fe_code_rate_t)(FEC_S2_8PSK_8_9+1)
+       #define FEC_S2_8PSK_4_5 (fe_code_rate_t)(FEC_S2_8PSK_3_5+1)
+       #define FEC_S2_8PSK_9_10 (fe_code_rate_t)(FEC_S2_8PSK_4_5+1)
+#endif
 #endif
 
 #include <dvbsi++/satellite_delivery_system_descriptor.h>
 #include <dvbsi++/cable_delivery_system_descriptor.h>
 #include <dvbsi++/terrestrial_delivery_system_descriptor.h>
 
+void eDVBDiseqcCommand::setCommandString(const char *str)
+{
+       if (!str)
+               return;
+       len=0;
+       int slen = strlen(str);
+       if (slen % 2)
+       {
+               eDebug("invalid diseqc command string length (not 2 byte aligned)");
+               return;
+       }
+       if (slen > MAX_DISEQC_LENGTH*2)
+       {
+               eDebug("invalid diseqc command string length (string is to long)");
+               return;
+       }
+       unsigned char val=0;
+       for (int i=0; i < slen; ++i)
+       {
+               unsigned char c = str[i];
+               switch(c)
+               {
+                       case '0' ... '9': c-=48; break;
+                       case 'a' ... 'f': c-=87; break;
+                       case 'A' ... 'F': c-=55; break;
+                       default:
+                               eDebug("invalid character in hex string..ignore complete diseqc command !");
+                               return;
+               }
+               if ( i % 2 )
+               {
+                       val |= c;
+                       data[i/2] = val;
+               }
+               else
+                       val = c << 4;
+       }
+       len = slen/2;
+}
+
 void eDVBFrontendParametersSatellite::set(const SatelliteDeliverySystemDescriptor &descriptor)
 {
        frequency    = descriptor.getFrequency() * 10;
        symbol_rate  = descriptor.getSymbolRate() * 100;
        polarisation = descriptor.getPolarization();
        fec = descriptor.getFecInner();
-       if ( fec == 0xF )
-               fec = FEC::fNone;
+       if ( fec != FEC::fNone && fec > FEC::f9_10 )
+               fec = FEC::fAuto;
        inversion = Inversion::Unknown;
        orbital_position  = ((descriptor.getOrbitalPosition() >> 12) & 0xF) * 1000;
        orbital_position += ((descriptor.getOrbitalPosition() >> 8) & 0xF) * 100;
@@ -68,11 +131,32 @@ void eDVBFrontendParametersSatellite::set(const SatelliteDeliverySystemDescripto
        orbital_position += ((descriptor.getOrbitalPosition()) & 0xF);
        if (orbital_position && (!descriptor.getWestEastFlag()))
                orbital_position = 3600 - orbital_position;
-       eDebug("SAT freq %d, %s, pos %d, sr %d, fec %d",
-               frequency,
-               polarisation ? "hor" : "vert",
-               orbital_position,
-               symbol_rate, fec);
+       system = descriptor.getModulationSystem();
+       modulation = descriptor.getModulation();
+       if (system == System::DVB_S && modulation == Modulation::M8PSK)
+       {
+               eDebug("satellite_delivery_descriptor non valid modulation type.. force QPSK");
+               modulation=QPSK;
+       }
+       roll_off = descriptor.getRollOff();
+       if (system == System::DVB_S2)
+       {
+               eDebug("SAT DVB-S2 freq %d, %s, pos %d, sr %d, fec %d, modulation %d, roll_off %d",
+                       frequency,
+                       polarisation ? "hor" : "vert",
+                       orbital_position,
+                       symbol_rate, fec,
+                       modulation,
+                       roll_off);
+       }
+       else
+       {
+               eDebug("SAT DVB-S freq %d, %s, pos %d, sr %d, fec %d",
+                       frequency,
+                       polarisation ? "hor" : "vert",
+                       orbital_position,
+                       symbol_rate, fec);
+       }
 }
 
 void eDVBFrontendParametersCable::set(const CableDeliverySystemDescriptor &descriptor)
@@ -104,7 +188,7 @@ void eDVBFrontendParametersTerrestrial::set(const TerrestrialDeliverySystemDescr
        if (code_rate_LP > 4)
                code_rate_LP = FEC::fAuto;
        transmission_mode = descriptor.getTransmissionMode();
-       if (transmission_mode > 2)
+       if (transmission_mode > 1) // TM4k forced to auto
                transmission_mode = TransmissionMode::TMAuto;
        guard_interval = descriptor.getGuardInterval();
        if (guard_interval > 3)
@@ -157,9 +241,10 @@ RESULT eDVBFrontendParameters::getDVBT(eDVBFrontendParametersTerrestrial &p) con
        return 0;
 }
 
-RESULT eDVBFrontendParameters::setDVBS(const eDVBFrontendParametersSatellite &p)
+RESULT eDVBFrontendParameters::setDVBS(const eDVBFrontendParametersSatellite &p, bool no_rotor_command_on_tune)
 {
        sat = p;
+       sat.no_rotor_command_on_tune = no_rotor_command_on_tune;
        m_type = iDVBFrontend::feSatellite;
        return 0;
 }
@@ -178,7 +263,7 @@ RESULT eDVBFrontendParameters::setDVBT(const eDVBFrontendParametersTerrestrial &
        return 0;
 }
 
-RESULT eDVBFrontendParameters::calculateDifference(const iDVBFrontendParameters *parm, int &diff) const
+RESULT eDVBFrontendParameters::calculateDifference(const iDVBFrontendParameters *parm, int &diff, bool exact) const
 {
        if (!parm)
                return -1;
@@ -190,7 +275,7 @@ RESULT eDVBFrontendParameters::calculateDifference(const iDVBFrontendParameters
                diff = 1<<30; // big difference
                return 0;
        }
-       
+
        switch (type)
        {
        case iDVBFrontend::feSatellite:
@@ -198,11 +283,15 @@ RESULT eDVBFrontendParameters::calculateDifference(const iDVBFrontendParameters
                eDVBFrontendParametersSatellite osat;
                if (parm->getDVBS(osat))
                        return -2;
-               
+
                if (sat.orbital_position != osat.orbital_position)
                        diff = 1<<29;
                else if (sat.polarisation != osat.polarisation)
                        diff = 1<<28;
+               else if (exact && sat.fec != osat.fec && sat.fec != eDVBFrontendParametersSatellite::FEC::fAuto && osat.fec != eDVBFrontendParametersSatellite::FEC::fAuto)
+                       diff = 1<<27;
+               else if (exact && sat.modulation != osat.modulation && sat.modulation != eDVBFrontendParametersSatellite::Modulation::Auto && osat.modulation != eDVBFrontendParametersSatellite::Modulation::Auto)
+                       diff = 1<<27;
                else
                {
                        diff = abs(sat.frequency - osat.frequency);
@@ -214,25 +303,55 @@ RESULT eDVBFrontendParameters::calculateDifference(const iDVBFrontendParameters
                eDVBFrontendParametersCable ocable;
                if (parm->getDVBC(ocable))
                        return -2;
-               
-               if (cable.modulation != ocable.modulation && cable.modulation != eDVBFrontendParametersCable::Modulation::Auto && ocable.modulation != eDVBFrontendParametersCable::Modulation::Auto)
+
+               if (exact && cable.modulation != ocable.modulation
+                       && cable.modulation != eDVBFrontendParametersCable::Modulation::Auto
+                       && ocable.modulation != eDVBFrontendParametersCable::Modulation::Auto)
                        diff = 1 << 29;
-               else if (cable.inversion != ocable.inversion && cable.inversion != eDVBFrontendParametersCable::Inversion::Unknown && ocable.inversion != eDVBFrontendParametersCable::Inversion::Unknown)
-                       diff = 1 << 28;
+               else if (exact && cable.fec_inner != ocable.fec_inner && cable.fec_inner != eDVBFrontendParametersCable::FEC::fAuto && ocable.fec_inner != eDVBFrontendParametersCable::FEC::fAuto)
+                       diff = 1 << 27;
                else
                {
                        diff = abs(cable.frequency - ocable.frequency);
                        diff += abs(cable.symbol_rate - ocable.symbol_rate);
                }
-               
                return 0;
        case iDVBFrontend::feTerrestrial:
                eDVBFrontendParametersTerrestrial oterrestrial;
                if (parm->getDVBT(oterrestrial))
                        return -2;
-               
-               diff = abs(terrestrial.frequency - oterrestrial.frequency);
 
+
+               if (exact && oterrestrial.bandwidth != terrestrial.bandwidth &&
+                       oterrestrial.bandwidth != eDVBFrontendParametersTerrestrial::Bandwidth::BwAuto &&
+                       terrestrial.bandwidth != eDVBFrontendParametersTerrestrial::Bandwidth::BwAuto)
+                       diff = 1 << 30;
+               else if (exact && oterrestrial.modulation != terrestrial.modulation &&
+                       oterrestrial.modulation != eDVBFrontendParametersTerrestrial::Modulation::Auto &&
+                       terrestrial.modulation != eDVBFrontendParametersTerrestrial::Modulation::Auto)
+                       diff = 1 << 30;
+               else if (exact && oterrestrial.transmission_mode != terrestrial.transmission_mode &&
+                       oterrestrial.transmission_mode != eDVBFrontendParametersTerrestrial::TransmissionMode::TMAuto &&
+                       terrestrial.transmission_mode != eDVBFrontendParametersTerrestrial::TransmissionMode::TMAuto)
+                       diff = 1 << 30;
+               else if (exact && oterrestrial.guard_interval != terrestrial.guard_interval &&
+                       oterrestrial.guard_interval != eDVBFrontendParametersTerrestrial::GuardInterval::GI_Auto &&
+                       terrestrial.guard_interval != eDVBFrontendParametersTerrestrial::GuardInterval::GI_Auto)
+                       diff = 1 << 30;
+               else if (exact && oterrestrial.hierarchy != terrestrial.hierarchy &&
+                       oterrestrial.hierarchy != eDVBFrontendParametersTerrestrial::Hierarchy::HAuto &&
+                       terrestrial.hierarchy != eDVBFrontendParametersTerrestrial::Hierarchy::HAuto)
+                       diff = 1 << 30;
+               else if (exact && oterrestrial.code_rate_LP != terrestrial.code_rate_LP &&
+                       oterrestrial.code_rate_LP != eDVBFrontendParametersTerrestrial::FEC::fAuto &&
+                       terrestrial.code_rate_LP != eDVBFrontendParametersTerrestrial::FEC::fAuto)
+                       diff = 1 << 30;
+               else if (exact && oterrestrial.code_rate_HP != terrestrial.code_rate_HP &&
+                       oterrestrial.code_rate_HP != eDVBFrontendParametersTerrestrial::FEC::fAuto &&
+                       terrestrial.code_rate_HP != eDVBFrontendParametersTerrestrial::FEC::fAuto)
+                       diff = 1 << 30;
+               else
+                       diff = abs(terrestrial.frequency - oterrestrial.frequency);
                return 0;
        default:
                return -1;
@@ -251,7 +370,11 @@ RESULT eDVBFrontendParameters::getHash(unsigned long &hash) const
                return 0;
        }
        case iDVBFrontend::feCable:
+               hash = 0xFFFF0000;
+               return 0;
        case iDVBFrontend::feTerrestrial:
+               hash = 0xEEEE0000;
+               return 0;
        default:
                return -1;
        }
@@ -259,8 +382,11 @@ RESULT eDVBFrontendParameters::getHash(unsigned long &hash) const
 
 DEFINE_REF(eDVBFrontend);
 
+int eDVBFrontend::PriorityOrder=0;
+
 eDVBFrontend::eDVBFrontend(int adap, int fe, int &ok)
-       :m_type(-1), m_fe(fe), m_fd(-1), m_timeout(0), m_tuneTimer(0)
+       :m_enabled(false), m_type(-1), m_dvbid(fe), m_slotid(fe)
+       ,m_fd(-1), m_need_rotor_workaround(false), m_sn(0), m_timeout(0), m_tuneTimer(0)
 #if HAVE_DVB_API_VERSION < 3
        ,m_secfd(-1)
 #endif
@@ -277,8 +403,7 @@ eDVBFrontend::eDVBFrontend(int adap, int fe, int &ok)
        m_tuneTimer = new eTimer(eApp);
        CONNECT(m_tuneTimer->timeout, eDVBFrontend::tuneLoop);
 
-       int entries = sizeof(m_data) / sizeof(int);
-       for (int i=0; i<entries; ++i)
+       for (int i=0; i<eDVBFrontend::NUM_DATA_ENTRIES; ++i)
                m_data[i] = -1;
 
        m_idleInputpower[0]=m_idleInputpower[1]=0;
@@ -289,35 +414,29 @@ eDVBFrontend::eDVBFrontend(int adap, int fe, int &ok)
 
 int eDVBFrontend::openFrontend()
 {
-       if (m_fd >= 0)
+       if (m_sn)
                return -1;  // already opened
 
        m_state=0;
        m_tuning=0;
 
 #if HAVE_DVB_API_VERSION < 3
-       m_secfd = ::open(m_sec_filename, O_RDWR);
-       if (m_secfd < 0)
-       {
-               eWarning("failed! (%s) %m", m_sec_filename);
-               return -1;
-       }
        FrontendInfo fe_info;
 #else
        dvb_frontend_info fe_info;
 #endif
-       eDebug("opening frontend %d", m_fe);
-       m_fd = ::open(m_filename, O_RDWR|O_NONBLOCK);
+       eDebug("opening frontend %d", m_dvbid);
        if (m_fd < 0)
        {
-               eWarning("failed! (%s) %m", m_filename);
-#if HAVE_DVB_API_VERSION < 3
-               ::close(m_secfd);
-               m_secfd=-1;
-#endif
-               return -1;
+               m_fd = ::open(m_filename, O_RDWR|O_NONBLOCK);
+               if (m_fd < 0)
+               {
+                       eWarning("failed! (%s) %m", m_filename);
+                       return -1;
+               }
        }
-
+       else
+               eWarning("frontend %d already opened", m_dvbid);
        if (m_type == -1)
        {
                if (::ioctl(m_fd, FE_GET_INFO, &fe_info) < 0)
@@ -325,10 +444,6 @@ int eDVBFrontend::openFrontend()
                        eWarning("ioctl FE_GET_INFO failed");
                        ::close(m_fd);
                        m_fd = -1;
-#if HAVE_DVB_API_VERSION < 3
-                       ::close(m_secfd);
-                       m_secfd=-1;
-#endif
                        return -1;
                }
 
@@ -347,52 +462,73 @@ int eDVBFrontend::openFrontend()
                        eWarning("unknown frontend type.");
                        ::close(m_fd);
                        m_fd = -1;
-#if HAVE_DVB_API_VERSION < 3
-                       ::close(m_secfd);
-                       m_secfd=-1;
-#endif
                        return -1;
                }
                eDebug("detected %s frontend", "satellite\0cable\0    terrestrial"+fe_info.type*10);
        }
 
+#if HAVE_DVB_API_VERSION < 3
+       if (m_type == iDVBFrontend::feSatellite)
+       {
+                       if (m_secfd < 0)
+                       {
+                               m_secfd = ::open(m_sec_filename, O_RDWR);
+                               if (m_secfd < 0)
+                               {
+                                       eWarning("failed! (%s) %m", m_sec_filename);
+                                       ::close(m_fd);
+                                       m_fd=-1;
+                                       return -1;
+                               }
+                       }
+                       else
+                               eWarning("sec %d already opened", m_dvbid);
+       }
+#endif
+
        setTone(iDVBFrontend::toneOff);
        setVoltage(iDVBFrontend::voltageOff);
 
        m_sn = new eSocketNotifier(eApp, m_fd, eSocketNotifier::Read);
        CONNECT(m_sn->activated, eDVBFrontend::feEvent);
-       m_sn->start();
 
        return 0;
 }
 
 int eDVBFrontend::closeFrontend()
 {
-       if (!m_fe && m_data[7] != -1)
+       eDVBRegisteredFrontend *linked_fe = (eDVBRegisteredFrontend*)m_data[LINKED_NEXT_PTR];
+       while (linked_fe != (eDVBRegisteredFrontend*)-1)
        {
-               // try to close the first frontend.. but the second is linked to the first
-               eDVBRegisteredFrontend *linked_fe = (eDVBRegisteredFrontend*)m_data[7];
                if (linked_fe->m_inuse)
                {
-                       eDebug("dont close frontend %d until the linked frontend %d is still in use",
-                               m_fe, linked_fe->m_frontend->getID());
+                       eDebug("dont close frontend %d until the linked frontend %d in slot %d is still in use",
+                               m_dvbid, linked_fe->m_frontend->getDVBID(), linked_fe->m_frontend->getSlotID());
                        return -1;
                }
+               linked_fe->m_frontend->getData(LINKED_NEXT_PTR, (int&)linked_fe);
        }
        if (m_fd >= 0)
        {
-               eDebug("close frontend %d", m_fe);
+               eDebug("close frontend %d", m_dvbid);
+               m_tuneTimer->stop();
                setTone(iDVBFrontend::toneOff);
                setVoltage(iDVBFrontend::voltageOff);
-               ::close(m_fd);
-               m_fd=-1;
-               m_data[0] = m_data[1] = m_data[2] = -1;
+               if (m_sec)
+                       m_sec->setRotorMoving(false);
+               if (!::close(m_fd))
+                       m_fd=-1;
+               else
+                       eWarning("couldnt close frontend %d", m_dvbid);
+               m_data[CSW] = m_data[UCSW] = m_data[TONEBURST] = -1;
        }
 #if HAVE_DVB_API_VERSION < 3
        if (m_secfd >= 0)
        {
-               ::close(m_secfd);
-               m_secfd=-1;
+               if (!::close(m_secfd))
+                       m_secfd=-1;
+               else
+                       eWarning("couldnt close sec %d", m_dvbid);
        }
 #endif
        delete m_sn;
@@ -420,7 +556,7 @@ void eDVBFrontend::feEvent(int w)
                int res;
                int state;
                res = ::ioctl(m_fd, FE_GET_EVENT, &event);
-               
+
                if (res && (errno == EAGAIN))
                        break;
 
@@ -429,14 +565,14 @@ void eDVBFrontend::feEvent(int w)
                        eWarning("FE_GET_EVENT failed! %m");
                        return;
                }
-               
+
                if (w < 0)
                        continue;
 
 #if HAVE_DVB_API_VERSION < 3
                if (event.type == FE_COMPLETION_EV)
 #else
-               eDebug("(%d)fe event: status %x, inversion %s", m_fe, event.status, (event.parameters.inversion == INVERSION_ON) ? "on" : "off");
+               eDebug("(%d)fe event: status %x, inversion %s", m_dvbid, event.status, (event.parameters.inversion == INVERSION_ON) ? "on" : "off");
                if (event.status & FE_HAS_LOCK)
 #endif
                {
@@ -447,8 +583,9 @@ void eDVBFrontend::feEvent(int w)
                                state = stateTuning;
                        else
                        {
+                               eDebug("stateLostLock");
                                state = stateLostLock;
-                               m_data[0] = m_data[1] = m_data[2] = -1; // reset diseqc
+                               m_data[CSW] = m_data[UCSW] = m_data[TONEBURST] = -1; // reset diseqc
                        }
                }
                if (m_state != state)
@@ -487,6 +624,56 @@ int eDVBFrontend::readFrontendData(int type)
                                eDebug("FE_READ_SNR failed (%m)");
                        return snr;
                }
+               case signalPowerdB: /* this will move into the driver */
+               {
+                       uint16_t snr=0;
+                       if (ioctl(m_fd, FE_READ_SNR, &snr) < 0 && errno != ERANGE)
+                               eDebug("FE_READ_SNR failed (%m)");
+                       if (!strcmp(m_description, "BCM4501 (internal)"))
+                       {
+                               unsigned int SDS_SNRE = snr << 16;
+
+                               static float SNR_COEFF[6] = {
+                                       100.0 / 4194304.0,
+                                       -7136.0 / 4194304.0,
+                                       197418.0 / 4194304.0,
+                                       -2602183.0 / 4194304.0,
+                                       20377212.0 / 4194304.0,
+                                       -37791203.0 / 4194304.0,
+                               };
+                       
+                               float fval1, fval2, snr_in_db;
+                               int i;
+                               fval1 = 12.44714 - (2.0 * log10(SDS_SNRE / 256.0));
+                               fval2 = pow(10.0, fval1)-1;
+                               fval1 = 10.0 * log10(fval2);
+                       
+                               if (fval1 < 10.0)
+                               {
+                                       fval2 = SNR_COEFF[0];
+                                       for (i=0; i<6; ++i)
+                                       {
+                                               fval2 *= fval1;
+                                               fval2 += SNR_COEFF[i];
+                                       }
+                                       fval1 = fval2;
+                               }
+                               snr_in_db = fval1;
+                       
+                               return (int)(snr_in_db * 100.0);
+                       }
+                       else if (!strcmp(m_description, "Alps BSBE1 702A") ||  // some frontends with STV0299
+                               !strcmp(m_description, "Alps -S") ||
+                               !strcmp(m_description, "Philips -S") ||
+                               !strcmp(m_description, "LG -S") )
+                       {
+                               float snr_in_db=(snr-39075)/1764.7;
+                               return (int)(snr_in_db * 100.0);
+                       }
+                       else
+                               eDebug("no SNR dB caluclation for frontendtype %s yet", m_description);
+                       return 0x12345678;
+               }
                case signalQuality:
                {
                        uint16_t strength=0;
@@ -494,7 +681,7 @@ int eDVBFrontend::readFrontendData(int type)
                                eDebug("FE_READ_SIGNAL_STRENGTH failed (%m)");
                        return strength;
                }
-               case Locked:
+               case locked:
                {
 #if HAVE_DVB_API_VERSION < 3
                        FrontendStatus status=0;
@@ -505,7 +692,7 @@ int eDVBFrontend::readFrontendData(int type)
                                eDebug("FE_READ_STATUS failed (%m)");
                        return !!(status&FE_HAS_LOCK);
                }
-               case Synced:
+               case synced:
                {
 #if HAVE_DVB_API_VERSION < 3
                        FrontendStatus status=0;
@@ -516,13 +703,15 @@ int eDVBFrontend::readFrontendData(int type)
                                eDebug("FE_READ_STATUS failed (%m)");
                        return !!(status&FE_HAS_SYNC);
                }
+               case frontendNumber:
+                       return m_slotid;
        }
        return 0;
 }
 
-void PutToDict(PyObject *dict, const char*key, long value)
+void PutToDict(ePyObject &dict, const char*key, long value)
 {
-       PyObject *item = PyInt_FromLong(value);
+       ePyObject item = PyInt_FromLong(value);
        if (item)
        {
                if (PyDict_SetItemString(dict, key, item))
@@ -533,9 +722,21 @@ void PutToDict(PyObject *dict, const char*key, long value)
                eDebug("could not create PyObject for %s", key);
 }
 
-void PutToDict(PyObject *dict, const char*key, const char *value)
+void PutToDict(ePyObject &dict, const char*key, ePyObject item)
+{
+       if (item)
+       {
+               if (PyDict_SetItemString(dict, key, item))
+                       eDebug("put %s to dict failed", key);
+               Py_DECREF(item);
+       }
+       else
+               eDebug("invalid PyObject for %s", key);
+}
+
+void PutToDict(ePyObject &dict, const char*key, const char *value)
 {
-       PyObject *item = PyString_FromString(value);
+       ePyObject item = PyString_FromString(value);
        if (item)
        {
                if (PyDict_SetItemString(dict, key, item))
@@ -546,43 +747,91 @@ void PutToDict(PyObject *dict, const char*key, const char *value)
                eDebug("could not create PyObject for %s", key);
 }
 
-void fillDictWithSatelliteData(PyObject *dict, const FRONTENDPARAMETERS &parm, eDVBFrontend *fe)
+void fillDictWithSatelliteData(ePyObject dict, const FRONTENDPARAMETERS &parm, eDVBFrontend *fe)
 {
        int freq_offset=0;
        int csw=0;
-       const char *fec=0;
-       fe->getData(0, csw);
-       fe->getData(9, freq_offset);
+       const char *tmp=0;
+       fe->getData(eDVBFrontend::CSW, csw);
+       fe->getData(eDVBFrontend::FREQ_OFFSET, freq_offset);
        int frequency = parm_frequency + freq_offset;
        PutToDict(dict, "frequency", frequency);
        PutToDict(dict, "symbol_rate", parm_u_qpsk_symbol_rate);
-
        switch(parm_u_qpsk_fec_inner)
        {
        case FEC_1_2:
-               fec = "FEC_1_2";
+               tmp = "FEC_1_2";
                break;
        case FEC_2_3:
-               fec = "FEC_2_3";
+               tmp = "FEC_2_3";
                break;
        case FEC_3_4:
-               fec = "FEC_3_4";
+               tmp = "FEC_3_4";
                break;
        case FEC_5_6:
-               fec = "FEC_5_6";
+               tmp = "FEC_5_6";
                break;
        case FEC_7_8:
-               fec = "FEC_7_8";
+               tmp = "FEC_7_8";
                break;
+       case FEC_NONE:
+               tmp = "FEC_NONE";
        default:
        case FEC_AUTO:
-               fec = "FEC_AUTO";
+               tmp = "FEC_AUTO";
+               break;
+#if HAVE_DVB_API_VERSION >=3
+       case FEC_S2_8PSK_1_2:
+       case FEC_S2_QPSK_1_2:
+               tmp = "FEC_1_2";
+               break;
+       case FEC_S2_8PSK_2_3:
+       case FEC_S2_QPSK_2_3:
+               tmp = "FEC_2_3";
+               break;
+       case FEC_S2_8PSK_3_4:
+       case FEC_S2_QPSK_3_4:
+               tmp = "FEC_3_4";
+               break;
+       case FEC_S2_8PSK_5_6:
+       case FEC_S2_QPSK_5_6:
+               tmp = "FEC_5_6";
+               break;
+       case FEC_S2_8PSK_7_8:
+       case FEC_S2_QPSK_7_8:
+               tmp = "FEC_7_8";
+               break;
+       case FEC_S2_8PSK_8_9:
+       case FEC_S2_QPSK_8_9:
+               tmp = "FEC_8_9";
+               break;
+       case FEC_S2_8PSK_3_5:
+       case FEC_S2_QPSK_3_5:
+               tmp = "FEC_3_5";
+               break;
+       case FEC_S2_8PSK_4_5:
+       case FEC_S2_QPSK_4_5:
+               tmp = "FEC_4_5";
                break;
+       case FEC_S2_8PSK_9_10:
+       case FEC_S2_QPSK_9_10:
+               tmp = "FEC_9_10";
+               break;
+#endif
        }
-       PutToDict(dict, "fec_inner", fec);
+#if HAVE_DVB_API_VERSION >=3
+       PutToDict(dict, "modulation",
+               parm_u_qpsk_fec_inner > FEC_S2_QPSK_9_10 ? "8PSK": "QPSK" );
+#else
+       PutToDict(dict, "modulation", "QPSK" );
+#endif
+       PutToDict(dict, "fec_inner", tmp);
+       tmp = parm_u_qpsk_fec_inner > FEC_AUTO ?
+               "DVB-S2" : "DVB-S";
+       PutToDict(dict, "system", tmp);
 }
 
-void fillDictWithCableData(PyObject *dict, const FRONTENDPARAMETERS &parm)
+void fillDictWithCableData(ePyObject dict, const FRONTENDPARAMETERS &parm)
 {
        const char *tmp=0;
        PutToDict(dict, "frequency", parm_frequency/1000);
@@ -643,7 +892,7 @@ void fillDictWithCableData(PyObject *dict, const FRONTENDPARAMETERS &parm)
        PutToDict(dict, "modulation", tmp);
 }
 
-void fillDictWithTerrestrialData(PyObject *dict, const FRONTENDPARAMETERS &parm)
+void fillDictWithTerrestrialData(ePyObject dict, const FRONTENDPARAMETERS &parm)
 {
        const char *tmp=0;
        PutToDict(dict, "frequency", parm_frequency);
@@ -718,6 +967,9 @@ void fillDictWithTerrestrialData(PyObject *dict, const FRONTENDPARAMETERS &parm)
        case QAM_16:
                tmp = "QAM_16";
                break;
+       case QAM_64:
+               tmp = "QAM_64";
+               break;
        default:
        case QAM_AUTO:
                tmp = "QAM_AUTO";
@@ -760,6 +1012,9 @@ void fillDictWithTerrestrialData(PyObject *dict, const FRONTENDPARAMETERS &parm)
        PutToDict(dict, "guard_interval", tmp);
        switch (parm_u_ofdm_hierarchy_information)
        {
+               case HIERARCHY_NONE:
+                       tmp = "HIERARCHY_NONE";
+                       break;
                case HIERARCHY_1:
                        tmp = "HIERARCHY_1";
                        break;
@@ -777,17 +1032,106 @@ void fillDictWithTerrestrialData(PyObject *dict, const FRONTENDPARAMETERS &parm)
        PutToDict(dict, "hierarchy_information", tmp);
 }
 
-PyObject *eDVBFrontend::readTransponderData(bool original)
+void eDVBFrontend::getFrontendStatus(ePyObject dest)
 {
-       PyObject *ret=PyDict_New();
+       if (dest && PyDict_Check(dest))
+       {
+               const char *tmp = "UNKNOWN";
+               switch(m_state)
+               {
+                       case stateIdle:
+                               tmp="IDLE";
+                               break;
+                       case stateTuning:
+                               tmp="TUNING";
+                               break;
+                       case stateFailed:
+                               tmp="FAILED";
+                               break;
+                       case stateLock:
+                               tmp="LOCKED";
+                               break;
+                       case stateLostLock:
+                               tmp="LOSTLOCK";
+                               break;
+                       default:
+                               break;
+               }
+               PutToDict(dest, "tuner_state", tmp);
+               PutToDict(dest, "tuner_locked", readFrontendData(locked));
+               PutToDict(dest, "tuner_synced", readFrontendData(synced));
+               PutToDict(dest, "tuner_bit_error_rate", readFrontendData(bitErrorRate));
+               PutToDict(dest, "tuner_signal_power", readFrontendData(signalPower));
+               int sigPowerdB = readFrontendData(signalPowerdB);
+               if (sigPowerdB == 0x12345678) // not support yet
+               {
+                       ePyObject obj=Py_None;
+                       Py_INCREF(obj);
+                       PutToDict(dest, "tuner_signal_power_db", obj);
+               }
+               else
+                       PutToDict(dest, "tuner_signal_power_db", sigPowerdB);
+               PutToDict(dest, "tuner_signal_quality", readFrontendData(signalQuality));
+       }
+}
 
-       if (ret)
+void eDVBFrontend::getTransponderData(ePyObject dest, bool original)
+{
+       if (m_fd != -1 && dest && PyDict_Check(dest))
        {
-               bool read=m_fd != -1;
-               const char *tmp=0;
+               switch(m_type)
+               {
+                       case feSatellite:
+                       case feCable:
+                       case feTerrestrial:
+                       {
+                               FRONTENDPARAMETERS front;
+                               if (!original && ioctl(m_fd, FE_GET_FRONTEND, &front)<0)
+                                       eDebug("FE_GET_FRONTEND (%m)");
+                               else
+                               {
+                                       const FRONTENDPARAMETERS &parm = original ? this->parm : front;
+                                       const char *tmp = "INVERSION_AUTO";
+                                       switch(parm_inversion)
+                                       {
+                                               case INVERSION_ON:
+                                                       tmp = "INVERSION_ON";
+                                                       break;
+                                               case INVERSION_OFF:
+                                                       tmp = "INVERSION_OFF";
+                                                       break;
+                                               default:
+                                                       break;
+                                       }
+                                       if (tmp)
+                                               PutToDict(dest, "inversion", tmp);
 
-               PutToDict(ret, "tuner_number", m_fe);
+                                       switch(m_type)
+                                       {
+                                               case feSatellite:
+                                                       fillDictWithSatelliteData(dest, original?parm:front, this);
+                                                       break;
+                                               case feCable:
+                                                       fillDictWithCableData(dest, original?parm:front);
+                                                       break;
+                                               case feTerrestrial:
+                                                       fillDictWithTerrestrialData(dest, original?parm:front);
+                                                       break;
+                                       }
+                               }
+                       }
+                       default:
+                               break;
+               }
+       }
+}
 
+void eDVBFrontend::getFrontendData(ePyObject dest)
+{
+       if (dest && PyDict_Check(dest))
+       {
+               const char *tmp=0;
+               PutToDict(dest, "tuner_number", m_dvbid);
                switch(m_type)
                {
                        case feSatellite:
@@ -801,84 +1145,10 @@ PyObject *eDVBFrontend::readTransponderData(bool original)
                                break;
                        default:
                                tmp = "UNKNOWN";
-                               read=false;
                                break;
                }
-               PutToDict(ret, "tuner_type", tmp);
-
-               if (read)
-               {
-                       FRONTENDPARAMETERS front;
-
-                       tmp = "UNKNOWN";
-                       switch(m_state)
-                       {
-                               case stateIdle:
-                                       tmp="IDLE";
-                                       break;
-                               case stateTuning:
-                                       tmp="TUNING";
-                                       break;
-                               case stateFailed:
-                                       tmp="FAILED";
-                                       break;
-                               case stateLock:
-                                       tmp="LOCKED";
-                                       break;
-                               case stateLostLock:
-                                       tmp="LOSTLOCK";
-                                       break;
-                               default:
-                                       break;
-                       }
-                       PutToDict(ret, "tuner_state", tmp);
-
-                       PutToDict(ret, "tuner_locked", readFrontendData(Locked));
-                       PutToDict(ret, "tuner_synced", readFrontendData(Synced));
-                       PutToDict(ret, "tuner_bit_error_rate", readFrontendData(bitErrorRate));
-                       PutToDict(ret, "tuner_signal_power", readFrontendData(signalPower));
-                       PutToDict(ret, "tuner_signal_quality", readFrontendData(signalQuality));
-
-                       if (!original && ioctl(m_fd, FE_GET_FRONTEND, &front)<0)
-                               eDebug("FE_GET_FRONTEND (%m)");
-                       else
-                       {
-                               tmp = "INVERSION_AUTO";
-                               switch(parm_inversion)
-                               {
-                                       case INVERSION_ON:
-                                               tmp = "INVERSION_ON";
-                                               break;
-                                       case INVERSION_OFF:
-                                               tmp = "INVERSION_OFF";
-                                               break;
-                                       default:
-                                               break;
-                               }
-                               if (tmp)
-                                       PutToDict(ret, "inversion", tmp);
-
-                               switch(m_type)
-                               {
-                                       case feSatellite:
-                                               fillDictWithSatelliteData(ret, original?parm:front, this);
-                                               break;
-                                       case feCable:
-                                               fillDictWithCableData(ret, original?parm:front);
-                                               break;
-                                       case feTerrestrial:
-                                               fillDictWithTerrestrialData(ret, original?parm:front);
-                                               break;
-                               }
-                       }
-               }
-       }
-       else
-       {
-               Py_INCREF(Py_None);
-               ret = Py_None;
+               PutToDict(dest, "tuner_type", tmp);
        }
-       return ret;
 }
 
 #ifndef FP_IOCTL_GET_ID
@@ -886,7 +1156,7 @@ PyObject *eDVBFrontend::readTransponderData(bool original)
 #endif
 int eDVBFrontend::readInputpower()
 {
-       int power=m_fe;  // this is needed for read inputpower from the correct tuner !
+       int power=m_slotid;  // this is needed for read inputpower from the correct tuner !
 
        // open front prozessor
        int fp=::open("/dev/dbox/fp0", O_RDWR);
@@ -926,6 +1196,35 @@ bool eDVBFrontend::setSecSequencePos(int steps)
        return true;
 }
 
+void eDVBFrontend::setRotorData(int pos, int cmd)
+{
+       m_data[ROTOR_CMD] = cmd;
+       m_data[ROTOR_POS] = pos;
+       if ( m_data[SATPOS_DEPENDS_PTR] != -1 )
+       {
+               eDVBRegisteredFrontend *satpos_depends_to_fe = (eDVBRegisteredFrontend*) m_data[SATPOS_DEPENDS_PTR];
+               satpos_depends_to_fe->m_frontend->m_data[ROTOR_CMD] = cmd;
+               satpos_depends_to_fe->m_frontend->m_data[ROTOR_POS] = pos;
+       }
+       else
+       {
+               eDVBRegisteredFrontend *next = (eDVBRegisteredFrontend *)m_data[LINKED_NEXT_PTR];
+               while ( (int)next != -1 )
+               {
+                       next->m_frontend->m_data[ROTOR_CMD] = cmd;
+                       next->m_frontend->m_data[ROTOR_POS] = pos;
+                       next = (eDVBRegisteredFrontend *)next->m_frontend->m_data[LINKED_NEXT_PTR];
+               }
+               eDVBRegisteredFrontend *prev = (eDVBRegisteredFrontend *)m_data[LINKED_PREV_PTR];
+               while ( (int)prev != -1 )
+               {
+                       prev->m_frontend->m_data[ROTOR_CMD] = cmd;
+                       prev->m_frontend->m_data[ROTOR_POS] = pos;
+                       prev = (eDVBRegisteredFrontend *)prev->m_frontend->m_data[LINKED_PREV_PTR];
+               }
+       }
+}
+
 void eDVBFrontend::tuneLoop()  // called by m_tuneTimer
 {
        int delay=0;
@@ -952,7 +1251,7 @@ void eDVBFrontend::tuneLoop()  // called by m_tuneTimer
                        case eSecCommand::IF_VOLTAGE_GOTO:
                        {
                                eSecCommand::pair &compare = m_sec_sequence.current()->compare;
-                               if ( compare.voltage == m_curVoltage && setSecSequencePos(compare.steps) )
+                               if ( compare.voltage == m_data[CUR_VOLTAGE] && setSecSequencePos(compare.steps) )
                                        break;
                                ++m_sec_sequence.current();
                                break;
@@ -960,7 +1259,23 @@ void eDVBFrontend::tuneLoop()  // called by m_tuneTimer
                        case eSecCommand::IF_NOT_VOLTAGE_GOTO:
                        {
                                eSecCommand::pair &compare = m_sec_sequence.current()->compare;
-                               if ( compare.voltage != m_curVoltage && setSecSequencePos(compare.steps) )
+                               if ( compare.voltage != m_data[CUR_VOLTAGE] && setSecSequencePos(compare.steps) )
+                                       break;
+                               ++m_sec_sequence.current();
+                               break;
+                       }
+                       case eSecCommand::IF_TONE_GOTO:
+                       {
+                               eSecCommand::pair &compare = m_sec_sequence.current()->compare;
+                               if ( compare.tone == m_data[CUR_TONE] && setSecSequencePos(compare.steps) )
+                                       break;
+                               ++m_sec_sequence.current();
+                               break;
+                       }
+                       case eSecCommand::IF_NOT_TONE_GOTO:
+                       {
+                               eSecCommand::pair &compare = m_sec_sequence.current()->compare;
+                               if ( compare.tone != m_data[CUR_TONE] && setSecSequencePos(compare.steps) )
                                        break;
                                ++m_sec_sequence.current();
                                break;
@@ -1019,7 +1334,7 @@ void eDVBFrontend::tuneLoop()  // called by m_tuneTimer
                        case eSecCommand::IF_MEASURE_IDLE_WAS_NOT_OK_GOTO:
                        {
                                eSecCommand::pair &compare = m_sec_sequence.current()->compare;
-                               int idx = compare.voltage;
+                               int idx = compare.val;
                                if ( idx == 0 || idx == 1 )
                                {
                                        int idle = readInputpower();
@@ -1037,7 +1352,7 @@ void eDVBFrontend::tuneLoop()  // called by m_tuneTimer
                        case eSecCommand::IF_TUNER_LOCKED_GOTO:
                        {
                                eSecCommand::rotor &cmd = m_sec_sequence.current()->measure;
-                               if (readFrontendData(Locked))
+                               if (readFrontendData(locked))
                                {
                                        eDebug("[SEC] locked step %d ok", cmd.okcount);
                                        ++cmd.okcount;
@@ -1066,7 +1381,7 @@ void eDVBFrontend::tuneLoop()  // called by m_tuneTimer
                                break;
                        case eSecCommand::IF_INPUTPOWER_DELTA_GOTO:
                        {
-                               int idleInputpower = m_idleInputpower[ (m_curVoltage&1) ? 0 : 1];
+                               int idleInputpower = m_idleInputpower[ (m_data[CUR_VOLTAGE]&1) ? 0 : 1];
                                eSecCommand::rotor &cmd = m_sec_sequence.current()->measure;
                                const char *txt = cmd.direction ? "running" : "stopped";
                                eDebug("[SEC] waiting for rotor %s %d, idle %d, delta %d",
@@ -1099,20 +1414,19 @@ void eDVBFrontend::tuneLoop()  // called by m_tuneTimer
                                break;
                        }
                        case eSecCommand::IF_ROTORPOS_VALID_GOTO:
-                               if (m_data[5] != -1 && m_data[6] != -1)
+                               if (m_data[ROTOR_CMD] != -1 && m_data[ROTOR_POS] != -1)
                                        setSecSequencePos(m_sec_sequence.current()->steps);
                                else
                                        ++m_sec_sequence.current();
                                break;
                        case eSecCommand::INVALIDATE_CURRENT_ROTORPARMS:
-                               m_data[5] = m_data[6] = -1;
                                eDebug("[SEC] invalidate current rotorparams");
+                               setRotorData(-1,-1);    
                                ++m_sec_sequence.current();
                                break;
                        case eSecCommand::UPDATE_CURRENT_ROTORPARAMS:
-                               m_data[5] = m_data[3];
-                               m_data[6] = m_data[4];
-                               eDebug("[SEC] update current rotorparams %d %04x %d", m_timeoutCount, m_data[5], m_data[6]);
+                               setRotorData(m_data[NEW_ROTOR_POS], m_data[NEW_ROTOR_CMD]);
+                               eDebug("[SEC] update current rotorparams %d %04x %d", m_timeoutCount, m_data[ROTOR_CMD], m_data[ROTOR_POS]);
                                ++m_sec_sequence.current();
                                break;
                        case eSecCommand::SET_ROTOR_DISEQC_RETRYS:
@@ -1130,9 +1444,19 @@ void eDVBFrontend::tuneLoop()  // called by m_tuneTimer
                                break;
                        case eSecCommand::SET_POWER_LIMITING_MODE:
                        {
-                               int fd = m_fe ?
-                                       ::open("/dev/i2c/1", O_RDWR) :
-                                       ::open("/dev/i2c/0", O_RDWR);
+                               if (!m_need_rotor_workaround)
+                                       break;
+
+                               char dev[16];
+
+                               // FIXMEEEEEE hardcoded i2c devices for dm7025 and dm8000
+                               if (m_slotid < 2)
+                                       sprintf(dev, "/dev/i2c/%d", m_slotid);
+                               else if (m_slotid == 2)
+                                       sprintf(dev, "/dev/i2c/2"); // first nim socket on DM8000 use /dev/i2c/2
+                               else if (m_slotid == 3)
+                                       sprintf(dev, "/dev/i2c/4"); // second nim socket on DM8000 use /dev/i2c/4
+                               int fd = ::open(dev, O_RDWR);
 
                                unsigned char data[2];
                                ::ioctl(fd, I2C_SLAVE_FORCE, 0x10 >> 1);
@@ -1155,8 +1479,9 @@ void eDVBFrontend::tuneLoop()  // called by m_tuneTimer
                                break;
                        }
                        default:
+                               eDebug("[SEC] unhandled sec command %d",
+                                       ++m_sec_sequence.current()->cmd);
                                ++m_sec_sequence.current();
-                               eDebug("[SEC] unhandled sec command");
                }
                m_tuneTimer->start(delay,true);
        }
@@ -1164,7 +1489,9 @@ void eDVBFrontend::tuneLoop()  // called by m_tuneTimer
 
 void eDVBFrontend::setFrontend()
 {
-       eDebug("setting frontend %d", m_fe);
+       eDebug("setting frontend %d", m_dvbid);
+       m_sn->start();
+       feEvent(-1);
        if (ioctl(m_fd, FE_SET_FRONTEND, &parm) == -1)
        {
                perror("FE_SET_FRONTEND failed");
@@ -1188,9 +1515,17 @@ RESULT eDVBFrontend::prepare_sat(const eDVBFrontendParametersSatellite &feparm)
                eWarning("no SEC module active!");
                return -ENOENT;
        }
-       res = m_sec->prepare(*this, parm, feparm, 1 << m_fe);
+       res = m_sec->prepare(*this, parm, feparm, 1 << m_slotid);
        if (!res)
        {
+               eDebug("prepare_sat System %d Freq %d Pol %d SR %d INV %d FEC %d orbpos %d",
+                       feparm.system,
+                       feparm.frequency,
+                       feparm.polarisation,
+                       feparm.symbol_rate,
+                       feparm.inversion,
+                       feparm.fec,
+                       feparm.orbital_position);
                parm_u_qpsk_symbol_rate = feparm.symbol_rate;
                switch (feparm.inversion)
                {
@@ -1205,29 +1540,79 @@ RESULT eDVBFrontend::prepare_sat(const eDVBFrontendParametersSatellite &feparm)
                                parm_inversion = INVERSION_AUTO;
                                break;
                }
-               switch (feparm.fec)
+               if (feparm.system == eDVBFrontendParametersSatellite::System::DVB_S)
+                       switch (feparm.fec)
+                       {
+                               case eDVBFrontendParametersSatellite::FEC::fNone:
+                                       parm_u_qpsk_fec_inner = FEC_NONE;
+                                       break;
+                               case eDVBFrontendParametersSatellite::FEC::f1_2:
+                                       parm_u_qpsk_fec_inner = FEC_1_2;
+                                       break;
+                               case eDVBFrontendParametersSatellite::FEC::f2_3:
+                                       parm_u_qpsk_fec_inner = FEC_2_3;
+                                       break;
+                               case eDVBFrontendParametersSatellite::FEC::f3_4:
+                                       parm_u_qpsk_fec_inner = FEC_3_4;
+                                       break;
+                               case eDVBFrontendParametersSatellite::FEC::f5_6:
+                                       parm_u_qpsk_fec_inner = FEC_5_6;
+                                       break;
+                               case eDVBFrontendParametersSatellite::FEC::f7_8:
+                                       parm_u_qpsk_fec_inner = FEC_7_8;
+                                       break;
+                               default:
+                                       eDebug("no valid fec for DVB-S set.. assume auto");
+                               case eDVBFrontendParametersSatellite::FEC::fAuto:
+                                       parm_u_qpsk_fec_inner = FEC_AUTO;
+                                       break;
+                       }
+#if HAVE_DVB_API_VERSION >= 3
+               else // DVB_S2
                {
-                       default:
-                       case eDVBFrontendParametersSatellite::FEC::fNone:
-                               eDebug("no fec set.. assume auto");
-                       case eDVBFrontendParametersSatellite::FEC::fAuto:
-                               parm_u_qpsk_fec_inner = FEC_AUTO;
-                               break;
-                       case eDVBFrontendParametersSatellite::FEC::f1_2:
-                               parm_u_qpsk_fec_inner = FEC_1_2;
-                               break;
-                       case eDVBFrontendParametersSatellite::FEC::f2_3:
-                               parm_u_qpsk_fec_inner = FEC_2_3;
-                               break;
-                       case eDVBFrontendParametersSatellite::FEC::f3_4:
-                               parm_u_qpsk_fec_inner = FEC_3_4;
-                               break;
-                       case eDVBFrontendParametersSatellite::FEC::f5_6:
-                               parm_u_qpsk_fec_inner = FEC_5_6;
-                               break;
-                       case eDVBFrontendParametersSatellite::FEC::f7_8:
-                               parm_u_qpsk_fec_inner = FEC_7_8;
-                               break;
+                       switch (feparm.fec)
+                       {
+                               case eDVBFrontendParametersSatellite::FEC::f1_2:
+                                       parm_u_qpsk_fec_inner = FEC_S2_QPSK_1_2;
+                                       break;
+                               case eDVBFrontendParametersSatellite::FEC::f2_3:
+                                       parm_u_qpsk_fec_inner = FEC_S2_QPSK_2_3;
+                                       break;
+                               case eDVBFrontendParametersSatellite::FEC::f3_4:
+                                       parm_u_qpsk_fec_inner = FEC_S2_QPSK_3_4;
+                                       break;
+                               case eDVBFrontendParametersSatellite::FEC::f3_5:
+                                       parm_u_qpsk_fec_inner = FEC_S2_QPSK_3_5;
+                                       break;
+                               case eDVBFrontendParametersSatellite::FEC::f4_5:
+                                       parm_u_qpsk_fec_inner = FEC_S2_QPSK_4_5;
+                                       break;
+                               case eDVBFrontendParametersSatellite::FEC::f5_6:
+                                       parm_u_qpsk_fec_inner = FEC_S2_QPSK_5_6;
+                                       break;
+                               case eDVBFrontendParametersSatellite::FEC::f7_8:
+                                       parm_u_qpsk_fec_inner = FEC_S2_QPSK_7_8;
+                                       break;
+                               case eDVBFrontendParametersSatellite::FEC::f8_9:
+                                       parm_u_qpsk_fec_inner = FEC_S2_QPSK_8_9;
+                                       break;
+                               case eDVBFrontendParametersSatellite::FEC::f9_10:
+                                       parm_u_qpsk_fec_inner = FEC_S2_QPSK_9_10;
+                                       break;
+                               default:
+                                       eDebug("no valid fec for DVB-S2 set.. abort !!");
+                                       return -EINVAL;
+                       }
+                       if (feparm.modulation == eDVBFrontendParametersSatellite::Modulation::M8PSK)
+                               parm_u_qpsk_fec_inner = (fe_code_rate_t)((int)parm_u_qpsk_fec_inner+9);
+                               // 8PSK fec driver values are decimal 9 bigger
+               }
+#endif
+               // FIXME !!! get frequency range from tuner
+               if ( parm_frequency < 900000 || parm_frequency > 2200000 )
+               {
+                       eDebug("%d mhz out of tuner range.. dont tune", parm_frequency/1000);
+                       return -EINVAL;
                }
                eDebug("tuning to %d mhz", parm_frequency/1000);
        }
@@ -1303,6 +1688,12 @@ RESULT eDVBFrontend::prepare_cable(const eDVBFrontendParametersCable &feparm)
                parm_u_qam_fec_inner = FEC_AUTO;
                break;
        }
+       eDebug("tuning to %d khz, sr %d, fec %d, modulation %d, inversion %d",
+               parm_frequency/1000,
+               parm_u_qam_symbol_rate,
+               parm_u_qam_fec_inner,
+               parm_u_qam_modulation,
+               parm_inversion);
        return 0;
 }
 
@@ -1378,6 +1769,9 @@ RESULT eDVBFrontend::prepare_terrestrial(const eDVBFrontendParametersTerrestrial
        case eDVBFrontendParametersTerrestrial::Modulation::QAM16:
                parm_u_ofdm_constellation = QAM_16;
                break;
+       case eDVBFrontendParametersTerrestrial::Modulation::QAM64:
+               parm_u_ofdm_constellation = QAM_64;
+               break;
        default:
        case eDVBFrontendParametersTerrestrial::Modulation::Auto:
                parm_u_ofdm_constellation = QAM_AUTO;
@@ -1417,6 +1811,9 @@ RESULT eDVBFrontend::prepare_terrestrial(const eDVBFrontendParametersTerrestrial
        }
        switch (feparm.hierarchy)
        {
+               case eDVBFrontendParametersTerrestrial::Hierarchy::HNone:
+                       parm_u_ofdm_hierarchy_information = HIERARCHY_NONE;
+                       break;
                case eDVBFrontendParametersTerrestrial::Hierarchy::H1:
                        parm_u_ofdm_hierarchy_information = HIERARCHY_1;
                        break;
@@ -1431,20 +1828,44 @@ RESULT eDVBFrontend::prepare_terrestrial(const eDVBFrontendParametersTerrestrial
                        parm_u_ofdm_hierarchy_information = HIERARCHY_AUTO;
                        break;
        }
+       switch (feparm.inversion)
+       {
+       case eDVBFrontendParametersTerrestrial::Inversion::On:
+               parm_inversion = INVERSION_ON;
+               break;
+       case eDVBFrontendParametersTerrestrial::Inversion::Off:
+               parm_inversion = INVERSION_OFF;
+               break;
+       default:
+       case eDVBFrontendParametersTerrestrial::Inversion::Unknown:
+               parm_inversion = INVERSION_AUTO;
+               break;
+       }
        return 0;
 }
 
 RESULT eDVBFrontend::tune(const iDVBFrontendParameters &where)
 {
-       eDebug("(%d)tune", m_fe);
+       eDebug("(%d)tune", m_dvbid);
+
+       m_timeout->stop();
 
        int res=0;
 
-       if (m_type == -1)
-               return -ENODEV;
+       if (!m_sn)
+       {
+               eDebug("no frontend device opened... do not try to tune !!!");
+               res = -ENODEV;
+               goto tune_error;
+       }
 
-       feEvent(-1);
+       if (m_type == -1)
+       {
+               res = -ENODEV;
+               goto tune_error;
+       }
 
+       m_sn->stop();
        m_sec_sequence.clear();
 
        switch (m_type)
@@ -1455,19 +1876,30 @@ RESULT eDVBFrontend::tune(const iDVBFrontendParameters &where)
                if (where.getDVBS(feparm))
                {
                        eDebug("no dvbs data!");
-                       return -EINVAL;
+                       res = -EINVAL;
+                       goto tune_error;
                }
+               m_sec->setRotorMoving(false);
                res=prepare_sat(feparm);
+               if (res)
+                       goto tune_error;
+
                break;
        }
        case feCable:
        {
                eDVBFrontendParametersCable feparm;
                if (where.getDVBC(feparm))
-                       return -EINVAL;
+               {
+                       res = -EINVAL;
+                       goto tune_error;
+               }
                res=prepare_cable(feparm);
-               if (!res)
-                       m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
+               if (res)
+                       goto tune_error;
+
+               m_sec_sequence.push_back( eSecCommand(eSecCommand::START_TUNE_TIMEOUT) );
+               m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
                break;
        }
        case feTerrestrial:
@@ -1476,30 +1908,43 @@ RESULT eDVBFrontend::tune(const iDVBFrontendParameters &where)
                if (where.getDVBT(feparm))
                {
                        eDebug("no -T data");
-                       return -EINVAL;
+                       res = -EINVAL;
+                       goto tune_error;
                }
                res=prepare_terrestrial(feparm);
-               if (!res)
-                       m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
+               if (res)
+                       goto tune_error;
+
+               std::string enable_5V;
+               char configStr[255];
+               snprintf(configStr, 255, "config.Nims.%d.terrestrial_5V", m_slotid);
+               m_sec_sequence.push_back( eSecCommand(eSecCommand::START_TUNE_TIMEOUT) );
+               ePythonConfigQuery::getConfigValue(configStr, enable_5V);
+               if (enable_5V == "True")
+                       m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, iDVBFrontend::voltage13) );
+               else
+                       m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, iDVBFrontend::voltageOff) );
+               m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
+
                break;
        }
        }
 
-       if (!res)  // prepare ok
-       {
-               m_tuneTimer->start(0,true);
-               m_timeout->stop();
-               m_sec_sequence.current() = m_sec_sequence.begin();
+       m_tuneTimer->start(0,true);
+       m_sec_sequence.current() = m_sec_sequence.begin();
 
-               if (m_state != stateTuning)
-               {
-                       m_tuning = 1;
-                       m_state = stateTuning;
-                       m_stateChanged(this);
-               }
+       if (m_state != stateTuning)
+       {
+               m_tuning = 1;
+               m_state = stateTuning;
+               m_stateChanged(this);
        }
 
        return res;
+
+tune_error:
+       m_tuneTimer->stop();
+       return res;
 }
 
 RESULT eDVBFrontend::connectStateChange(const Slot1<void,iDVBFrontend*> &stateChange, ePtr<eConnection> &connection)
@@ -1510,7 +1955,7 @@ RESULT eDVBFrontend::connectStateChange(const Slot1<void,iDVBFrontend*> &stateCh
 
 RESULT eDVBFrontend::setVoltage(int voltage)
 {
-       if (m_type != feSatellite)
+       if (m_type == feCable)
                return -1;
 #if HAVE_DVB_API_VERSION < 3
        secVoltage vlt;
@@ -1518,7 +1963,7 @@ RESULT eDVBFrontend::setVoltage(int voltage)
        bool increased=false;
        fe_sec_voltage_t vlt;
 #endif
-       m_curVoltage=voltage;
+       m_data[CUR_VOLTAGE]=voltage;
        switch (voltage)
        {
        case voltageOff:
@@ -1552,7 +1997,7 @@ RESULT eDVBFrontend::setVoltage(int voltage)
 #if HAVE_DVB_API_VERSION < 3
        return ::ioctl(m_secfd, SEC_SET_VOLTAGE, vlt);
 #else
-       if (::ioctl(m_fd, FE_ENABLE_HIGH_LNB_VOLTAGE, increased) < 0)
+       if (m_type == feSatellite && ::ioctl(m_fd, FE_ENABLE_HIGH_LNB_VOLTAGE, increased) < 0)
                perror("FE_ENABLE_HIGH_LNB_VOLTAGE");
        return ::ioctl(m_fd, FE_SET_VOLTAGE, vlt);
 #endif
@@ -1573,7 +2018,7 @@ RESULT eDVBFrontend::setTone(int t)
 #else
        fe_sec_tone_mode_t tone;
 #endif
-
+       m_data[CUR_TONE]=t;
        switch (t)
        {
        case toneOn:
@@ -1655,7 +2100,7 @@ RESULT eDVBFrontend::setSecSequence(const eSecCommandList &list)
 
 RESULT eDVBFrontend::getData(int num, int &data)
 {
-       if ( num < (int)(sizeof(m_data)/sizeof(int)) )
+       if ( num < NUM_DATA_ENTRIES )
        {
                data = m_data[num];
                return 0;
@@ -1665,7 +2110,7 @@ RESULT eDVBFrontend::getData(int num, int &data)
 
 RESULT eDVBFrontend::setData(int num, int val)
 {
-       if ( num < (int)(sizeof(m_data)/sizeof(int)) )
+       if ( num < NUM_DATA_ENTRIES )
        {
                m_data[num] = val;
                return 0;
@@ -1676,15 +2121,44 @@ RESULT eDVBFrontend::setData(int num, int val)
 int eDVBFrontend::isCompatibleWith(ePtr<iDVBFrontendParameters> &feparm)
 {
        int type;
-       if (feparm->getSystem(type) || type != m_type)
+       if (feparm->getSystem(type) || type != m_type || !m_enabled)
                return 0;
 
        if (m_type == eDVBFrontend::feSatellite)
        {
                ASSERT(m_sec);
                eDVBFrontendParametersSatellite sat_parm;
-               ASSERT(!feparm->getDVBS(sat_parm));
-               return m_sec->canTune(sat_parm, this, 1 << m_fe);
+               int ret = feparm->getDVBS(sat_parm);
+               ASSERT(!ret);
+               return m_sec->canTune(sat_parm, this, 1 << m_slotid);
        }
+       else if (m_type == eDVBFrontend::feCable)
+               return 2;  // more prio for cable frontends
        return 1;
 }
+
+bool eDVBFrontend::setSlotInfo(ePyObject obj)
+{
+       ePyObject Id, Descr, Enabled;
+       if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 3)
+               goto arg_error;
+       Id = PyTuple_GET_ITEM(obj, 0);
+       Descr = PyTuple_GET_ITEM(obj, 1);
+       Enabled = PyTuple_GET_ITEM(obj, 2);
+       if (!PyInt_Check(Id) || !PyString_Check(Descr) || !PyBool_Check(Enabled))
+               goto arg_error;
+       strcpy(m_description, PyString_AS_STRING(Descr));
+       m_slotid = PyInt_AsLong(Id);
+       m_enabled = Enabled == Py_True;
+       // HACK.. the rotor workaround is neede for all NIMs with LNBP21 voltage regulator...
+       m_need_rotor_workaround = !!strstr(m_description, "Alps BSBE1") ||
+               !!strstr(m_description, "Alps BSBE2") ||
+               !!strstr(m_description, "Alps -S");
+       eDebug("setSlotInfo for dvb frontend %d to slotid %d, descr %s, need rotorworkaround %s, enabled %s",
+               m_dvbid, m_slotid, m_description, m_need_rotor_workaround ? "Yes" : "No", m_enabled ? "Yes" : "No" );
+       return true;
+arg_error:
+       PyErr_SetString(PyExc_StandardError,
+               "eDVBFrontend::setSlotInfo must get a tuple with first param slotid, second param slot description and third param enabled boolean");
+       return false;
+}