]> git.sesse.net Git - vlc/blob - modules/access/dvb/linux_dvb.c
58249bdc91b6bbe751b022124b278e5cd73d3b82
[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     int i_ret;
262     struct dvb_frontend_event event;
263     fe_status_t i_status, i_diff;
264
265     if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_GET_EVENT,
266                         &event )) < 0 )
267     {
268         msg_Err( p_access, "reading frontend status failed (%d) %s",
269                  i_ret, strerror(errno) );
270         return;
271     }
272
273     i_status = event.status;
274     i_diff = i_status ^ p_frontend->i_last_status;
275     p_frontend->i_last_status = i_status;
276
277 #define IF_UP( x )                                                          \
278     }                                                                       \
279     if ( i_diff & (x) )                                                     \
280     {                                                                       \
281         if ( i_status & (x) )
282
283     {
284         IF_UP( FE_HAS_SIGNAL )
285             msg_Dbg( p_access, "frontend has acquired signal" );
286         else
287             msg_Dbg( p_access, "frontend has lost signal" );
288
289         IF_UP( FE_HAS_CARRIER )
290             msg_Dbg( p_access, "frontend has acquired carrier" );
291         else
292             msg_Dbg( p_access, "frontend has lost carrier" );
293
294         IF_UP( FE_HAS_VITERBI )
295             msg_Dbg( p_access, "frontend has acquired stable FEC" );
296         else
297             msg_Dbg( p_access, "frontend has lost FEC" );
298
299         IF_UP( FE_HAS_SYNC )
300             msg_Dbg( p_access, "frontend has acquired sync" );
301         else
302             msg_Dbg( p_access, "frontend has lost sync" );
303
304         IF_UP( FE_HAS_LOCK )
305         {
306             int32_t i_value;
307             msg_Dbg( p_access, "frontend has acquired lock" );
308             p_sys->i_frontend_timeout = 0;
309
310             /* Read some statistics */
311             if( ioctl( p_sys->i_frontend_handle, FE_READ_BER, &i_value ) >= 0 )
312                 msg_Dbg( p_access, "- Bit error rate: %d", i_value );
313             if( ioctl( p_sys->i_frontend_handle, FE_READ_SIGNAL_STRENGTH, &i_value ) >= 0 )
314                 msg_Dbg( p_access, "- Signal strength: %d", i_value );
315             if( ioctl( p_sys->i_frontend_handle, FE_READ_SNR, &i_value ) >= 0 )
316                 msg_Dbg( p_access, "- SNR: %d", i_value );
317         }
318         else
319         {
320             msg_Dbg( p_access, "frontend has lost lock" );
321             p_sys->i_frontend_timeout = mdate() + FRONTEND_LOCK_TIMEOUT;
322         }
323
324         IF_UP( FE_REINIT )
325         {
326             /* The frontend was reinited. */
327             msg_Warn( p_access, "reiniting frontend");
328             E_(FrontendSet)( p_access );
329         }
330     }
331 }
332 /*****************************************************************************
333  * FrontendInfo : Return information about given frontend
334  *****************************************************************************/
335 static int FrontendInfo( access_t *p_access )
336 {
337     access_sys_t *p_sys = p_access->p_sys;
338     frontend_t *p_frontend = p_sys->p_frontend;
339     int i_ret;
340
341     /* Determine type of frontend */
342     if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_GET_INFO,
343                         &p_frontend->info )) < 0 )
344     {
345         msg_Err( p_access, "ioctl FE_GET_INFO failed (%d) %s", i_ret,
346                  strerror(errno) );
347         return VLC_EGENERIC;
348     }
349
350     /* Print out frontend capabilities. */
351     msg_Dbg(p_access, "Frontend Info:" );
352     msg_Dbg(p_access, "  name = %s", p_frontend->info.name );
353     switch( p_frontend->info.type )
354     {
355         case FE_QPSK:
356             msg_Dbg( p_access, "  type = QPSK (DVB-S)" );
357             break;
358         case FE_QAM:
359             msg_Dbg( p_access, "  type = QAM (DVB-C)" );
360             break;
361         case FE_OFDM:
362             msg_Dbg( p_access, "  type = OFDM (DVB-T)" );
363             break;
364 #if 0 /* DVB_API_VERSION == 3 */
365         case FE_MEMORY:
366             msg_Dbg(p_access, "  type = MEMORY" );
367             break;
368         case FE_NET:
369             msg_Dbg(p_access, "  type = NETWORK" );
370             break;
371 #endif
372         default:
373             msg_Err( p_access, "  unknown frontend type (%d)",
374                      p_frontend->info.type );
375             return VLC_EGENERIC;
376     }
377     msg_Dbg(p_access, "  frequency_min = %u (kHz)",
378             p_frontend->info.frequency_min);
379     msg_Dbg(p_access, "  frequency_max = %u (kHz)",
380             p_frontend->info.frequency_max);
381     msg_Dbg(p_access, "  frequency_stepsize = %u",
382             p_frontend->info.frequency_stepsize);
383     msg_Dbg(p_access, "  frequency_tolerance = %u",
384             p_frontend->info.frequency_tolerance);
385     msg_Dbg(p_access, "  symbol_rate_min = %u (kHz)",
386             p_frontend->info.symbol_rate_min);
387     msg_Dbg(p_access, "  symbol_rate_max = %u (kHz)",
388             p_frontend->info.symbol_rate_max);
389     msg_Dbg(p_access, "  symbol_rate_tolerance (ppm) = %u",
390             p_frontend->info.symbol_rate_tolerance);
391     msg_Dbg(p_access, "  notifier_delay (ms) = %u",
392             p_frontend->info.notifier_delay );
393
394     msg_Dbg(p_access, "Frontend Info capability list:");
395     if( p_frontend->info.caps & FE_IS_STUPID)
396         msg_Dbg(p_access, "  no capabilities - frontend is stupid!");
397     if( p_frontend->info.caps & FE_CAN_INVERSION_AUTO)
398         msg_Dbg(p_access, "  inversion auto");
399     if( p_frontend->info.caps & FE_CAN_FEC_1_2)
400         msg_Dbg(p_access, "  forward error correction 1/2");
401     if( p_frontend->info.caps & FE_CAN_FEC_2_3)
402         msg_Dbg(p_access, "  forward error correction 2/3");
403     if( p_frontend->info.caps & FE_CAN_FEC_3_4)
404         msg_Dbg(p_access, "  forward error correction 3/4");
405     if( p_frontend->info.caps & FE_CAN_FEC_4_5)
406         msg_Dbg(p_access, "  forward error correction 4/5");
407     if( p_frontend->info.caps & FE_CAN_FEC_5_6)
408         msg_Dbg(p_access, "  forward error correction 5/6");
409     if( p_frontend->info.caps & FE_CAN_FEC_6_7)
410         msg_Dbg(p_access, "  forward error correction 6/7");
411     if( p_frontend->info.caps & FE_CAN_FEC_7_8)
412         msg_Dbg(p_access, "  forward error correction 7/8");
413     if( p_frontend->info.caps & FE_CAN_FEC_8_9)
414         msg_Dbg(p_access, "  forward error correction 8/9");
415     if( p_frontend->info.caps & FE_CAN_FEC_AUTO)
416         msg_Dbg(p_access, "  forward error correction auto");
417     if( p_frontend->info.caps & FE_CAN_QPSK)
418         msg_Dbg(p_access, "  card can do QPSK");
419     if( p_frontend->info.caps & FE_CAN_QAM_16)
420         msg_Dbg(p_access, "  card can do QAM 16");
421     if( p_frontend->info.caps & FE_CAN_QAM_32)
422         msg_Dbg(p_access, "  card can do QAM 32");
423     if( p_frontend->info.caps & FE_CAN_QAM_64)
424         msg_Dbg(p_access, "  card can do QAM 64");
425     if( p_frontend->info.caps & FE_CAN_QAM_128)
426         msg_Dbg(p_access, "  card can do QAM 128");
427     if( p_frontend->info.caps & FE_CAN_QAM_256)
428         msg_Dbg(p_access, "  card can do QAM 256");
429     if( p_frontend->info.caps & FE_CAN_QAM_AUTO)
430         msg_Dbg(p_access, "  card can do QAM auto");
431     if( p_frontend->info.caps & FE_CAN_TRANSMISSION_MODE_AUTO)
432         msg_Dbg(p_access, "  transmission mode auto");
433     if( p_frontend->info.caps & FE_CAN_BANDWIDTH_AUTO)
434         msg_Dbg(p_access, "  bandwidth mode auto");
435     if( p_frontend->info.caps & FE_CAN_GUARD_INTERVAL_AUTO)
436         msg_Dbg(p_access, "  guard interval mode auto");
437     if( p_frontend->info.caps & FE_CAN_HIERARCHY_AUTO)
438         msg_Dbg(p_access, "  hierarchy mode auto");
439     if( p_frontend->info.caps & FE_CAN_MUTE_TS)
440         msg_Dbg(p_access, "  card can mute TS");
441     if( p_frontend->info.caps & FE_CAN_RECOVER)
442         msg_Dbg(p_access, "  card can recover from a cable unplug");
443     msg_Dbg(p_access, "End of capability list");
444
445     return VLC_SUCCESS;
446 }
447
448 /*****************************************************************************
449  * Decoding the DVB parameters (common)
450  *****************************************************************************/
451 static fe_spectral_inversion_t DecodeInversion( access_t *p_access )
452 {
453     vlc_value_t         val;
454     fe_spectral_inversion_t fe_inversion = 0;
455
456     var_Get( p_access, "dvb-inversion", &val );
457     msg_Dbg( p_access, "using inversion=%d", val.i_int );
458
459     switch( val.i_int )
460     {
461         case 0: fe_inversion = INVERSION_OFF; break;
462         case 1: fe_inversion = INVERSION_ON; break;
463         case 2: fe_inversion = INVERSION_AUTO; break;
464         default:
465             msg_Dbg( p_access, "dvb has inversion not set, using auto");
466             fe_inversion = INVERSION_AUTO;
467             break;
468     }
469     return fe_inversion;
470 }
471
472 static fe_code_rate_t DecodeFEC( access_t *p_access, int i_val )
473 {
474     fe_code_rate_t      fe_fec = FEC_NONE;
475
476     msg_Dbg( p_access, "using fec=%d", i_val );
477
478     switch( i_val )
479     {
480         case 0: fe_fec = FEC_NONE; break;
481         case 1: fe_fec = FEC_1_2; break;
482         case 2: fe_fec = FEC_2_3; break;
483         case 3: fe_fec = FEC_3_4; break;
484         case 4: fe_fec = FEC_4_5; break;
485         case 5: fe_fec = FEC_5_6; break;
486         case 6: fe_fec = FEC_6_7; break;
487         case 7: fe_fec = FEC_7_8; break;
488         case 8: fe_fec = FEC_8_9; break;
489         case 9: fe_fec = FEC_AUTO; break;
490         default:
491             /* cannot happen */
492             fe_fec = FEC_NONE;
493             msg_Err( p_access, "argument has invalid FEC (%d)", i_val);
494             break;
495     }
496     return fe_fec;
497 }
498
499 static fe_modulation_t DecodeModulation( access_t *p_access )
500 {
501     vlc_value_t         val;
502     fe_modulation_t     fe_modulation = 0;
503
504     var_Get( p_access, "dvb-modulation", &val );
505
506     switch( val.i_int )
507     {
508         case -1: fe_modulation = QPSK; break;
509         case 0: fe_modulation = QAM_AUTO; break;
510         case 16: fe_modulation = QAM_16; break;
511         case 32: fe_modulation = QAM_32; break;
512         case 64: fe_modulation = QAM_64; break;
513         case 128: fe_modulation = QAM_128; break;
514         case 256: fe_modulation = QAM_256; break;
515         default:
516             msg_Dbg( p_access, "terrestrial/cable dvb has constellation/modulation not set, using auto");
517             fe_modulation = QAM_AUTO;
518             break;
519     }
520     return fe_modulation;
521 }
522
523 /*****************************************************************************
524  * FrontendSetQPSK : controls the FE device
525  *****************************************************************************/
526 static fe_sec_voltage_t DecodeVoltage( access_t *p_access )
527 {
528     vlc_value_t         val;
529     fe_sec_voltage_t    fe_voltage;
530
531     var_Get( p_access, "dvb-voltage", &val );
532     msg_Dbg( p_access, "using voltage=%d", val.i_int );
533
534     switch( val.i_int )
535     {
536         case 0: fe_voltage = SEC_VOLTAGE_OFF; break;
537         case 13: fe_voltage = SEC_VOLTAGE_13; break;
538         case 18: fe_voltage = SEC_VOLTAGE_18; break;
539         default:
540             fe_voltage = SEC_VOLTAGE_OFF;
541             msg_Err( p_access, "argument has invalid voltage (%d)", val.i_int );
542             break;
543     }
544     return fe_voltage;
545 }
546
547 static fe_sec_tone_mode_t DecodeTone( access_t *p_access )
548 {
549     vlc_value_t         val;
550     fe_sec_tone_mode_t  fe_tone;
551
552     var_Get( p_access, "dvb-tone", &val );
553     msg_Dbg( p_access, "using tone=%d", val.i_int );
554
555     switch( val.i_int )
556     {
557         case 0: fe_tone = SEC_TONE_OFF; break;
558         case 1: fe_tone = SEC_TONE_ON; break;
559         default:
560             fe_tone = SEC_TONE_OFF;
561             msg_Err( p_access, "argument has invalid tone mode (%d)", val.i_int);
562             break;
563     }
564     return fe_tone;
565 }
566
567 struct diseqc_cmd_t
568 {
569     struct dvb_diseqc_master_cmd cmd;
570     uint32_t wait;
571 };
572
573 static int DoDiseqc( access_t *p_access )
574 {
575     access_sys_t *p_sys = p_access->p_sys;
576     vlc_value_t val;
577     int i_frequency, i_lnb_slof;
578     fe_sec_voltage_t fe_voltage;
579     fe_sec_tone_mode_t fe_tone;
580     int i_err;
581
582     var_Get( p_access, "dvb-frequency", &val );
583     i_frequency = val.i_int;
584     var_Get( p_access, "dvb-lnb-slof", &val );
585     i_lnb_slof = val.i_int;
586
587     var_Get( p_access, "dvb-tone", &val );
588     if( val.i_int == -1 /* auto */ )
589     {
590         if( i_frequency >= i_lnb_slof )
591             val.i_int = 1;
592         else
593             val.i_int = 0;
594         var_Set( p_access, "dvb-tone", val );
595     }
596
597     fe_voltage = DecodeVoltage( p_access );
598     fe_tone = DecodeTone( p_access );
599
600     /* Switch off continuous tone. */
601     if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_TONE, SEC_TONE_OFF )) < 0 )
602     {
603         msg_Err( p_access, "ioctl FE_SET_TONE failed, tone=%s (%d) %s",
604                  fe_tone == SEC_TONE_ON ? "on" : "off", i_err,
605                  strerror(errno) );
606         return i_err;
607     }
608
609     /* Configure LNB voltage. */
610     if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_VOLTAGE, fe_voltage )) < 0 )
611     {
612         msg_Err( p_access, "ioctl FE_SET_VOLTAGE failed, voltage=%d (%d) %s",
613                  fe_voltage, i_err, strerror(errno) );
614         return i_err;
615     }
616
617     var_Get( p_access, "dvb-high-voltage", &val );
618     if( (i_err = ioctl( p_sys->i_frontend_handle, FE_ENABLE_HIGH_LNB_VOLTAGE,
619                         val.b_bool )) < 0 && val.b_bool )
620     {
621         msg_Err( p_access,
622                  "ioctl FE_ENABLE_HIGH_LNB_VOLTAGE failed, val=%d (%d) %s",
623                  val.b_bool, i_err, strerror(errno) );
624     }
625
626     /* Wait for at least 15 ms. */
627     msleep(15000);
628
629     var_Get( p_access, "dvb-satno", &val );
630     if( val.i_int > 0 && val.i_int < 5 )
631     {
632         /* digital satellite equipment control,
633          * specification is available from http://www.eutelsat.com/
634          */
635
636         /* 1.x compatible equipment */
637         struct diseqc_cmd_t cmd =  { {{0xe0, 0x10, 0x38, 0xf0, 0x00, 0x00}, 4}, 0 };
638
639         /* param: high nibble: reset bits, low nibble set bits,
640          * bits are: option, position, polarization, band
641          */
642         cmd.cmd.msg[3] = 0xf0 /* reset bits */
643                           | (((val.i_int - 1) * 4) & 0xc)
644                           | (fe_voltage == SEC_VOLTAGE_13 ? 0 : 2)
645                           | (fe_tone == SEC_TONE_ON ? 1 : 0);
646
647         if( (i_err = ioctl( p_sys->i_frontend_handle, FE_DISEQC_SEND_MASTER_CMD,
648                            &cmd.cmd )) < 0 )
649         {
650             msg_Err( p_access, "ioctl FE_SEND_MASTER_CMD failed (%d) %s",
651                      i_err, strerror(errno) );
652             return i_err;
653         }
654
655         msleep(15000 + cmd.wait * 1000);
656
657         /* A or B simple diseqc ("diseqc-compatible") */
658         if( (i_err = ioctl( p_sys->i_frontend_handle, FE_DISEQC_SEND_BURST,
659                       ((val.i_int - 1) % 2) ? SEC_MINI_B : SEC_MINI_A )) < 0 )
660         {
661             msg_Err( p_access, "ioctl FE_SEND_BURST failed (%d) %s",
662                      i_err, strerror(errno) );
663             return i_err;
664         }
665
666         msleep(15000);
667     }
668
669     if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_TONE, fe_tone )) < 0 )
670     {
671         msg_Err( p_access, "ioctl FE_SET_TONE failed, tone=%s (%d) %s",
672                  fe_tone == SEC_TONE_ON ? "on" : "off", i_err,
673                  strerror(errno) );
674         return i_err;
675     }
676
677     msleep(50000);
678     return 0;
679 }
680
681 static int FrontendSetQPSK( access_t *p_access )
682 {
683     access_sys_t *p_sys = p_access->p_sys;
684     struct dvb_frontend_parameters fep;
685     int i_ret;
686     vlc_value_t val;
687     int i_frequency, i_lnb_slof;
688
689     /* Prepare the fep structure */
690     var_Get( p_access, "dvb-frequency", &val );
691     i_frequency = val.i_int;
692     var_Get( p_access, "dvb-lnb-slof", &val );
693     i_lnb_slof = val.i_int;
694
695     if( i_frequency >= i_lnb_slof )
696         var_Get( p_access, "dvb-lnb-lof2", &val );
697     else
698         var_Get( p_access, "dvb-lnb-lof1", &val );
699     fep.frequency = i_frequency - val.i_int;
700
701     fep.inversion = DecodeInversion( p_access );
702
703     var_Get( p_access, "dvb-srate", &val );
704     fep.u.qpsk.symbol_rate = val.i_int;
705
706     var_Get( p_access, "dvb-fec", &val );
707     fep.u.qpsk.fec_inner = DecodeFEC( p_access, val.i_int );
708
709     if( DoDiseqc( p_access ) < 0 )
710     {
711         return VLC_EGENERIC;
712     }
713
714     /* Empty the event queue */
715     for( ; ; )
716     {
717         struct dvb_frontend_event event;
718         if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
719               && errno == EWOULDBLOCK )
720             break;
721     }
722
723     /* Now send it all to the frontend device */
724     if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
725     {
726         msg_Err( p_access, "DVB-S: setting frontend failed (%d) %s", i_ret,
727                  strerror(errno) );
728         return VLC_EGENERIC;
729     }
730
731     return VLC_SUCCESS;
732 }
733
734 /*****************************************************************************
735  * FrontendSetQAM : controls the FE device
736  *****************************************************************************/
737 static int FrontendSetQAM( access_t *p_access )
738 {
739     access_sys_t *p_sys = p_access->p_sys;
740     struct dvb_frontend_parameters fep;
741     vlc_value_t val;
742     int i_ret;
743
744     /* Prepare the fep structure */
745
746     var_Get( p_access, "dvb-frequency", &val );
747     fep.frequency = val.i_int;
748
749     fep.inversion = DecodeInversion( p_access );
750
751     var_Get( p_access, "dvb-srate", &val );
752     fep.u.qam.symbol_rate = val.i_int;
753
754     var_Get( p_access, "dvb-fec", &val );
755     fep.u.qam.fec_inner = DecodeFEC( p_access, val.i_int );
756
757     fep.u.qam.modulation = DecodeModulation( p_access );
758
759     /* Empty the event queue */
760     for( ; ; )
761     {
762         struct dvb_frontend_event event;
763         if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
764               && errno == EWOULDBLOCK )
765             break;
766     }
767
768     /* Now send it all to the frontend device */
769     if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
770     {
771         msg_Err( p_access, "DVB-C: setting frontend failed (%d) %s", i_ret,
772                  strerror(errno) );
773         return VLC_EGENERIC;
774     }
775
776     return VLC_SUCCESS;
777 }
778
779 /*****************************************************************************
780  * FrontendSetOFDM : controls the FE device
781  *****************************************************************************/
782 static fe_bandwidth_t DecodeBandwidth( access_t *p_access )
783 {
784     vlc_value_t         val;
785     fe_bandwidth_t      fe_bandwidth = 0;
786
787     var_Get( p_access, "dvb-bandwidth", &val );
788     msg_Dbg( p_access, "using bandwidth=%d", val.i_int );
789
790     switch( val.i_int )
791     {
792         case 0: fe_bandwidth = BANDWIDTH_AUTO; break;
793         case 6: fe_bandwidth = BANDWIDTH_6_MHZ; break;
794         case 7: fe_bandwidth = BANDWIDTH_7_MHZ; break;
795         case 8: fe_bandwidth = BANDWIDTH_8_MHZ; break;
796         default:
797             msg_Dbg( p_access, "terrestrial dvb has bandwidth not set, using auto" );
798             fe_bandwidth = BANDWIDTH_AUTO;
799             break;
800     }
801     return fe_bandwidth;
802 }
803
804 static fe_transmit_mode_t DecodeTransmission( access_t *p_access )
805 {
806     vlc_value_t         val;
807     fe_transmit_mode_t  fe_transmission = 0;
808
809     var_Get( p_access, "dvb-transmission", &val );
810     msg_Dbg( p_access, "using transmission=%d", val.i_int );
811
812     switch( val.i_int )
813     {
814         case 0: fe_transmission = TRANSMISSION_MODE_AUTO; break;
815         case 2: fe_transmission = TRANSMISSION_MODE_2K; break;
816         case 8: fe_transmission = TRANSMISSION_MODE_8K; break;
817         default:
818             msg_Dbg( p_access, "terrestrial dvb has transmission mode not set, using auto");
819             fe_transmission = TRANSMISSION_MODE_AUTO;
820             break;
821     }
822     return fe_transmission;
823 }
824
825 static fe_guard_interval_t DecodeGuardInterval( access_t *p_access )
826 {
827     vlc_value_t         val;
828     fe_guard_interval_t fe_guard = 0;
829
830     var_Get( p_access, "dvb-guard", &val );
831     msg_Dbg( p_access, "using guard=%d", val.i_int );
832
833     switch( val.i_int )
834     {
835         case 0: fe_guard = GUARD_INTERVAL_AUTO; break;
836         case 4: fe_guard = GUARD_INTERVAL_1_4; break;
837         case 8: fe_guard = GUARD_INTERVAL_1_8; break;
838         case 16: fe_guard = GUARD_INTERVAL_1_16; break;
839         case 32: fe_guard = GUARD_INTERVAL_1_32; break;
840         default:
841             msg_Dbg( p_access, "terrestrial dvb has guard interval not set, using auto");
842             fe_guard = GUARD_INTERVAL_AUTO;
843             break;
844     }
845     return fe_guard;
846 }
847
848 static fe_hierarchy_t DecodeHierarchy( access_t *p_access )
849 {
850     vlc_value_t         val;
851     fe_hierarchy_t      fe_hierarchy = 0;
852
853     var_Get( p_access, "dvb-hierarchy", &val );
854     msg_Dbg( p_access, "using hierarchy=%d", val.i_int );
855
856     switch( val.i_int )
857     {
858         case -1: fe_hierarchy = HIERARCHY_NONE; break;
859         case 0: fe_hierarchy = HIERARCHY_AUTO; break;
860         case 1: fe_hierarchy = HIERARCHY_1; break;
861         case 2: fe_hierarchy = HIERARCHY_2; break;
862         case 4: fe_hierarchy = HIERARCHY_4; break;
863         default:
864             msg_Dbg( p_access, "terrestrial dvb has hierarchy not set, using auto");
865             fe_hierarchy = HIERARCHY_AUTO;
866             break;
867     }
868     return fe_hierarchy;
869 }
870
871 static int FrontendSetOFDM( access_t * p_access )
872 {
873     access_sys_t *p_sys = p_access->p_sys;
874     struct dvb_frontend_parameters fep;
875     vlc_value_t val;
876     int ret;
877
878     /* Prepare the fep structure */
879
880     var_Get( p_access, "dvb-frequency", &val );
881     fep.frequency = val.i_int;
882
883     fep.inversion = DecodeInversion( p_access );
884
885     fep.u.ofdm.bandwidth = DecodeBandwidth( p_access );
886     var_Get( p_access, "dvb-code-rate-hp", &val );
887     fep.u.ofdm.code_rate_HP = DecodeFEC( p_access, val.i_int );
888     var_Get( p_access, "dvb-code-rate-lp", &val );
889     fep.u.ofdm.code_rate_LP = DecodeFEC( p_access, val.i_int );
890     fep.u.ofdm.constellation = DecodeModulation( p_access );
891     fep.u.ofdm.transmission_mode = DecodeTransmission( p_access );
892     fep.u.ofdm.guard_interval = DecodeGuardInterval( p_access );
893     fep.u.ofdm.hierarchy_information = DecodeHierarchy( p_access );
894
895     /* Empty the event queue */
896     for( ; ; )
897     {
898         struct dvb_frontend_event event;
899         if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
900               && errno == EWOULDBLOCK )
901             break;
902     }
903
904     /* Now send it all to the frontend device */
905     if( (ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
906     {
907         msg_Err( p_access, "DVB-T: setting frontend failed (%d) %s", ret,
908                  strerror(errno) );
909         return -1;
910     }
911
912     return VLC_SUCCESS;
913 }
914
915
916 /*
917  * Demux
918  */
919
920 /*****************************************************************************
921  * DMXSetFilter : controls the demux to add a filter
922  *****************************************************************************/
923 int E_(DMXSetFilter)( access_t * p_access, int i_pid, int * pi_fd, int i_type )
924 {
925     struct dmx_pes_filter_params s_filter_params;
926     int i_ret;
927     unsigned int i_adapter, i_device;
928     char dmx[128];
929     vlc_value_t val;
930
931     var_Get( p_access, "dvb-adapter", &val );
932     i_adapter = val.i_int;
933     var_Get( p_access, "dvb-device", &val );
934     i_device = val.i_int;
935
936     if( snprintf( dmx, sizeof(dmx), DMX, i_adapter, i_device )
937             >= (int)sizeof(dmx) )
938     {
939         msg_Err( p_access, "snprintf() truncated string for DMX" );
940         dmx[sizeof(dmx) - 1] = '\0';
941     }
942
943     msg_Dbg( p_access, "Opening device %s", dmx );
944     if( (*pi_fd = open(dmx, O_RDWR)) < 0 )
945     {
946         msg_Err( p_access, "DMXSetFilter: opening device failed (%s)",
947                  strerror(errno) );
948         return VLC_EGENERIC;
949     }
950
951     /* We fill the DEMUX structure : */
952     s_filter_params.pid     =   i_pid;
953     s_filter_params.input   =   DMX_IN_FRONTEND;
954     s_filter_params.output  =   DMX_OUT_TS_TAP;
955     s_filter_params.flags   =   DMX_IMMEDIATE_START;
956
957     switch ( i_type )
958     {   /* First device */
959         case 1:
960             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO0 for PID %d", i_pid);
961             s_filter_params.pes_type = DMX_PES_VIDEO0;
962             break;
963         case 2:
964             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO0 for PID %d", i_pid);
965             s_filter_params.pes_type = DMX_PES_AUDIO0;
966             break;
967         case 3:
968             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT0 for PID %d", i_pid);
969             s_filter_params.pes_type = DMX_PES_TELETEXT0;
970             break;
971         case 4:
972             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE0 for PID %d", i_pid);
973             s_filter_params.pes_type = DMX_PES_SUBTITLE0;
974             break;
975         case 5:
976             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR0 for PID %d", i_pid);
977             s_filter_params.pes_type = DMX_PES_PCR0;
978             break;
979         /* Second device */
980         case 6:
981             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO1 for PID %d", i_pid);
982             s_filter_params.pes_type = DMX_PES_VIDEO1;
983             break;
984         case 7:
985             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO1 for PID %d", i_pid);
986             s_filter_params.pes_type = DMX_PES_AUDIO1;
987             break;
988         case 8:
989             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT1 for PID %d", i_pid);
990             s_filter_params.pes_type = DMX_PES_TELETEXT1;
991             break;
992         case 9:
993             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE1 for PID %d", i_pid);
994             s_filter_params.pes_type = DMX_PES_SUBTITLE1;
995             break;
996         case 10:
997             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR1 for PID %d", i_pid);
998             s_filter_params.pes_type = DMX_PES_PCR1;
999             break;
1000         /* Third device */
1001         case 11:
1002             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO2 for PID %d", i_pid);
1003             s_filter_params.pes_type = DMX_PES_VIDEO2;
1004             break;
1005         case 12:
1006             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO2 for PID %d", i_pid);
1007             s_filter_params.pes_type = DMX_PES_AUDIO2;
1008             break;
1009         case 13:
1010             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT2 for PID %d", i_pid);
1011             s_filter_params.pes_type = DMX_PES_TELETEXT2;
1012             break;
1013         case 14:
1014             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE2 for PID %d", i_pid);
1015             s_filter_params.pes_type = DMX_PES_SUBTITLE2;
1016             break;
1017         case 15:
1018             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR2 for PID %d", i_pid);
1019             s_filter_params.pes_type = DMX_PES_PCR2;
1020             break;
1021         /* Forth device */
1022         case 16:
1023             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO3 for PID %d", i_pid);
1024             s_filter_params.pes_type = DMX_PES_VIDEO3;
1025             break;
1026         case 17:
1027             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO3 for PID %d", i_pid);
1028             s_filter_params.pes_type = DMX_PES_AUDIO3;
1029             break;
1030         case 18:
1031             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT3 for PID %d", i_pid);
1032             s_filter_params.pes_type = DMX_PES_TELETEXT3;
1033             break;
1034         case 19:
1035             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE3 for PID %d", i_pid);
1036             s_filter_params.pes_type = DMX_PES_SUBTITLE3;
1037             break;
1038         case 20:
1039             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR3 for PID %d", i_pid);
1040             s_filter_params.pes_type = DMX_PES_PCR3;
1041             break;
1042         /* Usually used by Nova cards */
1043         case 21:
1044         default:
1045             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_OTHER for PID %d", i_pid);
1046             s_filter_params.pes_type = DMX_PES_OTHER;
1047             break;
1048     }
1049
1050     /* We then give the order to the device : */
1051     if( (i_ret = ioctl( *pi_fd, DMX_SET_PES_FILTER, &s_filter_params )) < 0 )
1052     {
1053         msg_Err( p_access, "DMXSetFilter: failed with %d (%s)", i_ret,
1054                  strerror(errno) );
1055         return VLC_EGENERIC;
1056     }
1057     return VLC_SUCCESS;
1058 }
1059
1060 /*****************************************************************************
1061  * DMXUnsetFilter : removes a filter
1062  *****************************************************************************/
1063 int E_(DMXUnsetFilter)( access_t * p_access, int i_fd )
1064 {
1065     int i_ret;
1066
1067     if( (i_ret = ioctl( i_fd, DMX_STOP )) < 0 )
1068     {
1069         msg_Err( p_access, "DMX_STOP failed for demux (%d) %s",
1070                  i_ret, strerror(errno) );
1071         return i_ret;
1072     }
1073
1074     msg_Dbg( p_access, "DMXUnsetFilter: closing demux %d", i_fd );
1075     close( i_fd );
1076     return VLC_SUCCESS;
1077 }
1078
1079
1080 /*
1081  * DVR device
1082  */
1083
1084 /*****************************************************************************
1085  * DVROpen :
1086  *****************************************************************************/
1087 int E_(DVROpen)( access_t * p_access )
1088 {
1089     access_sys_t *p_sys = p_access->p_sys;
1090     unsigned int i_adapter, i_device;
1091     char dvr[128];
1092     vlc_value_t val;
1093
1094     var_Get( p_access, "dvb-adapter", &val );
1095     i_adapter = val.i_int;
1096     var_Get( p_access, "dvb-device", &val );
1097     i_device = val.i_int;
1098
1099     if( snprintf( dvr, sizeof(dvr), DVR, i_adapter, i_device )
1100             >= (int)sizeof(dvr) )
1101     {
1102         msg_Err( p_access, "snprintf() truncated string for DVR" );
1103         dvr[sizeof(dvr) - 1] = '\0';
1104     }
1105
1106     msg_Dbg( p_access, "Opening device %s", dvr );
1107     if( (p_sys->i_handle = open(dvr, O_RDONLY)) < 0 )
1108     {
1109         msg_Err( p_access, "DVROpen: opening device failed (%s)",
1110                  strerror(errno) );
1111         return VLC_EGENERIC;
1112     }
1113
1114     return VLC_SUCCESS;
1115 }
1116
1117 /*****************************************************************************
1118  * DVRClose :
1119  *****************************************************************************/
1120 void E_(DVRClose)( access_t * p_access )
1121 {
1122     access_sys_t *p_sys = p_access->p_sys;
1123
1124     close( p_sys->i_handle );
1125 }
1126
1127
1128 /*
1129  * CAM device
1130  */
1131
1132 /*****************************************************************************
1133  * CAMOpen :
1134  *****************************************************************************/
1135 int E_(CAMOpen)( access_t *p_access )
1136 {
1137     access_sys_t *p_sys = p_access->p_sys;
1138     char ca[128];
1139     int i_adapter, i_device, i_slot;
1140     ca_caps_t caps;
1141
1142     i_adapter = var_GetInteger( p_access, "dvb-adapter" );
1143     i_device = var_GetInteger( p_access, "dvb-device" );
1144
1145     if( snprintf( ca, sizeof(ca), CA, i_adapter, i_device ) >= (int)sizeof(ca) )
1146     {
1147         msg_Err( p_access, "snprintf() truncated string for CA" );
1148         ca[sizeof(ca) - 1] = '\0';
1149     }
1150
1151     msg_Dbg( p_access, "Opening device %s", ca );
1152     if( (p_sys->i_ca_handle = open(ca, O_RDWR | O_NONBLOCK)) < 0 )
1153     {
1154         msg_Err( p_access, "CAMInit: opening device failed (%s)",
1155                  strerror(errno) );
1156         p_sys->i_ca_handle = 0;
1157         return VLC_EGENERIC;
1158     }
1159
1160     if ( ioctl( p_sys->i_ca_handle, CA_GET_CAP, &caps ) != 0
1161           || caps.slot_num == 0 || caps.slot_type != CA_CI_LINK )
1162     {
1163         msg_Err( p_access, "CAMInit: no compatible CAM module" );
1164         close( p_sys->i_ca_handle );
1165         p_sys->i_ca_handle = 0;
1166         return VLC_EGENERIC;
1167     }
1168
1169     p_sys->i_nb_slots = caps.slot_num;
1170     memset( p_sys->pb_active_slot, 0, sizeof(vlc_bool_t) * MAX_CI_SLOTS );
1171
1172     for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
1173     {
1174         if ( ioctl( p_sys->i_ca_handle, CA_RESET, 1 << i_slot) != 0 )
1175         {
1176             msg_Err( p_access, "CAMInit: couldn't reset slot %d", i_slot );
1177         }
1178     }
1179
1180     msg_Dbg( p_access, "CAMInit: found a CI handler with %d slots",
1181              p_sys->i_nb_slots );
1182
1183     p_sys->i_ca_timeout = 100000;
1184     /* Wait a bit otherwise it doesn't initialize properly... */
1185     msleep( 1000000 );
1186
1187     return VLC_SUCCESS;
1188 }
1189
1190 /*****************************************************************************
1191  * CAMPoll :
1192  *****************************************************************************/
1193 int E_(CAMPoll)( access_t * p_access )
1194 {
1195     access_sys_t *p_sys = p_access->p_sys;
1196
1197     if ( p_sys->i_ca_handle == 0 )
1198     {
1199         return VLC_EGENERIC;
1200     }
1201
1202     return E_(en50221_Poll)( p_access );
1203 }
1204
1205 /*****************************************************************************
1206  * CAMSet :
1207  *****************************************************************************/
1208 int E_(CAMSet)( access_t * p_access, dvbpsi_pmt_t *p_pmt )
1209 {
1210     access_sys_t *p_sys = p_access->p_sys;
1211
1212     if ( p_sys->i_ca_handle == 0 )
1213     {
1214         return VLC_EGENERIC;
1215     }
1216
1217     E_(en50221_SetCAPMT)( p_access, p_pmt );
1218
1219     return VLC_SUCCESS;
1220 }
1221
1222 /*****************************************************************************
1223  * CAMClose :
1224  *****************************************************************************/
1225 void E_(CAMClose)( access_t * p_access )
1226 {
1227     access_sys_t *p_sys = p_access->p_sys;
1228
1229     E_(en50221_End)( p_access );
1230
1231     if ( p_sys->i_ca_handle )
1232     {
1233         close( p_sys->i_ca_handle );
1234     }
1235 }
1236