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