dvbci.cpp: get rid of strange messages from alphacrypt CI when zap from a scrambled...
[vuplus_dvbapp] / lib / dvb_ci / dvbci.cpp
1 #include <fcntl.h>
2 #include <sys/ioctl.h>
3
4 #include <lib/base/init.h>
5 #include <lib/base/init_num.h>
6 #include <lib/base/ebase.h>
7
8 #include <lib/base/eerror.h>
9 #include <lib/base/nconfig.h> // access to python config
10 #include <lib/dvb/db.h>
11 #include <lib/dvb/pmt.h>
12 #include <lib/dvb_ci/dvbci.h>
13 #include <lib/dvb_ci/dvbci_session.h>
14 #include <lib/dvb_ci/dvbci_camgr.h>
15 #include <lib/dvb_ci/dvbci_ui.h>
16 #include <lib/dvb_ci/dvbci_appmgr.h>
17 #include <lib/dvb_ci/dvbci_mmi.h>
18
19 #include <dvbsi++/ca_program_map_section.h>
20
21 //#define CIDEBUG 1
22
23 #ifdef CIDEBUG
24         #define eDebugCI(x...) eDebug(x)
25 #else
26         #define eDebugCI(x...)
27 #endif
28
29 eDVBCIInterfaces *eDVBCIInterfaces::instance = 0;
30
31 eDVBCIInterfaces::eDVBCIInterfaces()
32 {
33         int num_ci = 0;
34         
35         instance = this;
36         
37         eDebug("scanning for common interfaces..");
38
39         while (1)
40         {
41                 struct stat s;
42                 char filename[128];
43                 sprintf(filename, "/dev/ci%d", num_ci);
44
45                 if (stat(filename, &s))
46                         break;
47
48                 ePtr<eDVBCISlot> cislot;
49
50                 cislot = new eDVBCISlot(eApp, num_ci);
51                 m_slots.push_back(cislot);
52
53                 ++num_ci;
54         }
55
56         for (eSmartPtrList<eDVBCISlot>::iterator it(m_slots.begin()); it != m_slots.end(); ++it)
57                 it->setSource(TUNER_A);
58
59         if (num_ci > 1) // // FIXME .. we force DM8000 when more than one CI Slot is avail
60         {
61                 setInputSource(0, TUNER_A);
62                 setInputSource(1, TUNER_B);
63                 setInputSource(2, TUNER_C);
64                 setInputSource(3, TUNER_D);
65         }
66         else
67         {
68                 setInputSource(0, TUNER_A);
69                 setInputSource(1, TUNER_B);
70         }
71
72         eDebug("done, found %d common interface slots", num_ci);
73 }
74
75 eDVBCIInterfaces::~eDVBCIInterfaces()
76 {
77 }
78
79 eDVBCIInterfaces *eDVBCIInterfaces::getInstance()
80 {
81         return instance;
82 }
83
84 eDVBCISlot *eDVBCIInterfaces::getSlot(int slotid)
85 {
86         for(eSmartPtrList<eDVBCISlot>::iterator i(m_slots.begin()); i != m_slots.end(); ++i)
87                 if(i->getSlotID() == slotid)
88                         return i;
89
90         eDebug("FIXME: request for unknown slot");
91                         
92         return 0;
93 }
94
95 int eDVBCIInterfaces::getSlotState(int slotid)
96 {
97         eDVBCISlot *slot;
98
99         if( (slot = getSlot(slotid)) == 0 )
100                 return eDVBCISlot::stateInvalid;
101
102         return slot->getState();
103 }
104
105 int eDVBCIInterfaces::reset(int slotid)
106 {
107         eDVBCISlot *slot;
108
109         if( (slot = getSlot(slotid)) == 0 )
110                 return -1;
111
112         return slot->reset();
113 }
114
115 int eDVBCIInterfaces::initialize(int slotid)
116 {
117         eDVBCISlot *slot;
118
119         if( (slot = getSlot(slotid)) == 0 )
120                 return -1;
121
122         slot->removeService();
123
124         return sendCAPMT(slotid);
125 }
126
127 int eDVBCIInterfaces::sendCAPMT(int slotid)
128 {
129         eDVBCISlot *slot;
130
131         if( (slot = getSlot(slotid)) == 0 )
132                 return -1;
133
134         PMTHandlerList::iterator it = m_pmt_handlers.begin();
135         while (it != m_pmt_handlers.end())
136         {
137                 eDVBCISlot *tmp = it->cislot;
138                 while (tmp && tmp != slot)
139                         tmp = tmp->linked_next;
140                 if (tmp)
141                 {
142                         tmp->sendCAPMT(it->pmthandler);  // send capmt
143                         break;
144                 }
145                 ++it;
146         }
147
148         return 0;
149 }
150
151 int eDVBCIInterfaces::startMMI(int slotid)
152 {
153         eDVBCISlot *slot;
154
155         if( (slot = getSlot(slotid)) == 0 )
156                 return -1;
157         
158         return slot->startMMI();
159 }
160
161 int eDVBCIInterfaces::stopMMI(int slotid)
162 {
163         eDVBCISlot *slot;
164
165         if( (slot = getSlot(slotid)) == 0 )
166                 return -1;
167         
168         return slot->stopMMI();
169 }
170
171 int eDVBCIInterfaces::answerText(int slotid, int answer)
172 {
173         eDVBCISlot *slot;
174
175         if( (slot = getSlot(slotid)) == 0 )
176                 return -1;
177         
178         return slot->answerText(answer);
179 }
180
181 int eDVBCIInterfaces::answerEnq(int slotid, char *value)
182 {
183         eDVBCISlot *slot;
184
185         if( (slot = getSlot(slotid)) == 0 )
186                 return -1;
187         
188         return slot->answerEnq(value);
189 }
190
191 int eDVBCIInterfaces::cancelEnq(int slotid)
192 {
193         eDVBCISlot *slot;
194
195         if( (slot = getSlot(slotid)) == 0 )
196                 return -1;
197         
198         return slot->cancelEnq();
199 }
200
201 void eDVBCIInterfaces::ciRemoved(eDVBCISlot *slot)
202 {
203         if (slot->use_count)
204         {
205                 eDebug("CI Slot %d: removed... usecount %d", slot->getSlotID(), slot->use_count);
206                 for (PMTHandlerList::iterator it(m_pmt_handlers.begin());
207                         it != m_pmt_handlers.end(); ++it)
208                 {
209                         if (it->cislot == slot) // remove the base slot
210                                 it->cislot = slot->linked_next;
211                         else if (it->cislot)
212                         {
213                                 eDVBCISlot *prevSlot = it->cislot, *hSlot = it->cislot->linked_next;
214                                 while (hSlot)
215                                 {
216                                         if (hSlot == slot) {
217                                                 prevSlot->linked_next = slot->linked_next;
218                                                 break;
219                                         }
220                                         prevSlot = hSlot;
221                                         hSlot = hSlot->linked_next;
222                                 }
223                         }
224                 }
225                 if (slot->linked_next)
226                         slot->linked_next->setSource(slot->current_source);
227                 else // last CI in chain
228                         setInputSource(slot->current_tuner, slot->current_source);
229                 slot->linked_next = 0;
230                 slot->use_count=0;
231                 slot->plugged=true;
232                 slot->user_mapped=false;
233                 slot->removeService(0xFFFF);
234                 recheckPMTHandlers();
235         }
236 }
237
238 static bool canDescrambleMultipleServices(int slotid)
239 {
240         char configStr[255];
241         snprintf(configStr, 255, "config.ci.%d.canDescrambleMultipleServices", slotid);
242         std::string str;
243         ePythonConfigQuery::getConfigValue(configStr, str);
244         if ( str == "auto" )
245         {
246                 std::string appname = eDVBCI_UI::getInstance()->getAppName(slotid);
247                 if (appname.find("AlphaCrypt") != std::string::npos)
248                         return true;
249         }
250         else if (str == "yes")
251                 return true;
252         return false;
253 }
254
255 void eDVBCIInterfaces::recheckPMTHandlers()
256 {
257         eDebugCI("recheckPMTHAndlers()");
258         for (PMTHandlerList::iterator it(m_pmt_handlers.begin());
259                 it != m_pmt_handlers.end(); ++it)
260         {
261                 CAID_LIST caids;
262                 ePtr<eDVBService> service;
263                 eServiceReferenceDVB ref;
264                 eDVBCISlot *tmp = it->cislot;
265                 eDVBServicePMTHandler *pmthandler = it->pmthandler;
266                 eDVBServicePMTHandler::program p;
267                 bool plugged_cis_exist = false;
268
269                 pmthandler->getServiceReference(ref);
270                 pmthandler->getService(service);
271
272                 eDebugCI("recheck %p %s", pmthandler, ref.toString().c_str());
273                 for (eSmartPtrList<eDVBCISlot>::iterator ci_it(m_slots.begin()); ci_it != m_slots.end(); ++ci_it)
274                         if (ci_it->plugged && ci_it->getCAManager())
275                         {
276                                 eDebug("Slot %d plugged", ci_it->getSlotID());
277                                 ci_it->plugged = false;
278                                 plugged_cis_exist = true;
279                         }
280
281                 // check if this pmt handler has already assigned CI(s) .. and this CI(s) are already running
282                 if (!plugged_cis_exist)
283                 {
284                         while(tmp)
285                         {
286                                 if (!tmp->running_services.empty())
287                                         break;
288                                 tmp=tmp->linked_next;
289                         }
290                         if (tmp) // we dont like to change tsmux for running services
291                         {
292                                 eDebugCI("already assigned and running CI!\n");
293                                 continue;
294                         }
295                 }
296
297                 if (!pmthandler->getProgramInfo(p))
298                 {
299                         int cnt=0;
300                         for (caidSet::reverse_iterator x(p.caids.rbegin()); x != p.caids.rend(); ++x, ++cnt)
301                                 caids.push_front(*x);
302                         if (service && cnt)
303                                 service->m_ca = caids;
304                 }
305
306                 if (service)
307                         caids = service->m_ca;
308
309                 if (caids.empty())
310                         continue; // unscrambled service
311
312                 for (eSmartPtrList<eDVBCISlot>::iterator ci_it(m_slots.begin()); ci_it != m_slots.end(); ++ci_it)
313                 {
314                         eDebugCI("check Slot %d", ci_it->getSlotID());
315                         bool useThis=false;
316                         bool user_mapped=true;
317                         eDVBCICAManagerSession *ca_manager = ci_it->getCAManager();
318
319                         if (ca_manager)
320                         {
321                                 int mask=0;
322                                 if (!ci_it->possible_services.empty())
323                                 {
324                                         mask |= 1;
325                                         serviceSet::iterator it = ci_it->possible_services.find(ref);
326                                         if (it != ci_it->possible_services.end())
327                                         {
328                                                 eDebug("'%s' is in service list of slot %d... so use it", ref.toString().c_str(), ci_it->getSlotID());
329                                                 useThis = true;
330                                         }
331                                         else // check parent
332                                         {
333                                                 eServiceReferenceDVB parent_ref = ref.getParentServiceReference();
334                                                 if (parent_ref)
335                                                 {
336                                                         it = ci_it->possible_services.find(ref);
337                                                         if (it != ci_it->possible_services.end())
338                                                         {
339                                                                 eDebug("parent '%s' of '%s' is in service list of slot %d... so use it",
340                                                                         parent_ref.toString().c_str(), ref.toString().c_str(), ci_it->getSlotID());
341                                                                 useThis = true;
342                                                         }
343                                                 }
344                                         }
345                                 }
346                                 if (!useThis && !ci_it->possible_providers.empty())
347                                 {
348                                         eDVBNamespace ns = ref.getDVBNamespace();
349                                         mask |= 2;
350                                         if (!service) // subservice?
351                                         {
352                                                 eServiceReferenceDVB parent_ref = ref.getParentServiceReference();
353                                                 eDVBDB::getInstance()->getService(parent_ref, service);
354                                         }
355                                         if (service)
356                                         {
357                                                 providerSet::iterator it = ci_it->possible_providers.find(providerPair(service->m_provider_name, ns.get()));
358                                                 if (it != ci_it->possible_providers.end())
359                                                 {
360                                                         eDebug("'%s/%08x' is in provider list of slot %d... so use it", service->m_provider_name.c_str(), ns.get(), ci_it->getSlotID());
361                                                         useThis = true;
362                                                 }
363                                         }
364                                 }
365                                 if (!useThis && !ci_it->possible_caids.empty())
366                                 {
367                                         mask |= 4;
368                                         for (CAID_LIST::iterator ca(caids.begin()); ca != caids.end(); ++ca)
369                                         {
370                                                 caidSet::iterator it = ci_it->possible_caids.find(*ca);
371                                                 if (it != ci_it->possible_caids.end())
372                                                 {
373                                                         eDebug("caid '%04x' is in caid list of slot %d... so use it", *ca, ci_it->getSlotID());
374                                                         useThis = true;
375                                                         break;
376                                                 }
377                                         }
378                                 }
379                                 if (!useThis && !mask)
380                                 {
381                                         const std::vector<uint16_t> &ci_caids = ca_manager->getCAIDs();
382                                         for (CAID_LIST::iterator ca(caids.begin()); ca != caids.end(); ++ca)
383                                         {
384                                                 std::vector<uint16_t>::const_iterator z =
385                                                         std::lower_bound(ci_caids.begin(), ci_caids.end(), *ca);
386                                                 if ( z != ci_caids.end() && *z == *ca )
387                                                 {
388                                                         eDebug("The CI in Slot %d has said it can handle caid %04x... so use it", ci_it->getSlotID(), *z);
389                                                         useThis = true;
390                                                         user_mapped = false;
391                                                         break;
392                                                 }
393                                         }
394                                 }
395                         }
396
397                         if (useThis)
398                         {
399                                 // check if this CI is already assigned to this pmthandler
400                                 eDVBCISlot *tmp = it->cislot;
401                                 while(tmp)
402                                 {
403                                         if (tmp == ci_it)
404                                                 break;
405                                         tmp=tmp->linked_next;
406                                 }
407                                 if (tmp) // ignore already assigned cislots...
408                                 {
409                                         eDebugCI("already assigned!");
410                                         continue;
411                                 }
412                                 eDebugCI("current slot %d usecount %d", ci_it->getSlotID(), ci_it->use_count);
413                                 if (ci_it->use_count)  // check if this CI can descramble more than one service
414                                 {
415                                         bool found = false;
416                                         useThis = false;
417                                         PMTHandlerList::iterator tmp = m_pmt_handlers.begin();
418                                         while (!found && tmp != m_pmt_handlers.end())
419                                         {
420                                                 eDebugCI(".");
421                                                 eDVBCISlot *tmp_cislot = tmp->cislot;
422                                                 while (!found && tmp_cislot)
423                                                 {
424                                                         eDebugCI("..");
425                                                         eServiceReferenceDVB ref2;
426                                                         tmp->pmthandler->getServiceReference(ref2);
427                                                         if ( tmp_cislot == ci_it && it->pmthandler != tmp->pmthandler )
428                                                         {
429                                                                 eDebugCI("check pmthandler %s for same service/tp", ref2.toString().c_str());
430                                                                 eDVBChannelID s1, s2;
431                                                                 if (ref != ref2)
432                                                                 {
433                                                                         eDebugCI("different services!");
434                                                                         ref.getChannelID(s1);
435                                                                         ref2.getChannelID(s2);
436                                                                 }
437                                                                 if (ref == ref2 || (s1 == s2 && canDescrambleMultipleServices(tmp_cislot->getSlotID())))
438                                                                 {
439                                                                         found = true;
440                                                                         eDebugCI("found!");
441                                                                         eDVBCISlot *tmpci = it->cislot = tmp->cislot;
442                                                                         while(tmpci)
443                                                                         {
444                                                                                 ++tmpci->use_count;
445                                                                                 eDebug("(2)CISlot %d, usecount now %d", tmpci->getSlotID(), tmpci->use_count);
446                                                                                 tmpci=tmpci->linked_next;
447                                                                         }
448                                                                 }
449                                                         }
450                                                         tmp_cislot=tmp_cislot->linked_next;
451                                                 }
452                                                 eDebugCI("...");
453                                                 ++tmp;
454                                         }
455                                 }
456
457                                 if (useThis)
458                                 {
459                                         if (ci_it->user_mapped)  // we dont like to link user mapped CIs
460                                         {
461                                                 eDebugCI("user mapped CI already in use... dont link!");
462                                                 continue;
463                                         }
464
465                                         ++ci_it->use_count;
466                                         eDebug("(1)CISlot %d, usecount now %d", ci_it->getSlotID(), ci_it->use_count);
467
468                                         data_source ci_source=CI_A;
469                                         switch(ci_it->getSlotID())
470                                         {
471                                                 case 0: ci_source = CI_A; break;
472                                                 case 1: ci_source = CI_B; break;
473                                                 case 2: ci_source = CI_C; break;
474                                                 case 3: ci_source = CI_D; break;
475                                                 default:
476                                                         eDebug("try to get source for CI %d!!\n", ci_it->getSlotID());
477                                                         break;
478                                         }
479
480                                         if (!it->cislot)
481                                         {
482                                                 int tunernum = -1;
483                                                 eUsePtr<iDVBChannel> channel;
484                                                 if (!pmthandler->getChannel(channel))
485                                                 {
486                                                         ePtr<iDVBFrontend> frontend;
487                                                         if (!channel->getFrontend(frontend))
488                                                         {
489                                                                 eDVBFrontend *fe = (eDVBFrontend*) &(*frontend);
490                                                                 tunernum = fe->getSlotID();
491                                                         }
492                                                 }
493                                                 ASSERT(tunernum != -1);
494                                                 data_source tuner_source = TUNER_A;
495                                                 switch (tunernum)
496                                                 {
497                                                         case 0: tuner_source = TUNER_A; break;
498                                                         case 1: tuner_source = TUNER_B; break;
499                                                         case 2: tuner_source = TUNER_C; break;
500                                                         case 3: tuner_source = TUNER_D; break;
501                                                         default:
502                                                                 eDebug("try to get source for tuner %d!!\n", tunernum);
503                                                                 break;
504                                                 }
505                                                 ci_it->current_tuner = tunernum;
506                                                 setInputSource(tunernum, ci_source);
507                                                 ci_it->setSource(tuner_source);
508                                         }
509                                         else
510                                         {
511                                                 ci_it->current_tuner = it->cislot->current_tuner;
512                                                 ci_it->linked_next = it->cislot;
513                                                 ci_it->setSource(ci_it->linked_next->current_source);
514                                                 ci_it->linked_next->setSource(ci_source);
515                                         }
516                                         it->cislot = ci_it;
517                                         eDebugCI("assigned!");
518                                         gotPMT(pmthandler);
519                                 }
520
521                                 if (it->cislot && user_mapped) // CI assigned to this pmthandler in this run.. and user mapped? then we break here.. we dont like to link other CIs to user mapped CIs
522                                 {
523                                         eDebugCI("user mapped CI assigned... dont link CIs!");
524                                         break;
525                                 }
526                         }
527                 }
528         }
529 }
530
531 void eDVBCIInterfaces::addPMTHandler(eDVBServicePMTHandler *pmthandler)
532 {
533         // check if this pmthandler is already registered
534         PMTHandlerList::iterator it = m_pmt_handlers.begin();
535         while (it != m_pmt_handlers.end())
536         {
537                 if ( *it++ == pmthandler )
538                         return;
539         }
540
541         eServiceReferenceDVB ref;
542         pmthandler->getServiceReference(ref);
543         eDebug("[eDVBCIInterfaces] addPMTHandler %s", ref.toString().c_str());
544
545         m_pmt_handlers.push_back(CIPmtHandler(pmthandler));
546         recheckPMTHandlers();
547 }
548
549 void eDVBCIInterfaces::removePMTHandler(eDVBServicePMTHandler *pmthandler)
550 {
551         PMTHandlerList::iterator it=std::find(m_pmt_handlers.begin(),m_pmt_handlers.end(),pmthandler);
552         if (it != m_pmt_handlers.end())
553         {
554                 eDVBCISlot *slot = it->cislot;
555                 eDVBCISlot *base_slot = slot;
556                 eDVBServicePMTHandler *pmthandler = it->pmthandler;
557                 m_pmt_handlers.erase(it);
558
559                 eServiceReferenceDVB service_to_remove;
560                 pmthandler->getServiceReference(service_to_remove);
561
562                 bool sameServiceExist=false;
563                 for (PMTHandlerList::iterator i=m_pmt_handlers.begin(); i != m_pmt_handlers.end(); ++i)
564                 {
565                         if (i->cislot)
566                         {
567                                 eServiceReferenceDVB ref;
568                                 i->pmthandler->getServiceReference(ref);
569                                 if ( ref == service_to_remove )
570                                 {
571                                         sameServiceExist=true;
572                                         break;
573                                 }
574                         }
575                 }
576
577                 while(slot)
578                 {
579                         eDVBCISlot *next = slot->linked_next;
580                         if (!sameServiceExist)
581                         {
582                                 eDebug("[eDVBCIInterfaces] remove last pmt handler for service %s send empty capmt",
583                                         service_to_remove.toString().c_str());
584                                 std::vector<uint16_t> caids;
585                                 caids.push_back(0xFFFF);
586                                 slot->sendCAPMT(pmthandler, caids);  // send a capmt without caids to remove a running service
587                                 slot->removeService(service_to_remove.getServiceID().get());
588                         }
589
590                         if (!--slot->use_count)
591                         {
592                                 if (slot->linked_next)
593                                         slot->linked_next->setSource(slot->current_source);
594                                 else
595                                         setInputSource(slot->current_tuner, slot->current_source);
596
597                                 if (base_slot != slot)
598                                 {
599                                         eDVBCISlot *tmp = it->cislot;
600                                         while(tmp->linked_next != slot)
601                                                 tmp = tmp->linked_next;
602                                         ASSERT(tmp);
603                                         if (slot->linked_next)
604                                                 tmp->linked_next = slot->linked_next;
605                                         else
606                                                 tmp->linked_next = 0;
607                                 }
608                                 else // removed old base slot.. update ptr
609                                         base_slot = slot->linked_next;
610                                 slot->linked_next = 0;
611                                 slot->user_mapped = false;
612                         }
613                         eDebug("(3) slot %d usecount is now %d", slot->getSlotID(), slot->use_count);
614                         slot = next;
615                 }
616                 // check if another service is waiting for the CI
617                 recheckPMTHandlers();
618         }
619 }
620
621 void eDVBCIInterfaces::gotPMT(eDVBServicePMTHandler *pmthandler)
622 {
623         eDebug("[eDVBCIInterfaces] gotPMT");
624         PMTHandlerList::iterator it=std::find(m_pmt_handlers.begin(), m_pmt_handlers.end(), pmthandler);
625         if (it != m_pmt_handlers.end() && it->cislot)
626         {
627                 eDVBCISlot *tmp = it->cislot;
628                 while(tmp)
629                 {
630                         eDebugCI("check slot %d %d %d", tmp->getSlotID(), tmp->running_services.empty(), canDescrambleMultipleServices(tmp->getSlotID()));
631                         if (tmp->running_services.empty() || canDescrambleMultipleServices(tmp->getSlotID()))
632                                 tmp->sendCAPMT(pmthandler);
633                         tmp = tmp->linked_next;
634                 }
635         }
636 }
637
638 int eDVBCIInterfaces::getMMIState(int slotid)
639 {
640         eDVBCISlot *slot;
641
642         if( (slot = getSlot(slotid)) == 0 )
643                 return -1;
644
645         return slot->getMMIState();
646 }
647
648 int eDVBCIInterfaces::setInputSource(int tuner_no, data_source source)
649 {
650 //      eDebug("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
651 //      eDebug("eDVBCIInterfaces::setInputSource(%d %d)", tuner_no, (int)source);
652         if (getNumOfSlots() > 1) // FIXME .. we force DM8000 when more than one CI Slot is avail
653         {
654                 char buf[64];
655                 snprintf(buf, 64, "/proc/stb/tsmux/input%d", tuner_no);
656
657                 FILE *input=0;
658                 if((input = fopen(buf, "wb")) == NULL) {
659                         eDebug("cannot open %s", buf);
660                         return 0;
661                 }
662
663                 if (tuner_no > 3)
664                         eDebug("setInputSource(%d, %d) failed... dm8000 just have four inputs", tuner_no, (int)source);
665
666                 switch(source)
667                 {
668                         case CI_A:
669                                 fprintf(input, "CI0");
670                                 break;
671                         case CI_B:
672                                 fprintf(input, "CI1");
673                                 break;
674                         case CI_C:
675                                 fprintf(input, "CI2");
676                         break;
677                         case CI_D:
678                                 fprintf(input, "CI3");
679                                 break;
680                         case TUNER_A:
681                                 fprintf(input, "A");
682                                 break;
683                         case TUNER_B:
684                                 fprintf(input, "B");
685                                 break;
686                         case TUNER_C:
687                                 fprintf(input, "C");
688                                 break;
689                         case TUNER_D:
690                                 fprintf(input, "D");
691                                 break;
692                         default:
693                                 eDebug("setInputSource for input %d failed!!!\n", (int)source);
694                                 break;
695                 }
696
697                 fclose(input);
698         }
699         else  // DM7025
700         {
701                 char buf[64];
702                 snprintf(buf, 64, "/proc/stb/tsmux/input%d", tuner_no);
703
704                 if (tuner_no > 1)
705                         eDebug("setInputSource(%d, %d) failed... dm7025 just have two inputs", tuner_no, (int)source);
706
707                 FILE *input=0;
708                 if((input = fopen(buf, "wb")) == NULL) {
709                         eDebug("cannot open %s", buf);
710                         return 0;
711                 }
712
713                 switch(source)
714                 {
715                         case CI_A:
716                                 fprintf(input, "CI");
717                                 break;
718                         case TUNER_A:
719                                 fprintf(input, "A");
720                                 break;
721                         case TUNER_B:
722                                 fprintf(input, "B");
723                                 break;
724                         default:
725                                 eDebug("setInputSource for input %d failed!!!\n", (int)source);
726                                 break;
727                 }
728
729                 fclose(input);
730         }
731         eDebug("eDVBCIInterfaces->setInputSource(%d, %d)", tuner_no, (int)source);
732         return 0;
733 }
734
735 PyObject *eDVBCIInterfaces::getDescrambleRules(int slotid)
736 {
737         eDVBCISlot *slot = getSlot(slotid);
738         if (!slot)
739         {
740                 char tmp[255];
741                 snprintf(tmp, 255, "eDVBCIInterfaces::getDescrambleRules try to get rules for CI Slot %d... but just %d slots are available", slotid, m_slots.size());
742                 PyErr_SetString(PyExc_StandardError, tmp);
743                 return 0;
744         }
745         ePyObject tuple = PyTuple_New(3);
746         int caids = slot->possible_caids.size();
747         int services = slot->possible_services.size();
748         int providers = slot->possible_providers.size();
749         ePyObject caid_list = PyList_New(caids);
750         ePyObject service_list = PyList_New(services);
751         ePyObject provider_list = PyList_New(providers);
752         caidSet::iterator caid_it(slot->possible_caids.begin());
753         while(caids)
754         {
755                 --caids;
756                 PyList_SET_ITEM(caid_list, caids, PyLong_FromLong(*caid_it));
757                 ++caid_it;
758         }
759         serviceSet::iterator ref_it(slot->possible_services.begin());
760         while(services)
761         {
762                 --services;
763                 PyList_SET_ITEM(service_list, services, PyString_FromString(ref_it->toString().c_str()));
764                 ++ref_it;
765         }
766         providerSet::iterator provider_it(slot->possible_providers.begin());
767         while(providers)
768         {
769                 ePyObject tuple = PyTuple_New(2);
770                 PyTuple_SET_ITEM(tuple, 0, PyString_FromString(provider_it->first.c_str()));
771                 PyTuple_SET_ITEM(tuple, 1, PyLong_FromUnsignedLong(provider_it->second));
772                 --providers;
773                 PyList_SET_ITEM(provider_list, providers, tuple);
774                 ++provider_it;
775         }
776         PyTuple_SET_ITEM(tuple, 0, service_list);
777         PyTuple_SET_ITEM(tuple, 1, provider_list);
778         PyTuple_SET_ITEM(tuple, 2, caid_list);
779         return tuple;
780 }
781
782 const char *PyObject_TypeStr(PyObject *o)
783 {
784         return o->ob_type && o->ob_type->tp_name ? o->ob_type->tp_name : "unknown object type";
785 }
786
787 RESULT eDVBCIInterfaces::setDescrambleRules(int slotid, SWIG_PYOBJECT(ePyObject) obj )
788 {
789         eDVBCISlot *slot = getSlot(slotid);
790         if (!slot)
791         {
792                 char tmp[255];
793                 snprintf(tmp, 255, "eDVBCIInterfaces::setDescrambleRules try to set rules for CI Slot %d... but just %d slots are available", slotid, m_slots.size());
794                 PyErr_SetString(PyExc_StandardError, tmp);
795                 return -1;
796         }
797         if (!PyTuple_Check(obj))
798         {
799                 char tmp[255];
800                 snprintf(tmp, 255, "2nd argument of setDescrambleRules is not a tuple.. it is a '%s'!!", PyObject_TypeStr(obj));
801                 PyErr_SetString(PyExc_StandardError, tmp);
802                 return -1;
803         }
804         if (PyTuple_Size(obj) != 3)
805         {
806                 const char *errstr = "eDVBCIInterfaces::setDescrambleRules not enough entrys in argument tuple!!\n"
807                         "first argument should be a pythonlist with possible services\n"
808                         "second argument should be a pythonlist with possible providers/dvbnamespace tuples\n"
809                         "third argument should be a pythonlist with possible caids";
810                 PyErr_SetString(PyExc_StandardError, errstr);
811                 return -1;
812         }
813         ePyObject service_list = PyTuple_GET_ITEM(obj, 0);
814         ePyObject provider_list = PyTuple_GET_ITEM(obj, 1);
815         ePyObject caid_list = PyTuple_GET_ITEM(obj, 2);
816         if (!PyList_Check(service_list) || !PyList_Check(provider_list) || !PyList_Check(caid_list))
817         {
818                 char errstr[512];
819                 snprintf(errstr, 512, "eDVBCIInterfaces::setDescrambleRules incorrect data types in argument tuple!!\n"
820                         "first argument(%s) should be a pythonlist with possible services (reference strings)\n"
821                         "second argument(%s) should be a pythonlist with possible providers (providername strings)\n"
822                         "third argument(%s) should be a pythonlist with possible caids (ints)",
823                         PyObject_TypeStr(service_list), PyObject_TypeStr(provider_list), PyObject_TypeStr(caid_list));
824                 PyErr_SetString(PyExc_StandardError, errstr);
825                 return -1;
826         }
827         slot->possible_caids.clear();
828         slot->possible_services.clear();
829         slot->possible_providers.clear();
830         int size = PyList_Size(service_list);
831         while(size)
832         {
833                 --size;
834                 ePyObject refstr = PyList_GET_ITEM(service_list, size);
835                 if (!PyString_Check(refstr))
836                 {
837                         char buf[255];
838                         snprintf(buf, 255, "eDVBCIInterfaces::setDescrambleRules entry in service list is not a string.. it is '%s'!!", PyObject_TypeStr(refstr));
839                         PyErr_SetString(PyExc_StandardError, buf);
840                         return -1;
841                 }
842                 char *tmpstr = PyString_AS_STRING(refstr);
843                 eServiceReference ref(tmpstr);
844                 if (ref.valid())
845                         slot->possible_services.insert(ref);
846                 else
847                         eDebug("eDVBCIInterfaces::setDescrambleRules '%s' is not a valid service reference... ignore!!", tmpstr);
848         };
849         size = PyList_Size(provider_list);
850         while(size)
851         {
852                 --size;
853                 ePyObject tuple = PyList_GET_ITEM(provider_list, size);
854                 if (!PyTuple_Check(tuple))
855                 {
856                         char buf[255];
857                         snprintf(buf, 255, "eDVBCIInterfaces::setDescrambleRules entry in provider list is not a tuple it is '%s'!!", PyObject_TypeStr(tuple));
858                         PyErr_SetString(PyExc_StandardError, buf);
859                         return -1;
860                 }
861                 if (PyTuple_Size(tuple) != 2)
862                 {
863                         char buf[255];
864                         snprintf(buf, 255, "eDVBCIInterfaces::setDescrambleRules provider tuple has %d instead of 2 entries!!", PyTuple_Size(tuple));
865                         PyErr_SetString(PyExc_StandardError, buf);
866                         return -1;
867                 }
868                 if (!PyString_Check(PyTuple_GET_ITEM(tuple, 0)))
869                 {
870                         char buf[255];
871                         snprintf(buf, 255, "eDVBCIInterfaces::setDescrambleRules 1st entry in provider tuple is not a string it is '%s'", PyObject_TypeStr(PyTuple_GET_ITEM(tuple, 0)));
872                         PyErr_SetString(PyExc_StandardError, buf);
873                         return -1;
874                 }
875                 if (!PyLong_Check(PyTuple_GET_ITEM(tuple, 1)))
876                 {
877                         char buf[255];
878                         snprintf(buf, 255, "eDVBCIInterfaces::setDescrambleRules 2nd entry in provider tuple is not a long it is '%s'", PyObject_TypeStr(PyTuple_GET_ITEM(tuple, 1)));
879                         PyErr_SetString(PyExc_StandardError, buf);
880                         return -1;
881                 }
882                 char *tmpstr = PyString_AS_STRING(PyTuple_GET_ITEM(tuple, 0));
883                 uint32_t orbpos = PyLong_AsUnsignedLong(PyTuple_GET_ITEM(tuple, 1));
884                 if (strlen(tmpstr))
885                         slot->possible_providers.insert(std::pair<std::string, uint32_t>(tmpstr, orbpos));
886                 else
887                         eDebug("eDVBCIInterfaces::setDescrambleRules ignore invalid entry in provider tuple (string is empty)!!");
888         };
889         size = PyList_Size(caid_list);
890         while(size)
891         {
892                 --size;
893                 ePyObject caid = PyList_GET_ITEM(caid_list, size);
894                 if (!PyLong_Check(caid))
895                 {
896                         char buf[255];
897                         snprintf(buf, 255, "eDVBCIInterfaces::setDescrambleRules entry in caid list is not a long it is '%s'!!", PyObject_TypeStr(caid));
898                         PyErr_SetString(PyExc_StandardError, buf);
899                         return -1;
900                 }
901                 int tmpcaid = PyLong_AsLong(caid);
902                 if (tmpcaid > 0 && tmpcaid < 0x10000)
903                         slot->possible_caids.insert(tmpcaid);
904                 else
905                         eDebug("eDVBCIInterfaces::setDescrambleRules %d is not a valid caid... ignore!!", tmpcaid);
906         };
907         return 0;
908 }
909
910 PyObject *eDVBCIInterfaces::readCICaIds(int slotid)
911 {
912         eDVBCISlot *slot = getSlot(slotid);
913         if (!slot)
914         {
915                 char tmp[255];
916                 snprintf(tmp, 255, "eDVBCIInterfaces::readCICaIds try to get CAIds for CI Slot %d... but just %d slots are available", slotid, m_slots.size());
917                 PyErr_SetString(PyExc_StandardError, tmp);
918         }
919         else
920         {
921                 int idx=0;
922                 eDVBCICAManagerSession *ca_manager = slot->getCAManager();
923                 const std::vector<uint16_t> *ci_caids = ca_manager ? &ca_manager->getCAIDs() : 0;
924                 ePyObject list = PyList_New(ci_caids ? ci_caids->size() : 0);
925                 if (ci_caids)
926                 {
927                         for (std::vector<uint16_t>::const_iterator it = ci_caids->begin(); it != ci_caids->end(); ++it)
928                                 PyList_SET_ITEM(list, idx++, PyLong_FromLong(*it));
929                 }
930                 return list;
931         }
932         return 0;
933 }
934
935 int eDVBCIInterfaces::setCIClockRate(int slotid, int rate)
936 {
937         eDVBCISlot *slot = getSlot(slotid);
938         if (slot)
939                 return slot->setClockRate(rate);
940         return -1;
941 }
942
943 int eDVBCISlot::send(const unsigned char *data, size_t len)
944 {
945         int res=0;
946         //int i;
947         //eDebugNoNewLine("< ");
948         //for(i=0;i<len;i++)
949         //      eDebugNoNewLine("%02x ",data[i]);
950         //eDebug("");
951
952         if (sendqueue.empty())
953                 res = ::write(fd, data, len);
954
955         if (res < 0 || (unsigned int)res != len)
956         {
957                 unsigned char *d = new unsigned char[len];
958                 memcpy(d, data, len);
959                 sendqueue.push( queueData(d, len) );
960                 notifier->setRequested(eSocketNotifier::Read | eSocketNotifier::Priority | eSocketNotifier::Write);
961         }
962
963         return res;
964 }
965
966 void eDVBCISlot::data(int what)
967 {
968         eDebugCI("CISlot %d what %d\n", getSlotID(), what);
969         if(what == eSocketNotifier::Priority) {
970                 if(state != stateRemoved) {
971                         state = stateRemoved;
972                         while(sendqueue.size())
973                         {
974                                 delete [] sendqueue.top().data;
975                                 sendqueue.pop();
976                         }
977                         eDVBCISession::deleteSessions(this);
978                         eDVBCIInterfaces::getInstance()->ciRemoved(this);
979                         notifier->setRequested(eSocketNotifier::Read);
980                         eDVBCI_UI::getInstance()->setState(getSlotID(),0);
981                 }
982                 return;
983         }
984
985         if (state == stateInvalid)
986                 reset();
987
988         if(state != stateInserted) {
989                 eDebug("ci inserted in slot %d", getSlotID());
990                 state = stateInserted;
991                 eDVBCI_UI::getInstance()->setState(getSlotID(),1);
992                 notifier->setRequested(eSocketNotifier::Read|eSocketNotifier::Priority);
993                 /* enable PRI to detect removal or errors */
994         }
995
996         if (what & eSocketNotifier::Read) {
997                 __u8 data[4096];
998                 int r;
999                 r = ::read(fd, data, 4096);
1000                 if(r > 0) {
1001 //                      int i;
1002 //                      eDebugNoNewLine("> ");
1003 //                      for(i=0;i<r;i++)
1004 //                              eDebugNoNewLine("%02x ",data[i]);
1005 //                      eDebug("");
1006                         eDVBCISession::receiveData(this, data, r);
1007                         eDVBCISession::pollAll();
1008                         return;
1009                 }
1010         }
1011         else if (what & eSocketNotifier::Write) {
1012                 if (!sendqueue.empty()) {
1013                         const queueData &qe = sendqueue.top();
1014                         int res = ::write(fd, qe.data, qe.len);
1015                         if (res >= 0 && (unsigned int)res == qe.len)
1016                         {
1017                                 delete [] qe.data;
1018                                 sendqueue.pop();
1019                         }
1020                 }
1021                 else
1022                         notifier->setRequested(eSocketNotifier::Read|eSocketNotifier::Priority);
1023         }
1024 }
1025
1026 DEFINE_REF(eDVBCISlot);
1027
1028 eDVBCISlot::eDVBCISlot(eMainloop *context, int nr)
1029 {
1030         char filename[128];
1031
1032         application_manager = 0;
1033         mmi_session = 0;
1034         ca_manager = 0;
1035         use_count = 0;
1036         linked_next = 0;
1037         user_mapped = false;
1038         plugged = true;
1039         
1040         slotid = nr;
1041
1042         sprintf(filename, "/dev/ci%d", nr);
1043
1044 //      possible_caids.insert(0x1702);
1045 //      possible_providers.insert(providerPair("PREMIERE", 0xC00000));
1046 //      possible_services.insert(eServiceReference("1:0:1:2A:4:85:C00000:0:0:0:"));
1047
1048         fd = ::open(filename, O_RDWR | O_NONBLOCK);
1049
1050         eDebugCI("CI Slot %d has fd %d", getSlotID(), fd);
1051         state = stateInvalid;
1052
1053         if (fd >= 0)
1054         {
1055                 notifier = eSocketNotifier::create(context, fd, eSocketNotifier::Read | eSocketNotifier::Priority | eSocketNotifier::Write);
1056                 CONNECT(notifier->activated, eDVBCISlot::data);
1057         } else
1058         {
1059                 perror(filename);
1060         }
1061 }
1062
1063 eDVBCISlot::~eDVBCISlot()
1064 {
1065         eDVBCISession::deleteSessions(this);
1066 }
1067
1068 void eDVBCISlot::setAppManager( eDVBCIApplicationManagerSession *session )
1069 {
1070         application_manager=session;
1071 }
1072
1073 void eDVBCISlot::setMMIManager( eDVBCIMMISession *session )
1074 {
1075         mmi_session = session;
1076 }
1077
1078 void eDVBCISlot::setCAManager( eDVBCICAManagerSession *session )
1079 {
1080         ca_manager = session;
1081 }
1082
1083 int eDVBCISlot::getSlotID()
1084 {
1085         return slotid;
1086 }
1087
1088 int eDVBCISlot::reset()
1089 {
1090         eDebug("CI Slot %d: reset requested", getSlotID());
1091
1092         if (state == stateInvalid)
1093         {
1094                 unsigned char buf[256];
1095                 eDebug("ci flush");
1096                 while(::read(fd, buf, 256)>0);
1097                 state = stateResetted;
1098         }
1099
1100         while(sendqueue.size())
1101         {
1102                 delete [] sendqueue.top().data;
1103                 sendqueue.pop();
1104         }
1105
1106         ioctl(fd, 0);
1107
1108         return 0;
1109 }
1110
1111 int eDVBCISlot::startMMI()
1112 {
1113         eDebug("CI Slot %d: startMMI()", getSlotID());
1114         
1115         if(application_manager)
1116                 application_manager->startMMI();
1117         
1118         return 0;
1119 }
1120
1121 int eDVBCISlot::stopMMI()
1122 {
1123         eDebug("CI Slot %d: stopMMI()", getSlotID());
1124
1125         if(mmi_session)
1126                 mmi_session->stopMMI();
1127         
1128         return 0;
1129 }
1130
1131 int eDVBCISlot::answerText(int answer)
1132 {
1133         eDebug("CI Slot %d: answerText(%d)", getSlotID(), answer);
1134
1135         if(mmi_session)
1136                 mmi_session->answerText(answer);
1137
1138         return 0;
1139 }
1140
1141 int eDVBCISlot::getMMIState()
1142 {
1143         if(mmi_session)
1144                 return 1;
1145
1146         return 0;
1147 }
1148
1149 int eDVBCISlot::answerEnq(char *value)
1150 {
1151         eDebug("CI Slot %d: answerENQ(%s)", getSlotID(), value);
1152
1153         if(mmi_session)
1154                 mmi_session->answerEnq(value);
1155
1156         return 0;
1157 }
1158
1159 int eDVBCISlot::cancelEnq()
1160 {
1161         eDebug("CI Slot %d: cancelENQ", getSlotID());
1162
1163         if(mmi_session)
1164                 mmi_session->cancelEnq();
1165
1166         return 0;
1167 }
1168
1169 int eDVBCISlot::sendCAPMT(eDVBServicePMTHandler *pmthandler, const std::vector<uint16_t> &ids)
1170 {
1171         if (!ca_manager)
1172         {
1173                 eDebug("no ca_manager (no CI plugged?)");
1174                 return -1;
1175         }
1176         const std::vector<uint16_t> &caids = ids.empty() ? ca_manager->getCAIDs() : ids;
1177         ePtr<eTable<ProgramMapSection> > ptr;
1178         if (pmthandler->getPMT(ptr))
1179                 return -1;
1180         else
1181         {
1182                 eDVBTableSpec table_spec;
1183                 ptr->getSpec(table_spec);
1184                 int pmt_version = table_spec.version & 0x1F; // just 5 bits
1185
1186                 eServiceReferenceDVB ref;
1187                 pmthandler->getServiceReference(ref);
1188                 uint16_t program_number = ref.getServiceID().get();
1189                 std::map<uint16_t, uint8_t>::iterator it =
1190                         running_services.find(program_number);
1191                 bool sendEmpty = caids.size() == 1 && caids[0] == 0xFFFF;
1192
1193                 if ( it != running_services.end() &&
1194                         (pmt_version == it->second) &&
1195                         !sendEmpty )
1196                 {
1197                         eDebug("[eDVBCISlot] dont send self capmt version twice");
1198                         return -1;
1199                 }
1200
1201                 std::vector<ProgramMapSection*>::const_iterator i=ptr->getSections().begin();
1202                 if ( i == ptr->getSections().end() )
1203                         return -1;
1204                 else
1205                 {
1206                         unsigned char raw_data[2048];
1207
1208 //                      eDebug("send %s capmt for service %04x to slot %d",
1209 //                              it != running_services.end() ? "UPDATE" : running_services.empty() ? "ONLY" : "ADD",
1210 //                              program_number, slotid);
1211
1212                         CaProgramMapSection capmt(*i++,
1213                                 it != running_services.end() ? 0x05 /*update*/ : running_services.empty() ? 0x03 /*only*/ : 0x04 /*add*/, 0x01, caids );
1214                         while( i != ptr->getSections().end() )
1215                         {
1216                 //                      eDebug("append");
1217                                 capmt.append(*i++);
1218                         }
1219                         capmt.writeToBuffer(raw_data);
1220
1221 // begin calc capmt length
1222                         int wp=0;
1223                         int hlen;
1224                         if ( raw_data[3] & 0x80 )
1225                         {
1226                                 int i=0;
1227                                 int lenbytes = raw_data[3] & ~0x80;
1228                                 while(i < lenbytes)
1229                                         wp = (wp << 8) | raw_data[4 + i++];
1230                                 wp+=4;
1231                                 wp+=lenbytes;
1232                                 hlen = 4 + lenbytes;
1233                         }
1234                         else
1235                         {
1236                                 wp = raw_data[3];
1237                                 wp+=4;
1238                                 hlen = 4;
1239                         }
1240 // end calc capmt length
1241
1242                         if (sendEmpty)
1243                         {
1244 //                              eDebugNoNewLine("SEND EMPTY CAPMT.. old version is %02x", raw_data[hlen+3]);
1245                                 if (sendEmpty && running_services.size() == 1)  // check if this is the capmt for the last running service
1246                                         raw_data[hlen] = 0x03; // send only instead of update... because of strange effects with alphacrypt
1247                                 raw_data[hlen+3] &= ~0x3E;
1248                                 raw_data[hlen+3] |= ((pmt_version+1) & 0x1F) << 1;
1249 //                              eDebug(" new version is %02x", raw_data[hlen+3]);
1250                         }
1251
1252 //                      eDebug("ca_manager %p dump capmt:", ca_manager);
1253 //                      for(int i=0;i<wp;i++)
1254 //                              eDebugNoNewLine("%02x ", raw_data[i]);
1255 //                      eDebug("");
1256
1257                         //dont need tag and lenfield
1258                         ca_manager->sendCAPMT(raw_data + hlen, wp - hlen);
1259                         running_services[program_number] = pmt_version;
1260                 }
1261         }
1262         return 0;
1263 }
1264
1265 void eDVBCISlot::removeService(uint16_t program_number)
1266 {
1267         if (program_number == 0xFFFF)
1268                 running_services.clear();  // remove all
1269         else
1270                 running_services.erase(program_number);  // remove single service
1271 }
1272
1273 int eDVBCISlot::setSource(data_source source)
1274 {
1275         current_source = source;
1276         if (eDVBCIInterfaces::getInstance()->getNumOfSlots() > 1) // FIXME .. we force DM8000 when more than one CI Slot is avail
1277         {
1278                 char buf[64];
1279                 snprintf(buf, 64, "/proc/stb/tsmux/ci%d_input", slotid);
1280                 FILE *ci = fopen(buf, "wb");
1281                 switch(source)
1282                 {
1283                         case CI_A:
1284                                 fprintf(ci, "CI0");
1285                                 break;
1286                         case CI_B:
1287                                 fprintf(ci, "CI1");
1288                                 break;
1289                         case CI_C:
1290                                 fprintf(ci, "CI2");
1291                                 break;
1292                         case CI_D:
1293                                 fprintf(ci, "CI3");
1294                                 break;
1295                         case TUNER_A:
1296                                 fprintf(ci, "A");
1297                                 break;
1298                         case TUNER_B:
1299                                 fprintf(ci, "B");
1300                                 break;
1301                         case TUNER_C:
1302                                 fprintf(ci, "C");
1303                                 break;
1304                                 case TUNER_D:
1305                                 fprintf(ci, "D");
1306                                 break;
1307                         default:
1308                                 eDebug("CI Slot %d: setSource %d failed!!!\n", getSlotID(), (int)source);
1309                                 break;
1310                 }
1311                 fclose(ci);
1312         }
1313         else // DM7025
1314         {
1315 //              eDebug("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
1316 //              eDebug("eDVBCISlot::enableTS(%d %d)", enable, (int)source);
1317                 FILE *ci = fopen("/proc/stb/tsmux/input2", "wb");
1318                 if(ci == NULL) {
1319                         eDebug("cannot open /proc/stb/tsmux/input2");
1320                         return 0;
1321                 }
1322                 if (source != TUNER_A && source != TUNER_B)
1323                         eDebug("CI Slot %d: setSource %d failed!!!\n", getSlotID(), (int)source);
1324                 else
1325                         fprintf(ci, "%s", source==TUNER_A ? "A" : "B");  // configure CI data source (TunerA, TunerB)
1326                 fclose(ci);
1327         }
1328         eDebug("CI Slot %d setSource(%d)", getSlotID(), (int)source);
1329         return 0;
1330 }
1331
1332 int eDVBCISlot::setClockRate(int rate)
1333 {
1334         char buf[64];
1335         snprintf(buf, 64, "/proc/stb/tsmux/ci%d_tsclk", slotid);
1336         FILE *ci = fopen(buf, "wb");
1337         if (ci)
1338         {
1339                 if (rate)
1340                         fprintf(ci, "high");
1341                 else
1342                         fprintf(ci, "normal");
1343                 fclose(ci);
1344                 return 0;
1345         }
1346         return -1;
1347 }
1348
1349 eAutoInitP0<eDVBCIInterfaces> init_eDVBCIInterfaces(eAutoInitNumbers::dvb, "CI Slots");