set frontend state to stateTuning when tuneloop starts.. ( to avoid false lostlock...
[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 #else
224         sprintf(m_filename, "/dev/dvb/adapter%d/frontend%d", adap, fe);
225 #endif
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                                 m_data[0] = m_data[1] = m_data[2] = -1; // reset diseqc
404                         }
405                 }
406                 if (m_state != state)
407                 {
408                         m_state = state;
409                         m_stateChanged(this);
410                 }
411         }
412 }
413
414 void eDVBFrontend::timeout()
415 {
416         int state;
417         if (m_state == stateTuning)
418         {
419                 state = stateFailed;
420                 eDebug("DVBFrontend: timeout");
421                 if (m_state != state)
422                 {
423                         m_state = state;
424                         m_stateChanged(this);
425                 }
426                 m_tuning = 0;
427         } else
428                 m_tuning = 0;
429 }
430
431 int eDVBFrontend::readFrontendData(int type)
432 {
433         switch(type)
434         {
435                 case bitErrorRate:
436                 {
437                         uint32_t ber=0;
438                         if (ioctl(m_fd, FE_READ_BER, &ber) < 0 && errno != ERANGE)
439                                 eDebug("FE_READ_BER failed (%m)");
440                         return ber;
441                 }
442                 case signalPower:
443                 {
444                         uint16_t snr=0;
445                         if (ioctl(m_fd, FE_READ_SNR, &snr) < 0 && errno != ERANGE)
446                                 eDebug("FE_READ_SNR failed (%m)");
447                         return snr;
448                 }
449                 case signalQuality:
450                 {
451                         uint16_t strength=0;
452                         if (ioctl(m_fd, FE_READ_SIGNAL_STRENGTH, &strength) < 0 && errno != ERANGE)
453                                 eDebug("FE_READ_SIGNAL_STRENGTH failed (%m)");
454                         return strength;
455                 }
456         }
457         return 0;
458 }
459
460 #ifndef FP_IOCTL_GET_ID
461 #define FP_IOCTL_GET_ID 0
462 #endif
463 int eDVBFrontend::readInputpower()
464 {
465         int power=m_fe;  // this is needed for read inputpower from the correct tuner !
466
467         // open front prozessor
468         int fp=::open("/dev/dbox/fp0", O_RDWR);
469         if (fp < 0)
470         {
471                 eDebug("couldn't open fp");
472                 return -1;
473         }
474         static bool old_fp = (::ioctl(fp, FP_IOCTL_GET_ID) < 0);
475         if ( ioctl( fp, old_fp ? 9 : 0x100, &power ) < 0 )
476         {
477                 eDebug("FP_IOCTL_GET_LNB_CURRENT failed (%m)");
478                 return -1;
479         }
480         ::close(fp);
481
482         return power;
483 }
484
485 bool eDVBFrontend::setSecSequencePos(int steps)
486 {
487         eDebug("set sequence pos %d", steps);
488         if (!steps)
489                 return false;
490         while( steps > 0 )
491         {
492                 if (m_sec_sequence.current() != m_sec_sequence.end())
493                         ++m_sec_sequence.current();
494                 --steps;
495         }
496         while( steps < 0 )
497         {
498                 if (m_sec_sequence.current() != m_sec_sequence.begin() && m_sec_sequence.current() != m_sec_sequence.end())
499                         --m_sec_sequence.current();
500                 ++steps;
501         }
502         return true;
503 }
504
505 void eDVBFrontend::tuneLoop()  // called by m_tuneTimer
506 {
507         int delay=0;
508         if ( m_sec_sequence && m_sec_sequence.current() != m_sec_sequence.end() )
509         {
510                 switch (m_sec_sequence.current()->cmd)
511                 {
512                         case eSecCommand::SLEEP:
513                                 delay = m_sec_sequence.current()++->msec;
514                                 eDebug("[SEC] sleep %dms", delay);
515                                 break;
516                         case eSecCommand::GOTO:
517                                 if ( !setSecSequencePos(m_sec_sequence.current()->steps) )
518                                         ++m_sec_sequence.current();
519                                 break;
520                         case eSecCommand::SET_VOLTAGE:
521                         {
522                                 int voltage = m_sec_sequence.current()++->voltage;
523                                 eDebug("[SEC] setVoltage %d", voltage);
524                                 setVoltage(voltage);
525                                 break;
526                         }
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         m_timeout->start(5000, 1); // 5 sec timeout. TODO: symbolrate dependent
673 }
674
675 RESULT eDVBFrontend::getFrontendType(int &t)
676 {
677         if (m_type == -1)
678                 return -ENODEV;
679         t = m_type;
680         return 0;
681 }
682
683 RESULT eDVBFrontend::tune(const iDVBFrontendParameters &where)
684 {
685         eDebug("(%d)tune", m_fe);
686
687         if (m_type == -1)
688                 return -ENODEV;
689
690         feEvent(-1);
691
692         m_sec_sequence.clear();
693
694         switch (m_type)
695         {
696         case feSatellite:
697         {
698                 int res;
699                 eDVBFrontendParametersSatellite feparm;
700                 if (where.getDVBS(feparm))
701                 {
702                         eDebug("no dvbs data!");
703                         return -EINVAL;
704                 }
705                 if (!m_sec)
706                 {
707                         eWarning("no SEC module active!");
708                         return -ENOENT;
709                 }
710                 
711                 res = m_sec->prepare(*this, parm, feparm, 1 << m_fe);
712                 if (res)
713                         return res;
714 #if HAVE_DVB_API_VERSION < 3
715                 eDebug("tuning to %d mhz", parm.Frequency/1000);
716 #else
717                 eDebug("tuning to %d mhz", parm.frequency/1000);
718 #endif
719                 break;
720         }
721         case feCable:
722         {
723 #if HAVE_DVB_API_VERSION >= 3
724                 eDVBFrontendParametersCable feparm;
725                 if (where.getDVBC(feparm))
726                         return -EINVAL;
727 #if HAVE_DVB_API_VERSION < 3
728                 parm.Frequency = feparm.frequency * 1000;
729                 parm.u.qam.SymbolRate = feparm.symbol_rate;
730 #else
731                 parm.frequency = feparm.frequency * 1000;
732                 parm.u.qam.symbol_rate = feparm.symbol_rate;
733 #endif
734                 fe_modulation_t mod=QAM_AUTO;
735                 switch (feparm.modulation)
736                 {
737                 case eDVBFrontendParametersCable::Modulation::QAM16:
738                         mod = QAM_16;
739                         break;
740                 case eDVBFrontendParametersCable::Modulation::QAM32:
741                         mod = QAM_32;
742                         break;
743                 case eDVBFrontendParametersCable::Modulation::QAM64:
744                         mod = QAM_64;
745                         break;
746                 case eDVBFrontendParametersCable::Modulation::QAM128:
747                         mod = QAM_128;
748                         break;
749                 case eDVBFrontendParametersCable::Modulation::QAM256:
750                         mod = QAM_256;
751                         break;                  
752                 case eDVBFrontendParametersCable::Modulation::Auto:
753                         mod = QAM_AUTO;
754                         break;                  
755                 }
756 #if HAVE_DVB_API_VERSION < 3
757                 parm.u.qam.QAM = mod;
758 #else
759                 parm.u.qam.modulation = mod;
760 #endif
761                 switch (feparm.inversion)
762                 {               
763                 case eDVBFrontendParametersCable::Inversion::On:
764                         #if HAVE_DVB_API_VERSION < 3
765                         parm.Inversion =
766                         #else
767                         parm.inversion =
768                         #endif
769                                 INVERSION_ON;
770                         break;
771                 case eDVBFrontendParametersCable::Inversion::Off:
772                         #if HAVE_DVB_API_VERSION < 3
773                         parm.Inversion =
774                         #else
775                         parm.inversion =
776                         #endif
777                                 INVERSION_OFF;
778                         break;
779                 case eDVBFrontendParametersCable::Inversion::Unknown:
780                         #if HAVE_DVB_API_VERSION < 3
781                         parm.Inversion =
782                         #else
783                         parm.inversion =
784                         #endif
785                                 INVERSION_AUTO;
786                         break;
787                 }
788                 
789                 fe_code_rate_t fec_inner=FEC_AUTO;
790                 switch (feparm.fec_inner)
791                 {               
792                 case eDVBFrontendParametersCable::FEC::fNone:
793                         fec_inner = FEC_NONE;
794                         break;
795                 case eDVBFrontendParametersCable::FEC::f1_2:
796                         fec_inner = FEC_1_2;
797                         break;
798                 case eDVBFrontendParametersCable::FEC::f2_3:
799                         fec_inner = FEC_2_3;
800                         break;
801                 case eDVBFrontendParametersCable::FEC::f3_4:
802                         fec_inner = FEC_3_4;
803                         break;
804                 case eDVBFrontendParametersCable::FEC::f4_5:
805                         fec_inner = FEC_4_5;
806                         break;
807                 case eDVBFrontendParametersCable::FEC::f5_6:
808                         fec_inner = FEC_5_6;
809                         break;
810                 case eDVBFrontendParametersCable::FEC::f6_7:
811                         fec_inner = FEC_6_7;
812                         break;
813                 case eDVBFrontendParametersCable::FEC::f7_8:
814                         fec_inner = FEC_7_8;
815                         break;
816                 case eDVBFrontendParametersCable::FEC::f8_9:
817                         fec_inner = FEC_8_9;
818                         break;
819                 case eDVBFrontendParametersCable::FEC::fAuto:
820                         fec_inner = FEC_AUTO;
821                         break;
822                 }
823 #if HAVE_DVB_API_VERSION < 3
824                 parm.u.qam.FEC_inner = fec_inner;
825 #else
826                 parm.u.qam.fec_inner = fec_inner;
827 #endif
828 #else
829                 eFatal("Old API not fully supported");
830 #endif // old api
831                 break;
832         }
833         case feTerrestrial:
834         {
835                 eDVBFrontendParametersTerrestrial feparm;
836                 if (where.getDVBT(feparm))
837                 {
838                         eDebug("no -T data");
839                         return -EINVAL;
840                 }
841 #if HAVE_DVB_API_VERSION < 3
842                 parm.Frequency = feparm.frequency;
843 #else
844                 parm.frequency = feparm.frequency;
845 #endif
846
847                 switch (feparm.bandwidth)
848                 {
849                 case eDVBFrontendParametersTerrestrial::Bandwidth::Bw8MHz:
850 #if HAVE_DVB_API_VERSION < 3
851                         parm.u.ofdm.bandWidth =
852 #else
853                         parm.u.ofdm.bandwidth =
854 #endif
855                                 BANDWIDTH_8_MHZ;
856                         break;
857                 case eDVBFrontendParametersTerrestrial::Bandwidth::Bw7MHz:
858 #if HAVE_DVB_API_VERSION < 3
859                         parm.u.ofdm.bandWidth =
860 #else
861                         parm.u.ofdm.bandwidth =
862 #endif
863                                 BANDWIDTH_7_MHZ;
864                         break;
865                 case eDVBFrontendParametersTerrestrial::Bandwidth::Bw6MHz:
866 #if HAVE_DVB_API_VERSION < 3
867                         parm.u.ofdm.bandWidth =
868 #else
869                         parm.u.ofdm.bandwidth =
870 #endif
871                                 BANDWIDTH_6_MHZ;
872                         break;
873                 case eDVBFrontendParametersTerrestrial::Bandwidth::BwAuto:
874 #if HAVE_DVB_API_VERSION < 3
875                         parm.u.ofdm.bandWidth =
876 #else
877                         parm.u.ofdm.bandwidth =
878 #endif
879                                 BANDWIDTH_AUTO;
880                         break;
881                 default:
882                         eWarning("invalid OFDM bandwith");
883                         return -EINVAL;
884                 }
885                 
886                 parm.u.ofdm.code_rate_HP = FEC_AUTO;
887                 parm.u.ofdm.code_rate_LP = FEC_AUTO;
888                 
889                 switch (feparm.modulation)
890                 {
891                 case eDVBFrontendParametersTerrestrial::Modulation::QPSK:
892                         parm.u.ofdm.constellation = QPSK;
893                         break;
894                 case eDVBFrontendParametersTerrestrial::Modulation::QAM16:
895                         parm.u.ofdm.constellation = QAM_16;
896                         break;
897                 case eDVBFrontendParametersTerrestrial::Modulation::Auto:
898                         parm.u.ofdm.constellation = QAM_AUTO;
899                         break;
900                 }
901                 
902                 switch (feparm.transmission_mode)
903                 {
904                 case eDVBFrontendParametersTerrestrial::TransmissionMode::TM2k:
905 #if HAVE_DVB_API_VERSION < 3
906                         parm.u.ofdm.TransmissionMode =
907 #else
908                         parm.u.ofdm.transmission_mode =
909 #endif
910                                 TRANSMISSION_MODE_2K;
911                         break;
912                 case eDVBFrontendParametersTerrestrial::TransmissionMode::TM8k:
913 #if HAVE_DVB_API_VERSION < 3
914                         parm.u.ofdm.TransmissionMode =
915 #else
916                         parm.u.ofdm.transmission_mode =
917 #endif
918                                 TRANSMISSION_MODE_8K;
919                         break;
920                 case eDVBFrontendParametersTerrestrial::TransmissionMode::TMAuto:
921 #if HAVE_DVB_API_VERSION < 3
922                         parm.u.ofdm.TransmissionMode =
923 #else
924                         parm.u.ofdm.transmission_mode =
925 #endif
926                                 TRANSMISSION_MODE_AUTO;
927                         break;
928                 }
929                 
930                 parm.u.ofdm.guard_interval = GUARD_INTERVAL_AUTO;
931                 parm.u.ofdm.hierarchy_information = HIERARCHY_AUTO;
932 #if HAVE_DVB_API_VERSION < 3
933                 parm.Inversion =
934 #else
935                 parm.inversion =
936 #endif
937                         INVERSION_AUTO;
938                 break;
939         }
940         }
941
942         m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
943         m_tuneTimer->start(0,true);
944         m_sec_sequence.current() = m_sec_sequence.begin();
945
946         if (m_state != stateTuning)
947         {
948                 m_tuning = 1;
949                 m_state = stateTuning;
950                 m_stateChanged(this);
951         }
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 }