add zapping history.. useable with < > buttons
[enigma2.git] / lib / dvb / frontend.cpp
index 6e8bcaa7fd1b5afc58ce1a1a59b741efe1042c16..630c0308026afd81f0d7b2ebfd5c21dbb3d438bc 100644 (file)
@@ -1,4 +1,3 @@
-#include <config.h>
 #include <lib/dvb/dvb.h>
 #include <lib/base/eerror.h>
 #include <errno.h>
@@ -6,6 +5,10 @@
 #include <fcntl.h>
 #include <sys/ioctl.h>
 
+#ifndef I2C_SLAVE_FORCE
+#define I2C_SLAVE_FORCE        0x0706
+#endif
+
 #if HAVE_DVB_API_VERSION < 3
 #include <ost/frontend.h>
 #include <ost/sec.h>
@@ -23,9 +26,9 @@
 #include <linux/dvb/frontend.h>
 #endif
 
-#include <lib/dvb_si/satellite_delivery_system_descriptor.h>
-#include <lib/dvb_si/cable_delivery_system_descriptor.h>
-#include <lib/dvb_si/terrestrial_delivery_system_descriptor.h>
+#include <dvbsi++/satellite_delivery_system_descriptor.h>
+#include <dvbsi++/cable_delivery_system_descriptor.h>
+#include <dvbsi++/terrestrial_delivery_system_descriptor.h>
 
 void eDVBFrontendParametersSatellite::set(const SatelliteDeliverySystemDescriptor &descriptor)
 {
@@ -127,21 +130,21 @@ RESULT eDVBFrontendParameters::getDVBT(eDVBFrontendParametersTerrestrial &p) con
        return 0;
 }
 
-RESULT eDVBFrontendParameters::setDVBS(eDVBFrontendParametersSatellite &p)
+RESULT eDVBFrontendParameters::setDVBS(const eDVBFrontendParametersSatellite &p)
 {
        sat = p;
        m_type = iDVBFrontend::feSatellite;
        return 0;
 }
 
-RESULT eDVBFrontendParameters::setDVBC(eDVBFrontendParametersCable &p)
+RESULT eDVBFrontendParameters::setDVBC(const eDVBFrontendParametersCable &p)
 {
        cable = p;
        m_type = iDVBFrontend::feCable;
        return 0;
 }
 
