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