fix linked tuner handling for more then 2 sat tuners
[enigma2.git] / lib / dvb / frontend.cpp
index 1fa3cafe4c398dea32049f637adb131ddbe96cc8..5d17f4626e925fd58de64eed231059dc0e7b0967 100644 (file)
 #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_1_2 (fe_code_rate_t)(FEC_AUTO+1)
+       #define FEC_S2_2_3 (fe_code_rate_t)(FEC_S2_1_2+1)
+       #define FEC_S2_3_4 (fe_code_rate_t)(FEC_S2_2_3+1)
+       #define FEC_S2_5_6 (fe_code_rate_t)(FEC_S2_3_4+1)
+       #define FEC_S2_7_8 (fe_code_rate_t)(FEC_S2_5_6+1)
+       #define FEC_S2_8_9 (fe_code_rate_t)(FEC_S2_7_8+1)
+       #define FEC_S2_3_5 (fe_code_rate_t)(FEC_S2_8_9+1)
+       #define FEC_S2_4_5 (fe_code_rate_t)(FEC_S2_3_5+1)
+       #define FEC_S2_9_10 (fe_code_rate_t)(FEC_S2_4_5+1)
+#endif
 #endif
 
 #include <dvbsi++/satellite_delivery_system_descriptor.h>
@@ -99,8 +112,8 @@ void eDVBFrontendParametersSatellite::set(const SatelliteDeliverySystemDescripto
        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;
@@ -108,11 +121,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)
@@ -144,7 +178,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)
@@ -292,7 +326,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;
        }
@@ -301,7 +339,7 @@ RESULT eDVBFrontendParameters::getHash(unsigned long &hash) const
 DEFINE_REF(eDVBFrontend);
 
 eDVBFrontend::eDVBFrontend(int adap, int fe, int &ok)
-       :m_type(-1), m_fe(fe), m_fd(-1), m_timeout(0), m_tuneTimer(0)
+       :m_type(-1), m_fe(fe), m_fd(-1), m_sn(0), m_timeout(0), m_tuneTimer(0)
 #if HAVE_DVB_API_VERSION < 3
        ,m_secfd(-1)
 #endif
@@ -318,8 +356,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;
@@ -330,35 +367,44 @@ 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;
+               m_secfd = ::open(m_sec_filename, O_RDWR);
+               if (m_secfd < 0)
+               {
+                       eWarning("failed! (%s) %m", m_sec_filename);
+                       return -1;
+               }
        }
+       else
+               eWarning("sec %d already opened", m_fe);
        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);
        if (m_fd < 0)
        {
-               eWarning("failed! (%s) %m", m_filename);
+               m_fd = ::open(m_filename, O_RDWR|O_NONBLOCK);
+               if (m_fd < 0)
+               {
+                       eWarning("failed! (%s) %m", m_filename);
 #if HAVE_DVB_API_VERSION < 3
-               ::close(m_secfd);
-               m_secfd=-1;
+                       ::close(m_secfd);
+                       m_secfd=-1;
 #endif
-               return -1;
+                       return -1;
+               }
        }
-
+       else
+               eWarning("frontend %d already opened", m_fe);
        if (m_type == -1)
        {
                if (::ioctl(m_fd, FE_GET_INFO, &fe_info) < 0)
@@ -408,16 +454,16 @@ int eDVBFrontend::openFrontend()
 
 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());
                        return -1;
                }
+               linked_fe->m_frontend->getData(LINKED_NEXT_PTR, (int&)linked_fe);
        }
        if (m_fd >= 0)
        {
@@ -427,15 +473,19 @@ int eDVBFrontend::closeFrontend()
                setVoltage(iDVBFrontend::voltageOff);
                if (m_sec)
                        m_sec->setRotorMoving(false);
-               ::close(m_fd);
-               m_fd=-1;
-               m_data[0] = m_data[1] = m_data[2] = -1;
+               if (!::close(m_fd))
+                       m_fd=-1;
+               else
+                       eWarning("couldnt close frontend %d", m_fe);
+               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_fe);
        }
 #endif
        delete m_sn;
@@ -491,7 +541,7 @@ void eDVBFrontend::feEvent(int w)
                        else
                        {
                                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)
@@ -537,7 +587,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;
@@ -548,7 +598,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;
@@ -559,6 +609,8 @@ int eDVBFrontend::readFrontendData(int type)
                                eDebug("FE_READ_STATUS failed (%m)");
                        return !!(status&FE_HAS_SYNC);
                }
+               case frontendNumber:
+                       return m_fe;
        }
        return 0;
 }
