]> git.sesse.net Git - vlc/blob - modules/access/dvb/linux_dvb.c
* dvb: fix report of error in FrontendPoll (please report any problem).
[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 VideoLAN
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;
694
695     /* Prepare the fep structure */
696     var_Get( p_access, "dvb-frequency", &val );
697     i_frequency = val.i_int;
698     var_Get( p_access, "dvb-lnb-slof", &val );
699     i_lnb_slof = val.i_int;
700
701     if( i_frequency >= i_lnb_slof )
702         var_Get( p_access, "dvb-lnb-lof2", &val );
703     else
704         var_Get( p_access, "dvb-lnb-lof1", &val );
705     fep.frequency = i_frequency - val.i_int;
706
707     fep.inversion = DecodeInversion( p_access );
708
709     var_Get( p_access, "dvb-srate", &val );
710     fep.u.qpsk.symbol_rate = val.i_int;
711
712     var_Get( p_access, "dvb-fec", &val );
713     fep.u.qpsk.fec_inner = DecodeFEC( p_access, val.i_int );
714
715     if( DoDiseqc( p_access ) < 0 )
716     {
717         return VLC_EGENERIC;
718     }
719
720     /* Empty the event queue */
721     for( ; ; )
722     {
723         struct dvb_frontend_event event;
724         if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
725               && errno == EWOULDBLOCK )
726             break;
727     }
728
729     /* Now send it all to the frontend device */
730     if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
731     {
732         msg_Err( p_access, "DVB-S: setting frontend failed (%d) %s", i_ret,
733                  strerror(errno) );
734         return VLC_EGENERIC;
735     }
736
737     return VLC_SUCCESS;
738 }
739
740 /*****************************************************************************
741  * FrontendSetQAM : controls the FE device
742  *****************************************************************************/
743 static int FrontendSetQAM( access_t *p_access )
744 {
745     access_sys_t *p_sys = p_access->p_sys;
746     struct dvb_frontend_parameters fep;
747     vlc_value_t val;
748     int i_ret;
749
750     /* Prepare the fep structure */
751
752     var_Get( p_access, "dvb-frequency", &val );
753     fep.frequency = val.i_int;
754
755     fep.inversion = DecodeInversion( p_access );
756
757     var_Get( p_access, "dvb-srate", &val );
758     fep.u.qam.symbol_rate = val.i_int;
759
760     var_Get( p_access, "dvb-fec", &val );
761     fep.u.qam.fec_inner = DecodeFEC( p_access, val.i_int );
762
763     fep.u.qam.modulation = DecodeModulation( p_access );
764
765     /* Empty the event queue */
766     for( ; ; )
767     {
768         struct dvb_frontend_event event;
769         if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
770               && errno == EWOULDBLOCK )
771             break;
772     }
773
774     /* Now send it all to the frontend device */
775     if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
776     {
777         msg_Err( p_access, "DVB-C: setting frontend failed (%d) %s", i_ret,
778                  strerror(errno) );
779         return VLC_EGENERIC;
780     }
781
782     return VLC_SUCCESS;
783 }
784
785 /*****************************************************************************
786  * FrontendSetOFDM : controls the FE device
787  *****************************************************************************/
788 static fe_bandwidth_t DecodeBandwidth( access_t *p_access )
789 {
790     vlc_value_t         val;
791     fe_bandwidth_t      fe_bandwidth = 0;
792
793     var_Get( p_access, "dvb-bandwidth", &val );
794     msg_Dbg( p_access, "using bandwidth=%d", val.i_int );
795
796     switch( val.i_int )
797     {
798         case 0: fe_bandwidth = BANDWIDTH_AUTO; break;
799         case 6: fe_bandwidth = BANDWIDTH_6_MHZ; break;
800         case 7: fe_bandwidth = BANDWIDTH_7_MHZ; break;
801         case 8: fe_bandwidth = BANDWIDTH_8_MHZ; break;
802         default:
803             msg_Dbg( p_access, "terrestrial dvb has bandwidth not set, using auto" );
804             fe_bandwidth = BANDWIDTH_AUTO;
805             break;
806     }
807     return fe_bandwidth;
808 }
809
810 static fe_transmit_mode_t DecodeTransmission( access_t *p_access )
811 {
812     vlc_value_t         val;
813     fe_transmit_mode_t  fe_transmission = 0;
814
815     var_Get( p_access, "dvb-transmission", &val );
816     msg_Dbg( p_access, "using transmission=%d", val.i_int );
817
818     switch( val.i_int )
819     {
820         case 0: fe_transmission = TRANSMISSION_MODE_AUTO; break;
821         case 2: fe_transmission = TRANSMISSION_MODE_2K; break;
822         case 8: fe_transmission = TRANSMISSION_MODE_8K; break;
823         default:
824             msg_Dbg( p_access, "terrestrial dvb has transmission mode not set, using auto");
825             fe_transmission = TRANSMISSION_MODE_AUTO;
826             break;
827     }
828     return fe_transmission;
829 }
830
831 static fe_guard_interval_t DecodeGuardInterval( access_t *p_access )
832 {
833     vlc_value_t         val;
834     fe_guard_interval_t fe_guard = 0;
835
836     var_Get( p_access, "dvb-guard", &val );
837     msg_Dbg( p_access, "using guard=%d", val.i_int );
838
839     switch( val.i_int )
840     {
841         case 0: fe_guard = GUARD_INTERVAL_AUTO; break;
842         case 4: fe_guard = GUARD_INTERVAL_1_4; break;
843         case 8: fe_guard = GUARD_INTERVAL_1_8; break;
844         case 16: fe_guard = GUARD_INTERVAL_1_16; break;
845         case 32: fe_guard = GUARD_INTERVAL_1_32; break;
846         default:
847             msg_Dbg( p_access, "terrestrial dvb has guard interval not set, using auto");
848             fe_guard = GUARD_INTERVAL_AUTO;
849             break;
850     }
851     return fe_guard;
852 }
853
854 static fe_hierarchy_t DecodeHierarchy( access_t *p_access )
855 {
856     vlc_value_t         val;
857     fe_hierarchy_t      fe_hierarchy = 0;
858
859     var_Get( p_access, "dvb-hierarchy", &val );
860     msg_Dbg( p_access, "using hierarchy=%d", val.i_int );
861
862     switch( val.i_int )
863     {
864         case -1: fe_hierarchy = HIERARCHY_NONE; break;
865         case 0: fe_hierarchy = HIERARCHY_AUTO; break;
866         case 1: fe_hierarchy = HIERARCHY_1; break;
867         case 2: fe_hierarchy = HIERARCHY_2; break;
868         case 4: fe_hierarchy = HIERARCHY_4; break;
869         default:
870             msg_Dbg( p_access, "terrestrial dvb has hierarchy not set, using auto");
871             fe_hierarchy = HIERARCHY_AUTO;
872             break;
873     }
874     return fe_hierarchy;
875 }
876
877 static int FrontendSetOFDM( access_t * p_access )
878 {
879     access_sys_t *p_sys = p_access->p_sys;
880     struct dvb_frontend_parameters fep;
881     vlc_value_t val;
882     int ret;
883
884     /* Prepare the fep structure */
885
886     var_Get( p_access, "dvb-frequency", &val );
887     fep.frequency = val.i_int;
888
889     fep.inversion = DecodeInversion( p_access );
890
891     fep.u.ofdm.bandwidth = DecodeBandwidth( p_access );
892     var_Get( p_access, "dvb-code-rate-hp", &val );
893     fep.u.ofdm.code_rate_HP = DecodeFEC( p_access, val.i_int );
894     var_Get( p_access, "dvb-code-rate-lp", &val );
895     fep.u.ofdm.code_rate_LP = DecodeFEC( p_access, val.i_int );
896     fep.u.ofdm.constellation = DecodeModulation( p_access );
897     fep.u.ofdm.transmission_mode = DecodeTransmission( p_access );
898     fep.u.ofdm.guard_interval = DecodeGuardInterval( p_access );
899     fep.u.ofdm.hierarchy_information = DecodeHierarchy( p_access );
900
901     /* Empty the event queue */
902     for( ; ; )
903     {
904         struct dvb_frontend_event event;
905         if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
906               && errno == EWOULDBLOCK )
907             break;
908     }
909
910     /* Now send it all to the frontend device */
911     if( (ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
912     {
913         msg_Err( p_access, "DVB-T: setting frontend failed (%d) %s", ret,
914                  strerror(errno) );
915         return -1;
916     }
917
918     return VLC_SUCCESS;
919 }
920
921
922 /*
923  * Demux
924  */
925
926 /*****************************************************************************
927  * DMXSetFilter : controls the demux to add a filter
928  *****************************************************************************/
929 int E_(DMXSetFilter)( access_t * p_access, int i_pid, int * pi_fd, int i_type )
930 {
931     struct dmx_pes_filter_params s_filter_params;
932     int i_ret;
933     unsigned int i_adapter, i_device;
934     char dmx[128];
935     vlc_value_t val;
936
937     var_Get( p_access, "dvb-adapter", &val );
938     i_adapter = val.i_int;
939     var_Get( p_access, "dvb-device", &val );
940     i_device = val.i_int;
941
942     if( snprintf( dmx, sizeof(dmx), DMX, i_adapter, i_device )
943             >= (int)sizeof(dmx) )
944     {
945         msg_Err( p_access, "snprintf() truncated string for DMX" );
946         dmx[sizeof(dmx) - 1] = '\0';
947     }
948
949     msg_Dbg( p_access, "Opening device %s", dmx );
950     if( (*pi_fd = open(dmx, O_RDWR)) < 0 )
951     {
952         msg_Err( p_access, "DMXSetFilter: opening device failed (%s)",
953                  strerror(errno) );
954         return VLC_EGENERIC;
955     }
956
957     /* We fill the DEMUX structure : */
958     s_filter_params.pid     =   i_pid;
959     s_filter_params.input   =   DMX_IN_FRONTEND;
960     s_filter_params.output  =   DMX_OUT_TS_TAP;
961     s_filter_params.flags   =   DMX_IMMEDIATE_START;
962
963     switch ( i_type )
964     {   /* First device */
965         case 1:
966             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO0 for PID %d", i_pid);
967             s_filter_params.pes_type = DMX_PES_VIDEO0;
968             break;
969         case 2:
970             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO0 for PID %d", i_pid);
971             s_filter_params.pes_type = DMX_PES_AUDIO0;
972             break;
973         case 3:
974             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT0 for PID %d", i_pid);
975             s_filter_params.pes_type = DMX_PES_TELETEXT0;
976             break;
977         case 4:
978             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE0 for PID %d", i_pid);
979             s_filter_params.pes_type = DMX_PES_SUBTITLE0;
980             break;
981         case 5:
982             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR0 for PID %d", i_pid);
983             s_filter_params.pes_type = DMX_PES_PCR0;
984             break;
985         /* Second device */
986         case 6:
987             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO1 for PID %d", i_pid);
988             s_filter_params.pes_type = DMX_PES_VIDEO1;
989             break;
990         case 7:
991             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO1 for PID %d", i_pid);
992             s_filter_params.pes_type = DMX_PES_AUDIO1;
993             break;
994         case 8:
995             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT1 for PID %d", i_pid);
996             s_filter_params.pes_type = DMX_PES_TELETEXT1;
997             break;
998         case 9:
999             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE1 for PID %d", i_pid);
1000             s_filter_params.pes_type = DMX_PES_SUBTITLE1;
1001             break;
1002         case 10:
1003             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR1 for PID %d", i_pid);
1004             s_filter_params.pes_type = DMX_PES_PCR1;
1005             break;
1006         /* Third device */
1007         case 11:
1008             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO2 for PID %d", i_pid);
1009             s_filter_params.pes_type = DMX_PES_VIDEO2;
1010             break;
1011         case 12:
1012             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO2 for PID %d", i_pid);
1013             s_filter_params.pes_type = DMX_PES_AUDIO2;
1014             break;
1015         case 13:
1016             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT2 for PID %d", i_pid);
1017             s_filter_params.pes_type = DMX_PES_TELETEXT2;
1018             break;
1019         case 14:
1020             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE2 for PID %d", i_pid);
1021             s_filter_params.pes_type = DMX_PES_SUBTITLE2;
1022             break;
1023         case 15:
1024             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR2 for PID %d", i_pid);
1025             s_filter_params.pes_type = DMX_PES_PCR2;
1026             break;
1027         /* Forth device */
1028         case 16:
1029             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO3 for PID %d", i_pid);
1030             s_filter_params.pes_type = DMX_PES_VIDEO3;
1031             break;
1032         case 17:
1033             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO3 for PID %d", i_pid);
1034             s_filter_params.pes_type = DMX_PES_AUDIO3;
1035             break;
1036         case 18:
1037             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT3 for PID %d", i_pid);
1038             s_filter_params.pes_type = DMX_PES_TELETEXT3;
1039             break;
1040         case 19:
1041             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE3 for PID %d", i_pid);
1042             s_filter_params.pes_type = DMX_PES_SUBTITLE3;
1043             break;
1044         case 20:
1045             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR3 for PID %d", i_pid);
1046             s_filter_params.pes_type = DMX_PES_PCR3;
1047             break;
1048         /* Usually used by Nova cards */
1049         case 21:
1050         default:
1051             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_OTHER for PID %d", i_pid);
1052             s_filter_params.pes_type = DMX_PES_OTHER;
1053             break;
1054     }
1055
1056     /* We then give the order to the device : */
1057     if( (i_ret = ioctl( *pi_fd, DMX_SET_PES_FILTER, &s_filter_params )) < 0 )
1058     {
1059         msg_Err( p_access, "DMXSetFilter: failed with %d (%s)", i_ret,
1060                  strerror(errno) );
1061         return VLC_EGENERIC;
1062     }
1063     return VLC_SUCCESS;
1064 }
1065
1066 /*****************************************************************************
1067  * DMXUnsetFilter : removes a filter
1068  *****************************************************************************/
1069 int E_(DMXUnsetFilter)( access_t * p_access, int i_fd )
1070 {
1071     int i_ret;
1072
1073     if( (i_ret = ioctl( i_fd, DMX_STOP )) < 0 )
1074     {
1075         msg_Err( p_access, "DMX_STOP failed for demux (%d) %s",
1076                  i_ret, strerror(errno) );
1077         return i_ret;
1078     }
1079
1080     msg_Dbg( p_access, "DMXUnsetFilter: closing demux %d", i_fd );
1081     close( i_fd );
1082     return VLC_SUCCESS;
1083 }
1084
1085
1086 /*
1087  * DVR device
1088  */
1089
1090 /*****************************************************************************
1091  * DVROpen :
1092  *****************************************************************************/
1093 int E_(DVROpen)( access_t * p_access )
1094 {
1095     access_sys_t *p_sys = p_access->p_sys;
1096     unsigned int i_adapter, i_device;
1097     char dvr[128];
1098     vlc_value_t val;
1099
1100     var_Get( p_access, "dvb-adapter", &val );
1101     i_adapter = val.i_int;
1102     var_Get( p_access, "dvb-device", &val );
1103     i_device = val.i_int;
1104
1105     if( snprintf( dvr, sizeof(dvr), DVR, i_adapter, i_device )
1106             >= (int)sizeof(dvr) )
1107     {
1108         msg_Err( p_access, "snprintf() truncated string for DVR" );
1109         dvr[sizeof(dvr) - 1] = '\0';
1110     }
1111
1112     msg_Dbg( p_access, "Opening device %s", dvr );
1113     if( (p_sys->i_handle = open(dvr, O_RDONLY)) < 0 )
1114     {
1115         msg_Err( p_access, "DVROpen: opening device failed (%s)",
1116                  strerror(errno) );
1117         return VLC_EGENERIC;
1118     }
1119
1120     return VLC_SUCCESS;
1121 }
1122
1123 /*****************************************************************************
1124  * DVRClose :
1125  *****************************************************************************/
1126 void E_(DVRClose)( access_t * p_access )
1127 {
1128     access_sys_t *p_sys = p_access->p_sys;
1129
1130     close( p_sys->i_handle );
1131 }
1132
1133
1134 /*
1135  * CAM device
1136  */
1137
1138 /*****************************************************************************
1139  * CAMOpen :
1140  *****************************************************************************/
1141 int E_(CAMOpen)( access_t *p_access )
1142 {
1143     access_sys_t *p_sys = p_access->p_sys;
1144     char ca[128];
1145     int i_adapter, i_device, i_slot;
1146     ca_caps_t caps;
1147
1148     i_adapter = var_GetInteger( p_access, "dvb-adapter" );
1149     i_device = var_GetInteger( p_access, "dvb-device" );
1150
1151     if( snprintf( ca, sizeof(ca), CA, i_adapter, i_device ) >= (int)sizeof(ca) )
1152     {
1153         msg_Err( p_access, "snprintf() truncated string for CA" );
1154         ca[sizeof(ca) - 1] = '\0';
1155     }
1156
1157     msg_Dbg( p_access, "Opening device %s", ca );
1158     if( (p_sys->i_ca_handle = open(ca, O_RDWR | O_NONBLOCK)) < 0 )
1159     {
1160         msg_Err( p_access, "CAMInit: opening device failed (%s)",
1161                  strerror(errno) );
1162         p_sys->i_ca_handle = 0;
1163         return VLC_EGENERIC;
1164     }
1165
1166     if ( ioctl( p_sys->i_ca_handle, CA_GET_CAP, &caps ) != 0
1167           || caps.slot_num == 0 || caps.slot_type != CA_CI_LINK )
1168     {
1169         msg_Err( p_access, "CAMInit: no compatible CAM module" );
1170         close( p_sys->i_ca_handle );
1171         p_sys->i_ca_handle = 0;
1172         return VLC_EGENERIC;
1173     }
1174
1175     p_sys->i_nb_slots = caps.slot_num;
1176     memset( p_sys->pb_active_slot, 0, sizeof(vlc_bool_t) * MAX_CI_SLOTS );
1177
1178     for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
1179     {
1180         if ( ioctl( p_sys->i_ca_handle, CA_RESET, 1 << i_slot) != 0 )
1181         {
1182             msg_Err( p_access, "CAMInit: couldn't reset slot %d", i_slot );
1183         }
1184     }
1185
1186     msg_Dbg( p_access, "CAMInit: found a CI handler with %d slots",
1187              p_sys->i_nb_slots );
1188
1189     p_sys->i_ca_timeout = 100000;
1190     /* Wait a bit otherwise it doesn't initialize properly... */
1191     msleep( 1000000 );
1192
1193     return VLC_SUCCESS;
1194 }
1195
1196 /*****************************************************************************
1197  * CAMPoll :
1198  *****************************************************************************/
1199 int E_(CAMPoll)( access_t * p_access )
1200 {
1201     access_sys_t *p_sys = p_access->p_sys;
1202
1203     if ( p_sys->i_ca_handle == 0 )
1204     {
1205         return VLC_EGENERIC;
1206     }
1207
1208     return E_(en50221_Poll)( p_access );
1209 }
1210
1211 /*****************************************************************************
1212  * CAMSet :
1213  *****************************************************************************/
1214 int E_(CAMSet)( access_t * p_access, dvbpsi_pmt_t *p_pmt )
1215 {
1216     access_sys_t *p_sys = p_access->p_sys;
1217
1218     if ( p_sys->i_ca_handle == 0 )
1219     {
1220         return VLC_EGENERIC;
1221     }
1222
1223     E_(en50221_SetCAPMT)( p_access, p_pmt );
1224
1225     return VLC_SUCCESS;
1226 }
1227
1228 /*****************************************************************************
1229  * CAMClose :
1230  *****************************************************************************/
1231 void E_(CAMClose)( access_t * p_access )
1232 {
1233     access_sys_t *p_sys = p_access->p_sys;
1234
1235     E_(en50221_End)( p_access );
1236
1237     if ( p_sys->i_ca_handle )
1238     {
1239         close( p_sys->i_ca_handle );
1240     }
1241 }
1242