cable scan works now
[enigma2.git] / lib / dvb / frontend.cpp
index d4f3d3690adf3884affcd272a287e2d3498ca182..630c0308026afd81f0d7b2ebfd5c21dbb3d438bc 100644 (file)
@@ -1,4 +1,3 @@
-#include <config.h>
 #include <lib/dvb/dvb.h>
 #include <lib/base/eerror.h>
 #include <errno.h>
@@ -177,8 +176,11 @@ RESULT eDVBFrontendParameters::calculateDifference(const iDVBFrontendParameters
                        diff = 1<<29;
                else if (sat.polarisation != osat.polarisation)
                        diff = 1<<28;
-               else 
+               else
+               {
                        diff = abs(sat.frequency - osat.frequency);
+                       diff += abs(sat.symbol_rate - osat.symbol_rate);
+               }
                return 0;
        }
        case iDVBFrontend::feCable:
@@ -209,16 +211,17 @@ 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_timeout(0), m_tuneTimer(0), m_fd(-1)
+       :m_type(-1), m_fe(fe), m_fd(-1), m_timeout(0), m_tuneTimer(0)
 #if HAVE_DVB_API_VERSION < 3
        ,m_secfd(-1)
 #endif
 {
 #if HAVE_DVB_API_VERSION < 3
+       sprintf(m_filename, "/dev/dvb/card%d/frontend%d", adap, fe);
        sprintf(m_sec_filename, "/dev/dvb/card%d/sec%d", adap, fe);
-#endif
+#else
        sprintf(m_filename, "/dev/dvb/adapter%d/frontend%d", adap, fe);
-
+#endif
        m_timeout = new eTimer(eApp);
        CONNECT(m_timeout->timeout, eDVBFrontend::timeout);
 
@@ -229,6 +232,8 @@ eDVBFrontend::eDVBFrontend(int adap, int fe, int &ok)
        for (int i=0; i<entries; ++i)
                m_data[i] = -1;
 
+       m_idleInputpower[0]=m_idleInputpower[1]=0;
+
        ok = !openFrontend();
        closeFrontend();
 }
@@ -239,7 +244,6 @@ int eDVBFrontend::openFrontend()
                return -1;  // already opened
 
        m_state=0;
-       m_curVoltage=voltageOff;
        m_tuning=0;
 
 #if HAVE_DVB_API_VERSION < 3
@@ -326,11 +330,14 @@ int eDVBFrontend::closeFrontend()
                        return -1;
                }
        }
-       eDebug("close frontend %d", m_fe);
        if (m_fd >= 0)
        {
+               eDebug("close frontend %d", m_fe);
+               setTone(iDVBFrontend::toneOff);
+               setVoltage(iDVBFrontend::voltageOff);
                ::close(m_fd);
                m_fd=-1;
+               m_data[0] = m_data[1] = m_data[2] = -1;
        }
 #if HAVE_DVB_API_VERSION < 3
        if (m_secfd >= 0)
@@ -342,8 +349,6 @@ int eDVBFrontend::closeFrontend()
        delete m_sn;
        m_sn=0;
 
-       setTone(iDVBFrontend::toneOff);
-       setVoltage(iDVBFrontend::voltageOff);
        return 0;
 }
 
@@ -394,9 +399,7 @@ void eDVBFrontend::feEvent(int w)
                        else
                        {
                                state = stateLostLock;
-
-                               if (m_state != stateLostLock)
-                                       eDebug("FIXME: we lost lock, so we might have to retune.");
+                               m_data[0] = m_data[1] = m_data[2] = -1; // reset diseqc
                        }
                }
                if (m_state != state)
@@ -409,19 +412,12 @@ void eDVBFrontend::feEvent(int w)
 
 void eDVBFrontend::timeout()
 {
-       int state;
+       m_tuning = 0;
        if (m_state == stateTuning)
        {
-               state = stateFailed;
-               eDebug("DVBFrontend: timeout");
-               if (m_state != state)
-               {
-                       m_state = state;
-                       m_stateChanged(this);
-               }
-               m_tuning = 0;
-       } else
-               m_tuning = 0;
+               m_state = stateFailed;
+               m_stateChanged(this);
+       }
 }
 
 int eDVBFrontend::readFrontendData(int type)
@@ -514,10 +510,28 @@ void eDVBFrontend::tuneLoop()  // called by m_tuneTimer
                                        ++m_sec_sequence.current();
                                break;
                        case eSecCommand::SET_VOLTAGE:
+                       {
                                int voltage = m_sec_sequence.current()++->voltage;
                                eDebug("[SEC] setVoltage %d", voltage);
                                setVoltage(voltage);
                                break;
+                       }
+                       case eSecCommand::IF_VOLTAGE_GOTO:
+                       {
+                               eSecCommand::pair &compare = m_sec_sequence.current()->compare;
+                               if ( compare.voltage == m_curVoltage && setSecSequencePos(compare.steps) )
+                                       break;
+                               ++m_sec_sequence.current();
+                               break;
+                       }
+                       case eSecCommand::IF_NOT_VOLTAGE_GOTO:
+                       {
+                               eSecCommand::pair &compare = m_sec_sequence.current()->compare;
+                               if ( compare.voltage != m_curVoltage && setSecSequencePos(compare.steps) )
+                                       break;
+                               ++m_sec_sequence.current();
+                               break;
+                       }
                        case eSecCommand::SET_TONE:
                                eDebug("[SEC] setTone %d", m_sec_sequence.current()->tone);
                                setTone(m_sec_sequence.current()++->tone);
