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