]> git.sesse.net Git - vlc/blob - modules/access/dvb/linux_dvb.c
utf8_* -> vlc_* (sed roxxors)
[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-2005 the VideoLAN team
5  *
6  * Authors: Damien Lucas <nitrox@via.ecp.fr>
7  *          Johan Bilien <jobi@via.ecp.fr>
8  *          Jean-Paul Saman <jpsaman _at_ videolan _dot_ org>
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 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_access.h>
33 #include <sys/ioctl.h>
34 #include <errno.h>
35
36 #include <sys/types.h>
37 #include <fcntl.h>
38 #include <time.h>
39 #include <unistd.h>
40 #include <poll.h>
41 #include <vlc_fs.h>
42
43 /* DVB Card Drivers */
44 #include <linux/dvb/version.h>
45 #include <linux/dvb/dmx.h>
46 #include <linux/dvb/frontend.h>
47 #include <linux/dvb/ca.h>
48
49 /* Include dvbpsi headers */
50 #ifdef HAVE_DVBPSI_DR_H
51 #   include <dvbpsi/dvbpsi.h>
52 #   include <dvbpsi/descriptor.h>
53 #   include <dvbpsi/pat.h>
54 #   include <dvbpsi/pmt.h>
55 #   include <dvbpsi/dr.h>
56 #   include <dvbpsi/psi.h>
57 #   include <dvbpsi/demux.h>
58 #   include <dvbpsi/sdt.h>
59 #else
60 #   include "dvbpsi.h"
61 #   include "descriptor.h"
62 #   include "tables/pat.h"
63 #   include "tables/pmt.h"
64 #   include "descriptors/dr.h"
65 #   include "psi.h"
66 #   include "demux.h"
67 #   include "tables/sdt.h"
68 #endif
69
70 #ifdef ENABLE_HTTPD
71 #   include <vlc_httpd.h>
72 #endif
73
74 #include "dvb.h"
75
76 /*
77  * Frontends
78  */
79 struct frontend_t
80 {
81     fe_status_t i_last_status;
82     struct dvb_frontend_info info;
83 };
84
85 #define FRONTEND_LOCK_TIMEOUT 10000000 /* 10 s */
86
87 /* Local prototypes */
88 static int FrontendInfo( access_t * );
89 static int FrontendSetQPSK( access_t * );
90 static int FrontendSetQAM( access_t * );
91 static int FrontendSetOFDM( access_t * );
92 static int FrontendSetATSC( access_t * );
93
94 /*****************************************************************************
95  * FrontendOpen : Determine frontend device information and capabilities
96  *****************************************************************************/
97 int FrontendOpen( access_t *p_access )
98 {
99     access_sys_t *p_sys = p_access->p_sys;
100     frontend_t * p_frontend;
101     unsigned int i_adapter, i_device;
102     bool b_probe;
103     char frontend[128];
104
105     i_adapter = var_GetInteger( p_access, "dvb-adapter" );
106     i_device = var_GetInteger( p_access, "dvb-device" );
107     b_probe = var_GetBool( p_access, "dvb-probe" );
108
109     if( snprintf( frontend, sizeof(frontend), FRONTEND, i_adapter, i_device ) >= (int)sizeof(frontend) )
110     {
111         msg_Err( p_access, "snprintf() truncated string for FRONTEND" );
112         frontend[sizeof(frontend) - 1] = '\0';
113     }
114
115     p_sys->p_frontend = p_frontend = malloc( sizeof(frontend_t) );
116     if( !p_frontend )
117         return VLC_ENOMEM;
118
119     msg_Dbg( p_access, "Opening device %s", frontend );
120     if( (p_sys->i_frontend_handle = vlc_open(frontend, O_RDWR | O_NONBLOCK)) < 0 )
121     {
122         msg_Err( p_access, "FrontEndOpen: opening device failed (%m)" );
123         free( p_frontend );
124         return VLC_EGENERIC;
125     }
126
127     if( b_probe )
128     {
129         const char * psz_expected = NULL;
130         const char * psz_real;
131
132         if( FrontendInfo( p_access ) < 0 )
133         {
134             close( p_sys->i_frontend_handle );
135             free( p_frontend );
136             return VLC_EGENERIC;
137         }
138
139         switch( p_frontend->info.type )
140         {
141         case FE_OFDM:
142             psz_real = "DVB-T";
143             break;
144         case FE_QAM:
145             psz_real = "DVB-C";
146             break;
147         case FE_QPSK:
148             psz_real = "DVB-S";
149             break;
150         case FE_ATSC:
151             psz_real = "ATSC";
152             break;
153         default:
154             psz_real = "unknown";
155         }
156
157         /* Sanity checks */
158         if( (!strncmp( p_access->psz_access, "qpsk", 4 ) ||
159              !strncmp( p_access->psz_access, "dvb-s", 5 ) ||
160              !strncmp( p_access->psz_access, "satellite", 9 ) ) &&
161              (p_frontend->info.type != FE_QPSK) )
162         {
163             psz_expected = "DVB-S";
164         }
165         if( (!strncmp( p_access->psz_access, "cable", 5 ) ||
166              !strncmp( p_access->psz_access, "dvb-c", 5 ) ) &&
167              (p_frontend->info.type != FE_QAM) )
168         {
169             psz_expected = "DVB-C";
170         }
171         if( (!strncmp( p_access->psz_access, "terrestrial", 11 ) ||
172              !strncmp( p_access->psz_access, "dvb-t", 5 ) ) &&
173              (p_frontend->info.type != FE_OFDM) )
174         {
175             psz_expected = "DVB-T";
176         }
177
178         if( (!strncmp( p_access->psz_access, "usdigital", 9 ) ||
179              !strncmp( p_access->psz_access, "atsc", 4 ) ) &&
180              (p_frontend->info.type != FE_ATSC) )
181         {
182             psz_expected = "ATSC";
183         }
184
185         if( psz_expected != NULL )
186         {
187             msg_Err( p_access, "the user asked for %s, and the tuner is %s",
188                      psz_expected, psz_real );
189             close( p_sys->i_frontend_handle );
190             free( p_frontend );
191             return VLC_EGENERIC;
192         }
193     }
194     else /* no frontend probing is done so use default border values. */
195     {
196         msg_Dbg( p_access, "using default values for frontend info" );
197
198         msg_Dbg( p_access, "method of access is %s", p_access->psz_access );
199         p_frontend->info.type = FE_QPSK;
200         if( !strncmp( p_access->psz_access, "qpsk", 4 ) ||
201             !strncmp( p_access->psz_access, "dvb-s", 5 ) )
202             p_frontend->info.type = FE_QPSK;
203         else if( !strncmp( p_access->psz_access, "cable", 5 ) ||
204                  !strncmp( p_access->psz_access, "dvb-c", 5 ) )
205             p_frontend->info.type = FE_QAM;
206         else if( !strncmp( p_access->psz_access, "terrestrial", 11 ) ||
207                  !strncmp( p_access->psz_access, "dvb-t", 5 ) )
208             p_frontend->info.type = FE_OFDM;
209         else if( !strncmp( p_access->psz_access, "usdigital", 9 ) ||
210                  !strncmp( p_access->psz_access, "atsc", 4 ) )
211             p_frontend->info.type = FE_ATSC;
212     }
213
214     return VLC_SUCCESS;
215 }
216
217 /*****************************************************************************
218  * FrontendClose : Close the frontend
219  *****************************************************************************/
220 void FrontendClose( access_t *p_access )
221 {
222     access_sys_t *p_sys = p_access->p_sys;
223
224     if( p_sys->p_frontend )
225     {
226         close( p_sys->i_frontend_handle );
227         free( p_sys->p_frontend );
228
229         p_sys->p_frontend = NULL;
230     }
231 }
232
233 /*****************************************************************************
234  * FrontendSet : Tune !
235  *****************************************************************************/
236 int FrontendSet( access_t *p_access )
237 {
238     access_sys_t *p_sys = p_access->p_sys;
239
240     switch( p_sys->p_frontend->info.type )
241     {
242     /* DVB-S */
243     case FE_QPSK:
244         if( FrontendSetQPSK( p_access ) < 0 )
245         {
246             msg_Err( p_access, "DVB-S: tuning failed" );
247             return VLC_EGENERIC;
248         }
249         break;
250
251     /* DVB-C */
252     case FE_QAM:
253         if( FrontendSetQAM( p_access ) < 0 )
254         {
255             msg_Err( p_access, "DVB-C: tuning failed" );
256             return VLC_EGENERIC;
257         }
258         break;
259
260     /* DVB-T */
261     case FE_OFDM:
262         if( FrontendSetOFDM( p_access ) < 0 )
263         {
264             msg_Err( p_access, "DVB-T: tuning failed" );
265             return VLC_EGENERIC;
266         }
267         break;
268
269     /* ATSC */
270     case FE_ATSC:
271         if( FrontendSetATSC( p_access ) < 0 )
272         {
273             msg_Err( p_access, "ATSC: tuning failed" );
274             return VLC_EGENERIC;
275         }
276         break;
277
278     default:
279         msg_Err( p_access, "Could not determine frontend type on %s",
280                  p_sys->p_frontend->info.name );
281         return VLC_EGENERIC;
282     }
283     p_sys->p_frontend->i_last_status = 0;
284     p_sys->i_frontend_timeout = mdate() + FRONTEND_LOCK_TIMEOUT;
285     return VLC_SUCCESS;
286 }
287
288 /*****************************************************************************
289  * FrontendPoll : Poll for frontend events
290  *****************************************************************************/
291 void FrontendPoll( access_t *p_access )
292 {
293     access_sys_t *p_sys = p_access->p_sys;
294     frontend_t * p_frontend = p_sys->p_frontend;
295     struct dvb_frontend_event event;
296     fe_status_t i_status, i_diff;
297
298     for( ;; )
299     {
300         int i_ret = ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event );
301
302         if( i_ret < 0 )
303         {
304             if( errno == EWOULDBLOCK )
305                 return; /* no more events */
306
307             msg_Err( p_access, "reading frontend event failed (%d): %m",
308                      i_ret );
309             return;
310         }
311
312         i_status = event.status;
313         i_diff = i_status ^ p_frontend->i_last_status;
314         p_frontend->i_last_status = i_status;
315
316         {
317 #define IF_UP( x )                                                          \
318         }                                                                   \
319         if ( i_diff & (x) )                                                 \
320         {                                                                   \
321             if ( i_status & (x) )
322
323             IF_UP( FE_HAS_SIGNAL )
324                 msg_Dbg( p_access, "frontend has acquired signal" );
325             else
326                 msg_Dbg( p_access, "frontend has lost signal" );
327
328             IF_UP( FE_HAS_CARRIER )
329                 msg_Dbg( p_access, "frontend has acquired carrier" );
330             else
331                 msg_Dbg( p_access, "frontend has lost carrier" );
332
333             IF_UP( FE_HAS_VITERBI )
334                 msg_Dbg( p_access, "frontend has acquired stable FEC" );
335             else
336                 msg_Dbg( p_access, "frontend has lost FEC" );
337
338             IF_UP( FE_HAS_SYNC )
339                 msg_Dbg( p_access, "frontend has acquired sync" );
340             else
341                 msg_Dbg( p_access, "frontend has lost sync" );
342
343             IF_UP( FE_HAS_LOCK )
344             {
345                 frontend_statistic_t stat;
346
347                 msg_Dbg( p_access, "frontend has acquired lock" );
348                 p_sys->i_frontend_timeout = 0;
349
350                 /* Read some statistics */
351                 if( !FrontendGetStatistic( p_access, &stat ) )
352                 {
353                     if( stat.i_ber >= 0 )
354                         msg_Dbg( p_access, "- Bit error rate: %d", stat.i_ber );
355                     if( stat.i_signal_strenth >= 0 )
356                         msg_Dbg( p_access, "- Signal strength: %d", stat.i_signal_strenth );
357                     if( stat.i_snr >= 0 )
358                         msg_Dbg( p_access, "- SNR: %d", stat.i_snr );
359                 }
360             }
361             else
362             {
363                 msg_Dbg( p_access, "frontend has lost lock" );
364                 p_sys->i_frontend_timeout = mdate() + FRONTEND_LOCK_TIMEOUT;
365             }
366
367             IF_UP( FE_REINIT )
368             {
369                 /* The frontend was reinited. */
370                 msg_Warn( p_access, "reiniting frontend");
371                 FrontendSet( p_access );
372             }
373         }
374 #undef IF_UP
375     }
376 }
377 int FrontendGetStatistic( access_t *p_access, frontend_statistic_t *p_stat )
378 {
379     access_sys_t *p_sys = p_access->p_sys;
380     frontend_t * p_frontend = p_sys->p_frontend;
381
382     if( (p_frontend->i_last_status & FE_HAS_LOCK) == 0 )
383         return VLC_EGENERIC;
384
385     memset( p_stat, 0, sizeof(*p_stat) );
386     if( ioctl( p_sys->i_frontend_handle, FE_READ_BER, &p_stat->i_ber ) < 0 )
387         p_stat->i_ber = -1;
388     if( ioctl( p_sys->i_frontend_handle, FE_READ_SIGNAL_STRENGTH, &p_stat->i_signal_strenth ) < 0 )
389         p_stat->i_signal_strenth = -1;
390     if( ioctl( p_sys->i_frontend_handle, FE_READ_SNR, &p_stat->i_snr ) < 0 )
391         p_stat->i_snr = -1;
392
393     return VLC_SUCCESS;
394 }
395 void FrontendGetStatus( access_t *p_access, frontend_status_t *p_status )
396 {
397     access_sys_t *p_sys = p_access->p_sys;
398     frontend_t * p_frontend = p_sys->p_frontend;
399
400     p_status->b_has_signal = (p_frontend->i_last_status & FE_HAS_SIGNAL) != 0;
401     p_status->b_has_carrier = (p_frontend->i_last_status & FE_HAS_CARRIER) != 0;
402     p_status->b_has_lock = (p_frontend->i_last_status & FE_HAS_LOCK) != 0;
403 }
404 static int ScanParametersDvbC( access_t *p_access, scan_parameter_t *p_scan )
405 {
406     const frontend_t *p_frontend = p_access->p_sys->p_frontend;
407
408
409     memset( p_scan, 0, sizeof(*p_scan) );
410     p_scan->type = SCAN_DVB_C;
411     p_scan->b_exhaustive = false;
412
413     /* */
414     p_scan->frequency.i_min = p_frontend->info.frequency_min;
415     p_scan->frequency.i_max = p_frontend->info.frequency_max;
416     p_scan->frequency.i_step = p_frontend->info.frequency_stepsize
417         ? p_frontend->info.frequency_stepsize : 166667;
418     p_scan->frequency.i_count = (p_scan->frequency.i_max-p_scan->frequency.i_min)/p_scan->frequency.i_step;
419
420     /* */
421     p_scan->bandwidth.i_min  = 6;
422     p_scan->bandwidth.i_max  = 8;
423     p_scan->bandwidth.i_step = 1;
424     p_scan->bandwidth.i_count = 3;
425     return VLC_SUCCESS;
426 }
427 static int ScanParametersDvbT( access_t *p_access, scan_parameter_t *p_scan )
428 {
429     const frontend_t *p_frontend = p_access->p_sys->p_frontend;
430
431
432     memset( p_scan, 0, sizeof(*p_scan) );
433     p_scan->type = SCAN_DVB_T;
434     p_scan->b_exhaustive = false;
435
436     /* */
437     p_scan->frequency.i_min = p_frontend->info.frequency_min;
438     p_scan->frequency.i_max = p_frontend->info.frequency_max;
439     p_scan->frequency.i_step = p_frontend->info.frequency_stepsize
440         ? p_frontend->info.frequency_stepsize : 166667;
441     p_scan->frequency.i_count = (p_scan->frequency.i_max-p_scan->frequency.i_min)/p_scan->frequency.i_step;
442
443     /* */
444     p_scan->bandwidth.i_min  = 6;
445     p_scan->bandwidth.i_max  = 8;
446     p_scan->bandwidth.i_step = 1;
447     p_scan->bandwidth.i_count = 3;
448     return VLC_SUCCESS;
449 }
450 int  FrontendGetScanParameter( access_t *p_access, scan_parameter_t *p_scan )
451 {
452     access_sys_t *p_sys = p_access->p_sys;
453     const frontend_t *p_frontend = p_sys->p_frontend;
454
455     if( p_frontend->info.type == FE_OFDM )  // DVB-T
456         return ScanParametersDvbT( p_access, p_scan );
457     else if( p_frontend->info.type == FE_QAM )  // DVB-C
458         return ScanParametersDvbC( p_access, p_scan );
459
460     msg_Err( p_access, "Frontend type not supported for scanning" );
461     return VLC_EGENERIC;
462 }
463
464 #ifdef ENABLE_HTTPD
465 /*****************************************************************************
466  * FrontendStatus : Read frontend status
467  *****************************************************************************/
468 void FrontendStatus( access_t *p_access )
469 {
470     access_sys_t *p_sys = p_access->p_sys;
471     frontend_t *p_frontend = p_sys->p_frontend;
472     char *p = p_sys->psz_frontend_info = malloc( 10000 );
473     fe_status_t i_status;
474     int i_ret;
475
476     /* Determine type of frontend */
477     if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_GET_INFO,
478                         &p_frontend->info )) < 0 )
479     {
480         char buf[1000];
481         strerror_r( errno, buf, sizeof( buf ) );
482         p += sprintf( p, "ioctl FE_GET_INFO failed (%d) %s\n", i_ret, buf );
483         goto out;
484     }
485
486     /* Print out frontend capabilities. */
487     p += sprintf( p, "<table border=1><tr><th>name</th><td>%s</td></tr>\n",
488                   p_frontend->info.name );
489     switch( p_frontend->info.type )
490     {
491         case FE_QPSK:
492             p += sprintf( p, "<tr><th>type</th><td>QPSK (DVB-S)</td></tr>\n" );
493             break;
494         case FE_QAM:
495             p += sprintf( p, "<tr><th>type</th><td>QAM (DVB-C)</td></tr>\n" );
496             break;
497         case FE_OFDM:
498             p += sprintf( p, "<tr><th>type</th><td>OFDM (DVB-T)</td></tr>\n" );
499             break;
500 #if 0 /* DVB_API_VERSION == 3 */
501         case FE_MEMORY:
502             p += sprintf( p, "<tr><th>type</th><td>MEMORY</td></tr>\n" );
503             break;
504         case FE_NET:
505             p += sprintf( p, "<tr><th>type</th><td>NETWORK</td></tr>\n" );
506             break;
507 #endif
508         default:
509             p += sprintf( p, "<tr><th>type</th><td>UNKNOWN (%d)</td></tr>\n",
510                           p_frontend->info.type );
511             goto out;
512     }
513 #define CHECK_INFO( x )                                                     \
514     p += sprintf( p,                                                        \
515                   "<tr><th>" STRINGIFY(x) "</th><td>%u</td></tr>\n",        \
516                   p_frontend->info.x );
517
518     CHECK_INFO( frequency_min );
519     CHECK_INFO( frequency_max );
520     CHECK_INFO( frequency_stepsize );
521     CHECK_INFO( frequency_tolerance );
522     CHECK_INFO( symbol_rate_min );
523     CHECK_INFO( symbol_rate_max );
524     CHECK_INFO( symbol_rate_tolerance );
525     CHECK_INFO( notifier_delay );
526 #undef CHECK_INFO
527
528     p += sprintf( p, "</table><p>Frontend capability list:\n<table border=1>" );
529
530 #define CHECK_CAPS( x )                                                     \
531     if ( p_frontend->info.caps & (FE_##x) )                                 \
532         p += sprintf( p, "<tr><td>" STRINGIFY(x) "</td></tr>\n" );
533
534     CHECK_CAPS( IS_STUPID );
535     CHECK_CAPS( CAN_INVERSION_AUTO );
536     CHECK_CAPS( CAN_FEC_1_2 );
537     CHECK_CAPS( CAN_FEC_2_3 );
538     CHECK_CAPS( CAN_FEC_3_4 );
539     CHECK_CAPS( CAN_FEC_4_5 );
540     CHECK_CAPS( CAN_FEC_5_6 );
541     CHECK_CAPS( CAN_FEC_6_7 );
542     CHECK_CAPS( CAN_FEC_7_8 );
543     CHECK_CAPS( CAN_FEC_8_9 );
544     CHECK_CAPS( CAN_FEC_AUTO );
545     CHECK_CAPS( CAN_QPSK );
546     CHECK_CAPS( CAN_QAM_16 );
547     CHECK_CAPS( CAN_QAM_32 );
548     CHECK_CAPS( CAN_QAM_64 );
549     CHECK_CAPS( CAN_QAM_128 );
550     CHECK_CAPS( CAN_QAM_256 );
551     CHECK_CAPS( CAN_QAM_AUTO );
552     CHECK_CAPS( CAN_TRANSMISSION_MODE_AUTO );
553     CHECK_CAPS( CAN_BANDWIDTH_AUTO );
554     CHECK_CAPS( CAN_GUARD_INTERVAL_AUTO );
555     CHECK_CAPS( CAN_HIERARCHY_AUTO );
556     CHECK_CAPS( CAN_MUTE_TS );
557     CHECK_CAPS( CAN_RECOVER );
558 #if 0 /* Disabled because of older distributions */
559     CHECK_CAPS( CAN_CLEAN_SETUP );
560 #endif
561 #undef CHECK_CAPS
562
563     p += sprintf( p, "</table><p>Current frontend status:\n<table border=1>" );
564
565     if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_READ_STATUS, &i_status ))
566            < 0 )
567     {
568         char buf[1000];
569         strerror_r( errno, buf, sizeof( buf ) );
570         p += sprintf( p, "</table>ioctl FE_READ_STATUS failed (%d) %s\n",
571                       i_ret, buf );
572         goto out;
573     }
574
575 #define CHECK_STATUS( x )                                                   \
576     if ( i_status & (FE_##x) )                                              \
577         p += sprintf( p, "<tr><td>" STRINGIFY(x) "</td></tr>\n" );
578
579     CHECK_STATUS( HAS_SIGNAL );
580     CHECK_STATUS( HAS_CARRIER );
581     CHECK_STATUS( HAS_VITERBI );
582     CHECK_STATUS( HAS_SYNC );
583     CHECK_STATUS( HAS_LOCK );
584     CHECK_STATUS( REINIT );
585     if( i_status == 0 )
586         p += sprintf( p, "<tr><td>Tuning failed</td></tr>\n" );
587 #undef CHECK_STATUS
588
589     if ( i_status & FE_HAS_LOCK )
590     {
591         int32_t i_value;
592         p += sprintf( p, "</table><p>Signal status:\n<table border=1>" );
593         if( ioctl( p_sys->i_frontend_handle, FE_READ_BER, &i_value ) >= 0 )
594             p += sprintf( p, "<tr><th>Bit error rate</th><td>%d</td></tr>\n",
595                           i_value );
596         if( ioctl( p_sys->i_frontend_handle, FE_READ_SIGNAL_STRENGTH,
597                    &i_value ) >= 0 )
598             p += sprintf( p, "<tr><th>Signal strength</th><td>%d</td></tr>\n",
599                           i_value );
600         if( ioctl( p_sys->i_frontend_handle, FE_READ_SNR, &i_value ) >= 0 )
601             p += sprintf( p, "<tr><th>SNR</th><td>%d</td></tr>\n",
602                           i_value );
603     }
604     p += sprintf( p, "</table>" );
605
606 out:
607     vlc_mutex_lock( &p_sys->httpd_mutex );
608     p_sys->b_request_frontend_info = false;
609     vlc_cond_signal( &p_sys->httpd_cond );
610     vlc_mutex_unlock( &p_sys->httpd_mutex );
611 }
612 #endif
613
614 /*****************************************************************************
615  * FrontendInfo : Return information about given frontend
616  *****************************************************************************/
617 static int FrontendInfo( access_t *p_access )
618 {
619     access_sys_t *p_sys = p_access->p_sys;
620     frontend_t *p_frontend = p_sys->p_frontend;
621     int i_ret;
622
623     /* Determine type of frontend */
624     if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_GET_INFO,
625                         &p_frontend->info )) < 0 )
626     {
627         msg_Err( p_access, "ioctl FE_GET_INFO failed (%d): %m", i_ret );
628         return VLC_EGENERIC;
629     }
630
631     /* Print out frontend capabilities. */
632     msg_Dbg(p_access, "Frontend Info:" );
633     msg_Dbg(p_access, "  name = %s", p_frontend->info.name );
634     switch( p_frontend->info.type )
635     {
636         case FE_QPSK:
637             msg_Dbg( p_access, "  type = QPSK (DVB-S)" );
638             break;
639         case FE_QAM:
640             msg_Dbg( p_access, "  type = QAM (DVB-C)" );
641             break;
642         case FE_OFDM:
643             msg_Dbg( p_access, "  type = OFDM (DVB-T)" );
644             break;
645         case FE_ATSC:
646             msg_Dbg( p_access, "  type = ATSC (USA)" );
647             break;
648 #if 0 /* DVB_API_VERSION == 3 */
649         case FE_MEMORY:
650             msg_Dbg(p_access, "  type = MEMORY" );
651             break;
652         case FE_NET:
653             msg_Dbg(p_access, "  type = NETWORK" );
654             break;
655 #endif
656         default:
657             msg_Err( p_access, "  unknown frontend type (%d)",
658                      p_frontend->info.type );
659             return VLC_EGENERIC;
660     }
661     msg_Dbg(p_access, "  frequency_min = %u (kHz)",
662             p_frontend->info.frequency_min);
663     msg_Dbg(p_access, "  frequency_max = %u (kHz)",
664             p_frontend->info.frequency_max);
665     msg_Dbg(p_access, "  frequency_stepsize = %u",
666             p_frontend->info.frequency_stepsize);
667     msg_Dbg(p_access, "  frequency_tolerance = %u",
668             p_frontend->info.frequency_tolerance);
669     msg_Dbg(p_access, "  symbol_rate_min = %u (kHz)",
670             p_frontend->info.symbol_rate_min);
671     msg_Dbg(p_access, "  symbol_rate_max = %u (kHz)",
672             p_frontend->info.symbol_rate_max);
673     msg_Dbg(p_access, "  symbol_rate_tolerance (ppm) = %u",
674             p_frontend->info.symbol_rate_tolerance);
675     msg_Dbg(p_access, "  notifier_delay (ms) = %u",
676             p_frontend->info.notifier_delay );
677
678     msg_Dbg(p_access, "Frontend Info capability list:");
679     if( p_frontend->info.caps & FE_IS_STUPID)
680         msg_Dbg(p_access, "  no capabilities - frontend is stupid!");
681     if( p_frontend->info.caps & FE_CAN_INVERSION_AUTO)
682         msg_Dbg(p_access, "  inversion auto");
683     if( p_frontend->info.caps & FE_CAN_FEC_1_2)
684         msg_Dbg(p_access, "  forward error correction 1/2");
685     if( p_frontend->info.caps & FE_CAN_FEC_2_3)
686         msg_Dbg(p_access, "  forward error correction 2/3");
687     if( p_frontend->info.caps & FE_CAN_FEC_3_4)
688         msg_Dbg(p_access, "  forward error correction 3/4");
689     if( p_frontend->info.caps & FE_CAN_FEC_4_5)
690         msg_Dbg(p_access, "  forward error correction 4/5");
691     if( p_frontend->info.caps & FE_CAN_FEC_5_6)
692         msg_Dbg(p_access, "  forward error correction 5/6");
693     if( p_frontend->info.caps & FE_CAN_FEC_6_7)
694         msg_Dbg(p_access, "  forward error correction 6/7");
695     if( p_frontend->info.caps & FE_CAN_FEC_7_8)
696         msg_Dbg(p_access, "  forward error correction 7/8");
697     if( p_frontend->info.caps & FE_CAN_FEC_8_9)
698         msg_Dbg(p_access, "  forward error correction 8/9");
699     if( p_frontend->info.caps & FE_CAN_FEC_AUTO)
700         msg_Dbg(p_access, "  forward error correction auto");
701     if( p_frontend->info.caps & FE_CAN_QPSK)
702         msg_Dbg(p_access, "  card can do QPSK");
703     if( p_frontend->info.caps & FE_CAN_QAM_16)
704         msg_Dbg(p_access, "  card can do QAM 16");
705     if( p_frontend->info.caps & FE_CAN_QAM_32)
706         msg_Dbg(p_access, "  card can do QAM 32");
707     if( p_frontend->info.caps & FE_CAN_QAM_64)
708         msg_Dbg(p_access, "  card can do QAM 64");
709     if( p_frontend->info.caps & FE_CAN_QAM_128)
710         msg_Dbg(p_access, "  card can do QAM 128");
711     if( p_frontend->info.caps & FE_CAN_QAM_256)
712         msg_Dbg(p_access, "  card can do QAM 256");
713     if( p_frontend->info.caps & FE_CAN_QAM_AUTO)
714         msg_Dbg(p_access, "  card can do QAM auto");
715     if( p_frontend->info.caps & FE_CAN_TRANSMISSION_MODE_AUTO)
716         msg_Dbg(p_access, "  transmission mode auto");
717     if( p_frontend->info.caps & FE_CAN_BANDWIDTH_AUTO)
718         msg_Dbg(p_access, "  bandwidth mode auto");
719     if( p_frontend->info.caps & FE_CAN_GUARD_INTERVAL_AUTO)
720         msg_Dbg(p_access, "  guard interval mode auto");
721     if( p_frontend->info.caps & FE_CAN_HIERARCHY_AUTO)
722         msg_Dbg(p_access, "  hierarchy mode auto");
723     if( p_frontend->info.caps & FE_CAN_MUTE_TS)
724         msg_Dbg(p_access, "  card can mute TS");
725     if( p_frontend->info.caps & FE_CAN_RECOVER)
726         msg_Dbg(p_access, "  card can recover from a cable unplug");
727     if( p_frontend->info.caps & FE_CAN_8VSB)
728         msg_Dbg(p_access, "  card can do 8vsb");
729     if( p_frontend->info.caps & FE_CAN_16VSB)
730         msg_Dbg(p_access, "  card can do 16vsb");
731     msg_Dbg(p_access, "End of capability list");
732
733     return VLC_SUCCESS;
734 }
735
736 /*****************************************************************************
737  * Decoding the DVB parameters (common)
738  *****************************************************************************/
739 static fe_spectral_inversion_t DecodeInversion( access_t *p_access )
740 {
741     int i_val;
742     fe_spectral_inversion_t fe_inversion = 0;
743
744     i_val = var_GetInteger( p_access, "dvb-inversion" );
745     msg_Dbg( p_access, "using inversion=%d", i_val );
746
747     switch( i_val )
748     {
749         case 0: fe_inversion = INVERSION_OFF; break;
750         case 1: fe_inversion = INVERSION_ON; break;
751         case 2: fe_inversion = INVERSION_AUTO; break;
752         default:
753             msg_Dbg( p_access, "dvb has inversion not set, using auto");
754             fe_inversion = INVERSION_AUTO;
755             break;
756     }
757     return fe_inversion;
758 }
759
760 static fe_code_rate_t DecodeFEC( access_t *p_access, int i_val )
761 {
762     fe_code_rate_t      fe_fec = FEC_NONE;
763
764     msg_Dbg( p_access, "using fec=%d", i_val );
765
766     switch( i_val )
767     {
768         case 0: fe_fec = FEC_NONE; break;
769         case 1: fe_fec = FEC_1_2; break;
770         case 2: fe_fec = FEC_2_3; break;
771         case 3: fe_fec = FEC_3_4; break;
772         case 4: fe_fec = FEC_4_5; break;
773         case 5: fe_fec = FEC_5_6; break;
774         case 6: fe_fec = FEC_6_7; break;
775         case 7: fe_fec = FEC_7_8; break;
776         case 8: fe_fec = FEC_8_9; break;
777         case 9: fe_fec = FEC_AUTO; break;
778         default:
779             /* cannot happen */
780             fe_fec = FEC_NONE;
781             msg_Err( p_access, "argument has invalid FEC (%d)", i_val);
782             break;
783     }
784     return fe_fec;
785 }
786
787 static fe_modulation_t DecodeModulationQAM( access_t *p_access )
788 {
789     switch( var_GetInteger( p_access, "dvb-modulation" ) )
790     {
791         case 0:     return QAM_AUTO;
792         case 16:    return QAM_16;
793         case 32:    return QAM_32;
794         case 64:    return QAM_64;
795         case 128:   return QAM_128;
796         case 256:   return QAM_256;
797         default:
798             msg_Dbg( p_access, "QAM modulation not set, using auto");
799             return QAM_AUTO;
800     }
801 }
802 static fe_modulation_t DecodeModulationOFDM( access_t *p_access )
803 {
804     switch( var_GetInteger( p_access, "dvb-modulation" ) )
805     {
806         case -1:    return QPSK;
807         case 0:     return QAM_AUTO;
808         case 16:    return QAM_16;
809         case 32:    return QAM_32;
810         case 64:    return QAM_64;
811         case 128:   return QAM_128;
812         case 256:   return QAM_256;
813         default:
814             msg_Dbg( p_access, "OFDM modulation not set, using QAM auto");
815             return QAM_AUTO;
816     }
817 }
818 static fe_modulation_t DecodeModulationATSC( access_t *p_access )
819 {
820     switch( var_GetInteger( p_access, "dvb-modulation" ) )
821     {
822         case 0:     return QAM_AUTO;
823         case 8:     return VSB_8;
824         case 16:    return VSB_16;
825         case 32:    return QAM_32;
826         case 64:    return QAM_64;
827         case 128:   return QAM_128;
828         case 256:   return QAM_256;
829         default:
830             msg_Dbg( p_access, "ATSC modulation not set, using VSB 8");
831             return VSB_8;
832     }
833 }
834
835 /*****************************************************************************
836  * FrontendSetQPSK : controls the FE device
837  *****************************************************************************/
838 static fe_sec_voltage_t DecodeVoltage( access_t *p_access )
839 {
840     int i_val;
841     fe_sec_voltage_t    fe_voltage;
842
843     i_val = var_GetInteger( p_access, "dvb-voltage" );
844     msg_Dbg( p_access, "using voltage=%d", i_val );
845
846     switch( i_val )
847     {
848         case 0: fe_voltage = SEC_VOLTAGE_OFF; break;
849         case 13: fe_voltage = SEC_VOLTAGE_13; break;
850         case 18: fe_voltage = SEC_VOLTAGE_18; break;
851         default:
852             fe_voltage = SEC_VOLTAGE_OFF;
853             msg_Err( p_access, "argument has invalid voltage (%d)", i_val );
854             break;
855     }
856     return fe_voltage;
857 }
858
859 static fe_sec_tone_mode_t DecodeTone( access_t *p_access )
860 {
861     int i_val;
862     fe_sec_tone_mode_t  fe_tone;
863
864     i_val = var_GetInteger( p_access, "dvb-tone" );
865     msg_Dbg( p_access, "using tone=%d", i_val );
866
867     switch( i_val )
868     {
869         case 0: fe_tone = SEC_TONE_OFF; break;
870         case 1: fe_tone = SEC_TONE_ON; break;
871         default:
872             fe_tone = SEC_TONE_OFF;
873             msg_Err( p_access, "argument has invalid tone mode (%d)", i_val );
874             break;
875     }
876     return fe_tone;
877 }
878
879 struct diseqc_cmd_t
880 {
881     struct dvb_diseqc_master_cmd cmd;
882     uint32_t wait;
883 };
884
885 static int DoDiseqc( access_t *p_access )
886 {
887     access_sys_t *p_sys = p_access->p_sys;
888     int i_val;
889     bool b_val;
890     int i_frequency, i_lnb_slof;
891     fe_sec_voltage_t fe_voltage;
892     fe_sec_tone_mode_t fe_tone;
893     int i_err;
894
895     i_frequency = var_GetInteger( p_access, "dvb-frequency" );
896     i_lnb_slof = var_GetInteger( p_access, "dvb-lnb-slof" );
897
898     i_val = var_GetInteger( p_access, "dvb-tone" );
899     if( i_val == -1 /* auto */ )
900     {
901         if( i_frequency >= i_lnb_slof )
902             i_val = 1;
903         else
904             i_val = 0;
905         var_SetInteger( p_access, "dvb-tone", i_val );
906     }
907
908     fe_voltage = DecodeVoltage( p_access );
909     fe_tone = DecodeTone( p_access );
910
911     /* Switch off continuous tone. */
912     if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_TONE, SEC_TONE_OFF )) < 0 )
913     {
914         msg_Err( p_access, "ioctl FE_SET_TONE failed, tone=%s (%d) %m",
915                  fe_tone == SEC_TONE_ON ? "on" : "off", i_err );
916         return i_err;
917     }
918
919     /* Configure LNB voltage. */
920     if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_VOLTAGE, fe_voltage )) < 0 )
921     {
922         msg_Err( p_access, "ioctl FE_SET_VOLTAGE failed, voltage=%d (%d) %m",
923                  fe_voltage, i_err );
924         return i_err;
925     }
926
927     b_val = var_GetBool( p_access, "dvb-high-voltage" );
928     if( (i_err = ioctl( p_sys->i_frontend_handle, FE_ENABLE_HIGH_LNB_VOLTAGE,
929                         b_val )) < 0 && b_val )
930     {
931         msg_Err( p_access,
932                  "ioctl FE_ENABLE_HIGH_LNB_VOLTAGE failed, val=%d (%d) %m",
933                  b_val, i_err );
934     }
935
936     /* Wait for at least 15 ms. */
937     msleep(15000);
938
939     i_val = var_GetInteger( p_access, "dvb-satno" );
940     if( i_val > 0 && i_val < 5 )
941     {
942         /* digital satellite equipment control,
943          * specification is available from http://www.eutelsat.com/
944          */
945
946         /* 1.x compatible equipment */
947         struct diseqc_cmd_t cmd =  { {{0xe0, 0x10, 0x38, 0xf0, 0x00, 0x00}, 4}, 0 };
948
949         /* param: high nibble: reset bits, low nibble set bits,
950          * bits are: option, position, polarization, band
951          */
952         cmd.cmd.msg[3] = 0xf0 /* reset bits */
953                           | (((i_val - 1) * 4) & 0xc)
954                           | (fe_voltage == SEC_VOLTAGE_13 ? 0 : 2)
955                           | (fe_tone == SEC_TONE_ON ? 1 : 0);
956
957         if( (i_err = ioctl( p_sys->i_frontend_handle, FE_DISEQC_SEND_MASTER_CMD,
958                            &cmd.cmd )) < 0 )
959         {
960             msg_Err( p_access, "ioctl FE_SEND_MASTER_CMD failed (%d) %m",
961                      i_err );
962             return i_err;
963         }
964
965         msleep(15000 + cmd.wait * 1000);
966
967         /* A or B simple diseqc ("diseqc-compatible") */
968         if( (i_err = ioctl( p_sys->i_frontend_handle, FE_DISEQC_SEND_BURST,
969                       ((i_val - 1) % 2) ? SEC_MINI_B : SEC_MINI_A )) < 0 )
970         {
971             msg_Err( p_access, "ioctl FE_SEND_BURST failed (%d) %m",
972                      i_err );
973             return i_err;
974         }
975
976         msleep(15000);
977     }
978
979     if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_TONE, fe_tone )) < 0 )
980     {
981         msg_Err( p_access, "ioctl FE_SET_TONE failed, tone=%s (%d) %m",
982                  fe_tone == SEC_TONE_ON ? "on" : "off", i_err );
983         return i_err;
984     }
985
986     msleep(50000);
987     return 0;
988 }
989
990 static int FrontendSetQPSK( access_t *p_access )
991 {
992     access_sys_t *p_sys = p_access->p_sys;
993     struct dvb_frontend_parameters fep;
994     int i_ret;
995     int i_val;
996     int i_frequency, i_lnb_slof = 0, i_lnb_lof1, i_lnb_lof2 = 0;
997
998     /* Prepare the fep structure */
999     i_frequency = var_GetInteger( p_access, "dvb-frequency" );
1000
1001     i_val = var_GetInteger( p_access, "dvb-lnb-lof1" );
1002     if( i_val == 0 )
1003     {
1004         /* Automatic mode. */
1005         if ( i_frequency >= 950000 && i_frequency <= 2150000 )
1006         {
1007             msg_Dbg( p_access, "frequency %d is in IF-band", i_frequency );
1008             i_lnb_lof1 = 0;
1009         }
1010         else if ( i_frequency >= 2500000 && i_frequency <= 2700000 )
1011         {
1012             msg_Dbg( p_access, "frequency %d is in S-band", i_frequency );
1013             i_lnb_lof1 = 3650000;
1014         }
1015         else if ( i_frequency >= 3400000 && i_frequency <= 4200000 )
1016         {
1017             msg_Dbg( p_access, "frequency %d is in C-band (lower)",
1018                      i_frequency );
1019             i_lnb_lof1 = 5150000;
1020         }
1021         else if ( i_frequency >= 4500000 && i_frequency <= 4800000 )
1022         {
1023             msg_Dbg( p_access, "frequency %d is in C-band (higher)",
1024                      i_frequency );
1025             i_lnb_lof1 = 5950000;
1026         }
1027         else if ( i_frequency >= 10700000 && i_frequency <= 13250000 )
1028         {
1029             msg_Dbg( p_access, "frequency %d is in Ku-band",
1030                      i_frequency );
1031             i_lnb_lof1 = 9750000;
1032             i_lnb_lof2 = 10600000;
1033             i_lnb_slof = 11700000;
1034         }
1035         else
1036         {
1037             msg_Err( p_access, "frequency %d is out of any known band",
1038                      i_frequency );
1039             msg_Err( p_access, "specify dvb-lnb-lof1 manually for the local "
1040                      "oscillator frequency" );
1041             return VLC_EGENERIC;
1042         }
1043         var_SetInteger( p_access, "dvb-lnb-lof1", i_lnb_lof1 );
1044         var_SetInteger( p_access, "dvb-lnb-lof2", i_lnb_lof2 );
1045         var_SetInteger( p_access, "dvb-lnb-slof", i_lnb_slof );
1046     }
1047     else
1048     {
1049         i_lnb_lof1 = i_val;
1050         i_lnb_lof2 = var_GetInteger( p_access, "dvb-lnb-lof2" );
1051         i_lnb_slof = var_GetInteger( p_access, "dvb-lnb-slof" );
1052     }
1053
1054     if( i_lnb_slof && i_frequency >= i_lnb_slof )
1055     {
1056         i_frequency -= i_lnb_lof2;
1057     }
1058     else
1059     {
1060         i_frequency -= i_lnb_lof1;
1061     }
1062     fep.frequency = i_frequency >= 0 ? i_frequency : -i_frequency;
1063
1064     fep.inversion = DecodeInversion( p_access );
1065
1066     fep.u.qpsk.symbol_rate = var_GetInteger( p_access, "dvb-srate" );
1067
1068     fep.u.qpsk.fec_inner = DecodeFEC( p_access, var_GetInteger( p_access, "dvb-fec" ) );
1069
1070     if( DoDiseqc( p_access ) < 0 )
1071     {
1072         return VLC_EGENERIC;
1073     }
1074
1075     /* Empty the event queue */
1076     for( ; ; )
1077     {
1078         struct dvb_frontend_event event;
1079         if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
1080               && errno == EWOULDBLOCK )
1081             break;
1082     }
1083
1084     /* Now send it all to the frontend device */
1085     if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
1086     {
1087         msg_Err( p_access, "DVB-S: setting frontend failed (%d) %m", i_ret );
1088         return VLC_EGENERIC;
1089     }
1090
1091     return VLC_SUCCESS;
1092 }
1093
1094 /*****************************************************************************
1095  * FrontendSetQAM : controls the FE device
1096  *****************************************************************************/
1097 static int FrontendSetQAM( access_t *p_access )
1098 {
1099     access_sys_t *p_sys = p_access->p_sys;
1100     frontend_t *p_frontend = p_sys->p_frontend;
1101     struct dvb_frontend_parameters fep;
1102     int i_val;
1103     int i_ret;
1104
1105     /* Prepare the fep structure */
1106
1107     fep.frequency = var_GetInteger( p_access, "dvb-frequency" );
1108
1109     fep.inversion = DecodeInversion( p_access );
1110
1111     /* Default symbol-rate is for dvb-s, and doesn't fit
1112      * for dvb-c, so if it's over the limit of frontend, default to
1113      * somewhat common value
1114      */
1115     i_val = var_GetInteger( p_access, "dvb-srate" );
1116     if( i_val < p_frontend->info.symbol_rate_max &&
1117         i_val > p_frontend->info.symbol_rate_min )
1118         fep.u.qam.symbol_rate = i_val;
1119     else
1120         fep.u.qam.symbol_rate = 6875000;
1121
1122     fep.u.qam.fec_inner = DecodeFEC( p_access, var_GetInteger( p_access,
1123                                                                "dvb-fec" ) );
1124
1125     fep.u.qam.modulation = DecodeModulationQAM( p_access );
1126
1127     /* Empty the event queue */
1128     for( ; ; )
1129     {
1130         struct dvb_frontend_event event;
1131         if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
1132               && errno == EWOULDBLOCK )
1133             break;
1134     }
1135
1136     /* Now send it all to the frontend device */
1137     if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
1138     {
1139         msg_Err( p_access, "DVB-C: setting frontend failed (%d): %m", i_ret );
1140         return VLC_EGENERIC;
1141     }
1142
1143     return VLC_SUCCESS;
1144 }
1145
1146 /*****************************************************************************
1147  * FrontendSetOFDM : controls the FE device
1148  *****************************************************************************/
1149 static fe_bandwidth_t DecodeBandwidth( access_t *p_access )
1150 {
1151     fe_bandwidth_t      fe_bandwidth = 0;
1152     int i_bandwith = var_GetInteger( p_access, "dvb-bandwidth" );
1153
1154     msg_Dbg( p_access, "using bandwidth=%d", i_bandwith );
1155
1156     switch( i_bandwith )
1157     {
1158         case 0: fe_bandwidth = BANDWIDTH_AUTO; break;
1159         case 6: fe_bandwidth = BANDWIDTH_6_MHZ; break;
1160         case 7: fe_bandwidth = BANDWIDTH_7_MHZ; break;
1161         case 8: fe_bandwidth = BANDWIDTH_8_MHZ; break;
1162         default:
1163             msg_Dbg( p_access, "terrestrial dvb has bandwidth not set, using auto" );
1164             fe_bandwidth = BANDWIDTH_AUTO;
1165             break;
1166     }
1167     return fe_bandwidth;
1168 }
1169
1170 static fe_transmit_mode_t DecodeTransmission( access_t *p_access )
1171 {
1172     fe_transmit_mode_t  fe_transmission = 0;
1173     int i_transmission = var_GetInteger( p_access, "dvb-transmission" );
1174
1175     msg_Dbg( p_access, "using transmission=%d", i_transmission );
1176
1177     switch( i_transmission )
1178     {
1179         case 0: fe_transmission = TRANSMISSION_MODE_AUTO; break;
1180         case 2: fe_transmission = TRANSMISSION_MODE_2K; break;
1181         case 8: fe_transmission = TRANSMISSION_MODE_8K; break;
1182         default:
1183             msg_Dbg( p_access, "terrestrial dvb has transmission mode not set, using auto");
1184             fe_transmission = TRANSMISSION_MODE_AUTO;
1185             break;
1186     }
1187     return fe_transmission;
1188 }
1189
1190 static fe_guard_interval_t DecodeGuardInterval( access_t *p_access )
1191 {
1192     fe_guard_interval_t fe_guard = 0;
1193     int i_guard = var_GetInteger( p_access, "dvb-guard" );
1194
1195     msg_Dbg( p_access, "using guard=%d", i_guard );
1196
1197     switch( i_guard )
1198     {
1199         case 0: fe_guard = GUARD_INTERVAL_AUTO; break;
1200         case 4: fe_guard = GUARD_INTERVAL_1_4; break;
1201         case 8: fe_guard = GUARD_INTERVAL_1_8; break;
1202         case 16: fe_guard = GUARD_INTERVAL_1_16; break;
1203         case 32: fe_guard = GUARD_INTERVAL_1_32; break;
1204         default:
1205             msg_Dbg( p_access, "terrestrial dvb has guard interval not set, using auto");
1206             fe_guard = GUARD_INTERVAL_AUTO;
1207             break;
1208     }
1209     return fe_guard;
1210 }
1211
1212 static fe_hierarchy_t DecodeHierarchy( access_t *p_access )
1213 {
1214     fe_hierarchy_t      fe_hierarchy = 0;
1215     int i_hierarchy = var_GetInteger( p_access, "dvb-hierarchy" );
1216
1217     msg_Dbg( p_access, "using hierarchy=%d", i_hierarchy );
1218
1219     switch( i_hierarchy )
1220     {
1221         case -1: fe_hierarchy = HIERARCHY_NONE; break;
1222         case 0: fe_hierarchy = HIERARCHY_AUTO; break;
1223         case 1: fe_hierarchy = HIERARCHY_1; break;
1224         case 2: fe_hierarchy = HIERARCHY_2; break;
1225         case 4: fe_hierarchy = HIERARCHY_4; break;
1226         default:
1227             msg_Dbg( p_access, "terrestrial dvb has hierarchy not set, using auto");
1228             fe_hierarchy = HIERARCHY_AUTO;
1229             break;
1230     }
1231     return fe_hierarchy;
1232 }
1233
1234 static int FrontendSetOFDM( access_t * p_access )
1235 {
1236     access_sys_t *p_sys = p_access->p_sys;
1237     struct dvb_frontend_parameters fep;
1238     int ret;
1239
1240     /* Prepare the fep structure */
1241
1242     fep.frequency = var_GetInteger( p_access, "dvb-frequency" );
1243
1244     fep.inversion = DecodeInversion( p_access );
1245
1246     fep.u.ofdm.bandwidth = DecodeBandwidth( p_access );
1247     fep.u.ofdm.code_rate_HP = DecodeFEC( p_access, var_GetInteger( p_access,
1248                                                         "dvb-code-rate-hp" ) );
1249     fep.u.ofdm.code_rate_LP = DecodeFEC( p_access, var_GetInteger( p_access,
1250                                                         "dvb-code-rate-lp" ) );
1251     fep.u.ofdm.constellation = DecodeModulationOFDM( p_access );
1252     fep.u.ofdm.transmission_mode = DecodeTransmission( p_access );
1253     fep.u.ofdm.guard_interval = DecodeGuardInterval( p_access );
1254     fep.u.ofdm.hierarchy_information = DecodeHierarchy( p_access );
1255
1256     /* Empty the event queue */
1257     for( ; ; )
1258     {
1259         struct dvb_frontend_event event;
1260         if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
1261               && errno == EWOULDBLOCK )
1262             break;
1263     }
1264
1265     /* Now send it all to the frontend device */
1266     if( (ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
1267     {
1268         msg_Err( p_access, "DVB-T: setting frontend failed (%d): %m", ret );
1269         return -1;
1270     }
1271
1272     return VLC_SUCCESS;
1273 }
1274
1275 /*****************************************************************************
1276  * FrontendSetATSC : controls the FE device
1277  *****************************************************************************/
1278 static int FrontendSetATSC( access_t *p_access )
1279 {
1280     access_sys_t *p_sys = p_access->p_sys;
1281     struct dvb_frontend_parameters fep;
1282     int i_ret;
1283
1284     /* Prepare the fep structure */
1285
1286     fep.frequency = var_GetInteger( p_access, "dvb-frequency" );
1287
1288     fep.u.vsb.modulation = DecodeModulationATSC( p_access );
1289
1290     /* Empty the event queue */
1291     for( ; ; )
1292     {
1293         struct dvb_frontend_event event;
1294         if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
1295               && errno == EWOULDBLOCK )
1296             break;
1297     }
1298
1299     /* Now send it all to the frontend device */
1300     if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
1301     {
1302         msg_Err( p_access, "ATSC: setting frontend failed (%d): %m", i_ret );
1303         return VLC_EGENERIC;
1304     }
1305
1306     return VLC_SUCCESS;
1307 }
1308
1309
1310 /*
1311  * Demux
1312  */
1313
1314 /*****************************************************************************
1315  * DMXSetFilter : controls the demux to add a filter
1316  *****************************************************************************/
1317 int DMXSetFilter( access_t * p_access, int i_pid, int * pi_fd, int i_type )
1318 {
1319     struct dmx_pes_filter_params s_filter_params;
1320     int i_ret;
1321     unsigned int i_adapter, i_device;
1322     char dmx[128];
1323
1324     i_adapter = var_GetInteger( p_access, "dvb-adapter" );
1325     i_device = var_GetInteger( p_access, "dvb-device" );
1326
1327     if( snprintf( dmx, sizeof(dmx), DMX, i_adapter, i_device )
1328             >= (int)sizeof(dmx) )
1329     {
1330         msg_Err( p_access, "snprintf() truncated string for DMX" );
1331         dmx[sizeof(dmx) - 1] = '\0';
1332     }
1333
1334     msg_Dbg( p_access, "Opening device %s", dmx );
1335     if( (*pi_fd = vlc_open(dmx, O_RDWR)) < 0 )
1336     {
1337         msg_Err( p_access, "DMXSetFilter: opening device failed (%m)" );
1338         return VLC_EGENERIC;
1339     }
1340
1341     /* We fill the DEMUX structure : */
1342     s_filter_params.pid     =   i_pid;
1343     s_filter_params.input   =   DMX_IN_FRONTEND;
1344     s_filter_params.output  =   DMX_OUT_TS_TAP;
1345     s_filter_params.flags   =   DMX_IMMEDIATE_START;
1346
1347     switch ( i_type )
1348     {   /* First device */
1349         case 1:
1350             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO0 for PID %d", i_pid);
1351             s_filter_params.pes_type = DMX_PES_VIDEO0;
1352             break;
1353         case 2:
1354             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO0 for PID %d", i_pid);
1355             s_filter_params.pes_type = DMX_PES_AUDIO0;
1356             break;
1357         case 3:
1358             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT0 for PID %d", i_pid);
1359             s_filter_params.pes_type = DMX_PES_TELETEXT0;
1360             break;
1361         case 4:
1362             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE0 for PID %d", i_pid);
1363             s_filter_params.pes_type = DMX_PES_SUBTITLE0;
1364             break;
1365         case 5:
1366             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR0 for PID %d", i_pid);
1367             s_filter_params.pes_type = DMX_PES_PCR0;
1368             break;
1369         /* Second device */
1370         case 6:
1371             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO1 for PID %d", i_pid);
1372             s_filter_params.pes_type = DMX_PES_VIDEO1;
1373             break;
1374         case 7:
1375             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO1 for PID %d", i_pid);
1376             s_filter_params.pes_type = DMX_PES_AUDIO1;
1377             break;
1378         case 8:
1379             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT1 for PID %d", i_pid);
1380             s_filter_params.pes_type = DMX_PES_TELETEXT1;
1381             break;
1382         case 9:
1383             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE1 for PID %d", i_pid);
1384             s_filter_params.pes_type = DMX_PES_SUBTITLE1;
1385             break;
1386         case 10:
1387             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR1 for PID %d", i_pid);
1388             s_filter_params.pes_type = DMX_PES_PCR1;
1389             break;
1390         /* Third device */
1391         case 11:
1392             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO2 for PID %d", i_pid);
1393             s_filter_params.pes_type = DMX_PES_VIDEO2;
1394             break;
1395         case 12:
1396             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO2 for PID %d", i_pid);
1397             s_filter_params.pes_type = DMX_PES_AUDIO2;
1398             break;
1399         case 13:
1400             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT2 for PID %d", i_pid);
1401             s_filter_params.pes_type = DMX_PES_TELETEXT2;
1402             break;
1403         case 14:
1404             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE2 for PID %d", i_pid);
1405             s_filter_params.pes_type = DMX_PES_SUBTITLE2;
1406             break;
1407         case 15:
1408             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR2 for PID %d", i_pid);
1409             s_filter_params.pes_type = DMX_PES_PCR2;
1410             break;
1411         /* Forth device */
1412         case 16:
1413             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO3 for PID %d", i_pid);
1414             s_filter_params.pes_type = DMX_PES_VIDEO3;
1415             break;
1416         case 17:
1417             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO3 for PID %d", i_pid);
1418             s_filter_params.pes_type = DMX_PES_AUDIO3;
1419             break;
1420         case 18:
1421             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT3 for PID %d", i_pid);
1422             s_filter_params.pes_type = DMX_PES_TELETEXT3;
1423             break;
1424         case 19:
1425             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE3 for PID %d", i_pid);
1426             s_filter_params.pes_type = DMX_PES_SUBTITLE3;
1427             break;
1428         case 20:
1429             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR3 for PID %d", i_pid);
1430             s_filter_params.pes_type = DMX_PES_PCR3;
1431             break;
1432         /* Usually used by Nova cards */
1433         case 21:
1434         default:
1435             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_OTHER for PID %d", i_pid);
1436             s_filter_params.pes_type = DMX_PES_OTHER;
1437             break;
1438     }
1439
1440     /* We then give the order to the device : */
1441     if( (i_ret = ioctl( *pi_fd, DMX_SET_PES_FILTER, &s_filter_params )) < 0 )
1442     {
1443         msg_Err( p_access, "DMXSetFilter: failed with %d (%m)", i_ret );
1444         return VLC_EGENERIC;
1445     }
1446     return VLC_SUCCESS;
1447 }
1448
1449 /*****************************************************************************
1450  * DMXUnsetFilter : removes a filter
1451  *****************************************************************************/
1452 int DMXUnsetFilter( access_t * p_access, int i_fd )
1453 {
1454     int i_ret;
1455
1456     if( (i_ret = ioctl( i_fd, DMX_STOP )) < 0 )
1457     {
1458         msg_Err( p_access, "DMX_STOP failed for demux (%d): %m", i_ret );
1459         return i_ret;
1460     }
1461
1462     msg_Dbg( p_access, "DMXUnsetFilter: closing demux %d", i_fd );
1463     close( i_fd );
1464     return VLC_SUCCESS;
1465 }
1466
1467
1468 /*
1469  * DVR device
1470  */
1471
1472 /*****************************************************************************
1473  * DVROpen :
1474  *****************************************************************************/
1475 int DVROpen( access_t * p_access )
1476 {
1477     access_sys_t *p_sys = p_access->p_sys;
1478     unsigned int i_adapter, i_device;
1479     char dvr[128];
1480
1481     i_adapter = var_GetInteger( p_access, "dvb-adapter" );
1482     i_device = var_GetInteger( p_access, "dvb-device" );
1483
1484     if( snprintf( dvr, sizeof(dvr), DVR, i_adapter, i_device )
1485             >= (int)sizeof(dvr) )
1486     {
1487         msg_Err( p_access, "snprintf() truncated string for DVR" );
1488         dvr[sizeof(dvr) - 1] = '\0';
1489     }
1490
1491     msg_Dbg( p_access, "Opening device %s", dvr );
1492     if( (p_sys->i_handle = vlc_open(dvr, O_RDONLY)) < 0 )
1493     {
1494         msg_Err( p_access, "DVROpen: opening device failed (%m)" );
1495         return VLC_EGENERIC;
1496     }
1497
1498     if( fcntl( p_sys->i_handle, F_SETFL, O_NONBLOCK ) == -1 )
1499     {
1500         msg_Warn( p_access, "DVROpen: couldn't set non-blocking mode (%m)" );
1501     }
1502
1503     return VLC_SUCCESS;
1504 }
1505
1506 /*****************************************************************************
1507  * DVRClose :
1508  *****************************************************************************/
1509 void DVRClose( access_t * p_access )
1510 {
1511     access_sys_t *p_sys = p_access->p_sys;
1512
1513     close( p_sys->i_handle );
1514 }
1515
1516
1517 /*
1518  * CAM device
1519  */
1520
1521 /*****************************************************************************
1522  * CAMOpen :
1523  *****************************************************************************/
1524 int CAMOpen( access_t *p_access )
1525 {
1526     access_sys_t *p_sys = p_access->p_sys;
1527     char ca[128];
1528     int i_adapter, i_device;
1529     ca_caps_t caps;
1530
1531     i_adapter = var_GetInteger( p_access, "dvb-adapter" );
1532     i_device = var_GetInteger( p_access, "dvb-device" );
1533
1534     if( snprintf( ca, sizeof(ca), CA, i_adapter, i_device ) >= (int)sizeof(ca) )
1535     {
1536         msg_Err( p_access, "snprintf() truncated string for CA" );
1537         ca[sizeof(ca) - 1] = '\0';
1538     }
1539     memset( &caps, 0, sizeof( ca_caps_t ));
1540
1541     msg_Dbg( p_access, "Opening device %s", ca );
1542     if( (p_sys->i_ca_handle = vlc_open(ca, O_RDWR | O_NONBLOCK)) < 0 )
1543     {
1544         msg_Warn( p_access, "CAMInit: opening CAM device failed (%m)" );
1545         p_sys->i_ca_handle = 0;
1546         return VLC_EGENERIC;
1547     }
1548
1549     if ( ioctl( p_sys->i_ca_handle, CA_GET_CAP, &caps ) != 0 )
1550     {
1551         msg_Err( p_access, "CAMInit: ioctl() error getting CAM capabilities" );
1552         close( p_sys->i_ca_handle );
1553         p_sys->i_ca_handle = 0;
1554         return VLC_EGENERIC;
1555     }
1556
1557     /* Output CA capabilities */
1558     msg_Dbg( p_access, "CAMInit: CA interface with %d %s", caps.slot_num,
1559         caps.slot_num == 1 ? "slot" : "slots" );
1560     if ( caps.slot_type & CA_CI )
1561         msg_Dbg( p_access, "CAMInit: CI high level interface type" );
1562     if ( caps.slot_type & CA_CI_LINK )
1563         msg_Dbg( p_access, "CAMInit: CI link layer level interface type" );
1564     if ( caps.slot_type & CA_CI_PHYS )
1565         msg_Dbg( p_access, "CAMInit: CI physical layer level interface type (not supported) " );
1566     if ( caps.slot_type & CA_DESCR )
1567         msg_Dbg( p_access, "CAMInit: built-in descrambler detected" );
1568     if ( caps.slot_type & CA_SC )
1569         msg_Dbg( p_access, "CAMInit: simple smart card interface" );
1570
1571     msg_Dbg( p_access, "CAMInit: %d available %s", caps.descr_num,
1572         caps.descr_num == 1 ? "descrambler (key)" : "descramblers (keys)" );
1573     if ( caps.descr_type & CA_ECD )
1574         msg_Dbg( p_access, "CAMInit: ECD scrambling system supported" );
1575     if ( caps.descr_type & CA_NDS )
1576         msg_Dbg( p_access, "CAMInit: NDS scrambling system supported" );
1577     if ( caps.descr_type & CA_DSS )
1578         msg_Dbg( p_access, "CAMInit: DSS scrambling system supported" );
1579  
1580     if ( caps.slot_num == 0 )
1581     {
1582         msg_Err( p_access, "CAMInit: CAM module with no slots" );
1583         close( p_sys->i_ca_handle );
1584         p_sys->i_ca_handle = 0;
1585         return VLC_EGENERIC;
1586     }
1587
1588     if( caps.slot_type & CA_CI_LINK )
1589     {
1590         p_sys->i_ca_type = CA_CI_LINK;
1591     }
1592     else if( caps.slot_type & CA_CI )
1593     {
1594         p_sys->i_ca_type = CA_CI;
1595     }
1596     else
1597     {
1598         p_sys->i_ca_type = -1;
1599         msg_Err( p_access, "CAMInit: incompatible CAM interface" );
1600         close( p_sys->i_ca_handle );
1601         p_sys->i_ca_handle = 0;
1602         return VLC_EGENERIC;
1603     }
1604
1605     p_sys->i_nb_slots = caps.slot_num;
1606     memset( p_sys->pb_active_slot, 0, sizeof(bool) * MAX_CI_SLOTS );
1607     memset( p_sys->pb_slot_mmi_expected, 0, sizeof(bool) * MAX_CI_SLOTS );
1608     memset( p_sys->pb_slot_mmi_undisplayed, 0,
1609             sizeof(bool) * MAX_CI_SLOTS );
1610
1611     return en50221_Init( p_access );
1612 }
1613
1614 /*****************************************************************************
1615  * CAMPoll :
1616  *****************************************************************************/
1617 int CAMPoll( access_t * p_access )
1618 {
1619     access_sys_t *p_sys = p_access->p_sys;
1620     int i_ret = VLC_EGENERIC;
1621
1622     if ( p_sys->i_ca_handle == 0 )
1623     {
1624         return VLC_EGENERIC;
1625     }
1626
1627     switch( p_sys->i_ca_type )
1628     {
1629     case CA_CI_LINK:
1630         i_ret = en50221_Poll( p_access );
1631         break;
1632     case CA_CI:
1633         i_ret = VLC_SUCCESS;
1634         break;
1635     default:
1636         msg_Err( p_access, "CAMPoll: This should not happen" );
1637         break;
1638     }
1639
1640     return i_ret;
1641 }
1642
1643 #ifdef ENABLE_HTTPD
1644 /*****************************************************************************
1645  * CAMStatus :
1646  *****************************************************************************/
1647 void CAMStatus( access_t * p_access )
1648 {
1649     access_sys_t *p_sys = p_access->p_sys;
1650     char *p;
1651     ca_caps_t caps;
1652     int i_slot, i;
1653
1654     if ( p_sys->psz_request != NULL && *p_sys->psz_request )
1655     {
1656         /* Check if we have an undisplayed MMI message : in that case we ignore
1657          * the user input to avoid confusing the CAM. */
1658         for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
1659         {
1660             if ( p_sys->pb_slot_mmi_undisplayed[i_slot] == true )
1661             {
1662                 p_sys->psz_request = NULL;
1663                 msg_Dbg( p_access,
1664                          "ignoring user request because of a new MMI object" );
1665                 break;
1666             }
1667         }
1668     }
1669
1670     if ( p_sys->psz_request != NULL && *p_sys->psz_request )
1671     {
1672         /* We have a mission to accomplish. */
1673         en50221_mmi_object_t mmi_object;
1674         char *psz_request = p_sys->psz_request;
1675         char psz_value[255];
1676         int i_slot;
1677         bool b_ok = false;
1678
1679         p_sys->psz_request = NULL;
1680
1681         if ( HTTPExtractValue( psz_request, "slot", psz_value,
1682                                    sizeof(psz_value) ) == NULL )
1683         {
1684             p_sys->psz_mmi_info = strdup( "invalid request parameter\n" );
1685             goto out;
1686         }
1687         i_slot = atoi(psz_value);
1688
1689         if ( HTTPExtractValue( psz_request, "open", psz_value,
1690                                    sizeof(psz_value) ) != NULL )
1691         {
1692             en50221_OpenMMI( p_access, i_slot );
1693             return;
1694         }
1695
1696         if ( HTTPExtractValue( psz_request, "close", psz_value,
1697                                    sizeof(psz_value) ) != NULL )
1698         {
1699             en50221_CloseMMI( p_access, i_slot );
1700             return;
1701         }
1702
1703         if ( HTTPExtractValue( psz_request, "cancel", psz_value,
1704                                    sizeof(psz_value) ) == NULL )
1705         {
1706             b_ok = true;
1707         }
1708
1709         if ( HTTPExtractValue( psz_request, "type", psz_value,
1710                                    sizeof(psz_value) ) == NULL )
1711         {
1712             p_sys->psz_mmi_info = strdup( "invalid request parameter\n" );
1713             goto out;
1714         }
1715
1716         if ( !strcmp( psz_value, "enq" ) )
1717         {
1718             mmi_object.i_object_type = EN50221_MMI_ANSW;
1719             mmi_object.u.answ.b_ok = b_ok;
1720             if ( b_ok == false )
1721             {
1722                 mmi_object.u.answ.psz_answ = strdup("");
1723             }
1724             else
1725             {
1726                 if ( HTTPExtractValue( psz_request, "answ", psz_value,
1727                                            sizeof(psz_value) ) == NULL )
1728                 {
1729                     p_sys->psz_mmi_info = strdup( "invalid request parameter\n" );
1730                     goto out;
1731                 }
1732
1733                 mmi_object.u.answ.psz_answ = strdup(psz_value);
1734             }
1735         }
1736         else
1737         {
1738             mmi_object.i_object_type = EN50221_MMI_MENU_ANSW;
1739             if ( b_ok == false )
1740             {
1741                 mmi_object.u.menu_answ.i_choice = 0;
1742             }
1743             else
1744             {
1745                 if ( HTTPExtractValue( psz_request, "choice", psz_value,
1746                                            sizeof(psz_value) ) == NULL )
1747                     mmi_object.u.menu_answ.i_choice = 0;
1748                 else
1749                     mmi_object.u.menu_answ.i_choice = atoi(psz_value);
1750             }
1751         }
1752
1753         en50221_SendMMIObject( p_access, i_slot, &mmi_object );
1754         return;
1755     }
1756
1757     /* Check that we have all necessary MMI information. */
1758     for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
1759     {
1760         if ( p_sys->pb_slot_mmi_expected[i_slot] == true )
1761             return;
1762     }
1763
1764     p = p_sys->psz_mmi_info = malloc( 10000 );
1765
1766     if ( ioctl( p_sys->i_ca_handle, CA_GET_CAP, &caps ) != 0 )
1767     {
1768         char buf[1000];
1769         strerror_r( errno, buf, sizeof( buf ) );
1770         p += sprintf( p, "ioctl CA_GET_CAP failed (%s)\n", buf );
1771         goto out;
1772     }
1773
1774     /* Output CA capabilities */
1775     p += sprintf( p, "CA interface with %d %s, type:\n<table>", caps.slot_num,
1776                   caps.slot_num == 1 ? "slot" : "slots" );
1777 #define CHECK_CAPS( x, s )                                                  \
1778     if ( caps.slot_type & (CA_##x) )                                        \
1779         p += sprintf( p, "<tr><td>" s "</td></tr>\n" );
1780
1781     CHECK_CAPS( CI, "CI high level interface" );
1782     CHECK_CAPS( CI_LINK, "CI link layer level interface" );
1783     CHECK_CAPS( CI_PHYS, "CI physical layer level interface (not supported)" );
1784     CHECK_CAPS( DESCR, "built-in descrambler" );
1785     CHECK_CAPS( SC, "simple smartcard interface" );
1786 #undef CHECK_CAPS
1787
1788     p += sprintf( p, "</table>%d available %s\n<table>", caps.descr_num,
1789         caps.descr_num == 1 ? "descrambler (key)" : "descramblers (keys)" );
1790 #define CHECK_DESC( x )                                                     \
1791     if ( caps.descr_type & (CA_##x) )                                       \
1792         p += sprintf( p, "<tr><td>" STRINGIFY(x) "</td></tr>\n" );
1793
1794     CHECK_DESC( ECD );
1795     CHECK_DESC( NDS );
1796     CHECK_DESC( DSS );
1797 #undef CHECK_DESC
1798
1799     p += sprintf( p, "</table>" );
1800
1801     for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
1802     {
1803         ca_slot_info_t sinfo;
1804
1805         p_sys->pb_slot_mmi_undisplayed[i_slot] = false;
1806         p += sprintf( p, "<p>CA slot #%d: ", i_slot );
1807
1808         sinfo.num = i_slot;
1809         if ( ioctl( p_sys->i_ca_handle, CA_GET_SLOT_INFO, &sinfo ) != 0 )
1810         {
1811             char buf[1000];
1812             strerror_r( errno, buf, sizeof( buf ) );
1813             p += sprintf( p, "ioctl CA_GET_SLOT_INFO failed (%s)<br>\n", buf );
1814             continue;
1815         }
1816
1817 #define CHECK_TYPE( x, s )                                                  \
1818         if ( sinfo.type & (CA_##x) )                                        \
1819             p += sprintf( p, "%s", s );
1820
1821         CHECK_TYPE( CI, "high level, " );
1822         CHECK_TYPE( CI_LINK, "link layer level, " );
1823         CHECK_TYPE( CI_PHYS, "physical layer level, " );
1824 #undef CHECK_TYPE
1825
1826         if ( sinfo.flags & CA_CI_MODULE_READY )
1827         {
1828             en50221_mmi_object_t *p_object = en50221_GetMMIObject( p_access,
1829                                                                        i_slot );
1830
1831             p += sprintf( p, "module present and ready<p>\n" );
1832             p += sprintf( p, "<form action=index.html method=get>\n" );
1833             p += sprintf( p, "<input type=hidden name=slot value=\"%d\">\n",
1834                           i_slot );
1835
1836             if ( p_object == NULL )
1837             {
1838                 p += sprintf( p, "<input type=submit name=open value=\"Open session\">\n" );
1839             }
1840             else
1841             {
1842                 switch ( p_object->i_object_type )
1843                 {
1844                 case EN50221_MMI_ENQ:
1845                     p += sprintf( p, "<input type=hidden name=type value=enq>\n" );
1846                     p += sprintf( p, "<table border=1><tr><th>%s</th></tr>\n",
1847                                   p_object->u.enq.psz_text );
1848                     if ( p_object->u.enq.b_blind == false )
1849                         p += sprintf( p, "<tr><td><input type=text name=answ></td></tr>\n" );
1850                     else
1851                         p += sprintf( p, "<tr><td><input type=password name=answ></td></tr>\n" );
1852                     break;
1853
1854                 case EN50221_MMI_MENU:
1855                     p += sprintf( p, "<input type=hidden name=type value=menu>\n" );
1856                     p += sprintf( p, "<table border=1><tr><th>%s</th></tr>\n",
1857                                   p_object->u.menu.psz_title );
1858                     p += sprintf( p, "<tr><td>%s</td></tr><tr><td>\n",
1859                                   p_object->u.menu.psz_subtitle );
1860                     for ( i = 0; i < p_object->u.menu.i_choices; i++ )
1861                         p += sprintf( p, "<input type=radio name=choice value=\"%d\">%s<br>\n", i + 1, p_object->u.menu.ppsz_choices[i] );
1862                     p += sprintf( p, "</td></tr><tr><td>%s</td></tr>\n",
1863                                   p_object->u.menu.psz_bottom );
1864                     break;
1865
1866                 case EN50221_MMI_LIST:
1867                     p += sprintf( p, "<input type=hidden name=type value=menu>\n" );
1868                     p += sprintf( p, "<input type=hidden name=choice value=0>\n" );
1869                     p += sprintf( p, "<table border=1><tr><th>%s</th></tr>\n",
1870                                   p_object->u.menu.psz_title );
1871                     p += sprintf( p, "<tr><td>%s</td></tr><tr><td>\n",
1872                                   p_object->u.menu.psz_subtitle );
1873                     for ( i = 0; i < p_object->u.menu.i_choices; i++ )
1874                         p += sprintf( p, "%s<br>\n",
1875                                       p_object->u.menu.ppsz_choices[i] );
1876                     p += sprintf( p, "</td></tr><tr><td>%s</td></tr>\n",
1877                                   p_object->u.menu.psz_bottom );
1878                     break;
1879
1880                 default:
1881                     p += sprintf( p, "<table><tr><th>Unknown MMI object type</th></tr>\n" );
1882                 }
1883
1884                 p += sprintf( p, "</table><p><input type=submit name=ok value=\"OK\">\n" );
1885                 p += sprintf( p, "<input type=submit name=cancel value=\"Cancel\">\n" );
1886                 p += sprintf( p, "<input type=submit name=close value=\"Close Session\">\n" );
1887             }
1888             p += sprintf( p, "</form>\n" );
1889         }
1890         else if ( sinfo.flags & CA_CI_MODULE_PRESENT )
1891             p += sprintf( p, "module present, not ready<br>\n" );
1892         else
1893             p += sprintf( p, "module not present<br>\n" );
1894     }
1895
1896 out:
1897     vlc_mutex_lock( &p_sys->httpd_mutex );
1898     p_sys->b_request_mmi_info = false;
1899     vlc_cond_signal( &p_sys->httpd_cond );
1900     vlc_mutex_unlock( &p_sys->httpd_mutex );
1901 }
1902 #endif
1903
1904 /*****************************************************************************
1905  * CAMSet :
1906  *****************************************************************************/
1907 int CAMSet( access_t * p_access, dvbpsi_pmt_t *p_pmt )
1908 {
1909     access_sys_t *p_sys = p_access->p_sys;
1910
1911     if( p_sys->i_ca_handle == 0 )
1912     {
1913         dvbpsi_DeletePMT( p_pmt );
1914         return VLC_EGENERIC;
1915     }
1916
1917     en50221_SetCAPMT( p_access, p_pmt );
1918
1919     return VLC_SUCCESS;
1920 }
1921
1922 /*****************************************************************************
1923  * CAMClose :
1924  *****************************************************************************/
1925 void CAMClose( access_t * p_access )
1926 {
1927     access_sys_t *p_sys = p_access->p_sys;
1928
1929     en50221_End( p_access );
1930
1931     if ( p_sys->i_ca_handle )
1932     {
1933         close( p_sys->i_ca_handle );
1934     }
1935 }
1936