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