8d035bfa86b37fe6376d43607df7bca116ff4933
[enigma2.git] / lib / dvb / frontend.cpp
1 #include <config.h>
2 #include <lib/dvb/dvb.h>
3 #include <lib/base/eerror.h>
4 #include <errno.h>
5 #include <unistd.h>
6 #include <fcntl.h>
7 #include <sys/ioctl.h>
8
9 #ifndef I2C_SLAVE_FORCE
10 #define I2C_SLAVE_FORCE 0x0706
11 #endif
12
13 #if HAVE_DVB_API_VERSION < 3
14 #include <ost/frontend.h>
15 #include <ost/sec.h>
16 #define QAM_AUTO                                (Modulation)6
17 #define TRANSMISSION_MODE_AUTO  (TransmitMode)2
18 #define BANDWIDTH_AUTO                  (BandWidth)3
19 #define GUARD_INTERVAL_AUTO             (GuardInterval)4
20 #define HIERARCHY_AUTO                  (Hierarchy)4
21 #define constellation Constellation
22 #define guard_interval guardInterval
23 #define hierarchy_information HierarchyInformation
24 #define code_rate_HP HP_CodeRate
25 #define code_rate_LP LP_CodeRate
26 #else
27 #include <linux/dvb/frontend.h>
28 #endif
29
30 #include <dvbsi++/satellite_delivery_system_descriptor.h>
31 #include <dvbsi++/cable_delivery_system_descriptor.h>
32 #include <dvbsi++/terrestrial_delivery_system_descriptor.h>
33
34 void eDVBFrontendParametersSatellite::set(const SatelliteDeliverySystemDescriptor &descriptor)
35 {
36         frequency    = descriptor.getFrequency() * 10;
37         symbol_rate  = descriptor.getSymbolRate() * 100;
38         switch (descriptor.getPolarization())
39         {
40         case 0:
41                 polarisation = Polarisation::Horizontal;
42                 break;
43         case 1:
44                 polarisation = Polarisation::Vertical;
45                 break;
46         case 2:
47                 polarisation = Polarisation::CircularLeft;
48                 break;
49         case 3:
50                 polarisation = Polarisation::CircularRight;
51                 break;
52         }
53         switch (descriptor.getFecInner())
54         {
55         case 1:
56                 fec = FEC::f1_2;
57                 break;
58         case 2:
59                 fec = FEC::f2_3;
60                 break;
61         case 3:
62                 fec = FEC::f3_4;
63                 break;
64         case 4:
65                 fec = FEC::f5_6;
66                 break;
67         case 5:
68                 fec = FEC::f7_8;
69                 break;
70         case 0xF:
71                 fec = FEC::fNone;
72                 break;
73         default:
74                 fec = FEC::fAuto;
75                 break;
76         }
77         inversion = Inversion::Unknown;
78         orbital_position  = ((descriptor.getOrbitalPosition() >> 12) & 0xF) * 1000;
79         orbital_position += ((descriptor.getOrbitalPosition() >> 8) & 0xF) * 100;
80         orbital_position += ((descriptor.getOrbitalPosition() >> 4) & 0xF) * 10;
81         orbital_position += ((descriptor.getOrbitalPosition()) & 0xF);
82         if (orbital_position && (!descriptor.getWestEastFlag()))
83                 orbital_position = 3600 - orbital_position;
84 }
85
86 void eDVBFrontendParametersCable::set(const CableDeliverySystemDescriptor &descriptor)
87 {
88         eFatal("nyi");
89 }
90
91 void eDVBFrontendParametersTerrestrial::set(const TerrestrialDeliverySystemDescriptor  &)
92 {
93         eFatal("nyi");
94 }
95
96 eDVBFrontendParameters::eDVBFrontendParameters(): m_type(-1)
97 {
98 }
99
100 DEFINE_REF(eDVBFrontendParameters);
101
102 RESULT eDVBFrontendParameters::getSystem(int &t) const
103 {
104         if (m_type == -1)
105                 return -1;
106         t = m_type;
107         return 0;
108 }
109
110 RESULT eDVBFrontendParameters::getDVBS(eDVBFrontendParametersSatellite &p) const
111 {
112         if (m_type != iDVBFrontend::feSatellite)
113                 return -1;
114         p = sat;
115         return 0;
116 }
117
118 RESULT eDVBFrontendParameters::getDVBC(eDVBFrontendParametersCable &p) const
119 {
120         if (m_type != iDVBFrontend::feCable)
121                 return -1;
122         p = cable;
123         return 0;
124 }
125
126 RESULT eDVBFrontendParameters::getDVBT(eDVBFrontendParametersTerrestrial &p) const
127 {
128         if (m_type != iDVBFrontend::feTerrestrial)
129                 return -1;
130         p = terrestrial;
131         return 0;
132 }
133
134 RESULT eDVBFrontendParameters::setDVBS(const eDVBFrontendParametersSatellite &p)
135 {
136         sat = p;
137         m_type = iDVBFrontend::feSatellite;
138         return 0;
139 }
140
141 RESULT eDVBFrontendParameters::setDVBC(const eDVBFrontendParametersCable &p)
142 {
143         cable = p;
144         m_type = iDVBFrontend::feCable;
145         return 0;
146 }
147
148 RESULT eDVBFrontendParameters::setDVBT(const eDVBFrontendParametersTerrestrial &p)
149 {
150         terrestrial = p;
151         m_type = iDVBFrontend::feTerrestrial;
152         return 0;
153 }
154
155 RESULT eDVBFrontendParameters::calculateDifference(const iDVBFrontendParameters *parm, int &diff) const
156 {
157         if (!parm)
158                 return -1;
159         int type;
160         if (parm->getSystem(type))
161                 return -1;
162         if (type != m_type)
163         {
164                 diff = 1<<30; // big difference
165                 return 0;
166         }
167         
168         switch (type)
169         {
170         case iDVBFrontend::feSatellite:
171         {
172                 eDVBFrontendParametersSatellite osat;
173                 if (parm->getDVBS(osat))
174                         return -2;
175                 
176                 if (sat.orbital_position != osat.orbital_position)
177                         diff = 1<<29;
178                 else if (sat.polarisation != osat.polarisation)
179                         diff = 1<<28;
180                 else 
181                         diff = abs(sat.frequency - osat.frequency);
182                 return 0;
183         }
184         case iDVBFrontend::feCable:
185         case iDVBFrontend::feTerrestrial:
186         default:
187                 return -1;
188         }
189         return 0;
190 }
191
192 RESULT eDVBFrontendParameters::getHash(unsigned long &hash) const 
193 {
194         switch (m_type)
195         {
196         case iDVBFrontend::feSatellite:
197         {
198                 hash = (sat.orbital_position << 16);
199                 hash |= ((sat.frequency/1000)&0xFFFF)|((sat.polarisation&1) << 15);
200                 return 0;
201         }
202         case iDVBFrontend::feCable:
203         case iDVBFrontend::feTerrestrial:
204         default:
205                 return -1;
206         }
207 }
208
209 DEFINE_REF(eDVBFrontend);
210
211 eDVBFrontend::eDVBFrontend(int adap, int fe, int &ok): m_type(-1), m_fe(fe), m_curVoltage(-1)
212 {
213 #if HAVE_DVB_API_VERSION < 3
214         char sec_filename[128];
215 #endif
216         char filename[128];
217
218         int result;
219
220         m_sn = 0;
221         m_timeout = 0;
222
223 #if HAVE_DVB_API_VERSION < 3
224         sprintf(sec_filename, "/dev/dvb/card%d/sec%d", adap, fe);
225         m_secfd = ::open(sec_filename, O_RDWR);
226         if (m_secfd < 0)
227         {
228                 eWarning("failed! (%s) %m", sec_filename);
229                 ok = 0;
230                 return;
231         }
232         FrontendInfo fe_info;
233         sprintf(filename, "/dev/dvb/card%d/frontend%d", adap, fe);
234 #else
235         dvb_frontend_info fe_info;      
236         sprintf(filename, "/dev/dvb/adapter%d/frontend%d", adap, fe);
237 #endif
238         eDebug("opening frontend.");
239         m_fd = ::open(filename, O_RDWR|O_NONBLOCK);
240         if (m_fd < 0)
241         {
242                 eWarning("failed! (%s) %m", filename);
243                 ok = 0;
244                 return;
245         }
246
247         result = ::ioctl(m_fd, FE_GET_INFO, &fe_info);
248         
249         if (result < 0) {
250                 eWarning("ioctl FE_GET_INFO failed");
251                 ::close(m_fd);
252                 m_fd = -1;
253                 ok = 0;
254                 return;
255         }
256
257         switch (fe_info.type) 
258         {
259         case FE_QPSK:
260                 m_type = feSatellite;
261                 break;
262         case FE_QAM:
263                 m_type = feCable;
264                 break;
265         case FE_OFDM:
266                 m_type = feTerrestrial;
267                 break;
268         default:
269                 eWarning("unknown frontend type.");
270                 ::close(m_fd);
271                 m_fd = -1;
272                 ok = 0;
273                 return;
274         }
275         eDebug("detected %s frontend", "satellite\0cable\0    terrestrial"+fe_info.type*10);
276         ok = 1;
277
278         m_sn = new eSocketNotifier(eApp, m_fd, eSocketNotifier::Read);
279         CONNECT(m_sn->activated, eDVBFrontend::feEvent);
280         m_sn->start();
281
282         m_timeout = new eTimer(eApp);
283         CONNECT(m_timeout->timeout, eDVBFrontend::timeout);
284
285         m_tuneTimer = new eTimer(eApp);
286         CONNECT(m_tuneTimer->timeout, eDVBFrontend::tuneLoop);
287
288         int entries = sizeof(m_data) / sizeof(int);
289         for (int i=0; i<entries; ++i)
290                 m_data[i] = -1;
291
292 //      m_data[7] = !m_fe;
293
294 //      eDebug("m_data[7] = %d %d", m_data[7], m_fe);
295
296         return;
297 }
298
299 eDVBFrontend::~eDVBFrontend()
300 {
301         if (m_fd >= 0)
302                 ::close(m_fd);
303         if (m_sn)
304                 delete m_sn;
305         if (m_timeout)
306                 delete m_timeout;
307 }
308
309 void eDVBFrontend::feEvent(int w)
310 {
311         while (1)
312         {
313 #if HAVE_DVB_API_VERSION < 3
314                 FrontendEvent event;
315 #else
316                 dvb_frontend_event event;
317 #endif
318                 int res;
319                 int state;
320                 res = ::ioctl(m_fd, FE_GET_EVENT, &event);
321                 
322                 if (res && (errno == EAGAIN))
323                         break;
324
325                 if (res)
326                 {
327                         eWarning("FE_GET_EVENT failed! %m");
328                         return;
329                 }
330                 
331                 if (w < 0)
332                         continue;
333
334 #if HAVE_DVB_API_VERSION < 3
335                 if (event.type == FE_COMPLETION_EV)
336 #else
337                 eDebug("(%d)fe event: status %x, inversion %s", m_fe, event.status, (event.parameters.inversion == INVERSION_ON) ? "on" : "off");
338                 if (event.status & FE_HAS_LOCK)
339 #endif
340                 {
341                         state = stateLock;
342                 } else
343                 {
344                         if (m_tuning)
345                                 state = stateTuning;
346                         else
347                         {
348                                 state = stateLostLock;
349
350                                 if (m_state != stateLostLock)
351                                         eDebug("FIXME: we lost lock, so we might have to retune.");
352                         }
353                 }
354                 if (m_state != state)
355                 {
356                         m_state = state;
357                         m_stateChanged(this);
358                 }
359         }
360 }
361
362 void eDVBFrontend::timeout()
363 {
364         int state;
365         if (m_state == stateTuning)
366         {
367                 state = stateFailed;
368                 eDebug("DVBFrontend: timeout");
369                 if (m_state != state)
370                 {
371                         m_state = state;
372                         m_stateChanged(this);
373                 }
374                 m_tuning = 0;
375         } else
376                 m_tuning = 0;
377 }
378
379 #ifndef FP_IOCTL_GET_ID
380 #define FP_IOCTL_GET_ID 0
381 #endif
382 int eDVBFrontend::readInputpower()
383 {
384         int power=m_fe;  // this is needed for read inputpower from the correct tuner !
385
386         // open front prozessor
387         int fp=::open("/dev/dbox/fp0", O_RDWR);
388         if (fp < 0)
389         {
390                 eDebug("couldn't open fp");
391                 return -1;
392         }
393         static bool old_fp = (::ioctl(fp, FP_IOCTL_GET_ID) < 0);
394         if ( ioctl( fp, old_fp ? 9 : 0x100, &power ) < 0 )
395         {
396                 eDebug("FP_IOCTL_GET_LNB_CURRENT failed (%m)");
397                 return -1;
398         }
399         ::close(fp);
400
401         return power;
402 }
403
404 bool eDVBFrontend::setSecSequencePos(int steps)
405 {
406         eDebug("set sequence pos %d", steps);
407         if (!steps)
408                 return false;
409         while( steps > 0 )
410         {
411                 if (m_sec_sequence.current() != m_sec_sequence.end())
412                         ++m_sec_sequence.current();
413                 --steps;
414         }
415         while( steps < 0 )
416         {
417                 if (m_sec_sequence.current() != m_sec_sequence.begin() && m_sec_sequence.current() != m_sec_sequence.end())
418                         --m_sec_sequence.current();
419                 ++steps;
420         }
421         return true;
422 }
423
424 void eDVBFrontend::tuneLoop()  // called by m_tuneTimer
425 {
426         int delay=0;
427         if ( m_sec_sequence && m_sec_sequence.current() != m_sec_sequence.end() )
428         {
429                 switch (m_sec_sequence.current()->cmd)
430                 {
431                         case eSecCommand::SLEEP:
432                                 delay = m_sec_sequence.current()++->msec;
433                                 eDebug("[SEC] sleep %dms", delay);
434                                 break;
435                         case eSecCommand::GOTO:
436                                 if ( !setSecSequencePos(m_sec_sequence.current()->steps) )
437                                         ++m_sec_sequence.current();
438                                 break;
439                         case eSecCommand::SET_VOLTAGE:
440                                 int voltage = m_sec_sequence.current()++->voltage;
441                                 eDebug("[SEC] setVoltage %d", voltage);
442                                 setVoltage(voltage);
443                                 break;
444                         case eSecCommand::SET_TONE:
445                                 eDebug("[SEC] setTone %d", m_sec_sequence.current()->tone);
446                                 setTone(m_sec_sequence.current()++->tone);
447                                 break;
448                         case eSecCommand::SEND_DISEQC:
449                                 sendDiseqc(m_sec_sequence.current()->diseqc);
450                                 eDebugNoNewLine("[SEC] sendDiseqc: ");
451                                 for (int i=0; i < m_sec_sequence.current()->diseqc.len; ++i)
452                                     eDebugNoNewLine("%02x", m_sec_sequence.current()->diseqc.data[i]);
453                                 eDebug("");
454                                 ++m_sec_sequence.current();
455                                 break;
456                         case eSecCommand::SEND_TONEBURST:
457                                 eDebug("[SEC] sendToneburst: %d", m_sec_sequence.current()->toneburst);
458                                 sendToneburst(m_sec_sequence.current()++->toneburst);
459                                 break;
460                         case eSecCommand::SET_FRONTEND:
461                                 eDebug("[SEC] setFrontend");
462                                 setFrontend();
463                                 ++m_sec_sequence.current();
464                                 break;
465                         case eSecCommand::MEASURE_IDLE_INPUTPOWER:
466                         {
467                                 int idx = m_sec_sequence.current()++->val;
468                                 if ( idx == 0 || idx == 1 )
469                                 {
470                                         m_idleInputpower[idx] = readInputpower();
471                                         eDebug("[SEC] idleInputpower[%d] is %d", idx, m_idleInputpower[idx]);
472                                 }
473                                 else
474                                         eDebug("[SEC] idleInputpower measure index(%d) out of bound !!!", idx);
475                                 break;
476                         }
477                         case eSecCommand::MEASURE_RUNNING_INPUTPOWER:
478                                 m_runningInputpower = readInputpower();
479                                 eDebug("[SEC] runningInputpower is %d", m_runningInputpower);
480                                 ++m_sec_sequence.current();
481                                 break;
482                         case eSecCommand::SET_TIMEOUT:
483                                 m_timeoutCount = m_sec_sequence.current()++->val;
484                                 eDebug("[SEC] set timeout %d", m_timeoutCount);
485                                 break;
486                         case eSecCommand::UPDATE_CURRENT_ROTORPARAMS:
487                                 m_data[5] = m_data[3];
488                                 m_data[6] = m_data[4];
489                                 eDebug("[SEC] update current rotorparams %d %04x %d", m_timeoutCount, m_data[5], m_data[6]);
490                                 ++m_sec_sequence.current();
491                                 break;
492                         case eSecCommand::IF_TIMEOUT_GOTO:
493                                 if (!m_timeoutCount)
494                                 {
495                                         eDebug("[SEC] rotor timout");
496                                         m_sec->setRotorMoving(false);
497                                         setSecSequencePos(m_sec_sequence.current()->steps);
498                                 }
499                                 else
500                                         ++m_sec_sequence.current();
501                                 break;
502                         case eSecCommand::SET_POWER_LIMITING_MODE:
503                         {
504                                 int fd = m_fe ?
505                                         ::open("/dev/i2c/1", O_RDWR) :
506                                         ::open("/dev/i2c/0", O_RDWR);
507
508                                 unsigned char data[2];
509                                 ::ioctl(fd, I2C_SLAVE_FORCE, 0x10 >> 1);
510                                 if(::read(fd, data, 1) != 1)
511                                         eDebug("[SEC] error read lnbp (%m)");
512                                 if ( m_sec_sequence.current()->mode == eSecCommand::modeStatic )
513                                 {
514                                         data[0] |= 0x90;  // enable static current limiting
515                                         eDebug("[SEC] set static current limiting");
516                                 }
517                                 else
518                                 {
519                                         data[0] &= ~0x90;  // enable dynamic current limiting
520                                         eDebug("[SEC] set dynamic current limiting");
521                                 }
522                                 if(::write(fd, data, 1) != 1)
523                                         eDebug("[SEC] error write lnbp (%m)");
524                                 ::close(fd);
525                                 ++m_sec_sequence.current();
526                                 break;
527                         }
528                         case eSecCommand::IF_IDLE_INPUTPOWER_AVAIL_GOTO:
529                                 if (m_idleInputpower[0] && m_idleInputpower[1] && setSecSequencePos(m_sec_sequence.current()->steps))
530                                         break;
531                                 ++m_sec_sequence.current();
532                                 break;
533                         case eSecCommand::IF_INPUTPOWER_DELTA_GOTO:
534                         {
535                                 int idleInputpower = m_idleInputpower[m_curVoltage == iDVBFrontend::voltage13 ? 0 : 1];
536                                 eSecCommand::rotor &cmd = m_sec_sequence.current()->measure;
537                                 const char *txt = cmd.direction ? "running" : "stopped";
538                                 eDebug("[SEC] waiting for rotor %s %d, idle %d, delta %d",
539                                         txt,
540                                         m_runningInputpower,
541                                         idleInputpower,
542                                         cmd.deltaA);
543                                 if ( (cmd.direction && abs(m_runningInputpower - idleInputpower) >= cmd.deltaA)
544                                         || (!cmd.direction && abs(m_runningInputpower - idleInputpower) <= cmd.deltaA) )
545                                 {
546                                         ++cmd.okcount;
547                                         eDebug("[SEC] rotor %s step %d ok", txt, cmd.okcount);
548                                         if ( cmd.okcount > 6 )
549                                         {
550                                                 m_sec->setRotorMoving(cmd.direction);
551                                                 eDebug("[SEC] rotor is %s", txt);
552                                                 if (setSecSequencePos(cmd.steps))
553                                                         break;
554                                         }
555                                 }
556                                 else
557                                 {
558                                         eDebug("[SEC] rotor not %s... reset counter.. increase timeout", txt);
559                                         --m_timeoutCount;
560                                         cmd.okcount=0;
561                                 }
562                                 ++m_sec_sequence.current();
563                                 break;
564                         }
565                         case eSecCommand::IF_VOLTAGE_GOTO:
566                         {
567                                 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
568                                 if ( compare.voltage == m_curVoltage && setSecSequencePos(compare.steps) )
569                                         break;
570                                 ++m_sec_sequence.current();
571                                 break;
572                         }
573                         default:
574                                 ++m_sec_sequence.current();
575                                 eDebug("[SEC] unhandled sec command");
576                 }
577                 m_tuneTimer->start(delay,true);
578         }
579 }
580
581 void eDVBFrontend::setFrontend()
582 {
583         eDebug("setting frontend..\n");
584         if (ioctl(m_fd, FE_SET_FRONTEND, &parm) == -1)
585         {
586                 perror("FE_SET_FRONTEND failed");
587                 return;
588         }
589
590         if (m_state != stateTuning)
591         {
592                 m_tuning = 1;
593                 m_state = stateTuning;
594                 m_stateChanged(this);
595         }
596         m_timeout->start(5000, 1); // 5 sec timeout. TODO: symbolrate dependent
597 }
598
599 RESULT eDVBFrontend::getFrontendType(int &t)
600 {
601         if (m_type == -1)
602                 return -ENODEV;
603         t = m_type;
604         return 0;
605 }
606
607 RESULT eDVBFrontend::tune(const iDVBFrontendParameters &where)
608 {
609         eDebug("(%d)tune", m_fe);
610
611         if (m_type == -1)
612                 return -ENODEV;
613
614         feEvent(-1);
615
616         m_sec_sequence.clear();
617
618         switch (m_type)
619         {
620         case feSatellite:
621         {
622                 int res;
623                 eDVBFrontendParametersSatellite feparm;
624                 if (where.getDVBS(feparm))
625                 {
626                         eDebug("no dvbs data!");
627                         return -EINVAL;
628                 }
629                 if (!m_sec)
630                 {
631                         eWarning("no SEC module active!");
632                         return -ENOENT;
633                 }
634                 
635                 res = m_sec->prepare(*this, parm, feparm, 1 << m_fe);
636                 if (res)
637                         return res;
638 #if HAVE_DVB_API_VERSION < 3
639                 eDebug("tuning to %d mhz", parm.Frequency/1000);
640 #else
641                 eDebug("tuning to %d mhz", parm.frequency/1000);
642 #endif
643                 break;
644         }
645         case feCable:
646         {
647 #if HAVE_DVB_API_VERSION >= 3
648                 eDVBFrontendParametersCable feparm;
649                 if (where.getDVBC(feparm))
650                         return -EINVAL;
651 #if HAVE_DVB_API_VERSION < 3
652                 parm.Frequency = feparm.frequency * 1000;
653                 parm.u.qam.SymbolRate = feparm.symbol_rate;
654 #else
655                 parm.frequency = feparm.frequency * 1000;
656                 parm.u.qam.symbol_rate = feparm.symbol_rate;
657 #endif
658                 fe_modulation_t mod;
659                 switch (feparm.modulation)
660                 {
661                 case eDVBFrontendParametersCable::Modulation::QAM16:
662                         mod = QAM_16;
663                         break;
664                 case eDVBFrontendParametersCable::Modulation::QAM32:
665                         mod = QAM_32;
666                         break;
667                 case eDVBFrontendParametersCable::Modulation::QAM64:
668                         mod = QAM_64;
669                         break;
670                 case eDVBFrontendParametersCable::Modulation::QAM128:
671                         mod = QAM_128;
672                         break;
673                 case eDVBFrontendParametersCable::Modulation::QAM256:
674                         mod = QAM_256;
675                         break;                  
676                 case eDVBFrontendParametersCable::Modulation::Auto:
677                         mod = QAM_AUTO;
678                         break;                  
679                 }
680 #if HAVE_DVB_API_VERSION < 3
681                 parm.u.qam.QAM = mod;
682 #else
683                 parm.u.qam.modulation = mod;
684 #endif
685                 switch (feparm.inversion)
686                 {               
687                 case eDVBFrontendParametersCable::Inversion::On:
688                         #if HAVE_DVB_API_VERSION < 3
689                         parm.Inversion =
690                         #else
691                         parm.inversion =
692                         #endif
693                                 INVERSION_ON;
694                         break;
695                 case eDVBFrontendParametersCable::Inversion::Off:
696                         #if HAVE_DVB_API_VERSION < 3
697                         parm.Inversion =
698                         #else
699                         parm.inversion =
700                         #endif
701                                 INVERSION_OFF;
702                         break;
703                 case eDVBFrontendParametersCable::Inversion::Unknown:
704                         #if HAVE_DVB_API_VERSION < 3
705                         parm.Inversion =
706                         #else
707                         parm.inversion =
708                         #endif
709                                 INVERSION_AUTO;
710                         break;
711                 }
712                 
713                 fe_code_rate_t fec_inner;
714                 switch (feparm.fec_inner)
715                 {               
716                 case eDVBFrontendParametersCable::FEC::fNone:
717                         fec_inner = FEC_NONE;
718                         break;
719                 case eDVBFrontendParametersCable::FEC::f1_2:
720                         fec_inner = FEC_1_2;
721                         break;
722                 case eDVBFrontendParametersCable::FEC::f2_3:
723                         fec_inner = FEC_2_3;
724                         break;
725                 case eDVBFrontendParametersCable::FEC::f3_4:
726                         fec_inner = FEC_3_4;
727                         break;
728                 case eDVBFrontendParametersCable::FEC::f4_5:
729                         fec_inner = FEC_4_5;
730                         break;
731                 case eDVBFrontendParametersCable::FEC::f5_6:
732                         fec_inner = FEC_5_6;
733                         break;
734                 case eDVBFrontendParametersCable::FEC::f6_7:
735                         fec_inner = FEC_6_7;
736                         break;
737                 case eDVBFrontendParametersCable::FEC::f7_8:
738                         fec_inner = FEC_7_8;
739                         break;
740                 case eDVBFrontendParametersCable::FEC::f8_9:
741                         fec_inner = FEC_8_9;
742                         break;
743                 case eDVBFrontendParametersCable::FEC::fAuto:
744                         fec_inner = FEC_AUTO;
745                         break;
746                 }
747 #if HAVE_DVB_API_VERSION < 3
748                 parm.u.qam.FEC_inner = fec_inner;
749 #else
750                 parm.u.qam.fec_inner = fec_inner;
751 #endif
752 #else
753                 eFatal("Old API not fully supported");
754 #endif // old api
755                 break;
756         }
757         case feTerrestrial:
758         {
759                 eDVBFrontendParametersTerrestrial feparm;
760                 if (where.getDVBT(feparm))
761                 {
762                         eDebug("no -T data");
763                         return -EINVAL;
764                 }
765 #if HAVE_DVB_API_VERSION < 3
766                 parm.Frequency = feparm.frequency;
767 #else
768                 parm.frequency = feparm.frequency;
769 #endif
770
771                 switch (feparm.bandwidth)
772                 {
773                 case eDVBFrontendParametersTerrestrial::Bandwidth::Bw8MHz:
774 #if HAVE_DVB_API_VERSION < 3
775                         parm.u.ofdm.bandWidth =
776 #else
777                         parm.u.ofdm.bandwidth =
778 #endif
779                                 BANDWIDTH_8_MHZ;
780                         break;
781                 case eDVBFrontendParametersTerrestrial::Bandwidth::Bw7MHz:
782 #if HAVE_DVB_API_VERSION < 3
783                         parm.u.ofdm.bandWidth =
784 #else
785                         parm.u.ofdm.bandwidth =
786 #endif
787                                 BANDWIDTH_7_MHZ;
788                         break;
789                 case eDVBFrontendParametersTerrestrial::Bandwidth::Bw6MHz:
790 #if HAVE_DVB_API_VERSION < 3
791                         parm.u.ofdm.bandWidth =
792 #else
793                         parm.u.ofdm.bandwidth =
794 #endif
795                                 BANDWIDTH_6_MHZ;
796                         break;
797                 case eDVBFrontendParametersTerrestrial::Bandwidth::BwAuto:
798 #if HAVE_DVB_API_VERSION < 3
799                         parm.u.ofdm.bandWidth =
800 #else
801                         parm.u.ofdm.bandwidth =
802 #endif
803                                 BANDWIDTH_AUTO;
804                         break;
805                 default:
806                         eWarning("invalid OFDM bandwith");
807                         return -EINVAL;
808                 }
809                 
810                 parm.u.ofdm.code_rate_HP = FEC_AUTO;
811                 parm.u.ofdm.code_rate_LP = FEC_AUTO;
812                 
813                 switch (feparm.modulation)
814                 {
815                 case eDVBFrontendParametersTerrestrial::Modulation::QPSK:
816                         parm.u.ofdm.constellation = QPSK;
817                         break;
818                 case eDVBFrontendParametersTerrestrial::Modulation::QAM16:
819                         parm.u.ofdm.constellation = QAM_16;
820                         break;
821                 case eDVBFrontendParametersTerrestrial::Modulation::Auto:
822                         parm.u.ofdm.constellation = QAM_AUTO;
823                         break;
824                 }
825                 
826                 switch (feparm.transmission_mode)
827                 {
828                 case eDVBFrontendParametersTerrestrial::TransmissionMode::TM2k:
829 #if HAVE_DVB_API_VERSION < 3
830                         parm.u.ofdm.TransmissionMode =
831 #else
832                         parm.u.ofdm.transmission_mode =
833 #endif
834                                 TRANSMISSION_MODE_2K;
835                         break;
836                 case eDVBFrontendParametersTerrestrial::TransmissionMode::TM8k:
837 #if HAVE_DVB_API_VERSION < 3
838                         parm.u.ofdm.TransmissionMode =
839 #else
840                         parm.u.ofdm.transmission_mode =
841 #endif
842                                 TRANSMISSION_MODE_8K;
843                         break;
844                 case eDVBFrontendParametersTerrestrial::TransmissionMode::TMAuto:
845 #if HAVE_DVB_API_VERSION < 3
846                         parm.u.ofdm.TransmissionMode =
847 #else
848                         parm.u.ofdm.transmission_mode =
849 #endif
850                                 TRANSMISSION_MODE_AUTO;
851                         break;
852                 }
853                 
854                 parm.u.ofdm.guard_interval = GUARD_INTERVAL_AUTO;
855                 parm.u.ofdm.hierarchy_information = HIERARCHY_AUTO;
856 #if HAVE_DVB_API_VERSION < 3
857                 parm.Inversion =
858 #else
859                 parm.inversion =
860 #endif
861                         INVERSION_AUTO;
862                 break;
863         }
864         }
865
866         m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
867         m_tuneTimer->start(0,true);
868         m_sec_sequence.current() = m_sec_sequence.begin();
869
870         return 0;
871 }
872
873 RESULT eDVBFrontend::connectStateChange(const Slot1<void,iDVBFrontend*> &stateChange, ePtr<eConnection> &connection)
874 {
875         connection = new eConnection(this, m_stateChanged.connect(stateChange));
876         return 0;
877 }
878
879 RESULT eDVBFrontend::setVoltage(int voltage)
880 {
881 #if HAVE_DVB_API_VERSION < 3
882         secVoltage vlt;
883 #else
884         fe_sec_voltage_t vlt;
885 #endif
886
887         m_curVoltage=voltage;
888         switch (voltage)
889         {
890         case voltageOff:
891                 vlt = SEC_VOLTAGE_OFF;
892                 break;
893         case voltage13:
894                 vlt = SEC_VOLTAGE_13;
895                 break;
896         case voltage18:
897                 vlt = SEC_VOLTAGE_18;
898                 break;
899         default:
900                 return -ENODEV;
901         }
902 #if HAVE_DVB_API_VERSION < 3
903         return ::ioctl(m_secfd, SEC_SET_VOLTAGE, vlt);
904 #else
905         return ::ioctl(m_fd, FE_SET_VOLTAGE, vlt);
906 #endif
907 }
908
909 RESULT eDVBFrontend::getState(int &state)
910 {
911         state = m_state;
912         return 0;
913 }
914
915 RESULT eDVBFrontend::setTone(int t)
916 {
917 #if HAVE_DVB_API_VERSION < 3
918         secToneMode_t tone;
919 #else
920         fe_sec_tone_mode_t tone;
921 #endif
922
923         switch (t)
924         {
925         case toneOn:
926                 tone = SEC_TONE_ON;
927                 break;
928         case toneOff:
929                 tone = SEC_TONE_OFF;
930                 break;
931         default:
932                 return -ENODEV;
933         }
934 #if HAVE_DVB_API_VERSION < 3    
935         return ::ioctl(m_secfd, SEC_SET_TONE, tone);
936 #else   
937         return ::ioctl(m_fd, FE_SET_TONE, tone);
938 #endif
939 }
940
941 #if HAVE_DVB_API_VERSION < 3 && !defined(SEC_DISEQC_SEND_MASTER_CMD)
942         #define SEC_DISEQC_SEND_MASTER_CMD _IOW('o', 97, struct secCommand *)
943 #endif
944
945 RESULT eDVBFrontend::sendDiseqc(const eDVBDiseqcCommand &diseqc)
946 {
947 #if HAVE_DVB_API_VERSION < 3
948         struct secCommand cmd;
949         cmd.type = SEC_CMDTYPE_DISEQC_RAW;
950         cmd.u.diseqc.cmdtype = diseqc.data[0];
951         cmd.u.diseqc.addr = diseqc.data[1];
952         cmd.u.diseqc.cmd = diseqc.data[2];
953         cmd.u.diseqc.numParams = diseqc.len-3;
954         memcpy(cmd.u.diseqc.params, diseqc.data+3, diseqc.len-3);
955         if (::ioctl(m_secfd, SEC_DISEQC_SEND_MASTER_CMD, &cmd))
956 #else
957         struct dvb_diseqc_master_cmd cmd;
958         memcpy(cmd.msg, diseqc.data, diseqc.len);
959         cmd.msg_len = diseqc.len;
960         if (::ioctl(m_fd, FE_DISEQC_SEND_MASTER_CMD, &cmd))
961 #endif
962                 return -EINVAL;
963         return 0;
964 }
965
966 #if HAVE_DVB_API_VERSION < 3 && !defined(SEC_DISEQC_SEND_BURST)
967         #define SEC_DISEQC_SEND_BURST _IO('o', 96)
968 #endif
969 RESULT eDVBFrontend::sendToneburst(int burst)
970 {
971 #if HAVE_DVB_API_VERSION < 3
972         secMiniCmd cmd = SEC_MINI_NONE;
973         if ( burst == eDVBSatelliteDiseqcParameters::A )
974                 cmd = SEC_MINI_A;
975         else if ( burst == eDVBSatelliteDiseqcParameters::B )
976                 cmd = SEC_MINI_B;
977         if (::ioctl(m_secfd, SEC_DISEQC_SEND_BURST, cmd))
978                 return -EINVAL;
979 #endif
980         return 0;
981 }
982
983 RESULT eDVBFrontend::setSEC(iDVBSatelliteEquipmentControl *sec)
984 {
985         m_sec = sec;
986         return 0;
987 }
988
989 RESULT eDVBFrontend::setSecSequence(const eSecCommandList &list)
990 {
991         m_sec_sequence = list;
992         return 0;
993 }
994
995 RESULT eDVBFrontend::getData(int num, int &data)
996 {
997         if ( num < (int)(sizeof(m_data)/sizeof(int)) )
998         {
999                 data = m_data[num];
1000                 return 0;
1001         }
1002         return -EINVAL;
1003 }
1004
1005 RESULT eDVBFrontend::setData(int num, int val)
1006 {
1007         if ( num < (int)(sizeof(m_data)/sizeof(int)) )
1008         {
1009                 m_data[num] = val;
1010                 return 0;
1011         }
1012         return -EINVAL;
1013 }
1014
1015 int eDVBFrontend::isCompatibleWith(ePtr<iDVBFrontendParameters> &feparm)
1016 {
1017         int type;
1018         if (feparm->getSystem(type) || type != m_type)
1019                 return 0;
1020
1021         if (m_type == eDVBFrontend::feSatellite)
1022         {
1023                 ASSERT(m_sec);
1024                 eDVBFrontendParametersSatellite sat_parm;
1025                 ASSERT(!feparm->getDVBS(sat_parm));
1026                 return m_sec->canTune(sat_parm, this, 1 << m_fe);
1027         }
1028         return 1;
1029 }