add support for dm8000 onboard tuner linking
[enigma2.git] / lib / dvb / frontend.cpp
1 #include <lib/dvb/dvb.h>
2 #include <lib/base/eerror.h>
3 #include <lib/base/nconfig.h> // access to python config
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 parm_frequency parm.Frequency
22 #define parm_inversion parm.Inversion
23 #define parm_u_qpsk_symbol_rate parm.u.qpsk.SymbolRate
24 #define parm_u_qpsk_fec_inner parm.u.qpsk.FEC_inner
25 #define parm_u_qam_symbol_rate parm.u.qam.SymbolRate
26 #define parm_u_qam_fec_inner parm.u.qam.FEC_inner
27 #define parm_u_qam_modulation parm.u.qam.QAM
28 #define parm_u_ofdm_bandwidth parm.u.ofdm.bandWidth
29 #define parm_u_ofdm_code_rate_LP parm.u.ofdm.LP_CodeRate
30 #define parm_u_ofdm_code_rate_HP parm.u.ofdm.HP_CodeRate
31 #define parm_u_ofdm_constellation parm.u.ofdm.Constellation
32 #define parm_u_ofdm_transmission_mode parm.u.ofdm.TransmissionMode
33 #define parm_u_ofdm_guard_interval parm.u.ofdm.guardInterval
34 #define parm_u_ofdm_hierarchy_information parm.u.ofdm.HierarchyInformation
35 #else
36 #include <linux/dvb/frontend.h>
37 #define parm_frequency parm.frequency
38 #define parm_inversion parm.inversion
39 #define parm_u_qpsk_symbol_rate parm.u.qpsk.symbol_rate
40 #define parm_u_qpsk_fec_inner parm.u.qpsk.fec_inner
41 #define parm_u_qam_symbol_rate parm.u.qam.symbol_rate
42 #define parm_u_qam_fec_inner parm.u.qam.fec_inner
43 #define parm_u_qam_modulation parm.u.qam.modulation
44 #define parm_u_ofdm_bandwidth parm.u.ofdm.bandwidth
45 #define parm_u_ofdm_code_rate_LP parm.u.ofdm.code_rate_LP
46 #define parm_u_ofdm_code_rate_HP parm.u.ofdm.code_rate_HP
47 #define parm_u_ofdm_constellation parm.u.ofdm.constellation
48 #define parm_u_ofdm_transmission_mode parm.u.ofdm.transmission_mode
49 #define parm_u_ofdm_guard_interval parm.u.ofdm.guard_interval
50 #define parm_u_ofdm_hierarchy_information parm.u.ofdm.hierarchy_information
51 #ifdef FEC_9_10
52         #warning "FEC_9_10 already exist in dvb api ... it seems it is now ready for DVB-S2"
53 #else
54         #define FEC_S2_QPSK_1_2 (fe_code_rate_t)(FEC_AUTO+1)
55         #define FEC_S2_QPSK_2_3 (fe_code_rate_t)(FEC_S2_QPSK_1_2+1)
56         #define FEC_S2_QPSK_3_4 (fe_code_rate_t)(FEC_S2_QPSK_2_3+1)
57         #define FEC_S2_QPSK_5_6 (fe_code_rate_t)(FEC_S2_QPSK_3_4+1)
58         #define FEC_S2_QPSK_7_8 (fe_code_rate_t)(FEC_S2_QPSK_5_6+1)
59         #define FEC_S2_QPSK_8_9 (fe_code_rate_t)(FEC_S2_QPSK_7_8+1)
60         #define FEC_S2_QPSK_3_5 (fe_code_rate_t)(FEC_S2_QPSK_8_9+1)
61         #define FEC_S2_QPSK_4_5 (fe_code_rate_t)(FEC_S2_QPSK_3_5+1)
62         #define FEC_S2_QPSK_9_10 (fe_code_rate_t)(FEC_S2_QPSK_4_5+1)
63         #define FEC_S2_8PSK_1_2 (fe_code_rate_t)(FEC_S2_QPSK_9_10+1)
64         #define FEC_S2_8PSK_2_3 (fe_code_rate_t)(FEC_S2_8PSK_1_2+1)
65         #define FEC_S2_8PSK_3_4 (fe_code_rate_t)(FEC_S2_8PSK_2_3+1)
66         #define FEC_S2_8PSK_5_6 (fe_code_rate_t)(FEC_S2_8PSK_3_4+1)
67         #define FEC_S2_8PSK_7_8 (fe_code_rate_t)(FEC_S2_8PSK_5_6+1)
68         #define FEC_S2_8PSK_8_9 (fe_code_rate_t)(FEC_S2_8PSK_7_8+1)
69         #define FEC_S2_8PSK_3_5 (fe_code_rate_t)(FEC_S2_8PSK_8_9+1)
70         #define FEC_S2_8PSK_4_5 (fe_code_rate_t)(FEC_S2_8PSK_3_5+1)
71         #define FEC_S2_8PSK_9_10 (fe_code_rate_t)(FEC_S2_8PSK_4_5+1)
72 #endif
73 #endif
74
75 #include <dvbsi++/satellite_delivery_system_descriptor.h>
76 #include <dvbsi++/cable_delivery_system_descriptor.h>
77 #include <dvbsi++/terrestrial_delivery_system_descriptor.h>
78
79 void eDVBDiseqcCommand::setCommandString(const char *str)
80 {
81         if (!str)
82                 return;
83         len=0;
84         int slen = strlen(str);
85         if (slen % 2)
86         {
87                 eDebug("invalid diseqc command string length (not 2 byte aligned)");
88                 return;
89         }
90         if (slen > MAX_DISEQC_LENGTH*2)
91         {
92                 eDebug("invalid diseqc command string length (string is to long)");
93                 return;
94         }
95         unsigned char val=0;
96         for (int i=0; i < slen; ++i)
97         {
98                 unsigned char c = str[i];
99                 switch(c)
100                 {
101                         case '0' ... '9': c-=48; break;
102                         case 'a' ... 'f': c-=87; break;
103                         case 'A' ... 'F': c-=55; break;
104                         default:
105                                 eDebug("invalid character in hex string..ignore complete diseqc command !");
106                                 return;
107                 }
108                 if ( i % 2 )
109                 {
110                         val |= c;
111                         data[i/2] = val;
112                 }
113                 else
114                         val = c << 4;
115         }
116         len = slen/2;
117 }
118
119 void eDVBFrontendParametersSatellite::set(const SatelliteDeliverySystemDescriptor &descriptor)
120 {
121         frequency    = descriptor.getFrequency() * 10;
122         symbol_rate  = descriptor.getSymbolRate() * 100;
123         polarisation = descriptor.getPolarization();
124         fec = descriptor.getFecInner();
125         if ( fec != FEC::fNone && fec > FEC::f9_10 )
126                 fec = FEC::fAuto;
127         inversion = Inversion::Unknown;
128         orbital_position  = ((descriptor.getOrbitalPosition() >> 12) & 0xF) * 1000;
129         orbital_position += ((descriptor.getOrbitalPosition() >> 8) & 0xF) * 100;
130         orbital_position += ((descriptor.getOrbitalPosition() >> 4) & 0xF) * 10;
131         orbital_position += ((descriptor.getOrbitalPosition()) & 0xF);
132         if (orbital_position && (!descriptor.getWestEastFlag()))
133                 orbital_position = 3600 - orbital_position;
134         system = descriptor.getModulationSystem();
135         modulation = descriptor.getModulation();
136         if (system == System::DVB_S && modulation == Modulation::M8PSK)
137         {
138                 eDebug("satellite_delivery_descriptor non valid modulation type.. force QPSK");
139                 modulation=QPSK;
140         }
141         roll_off = descriptor.getRollOff();
142         if (system == System::DVB_S2)
143         {
144                 eDebug("SAT DVB-S2 freq %d, %s, pos %d, sr %d, fec %d, modulation %d, roll_off %d",
145                         frequency,
146                         polarisation ? "hor" : "vert",
147                         orbital_position,
148                         symbol_rate, fec,
149                         modulation,
150                         roll_off);
151         }
152         else
153         {
154                 eDebug("SAT DVB-S freq %d, %s, pos %d, sr %d, fec %d",
155                         frequency,
156                         polarisation ? "hor" : "vert",
157                         orbital_position,
158                         symbol_rate, fec);
159         }
160 }
161
162 void eDVBFrontendParametersCable::set(const CableDeliverySystemDescriptor &descriptor)
163 {
164         frequency = descriptor.getFrequency() / 10;
165         symbol_rate = descriptor.getSymbolRate() * 100;
166         fec_inner = descriptor.getFecInner();
167         if ( fec_inner == 0xF )
168                 fec_inner = FEC::fNone;
169         modulation = descriptor.getModulation();
170         if ( modulation > 0x5 )
171                 modulation = Modulation::Auto;
172         inversion = Inversion::Unknown;
173         eDebug("Cable freq %d, mod %d, sr %d, fec %d",
174                 frequency,
175                 modulation, symbol_rate, fec_inner);
176 }
177
178 void eDVBFrontendParametersTerrestrial::set(const TerrestrialDeliverySystemDescriptor &descriptor)
179 {
180         frequency = descriptor.getCentreFrequency() * 10;
181         bandwidth = descriptor.getBandwidth();
182         if ( bandwidth > 2 ) // 5Mhz forced to auto
183                 bandwidth = Bandwidth::BwAuto;
184         code_rate_HP = descriptor.getCodeRateHpStream();
185         if (code_rate_HP > 4)
186                 code_rate_HP = FEC::fAuto;
187         code_rate_LP = descriptor.getCodeRateLpStream();
188         if (code_rate_LP > 4)
189                 code_rate_LP = FEC::fAuto;
190         transmission_mode = descriptor.getTransmissionMode();
191         if (transmission_mode > 1) // TM4k forced to auto
192                 transmission_mode = TransmissionMode::TMAuto;
193         guard_interval = descriptor.getGuardInterval();
194         if (guard_interval > 3)
195                 guard_interval = GuardInterval::GI_Auto;
196         hierarchy = descriptor.getHierarchyInformation()&3;
197         modulation = descriptor.getConstellation();
198         if (modulation > 2)
199                 modulation = Modulation::Auto;
200         inversion = Inversion::Unknown;
201         eDebug("Terr freq %d, bw %d, cr_hp %d, cr_lp %d, tm_mode %d, guard %d, hierarchy %d, const %d",
202                 frequency, bandwidth, code_rate_HP, code_rate_LP, transmission_mode,
203                 guard_interval, hierarchy, modulation);
204 }
205
206 eDVBFrontendParameters::eDVBFrontendParameters(): m_type(-1)
207 {
208 }
209
210 DEFINE_REF(eDVBFrontendParameters);
211
212 RESULT eDVBFrontendParameters::getSystem(int &t) const
213 {
214         if (m_type == -1)
215                 return -1;
216         t = m_type;
217         return 0;
218 }
219
220 RESULT eDVBFrontendParameters::getDVBS(eDVBFrontendParametersSatellite &p) const
221 {
222         if (m_type != iDVBFrontend::feSatellite)
223                 return -1;
224         p = sat;
225         return 0;
226 }
227
228 RESULT eDVBFrontendParameters::getDVBC(eDVBFrontendParametersCable &p) const
229 {
230         if (m_type != iDVBFrontend::feCable)
231                 return -1;
232         p = cable;
233         return 0;
234 }
235
236 RESULT eDVBFrontendParameters::getDVBT(eDVBFrontendParametersTerrestrial &p) const
237 {
238         if (m_type != iDVBFrontend::feTerrestrial)
239                 return -1;
240         p = terrestrial;
241         return 0;
242 }
243
244 RESULT eDVBFrontendParameters::setDVBS(const eDVBFrontendParametersSatellite &p, bool no_rotor_command_on_tune)
245 {
246         sat = p;
247         sat.no_rotor_command_on_tune = no_rotor_command_on_tune;
248         m_type = iDVBFrontend::feSatellite;
249         return 0;
250 }
251
252 RESULT eDVBFrontendParameters::setDVBC(const eDVBFrontendParametersCable &p)
253 {
254         cable = p;
255         m_type = iDVBFrontend::feCable;
256         return 0;
257 }
258
259 RESULT eDVBFrontendParameters::setDVBT(const eDVBFrontendParametersTerrestrial &p)
260 {
261         terrestrial = p;
262         m_type = iDVBFrontend::feTerrestrial;
263         return 0;
264 }
265
266 RESULT eDVBFrontendParameters::calculateDifference(const iDVBFrontendParameters *parm, int &diff, bool exact) const
267 {
268         if (!parm)
269                 return -1;
270         int type;
271         if (parm->getSystem(type))
272                 return -1;
273         if (type != m_type)
274         {
275                 diff = 1<<30; // big difference
276                 return 0;
277         }
278
279         switch (type)
280         {
281         case iDVBFrontend::feSatellite:
282         {
283                 eDVBFrontendParametersSatellite osat;
284                 if (parm->getDVBS(osat))
285                         return -2;
286
287                 if (sat.orbital_position != osat.orbital_position)
288                         diff = 1<<29;
289                 else if (sat.polarisation != osat.polarisation)
290                         diff = 1<<28;
291                 else if (exact && sat.fec != osat.fec && sat.fec != eDVBFrontendParametersSatellite::FEC::fAuto && osat.fec != eDVBFrontendParametersSatellite::FEC::fAuto)
292                         diff = 1<<27;
293                 else if (exact && sat.modulation != osat.modulation && sat.modulation != eDVBFrontendParametersSatellite::Modulation::Auto && osat.modulation != eDVBFrontendParametersSatellite::Modulation::Auto)
294                         diff = 1<<27;
295                 else
296                 {
297                         diff = abs(sat.frequency - osat.frequency);
298                         diff += abs(sat.symbol_rate - osat.symbol_rate);
299                 }
300                 return 0;
301         }
302         case iDVBFrontend::feCable:
303                 eDVBFrontendParametersCable ocable;
304                 if (parm->getDVBC(ocable))
305                         return -2;
306
307                 if (exact && cable.modulation != ocable.modulation
308                         && cable.modulation != eDVBFrontendParametersCable::Modulation::Auto
309                         && ocable.modulation != eDVBFrontendParametersCable::Modulation::Auto)
310                         diff = 1 << 29;
311                 else if (exact && cable.fec_inner != ocable.fec_inner && cable.fec_inner != eDVBFrontendParametersCable::FEC::fAuto && ocable.fec_inner != eDVBFrontendParametersCable::FEC::fAuto)
312                         diff = 1 << 27;
313                 else
314                 {
315                         diff = abs(cable.frequency - ocable.frequency);
316                         diff += abs(cable.symbol_rate - ocable.symbol_rate);
317                 }
318                 return 0;
319         case iDVBFrontend::feTerrestrial:
320                 eDVBFrontendParametersTerrestrial oterrestrial;
321                 if (parm->getDVBT(oterrestrial))
322                         return -2;
323
324
325                 if (exact && oterrestrial.bandwidth != terrestrial.bandwidth &&
326                         oterrestrial.bandwidth != eDVBFrontendParametersTerrestrial::Bandwidth::BwAuto &&
327                         terrestrial.bandwidth != eDVBFrontendParametersTerrestrial::Bandwidth::BwAuto)
328                         diff = 1 << 30;
329                 else if (exact && oterrestrial.modulation != terrestrial.modulation &&
330                         oterrestrial.modulation != eDVBFrontendParametersTerrestrial::Modulation::Auto &&
331                         terrestrial.modulation != eDVBFrontendParametersTerrestrial::Modulation::Auto)
332                         diff = 1 << 30;
333                 else if (exact && oterrestrial.transmission_mode != terrestrial.transmission_mode &&
334                         oterrestrial.transmission_mode != eDVBFrontendParametersTerrestrial::TransmissionMode::TMAuto &&
335                         terrestrial.transmission_mode != eDVBFrontendParametersTerrestrial::TransmissionMode::TMAuto)
336                         diff = 1 << 30;
337                 else if (exact && oterrestrial.guard_interval != terrestrial.guard_interval &&
338                         oterrestrial.guard_interval != eDVBFrontendParametersTerrestrial::GuardInterval::GI_Auto &&
339                         terrestrial.guard_interval != eDVBFrontendParametersTerrestrial::GuardInterval::GI_Auto)
340                         diff = 1 << 30;
341                 else if (exact && oterrestrial.hierarchy != terrestrial.hierarchy &&
342                         oterrestrial.hierarchy != eDVBFrontendParametersTerrestrial::Hierarchy::HAuto &&
343                         terrestrial.hierarchy != eDVBFrontendParametersTerrestrial::Hierarchy::HAuto)
344                         diff = 1 << 30;
345                 else if (exact && oterrestrial.code_rate_LP != terrestrial.code_rate_LP &&
346                         oterrestrial.code_rate_LP != eDVBFrontendParametersTerrestrial::FEC::fAuto &&
347                         terrestrial.code_rate_LP != eDVBFrontendParametersTerrestrial::FEC::fAuto)
348                         diff = 1 << 30;
349                 else if (exact && oterrestrial.code_rate_HP != terrestrial.code_rate_HP &&
350                         oterrestrial.code_rate_HP != eDVBFrontendParametersTerrestrial::FEC::fAuto &&
351                         terrestrial.code_rate_HP != eDVBFrontendParametersTerrestrial::FEC::fAuto)
352                         diff = 1 << 30;
353                 else
354                         diff = abs(terrestrial.frequency - oterrestrial.frequency);
355                 return 0;
356         default:
357                 return -1;
358         }
359         return 0;
360 }
361
362 RESULT eDVBFrontendParameters::getHash(unsigned long &hash) const
363 {
364         switch (m_type)
365         {
366         case iDVBFrontend::feSatellite:
367         {
368                 hash = (sat.orbital_position << 16);
369                 hash |= ((sat.frequency/1000)&0xFFFF)|((sat.polarisation&1) << 15);
370                 return 0;
371         }
372         case iDVBFrontend::feCable:
373                 hash = 0xFFFF0000;
374                 return 0;
375         case iDVBFrontend::feTerrestrial:
376                 hash = 0xEEEE0000;
377                 return 0;
378         default:
379                 return -1;
380         }
381 }
382
383 DEFINE_REF(eDVBFrontend);
384
385 int eDVBFrontend::PriorityOrder=0;
386
387 eDVBFrontend::eDVBFrontend(int adap, int fe, int &ok)
388         :m_enabled(false), m_type(-1), m_dvbid(fe), m_slotid(fe)
389         ,m_fd(-1), m_need_rotor_workaround(false), m_sn(0), m_timeout(0), m_tuneTimer(0)
390 #if HAVE_DVB_API_VERSION < 3
391         ,m_secfd(-1)
392 #endif
393 {
394 #if HAVE_DVB_API_VERSION < 3
395         sprintf(m_filename, "/dev/dvb/card%d/frontend%d", adap, fe);
396         sprintf(m_sec_filename, "/dev/dvb/card%d/sec%d", adap, fe);
397 #else
398         sprintf(m_filename, "/dev/dvb/adapter%d/frontend%d", adap, fe);
399 #endif
400         m_timeout = new eTimer(eApp);
401         CONNECT(m_timeout->timeout, eDVBFrontend::timeout);
402
403         m_tuneTimer = new eTimer(eApp);
404         CONNECT(m_tuneTimer->timeout, eDVBFrontend::tuneLoop);
405
406         for (int i=0; i<eDVBFrontend::NUM_DATA_ENTRIES; ++i)
407                 m_data[i] = -1;
408
409         m_idleInputpower[0]=m_idleInputpower[1]=0;
410
411         ok = !openFrontend();
412         closeFrontend();
413 }
414
415 int eDVBFrontend::openFrontend()
416 {
417         if (m_sn)
418                 return -1;  // already opened
419
420         m_state=0;
421         m_tuning=0;
422
423 #if HAVE_DVB_API_VERSION < 3
424         FrontendInfo fe_info;
425 #else
426         dvb_frontend_info fe_info;
427 #endif
428         eDebug("opening frontend %d", m_dvbid);
429         if (m_fd < 0)
430         {
431                 m_fd = ::open(m_filename, O_RDWR|O_NONBLOCK);
432                 if (m_fd < 0)
433                 {
434                         eWarning("failed! (%s) %m", m_filename);
435                         return -1;
436                 }
437         }
438         else
439                 eWarning("frontend %d already opened", m_dvbid);
440         if (m_type == -1)
441         {
442                 if (::ioctl(m_fd, FE_GET_INFO, &fe_info) < 0)
443                 {
444                         eWarning("ioctl FE_GET_INFO failed");
445                         ::close(m_fd);
446                         m_fd = -1;
447                         return -1;
448                 }
449
450                 switch (fe_info.type)
451                 {
452                 case FE_QPSK:
453                         m_type = iDVBFrontend::feSatellite;
454                         break;
455                 case FE_QAM:
456                         m_type = iDVBFrontend::feCable;
457                         break;
458                 case FE_OFDM:
459                         m_type = iDVBFrontend::feTerrestrial;
460                         break;
461                 default:
462                         eWarning("unknown frontend type.");
463                         ::close(m_fd);
464                         m_fd = -1;
465                         return -1;
466                 }
467                 eDebug("detected %s frontend", "satellite\0cable\0    terrestrial"+fe_info.type*10);
468         }
469
470 #if HAVE_DVB_API_VERSION < 3
471         if (m_type == iDVBFrontend::feSatellite)
472         {
473                         if (m_secfd < 0)
474                         {
475                                 m_secfd = ::open(m_sec_filename, O_RDWR);
476                                 if (m_secfd < 0)
477                                 {
478                                         eWarning("failed! (%s) %m", m_sec_filename);
479                                         ::close(m_fd);
480                                         m_fd=-1;
481                                         return -1;
482                                 }
483                         }
484                         else
485                                 eWarning("sec %d already opened", m_dvbid);
486         }
487 #endif
488
489         setTone(iDVBFrontend::toneOff);
490         setVoltage(iDVBFrontend::voltageOff);
491
492         m_sn = new eSocketNotifier(eApp, m_fd, eSocketNotifier::Read);
493         CONNECT(m_sn->activated, eDVBFrontend::feEvent);
494
495         return 0;
496 }
497
498 int eDVBFrontend::closeFrontend()
499 {
500         eDVBRegisteredFrontend *linked_fe = (eDVBRegisteredFrontend*)m_data[LINKED_NEXT_PTR];
501         while (linked_fe != (eDVBRegisteredFrontend*)-1)
502         {
503                 if (linked_fe->m_inuse)
504                 {
505                         eDebug("dont close frontend %d until the linked frontend %d in slot %d is still in use",
506                                 m_dvbid, linked_fe->m_frontend->getDVBID(), linked_fe->m_frontend->getSlotID());
507                         return -1;
508                 }
509                 linked_fe->m_frontend->getData(LINKED_NEXT_PTR, (int&)linked_fe);
510         }
511         if (m_fd >= 0)
512         {
513                 eDebug("close frontend %d", m_dvbid);
514                 m_tuneTimer->stop();
515                 setTone(iDVBFrontend::toneOff);
516                 setVoltage(iDVBFrontend::voltageOff);
517                 if (m_sec)
518                         m_sec->setRotorMoving(false);
519                 if (!::close(m_fd))
520                         m_fd=-1;
521                 else
522                         eWarning("couldnt close frontend %d", m_dvbid);
523                 m_data[CSW] = m_data[UCSW] = m_data[TONEBURST] = -1;
524         }
525 #if HAVE_DVB_API_VERSION < 3
526         if (m_secfd >= 0)
527         {
528                 if (!::close(m_secfd))
529                         m_secfd=-1;
530                 else
531                         eWarning("couldnt close sec %d", m_dvbid);
532         }
533 #endif
534         delete m_sn;
535         m_sn=0;
536
537         return 0;
538 }
539
540 eDVBFrontend::~eDVBFrontend()
541 {
542         closeFrontend();
543         delete m_timeout;
544         delete m_tuneTimer;
545 }
546
547 void eDVBFrontend::feEvent(int w)
548 {
549         while (1)
550         {
551 #if HAVE_DVB_API_VERSION < 3
552                 FrontendEvent event;
553 #else
554                 dvb_frontend_event event;
555 #endif
556                 int res;
557                 int state;
558                 res = ::ioctl(m_fd, FE_GET_EVENT, &event);
559
560                 if (res && (errno == EAGAIN))
561                         break;
562
563                 if (res)
564                 {
565                         eWarning("FE_GET_EVENT failed! %m");
566                         return;
567                 }
568
569                 if (w < 0)
570                         continue;
571
572 #if HAVE_DVB_API_VERSION < 3
573                 if (event.type == FE_COMPLETION_EV)
574 #else
575                 eDebug("(%d)fe event: status %x, inversion %s", m_dvbid, event.status, (event.parameters.inversion == INVERSION_ON) ? "on" : "off");
576                 if (event.status & FE_HAS_LOCK)
577 #endif
578                 {
579                         state = stateLock;
580                 } else
581                 {
582                         if (m_tuning)
583                                 state = stateTuning;
584                         else
585                         {
586                                 eDebug("stateLostLock");
587                                 state = stateLostLock;
588                                 m_data[CSW] = m_data[UCSW] = m_data[TONEBURST] = -1; // reset diseqc
589                         }
590                 }
591                 if (m_state != state)
592                 {
593                         m_state = state;
594                         m_stateChanged(this);
595                 }
596         }
597 }
598
599 void eDVBFrontend::timeout()
600 {
601         m_tuning = 0;
602         if (m_state == stateTuning)
603         {
604                 m_state = stateFailed;
605                 m_stateChanged(this);
606         }
607 }
608
609 int eDVBFrontend::readFrontendData(int type)
610 {
611         switch(type)
612         {
613                 case bitErrorRate:
614                 {
615                         uint32_t ber=0;
616                         if (ioctl(m_fd, FE_READ_BER, &ber) < 0 && errno != ERANGE)
617                                 eDebug("FE_READ_BER failed (%m)");
618                         return ber;
619                 }
620                 case signalQuality:
621                 {
622                         uint16_t snr=0;
623                         if (ioctl(m_fd, FE_READ_SNR, &snr) < 0 && errno != ERANGE)
624                                 eDebug("FE_READ_SNR failed (%m)");
625                         return snr;
626                 }
627                 case signalQualitydB: /* this will move into the driver */
628                 {
629                         uint16_t snr=0;
630                         if (ioctl(m_fd, FE_READ_SNR, &snr) < 0 && errno != ERANGE)
631                                 eDebug("FE_READ_SNR failed (%m)");
632                         if (!strcmp(m_description, "BCM4501 (internal)"))
633                         {
634                                 unsigned int SDS_SNRE = snr << 16;
635
636                                 static float SNR_COEFF[6] = {
637                                         100.0 / 4194304.0,
638                                         -7136.0 / 4194304.0,
639                                         197418.0 / 4194304.0,
640                                         -2602183.0 / 4194304.0,
641                                         20377212.0 / 4194304.0,
642                                         -37791203.0 / 4194304.0,
643                                 };
644                         
645                                 float fval1, fval2, snr_in_db;
646                                 int i;
647                                 fval1 = 12.44714 - (2.0 * log10(SDS_SNRE / 256.0));
648                                 fval2 = pow(10.0, fval1)-1;
649                                 fval1 = 10.0 * log10(fval2);
650                         
651                                 if (fval1 < 10.0)
652                                 {
653                                         fval2 = SNR_COEFF[0];
654                                         for (i=0; i<6; ++i)
655                                         {
656                                                 fval2 *= fval1;
657                                                 fval2 += SNR_COEFF[i];
658                                         }
659                                         fval1 = fval2;
660                                 }
661                                 snr_in_db = fval1;
662                         
663                                 return (int)(snr_in_db * 100.0);
664                         }
665                         else if (!strcmp(m_description, "Alps BSBE1 702A") ||  // some frontends with STV0299
666                                 !strcmp(m_description, "Alps -S") ||
667                                 !strcmp(m_description, "Philips -S") ||
668                                 !strcmp(m_description, "LG -S") )
669                         {
670                                 float snr_in_db=(snr-39075)/1764.7;
671                                 return (int)(snr_in_db * 100.0);
672                         } else if (!strcmp(m_description, "Alps BSBE2"))
673                         {
674                                 return (int)((snr >> 7) * 10.0);
675                         } /* else
676                                 eDebug("no SNR dB calculation for frontendtype %s yet", m_description); */
677                         return 0x12345678;
678                 }
679                 case signalPower:
680                 {
681                         uint16_t strength=0;
682                         if (ioctl(m_fd, FE_READ_SIGNAL_STRENGTH, &strength) < 0 && errno != ERANGE)
683                                 eDebug("FE_READ_SIGNAL_STRENGTH failed (%m)");
684                         return strength;
685                 }
686                 case locked:
687                 {
688 #if HAVE_DVB_API_VERSION < 3
689                         FrontendStatus status=0;
690 #else
691                         fe_status_t status;
692 #endif
693                         if ( ioctl(m_fd, FE_READ_STATUS, &status) < 0 && errno != ERANGE )
694                                 eDebug("FE_READ_STATUS failed (%m)");
695                         return !!(status&FE_HAS_LOCK);
696                 }
697                 case synced:
698                 {
699 #if HAVE_DVB_API_VERSION < 3
700                         FrontendStatus status=0;
701 #else
702                         fe_status_t status;
703 #endif
704                         if ( ioctl(m_fd, FE_READ_STATUS, &status) < 0 && errno != ERANGE )
705                                 eDebug("FE_READ_STATUS failed (%m)");
706                         return !!(status&FE_HAS_SYNC);
707                 }
708                 case frontendNumber:
709                         return m_slotid;
710         }
711         return 0;
712 }
713
714 void PutToDict(ePyObject &dict, const char*key, long value)
715 {
716         ePyObject item = PyInt_FromLong(value);
717         if (item)
718         {
719                 if (PyDict_SetItemString(dict, key, item))
720                         eDebug("put %s to dict failed", key);
721                 Py_DECREF(item);
722         }
723         else
724                 eDebug("could not create PyObject for %s", key);
725 }
726
727 void PutToDict(ePyObject &dict, const char*key, ePyObject item)
728 {
729         if (item)
730         {
731                 if (PyDict_SetItemString(dict, key, item))
732                         eDebug("put %s to dict failed", key);
733                 Py_DECREF(item);
734         }
735         else
736                 eDebug("invalid PyObject for %s", key);
737 }
738
739 void PutToDict(ePyObject &dict, const char*key, const char *value)
740 {
741         ePyObject item = PyString_FromString(value);
742         if (item)
743         {
744                 if (PyDict_SetItemString(dict, key, item))
745                         eDebug("put %s to dict failed", key);
746                 Py_DECREF(item);
747         }
748         else
749                 eDebug("could not create PyObject for %s", key);
750 }
751
752 void fillDictWithSatelliteData(ePyObject dict, const FRONTENDPARAMETERS &parm, eDVBFrontend *fe)
753 {
754         int freq_offset=0;
755         int csw=0;
756         const char *tmp=0;
757         fe->getData(eDVBFrontend::CSW, csw);
758         fe->getData(eDVBFrontend::FREQ_OFFSET, freq_offset);
759         int frequency = parm_frequency + freq_offset;
760         PutToDict(dict, "frequency", frequency);
761         PutToDict(dict, "symbol_rate", parm_u_qpsk_symbol_rate);
762         switch(parm_u_qpsk_fec_inner)
763         {
764         case FEC_1_2:
765                 tmp = "FEC_1_2";
766                 break;
767         case FEC_2_3:
768                 tmp = "FEC_2_3";
769                 break;
770         case FEC_3_4:
771                 tmp = "FEC_3_4";
772                 break;
773         case FEC_5_6:
774                 tmp = "FEC_5_6";
775                 break;
776         case FEC_7_8:
777                 tmp = "FEC_7_8";
778                 break;
779         case FEC_NONE:
780                 tmp = "FEC_NONE";
781         default:
782         case FEC_AUTO:
783                 tmp = "FEC_AUTO";
784                 break;
785 #if HAVE_DVB_API_VERSION >=3
786         case FEC_S2_8PSK_1_2:
787         case FEC_S2_QPSK_1_2:
788                 tmp = "FEC_1_2";
789                 break;
790         case FEC_S2_8PSK_2_3:
791         case FEC_S2_QPSK_2_3:
792                 tmp = "FEC_2_3";
793                 break;
794         case FEC_S2_8PSK_3_4:
795         case FEC_S2_QPSK_3_4:
796                 tmp = "FEC_3_4";
797                 break;
798         case FEC_S2_8PSK_5_6:
799         case FEC_S2_QPSK_5_6:
800                 tmp = "FEC_5_6";
801                 break;
802         case FEC_S2_8PSK_7_8:
803         case FEC_S2_QPSK_7_8:
804                 tmp = "FEC_7_8";
805                 break;
806         case FEC_S2_8PSK_8_9:
807         case FEC_S2_QPSK_8_9:
808                 tmp = "FEC_8_9";
809                 break;
810         case FEC_S2_8PSK_3_5:
811         case FEC_S2_QPSK_3_5:
812                 tmp = "FEC_3_5";
813                 break;
814         case FEC_S2_8PSK_4_5:
815         case FEC_S2_QPSK_4_5:
816                 tmp = "FEC_4_5";
817                 break;
818         case FEC_S2_8PSK_9_10:
819         case FEC_S2_QPSK_9_10:
820                 tmp = "FEC_9_10";
821                 break;
822 #endif
823         }
824 #if HAVE_DVB_API_VERSION >=3
825         PutToDict(dict, "modulation",
826                 parm_u_qpsk_fec_inner > FEC_S2_QPSK_9_10 ? "8PSK": "QPSK" );
827 #else
828         PutToDict(dict, "modulation", "QPSK" );
829 #endif
830         PutToDict(dict, "fec_inner", tmp);
831         tmp = parm_u_qpsk_fec_inner > FEC_AUTO ?
832                 "DVB-S2" : "DVB-S";
833         PutToDict(dict, "system", tmp);
834 }
835
836 void fillDictWithCableData(ePyObject dict, const FRONTENDPARAMETERS &parm)
837 {
838         const char *tmp=0;
839         PutToDict(dict, "frequency", parm_frequency/1000);
840         PutToDict(dict, "symbol_rate", parm_u_qam_symbol_rate);
841         switch(parm_u_qam_fec_inner)
842         {
843         case FEC_NONE:
844                 tmp = "FEC_NONE";
845                 break;
846         case FEC_1_2:
847                 tmp = "FEC_1_2";
848                 break;
849         case FEC_2_3:
850                 tmp = "FEC_2_3";
851                 break;
852         case FEC_3_4:
853                 tmp = "FEC_3_4";
854                 break;
855         case FEC_5_6:
856                 tmp = "FEC_5_6";
857                 break;
858         case FEC_7_8:
859                 tmp = "FEC_7_8";
860                 break;
861 #if HAVE_DVB_API_VERSION >= 3
862         case FEC_8_9:
863                 tmp = "FEC_8_9";
864                 break;
865 #endif
866         default:
867         case FEC_AUTO:
868                 tmp = "FEC_AUTO";
869                 break;
870         }
871         PutToDict(dict, "fec_inner", tmp);
872         switch(parm_u_qam_modulation)
873         {
874         case QAM_16:
875                 tmp = "QAM_16";
876                 break;
877         case QAM_32:
878                 tmp = "QAM_32";
879                 break;
880         case QAM_64:
881                 tmp = "QAM_64";
882                 break;
883         case QAM_128:
884                 tmp = "QAM_128";
885                 break;
886         case QAM_256:
887                 tmp = "QAM_256";
888                 break;
889         default:
890         case QAM_AUTO:
891                 tmp = "QAM_AUTO";
892                 break;
893         }
894         PutToDict(dict, "modulation", tmp);
895 }
896
897 void fillDictWithTerrestrialData(ePyObject dict, const FRONTENDPARAMETERS &parm)
898 {
899         const char *tmp=0;
900         PutToDict(dict, "frequency", parm_frequency);
901         switch (parm_u_ofdm_bandwidth)
902         {
903         case BANDWIDTH_8_MHZ:
904                 tmp = "BANDWIDTH_8_MHZ";
905                 break;
906         case BANDWIDTH_7_MHZ:
907                 tmp = "BANDWIDTH_7_MHZ";
908                 break;
909         case BANDWIDTH_6_MHZ:
910                 tmp = "BANDWIDTH_6_MHZ";
911                 break;
912         default:
913         case BANDWIDTH_AUTO:
914                 tmp = "BANDWIDTH_AUTO";
915                 break;
916         }
917         PutToDict(dict, "bandwidth", tmp);
918         switch (parm_u_ofdm_code_rate_LP)
919         {
920         case FEC_1_2:
921                 tmp = "FEC_1_2";
922                 break;
923         case FEC_2_3:
924                 tmp = "FEC_2_3";
925                 break;
926         case FEC_3_4:
927                 tmp = "FEC_3_4";
928                 break;
929         case FEC_5_6:
930                 tmp = "FEC_5_6";
931                 break;
932         case FEC_7_8:
933                 tmp = "FEC_7_8";
934                 break;
935         default:
936         case FEC_AUTO:
937                 tmp = "FEC_AUTO";
938                 break;
939         }
940         PutToDict(dict, "code_rate_lp", tmp);
941         switch (parm_u_ofdm_code_rate_HP)
942         {
943         case FEC_1_2:
944                 tmp = "FEC_1_2";
945                 break;
946         case FEC_2_3:
947                 tmp = "FEC_2_3";
948                 break;
949         case FEC_3_4:
950                 tmp = "FEC_3_4";
951                 break;
952         case FEC_5_6:
953                 tmp = "FEC_5_6";
954                 break;
955         case FEC_7_8:
956                 tmp = "FEC_7_8";
957                 break;
958         default:
959         case FEC_AUTO:
960                 tmp = "FEC_AUTO";
961                 break;
962         }
963         PutToDict(dict, "code_rate_hp", tmp);
964         switch (parm_u_ofdm_constellation)
965         {
966         case QPSK:
967                 tmp = "QPSK";
968                 break;
969         case QAM_16:
970                 tmp = "QAM_16";
971                 break;
972         case QAM_64:
973                 tmp = "QAM_64";
974                 break;
975         default:
976         case QAM_AUTO:
977                 tmp = "QAM_AUTO";
978                 break;
979         }
980         PutToDict(dict, "constellation", tmp);
981         switch (parm_u_ofdm_transmission_mode)
982         {
983         case TRANSMISSION_MODE_2K:
984                 tmp = "TRANSMISSION_MODE_2K";
985                 break;
986         case TRANSMISSION_MODE_8K:
987                 tmp = "TRANSMISSION_MODE_8K";
988                 break;
989         default:
990         case TRANSMISSION_MODE_AUTO:
991                 tmp = "TRANSMISSION_MODE_AUTO";
992                 break;
993         }
994         PutToDict(dict, "transmission_mode", tmp);
995         switch (parm_u_ofdm_guard_interval)
996         {
997                 case GUARD_INTERVAL_1_32:
998                         tmp = "GUARD_INTERVAL_1_32";
999                         break;
1000                 case GUARD_INTERVAL_1_16:
1001                         tmp = "GUARD_INTERVAL_1_16";
1002                         break;
1003                 case GUARD_INTERVAL_1_8:
1004                         tmp = "GUARD_INTERVAL_1_8";
1005                         break;
1006                 case GUARD_INTERVAL_1_4:
1007                         tmp = "GUARD_INTERVAL_1_4";
1008                         break;
1009                 default:
1010                 case GUARD_INTERVAL_AUTO:
1011                         tmp = "GUARD_INTERVAL_AUTO";
1012                         break;
1013         }
1014         PutToDict(dict, "guard_interval", tmp);
1015         switch (parm_u_ofdm_hierarchy_information)
1016         {
1017                 case HIERARCHY_NONE:
1018                         tmp = "HIERARCHY_NONE";
1019                         break;
1020                 case HIERARCHY_1:
1021                         tmp = "HIERARCHY_1";
1022                         break;
1023                 case HIERARCHY_2:
1024                         tmp = "HIERARCHY_2";
1025                         break;
1026                 case HIERARCHY_4:
1027                         tmp = "HIERARCHY_4";
1028                         break;
1029                 default:
1030                 case HIERARCHY_AUTO:
1031                         tmp = "HIERARCHY_AUTO";
1032                         break;
1033         }
1034         PutToDict(dict, "hierarchy_information", tmp);
1035 }
1036
1037 void eDVBFrontend::getFrontendStatus(ePyObject dest)
1038 {
1039         if (dest && PyDict_Check(dest))
1040         {
1041                 const char *tmp = "UNKNOWN";
1042                 switch(m_state)
1043                 {
1044                         case stateIdle:
1045                                 tmp="IDLE";
1046                                 break;
1047                         case stateTuning:
1048                                 tmp="TUNING";
1049                                 break;
1050                         case stateFailed:
1051                                 tmp="FAILED";
1052                                 break;
1053                         case stateLock:
1054                                 tmp="LOCKED";
1055                                 break;
1056                         case stateLostLock:
1057                                 tmp="LOSTLOCK";
1058                                 break;
1059                         default:
1060                                 break;
1061                 }
1062                 PutToDict(dest, "tuner_state", tmp);
1063                 PutToDict(dest, "tuner_locked", readFrontendData(locked));
1064                 PutToDict(dest, "tuner_synced", readFrontendData(synced));
1065                 PutToDict(dest, "tuner_bit_error_rate", readFrontendData(bitErrorRate));
1066                 PutToDict(dest, "tuner_signal_quality", readFrontendData(signalQuality));
1067                 int sigQualitydB = readFrontendData(signalQualitydB);
1068                 if (sigQualitydB == 0x12345678) // not support yet
1069                 {
1070                         ePyObject obj=Py_None;
1071                         Py_INCREF(obj);
1072                         PutToDict(dest, "tuner_signal_quality_db", obj);
1073                 }
1074                 else
1075                         PutToDict(dest, "tuner_signal_quality_db", sigQualitydB);
1076                 PutToDict(dest, "tuner_signal_power", readFrontendData(signalPower));
1077         }
1078 }
1079
1080 void eDVBFrontend::getTransponderData(ePyObject dest, bool original)
1081 {
1082         if (m_fd != -1 && dest && PyDict_Check(dest))
1083         {
1084                 switch(m_type)
1085                 {
1086                         case feSatellite:
1087                         case feCable:
1088                         case feTerrestrial:
1089                         {
1090                                 FRONTENDPARAMETERS front;
1091                                 if (!original && ioctl(m_fd, FE_GET_FRONTEND, &front)<0)
1092                                         eDebug("FE_GET_FRONTEND (%m)");
1093                                 else
1094                                 {
1095                                         const FRONTENDPARAMETERS &parm = original ? this->parm : front;
1096                                         const char *tmp = "INVERSION_AUTO";
1097                                         switch(parm_inversion)
1098                                         {
1099                                                 case INVERSION_ON:
1100                                                         tmp = "INVERSION_ON";
1101                                                         break;
1102                                                 case INVERSION_OFF:
1103                                                         tmp = "INVERSION_OFF";
1104                                                         break;
1105                                                 default:
1106                                                         break;
1107                                         }
1108                                         if (tmp)
1109                                                 PutToDict(dest, "inversion", tmp);
1110
1111                                         switch(m_type)
1112                                         {
1113                                                 case feSatellite:
1114                                                         fillDictWithSatelliteData(dest, original?parm:front, this);
1115                                                         break;
1116                                                 case feCable:
1117                                                         fillDictWithCableData(dest, original?parm:front);
1118                                                         break;
1119                                                 case feTerrestrial:
1120                                                         fillDictWithTerrestrialData(dest, original?parm:front);
1121                                                         break;
1122                                         }
1123                                 }
1124                         }
1125                         default:
1126                                 break;
1127                 }
1128         }
1129 }
1130
1131 void eDVBFrontend::getFrontendData(ePyObject dest)
1132 {
1133         if (dest && PyDict_Check(dest))
1134         {
1135                 const char *tmp=0;
1136                 PutToDict(dest, "tuner_number", m_dvbid);
1137                 switch(m_type)
1138                 {
1139                         case feSatellite:
1140                                 tmp = "DVB-S";
1141                                 break;
1142                         case feCable:
1143                                 tmp = "DVB-C";
1144                                 break;
1145                         case feTerrestrial:
1146                                 tmp = "DVB-T";
1147                                 break;
1148                         default:
1149                                 tmp = "UNKNOWN";
1150                                 break;
1151                 }
1152                 PutToDict(dest, "tuner_type", tmp);
1153         }
1154 }
1155
1156 #ifndef FP_IOCTL_GET_ID
1157 #define FP_IOCTL_GET_ID 0
1158 #endif
1159 int eDVBFrontend::readInputpower()
1160 {
1161         int power=m_slotid;  // this is needed for read inputpower from the correct tuner !
1162
1163         // open front prozessor
1164         int fp=::open("/dev/dbox/fp0", O_RDWR);
1165         if (fp < 0)
1166         {
1167                 eDebug("couldn't open fp");
1168                 return -1;
1169         }
1170         static bool old_fp = (::ioctl(fp, FP_IOCTL_GET_ID) < 0);
1171         if ( ioctl( fp, old_fp ? 9 : 0x100, &power ) < 0 )
1172         {
1173                 eDebug("FP_IOCTL_GET_LNB_CURRENT failed (%m)");
1174                 return -1;
1175         }
1176         ::close(fp);
1177
1178         return power;
1179 }
1180
1181 bool eDVBFrontend::setSecSequencePos(int steps)
1182 {
1183         eDebug("set sequence pos %d", steps);
1184         if (!steps)
1185                 return false;
1186         while( steps > 0 )
1187         {
1188                 if (m_sec_sequence.current() != m_sec_sequence.end())
1189                         ++m_sec_sequence.current();
1190                 --steps;
1191         }
1192         while( steps < 0 )
1193         {
1194                 if (m_sec_sequence.current() != m_sec_sequence.begin() && m_sec_sequence.current() != m_sec_sequence.end())
1195                         --m_sec_sequence.current();
1196                 ++steps;
1197         }
1198         return true;
1199 }
1200
1201 void eDVBFrontend::setRotorData(int pos, int cmd)
1202 {
1203         m_data[ROTOR_CMD] = cmd;
1204         m_data[ROTOR_POS] = pos;
1205         if ( m_data[SATPOS_DEPENDS_PTR] != -1 )
1206         {
1207                 eDVBRegisteredFrontend *satpos_depends_to_fe = (eDVBRegisteredFrontend*) m_data[SATPOS_DEPENDS_PTR];
1208                 satpos_depends_to_fe->m_frontend->m_data[ROTOR_CMD] = cmd;
1209                 satpos_depends_to_fe->m_frontend->m_data[ROTOR_POS] = pos;
1210         }
1211         else
1212         {
1213                 eDVBRegisteredFrontend *next = (eDVBRegisteredFrontend *)m_data[LINKED_NEXT_PTR];
1214                 while ( (int)next != -1 )
1215                 {
1216                         next->m_frontend->m_data[ROTOR_CMD] = cmd;
1217                         next->m_frontend->m_data[ROTOR_POS] = pos;
1218                         next = (eDVBRegisteredFrontend *)next->m_frontend->m_data[LINKED_NEXT_PTR];
1219                 }
1220                 eDVBRegisteredFrontend *prev = (eDVBRegisteredFrontend *)m_data[LINKED_PREV_PTR];
1221                 while ( (int)prev != -1 )
1222                 {
1223                         prev->m_frontend->m_data[ROTOR_CMD] = cmd;
1224                         prev->m_frontend->m_data[ROTOR_POS] = pos;
1225                         prev = (eDVBRegisteredFrontend *)prev->m_frontend->m_data[LINKED_PREV_PTR];
1226                 }
1227         }
1228 }
1229
1230 void eDVBFrontend::tuneLoop()  // called by m_tuneTimer
1231 {
1232         int delay=0;
1233         if ( m_sec_sequence && m_sec_sequence.current() != m_sec_sequence.end() )
1234         {
1235 //              eDebug("tuneLoop %d\n", m_sec_sequence.current()->cmd);
1236                 switch (m_sec_sequence.current()->cmd)
1237                 {
1238                         case eSecCommand::SLEEP:
1239                                 delay = m_sec_sequence.current()++->msec;
1240                                 eDebug("[SEC] sleep %dms", delay);
1241                                 break;
1242                         case eSecCommand::GOTO:
1243                                 if ( !setSecSequencePos(m_sec_sequence.current()->steps) )
1244                                         ++m_sec_sequence.current();
1245                                 break;
1246                         case eSecCommand::SET_VOLTAGE:
1247                         {
1248                                 int voltage = m_sec_sequence.current()++->voltage;
1249                                 eDebug("[SEC] setVoltage %d", voltage);
1250                                 setVoltage(voltage);
1251                                 break;
1252                         }
1253                         case eSecCommand::IF_VOLTAGE_GOTO:
1254                         {
1255                                 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1256                                 if ( compare.voltage == m_data[CUR_VOLTAGE] && setSecSequencePos(compare.steps) )
1257                                         break;
1258                                 ++m_sec_sequence.current();
1259                                 break;
1260                         }
1261                         case eSecCommand::IF_NOT_VOLTAGE_GOTO:
1262                         {
1263                                 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1264                                 if ( compare.voltage != m_data[CUR_VOLTAGE] && setSecSequencePos(compare.steps) )
1265                                         break;
1266                                 ++m_sec_sequence.current();
1267                                 break;
1268                         }
1269                         case eSecCommand::IF_TONE_GOTO:
1270                         {
1271                                 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1272                                 if ( compare.tone == m_data[CUR_TONE] && setSecSequencePos(compare.steps) )
1273                                         break;
1274                                 ++m_sec_sequence.current();
1275                                 break;
1276                         }
1277                         case eSecCommand::IF_NOT_TONE_GOTO:
1278                         {
1279                                 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1280                                 if ( compare.tone != m_data[CUR_TONE] && setSecSequencePos(compare.steps) )
1281                                         break;
1282                                 ++m_sec_sequence.current();
1283                                 break;
1284                         }
1285                         case eSecCommand::SET_TONE:
1286                                 eDebug("[SEC] setTone %d", m_sec_sequence.current()->tone);
1287                                 setTone(m_sec_sequence.current()++->tone);
1288                                 break;
1289                         case eSecCommand::SEND_DISEQC:
1290                                 sendDiseqc(m_sec_sequence.current()->diseqc);
1291                                 eDebugNoNewLine("[SEC] sendDiseqc: ");
1292                                 for (int i=0; i < m_sec_sequence.current()->diseqc.len; ++i)
1293                                     eDebugNoNewLine("%02x", m_sec_sequence.current()->diseqc.data[i]);
1294                                 eDebug("");
1295                                 ++m_sec_sequence.current();
1296                                 break;
1297                         case eSecCommand::SEND_TONEBURST:
1298                                 eDebug("[SEC] sendToneburst: %d", m_sec_sequence.current()->toneburst);
1299                                 sendToneburst(m_sec_sequence.current()++->toneburst);
1300                                 break;
1301                         case eSecCommand::SET_FRONTEND:
1302                                 eDebug("[SEC] setFrontend");
1303                                 setFrontend();
1304                                 ++m_sec_sequence.current();
1305                                 break;
1306                         case eSecCommand::START_TUNE_TIMEOUT:
1307                                 m_timeout->start(5000, 1); // 5 sec timeout. TODO: symbolrate dependent
1308                                 ++m_sec_sequence.current();
1309                                 break;
1310                         case eSecCommand::SET_TIMEOUT:
1311                                 m_timeoutCount = m_sec_sequence.current()++->val;
1312                                 eDebug("[SEC] set timeout %d", m_timeoutCount);
1313                                 break;
1314                         case eSecCommand::IF_TIMEOUT_GOTO:
1315                                 if (!m_timeoutCount)
1316                                 {
1317                                         eDebug("[SEC] rotor timout");
1318                                         m_sec->setRotorMoving(false);
1319                                         setSecSequencePos(m_sec_sequence.current()->steps);
1320                                 }
1321                                 else
1322                                         ++m_sec_sequence.current();
1323                                 break;
1324                         case eSecCommand::MEASURE_IDLE_INPUTPOWER:
1325                         {
1326                                 int idx = m_sec_sequence.current()++->val;
1327                                 if ( idx == 0 || idx == 1 )
1328                                 {
1329                                         m_idleInputpower[idx] = readInputpower();
1330                                         eDebug("[SEC] idleInputpower[%d] is %d", idx, m_idleInputpower[idx]);
1331                                 }
1332                                 else
1333                                         eDebug("[SEC] idleInputpower measure index(%d) out of bound !!!", idx);
1334                                 break;
1335                         }
1336                         case eSecCommand::IF_MEASURE_IDLE_WAS_NOT_OK_GOTO:
1337                         {
1338                                 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1339                                 int idx = compare.val;
1340                                 if ( idx == 0 || idx == 1 )
1341                                 {
1342                                         int idle = readInputpower();
1343                                         int diff = abs(idle-m_idleInputpower[idx]);
1344                                         if ( diff > 0)
1345                                         {
1346                                                 eDebug("measure idle(%d) was not okay.. (%d - %d = %d) retry", idx, m_idleInputpower[idx], idle, diff);
1347                                                 setSecSequencePos(compare.steps);
1348                                                 break;
1349                                         }
1350                                 }
1351                                 ++m_sec_sequence.current();
1352                                 break;
1353                         }
1354                         case eSecCommand::IF_TUNER_LOCKED_GOTO:
1355                         {
1356                                 eSecCommand::rotor &cmd = m_sec_sequence.current()->measure;
1357                                 if (readFrontendData(locked))
1358                                 {
1359                                         eDebug("[SEC] locked step %d ok", cmd.okcount);
1360                                         ++cmd.okcount;
1361                                         if (cmd.okcount > 12)
1362                                         {
1363                                                 eDebug("ok > 12 .. goto %d\n",m_sec_sequence.current()->steps);
1364                                                 setSecSequencePos(cmd.steps);
1365                                                 break;
1366                                         }
1367                                 }
1368                                 else
1369                                 {
1370                                         eDebug("[SEC] rotor locked step %d failed", cmd.okcount);
1371                                         --m_timeoutCount;
1372                                         if (!m_timeoutCount && m_retryCount > 0)
1373                                                 --m_retryCount;
1374                                         cmd.okcount=0;
1375                                 }
1376                                 ++m_sec_sequence.current();
1377                                 break;
1378                         }
1379                         case eSecCommand::MEASURE_RUNNING_INPUTPOWER:
1380                                 m_runningInputpower = readInputpower();
1381                                 eDebug("[SEC] runningInputpower is %d", m_runningInputpower);
1382                                 ++m_sec_sequence.current();
1383                                 break;
1384                         case eSecCommand::IF_INPUTPOWER_DELTA_GOTO:
1385                         {
1386                                 int idleInputpower = m_idleInputpower[ (m_data[CUR_VOLTAGE]&1) ? 0 : 1];
1387                                 eSecCommand::rotor &cmd = m_sec_sequence.current()->measure;
1388                                 const char *txt = cmd.direction ? "running" : "stopped";
1389                                 eDebug("[SEC] waiting for rotor %s %d, idle %d, delta %d",
1390                                         txt,
1391                                         m_runningInputpower,
1392                                         idleInputpower,
1393                                         cmd.deltaA);
1394                                 if ( (cmd.direction && abs(m_runningInputpower - idleInputpower) >= cmd.deltaA)
1395                                         || (!cmd.direction && abs(m_runningInputpower - idleInputpower) <= cmd.deltaA) )
1396                                 {
1397                                         ++cmd.okcount;
1398                                         eDebug("[SEC] rotor %s step %d ok", txt, cmd.okcount);
1399                                         if ( cmd.okcount > 6 )
1400                                         {
1401                                                 m_sec->setRotorMoving(cmd.direction);
1402                                                 eDebug("[SEC] rotor is %s", txt);
1403                                                 if (setSecSequencePos(cmd.steps))
1404                                                         break;
1405                                         }
1406                                 }
1407                                 else
1408                                 {
1409                                         eDebug("[SEC] rotor not %s... reset counter.. increase timeout", txt);
1410                                         --m_timeoutCount;
1411                                         if (!m_timeoutCount && m_retryCount > 0)
1412                                                 --m_retryCount;
1413                                         cmd.okcount=0;
1414                                 }
1415                                 ++m_sec_sequence.current();
1416                                 break;
1417                         }
1418                         case eSecCommand::IF_ROTORPOS_VALID_GOTO:
1419                                 if (m_data[ROTOR_CMD] != -1 && m_data[ROTOR_POS] != -1)
1420                                         setSecSequencePos(m_sec_sequence.current()->steps);
1421                                 else
1422                                         ++m_sec_sequence.current();
1423                                 break;
1424                         case eSecCommand::INVALIDATE_CURRENT_ROTORPARMS:
1425                                 eDebug("[SEC] invalidate current rotorparams");
1426                                 setRotorData(-1,-1);    
1427                                 ++m_sec_sequence.current();
1428                                 break;
1429                         case eSecCommand::UPDATE_CURRENT_ROTORPARAMS:
1430                                 setRotorData(m_data[NEW_ROTOR_POS], m_data[NEW_ROTOR_CMD]);
1431                                 eDebug("[SEC] update current rotorparams %d %04x %d", m_timeoutCount, m_data[ROTOR_CMD], m_data[ROTOR_POS]);
1432                                 ++m_sec_sequence.current();
1433                                 break;
1434                         case eSecCommand::SET_ROTOR_DISEQC_RETRYS:
1435                                 m_retryCount = m_sec_sequence.current()++->val;
1436                                 eDebug("[SEC] set rotor retries %d", m_retryCount);
1437                                 break;
1438                         case eSecCommand::IF_NO_MORE_ROTOR_DISEQC_RETRYS_GOTO:
1439                                 if (!m_retryCount)
1440                                 {
1441                                         eDebug("[SEC] no more rotor retrys");
1442                                         setSecSequencePos(m_sec_sequence.current()->steps);
1443                                 }
1444                                 else
1445                                         ++m_sec_sequence.current();
1446                                 break;
1447                         case eSecCommand::SET_POWER_LIMITING_MODE:
1448                         {
1449                                 if (!m_need_rotor_workaround)
1450                                         break;
1451
1452                                 char dev[16];
1453
1454                                 // FIXMEEEEEE hardcoded i2c devices for dm7025 and dm8000
1455                                 if (m_slotid < 2)
1456                                         sprintf(dev, "/dev/i2c/%d", m_slotid);
1457                                 else if (m_slotid == 2)
1458                                         sprintf(dev, "/dev/i2c/2"); // first nim socket on DM8000 use /dev/i2c/2
1459                                 else if (m_slotid == 3)
1460                                         sprintf(dev, "/dev/i2c/4"); // second nim socket on DM8000 use /dev/i2c/4
1461                                 int fd = ::open(dev, O_RDWR);
1462
1463                                 unsigned char data[2];
1464                                 ::ioctl(fd, I2C_SLAVE_FORCE, 0x10 >> 1);
1465                                 if(::read(fd, data, 1) != 1)
1466                                         eDebug("[SEC] error read lnbp (%m)");
1467                                 if ( m_sec_sequence.current()->mode == eSecCommand::modeStatic )
1468                                 {
1469                                         data[0] |= 0x80;  // enable static current limiting
1470                                         eDebug("[SEC] set static current limiting");
1471                                 }
1472                                 else
1473                                 {
1474                                         data[0] &= ~0x80;  // enable dynamic current limiting
1475                                         eDebug("[SEC] set dynamic current limiting");
1476                                 }
1477                                 if(::write(fd, data, 1) != 1)
1478                                         eDebug("[SEC] error write lnbp (%m)");
1479                                 ::close(fd);
1480                                 ++m_sec_sequence.current();
1481                                 break;
1482                         }
1483                         default:
1484                                 eDebug("[SEC] unhandled sec command %d",
1485                                         ++m_sec_sequence.current()->cmd);
1486                                 ++m_sec_sequence.current();
1487                 }
1488                 m_tuneTimer->start(delay,true);
1489         }
1490 }
1491
1492 void eDVBFrontend::setFrontend()
1493 {
1494         eDebug("setting frontend %d", m_dvbid);
1495         m_sn->start();
1496         feEvent(-1);
1497         if (ioctl(m_fd, FE_SET_FRONTEND, &parm) == -1)
1498         {
1499                 perror("FE_SET_FRONTEND failed");
1500                 return;
1501         }
1502 }
1503
1504 RESULT eDVBFrontend::getFrontendType(int &t)
1505 {
1506         if (m_type == -1)
1507                 return -ENODEV;
1508         t = m_type;
1509         return 0;
1510 }
1511
1512 RESULT eDVBFrontend::prepare_sat(const eDVBFrontendParametersSatellite &feparm)
1513 {
1514         int res;
1515         if (!m_sec)
1516         {
1517                 eWarning("no SEC module active!");
1518                 return -ENOENT;
1519         }
1520         res = m_sec->prepare(*this, parm, feparm, 1 << m_slotid);
1521         if (!res)
1522         {
1523                 eDebug("prepare_sat System %d Freq %d Pol %d SR %d INV %d FEC %d orbpos %d",
1524                         feparm.system,
1525                         feparm.frequency,
1526                         feparm.polarisation,
1527                         feparm.symbol_rate,
1528                         feparm.inversion,
1529                         feparm.fec,
1530                         feparm.orbital_position);
1531                 parm_u_qpsk_symbol_rate = feparm.symbol_rate;
1532                 switch (feparm.inversion)
1533                 {
1534                         case eDVBFrontendParametersSatellite::Inversion::On:
1535                                 parm_inversion = INVERSION_ON;
1536                                 break;
1537                         case eDVBFrontendParametersSatellite::Inversion::Off:
1538                                 parm_inversion = INVERSION_OFF;
1539                                 break;
1540                         default:
1541                         case eDVBFrontendParametersSatellite::Inversion::Unknown:
1542                                 parm_inversion = INVERSION_AUTO;
1543                                 break;
1544                 }
1545                 if (feparm.system == eDVBFrontendParametersSatellite::System::DVB_S)
1546                         switch (feparm.fec)
1547                         {
1548                                 case eDVBFrontendParametersSatellite::FEC::fNone:
1549                                         parm_u_qpsk_fec_inner = FEC_NONE;
1550                                         break;
1551                                 case eDVBFrontendParametersSatellite::FEC::f1_2:
1552                                         parm_u_qpsk_fec_inner = FEC_1_2;
1553                                         break;
1554                                 case eDVBFrontendParametersSatellite::FEC::f2_3:
1555                                         parm_u_qpsk_fec_inner = FEC_2_3;
1556                                         break;
1557                                 case eDVBFrontendParametersSatellite::FEC::f3_4:
1558                                         parm_u_qpsk_fec_inner = FEC_3_4;
1559                                         break;
1560                                 case eDVBFrontendParametersSatellite::FEC::f5_6:
1561                                         parm_u_qpsk_fec_inner = FEC_5_6;
1562                                         break;
1563                                 case eDVBFrontendParametersSatellite::FEC::f7_8:
1564                                         parm_u_qpsk_fec_inner = FEC_7_8;
1565                                         break;
1566                                 default:
1567                                         eDebug("no valid fec for DVB-S set.. assume auto");
1568                                 case eDVBFrontendParametersSatellite::FEC::fAuto:
1569                                         parm_u_qpsk_fec_inner = FEC_AUTO;
1570                                         break;
1571                         }
1572 #if HAVE_DVB_API_VERSION >= 3
1573                 else // DVB_S2
1574                 {
1575                         switch (feparm.fec)
1576                         {
1577                                 case eDVBFrontendParametersSatellite::FEC::f1_2:
1578                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_1_2;
1579                                         break;
1580                                 case eDVBFrontendParametersSatellite::FEC::f2_3:
1581                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_2_3;
1582                                         break;
1583                                 case eDVBFrontendParametersSatellite::FEC::f3_4:
1584                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_3_4;
1585                                         break;
1586                                 case eDVBFrontendParametersSatellite::FEC::f3_5:
1587                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_3_5;
1588                                         break;
1589                                 case eDVBFrontendParametersSatellite::FEC::f4_5:
1590                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_4_5;
1591                                         break;
1592                                 case eDVBFrontendParametersSatellite::FEC::f5_6:
1593                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_5_6;
1594                                         break;
1595                                 case eDVBFrontendParametersSatellite::FEC::f7_8:
1596                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_7_8;
1597                                         break;
1598                                 case eDVBFrontendParametersSatellite::FEC::f8_9:
1599                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_8_9;
1600                                         break;
1601                                 case eDVBFrontendParametersSatellite::FEC::f9_10:
1602                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_9_10;
1603                                         break;
1604                                 default:
1605                                         eDebug("no valid fec for DVB-S2 set.. abort !!");
1606                                         return -EINVAL;
1607                         }
1608                         if (feparm.modulation == eDVBFrontendParametersSatellite::Modulation::M8PSK)
1609                                 parm_u_qpsk_fec_inner = (fe_code_rate_t)((int)parm_u_qpsk_fec_inner+9);
1610                                 // 8PSK fec driver values are decimal 9 bigger
1611                 }
1612 #endif
1613                 // FIXME !!! get frequency range from tuner
1614                 if ( parm_frequency < 900000 || parm_frequency > 2200000 )
1615                 {
1616                         eDebug("%d mhz out of tuner range.. dont tune", parm_frequency/1000);
1617                         return -EINVAL;
1618                 }
1619                 eDebug("tuning to %d mhz", parm_frequency/1000);
1620         }
1621         return res;
1622 }
1623
1624 RESULT eDVBFrontend::prepare_cable(const eDVBFrontendParametersCable &feparm)
1625 {
1626         parm_frequency = feparm.frequency * 1000;
1627         parm_u_qam_symbol_rate = feparm.symbol_rate;
1628         switch (feparm.modulation)
1629         {
1630         case eDVBFrontendParametersCable::Modulation::QAM16:
1631                 parm_u_qam_modulation = QAM_16;
1632                 break;
1633         case eDVBFrontendParametersCable::Modulation::QAM32:
1634                 parm_u_qam_modulation = QAM_32;
1635                 break;
1636         case eDVBFrontendParametersCable::Modulation::QAM64:
1637                 parm_u_qam_modulation = QAM_64;
1638                 break;
1639         case eDVBFrontendParametersCable::Modulation::QAM128:
1640                 parm_u_qam_modulation = QAM_128;
1641                 break;
1642         case eDVBFrontendParametersCable::Modulation::QAM256:
1643                 parm_u_qam_modulation = QAM_256;
1644                 break;
1645         default:
1646         case eDVBFrontendParametersCable::Modulation::Auto:
1647                 parm_u_qam_modulation = QAM_AUTO;
1648                 break;
1649         }
1650         switch (feparm.inversion)
1651         {
1652         case eDVBFrontendParametersCable::Inversion::On:
1653                 parm_inversion = INVERSION_ON;
1654                 break;
1655         case eDVBFrontendParametersCable::Inversion::Off:
1656                 parm_inversion = INVERSION_OFF;
1657                 break;
1658         default:
1659         case eDVBFrontendParametersCable::Inversion::Unknown:
1660                 parm_inversion = INVERSION_AUTO;
1661                 break;
1662         }
1663         switch (feparm.fec_inner)
1664         {
1665         case eDVBFrontendParametersCable::FEC::fNone:
1666                 parm_u_qam_fec_inner = FEC_NONE;
1667                 break;
1668         case eDVBFrontendParametersCable::FEC::f1_2:
1669                 parm_u_qam_fec_inner = FEC_1_2;
1670                 break;
1671         case eDVBFrontendParametersCable::FEC::f2_3:
1672                 parm_u_qam_fec_inner = FEC_2_3;
1673                 break;
1674         case eDVBFrontendParametersCable::FEC::f3_4:
1675                 parm_u_qam_fec_inner = FEC_3_4;
1676                 break;
1677         case eDVBFrontendParametersCable::FEC::f5_6:
1678                 parm_u_qam_fec_inner = FEC_5_6;
1679                 break;
1680         case eDVBFrontendParametersCable::FEC::f7_8:
1681                 parm_u_qam_fec_inner = FEC_7_8;
1682                 break;
1683 #if HAVE_DVB_API_VERSION >= 3
1684         case eDVBFrontendParametersCable::FEC::f8_9:
1685                 parm_u_qam_fec_inner = FEC_8_9;
1686                 break;
1687 #endif
1688         default:
1689         case eDVBFrontendParametersCable::FEC::fAuto:
1690                 parm_u_qam_fec_inner = FEC_AUTO;
1691                 break;
1692         }
1693         eDebug("tuning to %d khz, sr %d, fec %d, modulation %d, inversion %d",
1694                 parm_frequency/1000,
1695                 parm_u_qam_symbol_rate,
1696                 parm_u_qam_fec_inner,
1697                 parm_u_qam_modulation,
1698                 parm_inversion);
1699         return 0;
1700 }
1701
1702 RESULT eDVBFrontend::prepare_terrestrial(const eDVBFrontendParametersTerrestrial &feparm)
1703 {
1704         parm_frequency = feparm.frequency;
1705
1706         switch (feparm.bandwidth)
1707         {
1708         case eDVBFrontendParametersTerrestrial::Bandwidth::Bw8MHz:
1709                 parm_u_ofdm_bandwidth = BANDWIDTH_8_MHZ;
1710                 break;
1711         case eDVBFrontendParametersTerrestrial::Bandwidth::Bw7MHz:
1712                 parm_u_ofdm_bandwidth = BANDWIDTH_7_MHZ;
1713                 break;
1714         case eDVBFrontendParametersTerrestrial::Bandwidth::Bw6MHz:
1715                 parm_u_ofdm_bandwidth = BANDWIDTH_6_MHZ;
1716                 break;
1717         default:
1718         case eDVBFrontendParametersTerrestrial::Bandwidth::BwAuto:
1719                 parm_u_ofdm_bandwidth = BANDWIDTH_AUTO;
1720                 break;
1721         }
1722         switch (feparm.code_rate_LP)
1723         {
1724         case eDVBFrontendParametersTerrestrial::FEC::f1_2:
1725                 parm_u_ofdm_code_rate_LP = FEC_1_2;
1726                 break;
1727         case eDVBFrontendParametersTerrestrial::FEC::f2_3:
1728                 parm_u_ofdm_code_rate_LP = FEC_2_3;
1729                 break;
1730         case eDVBFrontendParametersTerrestrial::FEC::f3_4:
1731                 parm_u_ofdm_code_rate_LP = FEC_3_4;
1732                 break;
1733         case eDVBFrontendParametersTerrestrial::FEC::f5_6:
1734                 parm_u_ofdm_code_rate_LP = FEC_5_6;
1735                 break;
1736         case eDVBFrontendParametersTerrestrial::FEC::f7_8:
1737                 parm_u_ofdm_code_rate_LP = FEC_7_8;
1738                 break;
1739         default:
1740         case eDVBFrontendParametersTerrestrial::FEC::fAuto:
1741                 parm_u_ofdm_code_rate_LP = FEC_AUTO;
1742                 break;
1743         }
1744         switch (feparm.code_rate_HP)
1745         {
1746         case eDVBFrontendParametersTerrestrial::FEC::f1_2:
1747                 parm_u_ofdm_code_rate_HP = FEC_1_2;
1748                 break;
1749         case eDVBFrontendParametersTerrestrial::FEC::f2_3:
1750                 parm_u_ofdm_code_rate_HP = FEC_2_3;
1751                 break;
1752         case eDVBFrontendParametersTerrestrial::FEC::f3_4:
1753                 parm_u_ofdm_code_rate_HP = FEC_3_4;
1754                 break;
1755         case eDVBFrontendParametersTerrestrial::FEC::f5_6:
1756                 parm_u_ofdm_code_rate_HP = FEC_5_6;
1757                 break;
1758         case eDVBFrontendParametersTerrestrial::FEC::f7_8:
1759                 parm_u_ofdm_code_rate_HP = FEC_7_8;
1760                 break;
1761         default:
1762         case eDVBFrontendParametersTerrestrial::FEC::fAuto:
1763                 parm_u_ofdm_code_rate_HP = FEC_AUTO;
1764                 break;
1765         }
1766         switch (feparm.modulation)
1767         {
1768         case eDVBFrontendParametersTerrestrial::Modulation::QPSK:
1769                 parm_u_ofdm_constellation = QPSK;
1770                 break;
1771         case eDVBFrontendParametersTerrestrial::Modulation::QAM16:
1772                 parm_u_ofdm_constellation = QAM_16;
1773                 break;
1774         case eDVBFrontendParametersTerrestrial::Modulation::QAM64:
1775                 parm_u_ofdm_constellation = QAM_64;
1776                 break;
1777         default:
1778         case eDVBFrontendParametersTerrestrial::Modulation::Auto:
1779                 parm_u_ofdm_constellation = QAM_AUTO;
1780                 break;
1781         }
1782         switch (feparm.transmission_mode)
1783         {
1784         case eDVBFrontendParametersTerrestrial::TransmissionMode::TM2k:
1785                 parm_u_ofdm_transmission_mode = TRANSMISSION_MODE_2K;
1786                 break;
1787         case eDVBFrontendParametersTerrestrial::TransmissionMode::TM8k:
1788                 parm_u_ofdm_transmission_mode = TRANSMISSION_MODE_8K;
1789                 break;
1790         default:
1791         case eDVBFrontendParametersTerrestrial::TransmissionMode::TMAuto:
1792                 parm_u_ofdm_transmission_mode = TRANSMISSION_MODE_AUTO;
1793                 break;
1794         }
1795         switch (feparm.guard_interval)
1796         {
1797                 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_32:
1798                         parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_32;
1799                         break;
1800                 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_16:
1801                         parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_16;
1802                         break;
1803                 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_8:
1804                         parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_8;
1805                         break;
1806                 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_4:
1807                         parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_4;
1808                         break;
1809                 default:
1810                 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_Auto:
1811                         parm_u_ofdm_guard_interval = GUARD_INTERVAL_AUTO;
1812                         break;
1813         }
1814         switch (feparm.hierarchy)
1815         {
1816                 case eDVBFrontendParametersTerrestrial::Hierarchy::HNone:
1817                         parm_u_ofdm_hierarchy_information = HIERARCHY_NONE;
1818                         break;
1819                 case eDVBFrontendParametersTerrestrial::Hierarchy::H1:
1820                         parm_u_ofdm_hierarchy_information = HIERARCHY_1;
1821                         break;
1822                 case eDVBFrontendParametersTerrestrial::Hierarchy::H2:
1823                         parm_u_ofdm_hierarchy_information = HIERARCHY_2;
1824                         break;
1825                 case eDVBFrontendParametersTerrestrial::Hierarchy::H4:
1826                         parm_u_ofdm_hierarchy_information = HIERARCHY_4;
1827                         break;
1828                 default:
1829                 case eDVBFrontendParametersTerrestrial::Hierarchy::HAuto:
1830                         parm_u_ofdm_hierarchy_information = HIERARCHY_AUTO;
1831                         break;
1832         }
1833         switch (feparm.inversion)
1834         {
1835         case eDVBFrontendParametersTerrestrial::Inversion::On:
1836                 parm_inversion = INVERSION_ON;
1837                 break;
1838         case eDVBFrontendParametersTerrestrial::Inversion::Off:
1839                 parm_inversion = INVERSION_OFF;
1840                 break;
1841         default:
1842         case eDVBFrontendParametersTerrestrial::Inversion::Unknown:
1843                 parm_inversion = INVERSION_AUTO;
1844                 break;
1845         }
1846         return 0;
1847 }
1848
1849 RESULT eDVBFrontend::tune(const iDVBFrontendParameters &where)
1850 {
1851         eDebug("(%d)tune", m_dvbid);
1852
1853         m_timeout->stop();
1854
1855         int res=0;
1856
1857         if (!m_sn)
1858         {
1859                 eDebug("no frontend device opened... do not try to tune !!!");
1860                 res = -ENODEV;
1861                 goto tune_error;
1862         }
1863
1864         if (m_type == -1)
1865         {
1866                 res = -ENODEV;
1867                 goto tune_error;
1868         }
1869
1870         m_sn->stop();
1871         m_sec_sequence.clear();
1872
1873         switch (m_type)
1874         {
1875         case feSatellite:
1876         {
1877                 eDVBFrontendParametersSatellite feparm;
1878                 if (where.getDVBS(feparm))
1879                 {
1880                         eDebug("no dvbs data!");
1881                         res = -EINVAL;
1882                         goto tune_error;
1883                 }
1884                 m_sec->setRotorMoving(false);
1885                 res=prepare_sat(feparm);
1886                 if (res)
1887                         goto tune_error;
1888
1889                 break;
1890         }
1891         case feCable:
1892         {
1893                 eDVBFrontendParametersCable feparm;
1894                 if (where.getDVBC(feparm))
1895                 {
1896                         res = -EINVAL;
1897                         goto tune_error;
1898                 }
1899                 res=prepare_cable(feparm);
1900                 if (res)
1901                         goto tune_error;
1902
1903                 m_sec_sequence.push_back( eSecCommand(eSecCommand::START_TUNE_TIMEOUT) );
1904                 m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
1905                 break;
1906         }
1907         case feTerrestrial:
1908         {
1909                 eDVBFrontendParametersTerrestrial feparm;
1910                 if (where.getDVBT(feparm))
1911                 {
1912                         eDebug("no -T data");
1913                         res = -EINVAL;
1914                         goto tune_error;
1915                 }
1916                 res=prepare_terrestrial(feparm);
1917                 if (res)
1918                         goto tune_error;
1919
1920                 std::string enable_5V;
1921                 char configStr[255];
1922                 snprintf(configStr, 255, "config.Nims.%d.terrestrial_5V", m_slotid);
1923                 m_sec_sequence.push_back( eSecCommand(eSecCommand::START_TUNE_TIMEOUT) );
1924                 ePythonConfigQuery::getConfigValue(configStr, enable_5V);
1925                 if (enable_5V == "True")
1926                         m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, iDVBFrontend::voltage13) );
1927                 else
1928                         m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, iDVBFrontend::voltageOff) );
1929                 m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
1930
1931                 break;
1932         }
1933         }
1934
1935         m_tuneTimer->start(0,true);
1936         m_sec_sequence.current() = m_sec_sequence.begin();
1937
1938         if (m_state != stateTuning)
1939         {
1940                 m_tuning = 1;
1941                 m_state = stateTuning;
1942                 m_stateChanged(this);
1943         }
1944
1945         return res;
1946
1947 tune_error:
1948         m_tuneTimer->stop();
1949         return res;
1950 }
1951
1952 RESULT eDVBFrontend::connectStateChange(const Slot1<void,iDVBFrontend*> &stateChange, ePtr<eConnection> &connection)
1953 {
1954         connection = new eConnection(this, m_stateChanged.connect(stateChange));
1955         return 0;
1956 }
1957
1958 RESULT eDVBFrontend::setVoltage(int voltage)
1959 {
1960         if (m_type == feCable)
1961                 return -1;
1962 #if HAVE_DVB_API_VERSION < 3
1963         secVoltage vlt;
1964 #else
1965         bool increased=false;
1966         fe_sec_voltage_t vlt;
1967 #endif
1968         m_data[CUR_VOLTAGE]=voltage;
1969         switch (voltage)
1970         {
1971         case voltageOff:
1972                 for (int i=0; i < 3; ++i)  // reset diseqc
1973                         m_data[i]=-1;
1974                 vlt = SEC_VOLTAGE_OFF;
1975                 break;
1976         case voltage13_5:
1977 #if HAVE_DVB_API_VERSION < 3
1978                 vlt = SEC_VOLTAGE_13_5;
1979                 break;
1980 #else
1981                 increased = true;
1982 #endif
1983         case voltage13:
1984                 vlt = SEC_VOLTAGE_13;
1985                 break;
1986         case voltage18_5:
1987 #if HAVE_DVB_API_VERSION < 3
1988                 vlt = SEC_VOLTAGE_18_5;
1989                 break;
1990 #else
1991                 increased = true;
1992 #endif
1993         case voltage18:
1994                 vlt = SEC_VOLTAGE_18;
1995                 break;
1996         default:
1997                 return -ENODEV;
1998         }
1999 #if HAVE_DVB_API_VERSION < 3
2000         return ::ioctl(m_secfd, SEC_SET_VOLTAGE, vlt);
2001 #else
2002         if (m_type == feSatellite && ::ioctl(m_fd, FE_ENABLE_HIGH_LNB_VOLTAGE, increased) < 0)
2003                 perror("FE_ENABLE_HIGH_LNB_VOLTAGE");
2004         return ::ioctl(m_fd, FE_SET_VOLTAGE, vlt);
2005 #endif
2006 }
2007
2008 RESULT eDVBFrontend::getState(int &state)
2009 {
2010         state = m_state;
2011         return 0;
2012 }
2013
2014 RESULT eDVBFrontend::setTone(int t)
2015 {
2016         if (m_type != feSatellite)
2017                 return -1;
2018 #if HAVE_DVB_API_VERSION < 3
2019         secToneMode_t tone;
2020 #else
2021         fe_sec_tone_mode_t tone;
2022 #endif
2023         m_data[CUR_TONE]=t;
2024         switch (t)
2025         {
2026         case toneOn:
2027                 tone = SEC_TONE_ON;
2028                 break;
2029         case toneOff:
2030                 tone = SEC_TONE_OFF;
2031                 break;
2032         default:
2033                 return -ENODEV;
2034         }
2035 #if HAVE_DVB_API_VERSION < 3    
2036         return ::ioctl(m_secfd, SEC_SET_TONE, tone);
2037 #else   
2038         return ::ioctl(m_fd, FE_SET_TONE, tone);
2039 #endif
2040 }
2041
2042 #if HAVE_DVB_API_VERSION < 3 && !defined(SEC_DISEQC_SEND_MASTER_CMD)
2043         #define SEC_DISEQC_SEND_MASTER_CMD _IOW('o', 97, struct secCommand *)
2044 #endif
2045
2046 RESULT eDVBFrontend::sendDiseqc(const eDVBDiseqcCommand &diseqc)
2047 {
2048 #if HAVE_DVB_API_VERSION < 3
2049         struct secCommand cmd;
2050         cmd.type = SEC_CMDTYPE_DISEQC_RAW;
2051         cmd.u.diseqc.cmdtype = diseqc.data[0];
2052         cmd.u.diseqc.addr = diseqc.data[1];
2053         cmd.u.diseqc.cmd = diseqc.data[2];
2054         cmd.u.diseqc.numParams = diseqc.len-3;
2055         memcpy(cmd.u.diseqc.params, diseqc.data+3, diseqc.len-3);
2056         if (::ioctl(m_secfd, SEC_DISEQC_SEND_MASTER_CMD, &cmd))
2057 #else
2058         struct dvb_diseqc_master_cmd cmd;
2059         memcpy(cmd.msg, diseqc.data, diseqc.len);
2060         cmd.msg_len = diseqc.len;
2061         if (::ioctl(m_fd, FE_DISEQC_SEND_MASTER_CMD, &cmd))
2062 #endif
2063                 return -EINVAL;
2064         return 0;
2065 }
2066
2067 #if HAVE_DVB_API_VERSION < 3 && !defined(SEC_DISEQC_SEND_BURST)
2068         #define SEC_DISEQC_SEND_BURST _IO('o', 96)
2069 #endif
2070 RESULT eDVBFrontend::sendToneburst(int burst)
2071 {
2072 #if HAVE_DVB_API_VERSION < 3
2073         secMiniCmd cmd = SEC_MINI_NONE;
2074 #else
2075         fe_sec_mini_cmd_t cmd = SEC_MINI_A;
2076 #endif
2077         if ( burst == eDVBSatelliteDiseqcParameters::A )
2078                 cmd = SEC_MINI_A;
2079         else if ( burst == eDVBSatelliteDiseqcParameters::B )
2080                 cmd = SEC_MINI_B;
2081 #if HAVE_DVB_API_VERSION < 3
2082         if (::ioctl(m_secfd, SEC_DISEQC_SEND_BURST, cmd))
2083                 return -EINVAL;
2084 #else
2085         if (::ioctl(m_fd, FE_DISEQC_SEND_BURST, cmd))
2086                 return -EINVAL;
2087 #endif
2088         return 0;
2089 }
2090
2091 RESULT eDVBFrontend::setSEC(iDVBSatelliteEquipmentControl *sec)
2092 {
2093         m_sec = sec;
2094         return 0;
2095 }
2096
2097 RESULT eDVBFrontend::setSecSequence(const eSecCommandList &list)
2098 {
2099         m_sec_sequence = list;
2100         return 0;
2101 }
2102
2103 RESULT eDVBFrontend::getData(int num, int &data)
2104 {
2105         if ( num < NUM_DATA_ENTRIES )
2106         {
2107                 data = m_data[num];
2108                 return 0;
2109         }
2110         return -EINVAL;
2111 }
2112
2113 RESULT eDVBFrontend::setData(int num, int val)
2114 {
2115         if ( num < NUM_DATA_ENTRIES )
2116         {
2117                 m_data[num] = val;
2118                 return 0;
2119         }
2120         return -EINVAL;
2121 }
2122
2123 int eDVBFrontend::isCompatibleWith(ePtr<iDVBFrontendParameters> &feparm)
2124 {
2125         int type;
2126         if (feparm->getSystem(type) || type != m_type || !m_enabled)
2127                 return 0;
2128
2129         if (m_type == eDVBFrontend::feSatellite)
2130         {
2131                 ASSERT(m_sec);
2132                 eDVBFrontendParametersSatellite sat_parm;
2133                 int ret = feparm->getDVBS(sat_parm);
2134                 ASSERT(!ret);
2135                 return m_sec->canTune(sat_parm, this, 1 << m_slotid);
2136         }
2137         else if (m_type == eDVBFrontend::feCable)
2138                 return 2;  // more prio for cable frontends
2139         return 1;
2140 }
2141
2142 bool eDVBFrontend::setSlotInfo(ePyObject obj)
2143 {
2144         ePyObject Id, Descr, Enabled;
2145         if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 3)
2146                 goto arg_error;
2147         Id = PyTuple_GET_ITEM(obj, 0);
2148         Descr = PyTuple_GET_ITEM(obj, 1);
2149         Enabled = PyTuple_GET_ITEM(obj, 2);
2150         if (!PyInt_Check(Id) || !PyString_Check(Descr) || !PyBool_Check(Enabled))
2151                 goto arg_error;
2152         strcpy(m_description, PyString_AS_STRING(Descr));
2153         m_slotid = PyInt_AsLong(Id);
2154         m_enabled = Enabled == Py_True;
2155         // HACK.. the rotor workaround is neede for all NIMs with LNBP21 voltage regulator...
2156         m_need_rotor_workaround = !!strstr(m_description, "Alps BSBE1") ||
2157                 !!strstr(m_description, "Alps BSBE2") ||
2158                 !!strstr(m_description, "Alps -S");
2159         eDebug("setSlotInfo for dvb frontend %d to slotid %d, descr %s, need rotorworkaround %s, enabled %s",
2160                 m_dvbid, m_slotid, m_description, m_need_rotor_workaround ? "Yes" : "No", m_enabled ? "Yes" : "No" );
2161         return true;
2162 arg_error:
2163         PyErr_SetString(PyExc_StandardError,
2164                 "eDVBFrontend::setSlotInfo must get a tuple with first param slotid, second param slot description and third param enabled boolean");
2165         return false;
2166 }