]> git.sesse.net Git - vlc/blob - modules/access/dvb/linux_dvb.c
f6ed3afe8aa94c463376532c9c5383be4ddbfa43
[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 8:     return VSB_8;
798         case 16:    return VSB_16;
799         default:
800             msg_Dbg( p_access, "ATSC modulation not set, using VSB 8");
801             return VSB_8;
802     }
803 }
804
805 /*****************************************************************************
806  * FrontendSetQPSK : controls the FE device
807  *****************************************************************************/
808 static fe_sec_voltage_t DecodeVoltage( access_t *p_access )
809 {
810     vlc_value_t         val;
811     fe_sec_voltage_t    fe_voltage;
812
813     var_Get( p_access, "dvb-voltage", &val );
814     msg_Dbg( p_access, "using voltage=%d", val.i_int );
815
816     switch( val.i_int )
817     {
818         case 0: fe_voltage = SEC_VOLTAGE_OFF; break;
819         case 13: fe_voltage = SEC_VOLTAGE_13; break;
820         case 18: fe_voltage = SEC_VOLTAGE_18; break;
821         default:
822             fe_voltage = SEC_VOLTAGE_OFF;
823             msg_Err( p_access, "argument has invalid voltage (%d)", val.i_int );
824             break;
825     }
826     return fe_voltage;
827 }
828
829 static fe_sec_tone_mode_t DecodeTone( access_t *p_access )
830 {
831     vlc_value_t         val;
832     fe_sec_tone_mode_t  fe_tone;
833
834     var_Get( p_access, "dvb-tone", &val );
835     msg_Dbg( p_access, "using tone=%d", val.i_int );
836
837     switch( val.i_int )
838     {
839         case 0: fe_tone = SEC_TONE_OFF; break;
840         case 1: fe_tone = SEC_TONE_ON; break;
841         default:
842             fe_tone = SEC_TONE_OFF;
843             msg_Err( p_access, "argument has invalid tone mode (%d)", val.i_int);
844             break;
845     }
846     return fe_tone;
847 }
848
849 struct diseqc_cmd_t
850 {
851     struct dvb_diseqc_master_cmd cmd;
852     uint32_t wait;
853 };
854
855 static int DoDiseqc( access_t *p_access )
856 {
857     access_sys_t *p_sys = p_access->p_sys;
858     vlc_value_t val;
859     int i_frequency, i_lnb_slof;
860     fe_sec_voltage_t fe_voltage;
861     fe_sec_tone_mode_t fe_tone;
862     int i_err;
863
864     var_Get( p_access, "dvb-frequency", &val );
865     i_frequency = val.i_int;
866     var_Get( p_access, "dvb-lnb-slof", &val );
867     i_lnb_slof = val.i_int;
868
869     var_Get( p_access, "dvb-tone", &val );
870     if( val.i_int == -1 /* auto */ )
871     {
872         if( i_frequency >= i_lnb_slof )
873             val.i_int = 1;
874         else
875             val.i_int = 0;
876         var_Set( p_access, "dvb-tone", val );
877     }
878
879     fe_voltage = DecodeVoltage( p_access );
880     fe_tone = DecodeTone( p_access );
881
882     /* Switch off continuous tone. */
883     if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_TONE, SEC_TONE_OFF )) < 0 )
884     {
885         msg_Err( p_access, "ioctl FE_SET_TONE failed, tone=%s (%d) %m",
886                  fe_tone == SEC_TONE_ON ? "on" : "off", i_err );
887         return i_err;
888     }
889
890     /* Configure LNB voltage. */
891     if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_VOLTAGE, fe_voltage )) < 0 )
892     {
893         msg_Err( p_access, "ioctl FE_SET_VOLTAGE failed, voltage=%d (%d) %m",
894                  fe_voltage, i_err );
895         return i_err;
896     }
897
898     var_Get( p_access, "dvb-high-voltage", &val );
899     if( (i_err = ioctl( p_sys->i_frontend_handle, FE_ENABLE_HIGH_LNB_VOLTAGE,
900                         val.b_bool )) < 0 && val.b_bool )
901     {
902         msg_Err( p_access,
903                  "ioctl FE_ENABLE_HIGH_LNB_VOLTAGE failed, val=%d (%d) %m",
904                  val.b_bool, i_err );
905     }
906
907     /* Wait for at least 15 ms. */
908     msleep(15000);
909
910     var_Get( p_access, "dvb-satno", &val );
911     if( val.i_int > 0 && val.i_int < 5 )
912     {
913         /* digital satellite equipment control,
914          * specification is available from http://www.eutelsat.com/
915          */
916
917         /* 1.x compatible equipment */
918         struct diseqc_cmd_t cmd =  { {{0xe0, 0x10, 0x38, 0xf0, 0x00, 0x00}, 4}, 0 };
919
920         /* param: high nibble: reset bits, low nibble set bits,
921          * bits are: option, position, polarization, band
922          */
923         cmd.cmd.msg[3] = 0xf0 /* reset bits */
924                           | (((val.i_int - 1) * 4) & 0xc)
925                           | (fe_voltage == SEC_VOLTAGE_13 ? 0 : 2)
926                           | (fe_tone == SEC_TONE_ON ? 1 : 0);
927
928         if( (i_err = ioctl( p_sys->i_frontend_handle, FE_DISEQC_SEND_MASTER_CMD,
929                            &cmd.cmd )) < 0 )
930         {
931             msg_Err( p_access, "ioctl FE_SEND_MASTER_CMD failed (%d) %m",
932                      i_err );
933             return i_err;
934         }
935
936         msleep(15000 + cmd.wait * 1000);
937
938         /* A or B simple diseqc ("diseqc-compatible") */
939         if( (i_err = ioctl( p_sys->i_frontend_handle, FE_DISEQC_SEND_BURST,
940                       ((val.i_int - 1) % 2) ? SEC_MINI_B : SEC_MINI_A )) < 0 )
941         {
942             msg_Err( p_access, "ioctl FE_SEND_BURST failed (%d) %m",
943                      i_err );
944             return i_err;
945         }
946
947         msleep(15000);
948     }
949
950     if( (i_err = ioctl( p_sys->i_frontend_handle, FE_SET_TONE, fe_tone )) < 0 )
951     {
952         msg_Err( p_access, "ioctl FE_SET_TONE failed, tone=%s (%d) %m",
953                  fe_tone == SEC_TONE_ON ? "on" : "off", i_err );
954         return i_err;
955     }
956
957     msleep(50000);
958     return 0;
959 }
960
961 static int FrontendSetQPSK( access_t *p_access )
962 {
963     access_sys_t *p_sys = p_access->p_sys;
964     struct dvb_frontend_parameters fep;
965     int i_ret;
966     vlc_value_t val;
967     int i_frequency, i_lnb_slof = 0, i_lnb_lof1, i_lnb_lof2 = 0;
968
969     /* Prepare the fep structure */
970     var_Get( p_access, "dvb-frequency", &val );
971     i_frequency = val.i_int;
972
973     var_Get( p_access, "dvb-lnb-lof1", &val );
974     if ( val.i_int == 0 )
975     {
976         /* Automatic mode. */
977         if ( i_frequency >= 950000 && i_frequency <= 2150000 )
978         {
979             msg_Dbg( p_access, "frequency %d is in IF-band", i_frequency );
980             i_lnb_lof1 = 0;
981         }
982         else if ( i_frequency >= 2500000 && i_frequency <= 2700000 )
983         {
984             msg_Dbg( p_access, "frequency %d is in S-band", i_frequency );
985             i_lnb_lof1 = 3650000;
986         }
987         else if ( i_frequency >= 3400000 && i_frequency <= 4200000 )
988         {
989             msg_Dbg( p_access, "frequency %d is in C-band (lower)",
990                      i_frequency );
991             i_lnb_lof1 = 5150000;
992         }
993         else if ( i_frequency >= 4500000 && i_frequency <= 4800000 )
994         {
995             msg_Dbg( p_access, "frequency %d is in C-band (higher)",
996                      i_frequency );
997             i_lnb_lof1 = 5950000;
998         }
999         else if ( i_frequency >= 10700000 && i_frequency <= 13250000 )
1000         {
1001             msg_Dbg( p_access, "frequency %d is in Ku-band",
1002                      i_frequency );
1003             i_lnb_lof1 = 9750000;
1004             i_lnb_lof2 = 10600000;
1005             i_lnb_slof = 11700000;
1006         }
1007         else
1008         {
1009             msg_Err( p_access, "frequency %d is out of any known band",
1010                      i_frequency );
1011             msg_Err( p_access, "specify dvb-lnb-lof1 manually for the local "
1012                      "oscillator frequency" );
1013             return VLC_EGENERIC;
1014         }
1015         val.i_int = i_lnb_lof1;
1016         var_Set( p_access, "dvb-lnb-lof1", val );
1017         val.i_int = i_lnb_lof2;
1018         var_Set( p_access, "dvb-lnb-lof2", val );
1019         val.i_int = i_lnb_slof;
1020         var_Set( p_access, "dvb-lnb-slof", val );
1021     }
1022     else
1023     {
1024         i_lnb_lof1 = val.i_int;
1025         var_Get( p_access, "dvb-lnb-lof2", &val );
1026         i_lnb_lof2 = val.i_int;
1027         var_Get( p_access, "dvb-lnb-slof", &val );
1028         i_lnb_slof = val.i_int;
1029     }
1030
1031     if( i_lnb_slof && i_frequency >= i_lnb_slof )
1032     {
1033         i_frequency -= i_lnb_lof2;
1034     }
1035     else
1036     {
1037         i_frequency -= i_lnb_lof1;
1038     }
1039     fep.frequency = i_frequency >= 0 ? i_frequency : -i_frequency;
1040
1041     fep.inversion = DecodeInversion( p_access );
1042
1043     var_Get( p_access, "dvb-srate", &val );
1044     fep.u.qpsk.symbol_rate = val.i_int;
1045
1046     var_Get( p_access, "dvb-fec", &val );
1047     fep.u.qpsk.fec_inner = DecodeFEC( p_access, val.i_int );
1048
1049     if( DoDiseqc( p_access ) < 0 )
1050     {
1051         return VLC_EGENERIC;
1052     }
1053
1054     /* Empty the event queue */
1055     for( ; ; )
1056     {
1057         struct dvb_frontend_event event;
1058         if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
1059               && errno == EWOULDBLOCK )
1060             break;
1061     }
1062
1063     /* Now send it all to the frontend device */
1064     if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
1065     {
1066         msg_Err( p_access, "DVB-S: setting frontend failed (%d) %m", i_ret );
1067         return VLC_EGENERIC;
1068     }
1069
1070     return VLC_SUCCESS;
1071 }
1072
1073 /*****************************************************************************
1074  * FrontendSetQAM : controls the FE device
1075  *****************************************************************************/
1076 static int FrontendSetQAM( access_t *p_access )
1077 {
1078     access_sys_t *p_sys = p_access->p_sys;
1079     struct dvb_frontend_parameters fep;
1080     vlc_value_t val;
1081     int i_ret;
1082
1083     /* Prepare the fep structure */
1084
1085     var_Get( p_access, "dvb-frequency", &val );
1086     fep.frequency = val.i_int;
1087
1088     fep.inversion = DecodeInversion( p_access );
1089
1090     var_Get( p_access, "dvb-srate", &val );
1091     fep.u.qam.symbol_rate = val.i_int;
1092
1093     var_Get( p_access, "dvb-fec", &val );
1094     fep.u.qam.fec_inner = DecodeFEC( p_access, val.i_int );
1095
1096     fep.u.qam.modulation = DecodeModulationQAM( p_access );
1097
1098     /* Empty the event queue */
1099     for( ; ; )
1100     {
1101         struct dvb_frontend_event event;
1102         if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
1103               && errno == EWOULDBLOCK )
1104             break;
1105     }
1106
1107     /* Now send it all to the frontend device */
1108     if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
1109     {
1110         msg_Err( p_access, "DVB-C: setting frontend failed (%d): %m", i_ret );
1111         return VLC_EGENERIC;
1112     }
1113
1114     return VLC_SUCCESS;
1115 }
1116
1117 /*****************************************************************************
1118  * FrontendSetOFDM : controls the FE device
1119  *****************************************************************************/
1120 static fe_bandwidth_t DecodeBandwidth( access_t *p_access )
1121 {
1122     vlc_value_t         val;
1123     fe_bandwidth_t      fe_bandwidth = 0;
1124
1125     var_Get( p_access, "dvb-bandwidth", &val );
1126     msg_Dbg( p_access, "using bandwidth=%d", val.i_int );
1127
1128     switch( val.i_int )
1129     {
1130         case 0: fe_bandwidth = BANDWIDTH_AUTO; break;
1131         case 6: fe_bandwidth = BANDWIDTH_6_MHZ; break;
1132         case 7: fe_bandwidth = BANDWIDTH_7_MHZ; break;
1133         case 8: fe_bandwidth = BANDWIDTH_8_MHZ; break;
1134         default:
1135             msg_Dbg( p_access, "terrestrial dvb has bandwidth not set, using auto" );
1136             fe_bandwidth = BANDWIDTH_AUTO;
1137             break;
1138     }
1139     return fe_bandwidth;
1140 }
1141
1142 static fe_transmit_mode_t DecodeTransmission( access_t *p_access )
1143 {
1144     vlc_value_t         val;
1145     fe_transmit_mode_t  fe_transmission = 0;
1146
1147     var_Get( p_access, "dvb-transmission", &val );
1148     msg_Dbg( p_access, "using transmission=%d", val.i_int );
1149
1150     switch( val.i_int )
1151     {
1152         case 0: fe_transmission = TRANSMISSION_MODE_AUTO; break;
1153         case 2: fe_transmission = TRANSMISSION_MODE_2K; break;
1154         case 8: fe_transmission = TRANSMISSION_MODE_8K; break;
1155         default:
1156             msg_Dbg( p_access, "terrestrial dvb has transmission mode not set, using auto");
1157             fe_transmission = TRANSMISSION_MODE_AUTO;
1158             break;
1159     }
1160     return fe_transmission;
1161 }
1162
1163 static fe_guard_interval_t DecodeGuardInterval( access_t *p_access )
1164 {
1165     vlc_value_t         val;
1166     fe_guard_interval_t fe_guard = 0;
1167
1168     var_Get( p_access, "dvb-guard", &val );
1169     msg_Dbg( p_access, "using guard=%d", val.i_int );
1170
1171     switch( val.i_int )
1172     {
1173         case 0: fe_guard = GUARD_INTERVAL_AUTO; break;
1174         case 4: fe_guard = GUARD_INTERVAL_1_4; break;
1175         case 8: fe_guard = GUARD_INTERVAL_1_8; break;
1176         case 16: fe_guard = GUARD_INTERVAL_1_16; break;
1177         case 32: fe_guard = GUARD_INTERVAL_1_32; break;
1178         default:
1179             msg_Dbg( p_access, "terrestrial dvb has guard interval not set, using auto");
1180             fe_guard = GUARD_INTERVAL_AUTO;
1181             break;
1182     }
1183     return fe_guard;
1184 }
1185
1186 static fe_hierarchy_t DecodeHierarchy( access_t *p_access )
1187 {
1188     vlc_value_t         val;
1189     fe_hierarchy_t      fe_hierarchy = 0;
1190
1191     var_Get( p_access, "dvb-hierarchy", &val );
1192     msg_Dbg( p_access, "using hierarchy=%d", val.i_int );
1193
1194     switch( val.i_int )
1195     {
1196         case -1: fe_hierarchy = HIERARCHY_NONE; break;
1197         case 0: fe_hierarchy = HIERARCHY_AUTO; break;
1198         case 1: fe_hierarchy = HIERARCHY_1; break;
1199         case 2: fe_hierarchy = HIERARCHY_2; break;
1200         case 4: fe_hierarchy = HIERARCHY_4; break;
1201         default:
1202             msg_Dbg( p_access, "terrestrial dvb has hierarchy not set, using auto");
1203             fe_hierarchy = HIERARCHY_AUTO;
1204             break;
1205     }
1206     return fe_hierarchy;
1207 }
1208
1209 static int FrontendSetOFDM( access_t * p_access )
1210 {
1211     access_sys_t *p_sys = p_access->p_sys;
1212     struct dvb_frontend_parameters fep;
1213     vlc_value_t val;
1214     int ret;
1215
1216     /* Prepare the fep structure */
1217
1218     var_Get( p_access, "dvb-frequency", &val );
1219     fep.frequency = val.i_int;
1220
1221     fep.inversion = DecodeInversion( p_access );
1222
1223     fep.u.ofdm.bandwidth = DecodeBandwidth( p_access );
1224     var_Get( p_access, "dvb-code-rate-hp", &val );
1225     fep.u.ofdm.code_rate_HP = DecodeFEC( p_access, val.i_int );
1226     var_Get( p_access, "dvb-code-rate-lp", &val );
1227     fep.u.ofdm.code_rate_LP = DecodeFEC( p_access, val.i_int );
1228     fep.u.ofdm.constellation = DecodeModulationOFDM( p_access );
1229     fep.u.ofdm.transmission_mode = DecodeTransmission( p_access );
1230     fep.u.ofdm.guard_interval = DecodeGuardInterval( p_access );
1231     fep.u.ofdm.hierarchy_information = DecodeHierarchy( p_access );
1232
1233     /* Empty the event queue */
1234     for( ; ; )
1235     {
1236         struct dvb_frontend_event event;
1237         if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
1238               && errno == EWOULDBLOCK )
1239             break;
1240     }
1241
1242     /* Now send it all to the frontend device */
1243     if( (ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
1244     {
1245         msg_Err( p_access, "DVB-T: setting frontend failed (%d): %m", ret );
1246         return -1;
1247     }
1248
1249     return VLC_SUCCESS;
1250 }
1251
1252 /*****************************************************************************
1253  * FrontendSetATSC : controls the FE device
1254  *****************************************************************************/
1255 static int FrontendSetATSC( access_t *p_access )
1256 {
1257     access_sys_t *p_sys = p_access->p_sys;
1258     struct dvb_frontend_parameters fep;
1259     vlc_value_t val;
1260     int i_ret;
1261
1262     /* Prepare the fep structure */
1263
1264     var_Get( p_access, "dvb-frequency", &val );
1265     fep.frequency = val.i_int;
1266
1267     fep.u.vsb.modulation = DecodeModulationATSC( p_access );
1268
1269     /* Empty the event queue */
1270     for( ; ; )
1271     {
1272         struct dvb_frontend_event event;
1273         if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
1274               && errno == EWOULDBLOCK )
1275             break;
1276     }
1277
1278     /* Now send it all to the frontend device */
1279     if( (i_ret = ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep )) < 0 )
1280     {
1281         msg_Err( p_access, "ATSC: setting frontend failed (%d): %m", i_ret );
1282         return VLC_EGENERIC;
1283     }
1284
1285     return VLC_SUCCESS;
1286 }
1287
1288
1289 /*
1290  * Demux
1291  */
1292
1293 /*****************************************************************************
1294  * DMXSetFilter : controls the demux to add a filter
1295  *****************************************************************************/
1296 int DMXSetFilter( access_t * p_access, int i_pid, int * pi_fd, int i_type )
1297 {
1298     struct dmx_pes_filter_params s_filter_params;
1299     int i_ret;
1300     unsigned int i_adapter, i_device;
1301     char dmx[128];
1302     vlc_value_t val;
1303
1304     var_Get( p_access, "dvb-adapter", &val );
1305     i_adapter = val.i_int;
1306     var_Get( p_access, "dvb-device", &val );
1307     i_device = val.i_int;
1308
1309     if( snprintf( dmx, sizeof(dmx), DMX, i_adapter, i_device )
1310             >= (int)sizeof(dmx) )
1311     {
1312         msg_Err( p_access, "snprintf() truncated string for DMX" );
1313         dmx[sizeof(dmx) - 1] = '\0';
1314     }
1315
1316     msg_Dbg( p_access, "Opening device %s", dmx );
1317     if( (*pi_fd = open(dmx, O_RDWR)) < 0 )
1318     {
1319         msg_Err( p_access, "DMXSetFilter: opening device failed (%m)" );
1320         return VLC_EGENERIC;
1321     }
1322
1323     /* We fill the DEMUX structure : */
1324     s_filter_params.pid     =   i_pid;
1325     s_filter_params.input   =   DMX_IN_FRONTEND;
1326     s_filter_params.output  =   DMX_OUT_TS_TAP;
1327     s_filter_params.flags   =   DMX_IMMEDIATE_START;
1328
1329     switch ( i_type )
1330     {   /* First device */
1331         case 1:
1332             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO0 for PID %d", i_pid);
1333             s_filter_params.pes_type = DMX_PES_VIDEO0;
1334             break;
1335         case 2:
1336             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO0 for PID %d", i_pid);
1337             s_filter_params.pes_type = DMX_PES_AUDIO0;
1338             break;
1339         case 3:
1340             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT0 for PID %d", i_pid);
1341             s_filter_params.pes_type = DMX_PES_TELETEXT0;
1342             break;
1343         case 4:
1344             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE0 for PID %d", i_pid);
1345             s_filter_params.pes_type = DMX_PES_SUBTITLE0;
1346             break;
1347         case 5:
1348             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR0 for PID %d", i_pid);
1349             s_filter_params.pes_type = DMX_PES_PCR0;
1350             break;
1351         /* Second device */
1352         case 6:
1353             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO1 for PID %d", i_pid);
1354             s_filter_params.pes_type = DMX_PES_VIDEO1;
1355             break;
1356         case 7:
1357             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO1 for PID %d", i_pid);
1358             s_filter_params.pes_type = DMX_PES_AUDIO1;
1359             break;
1360         case 8:
1361             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT1 for PID %d", i_pid);
1362             s_filter_params.pes_type = DMX_PES_TELETEXT1;
1363             break;
1364         case 9:
1365             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE1 for PID %d", i_pid);
1366             s_filter_params.pes_type = DMX_PES_SUBTITLE1;
1367             break;
1368         case 10:
1369             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR1 for PID %d", i_pid);
1370             s_filter_params.pes_type = DMX_PES_PCR1;
1371             break;
1372         /* Third device */
1373         case 11:
1374             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO2 for PID %d", i_pid);
1375             s_filter_params.pes_type = DMX_PES_VIDEO2;
1376             break;
1377         case 12:
1378             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO2 for PID %d", i_pid);
1379             s_filter_params.pes_type = DMX_PES_AUDIO2;
1380             break;
1381         case 13:
1382             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT2 for PID %d", i_pid);
1383             s_filter_params.pes_type = DMX_PES_TELETEXT2;
1384             break;
1385         case 14:
1386             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE2 for PID %d", i_pid);
1387             s_filter_params.pes_type = DMX_PES_SUBTITLE2;
1388             break;
1389         case 15:
1390             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR2 for PID %d", i_pid);
1391             s_filter_params.pes_type = DMX_PES_PCR2;
1392             break;
1393         /* Forth device */
1394         case 16:
1395             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO3 for PID %d", i_pid);
1396             s_filter_params.pes_type = DMX_PES_VIDEO3;
1397             break;
1398         case 17:
1399             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO3 for PID %d", i_pid);
1400             s_filter_params.pes_type = DMX_PES_AUDIO3;
1401             break;
1402         case 18:
1403             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT3 for PID %d", i_pid);
1404             s_filter_params.pes_type = DMX_PES_TELETEXT3;
1405             break;
1406         case 19:
1407             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE3 for PID %d", i_pid);
1408             s_filter_params.pes_type = DMX_PES_SUBTITLE3;
1409             break;
1410         case 20:
1411             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR3 for PID %d", i_pid);
1412             s_filter_params.pes_type = DMX_PES_PCR3;
1413             break;
1414         /* Usually used by Nova cards */
1415         case 21:
1416         default:
1417             msg_Dbg(p_access, "DMXSetFilter: DMX_PES_OTHER for PID %d", i_pid);
1418             s_filter_params.pes_type = DMX_PES_OTHER;
1419             break;
1420     }
1421
1422     /* We then give the order to the device : */
1423     if( (i_ret = ioctl( *pi_fd, DMX_SET_PES_FILTER, &s_filter_params )) < 0 )
1424     {
1425         msg_Err( p_access, "DMXSetFilter: failed with %d (%m)", i_ret );
1426         return VLC_EGENERIC;
1427     }
1428     return VLC_SUCCESS;
1429 }
1430
1431 /*****************************************************************************
1432  * DMXUnsetFilter : removes a filter
1433  *****************************************************************************/
1434 int DMXUnsetFilter( access_t * p_access, int i_fd )
1435 {
1436     int i_ret;
1437
1438     if( (i_ret = ioctl( i_fd, DMX_STOP )) < 0 )
1439     {
1440         msg_Err( p_access, "DMX_STOP failed for demux (%d): %m", i_ret );
1441         return i_ret;
1442     }
1443
1444     msg_Dbg( p_access, "DMXUnsetFilter: closing demux %d", i_fd );
1445     close( i_fd );
1446     return VLC_SUCCESS;
1447 }
1448
1449
1450 /*
1451  * DVR device
1452  */
1453
1454 /*****************************************************************************
1455  * DVROpen :
1456  *****************************************************************************/
1457 int DVROpen( access_t * p_access )
1458 {
1459     access_sys_t *p_sys = p_access->p_sys;
1460     unsigned int i_adapter, i_device;
1461     char dvr[128];
1462     vlc_value_t val;
1463
1464     var_Get( p_access, "dvb-adapter", &val );
1465     i_adapter = val.i_int;
1466     var_Get( p_access, "dvb-device", &val );
1467     i_device = val.i_int;
1468
1469     if( snprintf( dvr, sizeof(dvr), DVR, i_adapter, i_device )
1470             >= (int)sizeof(dvr) )
1471     {
1472         msg_Err( p_access, "snprintf() truncated string for DVR" );
1473         dvr[sizeof(dvr) - 1] = '\0';
1474     }
1475
1476     msg_Dbg( p_access, "Opening device %s", dvr );
1477     if( (p_sys->i_handle = open(dvr, O_RDONLY)) < 0 )
1478     {
1479         msg_Err( p_access, "DVROpen: opening device failed (%m)" );
1480         return VLC_EGENERIC;
1481     }
1482
1483     if( fcntl( p_sys->i_handle, F_SETFL, O_NONBLOCK ) == -1 )
1484     {
1485         msg_Warn( p_access, "DVROpen: couldn't set non-blocking mode (%m)" );
1486     }
1487
1488     return VLC_SUCCESS;
1489 }
1490
1491 /*****************************************************************************
1492  * DVRClose :
1493  *****************************************************************************/
1494 void DVRClose( access_t * p_access )
1495 {
1496     access_sys_t *p_sys = p_access->p_sys;
1497
1498     close( p_sys->i_handle );
1499 }
1500
1501
1502 /*
1503  * CAM device
1504  */
1505
1506 /*****************************************************************************
1507  * CAMOpen :
1508  *****************************************************************************/
1509 int CAMOpen( access_t *p_access )
1510 {
1511     access_sys_t *p_sys = p_access->p_sys;
1512     char ca[128];
1513     int i_adapter, i_device;
1514     ca_caps_t caps;
1515
1516     i_adapter = var_GetInteger( p_access, "dvb-adapter" );
1517     i_device = var_GetInteger( p_access, "dvb-device" );
1518
1519     if( snprintf( ca, sizeof(ca), CA, i_adapter, i_device ) >= (int)sizeof(ca) )
1520     {
1521         msg_Err( p_access, "snprintf() truncated string for CA" );
1522         ca[sizeof(ca) - 1] = '\0';
1523     }
1524     memset( &caps, 0, sizeof( ca_caps_t ));
1525
1526     msg_Dbg( p_access, "Opening device %s", ca );
1527     if( (p_sys->i_ca_handle = open(ca, O_RDWR | O_NONBLOCK)) < 0 )
1528     {
1529         msg_Warn( p_access, "CAMInit: opening CAM device failed (%m)" );
1530         p_sys->i_ca_handle = 0;
1531         return VLC_EGENERIC;
1532     }
1533
1534     if ( ioctl( p_sys->i_ca_handle, CA_GET_CAP, &caps ) != 0 )
1535     {
1536         msg_Err( p_access, "CAMInit: ioctl() error getting CAM capabilities" );
1537         close( p_sys->i_ca_handle );
1538         p_sys->i_ca_handle = 0;
1539         return VLC_EGENERIC;
1540     }
1541
1542     /* Output CA capabilities */
1543     msg_Dbg( p_access, "CAMInit: CA interface with %d %s", caps.slot_num,
1544         caps.slot_num == 1 ? "slot" : "slots" );
1545     if ( caps.slot_type & CA_CI )
1546         msg_Dbg( p_access, "CAMInit: CI high level interface type" );
1547     if ( caps.slot_type & CA_CI_LINK )
1548         msg_Dbg( p_access, "CAMInit: CI link layer level interface type" );
1549     if ( caps.slot_type & CA_CI_PHYS )
1550         msg_Dbg( p_access, "CAMInit: CI physical layer level interface type (not supported) " );
1551     if ( caps.slot_type & CA_DESCR )
1552         msg_Dbg( p_access, "CAMInit: built-in descrambler detected" );
1553     if ( caps.slot_type & CA_SC )
1554         msg_Dbg( p_access, "CAMInit: simple smart card interface" );
1555
1556     msg_Dbg( p_access, "CAMInit: %d available %s", caps.descr_num,
1557         caps.descr_num == 1 ? "descrambler (key)" : "descramblers (keys)" );
1558     if ( caps.descr_type & CA_ECD )
1559         msg_Dbg( p_access, "CAMInit: ECD scrambling system supported" );
1560     if ( caps.descr_type & CA_NDS )
1561         msg_Dbg( p_access, "CAMInit: NDS scrambling system supported" );
1562     if ( caps.descr_type & CA_DSS )
1563         msg_Dbg( p_access, "CAMInit: DSS scrambling system supported" );
1564  
1565     if ( caps.slot_num == 0 )
1566     {
1567         msg_Err( p_access, "CAMInit: CAM module with no slots" );
1568         close( p_sys->i_ca_handle );
1569         p_sys->i_ca_handle = 0;
1570         return VLC_EGENERIC;
1571     }
1572
1573     if( caps.slot_type & CA_CI_LINK )
1574     {
1575         p_sys->i_ca_type = CA_CI_LINK;
1576     }
1577     else if( caps.slot_type & CA_CI )
1578     {
1579         p_sys->i_ca_type = CA_CI;
1580     }
1581     else
1582     {
1583         p_sys->i_ca_type = -1;
1584         msg_Err( p_access, "CAMInit: incompatible CAM interface" );
1585         close( p_sys->i_ca_handle );
1586         p_sys->i_ca_handle = 0;
1587         return VLC_EGENERIC;
1588     }
1589
1590     p_sys->i_nb_slots = caps.slot_num;
1591     memset( p_sys->pb_active_slot, 0, sizeof(bool) * MAX_CI_SLOTS );
1592     memset( p_sys->pb_slot_mmi_expected, 0, sizeof(bool) * MAX_CI_SLOTS );
1593     memset( p_sys->pb_slot_mmi_undisplayed, 0,
1594             sizeof(bool) * MAX_CI_SLOTS );
1595
1596     return en50221_Init( p_access );
1597 }
1598
1599 /*****************************************************************************
1600  * CAMPoll :
1601  *****************************************************************************/
1602 int CAMPoll( access_t * p_access )
1603 {
1604     access_sys_t *p_sys = p_access->p_sys;
1605     int i_ret = VLC_EGENERIC;
1606
1607     if ( p_sys->i_ca_handle == 0 )
1608     {
1609         return VLC_EGENERIC;
1610     }
1611
1612     switch( p_sys->i_ca_type )
1613     {
1614     case CA_CI_LINK:
1615         i_ret = en50221_Poll( p_access );
1616         break;
1617     case CA_CI:
1618         i_ret = VLC_SUCCESS;
1619         break;
1620     default:
1621         msg_Err( p_access, "CAMPoll: This should not happen" );
1622         break;
1623     }
1624
1625     return i_ret;
1626 }
1627
1628 #ifdef ENABLE_HTTPD
1629 /*****************************************************************************
1630  * CAMStatus :
1631  *****************************************************************************/
1632 void CAMStatus( access_t * p_access )
1633 {
1634     access_sys_t *p_sys = p_access->p_sys;
1635     char *p;
1636     ca_caps_t caps;
1637     int i_slot, i;
1638
1639     if ( p_sys->psz_request != NULL && *p_sys->psz_request )
1640     {
1641         /* Check if we have an undisplayed MMI message : in that case we ignore
1642          * the user input to avoid confusing the CAM. */
1643         for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
1644         {
1645             if ( p_sys->pb_slot_mmi_undisplayed[i_slot] == true )
1646             {
1647                 p_sys->psz_request = NULL;
1648                 msg_Dbg( p_access,
1649                          "ignoring user request because of a new MMI object" );
1650                 break;
1651             }
1652         }
1653     }
1654
1655     if ( p_sys->psz_request != NULL && *p_sys->psz_request )
1656     {
1657         /* We have a mission to accomplish. */
1658         en50221_mmi_object_t mmi_object;
1659         char *psz_request = p_sys->psz_request;
1660         char psz_value[255];
1661         int i_slot;
1662         bool b_ok = false;
1663
1664         p_sys->psz_request = NULL;
1665
1666         if ( HTTPExtractValue( psz_request, "slot", psz_value,
1667                                    sizeof(psz_value) ) == NULL )
1668         {
1669             p_sys->psz_mmi_info = strdup( "invalid request parameter\n" );
1670             goto out;
1671         }
1672         i_slot = atoi(psz_value);
1673
1674         if ( HTTPExtractValue( psz_request, "open", psz_value,
1675                                    sizeof(psz_value) ) != NULL )
1676         {
1677             en50221_OpenMMI( p_access, i_slot );
1678             return;
1679         }
1680
1681         if ( HTTPExtractValue( psz_request, "close", psz_value,
1682                                    sizeof(psz_value) ) != NULL )
1683         {
1684             en50221_CloseMMI( p_access, i_slot );
1685             return;
1686         }
1687
1688         if ( HTTPExtractValue( psz_request, "cancel", psz_value,
1689                                    sizeof(psz_value) ) == NULL )
1690         {
1691             b_ok = true;
1692         }
1693
1694         if ( HTTPExtractValue( psz_request, "type", psz_value,
1695                                    sizeof(psz_value) ) == NULL )
1696         {
1697             p_sys->psz_mmi_info = strdup( "invalid request parameter\n" );
1698             goto out;
1699         }
1700
1701         if ( !strcmp( psz_value, "enq" ) )
1702         {
1703             mmi_object.i_object_type = EN50221_MMI_ANSW;
1704             mmi_object.u.answ.b_ok = b_ok;
1705             if ( b_ok == false )
1706             {
1707                 mmi_object.u.answ.psz_answ = strdup("");
1708             }
1709             else
1710             {
1711                 if ( HTTPExtractValue( psz_request, "answ", psz_value,
1712                                            sizeof(psz_value) ) == NULL )
1713                 {
1714                     p_sys->psz_mmi_info = strdup( "invalid request parameter\n" );
1715                     goto out;
1716                 }
1717
1718                 mmi_object.u.answ.psz_answ = strdup(psz_value);
1719             }
1720         }
1721         else
1722         {
1723             mmi_object.i_object_type = EN50221_MMI_MENU_ANSW;
1724             if ( b_ok == false )
1725             {
1726                 mmi_object.u.menu_answ.i_choice = 0;
1727             }
1728             else
1729             {
1730                 if ( HTTPExtractValue( psz_request, "choice", psz_value,
1731                                            sizeof(psz_value) ) == NULL )
1732                     mmi_object.u.menu_answ.i_choice = 0;
1733                 else
1734                     mmi_object.u.menu_answ.i_choice = atoi(psz_value);
1735             }
1736         }
1737
1738         en50221_SendMMIObject( p_access, i_slot, &mmi_object );
1739         return;
1740     }
1741
1742     /* Check that we have all necessary MMI information. */
1743     for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
1744     {
1745         if ( p_sys->pb_slot_mmi_expected[i_slot] == true )
1746             return;
1747     }
1748
1749     p = p_sys->psz_mmi_info = malloc( 10000 );
1750
1751     if ( ioctl( p_sys->i_ca_handle, CA_GET_CAP, &caps ) != 0 )
1752     {
1753         char buf[1000];
1754         strerror_r( errno, buf, sizeof( buf ) );
1755         p += sprintf( p, "ioctl CA_GET_CAP failed (%s)\n", buf );
1756         goto out;
1757     }
1758
1759     /* Output CA capabilities */
1760     p += sprintf( p, "CA interface with %d %s, type:\n<table>", caps.slot_num,
1761                   caps.slot_num == 1 ? "slot" : "slots" );
1762 #define CHECK_CAPS( x, s )                                                  \
1763     if ( caps.slot_type & (CA_##x) )                                        \
1764         p += sprintf( p, "<tr><td>" s "</td></tr>\n" );
1765
1766     CHECK_CAPS( CI, "CI high level interface" );
1767     CHECK_CAPS( CI_LINK, "CI link layer level interface" );
1768     CHECK_CAPS( CI_PHYS, "CI physical layer level interface (not supported)" );
1769     CHECK_CAPS( DESCR, "built-in descrambler" );
1770     CHECK_CAPS( SC, "simple smartcard interface" );
1771 #undef CHECK_CAPS
1772
1773     p += sprintf( p, "</table>%d available %s\n<table>", caps.descr_num,
1774         caps.descr_num == 1 ? "descrambler (key)" : "descramblers (keys)" );
1775 #define CHECK_DESC( x )                                                     \
1776     if ( caps.descr_type & (CA_##x) )                                       \
1777         p += sprintf( p, "<tr><td>" STRINGIFY(x) "</td></tr>\n" );
1778
1779     CHECK_DESC( ECD );
1780     CHECK_DESC( NDS );
1781     CHECK_DESC( DSS );
1782 #undef CHECK_DESC
1783
1784     p += sprintf( p, "</table>" );
1785
1786     for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
1787     {
1788         ca_slot_info_t sinfo;
1789
1790         p_sys->pb_slot_mmi_undisplayed[i_slot] = false;
1791         p += sprintf( p, "<p>CA slot #%d: ", i_slot );
1792
1793         sinfo.num = i_slot;
1794         if ( ioctl( p_sys->i_ca_handle, CA_GET_SLOT_INFO, &sinfo ) != 0 )
1795         {
1796             char buf[1000];
1797             strerror_r( errno, buf, sizeof( buf ) );
1798             p += sprintf( p, "ioctl CA_GET_SLOT_INFO failed (%s)<br>\n", buf );
1799             continue;
1800         }
1801
1802 #define CHECK_TYPE( x, s )                                                  \
1803         if ( sinfo.type & (CA_##x) )                                        \
1804             p += sprintf( p, "%s", s );
1805
1806         CHECK_TYPE( CI, "high level, " );
1807         CHECK_TYPE( CI_LINK, "link layer level, " );
1808         CHECK_TYPE( CI_PHYS, "physical layer level, " );
1809 #undef CHECK_TYPE
1810
1811         if ( sinfo.flags & CA_CI_MODULE_READY )
1812         {
1813             en50221_mmi_object_t *p_object = en50221_GetMMIObject( p_access,
1814                                                                        i_slot );
1815
1816             p += sprintf( p, "module present and ready<p>\n" );
1817             p += sprintf( p, "<form action=index.html method=get>\n" );
1818             p += sprintf( p, "<input type=hidden name=slot value=\"%d\">\n",
1819                           i_slot );
1820
1821             if ( p_object == NULL )
1822             {
1823                 p += sprintf( p, "<input type=submit name=open value=\"Open session\">\n" );
1824             }
1825             else
1826             {
1827                 switch ( p_object->i_object_type )
1828                 {
1829                 case EN50221_MMI_ENQ:
1830                     p += sprintf( p, "<input type=hidden name=type value=enq>\n" );
1831                     p += sprintf( p, "<table border=1><tr><th>%s</th></tr>\n",
1832                                   p_object->u.enq.psz_text );
1833                     if ( p_object->u.enq.b_blind == false )
1834                         p += sprintf( p, "<tr><td><input type=text name=answ></td></tr>\n" );
1835                     else
1836                         p += sprintf( p, "<tr><td><input type=password name=answ></td></tr>\n" );
1837                     break;
1838
1839                 case EN50221_MMI_MENU:
1840                     p += sprintf( p, "<input type=hidden name=type value=menu>\n" );
1841                     p += sprintf( p, "<table border=1><tr><th>%s</th></tr>\n",
1842                                   p_object->u.menu.psz_title );
1843                     p += sprintf( p, "<tr><td>%s</td></tr><tr><td>\n",
1844                                   p_object->u.menu.psz_subtitle );
1845                     for ( i = 0; i < p_object->u.menu.i_choices; i++ )
1846                         p += sprintf( p, "<input type=radio name=choice value=\"%d\">%s<br>\n", i + 1, p_object->u.menu.ppsz_choices[i] );
1847                     p += sprintf( p, "</td></tr><tr><td>%s</td></tr>\n",
1848                                   p_object->u.menu.psz_bottom );
1849                     break;
1850
1851                 case EN50221_MMI_LIST:
1852                     p += sprintf( p, "<input type=hidden name=type value=menu>\n" );
1853                     p += sprintf( p, "<input type=hidden name=choice value=0>\n" );
1854                     p += sprintf( p, "<table border=1><tr><th>%s</th></tr>\n",
1855                                   p_object->u.menu.psz_title );
1856                     p += sprintf( p, "<tr><td>%s</td></tr><tr><td>\n",
1857                                   p_object->u.menu.psz_subtitle );
1858                     for ( i = 0; i < p_object->u.menu.i_choices; i++ )
1859                         p += sprintf( p, "%s<br>\n",
1860                                       p_object->u.menu.ppsz_choices[i] );
1861                     p += sprintf( p, "</td></tr><tr><td>%s</td></tr>\n",
1862                                   p_object->u.menu.psz_bottom );
1863                     break;
1864
1865                 default:
1866                     p += sprintf( p, "<table><tr><th>Unknown MMI object type</th></tr>\n" );
1867                 }
1868
1869                 p += sprintf( p, "</table><p><input type=submit name=ok value=\"OK\">\n" );
1870                 p += sprintf( p, "<input type=submit name=cancel value=\"Cancel\">\n" );
1871                 p += sprintf( p, "<input type=submit name=close value=\"Close Session\">\n" );
1872             }
1873             p += sprintf( p, "</form>\n" );
1874         }
1875         else if ( sinfo.flags & CA_CI_MODULE_PRESENT )
1876             p += sprintf( p, "module present, not ready<br>\n" );
1877         else
1878             p += sprintf( p, "module not present<br>\n" );
1879     }
1880
1881 out:
1882     vlc_mutex_lock( &p_sys->httpd_mutex );
1883     p_sys->b_request_mmi_info = false;
1884     vlc_cond_signal( &p_sys->httpd_cond );
1885     vlc_mutex_unlock( &p_sys->httpd_mutex );
1886 }
1887 #endif
1888
1889 /*****************************************************************************
1890  * CAMSet :
1891  *****************************************************************************/
1892 int CAMSet( access_t * p_access, dvbpsi_pmt_t *p_pmt )
1893 {
1894     access_sys_t *p_sys = p_access->p_sys;
1895
1896     if( p_sys->i_ca_handle == 0 )
1897     {
1898         dvbpsi_DeletePMT( p_pmt );
1899         return VLC_EGENERIC;
1900     }
1901
1902     en50221_SetCAPMT( p_access, p_pmt );
1903
1904     return VLC_SUCCESS;
1905 }
1906
1907 /*****************************************************************************
1908  * CAMClose :
1909  *****************************************************************************/
1910 void CAMClose( access_t * p_access )
1911 {
1912     access_sys_t *p_sys = p_access->p_sys;
1913
1914     en50221_End( p_access );
1915
1916     if ( p_sys->i_ca_handle )
1917     {
1918         close( p_sys->i_ca_handle );
1919     }
1920 }
1921