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