@@ -593,36 +645,69 @@ void fillDictWithSatelliteData(PyObject *dict, const FRONTENDPARAMETERS &parm, e
 {
        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_1_2:
+               tmp = "FEC_1_2";
+               break;
+       case FEC_S2_2_3:
+               tmp = "FEC_2_3";
+               break;
+       case FEC_S2_3_4:
+               tmp = "FEC_3_4";
+               break;
+       case FEC_S2_5_6:
+               tmp = "FEC_5_6";
+               break;
+       case FEC_S2_7_8:
+               tmp = "FEC_7_8";
+               break;
+       case FEC_S2_8_9:
+               tmp = "FEC_8_9";
                break;
+       case FEC_S2_3_5:
+               tmp = "FEC_3_5";
+               break;
+       case FEC_S2_4_5:
+               tmp = "FEC_4_5";
+               break;
+       case FEC_S2_9_10:
+               tmp = "FEC_9_10";
+               break;
+#endif
        }
-       PutToDict(dict, "fec_inner", fec);
+       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)
@@ -761,6 +846,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";
@@ -879,8 +967,8 @@ PyObject *eDVBFrontend::readTransponderData(bool original)
                        }
                        PutToDict(ret, "tuner_state", tmp);
 
-                       PutToDict(ret, "tuner_locked", readFrontendData(Locked));
-                       PutToDict(ret, "tuner_synced", readFrontendData(Synced));
+                       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));
@@ -1083,7 +1171,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;
@@ -1145,19 +1233,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;
+                               m_data[ROTOR_CMD] = m_data[ROTOR_POS] = -1;
                                eDebug("[SEC] invalidate current rotorparams");
                                ++m_sec_sequence.current();
                                break;
                        case eSecCommand::UPDATE_CURRENT_ROTORPARAMS:
-                               m_data[5] = m_data[3];
-                               m_data[6] = m_data[4];
+                               m_data[ROTOR_CMD] = m_data[NEW_ROTOR_CMD];
+                               m_data[ROTOR_POS] = m_data[NEW_ROTOR_POS];
                                eDebug("[SEC] update current rotorparams %d %04x %d", m_timeoutCount, m_data[5], m_data[6]);
                                ++m_sec_sequence.current();
                                break;
@@ -1253,30 +1341,69 @@ RESULT eDVBFrontend::prepare_sat(const eDVBFrontendParametersSatellite &feparm)
                                parm_inversion = INVERSION_AUTO;
                                break;
                }
-               switch (feparm.fec)
-               {
-                       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;
-               }
+               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
+                       switch (feparm.fec)
+                       {
+                               case eDVBFrontendParametersSatellite::FEC::f1_2:
+                                       parm_u_qpsk_fec_inner = FEC_S2_1_2;
+                                       break;
+                               case eDVBFrontendParametersSatellite::FEC::f2_3:
+                                       parm_u_qpsk_fec_inner = FEC_S2_2_3;
+                                       break;
+                               case eDVBFrontendParametersSatellite::FEC::f3_4:
+                                       parm_u_qpsk_fec_inner = FEC_S2_3_4;
+                                       break;
+                               case eDVBFrontendParametersSatellite::FEC::f3_5:
+                                       parm_u_qpsk_fec_inner = FEC_S2_3_5;
+                                       break;
+                               case eDVBFrontendParametersSatellite::FEC::f4_5:
+                                       parm_u_qpsk_fec_inner = FEC_S2_4_5;
+                                       break;
+                               case eDVBFrontendParametersSatellite::FEC::f5_6:
+                                       parm_u_qpsk_fec_inner = FEC_S2_5_6;
+                                       break;
+                               case eDVBFrontendParametersSatellite::FEC::f7_8:
+                                       parm_u_qpsk_fec_inner = FEC_S2_7_8;
+                                       break;
+                               case eDVBFrontendParametersSatellite::FEC::f8_9:
+                                       parm_u_qpsk_fec_inner = FEC_S2_8_9;
+                                       break;
+                               case eDVBFrontendParametersSatellite::FEC::f9_10:
+                                       parm_u_qpsk_fec_inner = FEC_S2_9_10;
+                                       break;
+                               default:
+                                       eDebug("no valid fec for DVB-S2 set.. abort !!");
+                                       return -EINVAL;
+                       }
+#endif
                // FIXME !!! get frequency range from tuner
                if ( parm_frequency < 900000 || parm_frequency > 2200000 )
                {
@@ -1432,6 +1559,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;
@@ -1488,6 +1618,19 @@ 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;
 }
 
@@ -1719,7 +1862,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;
@@ -1729,7 +1872,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;