-RESULT eDVBFrontendParameters::setDVBT(eDVBFrontendParametersTerrestrial &p)
+RESULT eDVBFrontendParameters::setDVBT(const eDVBFrontendParametersTerrestrial &p)
 {
        terrestrial = p;
        m_type = iDVBFrontend::feTerrestrial;
@@ -173,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:
@@ -191,8 +197,8 @@ RESULT eDVBFrontendParameters::getHash(unsigned long &hash) const
        {
        case iDVBFrontend::feSatellite:
        {
-               hash  = sat.frequency & 0xFFFF;
-               hash |= sat.orbital_position << 16;
+               hash = (sat.orbital_position << 16);
+               hash |= ((sat.frequency/1000)&0xFFFF)|((sat.polarisation&1) << 15);
                return 0;
        }
        case iDVBFrontend::feCable:
@@ -204,94 +210,153 @@ RESULT eDVBFrontendParameters::getHash(unsigned long &hash) const
 
 DEFINE_REF(eDVBFrontend);
 
-eDVBFrontend::eDVBFrontend(int adap, int fe, int &ok): m_type(-1)
+eDVBFrontend::eDVBFrontend(int adap, int fe, int &ok)
+       :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
-       char sec_filename[128];
+       sprintf(m_filename, "/dev/dvb/card%d/frontend%d", adap, fe);
+       sprintf(m_sec_filename, "/dev/dvb/card%d/sec%d", adap, fe);
+#else
+       sprintf(m_filename, "/dev/dvb/adapter%d/frontend%d", adap, fe);
 #endif
-       char filename[128];
+       m_timeout = new eTimer(eApp);
+       CONNECT(m_timeout->timeout, eDVBFrontend::timeout);
+
+       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)
+               m_data[i] = -1;
+
+       m_idleInputpower[0]=m_idleInputpower[1]=0;
 
-       int result;
+       ok = !openFrontend();
+       closeFrontend();
+}
+
+int eDVBFrontend::openFrontend()
+{
+       if (m_fd >= 0)
+               return -1;  // already opened
 
-       m_sn = 0;
-       m_timeout = 0;
+       m_state=0;
+       m_tuning=0;
 
 #if HAVE_DVB_API_VERSION < 3
-       sprintf(sec_filename, "/dev/dvb/card%d/sec%d", adap, fe);
-       m_secfd = ::open(sec_filename, O_RDWR);
+       m_secfd = ::open(m_sec_filename, O_RDWR);
        if (m_secfd < 0)
        {
-               eWarning("failed! (%s) %m", sec_filename);
-               ok = 0;
-               return;
+               eWarning("failed! (%s) %m", m_sec_filename);
+               return -1;
        }
-       else
-               eDebug("m_secfd is %d", m_secfd);
-
        FrontendInfo fe_info;
-       sprintf(filename, "/dev/dvb/card%d/frontend%d", adap, fe);
 #else
-       dvb_frontend_info fe_info;      
-       sprintf(filename, "/dev/dvb/adapter%d/frontend%d", adap, fe);
+       dvb_frontend_info fe_info;
 #endif
-       eDebug("opening frontend.");
-       m_fd = ::open(filename, O_RDWR|O_NONBLOCK);
+       eDebug("opening frontend %d", m_fe);
+       m_fd = ::open(m_filename, O_RDWR|O_NONBLOCK);
        if (m_fd < 0)
        {
-               eWarning("failed! (%s) %m", filename);
-               ok = 0;
-               return;
-       }
-
-       result = ::ioctl(m_fd, FE_GET_INFO, &fe_info);
-       
-       if (result < 0) {
-               eWarning("ioctl FE_GET_INFO failed");
-               ::close(m_fd);
-               m_fd = -1;
-               ok = 0;
-               return;
+               eWarning("failed! (%s) %m", m_filename);
+#if HAVE_DVB_API_VERSION < 3
+               ::close(m_secfd);
+               m_secfd=-1;
+#endif
+               return -1;
        }
 
-       switch (fe_info.type) 
+       if (m_type == -1)
        {
-       case FE_QPSK:
-               m_type = feSatellite;
-               break;
-       case FE_QAM:
-               m_type = feCable;
-               break;
-       case FE_OFDM:
-               m_type = feTerrestrial;
-               break;
-       default:
-               eWarning("unknown frontend type.");
-               ::close(m_fd);
-               m_fd = -1;
-               ok = 0;
-               return;
+               if (::ioctl(m_fd, FE_GET_INFO, &fe_info) < 0)
+               {
+                       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;
+               }
+
+               switch (fe_info.type)
+               {
+               case FE_QPSK:
+                       m_type = iDVBFrontend::feSatellite;
+                       break;
+               case FE_QAM:
+                       m_type = iDVBFrontend::feCable;
+                       break;
+               case FE_OFDM:
+                       m_type = iDVBFrontend::feTerrestrial;
+                       break;
+               default:
+                       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);
        }
-       eDebug("detected %s frontend", "satellite\0cable\0    terrestrial"+fe_info.type*9);
-       ok = 1;
+
+       setTone(iDVBFrontend::toneOff);
+       setVoltage(iDVBFrontend::voltageOff);
 
        m_sn = new eSocketNotifier(eApp, m_fd, eSocketNotifier::Read);
        CONNECT(m_sn->activated, eDVBFrontend::feEvent);
        m_sn->start();
-       
-       m_timeout = new eTimer(eApp);
-       CONNECT(m_timeout->timeout, eDVBFrontend::timeout);
-       
-       return;
+
+       return 0;
 }
 
-eDVBFrontend::~eDVBFrontend()
+int eDVBFrontend::closeFrontend()
 {
+       if (!m_fe && m_data[7] != -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;
+               }
+       }
        if (m_fd >= 0)
