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