@@ -539,33 +553,10 @@ void eDVBFrontend::tuneLoop()  // called by m_tuneTimer
                                setFrontend();
                                ++m_sec_sequence.current();
                                break;
-                       case eSecCommand::MEASURE_IDLE_INPUTPOWER:
-                       {
-                               int idx = m_sec_sequence.current()++->val;
-                               if ( idx == 0 || idx == 1 )
-                               {
-                                       m_idleInputpower[idx] = readInputpower();
-                                       eDebug("[SEC] idleInputpower[%d] is %d", idx, m_idleInputpower[idx]);
-                               }
-                               else
-                                       eDebug("[SEC] idleInputpower measure index(%d) out of bound !!!", idx);
-                               break;
-                       }
-                       case eSecCommand::MEASURE_RUNNING_INPUTPOWER:
-                               m_runningInputpower = readInputpower();
-                               eDebug("[SEC] runningInputpower is %d", m_runningInputpower);
-                               ++m_sec_sequence.current();
-                               break;
                        case eSecCommand::SET_TIMEOUT:
                                m_timeoutCount = m_sec_sequence.current()++->val;
                                eDebug("[SEC] set timeout %d", m_timeoutCount);
                                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]);
-                               ++m_sec_sequence.current();
-                               break;
                        case eSecCommand::IF_TIMEOUT_GOTO:
                                if (!m_timeoutCount)
                                {
@@ -576,40 +567,44 @@ void eDVBFrontend::tuneLoop()  // called by m_tuneTimer
                                else
                                        ++m_sec_sequence.current();
                                break;
-                       case eSecCommand::SET_POWER_LIMITING_MODE:
+                       case eSecCommand::MEASURE_IDLE_INPUTPOWER:
                        {
-                               int fd = m_fe ?
-                                       ::open("/dev/i2c/1", O_RDWR) :
-                                       ::open("/dev/i2c/0", O_RDWR);
-
-                               unsigned char data[2];
-                               ::ioctl(fd, I2C_SLAVE_FORCE, 0x10 >> 1);
-                               if(::read(fd, data, 1) != 1)
-                                       eDebug("[SEC] error read lnbp (%m)");
-                               if ( m_sec_sequence.current()->mode == eSecCommand::modeStatic )
+                               int idx = m_sec_sequence.current()++->val;
+                               if ( idx == 0 || idx == 1 )
                                {
-                                       data[0] |= 0x90;  // enable static current limiting
-                                       eDebug("[SEC] set static current limiting");
+                                       m_idleInputpower[idx] = readInputpower();
+                                       eDebug("[SEC] idleInputpower[%d] is %d", idx, m_idleInputpower[idx]);
                                }
                                else
+                                       eDebug("[SEC] idleInputpower measure index(%d) out of bound !!!", idx);
+                               break;
+                       }
+                       case eSecCommand::IF_MEASURE_IDLE_WAS_NOT_OK_GOTO:
+                       {
+                               eSecCommand::pair &compare = m_sec_sequence.current()->compare;
+                               int idx = compare.voltage;
+                               if ( idx == 0 || idx == 1 )
                                {
-                                       data[0] &= ~0x90;  // enable dynamic current limiting
-                                       eDebug("[SEC] set dynamic current limiting");
+                                       int idle = readInputpower();
+                                       int diff = abs(idle-m_idleInputpower[idx]);
+                                       if ( diff > 0)
+                                       {
+                                               eDebug("measure idle(%d) was not okay.. (%d - %d = %d) retry", idx, m_idleInputpower[idx], idle, diff);
+                                               setSecSequencePos(compare.steps);
+                                               break;
+                                       }
                                }
-                               if(::write(fd, data, 1) != 1)
-                                       eDebug("[SEC] error write lnbp (%m)");
-                               ::close(fd);
                                ++m_sec_sequence.current();
                                break;
                        }
-                       case eSecCommand::IF_IDLE_INPUTPOWER_AVAIL_GOTO:
-                               if (m_idleInputpower[0] && m_idleInputpower[1] && setSecSequencePos(m_sec_sequence.current()->steps))
-                                       break;
+                       case eSecCommand::MEASURE_RUNNING_INPUTPOWER:
+                               m_runningInputpower = readInputpower();
+                               eDebug("[SEC] runningInputpower is %d", m_runningInputpower);
                                ++m_sec_sequence.current();
                                break;
                        case eSecCommand::IF_INPUTPOWER_DELTA_GOTO:
                        {
-                               int idleInputpower = m_idleInputpower[m_curVoltage == iDVBFrontend::voltage13 ? 0 : 1];
+                               int idleInputpower = m_idleInputpower[ (m_curVoltage&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",
@@ -634,16 +629,66 @@ void eDVBFrontend::tuneLoop()  // called by m_tuneTimer
                                {
                                        eDebug("[SEC] rotor not %s... reset counter.. increase timeout", txt);
                                        --m_timeoutCount;
+                                       if (!m_timeoutCount && m_retryCount > 0)
+                                               --m_retryCount;
                                        cmd.okcount=0;
                                }
                                ++m_sec_sequence.current();
                                break;
                        }
-                       case eSecCommand::IF_VOLTAGE_GOTO:
+                       case eSecCommand::IF_ROTORPOS_VALID_GOTO:
+                               if (m_data[5] != -1 && m_data[6] != -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");
+                               ++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]);
+                               ++m_sec_sequence.current();
+                               break;
+                       case eSecCommand::SET_ROTOR_DISEQC_RETRYS:
+                               m_retryCount = m_sec_sequence.current()++->val;
+                               eDebug("[SEC] set rotor retries %d", m_retryCount);
+                               break;
+                       case eSecCommand::IF_NO_MORE_ROTOR_DISEQC_RETRYS_GOTO:
+                               if (!m_retryCount)
+                               {
+                                       eDebug("[SEC] no more rotor retrys");
+                                       setSecSequencePos(m_sec_sequence.current()->steps);
+                               }
+                               else
+                                       ++m_sec_sequence.current();
+                               break;
+                       case eSecCommand::SET_POWER_LIMITING_MODE:
                        {
-                               eSecCommand::pair &compare = m_sec_sequence.current()->compare;
-                               if ( compare.voltage == m_curVoltage && setSecSequencePos(compare.steps) )
-                                       break;
+                               int fd = m_fe ?
+                                       ::open("/dev/i2c/1", O_RDWR) :
+                                       ::open("/dev/i2c/0", O_RDWR);
+
+                               unsigned char data[2];
+                               ::ioctl(fd, I2C_SLAVE_FORCE, 0x10 >> 1);
+                               if(::read(fd, data, 1) != 1)
+                                       eDebug("[SEC] error read lnbp (%m)");
+                               if ( m_sec_sequence.current()->mode == eSecCommand::modeStatic )
+                               {
+                                       data[0] |= 0x80;  // enable static current limiting
+                                       eDebug("[SEC] set static current limiting");
+                               }
+                               else
+                               {
+                                       data[0] &= ~0x80;  // enable dynamic current limiting
+                                       eDebug("[SEC] set dynamic current limiting");
+                               }
+                               if(::write(fd, data, 1) != 1)
+                                       eDebug("[SEC] error write lnbp (%m)");
+                               ::close(fd);
                                ++m_sec_sequence.current();
                                break;
                        }
@@ -663,13 +708,6 @@ void eDVBFrontend::setFrontend()
                perror("FE_SET_FRONTEND failed");
                return;
        }
-
-       if (m_state != stateTuning)
-       {
-               m_tuning = 1;
-               m_state = stateTuning;
-               m_stateChanged(this);
-       }
        m_timeout->start(5000, 1); // 5 sec timeout. TODO: symbolrate dependent
 }
 