+       {
+               eDebug("close frontend %d", m_fe);
+               setTone(iDVBFrontend::toneOff);
+               setVoltage(iDVBFrontend::voltageOff);
                ::close(m_fd);
-       if (m_sn)
-               delete m_sn;
-       if (m_timeout)
-               delete m_timeout;
+               m_fd=-1;
+               m_data[0] = m_data[1] = m_data[2] = -1;
+       }
+#if HAVE_DVB_API_VERSION < 3
+       if (m_secfd >= 0)
+       {
+               ::close(m_secfd);
+               m_secfd=-1;
+       }
+#endif
+       delete m_sn;
+       m_sn=0;
+
+       return 0;
+}
+
+eDVBFrontend::~eDVBFrontend()
+{
+       closeFrontend();
+       delete m_timeout;
+       delete m_tuneTimer;
 }
 
 void eDVBFrontend::feEvent(int w)
@@ -322,7 +387,7 @@ void eDVBFrontend::feEvent(int w)
 #if HAVE_DVB_API_VERSION < 3
                if (event.type == FE_COMPLETION_EV)
 #else
-               eDebug("fe event: status %x, inversion %s", event.status, (event.parameters.inversion == INVERSION_ON) ? "on" : "off");
+               eDebug("(%d)fe event: status %x, inversion %s", m_fe, event.status, (event.parameters.inversion == INVERSION_ON) ? "on" : "off");
                if (event.status & FE_HAS_LOCK)
 #endif
                {
@@ -332,7 +397,10 @@ void eDVBFrontend::feEvent(int w)
                        if (m_tuning)
                                state = stateTuning;
                        else
-                               state = stateFailed;
+                       {
+                               state = stateLostLock;
+                               m_data[0] = m_data[1] = m_data[2] = -1; // reset diseqc
+                       }
                }
                if (m_state != state)
                {
@@ -344,18 +412,303 @@ 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 = stateFailed;
+               m_stateChanged(this);
+       }
+}
+
+int eDVBFrontend::readFrontendData(int type)
+{
+       switch(type)
+       {
+               case bitErrorRate:
                {
-                       m_state = state;
-                       m_stateChanged(this);
+                       uint32_t ber=0;
+                       if (ioctl(m_fd, FE_READ_BER, &ber) < 0 && errno != ERANGE)
+                               eDebug("FE_READ_BER failed (%m)");
+                       return ber;
+               }
+               case signalPower:
+               {
+                       uint16_t snr=0;
+                       if (ioctl(m_fd, FE_READ_SNR, &snr) < 0 && errno != ERANGE)
+                               eDebug("FE_READ_SNR failed (%m)");
+                       return snr;
+               }
+               case signalQuality:
+               {
+                       uint16_t strength=0;
+                       if (ioctl(m_fd, FE_READ_SIGNAL_STRENGTH, &strength) < 0 && errno != ERANGE)
+                               eDebug("FE_READ_SIGNAL_STRENGTH failed (%m)");
+                       return strength;
+               }
+       }
+       return 0;
+}
+
+#ifndef FP_IOCTL_GET_ID
+#define FP_IOCTL_GET_ID 0
+#endif
+int eDVBFrontend::readInputpower()
+{
+       int power=m_fe;  // this is needed for read inputpower from the correct tuner !
+
+       // open front prozessor
+       int fp=::open("/dev/dbox/fp0", O_RDWR);
+       if (fp < 0)
+       {
+               eDebug("couldn't open fp");
+               return -1;
+       }
+       static bool old_fp = (::ioctl(fp, FP_IOCTL_GET_ID) < 0);
+       if ( ioctl( fp, old_fp ? 9 : 0x100, &power ) < 0 )
+       {
+               eDebug("FP_IOCTL_GET_LNB_CURRENT failed (%m)");
+               return -1;
+       }
+       ::close(fp);
+
+       return power;
+}
+
+bool eDVBFrontend::setSecSequencePos(int steps)
+{
+       eDebug("set sequence pos %d", steps);
+       if (!steps)
+               return false;
+       while( steps > 0 )
+       {
+               if (m_sec_sequence.current() != m_sec_sequence.end())
+                       ++m_sec_sequence.current();
+               --steps;
+       }
+       while( steps < 0 )
+       {
+               if (m_sec_sequence.current() != m_sec_sequence.begin() && m_sec_sequence.current() != m_sec_sequence.end())
+                       --m_sec_sequence.current();
+               ++steps;
+       }
+       return true;
+}
+
+void eDVBFrontend::tuneLoop()  // called by m_tuneTimer
+{
+       int delay=0;
+       if ( m_sec_sequence && m_sec_sequence.current() != m_sec_sequence.end() )
+       {
+               switch (m_sec_sequence.current()->cmd)
+               {
+                       case eSecCommand::SLEEP:
+                               delay = m_sec_sequence.current()++->msec;
+                               eDebug("[SEC] sleep %dms", delay);
+                               break;
+                       case eSecCommand::GOTO:
+                               if ( !setSecSequencePos(m_sec_sequence.current()->steps) )
+                                       ++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);
+                               break;
+                       case eSecCommand::SEND_DISEQC:
+                               sendDiseqc(m_sec_sequence.current()->diseqc);
+                               eDebugNoNewLine("[SEC] sendDiseqc: ");
+                               for (int i=0; i < m_sec_sequence.current()->diseqc.len; ++i)
+                                   eDebugNoNewLine("%02x", m_sec_sequence.current()->diseqc.data[i]);
+                               eDebug("");
+                               ++m_sec_sequence.current();
+                               break;
+                       case eSecCommand::SEND_TONEBURST:
+                               eDebug("[SEC] sendToneburst: %d", m_sec_sequence.current()->toneburst);
+                               sendToneburst(m_sec_sequence.current()++->toneburst);
+                               break;
+                       case eSecCommand::SET_FRONTEND:
+                               eDebug("[SEC] setFrontend");
+                               setFrontend();
+                               ++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::IF_TIMEOUT_GOTO:
+                               if (!m_timeoutCount)
+                               {
+                                       eDebug("[SEC] rotor timout");
+                                       m_sec->setRotorMoving(false);
+                                       setSecSequencePos(m_sec_sequence.current()->steps);
+                               }
+                               else
+                                       ++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::IF_MEASURE_IDLE_WAS_NOT_OK_GOTO:
+                       {
+                               eSecCommand::pair &compare = m_sec_sequence.current()->compare;
+                               int idx = compare.voltage;
+                               if ( idx == 0 || idx == 1 )
+                               {
+                                       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;
+                                       }
+                               }
+                               ++m_sec_sequence.current();
+                               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&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",
+                                       txt,
+                                       m_runningInputpower,
+                                       idleInputpower,
+                                       cmd.deltaA);
+                               if ( (cmd.direction && abs(m_runningInputpower - idleInputpower) >= cmd.deltaA)
+                                       || (!cmd.direction && abs(m_runningInputpower - idleInputpower) <= cmd.deltaA) )
+                               {
+                                       ++cmd.okcount;
+                                       eDebug("[SEC] rotor %s step %d ok", txt, cmd.okcount);
+                                       if ( cmd.okcount > 6 )
+                                       {
+                                               m_sec->setRotorMoving(cmd.direction);
+                                               eDebug("[SEC] rotor is %s", txt);
+                                               if (setSecSequencePos(cmd.steps))
+                                                       break;
+                                       }
+                               }
+                               else
+                               {
+                                       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_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:
+                       {
+                               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;
+                       }
+                       default:
+                               ++m_sec_sequence.current();
+                               eDebug("[SEC] unhandled sec command");
                }
-       } else
-               m_tuning = 0;
+               m_tuneTimer->start(delay,true);
+       }
+}
+
+void eDVBFrontend::setFrontend()
+{
+       eDebug("setting frontend %d", m_fe);
+       if (ioctl(m_fd, FE_SET_FRONTEND, &parm) == -1)
+       {
+               perror("FE_SET_FRONTEND failed");
+               return;
+       }
+       m_timeout->start(5000, 1); // 5 sec timeout. TODO: symbolrate dependent
 }
 
 RESULT eDVBFrontend::getFrontendType(int &t)
