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