@@ -942,8 +980,16 @@ RESULT eDVBFrontend::tune(const iDVBFrontendParameters &where)
 
        m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
        m_tuneTimer->start(0,true);
+       m_timeout->stop();
        m_sec_sequence.current() = m_sec_sequence.begin();
 
+       if (m_state != stateTuning)
+       {
+               m_tuning = 1;
+               m_state = stateTuning;
+               m_stateChanged(this);
+       }
+
        return 0;
 }
 
@@ -960,9 +1006,9 @@ RESULT eDVBFrontend::setVoltage(int voltage)
 #if HAVE_DVB_API_VERSION < 3
        secVoltage vlt;
 #else
+       bool increased=false;
        fe_sec_voltage_t vlt;
 #endif
-
        m_curVoltage=voltage;
        switch (voltage)
        {
@@ -971,9 +1017,23 @@ RESULT eDVBFrontend::setVoltage(int voltage)
                        m_data[i]=-1;
                vlt = SEC_VOLTAGE_OFF;
                break;
+       case voltage13_5:
+#if HAVE_DVB_API_VERSION < 3
+               vlt = SEC_VOLTAGE_13_5;
+               break;
+#else
+               increased = true;
+#endif
        case voltage13:
                vlt = SEC_VOLTAGE_13;
                break;
+       case voltage18_5:
+#if HAVE_DVB_API_VERSION < 3
+               vlt = SEC_VOLTAGE_18_5;
+               break;
+#else
+               increased = true;
+#endif
        case voltage18:
                vlt = SEC_VOLTAGE_18;
                break;
@@ -983,6 +1043,8 @@ 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)
+               perror("FE_ENABLE_HIGH_LNB_VOLTAGE");
        return ::ioctl(m_fd, FE_SET_VOLTAGE, vlt);
 #endif
 }