@@ -368,15 +721,15 @@ RESULT eDVBFrontend::getFrontendType(int &t)
 
 RESULT eDVBFrontend::tune(const iDVBFrontendParameters &where)
 {
+       eDebug("(%d)tune", m_fe);
+
        if (m_type == -1)
                return -ENODEV;
 
-       FRONTENDPARAMETERS parm;
-
        feEvent(-1);
-       
-       eDebug("eDVBFrontend::tune. type: %d", m_type);
-       
+
+       m_sec_sequence.clear();
+
        switch (m_type)
        {
        case feSatellite:
@@ -394,7 +747,7 @@ RESULT eDVBFrontend::tune(const iDVBFrontendParameters &where)
                        return -ENOENT;
                }
                
-               res = m_sec->prepare(*this, parm, feparm);
+               res = m_sec->prepare(*this, parm, feparm, 1 << m_fe);
                if (res)
                        return res;
 #if HAVE_DVB_API_VERSION < 3
@@ -406,10 +759,115 @@ RESULT eDVBFrontend::tune(const iDVBFrontendParameters &where)
        }
        case feCable:
        {
+#if HAVE_DVB_API_VERSION >= 3
                eDVBFrontendParametersCable feparm;
                if (where.getDVBC(feparm))
                        return -EINVAL;
-               eFatal("cable tuning nyi");
+#if HAVE_DVB_API_VERSION < 3
+               parm.Frequency = feparm.frequency * 1000;
+               parm.u.qam.SymbolRate = feparm.symbol_rate;
+#else
+               parm.frequency = feparm.frequency * 1000;
+               parm.u.qam.symbol_rate = feparm.symbol_rate;
+#endif
+               fe_modulation_t mod=QAM_AUTO;
+               switch (feparm.modulation)
+               {
+               case eDVBFrontendParametersCable::Modulation::QAM16:
+                       mod = QAM_16;
+                       break;
+               case eDVBFrontendParametersCable::Modulation::QAM32:
+                       mod = QAM_32;
+                       break;
+               case eDVBFrontendParametersCable::Modulation::QAM64:
+                       mod = QAM_64;
+                       break;
+               case eDVBFrontendParametersCable::Modulation::QAM128:
+                       mod = QAM_128;
+                       break;
+               case eDVBFrontendParametersCable::Modulation::QAM256:
+                       mod = QAM_256;
+                       break;                  
+               case eDVBFrontendParametersCable::Modulation::Auto:
+                       mod = QAM_AUTO;
+                       break;                  
+               }
+#if HAVE_DVB_API_VERSION < 3
+               parm.u.qam.QAM = mod;
+#else
+               parm.u.qam.modulation = mod;
+#endif
+               switch (feparm.inversion)
+               {               
+               case eDVBFrontendParametersCable::Inversion::On:
+                       #if HAVE_DVB_API_VERSION < 3
+                       parm.Inversion =
+                       #else
+                       parm.inversion =
+                       #endif
+                               INVERSION_ON;
+                       break;
+               case eDVBFrontendParametersCable::Inversion::Off:
+                       #if HAVE_DVB_API_VERSION < 3
+                       parm.Inversion =
+                       #else
+                       parm.inversion =
+                       #endif
+                               INVERSION_OFF;
+                       break;
+               case eDVBFrontendParametersCable::Inversion::Unknown:
+                       #if HAVE_DVB_API_VERSION < 3
+                       parm.Inversion =
+                       #else
+                       parm.inversion =
+                       #endif
+                               INVERSION_AUTO;
+                       break;
+               }
+               
+               fe_code_rate_t fec_inner=FEC_AUTO;
+               switch (feparm.fec_inner)
+               {               
+               case eDVBFrontendParametersCable::FEC::fNone:
+                       fec_inner = FEC_NONE;
+                       break;
+               case eDVBFrontendParametersCable::FEC::f1_2:
+                       fec_inner = FEC_1_2;
+                       break;
+               case eDVBFrontendParametersCable::FEC::f2_3:
+                       fec_inner = FEC_2_3;
+                       break;
+               case eDVBFrontendParametersCable::FEC::f3_4:
+                       fec_inner = FEC_3_4;
+                       break;
+               case eDVBFrontendParametersCable::FEC::f4_5:
+                       fec_inner = FEC_4_5;
+                       break;
+               case eDVBFrontendParametersCable::FEC::f5_6:
+                       fec_inner = FEC_5_6;
+                       break;
+               case eDVBFrontendParametersCable::FEC::f6_7:
+                       fec_inner = FEC_6_7;
+                       break;
+               case eDVBFrontendParametersCable::FEC::f7_8:
+                       fec_inner = FEC_7_8;
+                       break;
+               case eDVBFrontendParametersCable::FEC::f8_9:
+                       fec_inner = FEC_8_9;
+                       break;
+               case eDVBFrontendParametersCable::FEC::fAuto:
+                       fec_inner = FEC_AUTO;
+                       break;
+               }
+#if HAVE_DVB_API_VERSION < 3
+               parm.u.qam.FEC_inner = fec_inner;
+#else
+               parm.u.qam.fec_inner = fec_inner;
+#endif
+#else
+               eFatal("Old API not fully supported");
+#endif // old api
+               break;
        }
        case feTerrestrial:
        {
@@ -519,23 +977,18 @@ RESULT eDVBFrontend::tune(const iDVBFrontendParameters &where)
                break;
        }
        }
