]> git.sesse.net Git - vlc/blob - modules/access/dvb/linux_dvb.c
* modules/access/dvb: Fixed link-level CAM API.
[vlc] / modules / access / dvb / linux_dvb.c
1 /*****************************************************************************
2  * linux_dvb.c : functions to control a DVB card under Linux with v4l2
3  *****************************************************************************
4  * Copyright (C) 1998-2004 the VideoLAN team
5  *
6  * Authors: Damien Lucas <nitrox@via.ecp.fr>
7  *          Johan Bilien <jobi@via.ecp.fr>
8  *          Jean-Paul Saman <jpsaman@wxs.nl>
9  *          Christopher Ross <chris@tebibyte.org>
10  *          Christophe Massiot <massiot@via.ecp.fr>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA    02111, USA.
25  *****************************************************************************/
26
27 #include <vlc/vlc.h>
28 #include <vlc/input.h>
29
30 #include <sys/ioctl.h>
31 #include <errno.h>
32
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include <time.h>
37 #include <unistd.h>
38 #include <sys/stat.h>
39 #include <sys/poll.h>
40
41 /* DVB Card Drivers */
42 #include <linux/dvb/version.h>
43 #include <linux/dvb/dmx.h>
44 #include <linux/dvb/frontend.h>
45 #include <linux/dvb/ca.h>
46
47 /* Include dvbpsi headers */
48 #ifdef HAVE_DVBPSI_DR_H
49 #   include <dvbpsi/dvbpsi.h>
50 #   include <dvbpsi/descriptor.h>
51 #   include <dvbpsi/pat.h>
52 #   include <dvbpsi/pmt.h>
53 #   include <dvbpsi/dr.h>
54 #   include <dvbpsi/psi.h>
55 #else
56 #   include "dvbpsi.h"
57 #   include "descriptor.h"
58 #   include "tables/pat.h"
59 #   include "tables/pmt.h"
60 #   include "descriptors/dr.h"
61 #   include "psi.h"
62 #endif
63
64 #include "dvb.h"
65
66 /*
67  * Frontends
68  */
69 struct frontend_t
70 {
71     fe_status_t i_last_status;
72     struct dvb_frontend_info info;
73 };
74
75 #define FRONTEND_LOCK_TIMEOUT 10000000 /* 10 s */
76
77 /* Local prototypes */
78 static int FrontendInfo( access_t * );
79 static int FrontendSetQPSK( access_t * );
80 static int FrontendSetQAM( access_t * );
81 static int FrontendSetOFDM( access_t * );
82
83 /*****************************************************************************
84  * FrontendOpen : Determine frontend device information and capabilities
85  *****************************************************************************/
86 int E_(FrontendOpen)( access_t *p_access )
87 {
88     access_sys_t *p_sys = p_access->p_sys;
89     frontend_t * p_frontend;
90     unsigned int i_adapter, i_device;
91     vlc_bool_t b_probe;
92     char frontend[128];
93
94     i_adapter = var_GetInteger( p_access, "dvb-adapter" );
95     i_device = var_GetInteger( p_access, "dvb-device" );
96     b_probe = var_GetBool( p_access, "dvb-probe" );
97
98     if( snprintf( frontend, sizeof(frontend), FRONTEND, i_adapter, i_device ) >= (int)sizeof(frontend) )
99     {
100         msg_Err( p_access, "snprintf() truncated string for FRONTEND" );
101         frontend[sizeof(frontend) - 1] = '\0';
102     }
103
104     p_sys->p_frontend = p_frontend = malloc( sizeof(frontend_t) );
105
106     msg_Dbg( p_access, "Opening device %s", frontend );
107     if( (p_sys->i_frontend_handle = open(frontend, O_RDWR | O_NONBLOCK)) < 0 )
108     {
109         msg_Err( p_access, "FrontEndOpen: opening device failed (%s)",
110                  strerror(errno) );
111         free( p_frontend );
112         return VLC_EGENERIC;
113     }
114
115     if( b_probe )
116     {
117         char * psz_expected = NULL;
118         char * psz_real;
119
120         if( FrontendInfo( p_access ) < 0 )
121         {
122             close( p_sys->i_frontend_handle );
123             free( p_frontend );
124             return VLC_EGENERIC;
125         }
126
127         switch( p_frontend->info.type )
128         {
129         case FE_OFDM:
130             psz_real = "DVB-T";
131             break;
132         case FE_QAM:
133             psz_real = "DVB-C";
134             break;
135         case FE_QPSK:
136             psz_real = "DVB-S";
137             break;
138         default:
139             psz_real = "unknown";
140         }
141
142         /* Sanity checks */
143         if( (!strncmp( p_access->psz_access, "qpsk", 4 ) ||
144              !strncmp( p_access->psz_access, "dvb-s", 5 ) ||
145              !strncmp( p_access->psz_access, "satellite", 9 ) ) &&
146              (p_frontend->info.type != FE_QPSK) )
147         {
148             psz_expected = "DVB-S";
149         }
150         if( (!strncmp( p_access->psz_access, "cable", 5 ) ||
151              !strncmp( p_access->psz_access, "dvb-c", 5 ) ) &&
152              (p_frontend->info.type != FE_QAM) )
153         {
154             psz_expected = "DVB-C";
155         }
156         if( (!strncmp( p_access->psz_access, "terrestrial", 11 ) ||
157              !strncmp( p_access->psz_access, "dvb-t", 5 ) ) &&
158              (p_frontend->info.type != FE_OFDM) )
159         {
160             psz_expected = "DVB-T";
161         }
162
163         if( psz_expected != NULL )
164         {
165             msg_Err( p_access, "the user asked for %s, and the tuner is %s",
166                      psz_expected, psz_real );
167             close( p_sys->i_frontend_handle );
168             free( p_frontend );
169             return VLC_EGENERIC;
170         }
171     }
172     else /* no frontend probing is done so use default border values. */
173     {
174         msg_Dbg( p_access, "using default values for frontend info" );
175
176         msg_Dbg( p_access, "method of access is %s", p_access->psz_access );
177         p_frontend->info.type = FE_QPSK;
178         if( !strncmp( p_access->psz_access, "qpsk", 4 ) ||
179             !strncmp( p_access->psz_access, "dvb-s", 5 ) )
180             p_frontend->info.type = FE_QPSK;
181         else if( !strncmp( p_access->psz_access, "cable", 5 ) ||
182                  !strncmp( p_access->psz_access, "dvb-c", 5 ) )
183             p_frontend->info.type = FE_QAM;
184         else if( !strncmp( p_access->psz_access, "terrestrial", 11 ) ||
185                  !strncmp( p_access->psz_access, "dvb-t", 5 ) )
186             p_frontend->info.type = FE_OFDM;
187     }
188
189     return VLC_SUCCESS;
190 }
191
192 /*****************************************************************************
193  * FrontendClose : Close the frontend
194  *****************************************************************************/
195 void E_(FrontendClose)( access_t *p_access )
196 {
197     access_sys_t *p_sys = p_access->p_sys;
198
199     if( p_sys->p_frontend )
200     {
201         close( p_sys->i_frontend_handle );
202         free( p_sys->p_frontend );
203
204         p_sys->p_frontend = NULL;
205     }
206 }
207
208 /*****************************************************************************
209  * FrontendSet : Tune !
210  *****************************************************************************/
211 int E_(FrontendSet)( access_t *p_access )
212 {
213     access_sys_t *p_sys = p_access->p_sys;
214
215     switch( p_sys->p_frontend->info.type )
216     {
217     /* DVB-S */
218     case FE_QPSK:
219         if( FrontendSetQPSK( p_access ) < 0 )
220         {
221             msg_Err( p_access, "DVB-S: tuning failed" );
222             return VLC_EGENERIC;
223         }
224         break;
225
226     /* DVB-C */
227     case FE_QAM:
228         if( FrontendSetQAM( p_access ) < 0 )
229         {
230             msg_Err( p_access, "DVB-C: tuning failed" );
231             return VLC_EGENERIC;
232         }
233         break;
234
235     /* DVB-T */
236     case FE_OFDM:
237         if( FrontendSetOFDM( p_access ) < 0 )
238         {
239             msg_Err( p_access, "DVB-T: tuning failed" );
240             return VLC_EGENERIC;
241         }
242         break;
243
244     default:
245         msg_Err( p_access, "Could not determine frontend type on %s",
246                  p_sys->p_frontend->info.name );
247         return VLC_EGENERIC;
248     }
249     p_sys->p_frontend->i_last_status = 0;
250     p_sys->i_frontend_timeout = mdate() + FRONTEND_LOCK_TIMEOUT;
251     return VLC_SUCCESS;
252 }
253
254 /*****************************************************************************
255  * FrontendPoll : Poll for frontend events
256  *****************************************************************************/
257 void E_(FrontendPoll)( access_t *p_access )
258 {
259     access_sys_t *p_sys = p_access->p_sys;
260     frontend_t * p_frontend = p_sys->p_frontend;
261     struct dvb_frontend_event event;
262     fe_status_t i_status, i_diff;
263
264     for( ;; )
265     {
266         int i_ret = ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event );
267
268         if( i_ret < 0 )
269         {
270             if( errno == EWOULDBLOCK )
271                 return;
272
273             msg_Err( p_access, "reading frontend status failed (%d) %s",
274                      i_ret, strerror(errno) );
275             continue;
276         }
277
278         i_status = event.status;
279         i_diff = i_status ^ p_frontend->i_last_status;
280         p_frontend->i_last_status = i_status;
281
282 #define IF_UP( x )                                                          \
283     }                                                                       \
284     if ( i_diff & (x) )                                                     \
285     {                                                                       \
286         if ( i_status & (x) )
287
288         {
289             IF_UP( FE_HAS_SIGNAL )
290                 msg_Dbg( p_access, "frontend has acquired signal" );
291             else
292                 msg_Dbg( p_access, "frontend has lost signal" );
293
294             IF_UP( FE_HAS_CARRIER )
295                 msg_Dbg( p_access, "frontend has acquired carrier" );
296             else
297                 msg_Dbg( p_access, "frontend has lost carrier" );
298
299             IF_UP( FE_HAS_VITERBI )
300                 msg_Dbg( p_access, "frontend has acquired stable FEC" );
301             else
302                 msg_Dbg( p_access, "frontend has lost FEC" );
303
304             IF_UP( FE_HAS_SYNC )
305                 msg_Dbg( p_access, "frontend has acquired sync" );
306             else
307                 msg_Dbg( p_access, "frontend has lost sync" );
308
309             IF_UP( FE_HAS_LOCK )
310             {
311                 int32_t i_value;
312                 msg_Dbg( p_access, "frontend has acquired lock" );
313                 p_sys->i_frontend_timeout = 0;
314
315                 /* Read some statistics */
316                 if( ioctl( p_sys->i_frontend_handle, FE_READ_BER, &i_value ) >= 0 )
317                     msg_Dbg( p_access, "- Bit error rate: %d", i_value );
318                 if( ioctl( p_sys->i_frontend_handle, FE_READ_SIGNAL_STRENGTH, &i_value ) >= 0 )
319                     msg_Dbg( p_access, "- Signal strength: %d", i_value );
320                 if( ioctl( p_sys->i_frontend_handle, FE_READ_SNR, &i_value ) >= 0 )
321                     msg_Dbg( p_access, "- SNR: %d", i_value );
322             }
323             else
324             {
325                 msg_Dbg( p_access, "frontend has lost lock" );
326                 p_sys->i_frontend_timeout = mdate() + FRONTEND_LOCK_TIMEOUT;
327             }
328
329             IF_UP( FE_REINIT )
330             {
331                 /* The frontend was reinited. */
332                 msg_Warn( p_access, "reiniting frontend");
333                 E_(FrontendSet)( p_access );
334             }
335         }
336     }
337 }
338 /*****************************************************************************
339  * FrontendInfo : Return information about given frontend
340  *****************************************************************************/
341 static int FrontendInfo( access_t *p_access )
342 {
343     access_sys_t *p_sys = p_access->p_sys;
344     frontend_t *p_frontend = p_sys->p_frontend;
345     int i_ret;
346
347     /* Determine type of frontend */
348     if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_GET_INFO,
349                         &p_frontend->info )) < 0 )
350     {
351         msg_Err( p_access, "ioctl FE_GET_INFO failed (%d) %s", i_ret,
352                  strerror(errno) );
353         return VLC_EGENERIC;
354     }
355
356     /* Print out frontend capabilities. */
357     msg_Dbg(p_access, "Frontend Info:" );
358     msg_Dbg(p_access, "  name = %s", p_frontend->info.name );
359     switch( p_frontend->info.type )
360     {
361         case FE_QPSK:
362             msg_Dbg( p_access, "  type = QPSK (DVB-S)" );
363             break;
364         case FE_QAM:
365             msg_Dbg( p_access, "  type = QAM (DVB-C)" );
366             break;
367         case FE_OFDM:
368             msg_Dbg( p_access, "  type = OFDM (DVB-T)" );
369             break;
370 #if 0 /* DVB_API_VERSION == 3 */
371         case FE_MEMORY:
372             msg_Dbg(p_access, "  type = MEMORY" );
373             break;
374         case FE_NET:
375             msg_Dbg(p_access, "  type = NETWORK" );
376             break;
377 #endif
378         default:
379             msg_Err( p_access, "  unknown frontend type (%d)",
380                      p_frontend->info.type );
381             return VLC_EGENERIC;
382     }
383     msg_Dbg(p_access, "  frequency_min = %u (kHz)",
384             p_frontend->info.frequency_min);
385     msg_Dbg(p_access, "  frequency_max = %u (kHz)",
386             p_frontend->info.frequency_max);
387     msg_Dbg(p_access, "  frequency_stepsize = %u",
388             p_frontend->info.frequency_stepsize);
389     msg_Dbg(p_access, "  frequency_tolerance = %u",
390             p_frontend->info.frequency_tolerance);
391     msg_Dbg(p_access, "  symbol_rate_min = %u (kHz)",
392             p_frontend->info.symbol_rate_min);
393     msg_Dbg(p_access, "  symbol_rate_max = %u (kHz)",
394             p_frontend->info.symbol_rate_max);
395     msg_Dbg(p_access, "  symbol_rate_tolerance (ppm) = %u",
396             p_frontend->info.symbol_rate_tolerance);
397     msg_Dbg(p_access, "  notifier_delay (ms) = %u",
398             p_frontend->info.notifier_delay );
399
400     msg_Dbg(p_access, "Frontend Info capability list:");
401     if( p_frontend->info.caps & FE_IS_STUPID)
402         msg_Dbg(p_access, "  no capabilities - frontend is stupid!");
403     if( p_frontend->info.caps & FE_CAN_INVERSION_AUTO)
404         msg_Dbg(p_access, "  inversion auto");
405     if( p_frontend->info.caps & FE_CAN_FEC_1_2)
406         msg_Dbg(p_access, "  forward error correction 1/2");
407     if( p_frontend->info.caps & FE_CAN_FEC_2_3)
408         msg_Dbg(p_access, "  forward error correction 2/3");
409     if( p_frontend->info.caps & FE_CAN_FEC_3_4)
410         msg_Dbg(p_access, "  forward error correction 3/4");
411     if( p_frontend->info.caps & FE_CAN_FEC_4_5)
412         msg_Dbg(p_access, "  forward error correction 4/5");
413     if( p_frontend->info.caps & FE_CAN_FEC_5_6)
414         msg_Dbg(p_access, "  forward error correction 5/6");
415     if( p_frontend->info.caps & FE_CAN_FEC_6_7)
416         msg_Dbg(p_access, "  forward error correction 6/7");
417     if( p_frontend->info.caps & FE_CAN_FEC_7_8)
418         msg_Dbg(p_access, "  forward error correction 7/8");
419     if( p_frontend->info.caps & FE_CAN_FEC_8_9)
420         msg_Dbg(p_access, "  forward error correction 8/9");
421     if( p_frontend->info.caps & FE_CAN_FEC_AUTO)
422         msg_Dbg(p_access, "  forward error correction auto");
423     if( p_frontend->info.caps & FE_CAN_QPSK)
424         msg_Dbg(p_access, "  card can do QPSK");
425     if( p_frontend->info.caps & FE_CAN_QAM_16)
426         msg_Dbg(p_access, "  card can do QAM 16");
427     if( p_frontend->info.caps & FE_CAN_QAM_32)
428         msg_Dbg(p_access, "  card can do QAM 32");
429     if( p_frontend->info.caps & FE_CAN_QAM_64)
430         msg_Dbg(p_access, "  card can do QAM 64");
431     if( p_frontend->info.caps & FE_CAN_QAM_128)
432         msg_Dbg(p_access, "  card can do QAM 128");
433     if( p_frontend->info.caps & FE_CAN_QAM_256)
434         msg_Dbg(p_access, "  card can do QAM 256");
435     if( p_frontend->info.caps & FE_CAN_QAM_AUTO)
436         msg_Dbg(p_access, "  card can do QAM auto");
437     if( p_frontend->info.caps & FE_CAN_TRANSMISSION_MODE_AUTO)
438         msg_Dbg(p_access, "  transmission mode auto");
439     if( p_frontend->info.caps & FE_CAN_BANDWIDTH_AUTO)
440         msg_Dbg(p_access, "  bandwidth mode auto");
441     if( p_frontend->info.caps & FE_CAN_GUARD_INTERVAL_AUTO)
442         msg_Dbg(p_access, "  guard interval mode auto");
443     if( p_frontend->info.caps & FE_CAN_HIERARCHY_AUTO)
444         msg_Dbg(p_access, "  hierarchy mode auto");
445     if( p_frontend->info.caps & FE_CAN_MUTE_TS)
446         msg_Dbg(p_access, "  card can mute TS");
447     if( p_frontend->info.caps & FE_CAN_RECOVER)
448         msg_Dbg(p_access, "  card can recover from a cable unplug");
449     msg_Dbg(p_access, "End of capability list");
450
451     return VLC_SUCCESS;
452 }
453
454 /*****************************************************************************
455  * Decoding the DVB parameters (common)
456  *****************************************************************************/
457 static fe_spectral_inversion_t DecodeInversion( access_t *p_access )
458 {
459     vlc_value_t         val;
460     fe_spectral_inversion_t fe_inversion = 0;
461
462     var_Get( p_access, "dvb-inversion", &val );
463     msg_Dbg( p_access, "using inversion=%d", val.i_int );
464
465     switch( val.i_int )
466     {
467         case 0: fe_inversion = INVERSION_OFF; break;
468         case 1: fe_inversion = INVERSION_ON; break;
469         case 2: fe_inversion = INVERSION_AUTO; break;
470         default:
471             msg_Dbg( p_access, "dvb has inversion not set, using auto");
472             fe_inversion = INVERSION_AUTO;
473             break;
474     }
475     return fe_inversion;
476 }
477
478 static fe_code_rate_t DecodeFEC( access_t *p_access, int i_val )
479 {
480     fe_code_rate_t      fe_fec = FEC_NONE;
481
482     msg_Dbg( p_access, "using fec=%d", i_val );
483
484     switch( i_val )
485     {
486         case 0: fe_fec = FEC_NONE; break;
487         case 1: fe_fec = FEC_1_2; break;
488         case 2: fe_fec = FEC_2_3; break;
489         case 3: fe_fec = FEC_3_4; break;
490         case 4: fe_fec = FEC_4_5; break;
491         case 5: fe_fec = FEC_5_6; break;
492         case 6: fe_fec = FEC_6_7; break;
493         case 7: fe_fec = FEC_7_8; break;
494         case 8: fe_fec = FEC_8_9; break;
495         case 9: fe_fec = FEC_AUTO; break;
496         default:
497             /* cannot happen */
498             fe_fec = FEC_NONE;
499             msg_Err( p_access, "argument has invalid FEC (%d)", i_val);
500             break;
501     }
502     return fe_fec;
503 }
504
505 static fe_modulation_t DecodeModulation( access_t *p_access )
506 {
507     vlc_value_t         val;
508     fe_modulation_t     fe_modulation = 0;
509
510     var_Get( p_access, "dvb-modulation", &val );
511
512     switch( val.i_int )
513     {
514         case -1: fe_modulation = QPSK; break;
515         case 0: fe_modulation = QAM_AUTO; break;
516         case 16: fe_modulation = QAM_16; break;
517         case 32: fe_modulation = QAM_32; break;
518         case 64: fe_modulation = QAM_64; break;
519         case 128: fe_modulation = QAM_128; break;
520         case 256: fe_modulation = QAM_256; break;
521         default:
522             msg_Dbg( p_access, "terrestrial/cable dvb has constellation/modulation not set, using auto");
523             fe_modulation = QAM_AUTO;
524             break;
525     }
526     return fe_modulation;
527 }
528
529 /*****************************************************************************
530  * FrontendSetQPSK : controls the FE device
531  *****************************************************************************/
532 static fe_sec_voltage_t DecodeVoltage( access_t *p_access )
533 {
534     vlc_value_t         val;
535     fe_sec_voltage_t    fe_voltage;
536
537     var_Get( p_access, "dvb-voltage", &val );
538     msg_Dbg( p_access, "using voltage=%d", val.i_int );
539
540     switch( val.i_int )
541     {
542         case 0: fe_voltage = SEC_VOLTAGE_OFF; break;
543         case 13: fe_voltage = SEC_VOLTAGE_13; break;
544         case 18: fe_voltage = SEC_VOLTAGE_18; break;
545         default:
546             fe_voltage = SEC_VOLTAGE_OFF;
547             msg_Err( p_access, "argument has invalid voltage (%d)", val.i_int );
548             break;
549     }
550     return fe_voltage;
551 }
552
553 static fe_sec_tone_mode_t DecodeTone( access_t *p_access )
554 {
555     vlc_value_t         val;
556     fe_sec_tone_mode_t  fe_tone;
557
558     var_Get( p_access, "dvb-tone", &val );
559     msg_Dbg( p_access, "using tone=%d", val.i_int );
560
561     switch( val.i_int )
562     {
563         case 0: fe_tone = SEC_TONE_OFF; break;
564         case 1: fe_tone = SEC_TONE_ON; break;
565         default:
566             fe_tone = SEC_TONE_OFF;
567             msg_Err( p_access, "argument has invalid tone mode (%d)", val.i_int);
568             break;
569     }
570     return fe_tone;
571 }
572
573 struct diseqc_cmd_t
574 {
575     struct dvb_diseqc_master_cmd cmd;
576     uint32_t wait;
577 };
578
579 static int DoDiseqc( access_t *p_access )
580 {
581     access_sys_t *p_sys = p_access->p_sys;
582     vlc_value_t val;
583     int i_frequency, i_lnb_slof;
584     fe_sec_voltage_t fe_voltage;
585     fe_sec_tone_mode_t fe_tone;
586     int i_err;
587
588     var_Get( p_access, "dvb-frequency", &val );
589     i_frequency = val.i_int;
590     var_Get( p_access, "dvb-lnb-slof", &val );
591     i_lnb_slof = val.i_int;
592
593     var_Get( p_access, "dvb-tone", &val );
594     if( val.i_int == -1 /* auto */ )
595     {
596         if( i_frequency >= i_lnb_slof )
597             val.i_int = 1;
598         else
599             val.i_int = 0;
600         var_Set( p_access, "dvb-tone", val );
601     }
602
603     fe_voltage = DecodeVoltage( p_access );
604     fe_tone = DecodeTone( p_access );
605
606     /* Switch off continuous tone. */
607     if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_TONE, SEC_TONE_OFF )) < 0 )
608     {
609         msg_Err( p_access, "ioctl FE_SET_TONE failed, tone=%s (%d) %s",
610                  fe_tone == SEC_TONE_ON ? "on" : "off", i_err,
611                  strerror(errno) );
612         return i_err;
613     }
614
615     /* Configure LNB voltage. */
616     if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_VOLTAGE, fe_voltage )) < 0 )
617     {
618         msg_Err( p_access, "ioctl FE_SET_VOLTAGE failed, voltage=%d (%d) %s",
619                  fe_voltage, i_err, strerror(errno) );
620         return i_err;
621     }
622
623     var_Get( p_access, "dvb-high-voltage", &val );
624     if( (i_err = ioctl( p_sys->i_frontend_handle, FE_ENABLE_HIGH_LNB_VOLTAGE,
625                         val.b_bool )) < 0 && val.b_bool )
626     {
627         msg_Err( p_access,
628                  "ioctl FE_ENABLE_HIGH_LNB_VOLTAGE failed, val=%d (%d) %s",
629                  val.b_bool, i_err, strerror(errno) );
630     }
631
632     /* Wait for at least 15 ms. */
633     msleep(15000);
634
635     var_Get( p_access, "dvb-satno", &val );
636     if( val.i_int > 0 && val.i_int < 5 )
637     {
638         /* digital satellite equipment control,
639          * specification is available from http://www.eutelsat.com/
640          */
641
642         /* 1.x compatible equipment */
643         struct diseqc_cmd_t cmd =  { {{0xe0, 0x10, 0x38, 0xf0, 0x00, 0x00}, 4}, 0 };
644
645         /* param: high nibble: reset bits, low nibble set bits,
646          * bits are: option, position, polarization, band
647          */
648         cmd.cmd.msg[3] = 0xf0 /* reset bits */
649                           | (((val.i_int - 1) * 4) & 0xc)
650                           | (fe_voltage == SEC_VOLTAGE_13 ? 0 : 2)
651                           | (fe_tone == SEC_TONE_ON ? 1 : 0);
652
653         if( (i_err = ioctl( p_sys->i_frontend_handle, FE_DISEQC_SEND_MASTER_CMD,
654                            &cmd.cmd )) < 0 )
655         {
656             msg_Err( p_access, "ioctl FE_SEND_MASTER_CMD failed (%d) %s",
657                      i_err, strerror(errno) );
658             return i_err;
659         }
660
661         msleep(15000 + cmd.wait * 1000);
662
663         /* A or B simple diseqc ("diseqc-compatible") */
664         if( (i_err = ioctl( p_sys->i_frontend_handle, FE_DISEQC_SEND_BURST,
665                       ((val.i_int - 1) % 2) ? SEC_MINI_B : SEC_MINI_A )) < 0 )
666         {
667             msg_Err( p_access, "ioctl FE_SEND_BURST failed (%d) %s",
668                      i_err, strerror(errno) );
669             return i_err;
670         }
671
672         msleep(15000);
673     }
674
675     if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_TONE, fe_tone )) < 0 )
676     {
677         msg_Err( p_access, "ioctl FE_SET_TONE failed, tone=%s (%d) %s",
678                  fe_tone == SEC_TONE_ON ? "on" : "off", i_err,
679                  strerror(errno) );
680         return i_err;
681     }
682
683     msleep(50000);
684     return 0;
685 }
686
687 static int FrontendSetQPSK( access_t *p_access )
688 {
689     access_sys_t *p_sys = p_access->p_sys;
690     struct dvb_frontend_parameters fep;
691     int i_ret;
692     vlc_value_t val;
693     int i_frequency, i_lnb_slof = 0, i_lnb_lof1, i_lnb_lof2 = 0;
694
695     /* Prepare the fep structure */
696     var_Get( p_access, "dvb-frequency", &val );
697     i_frequency = val.i_int;
698
699     var_Get( p_access, "dvb-lnb-lof1", &val );
700     if ( val.i_int == 0 )
701     {
702         /* Automatic mode. */
703         if ( i_frequency >= 950000 && i_frequency <= 2150000 )
704         {
705             msg_Dbg( p_access, "frequency %d is in IF-band", i_frequency );
706             i_lnb_lof1 = 0;
707         }
708         else if ( i_frequency >= 2500000 && i_frequency <= 2700000 )
709         {
710             msg_Dbg( p_access, "frequency %d is in S-band", i_frequency );
711             i_lnb_lof1 = 3650000;
712         }
713         else if ( i_frequency >= 3400000 && i_frequency <= 4200000 )
714         {
715             msg_Dbg( p_access, "frequency %d is in C-band (lower)",
716                      i_frequency );
717             i_lnb_lof1 = 5150000;
718         }
719         else if ( i_frequency >= 4500000 && i_frequency <= 4800000 )
720         {
721             msg_Dbg( p_access, "frequency %d is in C-band (higher)",
722                      i_frequency );
723             i_lnb_lof1 = 5950000;
724         }
725         else if ( i_frequency >= 10700000 && i_frequency <= 13250000 )
726         {
727             msg_Dbg( p_access, "frequency %d is in Ku-band",
728                      i_frequency );
729             i_lnb_lof1 = 9750000;
730             i_lnb_lof2 = 10600000;
731             i_lnb_slof = 11700000;
732         }
733         else
734         {
735             msg_Err( p_access, "frequency %d is out of any known band",
736                      i_frequency );
737             msg_Err( p_access, "specify dvb-lnb-lof1 manually for the local "
738                      "oscillator frequency" );
739             return VLC_EGENERIC;
740         }
741         val.i_int = i_lnb_lof1;
742         var_Set( p_access, "dvb-lnb-lof1", val );
743         val.i_int = i_lnb_lof2;
744         var_Set( p_access, "dvb-lnb-lof2", val );
745         val.i_int = i_lnb_slof;
746         var_Set( p_access, "dvb-lnb-slof", val );
747     }
748     else
749     {
750         i_lnb_lof1 = val.i_int;
751         var_Get( p_access, "dvb-lnb-lof2", &val );
752         i_lnb_lof2 = val.i_int;
753         var_Get( p_access, "dvb-lnb-slof", &val );
754         i_lnb_slof = val.i_int;
755     }
756
757     if( i_lnb_slof && i_frequency >= i_lnb_slof )
758     {
759         i_frequency -= i_lnb_lof2;
760     }
761     else
762     {
763         i_frequency -= i_lnb_lof1;
764     }
765     fep.frequency = i_frequency >= 0 ? i_frequency : -i_frequency;
766
767     fep.inversion = DecodeInversion( p_access );
768
769     var_Get( p_access, "dvb-srate", &val );
770     fep.u.qpsk.symbol_rate = val.i_int;
771
772     var_Get( p_access, "dvb-fec", &val );
773     fep.u.qpsk.fec_inner = DecodeFEC( p_access, val.i_int );
774
775     if( DoDiseqc( p_access ) < 0 )
776     {
777         return VLC_EGENERIC;
778     }
779
780     /* Empty the event queue */
781     for( ; ; )
782     {
783         struct dvb_frontend_event event;
784         if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
785               && errno == EWOULDBLOCK )
786             break;
787     }
788
789     /* Now send it all to the frontend device */
790     if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
791     {
792         msg_Err( p_access, "DVB-S: setting frontend failed (%d) %s", i_ret,
793                  strerror(errno) );
794         return VLC_EGENERIC;
795     }
796
797     return VLC_SUCCESS;
798 }
799
800 /*****************************************************************************
801  * FrontendSetQAM : controls the FE device
802  *****************************************************************************/
803 static int FrontendSetQAM( access_t *p_access )
804 {
805     access_sys_t *p_sys = p_access->p_sys;
806     struct dvb_frontend_parameters fep;
807     vlc_value_t val;
808     int i_ret;
809
810     /* Prepare the fep structure */
811
812     var_Get( p_access, "dvb-frequency", &val );
813     fep.frequency = val.i_int;
814
815     fep.inversion = DecodeInversion( p_access );
816
817     var_Get( p_access, "dvb-srate", &val );
818     fep.u.qam.symbol_rate = val.i_int;
819
820     var_Get( p_access, "dvb-fec", &val );
821     fep.u.qam.fec_inner = DecodeFEC( p_access, val.i_int );
822
823     fep.u.qam.modulation = DecodeModulation( p_access );
824
825     /* Empty the event queue */
826     for( ; ; )
827     {
828         struct dvb_frontend_event event;
829         if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
830               && errno == EWOULDBLOCK )
831             break;
832     }
833
834     /* Now send it all to the frontend device */
835     if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
836     {
837         msg_Err( p_access, "DVB-C: setting frontend failed (%d) %s", i_ret,
838                  strerror(errno) );
839         return VLC_EGENERIC;
840     }
841
842     return VLC_SUCCESS;
843 }
844
845 /*****************************************************************************
846  * FrontendSetOFDM : controls the FE device
847  *****************************************************************************/
848 static fe_bandwidth_t DecodeBandwidth( access_t *p_access )
849 {
850     vlc_value_t         val;
851     fe_bandwidth_t      fe_bandwidth = 0;
852
853     var_Get( p_access, "dvb-bandwidth", &val );
854     msg_Dbg( p_access, "using bandwidth=%d", val.i_int );
855
856     switch( val.i_int )
857     {
858         case 0: fe_bandwidth = BANDWIDTH_AUTO; break;
859         case 6: fe_bandwidth = BANDWIDTH_6_MHZ; break;
860         case 7: fe_bandwidth = BANDWIDTH_7_MHZ; break;
861         case 8: fe_bandwidth = BANDWIDTH_8_MHZ; break;
862         default:
863             msg_Dbg( p_access, "terrestrial dvb has bandwidth not set, using auto" );
864             fe_bandwidth = BANDWIDTH_AUTO;
865             break;
866     }
867     return fe_bandwidth;
868 }
869
870 static fe_transmit_mode_t DecodeTransmission( access_t *p_access )
871 {
872     vlc_value_t         val;
873     fe_transmit_mode_t  fe_transmission = 0;
874
875     var_Get( p_access, "dvb-transmission", &val );
876     msg_Dbg( p_access, "using transmission=%d", val.i_int );
877
878     switch( val.i_int )
879     {
880         case 0: fe_transmission = TRANSMISSION_MODE_AUTO; break;
881         case 2: fe_transmission = TRANSMISSION_MODE_2K; break;
882         case 8: fe_transmission = TRANSMISSION_MODE_8K; break;
883         default:
884             msg_Dbg( p_access, "terrestrial dvb has transmission mode not set, using auto");
885             fe_transmission = TRANSMISSION_MODE_AUTO;
886             break;
887     }
888     return fe_transmission;
889 }
890
891 static fe_guard_interval_t DecodeGuardInterval( access_t *p_access )
892 {
893     vlc_value_t         val;
894     fe_guard_interval_t fe_guard = 0;
895
896     var_Get( p_access, "dvb-guard", &val );
897     msg_Dbg( p_access, "using guard=%d", val.i_int );
898
899     switch( val.i_int )
900     {
901         case 0: fe_guard = GUARD_INTERVAL_AUTO; break;
902         case 4: fe_guard = GUARD_INTERVAL_1_4; break;
903         case 8: fe_guard = GUARD_INTERVAL_1_8; break;
904         case 16: fe_guard = GUARD_INTERVAL_1_16; break;
905         case 32: fe_guard = GUARD_INTERVAL_1_32; break;
906         default:
907             msg_Dbg( p_access, "terrestrial dvb has guard interval not set, using auto");
908             fe_guard = GUARD_INTERVAL_AUTO;
909             break;
910     }
911     return fe_guard;
912 }
913
914 static fe_hierarchy_t DecodeHierarchy( access_t *p_access )
915 {
916     vlc_value_t         val;
917     fe_hierarchy_t      fe_hierarchy = 0;
918
919     var_Get( p_access, "dvb-hierarchy", &val );
920     msg_Dbg( p_access, "using hierarchy=%d", val.i_int );
921
922     switch( val.i_int )
923     {
924         case -1: fe_hierarchy = HIERARCHY_NONE; break;
925         case 0: fe_hierarchy = HIERARCHY_AUTO; break;
926         case 1: fe_hierarchy = HIERARCHY_1; break;
927         case 2: fe_hierarchy = HIERARCHY_2; break;
928         case 4: fe_hierarchy = HIERARCHY_4; break;
929         default:
930             msg_Dbg( p_access, "terrestrial dvb has hierarchy not set, using auto");
931             fe_hierarchy = HIERARCHY_AUTO;
932             break;
933     }
934     return fe_hierarchy;
935 }
936
937 static int FrontendSetOFDM( access_t * p_access )
938 {
939     access_sys_t *p_sys = p_access->p_sys;
940     struct dvb_frontend_parameters fep;
941     vlc_value_t val;
942     int ret;
943
944     /* Prepare the fep structure */
945
946     var_Get( p_access, "dvb-frequency", &val );
947     fep.frequency = val.i_int;
948
949     fep.inversion = DecodeInversion( p_access );
950
951     fep.u.ofdm.bandwidth = DecodeBandwidth( p_access );
952     var_Get( p_access, "dvb-code-rate-hp", &val );
953     fep.u.ofdm.code_rate_HP = DecodeFEC( p_access, val.i_int );
954     var_Get( p_access, "dvb-code-rate-lp", &val );
955     fep.u.ofdm.code_rate_LP = DecodeFEC( p_access, val.i_int );
956     fep.u.ofdm.constellation = DecodeModulation( p_access );
957     fep.u.ofdm.transmission_mode = DecodeTransmission( p_access );
958     fep.u.ofdm.guard_interval = DecodeGuardInterval( p_access );
959     fep.u.ofdm.hierarchy_information = DecodeHierarchy( p_access );
960
961     /* Empty the event queue */
962     for( ; ; )
963     {
964         struct dvb_frontend_event event;
965         if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
966               && errno == EWOULDBLOCK )
967             break;
968     }
969
970     /* Now send it all to the frontend device */
971     if( (ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
972     {
973         msg_Err( p_access, "DVB-T: setting frontend failed (%d) %s", ret,
974                  strerror(errno) );
975         return -1;
976     }
977
978     return VLC_SUCCESS;
979 }
980
981
982 /*
983  * Demux
984  */
985
986 /*****************************************************************************
987  * DMXSetFilter : controls the demux to add a filter
988  *****************************************************************************/
989 int E_(DMXSetFilter)( access_t * p_access, int i_pid, int * pi_fd, int i_type )
990 {
991     struct dmx_pes_filter_params s_filter_params;
992     int i_ret;
993     unsigned int i_adapter, i_device;
994     char dmx[128];
995     vlc_value_t val;
996
997     var_Get( p_access, "dvb-adapter", &val );
998     i_adapter = val.i_int;
999     var_Get( p_access, "dvb-device", &val );
1000     i_device = val.i_int;
1001
1002     if( snprintf( dmx, sizeof(dmx), DMX, i_adapter, i_device )
1003             >= (int)sizeof(dmx) )
1004     {
1005         msg_Err( p_access, "snprintf() truncated string for DMX" );
1006         dmx[sizeof(dmx) - 1] = '\0';
1007     }
1008
1009     msg_Dbg( p_access, "Opening device %s", dmx );
1010     if( (*pi_fd = open(dmx, O_RDWR)) < 0 )
1011     {
1012         msg_Err( p_access, "DMXSetFilter: opening device failed (%s)",
1013                  strerror(errno) );
1014         return VLC_EGENERIC;
1015     }
1016
1017     /* We fill the DEMUX structure : */
1018     s_filter_params.pid     =   i_pid;
1019     s_filter_params.input   =   DMX_IN_FRONTEND;
1020     s_filter_params.output  =   DMX_OUT_TS_TAP;
1021     s_filter_params.flags   =   DMX_IMMEDIATE_START;
1022
1023     switch ( i_type )
1024     {   /* First device */
1025         case 1:
1026             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO0 for PID %d", i_pid);
1027             s_filter_params.pes_type = DMX_PES_VIDEO0;
1028             break;
1029         case 2:
1030             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO0 for PID %d", i_pid);
1031             s_filter_params.pes_type = DMX_PES_AUDIO0;
1032             break;
1033         case 3:
1034             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT0 for PID %d", i_pid);
1035             s_filter_params.pes_type = DMX_PES_TELETEXT0;
1036             break;
1037         case 4:
1038             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE0 for PID %d", i_pid);
1039             s_filter_params.pes_type = DMX_PES_SUBTITLE0;
1040             break;
1041         case 5:
1042             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR0 for PID %d", i_pid);
1043             s_filter_params.pes_type = DMX_PES_PCR0;
1044             break;
1045         /* Second device */
1046         case 6:
1047             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO1 for PID %d", i_pid);
1048             s_filter_params.pes_type = DMX_PES_VIDEO1;
1049             break;
1050         case 7:
1051             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO1 for PID %d", i_pid);
1052             s_filter_params.pes_type = DMX_PES_AUDIO1;
1053             break;
1054         case 8:
1055             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT1 for PID %d", i_pid);
1056             s_filter_params.pes_type = DMX_PES_TELETEXT1;
1057             break;
1058         case 9:
1059             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE1 for PID %d", i_pid);
1060             s_filter_params.pes_type = DMX_PES_SUBTITLE1;
1061             break;
1062         case 10:
1063             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR1 for PID %d", i_pid);
1064             s_filter_params.pes_type = DMX_PES_PCR1;
1065             break;
1066         /* Third device */
1067         case 11:
1068             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO2 for PID %d", i_pid);
1069             s_filter_params.pes_type = DMX_PES_VIDEO2;
1070             break;
1071         case 12:
1072             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO2 for PID %d", i_pid);
1073             s_filter_params.pes_type = DMX_PES_AUDIO2;
1074             break;
1075         case 13:
1076             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT2 for PID %d", i_pid);
1077             s_filter_params.pes_type = DMX_PES_TELETEXT2;
1078             break;
1079         case 14:
1080             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE2 for PID %d", i_pid);
1081             s_filter_params.pes_type = DMX_PES_SUBTITLE2;
1082             break;
1083         case 15:
1084             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR2 for PID %d", i_pid);
1085             s_filter_params.pes_type = DMX_PES_PCR2;
1086             break;
1087         /* Forth device */
1088         case 16:
1089             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO3 for PID %d", i_pid);
1090             s_filter_params.pes_type = DMX_PES_VIDEO3;
1091             break;
1092         case 17:
1093             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO3 for PID %d", i_pid);
1094             s_filter_params.pes_type = DMX_PES_AUDIO3;
1095             break;
1096         case 18:
1097             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT3 for PID %d", i_pid);
1098             s_filter_params.pes_type = DMX_PES_TELETEXT3;
1099             break;
1100         case 19:
1101             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE3 for PID %d", i_pid);
1102             s_filter_params.pes_type = DMX_PES_SUBTITLE3;
1103             break;
1104         case 20:
1105             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR3 for PID %d", i_pid);
1106             s_filter_params.pes_type = DMX_PES_PCR3;
1107             break;
1108         /* Usually used by Nova cards */
1109         case 21:
1110         default:
1111             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_OTHER for PID %d", i_pid);
1112             s_filter_params.pes_type = DMX_PES_OTHER;
1113             break;
1114     }
1115
1116     /* We then give the order to the device : */
1117     if( (i_ret = ioctl( *pi_fd, DMX_SET_PES_FILTER, &s_filter_params )) < 0 )
1118     {
1119         msg_Err( p_access, "DMXSetFilter: failed with %d (%s)", i_ret,
1120                  strerror(errno) );
1121         return VLC_EGENERIC;
1122     }
1123     return VLC_SUCCESS;
1124 }
1125
1126 /*****************************************************************************
1127  * DMXUnsetFilter : removes a filter
1128  *****************************************************************************/
1129 int E_(DMXUnsetFilter)( access_t * p_access, int i_fd )
1130 {
1131     int i_ret;
1132
1133     if( (i_ret = ioctl( i_fd, DMX_STOP )) < 0 )
1134     {
1135         msg_Err( p_access, "DMX_STOP failed for demux (%d) %s",
1136                  i_ret, strerror(errno) );
1137         return i_ret;
1138     }
1139
1140     msg_Dbg( p_access, "DMXUnsetFilter: closing demux %d", i_fd );
1141     close( i_fd );
1142     return VLC_SUCCESS;
1143 }
1144
1145
1146 /*
1147  * DVR device
1148  */
1149
1150 /*****************************************************************************
1151  * DVROpen :
1152  *****************************************************************************/
1153 int E_(DVROpen)( access_t * p_access )
1154 {
1155     access_sys_t *p_sys = p_access->p_sys;
1156     unsigned int i_adapter, i_device;
1157     char dvr[128];
1158     vlc_value_t val;
1159
1160     var_Get( p_access, "dvb-adapter", &val );
1161     i_adapter = val.i_int;
1162     var_Get( p_access, "dvb-device", &val );
1163     i_device = val.i_int;
1164
1165     if( snprintf( dvr, sizeof(dvr), DVR, i_adapter, i_device )
1166             >= (int)sizeof(dvr) )
1167     {
1168         msg_Err( p_access, "snprintf() truncated string for DVR" );
1169         dvr[sizeof(dvr) - 1] = '\0';
1170     }
1171
1172     msg_Dbg( p_access, "Opening device %s", dvr );
1173     if( (p_sys->i_handle = open(dvr, O_RDONLY)) < 0 )
1174     {
1175         msg_Err( p_access, "DVROpen: opening device failed (%s)",
1176                  strerror(errno) );
1177         return VLC_EGENERIC;
1178     }
1179
1180     if( fcntl( p_sys->i_handle, F_SETFL, O_NONBLOCK ) == -1 )
1181     {
1182         msg_Warn( p_access, "DVROpen: couldn't set non-blocking mode (%s)",
1183                   strerror(errno) );
1184     }
1185
1186     return VLC_SUCCESS;
1187 }
1188
1189 /*****************************************************************************
1190  * DVRClose :
1191  *****************************************************************************/
1192 void E_(DVRClose)( access_t * p_access )
1193 {
1194     access_sys_t *p_sys = p_access->p_sys;
1195
1196     close( p_sys->i_handle );
1197 }
1198
1199
1200 /*
1201  * CAM device
1202  */
1203
1204 /*****************************************************************************
1205  * CAMOpen :
1206  *****************************************************************************/
1207 int E_(CAMOpen)( access_t *p_access )
1208 {
1209     access_sys_t *p_sys = p_access->p_sys;
1210     char ca[128];
1211     int i_adapter, i_device;
1212     ca_caps_t caps;
1213
1214     i_adapter = var_GetInteger( p_access, "dvb-adapter" );
1215     i_device = var_GetInteger( p_access, "dvb-device" );
1216
1217     if( snprintf( ca, sizeof(ca), CA, i_adapter, i_device ) >= (int)sizeof(ca) )
1218     {
1219         msg_Err( p_access, "snprintf() truncated string for CA" );
1220         ca[sizeof(ca) - 1] = '\0';
1221     }
1222     memset( &caps, 0, sizeof( ca_caps_t ));
1223
1224     msg_Dbg( p_access, "Opening device %s", ca );
1225     if( (p_sys->i_ca_handle = open(ca, O_RDWR | O_NONBLOCK)) < 0 )
1226     {
1227         msg_Warn( p_access, "CAMInit: opening CAM device failed (%s)",
1228                   strerror(errno) );
1229         p_sys->i_ca_handle = 0;
1230         return VLC_EGENERIC;
1231     }
1232
1233     if ( ioctl( p_sys->i_ca_handle, CA_GET_CAP, &caps ) != 0 )
1234     {
1235         msg_Err( p_access, "CAMInit: ioctl() error getting CAM capabilities" );
1236         close( p_sys->i_ca_handle );
1237         p_sys->i_ca_handle = 0;
1238         return VLC_EGENERIC;
1239     }
1240
1241     /* Output CA capabilities */
1242     msg_Dbg( p_access, "CAMInit: CA interface with %d %s", caps.slot_num, 
1243         caps.slot_num == 1 ? "slot" : "slots" );
1244     if ( caps.slot_type & CA_CI )
1245         msg_Dbg( p_access, "CAMInit: CI high level interface type (not supported)" );
1246     if ( caps.slot_type & CA_CI_LINK )
1247         msg_Dbg( p_access, "CAMInit: CI link layer level interface type" );
1248     if ( caps.slot_type & CA_CI_PHYS )
1249         msg_Dbg( p_access, "CAMInit: CI physical layer level interface type (not supported) " );
1250     if ( caps.slot_type & CA_DESCR )
1251         msg_Dbg( p_access, "CAMInit: built-in descrambler detected" );
1252     if ( caps.slot_type & CA_SC )
1253         msg_Dbg( p_access, "CAMInit: simple smart card interface" );
1254
1255     msg_Dbg( p_access, "CAMInit: %d available %s", caps.descr_num,
1256         caps.descr_num == 1 ? "descrambler (key)" : "descramblers (keys)" );
1257     if ( caps.descr_type & CA_ECD )
1258         msg_Dbg( p_access, "CAMInit: ECD scrambling system supported" );
1259     if ( caps.descr_type & CA_NDS )
1260         msg_Dbg( p_access, "CAMInit: NDS scrambling system supported" );
1261     if ( caps.descr_type & CA_DSS )
1262         msg_Dbg( p_access, "CAMInit: DSS scrambling system supported" );
1263  
1264     if ( caps.slot_num == 0 )
1265     {
1266         msg_Err( p_access, "CAMInit: CAM module with no slots" );
1267         close( p_sys->i_ca_handle );
1268         p_sys->i_ca_handle = 0;
1269         return VLC_EGENERIC;
1270     }
1271
1272     p_sys->i_ca_type = caps.slot_type;
1273     if ( !(caps.slot_type & CA_CI_LINK) &&
1274          !(caps.slot_type & CA_CI) )
1275     {
1276         msg_Err( p_access, "CAMInit: incompatible CAM interface" );
1277         close( p_sys->i_ca_handle );
1278         p_sys->i_ca_handle = 0;
1279         return VLC_EGENERIC;
1280     }
1281
1282     p_sys->i_nb_slots = caps.slot_num;
1283     memset( p_sys->pb_active_slot, 0, sizeof(vlc_bool_t) * MAX_CI_SLOTS );
1284
1285     return E_(en50221_Init)( p_access );
1286 }
1287
1288 /*****************************************************************************
1289  * CAMPoll :
1290  *****************************************************************************/
1291 int E_(CAMPoll)( access_t * p_access )
1292 {
1293     access_sys_t *p_sys = p_access->p_sys;
1294     int i_ret = VLC_EGENERIC;
1295
1296     if ( p_sys->i_ca_handle == 0 )
1297     {
1298         return VLC_EGENERIC;
1299     }
1300
1301     switch( p_sys->i_ca_type )
1302     {
1303     case CA_CI_LINK:
1304         i_ret = E_(en50221_Poll)( p_access );
1305         break;
1306     case CA_CI:
1307         i_ret = VLC_SUCCESS;
1308         break;
1309     default:
1310         msg_Err( p_access, "CAMPoll: This should not happen" );
1311         break;
1312     }
1313
1314     return i_ret;
1315 }
1316
1317 /*****************************************************************************
1318  * CAMSet :
1319  *****************************************************************************/
1320 int E_(CAMSet)( access_t * p_access, dvbpsi_pmt_t *p_pmt )
1321 {
1322     access_sys_t *p_sys = p_access->p_sys;
1323
1324     if ( p_sys->i_ca_handle == 0 )
1325     {
1326         return VLC_EGENERIC;
1327     }
1328
1329     E_(en50221_SetCAPMT)( p_access, p_pmt );
1330
1331     return VLC_SUCCESS;
1332 }
1333
1334 /*****************************************************************************
1335  * CAMClose :
1336  *****************************************************************************/
1337 void E_(CAMClose)( access_t * p_access )
1338 {
1339     access_sys_t *p_sys = p_access->p_sys;
1340
1341     E_(en50221_End)( p_access );
1342
1343     if ( p_sys->i_ca_handle )
1344     {
1345         close( p_sys->i_ca_handle );
1346     }
1347 }
1348