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