-       
-       eDebug("setting frontend..\n");
-       
-       if (ioctl(m_fd, FE_SET_FRONTEND, &parm) == -1)
-       {
-               perror("FE_SET_FRONTEND failed");
-               return errno;
-       }
-       
+
+       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);
        }
-       
-       m_timeout->start(5000, 1); // 5 sec timeout. TODO: symbolrate dependent
 
        return 0;
 }
@@ -548,20 +1001,39 @@ RESULT eDVBFrontend::connectStateChange(const Slot1<void,iDVBFrontend*> &stateCh
 
 RESULT eDVBFrontend::setVoltage(int voltage)
 {
+       if (m_type != feSatellite)
+               return -1;
 #if HAVE_DVB_API_VERSION < 3
        secVoltage vlt;
 #else
+       bool increased=false;
        fe_sec_voltage_t vlt;
 #endif
-
+       m_curVoltage=voltage;
        switch (voltage)
        {
        case voltageOff:
+               for (int i=0; i < 3; ++i)  // reset diseqc
+                       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;
@@ -571,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
 }
@@ -583,6 +1057,8 @@ RESULT eDVBFrontend::getState(int &state)
 
 RESULT eDVBFrontend::setTone(int t)
 {
+       if (m_type != feSatellite)
+               return -1;
 #if HAVE_DVB_API_VERSION < 3
        secToneMode_t tone;
 #else
@@ -607,70 +1083,58 @@ RESULT eDVBFrontend::setTone(int t)
 #endif
 }
 
+#if HAVE_DVB_API_VERSION < 3 && !defined(SEC_DISEQC_SEND_MASTER_CMD)
+       #define SEC_DISEQC_SEND_MASTER_CMD _IOW('o', 97, struct secCommand *)
+#endif
+
 RESULT eDVBFrontend::sendDiseqc(const eDVBDiseqcCommand &diseqc)
 {
 #if HAVE_DVB_API_VERSION < 3
-       secCmdSequence seq;
-       secCommand cmd;
-
-       if ( len > 3 )
-       {
-               seq.numCommands=1;
-               cmd.type = SEC_CMDTYPE_DISEQC_RAW;
-               cmd.u.diseqc.cmdtype = diseqc.data[0];
-               eDebug("cmdtype is %02x", diseqc.data[0]);
-               cmd.u.diseqc.addr = diseqc.data[1];
-               eDebug("cmdaddr is %02x", diseqc.data[1]);
-               cmd.u.diseqc.cmd = diseqc.data[2];
-               eDebug("cmd is %02x", diseqc.data[2]);
-               cmd.u.diseqc.numParams = diseqc.len-3;
-               eDebug("numparams %d", diseqc.len-3);
-
-               memcpy(cmd.u.diseqc.params, diseqc.data+3, diseqc.len-3);
-               for (int i=0; i < diseqc.len-3; ++i )
-                       eDebugNoNewLine("%02x ", diseqc.data[3+i]);
-               eDebug("");
-       }
-       else
-               seq.numCommands=0;
-
-       seq.continuousTone = diseqc.tone == toneOn ? SEC_TONE_ON : SEC_TONE_OFF;
-       switch ( diseqc.voltage )
-       {
-               case voltageOff:
-                       seq.voltage = SEC_VOLTAGE_OFF;
-                       break;
-               case voltage13:
-                       seq.voltage = SEC_VOLTAGE_13;
-                       break;
-               case voltage18:
-                       seq.voltage = SEC_VOLTAGE_18;
-                       break;
-       }
-       seq.miniCommand = SEC_MINI_NONE;
-       seq.commands=&cmd;
-
-       if ( ioctl(m_secfd, SEC_SEND_SEQUENCE, &seq) < 0 )
-       {
-               eDebug("SEC_SEND_SEQUENCE failed ( %m )");
-               return -EINVAL;
-       }
-       return 0;
+       struct secCommand cmd;
+       cmd.type = SEC_CMDTYPE_DISEQC_RAW;
+       cmd.u.diseqc.cmdtype = diseqc.data[0];
+       cmd.u.diseqc.addr = diseqc.data[1];
+       cmd.u.diseqc.cmd = diseqc.data[2];
+       cmd.u.diseqc.numParams = diseqc.len-3;
+       memcpy(cmd.u.diseqc.params, diseqc.data+3, diseqc.len-3);
+       if (::ioctl(m_secfd, SEC_DISEQC_SEND_MASTER_CMD, &cmd))
 #else
-       if ( !diseqc.len )
-               return 0;
        struct dvb_diseqc_master_cmd cmd;
-       if (::ioctl(m_fd, FE_SET_TONE, SEC_TONE_OFF))
-               return -EINVAL;
-       usleep(15 * 1000);
        memcpy(cmd.msg, diseqc.data, diseqc.len);
        cmd.msg_len = diseqc.len;
-       
        if (::ioctl(m_fd, FE_DISEQC_SEND_MASTER_CMD, &cmd))
+#endif
+               return -EINVAL;
+       return 0;
+}
+
+#if HAVE_DVB_API_VERSION < 3 && !defined(SEC_DISEQC_SEND_BURST)
+       #define SEC_DISEQC_SEND_BURST _IO('o', 96)
+#endif
+RESULT eDVBFrontend::sendToneburst(int burst)
+{
+#if HAVE_DVB_API_VERSION < 3
+       secMiniCmd cmd = SEC_MINI_NONE;
+       if ( burst == eDVBSatelliteDiseqcParameters::A )
+               cmd = SEC_MINI_A;
+       else if ( burst == eDVBSatelliteDiseqcParameters::B )
+               cmd = SEC_MINI_B;
+       if (::ioctl(m_secfd, SEC_DISEQC_SEND_BURST, cmd))
+               return -EINVAL;
+#else
+       fe_sec_mini_cmd_t cmd = SEC_MINI_A;
+       if ( burst == eDVBSatelliteDiseqcParameters::A )
+               cmd = SEC_MINI_A;
+       else if ( burst == eDVBSatelliteDiseqcParameters::B )
+               cmd = SEC_MINI_B;
+       else
+       {
+               eDebug("unknown toneburst!");
+               return -EINVAL;
+       }
+       if (::ioctl(m_fd, FE_DISEQC_SEND_BURST, cmd))
                return -EINVAL;
-       usleep(15 * 1000);
 #endif
-       eDebug("diseqc ok");
        return 0;
 }
 
@@ -679,3 +1143,45 @@ RESULT eDVBFrontend::setSEC(iDVBSatelliteEquipmentControl *sec)
        m_sec = sec;
        return 0;
 }
+
+RESULT eDVBFrontend::setSecSequence(const eSecCommandList &list)
+{
+       m_sec_sequence = list;
+       return 0;
+}
+
+RESULT eDVBFrontend::getData(int num, int &data)
+{
+       if ( num < (int)(sizeof(m_data)/sizeof(int)) )
+       {
+               data = m_data[num];
+               return 0;
+       }
+       return -EINVAL;
+}
+
+RESULT eDVBFrontend::setData(int num, int val)
+{
+       if ( num < (int)(sizeof(m_data)/sizeof(int)) )
+       {
+               m_data[num] = val;
+               return 0;
+       }
+       return -EINVAL;
+}
+
+int eDVBFrontend::isCompatibleWith(ePtr<iDVBFrontendParameters> &feparm)
+{
+       int type;
+       if (feparm->getSystem(type) || type != m_type)
+               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);
+       }
+       return 1;
+}