1 /*****************************************************************************
2 * linux_dvb.c : functions to control a DVB card under Linux with v4l2
3 *****************************************************************************
4 * Copyright (C) 1998-2010 the VideoLAN team
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 * David Kaplan <david@of1.org>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_access.h>
37 #include <sys/types.h>
38 #include <sys/ioctl.h>
43 /* DVB Card Drivers */
44 #include <linux/dvb/version.h>
45 #include <linux/dvb/dmx.h>
46 #include <linux/dvb/frontend.h>
47 #include <linux/dvb/ca.h>
49 /* Include dvbpsi headers */
50 # include <dvbpsi/dvbpsi.h>
51 # include <dvbpsi/descriptor.h>
52 # include <dvbpsi/pat.h>
53 # include <dvbpsi/pmt.h>
54 # include <dvbpsi/dr.h>
55 # include <dvbpsi/psi.h>
56 # include <dvbpsi/demux.h>
57 # include <dvbpsi/sdt.h>
60 # include <vlc_httpd.h>
66 #define DMX "/dev/dvb/adapter%d/demux%d"
67 #define FRONTEND "/dev/dvb/adapter%d/frontend%d"
68 #define DVR "/dev/dvb/adapter%d/dvr%d"
69 #define CA "/dev/dvb/adapter%d/ca%d"
76 fe_status_t i_last_status;
77 struct dvb_frontend_info info;
80 #define FRONTEND_LOCK_TIMEOUT 10000000 /* 10 s */
82 /* Local prototypes */
83 static int FrontendInfo( access_t * );
84 static int FrontendSetQPSK( access_t * );
85 static int FrontendSetQAM( access_t * );
86 static int FrontendSetOFDM( access_t * );
87 static int FrontendSetATSC( access_t * );
89 /*****************************************************************************
90 * FrontendOpen : Determine frontend device information and capabilities
91 *****************************************************************************/
92 int FrontendOpen( access_t *p_access )
94 access_sys_t *p_sys = p_access->p_sys;
95 frontend_t * p_frontend;
96 unsigned int i_adapter, i_device;
100 i_adapter = var_GetInteger( p_access, "dvb-adapter" );
101 i_device = var_GetInteger( p_access, "dvb-device" );
102 b_probe = var_GetBool( p_access, "dvb-probe" );
104 if( snprintf( frontend, sizeof(frontend), FRONTEND, i_adapter, i_device ) >= (int)sizeof(frontend) )
106 msg_Err( p_access, "snprintf() truncated string for FRONTEND" );
107 frontend[sizeof(frontend) - 1] = '\0';
110 p_sys->p_frontend = p_frontend = malloc( sizeof(frontend_t) );
114 msg_Dbg( p_access, "Opening device %s", frontend );
115 if( (p_sys->i_frontend_handle = vlc_open(frontend, O_RDWR | O_NONBLOCK)) < 0 )
117 msg_Err( p_access, "FrontEndOpen: opening device failed (%m)" );
124 const char * psz_expected = NULL;
125 const char * psz_real;
127 if( FrontendInfo( p_access ) < 0 )
129 close( p_sys->i_frontend_handle );
134 switch( p_frontend->info.type )
149 psz_real = "unknown";
153 if( (!strncmp( p_access->psz_access, "qpsk", 4 ) ||
154 !strncmp( p_access->psz_access, "dvb-s", 5 ) ||
155 !strncmp( p_access->psz_access, "satellite", 9 ) ) &&
156 (p_frontend->info.type != FE_QPSK) )
158 psz_expected = "DVB-S";
160 if( (!strncmp( p_access->psz_access, "cable", 5 ) ||
161 !strncmp( p_access->psz_access, "dvb-c", 5 ) ) &&
162 (p_frontend->info.type != FE_QAM) )
164 psz_expected = "DVB-C";
166 if( (!strncmp( p_access->psz_access, "terrestrial", 11 ) ||
167 !strncmp( p_access->psz_access, "dvb-t", 5 ) ) &&
168 (p_frontend->info.type != FE_OFDM) )
170 psz_expected = "DVB-T";
173 if( (!strncmp( p_access->psz_access, "usdigital", 9 ) ||
174 !strncmp( p_access->psz_access, "atsc", 4 ) ) &&
175 (p_frontend->info.type != FE_ATSC) )
177 psz_expected = "ATSC";
180 if( psz_expected != NULL )
182 msg_Err( p_access, "requested type %s not supported by %s tuner",
183 psz_expected, psz_real );
184 close( p_sys->i_frontend_handle );
189 else /* no frontend probing is done so use default border values. */
191 msg_Dbg( p_access, "using default values for frontend info" );
193 msg_Dbg( p_access, "method of access is %s", p_access->psz_access );
194 p_frontend->info.type = FE_QPSK;
195 if( !strncmp( p_access->psz_access, "qpsk", 4 ) ||
196 !strncmp( p_access->psz_access, "dvb-s", 5 ) )
197 p_frontend->info.type = FE_QPSK;
198 else if( !strncmp( p_access->psz_access, "cable", 5 ) ||
199 !strncmp( p_access->psz_access, "dvb-c", 5 ) )
200 p_frontend->info.type = FE_QAM;
201 else if( !strncmp( p_access->psz_access, "terrestrial", 11 ) ||
202 !strncmp( p_access->psz_access, "dvb-t", 5 ) )
203 p_frontend->info.type = FE_OFDM;
204 else if( !strncmp( p_access->psz_access, "usdigital", 9 ) ||
205 !strncmp( p_access->psz_access, "atsc", 4 ) )
206 p_frontend->info.type = FE_ATSC;
212 /*****************************************************************************
213 * FrontendClose : Close the frontend
214 *****************************************************************************/
215 void FrontendClose( access_t *p_access )
217 access_sys_t *p_sys = p_access->p_sys;
219 if( p_sys->p_frontend )
221 close( p_sys->i_frontend_handle );
222 free( p_sys->p_frontend );
224 p_sys->p_frontend = NULL;
228 /*****************************************************************************
229 * FrontendSet : Tune !
230 *****************************************************************************/
231 int FrontendSet( access_t *p_access )
233 access_sys_t *p_sys = p_access->p_sys;
235 switch( p_sys->p_frontend->info.type )
239 if( FrontendSetQPSK( p_access ) )
241 msg_Err( p_access, "DVB-S tuning error" );
248 if( FrontendSetQAM( p_access ) )
250 msg_Err( p_access, "DVB-C tuning error" );
257 if( FrontendSetOFDM( p_access ) )
259 msg_Err( p_access, "DVB-T tuning error" );
266 if( FrontendSetATSC( p_access ) )
268 msg_Err( p_access, "ATSC tuning error" );
274 msg_Err( p_access, "tuner type %s not supported",
275 p_sys->p_frontend->info.name );
278 p_sys->p_frontend->i_last_status = 0;
279 p_sys->i_frontend_timeout = mdate() + FRONTEND_LOCK_TIMEOUT;
283 /*****************************************************************************
284 * FrontendPoll : Poll for frontend events
285 *****************************************************************************/
286 void FrontendPoll( access_t *p_access )
288 access_sys_t *p_sys = p_access->p_sys;
289 frontend_t * p_frontend = p_sys->p_frontend;
290 struct dvb_frontend_event event;
291 fe_status_t i_status, i_diff;
295 if( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0 )
297 if( errno != EWOULDBLOCK )
298 msg_Err( p_access, "frontend event error: %m" );
302 i_status = event.status;
303 i_diff = i_status ^ p_frontend->i_last_status;
304 p_frontend->i_last_status = i_status;
309 if ( i_diff & (x) ) \
311 if ( i_status & (x) )
313 IF_UP( FE_HAS_SIGNAL )
314 msg_Dbg( p_access, "frontend has acquired signal" );
316 msg_Dbg( p_access, "frontend has lost signal" );
318 IF_UP( FE_HAS_CARRIER )
319 msg_Dbg( p_access, "frontend has acquired carrier" );
321 msg_Dbg( p_access, "frontend has lost carrier" );
323 IF_UP( FE_HAS_VITERBI )
324 msg_Dbg( p_access, "frontend has acquired stable FEC" );
326 msg_Dbg( p_access, "frontend has lost FEC" );
329 msg_Dbg( p_access, "frontend has acquired sync" );
331 msg_Dbg( p_access, "frontend has lost sync" );
335 frontend_statistic_t stat;
337 msg_Dbg( p_access, "frontend has acquired lock" );
338 p_sys->i_frontend_timeout = 0;
340 /* Read some statistics */
341 if( !FrontendGetStatistic( p_access, &stat ) )
343 if( stat.i_ber >= 0 )
344 msg_Dbg( p_access, "- Bit error rate: %d", stat.i_ber );
345 if( stat.i_signal_strenth >= 0 )
346 msg_Dbg( p_access, "- Signal strength: %d", stat.i_signal_strenth );
347 if( stat.i_snr >= 0 )
348 msg_Dbg( p_access, "- SNR: %d", stat.i_snr );
353 msg_Dbg( p_access, "frontend has lost lock" );
354 p_sys->i_frontend_timeout = mdate() + FRONTEND_LOCK_TIMEOUT;
359 /* The frontend was reinited. */
360 msg_Warn( p_access, "reiniting frontend");
361 FrontendSet( p_access );
368 int FrontendGetStatistic( access_t *p_access, frontend_statistic_t *p_stat )
370 access_sys_t *p_sys = p_access->p_sys;
371 frontend_t * p_frontend = p_sys->p_frontend;
373 if( (p_frontend->i_last_status & FE_HAS_LOCK) == 0 )
376 memset( p_stat, 0, sizeof(*p_stat) );
377 if( ioctl( p_sys->i_frontend_handle, FE_READ_BER, &p_stat->i_ber ) < 0 )
379 if( ioctl( p_sys->i_frontend_handle, FE_READ_SIGNAL_STRENGTH, &p_stat->i_signal_strenth ) < 0 )
380 p_stat->i_signal_strenth = -1;
381 if( ioctl( p_sys->i_frontend_handle, FE_READ_SNR, &p_stat->i_snr ) < 0 )
387 void FrontendGetStatus( access_t *p_access, frontend_status_t *p_status )
389 access_sys_t *p_sys = p_access->p_sys;
390 frontend_t * p_frontend = p_sys->p_frontend;
392 p_status->b_has_signal = (p_frontend->i_last_status & FE_HAS_SIGNAL) != 0;
393 p_status->b_has_carrier = (p_frontend->i_last_status & FE_HAS_CARRIER) != 0;
394 p_status->b_has_lock = (p_frontend->i_last_status & FE_HAS_LOCK) != 0;
397 static int ScanParametersDvbS( access_t *p_access, scan_parameter_t *p_scan )
399 const frontend_t *p_frontend = p_access->p_sys->p_frontend;
401 memset( p_scan, 0, sizeof(*p_scan) );
402 p_scan->type = SCAN_DVB_S;
404 p_scan->frequency.i_min = p_frontend->info.frequency_min;
405 p_scan->frequency.i_max = p_frontend->info.frequency_max;
406 /* set satellite config file path */
407 p_scan->sat_info.psz_name = var_InheritString( p_access, "dvb-satellite" );
412 static int ScanParametersDvbC( access_t *p_access, scan_parameter_t *p_scan )
414 const frontend_t *p_frontend = p_access->p_sys->p_frontend;
416 memset( p_scan, 0, sizeof(*p_scan) );
417 p_scan->type = SCAN_DVB_C;
418 p_scan->b_exhaustive = false;
421 p_scan->frequency.i_min = p_frontend->info.frequency_min;
422 p_scan->frequency.i_max = p_frontend->info.frequency_max;
423 p_scan->frequency.i_step = p_frontend->info.frequency_stepsize
424 ? p_frontend->info.frequency_stepsize : 166667;
425 p_scan->frequency.i_count = (p_scan->frequency.i_max-p_scan->frequency.i_min)/p_scan->frequency.i_step;
428 p_scan->bandwidth.i_min = 6;
429 p_scan->bandwidth.i_max = 8;
430 p_scan->bandwidth.i_step = 1;
431 p_scan->bandwidth.i_count = 3;
435 static int ScanParametersDvbT( access_t *p_access, scan_parameter_t *p_scan )
437 const frontend_t *p_frontend = p_access->p_sys->p_frontend;
439 memset( p_scan, 0, sizeof(*p_scan) );
440 p_scan->type = SCAN_DVB_T;
441 p_scan->b_exhaustive = false;
444 p_scan->frequency.i_min = p_frontend->info.frequency_min;
445 p_scan->frequency.i_max = p_frontend->info.frequency_max;
446 p_scan->frequency.i_step = p_frontend->info.frequency_stepsize
447 ? p_frontend->info.frequency_stepsize : 166667;
448 p_scan->frequency.i_count = (p_scan->frequency.i_max-p_scan->frequency.i_min)/p_scan->frequency.i_step;
451 p_scan->bandwidth.i_min = 6;
452 p_scan->bandwidth.i_max = 8;
453 p_scan->bandwidth.i_step = 1;
454 p_scan->bandwidth.i_count = 3;
458 int FrontendGetScanParameter( access_t *p_access, scan_parameter_t *p_scan )
460 access_sys_t *p_sys = p_access->p_sys;
461 const frontend_t *p_frontend = p_sys->p_frontend;
463 if( p_frontend->info.type == FE_OFDM ) /* DVB-T */
464 return ScanParametersDvbT( p_access, p_scan );
465 else if( p_frontend->info.type == FE_QAM ) /* DVB-C */
466 return ScanParametersDvbC( p_access, p_scan );
467 else if( p_frontend->info.type == FE_QPSK )
468 return ScanParametersDvbS( p_access, p_scan ); /* DVB-S */
470 msg_Err( p_access, "frontend scanning not supported" );
475 /*****************************************************************************
476 * FrontendStatus : Read frontend status
477 *****************************************************************************/
478 void FrontendStatus( access_t *p_access )
480 access_sys_t *p_sys = p_access->p_sys;
481 frontend_t *p_frontend = p_sys->p_frontend;
482 char *p = p_sys->psz_frontend_info = malloc( 10000 );
483 fe_status_t i_status;
485 /* Determine type of frontend */
486 if( ioctl( p_sys->i_frontend_handle, FE_GET_INFO, &p_frontend->info ) < 0 )
489 strerror_r( errno, buf, sizeof( buf ) );
490 p += sprintf( p, "ioctl FE_GET_INFO failed %s\n", buf );
494 /* Print out frontend capabilities. */
495 p += sprintf( p, "<table border=1><tr><th>name</th><td>%s</td></tr>\n",
496 p_frontend->info.name );
497 switch( p_frontend->info.type )
500 p += sprintf( p, "<tr><th>type</th><td>QPSK (DVB-S)</td></tr>\n" );
503 p += sprintf( p, "<tr><th>type</th><td>QAM (DVB-C)</td></tr>\n" );
506 p += sprintf( p, "<tr><th>type</th><td>OFDM (DVB-T)</td></tr>\n" );
508 #if 0 /* DVB_API_VERSION == 3 */
510 p += sprintf( p, "<tr><th>type</th><td>MEMORY</td></tr>\n" );
513 p += sprintf( p, "<tr><th>type</th><td>NETWORK</td></tr>\n" );
517 p += sprintf( p, "<tr><th>type</th><td>UNKNOWN (%d)</td></tr>\n",
518 p_frontend->info.type );
521 #define CHECK_INFO( x ) \
523 "<tr><th>" STRINGIFY(x) "</th><td>%u</td></tr>\n", \
524 p_frontend->info.x );
526 CHECK_INFO( frequency_min );
527 CHECK_INFO( frequency_max );
528 CHECK_INFO( frequency_stepsize );
529 CHECK_INFO( frequency_tolerance );
530 CHECK_INFO( symbol_rate_min );
531 CHECK_INFO( symbol_rate_max );
532 CHECK_INFO( symbol_rate_tolerance );
533 CHECK_INFO( notifier_delay );
536 p += sprintf( p, "</table><p>Frontend capability list:\n<table border=1>" );
538 #define CHECK_CAPS( x ) \
539 if ( p_frontend->info.caps & (FE_##x) ) \
540 p += sprintf( p, "<tr><td>" STRINGIFY(x) "</td></tr>\n" );
542 CHECK_CAPS( IS_STUPID );
543 CHECK_CAPS( CAN_INVERSION_AUTO );
544 CHECK_CAPS( CAN_FEC_1_2 );
545 CHECK_CAPS( CAN_FEC_2_3 );
546 CHECK_CAPS( CAN_FEC_3_4 );
547 CHECK_CAPS( CAN_FEC_4_5 );
548 CHECK_CAPS( CAN_FEC_5_6 );
549 CHECK_CAPS( CAN_FEC_6_7 );
550 CHECK_CAPS( CAN_FEC_7_8 );
551 CHECK_CAPS( CAN_FEC_8_9 );
552 CHECK_CAPS( CAN_FEC_AUTO );
553 CHECK_CAPS( CAN_QPSK );
554 CHECK_CAPS( CAN_QAM_16 );
555 CHECK_CAPS( CAN_QAM_32 );
556 CHECK_CAPS( CAN_QAM_64 );
557 CHECK_CAPS( CAN_QAM_128 );
558 CHECK_CAPS( CAN_QAM_256 );
559 CHECK_CAPS( CAN_QAM_AUTO );
560 CHECK_CAPS( CAN_TRANSMISSION_MODE_AUTO );
561 CHECK_CAPS( CAN_BANDWIDTH_AUTO );
562 CHECK_CAPS( CAN_GUARD_INTERVAL_AUTO );
563 CHECK_CAPS( CAN_HIERARCHY_AUTO );
564 CHECK_CAPS( CAN_MUTE_TS );
565 CHECK_CAPS( CAN_RECOVER );
566 #if 0 /* Disabled because of older distributions */
567 CHECK_CAPS( CAN_CLEAN_SETUP );
571 p += sprintf( p, "</table><p>Current frontend status:\n<table border=1>" );
573 if( ioctl( p_sys->i_frontend_handle, FE_READ_STATUS, &i_status ) < 0 )
576 strerror_r( errno, buf, sizeof( buf ) );
577 p += sprintf( p, "</table>ioctl FE_READ_STATUS failed %s\n", buf );
581 #define CHECK_STATUS( x ) \
582 if ( i_status & (FE_##x) ) \
583 p += sprintf( p, "<tr><td>" STRINGIFY(x) "</td></tr>\n" );
585 CHECK_STATUS( HAS_SIGNAL );
586 CHECK_STATUS( HAS_CARRIER );
587 CHECK_STATUS( HAS_VITERBI );
588 CHECK_STATUS( HAS_SYNC );
589 CHECK_STATUS( HAS_LOCK );
590 CHECK_STATUS( REINIT );
592 p += sprintf( p, "<tr><td>Tuning failed</td></tr>\n" );
595 if ( i_status & FE_HAS_LOCK )
598 p += sprintf( p, "</table><p>Signal status:\n<table border=1>" );
599 if( ioctl( p_sys->i_frontend_handle, FE_READ_BER, &i_value ) >= 0 )
600 p += sprintf( p, "<tr><th>Bit error rate</th><td>%d</td></tr>\n",
602 if( ioctl( p_sys->i_frontend_handle, FE_READ_SIGNAL_STRENGTH,
604 p += sprintf( p, "<tr><th>Signal strength</th><td>%d</td></tr>\n",
606 if( ioctl( p_sys->i_frontend_handle, FE_READ_SNR, &i_value ) >= 0 )
607 p += sprintf( p, "<tr><th>SNR</th><td>%d</td></tr>\n",
610 p += sprintf( p, "</table>" );
613 vlc_mutex_lock( &p_sys->httpd_mutex );
614 p_sys->b_request_frontend_info = false;
615 vlc_cond_signal( &p_sys->httpd_cond );
616 vlc_mutex_unlock( &p_sys->httpd_mutex );
620 /*****************************************************************************
621 * FrontendInfo : Return information about given frontend
622 *****************************************************************************/
623 static int FrontendInfo( access_t *p_access )
625 access_sys_t *p_sys = p_access->p_sys;
626 frontend_t *p_frontend = p_sys->p_frontend;
628 /* Determine type of frontend */
629 if( ioctl( p_sys->i_frontend_handle, FE_GET_INFO, &p_frontend->info ) < 0 )
631 msg_Err( p_access, "frontend info request error: %m" );
635 /* Print out frontend capabilities. */
636 msg_Dbg(p_access, "Frontend Info:" );
637 msg_Dbg(p_access, " name = %s", p_frontend->info.name );
638 switch( p_frontend->info.type )
641 msg_Dbg( p_access, " type = QPSK (DVB-S)" );
644 msg_Dbg( p_access, " type = QAM (DVB-C)" );
647 msg_Dbg( p_access, " type = OFDM (DVB-T)" );
650 msg_Dbg( p_access, " type = ATSC (USA)" );
652 #if 0 /* DVB_API_VERSION == 3 */
654 msg_Dbg(p_access, " type = MEMORY" );
657 msg_Dbg(p_access, " type = NETWORK" );
661 msg_Err( p_access, " unknown frontend type (%d)",
662 p_frontend->info.type );
665 msg_Dbg(p_access, " frequency_min = %u (kHz)",
666 p_frontend->info.frequency_min);
667 msg_Dbg(p_access, " frequency_max = %u (kHz)",
668 p_frontend->info.frequency_max);
669 msg_Dbg(p_access, " frequency_stepsize = %u",
670 p_frontend->info.frequency_stepsize);
671 msg_Dbg(p_access, " frequency_tolerance = %u",
672 p_frontend->info.frequency_tolerance);
673 msg_Dbg(p_access, " symbol_rate_min = %u (kHz)",
674 p_frontend->info.symbol_rate_min);
675 msg_Dbg(p_access, " symbol_rate_max = %u (kHz)",
676 p_frontend->info.symbol_rate_max);
677 msg_Dbg(p_access, " symbol_rate_tolerance (ppm) = %u",
678 p_frontend->info.symbol_rate_tolerance);
679 msg_Dbg(p_access, " notifier_delay (ms) = %u",
680 p_frontend->info.notifier_delay );
682 msg_Dbg(p_access, "Frontend Info capability list:");
683 if( p_frontend->info.caps == FE_IS_STUPID)
684 msg_Dbg(p_access, " no capabilities - frontend is stupid!");
685 if( p_frontend->info.caps & FE_CAN_INVERSION_AUTO)
686 msg_Dbg(p_access, " inversion auto");
687 if( p_frontend->info.caps & FE_CAN_FEC_1_2)
688 msg_Dbg(p_access, " forward error correction 1/2");
689 if( p_frontend->info.caps & FE_CAN_FEC_2_3)
690 msg_Dbg(p_access, " forward error correction 2/3");
691 if( p_frontend->info.caps & FE_CAN_FEC_3_4)
692 msg_Dbg(p_access, " forward error correction 3/4");
693 if( p_frontend->info.caps & FE_CAN_FEC_4_5)
694 msg_Dbg(p_access, " forward error correction 4/5");
695 if( p_frontend->info.caps & FE_CAN_FEC_5_6)
696 msg_Dbg(p_access, " forward error correction 5/6");
697 if( p_frontend->info.caps & FE_CAN_FEC_6_7)
698 msg_Dbg(p_access, " forward error correction 6/7");
699 if( p_frontend->info.caps & FE_CAN_FEC_7_8)
700 msg_Dbg(p_access, " forward error correction 7/8");
701 if( p_frontend->info.caps & FE_CAN_FEC_8_9)
702 msg_Dbg(p_access, " forward error correction 8/9");
703 if( p_frontend->info.caps & FE_CAN_FEC_AUTO)
704 msg_Dbg(p_access, " forward error correction auto");
705 if( p_frontend->info.caps & FE_CAN_QPSK)
706 msg_Dbg(p_access, " QPSK modulation");
707 if( p_frontend->info.caps & FE_CAN_QAM_16)
708 msg_Dbg(p_access, " QAM 16 modulation");
709 if( p_frontend->info.caps & FE_CAN_QAM_32)
710 msg_Dbg(p_access, " QAM 32 modulation");
711 if( p_frontend->info.caps & FE_CAN_QAM_64)
712 msg_Dbg(p_access, " QAM 64 modulation");
713 if( p_frontend->info.caps & FE_CAN_QAM_128)
714 msg_Dbg(p_access, " QAM 128 modulation");
715 if( p_frontend->info.caps & FE_CAN_QAM_256)
716 msg_Dbg(p_access, " QAM 256 modulation");
717 if( p_frontend->info.caps & FE_CAN_QAM_AUTO)
718 msg_Dbg(p_access, " QAM auto modulation");
719 if( p_frontend->info.caps & FE_CAN_TRANSMISSION_MODE_AUTO)
720 msg_Dbg(p_access, " transmission mode auto");
721 if( p_frontend->info.caps & FE_CAN_BANDWIDTH_AUTO)
722 msg_Dbg(p_access, " bandwidth mode auto");
723 if( p_frontend->info.caps & FE_CAN_GUARD_INTERVAL_AUTO)
724 msg_Dbg(p_access, " guard interval mode auto");
725 if( p_frontend->info.caps & FE_CAN_HIERARCHY_AUTO)
726 msg_Dbg(p_access, " hierarchy mode auto");
727 if( p_frontend->info.caps & FE_CAN_8VSB)
728 msg_Dbg(p_access, " 8-level VSB modulation");
729 if( p_frontend->info.caps & FE_CAN_16VSB)
730 msg_Dbg(p_access, " 16-level VSB modulation");
731 if( p_frontend->info.caps & FE_HAS_EXTENDED_CAPS)
732 msg_Dbg(p_access, " 8-level VSB modulation");
733 /* 3 capabilities that don't exist yet HERE */
734 #ifdef FE_CAN_TURBO_FEC
735 if( p_frontend->info.caps & FE_CAN_TURBO_FEC)
736 msg_Dbg(p_access, " Turbo FEC modulation");
738 # warning Please update your Linux kernel headers!
740 if( p_frontend->info.caps & FE_CAN_2G_MODULATION)
741 msg_Dbg(p_access, " 2nd generation modulation (DVB-S2)");
742 /* FE_NEEDS_BENDING is deprecated */
743 if( p_frontend->info.caps & FE_CAN_RECOVER)
744 msg_Dbg(p_access, " cable unplug recovery");
745 if( p_frontend->info.caps & FE_CAN_MUTE_TS)
746 msg_Dbg(p_access, " spurious TS muting");
747 msg_Dbg(p_access, "End of capability list");
752 /*****************************************************************************
753 * Decoding the DVB parameters (common)
754 *****************************************************************************/
755 static fe_spectral_inversion_t DecodeInversion( access_t *p_access )
758 fe_spectral_inversion_t fe_inversion = 0;
760 i_val = var_GetInteger( p_access, "dvb-inversion" );
761 msg_Dbg( p_access, "using inversion=%d", i_val );
765 case 0: fe_inversion = INVERSION_OFF; break;
766 case 1: fe_inversion = INVERSION_ON; break;
767 case 2: fe_inversion = INVERSION_AUTO; break;
769 msg_Dbg( p_access, "dvb has inversion not set, using auto");
770 fe_inversion = INVERSION_AUTO;
776 static fe_code_rate_t DecodeFEC( access_t *p_access, const char *varname )
778 switch( var_GetInteger(p_access, varname) )
780 case 0: return FEC_NONE;
781 case 1: return FEC_1_2;
782 case 2: return FEC_2_3;
783 case 3: return FEC_3_4;
784 case 4: return FEC_4_5;
785 case 5: return FEC_5_6;
786 case 6: return FEC_6_7;
787 case 7: return FEC_7_8;
788 case 8: return FEC_8_9;
789 case 9: return FEC_AUTO;
790 default: return FEC_NONE;
794 static fe_modulation_t DecodeModulation( access_t *p_access,
795 fe_modulation_t def )
797 switch( var_GetInteger( p_access, "dvb-modulation" ) )
799 case -1: return QPSK;
800 case 0: return QAM_AUTO;
801 case 8: return VSB_8;
802 case 16: return QAM_16;
803 case 32: return QAM_32;
804 case 64: return QAM_64;
805 case 128: return QAM_128;
806 case 256: return QAM_256;
811 /*****************************************************************************
812 * FrontendSetQPSK : controls the FE device
813 *****************************************************************************/
814 static fe_sec_voltage_t DecodeVoltage( access_t *p_access )
816 switch( var_GetInteger( p_access, "dvb-voltage" ) )
818 case 0: return SEC_VOLTAGE_OFF;
819 case 13: return SEC_VOLTAGE_13;
820 case 18: return SEC_VOLTAGE_18;
821 default: return SEC_VOLTAGE_OFF;
825 static fe_sec_tone_mode_t DecodeTone( access_t *p_access )
827 switch( var_GetInteger( p_access, "dvb-tone" ) )
829 case 0: return SEC_TONE_OFF;
830 case 1: return SEC_TONE_ON;
831 default: return SEC_TONE_OFF;
837 struct dvb_diseqc_master_cmd cmd;
841 static int DoDiseqc( access_t *p_access )
843 access_sys_t *p_sys = p_access->p_sys;
846 int i_frequency, i_lnb_slof;
847 fe_sec_voltage_t fe_voltage;
848 fe_sec_tone_mode_t fe_tone;
850 i_frequency = var_GetInteger( p_access, "dvb-frequency" );
851 i_lnb_slof = var_GetInteger( p_access, "dvb-lnb-slof" );
853 i_val = var_GetInteger( p_access, "dvb-tone" );
854 if( i_val == -1 /* auto */ )
856 if( i_frequency >= i_lnb_slof )
860 var_SetInteger( p_access, "dvb-tone", i_val );
863 fe_voltage = DecodeVoltage( p_access );
864 fe_tone = DecodeTone( p_access );
866 /* Switch off continuous tone. */
867 if( ioctl( p_sys->i_frontend_handle, FE_SET_TONE, SEC_TONE_OFF ) < 0 )
869 msg_Err( p_access, "switching tone %s error: %m", "off" );
873 /* Configure LNB voltage. */
874 if( ioctl( p_sys->i_frontend_handle, FE_SET_VOLTAGE, fe_voltage ) < 0 )
876 msg_Err( p_access, "voltage error: %m" );
880 b_val = var_GetBool( p_access, "dvb-high-voltage" );
881 if( ioctl( p_sys->i_frontend_handle,
882 FE_ENABLE_HIGH_LNB_VOLTAGE, b_val ) < 0 && b_val )
884 msg_Err( p_access, "high LNB voltage error: %m" );
887 /* Wait for at least 15 ms. */
890 i_val = var_GetInteger( p_access, "dvb-satno" );
891 if( i_val > 0 && i_val < 5 )
893 /* digital satellite equipment control,
894 * specification is available from http://www.eutelsat.com/
897 /* 1.x compatible equipment */
898 struct diseqc_cmd_t cmd = { {{0xe0, 0x10, 0x38, 0xf0, 0x00, 0x00}, 4}, 0 };
900 /* param: high nibble: reset bits, low nibble set bits,
901 * bits are: option, position, polarization, band
903 cmd.cmd.msg[3] = 0xf0 /* reset bits */
904 | (((i_val - 1) * 4) & 0xc)
905 | (fe_voltage == SEC_VOLTAGE_13 ? 0 : 2)
906 | (fe_tone == SEC_TONE_ON ? 1 : 0);
908 if( ioctl( p_sys->i_frontend_handle, FE_DISEQC_SEND_MASTER_CMD,
911 msg_Err( p_access, "master command sending error: %m" );
915 msleep(15000 + cmd.wait * 1000);
917 /* A or B simple diseqc ("diseqc-compatible") */
918 if( ioctl( p_sys->i_frontend_handle, FE_DISEQC_SEND_BURST,
919 ((i_val - 1) % 2) ? SEC_MINI_B : SEC_MINI_A ) )
921 msg_Err( p_access, "burst sending error: %m" );
928 if( ioctl( p_sys->i_frontend_handle, FE_SET_TONE, fe_tone ) )
930 msg_Err( p_access, "switching tone %s error: %m",
931 (fe_tone == SEC_TONE_ON) ? "on" : "off" );
939 static int FrontendSetQPSK( access_t *p_access )
941 access_sys_t *p_sys = p_access->p_sys;
942 struct dvb_frontend_parameters fep;
944 int i_frequency, i_lnb_slof = 0, i_lnb_lof1, i_lnb_lof2 = 0;
946 /* Prepare the fep structure */
947 i_frequency = var_GetInteger( p_access, "dvb-frequency" );
949 i_val = var_GetInteger( p_access, "dvb-lnb-lof1" );
952 /* Automatic mode. */
953 if ( i_frequency >= 950000 && i_frequency <= 2150000 )
955 msg_Dbg( p_access, "frequency %d is in IF-band", i_frequency );
958 else if ( i_frequency >= 2500000 && i_frequency <= 2700000 )
960 msg_Dbg( p_access, "frequency %d is in S-band", i_frequency );
961 i_lnb_lof1 = 3650000;
963 else if ( i_frequency >= 3400000 && i_frequency <= 4200000 )
965 msg_Dbg( p_access, "frequency %d is in C-band (lower)",
967 i_lnb_lof1 = 5150000;
969 else if ( i_frequency >= 4500000 && i_frequency <= 4800000 )
971 msg_Dbg( p_access, "frequency %d is in C-band (higher)",
973 i_lnb_lof1 = 5950000;
975 else if ( i_frequency >= 10700000 && i_frequency <= 13250000 )
977 msg_Dbg( p_access, "frequency %d is in Ku-band",
979 i_lnb_lof1 = 9750000;
980 i_lnb_lof2 = 10600000;
981 i_lnb_slof = 11700000;
985 msg_Err( p_access, "frequency %d is out of any known band",
987 msg_Err( p_access, "specify dvb-lnb-lof1 manually for the local "
988 "oscillator frequency" );
991 var_SetInteger( p_access, "dvb-lnb-lof1", i_lnb_lof1 );
992 var_SetInteger( p_access, "dvb-lnb-lof2", i_lnb_lof2 );
993 var_SetInteger( p_access, "dvb-lnb-slof", i_lnb_slof );
998 i_lnb_lof2 = var_GetInteger( p_access, "dvb-lnb-lof2" );
999 i_lnb_slof = var_GetInteger( p_access, "dvb-lnb-slof" );
1002 if( i_lnb_slof && i_frequency >= i_lnb_slof )
1004 i_frequency -= i_lnb_lof2;
1008 i_frequency -= i_lnb_lof1;
1010 fep.frequency = i_frequency >= 0 ? i_frequency : -i_frequency;
1012 fep.inversion = DecodeInversion( p_access );
1014 fep.u.qpsk.symbol_rate = var_GetInteger( p_access, "dvb-srate" );
1016 fep.u.qpsk.fec_inner = DecodeFEC( p_access, "dvb-fec" );
1018 if( DoDiseqc( p_access ) < 0 )
1020 return VLC_EGENERIC;
1023 /* Empty the event queue */
1026 struct dvb_frontend_event event;
1027 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
1028 && errno == EWOULDBLOCK )
1032 /* Now send it all to the frontend device */
1033 if( ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep ) < 0 )
1035 msg_Err( p_access, "frontend error: %m" );
1036 return VLC_EGENERIC;
1042 /*****************************************************************************
1043 * FrontendSetQAM : controls the FE device
1044 *****************************************************************************/
1045 static int FrontendSetQAM( access_t *p_access )
1047 access_sys_t *p_sys = p_access->p_sys;
1048 frontend_t *p_frontend = p_sys->p_frontend;
1049 struct dvb_frontend_parameters fep;
1052 /* Prepare the fep structure */
1054 fep.frequency = var_GetInteger( p_access, "dvb-frequency" );
1056 fep.inversion = DecodeInversion( p_access );
1058 /* Default symbol-rate is for dvb-s, and doesn't fit
1059 * for dvb-c, so if it's over the limit of frontend, default to
1060 * somewhat common value
1062 i_val = var_GetInteger( p_access, "dvb-srate" );
1063 if( i_val < p_frontend->info.symbol_rate_max &&
1064 i_val > p_frontend->info.symbol_rate_min )
1065 fep.u.qam.symbol_rate = i_val;
1067 fep.u.qam.symbol_rate = 6875000;
1069 fep.u.qam.fec_inner = DecodeFEC( p_access, "dvb-fec" );
1071 fep.u.qam.modulation = DecodeModulation( p_access, QAM_AUTO );
1073 /* Empty the event queue */
1076 struct dvb_frontend_event event;
1077 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
1078 && errno == EWOULDBLOCK )
1082 /* Now send it all to the frontend device */
1083 if( ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep ) < 0 )
1085 msg_Err( p_access, "frontend error: %m" );
1086 return VLC_EGENERIC;
1092 /*****************************************************************************
1093 * FrontendSetOFDM : controls the FE device
1094 *****************************************************************************/
1095 static fe_bandwidth_t DecodeBandwidth( access_t *p_access )
1097 fe_bandwidth_t fe_bandwidth = 0;
1098 int i_bandwith = var_GetInteger( p_access, "dvb-bandwidth" );
1100 msg_Dbg( p_access, "using bandwidth=%d", i_bandwith );
1102 switch( i_bandwith )
1104 case 0: fe_bandwidth = BANDWIDTH_AUTO; break;
1105 case 6: fe_bandwidth = BANDWIDTH_6_MHZ; break;
1106 case 7: fe_bandwidth = BANDWIDTH_7_MHZ; break;
1107 case 8: fe_bandwidth = BANDWIDTH_8_MHZ; break;
1109 msg_Dbg( p_access, "terrestrial dvb has bandwidth not set, using auto" );
1110 fe_bandwidth = BANDWIDTH_AUTO;
1113 return fe_bandwidth;
1116 static fe_transmit_mode_t DecodeTransmission( access_t *p_access )
1118 fe_transmit_mode_t fe_transmission = 0;
1119 int i_transmission = var_GetInteger( p_access, "dvb-transmission" );
1121 msg_Dbg( p_access, "using transmission=%d", i_transmission );
1123 switch( i_transmission )
1125 case 0: fe_transmission = TRANSMISSION_MODE_AUTO; break;
1126 case 2: fe_transmission = TRANSMISSION_MODE_2K; break;
1127 case 8: fe_transmission = TRANSMISSION_MODE_8K; break;
1129 msg_Dbg( p_access, "terrestrial dvb has transmission mode not set, using auto");
1130 fe_transmission = TRANSMISSION_MODE_AUTO;
1133 return fe_transmission;
1136 static fe_guard_interval_t DecodeGuardInterval( access_t *p_access )
1138 fe_guard_interval_t fe_guard = 0;
1139 int i_guard = var_GetInteger( p_access, "dvb-guard" );
1141 msg_Dbg( p_access, "using guard=%d", i_guard );
1145 case 0: fe_guard = GUARD_INTERVAL_AUTO; break;
1146 case 4: fe_guard = GUARD_INTERVAL_1_4; break;
1147 case 8: fe_guard = GUARD_INTERVAL_1_8; break;
1148 case 16: fe_guard = GUARD_INTERVAL_1_16; break;
1149 case 32: fe_guard = GUARD_INTERVAL_1_32; break;
1151 msg_Dbg( p_access, "terrestrial dvb has guard interval not set, using auto");
1152 fe_guard = GUARD_INTERVAL_AUTO;
1158 static fe_hierarchy_t DecodeHierarchy( access_t *p_access )
1160 fe_hierarchy_t fe_hierarchy = 0;
1161 int i_hierarchy = var_GetInteger( p_access, "dvb-hierarchy" );
1163 msg_Dbg( p_access, "using hierarchy=%d", i_hierarchy );
1165 switch( i_hierarchy )
1167 case -1: fe_hierarchy = HIERARCHY_NONE; break;
1168 case 0: fe_hierarchy = HIERARCHY_AUTO; break;
1169 case 1: fe_hierarchy = HIERARCHY_1; break;
1170 case 2: fe_hierarchy = HIERARCHY_2; break;
1171 case 4: fe_hierarchy = HIERARCHY_4; break;
1173 msg_Dbg( p_access, "terrestrial dvb has hierarchy not set, using auto");
1174 fe_hierarchy = HIERARCHY_AUTO;
1177 return fe_hierarchy;
1180 static int FrontendSetOFDM( access_t * p_access )
1182 access_sys_t *p_sys = p_access->p_sys;
1183 struct dvb_frontend_parameters fep;
1185 /* Prepare the fep structure */
1187 fep.frequency = var_GetInteger( p_access, "dvb-frequency" );
1189 fep.inversion = DecodeInversion( p_access );
1191 fep.u.ofdm.bandwidth = DecodeBandwidth( p_access );
1192 fep.u.ofdm.code_rate_HP = DecodeFEC( p_access, "dvb-code-rate-hp" );
1193 fep.u.ofdm.code_rate_LP = DecodeFEC( p_access, "dvb-code-rate-lp" );
1194 fep.u.ofdm.constellation = DecodeModulation( p_access, QAM_AUTO );
1195 fep.u.ofdm.transmission_mode = DecodeTransmission( p_access );
1196 fep.u.ofdm.guard_interval = DecodeGuardInterval( p_access );
1197 fep.u.ofdm.hierarchy_information = DecodeHierarchy( p_access );
1199 /* Empty the event queue */
1202 struct dvb_frontend_event event;
1203 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
1204 && errno == EWOULDBLOCK )
1208 /* Now send it all to the frontend device */
1209 if( ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep ) < 0 )
1211 msg_Err( p_access, "frontend error: %m" );
1212 return VLC_EGENERIC;
1218 /*****************************************************************************
1219 * FrontendSetATSC : controls the FE device
1220 *****************************************************************************/
1221 static int FrontendSetATSC( access_t *p_access )
1223 access_sys_t *p_sys = p_access->p_sys;
1224 struct dvb_frontend_parameters fep;
1226 /* Prepare the fep structure */
1228 fep.frequency = var_GetInteger( p_access, "dvb-frequency" );
1230 fep.u.vsb.modulation = DecodeModulation( p_access, VSB_8 );
1232 /* Empty the event queue */
1235 struct dvb_frontend_event event;
1236 if ( ioctl( p_sys->i_frontend_handle, FE_GET_EVENT, &event ) < 0
1237 && errno == EWOULDBLOCK )
1241 /* Now send it all to the frontend device */
1242 if( ioctl( p_sys->i_frontend_handle, FE_SET_FRONTEND, &fep ) < 0 )
1244 msg_Err( p_access, "frontend error: %m" );
1245 return VLC_EGENERIC;
1256 /*****************************************************************************
1257 * DMXSetFilter : controls the demux to add a filter
1258 *****************************************************************************/
1259 int DMXSetFilter( access_t * p_access, int i_pid, int * pi_fd, int i_type )
1261 struct dmx_pes_filter_params s_filter_params;
1262 unsigned int i_adapter, i_device;
1265 i_adapter = var_GetInteger( p_access, "dvb-adapter" );
1266 i_device = var_GetInteger( p_access, "dvb-device" );
1268 if( snprintf( dmx, sizeof(dmx), DMX, i_adapter, i_device )
1269 >= (int)sizeof(dmx) )
1271 msg_Err( p_access, "snprintf() truncated string for DMX" );
1272 dmx[sizeof(dmx) - 1] = '\0';
1275 msg_Dbg( p_access, "Opening device %s", dmx );
1276 if( (*pi_fd = vlc_open(dmx, O_RDWR)) < 0 )
1278 msg_Err( p_access, "DMXSetFilter: opening device failed (%m)" );
1279 return VLC_EGENERIC;
1282 /* We fill the DEMUX structure : */
1283 s_filter_params.pid = i_pid;
1284 s_filter_params.input = DMX_IN_FRONTEND;
1285 s_filter_params.output = DMX_OUT_TS_TAP;
1286 s_filter_params.flags = DMX_IMMEDIATE_START;
1289 { /* First device */
1291 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO0 for PID %d", i_pid);
1292 s_filter_params.pes_type = DMX_PES_VIDEO0;
1295 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO0 for PID %d", i_pid);
1296 s_filter_params.pes_type = DMX_PES_AUDIO0;
1299 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT0 for PID %d", i_pid);
1300 s_filter_params.pes_type = DMX_PES_TELETEXT0;
1303 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE0 for PID %d", i_pid);
1304 s_filter_params.pes_type = DMX_PES_SUBTITLE0;
1307 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR0 for PID %d", i_pid);
1308 s_filter_params.pes_type = DMX_PES_PCR0;
1312 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO1 for PID %d", i_pid);
1313 s_filter_params.pes_type = DMX_PES_VIDEO1;
1316 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO1 for PID %d", i_pid);
1317 s_filter_params.pes_type = DMX_PES_AUDIO1;
1320 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT1 for PID %d", i_pid);
1321 s_filter_params.pes_type = DMX_PES_TELETEXT1;
1324 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE1 for PID %d", i_pid);
1325 s_filter_params.pes_type = DMX_PES_SUBTITLE1;
1328 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR1 for PID %d", i_pid);
1329 s_filter_params.pes_type = DMX_PES_PCR1;
1333 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO2 for PID %d", i_pid);
1334 s_filter_params.pes_type = DMX_PES_VIDEO2;
1337 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO2 for PID %d", i_pid);
1338 s_filter_params.pes_type = DMX_PES_AUDIO2;
1341 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT2 for PID %d", i_pid);
1342 s_filter_params.pes_type = DMX_PES_TELETEXT2;
1345 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE2 for PID %d", i_pid);
1346 s_filter_params.pes_type = DMX_PES_SUBTITLE2;
1349 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR2 for PID %d", i_pid);
1350 s_filter_params.pes_type = DMX_PES_PCR2;
1354 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_VIDEO3 for PID %d", i_pid);
1355 s_filter_params.pes_type = DMX_PES_VIDEO3;
1358 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_AUDIO3 for PID %d", i_pid);
1359 s_filter_params.pes_type = DMX_PES_AUDIO3;
1362 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_TELETEXT3 for PID %d", i_pid);
1363 s_filter_params.pes_type = DMX_PES_TELETEXT3;
1366 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_SUBTITLE3 for PID %d", i_pid);
1367 s_filter_params.pes_type = DMX_PES_SUBTITLE3;
1370 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_PCR3 for PID %d", i_pid);
1371 s_filter_params.pes_type = DMX_PES_PCR3;
1373 /* Usually used by Nova cards */
1376 msg_Dbg(p_access, "DMXSetFilter: DMX_PES_OTHER for PID %d", i_pid);
1377 s_filter_params.pes_type = DMX_PES_OTHER;
1381 /* We then give the order to the device : */
1382 if( ioctl( *pi_fd, DMX_SET_PES_FILTER, &s_filter_params ) )
1384 msg_Err( p_access, "setting demux PES filter failed: %m" );
1385 return VLC_EGENERIC;
1390 /*****************************************************************************
1391 * DMXUnsetFilter : removes a filter
1392 *****************************************************************************/
1393 int DMXUnsetFilter( access_t * p_access, int i_fd )
1395 if( ioctl( i_fd, DMX_STOP ) < 0 )
1397 msg_Err( p_access, "stopping demux failed: %m" );
1398 return VLC_EGENERIC;
1401 msg_Dbg( p_access, "DMXUnsetFilter: closing demux %d", i_fd );
1411 /*****************************************************************************
1413 *****************************************************************************/
1414 int DVROpen( access_t * p_access )
1416 access_sys_t *p_sys = p_access->p_sys;
1417 unsigned int i_adapter, i_device;
1420 i_adapter = var_GetInteger( p_access, "dvb-adapter" );
1421 i_device = var_GetInteger( p_access, "dvb-device" );
1423 if( snprintf( dvr, sizeof(dvr), DVR, i_adapter, i_device )
1424 >= (int)sizeof(dvr) )
1426 msg_Err( p_access, "snprintf() truncated string for DVR" );
1427 dvr[sizeof(dvr) - 1] = '\0';
1430 msg_Dbg( p_access, "Opening device %s", dvr );
1431 if( (p_sys->i_handle = vlc_open(dvr, O_RDONLY)) < 0 )
1433 msg_Err( p_access, "DVROpen: opening device failed (%m)" );
1434 return VLC_EGENERIC;
1437 if( fcntl( p_sys->i_handle, F_SETFL, O_NONBLOCK ) == -1 )
1439 msg_Warn( p_access, "DVROpen: couldn't set non-blocking mode (%m)" );
1445 /*****************************************************************************
1447 *****************************************************************************/
1448 void DVRClose( access_t * p_access )
1450 access_sys_t *p_sys = p_access->p_sys;
1452 close( p_sys->i_handle );
1460 /*****************************************************************************
1462 *****************************************************************************/
1463 int CAMOpen( access_t *p_access )
1465 cam_t *p_cam = &p_access->p_sys->cam;
1467 int i_adapter, i_device;
1470 p_cam->obj = VLC_OBJECT(p_access);
1472 i_adapter = var_GetInteger( p_access, "dvb-adapter" );
1473 i_device = var_GetInteger( p_access, "dvb-device" );
1475 if( snprintf( ca, sizeof(ca), CA, i_adapter, i_device ) >= (int)sizeof(ca) )
1477 msg_Err( p_access, "snprintf() truncated string for CA" );
1478 ca[sizeof(ca) - 1] = '\0';
1480 memset( &caps, 0, sizeof( ca_caps_t ));
1482 msg_Dbg( p_access, "Opening device %s", ca );
1483 p_cam->fd = vlc_open(ca, O_RDWR | O_NONBLOCK);
1484 if( p_cam->fd == -1 )
1486 msg_Warn( p_access, "CAMInit: opening CAM device failed (%m)" );
1487 return VLC_EGENERIC;
1490 if ( ioctl( p_cam->fd, CA_GET_CAP, &caps ) < 0 )
1492 msg_Err( p_access, "CAMInit: ioctl() error getting CAM capabilities" );
1496 /* Output CA capabilities */
1497 msg_Dbg( p_access, "CAMInit: CA interface with %d %s", caps.slot_num,
1498 caps.slot_num == 1 ? "slot" : "slots" );
1499 if ( caps.slot_type & CA_CI )
1500 msg_Dbg( p_access, "CAMInit: CI high level interface type" );
1501 if ( caps.slot_type & CA_CI_LINK )
1502 msg_Dbg( p_access, "CAMInit: CI link layer level interface type" );
1503 if ( caps.slot_type & CA_CI_PHYS )
1504 msg_Dbg( p_access, "CAMInit: CI physical layer level interface type (not supported) " );
1505 if ( caps.slot_type & CA_DESCR )
1506 msg_Dbg( p_access, "CAMInit: built-in descrambler detected" );
1507 if ( caps.slot_type & CA_SC )
1508 msg_Dbg( p_access, "CAMInit: simple smart card interface" );
1510 msg_Dbg( p_access, "CAMInit: %d available %s", caps.descr_num,
1511 caps.descr_num == 1 ? "descrambler (key)" : "descramblers (keys)" );
1512 if ( caps.descr_type & CA_ECD )
1513 msg_Dbg( p_access, "CAMInit: ECD scrambling system supported" );
1514 if ( caps.descr_type & CA_NDS )
1515 msg_Dbg( p_access, "CAMInit: NDS scrambling system supported" );
1516 if ( caps.descr_type & CA_DSS )
1517 msg_Dbg( p_access, "CAMInit: DSS scrambling system supported" );
1519 if ( caps.slot_num == 0 )
1521 msg_Err( p_access, "CAMInit: CAM module with no slots" );
1525 if( caps.slot_type & CA_CI_LINK )
1527 p_cam->i_ca_type = CA_CI_LINK;
1529 else if( caps.slot_type & CA_CI )
1531 p_cam->i_ca_type = CA_CI;
1535 p_cam->i_ca_type = -1;
1536 msg_Err( p_access, "CAMInit: incompatible CAM interface" );
1540 p_cam->i_nb_slots = caps.slot_num;
1541 memset( p_cam->pb_active_slot, 0, sizeof(bool) * MAX_CI_SLOTS );
1542 memset( p_cam->pb_slot_mmi_expected, 0, sizeof(bool) * MAX_CI_SLOTS );
1543 memset( p_cam->pb_slot_mmi_undisplayed, 0,
1544 sizeof(bool) * MAX_CI_SLOTS );
1546 return en50221_Init( p_cam );
1550 return VLC_EGENERIC;
1553 /*****************************************************************************
1555 *****************************************************************************/
1556 int CAMPoll( access_t * p_access )
1558 cam_t *p_cam = &p_access->p_sys->cam;
1560 if ( p_cam->fd == -1 )
1561 return VLC_EGENERIC;
1563 switch( p_cam->i_ca_type )
1566 if ( mdate() > p_cam->i_next_event )
1568 int ret = en50221_Poll( p_cam );
1569 p_cam->i_next_event = mdate() + p_cam->i_timeout;
1575 msg_Err( p_access, "CAMPoll: This should not happen" );
1576 return VLC_EGENERIC;;
1580 /*****************************************************************************
1582 *****************************************************************************/
1583 void CAMStatus( access_t * p_access )
1585 access_sys_t *p_sys = p_access->p_sys;
1586 cam_t *p_cam = &p_sys->cam;
1591 if ( p_sys->psz_request != NULL && *p_sys->psz_request )
1593 /* Check if we have an undisplayed MMI message : in that case we ignore
1594 * the user input to avoid confusing the CAM. */
1595 for ( unsigned i_slot = 0; i_slot < p_cam->i_nb_slots; i_slot++ )
1597 if ( p_cam->pb_slot_mmi_undisplayed[i_slot] == true )
1599 p_sys->psz_request = NULL;
1601 "ignoring user request because of a new MMI object" );
1607 if ( p_sys->psz_request != NULL && *p_sys->psz_request )
1609 /* We have a mission to accomplish. */
1610 en50221_mmi_object_t mmi_object;
1611 char *psz_request = p_sys->psz_request;
1612 char psz_value[255];
1616 p_sys->psz_request = NULL;
1618 if ( HTTPExtractValue( psz_request, "slot", psz_value,
1619 sizeof(psz_value) ) == NULL )
1621 p_sys->psz_mmi_info = strdup( "invalid request parameter\n" );
1624 i_slot = atoi(psz_value);
1626 if ( HTTPExtractValue( psz_request, "open", psz_value,
1627 sizeof(psz_value) ) != NULL )
1629 en50221_OpenMMI( p_cam, i_slot );
1633 if ( HTTPExtractValue( psz_request, "close", psz_value,
1634 sizeof(psz_value) ) != NULL )
1636 en50221_CloseMMI( p_cam, i_slot );
1640 if ( HTTPExtractValue( psz_request, "cancel", psz_value,
1641 sizeof(psz_value) ) == NULL )
1646 if ( HTTPExtractValue( psz_request, "type", psz_value,
1647 sizeof(psz_value) ) == NULL )
1649 p_sys->psz_mmi_info = strdup( "invalid request parameter\n" );
1653 if ( !strcmp( psz_value, "enq" ) )
1655 mmi_object.i_object_type = EN50221_MMI_ANSW;
1656 mmi_object.u.answ.b_ok = b_ok;
1657 if ( b_ok == false )
1659 mmi_object.u.answ.psz_answ = strdup("");
1663 if ( HTTPExtractValue( psz_request, "answ", psz_value,
1664 sizeof(psz_value) ) == NULL )
1666 p_sys->psz_mmi_info = strdup( "invalid request parameter\n" );
1670 mmi_object.u.answ.psz_answ = strdup(psz_value);
1675 mmi_object.i_object_type = EN50221_MMI_MENU_ANSW;
1676 if ( b_ok == false )
1678 mmi_object.u.menu_answ.i_choice = 0;
1682 if ( HTTPExtractValue( psz_request, "choice", psz_value,
1683 sizeof(psz_value) ) == NULL )
1684 mmi_object.u.menu_answ.i_choice = 0;
1686 mmi_object.u.menu_answ.i_choice = atoi(psz_value);
1690 en50221_SendMMIObject( p_cam, i_slot, &mmi_object );
1694 /* Check that we have all necessary MMI information. */
1695 for( unsigned i_slot = 0; i_slot < p_cam->i_nb_slots; i_slot++ )
1697 if ( p_cam->pb_slot_mmi_expected[i_slot] == true )
1701 p = p_sys->psz_mmi_info = malloc( 10000 );
1703 if ( ioctl( p_cam->fd, CA_GET_CAP, &caps ) != 0 )
1706 strerror_r( errno, buf, sizeof( buf ) );
1707 p += sprintf( p, "ioctl CA_GET_CAP failed (%s)\n", buf );
1711 /* Output CA capabilities */
1712 p += sprintf( p, "CA interface with %d %s, type:\n<table>", caps.slot_num,
1713 caps.slot_num == 1 ? "slot" : "slots" );
1714 #define CHECK_CAPS( x, s ) \
1715 if ( caps.slot_type & (CA_##x) ) \
1716 p += sprintf( p, "<tr><td>" s "</td></tr>\n" );
1718 CHECK_CAPS( CI, "CI high level interface" );
1719 CHECK_CAPS( CI_LINK, "CI link layer level interface" );
1720 CHECK_CAPS( CI_PHYS, "CI physical layer level interface (not supported)" );
1721 CHECK_CAPS( DESCR, "built-in descrambler" );
1722 CHECK_CAPS( SC, "simple smartcard interface" );
1725 p += sprintf( p, "</table>%d available %s\n<table>", caps.descr_num,
1726 caps.descr_num == 1 ? "descrambler (key)" : "descramblers (keys)" );
1727 #define CHECK_DESC( x ) \
1728 if ( caps.descr_type & (CA_##x) ) \
1729 p += sprintf( p, "<tr><td>" STRINGIFY(x) "</td></tr>\n" );
1736 p += sprintf( p, "</table>" );
1738 for( unsigned i_slot = 0; i_slot < p_cam->i_nb_slots; i_slot++ )
1740 ca_slot_info_t sinfo;
1742 p_cam->pb_slot_mmi_undisplayed[i_slot] = false;
1743 p += sprintf( p, "<p>CA slot #%d: ", i_slot );
1746 if ( ioctl( p_cam->fd, CA_GET_SLOT_INFO, &sinfo ) != 0 )
1749 strerror_r( errno, buf, sizeof( buf ) );
1750 p += sprintf( p, "ioctl CA_GET_SLOT_INFO failed (%s)<br>\n", buf );
1754 #define CHECK_TYPE( x, s ) \
1755 if ( sinfo.type & (CA_##x) ) \
1756 p += sprintf( p, "%s", s );
1758 CHECK_TYPE( CI, "high level, " );
1759 CHECK_TYPE( CI_LINK, "link layer level, " );
1760 CHECK_TYPE( CI_PHYS, "physical layer level, " );
1763 if ( sinfo.flags & CA_CI_MODULE_READY )
1765 en50221_mmi_object_t *p_object = en50221_GetMMIObject( p_cam,
1768 p += sprintf( p, "module present and ready<p>\n" );
1769 p += sprintf( p, "<form action=index.html method=get>\n" );
1770 p += sprintf( p, "<input type=hidden name=slot value=\"%d\">\n",
1773 if ( p_object == NULL )
1775 p += sprintf( p, "<input type=submit name=open value=\"Open session\">\n" );
1779 switch ( p_object->i_object_type )
1781 case EN50221_MMI_ENQ:
1782 p += sprintf( p, "<input type=hidden name=type value=enq>\n" );
1783 p += sprintf( p, "<table border=1><tr><th>%s</th></tr>\n",
1784 p_object->u.enq.psz_text );
1785 if ( p_object->u.enq.b_blind == false )
1786 p += sprintf( p, "<tr><td><input type=text name=answ></td></tr>\n" );
1788 p += sprintf( p, "<tr><td><input type=password name=answ></td></tr>\n" );
1791 case EN50221_MMI_MENU:
1792 p += sprintf( p, "<input type=hidden name=type value=menu>\n" );
1793 p += sprintf( p, "<table border=1><tr><th>%s</th></tr>\n",
1794 p_object->u.menu.psz_title );
1795 p += sprintf( p, "<tr><td>%s</td></tr><tr><td>\n",
1796 p_object->u.menu.psz_subtitle );
1797 for ( i = 0; i < p_object->u.menu.i_choices; i++ )
1798 p += sprintf( p, "<input type=radio name=choice value=\"%d\">%s<br>\n", i + 1, p_object->u.menu.ppsz_choices[i] );
1799 p += sprintf( p, "</td></tr><tr><td>%s</td></tr>\n",
1800 p_object->u.menu.psz_bottom );
1803 case EN50221_MMI_LIST:
1804 p += sprintf( p, "<input type=hidden name=type value=menu>\n" );
1805 p += sprintf( p, "<input type=hidden name=choice value=0>\n" );
1806 p += sprintf( p, "<table border=1><tr><th>%s</th></tr>\n",
1807 p_object->u.menu.psz_title );
1808 p += sprintf( p, "<tr><td>%s</td></tr><tr><td>\n",
1809 p_object->u.menu.psz_subtitle );
1810 for ( i = 0; i < p_object->u.menu.i_choices; i++ )
1811 p += sprintf( p, "%s<br>\n",
1812 p_object->u.menu.ppsz_choices[i] );
1813 p += sprintf( p, "</td></tr><tr><td>%s</td></tr>\n",
1814 p_object->u.menu.psz_bottom );
1818 p += sprintf( p, "<table><tr><th>Unknown MMI object type</th></tr>\n" );
1821 p += sprintf( p, "</table><p><input type=submit name=ok value=\"OK\">\n" );
1822 p += sprintf( p, "<input type=submit name=cancel value=\"Cancel\">\n" );
1823 p += sprintf( p, "<input type=submit name=close value=\"Close Session\">\n" );
1825 p += sprintf( p, "</form>\n" );
1827 else if ( sinfo.flags & CA_CI_MODULE_PRESENT )
1828 p += sprintf( p, "module present, not ready<br>\n" );
1830 p += sprintf( p, "module not present<br>\n" );
1834 vlc_mutex_lock( &p_sys->httpd_mutex );
1835 p_sys->b_request_mmi_info = false;
1836 vlc_cond_signal( &p_sys->httpd_cond );
1837 vlc_mutex_unlock( &p_sys->httpd_mutex );
1841 /*****************************************************************************
1843 *****************************************************************************/
1844 int CAMSet( access_t * p_access, dvbpsi_pmt_t *p_pmt )
1846 cam_t *p_cam = &p_access->p_sys->cam;
1848 if( p_cam->fd == -1 )
1850 dvbpsi_DeletePMT( p_pmt );
1851 return VLC_EGENERIC;
1854 en50221_SetCAPMT( p_cam, p_pmt );
1859 /*****************************************************************************
1861 *****************************************************************************/
1862 void CAMClose( access_t * p_access )
1864 cam_t *p_cam = &p_access->p_sys->cam;
1866 if ( p_cam->fd == -1 )
1869 en50221_End( p_cam );